process.c 5.8 KB

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