1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * tools/testing/selftests/kvm/include/x86_64/evmcs.h
4  *
5  * Copyright (C) 2018, Red Hat, Inc.
6  *
7  */
8 
9 #ifndef SELFTEST_KVM_EVMCS_H
10 #define SELFTEST_KVM_EVMCS_H
11 
12 #include <stdint.h>
13 #include "vmx.h"
14 
15 #define u16 uint16_t
16 #define u32 uint32_t
17 #define u64 uint64_t
18 
19 #define EVMCS_VERSION 1
20 
21 extern bool enable_evmcs;
22 
23 struct hv_vp_assist_page {
24 	__u32 apic_assist;
25 	__u32 reserved;
26 	__u64 vtl_control[2];
27 	__u64 nested_enlightenments_control[2];
28 	__u32 enlighten_vmentry;
29 	__u64 current_nested_vmcs;
30 };
31 
32 struct hv_enlightened_vmcs {
33 	u32 revision_id;
34 	u32 abort;
35 
36 	u16 host_es_selector;
37 	u16 host_cs_selector;
38 	u16 host_ss_selector;
39 	u16 host_ds_selector;
40 	u16 host_fs_selector;
41 	u16 host_gs_selector;
42 	u16 host_tr_selector;
43 
44 	u64 host_ia32_pat;
45 	u64 host_ia32_efer;
46 
47 	u64 host_cr0;
48 	u64 host_cr3;
49 	u64 host_cr4;
50 
51 	u64 host_ia32_sysenter_esp;
52 	u64 host_ia32_sysenter_eip;
53 	u64 host_rip;
54 	u32 host_ia32_sysenter_cs;
55 
56 	u32 pin_based_vm_exec_control;
57 	u32 vm_exit_controls;
58 	u32 secondary_vm_exec_control;
59 
60 	u64 io_bitmap_a;
61 	u64 io_bitmap_b;
62 	u64 msr_bitmap;
63 
64 	u16 guest_es_selector;
65 	u16 guest_cs_selector;
66 	u16 guest_ss_selector;
67 	u16 guest_ds_selector;
68 	u16 guest_fs_selector;
69 	u16 guest_gs_selector;
70 	u16 guest_ldtr_selector;
71 	u16 guest_tr_selector;
72 
73 	u32 guest_es_limit;
74 	u32 guest_cs_limit;
75 	u32 guest_ss_limit;
76 	u32 guest_ds_limit;
77 	u32 guest_fs_limit;
78 	u32 guest_gs_limit;
79 	u32 guest_ldtr_limit;
80 	u32 guest_tr_limit;
81 	u32 guest_gdtr_limit;
82 	u32 guest_idtr_limit;
83 
84 	u32 guest_es_ar_bytes;
85 	u32 guest_cs_ar_bytes;
86 	u32 guest_ss_ar_bytes;
87 	u32 guest_ds_ar_bytes;
88 	u32 guest_fs_ar_bytes;
89 	u32 guest_gs_ar_bytes;
90 	u32 guest_ldtr_ar_bytes;
91 	u32 guest_tr_ar_bytes;
92 
93 	u64 guest_es_base;
94 	u64 guest_cs_base;
95 	u64 guest_ss_base;
96 	u64 guest_ds_base;
97 	u64 guest_fs_base;
98 	u64 guest_gs_base;
99 	u64 guest_ldtr_base;
100 	u64 guest_tr_base;
101 	u64 guest_gdtr_base;
102 	u64 guest_idtr_base;
103 
104 	u64 padding64_1[3];
105 
106 	u64 vm_exit_msr_store_addr;
107 	u64 vm_exit_msr_load_addr;
108 	u64 vm_entry_msr_load_addr;
109 
110 	u64 cr3_target_value0;
111 	u64 cr3_target_value1;
112 	u64 cr3_target_value2;
113 	u64 cr3_target_value3;
114 
115 	u32 page_fault_error_code_mask;
116 	u32 page_fault_error_code_match;
117 
118 	u32 cr3_target_count;
119 	u32 vm_exit_msr_store_count;
120 	u32 vm_exit_msr_load_count;
121 	u32 vm_entry_msr_load_count;
122 
123 	u64 tsc_offset;
124 	u64 virtual_apic_page_addr;
125 	u64 vmcs_link_pointer;
126 
127 	u64 guest_ia32_debugctl;
128 	u64 guest_ia32_pat;
129 	u64 guest_ia32_efer;
130 
131 	u64 guest_pdptr0;
132 	u64 guest_pdptr1;
133 	u64 guest_pdptr2;
134 	u64 guest_pdptr3;
135 
136 	u64 guest_pending_dbg_exceptions;
137 	u64 guest_sysenter_esp;
138 	u64 guest_sysenter_eip;
139 
140 	u32 guest_activity_state;
141 	u32 guest_sysenter_cs;
142 
143 	u64 cr0_guest_host_mask;
144 	u64 cr4_guest_host_mask;
145 	u64 cr0_read_shadow;
146 	u64 cr4_read_shadow;
147 	u64 guest_cr0;
148 	u64 guest_cr3;
149 	u64 guest_cr4;
150 	u64 guest_dr7;
151 
152 	u64 host_fs_base;
153 	u64 host_gs_base;
154 	u64 host_tr_base;
155 	u64 host_gdtr_base;
156 	u64 host_idtr_base;
157 	u64 host_rsp;
158 
159 	u64 ept_pointer;
160 
161 	u16 virtual_processor_id;
162 	u16 padding16[3];
163 
164 	u64 padding64_2[5];
165 	u64 guest_physical_address;
166 
167 	u32 vm_instruction_error;
168 	u32 vm_exit_reason;
169 	u32 vm_exit_intr_info;
170 	u32 vm_exit_intr_error_code;
171 	u32 idt_vectoring_info_field;
172 	u32 idt_vectoring_error_code;
173 	u32 vm_exit_instruction_len;
174 	u32 vmx_instruction_info;
175 
176 	u64 exit_qualification;
177 	u64 exit_io_instruction_ecx;
178 	u64 exit_io_instruction_esi;
179 	u64 exit_io_instruction_edi;
180 	u64 exit_io_instruction_eip;
181 
182 	u64 guest_linear_address;
183 	u64 guest_rsp;
184 	u64 guest_rflags;
185 
186 	u32 guest_interruptibility_info;
187 	u32 cpu_based_vm_exec_control;
188 	u32 exception_bitmap;
189 	u32 vm_entry_controls;
190 	u32 vm_entry_intr_info_field;
191 	u32 vm_entry_exception_error_code;
192 	u32 vm_entry_instruction_len;
193 	u32 tpr_threshold;
194 
195 	u64 guest_rip;
196 
197 	u32 hv_clean_fields;
198 	u32 hv_padding_32;
199 	u32 hv_synthetic_controls;
200 	struct {
201 		u32 nested_flush_hypercall:1;
202 		u32 msr_bitmap:1;
203 		u32 reserved:30;
204 	} hv_enlightenments_control;
205 	u32 hv_vp_id;
206 	u32 padding32_2;
207 	u64 hv_vm_id;
208 	u64 partition_assist_page;
209 	u64 padding64_4[4];
210 	u64 guest_bndcfgs;
211 	u64 guest_ia32_perf_global_ctrl;
212 	u64 guest_ia32_s_cet;
213 	u64 guest_ssp;
214 	u64 guest_ia32_int_ssp_table_addr;
215 	u64 guest_ia32_lbr_ctl;
216 	u64 padding64_5[2];
217 	u64 xss_exit_bitmap;
218 	u64 encls_exiting_bitmap;
219 	u64 host_ia32_perf_global_ctrl;
220 	u64 tsc_multiplier;
221 	u64 host_ia32_s_cet;
222 	u64 host_ssp;
223 	u64 host_ia32_int_ssp_table_addr;
224 	u64 padding64_6;
225 };
226 
227 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE                     0
228 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP                BIT(0)
229 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP               BIT(1)
230 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2             BIT(2)
231 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1             BIT(3)
232 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC             BIT(4)
233 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT            BIT(5)
234 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY            BIT(6)
235 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN            BIT(7)
236 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR                     BIT(8)
237 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT             BIT(9)
238 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC              BIT(10)
239 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1               BIT(11)
240 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2               BIT(12)
241 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER             BIT(13)
242 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1                BIT(14)
243 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_ENLIGHTENMENTSCONTROL    BIT(15)
244 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL                      0xFFFF
245 
246 #define HV_X64_MSR_VP_ASSIST_PAGE		0x40000073
247 #define HV_X64_MSR_VP_ASSIST_PAGE_ENABLE	0x00000001
248 #define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT	12
249 #define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK	\
250 		(~((1ull << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) - 1))
251 
252 extern struct hv_enlightened_vmcs *current_evmcs;
253 extern struct hv_vp_assist_page *current_vp_assist;
254 
255 int vcpu_enable_evmcs(struct kvm_vcpu *vcpu);
256 
enable_vp_assist(uint64_t vp_assist_pa,void * vp_assist)257 static inline int enable_vp_assist(uint64_t vp_assist_pa, void *vp_assist)
258 {
259 	u64 val = (vp_assist_pa & HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK) |
260 		HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
261 
262 	wrmsr(HV_X64_MSR_VP_ASSIST_PAGE, val);
263 
264 	current_vp_assist = vp_assist;
265 
266 	enable_evmcs = true;
267 
268 	return 0;
269 }
270 
evmcs_vmptrld(uint64_t vmcs_pa,void * vmcs)271 static inline int evmcs_vmptrld(uint64_t vmcs_pa, void *vmcs)
272 {
273 	current_vp_assist->current_nested_vmcs = vmcs_pa;
274 	current_vp_assist->enlighten_vmentry = 1;
275 
276 	current_evmcs = vmcs;
277 
278 	return 0;
279 }
280 
evmcs_vmptrst(uint64_t * value)281 static inline int evmcs_vmptrst(uint64_t *value)
282 {
283 	*value = current_vp_assist->current_nested_vmcs &
284 		~HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
285 
286 	return 0;
287 }
288 
evmcs_vmread(uint64_t encoding,uint64_t * value)289 static inline int evmcs_vmread(uint64_t encoding, uint64_t *value)
290 {
291 	switch (encoding) {
292 	case GUEST_RIP:
293 		*value = current_evmcs->guest_rip;
294 		break;
295 	case GUEST_RSP:
296 		*value = current_evmcs->guest_rsp;
297 		break;
298 	case GUEST_RFLAGS:
299 		*value = current_evmcs->guest_rflags;
300 		break;
301 	case HOST_IA32_PAT:
302 		*value = current_evmcs->host_ia32_pat;
303 		break;
304 	case HOST_IA32_EFER:
305 		*value = current_evmcs->host_ia32_efer;
306 		break;
307 	case HOST_CR0:
308 		*value = current_evmcs->host_cr0;
309 		break;
310 	case HOST_CR3:
311 		*value = current_evmcs->host_cr3;
312 		break;
313 	case HOST_CR4:
314 		*value = current_evmcs->host_cr4;
315 		break;
316 	case HOST_IA32_SYSENTER_ESP:
317 		*value = current_evmcs->host_ia32_sysenter_esp;
318 		break;
319 	case HOST_IA32_SYSENTER_EIP:
320 		*value = current_evmcs->host_ia32_sysenter_eip;
321 		break;
322 	case HOST_RIP:
323 		*value = current_evmcs->host_rip;
324 		break;
325 	case IO_BITMAP_A:
326 		*value = current_evmcs->io_bitmap_a;
327 		break;
328 	case IO_BITMAP_B:
329 		*value = current_evmcs->io_bitmap_b;
330 		break;
331 	case MSR_BITMAP:
332 		*value = current_evmcs->msr_bitmap;
333 		break;
334 	case GUEST_ES_BASE:
335 		*value = current_evmcs->guest_es_base;
336 		break;
337 	case GUEST_CS_BASE:
338 		*value = current_evmcs->guest_cs_base;
339 		break;
340 	case GUEST_SS_BASE:
341 		*value = current_evmcs->guest_ss_base;
342 		break;
343 	case GUEST_DS_BASE:
344 		*value = current_evmcs->guest_ds_base;
345 		break;
346 	case GUEST_FS_BASE:
347 		*value = current_evmcs->guest_fs_base;
348 		break;
349 	case GUEST_GS_BASE:
350 		*value = current_evmcs->guest_gs_base;
351 		break;
352 	case GUEST_LDTR_BASE:
353 		*value = current_evmcs->guest_ldtr_base;
354 		break;
355 	case GUEST_TR_BASE:
356 		*value = current_evmcs->guest_tr_base;
357 		break;
358 	case GUEST_GDTR_BASE:
359 		*value = current_evmcs->guest_gdtr_base;
360 		break;
361 	case GUEST_IDTR_BASE:
362 		*value = current_evmcs->guest_idtr_base;
363 		break;
364 	case TSC_OFFSET:
365 		*value = current_evmcs->tsc_offset;
366 		break;
367 	case VIRTUAL_APIC_PAGE_ADDR:
368 		*value = current_evmcs->virtual_apic_page_addr;
369 		break;
370 	case VMCS_LINK_POINTER:
371 		*value = current_evmcs->vmcs_link_pointer;
372 		break;
373 	case GUEST_IA32_DEBUGCTL:
374 		*value = current_evmcs->guest_ia32_debugctl;
375 		break;
376 	case GUEST_IA32_PAT:
377 		*value = current_evmcs->guest_ia32_pat;
378 		break;
379 	case GUEST_IA32_EFER:
380 		*value = current_evmcs->guest_ia32_efer;
381 		break;
382 	case GUEST_PDPTR0:
383 		*value = current_evmcs->guest_pdptr0;
384 		break;
385 	case GUEST_PDPTR1:
386 		*value = current_evmcs->guest_pdptr1;
387 		break;
388 	case GUEST_PDPTR2:
389 		*value = current_evmcs->guest_pdptr2;
390 		break;
391 	case GUEST_PDPTR3:
392 		*value = current_evmcs->guest_pdptr3;
393 		break;
394 	case GUEST_PENDING_DBG_EXCEPTIONS:
395 		*value = current_evmcs->guest_pending_dbg_exceptions;
396 		break;
397 	case GUEST_SYSENTER_ESP:
398 		*value = current_evmcs->guest_sysenter_esp;
399 		break;
400 	case GUEST_SYSENTER_EIP:
401 		*value = current_evmcs->guest_sysenter_eip;
402 		break;
403 	case CR0_GUEST_HOST_MASK:
404 		*value = current_evmcs->cr0_guest_host_mask;
405 		break;
406 	case CR4_GUEST_HOST_MASK:
407 		*value = current_evmcs->cr4_guest_host_mask;
408 		break;
409 	case CR0_READ_SHADOW:
410 		*value = current_evmcs->cr0_read_shadow;
411 		break;
412 	case CR4_READ_SHADOW:
413 		*value = current_evmcs->cr4_read_shadow;
414 		break;
415 	case GUEST_CR0:
416 		*value = current_evmcs->guest_cr0;
417 		break;
418 	case GUEST_CR3:
419 		*value = current_evmcs->guest_cr3;
420 		break;
421 	case GUEST_CR4:
422 		*value = current_evmcs->guest_cr4;
423 		break;
424 	case GUEST_DR7:
425 		*value = current_evmcs->guest_dr7;
426 		break;
427 	case HOST_FS_BASE:
428 		*value = current_evmcs->host_fs_base;
429 		break;
430 	case HOST_GS_BASE:
431 		*value = current_evmcs->host_gs_base;
432 		break;
433 	case HOST_TR_BASE:
434 		*value = current_evmcs->host_tr_base;
435 		break;
436 	case HOST_GDTR_BASE:
437 		*value = current_evmcs->host_gdtr_base;
438 		break;
439 	case HOST_IDTR_BASE:
440 		*value = current_evmcs->host_idtr_base;
441 		break;
442 	case HOST_RSP:
443 		*value = current_evmcs->host_rsp;
444 		break;
445 	case EPT_POINTER:
446 		*value = current_evmcs->ept_pointer;
447 		break;
448 	case GUEST_BNDCFGS:
449 		*value = current_evmcs->guest_bndcfgs;
450 		break;
451 	case XSS_EXIT_BITMAP:
452 		*value = current_evmcs->xss_exit_bitmap;
453 		break;
454 	case GUEST_PHYSICAL_ADDRESS:
455 		*value = current_evmcs->guest_physical_address;
456 		break;
457 	case EXIT_QUALIFICATION:
458 		*value = current_evmcs->exit_qualification;
459 		break;
460 	case GUEST_LINEAR_ADDRESS:
461 		*value = current_evmcs->guest_linear_address;
462 		break;
463 	case VM_EXIT_MSR_STORE_ADDR:
464 		*value = current_evmcs->vm_exit_msr_store_addr;
465 		break;
466 	case VM_EXIT_MSR_LOAD_ADDR:
467 		*value = current_evmcs->vm_exit_msr_load_addr;
468 		break;
469 	case VM_ENTRY_MSR_LOAD_ADDR:
470 		*value = current_evmcs->vm_entry_msr_load_addr;
471 		break;
472 	case CR3_TARGET_VALUE0:
473 		*value = current_evmcs->cr3_target_value0;
474 		break;
475 	case CR3_TARGET_VALUE1:
476 		*value = current_evmcs->cr3_target_value1;
477 		break;
478 	case CR3_TARGET_VALUE2:
479 		*value = current_evmcs->cr3_target_value2;
480 		break;
481 	case CR3_TARGET_VALUE3:
482 		*value = current_evmcs->cr3_target_value3;
483 		break;
484 	case TPR_THRESHOLD:
485 		*value = current_evmcs->tpr_threshold;
486 		break;
487 	case GUEST_INTERRUPTIBILITY_INFO:
488 		*value = current_evmcs->guest_interruptibility_info;
489 		break;
490 	case CPU_BASED_VM_EXEC_CONTROL:
491 		*value = current_evmcs->cpu_based_vm_exec_control;
492 		break;
493 	case EXCEPTION_BITMAP:
494 		*value = current_evmcs->exception_bitmap;
495 		break;
496 	case VM_ENTRY_CONTROLS:
497 		*value = current_evmcs->vm_entry_controls;
498 		break;
499 	case VM_ENTRY_INTR_INFO_FIELD:
500 		*value = current_evmcs->vm_entry_intr_info_field;
501 		break;
502 	case VM_ENTRY_EXCEPTION_ERROR_CODE:
503 		*value = current_evmcs->vm_entry_exception_error_code;
504 		break;
505 	case VM_ENTRY_INSTRUCTION_LEN:
506 		*value = current_evmcs->vm_entry_instruction_len;
507 		break;
508 	case HOST_IA32_SYSENTER_CS:
509 		*value = current_evmcs->host_ia32_sysenter_cs;
510 		break;
511 	case PIN_BASED_VM_EXEC_CONTROL:
512 		*value = current_evmcs->pin_based_vm_exec_control;
513 		break;
514 	case VM_EXIT_CONTROLS:
515 		*value = current_evmcs->vm_exit_controls;
516 		break;
517 	case SECONDARY_VM_EXEC_CONTROL:
518 		*value = current_evmcs->secondary_vm_exec_control;
519 		break;
520 	case GUEST_ES_LIMIT:
521 		*value = current_evmcs->guest_es_limit;
522 		break;
523 	case GUEST_CS_LIMIT:
524 		*value = current_evmcs->guest_cs_limit;
525 		break;
526 	case GUEST_SS_LIMIT:
527 		*value = current_evmcs->guest_ss_limit;
528 		break;
529 	case GUEST_DS_LIMIT:
530 		*value = current_evmcs->guest_ds_limit;
531 		break;
532 	case GUEST_FS_LIMIT:
533 		*value = current_evmcs->guest_fs_limit;
534 		break;
535 	case GUEST_GS_LIMIT:
536 		*value = current_evmcs->guest_gs_limit;
537 		break;
538 	case GUEST_LDTR_LIMIT:
539 		*value = current_evmcs->guest_ldtr_limit;
540 		break;
541 	case GUEST_TR_LIMIT:
542 		*value = current_evmcs->guest_tr_limit;
543 		break;
544 	case GUEST_GDTR_LIMIT:
545 		*value = current_evmcs->guest_gdtr_limit;
546 		break;
547 	case GUEST_IDTR_LIMIT:
548 		*value = current_evmcs->guest_idtr_limit;
549 		break;
550 	case GUEST_ES_AR_BYTES:
551 		*value = current_evmcs->guest_es_ar_bytes;
552 		break;
553 	case GUEST_CS_AR_BYTES:
554 		*value = current_evmcs->guest_cs_ar_bytes;
555 		break;
556 	case GUEST_SS_AR_BYTES:
557 		*value = current_evmcs->guest_ss_ar_bytes;
558 		break;
559 	case GUEST_DS_AR_BYTES:
560 		*value = current_evmcs->guest_ds_ar_bytes;
561 		break;
562 	case GUEST_FS_AR_BYTES:
563 		*value = current_evmcs->guest_fs_ar_bytes;
564 		break;
565 	case GUEST_GS_AR_BYTES:
566 		*value = current_evmcs->guest_gs_ar_bytes;
567 		break;
568 	case GUEST_LDTR_AR_BYTES:
569 		*value = current_evmcs->guest_ldtr_ar_bytes;
570 		break;
571 	case GUEST_TR_AR_BYTES:
572 		*value = current_evmcs->guest_tr_ar_bytes;
573 		break;
574 	case GUEST_ACTIVITY_STATE:
575 		*value = current_evmcs->guest_activity_state;
576 		break;
577 	case GUEST_SYSENTER_CS:
578 		*value = current_evmcs->guest_sysenter_cs;
579 		break;
580 	case VM_INSTRUCTION_ERROR:
581 		*value = current_evmcs->vm_instruction_error;
582 		break;
583 	case VM_EXIT_REASON:
584 		*value = current_evmcs->vm_exit_reason;
585 		break;
586 	case VM_EXIT_INTR_INFO:
587 		*value = current_evmcs->vm_exit_intr_info;
588 		break;
589 	case VM_EXIT_INTR_ERROR_CODE:
590 		*value = current_evmcs->vm_exit_intr_error_code;
591 		break;
592 	case IDT_VECTORING_INFO_FIELD:
593 		*value = current_evmcs->idt_vectoring_info_field;
594 		break;
595 	case IDT_VECTORING_ERROR_CODE:
596 		*value = current_evmcs->idt_vectoring_error_code;
597 		break;
598 	case VM_EXIT_INSTRUCTION_LEN:
599 		*value = current_evmcs->vm_exit_instruction_len;
600 		break;
601 	case VMX_INSTRUCTION_INFO:
602 		*value = current_evmcs->vmx_instruction_info;
603 		break;
604 	case PAGE_FAULT_ERROR_CODE_MASK:
605 		*value = current_evmcs->page_fault_error_code_mask;
606 		break;
607 	case PAGE_FAULT_ERROR_CODE_MATCH:
608 		*value = current_evmcs->page_fault_error_code_match;
609 		break;
610 	case CR3_TARGET_COUNT:
611 		*value = current_evmcs->cr3_target_count;
612 		break;
613 	case VM_EXIT_MSR_STORE_COUNT:
614 		*value = current_evmcs->vm_exit_msr_store_count;
615 		break;
616 	case VM_EXIT_MSR_LOAD_COUNT:
617 		*value = current_evmcs->vm_exit_msr_load_count;
618 		break;
619 	case VM_ENTRY_MSR_LOAD_COUNT:
620 		*value = current_evmcs->vm_entry_msr_load_count;
621 		break;
622 	case HOST_ES_SELECTOR:
623 		*value = current_evmcs->host_es_selector;
624 		break;
625 	case HOST_CS_SELECTOR:
626 		*value = current_evmcs->host_cs_selector;
627 		break;
628 	case HOST_SS_SELECTOR:
629 		*value = current_evmcs->host_ss_selector;
630 		break;
631 	case HOST_DS_SELECTOR:
632 		*value = current_evmcs->host_ds_selector;
633 		break;
634 	case HOST_FS_SELECTOR:
635 		*value = current_evmcs->host_fs_selector;
636 		break;
637 	case HOST_GS_SELECTOR:
638 		*value = current_evmcs->host_gs_selector;
639 		break;
640 	case HOST_TR_SELECTOR:
641 		*value = current_evmcs->host_tr_selector;
642 		break;
643 	case GUEST_ES_SELECTOR:
644 		*value = current_evmcs->guest_es_selector;
645 		break;
646 	case GUEST_CS_SELECTOR:
647 		*value = current_evmcs->guest_cs_selector;
648 		break;
649 	case GUEST_SS_SELECTOR:
650 		*value = current_evmcs->guest_ss_selector;
651 		break;
652 	case GUEST_DS_SELECTOR:
653 		*value = current_evmcs->guest_ds_selector;
654 		break;
655 	case GUEST_FS_SELECTOR:
656 		*value = current_evmcs->guest_fs_selector;
657 		break;
658 	case GUEST_GS_SELECTOR:
659 		*value = current_evmcs->guest_gs_selector;
660 		break;
661 	case GUEST_LDTR_SELECTOR:
662 		*value = current_evmcs->guest_ldtr_selector;
663 		break;
664 	case GUEST_TR_SELECTOR:
665 		*value = current_evmcs->guest_tr_selector;
666 		break;
667 	case VIRTUAL_PROCESSOR_ID:
668 		*value = current_evmcs->virtual_processor_id;
669 		break;
670 	case HOST_IA32_PERF_GLOBAL_CTRL:
671 		*value = current_evmcs->host_ia32_perf_global_ctrl;
672 		break;
673 	case GUEST_IA32_PERF_GLOBAL_CTRL:
674 		*value = current_evmcs->guest_ia32_perf_global_ctrl;
675 		break;
676 	case ENCLS_EXITING_BITMAP:
677 		*value = current_evmcs->encls_exiting_bitmap;
678 		break;
679 	case TSC_MULTIPLIER:
680 		*value = current_evmcs->tsc_multiplier;
681 		break;
682 	default: return 1;
683 	}
684 
685 	return 0;
686 }
687 
evmcs_vmwrite(uint64_t encoding,uint64_t value)688 static inline int evmcs_vmwrite(uint64_t encoding, uint64_t value)
689 {
690 	switch (encoding) {
691 	case GUEST_RIP:
692 		current_evmcs->guest_rip = value;
693 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
694 		break;
695 	case GUEST_RSP:
696 		current_evmcs->guest_rsp = value;
697 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC;
698 		break;
699 	case GUEST_RFLAGS:
700 		current_evmcs->guest_rflags = value;
701 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC;
702 		break;
703 	case HOST_IA32_PAT:
704 		current_evmcs->host_ia32_pat = value;
705 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
706 		break;
707 	case HOST_IA32_EFER:
708 		current_evmcs->host_ia32_efer = value;
709 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
710 		break;
711 	case HOST_CR0:
712 		current_evmcs->host_cr0 = value;
713 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
714 		break;
715 	case HOST_CR3:
716 		current_evmcs->host_cr3 = value;
717 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
718 		break;
719 	case HOST_CR4:
720 		current_evmcs->host_cr4 = value;
721 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
722 		break;
723 	case HOST_IA32_SYSENTER_ESP:
724 		current_evmcs->host_ia32_sysenter_esp = value;
725 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
726 		break;
727 	case HOST_IA32_SYSENTER_EIP:
728 		current_evmcs->host_ia32_sysenter_eip = value;
729 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
730 		break;
731 	case HOST_RIP:
732 		current_evmcs->host_rip = value;
733 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
734 		break;
735 	case IO_BITMAP_A:
736 		current_evmcs->io_bitmap_a = value;
737 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP;
738 		break;
739 	case IO_BITMAP_B:
740 		current_evmcs->io_bitmap_b = value;
741 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP;
742 		break;
743 	case MSR_BITMAP:
744 		current_evmcs->msr_bitmap = value;
745 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
746 		break;
747 	case GUEST_ES_BASE:
748 		current_evmcs->guest_es_base = value;
749 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
750 		break;
751 	case GUEST_CS_BASE:
752 		current_evmcs->guest_cs_base = value;
753 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
754 		break;
755 	case GUEST_SS_BASE:
756 		current_evmcs->guest_ss_base = value;
757 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
758 		break;
759 	case GUEST_DS_BASE:
760 		current_evmcs->guest_ds_base = value;
761 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
762 		break;
763 	case GUEST_FS_BASE:
764 		current_evmcs->guest_fs_base = value;
765 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
766 		break;
767 	case GUEST_GS_BASE:
768 		current_evmcs->guest_gs_base = value;
769 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
770 		break;
771 	case GUEST_LDTR_BASE:
772 		current_evmcs->guest_ldtr_base = value;
773 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
774 		break;
775 	case GUEST_TR_BASE:
776 		current_evmcs->guest_tr_base = value;
777 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
778 		break;
779 	case GUEST_GDTR_BASE:
780 		current_evmcs->guest_gdtr_base = value;
781 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
782 		break;
783 	case GUEST_IDTR_BASE:
784 		current_evmcs->guest_idtr_base = value;
785 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
786 		break;
787 	case TSC_OFFSET:
788 		current_evmcs->tsc_offset = value;
789 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2;
790 		break;
791 	case VIRTUAL_APIC_PAGE_ADDR:
792 		current_evmcs->virtual_apic_page_addr = value;
793 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2;
794 		break;
795 	case VMCS_LINK_POINTER:
796 		current_evmcs->vmcs_link_pointer = value;
797 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
798 		break;
799 	case GUEST_IA32_DEBUGCTL:
800 		current_evmcs->guest_ia32_debugctl = value;
801 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
802 		break;
803 	case GUEST_IA32_PAT:
804 		current_evmcs->guest_ia32_pat = value;
805 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
806 		break;
807 	case GUEST_IA32_EFER:
808 		current_evmcs->guest_ia32_efer = value;
809 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
810 		break;
811 	case GUEST_PDPTR0:
812 		current_evmcs->guest_pdptr0 = value;
813 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
814 		break;
815 	case GUEST_PDPTR1:
816 		current_evmcs->guest_pdptr1 = value;
817 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
818 		break;
819 	case GUEST_PDPTR2:
820 		current_evmcs->guest_pdptr2 = value;
821 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
822 		break;
823 	case GUEST_PDPTR3:
824 		current_evmcs->guest_pdptr3 = value;
825 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
826 		break;
827 	case GUEST_PENDING_DBG_EXCEPTIONS:
828 		current_evmcs->guest_pending_dbg_exceptions = value;
829 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
830 		break;
831 	case GUEST_SYSENTER_ESP:
832 		current_evmcs->guest_sysenter_esp = value;
833 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
834 		break;
835 	case GUEST_SYSENTER_EIP:
836 		current_evmcs->guest_sysenter_eip = value;
837 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
838 		break;
839 	case CR0_GUEST_HOST_MASK:
840 		current_evmcs->cr0_guest_host_mask = value;
841 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
842 		break;
843 	case CR4_GUEST_HOST_MASK:
844 		current_evmcs->cr4_guest_host_mask = value;
845 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
846 		break;
847 	case CR0_READ_SHADOW:
848 		current_evmcs->cr0_read_shadow = value;
849 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
850 		break;
851 	case CR4_READ_SHADOW:
852 		current_evmcs->cr4_read_shadow = value;
853 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
854 		break;
855 	case GUEST_CR0:
856 		current_evmcs->guest_cr0 = value;
857 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
858 		break;
859 	case GUEST_CR3:
860 		current_evmcs->guest_cr3 = value;
861 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
862 		break;
863 	case GUEST_CR4:
864 		current_evmcs->guest_cr4 = value;
865 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
866 		break;
867 	case GUEST_DR7:
868 		current_evmcs->guest_dr7 = value;
869 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR;
870 		break;
871 	case HOST_FS_BASE:
872 		current_evmcs->host_fs_base = value;
873 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
874 		break;
875 	case HOST_GS_BASE:
876 		current_evmcs->host_gs_base = value;
877 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
878 		break;
879 	case HOST_TR_BASE:
880 		current_evmcs->host_tr_base = value;
881 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
882 		break;
883 	case HOST_GDTR_BASE:
884 		current_evmcs->host_gdtr_base = value;
885 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
886 		break;
887 	case HOST_IDTR_BASE:
888 		current_evmcs->host_idtr_base = value;
889 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
890 		break;
891 	case HOST_RSP:
892 		current_evmcs->host_rsp = value;
893 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
894 		break;
895 	case EPT_POINTER:
896 		current_evmcs->ept_pointer = value;
897 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT;
898 		break;
899 	case GUEST_BNDCFGS:
900 		current_evmcs->guest_bndcfgs = value;
901 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
902 		break;
903 	case XSS_EXIT_BITMAP:
904 		current_evmcs->xss_exit_bitmap = value;
905 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2;
906 		break;
907 	case GUEST_PHYSICAL_ADDRESS:
908 		current_evmcs->guest_physical_address = value;
909 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
910 		break;
911 	case EXIT_QUALIFICATION:
912 		current_evmcs->exit_qualification = value;
913 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
914 		break;
915 	case GUEST_LINEAR_ADDRESS:
916 		current_evmcs->guest_linear_address = value;
917 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
918 		break;
919 	case VM_EXIT_MSR_STORE_ADDR:
920 		current_evmcs->vm_exit_msr_store_addr = value;
921 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
922 		break;
923 	case VM_EXIT_MSR_LOAD_ADDR:
924 		current_evmcs->vm_exit_msr_load_addr = value;
925 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
926 		break;
927 	case VM_ENTRY_MSR_LOAD_ADDR:
928 		current_evmcs->vm_entry_msr_load_addr = value;
929 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
930 		break;
931 	case CR3_TARGET_VALUE0:
932 		current_evmcs->cr3_target_value0 = value;
933 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
934 		break;
935 	case CR3_TARGET_VALUE1:
936 		current_evmcs->cr3_target_value1 = value;
937 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
938 		break;
939 	case CR3_TARGET_VALUE2:
940 		current_evmcs->cr3_target_value2 = value;
941 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
942 		break;
943 	case CR3_TARGET_VALUE3:
944 		current_evmcs->cr3_target_value3 = value;
945 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
946 		break;
947 	case TPR_THRESHOLD:
948 		current_evmcs->tpr_threshold = value;
949 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
950 		break;
951 	case GUEST_INTERRUPTIBILITY_INFO:
952 		current_evmcs->guest_interruptibility_info = value;
953 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC;
954 		break;
955 	case CPU_BASED_VM_EXEC_CONTROL:
956 		current_evmcs->cpu_based_vm_exec_control = value;
957 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC;
958 		break;
959 	case EXCEPTION_BITMAP:
960 		current_evmcs->exception_bitmap = value;
961 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN;
962 		break;
963 	case VM_ENTRY_CONTROLS:
964 		current_evmcs->vm_entry_controls = value;
965 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY;
966 		break;
967 	case VM_ENTRY_INTR_INFO_FIELD:
968 		current_evmcs->vm_entry_intr_info_field = value;
969 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT;
970 		break;
971 	case VM_ENTRY_EXCEPTION_ERROR_CODE:
972 		current_evmcs->vm_entry_exception_error_code = value;
973 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT;
974 		break;
975 	case VM_ENTRY_INSTRUCTION_LEN:
976 		current_evmcs->vm_entry_instruction_len = value;
977 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT;
978 		break;
979 	case HOST_IA32_SYSENTER_CS:
980 		current_evmcs->host_ia32_sysenter_cs = value;
981 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
982 		break;
983 	case PIN_BASED_VM_EXEC_CONTROL:
984 		current_evmcs->pin_based_vm_exec_control = value;
985 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1;
986 		break;
987 	case VM_EXIT_CONTROLS:
988 		current_evmcs->vm_exit_controls = value;
989 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1;
990 		break;
991 	case SECONDARY_VM_EXEC_CONTROL:
992 		current_evmcs->secondary_vm_exec_control = value;
993 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1;
994 		break;
995 	case GUEST_ES_LIMIT:
996 		current_evmcs->guest_es_limit = value;
997 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
998 		break;
999 	case GUEST_CS_LIMIT:
1000 		current_evmcs->guest_cs_limit = value;
1001 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1002 		break;
1003 	case GUEST_SS_LIMIT:
1004 		current_evmcs->guest_ss_limit = value;
1005 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1006 		break;
1007 	case GUEST_DS_LIMIT:
1008 		current_evmcs->guest_ds_limit = value;
1009 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1010 		break;
1011 	case GUEST_FS_LIMIT:
1012 		current_evmcs->guest_fs_limit = value;
1013 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1014 		break;
1015 	case GUEST_GS_LIMIT:
1016 		current_evmcs->guest_gs_limit = value;
1017 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1018 		break;
1019 	case GUEST_LDTR_LIMIT:
1020 		current_evmcs->guest_ldtr_limit = value;
1021 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1022 		break;
1023 	case GUEST_TR_LIMIT:
1024 		current_evmcs->guest_tr_limit = value;
1025 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1026 		break;
1027 	case GUEST_GDTR_LIMIT:
1028 		current_evmcs->guest_gdtr_limit = value;
1029 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1030 		break;
1031 	case GUEST_IDTR_LIMIT:
1032 		current_evmcs->guest_idtr_limit = value;
1033 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1034 		break;
1035 	case GUEST_ES_AR_BYTES:
1036 		current_evmcs->guest_es_ar_bytes = value;
1037 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1038 		break;
1039 	case GUEST_CS_AR_BYTES:
1040 		current_evmcs->guest_cs_ar_bytes = value;
1041 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1042 		break;
1043 	case GUEST_SS_AR_BYTES:
1044 		current_evmcs->guest_ss_ar_bytes = value;
1045 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1046 		break;
1047 	case GUEST_DS_AR_BYTES:
1048 		current_evmcs->guest_ds_ar_bytes = value;
1049 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1050 		break;
1051 	case GUEST_FS_AR_BYTES:
1052 		current_evmcs->guest_fs_ar_bytes = value;
1053 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1054 		break;
1055 	case GUEST_GS_AR_BYTES:
1056 		current_evmcs->guest_gs_ar_bytes = value;
1057 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1058 		break;
1059 	case GUEST_LDTR_AR_BYTES:
1060 		current_evmcs->guest_ldtr_ar_bytes = value;
1061 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1062 		break;
1063 	case GUEST_TR_AR_BYTES:
1064 		current_evmcs->guest_tr_ar_bytes = value;
1065 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1066 		break;
1067 	case GUEST_ACTIVITY_STATE:
1068 		current_evmcs->guest_activity_state = value;
1069 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
1070 		break;
1071 	case GUEST_SYSENTER_CS:
1072 		current_evmcs->guest_sysenter_cs = value;
1073 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
1074 		break;
1075 	case VM_INSTRUCTION_ERROR:
1076 		current_evmcs->vm_instruction_error = value;
1077 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1078 		break;
1079 	case VM_EXIT_REASON:
1080 		current_evmcs->vm_exit_reason = value;
1081 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1082 		break;
1083 	case VM_EXIT_INTR_INFO:
1084 		current_evmcs->vm_exit_intr_info = value;
1085 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1086 		break;
1087 	case VM_EXIT_INTR_ERROR_CODE:
1088 		current_evmcs->vm_exit_intr_error_code = value;
1089 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1090 		break;
1091 	case IDT_VECTORING_INFO_FIELD:
1092 		current_evmcs->idt_vectoring_info_field = value;
1093 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1094 		break;
1095 	case IDT_VECTORING_ERROR_CODE:
1096 		current_evmcs->idt_vectoring_error_code = value;
1097 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1098 		break;
1099 	case VM_EXIT_INSTRUCTION_LEN:
1100 		current_evmcs->vm_exit_instruction_len = value;
1101 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1102 		break;
1103 	case VMX_INSTRUCTION_INFO:
1104 		current_evmcs->vmx_instruction_info = value;
1105 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE;
1106 		break;
1107 	case PAGE_FAULT_ERROR_CODE_MASK:
1108 		current_evmcs->page_fault_error_code_mask = value;
1109 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1110 		break;
1111 	case PAGE_FAULT_ERROR_CODE_MATCH:
1112 		current_evmcs->page_fault_error_code_match = value;
1113 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1114 		break;
1115 	case CR3_TARGET_COUNT:
1116 		current_evmcs->cr3_target_count = value;
1117 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1118 		break;
1119 	case VM_EXIT_MSR_STORE_COUNT:
1120 		current_evmcs->vm_exit_msr_store_count = value;
1121 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1122 		break;
1123 	case VM_EXIT_MSR_LOAD_COUNT:
1124 		current_evmcs->vm_exit_msr_load_count = value;
1125 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1126 		break;
1127 	case VM_ENTRY_MSR_LOAD_COUNT:
1128 		current_evmcs->vm_entry_msr_load_count = value;
1129 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
1130 		break;
1131 	case HOST_ES_SELECTOR:
1132 		current_evmcs->host_es_selector = value;
1133 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1134 		break;
1135 	case HOST_CS_SELECTOR:
1136 		current_evmcs->host_cs_selector = value;
1137 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1138 		break;
1139 	case HOST_SS_SELECTOR:
1140 		current_evmcs->host_ss_selector = value;
1141 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1142 		break;
1143 	case HOST_DS_SELECTOR:
1144 		current_evmcs->host_ds_selector = value;
1145 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1146 		break;
1147 	case HOST_FS_SELECTOR:
1148 		current_evmcs->host_fs_selector = value;
1149 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1150 		break;
1151 	case HOST_GS_SELECTOR:
1152 		current_evmcs->host_gs_selector = value;
1153 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1154 		break;
1155 	case HOST_TR_SELECTOR:
1156 		current_evmcs->host_tr_selector = value;
1157 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1158 		break;
1159 	case GUEST_ES_SELECTOR:
1160 		current_evmcs->guest_es_selector = value;
1161 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1162 		break;
1163 	case GUEST_CS_SELECTOR:
1164 		current_evmcs->guest_cs_selector = value;
1165 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1166 		break;
1167 	case GUEST_SS_SELECTOR:
1168 		current_evmcs->guest_ss_selector = value;
1169 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1170 		break;
1171 	case GUEST_DS_SELECTOR:
1172 		current_evmcs->guest_ds_selector = value;
1173 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1174 		break;
1175 	case GUEST_FS_SELECTOR:
1176 		current_evmcs->guest_fs_selector = value;
1177 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1178 		break;
1179 	case GUEST_GS_SELECTOR:
1180 		current_evmcs->guest_gs_selector = value;
1181 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1182 		break;
1183 	case GUEST_LDTR_SELECTOR:
1184 		current_evmcs->guest_ldtr_selector = value;
1185 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1186 		break;
1187 	case GUEST_TR_SELECTOR:
1188 		current_evmcs->guest_tr_selector = value;
1189 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2;
1190 		break;
1191 	case VIRTUAL_PROCESSOR_ID:
1192 		current_evmcs->virtual_processor_id = value;
1193 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT;
1194 		break;
1195 	case HOST_IA32_PERF_GLOBAL_CTRL:
1196 		current_evmcs->host_ia32_perf_global_ctrl = value;
1197 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1198 		break;
1199 	case GUEST_IA32_PERF_GLOBAL_CTRL:
1200 		current_evmcs->guest_ia32_perf_global_ctrl = value;
1201 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1;
1202 		break;
1203 	case ENCLS_EXITING_BITMAP:
1204 		current_evmcs->encls_exiting_bitmap = value;
1205 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2;
1206 		break;
1207 	case TSC_MULTIPLIER:
1208 		current_evmcs->tsc_multiplier = value;
1209 		current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2;
1210 		break;
1211 	default: return 1;
1212 	}
1213 
1214 	return 0;
1215 }
1216 
evmcs_vmlaunch(void)1217 static inline int evmcs_vmlaunch(void)
1218 {
1219 	int ret;
1220 
1221 	current_evmcs->hv_clean_fields = 0;
1222 
1223 	__asm__ __volatile__("push %%rbp;"
1224 			     "push %%rcx;"
1225 			     "push %%rdx;"
1226 			     "push %%rsi;"
1227 			     "push %%rdi;"
1228 			     "push $0;"
1229 			     "mov %%rsp, (%[host_rsp]);"
1230 			     "lea 1f(%%rip), %%rax;"
1231 			     "mov %%rax, (%[host_rip]);"
1232 			     "vmlaunch;"
1233 			     "incq (%%rsp);"
1234 			     "1: pop %%rax;"
1235 			     "pop %%rdi;"
1236 			     "pop %%rsi;"
1237 			     "pop %%rdx;"
1238 			     "pop %%rcx;"
1239 			     "pop %%rbp;"
1240 			     : [ret]"=&a"(ret)
1241 			     : [host_rsp]"r"
1242 			       ((uint64_t)&current_evmcs->host_rsp),
1243 			       [host_rip]"r"
1244 			       ((uint64_t)&current_evmcs->host_rip)
1245 			     : "memory", "cc", "rbx", "r8", "r9", "r10",
1246 			       "r11", "r12", "r13", "r14", "r15");
1247 	return ret;
1248 }
1249 
1250 /*
1251  * No guest state (e.g. GPRs) is established by this vmresume.
1252  */
evmcs_vmresume(void)1253 static inline int evmcs_vmresume(void)
1254 {
1255 	int ret;
1256 
1257 	/* HOST_RIP */
1258 	current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
1259 	/* HOST_RSP */
1260 	current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
1261 
1262 	__asm__ __volatile__("push %%rbp;"
1263 			     "push %%rcx;"
1264 			     "push %%rdx;"
1265 			     "push %%rsi;"
1266 			     "push %%rdi;"
1267 			     "push $0;"
1268 			     "mov %%rsp, (%[host_rsp]);"
1269 			     "lea 1f(%%rip), %%rax;"
1270 			     "mov %%rax, (%[host_rip]);"
1271 			     "vmresume;"
1272 			     "incq (%%rsp);"
1273 			     "1: pop %%rax;"
1274 			     "pop %%rdi;"
1275 			     "pop %%rsi;"
1276 			     "pop %%rdx;"
1277 			     "pop %%rcx;"
1278 			     "pop %%rbp;"
1279 			     : [ret]"=&a"(ret)
1280 			     : [host_rsp]"r"
1281 			       ((uint64_t)&current_evmcs->host_rsp),
1282 			       [host_rip]"r"
1283 			       ((uint64_t)&current_evmcs->host_rip)
1284 			     : "memory", "cc", "rbx", "r8", "r9", "r10",
1285 			       "r11", "r12", "r13", "r14", "r15");
1286 	return ret;
1287 }
1288 
1289 #endif /* !SELFTEST_KVM_EVMCS_H */
1290