From: cdall@linaro.org (Christoffer Dall)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC 39/55] KVM: arm/arm64: Add mmu context for the nesting
Date: Wed, 22 Feb 2017 14:34:41 +0100 [thread overview]
Message-ID: <20170222133441.GT26976@cbox> (raw)
In-Reply-To: <1483943091-1364-40-git-send-email-jintack@cs.columbia.edu>
On Mon, Jan 09, 2017 at 01:24:35AM -0500, Jintack Lim wrote:
> 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.
I feel like I'm missing some overall design description or rationale of
why this is needed. Can you expand on this commit message a bit?
>
> Signed-off-by: Jintack Lim <jintack@cs.columbia.edu>
> ---
> 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.
> + */
I don't really understand this comment in isolation - especially not the
baddr part.
> + 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 <asm/cacheflush.h>
> #include <asm/mmu_context.h>
> #include <asm/pgtable.h>
> +#include <asm/kvm_emulate.h>
>
> 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;
whitespacealertbetweentheshiftmarker.
> +}
> +
> +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 <jintack@cs.columbia.edu>
> + *
> + * 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 <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <linux/kvm_host.h>
> +
> +#include <asm/kvm_arm.h>
> +#include <asm/kvm_emulate.h>
> +#include <asm/kvm_mmu.h>
> +#include <asm/kvm_nested.h>
> +
> +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)
why is it sufficient to just look at the VMID and not having to consider
the baddr?
> + 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 */
this comment doesn't add any info not clear in the code
> + 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);
can you provide a slightly stronger rationale behind why this BUG_ON
should never fire - I don't feel convinced right now.
> +
> + return &nested_mmu->mmu;
> +}
> --
> 1.9.1
>
>
Thanks,
-Christoffer
next prev parent reply other threads:[~2017-02-22 13:34 UTC|newest]
Thread overview: 111+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-09 6:23 [RFC 00/55] Nested Virtualization on KVM/ARM Jintack Lim
2017-01-09 6:23 ` [RFC 01/55] arm64: Add missing TCR hw defines Jintack Lim
2017-01-09 6:23 ` [RFC 02/55] KVM: arm64: Add nesting config option Jintack Lim
2017-01-09 6:23 ` [RFC 03/55] KVM: arm64: Add KVM nesting feature Jintack Lim
2017-01-09 6:24 ` [RFC 04/55] KVM: arm64: Allow userspace to set PSR_MODE_EL2x Jintack Lim
2017-01-09 6:24 ` [RFC 05/55] KVM: arm64: Add vcpu_mode_el2 primitive to support nesting Jintack Lim
2017-01-09 6:24 ` [RFC 06/55] KVM: arm64: Add EL2 execution context for nesting Jintack Lim
2017-02-22 11:10 ` Christoffer Dall
2017-06-26 14:33 ` Jintack Lim
2017-07-03 9:03 ` Christoffer Dall
2017-07-03 9:32 ` Marc Zyngier
2017-07-03 9:54 ` Christoffer Dall
2017-07-03 14:44 ` Jintack Lim
2017-07-03 15:30 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 07/55] KVM: arm/arm64: Add virtual EL2 state emulation framework Jintack Lim
2017-02-22 11:12 ` Christoffer Dall
2017-06-01 20:05 ` Bandan Das
2017-06-02 11:51 ` Christoffer Dall
2017-06-02 17:36 ` Bandan Das
2017-06-02 19:06 ` Christoffer Dall
2017-06-02 19:25 ` Bandan Das
2017-01-09 6:24 ` [RFC 08/55] KVM: arm64: Set virtual EL2 context depending on the guest exception level Jintack Lim
2017-02-22 11:14 ` Christoffer Dall
2017-06-01 20:22 ` Bandan Das
2017-06-02 8:48 ` Marc Zyngier
2017-01-09 6:24 ` [RFC 09/55] KVM: arm64: Set shadow EL1 registers for virtual EL2 execution Jintack Lim
2017-02-22 11:19 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 10/55] KVM: arm64: Synchronize EL1 system registers on virtual EL2 entry and exit Jintack Lim
2017-06-06 20:16 ` Bandan Das
2017-06-07 4:26 ` Jintack Lim
2017-01-09 6:24 ` [RFC 11/55] KVM: arm64: Emulate taking an exception to the guest hypervisor Jintack Lim
2017-02-22 11:28 ` Christoffer Dall
2017-06-06 20:21 ` Bandan Das
2017-06-06 20:38 ` Jintack Lim
2017-06-06 22:07 ` Bandan Das
2017-06-06 23:16 ` Jintack Lim
2017-06-07 17:21 ` Bandan Das
2017-01-09 6:24 ` [RFC 12/55] KVM: arm64: Handle EL2 register access traps Jintack Lim
2017-02-22 11:30 ` Christoffer Dall
2017-02-22 11:31 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 13/55] KVM: arm64: Handle eret instruction traps Jintack Lim
2017-01-09 6:24 ` [RFC 14/55] KVM: arm64: Take account of system " Jintack Lim
2017-02-22 11:34 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 15/55] KVM: arm64: Trap EL1 VM register accesses in virtual EL2 Jintack Lim
2017-01-09 6:24 ` [RFC 16/55] KVM: arm64: Forward VM reg traps to the guest hypervisor Jintack Lim
2017-02-22 11:39 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 17/55] KVM: arm64: Trap SPSR_EL1, ELR_EL1 and VBAR_EL1 in virtual EL2 Jintack Lim
2017-02-22 11:40 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 18/55] KVM: arm64: Forward traps due to HCR_EL2.NV1 bit to the guest hypervisor Jintack Lim
2017-02-22 11:41 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 19/55] KVM: arm64: Trap CPACR_EL1 access in virtual EL2 Jintack Lim
2017-01-09 6:24 ` [RFC 20/55] KVM: arm64: Forward CPACR_EL1 traps to the guest hypervisor Jintack Lim
2017-01-09 6:24 ` [RFC 21/55] KVM: arm64: Forward HVC instruction " Jintack Lim
2017-02-22 11:47 ` Christoffer Dall
2017-06-26 15:21 ` Jintack Lim
2017-07-03 9:08 ` Christoffer Dall
2017-07-03 9:31 ` Andrew Jones
2017-07-03 9:51 ` Christoffer Dall
2017-07-03 12:03 ` Will Deacon
2017-07-03 12:35 ` Marc Zyngier
2017-07-03 13:29 ` Jintack Lim
2017-01-09 6:24 ` [RFC 22/55] KVM: arm64: Handle PSCI call from the guest Jintack Lim
2017-01-09 6:24 ` [RFC 23/55] KVM: arm64: Forward WFX to the guest hypervisor Jintack Lim
2017-01-09 6:24 ` [RFC 24/55] KVM: arm64: Forward FP exceptions " Jintack Lim
2017-01-09 6:24 ` [RFC 25/55] KVM: arm/arm64: Let vcpu thread modify its own active state Jintack Lim
2017-02-22 12:27 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 26/55] KVM: arm/arm64: Add VGIC data structures for the nesting Jintack Lim
2017-01-09 6:24 ` [RFC 27/55] KVM: arm/arm64: Emulate GICH interface on GICv2 Jintack Lim
2017-02-22 13:06 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 28/55] KVM: arm/arm64: Prepare vgic state for the nested VM Jintack Lim
2017-02-22 13:12 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 29/55] KVM: arm/arm64: Set up the prepared vgic state Jintack Lim
2017-01-09 6:24 ` [RFC 30/55] KVM: arm/arm64: Inject irqs to the guest hypervisor Jintack Lim
2017-02-22 13:16 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 31/55] KVM: arm/arm64: Inject maintenance interrupts " Jintack Lim
2017-02-22 13:19 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 32/55] KVM: arm/arm64: register GICH iodev for " Jintack Lim
2017-02-22 13:21 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 33/55] KVM: arm/arm64: Remove unused params in mmu functions Jintack Lim
2017-01-09 6:24 ` [RFC 34/55] KVM: arm/arm64: Abstract stage-2 MMU state into a separate structure Jintack Lim
2017-01-09 6:24 ` [RFC 35/55] KVM: arm/arm64: Support mmu for the virtual EL2 execution Jintack Lim
2017-02-22 13:38 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 36/55] KVM: arm64: Invalidate virtual EL2 TLB entries when needed Jintack Lim
2017-01-09 6:24 ` [RFC 37/55] KVM: arm64: Setup vttbr_el2 on each VM entry Jintack Lim
2017-01-09 6:24 ` [RFC 38/55] KVM: arm/arm64: Make mmu functions non-static Jintack Lim
2017-01-09 6:24 ` [RFC 39/55] KVM: arm/arm64: Add mmu context for the nesting Jintack Lim
2017-02-22 13:34 ` Christoffer Dall [this message]
2017-01-09 6:24 ` [RFC 40/55] KVM: arm/arm64: Handle vttbr_el2 write operation from the guest hypervisor Jintack Lim
2017-02-22 17:59 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 41/55] KVM: arm/arm64: Unmap/flush shadow stage 2 page tables Jintack Lim
2017-02-22 18:09 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 42/55] KVM: arm64: Implement nested Stage-2 page table walk logic Jintack Lim
2017-01-09 6:24 ` [RFC 43/55] KVM: arm/arm64: Handle shadow stage 2 page faults Jintack Lim
2017-01-09 6:24 ` [RFC 44/55] KVM: arm/arm64: Move kvm_is_write_fault to header file Jintack Lim
2017-01-09 6:24 ` [RFC 45/55] KVM: arm64: KVM: Inject stage-2 page faults Jintack Lim
2017-01-09 6:24 ` [RFC 46/55] KVM: arm64: Add more info to the S2 translation result Jintack Lim
2017-01-09 6:24 ` [RFC 47/55] KVM: arm/arm64: Forward the guest hypervisor's stage 2 permission faults Jintack Lim
2017-02-22 18:15 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 48/55] KVM: arm64: Emulate TLBI instruction Jintack Lim
2017-01-09 6:24 ` [RFC 49/55] KVM: arm64: Fixes to toggle_cache for nesting Jintack Lim
2017-01-09 6:24 ` [RFC 50/55] KVM: arm/arm64: Abstract kvm_phys_addr_ioremap() function Jintack Lim
2017-01-09 6:24 ` [RFC 51/55] KVM: arm64: Expose physical address of vcpu interface Jintack Lim
2017-01-09 6:24 ` [RFC 52/55] KVM: arm/arm64: Create a vcpu mapping for the nested VM Jintack Lim
2017-01-09 6:24 ` [RFC 53/55] KVM: arm64: Reflect shadow VMPIDR_EL2 value to MPIDR_EL1 Jintack Lim
2017-01-09 6:24 ` [RFC 54/55] KVM: arm/arm64: Adjust virtual offset considering nesting Jintack Lim
2017-02-22 19:28 ` Christoffer Dall
2017-01-09 6:24 ` [RFC 55/55] KVM: arm64: Enable nested virtualization Jintack Lim
2017-01-09 15:05 ` [RFC 00/55] Nested Virtualization on KVM/ARM David Hildenbrand
2017-01-10 16:18 ` Jintack Lim
2017-02-22 18:23 ` Christoffer Dall
2017-02-24 10:28 ` Jintack Lim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170222133441.GT26976@cbox \
--to=cdall@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).