示例#1
0
static void  
exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
    u_long stack)
{
	struct trapframe *tf;
	register_t entry_desc[3];

	tf = trapframe(td);
	exec_setregs(td, imgp, stack);

	/*
	 * For 64-bit ELFv1, we need to disentangle the function
	 * descriptor
	 *
	 * 0. entry point
	 * 1. TOC value (r2)
	 * 2. Environment pointer (r11)
	 */

	(void)copyin((void *)imgp->entry_addr, entry_desc,
	    sizeof(entry_desc));
	tf->srr0 = entry_desc[0] + imgp->reloc_base;
	tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
	tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
}
示例#2
0
文件: altivec.c 项目: 2asoft/freebsd
void
enable_vec(struct thread *td)
{
	int	msr;
	struct	pcb *pcb;
	struct	trapframe *tf;

	pcb = td->td_pcb;
	tf = trapframe(td);

	/*
	 * Save the thread's Altivec CPU number, and set the CPU's current
	 * vector thread
	 */
	td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
	PCPU_SET(vecthread, td);

	/*
	 * Enable the vector unit for when the thread returns from the
	 * exception. If this is the first time the unit has been used by
	 * the thread, initialise the vector registers and VSCR to 0, and
	 * set the flag to indicate that the vector unit is in use.
	 */
	tf->srr1 |= PSL_VEC;
	if (!(pcb->pcb_flags & PCB_VEC)) {
		memset(&pcb->pcb_vec, 0, sizeof pcb->pcb_vec);
		pcb->pcb_flags |= PCB_VEC;
	}

	/*
	 * Temporarily enable the vector unit so the registers
	 * can be restored.
	 */
	msr = mfmsr();
	mtmsr(msr | PSL_VEC);
	isync();

	/*
	 * Restore VSCR by first loading it into a vector and then into VSCR.
	 * (this needs to done before loading the user's vector registers
	 * since we need to use a scratch vector register)
	 */
	__asm __volatile("vxor 0,0,0; lvewx 0,0,%0; mtvscr 0" \
			  :: "b"(&pcb->pcb_vec.vscr));

#define LVX(n)   __asm ("lvx " #n ",0,%0" \
		:: "b"(&pcb->pcb_vec.vr[n]));
	LVX(0);		LVX(1);		LVX(2);		LVX(3);
	LVX(4);		LVX(5);		LVX(6);		LVX(7);
	LVX(8);		LVX(9);		LVX(10);	LVX(11);
	LVX(12);	LVX(13);	LVX(14);	LVX(15);
	LVX(16);	LVX(17);	LVX(18);	LVX(19);
	LVX(20);	LVX(21);	LVX(22);	LVX(23);
	LVX(24);	LVX(25);	LVX(26);	LVX(27);
	LVX(28);	LVX(29);	LVX(30);	LVX(31);
#undef LVX

	isync();
	mtmsr(msr);
}
示例#3
0
/*
 * Set the process's program counter.
 */
int
process_set_pc(struct lwp *l, void *addr)
{
	struct trapframe * const tf = trapframe(l);
	
	tf->srr0 = (register_t)addr;
	return 0;
}
示例#4
0
int
process_sstep(struct lwp *l, int sstep)
{
	struct trapframe *tf = trapframe(l);
	
	if (sstep)
		tf->srr1 |= PSL_SE;
	else
		tf->srr1 &= ~PSL_SE;
	return 0;
}
示例#5
0
int
process_write_regs(struct lwp *l, const struct reg *regs)
{
	struct trapframe * const tf = trapframe(l);

	memcpy(tf->fixreg, regs->fixreg, sizeof(regs->fixreg));
	tf->lr = regs->lr;
	tf->cr = regs->cr;
	tf->xer = regs->xer;
	tf->ctr = regs->ctr;
	tf->srr0 = regs->pc;

	return 0;
}
示例#6
0
int
process_read_regs(struct lwp *l, struct reg *regs)
{
	struct trapframe * const tf = trapframe(l);

	memcpy(regs->fixreg, tf->fixreg, sizeof(regs->fixreg));
	regs->lr = tf->lr;
	regs->cr = tf->cr;
	regs->xer = tf->xer;
	regs->ctr = tf->ctr;
	regs->pc = tf->srr0;

	return 0;
}
示例#7
0
/*
 * The following needs code review for potential security issues
 */
int
linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
{
	/* {
		syscallarg(struct linux_sigcontext *) scp;
	} */
	struct proc *p = l->l_proc;
	struct linux_sigcontext *scp, context;
	struct linux_sigregs sregs;
	struct linux_pt_regs *lregs;
	struct trapframe *tf;
	sigset_t mask;
	int i;

	/*
	 * The trampoline code hands us the context.
	 * It is unsafe to keep track of it ourselves, in the event that a
	 * program jumps out of a signal handler.
	 */
	scp = SCARG(uap, scp);

	/*
	 * Get the context from user stack
	 */
	if (copyin(scp, &context, sizeof(*scp)))
		return (EFAULT);

	/*
	 *  Restore register context.
	 */
	if (copyin((void *)context.lregs, &sregs, sizeof(sregs)))
		return (EFAULT);
	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;

	tf = trapframe(l);
#ifdef DEBUG_LINUX
	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
	    (unsigned long)tf, (unsigned long)scp);
#endif

	if (!PSL_USEROK_P(lregs->lmsr))
		return (EINVAL);

	for (i = 0; i < 32; i++)
		tf->tf_fixreg[i] = lregs->lgpr[i];
	tf->tf_lr = lregs->llink;
	tf->tf_cr = lregs->lccr;
	tf->tf_xer = lregs->lxer;
	tf->tf_ctr = lregs->lctr;
	tf->tf_srr0 = lregs->lnip;
	tf->tf_srr1 = lregs->lmsr;

	/*
	 * Make sure the fpu state is discarded
	 */
#ifdef PPC_HAVE_FPU
	fpu_discard();
#endif

	memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
	       sizeof(curpcb->pcb_fpu.fpreg));

	fpu_mark_used(curlwp);

	mutex_enter(p->p_lock);

	/*
	 * Restore signal stack.
	 *
	 * XXX cannot find the onstack information in Linux sig context.
	 * Is signal stack really supported on Linux?
	 */
#if 0
	if (sc.sc_onstack & SS_ONSTACK)
		l->l_sigstk.ss_flags |= SS_ONSTACK;
	else
#endif
		l->l_sigstk.ss_flags &= ~SS_ONSTACK;

	/* Restore signal mask. */
	linux_old_extra_to_native_sigset(&mask, &context.lmask,
	    &context._unused[3]);
	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);

	mutex_exit(p->p_lock);

	return (EJUSTRETURN);
}
示例#8
0
void
linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
{
	const int sig = ksi->ksi_signo;
	struct lwp *l = curlwp;
	struct proc *p = l->l_proc;
	struct trapframe *tf;
	sig_t catcher = SIGACTION(p, sig).sa_handler;
	struct linux_sigregs frame;
	struct linux_pt_regs linux_regs;
	struct linux_sigcontext sc;
	register_t fp;
	int onstack, error;
	int i;

	tf = trapframe(l);

	/*
	 * Do we need to jump onto the signal stack?
	 */
	onstack =
	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;

	/*
	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
	 * not use it yet. XXX fix this.
	 */
	onstack=0;

	/*
	 * Allocate space for the signal handler context.
	 */
	if (onstack) {
		fp = (register_t)
		    ((char *)l->l_sigstk.ss_sp +
		    l->l_sigstk.ss_size);
	} else {
		fp = tf->tf_fixreg[1];
	}
#ifdef DEBUG_LINUX
	printf("fp at start of linux_sendsig = %x\n", fp);
#endif
	fp -= sizeof(struct linux_sigregs);
	fp &= ~0xf;

	/*
	 * Prepare a sigcontext for later.
	 */
	memset(&sc, 0, sizeof sc);
	sc.lsignal = (int)native_to_linux_signo[sig];
	sc.lhandler = (unsigned long)catcher;
	native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
	sc.lregs = (struct linux_pt_regs*)fp;

	/*
	 * Setup the signal stack frame as Linux does it in
	 * arch/ppc/kernel/signal.c:setup_frame()
	 *
	 * Save register context.
	 */
	for (i = 0; i < 32; i++)
		linux_regs.lgpr[i] = tf->tf_fixreg[i];
	linux_regs.lnip = tf->tf_srr0;
	linux_regs.lmsr = tf->tf_srr1 & PSL_USERSRR1;
	linux_regs.lorig_gpr3 = tf->tf_fixreg[3]; /* XXX Is that right? */
	linux_regs.lctr = tf->tf_ctr;
	linux_regs.llink = tf->tf_lr;
	linux_regs.lxer = tf->tf_xer;
	linux_regs.lccr = tf->tf_cr;
	linux_regs.lmq = 0;  			/* Unused, 601 only */
	linux_regs.ltrap = tf->tf_exc;
	linux_regs.ldar = tf->tf_dar;
	linux_regs.ldsisr = tf->tf_dsisr;
	linux_regs.lresult = 0;

	memset(&frame, 0, sizeof(frame));
	memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));

#ifdef PPC_HAVE_FPU
	fpu_save();
#endif
	memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpreg, sizeof(frame.lfp_regs));

	/*
	 * Copy Linux's signal trampoline on the user stack It should not
	 * be used, but Linux binaries might expect it to be there.
	 */
	frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
	frame.ltramp[1] = 0x44000002; /* sc */

	/*
	 * Move it to the user stack
	 * There is a little trick here, about the LINUX_ABIGAP: the
	 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
	 * binaries. But the Linux kernel seems to do without it, and it
	 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
	 */
	sendsig_reset(l, sig);
	mutex_exit(p->p_lock);
	error = copyout(&frame, (void *)fp, sizeof (frame) - LINUX_ABIGAP);

	if (error != 0) {
		/*
		 * Process has trashed its stack; give it an illegal
		 * instruction to halt it in its tracks.
		 */
		mutex_enter(p->p_lock);
		sigexit(l, SIGILL);
		/* NOTREACHED */
	}

	/*
	 * Add a sigcontext on the stack
	 */
	fp -= sizeof(struct linux_sigcontext);
	error = copyout(&sc, (void *)fp, sizeof (struct linux_sigcontext));
	mutex_enter(p->p_lock);

	if (error != 0) {
		/*
		 * Process has trashed its stack; give it an illegal
		 * instruction to halt it in its tracks.
		 */
		sigexit(l, SIGILL);
		/* NOTREACHED */
	}

	/*
	 * Set the registers according to how the Linux process expects them.
	 * "Mind the gap" Linux expects a gap here.
	 */
	tf->tf_fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
	tf->tf_lr = (int)catcher;
	tf->tf_fixreg[3] = (int)native_to_linux_signo[sig];
	tf->tf_fixreg[4] = fp;
	tf->tf_srr0 = (int)p->p_sigctx.ps_sigcode;

#ifdef DEBUG_LINUX
	printf("fp at end of linux_sendsig = %x\n", fp);
#endif
	/*
	 * Remember that we're now on the signal stack.
	 */
	if (onstack)
		l->l_sigstk.ss_flags |= SS_ONSTACK;
#ifdef DEBUG_LINUX
	printf("linux_sendsig: exiting. fp=0x%lx\n",(long)fp);
#endif
}
示例#9
0
/*
 * Finish a fork operation, with execution context l2 nearly set up.
 * Copy and update the pcb and trap frame, making the child ready to run.
 *
 * Rig the child's kernel stack so that it will have a switch frame which
 * returns to cpu_lwp_bootstrap() which will call child_return() with l2
 * as its argument.  This causes the newly-created child process to go
 * directly to user level with an apparent return value of 0 from
 * fork(), while the parent process returns normally.
 *
 * l1 is the execution context being forked; if l1 == &lwp0, we are creating
 * a kernel thread, and the return path and argument are specified with
 * `func' and `arg'.
 *
 * If an alternate user-level stack is requested (with non-zero values
 * in both the stack and stacksize args), set up the user stack pointer
 * accordingly.
 */
void
cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
	void (*func)(void *), void *arg)
{

	/*
	 * If l1 != curlwp && l1 == &lwp0, we're creating a kernel thread.
	 */
	KASSERT(l1 == curlwp || l1 == &lwp0);

	struct pcb * const pcb1 = lwp_getpcb(l1);
	struct pcb * const pcb2 = lwp_getpcb(l2);

	/* Copy MD part of lwp and set up user trapframe pointer.  */
	l2->l_md = l1->l_md;
	l2->l_md.md_utf = trapframe(l2);

	/* Copy PCB. */
	*pcb2 = *pcb1;

	pcb2->pcb_pm = l2->l_proc->p_vmspace->vm_map.pmap;

	/*
	 * Setup the trap frame for the new process
	 */
	*l2->l_md.md_utf = *l1->l_md.md_utf;

	/*
	 * If specified, give the child a different stack.  Make sure to
	 * reserve enough at the top to store the previous LR.
	 */
	if (stack != NULL) {
		l2->l_md.md_utf->tf_fixreg[1] =	
		    ((register_t)stack + stacksize - STACK_ALIGNBYTES)
			& ~STACK_ALIGNBYTES;
	}

	/*
	 * Now deal setting up the initial function and its argument.
	 */
	struct ktrapframe * const ktf = ktrapframe(l2);
	struct callframe * const cf = ((struct callframe *)ktf) - 1;
	struct switchframe * const sf = ((struct switchframe *)cf) - 1;

	/*
	 * Align stack pointer
	 * struct ktrapframe has a partial callframe (sp & lr)
	 * followed by a real trapframe.  The partial callframe
	 * is for the callee to store LR.  The SP isn't really used
	 * since trap/syscall will use the SP in the trapframe.
	 * There happens to be a partial callframe in front of the
	 * trapframe, too.
	 */
	ktf->ktf_lr = (register_t) cpu_lwp_bootstrap;
	ktf->ktf_sp = (register_t) (ktf + 1);		/* just in case */

	cf->cf_sp = (register_t) ktf;
	cf->cf_r31 = (register_t) func;
	cf->cf_r30 = (register_t) arg;

	memset((void *)sf, 0, sizeof *sf);		/* just in case */
	sf->sf_sp = (register_t) cf;
#if defined (PPC_OEA) || defined (PPC_OEA64_BRIDGE)
	sf->sf_user_sr = pmap_kernel()->pm_sr[USER_SR]; /* again, just in case */
#endif
	pcb2->pcb_sp = (register_t)sf;
	pcb2->pcb_kmapsr = 0;
	pcb2->pcb_umapsr = 0;
#ifdef PPC_HAVE_FPU
	pcb2->pcb_flags = PSL_FE_DFLT;
#endif
#ifdef CACHE_PROTO_MEI
	{
		paddr_t pa;
		int dcache_line_size, i;

		/* Flush on cache values for other cpu. */

		dcache_line_size = curcpu()->ci_ci.dcache_line_size;
		pa = vtophys((vaddr_t)sf);
		for (i = 0; i < SFRAMELEN + CALLFRAMELEN + FRAMELEN;
		    i += dcache_line_size) {
			__asm volatile ("dcbf 0,%0"::"r"(pa):"memory");
			pa += dcache_line_size;
		}
		__asm volatile ("dcbf 0,%0"::"r"(pa):"memory");
		pa = vtophys((vaddr_t)pcb2->pcb_pm);
		for (i = 0; i < sizeof(*pcb2->pcb_pm); i += dcache_line_size) {
			__asm volatile ("dcbf 0,%0"::"r"(pa):"memory");
			pa += dcache_line_size;
		}
		__asm volatile ("dcbf 0,%0"::"r"(pa):"memory");
		pa = vtophys((vaddr_t)pcb2);
		for (i = 0; i < sizeof(*pcb2); i += dcache_line_size) {
			__asm volatile ("dcbf 0,%0"::"r"(pa):"memory");
			pa += dcache_line_size;
		}
		__asm volatile ("dcbf 0,%0"::"r"(pa):"memory");

		/* Need more flush? */
	}
#endif
}
示例#10
0
/*
 * Finish a fork operation, with process p2 nearly set up.
 */
void
cpu_fork(struct proc *p1, struct proc *p2, void *stack, size_t stacksize,
    void (*func)(void *), void *arg)
{
	struct trapframe *tf;
	struct callframe *cf;
	struct switchframe *sf;
	caddr_t stktop1, stktop2;
	extern void fork_trampoline(void);
	struct pcb *pcb = &p2->p_addr->u_pcb;
	struct cpu_info *ci = curcpu();

	if (p1 == ci->ci_fpuproc)
		save_fpu();
	*pcb = p1->p_addr->u_pcb;
	
#ifdef ALTIVEC
	if (p1->p_addr->u_pcb.pcb_vr != NULL) {
		if (p1 == ci->ci_vecproc)
			save_vec(p1);
		pcb->pcb_vr = pool_get(&ppc_vecpl, PR_WAITOK);
		*pcb->pcb_vr = *p1->p_addr->u_pcb.pcb_vr;
	} else
		pcb->pcb_vr = NULL;

#endif /* ALTIVEC */

	pcb->pcb_pm = p2->p_vmspace->vm_map.pmap;

	pmap_extract(pmap_kernel(),
	    (vaddr_t)pcb->pcb_pm, (paddr_t *)&pcb->pcb_pmreal);
	
	/*
	 * Setup the trap frame for the new process
	 */
	stktop1 = (caddr_t)trapframe(p1);
	stktop2 = (caddr_t)trapframe(p2);
	bcopy(stktop1, stktop2, sizeof(struct trapframe));

	/*
	 * If specified, give the child a different stack.
	 */
	if (stack != NULL) {
		tf = trapframe(p2);
		tf->fixreg[1] = (register_t)stack + stacksize;
	}

	stktop2 = (caddr_t)((u_long)stktop2 & ~15);  /* Align stack pointer */
	
	/*
	 * There happens to be a callframe, too.
	 */
	cf = (struct callframe *)stktop2;
	cf->lr = (int)fork_trampoline;
	
	/*
	 * Below the trap frame, there is another call frame:
	 */
	stktop2 -= 16;
	cf = (struct callframe *)stktop2;
	cf->r31 = (register_t)func;
	cf->r30 = (register_t)arg;
	
	/*
	 * Below that, we allocate the switch frame:
	 */
	/* must match SFRAMELEN in genassym */
	stktop2 -= roundup(sizeof *sf, 16);

	sf = (struct switchframe *)stktop2;
	bzero((void *)sf, sizeof *sf);		/* just in case */
	sf->sp = (int)cf;
	sf->user_sr = pmap_kernel()->pm_sr[PPC_USER_SR]; /* just in case */
	pcb->pcb_sp = (int)stktop2;
}