2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/config.h"
7 #include "linux/kernel.h"
8 #include "linux/sched.h"
9 #include "linux/interrupt.h"
11 #include "linux/slab.h"
12 #include "linux/utsname.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"
24 #include "asm/segment.h"
26 #include "asm/pgtable.h"
27 #include "asm/processor.h"
28 #include "asm/tlbflush.h"
29 #include "asm/uaccess.h"
31 #include "user_util.h"
32 #include "kern_util.h"
34 #include "signal_kern.h"
35 #include "signal_user.h"
39 #include "time_user.h"
41 #include "frame_kern.h"
42 #include "sigcontext.h"
43 #include "2_5compat.h"
46 #include "mode_kern.h"
47 #include "choose-mode.h"
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
53 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
55 struct task_struct *get_task(int pid, int require)
57 struct task_struct *ret;
59 read_lock(&tasklist_lock);
60 ret = find_task_by_pid(pid);
61 read_unlock(&tasklist_lock);
63 if(require && (ret == NULL)) panic("get_task couldn't find a task\n");
67 int external_pid(void *t)
69 struct task_struct *task = t ? t : current;
71 return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
74 int pid_to_processor_id(int pid)
78 for(i = 0; i < ncpus; i++){
79 if(cpu_tasks[i].pid == pid) return(i);
84 void free_stack(unsigned long stack, int order)
86 free_pages(stack, order);
89 unsigned long alloc_stack(int order, int atomic)
92 int flags = GFP_KERNEL;
94 if(atomic) flags |= GFP_ATOMIC;
95 page = __get_free_pages(flags, order);
98 stack_protections(page);
102 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
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,
111 panic("do_fork failed in kernel_thread, errno = %d", pid);
115 void switch_mm(struct mm_struct *prev, struct mm_struct *next,
116 struct task_struct *tsk)
118 int cpu = smp_processor_id();
121 cpu_clear(cpu, prev->cpu_vm_mask);
122 cpu_set(cpu, next->cpu_vm_mask);
125 void set_current(void *t)
127 struct task_struct *task = t;
129 cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
130 { external_pid(task), task });
133 void *_switch_to(void *prev, void *next, void *last)
135 return(CHOOSE_MODE(switch_to_tt(prev, next),
136 switch_to_skas(prev, next)));
139 void interrupt_end(void)
141 if(need_resched()) schedule();
142 if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal(0);
145 void release_thread(struct task_struct *task)
147 CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
150 void exit_thread(void)
152 CHOOSE_MODE(exit_thread_tt(), exit_thread_skas());
153 unprotect_stack((unsigned long) current_thread);
156 void *get_current(void)
161 void prepare_to_copy(struct task_struct *tsk)
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)
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));
174 void initial_thread_cb(void (*proc)(void *), void *arg)
176 int save_kmalloc_ok = kmalloc_ok;
179 CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
181 kmalloc_ok = save_kmalloc_ok;
184 unsigned long stack_sp(unsigned long page)
186 return(page + PAGE_SIZE - sizeof(void *));
189 int current_pid(void)
191 return(current->pid);
194 void default_idle(void)
198 atomic_inc(&init_mm.mm_count);
199 current->mm = &init_mm;
200 current->active_mm = &init_mm;
203 /* endless idle loop with no priority at all */
207 * although we are an idle CPU, we do not want to
208 * get into the scheduler unnecessarily.
210 irq_stat[smp_processor_id()].idle_timestamp = jiffies;
220 CHOOSE_MODE(init_idle_tt(), init_idle_skas());
233 void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
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));
251 return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
254 char *current_cmd(void)
256 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
259 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
260 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
264 void force_sigbus(void)
266 printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
269 sigaddset(¤t->pending.signal, SIGBUS);
271 current->flags |= PF_SIGNALED;
272 do_exit(SIGBUS | 0x80);
275 void dump_thread(struct pt_regs *regs, struct user *u)
279 void enable_hlt(void)
284 EXPORT_SYMBOL(enable_hlt);
286 void disable_hlt(void)
288 panic("disable_hlt");
291 EXPORT_SYMBOL(disable_hlt);
293 extern int signal_frame_size;
295 void *um_kmalloc(int size)
297 return(kmalloc(size, GFP_KERNEL));
300 void *um_kmalloc_atomic(int size)
302 return(kmalloc(size, GFP_ATOMIC));
305 void *um_vmalloc(int size)
307 return(vmalloc(size));
310 unsigned long get_fault_addr(void)
312 return((unsigned long) current->thread.fault_addr);
315 EXPORT_SYMBOL(get_fault_addr);
317 void not_implemented(void)
319 printk(KERN_DEBUG "Something isn't implemented in here\n");
322 EXPORT_SYMBOL(not_implemented);
324 int user_context(unsigned long sp)
328 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
329 return(stack != (unsigned long) current_thread);
332 extern void remove_umid_dir(void);
334 __uml_exitcall(remove_umid_dir);
336 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
338 void do_uml_exitcalls(void)
342 call = &__uml_exitcall_end;
343 while (--call >= &__uml_exitcall_begin)
347 char *uml_strdup(char *string)
351 new = kmalloc(strlen(string) + 1, GFP_KERNEL);
352 if(new == NULL) return(NULL);
357 void *get_init_task(void)
359 return(&init_thread_union.thread_info.task);
362 int copy_to_user_proc(void *to, void *from, int size)
364 return(copy_to_user(to, from, size));
367 int copy_from_user_proc(void *to, void *from, int size)
369 return(copy_from_user(to, from, size));
372 int clear_user_proc(void *buf, int size)
374 return(clear_user(buf, size));
377 int strlen_user_proc(char *str)
379 return(strlen_user(str));
382 int smp_sigio_handler(void)
385 int cpu = current_thread->cpu;
393 int um_in_interrupt(void)
395 return(in_interrupt());
400 return(current_thread->cpu);
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
408 * ---------------------------------------------------------------------------
410 * c-file-style: "linux"