process.c 9.5 KB

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