process_32.c 13 KB

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