process.c 8.4 KB

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