Received: by 2002:ac0:a679:0:0:0:0:0 with SMTP id p54csp615130imp; Thu, 21 Feb 2019 07:51:40 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib2MbOkfclCSTEhytctzOklfYEV0dEhVj/z2BalU8lR09XGs54f5sHoA5cobABVyzIGsNtv X-Received: by 2002:a62:6282:: with SMTP id w124mr41177893pfb.168.1550764300225; Thu, 21 Feb 2019 07:51:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550764300; cv=none; d=google.com; s=arc-20160816; b=gvGpnpRx1Bhcv0ZL66b1uNcJe5tnSgexWlkRFlqyTlLXrIoQ+pjaa8PIUnofmSXe1K gykyrQvaRhBvT6ShV0A6NGzknWQ4kf65OJrpXeOi/WmbOORcdfb3x/6wieqv/2c5wEjn Za3vzdp6OJ0rLxF8LRDuJvr/jZRZNsT/CCH9K8kyY1oSi1WaecqHHO4wSH8h5OP7iOfJ iay58SA1wpWAZnMdDibrt1tj/c1iMz+4/NBsb6xqBZ47n0b9fV3hLyH7JkirPbJtiK5f f6GhmdwpDYjd87Qal2c4qvyRZImbX8o28GyTRtSptXN5uS9a3+Wqzk5JsOa6CfqiKVJM 5bPA== 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; bh=GIZDU2GxdeNKhixv4PIwx9eBkrJWYyL4NpP0vU1Q54g=; b=zAmkVQX29JsdDhloEB6MpjkD5vgHyWeTqSbfaanFMBsJCK4nuuOcm8NhdPimJ7mP43 5iE09qv200bvuhZ8wAStE2+d7z6NLNl2AGeJZqeEPaQwt6bE3N5Io0mnkaEJS/beQZcc 4lFyyMZh2HJyYrXBWMIGbak2zV2DxkjlwtygSewY4OF1kOb/7gsOHL7DsALSrp1GnEJ3 qFoGEYuwTkvBsAGGXDEqwir0R2RgRjRZZcut4+eQDwN0ccZhcYuIyfPUeAY9tTixQ1d8 kvQk3MJd23U6RIxGiTTui1dJM5OYuUy6sesY90URJRaxxdfkgMTzBMerNKsgjF6kWRlt C4SQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 22si14138577pgs.336.2019.02.21.07.51.25; Thu, 21 Feb 2019 07:51:40 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726671AbfBUPtl (ORCPT + 99 others); Thu, 21 Feb 2019 10:49:41 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:46634 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726344AbfBUPtk (ORCPT ); Thu, 21 Feb 2019 10:49:40 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2A0CBA78; Thu, 21 Feb 2019 07:49:40 -0800 (PST) Received: from e103592.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3B1DB3F5C1; Thu, 21 Feb 2019 07:49:38 -0800 (PST) Date: Thu, 21 Feb 2019 15:49:35 +0000 From: Dave Martin To: Amit Daniel Kachhap Cc: linux-arm-kernel@lists.infradead.org, Marc Zyngier , Catalin Marinas , Will Deacon , Kristina Martsenko , kvmarm@lists.cs.columbia.edu, Ramana Radhakrishnan , linux-kernel@vger.kernel.org Subject: Re: [PATCH v6 1/6] arm64/kvm: preserve host HCR_EL2 value Message-ID: <20190221154935.GU3567@e103592.cambridge.arm.com> References: <1550568271-5319-1-git-send-email-amit.kachhap@arm.com> <1550568271-5319-2-git-send-email-amit.kachhap@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1550568271-5319-2-git-send-email-amit.kachhap@arm.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 19, 2019 at 02:54:26PM +0530, Amit Daniel Kachhap wrote: > From: Mark Rutland > > 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 > for the host HCR when switching to/from a guest HCR. The saving of the > register is done once during cpu hypervisor initialization state and is > just restored after switch from guest. > > For fetching HCR_EL2 during kvm initialisation, a hyp call is made using > kvm_call_hyp and is helpful in NHVE case. Minor nit: NVHE misspelled. This looks a bit like it's naming an arch feature rather than a kernel implementation detail though. Maybe write "non-VHE". > 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 value of hcr_el2 is now stored in struct kvm_cpu_context as both host > and guest can now use this field in a common way. > > Signed-off-by: Mark Rutland > [Added __cpu_copy_hyp_conf, hcr_el2 field in struct kvm_cpu_context] > Signed-off-by: Amit Daniel Kachhap > Cc: Marc Zyngier > Cc: Christoffer Dall > Cc: kvmarm@lists.cs.columbia.edu > --- > arch/arm/include/asm/kvm_host.h | 2 ++ > arch/arm64/include/asm/kvm_asm.h | 2 ++ > arch/arm64/include/asm/kvm_emulate.h | 22 +++++++++++----------- > arch/arm64/include/asm/kvm_host.h | 13 ++++++++++++- > arch/arm64/include/asm/kvm_hyp.h | 2 +- > arch/arm64/kvm/guest.c | 2 +- > arch/arm64/kvm/hyp/switch.c | 23 +++++++++++++---------- > arch/arm64/kvm/hyp/sysreg-sr.c | 21 ++++++++++++++++++++- > arch/arm64/kvm/hyp/tlb.c | 6 +++++- > virt/kvm/arm/arm.c | 1 + > 10 files changed, 68 insertions(+), 26 deletions(-) > > diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h > index ca56537..05706b4 100644 > --- a/arch/arm/include/asm/kvm_host.h > +++ b/arch/arm/include/asm/kvm_host.h > @@ -273,6 +273,8 @@ static inline void __cpu_init_stage2(void) > kvm_call_hyp(__init_stage2_translation); > } > > +static inline void __cpu_copy_hyp_conf(void) {} > + > static inline int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext) > { > return 0; > diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h > index f5b79e9..8acd73f 100644 > --- a/arch/arm64/include/asm/kvm_asm.h > +++ b/arch/arm64/include/asm/kvm_asm.h > @@ -80,6 +80,8 @@ extern void __vgic_v3_init_lrs(void); > > extern u32 __kvm_get_mdcr_el2(void); > > +extern void __kvm_populate_host_regs(void); > + > /* Home-grown __this_cpu_{ptr,read} variants that always work at HYP */ > #define __hyp_this_cpu_ptr(sym) \ > ({ \ > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index 506386a..0dbe795 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -50,25 +50,25 @@ void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr); > > static inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) > { > - return !(vcpu->arch.hcr_el2 & HCR_RW); > + return !(vcpu->arch.ctxt.hcr_el2 & HCR_RW); Putting hcr_el2 into struct kvm_cpu_context creates a lot of splatter here, and I'm wondering whether it's really necessary. Otherwise, we could just put the per-vcpu guest HCR_EL2 value in struct kvm_vcpu_arch. Is the *host* hcr_el2 value really different per-vcpu? That looks odd. I would have thought this is fixed across the system at KVM startup time. Having a single global host hcr_el2 would also avoid the need for __kvm_populate_host_regs(): instead, we just decide what HCR_EL2 is to be ahead of time and set a global variable that we map into Hyp. Or does the host HCR_EL2 need to vary at runtime for some reason I've missed? [...] +void __hyp_text __kvm_populate_host_regs(void) +{ + struct kvm_cpu_context *host_ctxt; + + if (has_vhe()) + host_ctxt = this_cpu_ptr(&kvm_host_cpu_state); + else + host_ctxt = __hyp_this_cpu_ptr(kvm_host_cpu_state); According to the comment by the definition of __hyp_this_cpu_ptr(), this always works at Hyp. I also see other calls with no fallback this_cpu_ptr() call like we have here. So, can we simply always call __hyp_this_cpu_ptr() here? (I'm not familiar with this, myself.) Cheers ---Dave