process.c 23 KB

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