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: 9504121 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 5E6FC6071A for ; Mon, 9 Jan 2017 06:57:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4D7A2280D0 for ; Mon, 9 Jan 2017 06:57:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 414A32815E; Mon, 9 Jan 2017 06:57:19 +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=-3.7 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C8BC1280D0 for ; Mon, 9 Jan 2017 06:57:17 +0000 (UTC) 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 1cQTtK-00035O-Gd; Mon, 09 Jan 2017 06:57:14 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cQTrV-0000DQ-Ek for linux-arm-kernel@bombadil.infradead.org; Mon, 09 Jan 2017 06:55:21 +0000 Received: from outprodmail02.cc.columbia.edu ([128.59.72.51]) by merlin.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cQTPj-0005Ga-A4 for linux-arm-kernel@lists.infradead.org; Mon, 09 Jan 2017 06:26:39 +0000 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 v096Od11005146 for ; Mon, 9 Jan 2017 01:26:22 -0500 Received: from hazelnut (localhost.localdomain [127.0.0.1]) by hazelnut (Postfix) with ESMTP id 692E980 for ; Mon, 9 Jan 2017 01:26:22 -0500 (EST) Received: from sendprodmail02.cc.columbia.edu (sendprodmail02.cc.columbia.edu [128.59.72.14]) by hazelnut (Postfix) with ESMTP id 51A0180 for ; Mon, 9 Jan 2017 01:26:22 -0500 (EST) Received: from mail-qt0-f199.google.com (mail-qt0-f199.google.com [209.85.216.199]) by sendprodmail02.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id v096QMb9042783 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 9 Jan 2017 01:26:22 -0500 Received: by mail-qt0-f199.google.com with SMTP id l7so63079603qtd.2 for ; Sun, 08 Jan 2017 22:26:22 -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=nqBl/uf7oYZX6dfqFuG0NkDTyd5VnWUxTN9oj5AdzFUtUVg7z2cZK/iqlb64MU5VQ3 vasULDnMdnyvaBsgv4Pn46aLkI2EhJjfSe1Hkwuptz7v2jdEUHut2DfT2Hhsr+jURBCT U1f7ChnKYh3fgZiuZpWmQ2jzRFAWx+1OSHqFXvUQ2ApqctXyQVCdOUoW6r4A0PAli83W gh5+bbpsVTRZNWII0SoHjczlrEELnjzO3vlL7LqKpRV1qrBqlqvsW2spKaYYAFKEr0nx DhtRx7OrRJSM130s7/WCYjXQ+sTL9PzN59p6zk7YykVxoaD+V7zwQYzqC7Mw4sQNtTEy rtJA== X-Gm-Message-State: AIkVDXIZQWrfWncxFV2cXpvk7MyZUDqDfOgSUUktHwcDDyvuQFBJruG+gyB1NC7NczCg3diE21WK9t9sAo32EzF8ancv2KPIDQYU0M808QGiKL0a9aVXn2AudLOSxjWk5v+AHki1ruE5oamXXmPu4JchcrE4Otg2OIcvMg== X-Received: by 10.55.161.11 with SMTP id k11mr86104211qke.149.1483943181309; Sun, 08 Jan 2017 22:26:21 -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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170109_012639_426040_ADBC0CC1 X-CRM114-Status: GOOD ( 30.29 ) 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: jintack@cs.columbia.edu MIME-Version: 1.0 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 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; +}