Path: blob/master/tools/testing/selftests/kvm/x86/kvm_buslock_test.c
38245 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2024 Advanced Micro Devices, Inc.3*/4#include <linux/atomic.h>56#include "kvm_util.h"7#include "processor.h"8#include "svm_util.h"9#include "vmx.h"10#include "test_util.h"1112#define NR_BUS_LOCKS_PER_LEVEL 10013#define CACHE_LINE_SIZE 641415/*16* To generate a bus lock, carve out a buffer that precisely occupies two cache17* lines and perform an atomic access that splits the two lines.18*/19static u8 buffer[CACHE_LINE_SIZE * 2] __aligned(CACHE_LINE_SIZE);20static atomic_t *val = (void *)&buffer[CACHE_LINE_SIZE - (sizeof(*val) / 2)];2122static void guest_generate_buslocks(void)23{24for (int i = 0; i < NR_BUS_LOCKS_PER_LEVEL; i++)25atomic_inc(val);26}2728#define L2_GUEST_STACK_SIZE 642930static void l2_guest_code(void)31{32guest_generate_buslocks();33GUEST_DONE();34}3536static void l1_svm_code(struct svm_test_data *svm)37{38unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];39struct vmcb *vmcb = svm->vmcb;4041generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);42run_guest(vmcb, svm->vmcb_gpa);43}4445static void l1_vmx_code(struct vmx_pages *vmx)46{47unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];4849GUEST_ASSERT_EQ(prepare_for_vmx_operation(vmx), true);50GUEST_ASSERT_EQ(load_vmcs(vmx), true);5152prepare_vmcs(vmx, NULL, &l2_guest_stack[L2_GUEST_STACK_SIZE]);5354GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code));55GUEST_ASSERT(!vmlaunch());56}5758static void guest_code(void *test_data)59{60guest_generate_buslocks();6162if (this_cpu_has(X86_FEATURE_SVM))63l1_svm_code(test_data);64else if (this_cpu_has(X86_FEATURE_VMX))65l1_vmx_code(test_data);66else67GUEST_DONE();6869TEST_FAIL("L2 should have signaled 'done'");70}7172int main(int argc, char *argv[])73{74const bool has_nested = kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX);75vm_vaddr_t nested_test_data_gva;76struct kvm_vcpu *vcpu;77struct kvm_run *run;78struct kvm_vm *vm;79int i, bus_locks = 0;8081TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT));8283vm = vm_create(1);84vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT);85vcpu = vm_vcpu_add(vm, 0, guest_code);8687if (kvm_cpu_has(X86_FEATURE_SVM))88vcpu_alloc_svm(vm, &nested_test_data_gva);89else90vcpu_alloc_vmx(vm, &nested_test_data_gva);9192vcpu_args_set(vcpu, 1, nested_test_data_gva);9394run = vcpu->run;9596for (i = 0; i <= NR_BUS_LOCKS_PER_LEVEL * (1 + has_nested); i++) {97struct ucall uc;9899vcpu_run(vcpu);100101if (run->exit_reason == KVM_EXIT_IO) {102switch (get_ucall(vcpu, &uc)) {103case UCALL_ABORT:104REPORT_GUEST_ASSERT(uc);105goto done;106case UCALL_SYNC:107continue;108case UCALL_DONE:109goto done;110default:111TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);112}113}114115TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_X86_BUS_LOCK);116117/*118* Verify the counter is actually getting incremented, e.g. that119* KVM isn't skipping the instruction. On Intel, the exit is120* trap-like, i.e. the counter should already have been121* incremented. On AMD, it's fault-like, i.e. the counter will122* be incremented when the guest re-executes the instruction.123*/124sync_global_from_guest(vm, *val);125TEST_ASSERT_EQ(atomic_read(val), bus_locks + host_cpu_is_intel);126127bus_locks++;128}129TEST_FAIL("Didn't receive UCALL_DONE, took %u bus lock exits\n", bus_locks);130done:131TEST_ASSERT_EQ(i, bus_locks);132kvm_vm_free(vm);133return 0;134}135136137