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