Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2513245yba; Sun, 7 Apr 2019 21:32:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzyfaVNEQTsmZ1dxgk82sAOGFiYSmrcJk8oGOF1LguCab7vj39BmKWXvnsUEoEj4rcW2LK X-Received: by 2002:a17:902:4381:: with SMTP id j1mr26791459pld.75.1554697975305; Sun, 07 Apr 2019 21:32:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554697975; cv=none; d=google.com; s=arc-20160816; b=cn6tG5xDbId/gEdVCFt7r2Om6hHr7PjQ+IwV4g9LETpK0FjO1aLgXAFHXV/mGUrfjv E7m7FsRnpWrVZgqNCT5SkNzCN1jTLE8fbQNASIY3aaDEFO26O6JoyP/gaCDrO6y1Psyi cWSBB5WQapAStsR7nbPAXYrywfGFBJC+L2WDF/pW9TPXbZ97dMuH85aqGreu0/nrABzA bf2kaFuoz6jXui/4HT9IpIJoYu2XihUOCkXR+9IfMeujEaxWjEKK6IjPnnN8nCeSSkXk OCttLMQz1fN5KBPy0Stnxwi/7tmV/WQUIdXCWuFyevzAIT7ksYzId37u/GZk1QTL4wKf qeQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=ZJo5SgHf4VHYZJRtpsqEXNkO95Q4lxKcWB/U6i6l6ng=; b=Ur9XSMXOqsV9wbzWHa8OC9rrn/peqswQ4Obz7ZLE9qfF7Y5dHg/4rWKCjPlnCzSs2e iSu3csMis/+XyN8h9Tk36zDLWvY4GvK9ypKuSZMzvlGDsBNwoRpdL/0a+/eaiODp1Muz nnVcb/ewAq3WjcaEDUsvxVj1YzQDbUZZjxJNituHKmC2ANMZVQAv/Q2XtXpLaYhzM1DJ /vDp0pMpMcVFLScJyyMaSscTzV6LD+F4yD1kRDejvmCq7/4HRIv4tbUISgJNVu5xSDKc fpLGyD5QfzVDVO0e6hHTf9GlT7hx334fJreN47pa/TSEzc7RqlEPVFee7FpjObfj5i8p rauQ== 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 p87si20205477pfa.77.2019.04.07.21.32.28; Sun, 07 Apr 2019 21:32:55 -0700 (PDT) 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 S1726060AbfDHEbd (ORCPT + 99 others); Mon, 8 Apr 2019 00:31:33 -0400 Received: from foss.arm.com ([217.140.101.70]:40914 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725933AbfDHEbd (ORCPT ); Mon, 8 Apr 2019 00:31:33 -0400 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 4D55E15AD; Sun, 7 Apr 2019 21:31:32 -0700 (PDT) Received: from [10.162.0.144] (a075553-lin.blr.arm.com [10.162.0.144]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4EE0C3F718; Sun, 7 Apr 2019 21:31:29 -0700 (PDT) Subject: Re: [PATCH v8 4/9] KVM: arm/arm64: preserve host HCR_EL2 value To: Dave Martin 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 References: <1554172037-4516-1-git-send-email-amit.kachhap@arm.com> <1554172037-4516-5-git-send-email-amit.kachhap@arm.com> <20190405110229.GR3567@e103592.cambridge.arm.com> From: Amit Daniel Kachhap Message-ID: <8066f634-b2e0-33c3-a6f4-4f38729b3f86@arm.com> Date: Mon, 8 Apr 2019 10:01:26 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190405110229.GR3567@e103592.cambridge.arm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 4/5/19 4:32 PM, Dave Martin wrote: > On Tue, Apr 02, 2019 at 07:57:12AM +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 non-VHE case. >> >> 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_init_host_ctxt, hcr_el2 field in struct kvm_cpu_context, >> save hcr_el2 in hyp init stage] >> Signed-off-by: Amit Daniel Kachhap >> Reviewed-by: James Morse >> Cc: Marc Zyngier >> Cc: Christoffer Dall >> Cc: kvmarm@lists.cs.columbia.edu > > [...] > >> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h >> index a01fe087..3b09fd0 100644 >> --- a/arch/arm64/include/asm/kvm_host.h >> +++ b/arch/arm64/include/asm/kvm_host.h >> @@ -209,6 +209,8 @@ struct kvm_cpu_context { >> u32 copro[NR_COPRO_REGS]; >> }; >> >> + /* HYP host/guest configuration */ >> + u64 hcr_el2; > > Minor nit: You could delete "host/guest" from the comment here. This is > implied by the fact that the member is in struct kvm_cpu_context in the > first place. ok. Agree with you. > >> struct kvm_vcpu *__hyp_running_vcpu; >> }; > > [...] > >> diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c >> index 3563fe6..f5cefa1 100644 >> --- a/arch/arm64/kvm/hyp/switch.c >> +++ b/arch/arm64/kvm/hyp/switch.c > > [...] > >> @@ -159,9 +159,10 @@ static void deactivate_traps_vhe(void) >> } >> NOKPROBE_SYMBOL(deactivate_traps_vhe); >> >> -static void __hyp_text __deactivate_traps_nvhe(void) >> +static void __hyp_text __deactivate_traps_nvhe(struct kvm_cpu_context *host_ctxt) > > Where __hyp_text functions accept pointer arguments, they are usually > hyp pointers already... (see below) > >> { >> u64 mdcr_el2 = read_sysreg(mdcr_el2); >> + struct kvm_cpu_context *hyp_host_ctxt = kern_hyp_va(host_ctxt); >> >> __deactivate_traps_common(); >> >> @@ -169,25 +170,28 @@ 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_HOST_NVHE_FLAGS, hcr_el2); >> + write_sysreg(hyp_host_ctxt->hcr_el2, hcr_el2); >> write_sysreg(CPTR_EL2_DEFAULT, cptr_el2); >> } >> >> static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu) >> { >> + struct kvm_cpu_context *host_ctxt; >> + >> + host_ctxt = vcpu->arch.host_cpu_context; > > host_ctxt is not otherwise used here, so can we convert it up-front so > that the argument to __deactivate_traps_nvhe() and > deactivate_traps_vhe() is a hyp pointer already? > > So: > > struct kvm_cpu_context *hyp_host_ctxt; > > hyp_host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context); > >> /* >> * If we pended a virtual abort, preserve it until it gets >> * cleared. See D1.14.3 (Virtual Interrupts) for details, but >> * the crucial bit is "On taking a vSError interrupt, >> * HCR_EL2.VSE is cleared to 0." >> */ >> - if (vcpu->arch.hcr_el2 & HCR_VSE) >> - vcpu->arch.hcr_el2 = read_sysreg(hcr_el2); >> + if (vcpu->arch.ctxt.hcr_el2 & HCR_VSE) >> + vcpu->arch.ctxt.hcr_el2 = read_sysreg(hcr_el2); >> >> if (has_vhe()) >> - deactivate_traps_vhe(); >> + deactivate_traps_vhe(host_ctxt); >> else >> - __deactivate_traps_nvhe(); >> + __deactivate_traps_nvhe(host_ctxt); > > Then just pass hyp_host_ctxt to both of these, and drop the > kern_hyp_va() conversion from __deactivate_traps_nvhe(). > > This may be a bit less confusing. Yes your explanation makes sense. > > Alternatively, just pass in the vcpu pointer (since this pattern is > already well established all over the place). I think passing vcpu as parameter will make it consistent with other existing functions. __kvm_vcpu_run_nvhe function also takes vcpu and extracts hyp_host_ctxt. > > Another option could be to pull the hcr_el2 write out of the backends > entirely and put it in this common code instead. This doesn't look > straightforward though (or at least, I don't remember enough about how > all these traps handling functions fit together...) ok. Thanks, Amit D > > [...] > > Cheers > ---Dave >