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