Home | History | Annotate | Line # | Download | only in i386
cpu_i386.c revision 1.4.28.1
      1 /* $NetBSD: cpu_i386.c,v 1.4.28.1 2019/01/30 13:29:52 martin 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  * Note that this machdep.c uses the `dummy' mcontext_t defined for usermode.
     32  * This is basicly a blob of PAGE_SIZE big. We might want to switch over to
     33  * non-generic mcontext_t's one day, but will this break non-NetBSD hosts?
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: cpu_i386.c,v 1.4.28.1 2019/01/30 13:29:52 martin Exp $");
     38 
     39 #include <sys/types.h>
     40 #include <sys/systm.h>
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 #include <sys/exec.h>
     44 #include <sys/buf.h>
     45 #include <sys/boot_flag.h>
     46 #include <sys/ucontext.h>
     47 #include <sys/utsname.h>
     48 #include <machine/pcb.h>
     49 #include <machine/psl.h>
     50 
     51 #include <uvm/uvm_extern.h>
     52 #include <uvm/uvm_page.h>
     53 
     54 #include <dev/mm.h>
     55 #include <machine/machdep.h>
     56 #include <machine/thunk.h>
     57 
     58 #include "opt_exec.h"
     59 
     60 #if 0
     61 static void dump_regs(register_t *reg);;
     62 
     63 static void
     64 dump_regs(register_t *reg)
     65 {
     66 	int i;
     67 
     68 	/* register dump before call */
     69 	const char *name[] = {"GS", "FS", "ES", "DS", "EDI", "ESI", "EBP", "ESP",
     70 		"EBX", "EDX", "ECX", "EAX", "TRAPNO", "ERR", "EIP", "CS", "EFL",
     71 		"UESP", "SS"};
     72 
     73 	for (i =0; i < 19; i++)
     74 		printf("reg[%02d] (%6s) = %"PRIx32"\n", i, name[i], (uint32_t) reg[i]);
     75 }
     76 #endif
     77 
     78 
     79 /* from sys/arch/i386/include/frame.h : KEEP IN SYNC */
     80 
     81 /*
     82  * New-style signal frame
     83  */
     84 struct sigframe_siginfo {
     85 	int		sf_ra;		/* return address for handler */
     86 	int		sf_signum;	/* "signum" argument for handler */
     87 	siginfo_t	*sf_sip;	/* "sip" argument for handler */
     88 	ucontext_t	*sf_ucp;	/* "ucp" argument for handler */
     89 	siginfo_t	sf_si;		/* actual saved siginfo */
     90 	ucontext_t	sf_uc;		/* actual saved ucontext */
     91 };
     92 
     93 
     94 /*
     95  * mcontext extensions to handle signal delivery.
     96  */
     97 #define _UC_SETSTACK	0x00010000
     98 #define _UC_CLRSTACK	0x00020000
     99 #define _UC_VM		0x00040000
    100 #define	_UC_TLSBASE	0x00080000
    101 
    102 
    103 void
    104 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
    105 {
    106 	struct lwp *l = curlwp;
    107 	struct proc *p = l->l_proc;
    108 	struct pcb *pcb = lwp_getpcb(l);
    109 	struct sigacts *ps = p->p_sigacts;
    110 	struct sigframe_siginfo *fp, frame;
    111 	int sig = ksi->ksi_signo;
    112 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    113 	ucontext_t *ucp;
    114 	register_t *reg;
    115 	int onstack, error;
    116 
    117 	KASSERT(mutex_owned(p->p_lock));
    118 
    119 	ucp = &pcb->pcb_userret_ucp;
    120 	reg = (register_t *) &ucp->uc_mcontext;
    121 #if 0
    122 	thunk_printf("%s: ", __func__);
    123 	thunk_printf("flags %d, ", (int) ksi->ksi_flags);
    124 	thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
    125 		(int) ksi->ksi_lid,
    126 		ksi->ksi_signo,
    127 		ksi->ksi_code,
    128 		ksi->ksi_errno);
    129 #endif
    130 
    131 	/* do we need to jump onto the signal stack? */
    132 	onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
    133 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    134 
    135 	fp = (void *) reg[17];	/* ESP */
    136 	if (onstack)
    137 		fp = (void *)
    138 			((char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
    139 
    140 	fp--;
    141 
    142 	/* set up stack frame */
    143 	memset(&frame, 0, sizeof(frame));
    144 	frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
    145 	frame.sf_signum = sig;
    146 	frame.sf_sip = &fp->sf_si;
    147 	frame.sf_ucp = &fp->sf_uc;
    148 	frame.sf_si._info = ksi->ksi_info;
    149 
    150 	/* copy our userret context into sf_uc */
    151 	memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
    152 	frame.sf_uc.uc_sigmask = *mask;
    153 	frame.sf_uc.uc_link = l->l_ctxlink; /* XXX ??? */
    154 	frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
    155 	    ? _UC_SETSTACK : _UC_CLRSTACK;
    156 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
    157 
    158 	sendsig_reset(l, sig);
    159 
    160 	/* copyout our frame to the stackframe */
    161 	mutex_exit(p->p_lock);
    162 	error = copyout(&frame, fp, sizeof(frame));
    163 	mutex_enter(p->p_lock);
    164 
    165 	if (error != 0) {
    166 		/*
    167 		 * Process has trashed its stack; give it an illegal
    168 		 * instruction to halt it in its tracks.
    169 		 */
    170 		sigexit(l, SIGILL);
    171 		/* NOTREACHED */
    172 	}
    173 
    174 	/* set catcher and the new stack pointer */
    175 	reg[17] = (register_t) fp;	/* ESP */
    176 	reg[14] = (register_t) catcher;	/* EIP */
    177 
    178 	/* Remember that we're now on the signal stack. */
    179 	if (onstack)
    180 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    181 }
    182 
    183 void
    184 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
    185 {
    186 	struct pcb *pcb = lwp_getpcb(l);
    187 	ucontext_t *ucp;
    188 	uint *reg, i;
    189 
    190 #ifdef DEBUG_EXEC
    191 	printf("setregs called: lwp %p, exec package %p, stack %p\n",
    192 		l, pack, (void *) stack);
    193 	printf("current stat of pcb %p\n", pcb);
    194 	printf("\tpcb->pcb_ucp.uc_stack.ss_sp   = %p\n",
    195 		pcb->pcb_ucp.uc_stack.ss_sp);
    196 	printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
    197 		(int) pcb->pcb_ucp.uc_stack.ss_size);
    198 #endif
    199 
    200 	/* set up the user context */
    201 	ucp = &pcb->pcb_userret_ucp;
    202 	reg = (int *) &ucp->uc_mcontext;
    203 	for (i = 4; i < 11; i++)
    204 		reg[i] = 0;
    205 
    206 	/* use given stack */
    207 	ucp->uc_stack.ss_sp = (void *) (stack-4);	/* to prevent clearing */
    208 	ucp->uc_stack.ss_size = 0; //pack->ep_ssize;
    209 	thunk_makecontext(ucp, (void *) pack->ep_entry,
    210 		0, NULL, NULL, NULL, NULL);
    211 
    212 	/* patch up */
    213 	reg[ 8] = l->l_proc->p_psstrp;	/* _REG_EBX */
    214 	reg[17] = (stack);		/* _REG_UESP */
    215 
    216 	//dump_regs(reg);
    217 
    218 #ifdef DEBUG_EXEC
    219 	printf("updated pcb %p\n", pcb);
    220 	printf("\tpcb->pcb_ucp.uc_stack.ss_sp   = %p\n",
    221 		pcb->pcb_ucp.uc_stack.ss_sp);
    222 	printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
    223 		(int) pcb->pcb_ucp.uc_stack.ss_size);
    224 	printf("\tpack->ep_entry                = %p\n",
    225 		(void *) pack->ep_entry);
    226 #endif
    227 }
    228 
    229 void
    230 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
    231 {
    232 	uint *reg = (int *) &ucp->uc_mcontext;
    233 	*code = reg[11];			/* EAX */
    234 }
    235 
    236 int
    237 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
    238 	register_t *args)
    239 {
    240 	uint *reg = (int *) &ucp->uc_mcontext;
    241 	register_t *sp = (register_t *) reg[17];/* ESP */
    242 	int ret;
    243 
    244 	//dump_regs(reg);
    245 	ret = copyin(sp + 1, args, argsize);
    246 
    247 	return ret;
    248 }
    249 
    250 void
    251 md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
    252 	int error, register_t *rval)
    253 {
    254 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    255 
    256 	reg[16] &= ~PSL_C;		/* EFL */
    257 	if (error > 0) {
    258 		rval[0] = error;
    259 		reg[16] |= PSL_C;	/* EFL */
    260 	}
    261 
    262 	/* set return parameters */
    263 	reg[11]	= rval[0];		/* EAX */
    264 	if (error == 0)
    265 		reg[ 9] = rval[1];	/* EDX */
    266 
    267 	//dump_regs(reg);
    268 }
    269 
    270 register_t
    271 md_get_pc(ucontext_t *ucp)
    272 {
    273 	KASSERT(ucp);
    274 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    275 
    276 	return reg[14];			/* EIP */
    277 }
    278 
    279 register_t
    280 md_get_sp(ucontext_t *ucp)
    281 {
    282 	KASSERT(ucp);
    283 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    284 
    285 	return reg[17];			/* ESP */
    286 }
    287 
    288 int
    289 md_syscall_check_opcode(ucontext_t *ucp)
    290 {
    291 	uint32_t opcode;
    292 
    293 	md_syscall_get_opcode(ucp, &opcode);
    294 
    295 	switch (opcode) {
    296 	case 0xff0f:	/* UD1      */
    297 	case 0xff0b:	/* UD2      */
    298 	case 0x80cd:	/* int $80  */
    299 	case 0x340f:	/* sysenter */
    300 	case 0x050f:	/* syscall */
    301 		return 1;
    302 	default:
    303 		return 0;
    304 	}
    305 }
    306 
    307 void
    308 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
    309 {
    310 	KASSERT(ucp);
    311 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    312 //	uint8_t  *p8  = (uint8_t *) (reg[14]);
    313 	uint16_t *p16 = (uint16_t*) (reg[14]);	/* EIP */
    314 
    315 	switch (*p16) {
    316 	case 0xff0f:	/* UD1      */
    317 	case 0xff0b:	/* UD2      */
    318 	case 0x80cd:	/* int $80  */
    319 	case 0x340f:	/* sysenter */
    320 	case 0x050f:	/* syscall */
    321 		*opcode = *p16;
    322 		break;
    323 	default:
    324 		*opcode = 0;
    325 	}
    326 }
    327 
    328 void
    329 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
    330 {
    331 	KASSERT(ucp);
    332 	uint *reg = (int *) &ucp->uc_mcontext;
    333 
    334 	/* advance program counter */
    335 	switch (opcode) {
    336 	case 0xff0f:	/* UD1      */
    337 	case 0xff0b:	/* UD2      */
    338 	case 0x80cd:	/* int $80  */
    339 	case 0x340f:	/* sysenter */
    340 	case 0x050f:	/* syscall */
    341 		reg[14] += 2;	/* EIP */
    342 		break;
    343 	default:
    344 		panic("%s, unknown illegal instruction: opcode = %x\n",
    345 			__func__, (uint32_t) opcode);
    346 	}
    347 }
    348 
    349 void
    350 md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
    351 {
    352 	KASSERT(ucp);
    353 	uint *reg = (int *) &ucp->uc_mcontext;
    354 
    355 	switch (opcode) {
    356 	case 0xff0f:	/* UD1      */
    357 	case 0xff0b:	/* UD2      */
    358 	case 0x80cd:	/* int $80  */
    359 	case 0x340f:	/* sysenter */
    360 	case 0x050f:	/* syscall */
    361 		reg[14] -= 2;	/* EIP */
    362 		break;
    363 	default:
    364 		panic("%s, unknown illegal instruction: opcode = %x\n",
    365 			__func__, (uint32_t) opcode);
    366 	}
    367 }
    368 
    369