process.c 11 KB

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