process.c 8.7 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/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 "entry.h"
  42. asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
  43. /*
  44. * Return saved PC of a blocked thread. used in kernel/sched.
  45. * resume in entry.S does not create a new stack frame, it
  46. * just stores the registers %r6-%r15 to the frame given by
  47. * schedule. We want to return the address of the caller of
  48. * schedule, so we have to walk the backchain one time to
  49. * find the frame schedule() store its return address.
  50. */
  51. unsigned long thread_saved_pc(struct task_struct *tsk)
  52. {
  53. struct stack_frame *sf, *low, *high;
  54. if (!tsk || !task_stack_page(tsk))
  55. return 0;
  56. low = task_stack_page(tsk);
  57. high = (struct stack_frame *) task_pt_regs(tsk);
  58. sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
  59. if (sf <= low || sf > high)
  60. return 0;
  61. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  62. if (sf <= low || sf > high)
  63. return 0;
  64. return sf->gprs[8];
  65. }
  66. extern void s390_handle_mcck(void);
  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. /*
  134. * Free current thread data structures etc..
  135. */
  136. void exit_thread(void)
  137. {
  138. }
  139. void flush_thread(void)
  140. {
  141. clear_used_math();
  142. clear_tsk_thread_flag(current, TIF_USEDFPU);
  143. }
  144. void release_thread(struct task_struct *dead_task)
  145. {
  146. }
  147. int copy_thread(int nr, 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 (test_thread_flag(TIF_31BIT)) {
  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. /* Initialize per thread user and system timer values */
  199. ti = task_thread_info(p);
  200. ti->user_timer = 0;
  201. ti->system_timer = 0;
  202. return 0;
  203. }
  204. SYSCALL_DEFINE0(fork)
  205. {
  206. struct pt_regs *regs = task_pt_regs(current);
  207. return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
  208. }
  209. SYSCALL_DEFINE0(clone)
  210. {
  211. struct pt_regs *regs = task_pt_regs(current);
  212. unsigned long clone_flags;
  213. unsigned long newsp;
  214. int __user *parent_tidptr, *child_tidptr;
  215. clone_flags = regs->gprs[3];
  216. newsp = regs->orig_gpr2;
  217. parent_tidptr = (int __user *) regs->gprs[4];
  218. child_tidptr = (int __user *) regs->gprs[5];
  219. if (!newsp)
  220. newsp = regs->gprs[15];
  221. return do_fork(clone_flags, newsp, regs, 0,
  222. parent_tidptr, child_tidptr);
  223. }
  224. /*
  225. * This is trivial, and on the face of it looks like it
  226. * could equally well be done in user mode.
  227. *
  228. * Not so, for quite unobvious reasons - register pressure.
  229. * In user mode vfork() cannot have a stack frame, and if
  230. * done by calling the "clone()" system call directly, you
  231. * do not have enough call-clobbered registers to hold all
  232. * the information you need.
  233. */
  234. SYSCALL_DEFINE0(vfork)
  235. {
  236. struct pt_regs *regs = task_pt_regs(current);
  237. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  238. regs->gprs[15], regs, 0, NULL, NULL);
  239. }
  240. asmlinkage void execve_tail(void)
  241. {
  242. task_lock(current);
  243. current->ptrace &= ~PT_DTRACE;
  244. task_unlock(current);
  245. current->thread.fp_regs.fpc = 0;
  246. if (MACHINE_HAS_IEEE)
  247. asm volatile("sfpc %0,%0" : : "d" (0));
  248. }
  249. /*
  250. * sys_execve() executes a new program.
  251. */
  252. SYSCALL_DEFINE0(execve)
  253. {
  254. struct pt_regs *regs = task_pt_regs(current);
  255. char *filename;
  256. unsigned long result;
  257. int rc;
  258. filename = getname((char __user *) regs->orig_gpr2);
  259. if (IS_ERR(filename)) {
  260. result = PTR_ERR(filename);
  261. goto out;
  262. }
  263. rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
  264. (char __user * __user *) regs->gprs[4], regs);
  265. if (rc) {
  266. result = rc;
  267. goto out_putname;
  268. }
  269. execve_tail();
  270. result = regs->gprs[2];
  271. out_putname:
  272. putname(filename);
  273. out:
  274. return result;
  275. }
  276. /*
  277. * fill in the FPU structure for a core dump.
  278. */
  279. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  280. {
  281. #ifndef CONFIG_64BIT
  282. /*
  283. * save fprs to current->thread.fp_regs to merge them with
  284. * the emulated registers and then copy the result to the dump.
  285. */
  286. save_fp_regs(&current->thread.fp_regs);
  287. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  288. #else /* CONFIG_64BIT */
  289. save_fp_regs(fpregs);
  290. #endif /* CONFIG_64BIT */
  291. return 1;
  292. }
  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. }