process.c 6.0 KB

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