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