process.c 11 KB

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