process.c 6.2 KB

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