process_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* linux/arch/sparc/kernel/process.c
  3. *
  4. * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
  5. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  6. */
  7. /*
  8. * This file handles the architecture-dependent parts of process handling..
  9. */
  10. #include <linux/elfcore.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/stddef.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/user.h>
  22. #include <linux/smp.h>
  23. #include <linux/reboot.h>
  24. #include <linux/delay.h>
  25. #include <linux/pm.h>
  26. #include <linux/slab.h>
  27. #include <linux/cpu.h>
  28. #include <asm/auxio.h>
  29. #include <asm/oplib.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/page.h>
  32. #include <asm/delay.h>
  33. #include <asm/processor.h>
  34. #include <asm/psr.h>
  35. #include <asm/elf.h>
  36. #include <asm/prom.h>
  37. #include <asm/unistd.h>
  38. #include <asm/setup.h>
  39. #include "kernel.h"
  40. /*
  41. * Power management idle function
  42. * Set in pm platform drivers (apc.c and pmc.c)
  43. */
  44. void (*sparc_idle)(void);
  45. /*
  46. * Power-off handler instantiation for pm.h compliance
  47. * This is done via auxio, but could be used as a fallback
  48. * handler when auxio is not present-- unused for now...
  49. */
  50. void (*pm_power_off)(void) = machine_power_off;
  51. EXPORT_SYMBOL(pm_power_off);
  52. /*
  53. * sysctl - toggle power-off restriction for serial console
  54. * systems in machine_power_off()
  55. */
  56. int scons_pwroff = 1;
  57. extern void fpsave(unsigned long *, unsigned long *, void *, unsigned long *);
  58. struct task_struct *last_task_used_math = NULL;
  59. struct thread_info *current_set[NR_CPUS];
  60. /* Idle loop support. */
  61. void arch_cpu_idle(void)
  62. {
  63. if (sparc_idle)
  64. (*sparc_idle)();
  65. }
  66. /* XXX cli/sti -> local_irq_xxx here, check this works once SMP is fixed. */
  67. void machine_halt(void)
  68. {
  69. local_irq_enable();
  70. mdelay(8);
  71. local_irq_disable();
  72. prom_halt();
  73. panic("Halt failed!");
  74. }
  75. void machine_restart(char * cmd)
  76. {
  77. char *p;
  78. local_irq_enable();
  79. mdelay(8);
  80. local_irq_disable();
  81. p = strchr (reboot_command, '\n');
  82. if (p) *p = 0;
  83. if (cmd)
  84. prom_reboot(cmd);
  85. if (*reboot_command)
  86. prom_reboot(reboot_command);
  87. prom_feval ("reset");
  88. panic("Reboot failed!");
  89. }
  90. void machine_power_off(void)
  91. {
  92. if (auxio_power_register &&
  93. (!of_node_is_type(of_console_device, "serial") || scons_pwroff)) {
  94. u8 power_register = sbus_readb(auxio_power_register);
  95. power_register |= AUXIO_POWER_OFF;
  96. sbus_writeb(power_register, auxio_power_register);
  97. }
  98. machine_halt();
  99. }
  100. void show_regs(struct pt_regs *r)
  101. {
  102. struct reg_window32 *rw = (struct reg_window32 *) r->u_regs[14];
  103. show_regs_print_info(KERN_DEFAULT);
  104. printk("PSR: %08lx PC: %08lx NPC: %08lx Y: %08lx %s\n",
  105. r->psr, r->pc, r->npc, r->y, print_tainted());
  106. printk("PC: <%pS>\n", (void *) r->pc);
  107. printk("%%G: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  108. r->u_regs[0], r->u_regs[1], r->u_regs[2], r->u_regs[3],
  109. r->u_regs[4], r->u_regs[5], r->u_regs[6], r->u_regs[7]);
  110. printk("%%O: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  111. r->u_regs[8], r->u_regs[9], r->u_regs[10], r->u_regs[11],
  112. r->u_regs[12], r->u_regs[13], r->u_regs[14], r->u_regs[15]);
  113. printk("RPC: <%pS>\n", (void *) r->u_regs[15]);
  114. printk("%%L: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  115. rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
  116. rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
  117. printk("%%I: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  118. rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
  119. rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
  120. }
  121. /*
  122. * The show_stack() is external API which we do not use ourselves.
  123. * The oops is printed in die_if_kernel.
  124. */
  125. void show_stack(struct task_struct *tsk, unsigned long *_ksp, const char *loglvl)
  126. {
  127. unsigned long pc, fp;
  128. unsigned long task_base;
  129. struct reg_window32 *rw;
  130. int count = 0;
  131. if (!tsk)
  132. tsk = current;
  133. if (tsk == current && !_ksp)
  134. __asm__ __volatile__("mov %%fp, %0" : "=r" (_ksp));
  135. task_base = (unsigned long) task_stack_page(tsk);
  136. fp = (unsigned long) _ksp;
  137. do {
  138. /* Bogus frame pointer? */
  139. if (fp < (task_base + sizeof(struct thread_info)) ||
  140. fp >= (task_base + (PAGE_SIZE << 1)))
  141. break;
  142. rw = (struct reg_window32 *) fp;
  143. pc = rw->ins[7];
  144. printk("%s[%08lx : ", loglvl, pc);
  145. printk("%s%pS ] ", loglvl, (void *) pc);
  146. fp = rw->ins[6];
  147. } while (++count < 16);
  148. printk("%s\n", loglvl);
  149. }
  150. /*
  151. * Free current thread data structures etc..
  152. */
  153. void exit_thread(struct task_struct *tsk)
  154. {
  155. #ifndef CONFIG_SMP
  156. if (last_task_used_math == tsk) {
  157. #else
  158. if (test_tsk_thread_flag(tsk, TIF_USEDFPU)) {
  159. #endif
  160. /* Keep process from leaving FPU in a bogon state. */
  161. put_psr(get_psr() | PSR_EF);
  162. fpsave(&tsk->thread.float_regs[0], &tsk->thread.fsr,
  163. &tsk->thread.fpqueue[0], &tsk->thread.fpqdepth);
  164. #ifndef CONFIG_SMP
  165. last_task_used_math = NULL;
  166. #else
  167. clear_ti_thread_flag(task_thread_info(tsk), TIF_USEDFPU);
  168. #endif
  169. }
  170. }
  171. void flush_thread(void)
  172. {
  173. current_thread_info()->w_saved = 0;
  174. #ifndef CONFIG_SMP
  175. if(last_task_used_math == current) {
  176. #else
  177. if (test_thread_flag(TIF_USEDFPU)) {
  178. #endif
  179. /* Clean the fpu. */
  180. put_psr(get_psr() | PSR_EF);
  181. fpsave(&current->thread.float_regs[0], &current->thread.fsr,
  182. &current->thread.fpqueue[0], &current->thread.fpqdepth);
  183. #ifndef CONFIG_SMP
  184. last_task_used_math = NULL;
  185. #else
  186. clear_thread_flag(TIF_USEDFPU);
  187. #endif
  188. }
  189. }
  190. static inline struct sparc_stackf __user *
  191. clone_stackframe(struct sparc_stackf __user *dst,
  192. struct sparc_stackf __user *src)
  193. {
  194. unsigned long size, fp;
  195. struct sparc_stackf *tmp;
  196. struct sparc_stackf __user *sp;
  197. if (get_user(tmp, &src->fp))
  198. return NULL;
  199. fp = (unsigned long) tmp;
  200. size = (fp - ((unsigned long) src));
  201. fp = (unsigned long) dst;
  202. sp = (struct sparc_stackf __user *)(fp - size);
  203. /* do_fork() grabs the parent semaphore, we must release it
  204. * temporarily so we can build the child clone stack frame
  205. * without deadlocking.
  206. */
  207. if (__copy_user(sp, src, size))
  208. sp = NULL;
  209. else if (put_user(fp, &sp->fp))
  210. sp = NULL;
  211. return sp;
  212. }
  213. /* Copy a Sparc thread. The fork() return value conventions
  214. * under SunOS are nothing short of bletcherous:
  215. * Parent --> %o0 == childs pid, %o1 == 0
  216. * Child --> %o0 == parents pid, %o1 == 1
  217. *
  218. * NOTE: We have a separate fork kpsr/kwim because
  219. * the parent could change these values between
  220. * sys_fork invocation and when we reach here
  221. * if the parent should sleep while trying to
  222. * allocate the task_struct and kernel stack in
  223. * do_fork().
  224. * XXX See comment above sys_vfork in sparc64. todo.
  225. */
  226. extern void ret_from_fork(void);
  227. extern void ret_from_kernel_thread(void);
  228. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  229. {
  230. unsigned long clone_flags = args->flags;
  231. unsigned long sp = args->stack;
  232. unsigned long tls = args->tls;
  233. struct thread_info *ti = task_thread_info(p);
  234. struct pt_regs *childregs, *regs = current_pt_regs();
  235. char *new_stack;
  236. #ifndef CONFIG_SMP
  237. if(last_task_used_math == current) {
  238. #else
  239. if (test_thread_flag(TIF_USEDFPU)) {
  240. #endif
  241. put_psr(get_psr() | PSR_EF);
  242. fpsave(&p->thread.float_regs[0], &p->thread.fsr,
  243. &p->thread.fpqueue[0], &p->thread.fpqdepth);
  244. }
  245. /*
  246. * p->thread_info new_stack childregs stack bottom
  247. * ! ! ! !
  248. * V V (stk.fr.) V (pt_regs) V
  249. * +----- - - - - - ------+===========+=============+
  250. */
  251. new_stack = task_stack_page(p) + THREAD_SIZE;
  252. new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
  253. childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
  254. /*
  255. * A new process must start with interrupts disabled, see schedule_tail()
  256. * and finish_task_switch(). (If we do not do it and if a timer interrupt
  257. * hits before we unlock and attempts to take the rq->lock, we deadlock.)
  258. *
  259. * Thus, kpsr |= PSR_PIL.
  260. */
  261. ti->ksp = (unsigned long) new_stack;
  262. p->thread.kregs = childregs;
  263. if (unlikely(args->fn)) {
  264. extern int nwindows;
  265. unsigned long psr;
  266. memset(new_stack, 0, STACKFRAME_SZ + TRACEREG_SZ);
  267. ti->kpc = (((unsigned long) ret_from_kernel_thread) - 0x8);
  268. childregs->u_regs[UREG_G1] = (unsigned long) args->fn;
  269. childregs->u_regs[UREG_G2] = (unsigned long) args->fn_arg;
  270. psr = childregs->psr = get_psr();
  271. ti->kpsr = psr | PSR_PIL;
  272. ti->kwim = 1 << (((psr & PSR_CWP) + 1) % nwindows);
  273. return 0;
  274. }
  275. memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
  276. childregs->u_regs[UREG_FP] = sp;
  277. ti->kpc = (((unsigned long) ret_from_fork) - 0x8);
  278. ti->kpsr = current->thread.fork_kpsr | PSR_PIL;
  279. ti->kwim = current->thread.fork_kwim;
  280. if (sp != regs->u_regs[UREG_FP]) {
  281. struct sparc_stackf __user *childstack;
  282. struct sparc_stackf __user *parentstack;
  283. /*
  284. * This is a clone() call with supplied user stack.
  285. * Set some valid stack frames to give to the child.
  286. */
  287. childstack = (struct sparc_stackf __user *)
  288. (sp & ~0xfUL);
  289. parentstack = (struct sparc_stackf __user *)
  290. regs->u_regs[UREG_FP];
  291. #if 0
  292. printk("clone: parent stack:\n");
  293. show_stackframe(parentstack);
  294. #endif
  295. childstack = clone_stackframe(childstack, parentstack);
  296. if (!childstack)
  297. return -EFAULT;
  298. #if 0
  299. printk("clone: child stack:\n");
  300. show_stackframe(childstack);
  301. #endif
  302. childregs->u_regs[UREG_FP] = (unsigned long)childstack;
  303. }
  304. #ifdef CONFIG_SMP
  305. /* FPU must be disabled on SMP. */
  306. childregs->psr &= ~PSR_EF;
  307. clear_tsk_thread_flag(p, TIF_USEDFPU);
  308. #endif
  309. /* Set the return value for the child. */
  310. childregs->u_regs[UREG_I0] = current->pid;
  311. childregs->u_regs[UREG_I1] = 1;
  312. /* Set the return value for the parent. */
  313. regs->u_regs[UREG_I1] = 0;
  314. if (clone_flags & CLONE_SETTLS)
  315. childregs->u_regs[UREG_G7] = tls;
  316. return 0;
  317. }
  318. unsigned long __get_wchan(struct task_struct *task)
  319. {
  320. unsigned long pc, fp, bias = 0;
  321. unsigned long task_base = (unsigned long) task;
  322. unsigned long ret = 0;
  323. struct reg_window32 *rw;
  324. int count = 0;
  325. fp = task_thread_info(task)->ksp + bias;
  326. do {
  327. /* Bogus frame pointer? */
  328. if (fp < (task_base + sizeof(struct thread_info)) ||
  329. fp >= (task_base + (2 * PAGE_SIZE)))
  330. break;
  331. rw = (struct reg_window32 *) fp;
  332. pc = rw->ins[7];
  333. if (!in_sched_functions(pc)) {
  334. ret = pc;
  335. goto out;
  336. }
  337. fp = rw->ins[6] + bias;
  338. } while (++count < 16);
  339. out:
  340. return ret;
  341. }