process.c 9.6 KB

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