Home | History | Annotate | Line # | Download | only in x86_64
cpu_x86_64.c revision 1.4
      1 /* $NetBSD: cpu_x86_64.c,v 1.4 2018/05/18 21:05:10 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 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: cpu_x86_64.c,v 1.4 2018/05/18 21:05:10 reinoud 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 
     53 
     54 #if 0
     55 static void dump_regs(register_t *reg);;
     56 
     57 static void
     58 dump_regs(register_t *reg)
     59 {
     60 	int i;
     61 
     62 	/* register dump before call */
     63 	const char *name[] = {"RDI", "RSI", "RDX", "RCX", "R8", "R9", "R10",
     64 		"R11", "R12", "R13", "R14", "R15", "RBP", "RAX",
     65 		"GS", "FS", "ES", "DS", "TRAPNO", "ERR", "RIP", "CS",
     66 		"RFLAGS", "RSP", "SS"};
     67 
     68 	for (i =0; i < 26; i++)
     69 		printf("reg[%02d] (%6s) = %"PRIx32"\n",
     70 			i, name[i], (uint32_t) reg[i]);
     71 }
     72 #endif
     73 
     74 
     75 /* from sys/arch/amd64/include/frame.h : KEEP IN SYNC */
     76 
     77 /*
     78  * Signal frame
     79  */
     80 struct sigframe_siginfo {
     81 	uint64_t	sf_ra;		/* return address for handler */
     82 	siginfo_t	sf_si;		/* actual saved siginfo */
     83 	ucontext_t	sf_uc;		/* actual saved ucontext */
     84 };
     85 
     86 
     87 /* should be the same as i386 */
     88 /*
     89  * mcontext extensions to handle signal delivery.
     90  */
     91 #define _UC_SETSTACK	0x00010000
     92 #define _UC_CLRSTACK	0x00020000
     93 #define _UC_VM		0x00040000
     94 #define	_UC_TLSBASE	0x00080000
     95 
     96 
     97 void
     98 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
     99 {
    100 	struct lwp *l = curlwp;
    101 	struct proc *p = l->l_proc;
    102 	struct pcb *pcb = lwp_getpcb(l);
    103 	struct sigacts *ps = p->p_sigacts;
    104 	struct sigframe_siginfo *fp, frame;
    105 	int sig = ksi->ksi_signo;
    106 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    107 	ucontext_t *ucp;
    108 	register_t *reg;
    109 	int onstack, error;
    110 	char *sp;
    111 
    112 	KASSERT(mutex_owned(p->p_lock));
    113 
    114 	ucp = &pcb->pcb_userret_ucp;
    115 	reg = (register_t *) &ucp->uc_mcontext.__gregs;
    116 #if 0
    117 	thunk_printf("%s: ", __func__);
    118 	thunk_printf("flags %d, ", (int) ksi->ksi_flags);
    119 	thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
    120 		(int) ksi->ksi_lid,
    121 		ksi->ksi_signo,
    122 		ksi->ksi_code,
    123 		ksi->ksi_errno);
    124 #endif
    125 
    126 	/* do we need to jump onto the signal stack? */
    127 	onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
    128 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    129 
    130 	sp = ((char *) reg[24] - 128);/* why -128?*//* RSP */
    131 	if (onstack)
    132 		sp = (char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
    133 
    134 	sp -= sizeof(struct sigframe_siginfo);
    135 	/*
    136 	 * Round down the stackpointer to a multiple of 16 for
    137 	 * fxsave and the ABI
    138 	 */
    139 	fp = (struct sigframe_siginfo *) (((unsigned long)sp & ~15) - 8);
    140 
    141 	/* set up stack frame */
    142 	frame.sf_ra = (uint64_t) ps->sa_sigdesc[sig].sd_tramp;
    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;
    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 	sendsig_reset(l, sig);
    153 
    154 	/* copyout our frame to the stackframe */
    155 	mutex_exit(p->p_lock);
    156 	error = copyout(&frame, fp, sizeof(frame));
    157 	mutex_enter(p->p_lock);
    158 
    159 	if (error != 0) {
    160 		/*
    161 		 * Process has trashed its stack; give it an illegal
    162 		 * instruction to halt it in its tracks.
    163 		 */
    164 		sigexit(l, SIGILL);
    165 		/* NOTREACHED */
    166 	}
    167 
    168 	/* set catcher and the new stack pointer */
    169 	reg[24] = (register_t) fp;		/* RSP */
    170 	reg[21] = (register_t) catcher;		/* RIP */
    171 
    172 	reg[ 0] = sig;				/* RDI */
    173 	reg[ 1] = (uint64_t) &fp->sf_si;	/* RSI */
    174 	reg[ 2] = (uint64_t) &fp->sf_uc;	/* RDX */
    175 	reg[11] = reg[ 2];			/* R15 = RDX */
    176 
    177 	/* Remember that we're now on the signal stack. */
    178 	if (onstack)
    179 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    180 }
    181 
    182 void
    183 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
    184 {
    185 	struct pcb *pcb = lwp_getpcb(l);
    186 	ucontext_t *ucp;
    187 	register_t *reg;
    188 	int i;
    189 
    190 	/* set up the user context */
    191 	ucp = &pcb->pcb_userret_ucp;
    192 	reg = (register_t *) &ucp->uc_mcontext.__gregs;
    193 	for (i = 0; i < 15; i++)
    194 		reg[i] = 0;
    195 
    196 	reg[13] = l->l_proc->p_psstrp;		/* RBX */
    197 	reg[21] = pack->ep_entry;		/* RIP */
    198 	reg[24] = (register_t) stack;		/* RSP */
    199 
    200 	/* use given stack */
    201 	ucp->uc_stack.ss_sp   = (void *) stack;
    202 	ucp->uc_stack.ss_size = pack->ep_ssize;
    203 
    204 	//dump_regs(reg);
    205 }
    206 
    207 void
    208 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
    209 {
    210 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    211 	*code = reg[14];			/* RAX */
    212 }
    213 
    214 int
    215 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
    216 	register_t *args)
    217 {
    218 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    219 	register_t *sp = (register_t *) reg[24];/* RSP */
    220 	int ret;
    221 
    222 	//dump_regs(reg);
    223 
    224 	/*
    225 	 * 1st 6 syscall args are passed in
    226 	 *    rdi, rsi, rdx, r10, r8 and r9
    227 	 */
    228 	args[0] = reg[ 0];		/* RDI */
    229 	args[1] = reg[ 1];		/* RSI */
    230 	args[2] = reg[ 2];		/* RDX */
    231 	args[3] = reg[ 6];		/* R10 (RCX got clobbered) */
    232 	args[4] = reg[ 4];		/* R8  */
    233 	args[5] = reg[ 5];		/* R9  */
    234 
    235 	ret = 0;
    236 	if (argsize > 6 * 8) {
    237 		ret = copyin(sp + 1,
    238 			args + 6, argsize - 6 * 8);
    239 	}
    240 
    241 	return ret;
    242 }
    243 
    244 void
    245 md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
    246 	int error, register_t *rval)
    247 {
    248 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    249 
    250 	reg[23] &= ~PSL_C;		/* RFLAGS */
    251 	if (error > 0) {
    252 		rval[0] = error;
    253 		reg[23] |= PSL_C;	/* RFLAGS */
    254 	}
    255 
    256 	/* set return parameters */
    257 	reg[14]	= rval[0];		/* RAX */
    258 	if (error == 0)
    259 		reg[ 2] = rval[1];	/* RDX */
    260 
    261 	//dump_regs(reg);
    262 }
    263 
    264 register_t
    265 md_get_pc(ucontext_t *ucp)
    266 {
    267 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    268 
    269 	return reg[21];			/* RIP */
    270 }
    271 
    272 register_t
    273 md_get_sp(ucontext_t *ucp)
    274 {
    275 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    276 
    277 	return reg[24];			/* RSP */
    278 }
    279 
    280 int
    281 md_syscall_check_opcode(ucontext_t *ucp)
    282 {
    283 	uint32_t opcode;
    284 
    285 	md_syscall_get_opcode(ucp, &opcode);
    286 
    287 	switch (opcode) {
    288 	case 0xff0f:	/* UD1      */
    289 	case 0xff0b:	/* UD2      */
    290 	case 0x80cd:	/* int $80  */
    291 	case 0x340f:	/* sysenter */
    292 	case 0x050f:	/* syscall */
    293 		return 1;
    294 	default:
    295 		return 0;
    296 	}
    297 }
    298 
    299 
    300 void
    301 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
    302 {
    303 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    304 //	uint8_t  *p8  = (uint8_t *) (reg[21]);
    305 	uint16_t *p16 = (uint16_t*) (reg[21]);	/* RIP */
    306 
    307 	switch (*p16) {
    308 	case 0xff0f:	/* UD1      */
    309 	case 0xff0b:	/* UD2      */
    310 	case 0x80cd:	/* int $80  */
    311 	case 0x340f:	/* sysenter */
    312 	case 0x050f:	/* syscall */
    313 		*opcode = *p16;
    314 		break;
    315 	default:
    316 		*opcode = 0;
    317 	}
    318 }
    319 
    320 void
    321 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
    322 {
    323 	register_t *reg = (register_t *) &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[21] += 2;	/* RIP */
    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 	register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
    344 
    345 	switch (opcode) {
    346 	case 0xff0f:	/* UD1      */
    347 	case 0xff0b:	/* UD2      */
    348 	case 0x80cd:	/* int $80  */
    349 	case 0x340f:	/* sysenter */
    350 	case 0x050f:	/* syscall */
    351 		reg[21] -= 2;	/* RIP */
    352 		break;
    353 	default:
    354 		panic("%s, unknown illegal instruction: opcode = %x\n",
    355 			__func__, (uint32_t) opcode);
    356 	}
    357 }
    358 
    359