xref: /linux/arch/arm64/kvm/hyp/nvhe/mm.c (revision 0e24f9db2ebedc271add0c5dbf2821b8f8a0ab06)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Google LLC
4  * Author: Quentin Perret <qperret@google.com>
5  */
6 
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>
12 #include <asm/spectre.h>
13 
14 #include <nvhe/early_alloc.h>
15 #include <nvhe/gfp.h>
16 #include <nvhe/memory.h>
17 #include <nvhe/mem_protect.h>
18 #include <nvhe/mm.h>
19 #include <nvhe/spinlock.h>
20 
21 struct kvm_pgtable pkvm_pgtable;
22 hyp_spinlock_t pkvm_pgd_lock;
23 
24 struct memblock_region hyp_memory[HYP_MEMBLOCK_REGIONS];
25 unsigned int hyp_memblock_nr;
26 
27 static u64 __io_map_base;
28 static u64 __io_map_next;
29 
30 struct hyp_fixmap_slot {
31 	u64 addr;
32 	kvm_pte_t *ptep;
33 };
34 static DEFINE_PER_CPU(struct hyp_fixmap_slot, fixmap_slots);
35 
36 static int __pkvm_create_mappings(unsigned long start, unsigned long size,
37 				  unsigned long phys, enum kvm_pgtable_prot prot)
38 {
39 	int err;
40 
41 	hyp_spin_lock(&pkvm_pgd_lock);
42 	err = kvm_pgtable_hyp_map(&pkvm_pgtable, start, size, phys, prot);
43 	hyp_spin_unlock(&pkvm_pgd_lock);
44 
45 	return err;
46 }
47 
48 static int __pkvm_alloc_private_va_range(unsigned long start, size_t size)
49 {
50 	unsigned long cur;
51 
52 	hyp_assert_lock_held(&pkvm_pgd_lock);
53 
54 	if (!start || start < __io_map_next)
55 		return -EINVAL;
56 
57 	/* The allocated size is always a multiple of PAGE_SIZE */
58 	cur = start + PAGE_ALIGN(size);
59 
60 	/* Are we overflowing on the vmemmap ? */
61 	if (cur > __hyp_vmemmap)
62 		return -ENOMEM;
63 
64 	__io_map_next = cur;
65 
66 	return 0;
67 }
68 
69 /**
70  * pkvm_alloc_private_va_range - Allocates a private VA range.
71  * @size:	The size of the VA range to reserve.
72  * @haddr:	The hypervisor virtual start address of the allocation.
73  *
74  * The private virtual address (VA) range is allocated above __io_map_next
75  * and aligned based on the order of @size.
76  *
77  * Return: 0 on success or negative error code on failure.
78  */
79 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr)
80 {
81 	unsigned long addr;
82 	int ret;
83 
84 	hyp_spin_lock(&pkvm_pgd_lock);
85 	addr = __io_map_next;
86 	ret = __pkvm_alloc_private_va_range(addr, size);
87 	hyp_spin_unlock(&pkvm_pgd_lock);
88 
89 	*haddr = addr;
90 
91 	return ret;
92 }
93 
94 /**
95  * pkvm_map_private_va_range() - Map a physical range into the private VA range
96  * @haddr:	The virtual address in the private range.
97  * @phys:	The physical address to map.
98  * @size:	The size of the range to map.
99  *
100  * The hypervisor VA @haddr must have been first allocated with
101  * pkvm_alloc_private_va_range(). The created mapping can later be removed
102  * with pkvm_remove_mappings().
103  *
104  * Return: 0 on success, negative error code on failure.
105  */
106 int pkvm_map_private_va_range(void *haddr, phys_addr_t phys, size_t size)
107 {
108 	unsigned long addr = (unsigned long)haddr;
109 	int ret;
110 
111 	if (!PAGE_ALIGNED(addr | phys | size))
112 		return -EINVAL;
113 
114 	guard(hyp_spinlock)(&pkvm_pgd_lock);
115 
116 	if (addr < __io_map_base || addr >= __io_map_next)
117 		return -EINVAL;
118 
119 	if (size > __io_map_next - addr)
120 		return -EINVAL;
121 
122 	ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr, size, phys, PAGE_HYP);
123 	if (ret)
124 		kvm_pgtable_hyp_unmap(&pkvm_pgtable, addr, size);
125 
126 	return ret;
127 }
128 
129 /**
130  * pkvm_private_va_range_pa - Translate a private VA to a physical address.
131  * @va:		The private virtual address to translate.
132  *
133  * Return: The physical address corresponding to @va.
134  */
135 phys_addr_t pkvm_private_va_range_pa(void *va)
136 {
137 	kvm_pte_t pte;
138 	s8 level;
139 
140 	guard(hyp_spinlock)(&pkvm_pgd_lock);
141 	WARN_ON(kvm_pgtable_get_leaf(&pkvm_pgtable, (u64)va, &pte, &level, 0));
142 	WARN_ON(!kvm_pte_valid(pte));
143 
144 	return kvm_pte_to_phys(pte) + ((u64)va & (kvm_granule_size(level) - 1));
145 }
146 
147 int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
148 				  enum kvm_pgtable_prot prot,
149 				  unsigned long *haddr)
150 {
151 	unsigned long addr;
152 	int err;
153 
154 	size = PAGE_ALIGN(size + offset_in_page(phys));
155 	err = pkvm_alloc_private_va_range(size, &addr);
156 	if (err)
157 		return err;
158 
159 	err = __pkvm_create_mappings(addr, size, phys, prot);
160 	if (err)
161 		return err;
162 
163 	*haddr = addr + offset_in_page(phys);
164 	return err;
165 }
166 
167 int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_prot prot)
168 {
169 	unsigned long start = (unsigned long)from;
170 	unsigned long end = (unsigned long)to;
171 	unsigned long virt_addr;
172 	phys_addr_t phys;
173 
174 	hyp_assert_lock_held(&pkvm_pgd_lock);
175 
176 	start = start & PAGE_MASK;
177 	end = PAGE_ALIGN(end);
178 
179 	for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
180 		int err;
181 
182 		phys = hyp_virt_to_phys((void *)virt_addr);
183 		err = kvm_pgtable_hyp_map(&pkvm_pgtable, virt_addr, PAGE_SIZE,
184 					  phys, prot);
185 		if (err)
186 			return err;
187 	}
188 
189 	return 0;
190 }
191 
192 int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
193 {
194 	int ret;
195 
196 	hyp_spin_lock(&pkvm_pgd_lock);
197 	ret = pkvm_create_mappings_locked(from, to, prot);
198 	hyp_spin_unlock(&pkvm_pgd_lock);
199 
200 	return ret;
201 }
202 
203 /**
204  * pkvm_remove_mappings - Remove mappings from the hypervisor page-table
205  * @from:	The starting virtual address of the range to remove
206  * @to:		The ending virtual address of the range to remove
207  *
208  * The range [@from, @to) can be a subset of the range previously mapped by
209  * pkvm_create_mappings(), as the latter enforces PTE-level mappings. However,
210  * it cannot be a subset of a range mapped by pkvm_map_private_va_range() as
211  * that function allows block mappings which the hypervisor page-table does not
212  * split.
213  */
214 void pkvm_remove_mappings(void *from, void *to)
215 {
216 	u64 size;
217 
218 	WARN_ON(from > to);
219 	to = PTR_ALIGN(to, PAGE_SIZE);
220 	from = PTR_ALIGN_DOWN(from, PAGE_SIZE);
221 	size = (u64)to - (u64)from;
222 
223 	guard(hyp_spinlock)(&pkvm_pgd_lock);
224 	WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, (u64)from, size) != size);
225 }
226 
227 int hyp_back_vmemmap(phys_addr_t back)
228 {
229 	unsigned long i, start, size, end = 0;
230 	int ret;
231 
232 	for (i = 0; i < hyp_memblock_nr; i++) {
233 		start = hyp_memory[i].base;
234 		start = ALIGN_DOWN((u64)hyp_phys_to_page(start), PAGE_SIZE);
235 		/*
236 		 * The beginning of the hyp_vmemmap region for the current
237 		 * memblock may already be backed by the page backing the end
238 		 * the previous region, so avoid mapping it twice.
239 		 */
240 		start = max(start, end);
241 
242 		end = hyp_memory[i].base + hyp_memory[i].size;
243 		end = PAGE_ALIGN((u64)hyp_phys_to_page(end));
244 		if (start >= end)
245 			continue;
246 
247 		size = end - start;
248 		ret = __pkvm_create_mappings(start, size, back, PAGE_HYP);
249 		if (ret)
250 			return ret;
251 
252 		memset(hyp_phys_to_virt(back), 0, size);
253 		back += size;
254 	}
255 
256 	return 0;
257 }
258 
259 static void *__hyp_bp_vect_base;
260 int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot)
261 {
262 	void *vector;
263 
264 	switch (slot) {
265 	case HYP_VECTOR_DIRECT: {
266 		vector = __kvm_hyp_vector;
267 		break;
268 	}
269 	case HYP_VECTOR_SPECTRE_DIRECT: {
270 		vector = __bp_harden_hyp_vecs;
271 		break;
272 	}
273 	case HYP_VECTOR_INDIRECT:
274 	case HYP_VECTOR_SPECTRE_INDIRECT: {
275 		vector = (void *)__hyp_bp_vect_base;
276 		break;
277 	}
278 	default:
279 		return -EINVAL;
280 	}
281 
282 	vector = __kvm_vector_slot2addr(vector, slot);
283 	*this_cpu_ptr(&kvm_hyp_vector) = (unsigned long)vector;
284 
285 	return 0;
286 }
287 
288 int hyp_map_vectors(void)
289 {
290 	phys_addr_t phys;
291 	unsigned long bp_base;
292 	int ret;
293 
294 	if (!kvm_system_needs_idmapped_vectors()) {
295 		__hyp_bp_vect_base = __bp_harden_hyp_vecs;
296 		return 0;
297 	}
298 
299 	phys = __hyp_pa(__bp_harden_hyp_vecs);
300 	ret = __pkvm_create_private_mapping(phys, __BP_HARDEN_HYP_VECS_SZ,
301 					    PAGE_HYP_EXEC, &bp_base);
302 	if (ret)
303 		return ret;
304 
305 	__hyp_bp_vect_base = (void *)bp_base;
306 
307 	return 0;
308 }
309 
310 static void *fixmap_map_slot(struct hyp_fixmap_slot *slot, phys_addr_t phys)
311 {
312 	kvm_pte_t pte, *ptep = slot->ptep;
313 
314 	pte = *ptep;
315 	pte &= ~kvm_phys_to_pte(KVM_PHYS_INVALID);
316 	pte |= kvm_phys_to_pte(phys) | KVM_PTE_VALID;
317 	WRITE_ONCE(*ptep, pte);
318 	dsb(ishst);
319 	isb();
320 
321 	return (void *)slot->addr;
322 }
323 
324 void *hyp_fixmap_map(phys_addr_t phys)
325 {
326 	return fixmap_map_slot(this_cpu_ptr(&fixmap_slots), phys) + offset_in_page(phys);
327 }
328 
329 static void fixmap_clear_slot(struct hyp_fixmap_slot *slot)
330 {
331 	kvm_pte_t *ptep = slot->ptep;
332 	u64 addr = slot->addr;
333 	u32 level;
334 
335 	if (FIELD_GET(KVM_PTE_TYPE, *ptep) == KVM_PTE_TYPE_PAGE)
336 		level = KVM_PGTABLE_LAST_LEVEL;
337 	else
338 		level = KVM_PGTABLE_LAST_LEVEL - 1; /* create_fixblock() guarantees PMD level */
339 
340 	WRITE_ONCE(*ptep, *ptep & ~KVM_PTE_VALID);
341 
342 	/*
343 	 * Irritatingly, the architecture requires that we use inner-shareable
344 	 * broadcast TLB invalidation here in case another CPU speculates
345 	 * through our fixmap and decides to create an "amalagamation of the
346 	 * values held in the TLB" due to the apparent lack of a
347 	 * break-before-make sequence.
348 	 *
349 	 * https://lore.kernel.org/kvm/20221017115209.2099-1-will@kernel.org/T/#mf10dfbaf1eaef9274c581b81c53758918c1d0f03
350 	 */
351 	dsb(ishst);
352 	__tlbi_level(vale2is, addr, level);
353 	__tlbi_sync_s1ish_hyp();
354 	isb();
355 }
356 
357 void hyp_fixmap_unmap(void)
358 {
359 	fixmap_clear_slot(this_cpu_ptr(&fixmap_slots));
360 }
361 
362 static int __create_fixmap_slot_cb(const struct kvm_pgtable_visit_ctx *ctx,
363 				   enum kvm_pgtable_walk_flags visit)
364 {
365 	struct hyp_fixmap_slot *slot = (struct hyp_fixmap_slot *)ctx->arg;
366 
367 	if (!kvm_pte_valid(ctx->old) || (ctx->end - ctx->start) != kvm_granule_size(ctx->level))
368 		return -EINVAL;
369 
370 	slot->addr = ctx->addr;
371 	slot->ptep = ctx->ptep;
372 
373 	/*
374 	 * Clear the PTE, but keep the page-table page refcount elevated to
375 	 * prevent it from ever being freed. This lets us manipulate the PTEs
376 	 * by hand safely without ever needing to allocate memory.
377 	 */
378 	fixmap_clear_slot(slot);
379 
380 	return 0;
381 }
382 
383 static int create_fixmap_slot(u64 addr, u64 cpu)
384 {
385 	struct kvm_pgtable_walker walker = {
386 		.cb	= __create_fixmap_slot_cb,
387 		.flags	= KVM_PGTABLE_WALK_LEAF,
388 		.arg	= per_cpu_ptr(&fixmap_slots, cpu),
389 	};
390 
391 	return kvm_pgtable_walk(&pkvm_pgtable, addr, PAGE_SIZE, &walker);
392 }
393 
394 #if PAGE_SHIFT < 16
395 #define HAS_FIXBLOCK
396 static struct hyp_fixmap_slot hyp_fixblock_slot;
397 static DEFINE_HYP_SPINLOCK(hyp_fixblock_lock);
398 #endif
399 
400 static int create_fixblock(void)
401 {
402 #ifdef HAS_FIXBLOCK
403 	struct kvm_pgtable_walker walker = {
404 		.cb	= __create_fixmap_slot_cb,
405 		.flags	= KVM_PGTABLE_WALK_LEAF,
406 		.arg	= &hyp_fixblock_slot,
407 	};
408 	unsigned long addr;
409 	phys_addr_t phys;
410 	int ret, i;
411 
412 	/* Find a RAM phys address, PMD aligned */
413 	for (i = 0; i < hyp_memblock_nr; i++) {
414 		phys = ALIGN(hyp_memory[i].base, PMD_SIZE);
415 		if (phys + PMD_SIZE < (hyp_memory[i].base + hyp_memory[i].size))
416 			break;
417 	}
418 
419 	if (i >= hyp_memblock_nr)
420 		return -EINVAL;
421 
422 	hyp_spin_lock(&pkvm_pgd_lock);
423 	addr = ALIGN(__io_map_next, PMD_SIZE);
424 	ret = __pkvm_alloc_private_va_range(addr, PMD_SIZE);
425 	if (ret)
426 		goto unlock;
427 
428 	ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr, PMD_SIZE, phys, PAGE_HYP);
429 	if (ret)
430 		goto unlock;
431 
432 	ret = kvm_pgtable_walk(&pkvm_pgtable, addr, PMD_SIZE, &walker);
433 
434 unlock:
435 	hyp_spin_unlock(&pkvm_pgd_lock);
436 
437 	return ret;
438 #else
439 	return 0;
440 #endif
441 }
442 
443 void *hyp_fixblock_map(phys_addr_t phys, size_t *size)
444 {
445 #ifdef HAS_FIXBLOCK
446 	*size = PMD_SIZE;
447 	hyp_spin_lock(&hyp_fixblock_lock);
448 	return fixmap_map_slot(&hyp_fixblock_slot, phys) + offset_in_page(phys);
449 #else
450 	*size = PAGE_SIZE;
451 	return hyp_fixmap_map(phys);
452 #endif
453 }
454 
455 void hyp_fixblock_unmap(void)
456 {
457 #ifdef HAS_FIXBLOCK
458 	fixmap_clear_slot(&hyp_fixblock_slot);
459 	hyp_spin_unlock(&hyp_fixblock_lock);
460 #else
461 	hyp_fixmap_unmap();
462 #endif
463 }
464 
465 int hyp_create_fixmap(void)
466 {
467 	unsigned long addr, i;
468 	int ret;
469 
470 	for (i = 0; i < hyp_nr_cpus; i++) {
471 		ret = pkvm_alloc_private_va_range(PAGE_SIZE, &addr);
472 		if (ret)
473 			return ret;
474 
475 		ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr, PAGE_SIZE,
476 					  __hyp_pa(__hyp_bss_start), PAGE_HYP);
477 		if (ret)
478 			return ret;
479 
480 		ret = create_fixmap_slot(addr, i);
481 		if (ret)
482 			return ret;
483 	}
484 
485 	return create_fixblock();
486 }
487 
488 int hyp_create_idmap(u32 hyp_va_bits)
489 {
490 	unsigned long start, end;
491 
492 	start = hyp_virt_to_phys((void *)__hyp_idmap_text_start);
493 	start = ALIGN_DOWN(start, PAGE_SIZE);
494 
495 	end = hyp_virt_to_phys((void *)__hyp_idmap_text_end);
496 	end = ALIGN(end, PAGE_SIZE);
497 
498 	/*
499 	 * One half of the VA space is reserved to linearly map portions of
500 	 * memory -- see va_layout.c for more details. The other half of the VA
501 	 * space contains the trampoline page, and needs some care. Split that
502 	 * second half in two and find the quarter of VA space not conflicting
503 	 * with the idmap to place the IOs and the vmemmap. IOs use the lower
504 	 * half of the quarter and the vmemmap the upper half.
505 	 */
506 	__io_map_base = start & BIT(hyp_va_bits - 2);
507 	__io_map_base ^= BIT(hyp_va_bits - 2);
508 	__io_map_next = __io_map_base;
509 	__hyp_vmemmap = __io_map_base | BIT(hyp_va_bits - 3);
510 
511 	return __pkvm_create_mappings(start, end - start, start, PAGE_HYP_EXEC);
512 }
513 
514 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr)
515 {
516 	unsigned long addr, prev_next;
517 	size_t size;
518 	int ret;
519 
520 	hyp_spin_lock(&pkvm_pgd_lock);
521 
522 	prev_next = __io_map_next;
523 	/*
524 	 * Efficient stack verification using the NVHE_STACK_SHIFT bit implies
525 	 * an alignment of our allocation on the order of the size.
526 	 */
527 	size = NVHE_STACK_SIZE * 2;
528 	addr = ALIGN(__io_map_next, size);
529 
530 	ret = __pkvm_alloc_private_va_range(addr, size);
531 	if (!ret) {
532 		/*
533 		 * Since the stack grows downwards, map the stack to the page
534 		 * at the higher address and leave the lower guard page
535 		 * unbacked.
536 		 *
537 		 * Any valid stack address now has the NVHE_STACK_SHIFT bit as 1
538 		 * and addresses corresponding to the guard page have the
539 		 * NVHE_STACK_SHIFT bit as 0 - this is used for overflow detection.
540 		 */
541 		ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr + NVHE_STACK_SIZE,
542 					  NVHE_STACK_SIZE, phys, PAGE_HYP);
543 		if (ret)
544 			__io_map_next = prev_next;
545 	}
546 	hyp_spin_unlock(&pkvm_pgd_lock);
547 
548 	*haddr = addr + size;
549 
550 	return ret;
551 }
552 
553 static int check_page_ownership(phys_addr_t phys)
554 {
555 	kvm_pte_t pte;
556 	bool host_ok;
557 	int ret;
558 
559 	if (addr_is_memory(phys)) {
560 		struct hyp_page *page = hyp_phys_to_page(phys);
561 
562 		if (get_hyp_state(page) != PKVM_PAGE_OWNED ||
563 		    get_host_state(page) != PKVM_NOPAGE)
564 			return -EPERM;
565 	}
566 
567 	ret = kvm_pgtable_get_leaf(&host_mmu.pgt, phys, &pte, NULL, 0);
568 	if (ret)
569 		return ret;
570 
571 	/* Hyp text may stay host-readable, see fix_host_ownership_walker(). */
572 	if (kvm_pte_valid(pte) && addr_is_hyp_text(phys))
573 		host_ok = !(kvm_pgtable_stage2_pte_prot(pte) & KVM_PGTABLE_PROT_W);
574 	else
575 		host_ok = host_stage2_pte_is_hyp_owned(pte);
576 
577 	return host_ok ? 0 : -EPERM;
578 }
579 
580 static int check_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
581 				       enum kvm_pgtable_walk_flags visit)
582 {
583 	phys_addr_t phys, end;
584 	int ret;
585 
586 	if (!kvm_pte_valid(ctx->old))
587 		return 0;
588 
589 	phys = kvm_pte_to_phys(ctx->old);
590 	end = phys + kvm_granule_size(ctx->level);
591 	for (; phys < end; phys += PAGE_SIZE) {
592 		ret = check_page_ownership(phys);
593 		if (ret)
594 			return ret;
595 	}
596 
597 	return 0;
598 }
599 
600 int pkvm_check_host_ownership(void)
601 {
602 	struct kvm_pgtable_walker walker = {
603 		.cb	= check_host_ownership_walker,
604 		.flags	= KVM_PGTABLE_WALK_LEAF,
605 	};
606 
607 	/* The private range and the vmemmap share one quarter of the VA space. */
608 	return kvm_pgtable_walk(&pkvm_pgtable, __io_map_base,
609 				BIT(pkvm_pgtable.ia_bits - 2), &walker);
610 }
611 
612 static void *admit_host_page(void *arg)
613 {
614 	struct kvm_hyp_memcache *host_mc = arg;
615 
616 	if (!host_mc->nr_pages)
617 		return NULL;
618 
619 	/*
620 	 * The host still owns the pages in its memcache, so we need to go
621 	 * through a full host-to-hyp donation cycle to change it. Fortunately,
622 	 * __pkvm_host_donate_hyp() takes care of races for us, so if it
623 	 * succeeds we're good to go.
624 	 */
625 	if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(host_mc->head), 1))
626 		return NULL;
627 
628 	return pop_hyp_memcache(host_mc, hyp_phys_to_virt);
629 }
630 
631 /* Refill our local memcache by popping pages from the one provided by the host. */
632 int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
633 		    struct kvm_hyp_memcache *host_mc)
634 {
635 	struct kvm_hyp_memcache tmp = *host_mc;
636 	int ret;
637 
638 	ret =  __topup_hyp_memcache(mc, min_pages, admit_host_page,
639 				    hyp_virt_to_phys, &tmp);
640 	*host_mc = tmp;
641 
642 	return ret;
643 }
644