xref: /linux/tools/testing/selftests/kvm/x86/fix_hypercall_test.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020, Google LLC.
4  *
5  * Tests for KVM paravirtual feature disablement
6  */
7 #include <asm/kvm_para.h>
8 #include <linux/kvm_para.h>
9 #include <stdint.h>
10 
11 #include "kvm_test_harness.h"
12 #include "apic.h"
13 #include "test_util.h"
14 #include "kvm_util.h"
15 #include "processor.h"
16 
17 /* VMCALL and VMMCALL are both 3-byte opcodes. */
18 #define HYPERCALL_INSN_SIZE	3
19 
20 static bool quirk_disabled;
21 
22 static void guest_ud_handler(struct ex_regs *regs)
23 {
24 	regs->rax = -EFAULT;
25 	regs->rip += HYPERCALL_INSN_SIZE;
26 }
27 
28 static const u8 vmx_vmcall[HYPERCALL_INSN_SIZE]  = { 0x0f, 0x01, 0xc1 };
29 static const u8 svm_vmmcall[HYPERCALL_INSN_SIZE] = { 0x0f, 0x01, 0xd9 };
30 
31 extern u8 hypercall_insn[HYPERCALL_INSN_SIZE];
32 static u64 do_sched_yield(u8 apic_id)
33 {
34 	u64 ret;
35 
36 	asm volatile("hypercall_insn:\n\t"
37 		     ".byte 0xcc,0xcc,0xcc\n\t"
38 		     : "=a"(ret)
39 		     : "a"((u64)KVM_HC_SCHED_YIELD), "b"((u64)apic_id)
40 		     : "memory");
41 
42 	return ret;
43 }
44 
45 static void guest_main(void)
46 {
47 	const u8 *native_hypercall_insn;
48 	const u8 *other_hypercall_insn;
49 	u64 ret;
50 
51 	if (host_cpu_is_intel) {
52 		native_hypercall_insn = vmx_vmcall;
53 		other_hypercall_insn  = svm_vmmcall;
54 	} else if (host_cpu_is_amd_compatible) {
55 		native_hypercall_insn = svm_vmmcall;
56 		other_hypercall_insn  = vmx_vmcall;
57 	} else {
58 		GUEST_ASSERT(0);
59 		/* unreachable */
60 		return;
61 	}
62 
63 	memcpy(hypercall_insn, other_hypercall_insn, HYPERCALL_INSN_SIZE);
64 
65 	ret = do_sched_yield(GET_APIC_ID_FIELD(xapic_read_reg(APIC_ID)));
66 
67 	/*
68 	 * If the quirk is disabled, verify that guest_ud_handler() "returned"
69 	 * -EFAULT and that KVM did NOT patch the hypercall.  If the quirk is
70 	 * enabled, verify that the hypercall succeeded and that KVM patched in
71 	 * the "right" hypercall.
72 	 */
73 	if (quirk_disabled) {
74 		GUEST_ASSERT(ret == (u64)-EFAULT);
75 		GUEST_ASSERT(!memcmp(other_hypercall_insn, hypercall_insn,
76 			     HYPERCALL_INSN_SIZE));
77 	} else {
78 		GUEST_ASSERT(!ret);
79 		GUEST_ASSERT(!memcmp(native_hypercall_insn, hypercall_insn,
80 			     HYPERCALL_INSN_SIZE));
81 	}
82 
83 	GUEST_DONE();
84 }
85 
86 KVM_ONE_VCPU_TEST_SUITE(fix_hypercall);
87 
88 static void enter_guest(struct kvm_vcpu *vcpu)
89 {
90 	struct kvm_run *run = vcpu->run;
91 	struct ucall uc;
92 
93 	vcpu_run(vcpu);
94 	switch (get_ucall(vcpu, &uc)) {
95 	case UCALL_SYNC:
96 		pr_info("%s: %016lx\n", (const char *)uc.args[2], uc.args[3]);
97 		break;
98 	case UCALL_DONE:
99 		return;
100 	case UCALL_ABORT:
101 		REPORT_GUEST_ASSERT(uc);
102 	default:
103 		TEST_FAIL("Unhandled ucall: %ld\nexit_reason: %u (%s)",
104 			  uc.cmd, run->exit_reason, exit_reason_str(run->exit_reason));
105 	}
106 }
107 
108 static void test_fix_hypercall(struct kvm_vcpu *vcpu, bool disable_quirk)
109 {
110 	struct kvm_vm *vm = vcpu->vm;
111 
112 	vm_install_exception_handler(vcpu->vm, UD_VECTOR, guest_ud_handler);
113 
114 	if (disable_quirk)
115 		vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2,
116 			      KVM_X86_QUIRK_FIX_HYPERCALL_INSN);
117 
118 	quirk_disabled = disable_quirk;
119 	sync_global_to_guest(vm, quirk_disabled);
120 
121 	virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
122 
123 	enter_guest(vcpu);
124 }
125 
126 KVM_ONE_VCPU_TEST(fix_hypercall, enable_quirk, guest_main)
127 {
128 	test_fix_hypercall(vcpu, false);
129 }
130 
131 KVM_ONE_VCPU_TEST(fix_hypercall, disable_quirk, guest_main)
132 {
133 	test_fix_hypercall(vcpu, true);
134 }
135 
136 int main(int argc, char *argv[])
137 {
138 	TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_FIX_HYPERCALL_INSN);
139 
140 	return test_harness_run(argc, argv);
141 }
142