process.c 9.4 KB

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