process.c 9.7 KB

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