process.c 23 KB

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