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