process.c 8.5 KB

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