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 B3F3AC3DA7F for ; Wed, 31 Jul 2024 15:44:03 +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:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID: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=kDp5KwDFdo3bisMO+fHGuVPkfeUNh6mw4FZP33DMZ4U=; b=Va30wmUV3uQ6XgRsK6/1vqBb6W 6TAfkJje9tGBC58ik8ksJZv+uVLDEM45fUiGAj3H0uoAE78s5V+ZhKWjbqNLMcuZOQX+6wR/4UX3/ WAWRSOyQ4XsDEaERIKMMCxp9a7fWuaRohL8VYFYKFb2XHK7nwiY0wSRbFtcJPo3PnM7qOpkohr3EZ bESG5stuhuza14MylbJcywswzjcvjlDVpOJinfwLTHIcznCXriq93AlZqgJLAHU6i00v7SbvzYdnb ixGd2NbQ0V6DkBK7Ux0A+ZFsROy4UMBc2pfrATej4Foc+QMloZe/9B6p2zWSvB+5DEkd8Lqn6qSQl 8oCk2oBw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sZBUV-00000001lR2-1v1h; Wed, 31 Jul 2024 15:43:51 +0000 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sZBU1-00000001lL9-3tjx for linux-arm-kernel@lists.infradead.org; Wed, 31 Jul 2024 15:43:23 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 4DA86CE16F5; Wed, 31 Jul 2024 15:43:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B29DC116B1; Wed, 31 Jul 2024 15:43:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722440599; bh=G896pcxF3uDoNw8ktN8pUWQyBBes3qJ3pESAiQmRU8w=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=MufYV+LbD+Av80LGLnUf3eChdaZo/HaZgSHMhUzlCPdgj8dWzhXAGWrhzMh/eMx2i WrimB6SqZ+pq0DySwdmHBgNZJYuRwNhrZVFjZEUhF9oo5TxG72XtpaPdCrqkrWqeDk zcw8Unnck/ljSr3gfsUWIErN5bReAp8k7bnu/ZfTsURRnXxKcZx91ns8+WVPCmA0cV JpLhr4LMhP2tnfBc/entEDrCtNiFTBUL3rnt5HUAuJXuouUMbFIKW/vw+o6w6L8S8B 92u2zx2nKsByRB1WfOdydUgZuOb76LY19Dt5LVHdpecH536paLdonU2qsX3ruXOis8 NyW6KoY2rAlHg== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1sZBTw-00H31o-UA; Wed, 31 Jul 2024 16:43:17 +0100 Date: Wed, 31 Jul 2024 16:43:16 +0100 Message-ID: <86r0b91sa3.wl-maz@kernel.org> From: Marc Zyngier To: Alexandru Elisei Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, James Morse , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Joey Gouly Subject: Re: [PATCH 10/12] KVM: arm64: nv: Add SW walker for AT S1 emulation In-Reply-To: References: <20240625133508.259829-1-maz@kernel.org> <20240708165800.1220065-1-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.3 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: alexandru.elisei@arm.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, joey.gouly@arm.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240731_084322_343360_A2D7C549 X-CRM114-Status: GOOD ( 35.72 ) 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 On Wed, 31 Jul 2024 15:33:25 +0100, Alexandru Elisei wrote: > > Hi Marc, > > On Mon, Jul 08, 2024 at 05:57:58PM +0100, Marc Zyngier wrote: > > In order to plug the brokenness of our current AT implementation, > > we need a SW walker that is going to... err.. walk the S1 tables > > and tell us what it finds. > > > > Of course, it builds on top of our S2 walker, and share similar > > concepts. The beauty of it is that since it uses kvm_read_guest(), > > it is able to bring back pages that have been otherwise evicted. > > > > This is then plugged in the two AT S1 emulation functions as > > a "slow path" fallback. I'm not sure it is that slow, but hey. > > > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/kvm/at.c | 538 ++++++++++++++++++++++++++++++++++++++++++-- > > 1 file changed, 520 insertions(+), 18 deletions(-) > > > > diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c > > index 71e3390b43b4c..8452273cbff6d 100644 > > --- a/arch/arm64/kvm/at.c > > +++ b/arch/arm64/kvm/at.c > > @@ -4,9 +4,305 @@ > > * Author: Jintack Lim > > */ > > > > +#include > > + > > +#include > > #include > > #include > > > > +struct s1_walk_info { > > + u64 baddr; > > + unsigned int max_oa_bits; > > + unsigned int pgshift; > > + unsigned int txsz; > > + int sl; > > + bool hpd; > > + bool be; > > + bool nvhe; > > + bool s2; > > +}; > > + > > +struct s1_walk_result { > > + union { > > + struct { > > + u64 desc; > > + u64 pa; > > + s8 level; > > + u8 APTable; > > + bool UXNTable; > > + bool PXNTable; > > + }; > > + struct { > > + u8 fst; > > + bool ptw; > > + bool s2; > > + }; > > + }; > > + bool failed; > > +}; > > + > > +static void fail_s1_walk(struct s1_walk_result *wr, u8 fst, bool ptw, bool s2) > > +{ > > + wr->fst = fst; > > + wr->ptw = ptw; > > + wr->s2 = s2; > > + wr->failed = true; > > +} > > + > > +#define S1_MMU_DISABLED (-127) > > + > > +static int setup_s1_walk(struct kvm_vcpu *vcpu, struct s1_walk_info *wi, > > + struct s1_walk_result *wr, const u64 va, const int el) > > +{ > > + u64 sctlr, tcr, tg, ps, ia_bits, ttbr; > > + unsigned int stride, x; > > + bool va55, tbi; > > + > > + wi->nvhe = el == 2 && !vcpu_el2_e2h_is_set(vcpu); > > + > > + va55 = va & BIT(55); > > + > > + if (wi->nvhe && va55) > > + goto addrsz; > > + > > + wi->s2 = el < 2 && (__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_VM); > > + > > + switch (el) { > > + case 1: > > + sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1); > > + tcr = vcpu_read_sys_reg(vcpu, TCR_EL1); > > + ttbr = (va55 ? > > + vcpu_read_sys_reg(vcpu, TTBR1_EL1) : > > + vcpu_read_sys_reg(vcpu, TTBR0_EL1)); > > + break; > > + case 2: > > + sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL2); > > + tcr = vcpu_read_sys_reg(vcpu, TCR_EL2); > > + ttbr = (va55 ? > > + vcpu_read_sys_reg(vcpu, TTBR1_EL2) : > > + vcpu_read_sys_reg(vcpu, TTBR0_EL2)); > > + break; > > + default: > > + BUG(); > > + } > > + > > + /* Let's put the MMU disabled case aside immediately */ > > + if (!(sctlr & SCTLR_ELx_M) || > > + (__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_DC)) { > > + if (va >= BIT(kvm_get_pa_bits(vcpu->kvm))) > > As far as I can tell, if TBI, the pseudocode ignores bits 63:56 when checking > for out-of-bounds VA for the MMU disabled case (above) and the MMU enabled case > (below). That also matches the description of TBIx bits in the TCR_ELx > registers. Right. Then the check needs to be hoisted up and the VA sanitised before we compare it to anything. Thanks for all your review comments, but I am going to ask you to stop here. You are reviewing a pretty old code base, and although I'm sure you look at what is in my tree, I'd really like to post a new version for everyone to enjoy. I'll stash that last change on top and post the result. M. -- Without deviation from the norm, progress is not possible.