From patchwork Mon Jan 9 06:24:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9503895 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 EFC6D60757 for ; Mon, 9 Jan 2017 06:30:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFAE5280D0 for ; Mon, 9 Jan 2017 06:30:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D23712815E; Mon, 9 Jan 2017 06:30:43 +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.4 required=2.0 tests=BAYES_00, 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 03979280D0 for ; Mon, 9 Jan 2017 06:30:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938278AbdAIG2M (ORCPT ); Mon, 9 Jan 2017 01:28:12 -0500 Received: from outprodmail02.cc.columbia.edu ([128.59.72.51]:52554 "EHLO outprodmail02.cc.columbia.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S939979AbdAIG0n (ORCPT ); Mon, 9 Jan 2017 01:26:43 -0500 Received: from hazelnut (hazelnut.cc.columbia.edu [128.59.213.250]) by outprodmail02.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id v096Q5Ol006199 for ; Mon, 9 Jan 2017 01:26:41 -0500 Received: from hazelnut (localhost.localdomain [127.0.0.1]) by hazelnut (Postfix) with ESMTP id 3B4856D for ; Mon, 9 Jan 2017 01:26:41 -0500 (EST) Received: from sendprodmail02.cc.columbia.edu (sendprodmail02.cc.columbia.edu [128.59.72.14]) by hazelnut (Postfix) with ESMTP id 235BC81 for ; Mon, 9 Jan 2017 01:26:41 -0500 (EST) Received: from mail-qk0-f198.google.com (mail-qk0-f198.google.com [209.85.220.198]) by sendprodmail02.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id v096QeIh043265 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 9 Jan 2017 01:26:40 -0500 Received: by mail-qk0-f198.google.com with SMTP id t84so101000243qke.7 for ; Sun, 08 Jan 2017 22:26:40 -0800 (PST) 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=/ec+uGO9+kNfDFnxrkcCuHXHLACs9jYV9puqcNFv9Bw=; b=F7hj8zK69SLBHt0DkFcR4IVkpiKNPaoNdXfsaMiN3IRkOGyQwTsNld/2fhDjVUStOU yDW0EJxJA9q2YiyBgK75fDL2TvkAvmjlNxGCAvWEVz+QtETwYMp/39oHpTrsToSAKrHf pnewVajPY0XT5edeFIfgcxMq7QjAZx+2dK4gctBv0cxi5DKI/zPIOB0PaosYP0iZwO3h j2o3QGB6eqNWMiBesHk6nIYKy01TcTWIGOLMD2bBVkNsGsJ+ibNvSGgAGRnxnE3aVdjA c90qa7mo4YRgK9LibovhapeGnI+H0rlaPnAd97O+Ee3acrHE+LFPwMVxyyqa1BwdMCfO gFDg== X-Gm-Message-State: AIkVDXKRanY6tQ+yDnITvc7ijJoAdqFfnKE5DgCZlSUDo1JMU8TGPTzXUengzP/VaRQthPPsqBoq6KtT8TUOpFira+E+KKm5PYxAqkELE6GZkmLWqFHgOiNH+8JLqNlDX96TTLOzqDbXS1g= X-Received: by 10.55.161.11 with SMTP id k11mr86104987qke.149.1483943200546; Sun, 08 Jan 2017 22:26:40 -0800 (PST) X-Received: by 10.55.161.11 with SMTP id k11mr86104191qke.149.1483943181034; Sun, 08 Jan 2017 22:26:21 -0800 (PST) Received: from jintack.cs.columbia.edu ([2001:18d8:ffff:16:21a:4aff:feaa:f900]) by smtp.gmail.com with ESMTPSA id h3sm8623257qtc.6.2017.01.08.22.26.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Jan 2017 22:26:20 -0800 (PST) From: Jintack Lim To: christoffer.dall@linaro.org, marc.zyngier@arm.com, pbonzini@redhat.com, rkrcmar@redhat.com, linux@armlinux.org.uk, catalin.marinas@arm.com, will.deacon@arm.com, vladimir.murzin@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com, james.morse@arm.com, lorenzo.pieralisi@arm.com, kevin.brodsky@arm.com, wcohen@redhat.com, shankerd@codeaurora.org, geoff@infradead.org, andre.przywara@arm.com, eric.auger@redhat.com, anna-maria@linutronix.de, shihwei@cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: jintack@cs.columbia.edu Subject: [RFC 39/55] KVM: arm/arm64: Add mmu context for the nesting Date: Mon, 9 Jan 2017 01:24:35 -0500 Message-Id: <1483943091-1364-40-git-send-email-jintack@cs.columbia.edu> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1483943091-1364-1-git-send-email-jintack@cs.columbia.edu> References: <1483943091-1364-1-git-send-email-jintack@cs.columbia.edu> X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.78 on 128.59.72.14 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the shadow stage-2 MMU context to be used for the nesting, but don't do anything with it yet. The host hypervisor maintains mmu structures for each nested VM. When entering a nested VM, the host hypervisor searches for the nested VM's mmu using vmid as a key. Note that this vmid is from the guest hypervisor's point of view. Signed-off-by: Jintack Lim --- arch/arm/include/asm/kvm_host.h | 3 ++ arch/arm/kvm/arm.c | 1 + arch/arm64/include/asm/kvm_emulate.h | 13 ++++----- arch/arm64/include/asm/kvm_host.h | 19 +++++++++++++ arch/arm64/include/asm/kvm_mmu.h | 31 ++++++++++++++++++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/context.c | 2 +- arch/arm64/kvm/mmu-nested.c | 55 ++++++++++++++++++++++++++++++++++++ 8 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 arch/arm64/kvm/mmu-nested.c diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index da45394..fbde48d 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -82,6 +82,9 @@ struct kvm_arch { * here. */ + /* Never used on arm but added to be compatible with arm64 */ + struct list_head nested_mmu_list; + /* Interrupt controller */ struct vgic_dist vgic; int max_vcpus; diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index 371b38e7..147df97 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -146,6 +146,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) /* Mark the initial VMID generation invalid */ kvm->arch.mmu.vmid.vmid_gen = 0; kvm->arch.mmu.el2_vmid.vmid_gen = 0; + INIT_LIST_HEAD(&kvm->arch.nested_mmu_list); /* The maximum number of VCPUs is limited by the host's GIC model */ kvm->arch.max_vcpus = vgic_present ? diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 94068e7..abad676 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -183,6 +183,11 @@ static inline bool vcpu_el2_imo_is_set(const struct kvm_vcpu *vcpu) return (vcpu_el2_reg(vcpu, HCR_EL2) & HCR_IMO); } +static inline bool vcpu_nested_stage2_enabled(const struct kvm_vcpu *vcpu) +{ + return (vcpu_el2_reg(vcpu, HCR_EL2) & HCR_VM); +} + static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu) { return vcpu->arch.fault.esr_el2; @@ -363,12 +368,4 @@ static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu, return data; /* Leave LE untouched */ } -static inline struct kvm_s2_vmid *vcpu_get_active_vmid(struct kvm_vcpu *vcpu) -{ - if (unlikely(vcpu_mode_el2(vcpu))) - return &vcpu->kvm->arch.mmu.el2_vmid; - - return &vcpu->kvm->arch.mmu.vmid; -} - #endif /* __ARM64_KVM_EMULATE_H__ */ diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index b33d35d..23e2267 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -65,6 +65,22 @@ struct kvm_s2_mmu { pgd_t *pgd; }; +/* Per nested VM mmu structure */ +struct kvm_nested_s2_mmu { + struct kvm_s2_mmu mmu; + + /* + * The vttbr value set by the guest hypervisor for this nested VM. + * vmid field is used as a key to search for this mmu structure among + * all nested VM mmu structures by the host hypervisor. + * baddr field is used to determine if we need to unmap stage 2 + * shadow page tables. + */ + u64 virtual_vttbr; + + struct list_head list; +}; + struct kvm_arch { /* Stage 2 paging state for the VM */ struct kvm_s2_mmu mmu; @@ -80,6 +96,9 @@ struct kvm_arch { /* Timer */ struct arch_timer_kvm timer; + + /* Stage 2 shadow paging contexts for nested L2 VM */ + struct list_head nested_mmu_list; }; #define KVM_NR_MEM_OBJS 40 diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h index a504162..d1ef650 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -112,6 +112,7 @@ #include #include #include +#include static inline unsigned long __kern_hyp_va(unsigned long v) { @@ -323,6 +324,21 @@ static inline unsigned int kvm_get_vmid_bits(void) return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; } +#ifdef CONFIG_KVM_ARM_NESTED_HYP +struct kvm_nested_s2_mmu *get_nested_mmu(struct kvm_vcpu *vcpu, u64 vttbr); +struct kvm_s2_mmu *vcpu_get_active_s2_mmu(struct kvm_vcpu *vcpu); +#else +static inline struct kvm_nested_s2_mmu *get_nested_mmu(struct kvm_vcpu *vcpu, + u64 vttbr) +{ + return NULL; +} +static inline struct kvm_s2_mmu *vcpu_get_active_s2_mmu(struct kvm_vcpu *vcpu) +{ + return &vcpu->kvm->arch.mmu; +} +#endif + static inline u64 kvm_get_vttbr(struct kvm_s2_vmid *vmid, struct kvm_s2_mmu *mmu) { @@ -334,5 +350,20 @@ static inline u64 kvm_get_vttbr(struct kvm_s2_vmid *vmid, return baddr | vmid_field; } +static inline u64 get_vmid(u64 vttbr) +{ + return (vttbr & VTTBR_VMID_MASK(get_kvm_vmid_bits()))>>VTTBR_VMID_SHIFT; +} + +static inline struct kvm_s2_vmid *vcpu_get_active_vmid(struct kvm_vcpu *vcpu) +{ + struct kvm_s2_mmu *mmu = vcpu_get_active_s2_mmu(vcpu); + + if (unlikely(vcpu_mode_el2(vcpu))) + return &mmu->el2_vmid; + else + return &mmu->vmid; +} + #endif /* __ASSEMBLY__ */ #endif /* __ARM64_KVM_MMU_H__ */ diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 8573faf..b0b1074 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -36,5 +36,6 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += handle_exit_nested.o +kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += mmu-nested.o kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += emulate-nested.o kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += $(KVM)/arm/vgic/vgic-v2-nested.o diff --git a/arch/arm64/kvm/context.c b/arch/arm64/kvm/context.c index b2c0220..9ebc38f 100644 --- a/arch/arm64/kvm/context.c +++ b/arch/arm64/kvm/context.c @@ -91,7 +91,7 @@ static void create_shadow_el1_sysregs(struct kvm_vcpu *vcpu) static void setup_s2_mmu(struct kvm_vcpu *vcpu) { - struct kvm_s2_mmu *mmu = &vcpu->kvm->arch.mmu; + struct kvm_s2_mmu *mmu = vcpu_get_active_s2_mmu(vcpu); struct kvm_s2_vmid *vmid = vcpu_get_active_vmid(vcpu); vcpu->arch.hw_vttbr = kvm_get_vttbr(vmid, mmu); diff --git a/arch/arm64/kvm/mmu-nested.c b/arch/arm64/kvm/mmu-nested.c new file mode 100644 index 0000000..d52078f --- /dev/null +++ b/arch/arm64/kvm/mmu-nested.c @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 - Columbia University + * Author: Jintack Lim + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include +#include +#include +#include + +struct kvm_nested_s2_mmu *get_nested_mmu(struct kvm_vcpu *vcpu, u64 vttbr) +{ + struct kvm_nested_s2_mmu *mmu; + u64 target_vmid = get_vmid(vttbr); + struct list_head *nested_mmu_list = &vcpu->kvm->arch.nested_mmu_list; + + list_for_each_entry_rcu(mmu, nested_mmu_list, list) { + u64 vmid = get_vmid(mmu->virtual_vttbr); + + if (target_vmid == vmid) + return mmu; + } + return NULL; +} + +struct kvm_s2_mmu *vcpu_get_active_s2_mmu(struct kvm_vcpu *vcpu) +{ + struct kvm_nested_s2_mmu *nested_mmu; + + /* If we are NOT entering the nested VM, return mmu in kvm_arch */ + if (vcpu_mode_el2(vcpu) || !vcpu_nested_stage2_enabled(vcpu)) + return &vcpu->kvm->arch.mmu; + + /* Otherwise, search for nested_mmu in the list */ + nested_mmu = get_nested_mmu(vcpu, vcpu_el2_reg(vcpu, VTTBR_EL2)); + + /* When this function is called, nested_mmu should be in the list */ + BUG_ON(!nested_mmu); + + return &nested_mmu->mmu; +}