process_32.c 13 KB

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