process.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file handles the architecture dependent parts of process handling.
  4. *
  5. * Copyright IBM Corp. 1999, 2009
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  7. * Hartmut Penner <hp@de.ibm.com>,
  8. * Denis Joseph Barrow,
  9. */
  10. #include <linux/elf-randomize.h>
  11. #include <linux/compiler.h>
  12. #include <linux/cpu.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/elfcore.h>
  20. #include <linux/smp.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/tick.h>
  24. #include <linux/personality.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/compat.h>
  27. #include <linux/kprobes.h>
  28. #include <linux/random.h>
  29. #include <linux/export.h>
  30. #include <linux/init_task.h>
  31. #include <linux/entry-common.h>
  32. #include <linux/io.h>
  33. #include <asm/guarded_storage.h>
  34. #include <asm/access-regs.h>
  35. #include <asm/switch_to.h>
  36. #include <asm/cpu_mf.h>
  37. #include <asm/processor.h>
  38. #include <asm/ptrace.h>
  39. #include <asm/vtimer.h>
  40. #include <asm/exec.h>
  41. #include <asm/fpu.h>
  42. #include <asm/irq.h>
  43. #include <asm/nmi.h>
  44. #include <asm/smp.h>
  45. #include <asm/stacktrace.h>
  46. #include <asm/runtime_instr.h>
  47. #include <asm/unwind.h>
  48. #include "entry.h"
  49. void ret_from_fork(void) asm("ret_from_fork");
  50. void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs)
  51. {
  52. void (*func)(void *arg);
  53. schedule_tail(prev);
  54. if (!user_mode(regs)) {
  55. /* Kernel thread */
  56. func = (void *)regs->gprs[9];
  57. func((void *)regs->gprs[10]);
  58. }
  59. clear_pt_regs_flag(regs, PIF_SYSCALL);
  60. syscall_exit_to_user_mode(regs);
  61. }
  62. void flush_thread(void)
  63. {
  64. }
  65. void arch_setup_new_exec(void)
  66. {
  67. if (get_lowcore()->current_pid != current->pid) {
  68. get_lowcore()->current_pid = current->pid;
  69. if (test_facility(40))
  70. lpp(&get_lowcore()->lpp);
  71. }
  72. }
  73. void arch_release_task_struct(struct task_struct *tsk)
  74. {
  75. runtime_instr_release(tsk);
  76. guarded_storage_release(tsk);
  77. }
  78. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  79. {
  80. save_user_fpu_regs();
  81. *dst = *src;
  82. dst->thread.kfpu_flags = 0;
  83. /*
  84. * Don't transfer over the runtime instrumentation or the guarded
  85. * storage control block pointers. These fields are cleared here instead
  86. * of in copy_thread() to avoid premature freeing of associated memory
  87. * on fork() failure. Wait to clear the RI flag because ->stack still
  88. * refers to the source thread.
  89. */
  90. dst->thread.ri_cb = NULL;
  91. dst->thread.gs_cb = NULL;
  92. dst->thread.gs_bc_cb = NULL;
  93. return 0;
  94. }
  95. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  96. {
  97. unsigned long clone_flags = args->flags;
  98. unsigned long new_stackp = args->stack;
  99. unsigned long tls = args->tls;
  100. struct fake_frame
  101. {
  102. struct stack_frame sf;
  103. struct pt_regs childregs;
  104. } *frame;
  105. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  106. p->thread.ksp = (unsigned long) frame;
  107. /* Save access registers to new thread structure. */
  108. save_access_regs(&p->thread.acrs[0]);
  109. /* start new process with ar4 pointing to the correct address space */
  110. /* Don't copy debug registers */
  111. memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
  112. memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
  113. clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
  114. p->thread.per_flags = 0;
  115. /* Initialize per thread user and system timer values */
  116. p->thread.user_timer = 0;
  117. p->thread.guest_timer = 0;
  118. p->thread.system_timer = 0;
  119. p->thread.hardirq_timer = 0;
  120. p->thread.softirq_timer = 0;
  121. p->thread.last_break = 1;
  122. frame->sf.back_chain = 0;
  123. frame->sf.gprs[11 - 6] = (unsigned long)&frame->childregs;
  124. frame->sf.gprs[12 - 6] = (unsigned long)p;
  125. /* new return point is ret_from_fork */
  126. frame->sf.gprs[14 - 6] = (unsigned long)ret_from_fork;
  127. /* fake return stack for resume(), don't go back to schedule */
  128. frame->sf.gprs[15 - 6] = (unsigned long)frame;
  129. /* Store access registers to kernel stack of new process. */
  130. if (unlikely(args->fn)) {
  131. /* kernel thread */
  132. memset(&frame->childregs, 0, sizeof(struct pt_regs));
  133. frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO |
  134. PSW_MASK_EXT | PSW_MASK_MCHECK;
  135. frame->childregs.gprs[9] = (unsigned long)args->fn;
  136. frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
  137. frame->childregs.orig_gpr2 = -1;
  138. frame->childregs.last_break = 1;
  139. return 0;
  140. }
  141. frame->childregs = *current_pt_regs();
  142. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  143. frame->childregs.flags = 0;
  144. if (new_stackp)
  145. frame->childregs.gprs[15] = new_stackp;
  146. /*
  147. * Clear the runtime instrumentation flag after the above childregs
  148. * copy. The CB pointer was already cleared in arch_dup_task_struct().
  149. */
  150. frame->childregs.psw.mask &= ~PSW_MASK_RI;
  151. /* Set a new TLS ? */
  152. if (clone_flags & CLONE_SETTLS) {
  153. if (is_compat_task()) {
  154. p->thread.acrs[0] = (unsigned int)tls;
  155. } else {
  156. p->thread.acrs[0] = (unsigned int)(tls >> 32);
  157. p->thread.acrs[1] = (unsigned int)tls;
  158. }
  159. }
  160. /*
  161. * s390 stores the svc return address in arch_data when calling
  162. * sigreturn()/restart_syscall() via vdso. 1 means no valid address
  163. * stored.
  164. */
  165. p->restart_block.arch_data = 1;
  166. return 0;
  167. }
  168. void execve_tail(void)
  169. {
  170. current->thread.ufpu.fpc = 0;
  171. fpu_sfpc(0);
  172. }
  173. struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
  174. {
  175. save_user_fpu_regs();
  176. save_kernel_fpu_regs(&prev->thread);
  177. save_access_regs(&prev->thread.acrs[0]);
  178. save_ri_cb(prev->thread.ri_cb);
  179. save_gs_cb(prev->thread.gs_cb);
  180. update_cr_regs(next);
  181. restore_kernel_fpu_regs(&next->thread);
  182. restore_access_regs(&next->thread.acrs[0]);
  183. restore_ri_cb(next->thread.ri_cb, prev->thread.ri_cb);
  184. restore_gs_cb(next->thread.gs_cb);
  185. return __switch_to_asm(prev, next);
  186. }
  187. unsigned long __get_wchan(struct task_struct *p)
  188. {
  189. struct unwind_state state;
  190. unsigned long ip = 0;
  191. if (!task_stack_page(p))
  192. return 0;
  193. if (!try_get_task_stack(p))
  194. return 0;
  195. unwind_for_each_frame(&state, p, NULL, 0) {
  196. if (state.stack_info.type != STACK_TYPE_TASK) {
  197. ip = 0;
  198. break;
  199. }
  200. ip = unwind_get_return_address(&state);
  201. if (!ip)
  202. break;
  203. if (!in_sched_functions(ip))
  204. break;
  205. }
  206. put_task_stack(p);
  207. return ip;
  208. }
  209. unsigned long arch_align_stack(unsigned long sp)
  210. {
  211. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  212. sp -= get_random_u32_below(PAGE_SIZE);
  213. return sp & ~0xf;
  214. }
  215. static inline unsigned long brk_rnd(void)
  216. {
  217. return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
  218. }
  219. unsigned long arch_randomize_brk(struct mm_struct *mm)
  220. {
  221. unsigned long ret;
  222. ret = PAGE_ALIGN(mm->brk + brk_rnd());
  223. return (ret > mm->brk) ? ret : mm->brk;
  224. }