Received: by 10.223.176.5 with SMTP id f5csp616838wra; Tue, 6 Feb 2018 04:41:07 -0800 (PST) X-Google-Smtp-Source: AH8x226xmQZY9MntN6seSSqOCkwqqxlo6opbALJmt3jL508Hw80rImRDq1cbzUEWGHm0kl7aq1dr X-Received: by 10.98.78.148 with SMTP id c142mr2306594pfb.153.1517920866886; Tue, 06 Feb 2018 04:41:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517920866; cv=none; d=google.com; s=arc-20160816; b=CYRWV3X4GTD//doY7q4xVwjW/GKUgiA8OCVITkRDYCEPdH5hSaCsWWyTM4VZO7MfsJ 6LwX9XOQpsTPVdaI1jYZgMwWYlif8qb65MAvCKH1aeUEetLG4uqBkPDMY6C8KNNwOgVP RzvUxCbHZXX6ExlAO1vM46lcGZike6pmzR+6Xzz/YvflHAx42T4JQfP4iRVdIrYUsz0R 80FP8dy9zBrdiS8T65H7odJ7wtcJXzxWHlPKUlm0/VOgihW2TH0Yjvgn+VxwP5Xwok0Z 00JedkMFwhSeMghY+787nh+EsGsnaPtPKwkvMLQHdHkRkStWujRI8+JD05XRms6peHsN L0aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=GnAcEgvQpCXFmD9STRBGepRB+RtNNhxWDS6dfeuO55k=; b=G94DMYu4DD/b1uEiTu2ynoGLPMxzmUC6qxRlYr2WjrTEp6jojw9isVVE3OMLtLTp0f QMvlfVWoTlsT2B99NP9txv+oG5zj0yQgL61mCnBBCRj0IA6eNxIijNZQbZJYdX96b3ef rYaLXDTCr/ZQ5+W1YYMtRF0cDiZ8KH0t3p+xp32eqS03F5e19DQ69xmPe8WG1NLMS+DZ 7s0dhLVruF8i5E4R+3PUC7H0670d11p5UEtptpCa8T72wOWyqr7OlqW28I6YAObhv5Yg RGgvm7irkEglo0/PfFzWmLMK5NmIK8S4gFwEzPJokIW+wDUFP8/JtvbQpaYG6lTV2Ihn 9tFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fPowP1ks; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g16si2798972pfh.248.2018.02.06.04.40.53; Tue, 06 Feb 2018 04:41:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fPowP1ks; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752988AbeBFMj3 (ORCPT + 99 others); Tue, 6 Feb 2018 07:39:29 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:54407 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752956AbeBFMjT (ORCPT ); Tue, 6 Feb 2018 07:39:19 -0500 Received: by mail-wm0-f68.google.com with SMTP id i186so3457341wmi.4 for ; Tue, 06 Feb 2018 04:39:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=GnAcEgvQpCXFmD9STRBGepRB+RtNNhxWDS6dfeuO55k=; b=fPowP1ksIKNFVAAqoSuVSxDsu+ILLpyhh+GcM3GuLLIFKx1S3UfggwWWXEtKD5/7Rq LqI5WP2/T9/SexIa8MG3M7YdA12OzWKBn3HH/nWKnIf7sd7zoCbkcU40Q7NCO3aUl8eM Eq9/pSLiDTlBy4bHlpNqWwnvgLozDX5SuwI3U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=GnAcEgvQpCXFmD9STRBGepRB+RtNNhxWDS6dfeuO55k=; b=lz+XX/67fM4kyVBw+nT4ZMBE0qiiYLCJqVMqYwAILMwsjgZrgwhz/SMiGGN9kHAR66 L/csZ+BdIgxjS/kQNlhJYExhuKcAuHD+CrZ4DrL6JW/jdT+WkHmd2H35n+JXwKfjGX4n 9IyCHj82Rts+biDI0pjUv7nvrl8jl+G6/FicUzTll17iSifoq/a8oOKII6xvQNPBIcd7 vpyMg6C0zybI1cHBtD16T4uoeifVOb5beS7zeaBZNmuwQFye+SAunv0eUgt4/K0oHHYI Ld+SCsuumUhOHEPOIjSsTMCUsorWFRRB84TvocZzbOlgL1c0YbeGi9dT1ukbfBVCUImn R3Ug== X-Gm-Message-State: APf1xPAt3ptO7RVRvgstOQNyeczxvwAhQG3gcjBAK4G6oOfFPKlZATir B+oZri2H10IYzJRp2VAqIEZOHw== X-Received: by 10.80.214.154 with SMTP id r26mr3623745edi.288.1517920757682; Tue, 06 Feb 2018 04:39:17 -0800 (PST) Received: from localhost (x50d2404e.cust.hiper.dk. [80.210.64.78]) by smtp.gmail.com with ESMTPSA id k11sm8522494eda.22.2018.02.06.04.39.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2018 04:39:16 -0800 (PST) Date: Tue, 6 Feb 2018 13:39:15 +0100 From: Christoffer Dall To: Mark Rutland Cc: linux-arm-kernel@lists.infradead.org, arnd@arndb.de, catalin.marinas@arm.com, cdall@linaro.org, kvmarm@lists.cs.columbia.edu, linux-arch@vger.kernel.org, marc.zyngier@arm.com, suzuki.poulose@arm.com, will.deacon@arm.com, yao.qi@arm.com, kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, awallis@codeaurora.org Subject: Re: [PATCHv2 09/12] arm64/kvm: preserve host HCR_EL2 value Message-ID: <20180206123915.GA21802@cbox> References: <20171127163806.31435-1-mark.rutland@arm.com> <20171127163806.31435-10-mark.rutland@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171127163806.31435-10-mark.rutland@arm.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 27, 2017 at 04:38:03PM +0000, Mark Rutland wrote: > When restoring HCR_EL2 for the host, KVM uses HCR_HOST_VHE_FLAGS, which > is a constant value. This works today, as the host HCR_EL2 value is > always the same, but this will get in the way of supporting extensions > that require HCR_EL2 bits to be set conditionally for the host. > > To allow such features to work without KVM having to explicitly handle > every possible host feature combination, this patch has KVM save/restore > the host HCR when switching to/from a guest HCR. > > For __{activate,deactivate}_traps(), the HCR save/restore is made common > across the !VHE and VHE paths. As the host and guest HCR values must > have E2H set when VHE is in use, register redirection should always be > in effect at EL2, and this change should not adversely affect the VHE > code. > > For the hyp TLB maintenance code, __tlb_switch_to_host_vhe() is updated > to toggle the TGE bit with a RMW sequence, as we already do in > __tlb_switch_to_guest_vhe(). > > The now unused HCR_HOST_VHE_FLAGS definition is removed. > > Signed-off-by: Mark Rutland > Reviewed-by: Christoffer Dall > Cc: Marc Zyngier > Cc: kvmarm@lists.cs.columbia.edu > --- > arch/arm64/include/asm/kvm_arm.h | 1 - > arch/arm64/include/asm/kvm_host.h | 5 ++++- > arch/arm64/kvm/hyp/switch.c | 5 +++-- > arch/arm64/kvm/hyp/tlb.c | 6 +++++- > 4 files changed, 12 insertions(+), 5 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h > index 62854d5d1d3b..aa02b05430e8 100644 > --- a/arch/arm64/include/asm/kvm_arm.h > +++ b/arch/arm64/include/asm/kvm_arm.h > @@ -84,7 +84,6 @@ > HCR_AMO | HCR_SWIO | HCR_TIDCP | HCR_RW) > #define HCR_VIRT_EXCP_MASK (HCR_VSE | HCR_VI | HCR_VF) > #define HCR_INT_OVERRIDE (HCR_FMO | HCR_IMO) > -#define HCR_HOST_VHE_FLAGS (HCR_RW | HCR_TGE | HCR_E2H) > > /* TCR_EL2 Registers bits */ > #define TCR_EL2_RES1 ((1 << 31) | (1 << 23)) > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index 674912d7a571..39184aa3e2f2 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -199,10 +199,13 @@ typedef struct kvm_cpu_context kvm_cpu_context_t; > struct kvm_vcpu_arch { > struct kvm_cpu_context ctxt; > > - /* HYP configuration */ > + /* Guest HYP configuration */ > u64 hcr_el2; > u32 mdcr_el2; > > + /* Host HYP configuration */ > + u64 host_hcr_el2; > + > /* Exception Information */ > struct kvm_vcpu_fault_info fault; > > diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c > index 525c01f48867..2205f0be3ced 100644 > --- a/arch/arm64/kvm/hyp/switch.c > +++ b/arch/arm64/kvm/hyp/switch.c > @@ -71,6 +71,8 @@ static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu) > { > u64 val; > > + vcpu->arch.host_hcr_el2 = read_sysreg(hcr_el2); > + Looking back at this, it seems excessive to switch this at every round-trip. I think it should be possible to have this as a single global (or per-CPU) variable that gets restored directly when returning from the VM. Thanks, -Christoffer > /* > * We are about to set CPTR_EL2.TFP to trap all floating point > * register accesses to EL2, however, the ARM ARM clearly states that > @@ -116,7 +118,6 @@ static void __hyp_text __deactivate_traps_vhe(void) > MDCR_EL2_TPMS; > > write_sysreg(mdcr_el2, mdcr_el2); > - write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2); > write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1); > write_sysreg(vectors, vbar_el1); > } > @@ -129,7 +130,6 @@ static void __hyp_text __deactivate_traps_nvhe(void) > mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT; > > write_sysreg(mdcr_el2, mdcr_el2); > - write_sysreg(HCR_RW, hcr_el2); > write_sysreg(CPTR_EL2_DEFAULT, cptr_el2); > } > > @@ -151,6 +151,7 @@ static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu) > __deactivate_traps_arch()(); > write_sysreg(0, hstr_el2); > write_sysreg(0, pmuserenr_el0); > + write_sysreg(vcpu->arch.host_hcr_el2, hcr_el2); > } > > static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu) > diff --git a/arch/arm64/kvm/hyp/tlb.c b/arch/arm64/kvm/hyp/tlb.c > index 73464a96c365..c2b0680efa2c 100644 > --- a/arch/arm64/kvm/hyp/tlb.c > +++ b/arch/arm64/kvm/hyp/tlb.c > @@ -49,12 +49,16 @@ static hyp_alternate_select(__tlb_switch_to_guest, > > static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm) > { > + u64 val; > + > /* > * We're done with the TLB operation, let's restore the host's > * view of HCR_EL2. > */ > write_sysreg(0, vttbr_el2); > - write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2); > + val = read_sysreg(hcr_el2); > + val |= HCR_TGE; > + write_sysreg(val, hcr_el2); > } > > static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm) > -- > 2.11.0 >