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