world/kernel: Use the rounddown2() macro in various places.
[dragonfly.git] / sys / platform / pc64 / vmm / vmx.h
1 /*
2  * Copyright (c) 2003-2013 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Mihai Carabas <mihai.carabas@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
34
35 #ifndef _VMM_VMX_H_
36 #define _VMM_VMX_H_
37
38 #include <machine/pmap.h>
39
40 #include "ept.h"
41
42 #define BIT(x)  (1ULL << x)
43
44
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;
50 };
51
52 /* The value of a setting */
53 typedef enum {
54         ZERO,
55         ONE,
56         DEFAULT
57 } setting_t;
58
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;
64 };
65
66 struct vmx_thread_info {
67         unsigned char *vmcs_region_na;
68         unsigned char *vmcs_region;
69
70         int launched;   /* vmclear or vmptrld called, must use vmlaunch */
71         int last_cpu;   /* per-cpu info cached in VMCS for this cpu */
72
73         /* Guest unsaved registers in VMCS */
74         struct trapframe guest; /* put them directly in trapframe */
75         /*union savefpu guest_fpu; FUTURE */
76         register_t      guest_cr2;
77
78         /* Host unsaved registers in VMCS */
79         register_t      host_rbx;
80         register_t      host_rbp;
81         register_t      host_r10;
82         register_t      host_r11;
83         register_t      host_r12;
84         register_t      host_r13;
85         register_t      host_r14;
86         register_t      host_r15;
87         register_t      host_rsp;
88         uint64_t        vmexit_reason;
89         uint64_t        vmexit_qualification;
90         uint64_t        vmexit_interruption_info;
91         uint64_t        vmexit_interruption_error;
92         uint64_t        vmexit_instruction_length;
93         uint64_t        guest_physical_address;
94
95         uint64_t        guest_cr0;
96         uint64_t        guest_cr3;
97         uint64_t        vmm_cr3;
98         invept_desc_t   invept_desc;
99         long            eptgen;
100 };
101
102 typedef enum {
103         ES,
104         CS,
105         SS,
106         DS,
107         FS,
108         GS,
109         LDTR,
110         TR
111 } descriptor_t;
112
113 int vmx_launch(struct vmx_thread_info *);
114 int vmx_resume(struct vmx_thread_info *);
115 void vmx_vmexit(void);
116
117
118
119 /*
120  * MSR register address
121  */
122 #define         IA32_FEATURE_CONTROL                    0x3A
123 #define         IA32_VMX_BASIC                          0x480
124 #define         IA32_VMX_PINBASED_CTLS                  0x481
125 #define         IA32_VMX_PROCBASED_CTLS                 0x482
126 #define         IA32_VMX_EXIT_CTLS                      0x483
127 #define         IA32_VMX_ENTRY_CTLS                     0x484
128 #define         IA32_VMX_CR0_FIXED0                     0x486
129 #define         IA32_VMX_CR0_FIXED1                     0x487
130 #define         IA32_VMX_CR4_FIXED0                     0x488
131 #define         IA32_VMX_CR4_FIXED1                     0x489
132 #define         IA32_VMX_EPT_VPID_CAP                   0x48C
133 #define         IA32_VMX_PROCBASED_CTLS2                0x48B
134 #define         IA32_VMX_TRUE_PINBASED_CTLS             0x48D
135 #define         IA32_VMX_TRUE_PROCBASED_CTLS            0x48E
136 #define         IA32_VMX_TRUE_EXIT_CTLS                 0x48F
137 #define         IA32_VMX_TRUE_ENTRY_CTLS                0x490
138
139
140
141 /*
142  * IA32 FEATURE CONTROL bits
143  */
144 #define         FEATURE_CONTROL_LOCKED                  0
145 #define         FEATURE_CONTROL_VMX_BIOS_ENABLED        2
146
147
148
149 /*
150  * IA32_VMX_BASIC
151  * A.1 BASIC VMX INFORMATION
152  */
153 #define         IS_TRUE_CTL_AVAIL(VMX_BASIC)    ((VMX_BASIC) & (1ULL << (55)))
154 #define         VMX_REVISION(reg_val)           (reg_val & 0x7fffffff) /* 0:30 */
155 #define         VMX_REGION_SIZE(reg_val)        ((reg_val >> 32) & 0x01fff) /* 32:44 */
156 #define         VMX_WIDTH_ADDR(reg_val)         (reg_val >> 48 & 0x1) /* 48 */
157 #define         VMXON_REGION_ALIGN_SIZE         4096ULL
158 #define         VMXON_REGION_ALIGN(p)           rounddown2((unsigned long long)(p) + VMXON_REGION_ALIGN_SIZE, VMXON_REGION_ALIGN_SIZE)
159
160
161
162 /*
163  * Pin-Based VM-Execution Controls
164  * Table 24-5. Definitions of Pin-Based Controls
165  * */
166 #define         PINBASED_EXTERNAL_INTERRUPT_EXITING     0
167 #define         PINBASED_NMI_EXITING                    3
168 #define         PINBASED_VIRTUAL_NMIS                   5
169 #define         PINBASED_ACTIVATE_VMX_PREEMPTION_TIEMR  6
170 #define         PINBASED_PROCESS_POSTED_INTERRUPTS      7
171
172
173
174 /*
175  * Processor-Based VM-Execution Controls
176  * Table 24-6. Definitions of Primary Processor-Based Controls
177  */
178 #define         PROCBASED_INTERRUPT_WINDOW_EXITING      2
179 #define         PROCBASED_USE_TSC_OFFSETING             3
180 #define         PROCBASED_HLT_OFFSETING                 7
181 #define         PROCBASED_INVLPG_EXITING                9
182 #define         PROCBASED_MWAIT_EXITING                 10
183 #define         PROCBASED_RDPMC_EXITING                 11
184 #define         PROCBASED_RDTSC_EXITING                 12
185 #define         PROCBASED_CR3_LOAD_EXITING              15
186 #define         PROCBASED_CR3_STORE_EXITING             16
187 #define         PROCBASED_CR8_LOAD_EXITING              19
188 #define         PROCBASED_CR8_STORE_EXITING             20
189 #define         PROCBASED_USE_TPR_SHADOW                21
190 #define         PROCBASED_NMI_WINDOWS_EXITING           22
191 #define         PROCBASED_MOV_DR_EXITING                23
192 #define         PROCBASED_UNCOND_IO_EXITING             24
193 #define         PROCBASED_USE_IO_BITMAPS                25
194 #define         PROCBASED_MONITOR_TRAP_FLAG             27
195 #define         PROCBASED_USE_MSR_BITMAPS               28
196 #define         PROCBASED_MONITOR_EXITING               29
197 #define         PROCBASED_PAUSE_EXITING                 30
198 #define         PROCBASED_ACTIVATE_SECONDARY_CONTROLS   31
199 /* Table 24-7. Definitions of Secondary Processor-Based Controls */
200 #define         PROCBASED2_VIRTUALIZE_APIC_ACCESSES     0
201 #define         PROCBASED2_ENABLE_EPT                   1
202 #define         PROCBASED2_DESCRIPTOR_TABLE_EXITING     2
203 #define         PROCBASED2_ENABLE_RDTSCP                3
204 #define         PROCBASED2_VIRTUAL_x2APIC_MODE          4
205 #define         PROCBASED2_ENABLE_VPID                  5
206 #define         PROCBASED2_WBINVD_EXITING               6
207 #define         PROCBASED2_UNRESTRICTED_GUEST           7
208 #define         PROCBASED2_APIC_REGISTER_VIRTULIZATION  8
209 #define         PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY   9
210 #define         PROCBASED2_PAUSE_LOOP_EXITING           10
211 #define         PROCBASED2_RDRAND_EXITING               11
212 #define         PROCBASED2_ENABLE_INVPCID               12
213 #define         PROCBASED2_ENABLE_VM_FUNCTIONS          13
214 #define         PROCBASED2_VMCS_SHADOWING               14
215 #define         PROCBASED2_EPT_VIOLATION_VE             18
216
217
218
219 /*
220  * VM-EXIT CONTROL FIELDS
221  * Table 24-10. Definitions of VM-Exit Controls
222  */
223 #define         VMEXIT_SAVE_DEBUG_CONTROLS              2
224 #define         VMEXIT_HOST_ADDRESS_SPACE_SIZE          9
225 #define         VMEXIT_LOAD_IA32_PERF_GLOBAL_CTRL       12
226 #define         VMEXIT_ACKNOWLEDGE_INTERRUPT_ON_EXIT    15
227 #define         VMEXIT_SAVE_IA32_PAT                    18
228 #define         VMEXIT_LOAD_IA32_PAT                    19
229 #define         VMEXIT_SAVE_IA32_EFER                   20
230 #define         VMEXIT_LOAD_IA32_EFER                   21
231 #define         VMEXIT_SAVE_VMX_PREEMPTION_TIMER        22
232
233
234
235 /*
236  * VM-ENTRY CONTROL FIELDS
237  * Table 24-12. Definitions of VM-Entry Controls
238  */
239 #define         VMENTRY_LOAD_DEBUG_CONTROLS             2
240 #define         VMENTRY_IA32e_MODE_GUEST                9
241 #define         VMENTRY_ENTRY_TO_SMM                    10
242 #define         VMENTRY_DEACTIVATE_DUAL_MONITOR         11
243 #define         VMENTRY_LOAD_IA32_PERF_GLOBAL_CTRL      13
244 #define         VMENTRY_LOAD_IA32_PAT                   14
245 #define         VMENTRY_LOAD_IA32_EFER                  15
246
247
248
249 #define IS_ONE_SETTING_ALLOWED(val, bit)        \
250     ((val) & (1ULL << (bit + 32)))
251
252 #define IS_ZERO_SETTING_ALLOWED(val, bit)       \
253     (((val) & (1ULL << (bit))) == 0)
254
255
256
257 /*
258  * VMX Basic Exit Reasons
259  */
260 #define         EXIT_REASON_EXCEPTION           0
261 #define         EXIT_REASON_EXT_INTR            1
262 #define         EXIT_REASON_TRIPLE_FAULT        2
263 #define         EXIT_REASON_INIT                3
264 #define         EXIT_REASON_SIPI                4
265 #define         EXIT_REASON_IO_SMI              5
266 #define         EXIT_REASON_SMI                 6
267 #define         EXIT_REASON_INTR_WINDOW         7
268 #define         EXIT_REASON_NMI_WINDOW          8
269 #define         EXIT_REASON_TASK_SWITCH         9
270 #define         EXIT_REASON_CPUID               10
271 #define         EXIT_REASON_GETSEC              11
272 #define         EXIT_REASON_HLT                 12
273 #define         EXIT_REASON_INVD                13
274 #define         EXIT_REASON_INVLPG              14
275 #define         EXIT_REASON_RDPMC               15
276 #define         EXIT_REASON_RDTSC               16
277 #define         EXIT_REASON_RSM                 17
278 #define         EXIT_REASON_VMCALL              18
279 #define         EXIT_REASON_VMCLEAR             19
280 #define         EXIT_REASON_VMLAUNCH            20
281 #define         EXIT_REASON_VMPTRLD             21
282 #define         EXIT_REASON_VMPTRST             22
283 #define         EXIT_REASON_VMREAD              23
284 #define         EXIT_REASON_VMRESUME            24
285 #define         EXIT_REASON_VMWRITE             25
286 #define         EXIT_REASON_VMXOFF              26
287 #define         EXIT_REASON_VMXON               27
288 #define         EXIT_REASON_CR_ACCESS           28
289 #define         EXIT_REASON_DR_ACCESS           29
290 #define         EXIT_REASON_INOUT               30
291 #define         EXIT_REASON_RDMSR               31
292 #define         EXIT_REASON_WRMSR               32
293 #define         EXIT_REASON_INVAL_VMCS          33
294 #define         EXIT_REASON_INVAL_MSR           34
295 #define         EXIT_REASON_MWAIT               36
296 #define         EXIT_REASON_MTF                 37
297 #define         EXIT_REASON_MONITOR             39
298 #define         EXIT_REASON_PAUSE               40
299 #define         EXIT_REASON_MCE                 41
300 #define         EXIT_REASON_TPR                 43
301 #define         EXIT_REASON_APIC                44
302 #define         EXIT_REASON_GDTR_IDTR           46
303 #define         EXIT_REASON_LDTR_TR             47
304 #define         EXIT_REASON_EPT_FAULT           48
305 #define         EXIT_REASON_EPT_MISCONFIG       49
306 #define         EXIT_REASON_INVEPT              50
307 #define         EXIT_REASON_RDTSCP              51
308 #define         EXIT_REASON_VMX_PREEMPT         52
309 #define         EXIT_REASON_INVVPID             53
310 #define         EXIT_REASON_WBINVD              54
311 #define         EXIT_REASON_XSETBV              55
312 #define         EXIT_REASON_APIC_WRITE          56
313 #define         EXIT_REASON_RDRAND              57
314 #define         EXIT_REASON_INVPCID             58
315 #define         EXIT_REASON_VMFUNC              59
316
317 /* Table 24-2. Format of Access Rights */
318 #define         VMCS_SEG_TYPE(x)        (x)
319 #define         VMCS_S                  (1 << 4)        /* Descriptor type - 0 = system; 1 = code or data */
320 #define         VMCS_DPL(x)             (x << 5)        /* Descriptor Privilege Level */
321 #define         VMCS_P                  (1 << 7)        /* Segment present*/
322 #define         VMCS_AVL                (1 << 12)       /* Available for use by system software */
323 #define         VMCS_L                  (1 << 13)       /* 64-bit mode active (for CS only) */
324 #define         VMCS_OP_SIZE            (1 << 14)       /* Default operation size (0 = 16-bit segment; 1 = 32-bit segment) */
325 #define         VMCS_G                  (1 << 15)       /* Granularity */
326 #define         VMCS_SEG_UNUSABLE       (1 << 16)       /* Segment unusable (0 = usable; 1 = unusable) */
327
328 #define VMCS_EXCEPTION_TYPE(x)                  ((x >> 8) & 0x7)
329 #define VMCS_EXCEPTION_EXTERNAL_INTERRUPT       0
330 #define VMCS_EXCEPTION_NMI                      2
331 #define VMCS_EXCEPTION_HARDWARE                 3
332 #define VMCS_EXCEPTION_SOFTWARE                 6
333
334 #define VMCS_EXCEPTION_NUMBER(x)                (x & 0xFF)
335 #endif