process.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * arch/s390/kernel/process.c
  3. *
  4. * S390 version
  5. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. * Hartmut Penner (hp@de.ibm.com),
  8. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  9. *
  10. * Derived from "arch/i386/kernel/process.c"
  11. * Copyright (C) 1995, Linus Torvalds
  12. */
  13. /*
  14. * This file handles the architecture-dependent parts of process handling..
  15. */
  16. #include <linux/config.h>
  17. #include <linux/compiler.h>
  18. #include <linux/cpu.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/user.h>
  31. #include <linux/a.out.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/delay.h>
  34. #include <linux/reboot.h>
  35. #include <linux/init.h>
  36. #include <linux/module.h>
  37. #include <linux/notifier.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/system.h>
  41. #include <asm/io.h>
  42. #include <asm/processor.h>
  43. #include <asm/irq.h>
  44. #include <asm/timer.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. * Need to know about CPUs going idle?
  71. */
  72. static struct notifier_block *idle_chain;
  73. int register_idle_notifier(struct notifier_block *nb)
  74. {
  75. return notifier_chain_register(&idle_chain, nb);
  76. }
  77. EXPORT_SYMBOL(register_idle_notifier);
  78. int unregister_idle_notifier(struct notifier_block *nb)
  79. {
  80. return notifier_chain_unregister(&idle_chain, nb);
  81. }
  82. EXPORT_SYMBOL(unregister_idle_notifier);
  83. void do_monitor_call(struct pt_regs *regs, long interruption_code)
  84. {
  85. /* disable monitor call class 0 */
  86. __ctl_clear_bit(8, 15);
  87. notifier_call_chain(&idle_chain, CPU_NOT_IDLE,
  88. (void *)(long) smp_processor_id());
  89. }
  90. extern void s390_handle_mcck(void);
  91. /*
  92. * The idle loop on a S390...
  93. */
  94. void default_idle(void)
  95. {
  96. int cpu, rc;
  97. /* CPU is going idle. */
  98. cpu = smp_processor_id();
  99. local_irq_disable();
  100. if (need_resched()) {
  101. local_irq_enable();
  102. return;
  103. }
  104. rc = notifier_call_chain(&idle_chain, CPU_IDLE, (void *)(long) cpu);
  105. if (rc != NOTIFY_OK && rc != NOTIFY_DONE)
  106. BUG();
  107. if (rc != NOTIFY_OK) {
  108. local_irq_enable();
  109. return;
  110. }
  111. /* enable monitor call class 0 */
  112. __ctl_set_bit(8, 15);
  113. #ifdef CONFIG_HOTPLUG_CPU
  114. if (cpu_is_offline(cpu))
  115. cpu_die();
  116. #endif
  117. local_mcck_disable();
  118. if (test_thread_flag(TIF_MCCK_PENDING)) {
  119. local_mcck_enable();
  120. local_irq_enable();
  121. s390_handle_mcck();
  122. return;
  123. }
  124. /* Wait for external, I/O or machine check interrupt. */
  125. __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_WAIT |
  126. PSW_MASK_IO | PSW_MASK_EXT);
  127. }
  128. void cpu_idle(void)
  129. {
  130. for (;;) {
  131. while (!need_resched())
  132. default_idle();
  133. preempt_enable_no_resched();
  134. schedule();
  135. preempt_disable();
  136. }
  137. }
  138. void show_regs(struct pt_regs *regs)
  139. {
  140. struct task_struct *tsk = current;
  141. printk("CPU: %d %s\n", task_thread_info(tsk)->cpu, print_tainted());
  142. printk("Process %s (pid: %d, task: %p, ksp: %p)\n",
  143. current->comm, current->pid, (void *) tsk,
  144. (void *) tsk->thread.ksp);
  145. show_registers(regs);
  146. /* Show stack backtrace if pt_regs is from kernel mode */
  147. if (!(regs->psw.mask & PSW_MASK_PSTATE))
  148. show_trace(0,(unsigned long *) regs->gprs[15]);
  149. }
  150. extern void kernel_thread_starter(void);
  151. __asm__(".align 4\n"
  152. "kernel_thread_starter:\n"
  153. " la 2,0(10)\n"
  154. " basr 14,9\n"
  155. " la 2,0\n"
  156. " br 11\n");
  157. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  158. {
  159. struct pt_regs regs;
  160. memset(&regs, 0, sizeof(regs));
  161. regs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO | PSW_MASK_EXT;
  162. regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
  163. regs.gprs[9] = (unsigned long) fn;
  164. regs.gprs[10] = (unsigned long) arg;
  165. regs.gprs[11] = (unsigned long) do_exit;
  166. regs.orig_gpr2 = -1;
  167. /* Ok, create the new process.. */
  168. return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
  169. 0, &regs, 0, NULL, NULL);
  170. }
  171. /*
  172. * Free current thread data structures etc..
  173. */
  174. void exit_thread(void)
  175. {
  176. }
  177. void flush_thread(void)
  178. {
  179. clear_used_math();
  180. clear_tsk_thread_flag(current, TIF_USEDFPU);
  181. }
  182. void release_thread(struct task_struct *dead_task)
  183. {
  184. }
  185. int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
  186. unsigned long unused,
  187. struct task_struct * p, struct pt_regs * regs)
  188. {
  189. struct fake_frame
  190. {
  191. struct stack_frame sf;
  192. struct pt_regs childregs;
  193. } *frame;
  194. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  195. p->thread.ksp = (unsigned long) frame;
  196. /* Store access registers to kernel stack of new process. */
  197. frame->childregs = *regs;
  198. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  199. frame->childregs.gprs[15] = new_stackp;
  200. frame->sf.back_chain = 0;
  201. /* new return point is ret_from_fork */
  202. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  203. /* fake return stack for resume(), don't go back to schedule */
  204. frame->sf.gprs[9] = (unsigned long) frame;
  205. /* Save access registers to new thread structure. */
  206. save_access_regs(&p->thread.acrs[0]);
  207. #ifndef CONFIG_64BIT
  208. /*
  209. * save fprs to current->thread.fp_regs to merge them with
  210. * the emulated registers and then copy the result to the child.
  211. */
  212. save_fp_regs(&current->thread.fp_regs);
  213. memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
  214. sizeof(s390_fp_regs));
  215. p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _SEGMENT_TABLE;
  216. /* Set a new TLS ? */
  217. if (clone_flags & CLONE_SETTLS)
  218. p->thread.acrs[0] = regs->gprs[6];
  219. #else /* CONFIG_64BIT */
  220. /* Save the fpu registers to new thread structure. */
  221. save_fp_regs(&p->thread.fp_regs);
  222. p->thread.user_seg = __pa((unsigned long) p->mm->pgd) | _REGION_TABLE;
  223. /* Set a new TLS ? */
  224. if (clone_flags & CLONE_SETTLS) {
  225. if (test_thread_flag(TIF_31BIT)) {
  226. p->thread.acrs[0] = (unsigned int) regs->gprs[6];
  227. } else {
  228. p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
  229. p->thread.acrs[1] = (unsigned int) regs->gprs[6];
  230. }
  231. }
  232. #endif /* CONFIG_64BIT */
  233. /* start new process with ar4 pointing to the correct address space */
  234. p->thread.mm_segment = get_fs();
  235. /* Don't copy debug registers */
  236. memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
  237. return 0;
  238. }
  239. asmlinkage long sys_fork(struct pt_regs regs)
  240. {
  241. return do_fork(SIGCHLD, regs.gprs[15], &regs, 0, NULL, NULL);
  242. }
  243. asmlinkage long sys_clone(struct pt_regs regs)
  244. {
  245. unsigned long clone_flags;
  246. unsigned long newsp;
  247. int __user *parent_tidptr, *child_tidptr;
  248. clone_flags = regs.gprs[3];
  249. newsp = regs.orig_gpr2;
  250. parent_tidptr = (int __user *) regs.gprs[4];
  251. child_tidptr = (int __user *) regs.gprs[5];
  252. if (!newsp)
  253. newsp = regs.gprs[15];
  254. return do_fork(clone_flags, newsp, &regs, 0,
  255. parent_tidptr, child_tidptr);
  256. }
  257. /*
  258. * This is trivial, and on the face of it looks like it
  259. * could equally well be done in user mode.
  260. *
  261. * Not so, for quite unobvious reasons - register pressure.
  262. * In user mode vfork() cannot have a stack frame, and if
  263. * done by calling the "clone()" system call directly, you
  264. * do not have enough call-clobbered registers to hold all
  265. * the information you need.
  266. */
  267. asmlinkage long sys_vfork(struct pt_regs regs)
  268. {
  269. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  270. regs.gprs[15], &regs, 0, NULL, NULL);
  271. }
  272. /*
  273. * sys_execve() executes a new program.
  274. */
  275. asmlinkage long sys_execve(struct pt_regs regs)
  276. {
  277. int error;
  278. char * filename;
  279. filename = getname((char __user *) regs.orig_gpr2);
  280. error = PTR_ERR(filename);
  281. if (IS_ERR(filename))
  282. goto out;
  283. error = do_execve(filename, (char __user * __user *) regs.gprs[3],
  284. (char __user * __user *) regs.gprs[4], &regs);
  285. if (error == 0) {
  286. task_lock(current);
  287. current->ptrace &= ~PT_DTRACE;
  288. task_unlock(current);
  289. current->thread.fp_regs.fpc = 0;
  290. if (MACHINE_HAS_IEEE)
  291. asm volatile("sfpc %0,%0" : : "d" (0));
  292. }
  293. putname(filename);
  294. out:
  295. return error;
  296. }
  297. /*
  298. * fill in the FPU structure for a core dump.
  299. */
  300. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  301. {
  302. #ifndef CONFIG_64BIT
  303. /*
  304. * save fprs to current->thread.fp_regs to merge them with
  305. * the emulated registers and then copy the result to the dump.
  306. */
  307. save_fp_regs(&current->thread.fp_regs);
  308. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  309. #else /* CONFIG_64BIT */
  310. save_fp_regs(fpregs);
  311. #endif /* CONFIG_64BIT */
  312. return 1;
  313. }
  314. unsigned long get_wchan(struct task_struct *p)
  315. {
  316. struct stack_frame *sf, *low, *high;
  317. unsigned long return_address;
  318. int count;
  319. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  320. return 0;
  321. low = task_stack_page(p);
  322. high = (struct stack_frame *) task_pt_regs(p);
  323. sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
  324. if (sf <= low || sf > high)
  325. return 0;
  326. for (count = 0; count < 16; count++) {
  327. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  328. if (sf <= low || sf > high)
  329. return 0;
  330. return_address = sf->gprs[8] & PSW_ADDR_INSN;
  331. if (!in_sched_functions(return_address))
  332. return return_address;
  333. }
  334. return 0;
  335. }