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