process.c 21 KB

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