process_32.c 13 KB

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