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