process.c 23 KB

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