xref: /linux/arch/arm64/kvm/hyp/nvhe/mem_protect.c (revision 03313efed5e2ca55e862bf514b907a431ebf642a)
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_emulate.h>
9 #include <asm/kvm_hyp.h>
10 #include <asm/kvm_mmu.h>
11 #include <asm/kvm_pgtable.h>
12 #include <asm/kvm_pkvm.h>
13 #include <asm/stage2_pgtable.h>
14 
15 #include <hyp/fault.h>
16 
17 #include <nvhe/gfp.h>
18 #include <nvhe/memory.h>
19 #include <nvhe/mem_protect.h>
20 #include <nvhe/mm.h>
21 #include <nvhe/trap_handler.h>
22 
23 #define KVM_HOST_S2_FLAGS (KVM_PGTABLE_S2_AS_S1 | KVM_PGTABLE_S2_IDMAP)
24 
25 struct host_mmu host_mmu;
26 
27 static struct hyp_pool host_s2_pool;
28 
29 static DEFINE_PER_CPU(struct pkvm_hyp_vm *, __current_vm);
30 #define current_vm (*this_cpu_ptr(&__current_vm))
31 
32 static void guest_lock_component(struct pkvm_hyp_vm *vm)
33 {
34 	hyp_spin_lock(&vm->lock);
35 	current_vm = vm;
36 }
37 
38 static void guest_unlock_component(struct pkvm_hyp_vm *vm)
39 {
40 	current_vm = NULL;
41 	hyp_spin_unlock(&vm->lock);
42 }
43 
44 static void host_lock_component(void)
45 {
46 	hyp_spin_lock(&host_mmu.lock);
47 }
48 
49 static void host_unlock_component(void)
50 {
51 	hyp_spin_unlock(&host_mmu.lock);
52 }
53 
54 static void hyp_lock_component(void)
55 {
56 	hyp_spin_lock(&pkvm_pgd_lock);
57 }
58 
59 static void hyp_unlock_component(void)
60 {
61 	hyp_spin_unlock(&pkvm_pgd_lock);
62 }
63 
64 #define for_each_hyp_page(__p, __st, __sz)				\
65 	for (struct hyp_page *__p = hyp_phys_to_page(__st),		\
66 			     *__e = __p + ((__sz) >> PAGE_SHIFT);	\
67 	     __p < __e; __p++)
68 
69 static void *host_s2_zalloc_pages_exact(size_t size)
70 {
71 	void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size));
72 
73 	hyp_split_page(hyp_virt_to_page(addr));
74 
75 	/*
76 	 * The size of concatenated PGDs is always a power of two of PAGE_SIZE,
77 	 * so there should be no need to free any of the tail pages to make the
78 	 * allocation exact.
79 	 */
80 	WARN_ON(size != (PAGE_SIZE << get_order(size)));
81 
82 	return addr;
83 }
84 
85 static void *host_s2_zalloc_page(void *pool)
86 {
87 	return hyp_alloc_pages(pool, 0);
88 }
89 
90 static void host_s2_get_page(void *addr)
91 {
92 	hyp_get_page(&host_s2_pool, addr);
93 }
94 
95 static void host_s2_put_page(void *addr)
96 {
97 	hyp_put_page(&host_s2_pool, addr);
98 }
99 
100 static void host_s2_free_unlinked_table(void *addr, s8 level)
101 {
102 	kvm_pgtable_stage2_free_unlinked(&host_mmu.mm_ops, addr, level);
103 }
104 
105 static int prepare_s2_pool(void *pgt_pool_base)
106 {
107 	unsigned long nr_pages, pfn;
108 	int ret;
109 
110 	pfn = hyp_virt_to_pfn(pgt_pool_base);
111 	nr_pages = host_s2_pgtable_pages();
112 	ret = hyp_pool_init(&host_s2_pool, pfn, nr_pages, 0);
113 	if (ret)
114 		return ret;
115 
116 	host_mmu.mm_ops = (struct kvm_pgtable_mm_ops) {
117 		.zalloc_pages_exact = host_s2_zalloc_pages_exact,
118 		.zalloc_page = host_s2_zalloc_page,
119 		.free_unlinked_table = host_s2_free_unlinked_table,
120 		.phys_to_virt = hyp_phys_to_virt,
121 		.virt_to_phys = hyp_virt_to_phys,
122 		.page_count = hyp_page_count,
123 		.get_page = host_s2_get_page,
124 		.put_page = host_s2_put_page,
125 	};
126 
127 	return 0;
128 }
129 
130 static void prepare_host_vtcr(void)
131 {
132 	u32 parange, phys_shift;
133 
134 	/* The host stage 2 is id-mapped, so use parange for T0SZ */
135 	parange = kvm_get_parange(id_aa64mmfr0_el1_sys_val);
136 	phys_shift = id_aa64mmfr0_parange_to_phys_shift(parange);
137 
138 	host_mmu.arch.mmu.vtcr = kvm_get_vtcr(id_aa64mmfr0_el1_sys_val,
139 					      id_aa64mmfr1_el1_sys_val, phys_shift);
140 }
141 
142 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot);
143 
144 int kvm_host_prepare_stage2(void *pgt_pool_base)
145 {
146 	struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu;
147 	int ret;
148 
149 	prepare_host_vtcr();
150 	hyp_spin_lock_init(&host_mmu.lock);
151 	mmu->arch = &host_mmu.arch;
152 
153 	ret = prepare_s2_pool(pgt_pool_base);
154 	if (ret)
155 		return ret;
156 
157 	ret = __kvm_pgtable_stage2_init(&host_mmu.pgt, mmu,
158 					&host_mmu.mm_ops, KVM_HOST_S2_FLAGS,
159 					host_stage2_force_pte_cb);
160 	if (ret)
161 		return ret;
162 
163 	mmu->pgd_phys = __hyp_pa(host_mmu.pgt.pgd);
164 	mmu->pgt = &host_mmu.pgt;
165 	atomic64_set(&mmu->vmid.id, 0);
166 
167 	return 0;
168 }
169 
170 static void *guest_s2_zalloc_pages_exact(size_t size)
171 {
172 	void *addr = hyp_alloc_pages(&current_vm->pool, get_order(size));
173 
174 	WARN_ON(size != (PAGE_SIZE << get_order(size)));
175 	hyp_split_page(hyp_virt_to_page(addr));
176 
177 	return addr;
178 }
179 
180 static void guest_s2_free_pages_exact(void *addr, unsigned long size)
181 {
182 	u8 order = get_order(size);
183 	unsigned int i;
184 
185 	for (i = 0; i < (1 << order); i++)
186 		hyp_put_page(&current_vm->pool, addr + (i * PAGE_SIZE));
187 }
188 
189 static void *guest_s2_zalloc_page(void *mc)
190 {
191 	struct hyp_page *p;
192 	void *addr;
193 
194 	addr = hyp_alloc_pages(&current_vm->pool, 0);
195 	if (addr)
196 		return addr;
197 
198 	addr = pop_hyp_memcache(mc, hyp_phys_to_virt);
199 	if (!addr)
200 		return addr;
201 
202 	memset(addr, 0, PAGE_SIZE);
203 	p = hyp_virt_to_page(addr);
204 	p->refcount = 1;
205 	p->order = 0;
206 
207 	return addr;
208 }
209 
210 static void guest_s2_get_page(void *addr)
211 {
212 	hyp_get_page(&current_vm->pool, addr);
213 }
214 
215 static void guest_s2_put_page(void *addr)
216 {
217 	hyp_put_page(&current_vm->pool, addr);
218 }
219 
220 static void __apply_guest_page(void *va, size_t size,
221 			       void (*func)(void *addr, size_t size))
222 {
223 	size += va - PTR_ALIGN_DOWN(va, PAGE_SIZE);
224 	va = PTR_ALIGN_DOWN(va, PAGE_SIZE);
225 	size = PAGE_ALIGN(size);
226 
227 	while (size) {
228 		size_t map_size = PAGE_SIZE;
229 		void *map;
230 
231 		if (IS_ALIGNED((unsigned long)va, PMD_SIZE) && size >= PMD_SIZE)
232 			map = hyp_fixblock_map(__hyp_pa(va), &map_size);
233 		else
234 			map = hyp_fixmap_map(__hyp_pa(va));
235 
236 		func(map, map_size);
237 
238 		if (map_size == PMD_SIZE)
239 			hyp_fixblock_unmap();
240 		else
241 			hyp_fixmap_unmap();
242 
243 		size -= map_size;
244 		va += map_size;
245 	}
246 }
247 
248 static void clean_dcache_guest_page(void *va, size_t size)
249 {
250 	__apply_guest_page(va, size, __clean_dcache_guest_page);
251 }
252 
253 static void invalidate_icache_guest_page(void *va, size_t size)
254 {
255 	__apply_guest_page(va, size, __invalidate_icache_guest_page);
256 }
257 
258 int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)
259 {
260 	struct kvm_s2_mmu *mmu = &vm->kvm.arch.mmu;
261 	unsigned long nr_pages;
262 	int ret;
263 
264 	nr_pages = kvm_pgtable_stage2_pgd_size(mmu->vtcr) >> PAGE_SHIFT;
265 	ret = hyp_pool_init(&vm->pool, hyp_virt_to_pfn(pgd), nr_pages, 0);
266 	if (ret)
267 		return ret;
268 
269 	hyp_spin_lock_init(&vm->lock);
270 	vm->mm_ops = (struct kvm_pgtable_mm_ops) {
271 		.zalloc_pages_exact	= guest_s2_zalloc_pages_exact,
272 		.free_pages_exact	= guest_s2_free_pages_exact,
273 		.zalloc_page		= guest_s2_zalloc_page,
274 		.phys_to_virt		= hyp_phys_to_virt,
275 		.virt_to_phys		= hyp_virt_to_phys,
276 		.page_count		= hyp_page_count,
277 		.get_page		= guest_s2_get_page,
278 		.put_page		= guest_s2_put_page,
279 		.dcache_clean_inval_poc	= clean_dcache_guest_page,
280 		.icache_inval_pou	= invalidate_icache_guest_page,
281 	};
282 
283 	guest_lock_component(vm);
284 	ret = __kvm_pgtable_stage2_init(mmu->pgt, mmu, &vm->mm_ops, 0, NULL);
285 	guest_unlock_component(vm);
286 	if (ret)
287 		return ret;
288 
289 	vm->kvm.arch.mmu.pgd_phys = __hyp_pa(vm->pgt.pgd);
290 
291 	return 0;
292 }
293 
294 void reclaim_pgtable_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)
295 {
296 	struct hyp_page *page;
297 	void *addr;
298 
299 	/* Dump all pgtable pages in the hyp_pool */
300 	guest_lock_component(vm);
301 	kvm_pgtable_stage2_destroy(&vm->pgt);
302 	vm->kvm.arch.mmu.pgd_phys = 0ULL;
303 	guest_unlock_component(vm);
304 
305 	/* Drain the hyp_pool into the memcache */
306 	addr = hyp_alloc_pages(&vm->pool, 0);
307 	while (addr) {
308 		page = hyp_virt_to_page(addr);
309 		page->refcount = 0;
310 		page->order = 0;
311 		push_hyp_memcache(mc, addr, hyp_virt_to_phys);
312 		WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(addr), 1));
313 		addr = hyp_alloc_pages(&vm->pool, 0);
314 	}
315 }
316 
317 int __pkvm_prot_finalize(void)
318 {
319 	struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu;
320 	struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
321 
322 	if (params->hcr_el2 & HCR_VM)
323 		return -EPERM;
324 
325 	params->vttbr = kvm_get_vttbr(mmu);
326 	params->vtcr = mmu->vtcr;
327 	params->hcr_el2 |= HCR_VM;
328 	if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
329 		params->hcr_el2 |= HCR_FWB;
330 
331 	/*
332 	 * The CMO below not only cleans the updated params to the
333 	 * PoC, but also provides the DSB that ensures ongoing
334 	 * page-table walks that have started before we trapped to EL2
335 	 * have completed.
336 	 */
337 	kvm_flush_dcache_to_poc(params, sizeof(*params));
338 
339 	write_sysreg_hcr(params->hcr_el2);
340 	__load_stage2(&host_mmu.arch.mmu, &host_mmu.arch);
341 
342 	/*
343 	 * Make sure to have an ISB before the TLB maintenance below but only
344 	 * when __load_stage2() doesn't include one already.
345 	 */
346 	asm(ALTERNATIVE("isb", "nop", ARM64_WORKAROUND_SPECULATIVE_AT));
347 
348 	/* Invalidate stale HCR bits that may be cached in TLBs */
349 	__tlbi(vmalls12e1);
350 	dsb(nsh);
351 	isb();
352 
353 	return 0;
354 }
355 
356 static int host_stage2_unmap_dev_all(void)
357 {
358 	struct kvm_pgtable *pgt = &host_mmu.pgt;
359 	struct memblock_region *reg;
360 	u64 addr = 0;
361 	int i, ret;
362 
363 	/* Unmap all non-memory regions to recycle the pages */
364 	for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) {
365 		reg = &hyp_memory[i];
366 		ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr);
367 		if (ret)
368 			return ret;
369 	}
370 	return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
371 }
372 
373 /*
374  * Ensure the PFN range is contained within PA-range.
375  *
376  * This check is also robust to overflows and is therefore a requirement before
377  * using a pfn/nr_pages pair from an untrusted source.
378  */
379 static bool pfn_range_is_valid(u64 pfn, u64 nr_pages)
380 {
381 	u64 limit = BIT(kvm_phys_shift(&host_mmu.arch.mmu) - PAGE_SHIFT);
382 
383 	return pfn < limit && ((limit - pfn) >= nr_pages);
384 }
385 
386 struct kvm_mem_range {
387 	u64 start;
388 	u64 end;
389 };
390 
391 static struct memblock_region *find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
392 {
393 	int cur, left = 0, right = hyp_memblock_nr;
394 	struct memblock_region *reg;
395 	phys_addr_t end;
396 
397 	range->start = 0;
398 	range->end = ULONG_MAX;
399 
400 	/* The list of memblock regions is sorted, binary search it */
401 	while (left < right) {
402 		cur = (left + right) >> 1;
403 		reg = &hyp_memory[cur];
404 		end = reg->base + reg->size;
405 		if (addr < reg->base) {
406 			right = cur;
407 			range->end = reg->base;
408 		} else if (addr >= end) {
409 			left = cur + 1;
410 			range->start = end;
411 		} else {
412 			range->start = reg->base;
413 			range->end = end;
414 			return reg;
415 		}
416 	}
417 
418 	return NULL;
419 }
420 
421 bool addr_is_memory(phys_addr_t phys)
422 {
423 	struct kvm_mem_range range;
424 
425 	return !!find_mem_range(phys, &range);
426 }
427 
428 static bool is_in_mem_range(u64 addr, struct kvm_mem_range *range)
429 {
430 	return range->start <= addr && addr < range->end;
431 }
432 
433 static int check_range_allowed_memory(u64 start, u64 end)
434 {
435 	struct memblock_region *reg;
436 	struct kvm_mem_range range;
437 
438 	/*
439 	 * Callers can't check the state of a range that overlaps memory and
440 	 * MMIO regions, so ensure [start, end[ is in the same kvm_mem_range.
441 	 */
442 	reg = find_mem_range(start, &range);
443 	if (!is_in_mem_range(end - 1, &range))
444 		return -EINVAL;
445 
446 	if (!reg || reg->flags & MEMBLOCK_NOMAP)
447 		return -EPERM;
448 
449 	return 0;
450 }
451 
452 static bool range_is_memory(u64 start, u64 end)
453 {
454 	struct kvm_mem_range r;
455 
456 	if (!find_mem_range(start, &r))
457 		return false;
458 
459 	return is_in_mem_range(end - 1, &r);
460 }
461 
462 static inline int __host_stage2_idmap(u64 start, u64 end,
463 				      enum kvm_pgtable_prot prot)
464 {
465 	/*
466 	 * We don't make permission changes to the host idmap after
467 	 * initialisation, so we can squash -EAGAIN to save callers
468 	 * having to treat it like success in the case that they try to
469 	 * map something that is already mapped.
470 	 */
471 	return kvm_pgtable_stage2_map(&host_mmu.pgt, start, end - start, start,
472 				      prot, &host_s2_pool,
473 				      KVM_PGTABLE_WALK_IGNORE_EAGAIN);
474 }
475 
476 /*
477  * The pool has been provided with enough pages to cover all of memory with
478  * page granularity, but it is difficult to know how much of the MMIO range
479  * we will need to cover upfront, so we may need to 'recycle' the pages if we
480  * run out.
481  */
482 #define host_stage2_try(fn, ...)					\
483 	({								\
484 		int __ret;						\
485 		hyp_assert_lock_held(&host_mmu.lock);			\
486 		__ret = fn(__VA_ARGS__);				\
487 		if (__ret == -ENOMEM) {					\
488 			__ret = host_stage2_unmap_dev_all();		\
489 			if (!__ret)					\
490 				__ret = fn(__VA_ARGS__);		\
491 		}							\
492 		__ret;							\
493 	 })
494 
495 static inline bool range_included(struct kvm_mem_range *child,
496 				  struct kvm_mem_range *parent)
497 {
498 	return parent->start <= child->start && child->end <= parent->end;
499 }
500 
501 static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)
502 {
503 	struct kvm_mem_range cur;
504 	kvm_pte_t pte;
505 	u64 granule;
506 	s8 level;
507 	int ret;
508 
509 	hyp_assert_lock_held(&host_mmu.lock);
510 	ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr, &pte, &level);
511 	if (ret)
512 		return ret;
513 
514 	if (kvm_pte_valid(pte))
515 		return -EEXIST;
516 
517 	if (pte) {
518 		WARN_ON(addr_is_memory(addr) &&
519 			get_host_state(hyp_phys_to_page(addr)) != PKVM_NOPAGE);
520 		return -EPERM;
521 	}
522 
523 	for (; level <= KVM_PGTABLE_LAST_LEVEL; level++) {
524 		if (!kvm_level_supports_block_mapping(level))
525 			continue;
526 		granule = kvm_granule_size(level);
527 		cur.start = ALIGN_DOWN(addr, granule);
528 		cur.end = cur.start + granule;
529 		if (!range_included(&cur, range) && level < KVM_PGTABLE_LAST_LEVEL)
530 			continue;
531 		*range = cur;
532 		return 0;
533 	}
534 
535 	WARN_ON(1);
536 
537 	return -EINVAL;
538 }
539 
540 int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
541 			     enum kvm_pgtable_prot prot)
542 {
543 	return host_stage2_try(__host_stage2_idmap, addr, addr + size, prot);
544 }
545 
546 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)
547 {
548 	for_each_hyp_page(page, addr, size)
549 		set_host_state(page, state);
550 }
551 
552 #define KVM_HOST_DONATION_PTE_OWNER_MASK	GENMASK(3, 1)
553 #define KVM_HOST_DONATION_PTE_EXTRA_MASK	GENMASK(59, 4)
554 static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size,
555 						 u8 owner_id, u64 meta)
556 {
557 	kvm_pte_t annotation;
558 	int ret;
559 
560 	if (owner_id == PKVM_ID_HOST)
561 		return -EINVAL;
562 
563 	if (!range_is_memory(addr, addr + size))
564 		return -EPERM;
565 
566 	if (!FIELD_FIT(KVM_HOST_DONATION_PTE_OWNER_MASK, owner_id))
567 		return -EINVAL;
568 
569 	if (!FIELD_FIT(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta))
570 		return -EINVAL;
571 
572 	annotation = FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, owner_id) |
573 		     FIELD_PREP(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta);
574 	ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
575 			      addr, size, &host_s2_pool,
576 			      KVM_HOST_INVALID_PTE_TYPE_DONATION, annotation);
577 	if (!ret)
578 		__host_update_page_state(addr, size, PKVM_NOPAGE);
579 
580 	return ret;
581 }
582 
583 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
584 {
585 	int ret = -EINVAL;
586 
587 	switch (owner_id) {
588 	case PKVM_ID_HOST:
589 		if (!range_is_memory(addr, addr + size))
590 			return -EPERM;
591 
592 		ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT);
593 		if (!ret)
594 			__host_update_page_state(addr, size, PKVM_PAGE_OWNED);
595 		break;
596 	case PKVM_ID_HYP:
597 		ret = host_stage2_set_owner_metadata_locked(addr, size,
598 							    owner_id, 0);
599 		break;
600 	}
601 
602 	return ret;
603 }
604 
605 #define KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK	GENMASK(15, 0)
606 /* We need 40 bits for the GFN to cover a 52-bit IPA with 4k pages and LPA2 */
607 #define KVM_HOST_PTE_OWNER_GUEST_GFN_MASK	GENMASK(55, 16)
608 static u64 host_stage2_encode_gfn_meta(struct pkvm_hyp_vm *vm, u64 gfn)
609 {
610 	pkvm_handle_t handle = vm->kvm.arch.pkvm.handle;
611 
612 	BUILD_BUG_ON((pkvm_handle_t)-1 > KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK);
613 	WARN_ON(!FIELD_FIT(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, gfn));
614 
615 	return FIELD_PREP(KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK, handle) |
616 	       FIELD_PREP(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, gfn);
617 }
618 
619 static int host_stage2_decode_gfn_meta(kvm_pte_t pte, struct pkvm_hyp_vm **vm,
620 				       u64 *gfn)
621 {
622 	pkvm_handle_t handle;
623 	u64 meta;
624 
625 	if (WARN_ON(kvm_pte_valid(pte)))
626 		return -EINVAL;
627 
628 	if (FIELD_GET(KVM_INVALID_PTE_TYPE_MASK, pte) !=
629 	    KVM_HOST_INVALID_PTE_TYPE_DONATION) {
630 		return -EINVAL;
631 	}
632 
633 	if (FIELD_GET(KVM_HOST_DONATION_PTE_OWNER_MASK, pte) != PKVM_ID_GUEST)
634 		return -EPERM;
635 
636 	meta = FIELD_GET(KVM_HOST_DONATION_PTE_EXTRA_MASK, pte);
637 	handle = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK, meta);
638 	*vm = get_vm_by_handle(handle);
639 	if (!*vm) {
640 		/* We probably raced with teardown; try again */
641 		return -EAGAIN;
642 	}
643 
644 	*gfn = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, meta);
645 	return 0;
646 }
647 
648 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot)
649 {
650 	/*
651 	 * Block mappings must be used with care in the host stage-2 as a
652 	 * kvm_pgtable_stage2_map() operation targeting a page in the range of
653 	 * an existing block will delete the block under the assumption that
654 	 * mappings in the rest of the block range can always be rebuilt lazily.
655 	 * That assumption is correct for the host stage-2 with RWX mappings
656 	 * targeting memory or RW mappings targeting MMIO ranges (see
657 	 * host_stage2_idmap() below which implements some of the host memory
658 	 * abort logic). However, this is not safe for any other mappings where
659 	 * the host stage-2 page-table is in fact the only place where this
660 	 * state is stored. In all those cases, it is safer to use page-level
661 	 * mappings, hence avoiding to lose the state because of side-effects in
662 	 * kvm_pgtable_stage2_map().
663 	 */
664 	if (range_is_memory(addr, end))
665 		return prot != PKVM_HOST_MEM_PROT;
666 	else
667 		return prot != PKVM_HOST_MMIO_PROT;
668 }
669 
670 static int host_stage2_idmap(u64 addr)
671 {
672 	struct kvm_mem_range range;
673 	bool is_memory = !!find_mem_range(addr, &range);
674 	enum kvm_pgtable_prot prot;
675 	int ret;
676 
677 	prot = is_memory ? PKVM_HOST_MEM_PROT : PKVM_HOST_MMIO_PROT;
678 
679 	host_lock_component();
680 	ret = host_stage2_adjust_range(addr, &range);
681 	if (ret)
682 		goto unlock;
683 
684 	ret = host_stage2_idmap_locked(range.start, range.end - range.start, prot);
685 unlock:
686 	host_unlock_component();
687 
688 	return ret;
689 }
690 
691 static void host_inject_mem_abort(struct kvm_cpu_context *host_ctxt)
692 {
693 	u64 ec, esr, spsr;
694 
695 	esr = read_sysreg_el2(SYS_ESR);
696 	spsr = read_sysreg_el2(SYS_SPSR);
697 
698 	/* Repaint the ESR to report a same-level fault if taken from EL1 */
699 	if ((spsr & PSR_MODE_MASK) != PSR_MODE_EL0t) {
700 		ec = ESR_ELx_EC(esr);
701 		if (ec == ESR_ELx_EC_DABT_LOW)
702 			ec = ESR_ELx_EC_DABT_CUR;
703 		else if (ec == ESR_ELx_EC_IABT_LOW)
704 			ec = ESR_ELx_EC_IABT_CUR;
705 		else
706 			WARN_ON(1);
707 		esr &= ~ESR_ELx_EC_MASK;
708 		esr |= ec << ESR_ELx_EC_SHIFT;
709 	}
710 
711 	/*
712 	 * Since S1PTW should only ever be set for stage-2 faults, we're pretty
713 	 * much guaranteed that it won't be set in ESR_EL1 by the hardware. So,
714 	 * let's use that bit to allow the host abort handler to differentiate
715 	 * this abort from normal userspace faults.
716 	 *
717 	 * Note: although S1PTW is RES0 at EL1, it is guaranteed by the
718 	 * architecture to be backed by flops, so it should be safe to use.
719 	 */
720 	esr |= ESR_ELx_S1PTW;
721 	inject_host_exception(esr);
722 }
723 
724 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
725 {
726 	struct kvm_vcpu_fault_info fault;
727 	u64 esr, addr;
728 
729 	esr = read_sysreg_el2(SYS_ESR);
730 	if (!__get_fault_info(esr, &fault)) {
731 		/*
732 		 * We've presumably raced with a page-table change which caused
733 		 * AT to fail, try again.
734 		 */
735 		return;
736 	}
737 
738 
739 	/*
740 	 * Yikes, we couldn't resolve the fault IPA. This should reinject an
741 	 * abort into the host when we figure out how to do that.
742 	 */
743 	BUG_ON(!(fault.hpfar_el2 & HPFAR_EL2_NS));
744 	addr = FIELD_GET(HPFAR_EL2_FIPA, fault.hpfar_el2) << 12;
745 
746 	switch (host_stage2_idmap(addr)) {
747 	case -EPERM:
748 		host_inject_mem_abort(host_ctxt);
749 		fallthrough;
750 	case -EEXIST:
751 	case 0:
752 		break;
753 	default:
754 		BUG();
755 	}
756 }
757 
758 struct check_walk_data {
759 	enum pkvm_page_state	desired;
760 	enum pkvm_page_state	(*get_page_state)(kvm_pte_t pte, u64 addr);
761 };
762 
763 static int __check_page_state_visitor(const struct kvm_pgtable_visit_ctx *ctx,
764 				      enum kvm_pgtable_walk_flags visit)
765 {
766 	struct check_walk_data *d = ctx->arg;
767 
768 	return d->get_page_state(ctx->old, ctx->addr) == d->desired ? 0 : -EPERM;
769 }
770 
771 static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,
772 				  struct check_walk_data *data)
773 {
774 	struct kvm_pgtable_walker walker = {
775 		.cb	= __check_page_state_visitor,
776 		.arg	= data,
777 		.flags	= KVM_PGTABLE_WALK_LEAF,
778 	};
779 
780 	return kvm_pgtable_walk(pgt, addr, size, &walker);
781 }
782 
783 static int __host_check_page_state_range(u64 addr, u64 size,
784 					 enum pkvm_page_state state)
785 {
786 	int ret;
787 
788 	ret = check_range_allowed_memory(addr, addr + size);
789 	if (ret)
790 		return ret;
791 
792 	hyp_assert_lock_held(&host_mmu.lock);
793 
794 	for_each_hyp_page(page, addr, size) {
795 		if (get_host_state(page) != state)
796 			return -EPERM;
797 	}
798 
799 	return 0;
800 }
801 
802 static int __host_set_page_state_range(u64 addr, u64 size,
803 				       enum pkvm_page_state state)
804 {
805 	if (get_host_state(hyp_phys_to_page(addr)) == PKVM_NOPAGE) {
806 		int ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT);
807 
808 		if (ret)
809 			return ret;
810 	}
811 
812 	__host_update_page_state(addr, size, state);
813 
814 	return 0;
815 }
816 
817 static void __hyp_set_page_state_range(phys_addr_t phys, u64 size, enum pkvm_page_state state)
818 {
819 	for_each_hyp_page(page, phys, size)
820 		set_hyp_state(page, state);
821 }
822 
823 static int __hyp_check_page_state_range(phys_addr_t phys, u64 size, enum pkvm_page_state state)
824 {
825 	for_each_hyp_page(page, phys, size) {
826 		if (get_hyp_state(page) != state)
827 			return -EPERM;
828 	}
829 
830 	return 0;
831 }
832 
833 static bool guest_pte_is_poisoned(kvm_pte_t pte)
834 {
835 	if (kvm_pte_valid(pte))
836 		return false;
837 
838 	return FIELD_GET(KVM_INVALID_PTE_TYPE_MASK, pte) ==
839 	       KVM_GUEST_INVALID_PTE_TYPE_POISONED;
840 }
841 
842 static enum pkvm_page_state guest_get_page_state(kvm_pte_t pte, u64 addr)
843 {
844 	if (guest_pte_is_poisoned(pte))
845 		return PKVM_POISON;
846 
847 	if (!kvm_pte_valid(pte))
848 		return PKVM_NOPAGE;
849 
850 	return pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte));
851 }
852 
853 static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
854 					  u64 size, enum pkvm_page_state state)
855 {
856 	struct check_walk_data d = {
857 		.desired	= state,
858 		.get_page_state	= guest_get_page_state,
859 	};
860 
861 	hyp_assert_lock_held(&vm->lock);
862 	return check_page_state_range(&vm->pgt, addr, size, &d);
863 }
864 
865 static int get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa, kvm_pte_t *ptep, u64 *physp)
866 {
867 	kvm_pte_t pte;
868 	u64 phys;
869 	s8 level;
870 	int ret;
871 
872 	ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level);
873 	if (ret)
874 		return ret;
875 	if (guest_pte_is_poisoned(pte))
876 		return -EHWPOISON;
877 	if (!kvm_pte_valid(pte))
878 		return -ENOENT;
879 	if (level != KVM_PGTABLE_LAST_LEVEL)
880 		return -E2BIG;
881 
882 	phys = kvm_pte_to_phys(pte);
883 	ret = check_range_allowed_memory(phys, phys + PAGE_SIZE);
884 	if (WARN_ON(ret))
885 		return ret;
886 
887 	*ptep = pte;
888 	*physp = phys;
889 
890 	return 0;
891 }
892 
893 int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu)
894 {
895 	struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
896 	kvm_pte_t pte;
897 	s8 level;
898 	u64 ipa;
899 	int ret;
900 
901 	switch (kvm_vcpu_trap_get_class(&hyp_vcpu->vcpu)) {
902 	case ESR_ELx_EC_DABT_LOW:
903 	case ESR_ELx_EC_IABT_LOW:
904 		if (kvm_vcpu_trap_is_translation_fault(&hyp_vcpu->vcpu))
905 			break;
906 		fallthrough;
907 	default:
908 		return -EINVAL;
909 	}
910 
911 	/*
912 	 * The host has the faulting IPA when it calls us from the guest
913 	 * fault handler but we retrieve it ourselves from the FAR so as
914 	 * to avoid exposing an "oracle" that could reveal data access
915 	 * patterns of the guest after initial donation of its pages.
916 	 */
917 	ipa = kvm_vcpu_get_fault_ipa(&hyp_vcpu->vcpu);
918 	ipa |= FAR_TO_FIPA_OFFSET(kvm_vcpu_get_hfar(&hyp_vcpu->vcpu));
919 
920 	guest_lock_component(vm);
921 	ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level);
922 	if (ret)
923 		goto unlock;
924 
925 	if (level != KVM_PGTABLE_LAST_LEVEL) {
926 		ret = -EINVAL;
927 		goto unlock;
928 	}
929 
930 	ret = guest_pte_is_poisoned(pte);
931 unlock:
932 	guest_unlock_component(vm);
933 	return ret;
934 }
935 
936 int __pkvm_host_share_hyp(u64 pfn)
937 {
938 	u64 phys = hyp_pfn_to_phys(pfn);
939 	u64 size = PAGE_SIZE;
940 	int ret;
941 
942 	host_lock_component();
943 	hyp_lock_component();
944 
945 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED);
946 	if (ret)
947 		goto unlock;
948 	ret = __hyp_check_page_state_range(phys, size, PKVM_NOPAGE);
949 	if (ret)
950 		goto unlock;
951 
952 	__hyp_set_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED);
953 	WARN_ON(__host_set_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED));
954 
955 unlock:
956 	hyp_unlock_component();
957 	host_unlock_component();
958 
959 	return ret;
960 }
961 
962 int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
963 {
964 	struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
965 	u64 phys, ipa = hyp_pfn_to_phys(gfn);
966 	kvm_pte_t pte;
967 	int ret;
968 
969 	host_lock_component();
970 	guest_lock_component(vm);
971 
972 	ret = get_valid_guest_pte(vm, ipa, &pte, &phys);
973 	if (ret)
974 		goto unlock;
975 
976 	ret = -EPERM;
977 	if (pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte)) != PKVM_PAGE_OWNED)
978 		goto unlock;
979 	if (__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE))
980 		goto unlock;
981 
982 	ret = 0;
983 	WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys,
984 				       pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_SHARED_OWNED),
985 				       &vcpu->vcpu.arch.pkvm_memcache, 0));
986 	WARN_ON(__host_set_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED));
987 unlock:
988 	guest_unlock_component(vm);
989 	host_unlock_component();
990 
991 	return ret;
992 }
993 
994 int __pkvm_host_unshare_hyp(u64 pfn)
995 {
996 	u64 phys = hyp_pfn_to_phys(pfn);
997 	u64 virt = (u64)__hyp_va(phys);
998 	u64 size = PAGE_SIZE;
999 	int ret;
1000 
1001 	host_lock_component();
1002 	hyp_lock_component();
1003 
1004 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED);
1005 	if (ret)
1006 		goto unlock;
1007 	ret = __hyp_check_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED);
1008 	if (ret)
1009 		goto unlock;
1010 	if (hyp_page_count((void *)virt)) {
1011 		ret = -EBUSY;
1012 		goto unlock;
1013 	}
1014 
1015 	__hyp_set_page_state_range(phys, size, PKVM_NOPAGE);
1016 	WARN_ON(__host_set_page_state_range(phys, size, PKVM_PAGE_OWNED));
1017 
1018 unlock:
1019 	hyp_unlock_component();
1020 	host_unlock_component();
1021 
1022 	return ret;
1023 }
1024 
1025 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)
1026 {
1027 	u64 phys = hyp_pfn_to_phys(pfn);
1028 	u64 size = PAGE_SIZE * nr_pages;
1029 	void *virt = __hyp_va(phys);
1030 	int ret;
1031 
1032 	if (!pfn_range_is_valid(pfn, nr_pages))
1033 		return -EINVAL;
1034 
1035 	host_lock_component();
1036 	hyp_lock_component();
1037 
1038 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED);
1039 	if (ret)
1040 		goto unlock;
1041 	ret = __hyp_check_page_state_range(phys, size, PKVM_NOPAGE);
1042 	if (ret)
1043 		goto unlock;
1044 
1045 	__hyp_set_page_state_range(phys, size, PKVM_PAGE_OWNED);
1046 	WARN_ON(pkvm_create_mappings_locked(virt, virt + size, PAGE_HYP));
1047 	WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HYP));
1048 
1049 unlock:
1050 	hyp_unlock_component();
1051 	host_unlock_component();
1052 
1053 	return ret;
1054 }
1055 
1056 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)
1057 {
1058 	u64 phys = hyp_pfn_to_phys(pfn);
1059 	u64 size = PAGE_SIZE * nr_pages;
1060 	u64 virt = (u64)__hyp_va(phys);
1061 	int ret;
1062 
1063 	if (!pfn_range_is_valid(pfn, nr_pages))
1064 		return -EINVAL;
1065 
1066 	host_lock_component();
1067 	hyp_lock_component();
1068 
1069 	ret = __hyp_check_page_state_range(phys, size, PKVM_PAGE_OWNED);
1070 	if (ret)
1071 		goto unlock;
1072 	ret = __host_check_page_state_range(phys, size, PKVM_NOPAGE);
1073 	if (ret)
1074 		goto unlock;
1075 
1076 	__hyp_set_page_state_range(phys, size, PKVM_NOPAGE);
1077 	WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, virt, size) != size);
1078 	WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HOST));
1079 
1080 unlock:
1081 	hyp_unlock_component();
1082 	host_unlock_component();
1083 
1084 	return ret;
1085 }
1086 
1087 int hyp_pin_shared_mem(void *from, void *to)
1088 {
1089 	u64 cur, start = ALIGN_DOWN((u64)from, PAGE_SIZE);
1090 	u64 end = PAGE_ALIGN((u64)to);
1091 	u64 phys = __hyp_pa(start);
1092 	u64 size = end - start;
1093 	struct hyp_page *p;
1094 	int ret;
1095 
1096 	host_lock_component();
1097 	hyp_lock_component();
1098 
1099 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED);
1100 	if (ret)
1101 		goto unlock;
1102 
1103 	ret = __hyp_check_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED);
1104 	if (ret)
1105 		goto unlock;
1106 
1107 	for (cur = start; cur < end; cur += PAGE_SIZE) {
1108 		p = hyp_virt_to_page(cur);
1109 		hyp_page_ref_inc(p);
1110 		if (p->refcount == 1)
1111 			WARN_ON(pkvm_create_mappings_locked((void *)cur,
1112 							    (void *)cur + PAGE_SIZE,
1113 							    PAGE_HYP));
1114 	}
1115 
1116 unlock:
1117 	hyp_unlock_component();
1118 	host_unlock_component();
1119 
1120 	return ret;
1121 }
1122 
1123 void hyp_unpin_shared_mem(void *from, void *to)
1124 {
1125 	u64 cur, start = ALIGN_DOWN((u64)from, PAGE_SIZE);
1126 	u64 end = PAGE_ALIGN((u64)to);
1127 	struct hyp_page *p;
1128 
1129 	host_lock_component();
1130 	hyp_lock_component();
1131 
1132 	for (cur = start; cur < end; cur += PAGE_SIZE) {
1133 		p = hyp_virt_to_page(cur);
1134 		if (p->refcount == 1)
1135 			WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, cur, PAGE_SIZE) != PAGE_SIZE);
1136 		hyp_page_ref_dec(p);
1137 	}
1138 
1139 	hyp_unlock_component();
1140 	host_unlock_component();
1141 }
1142 
1143 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)
1144 {
1145 	u64 phys = hyp_pfn_to_phys(pfn);
1146 	u64 size = PAGE_SIZE * nr_pages;
1147 	int ret;
1148 
1149 	if (!pfn_range_is_valid(pfn, nr_pages))
1150 		return -EINVAL;
1151 
1152 	host_lock_component();
1153 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED);
1154 	if (!ret)
1155 		ret = __host_set_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED);
1156 	host_unlock_component();
1157 
1158 	return ret;
1159 }
1160 
1161 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)
1162 {
1163 	u64 phys = hyp_pfn_to_phys(pfn);
1164 	u64 size = PAGE_SIZE * nr_pages;
1165 	int ret;
1166 
1167 	if (!pfn_range_is_valid(pfn, nr_pages))
1168 		return -EINVAL;
1169 
1170 	host_lock_component();
1171 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED);
1172 	if (!ret)
1173 		ret = __host_set_page_state_range(phys, size, PKVM_PAGE_OWNED);
1174 	host_unlock_component();
1175 
1176 	return ret;
1177 }
1178 
1179 static int __guest_check_transition_size(u64 phys, u64 ipa, u64 nr_pages, u64 *size)
1180 {
1181 	size_t block_size;
1182 
1183 	if (nr_pages == 1) {
1184 		*size = PAGE_SIZE;
1185 		return 0;
1186 	}
1187 
1188 	/* We solely support second to last level huge mapping */
1189 	block_size = kvm_granule_size(KVM_PGTABLE_LAST_LEVEL - 1);
1190 
1191 	if (nr_pages != block_size >> PAGE_SHIFT)
1192 		return -EINVAL;
1193 
1194 	if (!IS_ALIGNED(phys | ipa, block_size))
1195 		return -EINVAL;
1196 
1197 	*size = block_size;
1198 	return 0;
1199 }
1200 
1201 static void hyp_poison_page(phys_addr_t phys)
1202 {
1203 	void *addr = hyp_fixmap_map(phys);
1204 
1205 	memset(addr, 0, PAGE_SIZE);
1206 	/*
1207 	 * Prefer kvm_flush_dcache_to_poc() over __clean_dcache_guest_page()
1208 	 * here as the latter may elide the CMO under the assumption that FWB
1209 	 * will be enabled on CPUs that support it. This is incorrect for the
1210 	 * host stage-2 and would otherwise lead to a malicious host potentially
1211 	 * being able to read the contents of newly reclaimed guest pages.
1212 	 */
1213 	kvm_flush_dcache_to_poc(addr, PAGE_SIZE);
1214 	hyp_fixmap_unmap();
1215 }
1216 
1217 static int host_stage2_get_guest_info(phys_addr_t phys, struct pkvm_hyp_vm **vm,
1218 				      u64 *gfn)
1219 {
1220 	enum pkvm_page_state state;
1221 	kvm_pte_t pte;
1222 	s8 level;
1223 	int ret;
1224 
1225 	if (!addr_is_memory(phys))
1226 		return -EFAULT;
1227 
1228 	state = get_host_state(hyp_phys_to_page(phys));
1229 	switch (state) {
1230 	case PKVM_PAGE_OWNED:
1231 	case PKVM_PAGE_SHARED_OWNED:
1232 	case PKVM_PAGE_SHARED_BORROWED:
1233 		/* The access should no longer fault; try again. */
1234 		return -EAGAIN;
1235 	case PKVM_NOPAGE:
1236 		break;
1237 	default:
1238 		return -EPERM;
1239 	}
1240 
1241 	ret = kvm_pgtable_get_leaf(&host_mmu.pgt, phys, &pte, &level);
1242 	if (ret)
1243 		return ret;
1244 
1245 	if (WARN_ON(level != KVM_PGTABLE_LAST_LEVEL))
1246 		return -EINVAL;
1247 
1248 	return host_stage2_decode_gfn_meta(pte, vm, gfn);
1249 }
1250 
1251 int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
1252 {
1253 	struct pkvm_hyp_vm *vm;
1254 	u64 gfn, ipa, pa;
1255 	kvm_pte_t pte;
1256 	int ret;
1257 
1258 	phys &= PAGE_MASK;
1259 
1260 	hyp_spin_lock(&vm_table_lock);
1261 	host_lock_component();
1262 
1263 	ret = host_stage2_get_guest_info(phys, &vm, &gfn);
1264 	if (ret)
1265 		goto unlock_host;
1266 
1267 	ipa = hyp_pfn_to_phys(gfn);
1268 	guest_lock_component(vm);
1269 	ret = get_valid_guest_pte(vm, ipa, &pte, &pa);
1270 	if (ret)
1271 		goto unlock_guest;
1272 
1273 	WARN_ON(pa != phys);
1274 	if (guest_get_page_state(pte, ipa) != PKVM_PAGE_OWNED) {
1275 		ret = -EPERM;
1276 		goto unlock_guest;
1277 	}
1278 
1279 	/* We really shouldn't be allocating, so don't pass a memcache */
1280 	ret = kvm_pgtable_stage2_annotate(&vm->pgt, ipa, PAGE_SIZE, NULL,
1281 					  KVM_GUEST_INVALID_PTE_TYPE_POISONED,
1282 					  0);
1283 	if (ret)
1284 		goto unlock_guest;
1285 
1286 	hyp_poison_page(phys);
1287 	WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
1288 unlock_guest:
1289 	guest_unlock_component(vm);
1290 unlock_host:
1291 	host_unlock_component();
1292 	hyp_spin_unlock(&vm_table_lock);
1293 
1294 	return ret;
1295 }
1296 
1297 int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm)
1298 {
1299 	u64 ipa = hyp_pfn_to_phys(gfn);
1300 	kvm_pte_t pte;
1301 	u64 phys;
1302 	int ret;
1303 
1304 	host_lock_component();
1305 	guest_lock_component(vm);
1306 
1307 	ret = get_valid_guest_pte(vm, ipa, &pte, &phys);
1308 	if (ret)
1309 		goto unlock;
1310 
1311 	switch (guest_get_page_state(pte, ipa)) {
1312 	case PKVM_PAGE_OWNED:
1313 		WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE));
1314 		hyp_poison_page(phys);
1315 		break;
1316 	case PKVM_PAGE_SHARED_OWNED:
1317 		WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED));
1318 		break;
1319 	default:
1320 		ret = -EPERM;
1321 		goto unlock;
1322 	}
1323 
1324 	WARN_ON(kvm_pgtable_stage2_unmap(&vm->pgt, ipa, PAGE_SIZE));
1325 	WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
1326 
1327 unlock:
1328 	guest_unlock_component(vm);
1329 	host_unlock_component();
1330 
1331 	/*
1332 	 * -EHWPOISON implies that the page was forcefully reclaimed already
1333 	 * so return success for the GUP pin to be dropped.
1334 	 */
1335 	return ret && ret != -EHWPOISON ? ret : 0;
1336 }
1337 
1338 int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu)
1339 {
1340 	struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
1341 	u64 phys = hyp_pfn_to_phys(pfn);
1342 	u64 ipa = hyp_pfn_to_phys(gfn);
1343 	u64 meta;
1344 	int ret;
1345 
1346 	host_lock_component();
1347 	guest_lock_component(vm);
1348 
1349 	ret = __host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_OWNED);
1350 	if (ret)
1351 		goto unlock;
1352 
1353 	ret = __guest_check_page_state_range(vm, ipa, PAGE_SIZE, PKVM_NOPAGE);
1354 	if (ret)
1355 		goto unlock;
1356 
1357 	meta = host_stage2_encode_gfn_meta(vm, gfn);
1358 	WARN_ON(host_stage2_set_owner_metadata_locked(phys, PAGE_SIZE,
1359 						      PKVM_ID_GUEST, meta));
1360 	WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys,
1361 				       pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_OWNED),
1362 				       &vcpu->vcpu.arch.pkvm_memcache, 0));
1363 
1364 unlock:
1365 	guest_unlock_component(vm);
1366 	host_unlock_component();
1367 
1368 	return ret;
1369 }
1370 
1371 int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu,
1372 			    enum kvm_pgtable_prot prot)
1373 {
1374 	struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
1375 	u64 phys = hyp_pfn_to_phys(pfn);
1376 	u64 ipa = hyp_pfn_to_phys(gfn);
1377 	u64 size;
1378 	int ret;
1379 
1380 	if (prot & ~KVM_PGTABLE_PROT_RWX)
1381 		return -EINVAL;
1382 
1383 	if (!pfn_range_is_valid(pfn, nr_pages))
1384 		return -EINVAL;
1385 
1386 	ret = __guest_check_transition_size(phys, ipa, nr_pages, &size);
1387 	if (ret)
1388 		return ret;
1389 
1390 	ret = check_range_allowed_memory(phys, phys + size);
1391 	if (ret)
1392 		return ret;
1393 
1394 	host_lock_component();
1395 	guest_lock_component(vm);
1396 
1397 	ret = __guest_check_page_state_range(vm, ipa, size, PKVM_NOPAGE);
1398 	if (ret)
1399 		goto unlock;
1400 
1401 	for_each_hyp_page(page, phys, size) {
1402 		switch (get_host_state(page)) {
1403 		case PKVM_PAGE_OWNED:
1404 			continue;
1405 		case PKVM_PAGE_SHARED_OWNED:
1406 			if (page->host_share_guest_count == U32_MAX) {
1407 				ret = -EBUSY;
1408 				goto unlock;
1409 			}
1410 
1411 			/* Only host to np-guest multi-sharing is tolerated */
1412 			if (page->host_share_guest_count)
1413 				continue;
1414 
1415 			fallthrough;
1416 		default:
1417 			ret = -EPERM;
1418 			goto unlock;
1419 		}
1420 	}
1421 
1422 	for_each_hyp_page(page, phys, size) {
1423 		set_host_state(page, PKVM_PAGE_SHARED_OWNED);
1424 		page->host_share_guest_count++;
1425 	}
1426 
1427 	WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys,
1428 				       pkvm_mkstate(prot, PKVM_PAGE_SHARED_BORROWED),
1429 				       &vcpu->vcpu.arch.pkvm_memcache, 0));
1430 
1431 unlock:
1432 	guest_unlock_component(vm);
1433 	host_unlock_component();
1434 
1435 	return ret;
1436 }
1437 
1438 static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ipa, u64 size)
1439 {
1440 	enum pkvm_page_state state;
1441 	kvm_pte_t pte;
1442 	u64 phys;
1443 	s8 level;
1444 	int ret;
1445 
1446 	ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level);
1447 	if (ret)
1448 		return ret;
1449 	if (!kvm_pte_valid(pte))
1450 		return -ENOENT;
1451 	if (size && kvm_granule_size(level) != size)
1452 		return -E2BIG;
1453 
1454 	if (!size)
1455 		size = kvm_granule_size(level);
1456 
1457 	state = guest_get_page_state(pte, ipa);
1458 	if (state != PKVM_PAGE_SHARED_BORROWED)
1459 		return -EPERM;
1460 
1461 	phys = kvm_pte_to_phys(pte);
1462 	ret = check_range_allowed_memory(phys, phys + size);
1463 	if (WARN_ON(ret))
1464 		return ret;
1465 
1466 	for_each_hyp_page(page, phys, size) {
1467 		if (get_host_state(page) != PKVM_PAGE_SHARED_OWNED)
1468 			return -EPERM;
1469 		if (WARN_ON(!page->host_share_guest_count))
1470 			return -EINVAL;
1471 	}
1472 
1473 	*__phys = phys;
1474 
1475 	return 0;
1476 }
1477 
1478 int __pkvm_host_unshare_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
1479 {
1480 	u64 ipa = hyp_pfn_to_phys(gfn);
1481 	u64 size, phys;
1482 	int ret;
1483 
1484 	ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
1485 	if (ret)
1486 		return ret;
1487 
1488 	host_lock_component();
1489 	guest_lock_component(vm);
1490 
1491 	ret = __check_host_shared_guest(vm, &phys, ipa, size);
1492 	if (ret)
1493 		goto unlock;
1494 
1495 	ret = kvm_pgtable_stage2_unmap(&vm->pgt, ipa, size);
1496 	if (ret)
1497 		goto unlock;
1498 
1499 	for_each_hyp_page(page, phys, size) {
1500 		/* __check_host_shared_guest() protects against underflow */
1501 		page->host_share_guest_count--;
1502 		if (!page->host_share_guest_count)
1503 			set_host_state(page, PKVM_PAGE_OWNED);
1504 	}
1505 
1506 unlock:
1507 	guest_unlock_component(vm);
1508 	host_unlock_component();
1509 
1510 	return ret;
1511 }
1512 
1513 static void assert_host_shared_guest(struct pkvm_hyp_vm *vm, u64 ipa, u64 size)
1514 {
1515 	u64 phys;
1516 	int ret;
1517 
1518 	if (!IS_ENABLED(CONFIG_NVHE_EL2_DEBUG))
1519 		return;
1520 
1521 	host_lock_component();
1522 	guest_lock_component(vm);
1523 
1524 	ret = __check_host_shared_guest(vm, &phys, ipa, size);
1525 
1526 	guest_unlock_component(vm);
1527 	host_unlock_component();
1528 
1529 	WARN_ON(ret && ret != -ENOENT);
1530 }
1531 
1532 int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kvm_pgtable_prot prot)
1533 {
1534 	struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
1535 	u64 ipa = hyp_pfn_to_phys(gfn);
1536 	int ret;
1537 
1538 	if (pkvm_hyp_vm_is_protected(vm))
1539 		return -EPERM;
1540 
1541 	if (prot & ~KVM_PGTABLE_PROT_RWX)
1542 		return -EINVAL;
1543 
1544 	assert_host_shared_guest(vm, ipa, 0);
1545 	guest_lock_component(vm);
1546 	ret = kvm_pgtable_stage2_relax_perms(&vm->pgt, ipa, prot, 0);
1547 	guest_unlock_component(vm);
1548 
1549 	return ret;
1550 }
1551 
1552 int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
1553 {
1554 	u64 size, ipa = hyp_pfn_to_phys(gfn);
1555 	int ret;
1556 
1557 	if (pkvm_hyp_vm_is_protected(vm))
1558 		return -EPERM;
1559 
1560 	ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
1561 	if (ret)
1562 		return ret;
1563 
1564 	assert_host_shared_guest(vm, ipa, size);
1565 	guest_lock_component(vm);
1566 	ret = kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, size);
1567 	guest_unlock_component(vm);
1568 
1569 	return ret;
1570 }
1571 
1572 int __pkvm_host_test_clear_young_guest(u64 gfn, u64 nr_pages, bool mkold, struct pkvm_hyp_vm *vm)
1573 {
1574 	u64 size, ipa = hyp_pfn_to_phys(gfn);
1575 	int ret;
1576 
1577 	if (pkvm_hyp_vm_is_protected(vm))
1578 		return -EPERM;
1579 
1580 	ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
1581 	if (ret)
1582 		return ret;
1583 
1584 	assert_host_shared_guest(vm, ipa, size);
1585 	guest_lock_component(vm);
1586 	ret = kvm_pgtable_stage2_test_clear_young(&vm->pgt, ipa, size, mkold);
1587 	guest_unlock_component(vm);
1588 
1589 	return ret;
1590 }
1591 
1592 int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu)
1593 {
1594 	struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
1595 	u64 ipa = hyp_pfn_to_phys(gfn);
1596 
1597 	if (pkvm_hyp_vm_is_protected(vm))
1598 		return -EPERM;
1599 
1600 	assert_host_shared_guest(vm, ipa, 0);
1601 	guest_lock_component(vm);
1602 	kvm_pgtable_stage2_mkyoung(&vm->pgt, ipa, 0);
1603 	guest_unlock_component(vm);
1604 
1605 	return 0;
1606 }
1607 
1608 #ifdef CONFIG_NVHE_EL2_DEBUG
1609 struct pkvm_expected_state {
1610 	enum pkvm_page_state host;
1611 	enum pkvm_page_state hyp;
1612 	enum pkvm_page_state guest[2]; /* [ gfn, gfn + 1 ] */
1613 };
1614 
1615 static struct pkvm_expected_state selftest_state;
1616 static struct hyp_page *selftest_page;
1617 
1618 static struct pkvm_hyp_vm selftest_vm = {
1619 	.kvm = {
1620 		.arch = {
1621 			.mmu = {
1622 				.arch = &selftest_vm.kvm.arch,
1623 				.pgt = &selftest_vm.pgt,
1624 			},
1625 		},
1626 	},
1627 };
1628 
1629 static struct pkvm_hyp_vcpu selftest_vcpu = {
1630 	.vcpu = {
1631 		.arch = {
1632 			.hw_mmu = &selftest_vm.kvm.arch.mmu,
1633 		},
1634 		.kvm = &selftest_vm.kvm,
1635 	},
1636 };
1637 
1638 static void init_selftest_vm(void *virt)
1639 {
1640 	struct hyp_page *p = hyp_virt_to_page(virt);
1641 	int i;
1642 
1643 	selftest_vm.kvm.arch.mmu.vtcr = host_mmu.arch.mmu.vtcr;
1644 	WARN_ON(kvm_guest_prepare_stage2(&selftest_vm, virt));
1645 
1646 	for (i = 0; i < pkvm_selftest_pages(); i++) {
1647 		if (p[i].refcount)
1648 			continue;
1649 		p[i].refcount = 1;
1650 		hyp_put_page(&selftest_vm.pool, hyp_page_to_virt(&p[i]));
1651 	}
1652 }
1653 
1654 static u64 selftest_ipa(void)
1655 {
1656 	return BIT(selftest_vm.pgt.ia_bits - 1);
1657 }
1658 
1659 static void assert_page_state(void)
1660 {
1661 	void *virt = hyp_page_to_virt(selftest_page);
1662 	u64 size = PAGE_SIZE << selftest_page->order;
1663 	struct pkvm_hyp_vcpu *vcpu = &selftest_vcpu;
1664 	u64 phys = hyp_virt_to_phys(virt);
1665 	u64 ipa[2] = { selftest_ipa(), selftest_ipa() + PAGE_SIZE };
1666 	struct pkvm_hyp_vm *vm;
1667 
1668 	vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
1669 
1670 	host_lock_component();
1671 	WARN_ON(__host_check_page_state_range(phys, size, selftest_state.host));
1672 	host_unlock_component();
1673 
1674 	hyp_lock_component();
1675 	WARN_ON(__hyp_check_page_state_range(phys, size, selftest_state.hyp));
1676 	hyp_unlock_component();
1677 
1678 	guest_lock_component(&selftest_vm);
1679 	WARN_ON(__guest_check_page_state_range(vm, ipa[0], size, selftest_state.guest[0]));
1680 	WARN_ON(__guest_check_page_state_range(vm, ipa[1], size, selftest_state.guest[1]));
1681 	guest_unlock_component(&selftest_vm);
1682 }
1683 
1684 #define assert_transition_res(res, fn, ...)		\
1685 	do {						\
1686 		WARN_ON(fn(__VA_ARGS__) != res);	\
1687 		assert_page_state();			\
1688 	} while (0)
1689 
1690 void pkvm_ownership_selftest(void *base)
1691 {
1692 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX;
1693 	void *virt = hyp_alloc_pages(&host_s2_pool, 0);
1694 	struct pkvm_hyp_vcpu *vcpu = &selftest_vcpu;
1695 	struct pkvm_hyp_vm *vm = &selftest_vm;
1696 	u64 phys, size, pfn, gfn;
1697 
1698 	WARN_ON(!virt);
1699 	selftest_page = hyp_virt_to_page(virt);
1700 	selftest_page->refcount = 0;
1701 	init_selftest_vm(base);
1702 
1703 	size = PAGE_SIZE << selftest_page->order;
1704 	phys = hyp_virt_to_phys(virt);
1705 	pfn = hyp_phys_to_pfn(phys);
1706 	gfn = hyp_phys_to_pfn(selftest_ipa());
1707 
1708 	selftest_state.host = PKVM_NOPAGE;
1709 	selftest_state.hyp = PKVM_PAGE_OWNED;
1710 	selftest_state.guest[0] = selftest_state.guest[1] = PKVM_NOPAGE;
1711 	assert_page_state();
1712 	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
1713 	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
1714 	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
1715 	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
1716 	assert_transition_res(-EPERM,	__pkvm_host_unshare_ffa, pfn, 1);
1717 	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
1718 	assert_transition_res(-EPERM,	__pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
1719 	assert_transition_res(-ENOENT,	__pkvm_host_unshare_guest, gfn, 1, vm);
1720 
1721 	selftest_state.host = PKVM_PAGE_OWNED;
1722 	selftest_state.hyp = PKVM_NOPAGE;
1723 	assert_transition_res(0,	__pkvm_hyp_donate_host, pfn, 1);
1724 	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
1725 	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
1726 	assert_transition_res(-EPERM,	__pkvm_host_unshare_ffa, pfn, 1);
1727 	assert_transition_res(-ENOENT,	__pkvm_host_unshare_guest, gfn, 1, vm);
1728 	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
1729 
1730 	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
1731 	selftest_state.hyp = PKVM_PAGE_SHARED_BORROWED;
1732 	assert_transition_res(0,	__pkvm_host_share_hyp, pfn);
1733 	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
1734 	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
1735 	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
1736 	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
1737 	assert_transition_res(-EPERM,	__pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
1738 	assert_transition_res(-ENOENT,	__pkvm_host_unshare_guest, gfn, 1, vm);
1739 
1740 	assert_transition_res(0,	hyp_pin_shared_mem, virt, virt + size);
1741 	assert_transition_res(0,	hyp_pin_shared_mem, virt, virt + size);
1742 	hyp_unpin_shared_mem(virt, virt + size);
1743 	WARN_ON(hyp_page_count(virt) != 1);
1744 	assert_transition_res(-EBUSY,	__pkvm_host_unshare_hyp, pfn);
1745 	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
1746 	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
1747 	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
1748 	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
1749 	assert_transition_res(-EPERM,	__pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
1750 	assert_transition_res(-ENOENT,	__pkvm_host_unshare_guest, gfn, 1, vm);
1751 
1752 	hyp_unpin_shared_mem(virt, virt + size);
1753 	assert_page_state();
1754 	WARN_ON(hyp_page_count(virt));
1755 
1756 	selftest_state.host = PKVM_PAGE_OWNED;
1757 	selftest_state.hyp = PKVM_NOPAGE;
1758 	assert_transition_res(0,	__pkvm_host_unshare_hyp, pfn);
1759 
1760 	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
1761 	selftest_state.hyp = PKVM_NOPAGE;
1762 	assert_transition_res(0,	__pkvm_host_share_ffa, pfn, 1);
1763 	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
1764 	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
1765 	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
1766 	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
1767 	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
1768 	assert_transition_res(-EPERM,	__pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
1769 	assert_transition_res(-ENOENT,	__pkvm_host_unshare_guest, gfn, 1, vm);
1770 	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
1771 
1772 	selftest_state.host = PKVM_PAGE_OWNED;
1773 	selftest_state.hyp = PKVM_NOPAGE;
1774 	assert_transition_res(0,	__pkvm_host_unshare_ffa, pfn, 1);
1775 	assert_transition_res(-EPERM,	__pkvm_host_unshare_ffa, pfn, 1);
1776 
1777 	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
1778 	selftest_state.guest[0] = PKVM_PAGE_SHARED_BORROWED;
1779 	assert_transition_res(0,	__pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
1780 	assert_transition_res(-EPERM,	__pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
1781 	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
1782 	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
1783 	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
1784 	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
1785 	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
1786 	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
1787 
1788 	selftest_state.guest[1] = PKVM_PAGE_SHARED_BORROWED;
1789 	assert_transition_res(0,	__pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
1790 	WARN_ON(hyp_virt_to_page(virt)->host_share_guest_count != 2);
1791 
1792 	selftest_state.guest[0] = PKVM_NOPAGE;
1793 	assert_transition_res(0,	__pkvm_host_unshare_guest, gfn, 1, vm);
1794 
1795 	selftest_state.guest[1] = PKVM_NOPAGE;
1796 	selftest_state.host = PKVM_PAGE_OWNED;
1797 	assert_transition_res(0,	__pkvm_host_unshare_guest, gfn + 1, 1, vm);
1798 
1799 	selftest_state.host = PKVM_NOPAGE;
1800 	selftest_state.hyp = PKVM_PAGE_OWNED;
1801 	assert_transition_res(0,	__pkvm_host_donate_hyp, pfn, 1);
1802 
1803 	selftest_page->refcount = 1;
1804 	hyp_put_page(&host_s2_pool, virt);
1805 }
1806 #endif
1807