process.c 23 KB

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