process.c 10 KB

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