process.c 8.7 KB

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