process.c 23 KB

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