kernel - Fix a system lockup with vmm
[dragonfly.git] / sys / platform / pc64 / vmm / vmx.h
blobb3bc94e2fe3e3c280735c4165c222662dfc24406
1 /*
2 * Copyright (c) 2003-2013 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Mihai Carabas <mihai.carabas@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * 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
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifndef _VMM_VMX_H_
36 #define _VMM_VMX_H_
38 #include <machine/pmap.h>
40 #include "ept.h"
42 #define BIT(x) (1ULL << x)
45 /* VMX info for a generic control */
46 struct vmx_ctl_info {
47 uint32_t msr_addr;
48 uint32_t msr_true_addr;
49 uint32_t ctls;
52 /* The value of a setting */
53 typedef enum {
54 ZERO,
55 ONE,
56 DEFAULT
57 } setting_t;
59 /* VMX per cpu info */
60 struct vmx_pcpu_info {
61 unsigned char *vmxon_region_na;
62 unsigned char *vmxon_region;
63 struct vmx_thread_info *loaded_vmx;
66 struct vmx_thread_info {
67 unsigned char *vmcs_region_na;
68 unsigned char *vmcs_region;
70 int launched; /* vmclear or vmptrld called, must use vmlaunch */
71 int last_cpu; /* per-cpu info cached in VMCS for this cpu */
73 /* Guest unsaved registers in VMCS */
74 struct trapframe guest; /* put them directly in trapframe */
75 register_t guest_cr2;
77 /* Host unsaved registers in VMCS */
78 register_t host_rbx;
79 register_t host_rbp;
80 register_t host_r10;
81 register_t host_r11;
82 register_t host_r12;
83 register_t host_r13;
84 register_t host_r14;
85 register_t host_r15;
86 register_t host_rsp;
87 uint64_t vmexit_reason;
88 uint64_t vmexit_qualification;
89 uint64_t vmexit_interruption_info;
90 uint64_t vmexit_interruption_error;
91 uint64_t vmexit_instruction_length;
92 uint64_t guest_physical_address;
94 uint64_t guest_cr3;
95 uint64_t vmm_cr3;
96 invept_desc_t invept_desc;
97 long eptgen;
100 typedef enum {
107 LDTR,
109 } descriptor_t;
111 int vmx_launch(struct vmx_thread_info *);
112 int vmx_resume(struct vmx_thread_info *);
113 void vmx_vmexit(void);
118 * MSR register address
120 #define IA32_FEATURE_CONTROL 0x3A
121 #define IA32_VMX_BASIC 0x480
122 #define IA32_VMX_PINBASED_CTLS 0x481
123 #define IA32_VMX_PROCBASED_CTLS 0x482
124 #define IA32_VMX_EXIT_CTLS 0x483
125 #define IA32_VMX_ENTRY_CTLS 0x484
126 #define IA32_VMX_CR0_FIXED0 0x486
127 #define IA32_VMX_CR0_FIXED1 0x487
128 #define IA32_VMX_CR4_FIXED0 0x488
129 #define IA32_VMX_CR4_FIXED1 0x489
130 #define IA32_VMX_EPT_VPID_CAP 0x48C
131 #define IA32_VMX_PROCBASED_CTLS2 0x48B
132 #define IA32_VMX_TRUE_PINBASED_CTLS 0x48D
133 #define IA32_VMX_TRUE_PROCBASED_CTLS 0x48E
134 #define IA32_VMX_TRUE_EXIT_CTLS 0x48F
135 #define IA32_VMX_TRUE_ENTRY_CTLS 0x490
140 * IA32 FEATURE CONTROL bits
142 #define FEATURE_CONTROL_LOCKED 0
143 #define FEATURE_CONTROL_VMX_BIOS_ENABLED 2
148 * IA32_VMX_BASIC
149 * A.1 BASIC VMX INFORMATION
151 #define IS_TRUE_CTL_AVAIL(VMX_BASIC) ((VMX_BASIC) & (1ULL << (55)))
152 #define VMX_REVISION(reg_val) (reg_val & 0x7fffffff) /* 0:30 */
153 #define VMX_REGION_SIZE(reg_val) ((reg_val >> 32) & 0x01fff) /* 32:44 */
154 #define VMX_WIDTH_ADDR(reg_val) (reg_val >> 48 & 0x1) /* 48 */
155 #define VMXON_REGION_ALIGN_SIZE 4096ULL
156 #define VMXON_REGION_ALIGN(p) (((unsigned long long)(p) + VMXON_REGION_ALIGN_SIZE) & ~(VMXON_REGION_ALIGN_SIZE - 1))
161 * Pin-Based VM-Execution Controls
162 * Table 24-5. Definitions of Pin-Based Controls
163 * */
164 #define PINBASED_EXTERNAL_INTERRUPT_EXITING 0
165 #define PINBASED_NMI_EXITING 3
166 #define PINBASED_VIRTUAL_NMIS 5
167 #define PINBASED_ACTIVATE_VMX_PREEMPTION_TIEMR 6
168 #define PINBASED_PROCESS_POSTED_INTERRUPTS 7
173 * Processor-Based VM-Execution Controls
174 * Table 24-6. Definitions of Primary Processor-Based Controls
176 #define PROCBASED_INTERRUPT_WINDOW_EXITING 2
177 #define PROCBASED_USE_TSC_OFFSETING 3
178 #define PROCBASED_HLT_OFFSETING 7
179 #define PROCBASED_INVLPG_EXITING 9
180 #define PROCBASED_MWAIT_EXITING 10
181 #define PROCBASED_RDPMC_EXITING 11
182 #define PROCBASED_RDTSC_EXITING 12
183 #define PROCBASED_CR3_LOAD_EXITING 15
184 #define PROCBASED_CR3_STORE_EXITING 16
185 #define PROCBASED_CR8_LOAD_EXITING 19
186 #define PROCBASED_CR8_STORE_EXITING 20
187 #define PROCBASED_USE_TPR_SHADOW 21
188 #define PROCBASED_NMI_WINDOWS_EXITING 22
189 #define PROCBASED_MOV_DR_EXITING 23
190 #define PROCBASED_UNCOND_IO_EXITING 24
191 #define PROCBASED_USE_IO_BITMAPS 25
192 #define PROCBASED_MONITOR_TRAP_FLAG 27
193 #define PROCBASED_USE_MSR_BITMAPS 28
194 #define PROCBASED_MONITOR_EXITING 29
195 #define PROCBASED_PAUSE_EXITING 30
196 #define PROCBASED_ACTIVATE_SECONDARY_CONTROLS 31
197 /* Table 24-7. Definitions of Secondary Processor-Based Controls */
198 #define PROCBASED2_VIRTUALIZE_APIC_ACCESSES 0
199 #define PROCBASED2_ENABLE_EPT 1
200 #define PROCBASED2_DESCRIPTOR_TABLE_EXITING 2
201 #define PROCBASED2_ENABLE_RDTSCP 3
202 #define PROCBASED2_VIRTUAL_x2APIC_MODE 4
203 #define PROCBASED2_ENABLE_VPID 5
204 #define PROCBASED2_WBINVD_EXITING 6
205 #define PROCBASED2_UNRESTRICTED_GUEST 7
206 #define PROCBASED2_APIC_REGISTER_VIRTULIZATION 8
207 #define PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY 9
208 #define PROCBASED2_PAUSE_LOOP_EXITING 10
209 #define PROCBASED2_RDRAND_EXITING 11
210 #define PROCBASED2_ENABLE_INVPCID 12
211 #define PROCBASED2_ENABLE_VM_FUNCTIONS 13
212 #define PROCBASED2_VMCS_SHADOWING 14
213 #define PROCBASED2_EPT_VIOLATION_VE 18
218 * VM-EXIT CONTROL FIELDS
219 * Table 24-10. Definitions of VM-Exit Controls
221 #define VMEXIT_SAVE_DEBUG_CONTROLS 2
222 #define VMEXIT_HOST_ADDRESS_SPACE_SIZE 9
223 #define VMEXIT_LOAD_IA32_PERF_GLOBAL_CTRL 12
224 #define VMEXIT_ACKNOWLEDGE_INTERRUPT_ON_EXIT 15
225 #define VMEXIT_SAVE_IA32_PAT 18
226 #define VMEXIT_LOAD_IA32_PAT 19
227 #define VMEXIT_SAVE_IA32_EFER 20
228 #define VMEXIT_LOAD_IA32_EFER 21
229 #define VMEXIT_SAVE_VMX_PREEMPTION_TIMER 22
234 * VM-ENTRY CONTROL FIELDS
235 * Table 24-12. Definitions of VM-Entry Controls
237 #define VMENTRY_LOAD_DEBUG_CONTROLS 2
238 #define VMENTRY_IA32e_MODE_GUEST 9
239 #define VMENTRY_ENTRY_TO_SMM 10
240 #define VMENTRY_DEACTIVATE_DUAL_MONITOR 11
241 #define VMENTRY_LOAD_IA32_PERF_GLOBAL_CTRL 13
242 #define VMENTRY_LOAD_IA32_PAT 14
243 #define VMENTRY_LOAD_IA32_EFER 15
247 #define IS_ONE_SETTING_ALLOWED(val, bit) \
248 ((val) & (1ULL << (bit + 32)))
250 #define IS_ZERO_SETTING_ALLOWED(val, bit) \
251 (((val) & (1ULL << (bit))) == 0)
256 * VMX Basic Exit Reasons
258 #define EXIT_REASON_EXCEPTION 0
259 #define EXIT_REASON_EXT_INTR 1
260 #define EXIT_REASON_TRIPLE_FAULT 2
261 #define EXIT_REASON_INIT 3
262 #define EXIT_REASON_SIPI 4
263 #define EXIT_REASON_IO_SMI 5
264 #define EXIT_REASON_SMI 6
265 #define EXIT_REASON_INTR_WINDOW 7
266 #define EXIT_REASON_NMI_WINDOW 8
267 #define EXIT_REASON_TASK_SWITCH 9
268 #define EXIT_REASON_CPUID 10
269 #define EXIT_REASON_GETSEC 11
270 #define EXIT_REASON_HLT 12
271 #define EXIT_REASON_INVD 13
272 #define EXIT_REASON_INVLPG 14
273 #define EXIT_REASON_RDPMC 15
274 #define EXIT_REASON_RDTSC 16
275 #define EXIT_REASON_RSM 17
276 #define EXIT_REASON_VMCALL 18
277 #define EXIT_REASON_VMCLEAR 19
278 #define EXIT_REASON_VMLAUNCH 20
279 #define EXIT_REASON_VMPTRLD 21
280 #define EXIT_REASON_VMPTRST 22
281 #define EXIT_REASON_VMREAD 23
282 #define EXIT_REASON_VMRESUME 24
283 #define EXIT_REASON_VMWRITE 25
284 #define EXIT_REASON_VMXOFF 26
285 #define EXIT_REASON_VMXON 27
286 #define EXIT_REASON_CR_ACCESS 28
287 #define EXIT_REASON_DR_ACCESS 29
288 #define EXIT_REASON_INOUT 30
289 #define EXIT_REASON_RDMSR 31
290 #define EXIT_REASON_WRMSR 32
291 #define EXIT_REASON_INVAL_VMCS 33
292 #define EXIT_REASON_INVAL_MSR 34
293 #define EXIT_REASON_MWAIT 36
294 #define EXIT_REASON_MTF 37
295 #define EXIT_REASON_MONITOR 39
296 #define EXIT_REASON_PAUSE 40
297 #define EXIT_REASON_MCE 41
298 #define EXIT_REASON_TPR 43
299 #define EXIT_REASON_APIC 44
300 #define EXIT_REASON_GDTR_IDTR 46
301 #define EXIT_REASON_LDTR_TR 47
302 #define EXIT_REASON_EPT_FAULT 48
303 #define EXIT_REASON_EPT_MISCONFIG 49
304 #define EXIT_REASON_INVEPT 50
305 #define EXIT_REASON_RDTSCP 51
306 #define EXIT_REASON_VMX_PREEMPT 52
307 #define EXIT_REASON_INVVPID 53
308 #define EXIT_REASON_WBINVD 54
309 #define EXIT_REASON_XSETBV 55
310 #define EXIT_REASON_APIC_WRITE 56
311 #define EXIT_REASON_RDRAND 57
312 #define EXIT_REASON_INVPCID 58
313 #define EXIT_REASON_VMFUNC 59
315 /* Table 24-2. Format of Access Rights */
316 #define VMCS_SEG_TYPE(x) (x)
317 #define VMCS_S (1 << 4) /* Descriptor type - 0 = system; 1 = code or data */
318 #define VMCS_DPL(x) (x << 5) /* Descriptor Privilege Level */
319 #define VMCS_P (1 << 7) /* Segment present*/
320 #define VMCS_AVL (1 << 12) /* Available for use by system software */
321 #define VMCS_L (1 << 13) /* 64-bit mode active (for CS only) */
322 #define VMCS_OP_SIZE (1 << 14) /* Default operation size (0 = 16-bit segment; 1 = 32-bit segment) */
323 #define VMCS_G (1 << 15) /* Granularity */
324 #define VMCS_SEG_UNUSABLE (1 << 16) /* Segment unusable (0 = usable; 1 = unusable) */
326 #define VMCS_EXCEPTION_TYPE(x) ((x >> 8) & 0x7)
327 #define VMCS_EXCEPTION_EXTERNAL_INTERRUPT 0
328 #define VMCS_EXCEPTION_NMI 2
329 #define VMCS_EXCEPTION_HARDWARE 3
330 #define VMCS_EXCEPTION_SOFTWARE 6
332 #define VMCS_EXCEPTION_NUMBER(x) (x & 0xFF)
333 #endif