process.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. }
  144. void release_thread(struct task_struct *dead_task)
  145. {
  146. }
  147. int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
  148. unsigned long unused,
  149. struct task_struct *p, struct pt_regs *regs)
  150. {
  151. struct thread_info *ti;
  152. struct fake_frame
  153. {
  154. struct stack_frame sf;
  155. struct pt_regs childregs;
  156. } *frame;
  157. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  158. p->thread.ksp = (unsigned long) frame;
  159. /* Store access registers to kernel stack of new process. */
  160. frame->childregs = *regs;
  161. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  162. frame->childregs.gprs[15] = new_stackp;
  163. frame->sf.back_chain = 0;
  164. /* new return point is ret_from_fork */
  165. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  166. /* fake return stack for resume(), don't go back to schedule */
  167. frame->sf.gprs[9] = (unsigned long) frame;
  168. /* Save access registers to new thread structure. */
  169. save_access_regs(&p->thread.acrs[0]);
  170. #ifndef CONFIG_64BIT
  171. /*
  172. * save fprs to current->thread.fp_regs to merge them with
  173. * the emulated registers and then copy the result to the child.
  174. */
  175. save_fp_regs(&current->thread.fp_regs);
  176. memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
  177. sizeof(s390_fp_regs));
  178. /* Set a new TLS ? */
  179. if (clone_flags & CLONE_SETTLS)
  180. p->thread.acrs[0] = regs->gprs[6];
  181. #else /* CONFIG_64BIT */
  182. /* Save the fpu registers to new thread structure. */
  183. save_fp_regs(&p->thread.fp_regs);
  184. /* Set a new TLS ? */
  185. if (clone_flags & CLONE_SETTLS) {
  186. if (is_compat_task()) {
  187. p->thread.acrs[0] = (unsigned int) regs->gprs[6];
  188. } else {
  189. p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
  190. p->thread.acrs[1] = (unsigned int) regs->gprs[6];
  191. }
  192. }
  193. #endif /* CONFIG_64BIT */
  194. /* start new process with ar4 pointing to the correct address space */
  195. p->thread.mm_segment = get_fs();
  196. /* Don't copy debug registers */
  197. memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
  198. clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
  199. /* Initialize per thread user and system timer values */
  200. ti = task_thread_info(p);
  201. ti->user_timer = 0;
  202. ti->system_timer = 0;
  203. return 0;
  204. }
  205. SYSCALL_DEFINE0(fork)
  206. {
  207. struct pt_regs *regs = task_pt_regs(current);
  208. return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
  209. }
  210. SYSCALL_DEFINE4(clone, unsigned long, newsp, unsigned long, clone_flags,
  211. int __user *, parent_tidptr, int __user *, child_tidptr)
  212. {
  213. struct pt_regs *regs = task_pt_regs(current);
  214. if (!newsp)
  215. newsp = regs->gprs[15];
  216. return do_fork(clone_flags, newsp, regs, 0,
  217. parent_tidptr, child_tidptr);
  218. }
  219. /*
  220. * This is trivial, and on the face of it looks like it
  221. * could equally well be done in user mode.
  222. *
  223. * Not so, for quite unobvious reasons - register pressure.
  224. * In user mode vfork() cannot have a stack frame, and if
  225. * done by calling the "clone()" system call directly, you
  226. * do not have enough call-clobbered registers to hold all
  227. * the information you need.
  228. */
  229. SYSCALL_DEFINE0(vfork)
  230. {
  231. struct pt_regs *regs = task_pt_regs(current);
  232. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  233. regs->gprs[15], regs, 0, NULL, NULL);
  234. }
  235. asmlinkage void execve_tail(void)
  236. {
  237. current->thread.fp_regs.fpc = 0;
  238. if (MACHINE_HAS_IEEE)
  239. asm volatile("sfpc %0,%0" : : "d" (0));
  240. }
  241. /*
  242. * sys_execve() executes a new program.
  243. */
  244. SYSCALL_DEFINE3(execve, char __user *, name, char __user * __user *, argv,
  245. char __user * __user *, envp)
  246. {
  247. struct pt_regs *regs = task_pt_regs(current);
  248. char *filename;
  249. long rc;
  250. filename = getname(name);
  251. rc = PTR_ERR(filename);
  252. if (IS_ERR(filename))
  253. return rc;
  254. rc = do_execve(filename, argv, envp, regs);
  255. if (rc)
  256. goto out;
  257. execve_tail();
  258. rc = regs->gprs[2];
  259. out:
  260. putname(filename);
  261. return rc;
  262. }
  263. /*
  264. * fill in the FPU structure for a core dump.
  265. */
  266. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  267. {
  268. #ifndef CONFIG_64BIT
  269. /*
  270. * save fprs to current->thread.fp_regs to merge them with
  271. * the emulated registers and then copy the result to the dump.
  272. */
  273. save_fp_regs(&current->thread.fp_regs);
  274. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  275. #else /* CONFIG_64BIT */
  276. save_fp_regs(fpregs);
  277. #endif /* CONFIG_64BIT */
  278. return 1;
  279. }
  280. EXPORT_SYMBOL(dump_fpu);
  281. unsigned long get_wchan(struct task_struct *p)
  282. {
  283. struct stack_frame *sf, *low, *high;
  284. unsigned long return_address;
  285. int count;
  286. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  287. return 0;
  288. low = task_stack_page(p);
  289. high = (struct stack_frame *) task_pt_regs(p);
  290. sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
  291. if (sf <= low || sf > high)
  292. return 0;
  293. for (count = 0; count < 16; count++) {
  294. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  295. if (sf <= low || sf > high)
  296. return 0;
  297. return_address = sf->gprs[8] & PSW_ADDR_INSN;
  298. if (!in_sched_functions(return_address))
  299. return return_address;
  300. }
  301. return 0;
  302. }