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/random.h"
25 #include "asm/unistd.h"
27 #include "asm/segment.h"
29 #include "asm/pgtable.h"
30 #include "asm/processor.h"
31 #include "asm/tlbflush.h"
32 #include "asm/uaccess.h"
34 #include "user_util.h"
35 #include "kern_util.h"
37 #include "signal_kern.h"
38 #include "signal_user.h"
42 #include "time_user.h"
44 #include "frame_kern.h"
45 #include "sigcontext.h"
48 #include "mode_kern.h"
49 #include "choose-mode.h"
51 /* This is a per-cpu array. A processor only modifies its entry and it only
52 * cares about its entry, so it's OK if another processor is modifying its
55 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
57 int external_pid(void *t)
59 struct task_struct *task = t ? t : current;
61 return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
64 int pid_to_processor_id(int pid)
68 for(i = 0; i < ncpus; i++){
69 if(cpu_tasks[i].pid == pid) return(i);
74 void free_stack(unsigned long stack, int order)
76 free_pages(stack, order);
79 unsigned long alloc_stack(int order, int atomic)
82 int flags = GFP_KERNEL;
84 if(atomic) flags |= GFP_ATOMIC;
85 page = __get_free_pages(flags, order);
88 stack_protections(page);
92 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
96 current->thread.request.u.thread.proc = fn;
97 current->thread.request.u.thread.arg = arg;
98 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0, NULL, 0, NULL,
101 panic("do_fork failed in kernel_thread, errno = %d", pid);
105 void set_current(void *t)
107 struct task_struct *task = t;
109 cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
110 { external_pid(task), task });
113 void *_switch_to(void *prev, void *next, void *last)
115 return(CHOOSE_MODE(switch_to_tt(prev, next),
116 switch_to_skas(prev, next)));
119 void interrupt_end(void)
121 if(need_resched()) schedule();
122 if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
125 void release_thread(struct task_struct *task)
127 CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
130 void exit_thread(void)
132 unprotect_stack((unsigned long) current_thread);
135 void *get_current(void)
140 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
141 unsigned long stack_top, struct task_struct * p,
142 struct pt_regs *regs)
144 p->thread = (struct thread_struct) INIT_THREAD;
145 return(CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr,
146 clone_flags, sp, stack_top, p, regs));
149 void initial_thread_cb(void (*proc)(void *), void *arg)
151 int save_kmalloc_ok = kmalloc_ok;
154 CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
156 kmalloc_ok = save_kmalloc_ok;
159 unsigned long stack_sp(unsigned long page)
161 return(page + PAGE_SIZE - sizeof(void *));
164 int current_pid(void)
166 return(current->pid);
169 void default_idle(void)
173 atomic_inc(&init_mm.mm_count);
174 current->mm = &init_mm;
175 current->active_mm = &init_mm;
178 /* endless idle loop with no priority at all */
181 * although we are an idle CPU, we do not want to
182 * get into the scheduler unnecessarily.
193 CHOOSE_MODE(init_idle_tt(), init_idle_skas());
201 void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
210 return(ERR_PTR(-EINVAL));
211 pgd = pgd_offset(task->mm, addr);
212 if(!pgd_present(*pgd))
213 return(ERR_PTR(-EINVAL));
215 pud = pud_offset(pgd, addr);
216 if(!pud_present(*pud))
217 return(ERR_PTR(-EINVAL));
219 pmd = pmd_offset(pud, addr);
220 if(!pmd_present(*pmd))
221 return(ERR_PTR(-EINVAL));
223 pte = pte_offset_kernel(pmd, addr);
224 if(!pte_present(*pte))
225 return(ERR_PTR(-EINVAL));
229 return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
232 char *current_cmd(void)
234 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
237 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
238 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
242 void force_sigbus(void)
244 printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
247 sigaddset(¤t->pending.signal, SIGBUS);
249 current->flags |= PF_SIGNALED;
250 do_exit(SIGBUS | 0x80);
253 void dump_thread(struct pt_regs *regs, struct user *u)
257 void enable_hlt(void)
262 EXPORT_SYMBOL(enable_hlt);
264 void disable_hlt(void)
266 panic("disable_hlt");
269 EXPORT_SYMBOL(disable_hlt);
271 void *um_kmalloc(int size)
273 return(kmalloc(size, GFP_KERNEL));
276 void *um_kmalloc_atomic(int size)
278 return(kmalloc(size, GFP_ATOMIC));
281 void *um_vmalloc(int size)
283 return(vmalloc(size));
286 unsigned long get_fault_addr(void)
288 return((unsigned long) current->thread.fault_addr);
291 EXPORT_SYMBOL(get_fault_addr);
293 void not_implemented(void)
295 printk(KERN_DEBUG "Something isn't implemented in here\n");
298 EXPORT_SYMBOL(not_implemented);
300 int user_context(unsigned long sp)
304 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
305 return(stack != (unsigned long) current_thread);
308 extern void remove_umid_dir(void);
310 __uml_exitcall(remove_umid_dir);
312 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
314 void do_uml_exitcalls(void)
318 call = &__uml_exitcall_end;
319 while (--call >= &__uml_exitcall_begin)
323 char *uml_strdup(char *string)
327 new = kmalloc(strlen(string) + 1, GFP_KERNEL);
328 if(new == NULL) return(NULL);
333 int copy_to_user_proc(void __user *to, void *from, int size)
335 return(copy_to_user(to, from, size));
338 int copy_from_user_proc(void *to, void __user *from, int size)
340 return(copy_from_user(to, from, size));
343 int clear_user_proc(void __user *buf, int size)
345 return(clear_user(buf, size));
348 int strlen_user_proc(char __user *str)
350 return(strlen_user(str));
353 int smp_sigio_handler(void)
356 int cpu = current_thread->cpu;
364 int um_in_interrupt(void)
366 return(in_interrupt());
371 return(current_thread->cpu);
374 static atomic_t using_sysemu = ATOMIC_INIT(0);
375 int sysemu_supported;
377 void set_using_sysemu(int value)
379 if (value > sysemu_supported)
381 atomic_set(&using_sysemu, value);
384 int get_using_sysemu(void)
386 return atomic_read(&using_sysemu);
389 static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
391 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
397 static int proc_write_sysemu(struct file *file,const char *buf, unsigned long count,void *data)
401 if (copy_from_user(tmp, buf, 1))
404 if (tmp[0] >= '0' && tmp[0] <= '2')
405 set_using_sysemu(tmp[0] - '0');
406 return count; /*We use the first char, but pretend to write everything*/
409 int __init make_proc_sysemu(void)
411 struct proc_dir_entry *ent;
412 if (!sysemu_supported)
415 ent = create_proc_entry("sysemu", 0600, &proc_root);
419 printk("Failed to register /proc/sysemu\n");
423 ent->read_proc = proc_read_sysemu;
424 ent->write_proc = proc_write_sysemu;
429 late_initcall(make_proc_sysemu);
431 int singlestepping(void * t)
433 struct task_struct *task = t ? t : current;
435 if ( ! (task->ptrace & PT_DTRACE) )
438 if (task->thread.singlestep_syscall)
445 * Only x86 and x86_64 have an arch_align_stack().
446 * All other arches have "#define arch_align_stack(x) (x)"
447 * in their asm/system.h
448 * As this is included in UML from asm-um/system-generic.h,
449 * we can use it to behave as the subarch does.
451 #ifndef arch_align_stack
452 unsigned long arch_align_stack(unsigned long sp)
454 if (randomize_va_space)
455 sp -= get_random_int() % 8192;