xref: /linux/tools/testing/selftests/kvm/include/x86/svm_util.h (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2020, Red Hat, Inc.
4  */
5 
6 #ifndef SELFTEST_KVM_SVM_UTILS_H
7 #define SELFTEST_KVM_SVM_UTILS_H
8 
9 #include <asm/svm.h>
10 
11 #include <stdint.h>
12 #include "svm.h"
13 #include "processor.h"
14 
15 struct svm_test_data {
16 	/* VMCB */
17 	struct vmcb *vmcb; /* gva */
18 	void *vmcb_hva;
19 	u64 vmcb_gpa;
20 
21 	/* host state-save area */
22 	struct vmcb_save_area *save_area; /* gva */
23 	void *save_area_hva;
24 	u64 save_area_gpa;
25 
26 	/* MSR-Bitmap */
27 	void *msr; /* gva */
28 	void *msr_hva;
29 	u64 msr_gpa;
30 
31 	/* Stack */
32 	void *stack; /* gva */
33 
34 	/* NPT */
35 	u64 ncr3_gpa;
36 };
37 
38 static inline void vmmcall(void)
39 {
40 	/*
41 	 * Stuff RAX and RCX with "safe" values to make sure L0 doesn't handle
42 	 * it as a valid hypercall (e.g. Hyper-V L2 TLB flush) as the intended
43 	 * use of this function is to exit to L1 from L2.  Clobber all other
44 	 * GPRs as L1 doesn't correctly preserve them during vmexits.
45 	 */
46 	__asm__ __volatile__("push %%rbp; vmmcall; pop %%rbp"
47 			     : : "a"(0xdeadbeef), "c"(0xbeefdead)
48 			     : "rbx", "rdx", "rsi", "rdi", "r8", "r9",
49 			       "r10", "r11", "r12", "r13", "r14", "r15");
50 }
51 
52 #define stgi()			\
53 	__asm__ __volatile__(	\
54 		"stgi\n"	\
55 		)
56 
57 #define clgi()			\
58 	__asm__ __volatile__(	\
59 		"clgi\n"	\
60 		)
61 
62 struct svm_test_data *vcpu_alloc_svm(struct kvm_vm *vm, gva_t *p_svm_gva);
63 void generic_svm_setup(struct svm_test_data *svm, void *guest_rip);
64 void run_guest(struct vmcb *vmcb, u64 vmcb_gpa);
65 
66 static inline bool kvm_cpu_has_npt(void)
67 {
68 	return kvm_cpu_has(X86_FEATURE_NPT);
69 }
70 void vm_enable_npt(struct kvm_vm *vm);
71 
72 int open_sev_dev_path_or_exit(void);
73 
74 #endif /* SELFTEST_KVM_SVM_UTILS_H */
75