📄 process.c
字号:
void exit_thread(void){ struct thread_info *t = current_thread_info(); if (t->utraps) { if (t->utraps[0] < 2) kfree (t->utraps); else t->utraps[0]--; } if (test_and_clear_thread_flag(TIF_PERFCTR)) { t->user_cntd0 = t->user_cntd1 = NULL; t->pcr_reg = 0; write_pcr(0); }}void flush_thread(void){ struct thread_info *t = current_thread_info(); if (t->task->mm) { unsigned long pgd_cache = 0UL; if (test_thread_flag(TIF_32BIT)) { struct mm_struct *mm = t->task->mm; pgd_t *pgd0 = &mm->pgd[0]; if (pgd_none(*pgd0)) { pmd_t *page = pmd_alloc_one_fast(NULL, 0); if (!page) page = pmd_alloc_one(NULL, 0); pgd_set(pgd0, page); } pgd_cache = pgd_val(*pgd0) << 11UL; } __asm__ __volatile__("stxa %0, [%1] %2\n\t" "membar #Sync" : /* no outputs */ : "r" (pgd_cache), "r" (TSB_REG), "i" (ASI_DMMU)); } set_thread_wsaved(0); /* Turn off performance counters if on. */ if (test_and_clear_thread_flag(TIF_PERFCTR)) { t->user_cntd0 = t->user_cntd1 = NULL; t->pcr_reg = 0; write_pcr(0); } /* Clear FPU register state. */ t->fpsaved[0] = 0; if (get_thread_current_ds() != ASI_AIUS) set_fs(USER_DS); /* Init new signal delivery disposition. */ clear_thread_flag(TIF_NEWSIGNALS);}/* It's a bit more tricky when 64-bit tasks are involved... */static unsigned long clone_stackframe(unsigned long csp, unsigned long psp){ unsigned long fp, distance, rval; if (!(test_thread_flag(TIF_32BIT))) { csp += STACK_BIAS; psp += STACK_BIAS; __get_user(fp, &(((struct reg_window *)psp)->ins[6])); fp += STACK_BIAS; } else __get_user(fp, &(((struct reg_window32 *)psp)->ins[6])); /* Now 8-byte align the stack as this is mandatory in the * Sparc ABI due to how register windows work. This hides * the restriction from thread libraries etc. -DaveM */ csp &= ~7UL; distance = fp - psp; rval = (csp - distance); if (copy_in_user(rval, psp, distance)) rval = 0; else if (test_thread_flag(TIF_32BIT)) { if (put_user(((u32)csp), &(((struct reg_window32 *)rval)->ins[6]))) rval = 0; } else { if (put_user(((u64)csp - STACK_BIAS), &(((struct reg_window *)rval)->ins[6]))) rval = 0; else rval = rval - STACK_BIAS; } return rval;}/* Standard stuff. */static inline void shift_window_buffer(int first_win, int last_win, struct thread_info *t){ int i; for (i = first_win; i < last_win; i++) { t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1]; memcpy(&t->reg_window[i], &t->reg_window[i+1], sizeof(struct reg_window)); }}void synchronize_user_stack(void){ struct thread_info *t = current_thread_info(); unsigned long window; flush_user_windows(); if ((window = get_thread_wsaved()) != 0) { int winsize = REGWIN_SZ; int bias = 0; if (test_thread_flag(TIF_32BIT)) winsize = REGWIN32_SZ; else bias = STACK_BIAS; window -= 1; do { unsigned long sp = (t->rwbuf_stkptrs[window] + bias); struct reg_window *rwin = &t->reg_window[window]; if (!copy_to_user((char *)sp, rwin, winsize)) { shift_window_buffer(window, get_thread_wsaved() - 1, t); set_thread_wsaved(get_thread_wsaved() - 1); } } while (window--); }}void fault_in_user_windows(void){ struct thread_info *t = current_thread_info(); unsigned long window; int winsize = REGWIN_SZ; int bias = 0; if (test_thread_flag(TIF_32BIT)) winsize = REGWIN32_SZ; else bias = STACK_BIAS; flush_user_windows(); window = get_thread_wsaved(); if (window != 0) { window -= 1; do { unsigned long sp = (t->rwbuf_stkptrs[window] + bias); struct reg_window *rwin = &t->reg_window[window]; if (copy_to_user((char *)sp, rwin, winsize)) goto barf; } while (window--); } set_thread_wsaved(0); return;barf: set_thread_wsaved(window + 1); do_exit(SIGILL);}/* Copy a Sparc thread. The fork() return value conventions * under SunOS are nothing short of bletcherous: * Parent --> %o0 == childs pid, %o1 == 0 * Child --> %o0 == parents pid, %o1 == 1 * * NOTE: We have a separate fork kpsr/kwim because * the parent could change these values between * sys_fork invocation and when we reach here * if the parent should sleep while trying to * allocate the task_struct and kernel stack in * do_fork(). */int copy_thread(int nr, unsigned long clone_flags, unsigned long sp, unsigned long unused, struct task_struct *p, struct pt_regs *regs){ struct thread_info *t = p->thread_info; char *child_trap_frame;#ifdef CONFIG_DEBUG_SPINLOCK p->thread.smp_lock_count = 0; p->thread.smp_lock_pc = 0;#endif /* Calculate offset to stack_frame & pt_regs */ child_trap_frame = ((char *)t) + (THREAD_SIZE - (TRACEREG_SZ+REGWIN_SZ)); memcpy(child_trap_frame, (((struct reg_window *)regs)-1), (TRACEREG_SZ+REGWIN_SZ)); t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) | (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) | _TIF_NEWCHILD | (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT); t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS; t->kregs = (struct pt_regs *)(child_trap_frame+sizeof(struct reg_window)); t->fpsaved[0] = 0; if (regs->tstate & TSTATE_PRIV) { /* Special case, if we are spawning a kernel thread from * a userspace task (via KMOD, NFS, or similar) we must * disable performance counters in the child because the * address space and protection realm are changing. */ if (t->flags & _TIF_PERFCTR) { t->user_cntd0 = t->user_cntd1 = NULL; t->pcr_reg = 0; t->flags &= ~_TIF_PERFCTR; } t->kregs->u_regs[UREG_FP] = t->ksp; t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT); flush_register_windows(); memcpy((void *)(t->ksp + STACK_BIAS), (void *)(regs->u_regs[UREG_FP] + STACK_BIAS), sizeof(struct reg_window)); t->kregs->u_regs[UREG_G6] = (unsigned long) t; t->kregs->u_regs[UREG_G4] = (unsigned long) t->task; } else { if (t->flags & _TIF_32BIT) { sp &= 0x00000000ffffffffUL; regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL; } t->kregs->u_regs[UREG_FP] = sp; t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT); if (sp != regs->u_regs[UREG_FP]) { unsigned long csp; csp = clone_stackframe(sp, regs->u_regs[UREG_FP]); if (!csp) return -EFAULT; t->kregs->u_regs[UREG_FP] = csp; } if (t->utraps) t->utraps[0]++; } /* Set the return value for the child. */ t->kregs->u_regs[UREG_I0] = current->pid; t->kregs->u_regs[UREG_I1] = 1; /* Set the second return value for the parent. */ regs->u_regs[UREG_I1] = 0; return 0;}/* * This is the mechanism for creating a new kernel thread. * * NOTE! Only a kernel-only process(ie the swapper or direct descendants * who haven't done an "execve()") should use this: it will work within * a system call from a "real" process, but the process memory space will * not be free'd until both the parent and the child have exited. */pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags){ long retval; /* If the parent runs before fn(arg) is called by the child, * the input registers of this function can be clobbered. * So we stash 'fn' and 'arg' into global registers which * will not be modified by the parent. */ __asm__ __volatile("mov %4, %%g2\n\t" /* Save FN into global */ "mov %5, %%g3\n\t" /* Save ARG into global */ "mov %1, %%g1\n\t" /* Clone syscall nr. */ "mov %2, %%o0\n\t" /* Clone flags. */ "mov 0, %%o1\n\t" /* usp arg == 0 */ "t 0x6d\n\t" /* Linux/Sparc clone(). */ "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */ " mov %%o0, %0\n\t" "jmpl %%g2, %%o7\n\t" /* Call the function. */ " mov %%g3, %%o0\n\t" /* Set arg in delay. */ "mov %3, %%g1\n\t" "t 0x6d\n\t" /* Linux/Sparc exit(). */ /* Notreached by child. */ "1:" : "=r" (retval) : "i" (__NR_clone), "r" (flags | CLONE_VM), "i" (__NR_exit), "r" (fn), "r" (arg) : "g1", "g2", "g3", "o0", "o1", "memory", "cc"); return retval;}/* * fill in the user structure for a core dump.. */void dump_thread(struct pt_regs * regs, struct user * dump){ /* Only should be used for SunOS and ancient a.out * SparcLinux binaries... Not worth implementing. */ memset(dump, 0, sizeof(struct user));}typedef struct { union { unsigned int pr_regs[32]; unsigned long pr_dregs[16]; } pr_fr; unsigned int __unused; unsigned int pr_fsr; unsigned char pr_qcnt; unsigned char pr_q_entrysize; unsigned char pr_en; unsigned int pr_q[64];} elf_fpregset_t32;/* * fill in the fpu structure for a core dump. */int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs){ unsigned long *kfpregs = current_thread_info()->fpregs; unsigned long fprs = current_thread_info()->fpsaved[0]; if (test_thread_flag(TIF_32BIT)) { elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs; if (fprs & FPRS_DL) memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs, sizeof(unsigned int) * 32); else memset(&fpregs32->pr_fr.pr_regs[0], 0, sizeof(unsigned int) * 32); fpregs32->pr_qcnt = 0; fpregs32->pr_q_entrysize = 8; memset(&fpregs32->pr_q[0], 0, (sizeof(unsigned int) * 64)); if (fprs & FPRS_FEF) { fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0]; fpregs32->pr_en = 1; } else { fpregs32->pr_fsr = 0; fpregs32->pr_en = 0; } } else { if(fprs & FPRS_DL) memcpy(&fpregs->pr_regs[0], kfpregs, sizeof(unsigned int) * 32); else memset(&fpregs->pr_regs[0], 0, sizeof(unsigned int) * 32); if(fprs & FPRS_DU) memcpy(&fpregs->pr_regs[16], kfpregs+16, sizeof(unsigned int) * 32); else memset(&fpregs->pr_regs[16], 0, sizeof(unsigned int) * 32); if(fprs & FPRS_FEF) { fpregs->pr_fsr = current_thread_info()->xfsr[0]; fpregs->pr_gsr = current_thread_info()->gsr[0]; } else { fpregs->pr_fsr = fpregs->pr_gsr = 0; } fpregs->pr_fprs = fprs; } return 1;}/* * sparc_execve() executes a new program after the asm stub has set * things up for us. This should basically do what I want it to. */asmlinkage int sparc_execve(struct pt_regs *regs){ int error, base = 0; char *filename; /* User register window flush is done by entry.S */ /* Check for indirect call. */ if (regs->u_regs[UREG_G1] == 0) base = 1; filename = getname((char *)regs->u_regs[base + UREG_I0]); error = PTR_ERR(filename); if (IS_ERR(filename)) goto out; error = do_execve(filename, (char **) regs->u_regs[base + UREG_I1], (char **) regs->u_regs[base + UREG_I2], regs); putname(filename); if (!error) { fprs_write(0); current_thread_info()->xfsr[0] = 0; current_thread_info()->fpsaved[0] = 0; regs->tstate &= ~TSTATE_PEF; }out: return error;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -