From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 77825C5320E for ; Tue, 20 Aug 2024 23:21:58 +0000 (UTC) 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:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ZFv4/lTB7hWWUR8qTRgRDRlnxz+9mDXBuPG74UtV2c0=; b=uCHhvC/uh3ub3cn8WYrCMMytrj +RsU/n9ugMjtJihAjY3Izyw8tVfdYw7/QVTNNgSKjFbXantW6Jwj7+pRKpRNB6+zScXfRY+xvm4W5 n7o9F5M/IFmP+ggvy8HYEE6OEvDo2JUJLjfj/ljCrhWiv2xCodXoACCLHMbfPaWSs4MN8StoEFGqs ajVwcPmLkhZ/SCvJky+8YnyPmWdCGFZqrHBjVVjvCBV/cBCrS6j+6AJ/OHo0xJkXJQ8IHQLg2Lyjf ceTn8Kc23N++oOUPjk+TwnLuF0JROfkT3W4lUsaw1TsfWxqygnX6dgbxTqEA57H4xhVqtsu5WCAFk L9apdvhw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgY9L-00000006uSU-2PPA; Tue, 20 Aug 2024 23:20:27 +0000 Received: from out-187.mta0.migadu.com ([2001:41d0:1004:224b::bb]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgY8b-00000006uJ3-1S1Q for linux-arm-kernel@lists.infradead.org; Tue, 20 Aug 2024 23:19:43 +0000 Date: Tue, 20 Aug 2024 16:19:32 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1724195978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ZFv4/lTB7hWWUR8qTRgRDRlnxz+9mDXBuPG74UtV2c0=; b=A9MbO1InIwMxbHX/UFUHhwlJcNymIYOopTnsIdSepqXa4GhFYrpsaWTu75wyT7ZkwdQi8T vS1stf9+XzmrBxWq4IJG/EjyR34XnqERvv/l/vy4Klba/Nqtmwru3/bvWvW8SpH1QP34tY 8KCrDL5gZlWVz/rKaEyM6FKs1lJUg+8= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Marc Zyngier Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, James Morse , Suzuki K Poulose , Zenghui Yu , Alexander Potapenko Subject: Re: [PATCH 03/12] KVM: arm64: Force SRE traps when SRE access is not enabled Message-ID: References: <20240820100349.3544850-1-maz@kernel.org> <20240820100349.3544850-4-maz@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240820100349.3544850-4-maz@kernel.org> X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240820_161942_195461_5C711C89 X-CRM114-Status: GOOD ( 34.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hey, On Tue, Aug 20, 2024 at 11:03:40AM +0100, Marc Zyngier wrote: > We so far only write the ICH_HCR_EL2 config in two situations: > > - when we need to emulate the GICv3 CPU interface due to HW bugs > > - when we do direct injection, as the virtual CPU interface needs > to be enabled > > This is all good. But it also means that we don't do anything special > when we emulate a GICv2, or that there is no GIC at all. > > What happens in this case when the guest uses the GICv3 system > registers? The *guest* gets a trap for a sysreg access (EC=0x18) > while we'd really like it to get an UNDEF. > > Fixing this is a bit involved: > > - we need to set all the required trap bits (TC, TALL0, TALL1, TDIR) > > - for these traps to take effect, we need to (counter-intuitively) > set ICC_SRE_EL1.SRE to 1 so that the above traps take priority. > > Note that doesn't fully work when GICv2 emulation is enabled, as > we cannot set ICC_SRE_EL1.SRE to 1 (it breaks Group0 delivery as > IRQ). Just to make sure I'm following completely, GICv2-on-GICv3 guest sees the (barf) architected behavior of sysreg traps going to EL1. > Signed-off-by: Marc Zyngier > --- > arch/arm64/kvm/hyp/vgic-v3-sr.c | 22 ++++++++++++++++------ > arch/arm64/kvm/vgic/vgic-v3.c | 5 ++++- > 2 files changed, 20 insertions(+), 7 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/vgic-v3-sr.c b/arch/arm64/kvm/hyp/vgic-v3-sr.c > index 7b397fad26f2..a184def8f5ad 100644 > --- a/arch/arm64/kvm/hyp/vgic-v3-sr.c > +++ b/arch/arm64/kvm/hyp/vgic-v3-sr.c > @@ -268,8 +268,16 @@ void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if) > * starting to mess with the rest of the GIC, and VMCR_EL2 in > * particular. This logic must be called before > * __vgic_v3_restore_state(). > + * > + * However, if the vgic is disabled (ICH_HCR_EL2.EN==0), no GIC is > + * provisionned at all. In order to prevent illegal accesses to the typo: provisioned > + * system registers to trap to EL1 (duh), force ICC_SRE_EL1.SRE to 1 > + * so that the trap bits can take effect. Yes, we *loves* the GIC. > */ > - if (!cpu_if->vgic_sre) { > + if (!(cpu_if->vgic_hcr & ICH_HCR_EN)) { > + write_gicreg(ICC_SRE_EL1_SRE, ICC_SRE_EL1); > + isb(); > + } else if (!cpu_if->vgic_sre) { > write_gicreg(0, ICC_SRE_EL1); > isb(); > write_gicreg(cpu_if->vgic_vmcr, ICH_VMCR_EL2); > @@ -288,8 +296,9 @@ void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if) > } > > /* > - * Prevent the guest from touching the GIC system registers if > - * SRE isn't enabled for GICv3 emulation. > + * Prevent the guest from touching the ICC_SRE_EL1 system > + * register. Note that this may not have any effect, as > + * ICC_SRE_EL2.Enable being RAO/WI is a valid implementation. So this behavior is weird but still 'safe' as, ICC_SRE_EL1.SRE is also RAO/WI and the HCR traps are still effective. Right? > */ > write_gicreg(read_gicreg(ICC_SRE_EL2) & ~ICC_SRE_EL2_ENABLE, > ICC_SRE_EL2); > @@ -297,10 +306,11 @@ void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if) > /* > * If we need to trap system registers, we must write > * ICH_HCR_EL2 anyway, even if no interrupts are being > - * injected, > + * injected. Note that this also applies if we don't expect > + * any system register access (GICv2 or no vgic at all). We don't expect the traps to come in the GICv2 case, though, right? Looks alright to me otherwise, but blech! -- Thanks, Oliver