From patchwork Tue Jul 18 16:58:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9849011 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 CB537600CC for ; Tue, 18 Jul 2017 17:03:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B449C285BB for ; Tue, 18 Jul 2017 17:03:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A8F43285D1; Tue, 18 Jul 2017 17:03:12 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 32C8D285CE for ; Tue, 18 Jul 2017 17:03:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751883AbdGRRDK (ORCPT ); Tue, 18 Jul 2017 13:03:10 -0400 Received: from mail-io0-f171.google.com ([209.85.223.171]:35658 "EHLO mail-io0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752448AbdGRRAc (ORCPT ); Tue, 18 Jul 2017 13:00:32 -0400 Received: by mail-io0-f171.google.com with SMTP id k71so16789754iod.2 for ; Tue, 18 Jul 2017 10:00:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8KjAYZtqpeRGUcb4yBOwVe+PFyl5oXGPrMjlOuomUUE=; b=BCv7S676oclti+klFYZEx5PF9TpYj+YbwZPQMcJQp4cDunUS6D8Crk8uB0izSVSpWn OusMxMkSgjO7pmUlzWVzgO8/VQvAs23ZNpLLIbK2nR7vXu7usKb8dhA5cymns3thc4cI nP/t63cFNd+fmXwn5TcxzcLNtFijlS5dLumug= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8KjAYZtqpeRGUcb4yBOwVe+PFyl5oXGPrMjlOuomUUE=; b=UKh8sZY6WUSzHT3yfIMLUcdufvgdmJmONUEioZrYr0mvWl/zRHDDZkX0PII7MQMw21 xvL+UQDhicjY9x+8lBC6AXw/rzgmTS3C0QvFigGsY8zGgj58NAwvvYksxxaA/rVxnobz lxiKnw4dodpJ7G1SxcBEOQR5zt6heplGPlEFzu8I5QGFFZSl461S1FsRre3Xsk2JTMig J7DTG+OFpCLV4aM4584CSDIw7/mW3BGi9UJFrJoWtx/M/i0zvMa5J6YwhqciKNflJFPm p/Hf1Va7JO/yEuxC7txu5onuV9zcT6/kGtQ1+uCBeAHmqoOtxzUfa9FKx69NQBGYNWRo cYLQ== X-Gm-Message-State: AIVw113hCb0YttC/O04OEtnXhOfr3WeyYu2mv76BQEc9MvNPCoQh/FT4 A57YMU7hbGHGGf4a X-Received: by 10.107.198.82 with SMTP id w79mr2788905iof.121.1500397230201; Tue, 18 Jul 2017 10:00:30 -0700 (PDT) Received: from node.jintackl-qv26972.kvmarm-pg0.wisc.cloudlab.us (c220g1-030822.wisc.cloudlab.us. [128.104.222.82]) by smtp.gmail.com with ESMTPSA id j96sm1413075ioo.49.2017.07.18.10.00.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 10:00:29 -0700 (PDT) From: Jintack Lim To: kvmarm@lists.cs.columbia.edu, christoffer.dall@linaro.org, marc.zyngier@arm.com Cc: corbet@lwn.net, pbonzini@redhat.com, rkrcmar@redhat.com, linux@armlinux.org.uk, catalin.marinas@arm.com, will.deacon@arm.com, akpm@linux-foundation.org, mchehab@kernel.org, cov@codeaurora.org, daniel.lezcano@linaro.org, david.daney@cavium.com, mark.rutland@arm.com, suzuki.poulose@arm.com, stefan@hello-penguin.com, andy.gross@linaro.org, wcohen@redhat.com, ard.biesheuvel@linaro.org, shankerd@codeaurora.org, vladimir.murzin@arm.com, james.morse@arm.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jintack Lim Subject: [RFC PATCH v2 31/38] KVM: arm64: Manage the shadow states when virtual E2H bit enabled Date: Tue, 18 Jul 2017 11:58:57 -0500 Message-Id: <1500397144-16232-32-git-send-email-jintack.lim@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1500397144-16232-1-git-send-email-jintack.lim@linaro.org> References: <1500397144-16232-1-git-send-email-jintack.lim@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When creating the shadow context for the virtual EL2 execution, we can directly copy the EL2 register states to the shadow EL1 register states if the virtual HCR_EL2.E2H bit is set. This is because EL1 and EL2 system register formats compatible with E2H=1. Now that we allow the virtual EL2 modify its EL2 registers without trap via the physical EL1 system register accesses, we need to reflect the changes made to the EL1 system registers to the virtual EL2 register states. This is not required to the virtual EL2 without VHE, since the virtual EL2 should always use _EL2 accessors, which traps to EL2. Signed-off-by: Jintack Lim --- arch/arm64/kvm/context.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/context.c b/arch/arm64/kvm/context.c index 39bd92d..9947bc8 100644 --- a/arch/arm64/kvm/context.c +++ b/arch/arm64/kvm/context.c @@ -39,6 +39,27 @@ struct el1_el2_map { { VBAR_EL1, VBAR_EL2 }, }; +/* + * List of pair of EL1/EL2 registers which are used to access real EL2 + * registers in EL2 with E2H bit set. + */ +static const struct el1_el2_map vhe_map[] = { + { SCTLR_EL1, SCTLR_EL2 }, + { CPACR_EL1, CPTR_EL2 }, + { TTBR0_EL1, TTBR0_EL2 }, + { TTBR1_EL1, TTBR1_EL2 }, + { TCR_EL1, TCR_EL2}, + { AFSR0_EL1, AFSR0_EL2 }, + { AFSR1_EL1, AFSR1_EL2 }, + { ESR_EL1, ESR_EL2}, + { FAR_EL1, FAR_EL2}, + { MAIR_EL1, MAIR_EL2 }, + { AMAIR_EL1, AMAIR_EL2 }, + { VBAR_EL1, VBAR_EL2 }, + { CONTEXTIDR_EL1, CONTEXTIDR_EL2 }, + { CNTKCTL_EL1, CNTHCTL_EL2 }, +}; + static inline u64 tcr_el2_ips_to_tcr_el1_ps(u64 tcr_el2) { return ((tcr_el2 & TCR_EL2_PS_MASK) >> TCR_EL2_PS_SHIFT) @@ -57,7 +78,27 @@ static inline u64 cptr_to_cpacr(u64 cptr_el2) return cpacr_el1; } -static void flush_shadow_el1_sysregs(struct kvm_vcpu *vcpu) +static void sync_shadow_el1_sysregs(struct kvm_vcpu *vcpu) +{ + u64 *s_sys_regs = vcpu->arch.ctxt.shadow_sys_regs; + int i; + + /* + * In the virtual EL2 without VHE no EL1 system registers can't be + * changed without trap except el1_non_trap_regs[]. So we have nothing + * to sync on exit from a guest. + */ + if (!vcpu_el2_e2h_is_set(vcpu)) + return; + + for (i = 0; i < ARRAY_SIZE(vhe_map); i++) { + const struct el1_el2_map *map = &vhe_map[i]; + + vcpu_sys_reg(vcpu, map->el2) = s_sys_regs[map->el1]; + } +} + +static void flush_shadow_el1_sysregs_nvhe(struct kvm_vcpu *vcpu) { u64 *s_sys_regs = vcpu->arch.ctxt.shadow_sys_regs; u64 tcr_el2; @@ -86,6 +127,29 @@ static void flush_shadow_el1_sysregs(struct kvm_vcpu *vcpu) s_sys_regs[CPACR_EL1] = cptr_to_cpacr(vcpu_sys_reg(vcpu, CPTR_EL2)); } +static void flush_shadow_el1_sysregs_vhe(struct kvm_vcpu *vcpu) +{ + u64 *s_sys_regs = vcpu->arch.ctxt.shadow_sys_regs; + int i; + + /* + * When e2h bit is set, EL2 registers becomes compatible + * with corrensponding EL1 registers. So, no conversion required. + */ + for (i = 0; i < ARRAY_SIZE(vhe_map); i++) { + const struct el1_el2_map *map = &vhe_map[i]; + + s_sys_regs[map->el1] = vcpu_sys_reg(vcpu, map->el2); + } +} + +static void flush_shadow_el1_sysregs(struct kvm_vcpu *vcpu) +{ + if (vcpu_el2_e2h_is_set(vcpu)) + flush_shadow_el1_sysregs_vhe(vcpu); + else + flush_shadow_el1_sysregs_nvhe(vcpu); +} /* * List of EL0 and EL1 registers which we allow the virtual EL2 mode to access @@ -247,6 +311,7 @@ void kvm_arm_restore_shadow_state(struct kvm_vcpu *vcpu) if (unlikely(is_hyp_ctxt(vcpu))) { sync_shadow_special_regs(vcpu); sync_shadow_non_trap_el1_state(vcpu); + sync_shadow_el1_sysregs(vcpu); } else sync_special_regs(vcpu); }