process.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:22k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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