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