From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796057051; b=Xt2/EJeHKG3VNNYRjqehNNoImUCt0Xnrg6uZjZtGoXaIDhSh2InLAVK/yz2B9L5U25uy kpf8bhA9y69eGkHRhxXrFIhhiaY6FwjOgPIMCo/VJrSlrMHPSYyJx5UtSVNbg5kCTf/Lh 1bIaIw8nb3sXJ/1CgUeSjwqStn54vyNgkrqL8K07d3+0mMW39eK9btEAmCw2yzwTMMeFs q3LDeQvt4Yv8UHKv9wMk08Np88XmJjyPFvyfTRrhkGwA2MHYAorB3enCGSBv+J8T/PYQt jAgThfyp2TPvcY82IZ31XahcPHRCS9aeiQrq8NhEoPAHPcV/x8g2uMADvw7ZrfBpwpA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796057051; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=5Wi7njAo9rGDjTkU+AIMOQ68aYGLtwviHQ89Lv18Mzo=; b=po1r8bBr6yY/DGS/8+f70B9EJNwWlkNiy5f6vHRsXUouXDHRFyvVxvB4l9g4o0OxkycS iS/sc2zspSYU3hZZQ5Em3UxP6Mn6j0xhoGlOX3xTJmZJdMFG9NOeO9I4SJ5Qxdii+9gaX w03rwbjaCHPiaT9K648zzZOKHIrnfYcntVjSvDudVO4MVDs222h/AwcVOqUCgl6dw2xtt uei5EsPTyy59YTerYc+zcL0v2EaKb8ie/gdbgUABDbN8/VrPX080HJ7qzOV/JOKm39BVp m9PGtvfxHw0OliCA20f/mkQYNjKtRwaFolWYuEcl+p3f20k5b58CM3DtDg6kRuMZ9+A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QBIU6x1W; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C15CF606A335; Mon, 30 Nov 2026 16:44:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QBIU6x1W Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87A351F00925; Mon, 30 Nov 2026 16:44:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796057051; bh=5Wi7njAo9rGDjTkU+AIMOQ68aYGLtwviHQ89Lv18Mzo=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=QBIU6x1W0xeAvMmLdE9gS1RFHeJfpT31FESyr3Bcnxt9z97S5o2/Dwava2GfuYYe5 KDa+/yHniyERkxPBN8S0vDIFkTG6KKeR1wyq4+U1LMYVAPMbpXFnz+Gv86pxHwtflS XcjQLhg2a5c5Vi2IjH/fAECLZZL99wTc9K+3BPLhN7a2d52J08NHCcIdRUWzMnr8Qx 4p712EBeflFz979QvLuNwDne/jEnBMgjnpQqq1MICFc+wQ7idhQWiZD01W3qEtWz9X lhpfMu4MXzb8knEb3xfRgF7lRf2v1ebflyfEUrYwwpJtQ7GJg+kv25iosZ3WK7t1Kz RN5sUy15jS/wg== From: Mark Brown Date: Mon, 30 Nov 2026 16:35:57 +0000 Subject: [PATCH v22 16/16] KVM: selftests: arm64: Add GCS EXLOCK exception emulation test X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-arm64-gcs-v22-16-80454edf47ea@kernel.org> References: <20261130-arm64-gcs-v22-0-80454edf47ea@kernel.org> In-Reply-To: <20261130-arm64-gcs-v22-0-80454edf47ea@kernel.org> To: Catalin Marinas , Will Deacon , Marc Zyngier , Joey Gouly , Suzuki K Poulose , Shuah Khan , Oliver Upton , Fuad Tabba Cc: Peter Maydell , Leonardo Bras , Wei-Lin Chang , Yao Yuan , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, kvmarm@lists.linux.dev, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Mark Brown X-Mailer: b4 0.17-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=17308; i=broonie@kernel.org; h=from:subject:message-id; bh=SQEmc0JKJQBfnBWk9fI+5nP7juwhFJ/d/6VPQVHnVvg=; b=owGbwMvMwCWm/754w+YLFp2Mp9WSGLJ5l3ekM56dtjjhAeupaKZJAsu3fJdsPK9eZNS1U4Z55 mzfxOqCjlIWBjEuBlkxRZa4LO6X9SWv1/AdXNgGM4eVCWQIAxenAExk/1+G/w4dLE2P24zXH/7x v3br+8jTZ+J3ShcYsy2scmZzcdv88QrDP+WoDpHbzIYfK2S0fJdPe536Xm9+U0rZhP8ak9T5/a+ +YAEA X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB When running in vEL2 ERET is emulated, this means that we must emulate the behaviour of GCSCR_EL2.EXLOCKEN. As per DDI0487 R_WTXBY PSTATE.EXLOCK is set to the value of GCSCR_ELx.EXLOCKEN when taking an exception to the same exception level. As per DDI0487 R_TYTWB if on exception return to the same EL, GCSCR_ELx.EXLOCKEN is 1 and PSTATE.EXLOCK is not 1 then the exception return is illegal, and as per the pseudocode for the registers writes to SPSR_ELx and ELR_ELx will generate exceptions. Add a test that validates that we set PSTATE.EXLOCK, checking that behaviour is seen by vEL2 consistent with it being set. We nest handling of two exceptions to allow us to both observe and modify PSTATE.EXLOCK, checking that expected exceptions are generated for ERET. In order to cover both hardware programming and emulation the first exception is done with both UDF (for hardware) and HVC (for emulation). We also attempt to write to both SPSR_ELx and ELR_ELx, again using a UDF or HVC handler to control PSTATE.EXLOCK. We do all this both with and without having enabled EXLOCKEN, though only for code running at EL2 due to framework limitations. For completeness we also inject a range of exceptions with GCSCR_ELx.EXLOCKEN set, ensuring that PSTATE.EXLOCK is set appropriately. Signed-off-by: Mark Brown --- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/arm64/gcs_exceptions.c | 500 +++++++++++++++++++++ 2 files changed, 501 insertions(+) diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index 283c59d7649c..d6e7d049ff99 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -186,6 +186,7 @@ TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test TEST_GEN_PROGS_arm64 += arm64/external_aborts +TEST_GEN_PROGS_arm64 += arm64/gcs_exceptions TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext TEST_GEN_PROGS_arm64 += arm64/page_fault_test TEST_GEN_PROGS_arm64 += arm64/psci_test diff --git a/tools/testing/selftests/kvm/arm64/gcs_exceptions.c b/tools/testing/selftests/kvm/arm64/gcs_exceptions.c new file mode 100644 index 000000000000..bffd913e7530 --- /dev/null +++ b/tools/testing/selftests/kvm/arm64/gcs_exceptions.c @@ -0,0 +1,500 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test KVM's handling of Guarded Control Stack exception state. + */ + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#include +#include + +#define PSTATE_IL_BIT BIT(20) + +static void require_guest_gcs(struct kvm_vcpu *vcpu) +{ + u64 pfr1 = vcpu_get_reg(vcpu, + KVM_ARM64_SYS_REG(SYS_ID_AA64PFR1_EL1)); + + TEST_REQUIRE(SYS_FIELD_GET(ID_AA64PFR1_EL1, GCS, pfr1) >= + ID_AA64PFR1_EL1_GCS_IMP); +} + +static bool expect_exlock; +static unsigned int undef_count; +static unsigned int hvc_count; +static unsigned int illegal_count; +static unsigned int gcs_count; +static u64 handler_pstate; +static u64 illegal_pstate; + +static u64 expected_illegal_pc; +static enum udf_mode { + UDF_COLLECT, /* Initial UDF we collect PSTATE from */ + UDF_VALIDATE, /* Secondary UDF that collects and validates */ + UDF_REPAIR, /* UDF to clear EXLOCK */ + UDF_ELR_EL1, /* Write to ELR_EL1 with EXLOCK */ + UDF_ELR_EL2, /* Write to ELR_EL2 with EXLOCK */ + UDF_SPSR_EL1, /* Write to SPSR_EL1 with EXLOCK */ + UDF_SPSR_EL2, /* Write to SPSR_EL2 with EXLOCK */ +} udf_mode; + +/* + * A UDF at vEL2 is taken by the CPU without involving the host, the + * host only sees the ERET from the handler. A HVC traps to the host + * which then emulates taking the exception to vEL2, so it is KVM that + * has to set PSTATE.EXLOCK for the handler. + */ +static void take_exception(bool hvc) +{ + if (hvc) + asm volatile("hvc #0" ::: "memory"); + else + asm volatile("udf #0" ::: "memory"); +} + +static void guest_exception_handler(struct ex_regs *regs) +{ + u64 esr = read_sysreg(esr_el2); + u64 val; + + switch (ESR_ELx_EC(esr)) { + case ESR_ELx_EC_UNKNOWN: + undef_count++; + + /* Just skip the instruction either way. */ + GUEST_ASSERT_EQ(esr, ESR_ELx_IL); + regs->pc += 4; + break; + + case ESR_ELx_EC_HVC64: + hvc_count++; + + /* We return to the instruction after the HVC, don't skip. */ + GUEST_ASSERT_EQ(esr, (ESR_ELx_EC_HVC64 << ESR_ELx_EC_SHIFT) | + ESR_ELx_IL); + break; + + default: + GUEST_FAIL("Unexpected exception, ESR %#lx", esr); + } + + switch (udf_mode) { + case UDF_COLLECT: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + expect_exlock ? GCSCR_ELx_EXLOCKEN : 0); + + /* + * If we want the nested exception handler to change + * PSTATE it can't have EXLOCK set. + */ + write_sysreg_s(0, SYS_GCSCR_EL2); + isb(); + + /* + * Take another exception without returning so we can + * collect the EXLOCK state we're seeing here and make + * our return illegal. + */ + expected_illegal_pc = regs->pc; + udf_mode = UDF_VALIDATE; + asm volatile("udf #0" ::: "memory"); + + /* Reenable so PSTATE.EXLOCK is checked */ + write_sysreg_s(expect_exlock ? GCSCR_ELx_EXLOCKEN : 0, + SYS_GCSCR_EL2); + isb(); + break; + + case UDF_VALIDATE: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + 0); + + /* Validate the EXLOCK of the original exception. */ + handler_pstate = regs->pstate; + GUEST_ASSERT_EQ(!!(regs->pstate & PSR_EXLOCK_BIT), + expect_exlock); + + /* Clearing PSTATE.EXLOCK triggers an illegal ERET. */ + regs->pstate &= ~PSR_EXLOCK_BIT; + break; + + case UDF_REPAIR: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + 0); + + /* Disable EXLOCK for the requesting context */ + regs->pstate &= ~PSR_EXLOCK_BIT; + break; + + case UDF_ELR_EL1: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + expect_exlock ? GCSCR_ELx_EXLOCKEN : 0); + + asm volatile("mrs %0, elr_el1 \n\ + msr elr_el1, %0" + : "=r"(val) :: "memory"); + break; + + case UDF_ELR_EL2: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + expect_exlock ? GCSCR_ELx_EXLOCKEN : 0); + + asm volatile("mrs %0, elr_el2 \n\ + msr elr_el2, %0" + : "=r"(val) :: "memory"); + break; + + case UDF_SPSR_EL1: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + expect_exlock ? GCSCR_ELx_EXLOCKEN : 0); + + asm volatile("mrs %0, spsr_el1 \n\ + msr spsr_el1, %0" + : "=r"(val) :: "memory"); + break; + + case UDF_SPSR_EL2: + GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN, + expect_exlock ? GCSCR_ELx_EXLOCKEN : 0); + + asm volatile("mrs %0, spsr_el2 \n\ + msr spsr_el2, %0" + : "=r"(val) :: "memory"); + break; + + default: + GUEST_FAIL("Invalid udf_mode"); + } +} + +static void guest_illegal_handler(struct ex_regs *regs) +{ + u64 esr = read_sysreg(esr_el2); + + illegal_count++; + illegal_pstate = regs->pstate; + + GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_ILL); + GUEST_ASSERT(esr & ESR_ELx_IL); + GUEST_ASSERT(regs->pstate & PSTATE_IL_BIT); + GUEST_ASSERT(!(regs->pstate & PSR_EXLOCK_BIT)); + GUEST_ASSERT_EQ(regs->pc, expected_illegal_pc); + + /* Disable exception locking so we can repair the return state. */ + write_sysreg_s(0, SYS_GCSCR_EL2); + isb(); + + /* We need our own EXLOCK clearing so we can repair the original */ + udf_mode = UDF_REPAIR; + asm volatile("udf #0" ::: "memory"); + + /* Disable EXLOCK for the generating context, making things legal */ + regs->pstate &= ~(PSTATE_IL_BIT | PSR_EXLOCK_BIT); +} + +static void guest_gcs_handler(struct ex_regs *regs) +{ + u64 esr = read_sysreg(esr_el2); + + gcs_count++; + + GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_GCS); + GUEST_ASSERT_EQ(FIELD_GET(ESR_ELx_ExType_MASK, esr), + ESR_ELx_ExType_EXLOCK); + + /* Disabling EXLOCKEN makes sysreg writes legal. */ + write_sysreg_s(0, SYS_GCSCR_EL2); + isb(); +} + +static noinline void test_exception(bool hvc, bool enable_exlock) +{ + unsigned int initial_undef = undef_count; + unsigned int initial_hvc = hvc_count; + unsigned int initial_illegal = illegal_count; + unsigned int initial_gcs = gcs_count; + + expect_exlock = enable_exlock; + write_sysreg_s(enable_exlock ? GCSCR_ELx_EXLOCKEN : 0, + SYS_GCSCR_EL2); + isb(); + + udf_mode = UDF_COLLECT; + take_exception(hvc); + + /* Leave GCS disabled even if a broken ERET skipped the ILL handler. */ + write_sysreg_s(0, SYS_GCSCR_EL2); + isb(); + + /* The nested exceptions are always UDFs */ + GUEST_ASSERT_EQ(hvc_count, initial_hvc + hvc); + GUEST_ASSERT_EQ(undef_count, initial_undef + !hvc + 1 + enable_exlock); + GUEST_ASSERT_EQ(illegal_count, initial_illegal + enable_exlock); + GUEST_ASSERT_EQ(!!(handler_pstate & PSR_EXLOCK_BIT), enable_exlock); + if (enable_exlock) + GUEST_ASSERT(illegal_pstate & PSTATE_IL_BIT); + GUEST_ASSERT_EQ(initial_gcs, gcs_count); +} + +static noinline void test_sysreg(bool hvc, enum udf_mode reg, + bool enable_exlock) +{ + unsigned int initial_undef = undef_count; + unsigned int initial_hvc = hvc_count; + unsigned int initial_gcs = gcs_count; + + expect_exlock = enable_exlock; + + write_sysreg_s(enable_exlock ? GCSCR_ELx_EXLOCKEN : 0, + SYS_GCSCR_EL2); + isb(); + + udf_mode = reg; + take_exception(hvc); + + /* Ensure EXLOCK is disabled */ + write_sysreg_s(0, SYS_GCSCR_EL2); + isb(); + + GUEST_ASSERT_EQ(gcs_count, initial_gcs + expect_exlock); + GUEST_ASSERT_EQ(undef_count, initial_undef + !hvc); + GUEST_ASSERT_EQ(hvc_count, initial_hvc + hvc); +} + +static void test_exceptions(bool hvc) +{ + /* + * Try straightforwardly generating an exception without and + * with EXLOCK. + */ + test_exception(hvc, false); + test_sysreg(hvc, UDF_ELR_EL1, false); + test_sysreg(hvc, UDF_ELR_EL2, false); + test_sysreg(hvc, UDF_SPSR_EL1, false); + test_sysreg(hvc, UDF_SPSR_EL2, false); + + test_exception(hvc, true); + test_sysreg(hvc, UDF_ELR_EL1, true); + test_sysreg(hvc, UDF_ELR_EL2, true); + test_sysreg(hvc, UDF_SPSR_EL1, true); + test_sysreg(hvc, UDF_SPSR_EL2, true); +} + +static void guest_code(void) +{ + GUEST_ASSERT_EQ(get_current_el(), 2); + + /* Exceptions taken to vEL2 by the CPU */ + test_exceptions(false); + + /* Exceptions injected into vEL2 by KVM */ + test_exceptions(true); + + GUEST_DONE(); +} + +static void test_exception_from_same_el(void) +{ + struct kvm_vcpu_init init; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + vm = vm_create(1); + kvm_get_default_vcpu_target(vm, &init); + init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2); + vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code); + require_guest_gcs(vcpu); + + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vcpu); + vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT, + ESR_ELx_EC_UNKNOWN, guest_exception_handler); + vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT, + ESR_ELx_EC_HVC64, guest_exception_handler); + vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT, + ESR_ELx_EC_ILL, guest_illegal_handler); + vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT, + ESR_ELx_EC_GCS, guest_gcs_handler); + kvm_arch_vm_finalize_vcpus(vm); + + vcpu_run(vcpu); + switch (get_ucall(vcpu, &uc)) { + case UCALL_DONE: + break; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + default: + TEST_FAIL("Unhandled ucall: %ld", uc.cmd); + } + + kvm_vm_free(vm); +} + +/* + * The host injects an external abort on an MMIO exit. When the + * exception is taken to the EL it came from PSTATE.EXLOCK should be set + * to the EXLOCKEN for that EL, when it is taken from a lower EL it + * should be cleared. Either way the old value should be preserved in + * the SPSR. Where only one of the ELs is involved the EXLOCKEN for + * the other is given the opposite value in order to check that the + * right one is used. + */ +static const struct inject_test { + const char *name; + u64 pstate; + bool tge; + u64 gcscr_el1; + u64 gcscr_el2; + unsigned int target_mode; + bool exlock; +} inject_tests[] = { + { + .name = "EL2h to EL2 with EXLOCKEN", + .pstate = PSR_MODE_EL2h, + .tge = true, + .gcscr_el2 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL2h, + .exlock = true, + }, + { + .name = "EL2h to EL2 without EXLOCKEN", + .pstate = PSR_MODE_EL2h | PSR_EXLOCK_BIT, + .tge = true, + .gcscr_el1 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL2h, + .exlock = false, + }, + { + .name = "EL2t to EL2 with EXLOCKEN", + .pstate = PSR_MODE_EL2t, + .tge = true, + .gcscr_el2 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL2h, + .exlock = true, + }, + { + .name = "EL1h to EL1 with EXLOCKEN", + .pstate = PSR_MODE_EL1h, + .gcscr_el1 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL1h, + .exlock = true, + }, + { + .name = "EL1h to EL1 without EXLOCKEN", + .pstate = PSR_MODE_EL1h | PSR_EXLOCK_BIT, + .gcscr_el2 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL1h, + .exlock = false, + }, + { + .name = "EL0 to EL1", + .pstate = PSR_MODE_EL0t | PSR_EXLOCK_BIT, + .gcscr_el1 = GCSCR_ELx_EXLOCKEN, + .gcscr_el2 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL1h, + .exlock = false, + }, + { + .name = "EL0 to EL2", + .pstate = PSR_MODE_EL0t | PSR_EXLOCK_BIT, + .tge = true, + .gcscr_el1 = GCSCR_ELx_EXLOCKEN, + .gcscr_el2 = GCSCR_ELx_EXLOCKEN, + .target_mode = PSR_MODE_EL2h, + .exlock = false, + }, +}; + +/* ldr x0, [x0] runs at any EL without a stack or BTI landing pad. */ +static const __le32 guest_mmio_read = cpu_to_le32(0xf9400000); + +static void test_injected_exception(const struct inject_test *test) +{ + struct kvm_vcpu_events events = {}; + struct kvm_vcpu_init init; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + u64 hcr, pstate, spsr; + gpa_t fault_pc, mmio_gpa; + + vm = vm_create(1); + kvm_get_default_vcpu_target(vm, &init); + init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2); + + /* Leave stage 1 disabled so every source EL can run the load by IPA. */ + vcpu = __vm_vcpu_add(vm, 0); + vcpu_ioctl(vcpu, KVM_ARM_VCPU_INIT, &init); + require_guest_gcs(vcpu); + kvm_arch_vm_finalize_vcpus(vm); + + /* + * With TGE set exceptions from EL0 go to vEL2, without it + * exceptions from EL0 and EL1 go to vEL1. + */ + hcr = HCR_EL2_RW | HCR_EL2_E2H; + if (test->tge) + hcr |= HCR_EL2_TGE; + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2), hcr); + + fault_pc = addr_gva2gpa(vm, (gva_t)&guest_mmio_read); + vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), fault_pc); + + /* Use the unbacked ucall page, past the address reserved for ucalls. */ + mmio_gpa = vm->ucall_mmio_addr + sizeof(u64); + vcpu_args_set(vcpu, 1, mmio_gpa); + + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_GCSCR_EL1), test->gcscr_el1); + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_GCSCR_EL2), test->gcscr_el2); + vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pstate), test->pstate); + + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO); + TEST_ASSERT_EQ(vcpu->run->mmio.phys_addr, mmio_gpa); + TEST_ASSERT_EQ(vcpu->run->mmio.len, sizeof(u64)); + TEST_ASSERT(!vcpu->run->mmio.is_write, "%s: expected an MMIO read", test->name); + TEST_ASSERT_EQ(vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc)), fault_pc); + pstate = vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pstate)); + TEST_ASSERT((pstate & (PSR_MODE_MASK | PSR_EXLOCK_BIT)) == test->pstate, + "%s: unexpected PSTATE %#lx at MMIO exit", test->name, pstate); + + events.exception.ext_dabt_pending = true; + vcpu_events_set(vcpu, &events); + + pstate = vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pstate)); + TEST_ASSERT((pstate & PSR_MODE_MASK) == test->target_mode, + "%s: exception taken to mode %#x, expected %#x", + test->name, (unsigned int)(pstate & PSR_MODE_MASK), + test->target_mode); + TEST_ASSERT(!!(pstate & PSR_EXLOCK_BIT) == test->exlock, + "%s: PSTATE.EXLOCK %s by the exception", + test->name, test->exlock ? "not set" : "not cleared"); + + if (test->target_mode == PSR_MODE_EL2h) + spsr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SPSR_EL2)); + else + spsr = vcpu_get_reg(vcpu, ARM64_CORE_REG(spsr[KVM_SPSR_EL1])); + TEST_ASSERT((spsr & PSR_MODE_MASK) == (test->pstate & PSR_MODE_MASK), + "%s: source mode not preserved in SPSR", test->name); + TEST_ASSERT((spsr & PSR_EXLOCK_BIT) == (test->pstate & PSR_EXLOCK_BIT), + "%s: PSTATE.EXLOCK not preserved in SPSR", test->name); + + kvm_vm_free(vm); +} + +int main(void) +{ + int i; + + TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2)); + test_exception_from_same_el(); + TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_INJECT_EXT_DABT)); + for (i = 0; i < ARRAY_SIZE(inject_tests); i++) + test_injected_exception(&inject_tests[i]); + + return 0; +} -- 2.47.3