From patchwork Tue Jul 18 16:58:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9849005 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 066AD600CC for ; Tue, 18 Jul 2017 17:03:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4A4C285D5 for ; Tue, 18 Jul 2017 17:03:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D94A1285DA; Tue, 18 Jul 2017 17:03:01 +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=unavailable 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 52DB3285D5 for ; Tue, 18 Jul 2017 17:03:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752469AbdGRRCs (ORCPT ); Tue, 18 Jul 2017 13:02:48 -0400 Received: from mail-it0-f44.google.com ([209.85.214.44]:36013 "EHLO mail-it0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752468AbdGRRAd (ORCPT ); Tue, 18 Jul 2017 13:00:33 -0400 Received: by mail-it0-f44.google.com with SMTP id a62so23803324itd.1 for ; Tue, 18 Jul 2017 10:00:33 -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=RTOCeD9C0Q0Zk3vCwtWb/h9OY7AIslrucChSVac3rQY=; b=cBu/xqGOwuCorSPPnbKYQdY3aKQ64m8vQIajN5Jb0HUhz0TLaslBIeOw6N6vAbnV/5 UHDRf/tL8QvgI29VmC8nUoOPJljEhv7FDXgVMnUS4OC3S/hI5xCmFBfi+7dxjnh/VnFA 4s5IYeMK5MuwjLLzSWbbyd/3H7Da+mIYWlMRc= 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=RTOCeD9C0Q0Zk3vCwtWb/h9OY7AIslrucChSVac3rQY=; b=gcSX5tOBmUjw96RULmMJJC0vLYkoZif/q9Eis0RY6AdFOUIokp3BXi4mEoB9CkvaXc AwJIcXoNH52G1l/YXSuhZxgLAZDr/IKAzbHNR5tndjOe2d4xccvzn54UfZlHY3bFfp1I XmvEYI/4SAwJC+Cz5Mm1HlDMbN8lr5lCyuR/9Dd7qrX6YraIfgCkLLbmIKBL6DpxyzcM EcpAqWp1xI3yzFjQ+8mgcyVr6y/krSqPCkiK84FdnsKhTqCNmNg0KGouMt+/vWMBBxt4 Ot+kz9Ojq/Mi/e2o02+R1HReTUPYcEufexdG5dqSjykwHXq5iJlZjn2/w2sM2TOetc77 KtYA== X-Gm-Message-State: AIVw111KPV0s838LMEiN/XYuR/jALoSH1MkdnQ/XGuuhuCBkzsdGt+v0 fzBemD99SrQDOP5i X-Received: by 10.36.218.133 with SMTP id z127mr3714018itg.74.1500397232050; Tue, 18 Jul 2017 10:00:32 -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.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 10:00:31 -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 32/38] KVM: arm64: Trap and emulate CPTR_EL2 accesses via CPACR_EL1 from the virtual EL2 with VHE Date: Tue, 18 Jul 2017 11:58:58 -0500 Message-Id: <1500397144-16232-33-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 While the EL1 virtual memory control registers can be accessed in the virtual EL2 with VHE without trap to manuplate the virtual EL2 states, we can't do that for CPTR_EL2 for an unfortunate reason. This is because the top bit of CPTR_EL2, which is TCPAC, will be ignored if it is accessed via CPACR_EL1 in the virtual EL2 without trap since the top bot of cpacr_el1 is RES0. Therefore we need to trap CPACR_EL1 accesses from the virtual EL2 to emulate this bit correctly. Set CPTR_EL2.TCPAC bit to trap CPACR_EL1 accesses and handle them in the existing handler considering that they could be meant to access CPTR_EL2 instead in the virtual EL2 with VHE. Note that CPTR_EL2 format depends on HCR_EL2.E2H bit. We always keep it in v8.0 format for the convenience. Otherwise, we need to check E2H bit and use different bit masks in the entry.S, and we also check E2H bit in all places we access virtual CPTR_EL2. The downside of using v8.0 format is to convert the format when copying states between CPTR_EL2 and CPACR_EL1 to support the virtual EL2 with VHE. The decision is subject to change depending on the future discussion. Signed-off-by: Jintack Lim --- arch/arm64/include/asm/kvm_emulate.h | 2 ++ arch/arm64/kvm/context.c | 29 ++++++++++++++++++++++++++--- arch/arm64/kvm/hyp/switch.c | 2 ++ arch/arm64/kvm/sys_regs.c | 18 +++++++++++++++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 68aafbd..4776bfc 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -59,6 +59,8 @@ enum exception_type { void kvm_arm_setup_shadow_state(struct kvm_vcpu *vcpu); void kvm_arm_restore_shadow_state(struct kvm_vcpu *vcpu); void kvm_arm_init_cpu_context(kvm_cpu_context_t *cpu_ctxt); +u64 cptr_to_cpacr(u64 cptr_el2); +u64 cpacr_to_cptr(u64 cpacr_el1); static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu) { diff --git a/arch/arm64/kvm/context.c b/arch/arm64/kvm/context.c index 9947bc8..a7811e1 100644 --- a/arch/arm64/kvm/context.c +++ b/arch/arm64/kvm/context.c @@ -66,7 +66,7 @@ static inline u64 tcr_el2_ips_to_tcr_el1_ps(u64 tcr_el2) << TCR_IPS_SHIFT; } -static inline u64 cptr_to_cpacr(u64 cptr_el2) +u64 cptr_to_cpacr(u64 cptr_el2) { u64 cpacr_el1 = 0; @@ -78,6 +78,21 @@ static inline u64 cptr_to_cpacr(u64 cptr_el2) return cpacr_el1; } +u64 cpacr_to_cptr(u64 cpacr_el1) +{ + u64 cptr_el2; + + cptr_el2 = CPTR_EL2_DEFAULT; + if (!(cpacr_el1 & CPACR_EL1_FPEN)) + cptr_el2 |= CPTR_EL2_TFP; + if (cpacr_el1 & CPACR_EL1_TTA) + cptr_el2 |= CPTR_EL2_TTA; + if (cpacr_el1 & CPTR_EL2_TCPAC) + cptr_el2 |= CPTR_EL2_TCPAC; + + return cptr_el2; +} + static void sync_shadow_el1_sysregs(struct kvm_vcpu *vcpu) { u64 *s_sys_regs = vcpu->arch.ctxt.shadow_sys_regs; @@ -93,8 +108,12 @@ static void sync_shadow_el1_sysregs(struct kvm_vcpu *vcpu) for (i = 0; i < ARRAY_SIZE(vhe_map); i++) { const struct el1_el2_map *map = &vhe_map[i]; + u64 *el2_reg = &vcpu_sys_reg(vcpu, map->el2); - vcpu_sys_reg(vcpu, map->el2) = s_sys_regs[map->el1]; + /* We do trap-and-emulate CPACR_EL1 accesses. So, don't sync */ + if (map->el2 == CPTR_EL2) + continue; + *el2_reg = s_sys_regs[map->el1]; } } @@ -138,8 +157,12 @@ static void flush_shadow_el1_sysregs_vhe(struct kvm_vcpu *vcpu) */ for (i = 0; i < ARRAY_SIZE(vhe_map); i++) { const struct el1_el2_map *map = &vhe_map[i]; + u64 *el1_reg = &s_sys_regs[map->el1]; - s_sys_regs[map->el1] = vcpu_sys_reg(vcpu, map->el2); + if (map->el2 == CPTR_EL2) + *el1_reg = cptr_to_cpacr(vcpu_sys_reg(vcpu, map->el2)); + else + *el1_reg = vcpu_sys_reg(vcpu, map->el2); } } diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c index fffd0c7..50c90f2 100644 --- a/arch/arm64/kvm/hyp/switch.c +++ b/arch/arm64/kvm/hyp/switch.c @@ -50,6 +50,8 @@ static void __hyp_text __activate_traps_vhe(struct kvm_vcpu *vcpu) val = read_sysreg(cpacr_el1); val |= CPACR_EL1_TTA; val &= ~CPACR_EL1_FPEN; + if (is_hyp_ctxt(vcpu)) + val |= CPTR_EL2_TCPAC; write_sysreg(val, cpacr_el1); write_sysreg(__kvm_hyp_vector, vbar_el1); diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 2aa922c..79980be 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -972,7 +972,23 @@ static bool access_cpacr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) { - access_rw(p, &vcpu_sys_reg(vcpu, r->reg)); + u64 reg = sys_reg(p->Op0, p->Op1, p->CRn, p->CRm, p->Op2); + + /* + * When the virtual HCR_EL2.E2H == 1, an access to CPACR_EL1 + * in the virtual EL2 is to access CPTR_EL2. + */ + if (vcpu_el2_e2h_is_set(vcpu) && (reg == SYS_CPACR_EL1)) { + u64 *sysreg = &vcpu_sys_reg(vcpu, CPTR_EL2); + + /* We keep the value in ARMv8.0 CPTR_EL2 format. */ + if (!p->is_write) + p->regval = cptr_to_cpacr(*sysreg); + else + *sysreg = cpacr_to_cptr(p->regval); + } else /* CPACR_EL1 access with E2H == 0 or CPACR_EL12 access */ + access_rw(p, &vcpu_sys_reg(vcpu, r->reg)); + return true; }