process.c 9.0 KB

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