1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * xen_vmcall_test
4  *
5  * Copyright © 2020 Amazon.com, Inc. or its affiliates.
6  *
7  * Userspace hypercall testing
8  */
9 
10 #include "test_util.h"
11 #include "kvm_util.h"
12 #include "processor.h"
13 
14 #define HCALL_REGION_GPA	0xc0000000ULL
15 #define HCALL_REGION_SLOT	10
16 
17 #define INPUTVALUE 17
18 #define ARGVALUE(x) (0xdeadbeef5a5a0000UL + x)
19 #define RETVALUE 0xcafef00dfbfbffffUL
20 
21 #define XEN_HYPERCALL_MSR	0x40000200
22 #define HV_GUEST_OS_ID_MSR	0x40000000
23 #define HV_HYPERCALL_MSR	0x40000001
24 
25 #define HVCALL_SIGNAL_EVENT		0x005d
26 #define HV_STATUS_INVALID_ALIGNMENT	4
27 
28 static void guest_code(void)
29 {
30 	unsigned long rax = INPUTVALUE;
31 	unsigned long rdi = ARGVALUE(1);
32 	unsigned long rsi = ARGVALUE(2);
33 	unsigned long rdx = ARGVALUE(3);
34 	unsigned long rcx;
35 	register unsigned long r10 __asm__("r10") = ARGVALUE(4);
36 	register unsigned long r8 __asm__("r8") = ARGVALUE(5);
37 	register unsigned long r9 __asm__("r9") = ARGVALUE(6);
38 
39 	/* First a direct invocation of 'vmcall' */
40 	__asm__ __volatile__("vmcall" :
41 			     "=a"(rax) :
42 			     "a"(rax), "D"(rdi), "S"(rsi), "d"(rdx),
43 			     "r"(r10), "r"(r8), "r"(r9));
44 	GUEST_ASSERT(rax == RETVALUE);
45 
46 	/* Fill in the Xen hypercall page */
47 	__asm__ __volatile__("wrmsr" : : "c" (XEN_HYPERCALL_MSR),
48 			     "a" (HCALL_REGION_GPA & 0xffffffff),
49 			     "d" (HCALL_REGION_GPA >> 32));
50 
51 	/* Set Hyper-V Guest OS ID */
52 	__asm__ __volatile__("wrmsr" : : "c" (HV_GUEST_OS_ID_MSR),
53 			     "a" (0x5a), "d" (0));
54 
55 	/* Hyper-V hypercall page */
56 	u64 msrval = HCALL_REGION_GPA + PAGE_SIZE + 1;
57 	__asm__ __volatile__("wrmsr" : : "c" (HV_HYPERCALL_MSR),
58 			     "a" (msrval & 0xffffffff),
59 			     "d" (msrval >> 32));
60 
61 	/* Invoke a Xen hypercall */
62 	__asm__ __volatile__("call *%1" : "=a"(rax) :
63 			     "r"(HCALL_REGION_GPA + INPUTVALUE * 32),
64 			     "a"(rax), "D"(rdi), "S"(rsi), "d"(rdx),
65 			     "r"(r10), "r"(r8), "r"(r9));
66 	GUEST_ASSERT(rax == RETVALUE);
67 
68 	/* Invoke a Hyper-V hypercall */
69 	rax = 0;
70 	rcx = HVCALL_SIGNAL_EVENT;	/* code */
71 	rdx = 0x5a5a5a5a;		/* ingpa (badly aligned) */
72 	__asm__ __volatile__("call *%1" : "=a"(rax) :
73 			     "r"(HCALL_REGION_GPA + PAGE_SIZE),
74 			     "a"(rax), "c"(rcx), "d"(rdx),
75 			     "r"(r8));
76 	GUEST_ASSERT(rax == HV_STATUS_INVALID_ALIGNMENT);
77 
78 	GUEST_DONE();
79 }
80 
81 int main(int argc, char *argv[])
82 {
83 	unsigned int xen_caps;
84 	struct kvm_vcpu *vcpu;
85 	struct kvm_vm *vm;
86 
87 	xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
88 	TEST_REQUIRE(xen_caps & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
89 
90 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
91 	vcpu_set_hv_cpuid(vcpu);
92 
93 	struct kvm_xen_hvm_config hvmc = {
94 		.flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
95 		.msr = XEN_HYPERCALL_MSR,
96 	};
97 	vm_ioctl(vm, KVM_XEN_HVM_CONFIG, &hvmc);
98 
99 	/* Map a region for the hypercall pages */
100 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
101 				    HCALL_REGION_GPA, HCALL_REGION_SLOT, 2, 0);
102 	virt_map(vm, HCALL_REGION_GPA, HCALL_REGION_GPA, 2);
103 
104 	for (;;) {
105 		volatile struct kvm_run *run = vcpu->run;
106 		struct ucall uc;
107 
108 		vcpu_run(vcpu);
109 
110 		if (run->exit_reason == KVM_EXIT_XEN) {
111 			ASSERT_EQ(run->xen.type, KVM_EXIT_XEN_HCALL);
112 			ASSERT_EQ(run->xen.u.hcall.cpl, 0);
113 			ASSERT_EQ(run->xen.u.hcall.longmode, 1);
114 			ASSERT_EQ(run->xen.u.hcall.input, INPUTVALUE);
115 			ASSERT_EQ(run->xen.u.hcall.params[0], ARGVALUE(1));
116 			ASSERT_EQ(run->xen.u.hcall.params[1], ARGVALUE(2));
117 			ASSERT_EQ(run->xen.u.hcall.params[2], ARGVALUE(3));
118 			ASSERT_EQ(run->xen.u.hcall.params[3], ARGVALUE(4));
119 			ASSERT_EQ(run->xen.u.hcall.params[4], ARGVALUE(5));
120 			ASSERT_EQ(run->xen.u.hcall.params[5], ARGVALUE(6));
121 			run->xen.u.hcall.result = RETVALUE;
122 			continue;
123 		}
124 
125 		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
126 
127 		switch (get_ucall(vcpu, &uc)) {
128 		case UCALL_ABORT:
129 			REPORT_GUEST_ASSERT(uc);
130 			/* NOT REACHED */
131 		case UCALL_SYNC:
132 			break;
133 		case UCALL_DONE:
134 			goto done;
135 		default:
136 			TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
137 		}
138 	}
139 done:
140 	kvm_vm_free(vm);
141 	return 0;
142 }
143