process.c 20 KB

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