Home | History | Annotate | Line # | Download | only in i386
cpu_i386.c revision 1.8
      1 /* $NetBSD: cpu_i386.c,v 1.8 2018/11/27 14:09:54 maxv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 Reinoud Zandijk <reinoud (at) netbsd.org>
      5  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: cpu_i386.c,v 1.8 2018/11/27 14:09:54 maxv Exp $");
     33 
     34 #include <sys/types.h>
     35 #include <sys/systm.h>
     36 #include <sys/param.h>
     37 #include <sys/time.h>
     38 #include <sys/exec.h>
     39 #include <sys/buf.h>
     40 #include <sys/boot_flag.h>
     41 #include <sys/ucontext.h>
     42 #include <sys/utsname.h>
     43 #include <machine/pcb.h>
     44 #include <machine/psl.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 #include <uvm/uvm_page.h>
     48 
     49 #include <dev/mm.h>
     50 #include <machine/machdep.h>
     51 #include <machine/thunk.h>
     52 #include <machine/mcontext.h>
     53 
     54 #include "opt_exec.h"
     55 
     56 #if 0
     57 static void dump_regs(register_t *reg);
     58 
     59 static void
     60 dump_regs(register_t *reg)
     61 {
     62 	int i;
     63 
     64 	/* register dump before call */
     65 	const char *name[] = {"GS", "FS", "ES", "DS", "EDI", "ESI", "EBP", "ESP",
     66 		"EBX", "EDX", "ECX", "EAX", "TRAPNO", "ERR", "EIP", "CS", "EFL",
     67 		"UESP", "SS"};
     68 
     69 	for (i =0; i < 19; i++)
     70 		printf("reg[%02d] (%6s) = %"PRIx32"\n", i, name[i], (uint32_t) reg[i]);
     71 }
     72 #endif
     73 
     74 
     75 /* from sys/arch/i386/include/frame.h : KEEP IN SYNC */
     76 
     77 /*
     78  * New-style signal frame
     79  */
     80 struct sigframe_siginfo {
     81 	int		sf_ra;		/* return address for handler */
     82 	int		sf_signum;	/* "signum" argument for handler */
     83 	siginfo_t	*sf_sip;	/* "sip" argument for handler */
     84 	ucontext_t	*sf_ucp;	/* "ucp" argument for handler */
     85 	siginfo_t	sf_si;		/* actual saved siginfo */
     86 	ucontext_t	sf_uc;		/* actual saved ucontext */
     87 };
     88 
     89 
     90 /*
     91  * mcontext extensions to handle signal delivery.
     92  */
     93 
     94 void
     95 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
     96 {
     97 	struct lwp *l = curlwp;
     98 	struct proc *p = l->l_proc;
     99 	struct pcb *pcb = lwp_getpcb(l);
    100 	struct sigacts *ps = p->p_sigacts;
    101 	struct sigframe_siginfo *fp, frame;
    102 	int sig = ksi->ksi_signo;
    103 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    104 	ucontext_t *ucp;
    105 	register_t *reg;
    106 	int onstack, error;
    107 
    108 	KASSERT(mutex_owned(p->p_lock));
    109 
    110 	ucp = &pcb->pcb_userret_ucp;
    111 	reg = (register_t *) &ucp->uc_mcontext.__gregs;
    112 #if 0
    113 	thunk_printf("%s: ", __func__);
    114 	thunk_printf("flags %d, ", (int) ksi->ksi_flags);
    115 	thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
    116 		(int) ksi->ksi_lid,
    117 		ksi->ksi_signo,
    118 		ksi->ksi_code,
    119 		ksi->ksi_errno);
    120 #endif
    121 
    122 	/* do we need to jump onto the signal stack? */
    123 	onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
    124 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    125 
    126 	fp = (void *) reg[17];	/* ESP */
    127 	if (onstack)
    128 		fp = (void *)
    129 			((char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
    130 
    131 	fp--;
    132 
    133 	/* set up stack frame */
    134 	memset(&frame, 0, sizeof(frame));
    135 	frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
    136 	frame.sf_signum = sig;
    137 	frame.sf_sip = &fp->sf_si;
    138 	frame.sf_ucp = &fp->sf_uc;
    139 	frame.sf_si._info = ksi->ksi_info;
    140 
    141 	/* copy our userret context into sf_uc */
    142 	memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
    143 	frame.sf_uc.uc_sigmask = *mask;
    144 	frame.sf_uc.uc_link = l->l_ctxlink; /* XXX ??? */
    145 	frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
    146 	    ? _UC_SETSTACK : _UC_CLRSTACK;
    147 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
    148 
    149 	sendsig_reset(l, sig);
    150 
    151 	/* copyout our frame to the stackframe */
    152 	mutex_exit(p->p_lock);
    153 	error = copyout(&frame, fp, sizeof(frame));
    154 	mutex_enter(p->p_lock);
    155 
    156 	if (error != 0) {
    157 		/*
    158 		 * Process has trashed its stack; give it an illegal
    159 		 * instruction to halt it in its tracks.
    160 		 */
    161 		sigexit(l, SIGILL);
    162 		/* NOTREACHED */
    163 	}
    164 
    165 	/* set catcher and the new stack pointer */
    166 	reg[17] = (register_t) fp;	/* ESP */
    167 	reg[14] = (register_t) catcher;	/* EIP */
    168 
    169 	/* Remember that we're now on the signal stack. */
    170 	if (onstack)
    171 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    172 }
    173 
    174 void
    175 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
    176 {
    177 	struct pcb *pcb = lwp_getpcb(l);
    178 	ucontext_t *ucp;
    179 	uint *reg, i;
    180 
    181 #ifdef DEBUG_EXEC
    182 	printf("setregs called: lwp %p, exec package %p, stack %p\n",
    183 		l, pack, (void *) stack);
    184 	printf("current stat of pcb %p\n", pcb);
    185 	printf("\tpcb->pcb_ucp.uc_stack.ss_sp   = %p\n",
    186 		pcb->pcb_ucp.uc_stack.ss_sp);
    187 	printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
    188 		(int) pcb->pcb_ucp.uc_stack.ss_size);
    189 #endif
    190 
    191 	/* set up the user context */
    192 	ucp = &pcb->pcb_userret_ucp;
    193 	reg = (int *) &ucp->uc_mcontext.__gregs;
    194 	for (i = 4; i < 11; i++)
    195 		reg[i] = 0;
    196 
    197 	/* use given stack */
    198 	ucp->uc_stack.ss_sp = (void *) (stack-4);	/* to prevent clearing */
    199 	ucp->uc_stack.ss_size = 0; //pack->ep_ssize;
    200 	thunk_makecontext(ucp, (void *) pack->ep_entry,
    201 		0, NULL, NULL, NULL, NULL);
    202 
    203 	/* patch up */
    204 	reg[ 8] = l->l_proc->p_psstrp;	/* _REG_EBX */
    205 	reg[17] = (stack);		/* _REG_UESP */
    206 
    207 	//dump_regs(reg);
    208 
    209 #ifdef DEBUG_EXEC
    210 	printf("updated pcb %p\n", pcb);
    211 	printf("\tpcb->pcb_ucp.uc_stack.ss_sp   = %p\n",
    212 		pcb->pcb_ucp.uc_stack.ss_sp);
    213 	printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
    214 		(int) pcb->pcb_ucp.uc_stack.ss_size);
    215 	printf("\tpack->ep_entry                = %p\n",
    216 		(void *) pack->ep_entry);
    217 #endif
    218 }
    219 
    220 void
    221 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
    222 {
    223 	uint *reg = (int *) &ucp->uc_mcontext.__gregs;
    224 	*code = reg[11];			/* EAX */
    225 }
    226 
    227 int
    228 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
    229 	register_t *args)
    230 {
    231 	uint *reg = (int *) &ucp->uc_mcontext.__gregs;
    232 	register_t *sp = (register_t *) reg[17];/* ESP */
    233 	int ret;
    234 
    235 	//dump_regs(reg);
    236 	ret = copyin(sp + 1, args, argsize);
    237 
    238 	return ret;
    239 }
    240 
    241 void
    242 md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
    243 	int error, register_t *rval)
    244 {
    245 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    246 
    247 	reg[16] &= ~PSL_C;		/* EFL */
    248 	if (error > 0) {
    249 		rval[0] = error;
    250 		reg[16] |= PSL_C;	/* EFL */
    251 	}
    252 
    253 	/* set return parameters */
    254 	reg[11]	= rval[0];		/* EAX */
    255 	if (error == 0)
    256 		reg[ 9] = rval[1];	/* EDX */
    257 
    258 	//dump_regs(reg);
    259 }
    260 
    261 register_t
    262 md_get_pc(ucontext_t *ucp)
    263 {
    264 	KASSERT(ucp);
    265 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    266 
    267 	return reg[14];			/* EIP */
    268 }
    269 
    270 register_t
    271 md_get_sp(ucontext_t *ucp)
    272 {
    273 	KASSERT(ucp);
    274 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    275 
    276 	return reg[17];			/* ESP */
    277 }
    278 
    279 int
    280 md_syscall_check_opcode(ucontext_t *ucp)
    281 {
    282 	uint32_t opcode;
    283 
    284 	md_syscall_get_opcode(ucp, &opcode);
    285 
    286 	switch (opcode) {
    287 	case 0xff0f:	/* UD1      */
    288 	case 0xff0b:	/* UD2      */
    289 	case 0x80cd:	/* int $80  */
    290 	case 0x340f:	/* sysenter */
    291 	case 0x050f:	/* syscall */
    292 		return 1;
    293 	default:
    294 		return 0;
    295 	}
    296 }
    297 
    298 void
    299 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
    300 {
    301 	KASSERT(ucp);
    302 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    303 //	uint8_t  *p8  = (uint8_t *) (reg[14]);
    304 	uint16_t *p16 = (uint16_t*) (reg[14]);	/* EIP */
    305 
    306 	switch (*p16) {
    307 	case 0xff0f:	/* UD1      */
    308 	case 0xff0b:	/* UD2      */
    309 	case 0x80cd:	/* int $80  */
    310 	case 0x340f:	/* sysenter */
    311 	case 0x050f:	/* syscall */
    312 		*opcode = *p16;
    313 		break;
    314 	default:
    315 		*opcode = 0;
    316 	}
    317 }
    318 
    319 void
    320 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
    321 {
    322 	KASSERT(ucp);
    323 	uint *reg = (int *) &ucp->uc_mcontext.__gregs;
    324 
    325 	/* advance program counter */
    326 	switch (opcode) {
    327 	case 0xff0f:	/* UD1      */
    328 	case 0xff0b:	/* UD2      */
    329 	case 0x80cd:	/* int $80  */
    330 	case 0x340f:	/* sysenter */
    331 	case 0x050f:	/* syscall */
    332 		reg[14] += 2;	/* EIP */
    333 		break;
    334 	default:
    335 		panic("%s, unknown illegal instruction: opcode = %x\n",
    336 			__func__, (uint32_t) opcode);
    337 	}
    338 }
    339 
    340 void
    341 md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
    342 {
    343 	KASSERT(ucp);
    344 	uint *reg = (int *) &ucp->uc_mcontext.__gregs;
    345 
    346 	switch (opcode) {
    347 	case 0xff0f:	/* UD1      */
    348 	case 0xff0b:	/* UD2      */
    349 	case 0x80cd:	/* int $80  */
    350 	case 0x340f:	/* sysenter */
    351 	case 0x050f:	/* syscall */
    352 		reg[14] -= 2;	/* EIP */
    353 		break;
    354 	default:
    355 		panic("%s, unknown illegal instruction: opcode = %x\n",
    356 			__func__, (uint32_t) opcode);
    357 	}
    358 }
    359 
    360