process.c 8.6 KB

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