process.c 22 KB

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