process.c 8.4 KB

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