process.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <asm/cpu_mf.h>
  32. #include <asm/io.h>
  33. #include <asm/processor.h>
  34. #include <asm/vtimer.h>
  35. #include <asm/exec.h>
  36. #include <asm/irq.h>
  37. #include <asm/nmi.h>
  38. #include <asm/smp.h>
  39. #include <asm/switch_to.h>
  40. #include <asm/runtime_instr.h>
  41. #include "entry.h"
  42. asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
  43. extern void kernel_thread_starter(void);
  44. void flush_thread(void)
  45. {
  46. }
  47. void arch_setup_new_exec(void)
  48. {
  49. if (S390_lowcore.current_pid != current->pid) {
  50. S390_lowcore.current_pid = current->pid;
  51. if (test_facility(40))
  52. lpp(&S390_lowcore.lpp);
  53. }
  54. }
  55. void arch_release_task_struct(struct task_struct *tsk)
  56. {
  57. runtime_instr_release(tsk);
  58. guarded_storage_release(tsk);
  59. }
  60. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  61. {
  62. /*
  63. * Save the floating-point or vector register state of the current
  64. * task and set the CIF_FPU flag to lazy restore the FPU register
  65. * state when returning to user space.
  66. */
  67. save_fpu_regs();
  68. memcpy(dst, src, arch_task_struct_size);
  69. dst->thread.fpu.regs = dst->thread.fpu.fprs;
  70. return 0;
  71. }
  72. int copy_thread_tls(unsigned long clone_flags, unsigned long new_stackp,
  73. unsigned long arg, struct task_struct *p, unsigned long tls)
  74. {
  75. struct fake_frame
  76. {
  77. struct stack_frame sf;
  78. struct pt_regs childregs;
  79. } *frame;
  80. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  81. p->thread.ksp = (unsigned long) frame;
  82. /* Save access registers to new thread structure. */
  83. save_access_regs(&p->thread.acrs[0]);
  84. /* start new process with ar4 pointing to the correct address space */
  85. p->thread.mm_segment = get_fs();
  86. /* Don't copy debug registers */
  87. memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
  88. memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
  89. clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
  90. p->thread.per_flags = 0;
  91. /* Initialize per thread user and system timer values */
  92. p->thread.user_timer = 0;
  93. p->thread.guest_timer = 0;
  94. p->thread.system_timer = 0;
  95. p->thread.hardirq_timer = 0;
  96. p->thread.softirq_timer = 0;
  97. frame->sf.back_chain = 0;
  98. /* new return point is ret_from_fork */
  99. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  100. /* fake return stack for resume(), don't go back to schedule */
  101. frame->sf.gprs[9] = (unsigned long) frame;
  102. /* Store access registers to kernel stack of new process. */
  103. if (unlikely(p->flags & PF_KTHREAD)) {
  104. /* kernel thread */
  105. memset(&frame->childregs, 0, sizeof(struct pt_regs));
  106. frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
  107. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  108. frame->childregs.psw.addr =
  109. (unsigned long) kernel_thread_starter;
  110. frame->childregs.gprs[9] = new_stackp; /* function */
  111. frame->childregs.gprs[10] = arg;
  112. frame->childregs.gprs[11] = (unsigned long) do_exit;
  113. frame->childregs.orig_gpr2 = -1;
  114. return 0;
  115. }
  116. frame->childregs = *current_pt_regs();
  117. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  118. frame->childregs.flags = 0;
  119. if (new_stackp)
  120. frame->childregs.gprs[15] = new_stackp;
  121. /* Don't copy runtime instrumentation info */
  122. p->thread.ri_cb = NULL;
  123. frame->childregs.psw.mask &= ~PSW_MASK_RI;
  124. /* Don't copy guarded storage control block */
  125. p->thread.gs_cb = NULL;
  126. p->thread.gs_bc_cb = NULL;
  127. /* Set a new TLS ? */
  128. if (clone_flags & CLONE_SETTLS) {
  129. if (is_compat_task()) {
  130. p->thread.acrs[0] = (unsigned int)tls;
  131. } else {
  132. p->thread.acrs[0] = (unsigned int)(tls >> 32);
  133. p->thread.acrs[1] = (unsigned int)tls;
  134. }
  135. }
  136. return 0;
  137. }
  138. asmlinkage void execve_tail(void)
  139. {
  140. current->thread.fpu.fpc = 0;
  141. asm volatile("sfpc %0" : : "d" (0));
  142. }
  143. /*
  144. * fill in the FPU structure for a core dump.
  145. */
  146. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  147. {
  148. save_fpu_regs();
  149. fpregs->fpc = current->thread.fpu.fpc;
  150. fpregs->pad = 0;
  151. if (MACHINE_HAS_VX)
  152. convert_vx_to_fp((freg_t *)&fpregs->fprs,
  153. current->thread.fpu.vxrs);
  154. else
  155. memcpy(&fpregs->fprs, current->thread.fpu.fprs,
  156. sizeof(fpregs->fprs));
  157. return 1;
  158. }
  159. EXPORT_SYMBOL(dump_fpu);
  160. unsigned long get_wchan(struct task_struct *p)
  161. {
  162. struct stack_frame *sf, *low, *high;
  163. unsigned long return_address;
  164. int count;
  165. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  166. return 0;
  167. if (!try_get_task_stack(p))
  168. return 0;
  169. low = task_stack_page(p);
  170. high = (struct stack_frame *) task_pt_regs(p);
  171. sf = (struct stack_frame *) p->thread.ksp;
  172. if (sf <= low || sf > high) {
  173. return_address = 0;
  174. goto out;
  175. }
  176. for (count = 0; count < 16; count++) {
  177. sf = (struct stack_frame *) sf->back_chain;
  178. if (sf <= low || sf > high) {
  179. return_address = 0;
  180. goto out;
  181. }
  182. return_address = sf->gprs[8];
  183. if (!in_sched_functions(return_address))
  184. goto out;
  185. }
  186. out:
  187. put_task_stack(p);
  188. return return_address;
  189. }
  190. unsigned long arch_align_stack(unsigned long sp)
  191. {
  192. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  193. sp -= get_random_int() & ~PAGE_MASK;
  194. return sp & ~0xf;
  195. }
  196. static inline unsigned long brk_rnd(void)
  197. {
  198. return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
  199. }
  200. unsigned long arch_randomize_brk(struct mm_struct *mm)
  201. {
  202. unsigned long ret;
  203. ret = PAGE_ALIGN(mm->brk + brk_rnd());
  204. return (ret > mm->brk) ? ret : mm->brk;
  205. }
  206. void set_fs_fixup(void)
  207. {
  208. struct pt_regs *regs = current_pt_regs();
  209. static bool warned;
  210. set_fs(USER_DS);
  211. if (warned)
  212. return;
  213. WARN(1, "Unbalanced set_fs - int code: 0x%x\n", regs->int_code);
  214. show_registers(regs);
  215. warned = true;
  216. }