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