#include <palacios/vmm_types.h>
- /* Pin Based VM Execution Controls */
- /* INTEL MANUAL: 20-10 vol 3B */
-#define EXT_INTR_EXIT 0x00000001
-#define NMI_EXIT 0x00000008
-#define VIRTUAL_NMIS 0x00000020
-/* Processor Based VM Execution Controls */
-/* INTEL MANUAL: 20-11 vol. 3B */
-#define INTR_WIN_EXIT 0x00000004
-#define USE_TSC_OFFSET 0x00000008
-#define HLT_EXIT 0x00000080
-#define INVLPG_EXIT 0x00000200
-#define MWAIT_EXIT 0x00000400
-#define RDPMC_EXIT 0x00000800
-#define RDTSC_EXIT 0x00001000
-#define CR8_LOAD_EXIT 0x00080000
-#define CR8_STORE_EXIT 0x00100000
-#define USE_TPR_SHADOW 0x00200000
-#define NMI_WINDOW_EXIT 0x00400000
-#define MOVDR_EXIT 0x00800000
-#define UNCOND_IO_EXIT 0x01000000
-#define USE_IO_BITMAPS 0x02000000
-#define USE_MSR_BITMAPS 0x10000000
-#define MONITOR_EXIT 0x20000000
-#define PAUSE_EXIT 0x40000000
+#include <palacios/vm_guest.h>
+
/* VM-Exit Controls */
/* INTEL MANUAL: 20-16 vol. 3B */
#define HOST_ADDR_SPACE_SIZE 0x00000200
#define ACK_IRQ_ON_EXIT 0x00008000
+/* Control register exit masks */
+#define CR4_VMXE 0x00002000
+#define CR4_PAE 0x00000020
+
+
+
+struct vmcs_field_encoding {
+ uint8_t access_type : 1; /* 0 = full, 1 = high, (for accessing 64 bit fields on 32bit CPU) */
+ uint16_t index : 9;
+ uint8_t type : 2; /* 0=ctrl, 1=read-only, 2 = guest state, 3 = host state */
+ uint8_t rsvd1 : 1; /* MBZ */
+ uint8_t width : 2; /* 0 = 16bit, 1 = 64bit, 2 = 32bit, 3 = natural width */
+ uint32_t rsvd2 : 17;
+} __attribute__((packed));
+
+
typedef enum {
+ /* 16 bit control field */
+ VMCS_VPID = 0x00000000,
+ /* 16 bit guest state */
VMCS_GUEST_ES_SELECTOR = 0x00000800,
VMCS_GUEST_CS_SELECTOR = 0x00000802,
VMCS_GUEST_SS_SELECTOR = 0x00000804,
VMCS_TSC_OFFSET_HIGH = 0x00002011,
VMCS_VAPIC_ADDR = 0x00002012,
VMCS_VAPIC_ADDR_HIGH = 0x00002013,
+ VMCS_APIC_ACCESS_ADDR = 0x00002014,
+ VMCS_APIC_ACCESS_ADDR_HIGH = 0x00002015,
+ VMCS_EPT_PTR = 0x0000201A,
+ VMCS_EPT_PTR_HIGH = 0x0000201B,
+ /* 64 bit read only data field */
+ VMCS_GUEST_PHYS_ADDR = 0x00002400,
+ VMCS_GUEST_PHYS_ADDR_HIGH = 0x00002401,
/* 64 bit guest state fields */
VMCS_LINK_PTR = 0x00002800,
VMCS_LINK_PTR_HIGH = 0x00002801,
- VMCS_GUEST_DBG_CTL = 0x00002802,
- VMCS_GUEST_DBG_CTL_HIGH = 0x00002803,
+ VMCS_GUEST_DBG_CTL = 0x00002802,
+ VMCS_GUEST_DBG_CTL_HIGH = 0x00002803,
+ VMCS_GUEST_PAT = 0x00002804,
+ VMCS_GUEST_PAT_HIGH = 0x00002805,
+ VMCS_GUEST_EFER = 0x00002806,
+ VMCS_GUEST_EFER_HIGH = 0x00002807,
VMCS_GUEST_PERF_GLOBAL_CTRL = 0x00002808,
VMCS_GUEST_PERF_GLOBAL_CTRL_HIGH = 0x00002809,
+ VMCS_GUEST_PDPTE0 = 0x0000280A,
+ VMCS_GUEST_PDPTE0_HIGH = 0x0000280B,
+ VMCS_GUEST_PDPTE1 = 0x0000280C,
+ VMCS_GUEST_PDPTE1_HIGH = 0x0000280D,
+ VMCS_GUEST_PDPTE2 = 0x0000280E,
+ VMCS_GUEST_PDPTE2_HIGH = 0x0000280F,
+ VMCS_GUEST_PDPTE3 = 0x00002810,
+ VMCS_GUEST_PDPTE3_HIGH = 0x00002811,
+ /* 64 bit host state fields */
+ VMCS_HOST_PAT = 0x00002c00,
+ VMCS_HOST_PAT_HIGH = 0x00002c01,
+ VMCS_HOST_EFER = 0x00002c02,
+ VMCS_HOST_EFER_HIGH = 0x00002c03,
+ VMCS_HOST_PERF_GLOBAL_CTRL = 0x00002c04,
+ VMCS_HOST_PERF_GLOBAL_CTRL_HIGH = 0x00002c05,
/* 32 bit control fields */
VMCS_PIN_CTRLS = 0x00004000,
VMCS_PROC_CTRLS = 0x00004002,
VMCS_ENTRY_EXCP_ERR = 0x00004018,
VMCS_ENTRY_INSTR_LEN = 0x0000401A,
VMCS_TPR_THRESHOLD = 0x0000401C,
+ VMCS_SEC_PROC_CTRLS = 0x0000401e,
+ VMCS_PLE_GAP = 0x00004020,
+ VMCS_PLE_WINDOW = 0x00004022,
/* 32 bit Read Only data fields */
VMCS_INSTR_ERR = 0x00004400,
VMCS_EXIT_REASON = 0x00004402,
VMCS_IDT_VECTOR_INFO = 0x00004408,
VMCS_IDT_VECTOR_ERR = 0x0000440A,
VMCS_EXIT_INSTR_LEN = 0x0000440C,
- VMCS_VMX_INSTR_INFO = 0x0000440E,
+ VMCS_EXIT_INSTR_INFO = 0x0000440E,
/* 32 bit Guest state fields */
VMCS_GUEST_ES_LIMIT = 0x00004800,
VMCS_GUEST_CS_LIMIT = 0x00004802,
VMCS_GUEST_ACTIVITY_STATE = 0x00004826,
VMCS_GUEST_SMBASE = 0x00004828,
VMCS_GUEST_SYSENTER_CS = 0x0000482A,
+ VMCS_PREEMPT_TIMER = 0x0000482E,
/* 32 bit host state field */
VMCS_HOST_SYSENTER_CS = 0x00004C00,
/* Natural Width Control Fields */
VMCS_HOST_RIP = 0x00006C16,
} vmcs_field_t;
-int v3_vmcs_get_field_len(vmcs_field_t field);
-const char * v3_vmcs_get_field_name(vmcs_field_t field);
-
-/* VMCS Exit QUALIFICATIONs */
-struct VMExitIOQual {
- uint32_t accessSize : 3; // (0: 1 Byte ;; 1: 2 Bytes ;; 3: 4 Bytes)
- uint32_t dir : 1; // (0: Out ;; 1: In)
- uint32_t string : 1; // (0: not string ;; 1: string)
- uint32_t REP : 1; // (0: not REP ;; 1: REP)
- uint32_t opEnc : 1; // (0: DX ;; 1: immediate)
- uint32_t rsvd : 9; // Set to 0
- uint32_t port : 16; // IO Port Number
+struct vmx_exception_bitmap {
+ union {
+ uint32_t value;
+ struct {
+ uint_t de : 1; // (0) divide by zero
+ uint_t db : 1; // (1) Debug
+ uint_t nmi : 1; // (2) Non-maskable interrupt
+ uint_t bp : 1; // (3) Breakpoint
+ uint_t of : 1; // (4) Overflow
+ uint_t br : 1; // (5) Bound-Range
+ uint_t ud : 1; // (6) Invalid-Opcode
+ uint_t nm : 1; // (7) Device-not-available
+ uint_t df : 1; // (8) Double Fault
+ uint_t ex9 : 1;
+ uint_t ts : 1; // (10) Invalid TSS
+ uint_t np : 1; // (11) Segment-not-present
+ uint_t ss : 1; // (12) Stack
+ uint_t gp : 1; // (13) General Protection Fault
+ uint_t pf : 1; // (14) Page fault
+ uint_t ex15 : 1;
+ uint_t mf : 1; // (15) Floating point exception
+ uint_t ac : 1; // (16) Alignment-check
+ uint_t mc : 1; // (17) Machine Check
+ uint_t xf : 1; // (18) SIMD floating-point
+ uint_t ex20 : 1;
+ uint_t ex21 : 1;
+ uint_t ex22 : 1;
+ uint_t ex23 : 1;
+ uint_t ex24 : 1;
+ uint_t ex25 : 1;
+ uint_t ex26 : 1;
+ uint_t ex27 : 1;
+ uint_t ex28 : 1;
+ uint_t ex29 : 1;
+ uint_t sx : 1; // (30) Security Exception
+ uint_t ex31 : 1;
+ } __attribute__ ((packed));
+ } __attribute__ ((packed));
} __attribute__((packed));
-struct VMExitDBGQual {
- uint32_t B0 : 1; // Breakpoint 0 condition met
- uint32_t B1 : 1; // Breakpoint 1 condition met
- uint32_t B2 : 1; // Breakpoint 2 condition met
- uint32_t B3 : 1; // Breakpoint 3 condition met
- uint32_t rsvd : 9; // reserved to 0
- uint32_t BD : 1; // detected DBG reg access
- uint32_t BS : 1; // cause either single instr or taken branch
-} __attribute__((packed));
-
-
-struct VMExitTSQual {
- uint32_t selector : 16; // selector of destination TSS
- uint32_t rsvd : 14; // reserved to 0
- uint32_t src : 2; // (0: CALL ; 1: IRET ; 2: JMP ; 3: Task gate in IDT)
+struct vmx_intr_state {
+ union {
+ uint32_t value;
+ struct {
+ uint32_t block_sti : 1;
+ uint32_t block_mov_ss : 1;
+ uint32_t block_smi : 1;
+ uint32_t block_nmi : 1;
+ uint32_t rsvd : 28;
+ } __attribute__((packed));
+ } __attribute__((packed));
} __attribute__((packed));
-struct VMExitCRQual {
- uint32_t crID : 4; // cr number (0 for CLTS and LMSW) (bit 3 always 0, on 32bit)
- uint32_t accessType : 2; // (0: MOV to CR ; 1: MOV from CR ; 2: CLTS ; 3: LMSW)
- uint32_t lmswOpType : 1; // (0: register ; 1: memory)
- uint32_t rsvd1 : 1; // reserved to 0
- uint32_t gpr : 4; // (0:RAX+[CLTS/LMSW], 1:RCX, 2:RDX, 3:RBX, 4:RSP, 5:RBP, 6:RSI, 6:RDI, 8-15:64bit regs)
- uint32_t rsvd2 : 4; // reserved to 0
- uint32_t lmswSrc : 16; // src data for lmsw
-} __attribute__((packed));
-struct VMExitMovDRQual {
- uint32_t regID : 3; // debug register number
- uint32_t rsvd1 : 1; // reserved to 0
- uint32_t dir : 1; // (0: MOV to DR , 1: MOV from DR)
- uint32_t rsvd2 : 3; // reserved to 0
- uint32_t gpr : 4; // (0:RAX, 1:RCX, 2:RDX, 3:RBX, 4:RSP, 5:RBP, 6:RSI, 6:RDI, 8-15:64bit regs)
-} __attribute__((packed));
+struct vmx_pending_dbg_excps {
+ union {
+ uint64_t value;
-/* End Exit Qualifications */
+ struct {
+ uint32_t lo;
+ uint32_t hi;
+ } __attribute__((packed));
-/* Exit Vector Info */
-struct VMExitIntInfo {
- uint32_t nr : 8; // IRQ number, exception vector, NMI = 2
- uint32_t type : 3; // (0: ext. IRQ , 2: NMI , 3: hw exception , 6: sw exception
- uint32_t errorCode : 1; // 1: error Code present
- uint32_t iret : 1; // something to do with NMIs and IRETs (Intel 3B, sec. 23.2.2)
- uint32_t rsvd : 18; // always 0
- uint32_t valid : 1; // always 1 if valid
+ struct {
+ uint64_t b0 : 1;
+ uint64_t b1 : 1;
+ uint64_t b2 : 1;
+ uint64_t b3 : 1;
+ uint64_t rsvd1 : 8;
+ uint64_t bp_set : 1;
+ uint64_t rsvd2 : 1;
+ uint64_t bp_ss : 1;
+ uint64_t rsvd3 : 49;
+ } __attribute__((packed));
+ } __attribute__((packed));
} __attribute__((packed));
-
-
-
-/* End Exit Vector Info */
-
-
-
-
/* Segment Selector Access Rights (32 bits) */
/* INTEL Manual: 20-4 vol 3B */
+struct vmcs_segment {
+ uint16_t selector;
+ uint32_t limit;
+ uint64_t base;
-
-struct vmcs_segment_access {
union {
- uint32_t value;
+ uint32_t val;
struct {
uint32_t type : 4;
uint32_t desc_type : 1;
uint32_t rsvd1 : 4;
uint32_t avail : 1;
uint32_t long_mode : 1; // CS only (64 bit active), reserved otherwise
- uint32_t DB : 1;
+ uint32_t db : 1;
uint32_t granularity : 1;
uint32_t unusable : 1;
uint32_t rsvd2 : 15;
} __attribute__((packed));
- } __attribute__((packed));
-}__attribute__((packed));
+ } __attribute__((packed)) access;
+};
+
+
+struct vmcs_msr_entry {
+ uint32_t index;
+ uint32_t rsvd;
+ uint32_t lo;
+ uint32_t hi;
+} __attribute__((packed));
struct vmcs_interrupt_state {
- uint32_t sti_blocking : 1;
- uint32_t mov_ss_blocking : 1;
- uint32_t smi_blocking : 1;
- uint32_t nmi_blocking : 1;
- uint32_t rsvd1 : 28;
+ union {
+ uint32_t val;
+ struct {
+ uint32_t sti_blocking : 1;
+ uint32_t mov_ss_blocking : 1;
+ uint32_t smi_blocking : 1;
+ uint32_t nmi_blocking : 1;
+ uint32_t rsvd1 : 28;
+ } __attribute__((packed));
+ } __attribute__((packed));
} __attribute__((packed));
+
struct vmcs_data {
uint32_t revision ;
uint32_t abort ;
} __attribute__((packed));
-//uint_t VMCSRead(uint_t tag, void * val);
+int v3_vmcs_get_field_len(vmcs_field_t field);
+
+const char * v3_vmcs_field_to_str(vmcs_field_t field);
+
+void v3_print_vmcs();
+
+
+int v3_vmx_save_vmcs(struct guest_info * info);
+int v3_vmx_restore_vmcs(struct guest_info * info);
+
+
+int v3_update_vmcs_host_state(struct guest_info * info);
+int v3_update_vmcs_ctrl_fields(struct guest_info * info);
+
+
+int v3_read_vmcs_segments(struct v3_segments * segs);
+int v3_write_vmcs_segments(struct v3_segments * segs);
+void v3_vmxseg_to_seg(struct vmcs_segment * vmcs_seg, struct v3_segment * seg);
+void v3_seg_to_vmxseg(struct v3_segment * seg, struct vmcs_segment * vmcs_seg);
#endif // ! __V3VEE__