process.c 6.9 KB

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