xref: /freebsd/sys/amd64/vmm/intel/vmcs.h (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _VMCS_H_
30 #define	_VMCS_H_
31 
32 #ifdef _KERNEL
33 struct vmcs {
34 	uint32_t	identifier;
35 	uint32_t	abort_code;
36 	char		_impl_specific[PAGE_SIZE - sizeof(uint32_t) * 2];
37 };
38 CTASSERT(sizeof(struct vmcs) == PAGE_SIZE);
39 
40 /* MSR save region is composed of an array of 'struct msr_entry' */
41 struct msr_entry {
42 	uint32_t	index;
43 	uint32_t	reserved;
44 	uint64_t	val;
45 
46 };
47 
48 int vmcs_set_msr_save(struct vmcs *vmcs, u_long g_area, u_int g_count);
49 int	vmcs_set_defaults(struct vmcs *vmcs, u_long host_rip, u_long host_rsp,
50 			  uint64_t eptp,
51 			  uint32_t pinbased_ctls, uint32_t procbased_ctls,
52 			  uint32_t procbased_ctls2, uint32_t exit_ctls,
53 			  uint32_t entry_ctls, u_long msr_bitmap,
54 			  uint16_t vpid);
55 int	vmcs_getreg(struct vmcs *vmcs, int running, int ident, uint64_t *rv);
56 int	vmcs_setreg(struct vmcs *vmcs, int running, int ident, uint64_t val);
57 int	vmcs_getdesc(struct vmcs *vmcs, int ident,
58 		     struct seg_desc *desc);
59 int	vmcs_setdesc(struct vmcs *vmcs, int ident,
60 		     struct seg_desc *desc);
61 
62 static __inline uint64_t
63 vmcs_read(uint32_t encoding)
64 {
65 	int error;
66 	uint64_t val;
67 
68 	error = vmread(encoding, &val);
69 	KASSERT(error == 0, ("vmcs_read(%u) error %d", encoding, error));
70 	return (val);
71 }
72 
73 static __inline void
74 vmcs_write(uint32_t encoding, uint64_t val)
75 {
76 	int error;
77 
78 	error = vmwrite(encoding, val);
79 	KASSERT(error == 0, ("vmcs_write(%u) error %d", encoding, error));
80 }
81 
82 #define	vmexit_instruction_length()	vmcs_read(VMCS_EXIT_INSTRUCTION_LENGTH)
83 #define	vmcs_guest_rip()		vmcs_read(VMCS_GUEST_RIP)
84 #define	vmcs_instruction_error()	vmcs_read(VMCS_INSTRUCTION_ERROR)
85 #define	vmcs_exit_reason()		(vmcs_read(VMCS_EXIT_REASON) & 0xffff)
86 #define	vmcs_exit_qualification()	vmcs_read(VMCS_EXIT_QUALIFICATION)
87 #define	vmcs_guest_cr3()		vmcs_read(VMCS_GUEST_CR3)
88 #define	vmcs_gpa()			vmcs_read(VMCS_GUEST_PHYSICAL_ADDRESS)
89 #define	vmcs_gla()			vmcs_read(VMCS_GUEST_LINEAR_ADDRESS)
90 #define	vmcs_idt_vectoring_info()	vmcs_read(VMCS_IDT_VECTORING_INFO)
91 #define	vmcs_idt_vectoring_err()	vmcs_read(VMCS_IDT_VECTORING_ERROR)
92 
93 #endif	/* _KERNEL */
94 
95 #define	VMCS_INITIAL			0xffffffffffffffff
96 
97 #define	VMCS_IDENT(encoding)		((encoding) | 0x80000000)
98 /*
99  * VMCS field encodings from Appendix H, Intel Architecture Manual Vol3B.
100  */
101 #define	VMCS_INVALID_ENCODING		0xffffffff
102 
103 /* 16-bit control fields */
104 #define	VMCS_VPID			0x00000000
105 
106 /* 16-bit guest-state fields */
107 #define	VMCS_GUEST_ES_SELECTOR		0x00000800
108 #define	VMCS_GUEST_CS_SELECTOR		0x00000802
109 #define	VMCS_GUEST_SS_SELECTOR		0x00000804
110 #define	VMCS_GUEST_DS_SELECTOR		0x00000806
111 #define	VMCS_GUEST_FS_SELECTOR		0x00000808
112 #define	VMCS_GUEST_GS_SELECTOR		0x0000080A
113 #define	VMCS_GUEST_LDTR_SELECTOR	0x0000080C
114 #define	VMCS_GUEST_TR_SELECTOR		0x0000080E
115 
116 /* 16-bit host-state fields */
117 #define	VMCS_HOST_ES_SELECTOR		0x00000C00
118 #define	VMCS_HOST_CS_SELECTOR		0x00000C02
119 #define	VMCS_HOST_SS_SELECTOR		0x00000C04
120 #define	VMCS_HOST_DS_SELECTOR		0x00000C06
121 #define	VMCS_HOST_FS_SELECTOR		0x00000C08
122 #define	VMCS_HOST_GS_SELECTOR		0x00000C0A
123 #define	VMCS_HOST_TR_SELECTOR		0x00000C0C
124 
125 /* 64-bit control fields */
126 #define	VMCS_IO_BITMAP_A		0x00002000
127 #define	VMCS_IO_BITMAP_B		0x00002002
128 #define	VMCS_MSR_BITMAP			0x00002004
129 #define	VMCS_EXIT_MSR_STORE		0x00002006
130 #define	VMCS_EXIT_MSR_LOAD		0x00002008
131 #define	VMCS_ENTRY_MSR_LOAD		0x0000200A
132 #define	VMCS_EXECUTIVE_VMCS		0x0000200C
133 #define	VMCS_TSC_OFFSET			0x00002010
134 #define	VMCS_VIRTUAL_APIC		0x00002012
135 #define	VMCS_APIC_ACCESS		0x00002014
136 #define	VMCS_EPTP			0x0000201A
137 
138 /* 64-bit read-only fields */
139 #define	VMCS_GUEST_PHYSICAL_ADDRESS	0x00002400
140 
141 /* 64-bit guest-state fields */
142 #define	VMCS_LINK_POINTER		0x00002800
143 #define	VMCS_GUEST_IA32_DEBUGCTL	0x00002802
144 #define	VMCS_GUEST_IA32_PAT		0x00002804
145 #define	VMCS_GUEST_IA32_EFER		0x00002806
146 #define	VMCS_GUEST_IA32_PERF_GLOBAL_CTRL 0x00002808
147 #define	VMCS_GUEST_PDPTE0		0x0000280A
148 #define	VMCS_GUEST_PDPTE1		0x0000280C
149 #define	VMCS_GUEST_PDPTE2		0x0000280E
150 #define	VMCS_GUEST_PDPTE3		0x00002810
151 
152 /* 64-bit host-state fields */
153 #define	VMCS_HOST_IA32_PAT		0x00002C00
154 #define	VMCS_HOST_IA32_EFER		0x00002C02
155 #define	VMCS_HOST_IA32_PERF_GLOBAL_CTRL	0x00002C04
156 
157 /* 32-bit control fields */
158 #define	VMCS_PIN_BASED_CTLS		0x00004000
159 #define	VMCS_PRI_PROC_BASED_CTLS	0x00004002
160 #define	VMCS_EXCEPTION_BITMAP		0x00004004
161 #define	VMCS_PF_ERROR_MASK		0x00004006
162 #define	VMCS_PF_ERROR_MATCH		0x00004008
163 #define	VMCS_CR3_TARGET_COUNT		0x0000400A
164 #define	VMCS_EXIT_CTLS			0x0000400C
165 #define	VMCS_EXIT_MSR_STORE_COUNT	0x0000400E
166 #define	VMCS_EXIT_MSR_LOAD_COUNT	0x00004010
167 #define	VMCS_ENTRY_CTLS			0x00004012
168 #define	VMCS_ENTRY_MSR_LOAD_COUNT	0x00004014
169 #define	VMCS_ENTRY_INTR_INFO		0x00004016
170 #define	VMCS_ENTRY_EXCEPTION_ERROR	0x00004018
171 #define	VMCS_ENTRY_INST_LENGTH		0x0000401A
172 #define	VMCS_TPR_THRESHOLD		0x0000401C
173 #define	VMCS_SEC_PROC_BASED_CTLS	0x0000401E
174 #define	VMCS_PLE_GAP			0x00004020
175 #define	VMCS_PLE_WINDOW			0x00004022
176 
177 /* 32-bit read-only data fields */
178 #define	VMCS_INSTRUCTION_ERROR		0x00004400
179 #define	VMCS_EXIT_REASON		0x00004402
180 #define	VMCS_EXIT_INTERRUPTION_INFO	0x00004404
181 #define	VMCS_EXIT_INTERRUPTION_ERROR	0x00004406
182 #define	VMCS_IDT_VECTORING_INFO		0x00004408
183 #define	VMCS_IDT_VECTORING_ERROR	0x0000440A
184 #define	VMCS_EXIT_INSTRUCTION_LENGTH	0x0000440C
185 #define	VMCS_EXIT_INSTRUCTION_INFO	0x0000440E
186 
187 /* 32-bit guest-state fields */
188 #define	VMCS_GUEST_ES_LIMIT		0x00004800
189 #define	VMCS_GUEST_CS_LIMIT		0x00004802
190 #define	VMCS_GUEST_SS_LIMIT		0x00004804
191 #define	VMCS_GUEST_DS_LIMIT		0x00004806
192 #define	VMCS_GUEST_FS_LIMIT		0x00004808
193 #define	VMCS_GUEST_GS_LIMIT		0x0000480A
194 #define	VMCS_GUEST_LDTR_LIMIT		0x0000480C
195 #define	VMCS_GUEST_TR_LIMIT		0x0000480E
196 #define	VMCS_GUEST_GDTR_LIMIT		0x00004810
197 #define	VMCS_GUEST_IDTR_LIMIT		0x00004812
198 #define	VMCS_GUEST_ES_ACCESS_RIGHTS	0x00004814
199 #define	VMCS_GUEST_CS_ACCESS_RIGHTS	0x00004816
200 #define	VMCS_GUEST_SS_ACCESS_RIGHTS	0x00004818
201 #define	VMCS_GUEST_DS_ACCESS_RIGHTS	0x0000481A
202 #define	VMCS_GUEST_FS_ACCESS_RIGHTS	0x0000481C
203 #define	VMCS_GUEST_GS_ACCESS_RIGHTS	0x0000481E
204 #define	VMCS_GUEST_LDTR_ACCESS_RIGHTS	0x00004820
205 #define	VMCS_GUEST_TR_ACCESS_RIGHTS	0x00004822
206 #define	VMCS_GUEST_INTERRUPTIBILITY	0x00004824
207 #define	VMCS_GUEST_ACTIVITY		0x00004826
208 #define VMCS_GUEST_SMBASE		0x00004828
209 #define	VMCS_GUEST_IA32_SYSENTER_CS	0x0000482A
210 #define	VMCS_PREEMPTION_TIMER_VALUE	0x0000482E
211 
212 /* 32-bit host state fields */
213 #define	VMCS_HOST_IA32_SYSENTER_CS	0x00004C00
214 
215 /* Natural Width control fields */
216 #define	VMCS_CR0_MASK			0x00006000
217 #define	VMCS_CR4_MASK			0x00006002
218 #define	VMCS_CR0_SHADOW			0x00006004
219 #define	VMCS_CR4_SHADOW			0x00006006
220 #define	VMCS_CR3_TARGET0		0x00006008
221 #define	VMCS_CR3_TARGET1		0x0000600A
222 #define	VMCS_CR3_TARGET2		0x0000600C
223 #define	VMCS_CR3_TARGET3		0x0000600E
224 
225 /* Natural Width read-only fields */
226 #define	VMCS_EXIT_QUALIFICATION		0x00006400
227 #define	VMCS_IO_RCX			0x00006402
228 #define	VMCS_IO_RSI			0x00006404
229 #define	VMCS_IO_RDI			0x00006406
230 #define	VMCS_IO_RIP			0x00006408
231 #define	VMCS_GUEST_LINEAR_ADDRESS	0x0000640A
232 
233 /* Natural Width guest-state fields */
234 #define	VMCS_GUEST_CR0			0x00006800
235 #define	VMCS_GUEST_CR3			0x00006802
236 #define	VMCS_GUEST_CR4			0x00006804
237 #define	VMCS_GUEST_ES_BASE		0x00006806
238 #define	VMCS_GUEST_CS_BASE		0x00006808
239 #define	VMCS_GUEST_SS_BASE		0x0000680A
240 #define	VMCS_GUEST_DS_BASE		0x0000680C
241 #define	VMCS_GUEST_FS_BASE		0x0000680E
242 #define	VMCS_GUEST_GS_BASE		0x00006810
243 #define	VMCS_GUEST_LDTR_BASE		0x00006812
244 #define	VMCS_GUEST_TR_BASE		0x00006814
245 #define	VMCS_GUEST_GDTR_BASE		0x00006816
246 #define	VMCS_GUEST_IDTR_BASE		0x00006818
247 #define	VMCS_GUEST_DR7			0x0000681A
248 #define	VMCS_GUEST_RSP			0x0000681C
249 #define	VMCS_GUEST_RIP			0x0000681E
250 #define	VMCS_GUEST_RFLAGS		0x00006820
251 #define	VMCS_GUEST_PENDING_DBG_EXCEPTIONS 0x00006822
252 #define	VMCS_GUEST_IA32_SYSENTER_ESP	0x00006824
253 #define	VMCS_GUEST_IA32_SYSENTER_EIP	0x00006826
254 
255 /* Natural Width host-state fields */
256 #define	VMCS_HOST_CR0			0x00006C00
257 #define	VMCS_HOST_CR3			0x00006C02
258 #define	VMCS_HOST_CR4			0x00006C04
259 #define	VMCS_HOST_FS_BASE		0x00006C06
260 #define	VMCS_HOST_GS_BASE		0x00006C08
261 #define	VMCS_HOST_TR_BASE		0x00006C0A
262 #define	VMCS_HOST_GDTR_BASE		0x00006C0C
263 #define	VMCS_HOST_IDTR_BASE		0x00006C0E
264 #define	VMCS_HOST_IA32_SYSENTER_ESP	0x00006C10
265 #define	VMCS_HOST_IA32_SYSENTER_EIP	0x00006C12
266 #define	VMCS_HOST_RSP			0x00006C14
267 #define	VMCS_HOST_RIP			0x00006c16
268 
269 /*
270  * VM instruction error numbers
271  */
272 #define	VMRESUME_WITH_NON_LAUNCHED_VMCS	5
273 
274 /*
275  * VMCS exit reasons
276  */
277 #define EXIT_REASON_EXCEPTION		0
278 #define EXIT_REASON_EXT_INTR		1
279 #define EXIT_REASON_TRIPLE_FAULT	2
280 #define EXIT_REASON_INIT		3
281 #define EXIT_REASON_SIPI		4
282 #define EXIT_REASON_IO_SMI		5
283 #define EXIT_REASON_SMI			6
284 #define EXIT_REASON_INTR_WINDOW		7
285 #define EXIT_REASON_NMI_WINDOW		8
286 #define EXIT_REASON_TASK_SWITCH		9
287 #define EXIT_REASON_CPUID		10
288 #define EXIT_REASON_GETSEC		11
289 #define EXIT_REASON_HLT			12
290 #define EXIT_REASON_INVD		13
291 #define EXIT_REASON_INVLPG		14
292 #define EXIT_REASON_RDPMC		15
293 #define EXIT_REASON_RDTSC		16
294 #define EXIT_REASON_RSM			17
295 #define EXIT_REASON_VMCALL		18
296 #define EXIT_REASON_VMCLEAR		19
297 #define EXIT_REASON_VMLAUNCH		20
298 #define EXIT_REASON_VMPTRLD		21
299 #define EXIT_REASON_VMPTRST		22
300 #define EXIT_REASON_VMREAD		23
301 #define EXIT_REASON_VMRESUME		24
302 #define EXIT_REASON_VMWRITE		25
303 #define EXIT_REASON_VMXOFF		26
304 #define EXIT_REASON_VMXON		27
305 #define EXIT_REASON_CR_ACCESS		28
306 #define EXIT_REASON_DR_ACCESS		29
307 #define EXIT_REASON_INOUT		30
308 #define EXIT_REASON_RDMSR		31
309 #define EXIT_REASON_WRMSR		32
310 #define EXIT_REASON_INVAL_VMCS		33
311 #define EXIT_REASON_INVAL_MSR		34
312 #define EXIT_REASON_MWAIT		36
313 #define EXIT_REASON_MTF			37
314 #define EXIT_REASON_MONITOR		39
315 #define EXIT_REASON_PAUSE		40
316 #define EXIT_REASON_MCE			41
317 #define EXIT_REASON_TPR			43
318 #define EXIT_REASON_APIC		44
319 #define EXIT_REASON_GDTR_IDTR		46
320 #define EXIT_REASON_LDTR_TR		47
321 #define EXIT_REASON_EPT_FAULT		48
322 #define EXIT_REASON_EPT_MISCONFIG	49
323 #define EXIT_REASON_INVEPT		50
324 #define EXIT_REASON_RDTSCP		51
325 #define EXIT_REASON_VMX_PREEMPT		52
326 #define EXIT_REASON_INVVPID		53
327 #define EXIT_REASON_WBINVD		54
328 #define EXIT_REASON_XSETBV		55
329 
330 /*
331  * VMCS interrupt information fields
332  */
333 #define	VMCS_INTERRUPTION_INFO_VALID	(1U << 31)
334 #define	VMCS_INTERRUPTION_INFO_HW_INTR	(0 << 8)
335 #define	VMCS_INTERRUPTION_INFO_NMI	(2 << 8)
336 
337 /*
338  * VMCS IDT-Vectoring information fields
339  */
340 #define	VMCS_IDT_VEC_VALID		(1U << 31)
341 #define	VMCS_IDT_VEC_ERRCODE_VALID	(1 << 11)
342 
343 /*
344  * VMCS Guest interruptibility field
345  */
346 #define	VMCS_INTERRUPTIBILITY_STI_BLOCKING	(1 << 0)
347 #define	VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING	(1 << 1)
348 #define	VMCS_INTERRUPTIBILITY_SMI_BLOCKING	(1 << 2)
349 #define	VMCS_INTERRUPTIBILITY_NMI_BLOCKING	(1 << 3)
350 
351 /*
352  * Exit qualification for EXIT_REASON_INVAL_VMCS
353  */
354 #define	EXIT_QUAL_NMI_WHILE_STI_BLOCKING	3
355 
356 /*
357  * Exit qualification for EPT violation
358  */
359 #define	EPT_VIOLATION_DATA_READ		(1UL << 0)
360 #define	EPT_VIOLATION_DATA_WRITE	(1UL << 1)
361 #define	EPT_VIOLATION_INST_FETCH	(1UL << 2)
362 #define	EPT_VIOLATION_GPA_READABLE	(1UL << 3)
363 #define	EPT_VIOLATION_GPA_WRITEABLE	(1UL << 4)
364 #define	EPT_VIOLATION_GPA_EXECUTABLE	(1UL << 5)
365 #define	EPT_VIOLATION_GLA_VALID		(1UL << 7)
366 #define	EPT_VIOLATION_XLAT_VALID	(1UL << 8)
367 
368 #endif
369