process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * linux/arch/alpha/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. /*
  7. * This file handles the architecture-dependent parts of process handling.
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/stddef.h>
  16. #include <linux/unistd.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/user.h>
  19. #include <linux/time.h>
  20. #include <linux/major.h>
  21. #include <linux/stat.h>
  22. #include <linux/vt.h>
  23. #include <linux/mman.h>
  24. #include <linux/elfcore.h>
  25. #include <linux/reboot.h>
  26. #include <linux/tty.h>
  27. #include <linux/console.h>
  28. #include <linux/slab.h>
  29. #include <linux/rcupdate.h>
  30. #include <asm/reg.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/io.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/hwrpb.h>
  35. #include <asm/fpu.h>
  36. #include "proto.h"
  37. #include "pci_impl.h"
  38. /*
  39. * Power off function, if any
  40. */
  41. void (*pm_power_off)(void) = machine_power_off;
  42. EXPORT_SYMBOL(pm_power_off);
  43. void
  44. cpu_idle(void)
  45. {
  46. set_thread_flag(TIF_POLLING_NRFLAG);
  47. while (1) {
  48. /* FIXME -- EV6 and LCA45 know how to power down
  49. the CPU. */
  50. rcu_idle_enter();
  51. while (!need_resched())
  52. cpu_relax();
  53. rcu_idle_exit();
  54. schedule();
  55. }
  56. }
  57. struct halt_info {
  58. int mode;
  59. char *restart_cmd;
  60. };
  61. static void
  62. common_shutdown_1(void *generic_ptr)
  63. {
  64. struct halt_info *how = (struct halt_info *)generic_ptr;
  65. struct percpu_struct *cpup;
  66. unsigned long *pflags, flags;
  67. int cpuid = smp_processor_id();
  68. /* No point in taking interrupts anymore. */
  69. local_irq_disable();
  70. cpup = (struct percpu_struct *)
  71. ((unsigned long)hwrpb + hwrpb->processor_offset
  72. + hwrpb->processor_size * cpuid);
  73. pflags = &cpup->flags;
  74. flags = *pflags;
  75. /* Clear reason to "default"; clear "bootstrap in progress". */
  76. flags &= ~0x00ff0001UL;
  77. #ifdef CONFIG_SMP
  78. /* Secondaries halt here. */
  79. if (cpuid != boot_cpuid) {
  80. flags |= 0x00040000UL; /* "remain halted" */
  81. *pflags = flags;
  82. set_cpu_present(cpuid, false);
  83. set_cpu_possible(cpuid, false);
  84. halt();
  85. }
  86. #endif
  87. if (how->mode == LINUX_REBOOT_CMD_RESTART) {
  88. if (!how->restart_cmd) {
  89. flags |= 0x00020000UL; /* "cold bootstrap" */
  90. } else {
  91. /* For SRM, we could probably set environment
  92. variables to get this to work. We'd have to
  93. delay this until after srm_paging_stop unless
  94. we ever got srm_fixup working.
  95. At the moment, SRM will use the last boot device,
  96. but the file and flags will be the defaults, when
  97. doing a "warm" bootstrap. */
  98. flags |= 0x00030000UL; /* "warm bootstrap" */
  99. }
  100. } else {
  101. flags |= 0x00040000UL; /* "remain halted" */
  102. }
  103. *pflags = flags;
  104. #ifdef CONFIG_SMP
  105. /* Wait for the secondaries to halt. */
  106. set_cpu_present(boot_cpuid, false);
  107. set_cpu_possible(boot_cpuid, false);
  108. while (cpumask_weight(cpu_present_mask))
  109. barrier();
  110. #endif
  111. /* If booted from SRM, reset some of the original environment. */
  112. if (alpha_using_srm) {
  113. #ifdef CONFIG_DUMMY_CONSOLE
  114. /* If we've gotten here after SysRq-b, leave interrupt
  115. context before taking over the console. */
  116. if (in_interrupt())
  117. irq_exit();
  118. /* This has the effect of resetting the VGA video origin. */
  119. take_over_console(&dummy_con, 0, MAX_NR_CONSOLES-1, 1);
  120. #endif
  121. pci_restore_srm_config();
  122. set_hae(srm_hae);
  123. }
  124. if (alpha_mv.kill_arch)
  125. alpha_mv.kill_arch(how->mode);
  126. if (! alpha_using_srm && how->mode != LINUX_REBOOT_CMD_RESTART) {
  127. /* Unfortunately, since MILO doesn't currently understand
  128. the hwrpb bits above, we can't reliably halt the
  129. processor and keep it halted. So just loop. */
  130. return;
  131. }
  132. if (alpha_using_srm)
  133. srm_paging_stop();
  134. halt();
  135. }
  136. static void
  137. common_shutdown(int mode, char *restart_cmd)
  138. {
  139. struct halt_info args;
  140. args.mode = mode;
  141. args.restart_cmd = restart_cmd;
  142. on_each_cpu(common_shutdown_1, &args, 0);
  143. }
  144. void
  145. machine_restart(char *restart_cmd)
  146. {
  147. common_shutdown(LINUX_REBOOT_CMD_RESTART, restart_cmd);
  148. }
  149. void
  150. machine_halt(void)
  151. {
  152. common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
  153. }
  154. void
  155. machine_power_off(void)
  156. {
  157. common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
  158. }
  159. /* Used by sysrq-p, among others. I don't believe r9-r15 are ever
  160. saved in the context it's used. */
  161. void
  162. show_regs(struct pt_regs *regs)
  163. {
  164. dik_show_regs(regs, NULL);
  165. }
  166. /*
  167. * Re-start a thread when doing execve()
  168. */
  169. void
  170. start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  171. {
  172. regs->pc = pc;
  173. regs->ps = 8;
  174. wrusp(sp);
  175. }
  176. EXPORT_SYMBOL(start_thread);
  177. /*
  178. * Free current thread data structures etc..
  179. */
  180. void
  181. exit_thread(void)
  182. {
  183. }
  184. void
  185. flush_thread(void)
  186. {
  187. /* Arrange for each exec'ed process to start off with a clean slate
  188. with respect to the FPU. This is all exceptions disabled. */
  189. current_thread_info()->ieee_state = 0;
  190. wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
  191. /* Clean slate for TLS. */
  192. current_thread_info()->pcb.unique = 0;
  193. }
  194. void
  195. release_thread(struct task_struct *dead_task)
  196. {
  197. }
  198. /*
  199. * "alpha_clone()".. By the time we get here, the
  200. * non-volatile registers have also been saved on the
  201. * stack. We do some ugly pointer stuff here.. (see
  202. * also copy_thread)
  203. *
  204. * Notice that "fork()" is implemented in terms of clone,
  205. * with parameters (SIGCHLD, 0).
  206. */
  207. int
  208. alpha_clone(unsigned long clone_flags, unsigned long usp,
  209. int __user *parent_tid, int __user *child_tid,
  210. unsigned long tls_value, struct pt_regs *regs)
  211. {
  212. if (!usp)
  213. usp = rdusp();
  214. return do_fork(clone_flags, usp, regs, 0, parent_tid, child_tid);
  215. }
  216. int
  217. alpha_vfork(struct pt_regs *regs)
  218. {
  219. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(),
  220. regs, 0, NULL, NULL);
  221. }
  222. /*
  223. * Copy an alpha thread..
  224. *
  225. * Note the "stack_offset" stuff: when returning to kernel mode, we need
  226. * to have some extra stack-space for the kernel stack that still exists
  227. * after the "ret_from_fork". When returning to user mode, we only want
  228. * the space needed by the syscall stack frame (ie "struct pt_regs").
  229. * Use the passed "regs" pointer to determine how much space we need
  230. * for a kernel fork().
  231. */
  232. int
  233. copy_thread(unsigned long clone_flags, unsigned long usp,
  234. unsigned long unused,
  235. struct task_struct * p, struct pt_regs * regs)
  236. {
  237. extern void ret_from_fork(void);
  238. struct thread_info *childti = task_thread_info(p);
  239. struct pt_regs * childregs;
  240. struct switch_stack * childstack, *stack;
  241. unsigned long stack_offset, settls;
  242. stack_offset = PAGE_SIZE - sizeof(struct pt_regs);
  243. if (!(regs->ps & 8))
  244. stack_offset = (PAGE_SIZE-1) & (unsigned long) regs;
  245. childregs = (struct pt_regs *)
  246. (stack_offset + PAGE_SIZE + task_stack_page(p));
  247. *childregs = *regs;
  248. settls = regs->r20;
  249. childregs->r0 = 0;
  250. childregs->r19 = 0;
  251. childregs->r20 = 1; /* OSF/1 has some strange fork() semantics. */
  252. regs->r20 = 0;
  253. stack = ((struct switch_stack *) regs) - 1;
  254. childstack = ((struct switch_stack *) childregs) - 1;
  255. *childstack = *stack;
  256. childstack->r26 = (unsigned long) ret_from_fork;
  257. childti->pcb.usp = usp;
  258. childti->pcb.ksp = (unsigned long) childstack;
  259. childti->pcb.flags = 1; /* set FEN, clear everything else */
  260. /* Set a new TLS for the child thread? Peek back into the
  261. syscall arguments that we saved on syscall entry. Oops,
  262. except we'd have clobbered it with the parent/child set
  263. of r20. Read the saved copy. */
  264. /* Note: if CLONE_SETTLS is not set, then we must inherit the
  265. value from the parent, which will have been set by the block
  266. copy in dup_task_struct. This is non-intuitive, but is
  267. required for proper operation in the case of a threaded
  268. application calling fork. */
  269. if (clone_flags & CLONE_SETTLS)
  270. childti->pcb.unique = settls;
  271. return 0;
  272. }
  273. /*
  274. * Fill in the user structure for a ELF core dump.
  275. */
  276. void
  277. dump_elf_thread(elf_greg_t *dest, struct pt_regs *pt, struct thread_info *ti)
  278. {
  279. /* switch stack follows right below pt_regs: */
  280. struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
  281. dest[ 0] = pt->r0;
  282. dest[ 1] = pt->r1;
  283. dest[ 2] = pt->r2;
  284. dest[ 3] = pt->r3;
  285. dest[ 4] = pt->r4;
  286. dest[ 5] = pt->r5;
  287. dest[ 6] = pt->r6;
  288. dest[ 7] = pt->r7;
  289. dest[ 8] = pt->r8;
  290. dest[ 9] = sw->r9;
  291. dest[10] = sw->r10;
  292. dest[11] = sw->r11;
  293. dest[12] = sw->r12;
  294. dest[13] = sw->r13;
  295. dest[14] = sw->r14;
  296. dest[15] = sw->r15;
  297. dest[16] = pt->r16;
  298. dest[17] = pt->r17;
  299. dest[18] = pt->r18;
  300. dest[19] = pt->r19;
  301. dest[20] = pt->r20;
  302. dest[21] = pt->r21;
  303. dest[22] = pt->r22;
  304. dest[23] = pt->r23;
  305. dest[24] = pt->r24;
  306. dest[25] = pt->r25;
  307. dest[26] = pt->r26;
  308. dest[27] = pt->r27;
  309. dest[28] = pt->r28;
  310. dest[29] = pt->gp;
  311. dest[30] = ti == current_thread_info() ? rdusp() : ti->pcb.usp;
  312. dest[31] = pt->pc;
  313. /* Once upon a time this was the PS value. Which is stupid
  314. since that is always 8 for usermode. Usurped for the more
  315. useful value of the thread's UNIQUE field. */
  316. dest[32] = ti->pcb.unique;
  317. }
  318. EXPORT_SYMBOL(dump_elf_thread);
  319. int
  320. dump_elf_task(elf_greg_t *dest, struct task_struct *task)
  321. {
  322. dump_elf_thread(dest, task_pt_regs(task), task_thread_info(task));
  323. return 1;
  324. }
  325. EXPORT_SYMBOL(dump_elf_task);
  326. int
  327. dump_elf_task_fp(elf_fpreg_t *dest, struct task_struct *task)
  328. {
  329. struct switch_stack *sw = (struct switch_stack *)task_pt_regs(task) - 1;
  330. memcpy(dest, sw->fp, 32 * 8);
  331. return 1;
  332. }
  333. EXPORT_SYMBOL(dump_elf_task_fp);
  334. /*
  335. * sys_execve() executes a new program.
  336. */
  337. asmlinkage int
  338. do_sys_execve(const char __user *ufilename,
  339. const char __user *const __user *argv,
  340. const char __user *const __user *envp, struct pt_regs *regs)
  341. {
  342. int error;
  343. char *filename;
  344. filename = getname(ufilename);
  345. error = PTR_ERR(filename);
  346. if (IS_ERR(filename))
  347. goto out;
  348. error = do_execve(filename, argv, envp, regs);
  349. putname(filename);
  350. out:
  351. return error;
  352. }
  353. /*
  354. * Return saved PC of a blocked thread. This assumes the frame
  355. * pointer is the 6th saved long on the kernel stack and that the
  356. * saved return address is the first long in the frame. This all
  357. * holds provided the thread blocked through a call to schedule() ($15
  358. * is the frame pointer in schedule() and $15 is saved at offset 48 by
  359. * entry.S:do_switch_stack).
  360. *
  361. * Under heavy swap load I've seen this lose in an ugly way. So do
  362. * some extra sanity checking on the ranges we expect these pointers
  363. * to be in so that we can fail gracefully. This is just for ps after
  364. * all. -- r~
  365. */
  366. unsigned long
  367. thread_saved_pc(struct task_struct *t)
  368. {
  369. unsigned long base = (unsigned long)task_stack_page(t);
  370. unsigned long fp, sp = task_thread_info(t)->pcb.ksp;
  371. if (sp > base && sp+6*8 < base + 16*1024) {
  372. fp = ((unsigned long*)sp)[6];
  373. if (fp > sp && fp < base + 16*1024)
  374. return *(unsigned long *)fp;
  375. }
  376. return 0;
  377. }
  378. unsigned long
  379. get_wchan(struct task_struct *p)
  380. {
  381. unsigned long schedule_frame;
  382. unsigned long pc;
  383. if (!p || p == current || p->state == TASK_RUNNING)
  384. return 0;
  385. /*
  386. * This one depends on the frame size of schedule(). Do a
  387. * "disass schedule" in gdb to find the frame size. Also, the
  388. * code assumes that sleep_on() follows immediately after
  389. * interruptible_sleep_on() and that add_timer() follows
  390. * immediately after interruptible_sleep(). Ugly, isn't it?
  391. * Maybe adding a wchan field to task_struct would be better,
  392. * after all...
  393. */
  394. pc = thread_saved_pc(p);
  395. if (in_sched_functions(pc)) {
  396. schedule_frame = ((unsigned long *)task_thread_info(p)->pcb.ksp)[6];
  397. return ((unsigned long *)schedule_frame)[12];
  398. }
  399. return pc;
  400. }