process_64.c 20 KB

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