process_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* arch/sparc64/kernel/process.c
  3. *
  4. * Copyright (C) 1995, 1996, 2008 David S. Miller (davem@davemloft.net)
  5. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1997, 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  7. */
  8. /*
  9. * This file handles the architecture-dependent parts of process handling..
  10. */
  11. #include <stdarg.h>
  12. #include <linux/errno.h>
  13. #include <linux/export.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/sched/task_stack.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/fs.h>
  21. #include <linux/smp.h>
  22. #include <linux/stddef.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/slab.h>
  25. #include <linux/user.h>
  26. #include <linux/delay.h>
  27. #include <linux/compat.h>
  28. #include <linux/tick.h>
  29. #include <linux/init.h>
  30. #include <linux/cpu.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/elfcore.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/nmi.h>
  35. #include <linux/context_tracking.h>
  36. #include <linux/signal.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/page.h>
  39. #include <asm/pgalloc.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/processor.h>
  42. #include <asm/pstate.h>
  43. #include <asm/elf.h>
  44. #include <asm/fpumacro.h>
  45. #include <asm/head.h>
  46. #include <asm/cpudata.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/unistd.h>
  49. #include <asm/hypervisor.h>
  50. #include <asm/syscalls.h>
  51. #include <asm/irq_regs.h>
  52. #include <asm/smp.h>
  53. #include <asm/pcr.h>
  54. #include "kstack.h"
  55. /* Idle loop support on sparc64. */
  56. void arch_cpu_idle(void)
  57. {
  58. if (tlb_type != hypervisor) {
  59. touch_nmi_watchdog();
  60. local_irq_enable();
  61. } else {
  62. unsigned long pstate;
  63. local_irq_enable();
  64. /* The sun4v sleeping code requires that we have PSTATE.IE cleared over
  65. * the cpu sleep hypervisor call.
  66. */
  67. __asm__ __volatile__(
  68. "rdpr %%pstate, %0\n\t"
  69. "andn %0, %1, %0\n\t"
  70. "wrpr %0, %%g0, %%pstate"
  71. : "=&r" (pstate)
  72. : "i" (PSTATE_IE));
  73. if (!need_resched() && !cpu_is_offline(smp_processor_id())) {
  74. sun4v_cpu_yield();
  75. /* If resumed by cpu_poke then we need to explicitly
  76. * call scheduler_ipi().
  77. */
  78. scheduler_poke();
  79. }
  80. /* Re-enable interrupts. */
  81. __asm__ __volatile__(
  82. "rdpr %%pstate, %0\n\t"
  83. "or %0, %1, %0\n\t"
  84. "wrpr %0, %%g0, %%pstate"
  85. : "=&r" (pstate)
  86. : "i" (PSTATE_IE));
  87. }
  88. }
  89. #ifdef CONFIG_HOTPLUG_CPU
  90. void arch_cpu_idle_dead(void)
  91. {
  92. sched_preempt_enable_no_resched();
  93. cpu_play_dead();
  94. }
  95. #endif
  96. #ifdef CONFIG_COMPAT
  97. static void show_regwindow32(struct pt_regs *regs)
  98. {
  99. struct reg_window32 __user *rw;
  100. struct reg_window32 r_w;
  101. mm_segment_t old_fs;
  102. __asm__ __volatile__ ("flushw");
  103. rw = compat_ptr((unsigned int)regs->u_regs[14]);
  104. old_fs = get_fs();
  105. set_fs (USER_DS);
  106. if (copy_from_user (&r_w, rw, sizeof(r_w))) {
  107. set_fs (old_fs);
  108. return;
  109. }
  110. set_fs (old_fs);
  111. printk("l0: %08x l1: %08x l2: %08x l3: %08x "
  112. "l4: %08x l5: %08x l6: %08x l7: %08x\n",
  113. r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
  114. r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
  115. printk("i0: %08x i1: %08x i2: %08x i3: %08x "
  116. "i4: %08x i5: %08x i6: %08x i7: %08x\n",
  117. r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
  118. r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
  119. }
  120. #else
  121. #define show_regwindow32(regs) do { } while (0)
  122. #endif
  123. static void show_regwindow(struct pt_regs *regs)
  124. {
  125. struct reg_window __user *rw;
  126. struct reg_window *rwk;
  127. struct reg_window r_w;
  128. mm_segment_t old_fs;
  129. if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
  130. __asm__ __volatile__ ("flushw");
  131. rw = (struct reg_window __user *)
  132. (regs->u_regs[14] + STACK_BIAS);
  133. rwk = (struct reg_window *)
  134. (regs->u_regs[14] + STACK_BIAS);
  135. if (!(regs->tstate & TSTATE_PRIV)) {
  136. old_fs = get_fs();
  137. set_fs (USER_DS);
  138. if (copy_from_user (&r_w, rw, sizeof(r_w))) {
  139. set_fs (old_fs);
  140. return;
  141. }
  142. rwk = &r_w;
  143. set_fs (old_fs);
  144. }
  145. } else {
  146. show_regwindow32(regs);
  147. return;
  148. }
  149. printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
  150. rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
  151. printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
  152. rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
  153. printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
  154. rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
  155. printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
  156. rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
  157. if (regs->tstate & TSTATE_PRIV)
  158. printk("I7: <%pS>\n", (void *) rwk->ins[7]);
  159. }
  160. void show_regs(struct pt_regs *regs)
  161. {
  162. show_regs_print_info(KERN_DEFAULT);
  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. union global_cpu_snapshot global_cpu_snapshot[NR_CPUS];
  183. static DEFINE_SPINLOCK(global_cpu_snapshot_lock);
  184. static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs,
  185. int this_cpu)
  186. {
  187. struct global_reg_snapshot *rp;
  188. flushw_all();
  189. rp = &global_cpu_snapshot[this_cpu].reg;
  190. rp->tstate = regs->tstate;
  191. rp->tpc = regs->tpc;
  192. rp->tnpc = regs->tnpc;
  193. rp->o7 = regs->u_regs[UREG_I7];
  194. if (regs->tstate & TSTATE_PRIV) {
  195. struct reg_window *rw;
  196. rw = (struct reg_window *)
  197. (regs->u_regs[UREG_FP] + STACK_BIAS);
  198. if (kstack_valid(tp, (unsigned long) rw)) {
  199. rp->i7 = rw->ins[7];
  200. rw = (struct reg_window *)
  201. (rw->ins[6] + STACK_BIAS);
  202. if (kstack_valid(tp, (unsigned long) rw))
  203. rp->rpc = rw->ins[7];
  204. }
  205. } else {
  206. rp->i7 = 0;
  207. rp->rpc = 0;
  208. }
  209. rp->thread = tp;
  210. }
  211. /* In order to avoid hangs we do not try to synchronize with the
  212. * global register dump client cpus. The last store they make is to
  213. * the thread pointer, so do a short poll waiting for that to become
  214. * non-NULL.
  215. */
  216. static void __global_reg_poll(struct global_reg_snapshot *gp)
  217. {
  218. int limit = 0;
  219. while (!gp->thread && ++limit < 100) {
  220. barrier();
  221. udelay(1);
  222. }
  223. }
  224. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  225. {
  226. struct thread_info *tp = current_thread_info();
  227. struct pt_regs *regs = get_irq_regs();
  228. unsigned long flags;
  229. int this_cpu, cpu;
  230. if (!regs)
  231. regs = tp->kregs;
  232. spin_lock_irqsave(&global_cpu_snapshot_lock, flags);
  233. this_cpu = raw_smp_processor_id();
  234. memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
  235. if (cpumask_test_cpu(this_cpu, mask) && !exclude_self)
  236. __global_reg_self(tp, regs, this_cpu);
  237. smp_fetch_global_regs();
  238. for_each_cpu(cpu, mask) {
  239. struct global_reg_snapshot *gp;
  240. if (exclude_self && cpu == this_cpu)
  241. continue;
  242. gp = &global_cpu_snapshot[cpu].reg;
  243. __global_reg_poll(gp);
  244. tp = gp->thread;
  245. printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
  246. (cpu == this_cpu ? '*' : ' '), cpu,
  247. gp->tstate, gp->tpc, gp->tnpc,
  248. ((tp && tp->task) ? tp->task->comm : "NULL"),
  249. ((tp && tp->task) ? tp->task->pid : -1));
  250. if (gp->tstate & TSTATE_PRIV) {
  251. printk(" TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
  252. (void *) gp->tpc,
  253. (void *) gp->o7,
  254. (void *) gp->i7,
  255. (void *) gp->rpc);
  256. } else {
  257. printk(" TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
  258. gp->tpc, gp->o7, gp->i7, gp->rpc);
  259. }
  260. touch_nmi_watchdog();
  261. }
  262. memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
  263. spin_unlock_irqrestore(&global_cpu_snapshot_lock, flags);
  264. }
  265. #ifdef CONFIG_MAGIC_SYSRQ
  266. static void sysrq_handle_globreg(int key)
  267. {
  268. trigger_all_cpu_backtrace();
  269. }
  270. static struct sysrq_key_op sparc_globalreg_op = {
  271. .handler = sysrq_handle_globreg,
  272. .help_msg = "global-regs(y)",
  273. .action_msg = "Show Global CPU Regs",
  274. };
  275. static void __global_pmu_self(int this_cpu)
  276. {
  277. struct global_pmu_snapshot *pp;
  278. int i, num;
  279. if (!pcr_ops)
  280. return;
  281. pp = &global_cpu_snapshot[this_cpu].pmu;
  282. num = 1;
  283. if (tlb_type == hypervisor &&
  284. sun4v_chip_type >= SUN4V_CHIP_NIAGARA4)
  285. num = 4;
  286. for (i = 0; i < num; i++) {
  287. pp->pcr[i] = pcr_ops->read_pcr(i);
  288. pp->pic[i] = pcr_ops->read_pic(i);
  289. }
  290. }
  291. static void __global_pmu_poll(struct global_pmu_snapshot *pp)
  292. {
  293. int limit = 0;
  294. while (!pp->pcr[0] && ++limit < 100) {
  295. barrier();
  296. udelay(1);
  297. }
  298. }
  299. static void pmu_snapshot_all_cpus(void)
  300. {
  301. unsigned long flags;
  302. int this_cpu, cpu;
  303. spin_lock_irqsave(&global_cpu_snapshot_lock, flags);
  304. memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
  305. this_cpu = raw_smp_processor_id();
  306. __global_pmu_self(this_cpu);
  307. smp_fetch_global_pmu();
  308. for_each_online_cpu(cpu) {
  309. struct global_pmu_snapshot *pp = &global_cpu_snapshot[cpu].pmu;
  310. __global_pmu_poll(pp);
  311. printk("%c CPU[%3d]: PCR[%08lx:%08lx:%08lx:%08lx] PIC[%08lx:%08lx:%08lx:%08lx]\n",
  312. (cpu == this_cpu ? '*' : ' '), cpu,
  313. pp->pcr[0], pp->pcr[1], pp->pcr[2], pp->pcr[3],
  314. pp->pic[0], pp->pic[1], pp->pic[2], pp->pic[3]);
  315. touch_nmi_watchdog();
  316. }
  317. memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
  318. spin_unlock_irqrestore(&global_cpu_snapshot_lock, flags);
  319. }
  320. static void sysrq_handle_globpmu(int key)
  321. {
  322. pmu_snapshot_all_cpus();
  323. }
  324. static struct sysrq_key_op sparc_globalpmu_op = {
  325. .handler = sysrq_handle_globpmu,
  326. .help_msg = "global-pmu(x)",
  327. .action_msg = "Show Global PMU Regs",
  328. };
  329. static int __init sparc_sysrq_init(void)
  330. {
  331. int ret = register_sysrq_key('y', &sparc_globalreg_op);
  332. if (!ret)
  333. ret = register_sysrq_key('x', &sparc_globalpmu_op);
  334. return ret;
  335. }
  336. core_initcall(sparc_sysrq_init);
  337. #endif
  338. /* Free current thread data structures etc.. */
  339. void exit_thread(struct task_struct *tsk)
  340. {
  341. struct thread_info *t = task_thread_info(tsk);
  342. if (t->utraps) {
  343. if (t->utraps[0] < 2)
  344. kfree (t->utraps);
  345. else
  346. t->utraps[0]--;
  347. }
  348. }
  349. void flush_thread(void)
  350. {
  351. struct thread_info *t = current_thread_info();
  352. struct mm_struct *mm;
  353. mm = t->task->mm;
  354. if (mm)
  355. tsb_context_switch(mm);
  356. set_thread_wsaved(0);
  357. /* Clear FPU register state. */
  358. t->fpsaved[0] = 0;
  359. }
  360. /* It's a bit more tricky when 64-bit tasks are involved... */
  361. static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
  362. {
  363. bool stack_64bit = test_thread_64bit_stack(psp);
  364. unsigned long fp, distance, rval;
  365. if (stack_64bit) {
  366. csp += STACK_BIAS;
  367. psp += STACK_BIAS;
  368. __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
  369. fp += STACK_BIAS;
  370. if (test_thread_flag(TIF_32BIT))
  371. fp &= 0xffffffff;
  372. } else
  373. __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
  374. /* Now align the stack as this is mandatory in the Sparc ABI
  375. * due to how register windows work. This hides the
  376. * restriction from thread libraries etc.
  377. */
  378. csp &= ~15UL;
  379. distance = fp - psp;
  380. rval = (csp - distance);
  381. if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
  382. rval = 0;
  383. else if (!stack_64bit) {
  384. if (put_user(((u32)csp),
  385. &(((struct reg_window32 __user *)rval)->ins[6])))
  386. rval = 0;
  387. } else {
  388. if (put_user(((u64)csp - STACK_BIAS),
  389. &(((struct reg_window __user *)rval)->ins[6])))
  390. rval = 0;
  391. else
  392. rval = rval - STACK_BIAS;
  393. }
  394. return rval;
  395. }
  396. /* Standard stuff. */
  397. static inline void shift_window_buffer(int first_win, int last_win,
  398. struct thread_info *t)
  399. {
  400. int i;
  401. for (i = first_win; i < last_win; i++) {
  402. t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
  403. memcpy(&t->reg_window[i], &t->reg_window[i+1],
  404. sizeof(struct reg_window));
  405. }
  406. }
  407. void synchronize_user_stack(void)
  408. {
  409. struct thread_info *t = current_thread_info();
  410. unsigned long window;
  411. flush_user_windows();
  412. if ((window = get_thread_wsaved()) != 0) {
  413. window -= 1;
  414. do {
  415. struct reg_window *rwin = &t->reg_window[window];
  416. int winsize = sizeof(struct reg_window);
  417. unsigned long sp;
  418. sp = t->rwbuf_stkptrs[window];
  419. if (test_thread_64bit_stack(sp))
  420. sp += STACK_BIAS;
  421. else
  422. winsize = sizeof(struct reg_window32);
  423. if (!copy_to_user((char __user *)sp, rwin, winsize)) {
  424. shift_window_buffer(window, get_thread_wsaved() - 1, t);
  425. set_thread_wsaved(get_thread_wsaved() - 1);
  426. }
  427. } while (window--);
  428. }
  429. }
  430. static void stack_unaligned(unsigned long sp)
  431. {
  432. force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *) sp, 0, current);
  433. }
  434. static const char uwfault32[] = KERN_INFO \
  435. "%s[%d]: bad register window fault: SP %08lx (orig_sp %08lx) TPC %08lx O7 %08lx\n";
  436. static const char uwfault64[] = KERN_INFO \
  437. "%s[%d]: bad register window fault: SP %016lx (orig_sp %016lx) TPC %08lx O7 %016lx\n";
  438. void fault_in_user_windows(struct pt_regs *regs)
  439. {
  440. struct thread_info *t = current_thread_info();
  441. unsigned long window;
  442. flush_user_windows();
  443. window = get_thread_wsaved();
  444. if (likely(window != 0)) {
  445. window -= 1;
  446. do {
  447. struct reg_window *rwin = &t->reg_window[window];
  448. int winsize = sizeof(struct reg_window);
  449. unsigned long sp, orig_sp;
  450. orig_sp = sp = t->rwbuf_stkptrs[window];
  451. if (test_thread_64bit_stack(sp))
  452. sp += STACK_BIAS;
  453. else
  454. winsize = sizeof(struct reg_window32);
  455. if (unlikely(sp & 0x7UL))
  456. stack_unaligned(sp);
  457. if (unlikely(copy_to_user((char __user *)sp,
  458. rwin, winsize))) {
  459. if (show_unhandled_signals)
  460. printk_ratelimited(is_compat_task() ?
  461. uwfault32 : uwfault64,
  462. current->comm, current->pid,
  463. sp, orig_sp,
  464. regs->tpc,
  465. regs->u_regs[UREG_I7]);
  466. goto barf;
  467. }
  468. } while (window--);
  469. }
  470. set_thread_wsaved(0);
  471. return;
  472. barf:
  473. set_thread_wsaved(window + 1);
  474. force_sig(SIGSEGV, current);
  475. }
  476. asmlinkage long sparc_do_fork(unsigned long clone_flags,
  477. unsigned long stack_start,
  478. struct pt_regs *regs,
  479. unsigned long stack_size)
  480. {
  481. int __user *parent_tid_ptr, *child_tid_ptr;
  482. unsigned long orig_i1 = regs->u_regs[UREG_I1];
  483. long ret;
  484. #ifdef CONFIG_COMPAT
  485. if (test_thread_flag(TIF_32BIT)) {
  486. parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
  487. child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
  488. } else
  489. #endif
  490. {
  491. parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
  492. child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
  493. }
  494. ret = do_fork(clone_flags, stack_start, stack_size,
  495. parent_tid_ptr, child_tid_ptr);
  496. /* If we get an error and potentially restart the system
  497. * call, we're screwed because copy_thread() clobbered
  498. * the parent's %o1. So detect that case and restore it
  499. * here.
  500. */
  501. if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
  502. regs->u_regs[UREG_I1] = orig_i1;
  503. return ret;
  504. }
  505. /* Copy a Sparc thread. The fork() return value conventions
  506. * under SunOS are nothing short of bletcherous:
  507. * Parent --> %o0 == childs pid, %o1 == 0
  508. * Child --> %o0 == parents pid, %o1 == 1
  509. */
  510. int copy_thread(unsigned long clone_flags, unsigned long sp,
  511. unsigned long arg, struct task_struct *p)
  512. {
  513. struct thread_info *t = task_thread_info(p);
  514. struct pt_regs *regs = current_pt_regs();
  515. struct sparc_stackf *parent_sf;
  516. unsigned long child_stack_sz;
  517. char *child_trap_frame;
  518. /* Calculate offset to stack_frame & pt_regs */
  519. child_stack_sz = (STACKFRAME_SZ + TRACEREG_SZ);
  520. child_trap_frame = (task_stack_page(p) +
  521. (THREAD_SIZE - child_stack_sz));
  522. t->new_child = 1;
  523. t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
  524. t->kregs = (struct pt_regs *) (child_trap_frame +
  525. sizeof(struct sparc_stackf));
  526. t->fpsaved[0] = 0;
  527. if (unlikely(p->flags & PF_KTHREAD)) {
  528. memset(child_trap_frame, 0, child_stack_sz);
  529. __thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] =
  530. (current_pt_regs()->tstate + 1) & TSTATE_CWP;
  531. t->current_ds = ASI_P;
  532. t->kregs->u_regs[UREG_G1] = sp; /* function */
  533. t->kregs->u_regs[UREG_G2] = arg;
  534. return 0;
  535. }
  536. parent_sf = ((struct sparc_stackf *) regs) - 1;
  537. memcpy(child_trap_frame, parent_sf, child_stack_sz);
  538. if (t->flags & _TIF_32BIT) {
  539. sp &= 0x00000000ffffffffUL;
  540. regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
  541. }
  542. t->kregs->u_regs[UREG_FP] = sp;
  543. __thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] =
  544. (regs->tstate + 1) & TSTATE_CWP;
  545. t->current_ds = ASI_AIUS;
  546. if (sp != regs->u_regs[UREG_FP]) {
  547. unsigned long csp;
  548. csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
  549. if (!csp)
  550. return -EFAULT;
  551. t->kregs->u_regs[UREG_FP] = csp;
  552. }
  553. if (t->utraps)
  554. t->utraps[0]++;
  555. /* Set the return value for the child. */
  556. t->kregs->u_regs[UREG_I0] = current->pid;
  557. t->kregs->u_regs[UREG_I1] = 1;
  558. /* Set the second return value for the parent. */
  559. regs->u_regs[UREG_I1] = 0;
  560. if (clone_flags & CLONE_SETTLS)
  561. t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
  562. return 0;
  563. }
  564. /* TIF_MCDPER in thread info flags for current task is updated lazily upon
  565. * a context switch. Update this flag in current task's thread flags
  566. * before dup so the dup'd task will inherit the current TIF_MCDPER flag.
  567. */
  568. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  569. {
  570. if (adi_capable()) {
  571. register unsigned long tmp_mcdper;
  572. __asm__ __volatile__(
  573. ".word 0x83438000\n\t" /* rd %mcdper, %g1 */
  574. "mov %%g1, %0\n\t"
  575. : "=r" (tmp_mcdper)
  576. :
  577. : "g1");
  578. if (tmp_mcdper)
  579. set_thread_flag(TIF_MCDPER);
  580. else
  581. clear_thread_flag(TIF_MCDPER);
  582. }
  583. *dst = *src;
  584. return 0;
  585. }
  586. typedef struct {
  587. union {
  588. unsigned int pr_regs[32];
  589. unsigned long pr_dregs[16];
  590. } pr_fr;
  591. unsigned int __unused;
  592. unsigned int pr_fsr;
  593. unsigned char pr_qcnt;
  594. unsigned char pr_q_entrysize;
  595. unsigned char pr_en;
  596. unsigned int pr_q[64];
  597. } elf_fpregset_t32;
  598. /*
  599. * fill in the fpu structure for a core dump.
  600. */
  601. int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
  602. {
  603. unsigned long *kfpregs = current_thread_info()->fpregs;
  604. unsigned long fprs = current_thread_info()->fpsaved[0];
  605. if (test_thread_flag(TIF_32BIT)) {
  606. elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
  607. if (fprs & FPRS_DL)
  608. memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
  609. sizeof(unsigned int) * 32);
  610. else
  611. memset(&fpregs32->pr_fr.pr_regs[0], 0,
  612. sizeof(unsigned int) * 32);
  613. fpregs32->pr_qcnt = 0;
  614. fpregs32->pr_q_entrysize = 8;
  615. memset(&fpregs32->pr_q[0], 0,
  616. (sizeof(unsigned int) * 64));
  617. if (fprs & FPRS_FEF) {
  618. fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
  619. fpregs32->pr_en = 1;
  620. } else {
  621. fpregs32->pr_fsr = 0;
  622. fpregs32->pr_en = 0;
  623. }
  624. } else {
  625. if(fprs & FPRS_DL)
  626. memcpy(&fpregs->pr_regs[0], kfpregs,
  627. sizeof(unsigned int) * 32);
  628. else
  629. memset(&fpregs->pr_regs[0], 0,
  630. sizeof(unsigned int) * 32);
  631. if(fprs & FPRS_DU)
  632. memcpy(&fpregs->pr_regs[16], kfpregs+16,
  633. sizeof(unsigned int) * 32);
  634. else
  635. memset(&fpregs->pr_regs[16], 0,
  636. sizeof(unsigned int) * 32);
  637. if(fprs & FPRS_FEF) {
  638. fpregs->pr_fsr = current_thread_info()->xfsr[0];
  639. fpregs->pr_gsr = current_thread_info()->gsr[0];
  640. } else {
  641. fpregs->pr_fsr = fpregs->pr_gsr = 0;
  642. }
  643. fpregs->pr_fprs = fprs;
  644. }
  645. return 1;
  646. }
  647. EXPORT_SYMBOL(dump_fpu);
  648. unsigned long get_wchan(struct task_struct *task)
  649. {
  650. unsigned long pc, fp, bias = 0;
  651. struct thread_info *tp;
  652. struct reg_window *rw;
  653. unsigned long ret = 0;
  654. int count = 0;
  655. if (!task || task == current ||
  656. task->state == TASK_RUNNING)
  657. goto out;
  658. tp = task_thread_info(task);
  659. bias = STACK_BIAS;
  660. fp = task_thread_info(task)->ksp + bias;
  661. do {
  662. if (!kstack_valid(tp, fp))
  663. break;
  664. rw = (struct reg_window *) fp;
  665. pc = rw->ins[7];
  666. if (!in_sched_functions(pc)) {
  667. ret = pc;
  668. goto out;
  669. }
  670. fp = rw->ins[6] + bias;
  671. } while (++count < 16);
  672. out:
  673. return ret;
  674. }