KVM/arm64: A stage 2 for the host
Subsystems (3)
cover KVM/arm64: A stage 2 for the host
Hi all,
This RFC series provides the infrastructure enabling to wrap the host kernel with a stage 2 when running KVM in nVHE. This can be useful for several use-cases, but the primary motivation is to (eventually) be able to protect guest memory from the host kernel. More details about the overall idea, design, and motivations can be found in Will's talk at KVM Forum 2020 [1], or the pKVM talk at the Android uconf during LPC 2020 [2].
This series essentially gets us to a point where the 'VM' bit is set in the host's HCR_EL2 when running in nVHE and if 'kvm-arm.protected' is set on the kernel command line. The EL2 object directly handles memory aborts from the host and manages entirely its stage 2 page table.
However, this series does _not_ provide any real user for this (yet) and simply idmaps everything into the host stage 2 as RWX cacheable. This is all about the infrastructure for now, so clearly not ready for inclusion upstream yet (hence the RFC tag), but the bases are there and I thought it'd be useful to start a discussion with the community early as this is a rather intrusive change. So, here goes.
One of the interesting requirements that comes with the series is that managing page-tables requires some sort of memory allocator at EL2 to allocate, refcount and free memory pages. Clearly, none of that is currently possible in nVHE, so a significant chunk of the series is dedicated to solving that problem. The proposed EL2 memory allocator mimics Linux' buddy system in principles, and re-uses some of the arm64 mm design choices. Specifically, it uses a vmemmap at EL2 which contains a set of struct hyp_page entries to hold pages metadata. To support this, I extended the EL2 object to make it manage its own stage 1 page-table in addition to host stage 2. This simplifies the hyp_vmemmap creation and was going to be required anyway for the protected VM use-case -- the threat model implies the host cannot be trusted after boot, and it will thus be crucial to ensure it cannot map arbitrary code at EL2.
The pool of memory pages used by the EL2 allocator are reserved by the host early during boot (while it is still trusted) using the memblock API, and are donated to EL2 during KVM init. The current assumption is that the host reserves enough pages to allow the EL2 object to map all of memory at page granularity for both hyp stage 1 and host stage 2, plus some extra pages for device mappings.
On top of that the series introduces a few smaller features that are needed along the way, but hopefully all of those are detailed properly in the relevant commit messages.
And as a last note, I'd like to point out that there are at this point trivial ways for the host to circumvent its stage 2 protection. It still owns the guests stage 2 for example, meaning that nothing would prevent a malicious host from using a guest as a proxy to access protected memory, _yet_. This series lays the ground for future work to address these things, which will clearly require a stage 2 over the host at some point, so I just wanted to set the expectations right.
With all that in mind, the series is organized as follows:
- patches 01-03 provide EL2 with some utility libraries needed for memory management and synchronization;
- patches 04-09 mostly refactor smalls portions of the code to ease the EL2 memory management;
- patches 10-17 add the actual EL2 memory management code, as well as the setup/bootstrap code on the KVM init path;
- patches 18-24 refactor the existing stage 2 management code to make it re-usable from the EL2 object;
- and finally patches 25-27 introduce the host stage 2 and the trap handling logic at EL2.
This work is based on the latest kvmarm/queue (which includes Marc's host EL2 entry rework [3], as well as Will's guest vector refactoring [4]) + David's PSCI proxying series [5].
And if you'd like a branch that has all the bits and pieces:
https://android-kvm.googlesource.com/linux qperret/host-stage2
Boot-tested (host and guest) using qemu in VHE and nVHE, and on real hardware on a AML-S905X-CC (Le Potato).
Thanks,
Quentin
[1] https://kvmforum2020.sched.com/event/eE24/virtualization-for-the-masses-exposing-kvm-on-android-will-deacon-google
[2] https://youtu.be/54q6RzS9BpQ?t=10859
[3] https://lore.kernel.org/kvmarm/[email protected]/
[4] https://lore.kernel.org/kvmarm/[email protected]/
[5] https://lore.kernel.org/kvmarm/[email protected]/
Quentin Perret (24):
KVM: arm64: Initialize kvm_nvhe_init_params early
KVM: arm64: Avoid free_page() in page-table allocator
KVM: arm64: Factor memory allocation out of pgtable.c
KVM: arm64: Introduce a BSS section for use at Hyp
KVM: arm64: Make kvm_call_hyp() a function call at Hyp
KVM: arm64: Allow using kvm_nvhe_sym() in hyp code
KVM: arm64: Introduce an early Hyp page allocator
KVM: arm64: Stub CONFIG_DEBUG_LIST at Hyp
KVM: arm64: Introduce a Hyp buddy page allocator
KVM: arm64: Enable access to sanitized CPU features at EL2
KVM: arm64: Factor out vector address calculation of/fdt: Introduce early_init_dt_add_memory_hyp()
KVM: arm64: Prepare Hyp memory protection
KVM: arm64: Elevate Hyp mappings creation at EL2
KVM: arm64: Use kvm_arch for stage 2 pgtable
KVM: arm64: Use kvm_arch in kvm_s2_mmu
KVM: arm64: Set host stage 2 using kvm_nvhe_init_params
KVM: arm64: Refactor kvm_arm_setup_stage2()
KVM: arm64: Refactor __load_guest_stage2()
KVM: arm64: Refactor __populate_fault_info()
KVM: arm64: Make memcache anonymous in pgtable allocator
KVM: arm64: Reserve memory for host stage 2
KVM: arm64: Sort the memblock regions list
KVM: arm64: Wrap the host with a stage 2
Will Deacon (3):
arm64: lib: Annotate {clear,copy}_page() as position-independent
KVM: arm64: Link position-independent string routines into .hyp.text
KVM: arm64: Add standalone ticket spinlock implementation for use at
hyp
arch/arm64/include/asm/cpufeature.h | 1 + arch/arm64/include/asm/hyp_image.h | 4 + arch/arm64/include/asm/kvm_asm.h | 13 +- arch/arm64/include/asm/kvm_cpufeature.h | 19 ++ arch/arm64/include/asm/kvm_host.h | 17 +- arch/arm64/include/asm/kvm_hyp.h | 8 + arch/arm64/include/asm/kvm_mmu.h | 69 +++++- arch/arm64/include/asm/kvm_pgtable.h | 41 +++- arch/arm64/include/asm/sections.h | 1 + arch/arm64/kernel/asm-offsets.c | 3 + arch/arm64/kernel/cpufeature.c | 14 +- arch/arm64/kernel/image-vars.h | 35 +++ arch/arm64/kernel/vmlinux.lds.S | 7 + arch/arm64/kvm/arm.c | 136 +++++++++-- arch/arm64/kvm/hyp/Makefile | 2 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 36 +-- arch/arm64/kvm/hyp/include/nvhe/early_alloc.h | 14 ++ arch/arm64/kvm/hyp/include/nvhe/gfp.h | 32 +++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 33 +++ arch/arm64/kvm/hyp/include/nvhe/memory.h | 55 +++++ arch/arm64/kvm/hyp/include/nvhe/mm.h | 107 +++++++++ arch/arm64/kvm/hyp/include/nvhe/spinlock.h | 95 ++++++++ arch/arm64/kvm/hyp/include/nvhe/util.h | 25 ++ arch/arm64/kvm/hyp/nvhe/Makefile | 9 +- arch/arm64/kvm/hyp/nvhe/cache.S | 13 ++ arch/arm64/kvm/hyp/nvhe/cpufeature.c | 8 + arch/arm64/kvm/hyp/nvhe/early_alloc.c | 60 +++++ arch/arm64/kvm/hyp/nvhe/hyp-init.S | 39 ++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 50 ++++ arch/arm64/kvm/hyp/nvhe/hyp.lds.S | 1 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 191 ++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mm.c | 175 ++++++++++++++ arch/arm64/kvm/hyp/nvhe/page_alloc.c | 185 +++++++++++++++ arch/arm64/kvm/hyp/nvhe/psci-relay.c | 7 +- arch/arm64/kvm/hyp/nvhe/setup.c | 214 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/stub.c | 22 ++ arch/arm64/kvm/hyp/nvhe/switch.c | 12 +- arch/arm64/kvm/hyp/nvhe/tlb.c | 4 +- arch/arm64/kvm/hyp/pgtable.c | 98 ++++---- arch/arm64/kvm/hyp/reserved_mem.c | 95 ++++++++ arch/arm64/kvm/mmu.c | 114 +++++++++- arch/arm64/kvm/reset.c | 42 +--- arch/arm64/lib/clear_page.S | 4 +- arch/arm64/lib/copy_page.S | 4 +- arch/arm64/mm/init.c | 3 + drivers/of/fdt.c | 5 + 46 files changed, 1971 insertions(+), 151 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_cpufeature.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/early_alloc.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/gfp.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/memory.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/mm.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/spinlock.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/util.h create mode 100644 arch/arm64/kvm/hyp/nvhe/cache.S create mode 100644 arch/arm64/kvm/hyp/nvhe/cpufeature.c create mode 100644 arch/arm64/kvm/hyp/nvhe/early_alloc.c create mode 100644 arch/arm64/kvm/hyp/nvhe/mem_protect.c create mode 100644 arch/arm64/kvm/hyp/nvhe/mm.c create mode 100644 arch/arm64/kvm/hyp/nvhe/page_alloc.c create mode 100644 arch/arm64/kvm/hyp/nvhe/setup.c create mode 100644 arch/arm64/kvm/hyp/nvhe/stub.c create mode 100644 arch/arm64/kvm/hyp/reserved_mem.c
Signature · 1 line
2.29.2.299.gdc1121823c-goog
RFC PATCH 1/27 arm64: lib: Annotate {clear,copy}_page() as position-independent
From: Will Deacon <[email protected]>
clear_page() and copy_page() are suitable for use outside of the kernel address space, so annotate them as position-independent code.
--- arch/arm64/lib/clear_page.S | 4 ++-- arch/arm64/lib/copy_page.S | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
arch/arm64/lib/clear_page.S +2 -2
@@ -14,7 +14,7 @@ * Parameters: * x0 - dest */ -SYM_FUNC_START(clear_page) +SYM_FUNC_START_PI(clear_page) mrs x1, dczid_el0 and w1, w1, #0xf mov x2, #4
@@ -25,5 +25,5 @@ SYM_FUNC_START(clear_page) tst x0, #(PAGE_SIZE - 1) b.ne 1b ret -SYM_FUNC_END(clear_page) +SYM_FUNC_END_PI(clear_page) EXPORT_SYMBOL(clear_page)
arch/arm64/lib/copy_page.S +2 -2
@@ -17,7 +17,7 @@ * x0 - dest * x1 - src */ -SYM_FUNC_START(copy_page) +SYM_FUNC_START_PI(copy_page) alternative_if ARM64_HAS_NO_HW_PREFETCH // Prefetch three cache lines ahead. prfm pldl1strm, [x1, #128]
@@ -75,5 +75,5 @@ alternative_else_nop_endif stnp x16, x17, [x0, #112 - 256] ret -SYM_FUNC_END(copy_page) +SYM_FUNC_END_PI(copy_page) EXPORT_SYMBOL(copy_page)
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 2/27 KVM: arm64: Link position-independent string routines into .hyp.text
In reply to Quentin PerretFrom: Will Deacon <[email protected]>
Pull clear_page(), copy_page(), memcpy() and memset() into the nVHE hyp code and ensure that we always execute the '__pi_' entry point on the offchance that it changes in future.
[ qperret: Commit title nits ]
--- arch/arm64/kernel/image-vars.h | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 4 ++++ 2 files changed, 15 insertions(+)
arch/arm64/kernel/image-vars.h +11 -0
@@ -105,6 +105,17 @@ KVM_NVHE_ALIAS(__stop___kvm_ex_table);/* Array containing bases of nVHE per-CPU memory regions. */ KVM_NVHE_ALIAS (kvm_arm_hyp_percpu_base); +/* Position-independent library routines */ +__kvm_nvhe_clear_page = __kvm_nvhe___pi_clear_page; +__kvm_nvhe_copy_page = __kvm_nvhe___pi_copy_page; +__kvm_nvhe_memcpy = __kvm_nvhe___pi_memcpy; +__kvm_nvhe_memset = __kvm_nvhe___pi_memset; + +#ifdef CONFIG_KASAN +__kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; +__kvm_nvhe___memset = __kvm_nvhe___pi_memset; +#endif +#endif /* CONFIG_KVM */ #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
arch/arm64/kvm/hyp/nvhe/Makefile +4 -0
@@ -6,10 +6,14 @@ asflags-y := -D__KVM_NVHE_HYPERVISOR__ ccflags-y := -D__KVM_NVHE_HYPERVISOR__ +lib-objs := clear_page.o copy_page.o memcpy.o memset.o +lib-objs := $(addprefix ../../../lib/, $(lib-objs)) + obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o +obj-y += $(lib-objs) ## ## Build rules for compiling nVHE hyp code
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 3/27 KVM: arm64: Add standalone ticket spinlock implementation for use at hyp
In reply to Quentin PerretFrom: Will Deacon <[email protected]>
We will soon need to synchronise multiple CPUs in the hyp text at EL2. The qspinlock-based locking used by the host is overkill for this purpose and requires a working "percpu" implementation for the MCS nodes.
Implement a simple ticket locking scheme based heavily on the code removed by c11090474d70 ("arm64: locking: Replace ticket lock implementation with qspinlock").
[ qperret: removed the __KVM_NVHE_HYPERVISOR__ build-time check from
spinlock.h ]
--- arch/arm64/kvm/hyp/include/nvhe/spinlock.h | 95 ++++++++++++++++++++++ arch/arm64/kvm/hyp/include/nvhe/util.h | 25 ++++++ 2 files changed, 120 insertions(+) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/spinlock.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/util.h
arch/arm64/kvm/hyp/include/nvhe/spinlock.h new file +95 -0
@@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* A stand-alone ticket spinlock implementation, primarily for use by the +* non-VHE hypervisor code running at EL2. +* +* Copyright (C) 2020 Google LLC +* Author: Will Deacon <[email protected]> +* +* Heavily based on the implementation removed by c11090474d70 which was: +* Copyright (C) 2012 ARM Ltd. +*/ + +#ifndef __ARM64_KVM_HYP_SPINLOCK_H__ +#define __ARM64_KVM_HYP_SPINLOCK_H__ + +#include <asm/alternative.h> + +typedef union hyp_spinlock { + u32 __val; + struct { +#ifdef __AARCH64EB__ + u16 next, owner; +#else + u16 owner, next; +#endif + }; +} hyp_spinlock_t; + +#define hyp_spin_lock_init(l) \ +do { \ + *(l) = (hyp_spinlock_t){ .__val = 0 }; \ +} while (0) + +static inline void hyp_spin_lock(hyp_spinlock_t *lock) +{ + u32 tmp; + hyp_spinlock_t lockval, newval; + + asm volatile( + /* Atomically increment the next ticket. */ + ALTERNATIVE( + /* LL/SC */ +" prfm pstl1strm, %3\n" +"1: ldaxr %w0, %3\n" +" add %w1, %w0, #(1 << 16)\n" +" stxr %w2, %w1, %3\n" +" cbnz %w2, 1b\n", + /* LSE atomics */ +" .arch_extension lse\n" +" mov %w2, #(1 << 16)\n" +" ldadda %w2, %w0, %3\n" + __nops(3), + ARM64_HAS_LSE_ATOMICS) + + /* Did we get the lock? */ +" eor %w1, %w0, %w0, ror #16\n" +" cbz %w1, 3f\n" + /* + * No: spin on the owner. Send a local event to avoid missing an + * unlock before the exclusive load. + */ +" sevl\n" +"2: wfe\n" +" ldaxrh %w2, %4\n" +" eor %w1, %w2, %w0, lsr #16\n" +" cbnz %w1, 2b\n" + /* We got the lock. Critical section starts here. */ +"3:" + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock) + : "Q" (lock->owner) + : "memory"); +} + +static inline void hyp_spin_unlock(hyp_spinlock_t *lock) +{ + u64 tmp; + + asm volatile( + ALTERNATIVE( + /* LL/SC */ + " ldrh %w1, %0\n" + " add %w1, %w1, #1\n" + " stlrh %w1, %0", + /* LSE atomics */ + " .arch_extension lse\n" + " mov %w1, #1\n" + " staddlh %w1, %0\n" + __nops(1), + ARM64_HAS_LSE_ATOMICS) + : "=Q" (lock->owner), "=&r" (tmp) + : + : "memory"); +} + +#endif /* __ARM64_KVM_HYP_SPINLOCK_H__ */
arch/arm64/kvm/hyp/include/nvhe/util.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Standalone re-implementations of kernel interfaces for use at EL2. +* Copyright (C) 2020 Google LLC +* Author: Will Deacon <[email protected]> +*/ + +#ifndef __KVM_NVHE_HYPERVISOR__ +#error "Attempt to include nVHE code outside of EL2 object" +#endif + +#ifndef __ARM64_KVM_NVHE_UTIL_H__ +#define __ARM64_KVM_NVHE_UTIL_H__ + +/* Locking (hyp_spinlock_t) */ +#include <nvhe/spinlock.h> + +#undef spin_lock_init +#define spin_lock_init hyp_spin_lock_init +#undef spin_lock +#define spin_lock hyp_spin_lock +#undef spin_unlock +#define spin_unlock hyp_spin_unlock + +#endif /* __ARM64_KVM_NVHE_UTIL_H__ */
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 4/27 KVM: arm64: Initialize kvm_nvhe_init_params early
In reply to Quentin PerretMove the initialization of kvm_nvhe_init_params in a dedicated function that is run early, and only once during KVM init, rather than every time the KVM vectors are set and reset.
This also opens the opportunity for the hypervisor to change the init structs during boot, hence simplifying the replacement of host-provided page-tables and stacks by the ones the hypervisor will create for itself.
--- arch/arm64/kvm/arm.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
arch/arm64/kvm/arm.c +20 -8
@@ -1355,24 +1355,20 @@ static int kvm_init_vector_slots(void)return 0 ; } -static void cpu_init_hyp_mode (void ) +static void cpu_prepare_hyp_mode (int cpu) { -struct kvm_nvhe_init_params *params =this_cpu_ptr_nvhe_sym (kvm_init_params); -struct arm_smccc_res res; - -/* Switch from the HYP stub to our own HYP init vector */ -__hyp_set_vectors (kvm_get_idmap_vector ()); +struct kvm_nvhe_init_params *params =per_cpu_ptr_nvhe_sym (kvm_init_params, cpu);/* * Calculate the raw per-cpu offset without a translation from the * kernel's mapping to the linear mapping, and store it in tpidr_el2 * so that we can use adr_l to access per-cpu variables in EL2. */ - params->tpidr_el2 = (unsigned long )this_cpu_ptr_nvhe_sym (__per_cpu_start) - + params->tpidr_el2 = (unsigned long )per_cpu_ptr_nvhe_sym (__per_cpu_start, cpu) - (unsigned long )kvm_ksym_ref (CHOOSE_NVHE_SYM (__per_cpu_start)); params->vector_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_host_vector)); - params->stack_hyp_va =kern_hyp_va (__this_cpu_read (kvm_arm_hyp_stack_page) +PAGE_SIZE ); + params->stack_hyp_va =kern_hyp_va (per_cpu (kvm_arm_hyp_stack_page, cpu) +PAGE_SIZE ); params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_psci_cpu_entry)); params->pgd_pa =kvm_mmu_get_httbr ();
@@ -1381,6 +1377,15 @@ static void cpu_init_hyp_mode(void)* be read while the MMU is off. */ __flush_dcache_area (params,sizeof (*params)); +} + +static void cpu_init_hyp_mode (void ) +{ +struct kvm_nvhe_init_params *params; +struct arm_smccc_res res; + +/* Switch from the HYP stub to our own HYP init vector */ +__hyp_set_vectors (kvm_get_idmap_vector ());/* * Call initialization code, and switch to the full blown HYP code.
@@ -1389,6 +1394,7 @@ static void cpu_init_hyp_mode(void)* cpus_have_const_cap() wrapper. */ BUG_ON (!system_capabilities_finalized ()); + params =this_cpu_ptr_nvhe_sym (kvm_init_params);arm_smccc_1_1_hvc (KVM_HOST_SMCCC_FUNC (__kvm_hyp_init),virt_to_phys (params), &res);WARN_ON (res.a0 !=SMCCC_RET_SUCCESS );
@@ -1742,6 +1748,12 @@ static int init_hyp_mode(void)init_cpu_logical_map ();init_psci_relay (); +/* +* Prepare the CPU initialization parameters +*/ +for_each_possible_cpu (cpu) +cpu_prepare_hyp_mode (cpu); +return 0 ; out_err:
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 5/27 KVM: arm64: Avoid free_page() in page-table allocator
In reply to Quentin PerretCurrently, the KVM page-table allocator uses a mix of put_page() and free_page() calls depending on the context even though page-allocation is always achieved using variants of __get_free_page().
Make the code consitent by using put_page() throughout, and reduce the memory management API surface used by the page-table code. This will ease factoring out page-alloction from pgtable.c, which is a pre-requisite to creating page-tables at EL2.
--- arch/arm64/kvm/hyp/pgtable.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +5 -5
@@ -410,7 +410,7 @@ int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits)static int hyp_free_walker (u64 addr,u64 end,u32 level,kvm_pte_t *ptep,enum kvm_pgtable_walk_flags flag,void *const arg) { -free_page ((unsigned long )kvm_pte_follow (*ptep)); +put_page (virt_to_page (kvm_pte_follow (*ptep)));return 0 ; }
@@ -422,7 +422,7 @@ void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt) };WARN_ON (kvm_pgtable_walk (pgt,0 ,BIT (pgt->ia_bits), &walker)); -free_page ((unsigned long )pgt->pgd); +put_page (virt_to_page (pgt->pgd)); pgt->pgd =NULL ; }
@@ -551,7 +551,7 @@ static int stage2_map_walk_table_post(u64 addr, u64 end, u32 level,if (!data->anchor)return 0 ; -free_page ((unsigned long )kvm_pte_follow (*ptep)); +put_page (virt_to_page (kvm_pte_follow (*ptep)));put_page (virt_to_page (ptep));if (data->anchor == ptep) {
@@ -674,7 +674,7 @@ static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, }if (childp) -free_page ((unsigned long )childp); +put_page (virt_to_page (childp));return 0 ; }
@@ -871,7 +871,7 @@ static int stage2_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,put_page (virt_to_page (ptep));if (kvm_pte_table (pte, level)) -free_page ((unsigned long )kvm_pte_follow (pte)); +put_page (virt_to_page (kvm_pte_follow (pte)));return 0 ; }
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 6/27 KVM: arm64: Factor memory allocation out of pgtable.c
In reply to Quentin PerretIn preparation for enabling the creation of page-tables at EL2, factor all memory allocation out of the page-table code, hence making it re-usable with any compatible memory allocator.
No functional changes intended.
--- arch/arm64/include/asm/kvm_pgtable.h | 32 +++++++++- arch/arm64/kvm/hyp/pgtable.c | 90 +++++++++++++++++----------- arch/arm64/kvm/mmu.c | 70 +++++++++++++++++++++- 3 files changed, 154 insertions(+), 38 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +30 -2
@@ -13,17 +13,41 @@typedef u64 kvm_pte_t ; +/** +* struct kvm_pgtable_mm_ops - Memory management callbacks. +* @zalloc_page: Allocate a zeroed memory page. +* @zalloc_pages_exact: Allocate an exact number of zeroed memory pages. +* @free_pages_exact: Free an exact number of memory pages. +* @get_page: Increment the refcount on a page. +* @put_page: Decrement the refcount on a page. +* @page_count: Returns the refcount of a page. +* @phys_to_virt: Convert a physical address into a virtual address. +* @virt_to_phys: Convert a virtual address into a physical address. +*/ +struct kvm_pgtable_mm_ops { +void * (*zalloc_page)(void *arg); +void * (*zalloc_pages_exact)(size_t size); +void (*free_pages_exact)(void *addr,size_t size); +void (*get_page)(void *addr); +void (*put_page)(void *addr); +int (*page_count)(void *addr); +void * (*phys_to_virt)(phys_addr_t phys); +phys_addr_t (*virt_to_phys)(void *addr); +}; +/** * struct kvm_pgtable - KVM page-table. * @ia_bits: Maximum input address size, in bits. * @start_level: Level at which the page-table walk starts. * @pgd: Pointer to the first top-level entry of the page-table. +* @mm_ops: Memory management callbacks. * @mmu: Stage-2 KVM MMU struct. Unused for stage-1 page-tables. */ struct kvm_pgtable {u32 ia_bits;u32 start_level;kvm_pte_t *pgd; +struct kvm_pgtable_mm_ops *mm_ops;/* Stage-2 only */ struct kvm_s2_mmu *mmu;
@@ -86,10 +110,12 @@ struct kvm_pgtable_walker {* kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table. * @pgt: Uninitialised page-table structure to initialise. * @va_bits: Maximum virtual address bits. +* @mm_ops: Memory management callbacks. * * Return: 0 on success, negative error code on failure. */ -int kvm_pgtable_hyp_init (struct kvm_pgtable *pgt,u32 va_bits); +int kvm_pgtable_hyp_init (struct kvm_pgtable *pgt,u32 va_bits, +struct kvm_pgtable_mm_ops *mm_ops);/** * kvm_pgtable_hyp_destroy() - Destroy an unused hypervisor stage-1 page-table.
@@ -126,10 +152,12 @@ int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,* kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table. * @pgt: Uninitialised page-table structure to initialise. * @kvm: KVM structure representing the guest virtual machine. +* @mm_ops: Memory management callbacks. * * Return: 0 on success, negative error code on failure. */ -int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm *kvm); +int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm *kvm, +struct kvm_pgtable_mm_ops *mm_ops);/** * kvm_pgtable_stage2_destroy() - Destroy an unused guest stage-2 page-table.
arch/arm64/kvm/hyp/pgtable.c +56 -34
302 lines not on this page · raw message
arch/arm64/kvm/mmu.c +68 -2
100 lines not on this page · raw message
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 7/27 KVM: arm64: Introduce a BSS section for use at Hyp
In reply to Quentin PerretCurrently, the hyp code cannot make full use of a bss, as the kernel section is mapped read-only.
While this mapping could simply be changed to read-write, it would intermingle even more the hyp and kernel state than they currently are. Instead, introduce a __hyp_bss section, that uses reserved pages, and create the appropriate RW hyp mappings during KVM init.
--- arch/arm64/include/asm/sections.h | 1 + arch/arm64/kernel/vmlinux.lds.S | 7 +++++++ arch/arm64/kvm/arm.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/hyp.lds.S | 1 + 4 files changed, 20 insertions(+)
arch/arm64/include/asm/sections.h +1 -0
@@ -12,6 +12,7 @@ extern char __hibernate_exit_text_start[], __hibernate_exit_text_end[];extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];extern char __hyp_text_start[], __hyp_text_end[];extern char __hyp_data_ro_after_init_start[], __hyp_data_ro_after_init_end[]; +extern char __hyp_bss_start[], __hyp_bss_end[];extern char __idmap_text_start[], __idmap_text_end[];extern char __initdata_begin[], __initdata_end[];extern char __inittext_begin[], __inittext_end[];
arch/arm64/kernel/vmlinux.lds.S +7 -0
@@ -8,6 +8,13 @@ #define RO_EXCEPTION_TABLE_ALIGN 8 #define RUNTIME_DISCARD_EXIT +#define BSS_FIRST_SECTIONS \ + . = ALIGN(PAGE_SIZE); \ + __hyp_bss_start = .; \ + *(.hyp.bss) \ + . = ALIGN(PAGE_SIZE); \ + __hyp_bss_end = .; + #include <asm-generic/vmlinux.lds.h> #include <asm/cache.h> #include <asm/hyp_image.h>
arch/arm64/kvm/arm.c +11 -0
@@ -1709,7 +1709,18 @@ static int init_hyp_mode(void) goto out_err; } +/* +* .hyp.bss is placed at the beginning of the .bss section, so map that +* part RW, and the rest RO as the hyp shouldn't be touching it. +*/ err =create_hyp_mappings (kvm_ksym_ref (__bss_start), +kvm_ksym_ref (__hyp_bss_end),PAGE_HYP ); +if (err) { +kvm_err ("Cannot map hyp bss section: %d\n" , err); + goto out_err; + } + + err =create_hyp_mappings (kvm_ksym_ref (__hyp_bss_end),kvm_ksym_ref (__bss_stop),PAGE_HYP_RO );if (err) {kvm_err ("Cannot map bss section\n" );
arch/arm64/kvm/hyp/nvhe/hyp.lds.S +1 -0
@@ -17,4 +17,5 @@ SECTIONS { PERCPU_INPUT(L1_CACHE_BYTES) } HYP_SECTION(.data..ro_after_init) + HYP_SECTION(.bss) }
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 8/27 KVM: arm64: Make kvm_call_hyp() a function call at Hyp
In reply to Quentin Perretkvm_call_hyp() has some logic to issue a function call or a hypercall depending the EL at which the kernel is running. However, all the code compiled under __KVM_NVHE_HYPERVISOR__ is guaranteed to run only at EL2, and in this case a simple function call is needed.
Add ifdefery to kvm_host.h to symplify kvm_call_hyp() in .hyp.text.
--- arch/arm64/include/asm/kvm_host.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -557,6 +557,7 @@ int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);void kvm_arm_halt_guest (struct kvm *kvm);void kvm_arm_resume_guest (struct kvm *kvm); +#ifndef __KVM_NVHE_HYPERVISOR__#define kvm_call_hyp_nvhe (f, ...) \ ({ \ struct arm_smccc_res res; \
@@ -596,6 +597,11 @@ void kvm_arm_resume_guest(struct kvm *kvm); \ ret; \ }) +#else /* __KVM_NVHE_HYPERVISOR__ */ +#define kvm_call_hyp (f, ...) f(__VA_ARGS__) +#define kvm_call_hyp_ret (f, ...) f(__VA_ARGS__) +#define kvm_call_hyp_nvhe (f, ...) f(__VA_ARGS__) +#endif /* __KVM_NVHE_HYPERVISOR__ */ void force_vm_exit (const cpumask_t *mask);void kvm_mmu_wp_memory_region (struct kvm *kvm,int slot);
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 9/27 KVM: arm64: Allow using kvm_nvhe_sym() in hyp code
In reply to Quentin PerretIn order to allow the usage of code shared by the host and the hyp in static inline library function, allow the usage of kvm_nvhe_sym() at el2 by defaulting to the raw symbol name.
--- arch/arm64/include/asm/hyp_image.h | 4 ++++ arch/arm64/include/asm/kvm_asm.h | 4 ++-- arch/arm64/kvm/arm.c | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-)
arch/arm64/include/asm/hyp_image.h +4 -0
@@ -7,11 +7,15 @@#ifndef __ARM64_HYP_IMAGE_H__#define __ARM64_HYP_IMAGE_H__ +#ifndef __KVM_NVHE_HYPERVISOR__/* * KVM nVHE code has its own symbol namespace prefixed with __kvm_nvhe_, * to separate it from the kernel proper. */ #define kvm_nvhe_sym (sym) __kvm_nvhe_##sym +#else +#define kvm_nvhe_sym (sym) sym +#endif #ifdef LINKER_SCRIPT
arch/arm64/include/asm/kvm_asm.h +2 -2
@@ -173,11 +173,11 @@ struct kvm_s2_mmu;DECLARE_KVM_NVHE_SYM (__kvm_hyp_init);DECLARE_KVM_NVHE_SYM (__kvm_hyp_host_vector);DECLARE_KVM_HYP_SYM (__kvm_hyp_vector); -DECLARE_KVM_NVHE_SYM (__kvm_hyp_psci_cpu_entry);#define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init)#define __kvm_hyp_host_vector CHOOSE_NVHE_SYM(__kvm_hyp_host_vector)#define __kvm_hyp_vector CHOOSE_HYP_SYM(__kvm_hyp_vector) -#define __kvm_hyp_psci_cpu_entry CHOOSE_NVHE_SYM(__kvm_hyp_psci_cpu_entry) + +void kvm_nvhe_sym (__kvm_hyp_psci_cpu_entry )(void );extern unsigned long kvm_arm_hyp_percpu_base[NR_CPUS ];DECLARE_KVM_NVHE_SYM (__per_cpu_start);
arch/arm64/kvm/arm.c +1 -1
@@ -1369,7 +1369,7 @@ static void cpu_prepare_hyp_mode(int cpu) params->vector_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_host_vector)); params->stack_hyp_va =kern_hyp_va (per_cpu (kvm_arm_hyp_stack_page, cpu) +PAGE_SIZE ); - params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_psci_cpu_entry)); + params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref_nvhe (__kvm_hyp_psci_cpu_entry)); params->pgd_pa =kvm_mmu_get_httbr ();/*
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 10/27 KVM: arm64: Introduce an early Hyp page allocator
In reply to Quentin PerretWith nVHE, the host currently creates all s1 hypervisor mappings at EL1 during boot, installs them at EL2, and extends them as required (e.g. when creating a new VM). But in a world where the host is no longer trusted, it cannot have full control over the code mapped in the hypervisor.
In preparation for enabling the hypervisor to create its own s1 mappings during boot, introduce an early page allocator, with minimal functionality. This allocator is designed to be used only during early bootstrap of the hyp code when memory protection is enabled, which will then switch to using a full-fledged page allocator after init.
--- arch/arm64/kvm/hyp/include/nvhe/early_alloc.h | 14 +++++ arch/arm64/kvm/hyp/include/nvhe/memory.h | 24 ++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/early_alloc.c | 60 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/psci-relay.c | 5 +- 5 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/early_alloc.h create mode 100644 arch/arm64/kvm/hyp/include/nvhe/memory.h create mode 100644 arch/arm64/kvm/hyp/nvhe/early_alloc.c
arch/arm64/kvm/hyp/include/nvhe/early_alloc.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_HYP_EARLY_ALLOC_H +#define __KVM_HYP_EARLY_ALLOC_H + +#include <asm/kvm_pgtable.h> + +void hyp_early_alloc_init (void *virt,unsigned long size); +unsigned long hyp_early_alloc_nr_pages (void ); +void *hyp_early_alloc_page (void *arg); +void *hyp_early_alloc_contig (unsigned int nr_pages); + +extern struct kvm_pgtable_mm_ops hyp_early_alloc_mm_ops; + +#endif /* __KVM_HYP_EARLY_ALLOC_H */
arch/arm64/kvm/hyp/include/nvhe/memory.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_HYP_MEMORY_H +#define __KVM_HYP_MEMORY_H + +#include <asm/page.h> + +#include <linux/types.h> + +extern s64 hyp_physvirt_offset; + +#define __hyp_pa (virt) ((phys_addr_t)(virt) + hyp_physvirt_offset) +#define __hyp_va (virt) ((void *)((phys_addr_t)(virt) - hyp_physvirt_offset)) + +static inline void *hyp_phys_to_virt (phys_addr_t phys) +{ +return __hyp_va (phys); +} + +static inline phys_addr_t hyp_virt_to_phys (void *addr) +{ +return __hyp_pa (addr); +} + +#endif /* __KVM_HYP_MEMORY_H */
arch/arm64/kvm/hyp/nvhe/Makefile +1 -1
@@ -10,7 +10,7 @@ lib-objs := clear_page.o copy_page.o memcpy.o memset.o lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ - hyp-main.o hyp-smp.o psci-relay.o + hyp-main.o hyp-smp.o psci-relay.o early_alloc.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/early_alloc.c new file +60 -0
60 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/psci-relay.c +2 -3
@@ -14,6 +14,8 @@#include <kvm/arm_psci.h> #include <uapi/linux/psci.h> +#include <nvhe/memory.h> +#define INVALID_CPU_ID UINT_MAXextern char __kvm_hyp_cpu_entry[];
@@ -21,9 +23,6 @@ extern char __kvm_hyp_cpu_entry[];/* Config options set by the host. */ u32 __ro_after_init kvm_host_psci_version =PSCI_VERSION (0 ,0 );u32 __ro_after_init kvm_host_psci_function_id[PSCI_FN_MAX ]; -s64 __ro_after_init hyp_physvirt_offset; - -#define __hyp_pa (x) ((phys_addr_t)((x)) + hyp_physvirt_offset)struct kvm_host_psci_state {atomic_t pending_on;
Signature · 1 line
2.29.2.299.gdc1121823c-goog
RFC PATCH 11/27 KVM: arm64: Stub CONFIG_DEBUG_LIST at Hyp
In reply to Quentin PerretIn order to use the kernel list library at EL2, introduce stubs for the CONFIG_DEBUG_LIST out-of-lines calls.
--- arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/stub.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kvm/hyp/nvhe/stub.c
arch/arm64/kvm/hyp/nvhe/Makefile +1 -1
@@ -10,7 +10,7 @@ lib-objs := clear_page.o copy_page.o memcpy.o memset.o lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ - hyp-main.o hyp-smp.o psci-relay.o early_alloc.o + hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/stub.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Stubs for out-of-line function calls caused by re-using kernel +* infrastructure at EL2. +* +* Copyright (C) 2020 - Google LLC +*/ + +#include <linux/list.h> + +#ifdef CONFIG_DEBUG_LIST +bool __list_add_valid (struct list_head *new,struct list_head *prev, +struct list_head *next) +{ +return true; +} + +bool __list_del_entry_valid (struct list_head *entry) +{ +return true; +} +#endif
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 12/27 KVM: arm64: Introduce a Hyp buddy page allocator
In reply to Quentin PerretWhen memory protection is enabled, the hyp code will require a basic form of memory management in order to allocate and free memory pages at EL2. This is needed for various use-cases, including the creation of hyp mappings or the allocation of stage 2 page tables.
To address these use-case, introduce a simple memory allocator in the hyp code. The allocator is designed as a conventional 'buddy allocator', working with a page granularity. It allows to allocate and free physically contiguous pages from memory 'pools', with a guaranteed order alignment in the PA space. Each page in a memory pool is associated with a struct hyp_page which holds the page's metadata, including its refcount, as well as its current order, hence mimicking the kernel's buddy system in the GFP infrastructure. The hyp_page metadata are made accessible through a hyp_vmemmap, following the concept of SPARSE_VMEMMAP in the kernel.
--- arch/arm64/kvm/hyp/include/nvhe/gfp.h | 32 ++++ arch/arm64/kvm/hyp/include/nvhe/memory.h | 25 +++ arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/page_alloc.c | 185 +++++++++++++++++++++++ 4 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/gfp.h create mode 100644 arch/arm64/kvm/hyp/nvhe/page_alloc.c
arch/arm64/kvm/hyp/include/nvhe/gfp.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_HYP_GFP_H +#define __KVM_HYP_GFP_H + +#include <linux/list.h> + +#include <nvhe/memory.h> +#include <nvhe/spinlock.h> + +#define HYP_MAX_ORDER 11U +#define HYP_NO_ORDER UINT_MAX + +struct hyp_pool { +hyp_spinlock_t lock; +struct list_head free_area[HYP_MAX_ORDER +1 ]; +phys_addr_t range_start; +phys_addr_t range_end; +}; + +/* GFP flags */ +#define HYP_GFP_NONE 0 +#define HYP_GFP_ZERO 1 + +/* Allocation */ +void *hyp_alloc_pages (struct hyp_pool *pool,gfp_t mask,unsigned int order); +void hyp_get_page (void *addr); +void hyp_put_page (void *addr); + +/* Used pages cannot be freed */ +int hyp_pool_init (struct hyp_pool *pool,phys_addr_t phys, +unsigned int nr_pages,unsigned int used_pages); +#endif /* __KVM_HYP_GFP_H */
arch/arm64/kvm/hyp/include/nvhe/memory.h +25 -0
@@ -6,7 +6,17 @@#include <linux/types.h> +struct hyp_pool ; +struct hyp_page { +unsigned int refcount; +unsigned int order; +struct hyp_pool *pool; +struct list_head node; +}; +extern s64 hyp_physvirt_offset; +extern u64 __hyp_vmemmap; +#define hyp_vmemmap ((struct hyp_page *)__hyp_vmemmap)#define __hyp_pa (virt) ((phys_addr_t)(virt) + hyp_physvirt_offset)#define __hyp_va (virt) ((void *)((phys_addr_t)(virt) - hyp_physvirt_offset))
@@ -21,4 +31,19 @@ static inline phys_addr_t hyp_virt_to_phys(void *addr) return __hyp_pa(addr); } +#define hyp_phys_to_pfn (phys) ((phys) >> PAGE_SHIFT) +#define hyp_phys_to_page (phys) (&hyp_vmemmap[hyp_phys_to_pfn(phys)]) +#define hyp_virt_to_page (virt) hyp_phys_to_page(__hyp_pa(virt)) + +#define hyp_page_to_phys (page) ((phys_addr_t)((page) - hyp_vmemmap) << PAGE_SHIFT) +#define hyp_page_to_virt (page) __hyp_va(hyp_page_to_phys(page)) +#define hyp_page_to_pool (page) (((struct hyp_page *)page)->pool) + +static inline int hyp_page_count (void *addr) +{ +struct hyp_page *p =hyp_virt_to_page (addr); + +return p->refcount; +} +#endif /* __KVM_HYP_MEMORY_H */
arch/arm64/kvm/hyp/nvhe/Makefile +1 -1
@@ -10,7 +10,7 @@ lib-objs := clear_page.o copy_page.o memcpy.o memset.o lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ - hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o + hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/page_alloc.c new file +185 -0
185 lines not on this page · raw message
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 13/27 KVM: arm64: Enable access to sanitized CPU features at EL2
In reply to Quentin PerretIntroduce the infrastructure in KVM enabling to copy CPU feature registers into EL2-owned data-structures, to allow reading sanitised values directly at EL2 in nVHE.
Given that only a subset of these features are being read by the hypervisor, the ones that need to be copied are to be listed under <asm/kvm_cpufeature.h> together with the name of the nVHE variable that will hold the copy.
While at it, introduce the first user of this infrastructure by implementing __flush_dcache_area at EL2, which needs arm64_ftr_reg_ctrel0.
--- arch/arm64/include/asm/cpufeature.h | 1 + arch/arm64/include/asm/kvm_cpufeature.h | 17 ++++++++++++++ arch/arm64/kernel/cpufeature.c | 12 ++++++++++ arch/arm64/kernel/image-vars.h | 2 ++ arch/arm64/kvm/arm.c | 31 +++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 3 ++- arch/arm64/kvm/hyp/nvhe/cache.S | 13 +++++++++++ arch/arm64/kvm/hyp/nvhe/cpufeature.c | 8 +++++++ 8 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_cpufeature.h create mode 100644 arch/arm64/kvm/hyp/nvhe/cache.S create mode 100644 arch/arm64/kvm/hyp/nvhe/cpufeature.c
arch/arm64/include/asm/cpufeature.h +1 -0
@@ -600,6 +600,7 @@ void __init setup_cpu_features(void);void check_local_cpu_capabilities (void );u64 read_sanitised_ftr_reg (u32 id); +int copy_ftr_reg (u32 id,struct arm64_ftr_reg *dst);static inline bool cpu_supports_mixed_endian_el0 (void ) {
arch/arm64/include/asm/kvm_cpufeature.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2020 - Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#include <asm/cpufeature.h> + +#ifndef KVM_HYP_CPU_FTR_REG +#if defined(__KVM_NVHE_HYPERVISOR__) +#define KVM_HYP_CPU_FTR_REG (id, name) extern struct arm64_ftr_reg name; +#else +#define KVM_HYP_CPU_FTR_REG (id, name) DECLARE_KVM_NVHE_SYM(name); +#endif +#endif + +KVM_HYP_CPU_FTR_REG (SYS_CTR_EL0 , arm64_ftr_reg_ctrel0)
arch/arm64/kernel/cpufeature.c +12 -0
@@ -1116,6 +1116,18 @@ u64 read_sanitised_ftr_reg(u32 id) }EXPORT_SYMBOL_GPL (read_sanitised_ftr_reg); +int copy_ftr_reg (u32 id,struct arm64_ftr_reg *dst) +{ +struct arm64_ftr_reg *regp =get_arm64_ftr_reg (id); + +if (!regp) +return -EINVAL ; + +memcpy (dst, regp,sizeof (*regp)); + +return 0 ; +} +#define read_sysreg_case (r) \ case r: return read_sysreg_s(r)
arch/arm64/kernel/image-vars.h +2 -0
@@ -116,6 +116,8 @@ __kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; __kvm_nvhe___memset = __kvm_nvhe___pi_memset;#endif +_kvm_nvhe___flush_dcache_area = __kvm_nvhe___pi___flush_dcache_area; +#endif /* CONFIG_KVM */ #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
arch/arm64/kvm/arm.c +31 -0
@@ -34,6 +34,7 @@#include <asm/virt.h> #include <asm/kvm_arm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_emulate.h> #include <asm/sections.h>
@@ -1636,6 +1637,29 @@ static void teardown_hyp_mode(void) } } +#undef KVM_HYP_CPU_FTR_REG +#define KVM_HYP_CPU_FTR_REG (id, name) \ + { .sys_id = id, .dst = (struct arm64_ftr_reg *)&kvm_nvhe_sym(name) }, +static const struct __ftr_reg_copy_entry { +u32 sys_id; +struct arm64_ftr_reg *dst; +} hyp_ftr_regs[] = { + #include <asm/kvm_cpufeature.h> +}; + +static int copy_cpu_ftr_regs (void ) +{ +int i, ret; + +for (i =0 ; i <ARRAY_SIZE (hyp_ftr_regs); i++) { + ret =copy_ftr_reg (hyp_ftr_regs[i].sys_id, hyp_ftr_regs[i].dst); +if (ret) +return ret; + } + +return 0 ; +} +/** * Inits Hyp-mode on all online CPUs */
@@ -1644,6 +1668,13 @@ static int init_hyp_mode(void)int cpu;int err =0 ; +/* +* Copy the required CPU feature register in their EL2 counterpart +*/ + err =copy_cpu_ftr_regs (); +if (err) +return err; +/* * Allocate Hyp PGD and setup Hyp identity mapping */
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
@@ -10,7 +10,8 @@ lib-objs := clear_page.o copy_page.o memcpy.o memset.o lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ - hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o + hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \ + cache.o cpufeature.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/cache.S new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Code copied from arch/arm64/mm/cache.S. + */ + +#include <linux/linkage.h> +#include <asm/assembler.h> +#include <asm/alternative.h> + +SYM_FUNC_START_PI(__flush_dcache_area) + dcache_by_line_op civac, sy, x0, x1, x2, x3 + ret +SYM_FUNC_END_PI(__flush_dcache_area)
arch/arm64/kvm/hyp/nvhe/cpufeature.c new file +8 -0
@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2020 - Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#define KVM_HYP_CPU_FTR_REG (id, name) struct arm64_ftr_reg name; +#include <asm/kvm_cpufeature.h>
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 14/27 KVM: arm64: Factor out vector address calculation
In reply to Quentin PerretIn order to re-map the guest vectors at EL2 when pKVM is enabled, refactor __kvm_vector_slot2idx() and kvm_init_vector_slot() to move all the address calculation logic in a static inline function.
--- arch/arm64/include/asm/kvm_mmu.h | 8 ++++++++ arch/arm64/kvm/arm.c | 9 +-------- 2 files changed, 9 insertions(+), 8 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +8 -0
@@ -171,6 +171,14 @@ phys_addr_t kvm_mmu_get_httbr(void);phys_addr_t kvm_get_idmap_vector (void );int kvm_mmu_init (void ); +static inline void *__kvm_vector_slot2addr (void *base, +enum arm64_hyp_spectre_vector slot) +{ +int idx = slot - (slot !=HYP_VECTOR_DIRECT ); + +return base + (idx *SZ_2K ); +} +struct kvm ;#define kvm_flush_dcache_to_poc (a,l) __flush_dcache_area((a), (l))
arch/arm64/kvm/arm.c +1 -8
@@ -1318,16 +1318,9 @@ static unsigned long nvhe_percpu_order(void)/* A lookup table holding the hypervisor VA for each vector slot */ static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS ]; -static int __kvm_vector_slot2idx (enum arm64_hyp_spectre_vector slot) -{ -return slot - (slot !=HYP_VECTOR_DIRECT ); -} -static void kvm_init_vector_slot (void *base,enum arm64_hyp_spectre_vector slot) { -int idx =__kvm_vector_slot2idx (slot); - - hyp_spectre_vector_selector[slot] = base + (idx *SZ_2K ); + hyp_spectre_vector_selector[slot] =__kvm_vector_slot2addr (base, slot); }static int kvm_init_vector_slots (void )
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 15/27 of/fdt: Introduce early_init_dt_add_memory_hyp()
In reply to Quentin PerretIntroduce early_init_dt_add_memory_hyp() to allow KVM to conserve a copy of the memory regions parsed from DT. This will be needed in the context of the protected nVHE feature of KVM/arm64 where the code running at EL2 will be cleanly separated from the host kernel during boot, and will need its own representation of memory.
--- drivers/of/fdt.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/of/fdt.c +5 -0
@@ -1099,6 +1099,10 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)#endif +void __init __weak early_init_dt_add_memory_hyp (u64 base,u64 size) +{ +} +void __init __weak early_init_dt_add_memory_arch (u64 base,u64 size) {const u64 phys_offset =MIN_MEMBLOCK_ADDR ;
@@ -1139,6 +1143,7 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) base = phys_offset; }memblock_add (base, size); +early_init_dt_add_memory_hyp (base, size); }int __init __weak early_init_dt_mark_hotplug_memory_arch (u64 base,u64 size)
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 16/27 KVM: arm64: Prepare Hyp memory protection
In reply to Quentin PerretWhen memory protection is enabled, the Hyp code needs the ability to create and manage its own page-table. To do so, introduce a new set of hypercalls to initialize Hyp memory protection.
During the init hcall, the hypervisor runs with the host-provided page-table and uses the trivial early page allocator to create its own set of page-tables, using a memory pool that was donated by the host. Specifically, the hypervisor creates its own mappings for __hyp_text, the Hyp memory pool, the __hyp_bss, the portion of hyp_vmemmap corresponding to the Hyp pool, among other things. It then jumps back in the idmap page, switches to use the newly-created pgd (instead of the temporary one provided by the host) and then installs the full-fledged buddy allocator which will then be the only one in used from then on.
Note that for the sake of symplifying the review, this only introduces the code doing this operation, without actually being called by anyhing yet. This will be done in a subsequent patch, which will introduce the necessary host kernel changes.
Credits to Will for __kvm_init_switch_pgd.
--- arch/arm64/include/asm/kvm_asm.h | 6 +- arch/arm64/include/asm/kvm_host.h | 8 + arch/arm64/include/asm/kvm_hyp.h | 8 + arch/arm64/kernel/cpufeature.c | 2 +- arch/arm64/kernel/image-vars.h | 19 +++ arch/arm64/kvm/hyp/Makefile | 2 +- arch/arm64/kvm/hyp/include/nvhe/memory.h | 6 + arch/arm64/kvm/hyp/include/nvhe/mm.h | 79 +++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 4 +- arch/arm64/kvm/hyp/nvhe/hyp-init.S | 30 ++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 44 +++++ arch/arm64/kvm/hyp/nvhe/mm.c | 175 ++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/psci-relay.c | 2 - arch/arm64/kvm/hyp/nvhe/setup.c | 196 +++++++++++++++++++++++ arch/arm64/kvm/hyp/reserved_mem.c | 75 +++++++++ arch/arm64/kvm/mmu.c | 2 +- arch/arm64/mm/init.c | 3 + 17 files changed, 653 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/mm.h create mode 100644 arch/arm64/kvm/hyp/nvhe/mm.c create mode 100644 arch/arm64/kvm/hyp/nvhe/setup.c create mode 100644 arch/arm64/kvm/hyp/reserved_mem.c
arch/arm64/include/asm/kvm_asm.h +5 -1
@@ -57,6 +57,10 @@#define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 12#define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs 13#define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs 14 +#define __KVM_HOST_SMCCC_FUNC___kvm_hyp_protect 15 +#define __KVM_HOST_SMCCC_FUNC___hyp_create_mappings 16 +#define __KVM_HOST_SMCCC_FUNC___hyp_create_private_mapping 17 +#define __KVM_HOST_SMCCC_FUNC___hyp_cpu_set_vector 18#ifndef __ASSEMBLY__
@@ -171,7 +175,7 @@ struct kvm_vcpu;struct kvm_s2_mmu ;DECLARE_KVM_NVHE_SYM (__kvm_hyp_init); -DECLARE_KVM_NVHE_SYM (__kvm_hyp_host_vector); +DECLARE_KVM_HYP_SYM (__kvm_hyp_host_vector);DECLARE_KVM_HYP_SYM (__kvm_hyp_vector);#define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init)#define __kvm_hyp_host_vector CHOOSE_NVHE_SYM(__kvm_hyp_host_vector)
arch/arm64/include/asm/kvm_host.h +8 -0
@@ -742,4 +742,12 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);#define kvm_vcpu_has_pmu (vcpu) \ (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features)) +#ifdef CONFIG_KVM +extern phys_addr_t hyp_mem_base; +extern phys_addr_t hyp_mem_size; +void __init reserve_kvm_hyp (void ); +#else +static inline void reserve_kvm_hyp (void ) { } +#endif +#endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/include/asm/kvm_hyp.h +8 -0
@@ -105,5 +105,13 @@ void __noreturn hyp_panic(void);void __noreturn __hyp_do_panic (bool restore_host,u64 spsr,u64 elr,u64 par);#endif +#ifdef __KVM_NVHE_HYPERVISOR__ +void __kvm_init_switch_pgd (phys_addr_t phys,unsigned long size, +phys_addr_t pgd,void *sp,void *cont_fn); +int __kvm_hyp_protect (phys_addr_t phys,unsigned long size, +unsigned long nr_cpus,unsigned long *per_cpu_base); +void __noreturn __host_enter (struct kvm_cpu_context *host_ctxt); +#endif +#endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kernel/cpufeature.c +1 -1
@@ -1722,7 +1722,7 @@ static void cpu_enable_mte(struct arm64_cpu_capabilities const *cap)#endif /* CONFIG_ARM64_MTE */ #ifdef CONFIG_KVM -static bool enable_protected_kvm; +bool enable_protected_kvm;static bool has_protected_kvm (const struct arm64_cpu_capabilities *entry,int __unused) {
arch/arm64/kernel/image-vars.h +19 -0
@@ -118,6 +118,25 @@ __kvm_nvhe___memset = __kvm_nvhe___pi_memset; _kvm_nvhe___flush_dcache_area = __kvm_nvhe___pi___flush_dcache_area; +/* Hypevisor VA size */ +KVM_NVHE_ALIAS (hyp_va_bits); + +/* Kernel memory sections */ +KVM_NVHE_ALIAS (__start_rodata); +KVM_NVHE_ALIAS (__end_rodata); +KVM_NVHE_ALIAS (__bss_start); +KVM_NVHE_ALIAS (__bss_stop); + +/* Hyp memory sections */ +KVM_NVHE_ALIAS (__hyp_idmap_text_start); +KVM_NVHE_ALIAS (__hyp_idmap_text_end); +KVM_NVHE_ALIAS (__hyp_text_start); +KVM_NVHE_ALIAS (__hyp_text_end); +KVM_NVHE_ALIAS (__hyp_data_ro_after_init_start); +KVM_NVHE_ALIAS (__hyp_data_ro_after_init_end); +KVM_NVHE_ALIAS (__hyp_bss_start); +KVM_NVHE_ALIAS (__hyp_bss_end); +#endif /* CONFIG_KVM */ #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
arch/arm64/kvm/hyp/Makefile +1 -1
@@ -10,4 +10,4 @@ subdir-ccflags-y := -I$(incdir) \ -DDISABLE_BRANCH_PROFILING \ $(DISABLE_STACKLEAK_PLUGIN) -obj-$(CONFIG_KVM) += vhe/ nvhe/ pgtable.o +obj-$(CONFIG_KVM) += vhe/ nvhe/ pgtable.o reserved_mem.o
arch/arm64/kvm/hyp/include/nvhe/memory.h +6 -0
@@ -6,6 +6,12 @@#include <linux/types.h> +#define HYP_MEMBLOCK_REGIONS 128 +struct hyp_memblock_region { +phys_addr_t start; +phys_addr_t end; +}; +struct hyp_pool ;struct hyp_page {unsigned int refcount;
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +79 -0
79 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/Makefile +2 -2
@@ -11,9 +11,9 @@ lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \ - cache.o cpufeature.o + cache.o cpufeature.o setup.o mm.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ - ../fpsimd.o ../hyp-entry.o ../exception.o + ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o obj-y += $(lib-objs) ##
arch/arm64/kvm/hyp/nvhe/hyp-init.S +30 -0
@@ -247,4 +247,34 @@ alternative_else_nop_endif SYM_CODE_END(__kvm_handle_stub_hvc) +SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] + mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isb + msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) + .popsection
arch/arm64/kvm/hyp/nvhe/hyp-main.c +44 -0
68 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/mm.c new file +175 -0
175 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/psci-relay.c +0 -2
@@ -193,8 +193,6 @@ static int psci_cpu_on(u64 func_id, struct kvm_cpu_context *host_ctxt)return ret; } -void __noreturn __host_enter (struct kvm_cpu_context *host_ctxt); - asmlinkage void __noreturn __kvm_hyp_psci_cpu_entry(void) {struct kvm_host_psci_state *cpu_state =this_cpu_ptr (&kvm_host_psci_state);
arch/arm64/kvm/hyp/nvhe/setup.c new file +196 -0
196 lines not on this page · raw message
arch/arm64/kvm/hyp/reserved_mem.c new file +75 -0
75 lines not on this page · raw message
arch/arm64/kvm/mmu.c +1 -1
@@ -1264,10 +1264,10 @@ static struct kvm_pgtable_mm_ops kvm_hyp_mm_ops = { .virt_to_phys = kvm_host_pa, }; +u32 hyp_va_bits;int kvm_mmu_init (void ) {int err; -u32 hyp_va_bits; hyp_idmap_start =__pa_symbol (__hyp_idmap_text_start); hyp_idmap_start =ALIGN_DOWN (hyp_idmap_start,PAGE_SIZE );
arch/arm64/mm/init.c +3 -0
@@ -34,6 +34,7 @@#include <asm/fixmap.h> #include <asm/kasan.h> #include <asm/kernel-pgtable.h> +#include <asm/kvm_host.h> #include <asm/memory.h> #include <asm/numa.h> #include <asm/sections.h>
@@ -390,6 +391,8 @@ void __init arm64_memblock_init(void)reserve_elfcorehdr (); +reserve_kvm_hyp (); + high_memory =__va (memblock_end_of_DRAM () -1 ) +1 ;dma_contiguous_reserve (arm64_dma32_phys_limit);
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 17/27 KVM: arm64: Elevate Hyp mappings creation at EL2
In reply to Quentin PerretPrevious commits have introduced infrastructure at EL2 to enable the Hyp code to manage its own memory, and more specifically its stage 1 page tables. However, this was preliminary work, and none of it is currently in use.
Put all of this together by elevating the hyp mappings creation at EL2 when memory protection is enabled. In this case, the host kernel running at EL1 still creates _temporary_ Hyp mappings, only used while initializing the hypervisor, but frees them right after, and flips a static key marking the new 'protected' mode of operation.
As such, all calls to create_hyp_mappings() after kvm init has finished turn into hypercalls, as the host now has no 'legal' way to modify the hypevisor page tables directly.
--- arch/arm64/include/asm/kvm_mmu.h | 1 - arch/arm64/kvm/arm.c | 51 ++++++++++++++++++++++++++++++-- arch/arm64/kvm/mmu.c | 34 +++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +0 -1
@@ -285,6 +285,5 @@ static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu)*/ asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); } -#endif /* __ASSEMBLY__ */ #endif /* __ARM64_KVM_MMU_H__ */
arch/arm64/kvm/arm.c +48 -3
93 lines not on this page · raw message
arch/arm64/kvm/mmu.c +34 -0
@@ -225,15 +225,39 @@ void free_hyp_pgds(void)if (hyp_pgtable) {kvm_pgtable_hyp_destroy (hyp_pgtable);kfree (hyp_pgtable); + hyp_pgtable =NULL ; }mutex_unlock (&kvm_hyp_pgd_mutex); } +static bool kvm_host_owns_hyp_mappings (void ) +{ +if (static_branch_likely (&kvm_protected_mode_initialized)) +return false; + +/* +* This can happen at boot time when __create_hyp_mappings() is called +* after the hyp protection has been enabled, but the static key has +* not been flipped yet. +*/ +if (!hyp_pgtable &&is_protected_kvm_enabled ()) +return false; + +BUG_ON (!hyp_pgtable); + +return true; +} +static int __create_hyp_mappings (unsigned long start,unsigned long size,unsigned long phys,enum kvm_pgtable_prot prot) {int err; +if (!kvm_host_owns_hyp_mappings ()) { +return kvm_call_hyp_nvhe (__hyp_create_mappings, + start, size, phys, prot); + } +mutex_lock (&kvm_hyp_pgd_mutex); err =kvm_pgtable_hyp_map (hyp_pgtable, start, size, phys, prot);mutex_unlock (&kvm_hyp_pgd_mutex);
@@ -295,6 +319,16 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,unsigned long base;int ret =0 ; +if (!kvm_host_owns_hyp_mappings ()) { + base =kvm_call_hyp_nvhe (__hyp_create_private_mapping, + phys_addr, size, prot); +if (!base) +return -ENOMEM ; + *haddr = base; + +return 0 ; + } +mutex_lock (&kvm_hyp_pgd_mutex);/*
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 18/27 KVM: arm64: Use kvm_arch for stage 2 pgtable
In reply to Quentin PerretIn order to make use of the stage 2 pgtable code for the host stage 2, use struct kvm_arch in lieu of struct kvm as the host will have the former but not the latter.
--- arch/arm64/include/asm/kvm_pgtable.h | 5 +++-- arch/arm64/kvm/hyp/pgtable.c | 6 +++--- arch/arm64/kvm/mmu.c | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +3 -2
@@ -151,12 +151,13 @@ int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,/** * kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table. * @pgt: Uninitialised page-table structure to initialise. -* @kvm: KVM structure representing the guest virtual machine. +* @arch: Arch-specific KVM structure representing the guest virtual +* machine. * @mm_ops: Memory management callbacks. * * Return: 0 on success, negative error code on failure. */ -int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm *kvm, +int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm_arch *arch,struct kvm_pgtable_mm_ops *mm_ops);/**
arch/arm64/kvm/hyp/pgtable.c +3 -3
@@ -855,11 +855,11 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)return kvm_pgtable_walk (pgt, addr, size, &walker); } -int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm *kvm, +int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm_arch *arch,struct kvm_pgtable_mm_ops *mm_ops) {size_t pgd_sz; -u64 vtcr = kvm->arch.vtcr; +u64 vtcr = arch->vtcr;u32 ia_bits =VTCR_EL2_IPA (vtcr);u32 sl0 =FIELD_GET (VTCR_EL2_SL0_MASK , vtcr);u32 start_level =VTCR_EL2_TGRAN_SL0_BASE - sl0;
@@ -872,7 +872,7 @@ int kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm *kvm, pgt->ia_bits = ia_bits; pgt->start_level = start_level; pgt->mm_ops = mm_ops; - pgt->mmu = &kvm->arch.mmu; + pgt->mmu = &arch->mmu;/* Ensure zeroed PGD pages are visible to the hardware walker */ dsb (ishst);
arch/arm64/kvm/mmu.c +1 -1
@@ -461,7 +461,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)if (!pgt)return -ENOMEM ; - err =kvm_pgtable_stage2_init (pgt, kvm, &kvm_s2_mm_ops); + err =kvm_pgtable_stage2_init (pgt, &kvm->arch, &kvm_s2_mm_ops);if (err) goto out_free_pgtable;
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 19/27 KVM: arm64: Use kvm_arch in kvm_s2_mmu
In reply to Quentin PerretIn order to make use of the stage 2 pgtable code for the host stage 2, change kvm_s2_mmu to use a kvm_arch pointer in lieu of the kvm pointer, as the host will have the former but not the latter.
--- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_mmu.h | 7 ++++++- arch/arm64/kvm/mmu.c | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -86,7 +86,7 @@ struct kvm_s2_mmu {/* The last vcpu id that ran on each physical CPU */ int __percpu *last_vcpu_ran; -struct kvm *kvm; +struct kvm_arch *arch; };struct kvm_arch {
arch/arm64/include/asm/kvm_mmu.h +6 -1
@@ -275,7 +275,7 @@ static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu)*/ static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu) { -write_sysreg (kern_hyp_va (mmu->kvm)->arch.vtcr, vtcr_el2); +write_sysreg (kern_hyp_va (mmu->arch)->vtcr, vtcr_el2);write_sysreg (kvm_get_vttbr (mmu), vttbr_el2);/*
@@ -285,5 +285,10 @@ static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu)*/ asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); } + +static inline struct kvm *kvm_s2_mmu_to_kvm (struct kvm_s2_mmu *mmu) +{ + return container_of(mmu->arch, struct kvm, arch); +}#endif /* __ASSEMBLY__ */ #endif /* __ARM64_KVM_MMU_H__ */
arch/arm64/kvm/mmu.c +4 -4
@@ -169,7 +169,7 @@ static void *kvm_host_va(phys_addr_t phys)static void __unmap_stage2_range (struct kvm_s2_mmu *mmu,phys_addr_t start,u64 size,bool may_block) { -struct kvm *kvm = mmu->kvm; +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);phys_addr_t end = start + size;assert_spin_locked (&kvm->mmu_lock);
@@ -474,7 +474,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)for_each_possible_cpu (cpu) *per_cpu_ptr (mmu->last_vcpu_ran, cpu) =-1 ; - mmu->kvm = kvm; + mmu->arch = &kvm->arch; mmu->pgt = pgt; mmu->pgd_phys =__pa (pgt->pgd); mmu->vmid.vmid_gen =0 ;
@@ -556,7 +556,7 @@ void stage2_unmap_vm(struct kvm *kvm)void kvm_free_stage2_pgd (struct kvm_s2_mmu *mmu) { -struct kvm *kvm = mmu->kvm; +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);struct kvm_pgtable *pgt =NULL ;spin_lock (&kvm->mmu_lock);
@@ -625,7 +625,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,*/ static void stage2_wp_range (struct kvm_s2_mmu *mmu,phys_addr_t addr,phys_addr_t end) { -struct kvm *kvm = mmu->kvm; +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);stage2_apply_range_resched (kvm, addr, end, kvm_pgtable_stage2_wrprotect); }
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 20/27 KVM: arm64: Set host stage 2 using kvm_nvhe_init_params
In reply to Quentin PerretMove the registers relevant to host stage 2 enablement to kvm_nvhe_init_params to prepare the ground for enabling it in later patches.
--- arch/arm64/include/asm/kvm_asm.h | 3 +++ arch/arm64/kernel/asm-offsets.c | 3 +++ arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/hyp/nvhe/hyp-init.S | 9 +++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 5 +---- 5 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_asm.h +3 -0
@@ -158,6 +158,9 @@ struct kvm_nvhe_init_params {unsigned long stack_hyp_va;unsigned long entry_hyp_va;phys_addr_t pgd_pa; +unsigned long hcr_el2; +unsigned long vttbr; +unsigned long vtcr; };/* Translate a kernel address @ptr into its equivalent linear mapping */
arch/arm64/kernel/asm-offsets.c +3 -0
@@ -115,6 +115,9 @@ int main(void)DEFINE (NVHE_INIT_STACK_HYP_VA , offsetof(struct kvm_nvhe_init_params , stack_hyp_va));DEFINE (NVHE_INIT_ENTRY_HYP_VA , offsetof(struct kvm_nvhe_init_params , entry_hyp_va));DEFINE (NVHE_INIT_PGD_PA , offsetof(struct kvm_nvhe_init_params , pgd_pa)); +DEFINE (NVHE_INIT_HCR_EL2 , offsetof(struct kvm_nvhe_init_params , hcr_el2)); +DEFINE (NVHE_INIT_VTTBR , offsetof(struct kvm_nvhe_init_params , vttbr)); +DEFINE (NVHE_INIT_VTCR , offsetof(struct kvm_nvhe_init_params , vtcr));#endif #ifdef CONFIG_CPU_PM DEFINE (CPU_CTX_SP , offsetof(struct cpu_suspend_ctx , sp));
arch/arm64/kvm/arm.c +5 -0
@@ -1365,6 +1365,11 @@ static void cpu_prepare_hyp_mode(int cpu) params->stack_hyp_va =kern_hyp_va (per_cpu (kvm_arm_hyp_stack_page, cpu) +PAGE_SIZE ); params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref_nvhe (__kvm_hyp_psci_cpu_entry)); params->pgd_pa =kvm_mmu_get_httbr (); +if (is_protected_kvm_enabled ()) + params->hcr_el2 =HCR_HOST_NVHE_PROTECTED_FLAGS ; +else + params->hcr_el2 =HCR_HOST_NVHE_FLAGS ; + params->vttbr = params->vtcr =0 ;/* * Flush the init params from the data cache because the struct will
arch/arm64/kvm/hyp/nvhe/hyp-init.S +9 -0
@@ -103,6 +103,15 @@ alternative_else_nop_endif ldr x1, [x0, #NVHE_INIT_STACK_HYP_VA] mov sp, x1 + ldr x1, [x0, #NVHE_INIT_HCR_EL2] + msr hcr_el2, x1 + + ldr x1, [x0, #NVHE_INIT_VTTBR] + msr vttbr_el2, x1 + + ldr x1, [x0, #NVHE_INIT_VTCR] + msr vtcr_el2, x1 + ldr x1, [x0, #NVHE_INIT_PGD_PA] phys_to_ttbr x0, x1 alternative_if ARM64_HAS_CNP
arch/arm64/kvm/hyp/nvhe/switch.c +1 -4
@@ -97,10 +97,7 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu) mdcr_el2 |=MDCR_EL2_E2PB_MASK <<MDCR_EL2_E2PB_SHIFT ;write_sysreg (mdcr_el2, mdcr_el2); -if (is_protected_kvm_enabled ()) -write_sysreg (HCR_HOST_NVHE_PROTECTED_FLAGS , hcr_el2); -else -write_sysreg (HCR_HOST_NVHE_FLAGS , hcr_el2); +write_sysreg (this_cpu_ptr (&kvm_init_params)->hcr_el2, hcr_el2);write_sysreg (CPTR_EL2_DEFAULT , cptr_el2);write_sysreg (__kvm_hyp_host_vector, vbar_el2); }
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 21/27 KVM: arm64: Refactor kvm_arm_setup_stage2()
In reply to Quentin PerretIn order to re-use some of the stage 2 setup at EL2, factor parts of kvm_arm_setup_stage2() out into static inline functions.
No functional change intended.
--- arch/arm64/include/asm/kvm_mmu.h | 48 ++++++++++++++++++++++++++++++++ arch/arm64/kvm/reset.c | 42 +++------------------------- 2 files changed, 52 insertions(+), 38 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +48 -0
@@ -256,6 +256,54 @@ static inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa,return ret; } +static inline u64 kvm_get_parange (u64 mmfr0) +{ +u64 parange =cpuid_feature_extract_unsigned_field (mmfr0, +ID_AA64MMFR0_PARANGE_SHIFT ); +if (parange >ID_AA64MMFR0_PARANGE_MAX ) + parange =ID_AA64MMFR0_PARANGE_MAX ; + +return parange; +} + +/* +* The VTCR value is common across all the physical CPUs on the system. +* We use system wide sanitised values to fill in different fields, +* except for Hardware Management of Access Flags. HA Flag is set +* unconditionally on all CPUs, as it is safe to run with or without +* the feature and the bit is RES0 on CPUs that don't support it. +*/ +static inline u64 kvm_get_vtcr (u64 mmfr0,u64 mmfr1,u32 phys_shift) +{ +u64 vtcr =VTCR_EL2_FLAGS ; +u8 lvls; + + vtcr |=kvm_get_parange (mmfr0) <<VTCR_EL2_PS_SHIFT ; + vtcr |=VTCR_EL2_T0SZ (phys_shift); +/* +* Use a minimum 2 level page table to prevent splitting +* host PMD huge pages at stage2. +*/ + lvls =stage2_pgtable_levels (phys_shift); +if (lvls <2 ) + lvls =2 ; + vtcr |=VTCR_EL2_LVLS_TO_SL0 (lvls); + +/* +* Enable the Hardware Access Flag management, unconditionally +* on all CPUs. The features is RES0 on CPUs without the support +* and must be ignored by the CPUs. +*/ + vtcr |=VTCR_EL2_HA ; + +/* Set the vmid bits */ + vtcr |= (get_vmid_bits (mmfr1) ==16 ) ? +VTCR_EL2_VS_16BIT : +VTCR_EL2_VS_8BIT ; + +return vtcr; +} +#define kvm_phys_to_vttbr (addr) phys_to_ttbr(addr) static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu)
arch/arm64/kvm/reset.c +4 -38
56 lines not on this page · raw message
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 22/27 KVM: arm64: Refactor __load_guest_stage2()
In reply to Quentin PerretRefactor __load_guest_stage2() to introduce __load_stage2() which will be re-used when loading the host stage 2.
--- arch/arm64/include/asm/kvm_mmu.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +7 -2
@@ -321,9 +321,9 @@ static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu)* Must be called from hyp code running at EL2 with an updated VTTBR * and interrupts disabled. */ -static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu) +static __always_inline void __load_stage2(struct kvm_s2_mmu *mmu, unsigned long vtcr) { -write_sysreg (kern_hyp_va (mmu->arch)->vtcr, vtcr_el2); +write_sysreg (vtcr, vtcr_el2);write_sysreg (kvm_get_vttbr (mmu), vttbr_el2);/*
@@ -334,6 +334,11 @@ static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu) asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); } +static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu) +{ +__load_stage2 (mmu,kern_hyp_va (mmu->arch)->vtcr); +} +static inline struct kvm *kvm_s2_mmu_to_kvm (struct kvm_s2_mmu *mmu) { return container_of(mmu->arch, struct kvm, arch);
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 23/27 KVM: arm64: Refactor __populate_fault_info()
In reply to Quentin PerretRefactor __populate_fault_info() to introduce __get_fault_info() which will be used once the host is wrapped in a stage 2.
--- arch/arm64/kvm/hyp/include/hyp/switch.h | 36 +++++++++++++++---------- 1 file changed, 22 insertions(+), 14 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +22 -14
@@ -157,19 +157,9 @@ static inline bool __translate_far_to_hpfar(u64 far, u64 *hpfar)return true; } -static inline bool __populate_fault_info (struct kvm_vcpu *vcpu) +static inline bool __get_fault_info (u64 esr,u64 *far,u64 *hpfar) { -u8 ec; -u64 esr; -u64 hpfar, far; - - esr = vcpu->arch.fault.esr_el2; - ec =ESR_ELx_EC (esr); - -if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW) -return true; - - far =read_sysreg_el2 (SYS_FAR ); + *far =read_sysreg_el2 (SYS_FAR );/* * The HPFAR can be invalid if the stage 2 fault did not
@@ -185,12 +175,30 @@ static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)if (!(esr & ESR_ELx_S1PTW) && (cpus_have_final_cap (ARM64_WORKAROUND_834220 ) || (esr & ESR_ELx_FSC_TYPE) ==FSC_PERM )) { -if (!__translate_far_to_hpfar (far, &hpfar)) +if (!__translate_far_to_hpfar (*far, hpfar))return false; }else { - hpfar =read_sysreg (hpfar_el2); + *hpfar =read_sysreg (hpfar_el2); } +return true; +} + +static inline bool __populate_fault_info (struct kvm_vcpu *vcpu) +{ +u8 ec; +u64 esr; +u64 hpfar, far; + + esr = vcpu->arch.fault.esr_el2; + ec =ESR_ELx_EC (esr); + +if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW) +return true; + +if (!__get_fault_info (esr, &far, &hpfar)) +return false; + vcpu->arch.fault.far_el2 = far; vcpu->arch.fault.hpfar_el2 = hpfar;return true;
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 24/27 KVM: arm64: Make memcache anonymous in pgtable allocator
In reply to Quentin PerretThe current stage2 page-table allocator uses a memcache to get pre-allocated pages when it needs any. To allow re-using this code at EL2 which uses a concept of memory pools, make the memcache argument to kvm_pgtable_stage2_map() anonymous. and let the mm_ops zalloc_page() callbacks use it the way they need to.
--- arch/arm64/include/asm/kvm_pgtable.h | 6 +++--- arch/arm64/kvm/hyp/pgtable.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +3 -3
@@ -176,8 +176,8 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);* @size: Size of the mapping. * @phys: Physical address of the memory to map. * @prot: Permissions and attributes for the mapping. -* @mc: Cache of pre-allocated GFP_PGTABLE_USER memory from which to -* allocate page-table pages. +* @mc: Cache of pre-allocated memory from which to allocate page-table +* pages. * * The offset of @addr within a page is ignored, @size is rounded-up to * the next page boundary and @phys is rounded-down to the previous page
@@ -194,7 +194,7 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);*/ int kvm_pgtable_stage2_map (struct kvm_pgtable *pgt,u64 addr,u64 size,u64 phys,enum kvm_pgtable_prot prot, -struct kvm_mmu_memory_cache *mc); +void *mc);/** * kvm_pgtable_stage2_unmap() - Remove a mapping from a guest stage-2 page-table.
arch/arm64/kvm/hyp/pgtable.c +2 -2
@@ -443,7 +443,7 @@ struct stage2_map_data {kvm_pte_t *anchor;struct kvm_s2_mmu *mmu; -struct kvm_mmu_memory_cache *memcache; +void *memcache;struct kvm_pgtable_mm_ops *mm_ops; };
@@ -613,7 +613,7 @@ static int stage2_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,int kvm_pgtable_stage2_map (struct kvm_pgtable *pgt,u64 addr,u64 size,u64 phys,enum kvm_pgtable_prot prot, -struct kvm_mmu_memory_cache *mc) +void *mc) {int ret;struct stage2_map_data map_data = {
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 25/27 KVM: arm64: Reserve memory for host stage 2
In reply to Quentin PerretExtend the memory pool allocated for the hypervisor to include enough pages to map all of memory at page granularity for the host stage 2. While at it, also reserve some memory for device mappings.
--- arch/arm64/kvm/hyp/include/nvhe/mm.h | 36 ++++++++++++++++++++++++---- arch/arm64/kvm/hyp/nvhe/setup.c | 12 ++++++++++ arch/arm64/kvm/hyp/reserved_mem.c | 2 ++ 3 files changed, 46 insertions(+), 4 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mm.h +32 -4
@@ -52,15 +52,12 @@ static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)return total; } -static inline unsigned long hyp_s1_pgtable_size (void ) +static inline unsigned long __hyp_pgtable_total_size (void ) {struct hyp_memblock_region *reg;unsigned long nr_pages, res =0 ;int i; -if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) -return 0 ; -for (i =0 ; i <kvm_nvhe_sym (hyp_memblock_nr); i++) { reg = &kvm_nvhe_sym (hyp_memory)[i]; nr_pages = (reg->end - reg->start) >>PAGE_SHIFT ;
@@ -68,6 +65,18 @@ static inline unsigned long hyp_s1_pgtable_size(void) res += nr_pages <<PAGE_SHIFT ; } +return res; +} + +static inline unsigned long hyp_s1_pgtable_size (void ) +{ +unsigned long res, nr_pages; + +if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) +return 0 ; + + res =__hyp_pgtable_total_size (); +/* Allow 1 GiB for private mappings */ nr_pages = (1 <<30 ) >>PAGE_SHIFT ; nr_pages =__hyp_pgtable_max_pages (nr_pages);
@@ -76,4 +85,23 @@ static inline unsigned long hyp_s1_pgtable_size(void) return res; } +static inline unsigned long host_s2_mem_pgtable_size (void ) +{ +unsigned long max_pgd_sz =16 <<PAGE_SHIFT ; + +if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) +return 0 ; + +return __hyp_pgtable_total_size () + max_pgd_sz; +} + +static inline unsigned long host_s2_dev_pgtable_size (void ) +{ +if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) +return 0 ; + +/* Allow 1 GiB for private mappings */ +return __hyp_pgtable_max_pages ((1 <<30 ) >>PAGE_SHIFT ) <<PAGE_SHIFT ; +} +#endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/setup.c +12 -0
@@ -24,6 +24,8 @@ unsigned long hyp_nr_cpus;static void *stacks_base;static void *vmemmap_base;static void *hyp_pgt_base; +static void *host_s2_mem_pgt_base; +static void *host_s2_dev_pgt_base;static int divide_memory_pool (void *virt,unsigned long size) {
@@ -46,6 +48,16 @@ static int divide_memory_pool(void *virt, unsigned long size)if (!hyp_pgt_base)return -ENOMEM ; + nr_pages =host_s2_mem_pgtable_size () >>PAGE_SHIFT ; + host_s2_mem_pgt_base =hyp_early_alloc_contig (nr_pages); +if (!host_s2_mem_pgt_base) +return -ENOMEM ; + + nr_pages =host_s2_dev_pgtable_size () >>PAGE_SHIFT ; + host_s2_dev_pgt_base =hyp_early_alloc_contig (nr_pages); +if (!host_s2_dev_pgt_base) +return -ENOMEM ; +return 0 ; }
arch/arm64/kvm/hyp/reserved_mem.c +2 -0
@@ -47,6 +47,8 @@ void __init reserve_kvm_hyp(void) hyp_mem_size +=num_possible_cpus () <<PAGE_SHIFT ; hyp_mem_size +=hyp_s1_pgtable_size (); + hyp_mem_size +=host_s2_mem_pgtable_size (); + hyp_mem_size +=host_s2_dev_pgtable_size ();/* * The hyp_vmemmap needs to be backed by pages, but these pages
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 26/27 KVM: arm64: Sort the memblock regions list
In reply to Quentin PerretThe hypervisor will need the list of memblock regions sorted by increasing start address to make look-ups more efficient. Make the host do the hard work early while it is still trusted to avoid the need for a sorting library at EL2.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hyp/reserved_mem.c | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -746,6 +746,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);extern phys_addr_t hyp_mem_base;extern phys_addr_t hyp_mem_size;void __init reserve_kvm_hyp (void ); +void kvm_sort_memblock_regions (void );#else static inline void reserve_kvm_hyp (void ) { }#endif
arch/arm64/kvm/arm.c +1 -0
@@ -1685,6 +1685,7 @@ static int kvm_hyp_enable_protection(void)return ret;kvm_set_hyp_vector (); +kvm_sort_memblock_regions (); ret =kvm_call_hyp_nvhe (__kvm_hyp_protect, hyp_mem_base, hyp_mem_size,num_possible_cpus (),kern_hyp_va (per_cpu_base));if (ret)
arch/arm64/kvm/hyp/reserved_mem.c +18 -0
@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <linux/memblock.h> +#include <linux/sort.h> #include <asm/kvm_host.h>
@@ -31,6 +32,23 @@ void __init early_init_dt_add_memory_hyp(u64 base, u64 size)kvm_nvhe_sym (hyp_memblock_nr)++; } +static int cmp_hyp_memblock (const void *p1,const void *p2) +{ +const struct hyp_memblock_region *r1 = p1; +const struct hyp_memblock_region *r2 = p2; + +return r1->start < r2->start ?-1 : (r1->start > r2->start); +} + +void kvm_sort_memblock_regions (void ) +{ +sort (kvm_nvhe_sym (hyp_memory), +kvm_nvhe_sym (hyp_memblock_nr), +sizeof (struct hyp_memblock_region ), + cmp_hyp_memblock, +NULL ); +} +extern bool enable_protected_kvm;void __init reserve_kvm_hyp (void ) {
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH 27/27 KVM: arm64: Wrap the host with a stage 2
In reply to Quentin PerretWhen KVM runs in protected nVHE mode, make use of a stage 2 page-table to give the hypervisor some control over the host memory accesses. At the moment all memory aborts from the host will be instantly idmapped RWX at stage 2 in a lazy fashion. Later patches will make use of that infrastructure to implement access control restrictions to e.g. protect guest memory from the host.
--- arch/arm64/include/asm/kvm_cpufeature.h | 2 + arch/arm64/kernel/image-vars.h | 3 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 33 +++ arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 191 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 6 + arch/arm64/kvm/hyp/nvhe/switch.c | 7 +- arch/arm64/kvm/hyp/nvhe/tlb.c | 4 +- 9 files changed, 247 insertions(+), 7 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h create mode 100644 arch/arm64/kvm/hyp/nvhe/mem_protect.c
arch/arm64/include/asm/kvm_cpufeature.h +2 -0
@@ -15,3 +15,5 @@#endif KVM_HYP_CPU_FTR_REG(SYS_CTR_EL0, arm64_ftr_reg_ctrel0) +KVM_HYP_CPU_FTR_REG(SYS_ID_AA64MMFR0_EL1, arm64_ftr_reg_id_aa64mmfr0_el1) +KVM_HYP_CPU_FTR_REG (SYS_ID_AA64MMFR1_EL1 , arm64_ftr_reg_id_aa64mmfr1_el1)
arch/arm64/kernel/image-vars.h +3 -0
@@ -137,6 +137,9 @@ KVM_NVHE_ALIAS(__hyp_data_ro_after_init_end);KVM_NVHE_ALIAS (__hyp_bss_start);KVM_NVHE_ALIAS (__hyp_bss_end); +/* pKVM static key */ +KVM_NVHE_ALIAS (kvm_protected_mode_initialized); +#endif /* CONFIG_KVM */ #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2020 Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#ifndef __KVM_NVHE_MEM_PROTECT__ +#define __KVM_NVHE_MEM_PROTECT__ +#include <linux/kvm_host.h> +#include <asm/kvm_hyp.h> +#include <asm/kvm_pgtable.h> +#include <asm/virt.h> +#include <nvhe/spinlock.h> + +struct host_kvm { +struct kvm_arch arch; +struct kvm_pgtable pgt; +struct kvm_pgtable_mm_ops mm_ops; +hyp_spinlock_t lock; +}; +extern struct host_kvm host_kvm; + +int kvm_host_prepare_stage2 (void *mem_pgt_pool,void *dev_pgt_pool); +void handle_host_mem_abort (struct kvm_cpu_context *host_ctxt); + +static __always_inline void __load_host_stage2(void) +{ +if (static_branch_likely (&kvm_protected_mode_initialized)) +__load_stage2 (&host_kvm.arch.mmu, host_kvm.arch.vtcr); +else +write_sysreg (0 , vttbr_el2); +} +#endif /* __KVM_NVHE_MEM_PROTECT__ */
arch/arm64/kvm/hyp/nvhe/Makefile +1 -1
@@ -11,7 +11,7 @@ lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \ - cache.o cpufeature.o setup.o mm.o + cache.o cpufeature.o setup.o mm.o mem_protect.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6 -0
@@ -13,6 +13,7 @@#include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> +#include <nvhe/mem_protect.h> #include <nvhe/mm.h> DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
@@ -234,6 +235,11 @@ void handle_trap(struct kvm_cpu_context *host_ctxt)case ESR_ELx_EC_SMC64:handle_host_smc (host_ctxt);break ; +case ESR_ELx_EC_IABT_LOW: + fallthrough; +case ESR_ELx_EC_DABT_LOW: +handle_host_mem_abort (host_ctxt); +break ;default :hyp_panic (); }
arch/arm64/kvm/hyp/nvhe/mem_protect.c new file +191 -0
191 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/setup.c +6 -0
@@ -12,6 +12,7 @@#include <nvhe/early_alloc.h> #include <nvhe/gfp.h> #include <nvhe/memory.h> +#include <nvhe/mem_protect.h> #include <nvhe/mm.h> struct hyp_pool hpool;
@@ -161,6 +162,11 @@ void __noreturn __kvm_hyp_protect_finalise(void)if (ret) goto out; +/* Wrap the host with a stage 2 */ + ret =kvm_host_prepare_stage2 (host_s2_mem_pgt_base, host_s2_dev_pgt_base); +if (ret) + goto out; + hyp_pgtable_mm_ops.zalloc_page = hyp_zalloc_hyp_page; hyp_pgtable_mm_ops.phys_to_virt = hyp_phys_to_virt; hyp_pgtable_mm_ops.virt_to_phys = hyp_virt_to_phys;
arch/arm64/kvm/hyp/nvhe/switch.c +2 -5
@@ -28,6 +28,8 @@#include <asm/processor.h> #include <asm/thread_info.h> +#include <nvhe/mem_protect.h> +/* Non-VHE specific context */ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
@@ -102,11 +104,6 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)write_sysreg (__kvm_hyp_host_vector, vbar_el2); } -static void __load_host_stage2 (void ) -{ -write_sysreg (0 , vttbr_el2); -} -/* Save VGICv3 state on non-VHE systems */ static void __hyp_vgic_save_state (struct kvm_vcpu *vcpu) {
arch/arm64/kvm/hyp/nvhe/tlb.c +3 -1
@@ -8,6 +8,8 @@#include <asm/kvm_mmu.h> #include <asm/tlbflush.h> +#include <nvhe/mem_protect.h> +struct tlb_inv_context {u64 tcr; };
@@ -43,7 +45,7 @@ static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,static void __tlb_switch_to_host (struct tlb_inv_context *cxt) { -write_sysreg (0 , vttbr_el2); +__load_host_stage2 ();if (cpus_have_final_cap (ARM64_WORKAROUND_SPECULATIVE_AT )) {/* Ensure write of the host VMID */
Signature · 6 lines
2.29.2.299.gdc1121823c-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 15/27] of/fdt: Introduce early_init_dt_add_memory_hyp()
In reply to Quentin PerretQuoted 28 lines · Quentin Perret
Introduce
early_init_dt_add_memory_hyp()to allow KVM to conserve a copy of the memory regions parsed from DT. This will be needed in the context of the protected nVHE feature of KVM/arm64 where the code running at EL2 will be cleanly separated from the host kernel during boot, and will need its own representation of memory.Signed-off-by Quentin Perret <[email protected]>--- drivers/of/fdt.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/of/fdt.c +4
-0@@ -1099,6 +1099,10 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)#endif +void __init __weak early_init_dt_add_memory_hyp (u64 base,u64 size) +{ +} +void __init __weak early_init_dt_add_memory_arch (u64 base,u64 size) {const u64 phys_offset =MIN_MEMBLOCK_ADDR ;
drivers/of/fdt.c void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) · line 1146
Quoted 4 lines
drivers/of/fdt.c +1
-0@@ -1139,6 +1143,7 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) base = phys_offset; }memblock_add (base, size); +early_init_dt_add_memory_hyp (base, size);
Can this be done right after we add all the memblocks using the memblock API? I thought EFI would also need to be handled, but looks like it just calls early_init_dt_add_memory_arch(). That's odd especially for ACPI systems...
I don't really like putting what looks like an arm64 only hook here, but then I don't want an arm64 version of early_init_dt_add_memory_arch() either. We're almost to the point of getting rid of the arch specific ones. But I don't have a better suggestion currently.
Rob
Re: [RFC PATCH 15/27] of/fdt: Introduce early_init_dt_add_memory_hyp()
Hi Rob,
Quoted 2 lines · Rob Herring
Can this be done right after we add all the memblocks using the memblock API?
Possibly, but the thing I'm a bit worried about is the way 'no-map' regions are removed from memblocks early on.
The EL2 object needs to know about these parts of memory too (and in fact we may be able to enforce the 'no-map' attribute at the host stage 2 level as well). It's also possible we'll need to have portions of the guests payload preloaded (and verified) by the bootloader into reserved memory regions, possibly no-map, to make sure the host does not mess with them in a normal use-case. So, I couldn't find a much better place than this one but suggestions are very much welcome.
I'll have a go at the memblock stuff to see if I find a way to make it work from that angle.
Quoted 9 lines
I thought EFI would also need to be handled, but looks like it just calls
early_init_dt_add_memory_arch(). That's odd especially for ACPI systems...I don't really like putting what looks like an arm64 only hook here, but then I don't want an arm64 version of
early_init_dt_add_memory_arch()either. We're almost to the point of getting rid of the arch specific ones. But I don't have a better suggestion currently.
Ack, the ugly truth is that this is likely to remain arm64-specific. I figured this was simple enough that we might want to consider it, though.
Thanks,
Quentin
Re: [RFC PATCH 15/27] of/fdt: Introduce early_init_dt_add_memory_hyp()
Quoted 2 lines · Quentin Perret
I'll have a go at the memblock stuff to see if I find a way to make it work from that angle.
OK, no luck with the memblock API, but I figured that I can actually postpone the KVM memory reservation to a later point, after unflatten_device_tree(), which lets me iterate over the memory nodes directly rather than having the fdt driver do it for me.
The below seems to boot alright (though I'm not too familiar with of_address_to_resource() so I may not be using right) and keeps the whole thing in arch/arm64. Thoughts?
Thanks,
Quentin
---8<---
arch/arm64/kvm/hyp/reserved_mem.c +18 -6
@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <linux/memblock.h> +#include <linux/of_address.h> #include <linux/sort.h> #include <asm/kvm_host.h>
@@ -16,7 +17,7 @@phys_addr_t hyp_mem_base;phys_addr_t hyp_mem_size; -void __init early_init_dt_add_memory_hyp (u64 base,u64 size) +static int __init add_hyp_memblock_region (struct resource *rsrc) {struct hyp_memblock_region *reg;
@@ -24,12 +25,14 @@ void __init early_init_dt_add_memory_hyp(u64 base, u64 size)kvm_nvhe_sym (hyp_memblock_nr) =-1 ;if (kvm_nvhe_sym (hyp_memblock_nr) <0 ) -return ; +return -ENOMEM ; reg =kvm_nvhe_sym (hyp_memory); - reg[kvm_nvhe_sym (hyp_memblock_nr)].start = base; - reg[kvm_nvhe_sym (hyp_memblock_nr)].end = base + size; + reg[kvm_nvhe_sym (hyp_memblock_nr)].start = rsrc->start; + reg[kvm_nvhe_sym (hyp_memblock_nr)].end = rsrc->end;kvm_nvhe_sym (hyp_memblock_nr)++; + +return 0 ; }static int cmp_hyp_memblock (const void *p1,const void *p2)
@@ -52,7 +55,10 @@ void kvm_sort_memblock_regions(void)extern bool enable_protected_kvm;void __init reserve_kvm_hyp (void ) { +struct device_node *np; +struct resource rsrc;u64 nr_pages, prev; +int i;if (!enable_protected_kvm)return ;
@@ -60,8 +66,14 @@ void __init reserve_kvm_hyp(void)if (!is_hyp_mode_available () ||is_kernel_in_hyp_mode ())return ; -if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) -return ; +for_each_node_by_type (np,"memory" ) { +for (i =0 ; !of_address_to_resource (np, i, &rsrc); i++) { +if (!add_hyp_memblock_region (&rsrc)) +continue ; +kvm_err ("Failed to add hyp memblock\n" ); +return ; + } + } hyp_mem_size +=num_possible_cpus () <<PAGE_SHIFT ; hyp_mem_size +=hyp_s1_pgtable_size ();
arch/arm64/mm/init.c +2 -1
@@ -391,7 +391,6 @@ void __init arm64_memblock_init(void)reserve_elfcorehdr (); -reserve_kvm_hyp (); high_memory =__va (memblock_end_of_DRAM () -1 ) +1 ;
@@ -423,6 +422,8 @@ void __init bootmem_init(void)dma_pernuma_cma_reserve (); +reserve_kvm_hyp (); +/* * sparse_init() tries to allocate memory from memblock, so must be * done after the fixed reservations
drivers/of/fdt.c +0 -5
@@ -1099,10 +1099,6 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)#endif -void __init __weak early_init_dt_add_memory_hyp (u64 base,u64 size) -{ -} -void __init __weak early_init_dt_add_memory_arch (u64 base,u64 size) {const u64 phys_offset =MIN_MEMBLOCK_ADDR ;
@@ -1143,7 +1139,6 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) base = phys_offset; }memblock_add (base, size); -early_init_dt_add_memory_hyp (base, size); }int __init __weak early_init_dt_mark_hotplug_memory_arch (u64 base,u64 size)
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 13/27] KVM: arm64: Enable access to sanitized CPU features at EL2
In reply to Quentin PerretHi Quentin,
Quoted 47 lines · Quentin Perret' via kernel-team
Introduce the infrastructure in KVM enabling to copy CPU feature registers into EL2-owned data-structures, to allow reading sanitised values directly at EL2 in nVHE.
Given that only a subset of these features are being read by the hypervisor, the ones that need to be copied are to be listed under <
asm/kvm_cpufeature.h> together with the name of the nVHE variable that will hold the copy.While at it, introduce the first user of this infrastructure by implementing
__flush_dcache_areaat EL2, which needsarm64_ftr_reg_ctrel0.Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/cpufeature.h | 1 + arch/arm64/include/asm/kvm_cpufeature.h | 17 ++++++++++++++ arch/arm64/kernel/cpufeature.c | 12 ++++++++++ arch/arm64/kernel/image-vars.h | 2 ++ arch/arm64/kvm/arm.c | 31 +++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 3 ++- arch/arm64/kvm/hyp/nvhe/cache.S | 13 +++++++++++ arch/arm64/kvm/hyp/nvhe/cpufeature.c | 8 +++++++ 8 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_cpufeature.h create mode 100644 arch/arm64/kvm/hyp/nvhe/cache.S create mode 100644 arch/arm64/kvm/hyp/nvhe/cpufeature.carch/arm64/include/asm/cpufeature.h +1
-0@@ -600,6 +600,7 @@ void __init setup_cpu_features(void);void check_local_cpu_capabilities (void );u64 read_sanitised_ftr_reg (u32 id); +int copy_ftr_reg (u32 id,struct arm64_ftr_reg *dst);static inline bool cpu_supports_mixed_endian_el0 (void ) {arch/arm64/include/asm/kvm_cpufeature.h new file +0
-0
arch/arm64/include/asm/kvm_cpufeature.h line 5
Quoted 5 lines
arch/arm64/include/asm/kvm_cpufeature.h new file +5
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2020 - Google LLC +* Author: Quentin Perret <[email protected]> +*/
Missing include guard.
Quoted 154 lines
arch/arm64/include/asm/kvm_cpufeature.h new file +12
-0+ +#include <asm/cpufeature.h> + +#ifndef KVM_HYP_CPU_FTR_REG +#if defined(__KVM_NVHE_HYPERVISOR__) +#define KVM_HYP_CPU_FTR_REG (id, name) extern struct arm64_ftr_reg name; +#else +#define KVM_HYP_CPU_FTR_REG (id, name) DECLARE_KVM_NVHE_SYM(name); +#endif +#endif + +KVM_HYP_CPU_FTR_REG (SYS_CTR_EL0 , arm64_ftr_reg_ctrel0)arch/arm64/kernel/cpufeature.c +12
-0@@ -1116,6 +1116,18 @@ u64 read_sanitised_ftr_reg(u32 id) }EXPORT_SYMBOL_GPL (read_sanitised_ftr_reg); +int copy_ftr_reg (u32 id,struct arm64_ftr_reg *dst) +{ +struct arm64_ftr_reg *regp =get_arm64_ftr_reg (id); + +if (!regp) +return -EINVAL ; + +memcpy (dst, regp,sizeof (*regp)); + +return 0 ; +} +#define read_sysreg_case (r) \ case r: return read_sysreg_s(r)arch/arm64/kernel/image-vars.h +2
-0@@ -116,6 +116,8 @@ __kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; __kvm_nvhe___memset = __kvm_nvhe___pi_memset;#endif +_kvm_nvhe___flush_dcache_area = __kvm_nvhe___pi___flush_dcache_area; +#endif /* CONFIG_KVM */ #endif /* __ARM64_KERNEL_IMAGE_VARS_H */ arch/arm64/kvm/arm.c +31
-0@@ -34,6 +34,7 @@#include <asm/virt.h> #include <asm/kvm_arm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_cpufeature.h> #include <asm/kvm_mmu.h> #include <asm/kvm_emulate.h> #include <asm/sections.h> @@ -1636,6 +1637,29 @@ static void teardown_hyp_mode(void) } } +#undef KVM_HYP_CPU_FTR_REG +#define KVM_HYP_CPU_FTR_REG (id, name) \ + { .sys_id = id, .dst = (struct arm64_ftr_reg *)&kvm_nvhe_sym(name) }, +static const struct __ftr_reg_copy_entry { +u32 sys_id; +struct arm64_ftr_reg *dst; +} hyp_ftr_regs[] = { + #include <asm/kvm_cpufeature.h> +}; + +static int copy_cpu_ftr_regs (void ) +{ +int i, ret; + +for (i =0 ; i <ARRAY_SIZE (hyp_ftr_regs); i++) { + ret =copy_ftr_reg (hyp_ftr_regs[i].sys_id, hyp_ftr_regs[i].dst); +if (ret) +return ret; + } + +return 0 ; +} +/** * Inits Hyp-mode on all online CPUs */ @@ -1644,6 +1668,13 @@ static int init_hyp_mode(void)int cpu;int err =0 ; +/* +* Copy the required CPU feature register in their EL2 counterpart +*/ + err =copy_cpu_ftr_regs (); +if (err) +return err; +/* * Allocate Hyp PGD and setup Hyp identity mapping */ arch/arm64/kvm/hyp/nvhe/Makefile +2
-1@@ -10,7 +10,8 @@ lib-objs := clear_page.o copy_page.o memcpy.o memset.o lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ - hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o + hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \ + cache.o cpufeature.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o obj-y += $(lib-objs)arch/arm64/kvm/hyp/nvhe/cache.S new file +13
-0@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Code copied from arch/arm64/mm/cache.S. + */ + +#include <linux/linkage.h> +#include <asm/assembler.h> +#include <asm/alternative.h> + +SYM_FUNC_START_PI(__flush_dcache_area) + dcache_by_line_op civac, sy, x0, x1, x2, x3 + ret +SYM_FUNC_END_PI(__flush_dcache_area)arch/arm64/kvm/hyp/nvhe/cpufeature.c new file +8
-1@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2020 - Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#define KVM_HYP_CPU_FTR_REG (id, name) struct arm64_ftr_reg name; +#include <asm/kvm_cpufeature.h> --2.29.2.299.gdc1121823c-goog
Quoted 3 lines
--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
/fuad
Re: [RFC PATCH 02/27] KVM: arm64: Link position-independent string routines into .hyp.text
In reply to Quentin PerretQuoted 25 lines · Quentin Perret' via kernel-team
From: Will Deacon <[email protected]>
Pull
clear_page(),copy_page(),memcpy()andmemset()into the nVHE hyp code and ensure that we always execute the '__pi_' entry point on the offchance that it changes in future.[ qperret: Commit title nits ]
Signed-off-by Will Deacon <[email protected]>Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/kernel/image-vars.h | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 4 ++++ 2 files changed, 15 insertions(+)arch/arm64/kernel/image-vars.h +2
-0@@ -105,6 +105,17 @@ KVM_NVHE_ALIAS(__stop___kvm_ex_table);/* Array containing bases of nVHE per-CPU memory regions. */ KVM_NVHE_ALIAS (kvm_arm_hyp_percpu_base); +/* Position-independent library routines */ +__kvm_nvhe_clear_page = __kvm_nvhe___pi_clear_page;
arch/arm64/kernel/image-vars.h KVM_NVHE_ALIAS(__stop___kvm_ex_table); · line 119
Quoted 10 lines
arch/arm64/kernel/image-vars.h +9
-0+__kvm_nvhe_copy_page = __kvm_nvhe___pi_copy_page; +__kvm_nvhe_memcpy = __kvm_nvhe___pi_memcpy; +__kvm_nvhe_memset = __kvm_nvhe___pi_memset; + +#ifdef CONFIG_KASAN +__kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; +__kvm_nvhe___memset = __kvm_nvhe___pi_memset; +#endif +#endif /* CONFIG_KVM */
Nit: Would be good to use the kvm_nvhe_sym() helper for the namespacing. And feel free to define something like KVM_NVHE_ALIAS for PI in hyp-image.h.
Quoted 27 lines
arch/arm64/kernel/image-vars.h +0
-0#endif /* __ARM64_KERNEL_IMAGE_VARS_H */ arch/arm64/kvm/hyp/nvhe/Makefile +4
-0@@ -6,10 +6,14 @@ asflags-y := -D__KVM_NVHE_HYPERVISOR__ ccflags-y := -D__KVM_NVHE_HYPERVISOR__ +lib-objs := clear_page.o copy_page.o memcpy.o memset.o +lib-objs := $(addprefix ../../../lib/, $(lib-objs)) + obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o +obj-y += $(lib-objs) ## ## Build rules for compiling nVHE hyp codeSignature · 4 lines
2.29.2.299.gdc1121823c-goog -- To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 08/27] KVM: arm64: Make kvm_call_hyp() a function call at Hyp
In reply to Quentin PerretQuoted 25 lines · Quentin Perret' via kernel-team
kvm_call_hyp()has some logic to issue a function call or a hypercall depending the EL at which the kernel is running. However, all the code compiled under__KVM_NVHE_HYPERVISOR__is guaranteed to run only at EL2, and in this case a simple function call is needed.Add ifdefery to
kvm_host.hto symplifykvm_call_hyp()in .hyp.text.Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 6 ++++++ 1 file changed, 6 insertions(+)arch/arm64/include/asm/kvm_host.h +1
-0@@ -557,6 +557,7 @@ int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);void kvm_arm_halt_guest (struct kvm *kvm);void kvm_arm_resume_guest (struct kvm *kvm); +#ifndef __KVM_NVHE_HYPERVISOR__#define kvm_call_hyp_nvhe (f, ...) \ ({ \ struct arm_smccc_res res; \
arch/arm64/include/asm/kvm_host.h void kvm_arm_resume_guest(struct kvm *kvm); · line 604
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +5
-0@@ -596,6 +597,11 @@ void kvm_arm_resume_guest(struct kvm *kvm); \ ret; \ }) +#else /* __KVM_NVHE_HYPERVISOR__ */ +#define kvm_call_hyp (f, ...) f(__VA_ARGS__) +#define kvm_call_hyp_ret (f, ...) f(__VA_ARGS__) +#define kvm_call_hyp_nvhe (f, ...) f(__VA_ARGS__) +#endif /* __KVM_NVHE_HYPERVISOR__ */
I was hoping we could define this as the following instead. That would require adding host-side declarations of all functions currently called with _nvhe.
#define kvm_call_hyp_nvhe(f, ...) \
+ is_nvhe_hyp_code() ? f(__VA_ARGS__) : \
({ \
struct arm_smccc_res res; \
\
arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f), \
##__VA_ARGS__, &res); \
WARN_ON(res.a0 != SMCCC_RET_SUCCESS); \
\
res.a1; \
})Up to you what you think is cleaner, just my 2 cents...
_______________________________________________
kvmarm mailing list [email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 09/27] KVM: arm64: Allow using kvm_nvhe_sym() in hyp code
In reply to Quentin PerretQuoted 54 lines · Quentin Perret' via kernel-team
In order to allow the usage of code shared by the host and the hyp in static inline library function, allow the usage of
kvm_nvhe_sym()at el2 by defaulting to the raw symbol name.Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/hyp_image.h | 4 ++++ arch/arm64/include/asm/kvm_asm.h | 4 ++-- arch/arm64/kvm/arm.c | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-)arch/arm64/include/asm/hyp_image.h +4
-0@@ -7,11 +7,15 @@#ifndef __ARM64_HYP_IMAGE_H__#define __ARM64_HYP_IMAGE_H__ +#ifndef __KVM_NVHE_HYPERVISOR__/* * KVM nVHE code has its own symbol namespace prefixed with __kvm_nvhe_, * to separate it from the kernel proper. */ #define kvm_nvhe_sym (sym) __kvm_nvhe_##sym +#else +#define kvm_nvhe_sym (sym) sym +#endif #ifdef LINKER_SCRIPT arch/arm64/include/asm/kvm_asm.h +2
-2@@ -173,11 +173,11 @@ struct kvm_s2_mmu;DECLARE_KVM_NVHE_SYM (__kvm_hyp_init);DECLARE_KVM_NVHE_SYM (__kvm_hyp_host_vector);DECLARE_KVM_HYP_SYM (__kvm_hyp_vector); -DECLARE_KVM_NVHE_SYM (__kvm_hyp_psci_cpu_entry);#define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init)#define __kvm_hyp_host_vector CHOOSE_NVHE_SYM(__kvm_hyp_host_vector)#define __kvm_hyp_vector CHOOSE_HYP_SYM(__kvm_hyp_vector) -#define __kvm_hyp_psci_cpu_entry CHOOSE_NVHE_SYM(__kvm_hyp_psci_cpu_entry) + +void kvm_nvhe_sym (__kvm_hyp_psci_cpu_entry )(void );extern unsigned long kvm_arm_hyp_percpu_base[NR_CPUS ];DECLARE_KVM_NVHE_SYM (__per_cpu_start);arch/arm64/kvm/arm.c +0
-0
arch/arm64/kvm/arm.c static void cpu_prepare_hyp_mode(int cpu) · line 1372
Quoted 5 lines
arch/arm64/kvm/arm.c +1
-1@@ -1369,7 +1369,7 @@ static void cpu_prepare_hyp_mode(int cpu) params->vector_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_host_vector)); params->stack_hyp_va =kern_hyp_va (per_cpu (kvm_arm_hyp_stack_page, cpu) +PAGE_SIZE ); - params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_psci_cpu_entry)); + params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref_nvhe (__kvm_hyp_psci_cpu_entry));
Why is this change needed?
Quoted 9 lines
arch/arm64/kvm/arm.c +0
-0params->pgd_pa =kvm_mmu_get_httbr ();/* Signature · 4 lines
2.29.2.299.gdc1121823c-goog -- To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 13/27] KVM: arm64: Enable access to sanitized CPU features at EL2
In reply to Quentin PerretQuoted 20 lines
arch/arm64/kernel/cpufeature.c +12
-0+int copy_ftr_reg (u32 id,struct arm64_ftr_reg *dst) +{ +struct arm64_ftr_reg *regp =get_arm64_ftr_reg (id); + +if (!regp) +return -EINVAL ; + +memcpy (dst, regp,sizeof (*regp)); + +return 0 ; +} +#define read_sysreg_case (r) \ case r: return read_sysreg_s(r)arch/arm64/kernel/image-vars.h +0
-0
arch/arm64/kernel/image-vars.h __kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; · line 120
Quoted 5 lines
arch/arm64/kernel/image-vars.h +2
-0@@ -116,6 +116,8 @@ __kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; __kvm_nvhe___memset = __kvm_nvhe___pi_memset;#endif +_kvm_nvhe___flush_dcache_area = __kvm_nvhe___pi___flush_dcache_area; +
Could you help my understand why we need this?
* Why do we need PI routines in the first place? Would my series that fixes relocations in hyp code remove the need?
* You added these aliases for the string routines because you were worried somebody would change the implementation in arch/arm64/lib, right? But this cache flush function is defined in hyp/nvhe. So why do we need to point to the PI alias if we control the implementation?
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 13/27] KVM: arm64: Enable access to sanitized CPU features at EL2
In reply to Fuad TabbaQuoted 5 lines · Fuad Tabba
Quoted 5 lines
arch/arm64/include/asm/kvm_cpufeature.h new file +0
-0
Quoted 8 lines
arch/arm64/include/asm/kvm_cpufeature.h line 5
Quoted 6 lines
arch/arm64/include/asm/kvm_cpufeature.h new file +5
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2020 - Google LLC +* Author: Quentin Perret <[email protected]> +*/ Missing include guard.
Right, but on purpose :)
See how arm.c includes this header twice with different definitions of KVM_HYP_CPU_FTR_REG for instance.
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 09/27] KVM: arm64: Allow using kvm_nvhe_sym() in hyp code
In reply to David BrazdilQuoted 6 lines · David Brazdil
Quoted 6 lines
arch/arm64/kvm/arm.c +0
-0@@ -1369,7 +1369,7 @@ static void cpu_prepare_hyp_mode(int cpu)
Quoted 6 lines
arch/arm64/kvm/arm.c static void cpu_prepare_hyp_mode(int cpu) · line 1372
Quoted 4 lines
arch/arm64/kvm/arm.c +1
-1params->vector_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_host_vector)); params->stack_hyp_va =kern_hyp_va (per_cpu (kvm_arm_hyp_stack_page, cpu) +PAGE_SIZE ); - params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_psci_cpu_entry)); + params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref_nvhe (__kvm_hyp_psci_cpu_entry));Why is this change needed?
You mean this line specifically or the whole __kvm_hyp_psci_cpu_entry thing?
For the latter, it is to avoid having the compiler complain about __kvm_hyp_psci_cpu_entry being re-defined as a different symbol. If there is a better way to solve this problem I'm happy to change it -- I must admit I got a little confused with the namespacing along the way.
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 02/27] KVM: arm64: Link position-independent string routines into .hyp.text
In reply to David BrazdilQuoted 9 lines · David Brazdil
Quoted 8 lines · Quentin Perret' via kernel-team
arch/arm64/kernel/image-vars.h +0
-0@@ -105,6 +105,17 @@ KVM_NVHE_ALIAS(__stop___kvm_ex_table);/* Array containing bases of nVHE per-CPU memory regions. */ KVM_NVHE_ALIAS (kvm_arm_hyp_percpu_base);
Quoted 15 lines
arch/arm64/kernel/image-vars.h KVM_NVHE_ALIAS(__stop___kvm_ex_table); · line 119
Quoted 12 lines
arch/arm64/kernel/image-vars.h +11
-0+/* Position-independent library routines */ +__kvm_nvhe_clear_page = __kvm_nvhe___pi_clear_page; +__kvm_nvhe_copy_page = __kvm_nvhe___pi_copy_page; +__kvm_nvhe_memcpy = __kvm_nvhe___pi_memcpy; +__kvm_nvhe_memset = __kvm_nvhe___pi_memset; + +#ifdef CONFIG_KASAN +__kvm_nvhe___memcpy = __kvm_nvhe___pi_memcpy; +__kvm_nvhe___memset = __kvm_nvhe___pi_memset; +#endif +#endif /* CONFIG_KVM */ Nit: Would be good to use the
kvm_nvhe_sym()helper for the namespacing. And feel free to define something likeKVM_NVHE_ALIASfor PI inhyp-image.h.
Ack, that'd be much nicer, I'll fix it up for v2.
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 13/27] KVM: arm64: Enable access to sanitized CPU features at EL2
In reply to David BrazdilQuoted 7 lines · David Brazdil
Could you help my understand why we need this?
* Why do we need PI routines in the first place? Would my series that fixes relocations in hyp code remove the need?
* You added these aliases for the string routines because you were worried somebody would change the implementation inarch/arm64/lib, right? But this cache flush function is defined in hyp/nvhe. So why do we need to point to the PI alias if we control the implementation?
Right, in the specific case of the __flush_dcache_area() function none of the PI stuff is really needed I think. I did it this way to keep things as consistent as possible with the host-side implementation, but that is not required.
I understand this can cause confusion, so yes, I'll simplify this for v2.
Cheers,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 09/27] KVM: arm64: Allow using kvm_nvhe_sym() in hyp code
In reply to Quentin PerretQuoted 14 lines · Quentin Perret' via kernel-team
Quoted 6 lines · David Brazdil
Quoted 6 lines
arch/arm64/kvm/arm.c +0
-0@@ -1369,7 +1369,7 @@ static void cpu_prepare_hyp_mode(int cpu)Quoted 6 lines
arch/arm64/kvm/arm.c static void cpu_prepare_hyp_mode(int cpu) · line 1372
Quoted 4 lines
arch/arm64/kvm/arm.c +1
-1params->vector_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_host_vector)); params->stack_hyp_va =kern_hyp_va (per_cpu (kvm_arm_hyp_stack_page, cpu) +PAGE_SIZE ); - params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref (__kvm_hyp_psci_cpu_entry)); + params->entry_hyp_va =kern_hyp_va ((unsigned long )kvm_ksym_ref_nvhe (__kvm_hyp_psci_cpu_entry));Why is this change needed?
Quoted 7 lines
You mean this line specifically or the whole
__kvm_hyp_psci_cpu_entrything?For the latter, it is to avoid having the compiler complain about
__kvm_hyp_psci_cpu_entrybeing re-defined as a different symbol. If there is a better way to solve this problem I'm happy to change it -- I must admit I got a little confused with the namespacing along the way.
Yeah, we do need a more robust approach. It's getting out of control.
Quoted 7 lines
Thanks,
QuentinSignature · 1 line
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Quentin PerretHi Quentin,
Quoted 187 lines · Quentin Perret' via kernel-team
When memory protection is enabled, the Hyp code needs the ability to create and manage its own page-table. To do so, introduce a new set of hypercalls to initialize Hyp memory protection.
During the init hcall, the hypervisor runs with the host-provided page-table and uses the trivial early page allocator to create its own set of page-tables, using a memory pool that was donated by the host. Specifically, the hypervisor creates its own mappings for
__hyp_text, the Hyp memory pool, the__hyp_bss, the portion ofhyp_vmemmapcorresponding to the Hyp pool, among other things. It then jumps back in the idmap page, switches to use the newly-created pgd (instead of the temporary one provided by the host) and then installs the full-fledged buddy allocator which will then be the only one in used from then on.Note that for the sake of symplifying the review, this only introduces the code doing this operation, without actually being called by anyhing yet. This will be done in a subsequent patch, which will introduce the necessary host kernel changes.
Credits to Will for
__kvm_init_switch_pgd.Co-authored-by Will Deacon <[email protected]>Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 6 +- arch/arm64/include/asm/kvm_host.h | 8 + arch/arm64/include/asm/kvm_hyp.h | 8 + arch/arm64/kernel/cpufeature.c | 2 +- arch/arm64/kernel/image-vars.h | 19 +++ arch/arm64/kvm/hyp/Makefile | 2 +- arch/arm64/kvm/hyp/include/nvhe/memory.h | 6 + arch/arm64/kvm/hyp/include/nvhe/mm.h | 79 +++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 4 +- arch/arm64/kvm/hyp/nvhe/hyp-init.S | 30 ++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 44 +++++ arch/arm64/kvm/hyp/nvhe/mm.c | 175 ++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/psci-relay.c | 2 - arch/arm64/kvm/hyp/nvhe/setup.c | 196 +++++++++++++++++++++++ arch/arm64/kvm/hyp/reserved_mem.c | 75 +++++++++ arch/arm64/kvm/mmu.c | 2 +- arch/arm64/mm/init.c | 3 + 17 files changed, 653 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/mm.h create mode 100644 arch/arm64/kvm/hyp/nvhe/mm.c create mode 100644 arch/arm64/kvm/hyp/nvhe/setup.c create mode 100644 arch/arm64/kvm/hyp/reserved_mem.carch/arm64/include/asm/kvm_asm.h +5
-1@@ -57,6 +57,10 @@#define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 12#define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs 13#define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs 14 +#define __KVM_HOST_SMCCC_FUNC___kvm_hyp_protect 15 +#define __KVM_HOST_SMCCC_FUNC___hyp_create_mappings 16 +#define __KVM_HOST_SMCCC_FUNC___hyp_create_private_mapping 17 +#define __KVM_HOST_SMCCC_FUNC___hyp_cpu_set_vector 18#ifndef __ASSEMBLY__@@ -171,7 +175,7 @@ struct kvm_vcpu;struct kvm_s2_mmu ;DECLARE_KVM_NVHE_SYM (__kvm_hyp_init); -DECLARE_KVM_NVHE_SYM (__kvm_hyp_host_vector); +DECLARE_KVM_HYP_SYM (__kvm_hyp_host_vector);DECLARE_KVM_HYP_SYM (__kvm_hyp_vector);#define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init)#define __kvm_hyp_host_vector CHOOSE_NVHE_SYM(__kvm_hyp_host_vector)arch/arm64/include/asm/kvm_host.h +8
-0@@ -742,4 +742,12 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);#define kvm_vcpu_has_pmu (vcpu) \ (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features)) +#ifdef CONFIG_KVM +extern phys_addr_t hyp_mem_base; +extern phys_addr_t hyp_mem_size; +void __init reserve_kvm_hyp (void ); +#else +static inline void reserve_kvm_hyp (void ) { } +#endif +#endif /* __ARM64_KVM_HOST_H__ */ arch/arm64/include/asm/kvm_hyp.h +8
-0@@ -105,5 +105,13 @@ void __noreturn hyp_panic(void);void __noreturn __hyp_do_panic (bool restore_host,u64 spsr,u64 elr,u64 par);#endif +#ifdef __KVM_NVHE_HYPERVISOR__ +void __kvm_init_switch_pgd (phys_addr_t phys,unsigned long size, +phys_addr_t pgd,void *sp,void *cont_fn); +int __kvm_hyp_protect (phys_addr_t phys,unsigned long size, +unsigned long nr_cpus,unsigned long *per_cpu_base); +void __noreturn __host_enter (struct kvm_cpu_context *host_ctxt); +#endif +#endif /* __ARM64_KVM_HYP_H__ */ arch/arm64/kernel/cpufeature.c +1
-1@@ -1722,7 +1722,7 @@ static void cpu_enable_mte(struct arm64_cpu_capabilities const *cap)#endif /* CONFIG_ARM64_MTE */ #ifdef CONFIG_KVM -static bool enable_protected_kvm; +bool enable_protected_kvm;static bool has_protected_kvm (const struct arm64_cpu_capabilities *entry,int __unused) {arch/arm64/kernel/image-vars.h +19
-0@@ -118,6 +118,25 @@ __kvm_nvhe___memset = __kvm_nvhe___pi_memset; _kvm_nvhe___flush_dcache_area = __kvm_nvhe___pi___flush_dcache_area; +/* Hypevisor VA size */ +KVM_NVHE_ALIAS (hyp_va_bits); + +/* Kernel memory sections */ +KVM_NVHE_ALIAS (__start_rodata); +KVM_NVHE_ALIAS (__end_rodata); +KVM_NVHE_ALIAS (__bss_start); +KVM_NVHE_ALIAS (__bss_stop); + +/* Hyp memory sections */ +KVM_NVHE_ALIAS (__hyp_idmap_text_start); +KVM_NVHE_ALIAS (__hyp_idmap_text_end); +KVM_NVHE_ALIAS (__hyp_text_start); +KVM_NVHE_ALIAS (__hyp_text_end); +KVM_NVHE_ALIAS (__hyp_data_ro_after_init_start); +KVM_NVHE_ALIAS (__hyp_data_ro_after_init_end); +KVM_NVHE_ALIAS (__hyp_bss_start); +KVM_NVHE_ALIAS (__hyp_bss_end); +#endif /* CONFIG_KVM */ #endif /* __ARM64_KERNEL_IMAGE_VARS_H */ arch/arm64/kvm/hyp/Makefile +1
-1@@ -10,4 +10,4 @@ subdir-ccflags-y := -I$(incdir) \ -DDISABLE_BRANCH_PROFILING \ $(DISABLE_STACKLEAK_PLUGIN) -obj-$(CONFIG_KVM) += vhe/ nvhe/ pgtable.o +obj-$(CONFIG_KVM) += vhe/ nvhe/ pgtable.o reserved_mem.oarch/arm64/kvm/hyp/include/nvhe/memory.h +6
-0@@ -6,6 +6,12 @@#include <linux/types.h> +#define HYP_MEMBLOCK_REGIONS 128 +struct hyp_memblock_region { +phys_addr_t start; +phys_addr_t end; +}; +struct hyp_pool ;struct hyp_page {unsigned int refcount;arch/arm64/kvm/hyp/include/nvhe/mm.h new file +4
-0@@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_HYP_MM_H +#define __KVM_HYP_MM_H +
arch/arm64/kvm/hyp/include/nvhe/mm.h line 14
Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +10
-0+#include <asm/kvm_pgtable.h> +#include <asm/spectre.h> +#include <linux/types.h> + +#include <nvhe/memory.h> +#include <nvhe/spinlock.h> + +extern struct hyp_memblock_region kvm_nvhe_sym (hyp_memory )[]; +extern int kvm_nvhe_sym (hyp_memblock_nr ); +extern struct kvm_pgtable hyp_pgtable;
nit: I found the name of this struct to be confusing (hyp_pgtable), since there's also
arch/arm64/kvm/mmu.c:25:static struct kvm_pgtable *hyp_pgtable; which has the same name, but is a pointer to the hyp page table before being swapped out in favor of this one.
Quoted 5 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +5
-0+extern hyp_spinlock_t __hyp_pgd_lock; +extern struct hyp_pool hpool; +extern u64 __io_map_base; +extern u32 hyp_va_bits; +
arch/arm64/kvm/hyp/include/nvhe/mm.h line 29
Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +10
-0+int hyp_create_idmap (void ); +int hyp_map_vectors (void ); +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back); +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot); +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot); +int __hyp_create_mappings (unsigned long start,unsigned long size, +unsigned long phys,unsigned long prot); +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot); +
nit: I also thought that the hyp_create_mappings function names are a bit confusing, since there's the create_hyp_mappings functions which use the aforementioned *hyp_pgtable.
Quoted 94 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +50
-0+static inline void hyp_vmemmap_range (phys_addr_t phys,unsigned long size, +unsigned long *start,unsigned long *end) +{ +unsigned long nr_pages = size >>PAGE_SHIFT ; +struct hyp_page *p =hyp_phys_to_page (phys); + + *start = (unsigned long )p; + *end = *start + nr_pages *sizeof (struct hyp_page ); + *start =ALIGN_DOWN (*start,PAGE_SIZE ); + *end =ALIGN (*end,PAGE_SIZE ); +} + +static inline unsigned long __hyp_pgtable_max_pages (unsigned long nr_pages) +{ +unsigned long total =0 , i; + +/* Provision the worst case scenario with 4 levels of page-table */ +for (i =0 ; i <4 ; i++) { + nr_pages =DIV_ROUND_UP (nr_pages,PTRS_PER_PTE ); + total += nr_pages; + } + +return total; +} + +static inline unsigned long hyp_s1_pgtable_size (void ) +{ +struct hyp_memblock_region *reg; +unsigned long nr_pages, res =0 ; +int i; + +if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) +return 0 ; + +for (i =0 ; i <kvm_nvhe_sym (hyp_memblock_nr); i++) { + reg = &kvm_nvhe_sym (hyp_memory)[i]; + nr_pages = (reg->end - reg->start) >>PAGE_SHIFT ; + nr_pages =__hyp_pgtable_max_pages (nr_pages); + res += nr_pages <<PAGE_SHIFT ; + } + +/* Allow 1 GiB for private mappings */ + nr_pages = (1 <<30 ) >>PAGE_SHIFT ; + nr_pages =__hyp_pgtable_max_pages (nr_pages); + res += nr_pages <<PAGE_SHIFT ; + +return res; +} + +#endif /* __KVM_HYP_MM_H */ arch/arm64/kvm/hyp/nvhe/Makefile +2
-2@@ -11,9 +11,9 @@ lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \ - cache.o cpufeature.o + cache.o cpufeature.o setup.o mm.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ - ../fpsimd.o ../hyp-entry.o ../exception.o + ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o obj-y += $(lib-objs) ##arch/arm64/kvm/hyp/nvhe/hyp-init.S +20
-0@@ -247,4 +247,34 @@ alternative_else_nop_endif SYM_CODE_END(__kvm_handle_stub_hvc) +SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA]
arch/arm64/kvm/hyp/nvhe/hyp-init.S alternative_else_nop_endif · line 279
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +10
-0+ mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isb + msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) +
Should the instruction cache be flushed here (ic iallu), to discard speculatively fetched instructions?
Quoted 591 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +0
-0.popsectionarch/arm64/kvm/hyp/nvhe/hyp-main.c +44
-0@@ -6,12 +6,15 @@#include <hyp/switch.h> +#include <asm/pgtable-types.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h> #include <asm/kvm_host.h> #include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> +#include <nvhe/mm.h> + DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);#define cpu_reg (ctxt, r) (ctxt)->regs.regs[r]@@ -106,6 +109,43 @@ static void handle___vgic_v3_restore_aprs(struct kvm_cpu_context *host_ctxt)__vgic_v3_restore_aprs (kern_hyp_va (cpu_if)); } +static void handle___kvm_hyp_protect (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (phys_addr_t, phys, host_ctxt,1 ); + DECLARE_REG(unsigned long, size, host_ctxt, 2); + DECLARE_REG(unsigned long, nr_cpus, host_ctxt, 3); + DECLARE_REG(unsigned long *, per_cpu_base, host_ctxt, 4); + +cpu_reg (host_ctxt,1 ) =__kvm_hyp_protect (phys, size, nr_cpus, + per_cpu_base); +} + +static void handle___hyp_cpu_set_vector (struct kvm_cpu_context *host_ctxt) +{ + DECLARE_REG(enum arm64_hyp_spectre_vector, slot, host_ctxt, 1); + +cpu_reg (host_ctxt,1 ) =hyp_cpu_set_vector (slot); +} + +static void handle___hyp_create_mappings (struct kvm_cpu_context *host_ctxt) +{ + DECLARE_REG(unsigned long, start, host_ctxt, 1); + DECLARE_REG(unsigned long, size, host_ctxt, 2); + DECLARE_REG(unsigned long, phys, host_ctxt, 3); + DECLARE_REG(unsigned long, prot, host_ctxt, 4); + +cpu_reg (host_ctxt,1 ) =__hyp_create_mappings (start, size, phys, prot); +} + +static void handle___hyp_create_private_mapping (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (phys_addr_t, phys, host_ctxt,1 ); +DECLARE_REG (size_t, size, host_ctxt,2 ); + DECLARE_REG(unsigned long, prot, host_ctxt, 3); + +cpu_reg (host_ctxt,1 ) =__hyp_create_private_mapping (phys, size, prot); +} +typedef void (*hcall_t )(struct kvm_cpu_context *);#define HANDLE_FUNC (x) [__KVM_HOST_SMCCC_FUNC_##x] = kimg_fn_ptr(handle_##x)@@ -125,6 +165,10 @@ static const hcall_t *host_hcall[] = {HANDLE_FUNC (__kvm_get_mdcr_el2),HANDLE_FUNC (__vgic_v3_save_aprs),HANDLE_FUNC (__vgic_v3_restore_aprs), +HANDLE_FUNC (__kvm_hyp_protect), +HANDLE_FUNC (__hyp_cpu_set_vector), +HANDLE_FUNC (__hyp_create_mappings), + HANDLE_FUNC(__hyp_create_private_mapping), };static void handle_host_hcall (struct kvm_cpu_context *host_ctxt)arch/arm64/kvm/hyp/nvhe/mm.c new file +175
-0@@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2020 Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#include <linux/kvm_host.h> +#include <asm/kvm_hyp.h> +#include <asm/kvm_mmu.h> +#include <asm/kvm_pgtable.h> +#include <asm/spectre.h> + +#include <nvhe/early_alloc.h> +#include <nvhe/gfp.h> +#include <nvhe/memory.h> +#include <nvhe/mm.h> +#include <nvhe/spinlock.h> + +struct kvm_pgtable hyp_pgtable; + +hyp_spinlock_t __hyp_pgd_lock; +u64 __io_map_base; + +struct hyp_memblock_region hyp_memory[HYP_MEMBLOCK_REGIONS ]; +int hyp_memblock_nr; + +int __hyp_create_mappings (unsigned long start,unsigned long size, +unsigned long phys,unsigned long prot) +{ +int err; + +hyp_spin_lock (&__hyp_pgd_lock); + err =kvm_pgtable_hyp_map (&hyp_pgtable, start, size, phys, prot); +hyp_spin_unlock (&__hyp_pgd_lock); + +return err; +} + +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot) +{ +unsigned long addr; +int ret; + +hyp_spin_lock (&__hyp_pgd_lock); + + size =PAGE_ALIGN (size +offset_in_page (phys)); + addr = __io_map_base; + __io_map_base += size; + +/* Are we overflowing on the vmemmap ? */ +if (__io_map_base > __hyp_vmemmap) { + __io_map_base -= size; + addr =0 ; + goto out; + } + + ret =kvm_pgtable_hyp_map (&hyp_pgtable, addr, size, phys, prot); +if (ret) { + addr =0 ; + goto out; + } + + addr = addr +offset_in_page (phys); +out: +hyp_spin_unlock (&__hyp_pgd_lock); + +return addr; +} + +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot) +{ +unsigned long start = (unsigned long )from; +unsigned long end = (unsigned long )to; +unsigned long virt_addr; +phys_addr_t phys; + + start = start &PAGE_MASK ; + end =PAGE_ALIGN (end); + +for (virt_addr = start; virt_addr < end; virt_addr +=PAGE_SIZE ) { +int err; + + phys =hyp_virt_to_phys ((void *)virt_addr); + err =__hyp_create_mappings (virt_addr,PAGE_SIZE , phys, prot); +if (err) +return err; + } + +return 0 ; +} + +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back) +{ +unsigned long start, end; + +hyp_vmemmap_range (phys, size, &start, &end); + +return __hyp_create_mappings (start, end - start, back,PAGE_HYP ); +} + +static void *__hyp_bp_vect_base; +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot) +{ +void *vector; + +switch (slot) { +case HYP_VECTOR_DIRECT : { + vector =hyp_symbol_addr (__kvm_hyp_vector); +break ; + } +case HYP_VECTOR_SPECTRE_DIRECT : { + vector =hyp_symbol_addr (__bp_harden_hyp_vecs); +break ; + } +case HYP_VECTOR_INDIRECT : +case HYP_VECTOR_SPECTRE_INDIRECT : { + vector = (void *)__hyp_bp_vect_base; +break ; + } +default : +return -EINVAL ; + } + + vector =__kvm_vector_slot2addr (vector, slot); + *this_cpu_ptr (&kvm_hyp_vector) = (unsigned long )vector; + +return 0 ; +} + +int hyp_map_vectors (void ) +{ +unsigned long bp_base; + +if (!cpus_have_const_cap (ARM64_SPECTRE_V3A )) +return 0 ; + + bp_base = (unsigned long )hyp_symbol_addr (__bp_harden_hyp_vecs); + bp_base =__hyp_pa (bp_base); + bp_base =__hyp_create_private_mapping (bp_base, __BP_HARDEN_HYP_VECS_SZ, +PAGE_HYP_EXEC ); +if (!bp_base) +return -1 ; + + __hyp_bp_vect_base = (void *)bp_base; + +return 0 ; +} + +int hyp_create_idmap (void ) +{ +unsigned long start, end; + + start = (unsigned long )hyp_symbol_addr (__hyp_idmap_text_start); + start =hyp_virt_to_phys ((void *)start); + start =ALIGN_DOWN (start,PAGE_SIZE ); + + end = (unsigned long )hyp_symbol_addr (__hyp_idmap_text_end); + end =hyp_virt_to_phys ((void *)end); + end =ALIGN (end,PAGE_SIZE ); + +/* +* One half of the VA space is reserved to linearly map portions of +* memory -- see va_layout.c for more details. The other half of the VA +* space contains the trampoline page, and needs some care. Split that +* second half in two and find the quarter of VA space not conflicting +* with the idmap to place the IOs and the vmemmap. IOs use the lower +* half of the quarter and the vmemmap the upper half. +*/ + __io_map_base = start &BIT (hyp_va_bits -2 ); + __io_map_base ^=BIT (hyp_va_bits -2 ); + __hyp_vmemmap = __io_map_base |BIT (hyp_va_bits -3 ); + +return __hyp_create_mappings (start, end - start, start,PAGE_HYP_EXEC ); +}arch/arm64/kvm/hyp/nvhe/psci-relay.c +0
-2@@ -193,8 +193,6 @@ static int psci_cpu_on(u64 func_id, struct kvm_cpu_context *host_ctxt)return ret; } -void __noreturn __host_enter (struct kvm_cpu_context *host_ctxt); - asmlinkage void __noreturn __kvm_hyp_psci_cpu_entry(void) {struct kvm_host_psci_state *cpu_state =this_cpu_ptr (&kvm_host_psci_state);arch/arm64/kvm/hyp/nvhe/setup.c new file +196
-0@@ -0,0 +1,196 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2020 Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#include <linux/kvm_host.h> +#include <asm/kvm_hyp.h> +#include <asm/kvm_mmu.h> +#include <asm/kvm_pgtable.h> + +#include <nvhe/early_alloc.h> +#include <nvhe/gfp.h> +#include <nvhe/memory.h> +#include <nvhe/mm.h> + +struct hyp_pool hpool; +struct kvm_pgtable_mm_ops hyp_pgtable_mm_ops; +unsigned long hyp_nr_cpus; + +#define hyp_percpu_size ((unsigned long)__per_cpu_end - \ + (unsigned long)__per_cpu_start) + +static void *stacks_base; +static void *vmemmap_base; +static void *hyp_pgt_base; + +static int divide_memory_pool (void *virt,unsigned long size) +{ +unsigned long vstart, vend, nr_pages; + +hyp_early_alloc_init (virt, size); + + stacks_base =hyp_early_alloc_contig (hyp_nr_cpus); +if (!stacks_base) +return -ENOMEM ; + +hyp_vmemmap_range (__hyp_pa (virt), size, &vstart, &vend); + nr_pages = (vend - vstart) >>PAGE_SHIFT ; + vmemmap_base =hyp_early_alloc_contig (nr_pages); +if (!vmemmap_base) +return -ENOMEM ; + + nr_pages =hyp_s1_pgtable_size () >>PAGE_SHIFT ; + hyp_pgt_base =hyp_early_alloc_contig (nr_pages); +if (!hyp_pgt_base) +return -ENOMEM ; + +return 0 ; +} + +static int recreate_hyp_mappings (phys_addr_t phys,unsigned long size, +unsigned long *per_cpu_base) +{ +void *start, *end, *virt =hyp_phys_to_virt (phys); +int ret, i; + +/* Recreate the hyp page-table using the early page allocator */ +hyp_early_alloc_init (hyp_pgt_base,hyp_s1_pgtable_size ()); + ret =kvm_pgtable_hyp_init (&hyp_pgtable, hyp_va_bits, + &hyp_early_alloc_mm_ops); +if (ret) +return ret; + + ret =hyp_create_idmap (); +if (ret) +return ret; + + ret =hyp_map_vectors (); +if (ret) +return ret; + + ret =hyp_back_vmemmap (phys, size,hyp_virt_to_phys (vmemmap_base)); +if (ret) +return ret; + + ret =hyp_create_mappings (hyp_symbol_addr (__hyp_text_start), +hyp_symbol_addr (__hyp_text_end), +PAGE_HYP_EXEC ); +if (ret) +return ret; + + ret =hyp_create_mappings (hyp_symbol_addr (__start_rodata), +hyp_symbol_addr (__end_rodata),PAGE_HYP_RO ); +if (ret) +return ret; + + ret =hyp_create_mappings (hyp_symbol_addr (__hyp_data_ro_after_init_start), +hyp_symbol_addr (__hyp_data_ro_after_init_end), +PAGE_HYP_RO ); +if (ret) +return ret; + + ret =hyp_create_mappings (hyp_symbol_addr (__bss_start), +hyp_symbol_addr (__hyp_bss_end),PAGE_HYP ); +if (ret) +return ret; + + ret =hyp_create_mappings (hyp_symbol_addr (__hyp_bss_end), +hyp_symbol_addr (__bss_stop),PAGE_HYP_RO ); +if (ret) +return ret; + + ret =hyp_create_mappings (virt, virt + size -1 ,PAGE_HYP ); +if (ret) +return ret; + +for (i =0 ; i < hyp_nr_cpus; i++) { + start = (void *)kern_hyp_va (per_cpu_base[i]); + end = start +PAGE_ALIGN (hyp_percpu_size); + ret =hyp_create_mappings (start, end,PAGE_HYP ); +if (ret) +return ret; + } + +return 0 ; +} + +static void update_nvhe_init_params (void ) +{ +struct kvm_nvhe_init_params *params; +unsigned long i, stack; + +for (i =0 ; i < hyp_nr_cpus; i++) { + stack = (unsigned long )stacks_base + (i <<PAGE_SHIFT ); + params =per_cpu_ptr (&kvm_init_params, i); + params->stack_hyp_va = stack +PAGE_SIZE ; + params->pgd_pa =__hyp_pa (hyp_pgtable.pgd); +__flush_dcache_area (params,sizeof (*params)); + } +} + +static void *hyp_zalloc_hyp_page (void *arg) +{ +return hyp_alloc_pages (&hpool,HYP_GFP_ZERO ,0 ); +} + +void __noreturn __kvm_hyp_protect_finalise (void ) +{ +struct kvm_host_data *host_data =this_cpu_ptr (&kvm_host_data); +struct kvm_cpu_context *host_ctxt = &host_data->host_ctxt; +unsigned long nr_pages, used_pages; +int ret; + +/* Now that the vmemmap is backed, install the full-fledged allocator */ + nr_pages =hyp_s1_pgtable_size () >>PAGE_SHIFT ; + used_pages =hyp_early_alloc_nr_pages (); + ret =hyp_pool_init (&hpool,__hyp_pa (hyp_pgt_base), nr_pages, used_pages); +if (ret) + goto out; + + hyp_pgtable_mm_ops.zalloc_page = hyp_zalloc_hyp_page; + hyp_pgtable_mm_ops.phys_to_virt = hyp_phys_to_virt; + hyp_pgtable_mm_ops.virt_to_phys = hyp_virt_to_phys; + hyp_pgtable_mm_ops.get_page = hyp_get_page; + hyp_pgtable_mm_ops.put_page = hyp_put_page; + hyp_pgtable.mm_ops = &hyp_pgtable_mm_ops; + +out: + host_ctxt->regs.regs[0 ] =SMCCC_RET_SUCCESS ; + host_ctxt->regs.regs[1 ] = ret; + +__host_enter (host_ctxt); +} + +int __kvm_hyp_protect (phys_addr_t phys,unsigned long size, +unsigned long nr_cpus,unsigned long *per_cpu_base) +{ +struct kvm_nvhe_init_params *params; +void *virt =hyp_phys_to_virt (phys); +void (*fn)(phys_addr_t params_pa,void *finalize_fn_va); +int ret; + +if (phys %PAGE_SIZE || size %PAGE_SIZE || (u64 )virt %PAGE_SIZE ) +return -EINVAL ; + +hyp_spin_lock_init (&__hyp_pgd_lock); + hyp_nr_cpus = nr_cpus; + + ret =divide_memory_pool (virt, size); +if (ret) +return ret; + + ret =recreate_hyp_mappings (phys, size, per_cpu_base); +if (ret) +return ret; + +update_nvhe_init_params (); + +/* Jump in the idmap page to switch to the new page-tables */ + params =this_cpu_ptr (&kvm_init_params); + fn = (typeof (fn ))__hyp_pa (hyp_symbol_addr (__kvm_init_switch_pgd)); +fn (__hyp_pa (params),hyp_symbol_addr (__kvm_hyp_protect_finalise)); + +unreachable (); +}arch/arm64/kvm/hyp/reserved_mem.c new file +75
-0@@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020 - Google LLC +* Author: Quentin Perret <[email protected]> +*/ + +#include <linux/kvm_host.h> +#include <linux/memblock.h> + +#include <asm/kvm_host.h> + +#include <nvhe/memory.h> +#include <nvhe/mm.h> + +phys_addr_t hyp_mem_base; +phys_addr_t hyp_mem_size; + +void __init early_init_dt_add_memory_hyp (u64 base,u64 size) +{ +struct hyp_memblock_region *reg; + +if (kvm_nvhe_sym (hyp_memblock_nr) >=HYP_MEMBLOCK_REGIONS ) +kvm_nvhe_sym (hyp_memblock_nr) =-1 ; + +if (kvm_nvhe_sym (hyp_memblock_nr) <0 ) +return ; + + reg =kvm_nvhe_sym (hyp_memory); + reg[kvm_nvhe_sym (hyp_memblock_nr)].start = base; + reg[kvm_nvhe_sym (hyp_memblock_nr)].end = base + size; +kvm_nvhe_sym (hyp_memblock_nr)++; +} + +extern bool enable_protected_kvm; +void __init reserve_kvm_hyp (void ) +{ +u64 nr_pages, prev; + +if (!enable_protected_kvm) +return ; + +if (!is_hyp_mode_available () ||is_kernel_in_hyp_mode ()) +return ; + +if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) +return ; + + hyp_mem_size +=num_possible_cpus () <<PAGE_SHIFT ; + hyp_mem_size +=hyp_s1_pgtable_size (); + +/* +* The hyp_vmemmap needs to be backed by pages, but these pages +* themselves need to be present in the vmemmap, so compute the number +* of pages needed by looking for a fixed point. +*/ + nr_pages =0 ; +do { + prev = nr_pages; + nr_pages = (hyp_mem_size >>PAGE_SHIFT ) + prev; + nr_pages =DIV_ROUND_UP (nr_pages *sizeof (struct hyp_page ),PAGE_SIZE ); + nr_pages +=__hyp_pgtable_max_pages (nr_pages); + }while (nr_pages != prev); + hyp_mem_size += nr_pages <<PAGE_SHIFT ; + + hyp_mem_base =memblock_find_in_range (0 ,memblock_end_of_DRAM (), + hyp_mem_size,SZ_2M ); +if (!hyp_mem_base) { +kvm_err ("Failed to reserve hyp memory\n" ); +return ; + } +memblock_reserve (hyp_mem_base, hyp_mem_size); + +kvm_info ("Reserved %lld MiB at 0x%llx\n" , hyp_mem_size >>20 , + hyp_mem_base); +}arch/arm64/kvm/mmu.c +1
-1@@ -1264,10 +1264,10 @@ static struct kvm_pgtable_mm_ops kvm_hyp_mm_ops = { .virt_to_phys = kvm_host_pa, }; +u32 hyp_va_bits;int kvm_mmu_init (void ) {int err; -u32 hyp_va_bits; hyp_idmap_start =__pa_symbol (__hyp_idmap_text_start); hyp_idmap_start =ALIGN_DOWN (hyp_idmap_start,PAGE_SIZE );arch/arm64/mm/init.c +3
-1@@ -34,6 +34,7 @@#include <asm/fixmap.h> #include <asm/kasan.h> #include <asm/kernel-pgtable.h> +#include <asm/kvm_host.h> #include <asm/memory.h> #include <asm/numa.h> #include <asm/sections.h> @@ -390,6 +391,8 @@ void __init arm64_memblock_init(void)reserve_elfcorehdr (); +reserve_kvm_hyp (); + high_memory =__va (memblock_end_of_DRAM () -1 ) +1 ;dma_contiguous_reserve (arm64_dma32_phys_limit); --
Quoted 1 line
2.29.2.299.gdc1121823c-goog
Cheers,
/fuad
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>
Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +6
-0+int hyp_create_idmap (void ); +int hyp_map_vectors (void ); +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back); +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot); +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot); +int __hyp_create_mappings(unsigned long start, unsigned long size,
Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h line 29
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +4
-0+ unsigned long phys, unsigned long prot); +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot); +nit: I also thought that the
hyp_create_mappingsfunction names are a bit confusing, since there's thecreate_hyp_mappingsfunctions which use the aforementioned*hyp_pgtable.
Sure, happy to re-name those (and hyp_pgtable above). Any suggestions?
<snip>
Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +25
-0+SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] + mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isb
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S alternative_else_nop_endif · line 279
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +5
-0+ msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) +Should the instruction cache be flushed here (ic iallu), to discard speculatively fetched instructions?
Hmm, Will? Thoughts?
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Will Deacon
maintainerRe: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
Quoted 55 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +6
-0+int hyp_create_idmap (void ); +int hyp_map_vectors (void ); +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back); +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot); +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot); +int __hyp_create_mappings(unsigned long start, unsigned long size,Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h line 29
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +4
-0+ unsigned long phys, unsigned long prot); +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot); +nit: I also thought that the
hyp_create_mappingsfunction names are a bit confusing, since there's thecreate_hyp_mappingsfunctions which use the aforementioned*hyp_pgtable.Sure, happy to re-name those (and
hyp_pgtableabove). Any suggestions?<snip>
Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +25
-0+SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] + mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isbQuoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S alternative_else_nop_endif · line 279
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +5
-0+ msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) +Should the instruction cache be flushed here (ic iallu), to discard speculatively fetched instructions?
Quoted 1 line
Hmm, Will? Thoughts?
The I-cache is physically tagged, so not sure what invalidation would achieve here. Fuad -- what do you think could go wrong specifically?
Will
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Mark Rutland
reviewerRe: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
Quoted 37 lines · Will Deacon
Quoted 36 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 33 lines
Quoted 20 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +20
-0+SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA]arch/arm64/kvm/hyp/nvhe/hyp-init.S alternative_else_nop_endif · line 279
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +10
-0+ mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isb + msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) +Should the instruction cache be flushed here (ic iallu), to discard speculatively fetched instructions?
Quoted 4 lines
Quoted 1 line
Hmm, Will? Thoughts?
The I-cache is physically tagged, so not sure what invalidation would achieve here. Fuad -- what do you think could go wrong specifically?
While the MMU is off, instruction fetches can be made from the PoC rather than the PoU, so where instructions have been modified/copied and not cleaned to the PoC, it's possible to fetch stale copies into the I-caches. The physical tag doesn't prevent that.
In the regular CPU boot paths, __enabble_mmu() has an IC IALLU after enabling the MMU to ensure that we get rid of anything stale (e.g. so secondaries don't miss ftrace patching, which is only cleaned to the PoU).
That might not be a problem here, if things are suitably padded and never dynamically patched, but if so it's probably worth a comment.
Fuad, is that the sort of thing you were considering, or did you have additional concerns?
Thanks,
Mark.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Will Deacon
maintainerRe: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
Quoted 40 lines · Mark Rutland
Quoted 39 lines · Will Deacon
Quoted 37 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +25
-0+SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] + mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isbQuoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S alternative_else_nop_endif · line 279
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +5
-0+ msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) +Should the instruction cache be flushed here (ic iallu), to discard speculatively fetched instructions?
Hmm, Will? Thoughts?
Quoted 7 lines
Quoted 2 lines
The I-cache is physically tagged, so not sure what invalidation would achieve here. Fuad -- what do you think could go wrong specifically?
While the MMU is off, instruction fetches can be made from the PoC rather than the PoU, so where instructions have been modified/copied and not cleaned to the PoC, it's possible to fetch stale copies into the I-caches. The physical tag doesn't prevent that.
Oh yeah, we even have a comment about that in idmap_kpti_install_ng_mappings(). Maybe we should wrap disable_mmu and enable_mmu in some macros so we don't have to trip over this every time (and this would mean we could get rid of pre_disable_mmu_workaround too).
Quoted 7 lines
In the regular CPU boot paths,
__enabble_mmu()has an IC IALLU after enabling the MMU to ensure that we get rid of anything stale (e.g. so secondaries don't miss ftrace patching, which is only cleaned to the PoU).That might not be a problem here, if things are suitably padded and never dynamically patched, but if so it's probably worth a comment.
It's fragile enough that we should just do the invalidation.
Will
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Mark RutlandQuoted 54 lines · Mark Rutland
Quoted 37 lines · Will Deacon
Quoted 36 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 33 lines
Quoted 20 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +20
-0+SYM_FUNC_START(__kvm_init_switch_pgd) + /* Turn the MMU off */ + pre_disable_mmu_workaround + mrs x2, sctlr_el2 + bic x3, x2, #SCTLR_ELx_M + msr sctlr_el2, x3 + isb + + tlbi alle2 + + /* Install the new pgtables */ + ldr x3, [x0, #NVHE_INIT_PGD_PA] + phys_to_ttbr x4, x3 +alternative_if ARM64_HAS_CNP + orr x4, x4, #TTBR_CNP_BIT +alternative_else_nop_endif + msr ttbr0_el2, x4 + + /* Set the new stack pointer */ + ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA]arch/arm64/kvm/hyp/nvhe/hyp-init.S alternative_else_nop_endif · line 279
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-init.S +10
-0+ mov sp, x0 + + /* And turn the MMU back on! */ + dsb nsh + isb + msr sctlr_el2, x2 + isb + ret x1 +SYM_FUNC_END(__kvm_init_switch_pgd) +Should the instruction cache be flushed here (ic iallu), to discard speculatively fetched instructions?
Quoted 4 lines
Quoted 1 line
Hmm, Will? Thoughts?
The I-cache is physically tagged, so not sure what invalidation would achieve here. Fuad -- what do you think could go wrong specifically?
While the MMU is off, instruction fetches can be made from the PoC rather than the PoU, so where instructions have been modified/copied and not cleaned to the PoC, it's possible to fetch stale copies into the I-caches. The physical tag doesn't prevent that.
In the regular CPU boot paths,
__enabble_mmu()has an IC IALLU after enabling the MMU to ensure that we get rid of anything stale (e.g. so secondaries don't miss ftrace patching, which is only cleaned to the PoU).
Quoted 5 lines
That might not be a problem here, if things are suitably padded and never dynamically patched, but if so it's probably worth a comment.
Fuad, is that the sort of thing you were considering, or did you have additional concerns?
No other concerns. Thanks Mark.
/fuad
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Quentin PerretQuoted 18 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +6
-0+int hyp_create_idmap (void ); +int hyp_map_vectors (void ); +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back); +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot); +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot); +int __hyp_create_mappings(unsigned long start, unsigned long size,Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h line 29
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +4
-0+ unsigned long phys, unsigned long prot); +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot); +nit: I also thought that the
hyp_create_mappingsfunction names are a bit confusing, since there's thecreate_hyp_mappingsfunctions which use the aforementioned*hyp_pgtable.
Quoted 1 line
Sure, happy to re-name those (and
hyp_pgtableabove). Any suggestions?
Perhaps something to indicate that these are temporary, tmp_ or bootstrap_ maybe?
Cheers,
/fuad
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
Quoted 19 lines · Fuad Tabba
Quoted 18 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 14 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h line 29
Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +10
-0+int hyp_create_idmap (void ); +int hyp_map_vectors (void ); +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back); +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot); +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot); +int __hyp_create_mappings (unsigned long start,unsigned long size, +unsigned long phys,unsigned long prot); +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot); +nit: I also thought that the
hyp_create_mappingsfunction names are a bit confusing, since there's thecreate_hyp_mappingsfunctions which use the aforementioned*hyp_pgtable.
Quoted 4 lines
Quoted 1 line
Sure, happy to re-name those (and
hyp_pgtableabove). Any suggestions?Perhaps something to indicate that these are temporary,
tmp_orbootstrap_maybe?
Hmm, the thing is these are temporary only in protected mode, they're permanent otherwise :/
Perhaps I could prefix the protected pgtable (and associated functions) with 'pkvm_' or so? Marc, any preferences?
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Will Deacon
maintainerRe: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Quentin PerretHi Quentin,
Quoted 18 lines · Quentin Perret
When memory protection is enabled, the Hyp code needs the ability to create and manage its own page-table. To do so, introduce a new set of hypercalls to initialize Hyp memory protection.
During the init hcall, the hypervisor runs with the host-provided page-table and uses the trivial early page allocator to create its own set of page-tables, using a memory pool that was donated by the host. Specifically, the hypervisor creates its own mappings for
__hyp_text, the Hyp memory pool, the__hyp_bss, the portion ofhyp_vmemmapcorresponding to the Hyp pool, among other things. It then jumps back in the idmap page, switches to use the newly-created pgd (instead of the temporary one provided by the host) and then installs the full-fledged buddy allocator which will then be the only one in used from then on.Note that for the sake of symplifying the review, this only introduces the code doing this operation, without actually being called by anyhing yet. This will be done in a subsequent patch, which will introduce the necessary host kernel changes.
[...]
Quoted 5 lines
arch/arm64/kvm/hyp/reserved_mem.c new file +0
-0
[...]
Quoted 28 lines
arch/arm64/kvm/hyp/reserved_mem.c new file +28
-0+extern bool enable_protected_kvm; +void __init reserve_kvm_hyp (void ) +{ +u64 nr_pages, prev; + +if (!enable_protected_kvm) +return ; + +if (!is_hyp_mode_available () ||is_kernel_in_hyp_mode ()) +return ; + +if (kvm_nvhe_sym (hyp_memblock_nr) <=0 ) +return ; + + hyp_mem_size +=num_possible_cpus () <<PAGE_SHIFT ; + hyp_mem_size +=hyp_s1_pgtable_size (); + +/* +* The hyp_vmemmap needs to be backed by pages, but these pages +* themselves need to be present in the vmemmap, so compute the number +* of pages needed by looking for a fixed point. +*/ + nr_pages =0 ; + do { + prev = nr_pages; + nr_pages = (hyp_mem_size >> PAGE_SHIFT) + prev; + nr_pages = DIV_ROUND_UP(nr_pages * sizeof(struct hyp_page), PAGE_SIZE); + nr_pages += __hyp_pgtable_max_pages(nr_pages);
arch/arm64/kvm/hyp/reserved_mem.c line 71
Quoted 10 lines
arch/arm64/kvm/hyp/reserved_mem.c new file +10
-0+ }while (nr_pages != prev); + hyp_mem_size += nr_pages <<PAGE_SHIFT ; + + hyp_mem_base =memblock_find_in_range (0 ,memblock_end_of_DRAM (), + hyp_mem_size,SZ_2M ); +if (!hyp_mem_base) { +kvm_err ("Failed to reserve hyp memory\n" ); +return ; + } +memblock_reserve (hyp_mem_base, hyp_mem_size);
Why not use the RESERVEDMEM_OF_DECLARE() interface for the hypervisor memory? That way, the hypervisor memory can either be statically partitioned as a carveout or allocated dynamically for us -- we wouldn't need to care.
Will
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerRe: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Quentin PerretQuoted 22 lines · Quentin Perret
Quoted 21 lines · Fuad Tabba
Quoted 19 lines · Quentin Perret
On Thursday 03 Dec 2020 at 12:57:33 (+0000), Fuad Tabba wrote:
<snip>Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +6
-0+int hyp_create_idmap (void ); +int hyp_map_vectors (void ); +int hyp_back_vmemmap (phys_addr_t phys,unsigned long size,phys_addr_t back); +int hyp_cpu_set_vector (enum arm64_hyp_spectre_vector slot); +int hyp_create_mappings (void *from,void *to,enum kvm_pgtable_prot prot); +int __hyp_create_mappings(unsigned long start, unsigned long size,Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h line 29
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h new file +4
-0+ unsigned long phys, unsigned long prot); +unsigned long __hyp_create_private_mapping (phys_addr_t phys,size_t size, +unsigned long prot); +nit: I also thought that the
hyp_create_mappingsfunction names are a bit confusing, since there's thecreate_hyp_mappingsfunctions which use the aforementioned*hyp_pgtable.Sure, happy to re-name those (and
hyp_pgtableabove). Any suggestions?
Quoted 8 lines
Quoted 2 lines
Perhaps something to indicate that these are temporary,
tmp_orbootstrap_maybe?Hmm, the thing is these are temporary only in protected mode, they're permanent otherwise :/
Perhaps I could prefix the protected pgtable (and associated functions) with '
pkvm_' or so? Marc, any preferences?
None. Whichever name you pick, someone will ask you to change it. Just call it Bob.
What I really *don't* want is see a blanket rename of existing symbols or concepts.
Thanks,
M.
Signature · 5 lines
Jazz is not dead. It just smells funny... _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Will DeaconQuoted 3 lines · Will Deacon
Why not use the
RESERVEDMEM_OF_DECLARE()interface for the hypervisor memory? That way, the hypervisor memory can either be statically partitioned as a carveout or allocated dynamically for us -- we wouldn't need to care.
Yup, I did consider that, but the actual amount of memory we need to reserve for the hypervisor depends on things such as the size of struct hyp_page, which depends on the kernel you're running (that is, it might change over time). So, that really felt like something the kernel should be doing, to keep the DT backward compatible, ... Or did you have something more elaborate in mind?
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Marc ZyngierQuoted 2 lines · Marc Zyngier
None. Whichever name you pick, someone will ask you to change it. Just call it Bob.
:-)
Quoted 2 lines
What I really *don't* want is see a blanket rename of existing symbols or concepts.
Understood. I'll go with pkvm_create_mappings() and friends for all the new functions unless someone comes up with a better name in the meantime.
Thanks,
Quentin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Will Deacon
maintainerRe: [RFC PATCH 16/27] KVM: arm64: Prepare Hyp memory protection
In reply to Quentin PerretQuoted 11 lines · Quentin Perret
Quoted 3 lines · Will Deacon
Why not use the
RESERVEDMEM_OF_DECLARE()interface for the hypervisor memory? That way, the hypervisor memory can either be statically partitioned as a carveout or allocated dynamically for us -- we wouldn't need to care.Yup, I did consider that, but the actual amount of memory we need to reserve for the hypervisor depends on things such as the size of struct
hyp_page, which depends on the kernel you're running (that is, it might change over time). So, that really felt like something the kernel should be doing, to keep the DT backward compatible, ... Or did you have something more elaborate in mind?
No, that's fair. Just wanted to make sure we had a good reason not to use the existing memory reservation code.
Will
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm