1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * KVM_GET/SET_* tests
4  *
5  * Copyright (C) 2022, Red Hat, Inc.
6  *
7  * Tests for Hyper-V extensions to SVM.
8  */
9 #define _GNU_SOURCE /* for program_invocation_short_name */
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/ioctl.h>
15 #include <linux/bitmap.h>
16 
17 #include "test_util.h"
18 
19 #include "kvm_util.h"
20 #include "processor.h"
21 #include "svm_util.h"
22 #include "hyperv.h"
23 
24 #define L2_GUEST_STACK_SIZE 256
25 
26 struct hv_enlightenments {
27 	struct __packed hv_enlightenments_control {
28 		u32 nested_flush_hypercall:1;
29 		u32 msr_bitmap:1;
30 		u32 enlightened_npt_tlb: 1;
31 		u32 reserved:29;
32 	} __packed hv_enlightenments_control;
33 	u32 hv_vp_id;
34 	u64 hv_vm_id;
35 	u64 partition_assist_page;
36 	u64 reserved;
37 } __packed;
38 
39 /*
40  * Hyper-V uses the software reserved clean bit in VMCB
41  */
42 #define VMCB_HV_NESTED_ENLIGHTENMENTS (1U << 31)
43 
l2_guest_code(void)44 void l2_guest_code(void)
45 {
46 	GUEST_SYNC(3);
47 	/* Exit to L1 */
48 	vmmcall();
49 
50 	/* MSR-Bitmap tests */
51 	rdmsr(MSR_FS_BASE); /* intercepted */
52 	rdmsr(MSR_FS_BASE); /* intercepted */
53 	rdmsr(MSR_GS_BASE); /* not intercepted */
54 	vmmcall();
55 	rdmsr(MSR_GS_BASE); /* intercepted */
56 
57 	GUEST_SYNC(5);
58 
59 	/* Done, exit to L1 and never come back.  */
60 	vmmcall();
61 }
62 
guest_code(struct svm_test_data * svm)63 static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm)
64 {
65 	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
66 	struct vmcb *vmcb = svm->vmcb;
67 	struct hv_enlightenments *hve =
68 		(struct hv_enlightenments *)vmcb->control.reserved_sw;
69 
70 	GUEST_SYNC(1);
71 
72 	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
73 
74 	GUEST_ASSERT(svm->vmcb_gpa);
75 	/* Prepare for L2 execution. */
76 	generic_svm_setup(svm, l2_guest_code,
77 			  &l2_guest_stack[L2_GUEST_STACK_SIZE]);
78 
79 	GUEST_SYNC(2);
80 	run_guest(vmcb, svm->vmcb_gpa);
81 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
82 	GUEST_SYNC(4);
83 	vmcb->save.rip += 3;
84 
85 	/* Intercept RDMSR 0xc0000100 */
86 	vmcb->control.intercept |= 1ULL << INTERCEPT_MSR_PROT;
87 	set_bit(2 * (MSR_FS_BASE & 0x1fff), svm->msr + 0x800);
88 	run_guest(vmcb, svm->vmcb_gpa);
89 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
90 	vmcb->save.rip += 2; /* rdmsr */
91 
92 	/* Enable enlightened MSR bitmap */
93 	hve->hv_enlightenments_control.msr_bitmap = 1;
94 	run_guest(vmcb, svm->vmcb_gpa);
95 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
96 	vmcb->save.rip += 2; /* rdmsr */
97 
98 	/* Intercept RDMSR 0xc0000101 without telling KVM about it */
99 	set_bit(2 * (MSR_GS_BASE & 0x1fff), svm->msr + 0x800);
100 	/* Make sure HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP is set */
101 	vmcb->control.clean |= VMCB_HV_NESTED_ENLIGHTENMENTS;
102 	run_guest(vmcb, svm->vmcb_gpa);
103 	/* Make sure we don't see SVM_EXIT_MSR here so eMSR bitmap works */
104 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
105 	vmcb->save.rip += 3; /* vmcall */
106 
107 	/* Now tell KVM we've changed MSR-Bitmap */
108 	vmcb->control.clean &= ~VMCB_HV_NESTED_ENLIGHTENMENTS;
109 	run_guest(vmcb, svm->vmcb_gpa);
110 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
111 	vmcb->save.rip += 2; /* rdmsr */
112 
113 	run_guest(vmcb, svm->vmcb_gpa);
114 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
115 	GUEST_SYNC(6);
116 
117 	GUEST_DONE();
118 }
119 
main(int argc,char * argv[])120 int main(int argc, char *argv[])
121 {
122 	vm_vaddr_t nested_gva = 0;
123 
124 	struct kvm_vcpu *vcpu;
125 	struct kvm_vm *vm;
126 	struct kvm_run *run;
127 	struct ucall uc;
128 	int stage;
129 
130 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
131 
132 	/* Create VM */
133 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
134 	vcpu_set_hv_cpuid(vcpu);
135 	run = vcpu->run;
136 	vcpu_alloc_svm(vm, &nested_gva);
137 	vcpu_args_set(vcpu, 1, nested_gva);
138 
139 	for (stage = 1;; stage++) {
140 		vcpu_run(vcpu);
141 		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
142 			    "Stage %d: unexpected exit reason: %u (%s),\n",
143 			    stage, run->exit_reason,
144 			    exit_reason_str(run->exit_reason));
145 
146 		switch (get_ucall(vcpu, &uc)) {
147 		case UCALL_ABORT:
148 			REPORT_GUEST_ASSERT(uc);
149 			/* NOT REACHED */
150 		case UCALL_SYNC:
151 			break;
152 		case UCALL_DONE:
153 			goto done;
154 		default:
155 			TEST_FAIL("Unknown ucall %lu", uc.cmd);
156 		}
157 
158 		/* UCALL_SYNC is handled here.  */
159 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
160 			    uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
161 			    stage, (ulong)uc.args[1]);
162 
163 	}
164 
165 done:
166 	kvm_vm_free(vm);
167 }
168