process.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/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_DEFINE0(clone)
  212. {
  213. struct pt_regs *regs = task_pt_regs(current);
  214. unsigned long clone_flags;
  215. unsigned long newsp;
  216. int __user *parent_tidptr, *child_tidptr;
  217. clone_flags = regs->gprs[3];
  218. newsp = regs->orig_gpr2;
  219. parent_tidptr = (int __user *) regs->gprs[4];
  220. child_tidptr = (int __user *) regs->gprs[5];
  221. if (!newsp)
  222. newsp = regs->gprs[15];
  223. return do_fork(clone_flags, newsp, regs, 0,
  224. parent_tidptr, child_tidptr);
  225. }
  226. /*
  227. * This is trivial, and on the face of it looks like it
  228. * could equally well be done in user mode.
  229. *
  230. * Not so, for quite unobvious reasons - register pressure.
  231. * In user mode vfork() cannot have a stack frame, and if
  232. * done by calling the "clone()" system call directly, you
  233. * do not have enough call-clobbered registers to hold all
  234. * the information you need.
  235. */
  236. SYSCALL_DEFINE0(vfork)
  237. {
  238. struct pt_regs *regs = task_pt_regs(current);
  239. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  240. regs->gprs[15], regs, 0, NULL, NULL);
  241. }
  242. asmlinkage void execve_tail(void)
  243. {
  244. current->thread.fp_regs.fpc = 0;
  245. if (MACHINE_HAS_IEEE)
  246. asm volatile("sfpc %0,%0" : : "d" (0));
  247. }
  248. /*
  249. * sys_execve() executes a new program.
  250. */
  251. SYSCALL_DEFINE0(execve)
  252. {
  253. struct pt_regs *regs = task_pt_regs(current);
  254. char *filename;
  255. unsigned long result;
  256. int rc;
  257. filename = getname((char __user *) regs->orig_gpr2);
  258. if (IS_ERR(filename)) {
  259. result = PTR_ERR(filename);
  260. goto out;
  261. }
  262. rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
  263. (char __user * __user *) regs->gprs[4], regs);
  264. if (rc) {
  265. result = rc;
  266. goto out_putname;
  267. }
  268. execve_tail();
  269. result = regs->gprs[2];
  270. out_putname:
  271. putname(filename);
  272. out:
  273. return result;
  274. }
  275. /*
  276. * fill in the FPU structure for a core dump.
  277. */
  278. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  279. {
  280. #ifndef CONFIG_64BIT
  281. /*
  282. * save fprs to current->thread.fp_regs to merge them with
  283. * the emulated registers and then copy the result to the dump.
  284. */
  285. save_fp_regs(&current->thread.fp_regs);
  286. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  287. #else /* CONFIG_64BIT */
  288. save_fp_regs(fpregs);
  289. #endif /* CONFIG_64BIT */
  290. return 1;
  291. }
  292. EXPORT_SYMBOL(dump_fpu);
  293. unsigned long get_wchan(struct task_struct *p)
  294. {
  295. struct stack_frame *sf, *low, *high;
  296. unsigned long return_address;
  297. int count;
  298. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  299. return 0;
  300. low = task_stack_page(p);
  301. high = (struct stack_frame *) task_pt_regs(p);
  302. sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
  303. if (sf <= low || sf > high)
  304. return 0;
  305. for (count = 0; count < 16; count++) {
  306. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  307. if (sf <= low || sf > high)
  308. return 0;
  309. return_address = sf->gprs[8] & PSW_ADDR_INSN;
  310. if (!in_sched_functions(return_address))
  311. return return_address;
  312. }
  313. return 0;
  314. }