process.c 23 KB

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