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