process.c 11 KB

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