From patchwork Wed Sep 6 09:32:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongjiu Geng X-Patchwork-Id: 9940211 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6676F6035F for ; Wed, 6 Sep 2017 09:33:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A74228B0E for ; Wed, 6 Sep 2017 09:33:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4EBD728B69; Wed, 6 Sep 2017 09:33:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9F65428B0E for ; Wed, 6 Sep 2017 09:33:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date: Message-ID:From:References:To:Subject:Reply-To:Content-ID:Content-Description :Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=53M8RwtVCpyh9Ld3Xng0slJ0umfxr1iS2+c8NFfp7x8=; b=QE0kuALEVoNT5A M4tsXAXlHXOsFMg30R3y4JhfmM8h5ajvedC6hp4bxC3YAZ289u5eiQ58TZ6a73IBIAXKjzf0RqnU9 5G6mG7WYhcD+Zskds6utqwkUHmyCNRFxNaGQ0bdQK7NX3+cQqb4+azIo0zYTCbUP9CkI8VsgeIqUV pmaHwFNVduHNuUAhoNIxczNzJSINahtCLvMI3FhuzX1iFNZN6evgddEa+9UBkXU3Bfuq7DYEvtquZ +mu45DnmG2X/tp+ErlLsN4qx9LIiWXQNaf5Uh0V/yoSh1ZFzGWNWEMkSCWaJBcJthYDhF1WTUF5fm fx3LT3Z5kqNWF+tqQq7A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dpWiD-00054x-HW; Wed, 06 Sep 2017 09:33:33 +0000 Received: from szxga04-in.huawei.com ([45.249.212.190]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dpWi9-00051Z-VY for linux-arm-kernel@lists.infradead.org; Wed, 06 Sep 2017 09:33:32 +0000 Received: from 172.30.72.60 (EHLO DGGEMS403-HUB.china.huawei.com) ([172.30.72.60]) by dggrg04-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DGP91365; Wed, 06 Sep 2017 17:32:56 +0800 (CST) Received: from [127.0.0.1] (10.142.68.147) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.301.0; Wed, 6 Sep 2017 17:32:48 +0800 Subject: Re: [PATCH] arm64: KVM: VHE: save and restore some PSTATE bits To: Marc Zyngier , "christoffer.dall@linaro.org" , "pbonzini@redhat.com" , "rkrcmar@redhat.com" , "vladimir.murzin@arm.com" , "linux-arm-kernel@lists.infradead.org" , "kvmarm@lists.cs.columbia.edu" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "suzuki.poulose@arm.com" , , References: <0184EA26B2509940AA629AE1405DD7F2015DF717@DGGEMA503-MBX.china.huawei.com> <2a5d4299-2523-aef5-7db1-f351ca66b562@arm.com> From: gengdongjiu Message-ID: Date: Wed, 6 Sep 2017 17:32:38 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <2a5d4299-2523-aef5-7db1-f351ca66b562@arm.com> X-Originating-IP: [10.142.68.147] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.59AFC0C9.00A3, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: f91d7ba1cdb2175cadfa251c88f3610f X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170906_023330_368283_8C131FA7 X-CRM114-Status: GOOD ( 14.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Huangshaoyu , James Morse , zhanghaibin7@huawei.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Hi Marc, On 2017/9/6 16:17, Marc Zyngier wrote: > On 05/09/17 19:58, gengdongjiu wrote: >> when exit from guest, some host PSTATE bits may be lost, such as >> PSTATE.PAN or PSTATE.UAO. It is because host and hypervisor all run >> in the EL2, host PSTATE value cannot be saved and restored via >> SPSR_EL2. So if guest has changed the PSTATE, host continues with >> a wrong value guest has set. >> >> Signed-off-by: Dongjiu Geng >> Signed-off-by: Haibin Zhang >> --- >> arch/arm64/include/asm/kvm_host.h | 8 +++++++ >> arch/arm64/include/asm/kvm_hyp.h | 2 ++ >> arch/arm64/include/asm/sysreg.h | 23 +++++++++++++++++++ >> arch/arm64/kvm/hyp/entry.S | 2 -- >> arch/arm64/kvm/hyp/switch.c | 24 ++++++++++++++++++-- >> arch/arm64/kvm/hyp/sysreg-sr.c | 48 ++++++++++++++++++++++++++++++++++++--- >> 6 files changed, 100 insertions(+), 7 deletions(-) >> >> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h >> index e923b58..cba7d3e 100644 >> --- a/arch/arm64/include/asm/kvm_host.h >> +++ b/arch/arm64/include/asm/kvm_host.h >> @@ -193,6 +193,12 @@ struct kvm_cpu_context { >> }; >> }; >> >> +struct kvm_cpu_host_pstate { >> + u64 daif; >> + u64 uao; >> + u64 pan; >> +}; > > I love it. This is the most expensive way of saving/restoring a single > 32bit value. > > More seriously, please see the discussion between James and Christoffer > there[1]. I expect James to address the PAN/UAO states together with the > debug state in the next iteration of his patch. I roughly see the discussion between James and Christoffer, Seems Christoffer does not suggest save and restore it, and suggest to do below, and UAO/PAN may not use the same ways. __kvm_vcpu_run(struct kvm_vcpu *vcpu) { if (has_vhe()) asm("msr daifset, #0xf"); ... exit_code = __guest_enter(vcpu, host_ctxt); ... if (has_vhe()) asm("msr daifclr, #0xd"); } If not save/restore them, the KVM will set them according to the CPU capability. For example below fixing, it will check CPU capability, If CPU supports PAN, the kvm will always enable the PAN for the host. But in fact, the host may be not enable the PAN. Of course for the UAO, we can use the similar fixing if Marc or Christoffer is agreed. but seems not make sense. commit cb96408da4e11698674abd04aeac941c1bed2038 Author: Vladimir Murzin Date: Thu Sep 1 15:29:03 2016 +0100 arm64: KVM: VHE: reset PSTATE.PAN on entry to EL2 SCTLR_EL2.SPAN bit controls what happens with the PSTATE.PAN bit on an exception. However, this bit has no effect on the PSTATE.PAN when HCR_EL2.E2H or HCR_EL2.TGE is unset. Thus when VHE is used and exception taken from a guest PSTATE.PAN bit left unchanged and we continue with a value guest has set. To address that always reset PSTATE.PAN on entry from EL1. Fixes: 1f364c8c48a0 ("arm64: VHE: Add support for running Linux in EL2 mode") Signed-off-by: Vladimir Murzin Reviewed-by: James Morse Acked-by: Marc Zyngier Cc: # v4.6+ Signed-off-by: Christoffer Dall > > Thanks, > > M. > > [1] https://www.spinics.net/lists/arm-kernel/msg599798.html > diff --git a/arch/arm64/kvm/hyp/entry.S b/arch/arm64/kvm/hyp/entry.S index 3967c231..b5926ee 100644 --- a/arch/arm64/kvm/hyp/entry.S +++ b/arch/arm64/kvm/hyp/entry.S @@ -96,6 +96,8 @@ ENTRY(__guest_exit) add x1, x1, #VCPU_CONTEXT + ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN) + // Store the guest regs x2 and x3 stp x2, x3, [x1, #CPU_XREG_OFFSET(2)]