Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36390CA9EB9 for ; Sat, 26 Oct 2019 10:20:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ED3AF2070B for ; Sat, 26 Oct 2019 10:20:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572085246; bh=znNEH+wcQYhZhCNEzzdhkGeRmGpZHyEt56Hkwbg/cds=; h=Date:From:To:Cc:Subject:In-Reply-To:References:List-ID:From; b=KlinvojRl6WZ4LRsM2E+JUuogtniVtgnFRUd+TVCBw85YLflNZM1k9AB5CS1XMyC8 eEyoyR/R+kDP4T+YVRXsDQXd10DhqAfl+L7OM21ds9+/QKWflj01N8dc7OUgf8vK2M NDfEBMS/1QQfs3iM0vP2oAhWrWBVExAak2ZrLbs8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726139AbfJZKUo (ORCPT ); Sat, 26 Oct 2019 06:20:44 -0400 Received: from inca-roads.misterjones.org ([213.251.177.50]:34308 "EHLO inca-roads.misterjones.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726098AbfJZKUo (ORCPT ); Sat, 26 Oct 2019 06:20:44 -0400 Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=big-swifty.misterjones.org) by cheepnis.misterjones.org with esmtpsa (TLSv1.2:AES256-GCM-SHA384:256) (Exim 4.80) (envelope-from ) id 1iOJBU-0003Pg-TZ; Sat, 26 Oct 2019 12:20:37 +0200 Date: Sat, 26 Oct 2019 11:20:35 +0100 Message-ID: <8636ffzu30.wl-maz@kernel.org> From: Marc Zyngier To: James Morse , Catalin Marinas , Will Deacon Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, Mark Rutland , Suzuki K Poulose , Julien Thierry Subject: Re: [PATCH v2 4/5] arm64: KVM: Prevent speculative S1 PTW when restoring vcpu context In-Reply-To: <151fc868-6709-3017-e34d-649ec0e1812c@arm.com> References: <20191019095521.31722-1-maz@kernel.org> <20191019095521.31722-5-maz@kernel.org> <151fc868-6709-3017-e34d-649ec0e1812c@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 EasyPG/1.0.0 Emacs/26 (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: 62.31.163.78 X-SA-Exim-Rcpt-To: james.morse@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, mark.rutland@arm.com, suzuki.poulose@arm.com, julien.thierry.kdev@gmail.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on cheepnis.misterjones.org); SAEximRunCond expanded to false Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Thu, 24 Oct 2019 17:10:44 +0100, James Morse wrote: Hi James, > Hi Marc, > > On 19/10/2019 10:55, Marc Zyngier wrote: > > When handling erratum 1319367, we must ensure that the page table > > walker cannot parse the S1 page tables while the guest is in an > > inconsistent state. This is done as follows: > > > > On guest entry: > > - TCR_EL1.EPD{0,1} are set, ensuring that no PTW can occur > > - all system registers are restored, except for TCR_EL1 and SCTLR_EL1 > > - stage-2 is restored > > - SCTLR_EL1 and TCR_EL1 are restored > > > > On guest exit: > > - SCTLR_EL1.M and TCR_EL1.EPD{0,1} are set, ensuring that no PTW can occur > > - stage-2 is disabled > > - All host system registers are restored > > Reviewed-by: James Morse > > (whitespace nit below) > > > > diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c > > index 69e10b29cbd0..5765b17c38c7 100644 > > --- a/arch/arm64/kvm/hyp/switch.c > > +++ b/arch/arm64/kvm/hyp/switch.c > > @@ -118,6 +118,20 @@ static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu) > > } > > > > write_sysreg(val, cptr_el2); > > + > > + if (cpus_have_const_cap(ARM64_WORKAROUND_1319367)) { > > + struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; > > + > > + isb(); > > + /* > > + * At this stage, and thanks to the above isb(), S2 is > > + * configured and enabled. We can now restore the guest's S1 > > + * configuration: SCTLR, and only then TCR. > > + */ > > (note for my future self: because the guest may have had M=0 and rubbish in the TTBRs) > > > + write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], SYS_SCTLR); > > + isb(); > > + write_sysreg_el1(ctxt->sys_regs[TCR_EL1], SYS_TCR); > > + } > > } > > > > > > diff --git a/arch/arm64/kvm/hyp/sysreg-sr.c b/arch/arm64/kvm/hyp/sysreg-sr.c > > index 7ddbc849b580..fb97547bfa79 100644 > > --- a/arch/arm64/kvm/hyp/sysreg-sr.c > > +++ b/arch/arm64/kvm/hyp/sysreg-sr.c > > @@ -117,12 +117,26 @@ static void __hyp_text __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt) > > { > > write_sysreg(ctxt->sys_regs[MPIDR_EL1], vmpidr_el2); > > write_sysreg(ctxt->sys_regs[CSSELR_EL1], csselr_el1); > > - write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], SYS_SCTLR); > > + > > + if (!cpus_have_const_cap(ARM64_WORKAROUND_1319367)) { > > + write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], SYS_SCTLR); > > + write_sysreg_el1(ctxt->sys_regs[TCR_EL1], SYS_TCR); > > + } else if (!ctxt->__hyp_running_vcpu) { > > + /* > > + * Must only be done for guest registers, hence the context > > + * test. We'recoming from the host, so SCTLR.M is already > > (Nit: We'recoming?) Well spotted, now fixed. And thanks for the reviewing, much appreciated. Catalin, Will: given that this series conflicts with the workaround for erratum 1542419, do you mind taking it via the arm64 tree? To make things a bit simpler, I've updated the series with James' tags at [1], and pushed out a resolution of the merge with arm64/for-next/core [2]. Thanks, M. [1] git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git kvm-arm64/erratum-1319367 [2] git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git kvm-arm64/erratum-1319367-resolved -- Jazz is not dead, it just smells funny.