Home | History | Annotate | Line # | Download | only in x86
nvmm_x86.h revision 1.16
      1 /*	$NetBSD: nvmm_x86.h,v 1.16 2019/10/23 07:01:11 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _NVMM_X86_H_
     33 #define _NVMM_X86_H_
     34 
     35 /* -------------------------------------------------------------------------- */
     36 
     37 #ifndef ASM_NVMM
     38 
     39 struct nvmm_x86_exit_memory {
     40 	int prot;
     41 	gpaddr_t gpa;
     42 	uint8_t inst_len;
     43 	uint8_t inst_bytes[15];
     44 };
     45 
     46 struct nvmm_x86_exit_io {
     47 	bool in;
     48 	uint16_t port;
     49 	int8_t seg;
     50 	uint8_t address_size;
     51 	uint8_t operand_size;
     52 	bool rep;
     53 	bool str;
     54 	uint64_t npc;
     55 };
     56 
     57 struct nvmm_x86_exit_rdmsr {
     58 	uint32_t msr;
     59 	uint64_t npc;
     60 };
     61 
     62 struct nvmm_x86_exit_wrmsr {
     63 	uint32_t msr;
     64 	uint64_t val;
     65 	uint64_t npc;
     66 };
     67 
     68 struct nvmm_x86_exit_insn {
     69 	uint64_t npc;
     70 };
     71 
     72 struct nvmm_x86_exit_invalid {
     73 	uint64_t hwcode;
     74 };
     75 
     76 /* Generic. */
     77 #define NVMM_VCPU_EXIT_NONE		0x0000000000000000ULL
     78 #define NVMM_VCPU_EXIT_INVALID		0xFFFFFFFFFFFFFFFFULL
     79 /* x86: operations. */
     80 #define NVMM_VCPU_EXIT_MEMORY		0x0000000000000001ULL
     81 #define NVMM_VCPU_EXIT_IO		0x0000000000000002ULL
     82 /* x86: changes in VCPU state. */
     83 #define NVMM_VCPU_EXIT_SHUTDOWN		0x0000000000001000ULL
     84 #define NVMM_VCPU_EXIT_INT_READY	0x0000000000001001ULL
     85 #define NVMM_VCPU_EXIT_NMI_READY	0x0000000000001002ULL
     86 #define NVMM_VCPU_EXIT_HALTED		0x0000000000001003ULL
     87 /* x86: instructions. */
     88 #define NVMM_VCPU_EXIT_RDMSR		0x0000000000002000ULL
     89 #define NVMM_VCPU_EXIT_WRMSR		0x0000000000002001ULL
     90 #define NVMM_VCPU_EXIT_MONITOR		0x0000000000002002ULL
     91 #define NVMM_VCPU_EXIT_MWAIT		0x0000000000002003ULL
     92 #define NVMM_VCPU_EXIT_CPUID		0x0000000000002004ULL
     93 
     94 struct nvmm_x86_exit {
     95 	uint64_t reason;
     96 	union {
     97 		struct nvmm_x86_exit_memory mem;
     98 		struct nvmm_x86_exit_io io;
     99 		struct nvmm_x86_exit_rdmsr rdmsr;
    100 		struct nvmm_x86_exit_wrmsr wrmsr;
    101 		struct nvmm_x86_exit_insn insn;
    102 		struct nvmm_x86_exit_invalid inv;
    103 	} u;
    104 	uint64_t exitstate[8];
    105 };
    106 
    107 #define NVMM_VCPU_EVENT_EXCP	0
    108 #define NVMM_VCPU_EVENT_INTR	1
    109 
    110 struct nvmm_x86_event {
    111 	u_int type;
    112 	uint8_t vector;
    113 	union {
    114 		struct {
    115 			uint64_t error;
    116 		} excp;
    117 	} u;
    118 };
    119 
    120 struct nvmm_cap_md {
    121 	uint64_t xcr0_mask;
    122 	uint32_t mxcsr_mask;
    123 	uint32_t conf_cpuid_maxops;
    124 	uint64_t rsvd[6];
    125 };
    126 
    127 #endif
    128 
    129 /* -------------------------------------------------------------------------- */
    130 
    131 /*
    132  * Segment state indexes. We use X64 as naming convention, not to confuse with
    133  * X86 which originally implied 32bit.
    134  */
    135 
    136 /* Segments. */
    137 #define NVMM_X64_SEG_ES			0
    138 #define NVMM_X64_SEG_CS			1
    139 #define NVMM_X64_SEG_SS			2
    140 #define NVMM_X64_SEG_DS			3
    141 #define NVMM_X64_SEG_FS			4
    142 #define NVMM_X64_SEG_GS			5
    143 #define NVMM_X64_SEG_GDT		6
    144 #define NVMM_X64_SEG_IDT		7
    145 #define NVMM_X64_SEG_LDT		8
    146 #define NVMM_X64_SEG_TR			9
    147 #define NVMM_X64_NSEG			10
    148 
    149 /* General Purpose Registers. */
    150 #define NVMM_X64_GPR_RAX		0
    151 #define NVMM_X64_GPR_RCX		1
    152 #define NVMM_X64_GPR_RDX		2
    153 #define NVMM_X64_GPR_RBX		3
    154 #define NVMM_X64_GPR_RSP		4
    155 #define NVMM_X64_GPR_RBP		5
    156 #define NVMM_X64_GPR_RSI		6
    157 #define NVMM_X64_GPR_RDI		7
    158 #define NVMM_X64_GPR_R8			8
    159 #define NVMM_X64_GPR_R9			9
    160 #define NVMM_X64_GPR_R10		10
    161 #define NVMM_X64_GPR_R11		11
    162 #define NVMM_X64_GPR_R12		12
    163 #define NVMM_X64_GPR_R13		13
    164 #define NVMM_X64_GPR_R14		14
    165 #define NVMM_X64_GPR_R15		15
    166 #define NVMM_X64_GPR_RIP		16
    167 #define NVMM_X64_GPR_RFLAGS		17
    168 #define NVMM_X64_NGPR			18
    169 
    170 /* Control Registers. */
    171 #define NVMM_X64_CR_CR0			0
    172 #define NVMM_X64_CR_CR2			1
    173 #define NVMM_X64_CR_CR3			2
    174 #define NVMM_X64_CR_CR4			3
    175 #define NVMM_X64_CR_CR8			4
    176 #define NVMM_X64_CR_XCR0		5
    177 #define NVMM_X64_NCR			6
    178 
    179 /* Debug Registers. */
    180 #define NVMM_X64_DR_DR0			0
    181 #define NVMM_X64_DR_DR1			1
    182 #define NVMM_X64_DR_DR2			2
    183 #define NVMM_X64_DR_DR3			3
    184 #define NVMM_X64_DR_DR6			4
    185 #define NVMM_X64_DR_DR7			5
    186 #define NVMM_X64_NDR			6
    187 
    188 /* MSRs. */
    189 #define NVMM_X64_MSR_EFER		0
    190 #define NVMM_X64_MSR_STAR		1
    191 #define NVMM_X64_MSR_LSTAR		2
    192 #define NVMM_X64_MSR_CSTAR		3
    193 #define NVMM_X64_MSR_SFMASK		4
    194 #define NVMM_X64_MSR_KERNELGSBASE	5
    195 #define NVMM_X64_MSR_SYSENTER_CS	6
    196 #define NVMM_X64_MSR_SYSENTER_ESP	7
    197 #define NVMM_X64_MSR_SYSENTER_EIP	8
    198 #define NVMM_X64_MSR_PAT		9
    199 #define NVMM_X64_MSR_TSC		10
    200 #define NVMM_X64_NMSR			11
    201 
    202 #ifndef ASM_NVMM
    203 
    204 #include <sys/types.h>
    205 #include <x86/cpu_extended_state.h>
    206 
    207 struct nvmm_x64_state_seg {
    208 	uint16_t selector;
    209 	struct {		/* hidden */
    210 		uint16_t type:4;
    211 		uint16_t s:1;
    212 		uint16_t dpl:2;
    213 		uint16_t p:1;
    214 		uint16_t avl:1;
    215 		uint16_t l:1;
    216 		uint16_t def:1;
    217 		uint16_t g:1;
    218 		uint16_t rsvd:4;
    219 	} attrib;
    220 	uint32_t limit;		/* hidden */
    221 	uint64_t base;		/* hidden */
    222 };
    223 
    224 struct nvmm_x64_state_intr {
    225 	uint64_t int_shadow:1;
    226 	uint64_t int_window_exiting:1;
    227 	uint64_t nmi_window_exiting:1;
    228 	uint64_t evt_pending:1;
    229 	uint64_t rsvd:60;
    230 };
    231 
    232 /* VM exit state indexes. */
    233 #define NVMM_X64_EXITSTATE_CR8			0
    234 #define NVMM_X64_EXITSTATE_RFLAGS		1
    235 #define NVMM_X64_EXITSTATE_INT_SHADOW		2
    236 #define NVMM_X64_EXITSTATE_INT_WINDOW_EXIT	3
    237 #define NVMM_X64_EXITSTATE_NMI_WINDOW_EXIT	4
    238 #define NVMM_X64_EXITSTATE_EVT_PENDING		5
    239 
    240 /* Flags. */
    241 #define NVMM_X64_STATE_SEGS	0x01
    242 #define NVMM_X64_STATE_GPRS	0x02
    243 #define NVMM_X64_STATE_CRS	0x04
    244 #define NVMM_X64_STATE_DRS	0x08
    245 #define NVMM_X64_STATE_MSRS	0x10
    246 #define NVMM_X64_STATE_INTR	0x20
    247 #define NVMM_X64_STATE_FPU	0x40
    248 #define NVMM_X64_STATE_ALL	\
    249 	(NVMM_X64_STATE_SEGS | NVMM_X64_STATE_GPRS | NVMM_X64_STATE_CRS | \
    250 	 NVMM_X64_STATE_DRS | NVMM_X64_STATE_MSRS | NVMM_X64_STATE_INTR | \
    251 	 NVMM_X64_STATE_FPU)
    252 
    253 struct nvmm_x64_state {
    254 	struct nvmm_x64_state_seg segs[NVMM_X64_NSEG];
    255 	uint64_t gprs[NVMM_X64_NGPR];
    256 	uint64_t crs[NVMM_X64_NCR];
    257 	uint64_t drs[NVMM_X64_NDR];
    258 	uint64_t msrs[NVMM_X64_NMSR];
    259 	struct nvmm_x64_state_intr intr;
    260 	struct fxsave fpu;
    261 };
    262 
    263 #define NVMM_VCPU_CONF_CPUID	NVMM_VCPU_CONF_MD_BEGIN
    264 
    265 struct nvmm_vcpu_conf_cpuid {
    266 	/* The options. */
    267 	uint32_t mask:1;
    268 	uint32_t exit:1;
    269 	uint32_t rsvd:30;
    270 
    271 	/* The leaf. */
    272 	uint32_t leaf;
    273 
    274 	/* The params. */
    275 	union {
    276 		struct {
    277 			struct {
    278 				uint32_t eax;
    279 				uint32_t ebx;
    280 				uint32_t ecx;
    281 				uint32_t edx;
    282 			} set;
    283 			struct {
    284 				uint32_t eax;
    285 				uint32_t ebx;
    286 				uint32_t ecx;
    287 				uint32_t edx;
    288 			} del;
    289 		} mask;
    290 	} u;
    291 };
    292 
    293 #define nvmm_vcpu_exit		nvmm_x86_exit
    294 #define nvmm_vcpu_event		nvmm_x86_event
    295 #define nvmm_vcpu_state		nvmm_x64_state
    296 
    297 #ifdef _KERNEL
    298 #define NVMM_X86_MACH_NCONF	0
    299 #define NVMM_X86_VCPU_NCONF	1
    300 struct nvmm_x86_cpuid_mask {
    301 	uint32_t eax;
    302 	uint32_t ebx;
    303 	uint32_t ecx;
    304 	uint32_t edx;
    305 };
    306 extern const struct nvmm_x64_state nvmm_x86_reset_state;
    307 extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_00000001;
    308 extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_00000007;
    309 extern const struct nvmm_x86_cpuid_mask nvmm_cpuid_80000001;
    310 bool nvmm_x86_pat_validate(uint64_t);
    311 #endif
    312 
    313 #endif /* ASM_NVMM */
    314 
    315 #endif /* _NVMM_X86_H_ */
    316