process.c 22 KB

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