process_32.c 13 KB

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