2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Copyright 2003 PathScale, Inc.
4 * Licensed under the GPL
7 #include "linux/stddef.h"
9 #include "linux/hardirq.h"
11 #include "linux/personality.h"
12 #include "linux/proc_fs.h"
13 #include "linux/ptrace.h"
14 #include "linux/random.h"
15 #include "linux/sched.h"
16 #include "linux/threads.h"
17 #include "asm/pgtable.h"
18 #include "asm/uaccess.h"
19 #include "as-layout.h"
20 #include "kern_util.h"
26 * This is a per-cpu array. A processor only modifies its entry and it only
27 * cares about its entry, so it's OK if another processor is modifying its
30 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
32 static inline int external_pid(struct task_struct *task)
34 /* FIXME: Need to look up userspace_pid by cpu */
35 return userspace_pid[0];
38 int pid_to_processor_id(int pid)
42 for(i = 0; i < ncpus; i++) {
43 if (cpu_tasks[i].pid == pid)
49 void free_stack(unsigned long stack, int order)
51 free_pages(stack, order);
54 unsigned long alloc_stack(int order, int atomic)
57 gfp_t flags = GFP_KERNEL;
61 page = __get_free_pages(flags, order);
68 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
72 current->thread.request.u.thread.proc = fn;
73 current->thread.request.u.thread.arg = arg;
74 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
75 ¤t->thread.regs, 0, NULL, NULL);
79 static inline void set_current(struct task_struct *task)
81 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
82 { external_pid(task), task });
85 extern void arch_switch_to(struct task_struct *from, struct task_struct *to);
87 void *_switch_to(void *prev, void *next, void *last)
89 struct task_struct *from = prev;
90 struct task_struct *to= next;
92 to->thread.prev_sched = from;
96 current->thread.saved_task = NULL;
98 switch_threads(&from->thread.switch_buf,
99 &to->thread.switch_buf);
101 arch_switch_to(current->thread.prev_sched, current);
103 if (current->thread.saved_task)
104 show_regs(&(current->thread.regs));
105 next= current->thread.saved_task;
107 } while(current->thread.saved_task);
109 return current->thread.prev_sched;
113 void interrupt_end(void)
117 if (test_tsk_thread_flag(current, TIF_SIGPENDING))
121 void exit_thread(void)
125 void *get_current(void)
130 extern void schedule_tail(struct task_struct *prev);
133 * This is called magically, by its address being stuffed in a jmp_buf
134 * and being longjmp-d to.
136 void new_thread_handler(void)
138 int (*fn)(void *), n;
141 if (current->thread.prev_sched != NULL)
142 schedule_tail(current->thread.prev_sched);
143 current->thread.prev_sched = NULL;
145 fn = current->thread.request.u.thread.proc;
146 arg = current->thread.request.u.thread.arg;
149 * The return value is 1 if the kernel thread execs a process,
152 n = run_kernel_thread(fn, arg, ¤t->thread.exec_buf);
154 /* Handle any immediate reschedules or signals */
156 userspace(¤t->thread.regs.regs);
161 /* Called magically, see new_thread_handler above */
162 void fork_handler(void)
165 if (current->thread.prev_sched == NULL)
168 schedule_tail(current->thread.prev_sched);
171 * XXX: if interrupt_end() calls schedule, this call to
172 * arch_switch_to isn't needed. We could want to apply this to
173 * improve performance. -bb
175 arch_switch_to(current->thread.prev_sched, current);
177 current->thread.prev_sched = NULL;
179 /* Handle any immediate reschedules or signals */
182 userspace(¤t->thread.regs.regs);
185 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
186 unsigned long stack_top, struct task_struct * p,
187 struct pt_regs *regs)
189 void (*handler)(void);
192 p->thread = (struct thread_struct) INIT_THREAD;
194 if (current->thread.forking) {
195 memcpy(&p->thread.regs.regs, ®s->regs,
196 sizeof(p->thread.regs.regs));
197 REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.gp, 0);
199 REGS_SP(p->thread.regs.regs.gp) = sp;
201 handler = fork_handler;
203 arch_copy_thread(¤t->thread.arch, &p->thread.arch);
206 init_thread_registers(&p->thread.regs.regs);
207 p->thread.request.u.thread = current->thread.request.u.thread;
208 handler = new_thread_handler;
211 new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
213 if (current->thread.forking) {
214 clear_flushed_tls(p);
217 * Set a new TLS for the child thread?
219 if (clone_flags & CLONE_SETTLS)
220 ret = arch_copy_tls(p);
226 void initial_thread_cb(void (*proc)(void *), void *arg)
228 int save_kmalloc_ok = kmalloc_ok;
231 initial_thread_cb_skas(proc, arg);
232 kmalloc_ok = save_kmalloc_ok;
235 void default_idle(void)
238 /* endless idle loop with no priority at all */
241 * although we are an idle CPU, we do not want to
242 * get into the scheduler unnecessarily.
255 cpu_tasks[current_thread->cpu].pid = os_getpid();
259 void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
268 if (task->mm == NULL)
269 return ERR_PTR(-EINVAL);
270 pgd = pgd_offset(task->mm, addr);
271 if (!pgd_present(*pgd))
272 return ERR_PTR(-EINVAL);
274 pud = pud_offset(pgd, addr);
275 if (!pud_present(*pud))
276 return ERR_PTR(-EINVAL);
278 pmd = pmd_offset(pud, addr);
279 if (!pmd_present(*pmd))
280 return ERR_PTR(-EINVAL);
282 pte = pte_offset_kernel(pmd, addr);
284 if (!pte_present(ptent))
285 return ERR_PTR(-EINVAL);
289 return (void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK);
292 char *current_cmd(void)
294 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
297 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
298 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
302 void dump_thread(struct pt_regs *regs, struct user *u)
306 int __cant_sleep(void) {
307 return in_atomic() || irqs_disabled() || in_interrupt();
308 /* Is in_interrupt() really needed? */
311 int user_context(unsigned long sp)
315 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
316 return stack != (unsigned long) current_thread;
319 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
321 void do_uml_exitcalls(void)
325 call = &__uml_exitcall_end;
326 while (--call >= &__uml_exitcall_begin)
330 char *uml_strdup(char *string)
332 return kstrdup(string, GFP_KERNEL);
335 int copy_to_user_proc(void __user *to, void *from, int size)
337 return copy_to_user(to, from, size);
340 int copy_from_user_proc(void *to, void __user *from, int size)
342 return copy_from_user(to, from, size);
345 int clear_user_proc(void __user *buf, int size)
347 return clear_user(buf, size);
350 int strlen_user_proc(char __user *str)
352 return strlen_user(str);
355 int smp_sigio_handler(void)
358 int cpu = current_thread->cpu;
368 return current_thread->cpu;
371 static atomic_t using_sysemu = ATOMIC_INIT(0);
372 int sysemu_supported;
374 void set_using_sysemu(int value)
376 if (value > sysemu_supported)
378 atomic_set(&using_sysemu, value);
381 int get_using_sysemu(void)
383 return atomic_read(&using_sysemu);
386 static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
388 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size)
395 static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
399 if (copy_from_user(tmp, buf, 1))
402 if (tmp[0] >= '0' && tmp[0] <= '2')
403 set_using_sysemu(tmp[0] - '0');
404 /* We use the first char, but pretend to write everything */
408 int __init make_proc_sysemu(void)
410 struct proc_dir_entry *ent;
411 if (!sysemu_supported)
414 ent = create_proc_entry("sysemu", 0600, &proc_root);
418 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
422 ent->read_proc = proc_read_sysemu;
423 ent->write_proc = proc_write_sysemu;
428 late_initcall(make_proc_sysemu);
430 int singlestepping(void * t)
432 struct task_struct *task = t ? t : current;
434 if ( ! (task->ptrace & PT_DTRACE) )
437 if (task->thread.singlestep_syscall)
444 * Only x86 and x86_64 have an arch_align_stack().
445 * All other arches have "#define arch_align_stack(x) (x)"
446 * in their asm/system.h
447 * As this is included in UML from asm-um/system-generic.h,
448 * we can use it to behave as the subarch does.
450 #ifndef arch_align_stack
451 unsigned long arch_align_stack(unsigned long sp)
453 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
454 sp -= get_random_int() % 8192;