xref: /linux/arch/arm64/kvm/hyp/nvhe/mm.c (revision fa1219ec8c03d09b7a62046ea9680c0cf141bc4a)
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 
320 	return (void *)slot->addr;
321 }
322 
323 void *hyp_fixmap_map(phys_addr_t phys)
324 {
325 	return fixmap_map_slot(this_cpu_ptr(&fixmap_slots), phys) + offset_in_page(phys);
326 }
327 
328 static void fixmap_clear_slot(struct hyp_fixmap_slot *slot)
329 {
330 	kvm_pte_t *ptep = slot->ptep;
331 	u64 addr = slot->addr;
332 	u32 level;
333 
334 	if (FIELD_GET(KVM_PTE_TYPE, *ptep) == KVM_PTE_TYPE_PAGE)
335 		level = KVM_PGTABLE_LAST_LEVEL;
336 	else
337 		level = KVM_PGTABLE_LAST_LEVEL - 1; /* create_fixblock() guarantees PMD level */
338 
339 	WRITE_ONCE(*ptep, *ptep & ~KVM_PTE_VALID);
340 
341 	/*
342 	 * Irritatingly, the architecture requires that we use inner-shareable
343 	 * broadcast TLB invalidation here in case another CPU speculates
344 	 * through our fixmap and decides to create an "amalagamation of the
345 	 * values held in the TLB" due to the apparent lack of a
346 	 * break-before-make sequence.
347 	 *
348 	 * https://lore.kernel.org/kvm/20221017115209.2099-1-will@kernel.org/T/#mf10dfbaf1eaef9274c581b81c53758918c1d0f03
349 	 */
350 	dsb(ishst);
351 	__tlbi_level(vale2is, addr, level);
352 	__tlbi_sync_s1ish_hyp();
353 	isb();
354 }
355 
356 void hyp_fixmap_unmap(void)
357 {
358 	fixmap_clear_slot(this_cpu_ptr(&fixmap_slots));
359 }
360 
361 static int __create_fixmap_slot_cb(const struct kvm_pgtable_visit_ctx *ctx,
362 				   enum kvm_pgtable_walk_flags visit)
363 {
364 	struct hyp_fixmap_slot *slot = (struct hyp_fixmap_slot *)ctx->arg;
365 
366 	if (!kvm_pte_valid(ctx->old) || (ctx->end - ctx->start) != kvm_granule_size(ctx->level))
367 		return -EINVAL;
368 
369 	slot->addr = ctx->addr;
370 	slot->ptep = ctx->ptep;
371 
372 	/*
373 	 * Clear the PTE, but keep the page-table page refcount elevated to
374 	 * prevent it from ever being freed. This lets us manipulate the PTEs
375 	 * by hand safely without ever needing to allocate memory.
376 	 */
377 	fixmap_clear_slot(slot);
378 
379 	return 0;
380 }
381 
382 static int create_fixmap_slot(u64 addr, u64 cpu)
383 {
384 	struct kvm_pgtable_walker walker = {
385 		.cb	= __create_fixmap_slot_cb,
386 		.flags	= KVM_PGTABLE_WALK_LEAF,
387 		.arg	= per_cpu_ptr(&fixmap_slots, cpu),
388 	};
389 
390 	return kvm_pgtable_walk(&pkvm_pgtable, addr, PAGE_SIZE, &walker);
391 }
392 
393 #if PAGE_SHIFT < 16
394 #define HAS_FIXBLOCK
395 static struct hyp_fixmap_slot hyp_fixblock_slot;
396 static DEFINE_HYP_SPINLOCK(hyp_fixblock_lock);
397 #endif
398 
399 static int create_fixblock(void)
400 {
401 #ifdef HAS_FIXBLOCK
402 	struct kvm_pgtable_walker walker = {
403 		.cb	= __create_fixmap_slot_cb,
404 		.flags	= KVM_PGTABLE_WALK_LEAF,
405 		.arg	= &hyp_fixblock_slot,
406 	};
407 	unsigned long addr;
408 	phys_addr_t phys;
409 	int ret, i;
410 
411 	/* Find a RAM phys address, PMD aligned */
412 	for (i = 0; i < hyp_memblock_nr; i++) {
413 		phys = ALIGN(hyp_memory[i].base, PMD_SIZE);
414 		if (phys + PMD_SIZE < (hyp_memory[i].base + hyp_memory[i].size))
415 			break;
416 	}
417 
418 	if (i >= hyp_memblock_nr)
419 		return -EINVAL;
420 
421 	hyp_spin_lock(&pkvm_pgd_lock);
422 	addr = ALIGN(__io_map_next, PMD_SIZE);
423 	ret = __pkvm_alloc_private_va_range(addr, PMD_SIZE);
424 	if (ret)
425 		goto unlock;
426 
427 	ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr, PMD_SIZE, phys, PAGE_HYP);
428 	if (ret)
429 		goto unlock;
430 
431 	ret = kvm_pgtable_walk(&pkvm_pgtable, addr, PMD_SIZE, &walker);
432 
433 unlock:
434 	hyp_spin_unlock(&pkvm_pgd_lock);
435 
436 	return ret;
437 #else
438 	return 0;
439 #endif
440 }
441 
442 void *hyp_fixblock_map(phys_addr_t phys, size_t *size)
443 {
444 #ifdef HAS_FIXBLOCK
445 	*size = PMD_SIZE;
446 	hyp_spin_lock(&hyp_fixblock_lock);
447 	return fixmap_map_slot(&hyp_fixblock_slot, phys) + offset_in_page(phys);
448 #else
449 	*size = PAGE_SIZE;
450 	return hyp_fixmap_map(phys);
451 #endif
452 }
453 
454 void hyp_fixblock_unmap(void)
455 {
456 #ifdef HAS_FIXBLOCK
457 	fixmap_clear_slot(&hyp_fixblock_slot);
458 	hyp_spin_unlock(&hyp_fixblock_lock);
459 #else
460 	hyp_fixmap_unmap();
461 #endif
462 }
463 
464 int hyp_create_fixmap(void)
465 {
466 	unsigned long addr, i;
467 	int ret;
468 
469 	for (i = 0; i < hyp_nr_cpus; i++) {
470 		ret = pkvm_alloc_private_va_range(PAGE_SIZE, &addr);
471 		if (ret)
472 			return ret;
473 
474 		ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr, PAGE_SIZE,
475 					  __hyp_pa(__hyp_bss_start), PAGE_HYP);
476 		if (ret)
477 			return ret;
478 
479 		ret = create_fixmap_slot(addr, i);
480 		if (ret)
481 			return ret;
482 	}
483 
484 	return create_fixblock();
485 }
486 
487 int hyp_create_idmap(u32 hyp_va_bits)
488 {
489 	unsigned long start, end;
490 
491 	start = hyp_virt_to_phys((void *)__hyp_idmap_text_start);
492 	start = ALIGN_DOWN(start, PAGE_SIZE);
493 
494 	end = hyp_virt_to_phys((void *)__hyp_idmap_text_end);
495 	end = ALIGN(end, PAGE_SIZE);
496 
497 	/*
498 	 * One half of the VA space is reserved to linearly map portions of
499 	 * memory -- see va_layout.c for more details. The other half of the VA
500 	 * space contains the trampoline page, and needs some care. Split that
501 	 * second half in two and find the quarter of VA space not conflicting
502 	 * with the idmap to place the IOs and the vmemmap. IOs use the lower
503 	 * half of the quarter and the vmemmap the upper half.
504 	 */
505 	__io_map_base = start & BIT(hyp_va_bits - 2);
506 	__io_map_base ^= BIT(hyp_va_bits - 2);
507 	__io_map_next = __io_map_base;
508 	__hyp_vmemmap = __io_map_base | BIT(hyp_va_bits - 3);
509 
510 	return __pkvm_create_mappings(start, end - start, start, PAGE_HYP_EXEC);
511 }
512 
513 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr)
514 {
515 	unsigned long addr, prev_next;
516 	size_t size;
517 	int ret;
518 
519 	hyp_spin_lock(&pkvm_pgd_lock);
520 
521 	prev_next = __io_map_next;
522 	/*
523 	 * Efficient stack verification using the NVHE_STACK_SHIFT bit implies
524 	 * an alignment of our allocation on the order of the size.
525 	 */
526 	size = NVHE_STACK_SIZE * 2;
527 	addr = ALIGN(__io_map_next, size);
528 
529 	ret = __pkvm_alloc_private_va_range(addr, size);
530 	if (!ret) {
531 		/*
532 		 * Since the stack grows downwards, map the stack to the page
533 		 * at the higher address and leave the lower guard page
534 		 * unbacked.
535 		 *
536 		 * Any valid stack address now has the NVHE_STACK_SHIFT bit as 1
537 		 * and addresses corresponding to the guard page have the
538 		 * NVHE_STACK_SHIFT bit as 0 - this is used for overflow detection.
539 		 */
540 		ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr + NVHE_STACK_SIZE,
541 					  NVHE_STACK_SIZE, phys, PAGE_HYP);
542 		if (ret)
543 			__io_map_next = prev_next;
544 	}
545 	hyp_spin_unlock(&pkvm_pgd_lock);
546 
547 	*haddr = addr + size;
548 
549 	return ret;
550 }
551 
552 static int check_page_ownership(phys_addr_t phys)
553 {
554 	kvm_pte_t pte;
555 	bool host_ok;
556 	int ret;
557 
558 	if (addr_is_memory(phys)) {
559 		struct hyp_page *page = hyp_phys_to_page(phys);
560 
561 		if (get_hyp_state(page) != PKVM_PAGE_OWNED ||
562 		    get_host_state(page) != PKVM_NOPAGE)
563 			return -EPERM;
564 	}
565 
566 	ret = kvm_pgtable_get_leaf(&host_mmu.pgt, phys, &pte, NULL, 0);
567 	if (ret)
568 		return ret;
569 
570 	/* Hyp text may stay host-readable, see fix_host_ownership_walker(). */
571 	if (kvm_pte_valid(pte) && addr_is_hyp_text(phys))
572 		host_ok = !(kvm_pgtable_stage2_pte_prot(pte) & KVM_PGTABLE_PROT_W);
573 	else
574 		host_ok = host_stage2_pte_is_hyp_owned(pte);
575 
576 	return host_ok ? 0 : -EPERM;
577 }
578 
579 static int check_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
580 				       enum kvm_pgtable_walk_flags visit)
581 {
582 	phys_addr_t phys, end;
583 	int ret;
584 
585 	if (!kvm_pte_valid(ctx->old))
586 		return 0;
587 
588 	phys = kvm_pte_to_phys(ctx->old);
589 	end = phys + kvm_granule_size(ctx->level);
590 	for (; phys < end; phys += PAGE_SIZE) {
591 		ret = check_page_ownership(phys);
592 		if (ret)
593 			return ret;
594 	}
595 
596 	return 0;
597 }
598 
599 int pkvm_check_host_ownership(void)
600 {
601 	struct kvm_pgtable_walker walker = {
602 		.cb	= check_host_ownership_walker,
603 		.flags	= KVM_PGTABLE_WALK_LEAF,
604 	};
605 
606 	/* The private range and the vmemmap share one quarter of the VA space. */
607 	return kvm_pgtable_walk(&pkvm_pgtable, __io_map_base,
608 				BIT(pkvm_pgtable.ia_bits - 2), &walker);
609 }
610 
611 static void *admit_host_page(void *arg)
612 {
613 	struct kvm_hyp_memcache *host_mc = arg;
614 
615 	if (!host_mc->nr_pages)
616 		return NULL;
617 
618 	/*
619 	 * The host still owns the pages in its memcache, so we need to go
620 	 * through a full host-to-hyp donation cycle to change it. Fortunately,
621 	 * __pkvm_host_donate_hyp() takes care of races for us, so if it
622 	 * succeeds we're good to go.
623 	 */
624 	if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(host_mc->head), 1))
625 		return NULL;
626 
627 	return pop_hyp_memcache(host_mc, hyp_phys_to_virt);
628 }
629 
630 /* Refill our local memcache by popping pages from the one provided by the host. */
631 int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
632 		    struct kvm_hyp_memcache *host_mc)
633 {
634 	struct kvm_hyp_memcache tmp = *host_mc;
635 	int ret;
636 
637 	ret =  __topup_hyp_memcache(mc, min_pages, admit_host_page,
638 				    hyp_virt_to_phys, &tmp);
639 	*host_mc = tmp;
640 
641 	return ret;
642 }
643