From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 195F1606A325 for ; Mon, 30 Nov 2026 16:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=MB8pVQYB; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mAI1t2Qb DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=5AcvRitNM+RG2srayCx8RMCRM12wXcLJ0qMGbyHIZro=; b=MB8pVQYB4aeHfc Kg4Xzq1AmTdBcxJn45MS0B/2f5WuNTBA4VHzhxaCdsOIJgf3JLuRsxCrqS1dE3WyB0FFDa87uVQcf mE75DkV4fEziTces1ounF24ANarh+u12ZF2ixrUjEda2LCaTSCDyIcliWeC4wPe86VsLInlrkT+k4 qstuGgGk9iKoao1kqY7uv1zK/r3uxA9X3Qr2aKWfg6xqTA/cAEdMQddaAHjv4jDDiuBOO7MvMaADC FnNjiEx4OgsAZqB5E92TvaqcY23BbXaorI+uUdd6sPGFvVADb8Tao+11hAp+hlarD3sVSZx/4D3CZ OdvqjcQ/1JRDdqJPok1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xY4TY-00000005IHE-3lBS; Mon, 30 Nov 2026 16:43:36 +0000 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xY4TP-00000005ICB-34nY for linux-arm-kernel@lists.infradead.org; Mon, 30 Nov 2026 16:43:27 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 30BB661AE2; Mon, 30 Nov 2026 16:43:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76DDE1F0092B; Mon, 30 Nov 2026 16:43:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796057006; bh=5AcvRitNM+RG2srayCx8RMCRM12wXcLJ0qMGbyHIZro=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=mAI1t2QbvhspdEX2eaXeFd95KotbVj6N2eOX7IAMNe1zv96R7Fu7Ga6EW4DWT2zf2 Da7/zbgdqjWM/+ks0mulse8ToELpVAXbE/hLOKmtt55YC+bYVw/AtLawA5HvGIIIU9 BhUvoH/VExA8+EkjwkHyHps39l7eLnSmmLEisGBgzf8iQ+DJu6HfnvXKUVwjy5X9rI Jbo3YjqF0OLBVMNlTCeutE9osOUxNv1oUMUdLG1wujUa7C085MiysEz1qzZBR9GeZ8 UV+0vzzD3w7t3ZxOq84oJKCfGTzjOCz28+fTpTAulzZ3ArnsvBLzO1BDnLI319ItrU po0j9a8LrO9FA== From: Mark Brown Date: Mon, 30 Nov 2026 16:35:47 +0000 Subject: [PATCH v22 06/16] KVM: arm64: Validate GCS exception lock when emulating ERET X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-arm64-gcs-v22-6-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=3901; i=broonie@kernel.org; h=from:subject:message-id; bh=jhUzeiiA3GEITP7XWSlxc7GeZs7fs4nWCglp+actjHw=; b=owGbwMvMwCWm/754w+YLFp2Mp9WSGLJ5l7f/fT17367OPer7Z/Nd+dHenjP/45stv7/dY/nTO 8tvV6U4W0cpC4MYF4OsmCJLXBb3y/qS12v4Di5sg5nDygQyhIGLUwAmwjCJkWG5wjOX3hkvCm9/ X50on1Cd+2Fd0qLLboz3pqj9N16T0vWJ4Z/W/GVCwmG2m1WVJL1vP+yYmq+7b5GDrWn1JDO9++w 3JJkA X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 As per DDI0487 R_TYTWB GCS adds an additional case where an illegal exception return can be generated. If all of: - PSTATE.EXLOCK is 0. - The EL is not being changed by the ERET. - GCSCR_ELx.EXLOCKEN is 1. are true then the return is illegal. Emulate this behaviour when emulating ERET for nested guests, while we're at it using the symbolic definition for EXLOCK in SPSR. Reviewed-by: Leonardo Bras Signed-off-by: Mark Brown --- arch/arm64/include/asm/kvm_nested.h | 32 ++++++++++++++++++++++++++++++++ arch/arm64/kvm/emulate-nested.c | 7 +++++-- arch/arm64/kvm/hyp/vhe/switch.c | 4 ++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h index 27ec73da02c6..a158de5f4f92 100644 --- a/arch/arm64/include/asm/kvm_nested.h +++ b/arch/arm64/include/asm/kvm_nested.h @@ -268,6 +268,38 @@ static inline bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr) } #endif +#ifdef CONFIG_ARM64_GCS +/* See IllegalExceptionReturn() pseudocode */ +static inline bool kvm_check_illegal_exlock_return(struct kvm_vcpu *vcpu, + u64 spsr) +{ + u64 pstate, cur_mode, target_mode; + + if (!kvm_has_gcs(vcpu->kvm)) + return false; + + if (vcpu->arch.ctxt.regs.pstate & PSR_EXLOCK_BIT) + return false; + + /* Check the EL only, ignore thread mode */ + pstate = vcpu->arch.ctxt.regs.pstate; + cur_mode = (pstate & PSR_MODE_MASK) | PSR_MODE_THREAD_BIT; + target_mode = (spsr & PSR_MODE_MASK) | PSR_MODE_THREAD_BIT; + + if (cur_mode != target_mode) + return false; + + return vcpu_read_sys_reg(vcpu, GCSCR_EL2) & GCSCR_ELx_EXLOCKEN; +} + +#else +static inline bool kvm_check_illegal_exlock_return(struct kvm_vcpu *vcpu, + u64 spsr) +{ + return false; +} +#endif + #define KVM_NV_GUEST_MAP_SZ (KVM_PGTABLE_PROT_SW1 | KVM_PGTABLE_PROT_SW0) static inline u64 kvm_encode_nested_level(struct kvm_s2_trans *trans) diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c index cb9bf3b9340a..c4a33f1f6ad4 100644 --- a/arch/arm64/kvm/emulate-nested.c +++ b/arch/arm64/kvm/emulate-nested.c @@ -2755,10 +2755,13 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr) * - trying to return to an illegal M value * - trying to return to a 32bit EL * - trying to return to EL1 with HCR_EL2.TGE set + * - GCSCR_ELx.EXLOCKEN is 1 and PSTATE.EXLOCK is 0 when attempting + * to return to the same EL. */ if (mode == PSR_MODE_EL3t || mode == PSR_MODE_EL3h || mode == 0b00001 || (mode & BIT(1)) || (spsr & PSR_MODE32_BIT) || + kvm_check_illegal_exlock_return(vcpu, spsr) || (vcpu_el2_tge_is_set(vcpu) && (mode == PSR_MODE_EL1t || mode == PSR_MODE_EL1h))) { u64 mask; @@ -2784,8 +2787,8 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr) spsr &= mask; mask = PSR_MODE_MASK | PSR_MODE32_BIT; - if (kvm_has_feat(vcpu->kvm, ID_AA64PFR1_EL1, GCS, IMP)) - mask |= BIT_ULL(34); /* PSTATE.EXLOCK */ + if (kvm_has_gcs(vcpu->kvm)) + mask |= PSR_EXLOCK_BIT; spsr |= *vcpu_cpsr(vcpu) & mask; spsr |= PSR_IL_BIT; diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c index 5f7a46c4f516..12e9762e1a86 100644 --- a/arch/arm64/kvm/hyp/vhe/switch.c +++ b/arch/arm64/kvm/hyp/vhe/switch.c @@ -395,6 +395,10 @@ static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code) return false; } + /* Push GCS exception lock failures into the slow path */ + if (kvm_check_illegal_exlock_return(vcpu, spsr)) + return false; + /* If ERETAx fails, take the slow path */ if (esr_iss_is_eretax(esr)) { if (!(vcpu_has_ptrauth(vcpu) && kvm_auth_eretax(vcpu, &elr))) -- 2.47.3