This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / arch / um / kernel / process_kern.c
1 /* 
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/config.h"
7 #include "linux/kernel.h"
8 #include "linux/sched.h"
9 #include "linux/interrupt.h"
10 #include "linux/mm.h"
11 #include "linux/slab.h"
12 #include "linux/utsname.h"
13 #include "linux/fs.h"
14 #include "linux/utime.h"
15 #include "linux/smp_lock.h"
16 #include "linux/module.h"
17 #include "linux/init.h"
18 #include "linux/capability.h"
19 #include "linux/vmalloc.h"
20 #include "linux/spinlock.h"
21 #include "asm/unistd.h"
22 #include "asm/mman.h"
23 #include "asm/segment.h"
24 #include "asm/stat.h"
25 #include "asm/pgtable.h"
26 #include "asm/processor.h"
27 #include "asm/tlbflush.h"
28 #include "asm/uaccess.h"
29 #include "asm/user.h"
30 #include "user_util.h"
31 #include "kern_util.h"
32 #include "kern.h"
33 #include "signal_kern.h"
34 #include "signal_user.h"
35 #include "init.h"
36 #include "irq_user.h"
37 #include "mem_user.h"
38 #include "time_user.h"
39 #include "tlb.h"
40 #include "frame_kern.h"
41 #include "sigcontext.h"
42 #include "2_5compat.h"
43 #include "os.h"
44 #include "mode.h"
45 #include "mode_kern.h"
46 #include "choose-mode.h"
47
48 /* This is a per-cpu array.  A processor only modifies its entry and it only
49  * cares about its entry, so it's OK if another processor is modifying its
50  * entry.
51  */
52 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
53
54 struct task_struct *get_task(int pid, int require)
55 {
56         struct task_struct *ret;
57
58         read_lock(&tasklist_lock);
59         ret = find_task_by_pid(pid);
60         read_unlock(&tasklist_lock);
61
62         if(require && (ret == NULL)) panic("get_task couldn't find a task\n");
63         return(ret);
64 }
65
66 int external_pid(void *t)
67 {
68         struct task_struct *task = t ? t : current;
69
70         return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
71 }
72
73 int pid_to_processor_id(int pid)
74 {
75         int i;
76
77         for(i = 0; i < ncpus; i++){
78                 if(cpu_tasks[i].pid == pid) return(i);
79         }
80         return(-1);
81 }
82
83 void free_stack(unsigned long stack, int order)
84 {
85         free_pages(stack, order);
86 }
87
88 unsigned long alloc_stack(int order, int atomic)
89 {
90         unsigned long page;
91         int flags = GFP_KERNEL;
92
93         if(atomic) flags |= GFP_ATOMIC;
94         page = __get_free_pages(flags, order);
95         if(page == 0)
96                 return(0);
97         stack_protections(page);
98         return(page);
99 }
100
101 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
102 {
103         int pid;
104
105         current->thread.request.u.thread.proc = fn;
106         current->thread.request.u.thread.arg = arg;
107         pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0, NULL, 0, NULL, 
108                       NULL);
109         if(pid < 0)
110                 panic("do_fork failed in kernel_thread, errno = %d", pid);
111         return(pid);
112 }
113
114 void switch_mm(struct mm_struct *prev, struct mm_struct *next, 
115                struct task_struct *tsk)
116 {
117         unsigned cpu = smp_processor_id();
118         if (prev != next) 
119                 clear_bit(cpu, &prev->cpu_vm_mask);
120         set_bit(cpu, &next->cpu_vm_mask);
121 }
122
123 void set_current(void *t)
124 {
125         struct task_struct *task = t;
126
127         cpu_tasks[task->thread_info->cpu] = ((struct cpu_task) 
128                 { external_pid(task), task });
129 }
130
131 void *_switch_to(void *prev, void *next, void *last)
132 {
133         return(CHOOSE_MODE(switch_to_tt(prev, next), 
134                            switch_to_skas(prev, next)));
135 }
136
137 void interrupt_end(void)
138 {
139         if(need_resched()) schedule();
140         if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal(0);
141 }
142
143 void release_thread(struct task_struct *task)
144 {
145         CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
146 }
147  
148 void exit_thread(void)
149 {
150         CHOOSE_MODE(exit_thread_tt(), exit_thread_skas());
151         unprotect_stack((unsigned long) current_thread);
152 }
153  
154 void *get_current(void)
155 {
156         return(current);
157 }
158
159 void prepare_to_copy(struct task_struct *tsk)
160 {
161 }
162
163 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
164                 unsigned long stack_top, struct task_struct * p, 
165                 struct pt_regs *regs)
166 {
167         p->thread = (struct thread_struct) INIT_THREAD;
168         return(CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr, 
169                                 clone_flags, sp, stack_top, p, regs));
170 }
171
172 void initial_thread_cb(void (*proc)(void *), void *arg)
173 {
174         int save_kmalloc_ok = kmalloc_ok;
175
176         kmalloc_ok = 0;
177         CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc, 
178                          arg);
179         kmalloc_ok = save_kmalloc_ok;
180 }
181  
182 unsigned long stack_sp(unsigned long page)
183 {
184         return(page + PAGE_SIZE - sizeof(void *));
185 }
186
187 int current_pid(void)
188 {
189         return(current->pid);
190 }
191
192 void default_idle(void)
193 {
194         uml_idle_timer();
195
196         atomic_inc(&init_mm.mm_count);
197         current->mm = &init_mm;
198         current->active_mm = &init_mm;
199
200         while(1){
201                 /* endless idle loop with no priority at all */
202                 SET_PRI(current);
203
204                 /*
205                  * although we are an idle CPU, we do not want to
206                  * get into the scheduler unnecessarily.
207                  */
208                 irq_stat[smp_processor_id()].idle_timestamp = jiffies;
209                 if(need_resched())
210                         schedule();
211                 
212                 idle_sleep(10);
213         }
214 }
215
216 void cpu_idle(void)
217 {
218         CHOOSE_MODE(init_idle_tt(), init_idle_skas());
219 }
220
221 int page_size(void)
222 {
223         return(PAGE_SIZE);
224 }
225
226 int page_mask(void)
227 {
228         return(PAGE_MASK);
229 }
230
231 void *um_virt_to_phys(struct task_struct *task, unsigned long addr, 
232                       pte_t *pte_out)
233 {
234         pgd_t *pgd;
235         pmd_t *pmd;
236         pte_t *pte;
237
238         if(task->mm == NULL) 
239                 return(ERR_PTR(-EINVAL));
240         pgd = pgd_offset(task->mm, addr);
241         pmd = pmd_offset(pgd, addr);
242         if(!pmd_present(*pmd)) 
243                 return(ERR_PTR(-EINVAL));
244         pte = pte_offset_kernel(pmd, addr);
245         if(!pte_present(*pte)) 
246                 return(ERR_PTR(-EINVAL));
247         if(pte_out != NULL)
248                 *pte_out = *pte;
249         return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
250 }
251
252 char *current_cmd(void)
253 {
254 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
255         return("(Unknown)");
256 #else
257         void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
258         return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
259 #endif
260 }
261
262 void force_sigbus(void)
263 {
264         printk(KERN_ERR "Killing pid %d because of a lack of memory\n", 
265                current->pid);
266         lock_kernel();
267         sigaddset(&current->pending.signal, SIGBUS);
268         recalc_sigpending();
269         current->flags |= PF_SIGNALED;
270         do_exit(SIGBUS | 0x80);
271 }
272
273 void dump_thread(struct pt_regs *regs, struct user *u)
274 {
275 }
276
277 void enable_hlt(void)
278 {
279         panic("enable_hlt");
280 }
281
282 EXPORT_SYMBOL(enable_hlt);
283
284 void disable_hlt(void)
285 {
286         panic("disable_hlt");
287 }
288
289 EXPORT_SYMBOL(disable_hlt);
290
291 extern int signal_frame_size;
292
293 void *um_kmalloc(int size)
294 {
295         return(kmalloc(size, GFP_KERNEL));
296 }
297
298 void *um_kmalloc_atomic(int size)
299 {
300         return(kmalloc(size, GFP_ATOMIC));
301 }
302
303 void *um_vmalloc(int size)
304 {
305         return(vmalloc(size));
306 }
307
308 unsigned long get_fault_addr(void)
309 {
310         return((unsigned long) current->thread.fault_addr);
311 }
312
313 EXPORT_SYMBOL(get_fault_addr);
314
315 void not_implemented(void)
316 {
317         printk(KERN_DEBUG "Something isn't implemented in here\n");
318 }
319
320 EXPORT_SYMBOL(not_implemented);
321
322 int user_context(unsigned long sp)
323 {
324         unsigned long stack;
325
326         stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
327         return(stack != (unsigned long) current_thread);
328 }
329
330 extern void remove_umid_dir(void);
331
332 __uml_exitcall(remove_umid_dir);
333
334 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
335
336 void do_uml_exitcalls(void)
337 {
338         exitcall_t *call;
339
340         call = &__uml_exitcall_end;
341         while (--call >= &__uml_exitcall_begin)
342                 (*call)();
343 }
344
345 char *uml_strdup(char *string)
346 {
347         char *new;
348
349         new = kmalloc(strlen(string) + 1, GFP_KERNEL);
350         if(new == NULL) return(NULL);
351         strcpy(new, string);
352         return(new);
353 }
354
355 void *get_init_task(void)
356 {
357         return(&init_thread_union.thread_info.task);
358 }
359
360 int copy_to_user_proc(void *to, void *from, int size)
361 {
362         return(copy_to_user(to, from, size));
363 }
364
365 int copy_from_user_proc(void *to, void *from, int size)
366 {
367         return(copy_from_user(to, from, size));
368 }
369
370 int clear_user_proc(void *buf, int size)
371 {
372         return(clear_user(buf, size));
373 }
374
375 int strlen_user_proc(char *str)
376 {
377         return(strlen_user(str));
378 }
379
380 int smp_sigio_handler(void)
381 {
382 #ifdef CONFIG_SMP
383         int cpu = current_thread->cpu;
384         IPI_handler(cpu);
385         if(cpu != 0)
386                 return(1);
387 #endif
388         return(0);
389 }
390
391 int um_in_interrupt(void)
392 {
393         return(in_interrupt());
394 }
395
396 int cpu(void)
397 {
398         return(current_thread->cpu);
399 }
400
401 /*
402  * Overrides for Emacs so that we follow Linus's tabbing style.
403  * Emacs will notice this stuff at the end of the file and automatically
404  * adjust the settings for this buffer only.  This must remain at the end
405  * of the file.
406  * ---------------------------------------------------------------------------
407  * Local variables:
408  * c-file-style: "linux"
409  * End:
410  */