vserver 2.0 rc7
[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/random.h"
25 #include "linux/vs_cvirt.h"
26
27 #include "asm/unistd.h"
28 #include "asm/mman.h"
29 #include "asm/segment.h"
30 #include "asm/stat.h"
31 #include "asm/pgtable.h"
32 #include "asm/processor.h"
33 #include "asm/tlbflush.h"
34 #include "asm/uaccess.h"
35 #include "asm/user.h"
36 #include "user_util.h"
37 #include "kern_util.h"
38 #include "kern.h"
39 #include "signal_kern.h"
40 #include "signal_user.h"
41 #include "init.h"
42 #include "irq_user.h"
43 #include "mem_user.h"
44 #include "time_user.h"
45 #include "tlb.h"
46 #include "frame_kern.h"
47 #include "sigcontext.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 int external_pid(void *t)
60 {
61         struct task_struct *task = t ? t : current;
62
63         return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
64 }
65
66 int pid_to_processor_id(int pid)
67 {
68         int i;
69
70         for(i = 0; i < ncpus; i++){
71                 if(cpu_tasks[i].pid == pid) return(i);
72         }
73         return(-1);
74 }
75
76 void free_stack(unsigned long stack, int order)
77 {
78         free_pages(stack, order);
79 }
80
81 unsigned long alloc_stack(int order, int atomic)
82 {
83         unsigned long page;
84         int flags = GFP_KERNEL;
85
86         if(atomic) flags |= GFP_ATOMIC;
87         page = __get_free_pages(flags, order);
88         if(page == 0)
89                 return(0);
90         stack_protections(page);
91         return(page);
92 }
93
94 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
95 {
96         int pid;
97
98         current->thread.request.u.thread.proc = fn;
99         current->thread.request.u.thread.arg = arg;
100         pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0, NULL, 0, NULL,
101                       NULL);
102         if(pid < 0)
103                 panic("do_fork failed in kernel_thread, errno = %d", pid);
104         return(pid);
105 }
106
107 void set_current(void *t)
108 {
109         struct task_struct *task = t;
110
111         cpu_tasks[task->thread_info->cpu] = ((struct cpu_task) 
112                 { external_pid(task), task });
113 }
114
115 void *_switch_to(void *prev, void *next, void *last)
116 {
117         return(CHOOSE_MODE(switch_to_tt(prev, next), 
118                            switch_to_skas(prev, next)));
119 }
120
121 void interrupt_end(void)
122 {
123         if(need_resched()) schedule();
124         if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
125 }
126
127 void release_thread(struct task_struct *task)
128 {
129         CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
130 }
131  
132 void exit_thread(void)
133 {
134         unprotect_stack((unsigned long) current_thread);
135 }
136  
137 void *get_current(void)
138 {
139         return(current);
140 }
141
142 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
143                 unsigned long stack_top, struct task_struct * p, 
144                 struct pt_regs *regs)
145 {
146         p->thread = (struct thread_struct) INIT_THREAD;
147         return(CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr, 
148                                 clone_flags, sp, stack_top, p, regs));
149 }
150
151 void initial_thread_cb(void (*proc)(void *), void *arg)
152 {
153         int save_kmalloc_ok = kmalloc_ok;
154
155         kmalloc_ok = 0;
156         CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc, 
157                          arg);
158         kmalloc_ok = save_kmalloc_ok;
159 }
160  
161 unsigned long stack_sp(unsigned long page)
162 {
163         return(page + PAGE_SIZE - sizeof(void *));
164 }
165
166 int current_pid(void)
167 {
168         return(current->pid);
169 }
170
171 void default_idle(void)
172 {
173         uml_idle_timer();
174
175         atomic_inc(&init_mm.mm_count);
176         current->mm = &init_mm;
177         current->active_mm = &init_mm;
178
179         while(1){
180                 /* endless idle loop with no priority at all */
181
182                 /*
183                  * although we are an idle CPU, we do not want to
184                  * get into the scheduler unnecessarily.
185                  */
186                 if(need_resched())
187                         schedule();
188                 
189                 idle_sleep(10);
190         }
191 }
192
193 void cpu_idle(void)
194 {
195         CHOOSE_MODE(init_idle_tt(), init_idle_skas());
196 }
197
198 int page_size(void)
199 {
200         return(PAGE_SIZE);
201 }
202
203 void *um_virt_to_phys(struct task_struct *task, unsigned long addr, 
204                       pte_t *pte_out)
205 {
206         pgd_t *pgd;
207         pud_t *pud;
208         pmd_t *pmd;
209         pte_t *pte;
210
211         if(task->mm == NULL) 
212                 return(ERR_PTR(-EINVAL));
213         pgd = pgd_offset(task->mm, addr);
214         if(!pgd_present(*pgd))
215                 return(ERR_PTR(-EINVAL));
216
217         pud = pud_offset(pgd, addr);
218         if(!pud_present(*pud))
219                 return(ERR_PTR(-EINVAL));
220
221         pmd = pmd_offset(pud, addr);
222         if(!pmd_present(*pmd)) 
223                 return(ERR_PTR(-EINVAL));
224
225         pte = pte_offset_kernel(pmd, addr);
226         if(!pte_present(*pte)) 
227                 return(ERR_PTR(-EINVAL));
228
229         if(pte_out != NULL)
230                 *pte_out = *pte;
231         return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
232 }
233
234 char *current_cmd(void)
235 {
236 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
237         return("(Unknown)");
238 #else
239         void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
240         return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
241 #endif
242 }
243
244 void force_sigbus(void)
245 {
246         printk(KERN_ERR "Killing pid %d because of a lack of memory\n", 
247                current->pid);
248         lock_kernel();
249         sigaddset(&current->pending.signal, SIGBUS);
250         recalc_sigpending();
251         current->flags |= PF_SIGNALED;
252         do_exit(SIGBUS | 0x80);
253 }
254
255 void dump_thread(struct pt_regs *regs, struct user *u)
256 {
257 }
258
259 void enable_hlt(void)
260 {
261         panic("enable_hlt");
262 }
263
264 EXPORT_SYMBOL(enable_hlt);
265
266 void disable_hlt(void)
267 {
268         panic("disable_hlt");
269 }
270
271 EXPORT_SYMBOL(disable_hlt);
272
273 void *um_kmalloc(int size)
274 {
275         return(kmalloc(size, GFP_KERNEL));
276 }
277
278 void *um_kmalloc_atomic(int size)
279 {
280         return(kmalloc(size, GFP_ATOMIC));
281 }
282
283 void *um_vmalloc(int size)
284 {
285         return(vmalloc(size));
286 }
287
288 unsigned long get_fault_addr(void)
289 {
290         return((unsigned long) current->thread.fault_addr);
291 }
292
293 EXPORT_SYMBOL(get_fault_addr);
294
295 void not_implemented(void)
296 {
297         printk(KERN_DEBUG "Something isn't implemented in here\n");
298 }
299
300 EXPORT_SYMBOL(not_implemented);
301
302 int user_context(unsigned long sp)
303 {
304         unsigned long stack;
305
306         stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
307         return(stack != (unsigned long) current_thread);
308 }
309
310 extern void remove_umid_dir(void);
311
312 __uml_exitcall(remove_umid_dir);
313
314 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
315
316 void do_uml_exitcalls(void)
317 {
318         exitcall_t *call;
319
320         call = &__uml_exitcall_end;
321         while (--call >= &__uml_exitcall_begin)
322                 (*call)();
323 }
324
325 char *uml_strdup(char *string)
326 {
327         char *new;
328
329         new = kmalloc(strlen(string) + 1, GFP_KERNEL);
330         if(new == NULL) return(NULL);
331         strcpy(new, string);
332         return(new);
333 }
334
335 int copy_to_user_proc(void __user *to, void *from, int size)
336 {
337         return(copy_to_user(to, from, size));
338 }
339
340 int copy_from_user_proc(void *to, void __user *from, int size)
341 {
342         return(copy_from_user(to, from, size));
343 }
344
345 int clear_user_proc(void __user *buf, int size)
346 {
347         return(clear_user(buf, size));
348 }
349
350 int strlen_user_proc(char __user *str)
351 {
352         return(strlen_user(str));
353 }
354
355 int smp_sigio_handler(void)
356 {
357 #ifdef CONFIG_SMP
358         int cpu = current_thread->cpu;
359         IPI_handler(cpu);
360         if(cpu != 0)
361                 return(1);
362 #endif
363         return(0);
364 }
365
366 int um_in_interrupt(void)
367 {
368         return(in_interrupt());
369 }
370
371 int cpu(void)
372 {
373         return(current_thread->cpu);
374 }
375
376 static atomic_t using_sysemu = ATOMIC_INIT(0);
377 int sysemu_supported;
378
379 void set_using_sysemu(int value)
380 {
381         if (value > sysemu_supported)
382                 return;
383         atomic_set(&using_sysemu, value);
384 }
385
386 int get_using_sysemu(void)
387 {
388         return atomic_read(&using_sysemu);
389 }
390
391 static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
392 {
393         if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
394                 *eof = 1;
395
396         return strlen(buf);
397 }
398
399 static int proc_write_sysemu(struct file *file,const char *buf, unsigned long count,void *data)
400 {
401         char tmp[2];
402
403         if (copy_from_user(tmp, buf, 1))
404                 return -EFAULT;
405
406         if (tmp[0] >= '0' && tmp[0] <= '2')
407                 set_using_sysemu(tmp[0] - '0');
408         return count; /*We use the first char, but pretend to write everything*/
409 }
410
411 int __init make_proc_sysemu(void)
412 {
413         struct proc_dir_entry *ent;
414         if (!sysemu_supported)
415                 return 0;
416
417         ent = create_proc_entry("sysemu", 0600, &proc_root);
418
419         if (ent == NULL)
420         {
421                 printk("Failed to register /proc/sysemu\n");
422                 return(0);
423         }
424
425         ent->read_proc  = proc_read_sysemu;
426         ent->write_proc = proc_write_sysemu;
427
428         return 0;
429 }
430
431 late_initcall(make_proc_sysemu);
432
433 int singlestepping(void * t)
434 {
435         struct task_struct *task = t ? t : current;
436
437         if ( ! (task->ptrace & PT_DTRACE) )
438                 return(0);
439
440         if (task->thread.singlestep_syscall)
441                 return(1);
442
443         return 2;
444 }
445
446 /*
447  * Only x86 and x86_64 have an arch_align_stack().
448  * All other arches have "#define arch_align_stack(x) (x)"
449  * in their asm/system.h
450  * As this is included in UML from asm-um/system-generic.h,
451  * we can use it to behave as the subarch does.
452  */
453 #ifndef arch_align_stack
454 unsigned long arch_align_stack(unsigned long sp)
455 {
456         if (randomize_va_space)
457                 sp -= get_random_int() % 8192;
458         return sp & ~0xf;
459 }
460 #endif