process.c 10 KB

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