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