Home | History | Annotate | Line # | Download | only in amd64
linux_machdep.c revision 1.1
      1 /*	$NetBSD: linux_machdep.c,v 1.1 2005/05/03 16:26:30 manu Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Emmanuel Dreyfus
     17  * 4. The name of the author may not be used to endorse or promote
     18  *    products derived from this software without specific prior written
     19  *    permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.1 2005/05/03 16:26:30 manu Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 #include <sys/systm.h>
     41 #include <sys/signal.h>
     42 #include <sys/exec.h>
     43 #include <sys/proc.h>
     44 #include <sys/ptrace.h> /* for process_read_fpregs() */
     45 #include <sys/user.h>
     46 #include <sys/ucontext.h>
     47 
     48 #include <machine/reg.h>
     49 #include <machine/pcb.h>
     50 #include <machine/fpu.h>
     51 #include <machine/mcontext.h>
     52 
     53 #include <compat/linux/common/linux_signal.h>
     54 #include <compat/linux/common/linux_errno.h>
     55 #include <compat/linux/common/linux_exec.h>
     56 #include <compat/linux/common/linux_ioctl.h>
     57 #include <compat/linux/common/linux_prctl.h>
     58 #include <compat/linux/common/linux_machdep.h>
     59 #include <compat/linux/linux_syscallargs.h>
     60 
     61 
     62 void
     63 linux_setregs(l, epp, stack)
     64         struct lwp *l;
     65 	struct exec_package *epp;
     66 	u_long stack;
     67 {
     68 	struct pcb *pcb = &l->l_addr->u_pcb;
     69 	struct trapframe *tf;
     70 
     71 	/* If we were using the FPU, forget about it. */
     72 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
     73 		fpusave_lwp(l, 0);
     74 
     75 	l->l_md.md_flags &= ~MDP_USEDFPU;
     76 	pcb->pcb_flags = 0;
     77 	pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
     78 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
     79 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
     80 
     81 	l->l_proc->p_flag &= ~P_32;
     82 
     83 	printf("stack = 0x%lx, entry = 0x%lx\n", stack, epp->ep_entry);
     84 	tf = l->l_md.md_regs;
     85 	tf->tf_rax = 0;
     86 	tf->tf_rbx = 0;
     87 	tf->tf_rcx = epp->ep_entry;
     88 	tf->tf_rdx = 0;
     89 	tf->tf_rsi = 0;
     90 	tf->tf_rdi = 0;
     91 	tf->tf_rbp = 0;
     92 	tf->tf_rsp = stack;
     93 	tf->tf_r8 = 0;
     94 	tf->tf_r9 = 0;
     95 	tf->tf_r10 = 0;
     96 	tf->tf_r11 = 0;
     97 	tf->tf_r12 = 0;
     98 	tf->tf_r13 = 0;
     99 	tf->tf_r14 = 0;
    100 	tf->tf_r15 = 0;
    101 	tf->tf_rip = epp->ep_entry;
    102 	tf->tf_rflags = PSL_MBO | PSL_I;
    103 	tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
    104 	tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
    105 	tf->tf_ds = 0;
    106 	tf->tf_es = 0;
    107 	tf->tf_fs = 0;
    108 	tf->tf_gs = 0;
    109 
    110 	return;
    111 }
    112 
    113 void
    114 linux_sendsig(ksi, mask)
    115 	const ksiginfo_t *ksi;
    116 	const sigset_t *mask;
    117 {
    118 	struct lwp *l = curlwp;
    119 	struct proc *p = l->l_proc;
    120 	struct sigacts *ps = p->p_sigacts;
    121 	int onstack;
    122 	int sig = ksi->ksi_signo;
    123 	struct linux_rt_sigframe *sfp, sigframe;
    124 	struct linux__fpstate *fpsp, fpstate;
    125 	struct fpreg fpregs;
    126 	struct trapframe *tf = l->l_md.md_regs;
    127 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    128 	linux_sigset_t lmask;
    129 	char *sp;
    130 	int error;
    131 
    132 	/* Do we need to jump onto the signal stack? */
    133 	onstack =
    134 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    135 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    136 
    137 	/* Allocate space for the signal handler context. */
    138 	if (onstack)
    139 		sp = ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
    140 		    p->p_sigctx.ps_sigstk.ss_size);
    141 	else
    142 		sp = (caddr_t)tf->tf_rsp - 128;
    143 
    144 
    145 	/*
    146 	 * Save FPU state, if any
    147 	 */
    148 	if (l->l_md.md_flags & MDP_USEDFPU) {
    149 		sp = (char *)
    150 		    (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
    151 		fpsp = (struct linux__fpstate *)sp;
    152 
    153 		(void)process_read_fpregs(l, &fpregs);
    154 		bzero(&fpstate, sizeof(fpstate));
    155 
    156 		fpstate.cwd = fpregs.fp_fcw;
    157 		fpstate.swd = fpregs.fp_fsw;
    158 		fpstate.twd = fpregs.fp_ftw;
    159 		fpstate.fop = fpregs.fp_fop;
    160 		fpstate.rip = fpregs.fp_rip;
    161 		fpstate.rdp = fpregs.fp_rdp;
    162 		fpstate.mxcsr = fpregs.fp_mxcsr;
    163 		fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
    164 		memcpy(&fpstate.st_space, &fpregs.fp_st,
    165 		    sizeof(fpstate.st_space));
    166 		memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
    167 		    sizeof(fpstate.xmm_space));
    168 
    169 		if ((error = copyout(&fpstate, fpsp, sizeof(fpstate))) != 0) {
    170 			sigexit(l, SIGILL);
    171 			return;
    172 		}
    173 	} else {
    174 		fpsp = NULL;
    175 	}
    176 
    177 	/*
    178 	 * Populate the rt_sigframe
    179 	 */
    180 	sp = (char *)
    181 	    ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
    182 	sfp = (struct linux_rt_sigframe *)sp;
    183 
    184 	bzero(&sigframe, sizeof(sigframe));
    185 	sigframe.pretcode = (char *)ps->sa_sigdesc[sig].sd_tramp;
    186 
    187 	/*
    188 	 * The user context
    189 	 */
    190 	sigframe.uc.luc_flags = 0;
    191 	sigframe.uc.luc_link = NULL;
    192 
    193 	/* This is used regardless of SA_ONSTACK in Linux */
    194 	sigframe.uc.luc_stack.ss_sp = p->p_sigctx.ps_sigstk.ss_sp;
    195 	sigframe.uc.luc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
    196 	sigframe.uc.luc_stack.ss_flags = 0;
    197 	if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    198 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
    199 	if (p->p_sigctx.ps_sigstk.ss_flags & SS_DISABLE)
    200 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
    201 
    202 	sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
    203 	sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
    204 	sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
    205 	sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
    206 	sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
    207 	sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
    208 	sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
    209 	sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
    210 	sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
    211 	sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
    212 	sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
    213 	sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
    214 	sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
    215 	sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
    216 	sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
    217 	sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
    218 	sigframe.uc.luc_mcontext.cs = tf->tf_cs;
    219 	sigframe.uc.luc_mcontext.gs = tf->tf_gs;
    220 	sigframe.uc.luc_mcontext.fs = tf->tf_fs;
    221 	sigframe.uc.luc_mcontext.err = tf->tf_err;
    222 	sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
    223 	native_to_linux_sigset(&lmask, mask);
    224 	sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
    225 	sigframe.uc.luc_mcontext.cr2 = (long)l->l_addr->u_pcb.pcb_onfault;
    226 	sigframe.uc.luc_mcontext.fpstate = fpsp;
    227 	native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
    228 
    229 	/*
    230 	 * the siginfo structure
    231 	 */
    232 	sigframe.info.lsi_signo = native_to_linux_signo[sig];
    233 	sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
    234 	sigframe.info.lsi_code = ksi->ksi_code;
    235 
    236 	/* XXX This is a rought conversion, taken from i386 code */
    237 	switch (sigframe.info.lsi_signo) {
    238 	case LINUX_SIGILL:
    239 	case LINUX_SIGFPE:
    240 	case LINUX_SIGSEGV:
    241 	case LINUX_SIGBUS:
    242 	case LINUX_SIGTRAP:
    243 		sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
    244 		break;
    245 	case LINUX_SIGCHLD:
    246 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
    247 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
    248 		sigframe.info._sifields._sigchld._status = ksi->ksi_status;
    249 		sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
    250 		sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
    251 		break;
    252 	case LINUX_SIGIO:
    253 		sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
    254 		sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
    255 		break;
    256 	default:
    257 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
    258 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
    259 		if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
    260 		    (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
    261 			sigframe.info._sifields._timer._sigval.sival_ptr =
    262 			     ksi->ksi_sigval.sival_ptr;
    263 		break;
    264 	}
    265 
    266 	if ((error = copyout(&sigframe, sp, sizeof(sigframe))) != 0) {
    267 		sigexit(l, SIGILL);
    268 		return;
    269 	}
    270 
    271 	/*
    272 	 * Setup registers
    273 	 */
    274 	buildcontext(l, catcher, sp);
    275 	tf->tf_rdi = sigframe.info.lsi_signo;
    276 	tf->tf_rax = 0;
    277 	tf->tf_rsi = (long)&sfp->info;
    278 	tf->tf_rdx = (long)&sfp->uc;
    279 
    280 	/*
    281 	 * Remember we use signal stack
    282 	 */
    283 	if (onstack)
    284 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    285 	return;
    286 }
    287 
    288 int
    289 linux_sys_modify_ldt(l, v, retval)
    290         struct lwp *l;
    291         void *v;
    292         register_t *retval;
    293 {
    294 	return 0;
    295 }
    296 
    297 int
    298 linux_sys_iopl(l, v, retval)
    299         struct lwp *l;
    300         void *v;
    301         register_t *retval;
    302 {
    303 	return 0;
    304 }
    305 
    306 int
    307 linux_sys_ioperm(l, v, retval)
    308         struct lwp *l;
    309         void *v;
    310         register_t *retval;
    311 {
    312 	return 0;
    313 }
    314 
    315 dev_t
    316 linux_fakedev(dev, raw)
    317         dev_t dev;
    318 	int raw;
    319 {
    320 	return 0;
    321 }
    322 
    323 int
    324 linux_machdepioctl(p, v, retval)
    325         struct proc *p;
    326         void *v;
    327         register_t *retval;
    328 {
    329 	return 0;
    330 }
    331 
    332 int
    333 linux_sys_rt_sigreturn(l, v, retval)
    334         struct lwp *l;
    335         void *v;
    336         register_t *retval;
    337 {
    338 	struct linux_sys_rt_sigreturn_args /* {
    339 		syscallarg(struct linux_ucontext *) ucp;
    340 	} */ *uap = v;
    341 	struct linux_ucontext luctx;
    342 	struct trapframe *tf = l->l_md.md_regs;
    343 	struct linux_sigcontext *lsigctx;
    344 	struct linux__fpstate fpstate;
    345 	ucontext_t uctx;
    346 	mcontext_t *mctx;
    347 	struct fxsave64 *fxsave;
    348 	int error;
    349 
    350 	if ((error = copyin(SCARG(uap, ucp), &luctx, sizeof(luctx))) != 0) {
    351 		sigexit(l, SIGILL);
    352 		return error;
    353 	}
    354 	lsigctx = &luctx.luc_mcontext;
    355 
    356 	bzero(&uctx, sizeof(uctx));
    357 	mctx = (mcontext_t *)&uctx.uc_mcontext;
    358 	fxsave = (struct fxsave64 *)&mctx->__fpregs;
    359 
    360 	/*
    361 	 * Set the flags. Linux always have CPU, stack and signal state,
    362 	 * FPU is optional. uc_flags is not used to tell what we have.
    363 	 */
    364 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
    365 	if (lsigctx->fpstate != NULL)
    366 		uctx.uc_flags |= _UC_FPU;
    367 	uctx.uc_link = NULL;
    368 
    369 	/*
    370 	 * Signal set
    371 	 */
    372 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx.luc_sigmask);
    373 
    374 	/*
    375 	 * CPU state
    376 	 */
    377 	mctx->__gregs[_REG_R8] = lsigctx->r8;
    378 	mctx->__gregs[_REG_R9] = lsigctx->r9;
    379 	mctx->__gregs[_REG_R10] = lsigctx->r10;
    380 	mctx->__gregs[_REG_R11] = lsigctx->r11;
    381 	mctx->__gregs[_REG_R12] = lsigctx->r12;
    382 	mctx->__gregs[_REG_R13] = lsigctx->r13;
    383 	mctx->__gregs[_REG_R14] = lsigctx->r14;
    384 	mctx->__gregs[_REG_R15] = lsigctx->r15;
    385 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
    386 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
    387 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
    388 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
    389 	mctx->__gregs[_REG_RAX] = tf->tf_rax;
    390 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
    391 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
    392 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
    393 	mctx->__gregs[_REG_RFL] = lsigctx->eflags;
    394 	mctx->__gregs[_REG_CS] = lsigctx->cs;
    395 	mctx->__gregs[_REG_GS] = lsigctx->gs;
    396 	mctx->__gregs[_REG_FS] = lsigctx->fs;
    397 	mctx->__gregs[_REG_ERR] = lsigctx->err;
    398 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
    399 	mctx->__gregs[_REG_ES] = tf->tf_es;
    400 	mctx->__gregs[_REG_DS] = tf->tf_ds;
    401 	mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
    402 	mctx->__gregs[_REG_SS] = tf->tf_ss;
    403 
    404 	/*
    405 	 * FPU state
    406 	 */
    407 	if (lsigctx->fpstate != NULL) {
    408 		error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
    409 		if (error != 0) {
    410 			sigexit(l, SIGILL);
    411 			return error;
    412 		}
    413 
    414 		fxsave->fx_fcw = fpstate.cwd;
    415 		fxsave->fx_fsw = fpstate.swd;
    416 		fxsave->fx_ftw = fpstate.twd;
    417 		fxsave->fx_fop = fpstate.fop;
    418 		fxsave->fx_rip = fpstate.rip;
    419 		fxsave->fx_rdp = fpstate.rdp;
    420 		fxsave->fx_mxcsr = fpstate.mxcsr;
    421 		fxsave->fx_mxcsr_mask = fpstate.mxcsr_mask;
    422 		memcpy(&fxsave->fx_st, &fpstate.st_space,
    423 		    sizeof(fxsave->fx_st));
    424 		memcpy(&fxsave->fx_xmm, &fpstate.xmm_space,
    425 		    sizeof(fxsave->fx_xmm));
    426 	}
    427 
    428 	/*
    429 	 * And the stack
    430 	 */
    431 	uctx.uc_stack.ss_flags = 0;
    432 	if (luctx.luc_stack.ss_flags & LINUX_SS_ONSTACK);
    433 		uctx.uc_stack.ss_flags = SS_ONSTACK;
    434 
    435 	if (luctx.luc_stack.ss_flags & LINUX_SS_DISABLE);
    436 		uctx.uc_stack.ss_flags = SS_DISABLE;
    437 
    438 	uctx.uc_stack.ss_sp = luctx.luc_stack.ss_sp;
    439 	uctx.uc_stack.ss_size = luctx.luc_stack.ss_size;
    440 
    441 	/*
    442 	 * And let setucontext deal with that.
    443 	 */
    444 	return setucontext(l, &uctx);
    445 }
    446 
    447 int
    448 linux_sys_arch_prctl(l, v, retval)
    449 	struct lwp *l;
    450 	void *v;
    451 	register_t *retval;
    452 {
    453 	struct linux_sys_arch_prctl_args /* {
    454 		syscallarg(int) code;
    455 		syscallarg(unsigned long) addr;
    456 	} */ *uap = v;
    457 	ucontext_t uctx;
    458 	int error;
    459 
    460 	getucontext(l, &uctx);
    461 
    462 	switch(SCARG(uap, code)) {
    463 	case LINUX_ARCH_SET_GS:
    464 #ifndef tmp_hack
    465 		return 0; /* XXX */
    466 #endif
    467 		uctx.uc_mcontext.__gregs[_REG_GS] = (u_int64_t)SCARG(uap, addr);
    468 		if ((error = setucontext(l, &uctx)) != 0)
    469 			return error;
    470 		break;
    471 
    472 	case LINUX_ARCH_GET_GS:
    473 		if ((error = copyout(&uctx.uc_mcontext.__gregs[_REG_GS],
    474 		    (char *)SCARG(uap, addr),
    475 		    sizeof(uctx.uc_mcontext.__gregs[_REG_GS]))) != 0)
    476 			return error;
    477 		break;
    478 
    479 	case LINUX_ARCH_SET_FS:
    480 #ifndef tmp_hack
    481 		return 0; /* XXX */
    482 #endif
    483 		uctx.uc_mcontext.__gregs[_REG_FS] = (u_int64_t)SCARG(uap, addr);
    484 		if ((error = setucontext(l, &uctx)) != 0)
    485 			return error;
    486 		break;
    487 
    488 	case LINUX_ARCH_GET_FS:
    489 		if ((error = copyout(&uctx.uc_mcontext.__gregs[_REG_FS],
    490 		    (char *)SCARG(uap, addr),
    491 		    sizeof(uctx.uc_mcontext.__gregs[_REG_FS]))) != 0)
    492 			return error;
    493 		break;
    494 
    495 	default:
    496 #ifdef DEBUG_LINUX
    497 		printf("linux_sys_arch_prctl: unexpected code %d\n",
    498 		    SCARG(uap, code));
    499 #endif
    500 		return EINVAL;
    501 		break;
    502 	}
    503 
    504 	return 0;
    505 }
    506