process.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <linux/kernel_stat.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/system.h>
  43. #include <asm/io.h>
  44. #include <asm/processor.h>
  45. #include <asm/irq.h>
  46. #include <asm/timer.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. extern void s390_handle_mcck(void);
  73. /*
  74. * The idle loop on a S390...
  75. */
  76. static void default_idle(void)
  77. {
  78. /* CPU is going idle. */
  79. local_irq_disable();
  80. if (need_resched()) {
  81. local_irq_enable();
  82. return;
  83. }
  84. #ifdef CONFIG_HOTPLUG_CPU
  85. if (cpu_is_offline(smp_processor_id())) {
  86. preempt_enable_no_resched();
  87. cpu_die();
  88. }
  89. #endif
  90. local_mcck_disable();
  91. if (test_thread_flag(TIF_MCCK_PENDING)) {
  92. local_mcck_enable();
  93. local_irq_enable();
  94. s390_handle_mcck();
  95. return;
  96. }
  97. trace_hardirqs_on();
  98. /* Don't trace preempt off for idle. */
  99. stop_critical_timings();
  100. /* Stop virtual timer and halt the cpu. */
  101. vtime_stop_cpu();
  102. /* Reenable preemption tracer. */
  103. start_critical_timings();
  104. }
  105. void cpu_idle(void)
  106. {
  107. for (;;) {
  108. tick_nohz_stop_sched_tick(1);
  109. while (!need_resched())
  110. default_idle();
  111. tick_nohz_restart_sched_tick();
  112. preempt_enable_no_resched();
  113. schedule();
  114. preempt_disable();
  115. }
  116. }
  117. extern void kernel_thread_starter(void);
  118. asm(
  119. ".align 4\n"
  120. "kernel_thread_starter:\n"
  121. " la 2,0(10)\n"
  122. " basr 14,9\n"
  123. " la 2,0\n"
  124. " br 11\n");
  125. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  126. {
  127. struct pt_regs regs;
  128. memset(&regs, 0, sizeof(regs));
  129. regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
  130. regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
  131. regs.gprs[9] = (unsigned long) fn;
  132. regs.gprs[10] = (unsigned long) arg;
  133. regs.gprs[11] = (unsigned long) do_exit;
  134. regs.orig_gpr2 = -1;
  135. /* Ok, create the new process.. */
  136. return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
  137. 0, &regs, 0, NULL, NULL);
  138. }
  139. /*
  140. * Free current thread data structures etc..
  141. */
  142. void exit_thread(void)
  143. {
  144. }
  145. void flush_thread(void)
  146. {
  147. clear_used_math();
  148. clear_tsk_thread_flag(current, TIF_USEDFPU);
  149. }
  150. void release_thread(struct task_struct *dead_task)
  151. {
  152. }
  153. int copy_thread(int nr, unsigned long clone_flags, unsigned long new_stackp,
  154. unsigned long unused,
  155. struct task_struct * p, struct pt_regs * regs)
  156. {
  157. struct fake_frame
  158. {
  159. struct stack_frame sf;
  160. struct pt_regs childregs;
  161. } *frame;
  162. frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
  163. p->thread.ksp = (unsigned long) frame;
  164. /* Store access registers to kernel stack of new process. */
  165. frame->childregs = *regs;
  166. frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */
  167. frame->childregs.gprs[15] = new_stackp;
  168. frame->sf.back_chain = 0;
  169. /* new return point is ret_from_fork */
  170. frame->sf.gprs[8] = (unsigned long) ret_from_fork;
  171. /* fake return stack for resume(), don't go back to schedule */
  172. frame->sf.gprs[9] = (unsigned long) frame;
  173. /* Save access registers to new thread structure. */
  174. save_access_regs(&p->thread.acrs[0]);
  175. #ifndef CONFIG_64BIT
  176. /*
  177. * save fprs to current->thread.fp_regs to merge them with
  178. * the emulated registers and then copy the result to the child.
  179. */
  180. save_fp_regs(&current->thread.fp_regs);
  181. memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
  182. sizeof(s390_fp_regs));
  183. /* Set a new TLS ? */
  184. if (clone_flags & CLONE_SETTLS)
  185. p->thread.acrs[0] = regs->gprs[6];
  186. #else /* CONFIG_64BIT */
  187. /* Save the fpu registers to new thread structure. */
  188. save_fp_regs(&p->thread.fp_regs);
  189. /* Set a new TLS ? */
  190. if (clone_flags & CLONE_SETTLS) {
  191. if (test_thread_flag(TIF_31BIT)) {
  192. p->thread.acrs[0] = (unsigned int) regs->gprs[6];
  193. } else {
  194. p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
  195. p->thread.acrs[1] = (unsigned int) regs->gprs[6];
  196. }
  197. }
  198. #endif /* CONFIG_64BIT */
  199. /* start new process with ar4 pointing to the correct address space */
  200. p->thread.mm_segment = get_fs();
  201. /* Don't copy debug registers */
  202. memset(&p->thread.per_info,0,sizeof(p->thread.per_info));
  203. return 0;
  204. }
  205. asmlinkage long sys_fork(void)
  206. {
  207. struct pt_regs *regs = task_pt_regs(current);
  208. return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
  209. }
  210. asmlinkage long sys_clone(void)
  211. {
  212. struct pt_regs *regs = task_pt_regs(current);
  213. unsigned long clone_flags;
  214. unsigned long newsp;
  215. int __user *parent_tidptr, *child_tidptr;
  216. clone_flags = regs->gprs[3];
  217. newsp = regs->orig_gpr2;
  218. parent_tidptr = (int __user *) regs->gprs[4];
  219. child_tidptr = (int __user *) regs->gprs[5];
  220. if (!newsp)
  221. newsp = regs->gprs[15];
  222. return do_fork(clone_flags, newsp, regs, 0,
  223. parent_tidptr, child_tidptr);
  224. }
  225. /*
  226. * This is trivial, and on the face of it looks like it
  227. * could equally well be done in user mode.
  228. *
  229. * Not so, for quite unobvious reasons - register pressure.
  230. * In user mode vfork() cannot have a stack frame, and if
  231. * done by calling the "clone()" system call directly, you
  232. * do not have enough call-clobbered registers to hold all
  233. * the information you need.
  234. */
  235. asmlinkage long sys_vfork(void)
  236. {
  237. struct pt_regs *regs = task_pt_regs(current);
  238. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  239. regs->gprs[15], regs, 0, NULL, NULL);
  240. }
  241. asmlinkage void execve_tail(void)
  242. {
  243. task_lock(current);
  244. current->ptrace &= ~PT_DTRACE;
  245. task_unlock(current);
  246. current->thread.fp_regs.fpc = 0;
  247. if (MACHINE_HAS_IEEE)
  248. asm volatile("sfpc %0,%0" : : "d" (0));
  249. }
  250. /*
  251. * sys_execve() executes a new program.
  252. */
  253. asmlinkage long sys_execve(void)
  254. {
  255. struct pt_regs *regs = task_pt_regs(current);
  256. char *filename;
  257. unsigned long result;
  258. int rc;
  259. filename = getname((char __user *) regs->orig_gpr2);
  260. if (IS_ERR(filename)) {
  261. result = PTR_ERR(filename);
  262. goto out;
  263. }
  264. rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
  265. (char __user * __user *) regs->gprs[4], regs);
  266. if (rc) {
  267. result = rc;
  268. goto out_putname;
  269. }
  270. execve_tail();
  271. result = regs->gprs[2];
  272. out_putname:
  273. putname(filename);
  274. out:
  275. return result;
  276. }
  277. /*
  278. * fill in the FPU structure for a core dump.
  279. */
  280. int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
  281. {
  282. #ifndef CONFIG_64BIT
  283. /*
  284. * save fprs to current->thread.fp_regs to merge them with
  285. * the emulated registers and then copy the result to the dump.
  286. */
  287. save_fp_regs(&current->thread.fp_regs);
  288. memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
  289. #else /* CONFIG_64BIT */
  290. save_fp_regs(fpregs);
  291. #endif /* CONFIG_64BIT */
  292. return 1;
  293. }
  294. unsigned long get_wchan(struct task_struct *p)
  295. {
  296. struct stack_frame *sf, *low, *high;
  297. unsigned long return_address;
  298. int count;
  299. if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
  300. return 0;
  301. low = task_stack_page(p);
  302. high = (struct stack_frame *) task_pt_regs(p);
  303. sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
  304. if (sf <= low || sf > high)
  305. return 0;
  306. for (count = 0; count < 16; count++) {
  307. sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
  308. if (sf <= low || sf > high)
  309. return 0;
  310. return_address = sf->gprs[8] & PSW_ADDR_INSN;
  311. if (!in_sched_functions(return_address))
  312. return return_address;
  313. }
  314. return 0;
  315. }