Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020, Google LLC.
0004  *
0005  * Tests for KVM paravirtual feature disablement
0006  */
0007 #include <asm/kvm_para.h>
0008 #include <linux/kvm_para.h>
0009 #include <linux/stringify.h>
0010 #include <stdint.h>
0011 
0012 #include "apic.h"
0013 #include "test_util.h"
0014 #include "kvm_util.h"
0015 #include "processor.h"
0016 
0017 /* VMCALL and VMMCALL are both 3-byte opcodes. */
0018 #define HYPERCALL_INSN_SIZE 3
0019 
0020 static bool ud_expected;
0021 
0022 static void guest_ud_handler(struct ex_regs *regs)
0023 {
0024     GUEST_ASSERT(ud_expected);
0025     GUEST_DONE();
0026 }
0027 
0028 extern uint8_t svm_hypercall_insn[HYPERCALL_INSN_SIZE];
0029 static uint64_t svm_do_sched_yield(uint8_t apic_id)
0030 {
0031     uint64_t ret;
0032 
0033     asm volatile("mov %1, %%rax\n\t"
0034              "mov %2, %%rbx\n\t"
0035              "svm_hypercall_insn:\n\t"
0036              "vmmcall\n\t"
0037              "mov %%rax, %0\n\t"
0038              : "=r"(ret)
0039              : "r"((uint64_t)KVM_HC_SCHED_YIELD), "r"((uint64_t)apic_id)
0040              : "rax", "rbx", "memory");
0041 
0042     return ret;
0043 }
0044 
0045 extern uint8_t vmx_hypercall_insn[HYPERCALL_INSN_SIZE];
0046 static uint64_t vmx_do_sched_yield(uint8_t apic_id)
0047 {
0048     uint64_t ret;
0049 
0050     asm volatile("mov %1, %%rax\n\t"
0051              "mov %2, %%rbx\n\t"
0052              "vmx_hypercall_insn:\n\t"
0053              "vmcall\n\t"
0054              "mov %%rax, %0\n\t"
0055              : "=r"(ret)
0056              : "r"((uint64_t)KVM_HC_SCHED_YIELD), "r"((uint64_t)apic_id)
0057              : "rax", "rbx", "memory");
0058 
0059     return ret;
0060 }
0061 
0062 static void guest_main(void)
0063 {
0064     uint8_t *native_hypercall_insn, *hypercall_insn;
0065     uint8_t apic_id;
0066 
0067     apic_id = GET_APIC_ID_FIELD(xapic_read_reg(APIC_ID));
0068 
0069     if (is_intel_cpu()) {
0070         native_hypercall_insn = vmx_hypercall_insn;
0071         hypercall_insn = svm_hypercall_insn;
0072         svm_do_sched_yield(apic_id);
0073     } else if (is_amd_cpu()) {
0074         native_hypercall_insn = svm_hypercall_insn;
0075         hypercall_insn = vmx_hypercall_insn;
0076         vmx_do_sched_yield(apic_id);
0077     } else {
0078         GUEST_ASSERT(0);
0079         /* unreachable */
0080         return;
0081     }
0082 
0083     /*
0084      * The hypercall didn't #UD (guest_ud_handler() signals "done" if a #UD
0085      * occurs).  Verify that a #UD is NOT expected and that KVM patched in
0086      * the native hypercall.
0087      */
0088     GUEST_ASSERT(!ud_expected);
0089     GUEST_ASSERT(!memcmp(native_hypercall_insn, hypercall_insn, HYPERCALL_INSN_SIZE));
0090     GUEST_DONE();
0091 }
0092 
0093 static void setup_ud_vector(struct kvm_vcpu *vcpu)
0094 {
0095     vm_init_descriptor_tables(vcpu->vm);
0096     vcpu_init_descriptor_tables(vcpu);
0097     vm_install_exception_handler(vcpu->vm, UD_VECTOR, guest_ud_handler);
0098 }
0099 
0100 static void enter_guest(struct kvm_vcpu *vcpu)
0101 {
0102     struct kvm_run *run = vcpu->run;
0103     struct ucall uc;
0104 
0105     vcpu_run(vcpu);
0106     switch (get_ucall(vcpu, &uc)) {
0107     case UCALL_SYNC:
0108         pr_info("%s: %016lx\n", (const char *)uc.args[2], uc.args[3]);
0109         break;
0110     case UCALL_DONE:
0111         return;
0112     case UCALL_ABORT:
0113         REPORT_GUEST_ASSERT(uc);
0114     default:
0115         TEST_FAIL("Unhandled ucall: %ld\nexit_reason: %u (%s)",
0116               uc.cmd, run->exit_reason, exit_reason_str(run->exit_reason));
0117     }
0118 }
0119 
0120 static void test_fix_hypercall(void)
0121 {
0122     struct kvm_vcpu *vcpu;
0123     struct kvm_vm *vm;
0124 
0125     vm = vm_create_with_one_vcpu(&vcpu, guest_main);
0126     setup_ud_vector(vcpu);
0127 
0128     ud_expected = false;
0129     sync_global_to_guest(vm, ud_expected);
0130 
0131     virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
0132 
0133     enter_guest(vcpu);
0134 }
0135 
0136 static void test_fix_hypercall_disabled(void)
0137 {
0138     struct kvm_vcpu *vcpu;
0139     struct kvm_vm *vm;
0140 
0141     vm = vm_create_with_one_vcpu(&vcpu, guest_main);
0142     setup_ud_vector(vcpu);
0143 
0144     vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2,
0145               KVM_X86_QUIRK_FIX_HYPERCALL_INSN);
0146 
0147     ud_expected = true;
0148     sync_global_to_guest(vm, ud_expected);
0149 
0150     virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
0151 
0152     enter_guest(vcpu);
0153 }
0154 
0155 int main(void)
0156 {
0157     TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_FIX_HYPERCALL_INSN);
0158 
0159     test_fix_hypercall();
0160     test_fix_hypercall_disabled();
0161 }