process.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. struct halt_info {
  44. int mode;
  45. char *restart_cmd;
  46. };
  47. static void
  48. common_shutdown_1(void *generic_ptr)
  49. {
  50. struct halt_info *how = (struct halt_info *)generic_ptr;
  51. struct percpu_struct *cpup;
  52. unsigned long *pflags, flags;
  53. int cpuid = smp_processor_id();
  54. /* No point in taking interrupts anymore. */
  55. local_irq_disable();
  56. cpup = (struct percpu_struct *)
  57. ((unsigned long)hwrpb + hwrpb->processor_offset
  58. + hwrpb->processor_size * cpuid);
  59. pflags = &cpup->flags;
  60. flags = *pflags;
  61. /* Clear reason to "default"; clear "bootstrap in progress". */
  62. flags &= ~0x00ff0001UL;
  63. #ifdef CONFIG_SMP
  64. /* Secondaries halt here. */
  65. if (cpuid != boot_cpuid) {
  66. flags |= 0x00040000UL; /* "remain halted" */
  67. *pflags = flags;
  68. set_cpu_present(cpuid, false);
  69. set_cpu_possible(cpuid, false);
  70. halt();
  71. }
  72. #endif
  73. if (how->mode == LINUX_REBOOT_CMD_RESTART) {
  74. if (!how->restart_cmd) {
  75. flags |= 0x00020000UL; /* "cold bootstrap" */
  76. } else {
  77. /* For SRM, we could probably set environment
  78. variables to get this to work. We'd have to
  79. delay this until after srm_paging_stop unless
  80. we ever got srm_fixup working.
  81. At the moment, SRM will use the last boot device,
  82. but the file and flags will be the defaults, when
  83. doing a "warm" bootstrap. */
  84. flags |= 0x00030000UL; /* "warm bootstrap" */
  85. }
  86. } else {
  87. flags |= 0x00040000UL; /* "remain halted" */
  88. }
  89. *pflags = flags;
  90. #ifdef CONFIG_SMP
  91. /* Wait for the secondaries to halt. */
  92. set_cpu_present(boot_cpuid, false);
  93. set_cpu_possible(boot_cpuid, false);
  94. while (cpumask_weight(cpu_present_mask))
  95. barrier();
  96. #endif
  97. /* If booted from SRM, reset some of the original environment. */
  98. if (alpha_using_srm) {
  99. #ifdef CONFIG_DUMMY_CONSOLE
  100. /* If we've gotten here after SysRq-b, leave interrupt
  101. context before taking over the console. */
  102. if (in_interrupt())
  103. irq_exit();
  104. /* This has the effect of resetting the VGA video origin. */
  105. console_lock();
  106. do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES-1, 1);
  107. console_unlock();
  108. #endif
  109. pci_restore_srm_config();
  110. set_hae(srm_hae);
  111. }
  112. if (alpha_mv.kill_arch)
  113. alpha_mv.kill_arch(how->mode);
  114. if (! alpha_using_srm && how->mode != LINUX_REBOOT_CMD_RESTART) {
  115. /* Unfortunately, since MILO doesn't currently understand
  116. the hwrpb bits above, we can't reliably halt the
  117. processor and keep it halted. So just loop. */
  118. return;
  119. }
  120. if (alpha_using_srm)
  121. srm_paging_stop();
  122. halt();
  123. }
  124. static void
  125. common_shutdown(int mode, char *restart_cmd)
  126. {
  127. struct halt_info args;
  128. args.mode = mode;
  129. args.restart_cmd = restart_cmd;
  130. on_each_cpu(common_shutdown_1, &args, 0);
  131. }
  132. void
  133. machine_restart(char *restart_cmd)
  134. {
  135. common_shutdown(LINUX_REBOOT_CMD_RESTART, restart_cmd);
  136. }
  137. void
  138. machine_halt(void)
  139. {
  140. common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
  141. }
  142. void
  143. machine_power_off(void)
  144. {
  145. common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
  146. }
  147. /* Used by sysrq-p, among others. I don't believe r9-r15 are ever
  148. saved in the context it's used. */
  149. void
  150. show_regs(struct pt_regs *regs)
  151. {
  152. show_regs_print_info(KERN_DEFAULT);
  153. dik_show_regs(regs, NULL);
  154. }
  155. /*
  156. * Re-start a thread when doing execve()
  157. */
  158. void
  159. start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  160. {
  161. regs->pc = pc;
  162. regs->ps = 8;
  163. wrusp(sp);
  164. }
  165. EXPORT_SYMBOL(start_thread);
  166. /*
  167. * Free current thread data structures etc..
  168. */
  169. void
  170. exit_thread(void)
  171. {
  172. }
  173. void
  174. flush_thread(void)
  175. {
  176. /* Arrange for each exec'ed process to start off with a clean slate
  177. with respect to the FPU. This is all exceptions disabled. */
  178. current_thread_info()->ieee_state = 0;
  179. wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
  180. /* Clean slate for TLS. */
  181. current_thread_info()->pcb.unique = 0;
  182. }
  183. void
  184. release_thread(struct task_struct *dead_task)
  185. {
  186. }
  187. /*
  188. * Copy an alpha thread..
  189. */
  190. int
  191. copy_thread(unsigned long clone_flags, unsigned long usp,
  192. unsigned long arg,
  193. struct task_struct *p)
  194. {
  195. extern void ret_from_fork(void);
  196. extern void ret_from_kernel_thread(void);
  197. struct thread_info *childti = task_thread_info(p);
  198. struct pt_regs *childregs = task_pt_regs(p);
  199. struct pt_regs *regs = current_pt_regs();
  200. struct switch_stack *childstack, *stack;
  201. childstack = ((struct switch_stack *) childregs) - 1;
  202. childti->pcb.ksp = (unsigned long) childstack;
  203. childti->pcb.flags = 1; /* set FEN, clear everything else */
  204. if (unlikely(p->flags & PF_KTHREAD)) {
  205. /* kernel thread */
  206. memset(childstack, 0,
  207. sizeof(struct switch_stack) + sizeof(struct pt_regs));
  208. childstack->r26 = (unsigned long) ret_from_kernel_thread;
  209. childstack->r9 = usp; /* function */
  210. childstack->r10 = arg;
  211. childregs->hae = alpha_mv.hae_cache,
  212. childti->pcb.usp = 0;
  213. return 0;
  214. }
  215. /* Note: if CLONE_SETTLS is not set, then we must inherit the
  216. value from the parent, which will have been set by the block
  217. copy in dup_task_struct. This is non-intuitive, but is
  218. required for proper operation in the case of a threaded
  219. application calling fork. */
  220. if (clone_flags & CLONE_SETTLS)
  221. childti->pcb.unique = regs->r20;
  222. childti->pcb.usp = usp ?: rdusp();
  223. *childregs = *regs;
  224. childregs->r0 = 0;
  225. childregs->r19 = 0;
  226. childregs->r20 = 1; /* OSF/1 has some strange fork() semantics. */
  227. regs->r20 = 0;
  228. stack = ((struct switch_stack *) regs) - 1;
  229. *childstack = *stack;
  230. childstack->r26 = (unsigned long) ret_from_fork;
  231. return 0;
  232. }
  233. /*
  234. * Fill in the user structure for a ELF core dump.
  235. */
  236. void
  237. dump_elf_thread(elf_greg_t *dest, struct pt_regs *pt, struct thread_info *ti)
  238. {
  239. /* switch stack follows right below pt_regs: */
  240. struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
  241. dest[ 0] = pt->r0;
  242. dest[ 1] = pt->r1;
  243. dest[ 2] = pt->r2;
  244. dest[ 3] = pt->r3;
  245. dest[ 4] = pt->r4;
  246. dest[ 5] = pt->r5;
  247. dest[ 6] = pt->r6;
  248. dest[ 7] = pt->r7;
  249. dest[ 8] = pt->r8;
  250. dest[ 9] = sw->r9;
  251. dest[10] = sw->r10;
  252. dest[11] = sw->r11;
  253. dest[12] = sw->r12;
  254. dest[13] = sw->r13;
  255. dest[14] = sw->r14;
  256. dest[15] = sw->r15;
  257. dest[16] = pt->r16;
  258. dest[17] = pt->r17;
  259. dest[18] = pt->r18;
  260. dest[19] = pt->r19;
  261. dest[20] = pt->r20;
  262. dest[21] = pt->r21;
  263. dest[22] = pt->r22;
  264. dest[23] = pt->r23;
  265. dest[24] = pt->r24;
  266. dest[25] = pt->r25;
  267. dest[26] = pt->r26;
  268. dest[27] = pt->r27;
  269. dest[28] = pt->r28;
  270. dest[29] = pt->gp;
  271. dest[30] = ti == current_thread_info() ? rdusp() : ti->pcb.usp;
  272. dest[31] = pt->pc;
  273. /* Once upon a time this was the PS value. Which is stupid
  274. since that is always 8 for usermode. Usurped for the more
  275. useful value of the thread's UNIQUE field. */
  276. dest[32] = ti->pcb.unique;
  277. }
  278. EXPORT_SYMBOL(dump_elf_thread);
  279. int
  280. dump_elf_task(elf_greg_t *dest, struct task_struct *task)
  281. {
  282. dump_elf_thread(dest, task_pt_regs(task), task_thread_info(task));
  283. return 1;
  284. }
  285. EXPORT_SYMBOL(dump_elf_task);
  286. int
  287. dump_elf_task_fp(elf_fpreg_t *dest, struct task_struct *task)
  288. {
  289. struct switch_stack *sw = (struct switch_stack *)task_pt_regs(task) - 1;
  290. memcpy(dest, sw->fp, 32 * 8);
  291. return 1;
  292. }
  293. EXPORT_SYMBOL(dump_elf_task_fp);
  294. /*
  295. * Return saved PC of a blocked thread. This assumes the frame
  296. * pointer is the 6th saved long on the kernel stack and that the
  297. * saved return address is the first long in the frame. This all
  298. * holds provided the thread blocked through a call to schedule() ($15
  299. * is the frame pointer in schedule() and $15 is saved at offset 48 by
  300. * entry.S:do_switch_stack).
  301. *
  302. * Under heavy swap load I've seen this lose in an ugly way. So do
  303. * some extra sanity checking on the ranges we expect these pointers
  304. * to be in so that we can fail gracefully. This is just for ps after
  305. * all. -- r~
  306. */
  307. unsigned long
  308. thread_saved_pc(struct task_struct *t)
  309. {
  310. unsigned long base = (unsigned long)task_stack_page(t);
  311. unsigned long fp, sp = task_thread_info(t)->pcb.ksp;
  312. if (sp > base && sp+6*8 < base + 16*1024) {
  313. fp = ((unsigned long*)sp)[6];
  314. if (fp > sp && fp < base + 16*1024)
  315. return *(unsigned long *)fp;
  316. }
  317. return 0;
  318. }
  319. unsigned long
  320. get_wchan(struct task_struct *p)
  321. {
  322. unsigned long schedule_frame;
  323. unsigned long pc;
  324. if (!p || p == current || p->state == TASK_RUNNING)
  325. return 0;
  326. /*
  327. * This one depends on the frame size of schedule(). Do a
  328. * "disass schedule" in gdb to find the frame size. Also, the
  329. * code assumes that sleep_on() follows immediately after
  330. * interruptible_sleep_on() and that add_timer() follows
  331. * immediately after interruptible_sleep(). Ugly, isn't it?
  332. * Maybe adding a wchan field to task_struct would be better,
  333. * after all...
  334. */
  335. pc = thread_saved_pc(p);
  336. if (in_sched_functions(pc)) {
  337. schedule_frame = ((unsigned long *)task_thread_info(p)->pcb.ksp)[6];
  338. return ((unsigned long *)schedule_frame)[12];
  339. }
  340. return pc;
  341. }