process.c 9.5 KB

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