process.c 8.9 KB

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