2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Copyright 2003 PathScale, Inc.
4 * Licensed under the GPL
7 #include "linux/config.h"
8 #include "linux/kernel.h"
9 #include "linux/sched.h"
10 #include "linux/interrupt.h"
12 #include "linux/slab.h"
13 #include "linux/utsname.h"
15 #include "linux/utime.h"
16 #include "linux/smp_lock.h"
17 #include "linux/module.h"
18 #include "linux/init.h"
19 #include "linux/capability.h"
20 #include "linux/vmalloc.h"
21 #include "linux/spinlock.h"
22 #include "linux/proc_fs.h"
23 #include "linux/ptrace.h"
24 #include "linux/vs_cvirt.h"
26 #include "asm/unistd.h"
28 #include "asm/segment.h"
30 #include "asm/pgtable.h"
31 #include "asm/processor.h"
32 #include "asm/tlbflush.h"
33 #include "asm/uaccess.h"
35 #include "user_util.h"
36 #include "kern_util.h"
38 #include "signal_kern.h"
39 #include "signal_user.h"
43 #include "time_user.h"
45 #include "frame_kern.h"
46 #include "sigcontext.h"
47 #include "2_5compat.h"
50 #include "mode_kern.h"
51 #include "choose-mode.h"
53 /* This is a per-cpu array. A processor only modifies its entry and it only
54 * cares about its entry, so it's OK if another processor is modifying its
57 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
59 struct task_struct *get_task(int pid, int require)
61 struct task_struct *ret;
63 read_lock(&tasklist_lock);
64 ret = find_task_by_pid(pid);
65 read_unlock(&tasklist_lock);
67 if(require && (ret == NULL)) panic("get_task couldn't find a task\n");
71 int external_pid(void *t)
73 struct task_struct *task = t ? t : current;
75 return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
78 int pid_to_processor_id(int pid)
82 for(i = 0; i < ncpus; i++){
83 if(cpu_tasks[i].pid == pid) return(i);
88 void free_stack(unsigned long stack, int order)
90 free_pages(stack, order);
93 unsigned long alloc_stack(int order, int atomic)
96 int flags = GFP_KERNEL;
98 if(atomic) flags |= GFP_ATOMIC;
99 page = __get_free_pages(flags, order);
102 stack_protections(page);
106 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
110 current->thread.request.u.thread.proc = fn;
111 current->thread.request.u.thread.arg = arg;
112 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0, NULL, 0, NULL,
115 panic("do_fork failed in kernel_thread, errno = %d", pid);
119 void switch_mm(struct mm_struct *prev, struct mm_struct *next,
120 struct task_struct *tsk)
122 int cpu = smp_processor_id();
125 cpu_clear(cpu, prev->cpu_vm_mask);
126 cpu_set(cpu, next->cpu_vm_mask);
129 void set_current(void *t)
131 struct task_struct *task = t;
133 cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
134 { external_pid(task), task });
137 void *_switch_to(void *prev, void *next, void *last)
139 return(CHOOSE_MODE(switch_to_tt(prev, next),
140 switch_to_skas(prev, next)));
143 void interrupt_end(void)
145 if(need_resched()) schedule();
146 if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
149 void release_thread(struct task_struct *task)
151 CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
154 void exit_thread(void)
156 CHOOSE_MODE(exit_thread_tt(), exit_thread_skas());
157 unprotect_stack((unsigned long) current_thread);
160 void *get_current(void)
165 void prepare_to_copy(struct task_struct *tsk)
169 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
170 unsigned long stack_top, struct task_struct * p,
171 struct pt_regs *regs)
173 p->thread = (struct thread_struct) INIT_THREAD;
174 return(CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr,
175 clone_flags, sp, stack_top, p, regs));
178 void initial_thread_cb(void (*proc)(void *), void *arg)
180 int save_kmalloc_ok = kmalloc_ok;
183 CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
185 kmalloc_ok = save_kmalloc_ok;
188 unsigned long stack_sp(unsigned long page)
190 return(page + PAGE_SIZE - sizeof(void *));
193 int current_pid(void)
195 return(current->pid);
198 void default_idle(void)
202 atomic_inc(&init_mm.mm_count);
203 current->mm = &init_mm;
204 current->active_mm = &init_mm;
207 /* endless idle loop with no priority at all */
211 * although we are an idle CPU, we do not want to
212 * get into the scheduler unnecessarily.
223 CHOOSE_MODE(init_idle_tt(), init_idle_skas());
231 unsigned long page_mask(void)
236 void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
245 return(ERR_PTR(-EINVAL));
246 pgd = pgd_offset(task->mm, addr);
247 if(!pgd_present(*pgd))
248 return(ERR_PTR(-EINVAL));
250 pud = pud_offset(pgd, addr);
251 if(!pud_present(*pud))
252 return(ERR_PTR(-EINVAL));
254 pmd = pmd_offset(pud, addr);
255 if(!pmd_present(*pmd))
256 return(ERR_PTR(-EINVAL));
258 pte = pte_offset_kernel(pmd, addr);
259 if(!pte_present(*pte))
260 return(ERR_PTR(-EINVAL));
264 return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
267 char *current_cmd(void)
269 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
272 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
273 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
277 void force_sigbus(void)
279 printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
282 sigaddset(¤t->pending.signal, SIGBUS);
284 current->flags |= PF_SIGNALED;
285 do_exit(SIGBUS | 0x80);
288 void dump_thread(struct pt_regs *regs, struct user *u)
292 void enable_hlt(void)
297 EXPORT_SYMBOL(enable_hlt);
299 void disable_hlt(void)
301 panic("disable_hlt");
304 EXPORT_SYMBOL(disable_hlt);
306 void *um_kmalloc(int size)
308 return(kmalloc(size, GFP_KERNEL));
311 void *um_kmalloc_atomic(int size)
313 return(kmalloc(size, GFP_ATOMIC));
316 void *um_vmalloc(int size)
318 return(vmalloc(size));
321 unsigned long get_fault_addr(void)
323 return((unsigned long) current->thread.fault_addr);
326 EXPORT_SYMBOL(get_fault_addr);
328 void not_implemented(void)
330 printk(KERN_DEBUG "Something isn't implemented in here\n");
333 EXPORT_SYMBOL(not_implemented);
335 int user_context(unsigned long sp)
339 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
340 return(stack != (unsigned long) current_thread);
343 extern void remove_umid_dir(void);
345 __uml_exitcall(remove_umid_dir);
347 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
349 void do_uml_exitcalls(void)
353 call = &__uml_exitcall_end;
354 while (--call >= &__uml_exitcall_begin)
358 char *uml_strdup(char *string)
362 new = kmalloc(strlen(string) + 1, GFP_KERNEL);
363 if(new == NULL) return(NULL);
368 void *get_init_task(void)
370 return(&init_thread_union.thread_info.task);
373 int copy_to_user_proc(void __user *to, void *from, int size)
375 return(copy_to_user(to, from, size));
378 int copy_from_user_proc(void *to, void __user *from, int size)
380 return(copy_from_user(to, from, size));
383 int clear_user_proc(void __user *buf, int size)
385 return(clear_user(buf, size));
388 int strlen_user_proc(char __user *str)
390 return(strlen_user(str));
393 int smp_sigio_handler(void)
396 int cpu = current_thread->cpu;
404 int um_in_interrupt(void)
406 return(in_interrupt());
411 return(current_thread->cpu);
414 static atomic_t using_sysemu = ATOMIC_INIT(0);
415 int sysemu_supported;
417 void set_using_sysemu(int value)
419 if (value > sysemu_supported)
421 atomic_set(&using_sysemu, value);
424 int get_using_sysemu(void)
426 return atomic_read(&using_sysemu);
429 static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
431 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
437 static int proc_write_sysemu(struct file *file,const char *buf, unsigned long count,void *data)
441 if (copy_from_user(tmp, buf, 1))
444 if (tmp[0] >= '0' && tmp[0] <= '2')
445 set_using_sysemu(tmp[0] - '0');
446 return count; /*We use the first char, but pretend to write everything*/
449 int __init make_proc_sysemu(void)
451 struct proc_dir_entry *ent;
452 if (!sysemu_supported)
455 ent = create_proc_entry("sysemu", 0600, &proc_root);
459 printk("Failed to register /proc/sysemu\n");
463 ent->read_proc = proc_read_sysemu;
464 ent->write_proc = proc_write_sysemu;
469 late_initcall(make_proc_sysemu);
471 int singlestepping(void * t)
473 struct task_struct *task = t ? t : current;
475 if ( ! (task->ptrace & PT_DTRACE) )
478 if (task->thread.singlestep_syscall)
485 * Overrides for Emacs so that we follow Linus's tabbing style.
486 * Emacs will notice this stuff at the end of the file and automatically
487 * adjust the settings for this buffer only. This must remain at the end
489 * ---------------------------------------------------------------------------
491 * c-file-style: "linux"