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