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