process.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Copyright 2003 PathScale, Inc.
  4. * Licensed under the GPL
  5. */
  6. #include <linux/stddef.h>
  7. #include <linux/err.h>
  8. #include <linux/hardirq.h>
  9. #include <linux/mm.h>
  10. #include <linux/module.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/slab.h>
  16. #include <linux/sched.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/tick.h>
  19. #include <linux/threads.h>
  20. #include <linux/tracehook.h>
  21. #include <asm/current.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/uaccess.h>
  25. #include <as-layout.h>
  26. #include <kern_util.h>
  27. #include <os.h>
  28. #include <skas.h>
  29. /*
  30. * This is a per-cpu array. A processor only modifies its entry and it only
  31. * cares about its entry, so it's OK if another processor is modifying its
  32. * entry.
  33. */
  34. struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
  35. static inline int external_pid(void)
  36. {
  37. /* FIXME: Need to look up userspace_pid by cpu */
  38. return userspace_pid[0];
  39. }
  40. int pid_to_processor_id(int pid)
  41. {
  42. int i;
  43. for (i = 0; i < ncpus; i++) {
  44. if (cpu_tasks[i].pid == pid)
  45. return i;
  46. }
  47. return -1;
  48. }
  49. void free_stack(unsigned long stack, int order)
  50. {
  51. free_pages(stack, order);
  52. }
  53. unsigned long alloc_stack(int order, int atomic)
  54. {
  55. unsigned long page;
  56. gfp_t flags = GFP_KERNEL;
  57. if (atomic)
  58. flags = GFP_ATOMIC;
  59. page = __get_free_pages(flags, order);
  60. return page;
  61. }
  62. static inline void set_current(struct task_struct *task)
  63. {
  64. cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
  65. { external_pid(), task });
  66. }
  67. extern void arch_switch_to(struct task_struct *to);
  68. void *__switch_to(struct task_struct *from, struct task_struct *to)
  69. {
  70. to->thread.prev_sched = from;
  71. set_current(to);
  72. switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
  73. arch_switch_to(current);
  74. return current->thread.prev_sched;
  75. }
  76. void interrupt_end(void)
  77. {
  78. if (need_resched())
  79. schedule();
  80. if (test_thread_flag(TIF_SIGPENDING))
  81. do_signal();
  82. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  83. tracehook_notify_resume(&current->thread.regs);
  84. }
  85. void exit_thread(void)
  86. {
  87. }
  88. int get_current_pid(void)
  89. {
  90. return task_pid_nr(current);
  91. }
  92. /*
  93. * This is called magically, by its address being stuffed in a jmp_buf
  94. * and being longjmp-d to.
  95. */
  96. void new_thread_handler(void)
  97. {
  98. int (*fn)(void *), n;
  99. void *arg;
  100. if (current->thread.prev_sched != NULL)
  101. schedule_tail(current->thread.prev_sched);
  102. current->thread.prev_sched = NULL;
  103. fn = current->thread.request.u.thread.proc;
  104. arg = current->thread.request.u.thread.arg;
  105. /*
  106. * callback returns only if the kernel thread execs a process
  107. */
  108. n = fn(arg);
  109. userspace(&current->thread.regs.regs);
  110. }
  111. /* Called magically, see new_thread_handler above */
  112. void fork_handler(void)
  113. {
  114. force_flush_all();
  115. schedule_tail(current->thread.prev_sched);
  116. /*
  117. * XXX: if interrupt_end() calls schedule, this call to
  118. * arch_switch_to isn't needed. We could want to apply this to
  119. * improve performance. -bb
  120. */
  121. arch_switch_to(current);
  122. current->thread.prev_sched = NULL;
  123. userspace(&current->thread.regs.regs);
  124. }
  125. int copy_thread(unsigned long clone_flags, unsigned long sp,
  126. unsigned long arg, struct task_struct * p)
  127. {
  128. void (*handler)(void);
  129. int kthread = current->flags & PF_KTHREAD;
  130. int ret = 0;
  131. p->thread = (struct thread_struct) INIT_THREAD;
  132. if (!kthread) {
  133. memcpy(&p->thread.regs.regs, current_pt_regs(),
  134. sizeof(p->thread.regs.regs));
  135. PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
  136. if (sp != 0)
  137. REGS_SP(p->thread.regs.regs.gp) = sp;
  138. handler = fork_handler;
  139. arch_copy_thread(&current->thread.arch, &p->thread.arch);
  140. } else {
  141. get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
  142. p->thread.request.u.thread.proc = (int (*)(void *))sp;
  143. p->thread.request.u.thread.arg = (void *)arg;
  144. handler = new_thread_handler;
  145. }
  146. new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
  147. if (!kthread) {
  148. clear_flushed_tls(p);
  149. /*
  150. * Set a new TLS for the child thread?
  151. */
  152. if (clone_flags & CLONE_SETTLS)
  153. ret = arch_copy_tls(p);
  154. }
  155. return ret;
  156. }
  157. void initial_thread_cb(void (*proc)(void *), void *arg)
  158. {
  159. int save_kmalloc_ok = kmalloc_ok;
  160. kmalloc_ok = 0;
  161. initial_thread_cb_skas(proc, arg);
  162. kmalloc_ok = save_kmalloc_ok;
  163. }
  164. void arch_cpu_idle(void)
  165. {
  166. unsigned long long nsecs;
  167. cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
  168. nsecs = disable_timer();
  169. idle_sleep(nsecs);
  170. local_irq_enable();
  171. }
  172. int __cant_sleep(void) {
  173. return in_atomic() || irqs_disabled() || in_interrupt();
  174. /* Is in_interrupt() really needed? */
  175. }
  176. int user_context(unsigned long sp)
  177. {
  178. unsigned long stack;
  179. stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
  180. return stack != (unsigned long) current_thread_info();
  181. }
  182. extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
  183. void do_uml_exitcalls(void)
  184. {
  185. exitcall_t *call;
  186. call = &__uml_exitcall_end;
  187. while (--call >= &__uml_exitcall_begin)
  188. (*call)();
  189. }
  190. char *uml_strdup(const char *string)
  191. {
  192. return kstrdup(string, GFP_KERNEL);
  193. }
  194. EXPORT_SYMBOL(uml_strdup);
  195. int copy_to_user_proc(void __user *to, void *from, int size)
  196. {
  197. return copy_to_user(to, from, size);
  198. }
  199. int copy_from_user_proc(void *to, void __user *from, int size)
  200. {
  201. return copy_from_user(to, from, size);
  202. }
  203. int clear_user_proc(void __user *buf, int size)
  204. {
  205. return clear_user(buf, size);
  206. }
  207. int strlen_user_proc(char __user *str)
  208. {
  209. return strlen_user(str);
  210. }
  211. int smp_sigio_handler(void)
  212. {
  213. #ifdef CONFIG_SMP
  214. int cpu = current_thread_info()->cpu;
  215. IPI_handler(cpu);
  216. if (cpu != 0)
  217. return 1;
  218. #endif
  219. return 0;
  220. }
  221. int cpu(void)
  222. {
  223. return current_thread_info()->cpu;
  224. }
  225. static atomic_t using_sysemu = ATOMIC_INIT(0);
  226. int sysemu_supported;
  227. void set_using_sysemu(int value)
  228. {
  229. if (value > sysemu_supported)
  230. return;
  231. atomic_set(&using_sysemu, value);
  232. }
  233. int get_using_sysemu(void)
  234. {
  235. return atomic_read(&using_sysemu);
  236. }
  237. static int sysemu_proc_show(struct seq_file *m, void *v)
  238. {
  239. seq_printf(m, "%d\n", get_using_sysemu());
  240. return 0;
  241. }
  242. static int sysemu_proc_open(struct inode *inode, struct file *file)
  243. {
  244. return single_open(file, sysemu_proc_show, NULL);
  245. }
  246. static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
  247. size_t count, loff_t *pos)
  248. {
  249. char tmp[2];
  250. if (copy_from_user(tmp, buf, 1))
  251. return -EFAULT;
  252. if (tmp[0] >= '0' && tmp[0] <= '2')
  253. set_using_sysemu(tmp[0] - '0');
  254. /* We use the first char, but pretend to write everything */
  255. return count;
  256. }
  257. static const struct file_operations sysemu_proc_fops = {
  258. .owner = THIS_MODULE,
  259. .open = sysemu_proc_open,
  260. .read = seq_read,
  261. .llseek = seq_lseek,
  262. .release = single_release,
  263. .write = sysemu_proc_write,
  264. };
  265. int __init make_proc_sysemu(void)
  266. {
  267. struct proc_dir_entry *ent;
  268. if (!sysemu_supported)
  269. return 0;
  270. ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_fops);
  271. if (ent == NULL)
  272. {
  273. printk(KERN_WARNING "Failed to register /proc/sysemu\n");
  274. return 0;
  275. }
  276. return 0;
  277. }
  278. late_initcall(make_proc_sysemu);
  279. int singlestepping(void * t)
  280. {
  281. struct task_struct *task = t ? t : current;
  282. if (!(task->ptrace & PT_DTRACE))
  283. return 0;
  284. if (task->thread.singlestep_syscall)
  285. return 1;
  286. return 2;
  287. }
  288. /*
  289. * Only x86 and x86_64 have an arch_align_stack().
  290. * All other arches have "#define arch_align_stack(x) (x)"
  291. * in their asm/system.h
  292. * As this is included in UML from asm-um/system-generic.h,
  293. * we can use it to behave as the subarch does.
  294. */
  295. #ifndef arch_align_stack
  296. unsigned long arch_align_stack(unsigned long sp)
  297. {
  298. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  299. sp -= get_random_int() % 8192;
  300. return sp & ~0xf;
  301. }
  302. #endif
  303. unsigned long get_wchan(struct task_struct *p)
  304. {
  305. unsigned long stack_page, sp, ip;
  306. bool seen_sched = 0;
  307. if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
  308. return 0;
  309. stack_page = (unsigned long) task_stack_page(p);
  310. /* Bail if the process has no kernel stack for some reason */
  311. if (stack_page == 0)
  312. return 0;
  313. sp = p->thread.switch_buf->JB_SP;
  314. /*
  315. * Bail if the stack pointer is below the bottom of the kernel
  316. * stack for some reason
  317. */
  318. if (sp < stack_page)
  319. return 0;
  320. while (sp < stack_page + THREAD_SIZE) {
  321. ip = *((unsigned long *) sp);
  322. if (in_sched_functions(ip))
  323. /* Ignore everything until we're above the scheduler */
  324. seen_sched = 1;
  325. else if (kernel_text_address(ip) && seen_sched)
  326. return ip;
  327. sp += sizeof(unsigned long);
  328. }
  329. return 0;
  330. }
  331. int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
  332. {
  333. int cpu = current_thread_info()->cpu;
  334. return save_fp_registers(userspace_pid[cpu], (unsigned long *) fpu);
  335. }