process.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * This file handles the architecture dependent parts of process handling.
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  6. * Hartmut Penner <hp@de.ibm.com>,
  7. * Denis Joseph Barrow,
  8. */
  9. #include <linux/elf-randomize.h>
  10. #include <linux/compiler.h>
  11. #include <linux/cpu.h>
  12. #include <linux/sched.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/sched/task.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/elfcore.h>
  19. #include <linux/smp.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/tick.h>
  23. #include <linux/personality.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/compat.h>
  26. #include <linux/kprobes.h>
  27. #include <linux/random.h>
  28. #include <linux/export.h>
  29. #include <linux/init_task.h>
  30. #include <asm/io.h>
  31. #include <asm/processor.h>
  32. #include <asm/vtimer.h>
  33. #include <asm/exec.h>
  34. #include <asm/irq.h>
  35. #include <asm/nmi.h>
  36. #include <asm/smp.h>
  37. #include <asm/switch_to.h>
  38. #include <asm/runtime_instr.h>
  39. #include "entry.h"
  40. asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
  41. /*
  42. * Return saved PC of a blocked thread. used in kernel/sched.
  43. * resume in entry.S does not create a new stack frame, it
  44. * just stores the registers %r6-%r15 to the frame given by
  45. * schedule. We want to return the address of the caller of
  46. * schedule, so we have to walk the backchain one time to
  47. * find the frame schedule() store its return address.
  48. */
  49. unsigned long thread_saved_pc(struct task_struct *tsk)
  50. {
  51. struct stack_frame *sf, *low, *high;
  52. if (!tsk || !task_stack_page(tsk))
  53. return 0;
  54. low = task_stack_page(tsk);
  55. high = (struct stack_frame *) task_pt_regs(tsk);
  56. sf = (struct stack_frame *) tsk->thread.ksp;
  57. if (sf <= low || sf > high)
  58. return 0;
  59. sf = (struct stack_frame *) sf->back_chain;
  60. if (sf <= low || sf > high)
  61. return 0;
  62. return sf->gprs[8];
  63. }
  64. extern void kernel_thread_starter(void);
  65. /*
  66. * Free current thread data structures etc..
  67. */
  68. void exit_thread(struct task_struct *tsk)
  69. {
  70. if (tsk == current) {
  71. exit_thread_runtime_instr();
  72. exit_thread_gs();
  73. }
  74. }
  75. void flush_thread(void)
  76. {
  77. }
  78. void release_thread(struct task_struct *dead_task)
  79. {
  80. }
  81. void arch_release_task_struct(struct task_struct *tsk)
  82. {
  83. }
  84. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  85. {
  86. /*
  87. * Save the floating-point or vector register state of the current
  88. * task and set the CIF_FPU flag to lazy restore the FPU register
  89. * state when returning to user space.
  90. */
  91. save_fpu_regs();
  92. memcpy(dst, src, arch_task_struct_size);
  93. dst->thread.fpu.regs = dst->thread.fpu.fprs;
  94. return 0;
  95. }
  96. int copy_thread_tls(unsigned long clone_flags, unsigned long new_stackp,
  97. unsigned long arg, struct task_struct *p, unsigned long tls)
  98. {
  99. struct fake_frame
  100. {
  101. struct stack_frame sf;
  102. struct pt_regs childregs;
  103. } *frame;
  104. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  105. p->thread.ksp = (unsigned long) frame;
  106. /* Save access registers to new thread structure. */
  107. save_access_regs(&p->thread.acrs[0]);
  108. /* start new process with ar4 pointing to the correct address space */
  109. p->thread.mm_segment = get_fs();
  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. /* Initialize per thread user and system timer values */
  115. p->thread.user_timer = 0;
  116. p->thread.guest_timer = 0;
  117. p->thread.system_timer = 0;
  118. p->thread.hardirq_timer = 0;
  119. p->thread.softirq_timer = 0;
  120. frame->sf.back_chain = 0;
  121. /* new return point is ret_from_fork */
  122. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  123. /* fake return stack for resume(), don't go back to schedule */
  124. frame->sf.gprs[9] = (unsigned long) frame;
  125. /* Store access registers to kernel stack of new process. */
  126. if (unlikely(p->flags & PF_KTHREAD)) {
  127. /* kernel thread */
  128. memset(&frame->childregs, 0, sizeof(struct pt_regs));
  129. frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
  130. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  131. frame->childregs.psw.addr =
  132. (unsigned long) kernel_thread_starter;
  133. frame->childregs.gprs[9] = new_stackp; /* function */
  134. frame->childregs.gprs[10] = arg;
  135. frame->childregs.gprs[11] = (unsigned long) do_exit;
  136. frame->childregs.orig_gpr2 = -1;
  137. return 0;
  138. }
  139. frame->childregs = *current_pt_regs();
  140. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  141. frame->childregs.flags = 0;
  142. if (new_stackp)
  143. frame->childregs.gprs[15] = new_stackp;
  144. /* Don't copy runtime instrumentation info */
  145. p->thread.ri_cb = NULL;
  146. frame->childregs.psw.mask &= ~PSW_MASK_RI;
  147. /* Don't copy guarded storage control block */
  148. p->thread.gs_cb = NULL;
  149. p->thread.gs_bc_cb = NULL;
  150. /* Set a new TLS ? */
  151. if (clone_flags & CLONE_SETTLS) {
  152. if (is_compat_task()) {
  153. p->thread.acrs[0] = (unsigned int)tls;
  154. } else {
  155. p->thread.acrs[0] = (unsigned int)(tls >> 32);
  156. p->thread.acrs[1] = (unsigned int)tls;
  157. }
  158. }
  159. return 0;
  160. }
  161. asmlinkage void execve_tail(void)
  162. {
  163. current->thread.fpu.fpc = 0;
  164. asm volatile("sfpc %0" : : "d" (0));
  165. }
  166. /*
  167. * fill in the FPU structure for a core dump.
  168. */
  169. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  170. {
  171. save_fpu_regs();
  172. fpregs->fpc = current->thread.fpu.fpc;
  173. fpregs->pad = 0;
  174. if (MACHINE_HAS_VX)
  175. convert_vx_to_fp((freg_t *)&fpregs->fprs,
  176. current->thread.fpu.vxrs);
  177. else
  178. memcpy(&fpregs->fprs, current->thread.fpu.fprs,
  179. sizeof(fpregs->fprs));
  180. return 1;
  181. }
  182. EXPORT_SYMBOL(dump_fpu);
  183. unsigned long get_wchan(struct task_struct *p)
  184. {
  185. struct stack_frame *sf, *low, *high;
  186. unsigned long return_address;
  187. int count;
  188. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  189. return 0;
  190. low = task_stack_page(p);
  191. high = (struct stack_frame *) task_pt_regs(p);
  192. sf = (struct stack_frame *) p->thread.ksp;
  193. if (sf <= low || sf > high)
  194. return 0;
  195. for (count = 0; count < 16; count++) {
  196. sf = (struct stack_frame *) sf->back_chain;
  197. if (sf <= low || sf > high)
  198. return 0;
  199. return_address = sf->gprs[8];
  200. if (!in_sched_functions(return_address))
  201. return return_address;
  202. }
  203. return 0;
  204. }
  205. unsigned long arch_align_stack(unsigned long sp)
  206. {
  207. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  208. sp -= get_random_int() & ~PAGE_MASK;
  209. return sp & ~0xf;
  210. }
  211. static inline unsigned long brk_rnd(void)
  212. {
  213. return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
  214. }
  215. unsigned long arch_randomize_brk(struct mm_struct *mm)
  216. {
  217. unsigned long ret;
  218. ret = PAGE_ALIGN(mm->brk + brk_rnd());
  219. return (ret > mm->brk) ? ret : mm->brk;
  220. }
  221. void set_fs_fixup(void)
  222. {
  223. struct pt_regs *regs = current_pt_regs();
  224. static bool warned;
  225. set_fs(USER_DS);
  226. if (warned)
  227. return;
  228. WARN(1, "Unbalanced set_fs - int code: 0x%x\n", regs->int_code);
  229. show_registers(regs);
  230. warned = true;
  231. }