process_32.c 11 KB

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