process_32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 (*pm_idle)(void);
  42. EXPORT_SYMBOL(pm_idle);
  43. /*
  44. * Power-off handler instantiation for pm.h compliance
  45. * This is done via auxio, but could be used as a fallback
  46. * handler when auxio is not present-- unused for now...
  47. */
  48. void (*pm_power_off)(void) = machine_power_off;
  49. EXPORT_SYMBOL(pm_power_off);
  50. /*
  51. * sysctl - toggle power-off restriction for serial console
  52. * systems in machine_power_off()
  53. */
  54. int scons_pwroff = 1;
  55. extern void fpsave(unsigned long *, unsigned long *, void *, unsigned long *);
  56. struct task_struct *last_task_used_math = NULL;
  57. struct thread_info *current_set[NR_CPUS];
  58. /*
  59. * the idle loop on a Sparc... ;)
  60. */
  61. void cpu_idle(void)
  62. {
  63. set_thread_flag(TIF_POLLING_NRFLAG);
  64. /* endless idle loop with no priority at all */
  65. for (;;) {
  66. while (!need_resched()) {
  67. if (pm_idle)
  68. (*pm_idle)();
  69. else
  70. cpu_relax();
  71. }
  72. schedule_preempt_disabled();
  73. }
  74. }
  75. /* XXX cli/sti -> local_irq_xxx here, check this works once SMP is fixed. */
  76. void machine_halt(void)
  77. {
  78. local_irq_enable();
  79. mdelay(8);
  80. local_irq_disable();
  81. prom_halt();
  82. panic("Halt failed!");
  83. }
  84. void machine_restart(char * cmd)
  85. {
  86. char *p;
  87. local_irq_enable();
  88. mdelay(8);
  89. local_irq_disable();
  90. p = strchr (reboot_command, '\n');
  91. if (p) *p = 0;
  92. if (cmd)
  93. prom_reboot(cmd);
  94. if (*reboot_command)
  95. prom_reboot(reboot_command);
  96. prom_feval ("reset");
  97. panic("Reboot failed!");
  98. }
  99. void machine_power_off(void)
  100. {
  101. if (auxio_power_register &&
  102. (strcmp(of_console_device->type, "serial") || scons_pwroff))
  103. *auxio_power_register |= AUXIO_POWER_OFF;
  104. machine_halt();
  105. }
  106. void show_regs(struct pt_regs *r)
  107. {
  108. struct reg_window32 *rw = (struct reg_window32 *) r->u_regs[14];
  109. printk("PSR: %08lx PC: %08lx NPC: %08lx Y: %08lx %s\n",
  110. r->psr, r->pc, r->npc, r->y, print_tainted());
  111. printk("PC: <%pS>\n", (void *) r->pc);
  112. printk("%%G: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  113. r->u_regs[0], r->u_regs[1], r->u_regs[2], r->u_regs[3],
  114. r->u_regs[4], r->u_regs[5], r->u_regs[6], r->u_regs[7]);
  115. printk("%%O: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  116. r->u_regs[8], r->u_regs[9], r->u_regs[10], r->u_regs[11],
  117. r->u_regs[12], r->u_regs[13], r->u_regs[14], r->u_regs[15]);
  118. printk("RPC: <%pS>\n", (void *) r->u_regs[15]);
  119. printk("%%L: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  120. rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
  121. rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
  122. printk("%%I: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  123. rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
  124. rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
  125. }
  126. /*
  127. * The show_stack is an external API which we do not use ourselves.
  128. * The oops is printed in die_if_kernel.
  129. */
  130. void show_stack(struct task_struct *tsk, unsigned long *_ksp)
  131. {
  132. unsigned long pc, fp;
  133. unsigned long task_base;
  134. struct reg_window32 *rw;
  135. int count = 0;
  136. if (tsk != NULL)
  137. task_base = (unsigned long) task_stack_page(tsk);
  138. else
  139. task_base = (unsigned long) current_thread_info();
  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. void dump_stack(void)
  155. {
  156. unsigned long *ksp;
  157. __asm__ __volatile__("mov %%fp, %0"
  158. : "=r" (ksp));
  159. show_stack(current, ksp);
  160. }
  161. EXPORT_SYMBOL(dump_stack);
  162. /*
  163. * Note: sparc64 has a pretty intricated thread_saved_pc, check it out.
  164. */
  165. unsigned long thread_saved_pc(struct task_struct *tsk)
  166. {
  167. return task_thread_info(tsk)->kpc;
  168. }
  169. /*
  170. * Free current thread data structures etc..
  171. */
  172. void exit_thread(void)
  173. {
  174. #ifndef CONFIG_SMP
  175. if(last_task_used_math == current) {
  176. #else
  177. if (test_thread_flag(TIF_USEDFPU)) {
  178. #endif
  179. /* Keep process from leaving FPU in a bogon state. */
  180. put_psr(get_psr() | PSR_EF);
  181. fpsave(&current->thread.float_regs[0], &current->thread.fsr,
  182. &current->thread.fpqueue[0], &current->thread.fpqdepth);
  183. #ifndef CONFIG_SMP
  184. last_task_used_math = NULL;
  185. #else
  186. clear_thread_flag(TIF_USEDFPU);
  187. #endif
  188. }
  189. }
  190. void flush_thread(void)
  191. {
  192. current_thread_info()->w_saved = 0;
  193. #ifndef CONFIG_SMP
  194. if(last_task_used_math == current) {
  195. #else
  196. if (test_thread_flag(TIF_USEDFPU)) {
  197. #endif
  198. /* Clean the fpu. */
  199. put_psr(get_psr() | PSR_EF);
  200. fpsave(&current->thread.float_regs[0], &current->thread.fsr,
  201. &current->thread.fpqueue[0], &current->thread.fpqdepth);
  202. #ifndef CONFIG_SMP
  203. last_task_used_math = NULL;
  204. #else
  205. clear_thread_flag(TIF_USEDFPU);
  206. #endif
  207. }
  208. /* This task is no longer a kernel thread. */
  209. if (current->thread.flags & SPARC_FLAG_KTHREAD) {
  210. current->thread.flags &= ~SPARC_FLAG_KTHREAD;
  211. /* We must fixup kregs as well. */
  212. /* XXX This was not fixed for ti for a while, worked. Unused? */
  213. current->thread.kregs = (struct pt_regs *)
  214. (task_stack_page(current) + (THREAD_SIZE - TRACEREG_SZ));
  215. }
  216. }
  217. static inline struct sparc_stackf __user *
  218. clone_stackframe(struct sparc_stackf __user *dst,
  219. struct sparc_stackf __user *src)
  220. {
  221. unsigned long size, fp;
  222. struct sparc_stackf *tmp;
  223. struct sparc_stackf __user *sp;
  224. if (get_user(tmp, &src->fp))
  225. return NULL;
  226. fp = (unsigned long) tmp;
  227. size = (fp - ((unsigned long) src));
  228. fp = (unsigned long) dst;
  229. sp = (struct sparc_stackf __user *)(fp - size);
  230. /* do_fork() grabs the parent semaphore, we must release it
  231. * temporarily so we can build the child clone stack frame
  232. * without deadlocking.
  233. */
  234. if (__copy_user(sp, src, size))
  235. sp = NULL;
  236. else if (put_user(fp, &sp->fp))
  237. sp = NULL;
  238. return sp;
  239. }
  240. asmlinkage int sparc_do_fork(unsigned long clone_flags,
  241. unsigned long stack_start,
  242. struct pt_regs *regs,
  243. unsigned long stack_size)
  244. {
  245. unsigned long parent_tid_ptr, child_tid_ptr;
  246. unsigned long orig_i1 = regs->u_regs[UREG_I1];
  247. long ret;
  248. parent_tid_ptr = regs->u_regs[UREG_I2];
  249. child_tid_ptr = regs->u_regs[UREG_I4];
  250. ret = do_fork(clone_flags, stack_start,
  251. regs, stack_size,
  252. (int __user *) parent_tid_ptr,
  253. (int __user *) child_tid_ptr);
  254. /* If we get an error and potentially restart the system
  255. * call, we're screwed because copy_thread() clobbered
  256. * the parent's %o1. So detect that case and restore it
  257. * here.
  258. */
  259. if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
  260. regs->u_regs[UREG_I1] = orig_i1;
  261. return ret;
  262. }
  263. /* Copy a Sparc thread. The fork() return value conventions
  264. * under SunOS are nothing short of bletcherous:
  265. * Parent --> %o0 == childs pid, %o1 == 0
  266. * Child --> %o0 == parents pid, %o1 == 1
  267. *
  268. * NOTE: We have a separate fork kpsr/kwim because
  269. * the parent could change these values between
  270. * sys_fork invocation and when we reach here
  271. * if the parent should sleep while trying to
  272. * allocate the task_struct and kernel stack in
  273. * do_fork().
  274. * XXX See comment above sys_vfork in sparc64. todo.
  275. */
  276. extern void ret_from_fork(void);
  277. int copy_thread(unsigned long clone_flags, unsigned long sp,
  278. unsigned long unused,
  279. struct task_struct *p, struct pt_regs *regs)
  280. {
  281. struct thread_info *ti = task_thread_info(p);
  282. struct pt_regs *childregs;
  283. char *new_stack;
  284. #ifndef CONFIG_SMP
  285. if(last_task_used_math == current) {
  286. #else
  287. if (test_thread_flag(TIF_USEDFPU)) {
  288. #endif
  289. put_psr(get_psr() | PSR_EF);
  290. fpsave(&p->thread.float_regs[0], &p->thread.fsr,
  291. &p->thread.fpqueue[0], &p->thread.fpqdepth);
  292. #ifdef CONFIG_SMP
  293. clear_thread_flag(TIF_USEDFPU);
  294. #endif
  295. }
  296. /*
  297. * p->thread_info new_stack childregs
  298. * ! ! ! {if(PSR_PS) }
  299. * V V (stk.fr.) V (pt_regs) { (stk.fr.) }
  300. * +----- - - - - - ------+===========+============={+==========}+
  301. */
  302. new_stack = task_stack_page(p) + THREAD_SIZE;
  303. if (regs->psr & PSR_PS)
  304. new_stack -= STACKFRAME_SZ;
  305. new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
  306. memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
  307. childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
  308. /*
  309. * A new process must start with interrupts closed in 2.5,
  310. * because this is how Mingo's scheduler works (see schedule_tail
  311. * and finish_arch_switch). If we do not do it, a timer interrupt hits
  312. * before we unlock, attempts to re-take the rq->lock, and then we die.
  313. * Thus, kpsr|=PSR_PIL.
  314. */
  315. ti->ksp = (unsigned long) new_stack;
  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(regs->psr & PSR_PS) {
  320. extern struct pt_regs fake_swapper_regs;
  321. p->thread.kregs = &fake_swapper_regs;
  322. new_stack += STACKFRAME_SZ + TRACEREG_SZ;
  323. childregs->u_regs[UREG_FP] = (unsigned long) new_stack;
  324. p->thread.flags |= SPARC_FLAG_KTHREAD;
  325. p->thread.current_ds = KERNEL_DS;
  326. memcpy(new_stack, (void *)regs->u_regs[UREG_FP], STACKFRAME_SZ);
  327. childregs->u_regs[UREG_G6] = (unsigned long) ti;
  328. } else {
  329. p->thread.kregs = childregs;
  330. childregs->u_regs[UREG_FP] = sp;
  331. p->thread.flags &= ~SPARC_FLAG_KTHREAD;
  332. p->thread.current_ds = USER_DS;
  333. if (sp != regs->u_regs[UREG_FP]) {
  334. struct sparc_stackf __user *childstack;
  335. struct sparc_stackf __user *parentstack;
  336. /*
  337. * This is a clone() call with supplied user stack.
  338. * Set some valid stack frames to give to the child.
  339. */
  340. childstack = (struct sparc_stackf __user *)
  341. (sp & ~0xfUL);
  342. parentstack = (struct sparc_stackf __user *)
  343. regs->u_regs[UREG_FP];
  344. #if 0
  345. printk("clone: parent stack:\n");
  346. show_stackframe(parentstack);
  347. #endif
  348. childstack = clone_stackframe(childstack, parentstack);
  349. if (!childstack)
  350. return -EFAULT;
  351. #if 0
  352. printk("clone: child stack:\n");
  353. show_stackframe(childstack);
  354. #endif
  355. childregs->u_regs[UREG_FP] = (unsigned long)childstack;
  356. }
  357. }
  358. #ifdef CONFIG_SMP
  359. /* FPU must be disabled on SMP. */
  360. childregs->psr &= ~PSR_EF;
  361. #endif
  362. /* Set the return value for the child. */
  363. childregs->u_regs[UREG_I0] = current->pid;
  364. childregs->u_regs[UREG_I1] = 1;
  365. /* Set the return value for the parent. */
  366. regs->u_regs[UREG_I1] = 0;
  367. if (clone_flags & CLONE_SETTLS)
  368. childregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
  369. return 0;
  370. }
  371. /*
  372. * fill in the fpu structure for a core dump.
  373. */
  374. int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
  375. {
  376. if (used_math()) {
  377. memset(fpregs, 0, sizeof(*fpregs));
  378. fpregs->pr_q_entrysize = 8;
  379. return 1;
  380. }
  381. #ifdef CONFIG_SMP
  382. if (test_thread_flag(TIF_USEDFPU)) {
  383. put_psr(get_psr() | PSR_EF);
  384. fpsave(&current->thread.float_regs[0], &current->thread.fsr,
  385. &current->thread.fpqueue[0], &current->thread.fpqdepth);
  386. if (regs != NULL) {
  387. regs->psr &= ~(PSR_EF);
  388. clear_thread_flag(TIF_USEDFPU);
  389. }
  390. }
  391. #else
  392. if (current == last_task_used_math) {
  393. put_psr(get_psr() | PSR_EF);
  394. fpsave(&current->thread.float_regs[0], &current->thread.fsr,
  395. &current->thread.fpqueue[0], &current->thread.fpqdepth);
  396. if (regs != NULL) {
  397. regs->psr &= ~(PSR_EF);
  398. last_task_used_math = NULL;
  399. }
  400. }
  401. #endif
  402. memcpy(&fpregs->pr_fr.pr_regs[0],
  403. &current->thread.float_regs[0],
  404. (sizeof(unsigned long) * 32));
  405. fpregs->pr_fsr = current->thread.fsr;
  406. fpregs->pr_qcnt = current->thread.fpqdepth;
  407. fpregs->pr_q_entrysize = 8;
  408. fpregs->pr_en = 1;
  409. if(fpregs->pr_qcnt != 0) {
  410. memcpy(&fpregs->pr_q[0],
  411. &current->thread.fpqueue[0],
  412. sizeof(struct fpq) * fpregs->pr_qcnt);
  413. }
  414. /* Zero out the rest. */
  415. memset(&fpregs->pr_q[fpregs->pr_qcnt], 0,
  416. sizeof(struct fpq) * (32 - fpregs->pr_qcnt));
  417. return 1;
  418. }
  419. /*
  420. * sparc_execve() executes a new program after the asm stub has set
  421. * things up for us. This should basically do what I want it to.
  422. */
  423. asmlinkage int sparc_execve(struct pt_regs *regs)
  424. {
  425. int error, base = 0;
  426. char *filename;
  427. /* Check for indirect call. */
  428. if(regs->u_regs[UREG_G1] == 0)
  429. base = 1;
  430. filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
  431. error = PTR_ERR(filename);
  432. if(IS_ERR(filename))
  433. goto out;
  434. error = do_execve(filename,
  435. (const char __user *const __user *)
  436. regs->u_regs[base + UREG_I1],
  437. (const char __user *const __user *)
  438. regs->u_regs[base + UREG_I2],
  439. regs);
  440. putname(filename);
  441. out:
  442. return error;
  443. }
  444. /*
  445. * This is the mechanism for creating a new kernel thread.
  446. *
  447. * NOTE! Only a kernel-only process(ie the swapper or direct descendants
  448. * who haven't done an "execve()") should use this: it will work within
  449. * a system call from a "real" process, but the process memory space will
  450. * not be freed until both the parent and the child have exited.
  451. */
  452. pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  453. {
  454. long retval;
  455. __asm__ __volatile__("mov %4, %%g2\n\t" /* Set aside fn ptr... */
  456. "mov %5, %%g3\n\t" /* and arg. */
  457. "mov %1, %%g1\n\t"
  458. "mov %2, %%o0\n\t" /* Clone flags. */
  459. "mov 0, %%o1\n\t" /* usp arg == 0 */
  460. "t 0x10\n\t" /* Linux/Sparc clone(). */
  461. "cmp %%o1, 0\n\t"
  462. "be 1f\n\t" /* The parent, just return. */
  463. " nop\n\t" /* Delay slot. */
  464. "jmpl %%g2, %%o7\n\t" /* Call the function. */
  465. " mov %%g3, %%o0\n\t" /* Get back the arg in delay. */
  466. "mov %3, %%g1\n\t"
  467. "t 0x10\n\t" /* Linux/Sparc exit(). */
  468. /* Notreached by child. */
  469. "1: mov %%o0, %0\n\t" :
  470. "=r" (retval) :
  471. "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
  472. "i" (__NR_exit), "r" (fn), "r" (arg) :
  473. "g1", "g2", "g3", "o0", "o1", "memory", "cc");
  474. return retval;
  475. }
  476. EXPORT_SYMBOL(kernel_thread);
  477. unsigned long get_wchan(struct task_struct *task)
  478. {
  479. unsigned long pc, fp, bias = 0;
  480. unsigned long task_base = (unsigned long) task;
  481. unsigned long ret = 0;
  482. struct reg_window32 *rw;
  483. int count = 0;
  484. if (!task || task == current ||
  485. task->state == TASK_RUNNING)
  486. goto out;
  487. fp = task_thread_info(task)->ksp + bias;
  488. do {
  489. /* Bogus frame pointer? */
  490. if (fp < (task_base + sizeof(struct thread_info)) ||
  491. fp >= (task_base + (2 * PAGE_SIZE)))
  492. break;
  493. rw = (struct reg_window32 *) fp;
  494. pc = rw->ins[7];
  495. if (!in_sched_functions(pc)) {
  496. ret = pc;
  497. goto out;
  498. }
  499. fp = rw->ins[6] + bias;
  500. } while (++count < 16);
  501. out:
  502. return ret;
  503. }