process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /* arch/sparc64/kernel/process.c
  2. *
  3. * Copyright (C) 1995, 1996, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1997, 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. /*
  8. * This file handles the architecture-dependent parts of process handling..
  9. */
  10. #include <stdarg.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/fs.h>
  17. #include <linux/smp.h>
  18. #include <linux/stddef.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/slab.h>
  21. #include <linux/user.h>
  22. #include <linux/reboot.h>
  23. #include <linux/delay.h>
  24. #include <linux/compat.h>
  25. #include <linux/tick.h>
  26. #include <linux/init.h>
  27. #include <linux/cpu.h>
  28. #include <linux/elfcore.h>
  29. #include <linux/sysrq.h>
  30. #include <asm/oplib.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/system.h>
  33. #include <asm/page.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/processor.h>
  37. #include <asm/pstate.h>
  38. #include <asm/elf.h>
  39. #include <asm/fpumacro.h>
  40. #include <asm/head.h>
  41. #include <asm/cpudata.h>
  42. #include <asm/mmu_context.h>
  43. #include <asm/unistd.h>
  44. #include <asm/hypervisor.h>
  45. #include <asm/sstate.h>
  46. #include <asm/reboot.h>
  47. #include <asm/syscalls.h>
  48. #include <asm/irq_regs.h>
  49. #include <asm/smp.h>
  50. #include "kstack.h"
  51. static void sparc64_yield(int cpu)
  52. {
  53. if (tlb_type != hypervisor)
  54. return;
  55. clear_thread_flag(TIF_POLLING_NRFLAG);
  56. smp_mb__after_clear_bit();
  57. while (!need_resched() && !cpu_is_offline(cpu)) {
  58. unsigned long pstate;
  59. /* Disable interrupts. */
  60. __asm__ __volatile__(
  61. "rdpr %%pstate, %0\n\t"
  62. "andn %0, %1, %0\n\t"
  63. "wrpr %0, %%g0, %%pstate"
  64. : "=&r" (pstate)
  65. : "i" (PSTATE_IE));
  66. if (!need_resched() && !cpu_is_offline(cpu))
  67. sun4v_cpu_yield();
  68. /* Re-enable interrupts. */
  69. __asm__ __volatile__(
  70. "rdpr %%pstate, %0\n\t"
  71. "or %0, %1, %0\n\t"
  72. "wrpr %0, %%g0, %%pstate"
  73. : "=&r" (pstate)
  74. : "i" (PSTATE_IE));
  75. }
  76. set_thread_flag(TIF_POLLING_NRFLAG);
  77. }
  78. /* The idle loop on sparc64. */
  79. void cpu_idle(void)
  80. {
  81. int cpu = smp_processor_id();
  82. set_thread_flag(TIF_POLLING_NRFLAG);
  83. while(1) {
  84. tick_nohz_stop_sched_tick(1);
  85. while (!need_resched() && !cpu_is_offline(cpu))
  86. sparc64_yield(cpu);
  87. tick_nohz_restart_sched_tick();
  88. preempt_enable_no_resched();
  89. #ifdef CONFIG_HOTPLUG_CPU
  90. if (cpu_is_offline(cpu))
  91. cpu_play_dead();
  92. #endif
  93. schedule();
  94. preempt_disable();
  95. }
  96. }
  97. void machine_halt(void)
  98. {
  99. sstate_halt();
  100. prom_halt();
  101. panic("Halt failed!");
  102. }
  103. void machine_alt_power_off(void)
  104. {
  105. sstate_poweroff();
  106. prom_halt_power_off();
  107. panic("Power-off failed!");
  108. }
  109. void machine_restart(char * cmd)
  110. {
  111. char *p;
  112. sstate_reboot();
  113. p = strchr (reboot_command, '\n');
  114. if (p) *p = 0;
  115. if (cmd)
  116. prom_reboot(cmd);
  117. if (*reboot_command)
  118. prom_reboot(reboot_command);
  119. prom_reboot("");
  120. panic("Reboot failed!");
  121. }
  122. #ifdef CONFIG_COMPAT
  123. static void show_regwindow32(struct pt_regs *regs)
  124. {
  125. struct reg_window32 __user *rw;
  126. struct reg_window32 r_w;
  127. mm_segment_t old_fs;
  128. __asm__ __volatile__ ("flushw");
  129. rw = compat_ptr((unsigned)regs->u_regs[14]);
  130. old_fs = get_fs();
  131. set_fs (USER_DS);
  132. if (copy_from_user (&r_w, rw, sizeof(r_w))) {
  133. set_fs (old_fs);
  134. return;
  135. }
  136. set_fs (old_fs);
  137. printk("l0: %08x l1: %08x l2: %08x l3: %08x "
  138. "l4: %08x l5: %08x l6: %08x l7: %08x\n",
  139. r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
  140. r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
  141. printk("i0: %08x i1: %08x i2: %08x i3: %08x "
  142. "i4: %08x i5: %08x i6: %08x i7: %08x\n",
  143. r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
  144. r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
  145. }
  146. #else
  147. #define show_regwindow32(regs) do { } while (0)
  148. #endif
  149. static void show_regwindow(struct pt_regs *regs)
  150. {
  151. struct reg_window __user *rw;
  152. struct reg_window *rwk;
  153. struct reg_window r_w;
  154. mm_segment_t old_fs;
  155. if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
  156. __asm__ __volatile__ ("flushw");
  157. rw = (struct reg_window __user *)
  158. (regs->u_regs[14] + STACK_BIAS);
  159. rwk = (struct reg_window *)
  160. (regs->u_regs[14] + STACK_BIAS);
  161. if (!(regs->tstate & TSTATE_PRIV)) {
  162. old_fs = get_fs();
  163. set_fs (USER_DS);
  164. if (copy_from_user (&r_w, rw, sizeof(r_w))) {
  165. set_fs (old_fs);
  166. return;
  167. }
  168. rwk = &r_w;
  169. set_fs (old_fs);
  170. }
  171. } else {
  172. show_regwindow32(regs);
  173. return;
  174. }
  175. printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
  176. rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
  177. printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
  178. rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
  179. printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
  180. rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
  181. printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
  182. rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
  183. if (regs->tstate & TSTATE_PRIV)
  184. printk("I7: <%pS>\n", (void *) rwk->ins[7]);
  185. }
  186. void show_regs(struct pt_regs *regs)
  187. {
  188. printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x %s\n", regs->tstate,
  189. regs->tpc, regs->tnpc, regs->y, print_tainted());
  190. printk("TPC: <%pS>\n", (void *) regs->tpc);
  191. printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
  192. regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
  193. regs->u_regs[3]);
  194. printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
  195. regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
  196. regs->u_regs[7]);
  197. printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
  198. regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
  199. regs->u_regs[11]);
  200. printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
  201. regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
  202. regs->u_regs[15]);
  203. printk("RPC: <%pS>\n", (void *) regs->u_regs[15]);
  204. show_regwindow(regs);
  205. }
  206. struct global_reg_snapshot global_reg_snapshot[NR_CPUS];
  207. static DEFINE_SPINLOCK(global_reg_snapshot_lock);
  208. static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs,
  209. int this_cpu)
  210. {
  211. flushw_all();
  212. global_reg_snapshot[this_cpu].tstate = regs->tstate;
  213. global_reg_snapshot[this_cpu].tpc = regs->tpc;
  214. global_reg_snapshot[this_cpu].tnpc = regs->tnpc;
  215. global_reg_snapshot[this_cpu].o7 = regs->u_regs[UREG_I7];
  216. if (regs->tstate & TSTATE_PRIV) {
  217. struct thread_info *tp = current_thread_info();
  218. struct reg_window *rw;
  219. rw = (struct reg_window *)
  220. (regs->u_regs[UREG_FP] + STACK_BIAS);
  221. if (kstack_valid(tp, (unsigned long) rw)) {
  222. global_reg_snapshot[this_cpu].i7 = rw->ins[7];
  223. rw = (struct reg_window *)
  224. (rw->ins[6] + STACK_BIAS);
  225. if (kstack_valid(tp, (unsigned long) rw))
  226. global_reg_snapshot[this_cpu].rpc = rw->ins[7];
  227. }
  228. } else {
  229. global_reg_snapshot[this_cpu].i7 = 0;
  230. global_reg_snapshot[this_cpu].rpc = 0;
  231. }
  232. global_reg_snapshot[this_cpu].thread = tp;
  233. }
  234. /* In order to avoid hangs we do not try to synchronize with the
  235. * global register dump client cpus. The last store they make is to
  236. * the thread pointer, so do a short poll waiting for that to become
  237. * non-NULL.
  238. */
  239. static void __global_reg_poll(struct global_reg_snapshot *gp)
  240. {
  241. int limit = 0;
  242. while (!gp->thread && ++limit < 100) {
  243. barrier();
  244. udelay(1);
  245. }
  246. }
  247. void __trigger_all_cpu_backtrace(void)
  248. {
  249. struct thread_info *tp = current_thread_info();
  250. struct pt_regs *regs = get_irq_regs();
  251. unsigned long flags;
  252. int this_cpu, cpu;
  253. if (!regs)
  254. regs = tp->kregs;
  255. spin_lock_irqsave(&global_reg_snapshot_lock, flags);
  256. memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
  257. this_cpu = raw_smp_processor_id();
  258. __global_reg_self(tp, regs, this_cpu);
  259. smp_fetch_global_regs();
  260. for_each_online_cpu(cpu) {
  261. struct global_reg_snapshot *gp = &global_reg_snapshot[cpu];
  262. struct thread_info *tp;
  263. __global_reg_poll(gp);
  264. tp = gp->thread;
  265. printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
  266. (cpu == this_cpu ? '*' : ' '), cpu,
  267. gp->tstate, gp->tpc, gp->tnpc,
  268. ((tp && tp->task) ? tp->task->comm : "NULL"),
  269. ((tp && tp->task) ? tp->task->pid : -1));
  270. if (gp->tstate & TSTATE_PRIV) {
  271. printk(" TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
  272. (void *) gp->tpc,
  273. (void *) gp->o7,
  274. (void *) gp->i7,
  275. (void *) gp->rpc);
  276. } else {
  277. printk(" TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
  278. gp->tpc, gp->o7, gp->i7, gp->rpc);
  279. }
  280. }
  281. memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
  282. spin_unlock_irqrestore(&global_reg_snapshot_lock, flags);
  283. }
  284. #ifdef CONFIG_MAGIC_SYSRQ
  285. static void sysrq_handle_globreg(int key, struct tty_struct *tty)
  286. {
  287. __trigger_all_cpu_backtrace();
  288. }
  289. static struct sysrq_key_op sparc_globalreg_op = {
  290. .handler = sysrq_handle_globreg,
  291. .help_msg = "Globalregs",
  292. .action_msg = "Show Global CPU Regs",
  293. };
  294. static int __init sparc_globreg_init(void)
  295. {
  296. return register_sysrq_key('y', &sparc_globalreg_op);
  297. }
  298. core_initcall(sparc_globreg_init);
  299. #endif
  300. unsigned long thread_saved_pc(struct task_struct *tsk)
  301. {
  302. struct thread_info *ti = task_thread_info(tsk);
  303. unsigned long ret = 0xdeadbeefUL;
  304. if (ti && ti->ksp) {
  305. unsigned long *sp;
  306. sp = (unsigned long *)(ti->ksp + STACK_BIAS);
  307. if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
  308. sp[14]) {
  309. unsigned long *fp;
  310. fp = (unsigned long *)(sp[14] + STACK_BIAS);
  311. if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
  312. ret = fp[15];
  313. }
  314. }
  315. return ret;
  316. }
  317. /* Free current thread data structures etc.. */
  318. void exit_thread(void)
  319. {
  320. struct thread_info *t = current_thread_info();
  321. if (t->utraps) {
  322. if (t->utraps[0] < 2)
  323. kfree (t->utraps);
  324. else
  325. t->utraps[0]--;
  326. }
  327. if (test_and_clear_thread_flag(TIF_PERFCTR)) {
  328. t->user_cntd0 = t->user_cntd1 = NULL;
  329. t->pcr_reg = 0;
  330. write_pcr(0);
  331. }
  332. }
  333. void flush_thread(void)
  334. {
  335. struct thread_info *t = current_thread_info();
  336. struct mm_struct *mm;
  337. if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
  338. clear_ti_thread_flag(t, TIF_ABI_PENDING);
  339. if (test_ti_thread_flag(t, TIF_32BIT))
  340. clear_ti_thread_flag(t, TIF_32BIT);
  341. else
  342. set_ti_thread_flag(t, TIF_32BIT);
  343. }
  344. mm = t->task->mm;
  345. if (mm)
  346. tsb_context_switch(mm);
  347. set_thread_wsaved(0);
  348. /* Turn off performance counters if on. */
  349. if (test_and_clear_thread_flag(TIF_PERFCTR)) {
  350. t->user_cntd0 = t->user_cntd1 = NULL;
  351. t->pcr_reg = 0;
  352. write_pcr(0);
  353. }
  354. /* Clear FPU register state. */
  355. t->fpsaved[0] = 0;
  356. if (get_thread_current_ds() != ASI_AIUS)
  357. set_fs(USER_DS);
  358. }
  359. /* It's a bit more tricky when 64-bit tasks are involved... */
  360. static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
  361. {
  362. unsigned long fp, distance, rval;
  363. if (!(test_thread_flag(TIF_32BIT))) {
  364. csp += STACK_BIAS;
  365. psp += STACK_BIAS;
  366. __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
  367. fp += STACK_BIAS;
  368. } else
  369. __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
  370. /* Now 8-byte align the stack as this is mandatory in the
  371. * Sparc ABI due to how register windows work. This hides
  372. * the restriction from thread libraries etc. -DaveM
  373. */
  374. csp &= ~7UL;
  375. distance = fp - psp;
  376. rval = (csp - distance);
  377. if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
  378. rval = 0;
  379. else if (test_thread_flag(TIF_32BIT)) {
  380. if (put_user(((u32)csp),
  381. &(((struct reg_window32 __user *)rval)->ins[6])))
  382. rval = 0;
  383. } else {
  384. if (put_user(((u64)csp - STACK_BIAS),
  385. &(((struct reg_window __user *)rval)->ins[6])))
  386. rval = 0;
  387. else
  388. rval = rval - STACK_BIAS;
  389. }
  390. return rval;
  391. }
  392. /* Standard stuff. */
  393. static inline void shift_window_buffer(int first_win, int last_win,
  394. struct thread_info *t)
  395. {
  396. int i;
  397. for (i = first_win; i < last_win; i++) {
  398. t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
  399. memcpy(&t->reg_window[i], &t->reg_window[i+1],
  400. sizeof(struct reg_window));
  401. }
  402. }
  403. void synchronize_user_stack(void)
  404. {
  405. struct thread_info *t = current_thread_info();
  406. unsigned long window;
  407. flush_user_windows();
  408. if ((window = get_thread_wsaved()) != 0) {
  409. int winsize = sizeof(struct reg_window);
  410. int bias = 0;
  411. if (test_thread_flag(TIF_32BIT))
  412. winsize = sizeof(struct reg_window32);
  413. else
  414. bias = STACK_BIAS;
  415. window -= 1;
  416. do {
  417. unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
  418. struct reg_window *rwin = &t->reg_window[window];
  419. if (!copy_to_user((char __user *)sp, rwin, winsize)) {
  420. shift_window_buffer(window, get_thread_wsaved() - 1, t);
  421. set_thread_wsaved(get_thread_wsaved() - 1);
  422. }
  423. } while (window--);
  424. }
  425. }
  426. static void stack_unaligned(unsigned long sp)
  427. {
  428. siginfo_t info;
  429. info.si_signo = SIGBUS;
  430. info.si_errno = 0;
  431. info.si_code = BUS_ADRALN;
  432. info.si_addr = (void __user *) sp;
  433. info.si_trapno = 0;
  434. force_sig_info(SIGBUS, &info, current);
  435. }
  436. void fault_in_user_windows(void)
  437. {
  438. struct thread_info *t = current_thread_info();
  439. unsigned long window;
  440. int winsize = sizeof(struct reg_window);
  441. int bias = 0;
  442. if (test_thread_flag(TIF_32BIT))
  443. winsize = sizeof(struct reg_window32);
  444. else
  445. bias = STACK_BIAS;
  446. flush_user_windows();
  447. window = get_thread_wsaved();
  448. if (likely(window != 0)) {
  449. window -= 1;
  450. do {
  451. unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
  452. struct reg_window *rwin = &t->reg_window[window];
  453. if (unlikely(sp & 0x7UL))
  454. stack_unaligned(sp);
  455. if (unlikely(copy_to_user((char __user *)sp,
  456. rwin, winsize)))
  457. goto barf;
  458. } while (window--);
  459. }
  460. set_thread_wsaved(0);
  461. return;
  462. barf:
  463. set_thread_wsaved(window + 1);
  464. do_exit(SIGILL);
  465. }
  466. asmlinkage long sparc_do_fork(unsigned long clone_flags,
  467. unsigned long stack_start,
  468. struct pt_regs *regs,
  469. unsigned long stack_size)
  470. {
  471. int __user *parent_tid_ptr, *child_tid_ptr;
  472. unsigned long orig_i1 = regs->u_regs[UREG_I1];
  473. long ret;
  474. #ifdef CONFIG_COMPAT
  475. if (test_thread_flag(TIF_32BIT)) {
  476. parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
  477. child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
  478. } else
  479. #endif
  480. {
  481. parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
  482. child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
  483. }
  484. ret = do_fork(clone_flags, stack_start,
  485. regs, stack_size,
  486. parent_tid_ptr, child_tid_ptr);
  487. /* If we get an error and potentially restart the system
  488. * call, we're screwed because copy_thread() clobbered
  489. * the parent's %o1. So detect that case and restore it
  490. * here.
  491. */
  492. if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
  493. regs->u_regs[UREG_I1] = orig_i1;
  494. return ret;
  495. }
  496. /* Copy a Sparc thread. The fork() return value conventions
  497. * under SunOS are nothing short of bletcherous:
  498. * Parent --> %o0 == childs pid, %o1 == 0
  499. * Child --> %o0 == parents pid, %o1 == 1
  500. */
  501. int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
  502. unsigned long unused,
  503. struct task_struct *p, struct pt_regs *regs)
  504. {
  505. struct thread_info *t = task_thread_info(p);
  506. struct sparc_stackf *parent_sf;
  507. unsigned long child_stack_sz;
  508. char *child_trap_frame;
  509. int kernel_thread;
  510. kernel_thread = (regs->tstate & TSTATE_PRIV) ? 1 : 0;
  511. parent_sf = ((struct sparc_stackf *) regs) - 1;
  512. /* Calculate offset to stack_frame & pt_regs */
  513. child_stack_sz = ((STACKFRAME_SZ + TRACEREG_SZ) +
  514. (kernel_thread ? STACKFRAME_SZ : 0));
  515. child_trap_frame = (task_stack_page(p) +
  516. (THREAD_SIZE - child_stack_sz));
  517. memcpy(child_trap_frame, parent_sf, child_stack_sz);
  518. t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) |
  519. (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
  520. (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
  521. t->new_child = 1;
  522. t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
  523. t->kregs = (struct pt_regs *) (child_trap_frame +
  524. sizeof(struct sparc_stackf));
  525. t->fpsaved[0] = 0;
  526. if (kernel_thread) {
  527. struct sparc_stackf *child_sf = (struct sparc_stackf *)
  528. (child_trap_frame + (STACKFRAME_SZ + TRACEREG_SZ));
  529. /* Zero terminate the stack backtrace. */
  530. child_sf->fp = NULL;
  531. t->kregs->u_regs[UREG_FP] =
  532. ((unsigned long) child_sf) - STACK_BIAS;
  533. /* Special case, if we are spawning a kernel thread from
  534. * a userspace task (usermode helper, NFS or similar), we
  535. * must disable performance counters in the child because
  536. * the address space and protection realm are changing.
  537. */
  538. if (t->flags & _TIF_PERFCTR) {
  539. t->user_cntd0 = t->user_cntd1 = NULL;
  540. t->pcr_reg = 0;
  541. t->flags &= ~_TIF_PERFCTR;
  542. }
  543. t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
  544. t->kregs->u_regs[UREG_G6] = (unsigned long) t;
  545. t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
  546. } else {
  547. if (t->flags & _TIF_32BIT) {
  548. sp &= 0x00000000ffffffffUL;
  549. regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
  550. }
  551. t->kregs->u_regs[UREG_FP] = sp;
  552. t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
  553. if (sp != regs->u_regs[UREG_FP]) {
  554. unsigned long csp;
  555. csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
  556. if (!csp)
  557. return -EFAULT;
  558. t->kregs->u_regs[UREG_FP] = csp;
  559. }
  560. if (t->utraps)
  561. t->utraps[0]++;
  562. }
  563. /* Set the return value for the child. */
  564. t->kregs->u_regs[UREG_I0] = current->pid;
  565. t->kregs->u_regs[UREG_I1] = 1;
  566. /* Set the second return value for the parent. */
  567. regs->u_regs[UREG_I1] = 0;
  568. if (clone_flags & CLONE_SETTLS)
  569. t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
  570. return 0;
  571. }
  572. /*
  573. * This is the mechanism for creating a new kernel thread.
  574. *
  575. * NOTE! Only a kernel-only process(ie the swapper or direct descendants
  576. * who haven't done an "execve()") should use this: it will work within
  577. * a system call from a "real" process, but the process memory space will
  578. * not be freed until both the parent and the child have exited.
  579. */
  580. pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  581. {
  582. long retval;
  583. /* If the parent runs before fn(arg) is called by the child,
  584. * the input registers of this function can be clobbered.
  585. * So we stash 'fn' and 'arg' into global registers which
  586. * will not be modified by the parent.
  587. */
  588. __asm__ __volatile__("mov %4, %%g2\n\t" /* Save FN into global */
  589. "mov %5, %%g3\n\t" /* Save ARG into global */
  590. "mov %1, %%g1\n\t" /* Clone syscall nr. */
  591. "mov %2, %%o0\n\t" /* Clone flags. */
  592. "mov 0, %%o1\n\t" /* usp arg == 0 */
  593. "t 0x6d\n\t" /* Linux/Sparc clone(). */
  594. "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
  595. " mov %%o0, %0\n\t"
  596. "jmpl %%g2, %%o7\n\t" /* Call the function. */
  597. " mov %%g3, %%o0\n\t" /* Set arg in delay. */
  598. "mov %3, %%g1\n\t"
  599. "t 0x6d\n\t" /* Linux/Sparc exit(). */
  600. /* Notreached by child. */
  601. "1:" :
  602. "=r" (retval) :
  603. "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
  604. "i" (__NR_exit), "r" (fn), "r" (arg) :
  605. "g1", "g2", "g3", "o0", "o1", "memory", "cc");
  606. return retval;
  607. }
  608. typedef struct {
  609. union {
  610. unsigned int pr_regs[32];
  611. unsigned long pr_dregs[16];
  612. } pr_fr;
  613. unsigned int __unused;
  614. unsigned int pr_fsr;
  615. unsigned char pr_qcnt;
  616. unsigned char pr_q_entrysize;
  617. unsigned char pr_en;
  618. unsigned int pr_q[64];
  619. } elf_fpregset_t32;
  620. /*
  621. * fill in the fpu structure for a core dump.
  622. */
  623. int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
  624. {
  625. unsigned long *kfpregs = current_thread_info()->fpregs;
  626. unsigned long fprs = current_thread_info()->fpsaved[0];
  627. if (test_thread_flag(TIF_32BIT)) {
  628. elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
  629. if (fprs & FPRS_DL)
  630. memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
  631. sizeof(unsigned int) * 32);
  632. else
  633. memset(&fpregs32->pr_fr.pr_regs[0], 0,
  634. sizeof(unsigned int) * 32);
  635. fpregs32->pr_qcnt = 0;
  636. fpregs32->pr_q_entrysize = 8;
  637. memset(&fpregs32->pr_q[0], 0,
  638. (sizeof(unsigned int) * 64));
  639. if (fprs & FPRS_FEF) {
  640. fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
  641. fpregs32->pr_en = 1;
  642. } else {
  643. fpregs32->pr_fsr = 0;
  644. fpregs32->pr_en = 0;
  645. }
  646. } else {
  647. if(fprs & FPRS_DL)
  648. memcpy(&fpregs->pr_regs[0], kfpregs,
  649. sizeof(unsigned int) * 32);
  650. else
  651. memset(&fpregs->pr_regs[0], 0,
  652. sizeof(unsigned int) * 32);
  653. if(fprs & FPRS_DU)
  654. memcpy(&fpregs->pr_regs[16], kfpregs+16,
  655. sizeof(unsigned int) * 32);
  656. else
  657. memset(&fpregs->pr_regs[16], 0,
  658. sizeof(unsigned int) * 32);
  659. if(fprs & FPRS_FEF) {
  660. fpregs->pr_fsr = current_thread_info()->xfsr[0];
  661. fpregs->pr_gsr = current_thread_info()->gsr[0];
  662. } else {
  663. fpregs->pr_fsr = fpregs->pr_gsr = 0;
  664. }
  665. fpregs->pr_fprs = fprs;
  666. }
  667. return 1;
  668. }
  669. /*
  670. * sparc_execve() executes a new program after the asm stub has set
  671. * things up for us. This should basically do what I want it to.
  672. */
  673. asmlinkage int sparc_execve(struct pt_regs *regs)
  674. {
  675. int error, base = 0;
  676. char *filename;
  677. /* User register window flush is done by entry.S */
  678. /* Check for indirect call. */
  679. if (regs->u_regs[UREG_G1] == 0)
  680. base = 1;
  681. filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
  682. error = PTR_ERR(filename);
  683. if (IS_ERR(filename))
  684. goto out;
  685. error = do_execve(filename,
  686. (char __user * __user *)
  687. regs->u_regs[base + UREG_I1],
  688. (char __user * __user *)
  689. regs->u_regs[base + UREG_I2], regs);
  690. putname(filename);
  691. if (!error) {
  692. fprs_write(0);
  693. current_thread_info()->xfsr[0] = 0;
  694. current_thread_info()->fpsaved[0] = 0;
  695. regs->tstate &= ~TSTATE_PEF;
  696. }
  697. out:
  698. return error;
  699. }
  700. unsigned long get_wchan(struct task_struct *task)
  701. {
  702. unsigned long pc, fp, bias = 0;
  703. struct thread_info *tp;
  704. struct reg_window *rw;
  705. unsigned long ret = 0;
  706. int count = 0;
  707. if (!task || task == current ||
  708. task->state == TASK_RUNNING)
  709. goto out;
  710. tp = task_thread_info(task);
  711. bias = STACK_BIAS;
  712. fp = task_thread_info(task)->ksp + bias;
  713. do {
  714. if (!kstack_valid(tp, fp))
  715. break;
  716. rw = (struct reg_window *) fp;
  717. pc = rw->ins[7];
  718. if (!in_sched_functions(pc)) {
  719. ret = pc;
  720. goto out;
  721. }
  722. fp = rw->ins[6] + bias;
  723. } while (++count < 16);
  724. out:
  725. return ret;
  726. }