process.c 23 KB

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