1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tools/testing/selftests/kvm/lib/x86_64/svm.c
4  * Helpers used for nested SVM testing
5  * Largely inspired from KVM unit test svm.c
6  *
7  * Copyright (C) 2020, Red Hat, Inc.
8  */
9 
10 #include "test_util.h"
11 #include "kvm_util.h"
12 #include "../kvm_util_internal.h"
13 #include "processor.h"
14 #include "svm_util.h"
15 
16 #define SEV_DEV_PATH "/dev/sev"
17 
18 struct gpr64_regs guest_regs;
19 u64 rflags;
20 
21 /* Allocate memory regions for nested SVM tests.
22  *
23  * Input Args:
24  *   vm - The VM to allocate guest-virtual addresses in.
25  *
26  * Output Args:
27  *   p_svm_gva - The guest virtual address for the struct svm_test_data.
28  *
29  * Return:
30  *   Pointer to structure with the addresses of the SVM areas.
31  */
32 struct svm_test_data *
vcpu_alloc_svm(struct kvm_vm * vm,vm_vaddr_t * p_svm_gva)33 vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t *p_svm_gva)
34 {
35 	vm_vaddr_t svm_gva = vm_vaddr_alloc_page(vm);
36 	struct svm_test_data *svm = addr_gva2hva(vm, svm_gva);
37 
38 	svm->vmcb = (void *)vm_vaddr_alloc_page(vm);
39 	svm->vmcb_hva = addr_gva2hva(vm, (uintptr_t)svm->vmcb);
40 	svm->vmcb_gpa = addr_gva2gpa(vm, (uintptr_t)svm->vmcb);
41 
42 	svm->save_area = (void *)vm_vaddr_alloc_page(vm);
43 	svm->save_area_hva = addr_gva2hva(vm, (uintptr_t)svm->save_area);
44 	svm->save_area_gpa = addr_gva2gpa(vm, (uintptr_t)svm->save_area);
45 
46 	svm->msr = (void *)vm_vaddr_alloc_page(vm);
47 	svm->msr_hva = addr_gva2hva(vm, (uintptr_t)svm->msr);
48 	svm->msr_gpa = addr_gva2gpa(vm, (uintptr_t)svm->msr);
49 	memset(svm->msr_hva, 0, getpagesize());
50 
51 	*p_svm_gva = svm_gva;
52 	return svm;
53 }
54 
vmcb_set_seg(struct vmcb_seg * seg,u16 selector,u64 base,u32 limit,u32 attr)55 static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
56 			 u64 base, u32 limit, u32 attr)
57 {
58 	seg->selector = selector;
59 	seg->attrib = attr;
60 	seg->limit = limit;
61 	seg->base = base;
62 }
63 
64 /*
65  * Avoid using memset to clear the vmcb, since libc may not be
66  * available in L1 (and, even if it is, features that libc memset may
67  * want to use, like AVX, may not be enabled).
68  */
clear_vmcb(struct vmcb * vmcb)69 static void clear_vmcb(struct vmcb *vmcb)
70 {
71 	int n = sizeof(*vmcb) / sizeof(u32);
72 
73 	asm volatile ("rep stosl" : "+c"(n), "+D"(vmcb) : "a"(0) : "memory");
74 }
75 
generic_svm_setup(struct svm_test_data * svm,void * guest_rip,void * guest_rsp)76 void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp)
77 {
78 	struct vmcb *vmcb = svm->vmcb;
79 	uint64_t vmcb_gpa = svm->vmcb_gpa;
80 	struct vmcb_save_area *save = &vmcb->save;
81 	struct vmcb_control_area *ctrl = &vmcb->control;
82 	u32 data_seg_attr = 3 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_P_MASK
83 	      | SVM_SELECTOR_DB_MASK | SVM_SELECTOR_G_MASK;
84 	u32 code_seg_attr = 9 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_P_MASK
85 		| SVM_SELECTOR_L_MASK | SVM_SELECTOR_G_MASK;
86 	uint64_t efer;
87 
88 	efer = rdmsr(MSR_EFER);
89 	wrmsr(MSR_EFER, efer | EFER_SVME);
90 	wrmsr(MSR_VM_HSAVE_PA, svm->save_area_gpa);
91 
92 	clear_vmcb(vmcb);
93 	asm volatile ("vmsave %0\n\t" : : "a" (vmcb_gpa) : "memory");
94 	vmcb_set_seg(&save->es, get_es(), 0, -1U, data_seg_attr);
95 	vmcb_set_seg(&save->cs, get_cs(), 0, -1U, code_seg_attr);
96 	vmcb_set_seg(&save->ss, get_ss(), 0, -1U, data_seg_attr);
97 	vmcb_set_seg(&save->ds, get_ds(), 0, -1U, data_seg_attr);
98 	vmcb_set_seg(&save->gdtr, 0, get_gdt().address, get_gdt().size, 0);
99 	vmcb_set_seg(&save->idtr, 0, get_idt().address, get_idt().size, 0);
100 
101 	ctrl->asid = 1;
102 	save->cpl = 0;
103 	save->efer = rdmsr(MSR_EFER);
104 	asm volatile ("mov %%cr4, %0" : "=r"(save->cr4) : : "memory");
105 	asm volatile ("mov %%cr3, %0" : "=r"(save->cr3) : : "memory");
106 	asm volatile ("mov %%cr0, %0" : "=r"(save->cr0) : : "memory");
107 	asm volatile ("mov %%dr7, %0" : "=r"(save->dr7) : : "memory");
108 	asm volatile ("mov %%dr6, %0" : "=r"(save->dr6) : : "memory");
109 	asm volatile ("mov %%cr2, %0" : "=r"(save->cr2) : : "memory");
110 	save->g_pat = rdmsr(MSR_IA32_CR_PAT);
111 	save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
112 	ctrl->intercept = (1ULL << INTERCEPT_VMRUN) |
113 				(1ULL << INTERCEPT_VMMCALL);
114 	ctrl->msrpm_base_pa = svm->msr_gpa;
115 
116 	vmcb->save.rip = (u64)guest_rip;
117 	vmcb->save.rsp = (u64)guest_rsp;
118 	guest_regs.rdi = (u64)svm;
119 }
120 
121 /*
122  * save/restore 64-bit general registers except rax, rip, rsp
123  * which are directly handed through the VMCB guest processor state
124  */
125 #define SAVE_GPR_C				\
126 	"xchg %%rbx, guest_regs+0x20\n\t"	\
127 	"xchg %%rcx, guest_regs+0x10\n\t"	\
128 	"xchg %%rdx, guest_regs+0x18\n\t"	\
129 	"xchg %%rbp, guest_regs+0x30\n\t"	\
130 	"xchg %%rsi, guest_regs+0x38\n\t"	\
131 	"xchg %%rdi, guest_regs+0x40\n\t"	\
132 	"xchg %%r8,  guest_regs+0x48\n\t"	\
133 	"xchg %%r9,  guest_regs+0x50\n\t"	\
134 	"xchg %%r10, guest_regs+0x58\n\t"	\
135 	"xchg %%r11, guest_regs+0x60\n\t"	\
136 	"xchg %%r12, guest_regs+0x68\n\t"	\
137 	"xchg %%r13, guest_regs+0x70\n\t"	\
138 	"xchg %%r14, guest_regs+0x78\n\t"	\
139 	"xchg %%r15, guest_regs+0x80\n\t"
140 
141 #define LOAD_GPR_C      SAVE_GPR_C
142 
143 /*
144  * selftests do not use interrupts so we dropped clgi/sti/cli/stgi
145  * for now. registers involved in LOAD/SAVE_GPR_C are eventually
146  * unmodified so they do not need to be in the clobber list.
147  */
run_guest(struct vmcb * vmcb,uint64_t vmcb_gpa)148 void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa)
149 {
150 	asm volatile (
151 		"vmload %[vmcb_gpa]\n\t"
152 		"mov rflags, %%r15\n\t"	// rflags
153 		"mov %%r15, 0x170(%[vmcb])\n\t"
154 		"mov guest_regs, %%r15\n\t"	// rax
155 		"mov %%r15, 0x1f8(%[vmcb])\n\t"
156 		LOAD_GPR_C
157 		"vmrun %[vmcb_gpa]\n\t"
158 		SAVE_GPR_C
159 		"mov 0x170(%[vmcb]), %%r15\n\t"	// rflags
160 		"mov %%r15, rflags\n\t"
161 		"mov 0x1f8(%[vmcb]), %%r15\n\t"	// rax
162 		"mov %%r15, guest_regs\n\t"
163 		"vmsave %[vmcb_gpa]\n\t"
164 		: : [vmcb] "r" (vmcb), [vmcb_gpa] "a" (vmcb_gpa)
165 		: "r15", "memory");
166 }
167 
nested_svm_supported(void)168 bool nested_svm_supported(void)
169 {
170 	struct kvm_cpuid_entry2 *entry =
171 		kvm_get_supported_cpuid_entry(0x80000001);
172 
173 	return entry->ecx & CPUID_SVM;
174 }
175 
nested_svm_check_supported(void)176 void nested_svm_check_supported(void)
177 {
178 	if (!nested_svm_supported()) {
179 		print_skip("nested SVM not enabled");
180 		exit(KSFT_SKIP);
181 	}
182 }
183 
184 /*
185  * Open SEV_DEV_PATH if available, otherwise exit the entire program.
186  *
187  * Return:
188  *   The opened file descriptor of /dev/sev.
189  */
open_sev_dev_path_or_exit(void)190 int open_sev_dev_path_or_exit(void)
191 {
192 	return open_path_or_exit(SEV_DEV_PATH, 0);
193 }
194