process_64.c 22 KB

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