process.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /*
  60. * The idle loop on a S390...
  61. */
  62. static void default_idle(void)
  63. {
  64. if (cpu_is_offline(smp_processor_id()))
  65. cpu_die();
  66. local_irq_disable();
  67. if (need_resched()) {
  68. local_irq_enable();
  69. return;
  70. }
  71. local_mcck_disable();
  72. if (test_thread_flag(TIF_MCCK_PENDING)) {
  73. local_mcck_enable();
  74. local_irq_enable();
  75. return;
  76. }
  77. /* Halt the cpu and keep track of cpu time accounting. */
  78. vtime_stop_cpu();
  79. }
  80. void cpu_idle(void)
  81. {
  82. for (;;) {
  83. tick_nohz_idle_enter();
  84. rcu_idle_enter();
  85. while (!need_resched() && !test_thread_flag(TIF_MCCK_PENDING))
  86. default_idle();
  87. rcu_idle_exit();
  88. tick_nohz_idle_exit();
  89. if (test_thread_flag(TIF_MCCK_PENDING))
  90. s390_handle_mcck();
  91. schedule_preempt_disabled();
  92. }
  93. }
  94. extern void __kprobes kernel_thread_starter(void);
  95. /*
  96. * Free current thread data structures etc..
  97. */
  98. void exit_thread(void)
  99. {
  100. exit_thread_runtime_instr();
  101. }
  102. void flush_thread(void)
  103. {
  104. }
  105. void release_thread(struct task_struct *dead_task)
  106. {
  107. }
  108. int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
  109. unsigned long arg,
  110. struct task_struct *p, struct pt_regs *regs)
  111. {
  112. struct thread_info *ti;
  113. struct fake_frame
  114. {
  115. struct stack_frame sf;
  116. struct pt_regs childregs;
  117. } *frame;
  118. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  119. p->thread.ksp = (unsigned long) frame;
  120. /* Save access registers to new thread structure. */
  121. save_access_regs(&p->thread.acrs[0]);
  122. /* start new process with ar4 pointing to the correct address space */
  123. p->thread.mm_segment = get_fs();
  124. /* Don't copy debug registers */
  125. memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
  126. memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
  127. clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
  128. clear_tsk_thread_flag(p, TIF_PER_TRAP);
  129. /* Initialize per thread user and system timer values */
  130. ti = task_thread_info(p);
  131. ti->user_timer = 0;
  132. ti->system_timer = 0;
  133. frame->sf.back_chain = 0;
  134. /* new return point is ret_from_fork */
  135. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  136. /* fake return stack for resume(), don't go back to schedule */
  137. frame->sf.gprs[9] = (unsigned long) frame;
  138. /* Store access registers to kernel stack of new process. */
  139. if (unlikely(!regs)) {
  140. /* kernel thread */
  141. memset(&frame->childregs, 0, sizeof(struct pt_regs));
  142. frame->childregs.psw.mask = psw_kernel_bits | PSW_MASK_DAT |
  143. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  144. frame->childregs.psw.addr = PSW_ADDR_AMODE |
  145. (unsigned long) kernel_thread_starter;
  146. frame->childregs.gprs[9] = new_stackp; /* function */
  147. frame->childregs.gprs[10] = arg;
  148. frame->childregs.gprs[11] = (unsigned long) do_exit;
  149. frame->childregs.orig_gpr2 = -1;
  150. return 0;
  151. }
  152. frame->childregs = *regs;
  153. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  154. frame->childregs.gprs[15] = new_stackp;
  155. /* Don't copy runtime instrumentation info */
  156. p->thread.ri_cb = NULL;
  157. p->thread.ri_signum = 0;
  158. frame->childregs.psw.mask &= ~PSW_MASK_RI;
  159. #ifndef CONFIG_64BIT
  160. /*
  161. * save fprs to current->thread.fp_regs to merge them with
  162. * the emulated registers and then copy the result to the child.
  163. */
  164. save_fp_regs(&current->thread.fp_regs);
  165. memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
  166. sizeof(s390_fp_regs));
  167. /* Set a new TLS ? */
  168. if (clone_flags & CLONE_SETTLS)
  169. p->thread.acrs[0] = regs->gprs[6];
  170. #else /* CONFIG_64BIT */
  171. /* Save the fpu registers to new thread structure. */
  172. save_fp_regs(&p->thread.fp_regs);
  173. /* Set a new TLS ? */
  174. if (clone_flags & CLONE_SETTLS) {
  175. if (is_compat_task()) {
  176. p->thread.acrs[0] = (unsigned int) regs->gprs[6];
  177. } else {
  178. p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
  179. p->thread.acrs[1] = (unsigned int) regs->gprs[6];
  180. }
  181. }
  182. #endif /* CONFIG_64BIT */
  183. return 0;
  184. }
  185. SYSCALL_DEFINE0(fork)
  186. {
  187. struct pt_regs *regs = task_pt_regs(current);
  188. return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
  189. }
  190. SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
  191. int __user *, parent_tidptr, int __user *, child_tidptr)
  192. {
  193. struct pt_regs *regs = task_pt_regs(current);
  194. if (!newsp)
  195. newsp = regs->gprs[15];
  196. return do_fork(clone_flags, newsp, regs, 0,
  197. parent_tidptr, child_tidptr);
  198. }
  199. /*
  200. * This is trivial, and on the face of it looks like it
  201. * could equally well be done in user mode.
  202. *
  203. * Not so, for quite unobvious reasons - register pressure.
  204. * In user mode vfork() cannot have a stack frame, and if
  205. * done by calling the "clone()" system call directly, you
  206. * do not have enough call-clobbered registers to hold all
  207. * the information you need.
  208. */
  209. SYSCALL_DEFINE0(vfork)
  210. {
  211. struct pt_regs *regs = task_pt_regs(current);
  212. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  213. regs->gprs[15], regs, 0, NULL, NULL);
  214. }
  215. asmlinkage void execve_tail(void)
  216. {
  217. current->thread.fp_regs.fpc = 0;
  218. if (MACHINE_HAS_IEEE)
  219. asm volatile("sfpc %0,%0" : : "d" (0));
  220. }
  221. /*
  222. * fill in the FPU structure for a core dump.
  223. */
  224. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  225. {
  226. #ifndef CONFIG_64BIT
  227. /*
  228. * save fprs to current->thread.fp_regs to merge them with
  229. * the emulated registers and then copy the result to the dump.
  230. */
  231. save_fp_regs(&current->thread.fp_regs);
  232. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  233. #else /* CONFIG_64BIT */
  234. save_fp_regs(fpregs);
  235. #endif /* CONFIG_64BIT */
  236. return 1;
  237. }
  238. EXPORT_SYMBOL(dump_fpu);
  239. unsigned long get_wchan(struct task_struct *p)
  240. {
  241. struct stack_frame *sf, *low, *high;
  242. unsigned long return_address;
  243. int count;
  244. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  245. return 0;
  246. low = task_stack_page(p);
  247. high = (struct stack_frame *) task_pt_regs(p);
  248. sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
  249. if (sf <= low || sf > high)
  250. return 0;
  251. for (count = 0; count < 16; count++) {
  252. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  253. if (sf <= low || sf > high)
  254. return 0;
  255. return_address = sf->gprs[8] & PSW_ADDR_INSN;
  256. if (!in_sched_functions(return_address))
  257. return return_address;
  258. }
  259. return 0;
  260. }
  261. unsigned long arch_align_stack(unsigned long sp)
  262. {
  263. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  264. sp -= get_random_int() & ~PAGE_MASK;
  265. return sp & ~0xf;
  266. }
  267. static inline unsigned long brk_rnd(void)
  268. {
  269. /* 8MB for 32bit, 1GB for 64bit */
  270. if (is_32bit_task())
  271. return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
  272. else
  273. return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
  274. }
  275. unsigned long arch_randomize_brk(struct mm_struct *mm)
  276. {
  277. unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
  278. if (ret < mm->brk)
  279. return mm->brk;
  280. return ret;
  281. }
  282. unsigned long randomize_et_dyn(unsigned long base)
  283. {
  284. unsigned long ret = PAGE_ALIGN(base + brk_rnd());
  285. if (!(current->flags & PF_RANDOMIZE))
  286. return base;
  287. if (ret < base)
  288. return base;
  289. return ret;
  290. }