Home | History | Annotate | Line # | Download | only in x86_64
cpu_x86_64.c revision 1.2.4.2
      1 /* $NetBSD: cpu_x86_64.c,v 1.2.4.2 2012/04/17 00:07:00 yamt 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 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: cpu_x86_64.c,v 1.2.4.2 2012/04/17 00:07:00 yamt Exp $");
     39 
     40 #include <sys/types.h>
     41 #include <sys/systm.h>
     42 #include <sys/param.h>
     43 #include <sys/time.h>
     44 #include <sys/exec.h>
     45 #include <sys/buf.h>
     46 #include <sys/boot_flag.h>
     47 #include <sys/ucontext.h>
     48 #include <sys/utsname.h>
     49 #include <machine/pcb.h>
     50 #include <machine/psl.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 #include <uvm/uvm_page.h>
     54 
     55 #include <dev/mm.h>
     56 #include <machine/machdep.h>
     57 #include <machine/thunk.h>
     58 
     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[] = {"RDI", "RSI", "RDX", "RCX", "R8", "R9", "R10",
     70 		"R11", "R12", "R13", "R14", "R15", "RBP", "RAX",
     71 		"GS", "FS", "ES", "DS", "TRAPNO", "ERR", "RIP", "CS",
     72 		"RFLAGS", "RSP", "SS"};
     73 
     74 	for (i =0; i < 26; i++)
     75 		printf("reg[%02d] (%6s) = %"PRIx32"\n",
     76 			i, name[i], (uint32_t) reg[i]);
     77 }
     78 #endif
     79 
     80 
     81 /* from sys/arch/amd64/include/frame.h : KEEP IN SYNC */
     82 
     83 /*
     84  * Signal frame
     85  */
     86 struct sigframe_siginfo {
     87 	uint64_t	sf_ra;		/* return address for handler */
     88 	siginfo_t	sf_si;		/* actual saved siginfo */
     89 	ucontext_t	sf_uc;		/* actual saved ucontext */
     90 };
     91 
     92 
     93 /* should be the same as i386 */
     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 	char *sp;
    117 
    118 	KASSERT(mutex_owned(p->p_lock));
    119 
    120 	ucp = &pcb->pcb_userret_ucp;
    121 	reg = (register_t *) &ucp->uc_mcontext;
    122 #if 0
    123 	thunk_printf("%s: ", __func__);
    124 	thunk_printf("flags %d, ", (int) ksi->ksi_flags);
    125 	thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
    126 		(int) ksi->ksi_lid,
    127 		ksi->ksi_signo,
    128 		ksi->ksi_code,
    129 		ksi->ksi_errno);
    130 #endif
    131 
    132 	/* do we need to jump onto the signal stack? */
    133 	onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
    134 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    135 
    136 	sp = ((char *) reg[24] - 128);/* why -128?*//* RSP */
    137 	if (onstack)
    138 		sp = (char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
    139 
    140 	sp -= sizeof(struct sigframe_siginfo);
    141 	/*
    142 	 * Round down the stackpointer to a multiple of 16 for
    143 	 * fxsave and the ABI
    144 	 */
    145 	fp = (struct sigframe_siginfo *) (((unsigned long)sp & ~15) - 8);
    146 
    147 	/* set up stack frame */
    148 	frame.sf_ra = (uint64_t) ps->sa_sigdesc[sig].sd_tramp;
    149 	frame.sf_si._info = ksi->ksi_info;
    150 
    151 	/* copy our userret context into sf_uc */
    152 	memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
    153 	frame.sf_uc.uc_sigmask = *mask;
    154 	frame.sf_uc.uc_link = l->l_ctxlink;
    155 	frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
    156 	    ? _UC_SETSTACK : _UC_CLRSTACK;
    157 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
    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[24] = (register_t) fp;		/* RSP */
    176 	reg[21] = (register_t) catcher;		/* RIP */
    177 
    178 	reg[ 0] = sig;				/* RDI */
    179 	reg[ 1] = (uint64_t) &fp->sf_si;	/* RSI */
    180 	reg[ 2] = (uint64_t) &fp->sf_uc;	/* RDX */
    181 	reg[11] = reg[ 2];			/* R15 = RDX */
    182 
    183 	/* Remember that we're now on the signal stack. */
    184 	if (onstack)
    185 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    186 }
    187 
    188 void
    189 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
    190 {
    191 	struct pcb *pcb = lwp_getpcb(l);
    192 	ucontext_t *ucp;
    193 	register_t *reg;
    194 	int i;
    195 
    196 	/* set up the user context */
    197 	ucp = &pcb->pcb_userret_ucp;
    198 	reg = (register_t *) &ucp->uc_mcontext;
    199 	for (i = 0; i < 15; i++)
    200 		reg[i] = 0;
    201 
    202 	reg[13] = l->l_proc->p_psstrp;		/* RBX */
    203 	reg[21] = pack->ep_entry;		/* RIP */
    204 	reg[24] = (register_t) stack;		/* RSP */
    205 
    206 	/* use given stack */
    207 	ucp->uc_stack.ss_sp   = (void *) stack;
    208 	ucp->uc_stack.ss_size = pack->ep_ssize;
    209 
    210 	//dump_regs(reg);
    211 }
    212 
    213 void
    214 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
    215 {
    216 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    217 	*code = reg[14];			/* RAX */
    218 }
    219 
    220 int
    221 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
    222 	register_t *args)
    223 {
    224 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    225 	register_t *sp = (register_t *) reg[24];/* RSP */
    226 	int ret;
    227 
    228 	//dump_regs(reg);
    229 
    230 	/*
    231 	 * 1st 6 syscall args are passed in
    232 	 *    rdi, rsi, rdx, r10, r8 and r9
    233 	 */
    234 	args[0] = reg[ 0];		/* RDI */
    235 	args[1] = reg[ 1];		/* RSI */
    236 	args[2] = reg[ 2];		/* RDX */
    237 	args[3] = reg[ 6];		/* R10 (RCX got clobbered) */
    238 	args[4] = reg[ 4];		/* R8  */
    239 	args[5] = reg[ 5];		/* R9  */
    240 
    241 	ret = 0;
    242 	if (argsize > 6 * 8) {
    243 		ret = copyin(sp + 1,
    244 			args + 6, argsize - 6 * 8);
    245 	}
    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[23] &= ~PSL_C;		/* RFLAGS */
    257 	if (error > 0) {
    258 		rval[0] = error;
    259 		reg[23] |= PSL_C;	/* RFLAGS */
    260 	}
    261 
    262 	/* set return parameters */
    263 	reg[14]	= rval[0];		/* RAX */
    264 	if (error == 0)
    265 		reg[ 2] = rval[1];	/* RDX */
    266 
    267 	//dump_regs(reg);
    268 }
    269 
    270 register_t
    271 md_get_pc(ucontext_t *ucp)
    272 {
    273 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    274 
    275 	return reg[21];			/* RIP */
    276 }
    277 
    278 register_t
    279 md_get_sp(ucontext_t *ucp)
    280 {
    281 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    282 
    283 	return reg[24];			/* RSP */
    284 }
    285 
    286 int
    287 md_syscall_check_opcode(ucontext_t *ucp)
    288 {
    289 	uint32_t opcode;
    290 
    291 	md_syscall_get_opcode(ucp, &opcode);
    292 
    293 	switch (opcode) {
    294 	case 0xff0f:	/* UD1      */
    295 	case 0xff0b:	/* UD2      */
    296 	case 0x80cd:	/* int $80  */
    297 	case 0x340f:	/* sysenter */
    298 	case 0x050f:	/* syscall */
    299 		return 1;
    300 	default:
    301 		return 0;
    302 	}
    303 }
    304 
    305 
    306 void
    307 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
    308 {
    309 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    310 //	uint8_t  *p8  = (uint8_t *) (reg[21]);
    311 	uint16_t *p16 = (uint16_t*) (reg[21]);	/* RIP */
    312 
    313 	switch (*p16) {
    314 	case 0xff0f:	/* UD1      */
    315 	case 0xff0b:	/* UD2      */
    316 	case 0x80cd:	/* int $80  */
    317 	case 0x340f:	/* sysenter */
    318 	case 0x050f:	/* syscall */
    319 		*opcode = *p16;
    320 		break;
    321 	default:
    322 		*opcode = 0;
    323 	}
    324 }
    325 
    326 void
    327 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
    328 {
    329 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    330 
    331 	/* advance program counter */
    332 	switch (opcode) {
    333 	case 0xff0f:	/* UD1      */
    334 	case 0xff0b:	/* UD2      */
    335 	case 0x80cd:	/* int $80  */
    336 	case 0x340f:	/* sysenter */
    337 	case 0x050f:	/* syscall */
    338 		reg[21] += 2;	/* RIP */
    339 		break;
    340 	default:
    341 		panic("%s, unknown illegal instruction: opcode = %x\n",
    342 			__func__, (uint32_t) opcode);
    343 	}
    344 }
    345 
    346 void
    347 md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
    348 {
    349 	register_t *reg = (register_t *) &ucp->uc_mcontext;
    350 
    351 	switch (opcode) {
    352 	case 0xff0f:	/* UD1      */
    353 	case 0xff0b:	/* UD2      */
    354 	case 0x80cd:	/* int $80  */
    355 	case 0x340f:	/* sysenter */
    356 	case 0x050f:	/* syscall */
    357 		reg[21] -= 2;	/* RIP */
    358 		break;
    359 	default:
    360 		panic("%s, unknown illegal instruction: opcode = %x\n",
    361 			__func__, (uint32_t) opcode);
    362 	}
    363 }
    364 
    365