process.c 20 KB

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