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