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