process.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 (thomas@m3y3r.de)
  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/cpu.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/debug.h>
  21. #include <linux/sched/task.h>
  22. #include <linux/sched/task_stack.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/tick.h>
  25. #include <linux/threads.h>
  26. #include <linux/resume_user_mode.h>
  27. #include <asm/current.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/switch_to.h>
  30. #include <asm/exec.h>
  31. #include <linux/uaccess.h>
  32. #include <as-layout.h>
  33. #include <kern_util.h>
  34. #include <os.h>
  35. #include <skas.h>
  36. #include <registers.h>
  37. #include <linux/time-internal.h>
  38. #include <linux/elfcore.h>
  39. /*
  40. * This is a per-cpu array. A processor only modifies its entry and it only
  41. * cares about its entry, so it's OK if another processor is modifying its
  42. * entry.
  43. */
  44. struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { NULL } };
  45. void free_stack(unsigned long stack, int order)
  46. {
  47. free_pages(stack, order);
  48. }
  49. unsigned long alloc_stack(int order, int atomic)
  50. {
  51. unsigned long page;
  52. gfp_t flags = GFP_KERNEL;
  53. if (atomic)
  54. flags = GFP_ATOMIC;
  55. page = __get_free_pages(flags, order);
  56. return page;
  57. }
  58. static inline void set_current(struct task_struct *task)
  59. {
  60. cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task) { task });
  61. }
  62. struct task_struct *__switch_to(struct task_struct *from, struct task_struct *to)
  63. {
  64. to->thread.prev_sched = from;
  65. set_current(to);
  66. switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
  67. arch_switch_to(current);
  68. return current->thread.prev_sched;
  69. }
  70. void interrupt_end(void)
  71. {
  72. struct pt_regs *regs = &current->thread.regs;
  73. unsigned long thread_flags;
  74. thread_flags = read_thread_flags();
  75. while (thread_flags & _TIF_WORK_MASK) {
  76. if (thread_flags & _TIF_NEED_RESCHED)
  77. schedule();
  78. if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
  79. do_signal(regs);
  80. if (thread_flags & _TIF_NOTIFY_RESUME)
  81. resume_user_mode_work(regs);
  82. thread_flags = read_thread_flags();
  83. }
  84. }
  85. int get_current_pid(void)
  86. {
  87. return task_pid_nr(current);
  88. }
  89. /*
  90. * This is called magically, by its address being stuffed in a jmp_buf
  91. * and being longjmp-d to.
  92. */
  93. void new_thread_handler(void)
  94. {
  95. int (*fn)(void *);
  96. void *arg;
  97. if (current->thread.prev_sched != NULL)
  98. schedule_tail(current->thread.prev_sched);
  99. current->thread.prev_sched = NULL;
  100. fn = current->thread.request.thread.proc;
  101. arg = current->thread.request.thread.arg;
  102. /*
  103. * callback returns only if the kernel thread execs a process
  104. */
  105. fn(arg);
  106. userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
  107. }
  108. /* Called magically, see new_thread_handler above */
  109. static void fork_handler(void)
  110. {
  111. schedule_tail(current->thread.prev_sched);
  112. /*
  113. * XXX: if interrupt_end() calls schedule, this call to
  114. * arch_switch_to isn't needed. We could want to apply this to
  115. * improve performance. -bb
  116. */
  117. arch_switch_to(current);
  118. current->thread.prev_sched = NULL;
  119. userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
  120. }
  121. int copy_thread(struct task_struct * p, const struct kernel_clone_args *args)
  122. {
  123. unsigned long clone_flags = args->flags;
  124. unsigned long sp = args->stack;
  125. unsigned long tls = args->tls;
  126. void (*handler)(void);
  127. int ret = 0;
  128. p->thread = (struct thread_struct) INIT_THREAD;
  129. if (!args->fn) {
  130. memcpy(&p->thread.regs.regs, current_pt_regs(),
  131. sizeof(p->thread.regs.regs));
  132. PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
  133. if (sp != 0)
  134. REGS_SP(p->thread.regs.regs.gp) = sp;
  135. handler = fork_handler;
  136. arch_copy_thread(&current->thread.arch, &p->thread.arch);
  137. } else {
  138. get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
  139. p->thread.request.thread.proc = args->fn;
  140. p->thread.request.thread.arg = args->fn_arg;
  141. handler = new_thread_handler;
  142. }
  143. new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
  144. if (!args->fn) {
  145. clear_flushed_tls(p);
  146. /*
  147. * Set a new TLS for the child thread?
  148. */
  149. if (clone_flags & CLONE_SETTLS)
  150. ret = arch_set_tls(p, tls);
  151. }
  152. return ret;
  153. }
  154. void initial_thread_cb(void (*proc)(void *), void *arg)
  155. {
  156. int save_kmalloc_ok = kmalloc_ok;
  157. kmalloc_ok = 0;
  158. initial_thread_cb_skas(proc, arg);
  159. kmalloc_ok = save_kmalloc_ok;
  160. }
  161. void um_idle_sleep(void)
  162. {
  163. if (time_travel_mode != TT_MODE_OFF)
  164. time_travel_sleep();
  165. else
  166. os_idle_sleep();
  167. }
  168. void arch_cpu_idle(void)
  169. {
  170. um_idle_sleep();
  171. }
  172. int __uml_cant_sleep(void) {
  173. return in_atomic() || irqs_disabled() || in_interrupt();
  174. /* Is in_interrupt() really needed? */
  175. }
  176. int user_context(unsigned long sp)
  177. {
  178. unsigned long stack;
  179. stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
  180. return stack != (unsigned long) current_thread_info();
  181. }
  182. extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
  183. void do_uml_exitcalls(void)
  184. {
  185. exitcall_t *call;
  186. call = &__uml_exitcall_end;
  187. while (--call >= &__uml_exitcall_begin)
  188. (*call)();
  189. }
  190. char *uml_strdup(const char *string)
  191. {
  192. return kstrdup(string, GFP_KERNEL);
  193. }
  194. EXPORT_SYMBOL(uml_strdup);
  195. int copy_from_user_proc(void *to, void __user *from, int size)
  196. {
  197. return copy_from_user(to, from, size);
  198. }
  199. int singlestepping(void)
  200. {
  201. return test_thread_flag(TIF_SINGLESTEP);
  202. }
  203. /*
  204. * Only x86 and x86_64 have an arch_align_stack().
  205. * All other arches have "#define arch_align_stack(x) (x)"
  206. * in their asm/exec.h
  207. * As this is included in UML from asm-um/system-generic.h,
  208. * we can use it to behave as the subarch does.
  209. */
  210. #ifndef arch_align_stack
  211. unsigned long arch_align_stack(unsigned long sp)
  212. {
  213. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  214. sp -= get_random_u32_below(8192);
  215. return sp & ~0xf;
  216. }
  217. #endif
  218. unsigned long __get_wchan(struct task_struct *p)
  219. {
  220. unsigned long stack_page, sp, ip;
  221. bool seen_sched = 0;
  222. stack_page = (unsigned long) task_stack_page(p);
  223. /* Bail if the process has no kernel stack for some reason */
  224. if (stack_page == 0)
  225. return 0;
  226. sp = p->thread.switch_buf->JB_SP;
  227. /*
  228. * Bail if the stack pointer is below the bottom of the kernel
  229. * stack for some reason
  230. */
  231. if (sp < stack_page)
  232. return 0;
  233. while (sp < stack_page + THREAD_SIZE) {
  234. ip = *((unsigned long *) sp);
  235. if (in_sched_functions(ip))
  236. /* Ignore everything until we're above the scheduler */
  237. seen_sched = 1;
  238. else if (kernel_text_address(ip) && seen_sched)
  239. return ip;
  240. sp += sizeof(unsigned long);
  241. }
  242. return 0;
  243. }
  244. int elf_core_copy_task_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
  245. {
  246. int cpu = current_thread_info()->cpu;
  247. return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu) == 0;
  248. }