process_32.c 10 KB

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