process.c 11 KB

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