process.c 9.2 KB

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