process.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <[email protected]>,
  7. * Hartmut Penner <[email protected]>,
  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 <asm/cpu_mf.h>
  33. #include <asm/io.h>
  34. #include <asm/processor.h>
  35. #include <asm/vtimer.h>
  36. #include <asm/exec.h>
  37. #include <asm/irq.h>
  38. #include <asm/nmi.h>
  39. #include <asm/smp.h>
  40. #include <asm/stacktrace.h>
  41. #include <asm/switch_to.h>
  42. #include <asm/runtime_instr.h>
  43. #include <asm/unwind.h>
  44. #include "entry.h"
  45. void ret_from_fork(void) asm("ret_from_fork");
  46. void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs)
  47. {
  48. void (*func)(void *arg);
  49. schedule_tail(prev);
  50. if (!user_mode(regs)) {
  51. /* Kernel thread */
  52. func = (void *)regs->gprs[9];
  53. func((void *)regs->gprs[10]);
  54. }
  55. clear_pt_regs_flag(regs, PIF_SYSCALL);
  56. syscall_exit_to_user_mode(regs);
  57. }
  58. void flush_thread(void)
  59. {
  60. }
  61. void arch_setup_new_exec(void)
  62. {
  63. if (S390_lowcore.current_pid != current->pid) {
  64. S390_lowcore.current_pid = current->pid;
  65. if (test_facility(40))
  66. lpp(&S390_lowcore.lpp);
  67. }
  68. }
  69. void arch_release_task_struct(struct task_struct *tsk)
  70. {
  71. runtime_instr_release(tsk);
  72. guarded_storage_release(tsk);
  73. }
  74. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  75. {
  76. /*
  77. * Save the floating-point or vector register state of the current
  78. * task and set the CIF_FPU flag to lazy restore the FPU register
  79. * state when returning to user space.
  80. */
  81. save_fpu_regs();
  82. memcpy(dst, src, arch_task_struct_size);
  83. dst->thread.fpu.regs = dst->thread.fpu.fprs;
  84. /*
  85. * Don't transfer over the runtime instrumentation or the guarded
  86. * storage control block pointers. These fields are cleared here instead
  87. * of in copy_thread() to avoid premature freeing of associated memory
  88. * on fork() failure. Wait to clear the RI flag because ->stack still
  89. * refers to the source thread.
  90. */
  91. dst->thread.ri_cb = NULL;
  92. dst->thread.gs_cb = NULL;
  93. dst->thread.gs_bc_cb = NULL;
  94. return 0;
  95. }
  96. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  97. {
  98. unsigned long clone_flags = args->flags;
  99. unsigned long new_stackp = args->stack;
  100. unsigned long tls = args->tls;
  101. struct fake_frame
  102. {
  103. struct stack_frame sf;
  104. struct pt_regs childregs;
  105. } *frame;
  106. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  107. p->thread.ksp = (unsigned long) frame;
  108. /* Save access registers to new thread structure. */
  109. save_access_regs(&p->thread.acrs[0]);
  110. /* start new process with ar4 pointing to the correct address space */
  111. /* Don't copy debug registers */
  112. memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
  113. memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
  114. clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
  115. p->thread.per_flags = 0;
  116. /* Initialize per thread user and system timer values */
  117. p->thread.user_timer = 0;
  118. p->thread.guest_timer = 0;
  119. p->thread.system_timer = 0;
  120. p->thread.hardirq_timer = 0;
  121. p->thread.softirq_timer = 0;
  122. p->thread.last_break = 1;
  123. frame->sf.back_chain = 0;
  124. frame->sf.gprs[5] = (unsigned long)frame + sizeof(struct stack_frame);
  125. frame->sf.gprs[6] = (unsigned long)p;
  126. /* new return point is ret_from_fork */
  127. frame->sf.gprs[8] = (unsigned long)ret_from_fork;
  128. /* fake return stack for resume(), don't go back to schedule */
  129. frame->sf.gprs[9] = (unsigned long)frame;
  130. /* Store access registers to kernel stack of new process. */
  131. if (unlikely(args->fn)) {
  132. /* kernel thread */
  133. memset(&frame->childregs, 0, sizeof(struct pt_regs));
  134. frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
  135. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  136. frame->childregs.psw.addr =
  137. (unsigned long)__ret_from_fork;
  138. frame->childregs.gprs[9] = (unsigned long)args->fn;
  139. frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
  140. frame->childregs.orig_gpr2 = -1;
  141. frame->childregs.last_break = 1;
  142. return 0;
  143. }
  144. frame->childregs = *current_pt_regs();
  145. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  146. frame->childregs.flags = 0;
  147. if (new_stackp)
  148. frame->childregs.gprs[15] = new_stackp;
  149. /*
  150. * Clear the runtime instrumentation flag after the above childregs
  151. * copy. The CB pointer was already cleared in arch_dup_task_struct().
  152. */
  153. frame->childregs.psw.mask &= ~PSW_MASK_RI;
  154. /* Set a new TLS ? */
  155. if (clone_flags & CLONE_SETTLS) {
  156. if (is_compat_task()) {
  157. p->thread.acrs[0] = (unsigned int)tls;
  158. } else {
  159. p->thread.acrs[0] = (unsigned int)(tls >> 32);
  160. p->thread.acrs[1] = (unsigned int)tls;
  161. }
  162. }
  163. /*
  164. * s390 stores the svc return address in arch_data when calling
  165. * sigreturn()/restart_syscall() via vdso. 1 means no valid address
  166. * stored.
  167. */
  168. p->restart_block.arch_data = 1;
  169. return 0;
  170. }
  171. void execve_tail(void)
  172. {
  173. current->thread.fpu.fpc = 0;
  174. asm volatile("sfpc %0" : : "d" (0));
  175. }
  176. unsigned long __get_wchan(struct task_struct *p)
  177. {
  178. struct unwind_state state;
  179. unsigned long ip = 0;
  180. if (!task_stack_page(p))
  181. return 0;
  182. if (!try_get_task_stack(p))
  183. return 0;
  184. unwind_for_each_frame(&state, p, NULL, 0) {
  185. if (state.stack_info.type != STACK_TYPE_TASK) {
  186. ip = 0;
  187. break;
  188. }
  189. ip = unwind_get_return_address(&state);
  190. if (!ip)
  191. break;
  192. if (!in_sched_functions(ip))
  193. break;
  194. }
  195. put_task_stack(p);
  196. return ip;
  197. }
  198. unsigned long arch_align_stack(unsigned long sp)
  199. {
  200. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  201. sp -= prandom_u32_max(PAGE_SIZE);
  202. return sp & ~0xf;
  203. }
  204. static inline unsigned long brk_rnd(void)
  205. {
  206. return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
  207. }
  208. unsigned long arch_randomize_brk(struct mm_struct *mm)
  209. {
  210. unsigned long ret;
  211. ret = PAGE_ALIGN(mm->brk + brk_rnd());
  212. return (ret > mm->brk) ? ret : mm->brk;
  213. }