/arch/s390/kernel/process.c

https://bitbucket.org/evzijst/gittest · C · 416 lines · 289 code · 49 blank · 78 comment · 32 complexity · a5b76b1244aaf06219a7049aab75db9c MD5 · raw file

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