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