process.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/irq.h>
  29. #include <asm/nmi.h>
  30. #include <asm/smp.h>
  31. #include <asm/switch_to.h>
  32. #include <asm/runtime_instr.h>
  33. #include "entry.h"
  34. asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
  35. /*
  36. * Return saved PC of a blocked thread. used in kernel/sched.
  37. * resume in entry.S does not create a new stack frame, it
  38. * just stores the registers %r6-%r15 to the frame given by
  39. * schedule. We want to return the address of the caller of
  40. * schedule, so we have to walk the backchain one time to
  41. * find the frame schedule() store its return address.
  42. */
  43. unsigned long thread_saved_pc(struct task_struct *tsk)
  44. {
  45. struct stack_frame *sf, *low, *high;
  46. if (!tsk || !task_stack_page(tsk))
  47. return 0;
  48. low = task_stack_page(tsk);
  49. high = (struct stack_frame *) task_pt_regs(tsk);
  50. sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
  51. if (sf <= low || sf > high)
  52. return 0;
  53. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  54. if (sf <= low || sf > high)
  55. return 0;
  56. return sf->gprs[8];
  57. }
  58. /*
  59. * The idle loop on a S390...
  60. */
  61. static void default_idle(void)
  62. {
  63. if (cpu_is_offline(smp_processor_id()))
  64. cpu_die();
  65. local_irq_disable();
  66. if (need_resched()) {
  67. local_irq_enable();
  68. return;
  69. }
  70. local_mcck_disable();
  71. if (test_thread_flag(TIF_MCCK_PENDING)) {
  72. local_mcck_enable();
  73. local_irq_enable();
  74. return;
  75. }
  76. /* Halt the cpu and keep track of cpu time accounting. */
  77. vtime_stop_cpu();
  78. }
  79. void cpu_idle(void)
  80. {
  81. for (;;) {
  82. tick_nohz_idle_enter();
  83. rcu_idle_enter();
  84. while (!need_resched() && !test_thread_flag(TIF_MCCK_PENDING))
  85. default_idle();
  86. rcu_idle_exit();
  87. tick_nohz_idle_exit();
  88. if (test_thread_flag(TIF_MCCK_PENDING))
  89. s390_handle_mcck();
  90. schedule_preempt_disabled();
  91. }
  92. }
  93. extern void __kprobes kernel_thread_starter(void);
  94. asm(
  95. ".section .kprobes.text, \"ax\"\n"
  96. ".global kernel_thread_starter\n"
  97. "kernel_thread_starter:\n"
  98. " la 2,0(10)\n"
  99. " basr 14,9\n"
  100. " la 2,0\n"
  101. " br 11\n"
  102. ".previous\n");
  103. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  104. {
  105. struct pt_regs regs;
  106. memset(&regs, 0, sizeof(regs));
  107. regs.psw.mask = psw_kernel_bits |
  108. PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  109. regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
  110. regs.gprs[9] = (unsigned long) fn;
  111. regs.gprs[10] = (unsigned long) arg;
  112. regs.gprs[11] = (unsigned long) do_exit;
  113. regs.orig_gpr2 = -1;
  114. /* Ok, create the new process.. */
  115. return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
  116. 0, &regs, 0, NULL, NULL);
  117. }
  118. EXPORT_SYMBOL(kernel_thread);
  119. /*
  120. * Free current thread data structures etc..
  121. */
  122. void exit_thread(void)
  123. {
  124. exit_thread_runtime_instr();
  125. }
  126. void flush_thread(void)
  127. {
  128. }
  129. void release_thread(struct task_struct *dead_task)
  130. {
  131. }
  132. int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
  133. unsigned long unused,
  134. struct task_struct *p, struct pt_regs *regs)
  135. {
  136. struct thread_info *ti;
  137. struct fake_frame
  138. {
  139. struct stack_frame sf;
  140. struct pt_regs childregs;
  141. } *frame;
  142. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  143. p->thread.ksp = (unsigned long) frame;
  144. /* Store access registers to kernel stack of new process. */
  145. frame->childregs = *regs;
  146. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  147. frame->childregs.gprs[15] = new_stackp;
  148. frame->sf.back_chain = 0;
  149. /* new return point is ret_from_fork */
  150. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  151. /* fake return stack for resume(), don't go back to schedule */
  152. frame->sf.gprs[9] = (unsigned long) frame;
  153. /* Save access registers to new thread structure. */
  154. save_access_regs(&p->thread.acrs[0]);
  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. /* start new process with ar4 pointing to the correct address space */
  184. p->thread.mm_segment = get_fs();
  185. /* Don't copy debug registers */
  186. memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
  187. memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
  188. clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
  189. clear_tsk_thread_flag(p, TIF_PER_TRAP);
  190. /* Initialize per thread user and system timer values */
  191. ti = task_thread_info(p);
  192. ti->user_timer = 0;
  193. ti->system_timer = 0;
  194. return 0;
  195. }
  196. SYSCALL_DEFINE0(fork)
  197. {
  198. struct pt_regs *regs = task_pt_regs(current);
  199. return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
  200. }
  201. SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
  202. int __user *, parent_tidptr, int __user *, child_tidptr)
  203. {
  204. struct pt_regs *regs = task_pt_regs(current);
  205. if (!newsp)
  206. newsp = regs->gprs[15];
  207. return do_fork(clone_flags, newsp, regs, 0,
  208. parent_tidptr, child_tidptr);
  209. }
  210. /*
  211. * This is trivial, and on the face of it looks like it
  212. * could equally well be done in user mode.
  213. *
  214. * Not so, for quite unobvious reasons - register pressure.
  215. * In user mode vfork() cannot have a stack frame, and if
  216. * done by calling the "clone()" system call directly, you
  217. * do not have enough call-clobbered registers to hold all
  218. * the information you need.
  219. */
  220. SYSCALL_DEFINE0(vfork)
  221. {
  222. struct pt_regs *regs = task_pt_regs(current);
  223. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  224. regs->gprs[15], regs, 0, NULL, NULL);
  225. }
  226. asmlinkage void execve_tail(void)
  227. {
  228. current->thread.fp_regs.fpc = 0;
  229. if (MACHINE_HAS_IEEE)
  230. asm volatile("sfpc %0,%0" : : "d" (0));
  231. }
  232. /*
  233. * sys_execve() executes a new program.
  234. */
  235. SYSCALL_DEFINE3(execve, const char __user *, name,
  236. const char __user *const __user *, argv,
  237. const char __user *const __user *, envp)
  238. {
  239. struct pt_regs *regs = task_pt_regs(current);
  240. char *filename;
  241. long rc;
  242. filename = getname(name);
  243. rc = PTR_ERR(filename);
  244. if (IS_ERR(filename))
  245. return rc;
  246. rc = do_execve(filename, argv, envp, regs);
  247. if (rc)
  248. goto out;
  249. execve_tail();
  250. rc = regs->gprs[2];
  251. out:
  252. putname(filename);
  253. return rc;
  254. }
  255. /*
  256. * fill in the FPU structure for a core dump.
  257. */
  258. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  259. {
  260. #ifndef CONFIG_64BIT
  261. /*
  262. * save fprs to current->thread.fp_regs to merge them with
  263. * the emulated registers and then copy the result to the dump.
  264. */
  265. save_fp_regs(&current->thread.fp_regs);
  266. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  267. #else /* CONFIG_64BIT */
  268. save_fp_regs(fpregs);
  269. #endif /* CONFIG_64BIT */
  270. return 1;
  271. }
  272. EXPORT_SYMBOL(dump_fpu);
  273. unsigned long get_wchan(struct task_struct *p)
  274. {
  275. struct stack_frame *sf, *low, *high;
  276. unsigned long return_address;
  277. int count;
  278. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  279. return 0;
  280. low = task_stack_page(p);
  281. high = (struct stack_frame *) task_pt_regs(p);
  282. sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
  283. if (sf <= low || sf > high)
  284. return 0;
  285. for (count = 0; count < 16; count++) {
  286. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  287. if (sf <= low || sf > high)
  288. return 0;
  289. return_address = sf->gprs[8] & PSW_ADDR_INSN;
  290. if (!in_sched_functions(return_address))
  291. return return_address;
  292. }
  293. return 0;
  294. }
  295. unsigned long arch_align_stack(unsigned long sp)
  296. {
  297. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  298. sp -= get_random_int() & ~PAGE_MASK;
  299. return sp & ~0xf;
  300. }
  301. static inline unsigned long brk_rnd(void)
  302. {
  303. /* 8MB for 32bit, 1GB for 64bit */
  304. if (is_32bit_task())
  305. return (get_random_int() & 0x7ffUL) << PAGE_SHIFT;
  306. else
  307. return (get_random_int() & 0x3ffffUL) << PAGE_SHIFT;
  308. }
  309. unsigned long arch_randomize_brk(struct mm_struct *mm)
  310. {
  311. unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
  312. if (ret < mm->brk)
  313. return mm->brk;
  314. return ret;
  315. }
  316. unsigned long randomize_et_dyn(unsigned long base)
  317. {
  318. unsigned long ret = PAGE_ALIGN(base + brk_rnd());
  319. if (!(current->flags & PF_RANDOMIZE))
  320. return base;
  321. if (ret < base)
  322. return base;
  323. return ret;
  324. }