process.c 9.5 KB

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