forked from torvalds/linux
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
KVM: arm64: Prepare the creation of s1 mappings at EL2
When memory protection is enabled, the EL2 code needs the ability to
create and manage its own page-table. To do so, introduce a new set of
hypercalls to bootstrap a memory management system at EL2.
This leads to the following boot flow in nVHE Protected mode:
1. the host allocates memory for the hypervisor very early on, using
the memblock API;
2. the host creates a set of stage 1 page-table for EL2, installs the
EL2 vectors, and issues the __pkvm_init hypercall;
3. during __pkvm_init, the hypervisor re-creates its stage 1 page-table
and stores it in the memory pool provided by the host;
4. the hypervisor then extends its stage 1 mappings to include a
vmemmap in the EL2 VA space, hence allowing to use the buddy
allocator introduced in a previous patch;
5. the hypervisor jumps back in the idmap page, switches from the
host-provided page-table to the new one, and wraps up its
initialization by enabling the new allocator, before returning to
the host.
6. the host can free the now unused page-table created for EL2, and
will now need to issue hypercalls to make changes to the EL2 stage 1
mappings instead of modifying them directly.
Note that for the sake of simplifying the review, this patch focuses on
the hypervisor side of things. In other words, this only implements the
new hypercalls, but does not make use of them from the host yet. The
host-side changes will follow in a subsequent patch.
Credits to Will for __pkvm_init_switch_pgd.
Acked-by: Will Deacon <will@kernel.org>
Co-authored-by: Will Deacon <will@kernel.org>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>- Loading branch information
1 parent
c591c17
commit d9cbcf8f8115531ae6333bb07ed1d865233e7e10
Showing
15 changed files
with
652 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -11,6 +11,8 @@ | ||
| #include <linux/kvm_host.h> | ||
| #include <linux/types.h> | ||
|
|
||
| #define KVM_PGTABLE_MAX_LEVELS 4U | ||
|
|
||
| typedef u64 kvm_pte_t; | ||
|
|
||
| /** | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| #ifndef __KVM_HYP_MM_H | ||
| #define __KVM_HYP_MM_H | ||
|
|
||
| #include <asm/kvm_pgtable.h> | ||
| #include <asm/spectre.h> | ||
| #include <linux/memblock.h> | ||
| #include <linux/types.h> | ||
|
|
||
| #include <nvhe/memory.h> | ||
| #include <nvhe/spinlock.h> | ||
|
|
||
| #define HYP_MEMBLOCK_REGIONS 128 | ||
| extern struct memblock_region kvm_nvhe_sym(hyp_memory)[]; | ||
| extern unsigned int kvm_nvhe_sym(hyp_memblock_nr); | ||
| extern struct kvm_pgtable pkvm_pgtable; | ||
| extern hyp_spinlock_t pkvm_pgd_lock; | ||
| extern struct hyp_pool hpool; | ||
| extern u64 __io_map_base; | ||
|
|
||
| int hyp_create_idmap(u32 hyp_va_bits); | ||
| int hyp_map_vectors(void); | ||
| int hyp_back_vmemmap(phys_addr_t phys, unsigned long size, phys_addr_t back); | ||
| int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot); | ||
| int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot); | ||
| int __pkvm_create_mappings(unsigned long start, unsigned long size, | ||
| unsigned long phys, enum kvm_pgtable_prot prot); | ||
| unsigned long __pkvm_create_private_mapping(phys_addr_t phys, size_t size, | ||
| enum kvm_pgtable_prot prot); | ||
|
|
||
| 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 */ | ||
| for (i = 0; i < KVM_PGTABLE_MAX_LEVELS; i++) { | ||
| nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE); | ||
| total += nr_pages; | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| static inline unsigned long hyp_s1_pgtable_pages(void) | ||
| { | ||
| unsigned long res = 0, i; | ||
|
|
||
| /* Cover all of memory with page-granularity */ | ||
| for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) { | ||
| struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i]; | ||
| res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT); | ||
| } | ||
|
|
||
| /* Allow 1 GiB for private mappings */ | ||
| res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT); | ||
|
|
||
| return res; | ||
| } | ||
| #endif /* __KVM_HYP_MM_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.