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