1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Google LLC
4 * Author: Quentin Perret <qperret@google.com>
7 #include <linux/kvm_host.h>
8 #include <asm/kvm_hyp.h>
9 #include <asm/kvm_mmu.h>
10 #include <asm/kvm_pgtable.h>
11 #include <asm/kvm_pkvm.h>
13 #include <nvhe/early_alloc.h>
14 #include <nvhe/fixed_config.h>
16 #include <nvhe/memory.h>
17 #include <nvhe/mem_protect.h>
19 #include <nvhe/pkvm.h>
20 #include <nvhe/trap_handler.h>
22 unsigned long hyp_nr_cpus;
24 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
25 (unsigned long)__per_cpu_start)
27 static void *vmemmap_base;
28 static void *vm_table_base;
29 static void *hyp_pgt_base;
30 static void *host_s2_pgt_base;
31 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
32 static struct hyp_pool hpool;
34 static int divide_memory_pool(void *virt, unsigned long size)
36 unsigned long nr_pages;
38 hyp_early_alloc_init(virt, size);
40 nr_pages = hyp_vmemmap_pages(sizeof(struct hyp_page));
41 vmemmap_base = hyp_early_alloc_contig(nr_pages);
45 nr_pages = hyp_vm_table_pages();
46 vm_table_base = hyp_early_alloc_contig(nr_pages);
50 nr_pages = hyp_s1_pgtable_pages();
51 hyp_pgt_base = hyp_early_alloc_contig(nr_pages);
55 nr_pages = host_s2_pgtable_pages();
56 host_s2_pgt_base = hyp_early_alloc_contig(nr_pages);
57 if (!host_s2_pgt_base)
63 static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
64 unsigned long *per_cpu_base,
67 void *start, *end, *virt = hyp_phys_to_virt(phys);
68 unsigned long pgt_size = hyp_s1_pgtable_pages() << PAGE_SHIFT;
69 enum kvm_pgtable_prot prot;
72 /* Recreate the hyp page-table using the early page allocator */
73 hyp_early_alloc_init(hyp_pgt_base, pgt_size);
74 ret = kvm_pgtable_hyp_init(&pkvm_pgtable, hyp_va_bits,
75 &hyp_early_alloc_mm_ops);
79 ret = hyp_create_idmap(hyp_va_bits);
83 ret = hyp_map_vectors();
87 ret = hyp_back_vmemmap(hyp_virt_to_phys(vmemmap_base));
91 ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, PAGE_HYP_EXEC);
95 ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO);
99 ret = pkvm_create_mappings(__hyp_bss_start, __hyp_bss_end, PAGE_HYP);
103 ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP);
107 for (i = 0; i < hyp_nr_cpus; i++) {
108 struct kvm_nvhe_init_params *params = per_cpu_ptr(&kvm_init_params, i);
109 unsigned long hyp_addr;
111 start = (void *)kern_hyp_va(per_cpu_base[i]);
112 end = start + PAGE_ALIGN(hyp_percpu_size);
113 ret = pkvm_create_mappings(start, end, PAGE_HYP);
118 * Allocate a contiguous HYP private VA range for the stack
119 * and guard page. The allocation is also aligned based on
120 * the order of its size.
122 ret = pkvm_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr);
127 * Since the stack grows downwards, map the stack to the page
128 * at the higher address and leave the lower guard page
131 * Any valid stack address now has the PAGE_SHIFT bit as 1
132 * and addresses corresponding to the guard page have the
133 * PAGE_SHIFT bit as 0 - this is used for overflow detection.
135 hyp_spin_lock(&pkvm_pgd_lock);
136 ret = kvm_pgtable_hyp_map(&pkvm_pgtable, hyp_addr + PAGE_SIZE,
137 PAGE_SIZE, params->stack_pa, PAGE_HYP);
138 hyp_spin_unlock(&pkvm_pgd_lock);
142 /* Update stack_hyp_va to end of the stack's private VA range */
143 params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE);
147 * Map the host sections RO in the hypervisor, but transfer the
148 * ownership from the host to the hypervisor itself to make sure they
149 * can't be donated or shared with another entity.
151 * The ownership transition requires matching changes in the host
152 * stage-2. This will be done later (see finalize_host_mappings()) once
153 * the hyp_vmemmap is addressable.
155 prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED);
156 ret = pkvm_create_mappings(&kvm_vgic_global_state,
157 &kvm_vgic_global_state + 1, prot);
164 static void update_nvhe_init_params(void)
166 struct kvm_nvhe_init_params *params;
169 for (i = 0; i < hyp_nr_cpus; i++) {
170 params = per_cpu_ptr(&kvm_init_params, i);
171 params->pgd_pa = __hyp_pa(pkvm_pgtable.pgd);
172 dcache_clean_inval_poc((unsigned long)params,
173 (unsigned long)params + sizeof(*params));
177 static void *hyp_zalloc_hyp_page(void *arg)
179 return hyp_alloc_pages(&hpool, 0);
182 static void hpool_get_page(void *addr)
184 hyp_get_page(&hpool, addr);
187 static void hpool_put_page(void *addr)
189 hyp_put_page(&hpool, addr);
192 static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
193 enum kvm_pgtable_walk_flags visit)
195 enum kvm_pgtable_prot prot;
196 enum pkvm_page_state state;
199 if (!kvm_pte_valid(ctx->old))
202 if (ctx->level != (KVM_PGTABLE_MAX_LEVELS - 1))
205 phys = kvm_pte_to_phys(ctx->old);
206 if (!addr_is_memory(phys))
210 * Adjust the host stage-2 mappings to match the ownership attributes
211 * configured in the hypervisor stage-1.
213 state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(ctx->old));
215 case PKVM_PAGE_OWNED:
216 return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
217 case PKVM_PAGE_SHARED_OWNED:
218 prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_BORROWED);
220 case PKVM_PAGE_SHARED_BORROWED:
221 prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_OWNED);
227 return host_stage2_idmap_locked(phys, PAGE_SIZE, prot);
230 static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx,
231 enum kvm_pgtable_walk_flags visit)
234 * Fix-up the refcount for the page-table pages as the early allocator
235 * was unable to access the hyp_vmemmap and so the buddy allocator has
236 * initialised the refcount to '1'.
238 if (kvm_pte_valid(ctx->old))
239 ctx->mm_ops->get_page(ctx->ptep);
244 static int fix_host_ownership(void)
246 struct kvm_pgtable_walker walker = {
247 .cb = fix_host_ownership_walker,
248 .flags = KVM_PGTABLE_WALK_LEAF,
252 for (i = 0; i < hyp_memblock_nr; i++) {
253 struct memblock_region *reg = &hyp_memory[i];
254 u64 start = (u64)hyp_phys_to_virt(reg->base);
256 ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
264 static int fix_hyp_pgtable_refcnt(void)
266 struct kvm_pgtable_walker walker = {
267 .cb = fix_hyp_pgtable_refcnt_walker,
268 .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
269 .arg = pkvm_pgtable.mm_ops,
272 return kvm_pgtable_walk(&pkvm_pgtable, 0, BIT(pkvm_pgtable.ia_bits),
276 void __noreturn __pkvm_init_finalise(void)
278 struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data);
279 struct kvm_cpu_context *host_ctxt = &host_data->host_ctxt;
280 unsigned long nr_pages, reserved_pages, pfn;
283 /* Now that the vmemmap is backed, install the full-fledged allocator */
284 pfn = hyp_virt_to_pfn(hyp_pgt_base);
285 nr_pages = hyp_s1_pgtable_pages();
286 reserved_pages = hyp_early_alloc_nr_used_pages();
287 ret = hyp_pool_init(&hpool, pfn, nr_pages, reserved_pages);
291 ret = kvm_host_prepare_stage2(host_s2_pgt_base);
295 pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) {
296 .zalloc_page = hyp_zalloc_hyp_page,
297 .phys_to_virt = hyp_phys_to_virt,
298 .virt_to_phys = hyp_virt_to_phys,
299 .get_page = hpool_get_page,
300 .put_page = hpool_put_page,
301 .page_count = hyp_page_count,
303 pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;
305 ret = fix_host_ownership();
309 ret = fix_hyp_pgtable_refcnt();
313 ret = hyp_create_pcpu_fixmap();
317 pkvm_hyp_vm_table_init(vm_table_base);
320 * We tail-called to here from handle___pkvm_init() and will not return,
321 * so make sure to propagate the return value to the host.
323 cpu_reg(host_ctxt, 1) = ret;
325 __host_enter(host_ctxt);
328 int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus,
329 unsigned long *per_cpu_base, u32 hyp_va_bits)
331 struct kvm_nvhe_init_params *params;
332 void *virt = hyp_phys_to_virt(phys);
333 void (*fn)(phys_addr_t params_pa, void *finalize_fn_va);
336 BUG_ON(kvm_check_pvm_sysreg_table());
338 if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size))
341 hyp_spin_lock_init(&pkvm_pgd_lock);
342 hyp_nr_cpus = nr_cpus;
344 ret = divide_memory_pool(virt, size);
348 ret = recreate_hyp_mappings(phys, size, per_cpu_base, hyp_va_bits);
352 update_nvhe_init_params();
354 /* Jump in the idmap page to switch to the new page-tables */
355 params = this_cpu_ptr(&kvm_init_params);
356 fn = (typeof(fn))__hyp_pa(__pkvm_init_switch_pgd);
357 fn(__hyp_pa(params), __pkvm_init_finalise);