xref: /linux/arch/arm64/kvm/hyp/nvhe/mem_protect.c (revision a634dda26186cf9a51567020fcce52bcba5e1e59)
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 
22 #define KVM_HOST_S2_FLAGS (KVM_PGTABLE_S2_NOFWB | KVM_PGTABLE_S2_IDMAP)
23 
24 struct host_mmu host_mmu;
25 
26 static struct hyp_pool host_s2_pool;
27 
28 static DEFINE_PER_CPU(struct pkvm_hyp_vm *, __current_vm);
29 #define current_vm (*this_cpu_ptr(&__current_vm))
30 
31 static void guest_lock_component(struct pkvm_hyp_vm *vm)
32 {
33 	hyp_spin_lock(&vm->lock);
34 	current_vm = vm;
35 }
36 
37 static void guest_unlock_component(struct pkvm_hyp_vm *vm)
38 {
39 	current_vm = NULL;
40 	hyp_spin_unlock(&vm->lock);
41 }
42 
43 static void host_lock_component(void)
44 {
45 	hyp_spin_lock(&host_mmu.lock);
46 }
47 
48 static void host_unlock_component(void)
49 {
50 	hyp_spin_unlock(&host_mmu.lock);
51 }
52 
53 static void hyp_lock_component(void)
54 {
55 	hyp_spin_lock(&pkvm_pgd_lock);
56 }
57 
58 static void hyp_unlock_component(void)
59 {
60 	hyp_spin_unlock(&pkvm_pgd_lock);
61 }
62 
63 static void *host_s2_zalloc_pages_exact(size_t size)
64 {
65 	void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size));
66 
67 	hyp_split_page(hyp_virt_to_page(addr));
68 
69 	/*
70 	 * The size of concatenated PGDs is always a power of two of PAGE_SIZE,
71 	 * so there should be no need to free any of the tail pages to make the
72 	 * allocation exact.
73 	 */
74 	WARN_ON(size != (PAGE_SIZE << get_order(size)));
75 
76 	return addr;
77 }
78 
79 static void *host_s2_zalloc_page(void *pool)
80 {
81 	return hyp_alloc_pages(pool, 0);
82 }
83 
84 static void host_s2_get_page(void *addr)
85 {
86 	hyp_get_page(&host_s2_pool, addr);
87 }
88 
89 static void host_s2_put_page(void *addr)
90 {
91 	hyp_put_page(&host_s2_pool, addr);
92 }
93 
94 static void host_s2_free_unlinked_table(void *addr, s8 level)
95 {
96 	kvm_pgtable_stage2_free_unlinked(&host_mmu.mm_ops, addr, level);
97 }
98 
99 static int prepare_s2_pool(void *pgt_pool_base)
100 {
101 	unsigned long nr_pages, pfn;
102 	int ret;
103 
104 	pfn = hyp_virt_to_pfn(pgt_pool_base);
105 	nr_pages = host_s2_pgtable_pages();
106 	ret = hyp_pool_init(&host_s2_pool, pfn, nr_pages, 0);
107 	if (ret)
108 		return ret;
109 
110 	host_mmu.mm_ops = (struct kvm_pgtable_mm_ops) {
111 		.zalloc_pages_exact = host_s2_zalloc_pages_exact,
112 		.zalloc_page = host_s2_zalloc_page,
113 		.free_unlinked_table = host_s2_free_unlinked_table,
114 		.phys_to_virt = hyp_phys_to_virt,
115 		.virt_to_phys = hyp_virt_to_phys,
116 		.page_count = hyp_page_count,
117 		.get_page = host_s2_get_page,
118 		.put_page = host_s2_put_page,
119 	};
120 
121 	return 0;
122 }
123 
124 static void prepare_host_vtcr(void)
125 {
126 	u32 parange, phys_shift;
127 
128 	/* The host stage 2 is id-mapped, so use parange for T0SZ */
129 	parange = kvm_get_parange(id_aa64mmfr0_el1_sys_val);
130 	phys_shift = id_aa64mmfr0_parange_to_phys_shift(parange);
131 
132 	host_mmu.arch.mmu.vtcr = kvm_get_vtcr(id_aa64mmfr0_el1_sys_val,
133 					      id_aa64mmfr1_el1_sys_val, phys_shift);
134 }
135 
136 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot);
137 
138 int kvm_host_prepare_stage2(void *pgt_pool_base)
139 {
140 	struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu;
141 	int ret;
142 
143 	prepare_host_vtcr();
144 	hyp_spin_lock_init(&host_mmu.lock);
145 	mmu->arch = &host_mmu.arch;
146 
147 	ret = prepare_s2_pool(pgt_pool_base);
148 	if (ret)
149 		return ret;
150 
151 	ret = __kvm_pgtable_stage2_init(&host_mmu.pgt, mmu,
152 					&host_mmu.mm_ops, KVM_HOST_S2_FLAGS,
153 					host_stage2_force_pte_cb);
154 	if (ret)
155 		return ret;
156 
157 	mmu->pgd_phys = __hyp_pa(host_mmu.pgt.pgd);
158 	mmu->pgt = &host_mmu.pgt;
159 	atomic64_set(&mmu->vmid.id, 0);
160 
161 	return 0;
162 }
163 
164 static bool guest_stage2_force_pte_cb(u64 addr, u64 end,
165 				      enum kvm_pgtable_prot prot)
166 {
167 	return true;
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 	memset(p, 0, sizeof(*p));
205 	p->refcount = 1;
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 clean_dcache_guest_page(void *va, size_t size)
221 {
222 	__clean_dcache_guest_page(hyp_fixmap_map(__hyp_pa(va)), size);
223 	hyp_fixmap_unmap();
224 }
225 
226 static void invalidate_icache_guest_page(void *va, size_t size)
227 {
228 	__invalidate_icache_guest_page(hyp_fixmap_map(__hyp_pa(va)), size);
229 	hyp_fixmap_unmap();
230 }
231 
232 int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)
233 {
234 	struct kvm_s2_mmu *mmu = &vm->kvm.arch.mmu;
235 	unsigned long nr_pages;
236 	int ret;
237 
238 	nr_pages = kvm_pgtable_stage2_pgd_size(mmu->vtcr) >> PAGE_SHIFT;
239 	ret = hyp_pool_init(&vm->pool, hyp_virt_to_pfn(pgd), nr_pages, 0);
240 	if (ret)
241 		return ret;
242 
243 	hyp_spin_lock_init(&vm->lock);
244 	vm->mm_ops = (struct kvm_pgtable_mm_ops) {
245 		.zalloc_pages_exact	= guest_s2_zalloc_pages_exact,
246 		.free_pages_exact	= guest_s2_free_pages_exact,
247 		.zalloc_page		= guest_s2_zalloc_page,
248 		.phys_to_virt		= hyp_phys_to_virt,
249 		.virt_to_phys		= hyp_virt_to_phys,
250 		.page_count		= hyp_page_count,
251 		.get_page		= guest_s2_get_page,
252 		.put_page		= guest_s2_put_page,
253 		.dcache_clean_inval_poc	= clean_dcache_guest_page,
254 		.icache_inval_pou	= invalidate_icache_guest_page,
255 	};
256 
257 	guest_lock_component(vm);
258 	ret = __kvm_pgtable_stage2_init(mmu->pgt, mmu, &vm->mm_ops, 0,
259 					guest_stage2_force_pte_cb);
260 	guest_unlock_component(vm);
261 	if (ret)
262 		return ret;
263 
264 	vm->kvm.arch.mmu.pgd_phys = __hyp_pa(vm->pgt.pgd);
265 
266 	return 0;
267 }
268 
269 void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)
270 {
271 	void *addr;
272 
273 	/* Dump all pgtable pages in the hyp_pool */
274 	guest_lock_component(vm);
275 	kvm_pgtable_stage2_destroy(&vm->pgt);
276 	vm->kvm.arch.mmu.pgd_phys = 0ULL;
277 	guest_unlock_component(vm);
278 
279 	/* Drain the hyp_pool into the memcache */
280 	addr = hyp_alloc_pages(&vm->pool, 0);
281 	while (addr) {
282 		memset(hyp_virt_to_page(addr), 0, sizeof(struct hyp_page));
283 		push_hyp_memcache(mc, addr, hyp_virt_to_phys);
284 		WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(addr), 1));
285 		addr = hyp_alloc_pages(&vm->pool, 0);
286 	}
287 }
288 
289 int __pkvm_prot_finalize(void)
290 {
291 	struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu;
292 	struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
293 
294 	if (params->hcr_el2 & HCR_VM)
295 		return -EPERM;
296 
297 	params->vttbr = kvm_get_vttbr(mmu);
298 	params->vtcr = mmu->vtcr;
299 	params->hcr_el2 |= HCR_VM;
300 
301 	/*
302 	 * The CMO below not only cleans the updated params to the
303 	 * PoC, but also provides the DSB that ensures ongoing
304 	 * page-table walks that have started before we trapped to EL2
305 	 * have completed.
306 	 */
307 	kvm_flush_dcache_to_poc(params, sizeof(*params));
308 
309 	write_sysreg(params->hcr_el2, hcr_el2);
310 	__load_stage2(&host_mmu.arch.mmu, &host_mmu.arch);
311 
312 	/*
313 	 * Make sure to have an ISB before the TLB maintenance below but only
314 	 * when __load_stage2() doesn't include one already.
315 	 */
316 	asm(ALTERNATIVE("isb", "nop", ARM64_WORKAROUND_SPECULATIVE_AT));
317 
318 	/* Invalidate stale HCR bits that may be cached in TLBs */
319 	__tlbi(vmalls12e1);
320 	dsb(nsh);
321 	isb();
322 
323 	return 0;
324 }
325 
326 static int host_stage2_unmap_dev_all(void)
327 {
328 	struct kvm_pgtable *pgt = &host_mmu.pgt;
329 	struct memblock_region *reg;
330 	u64 addr = 0;
331 	int i, ret;
332 
333 	/* Unmap all non-memory regions to recycle the pages */
334 	for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) {
335 		reg = &hyp_memory[i];
336 		ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr);
337 		if (ret)
338 			return ret;
339 	}
340 	return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
341 }
342 
343 struct kvm_mem_range {
344 	u64 start;
345 	u64 end;
346 };
347 
348 static struct memblock_region *find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
349 {
350 	int cur, left = 0, right = hyp_memblock_nr;
351 	struct memblock_region *reg;
352 	phys_addr_t end;
353 
354 	range->start = 0;
355 	range->end = ULONG_MAX;
356 
357 	/* The list of memblock regions is sorted, binary search it */
358 	while (left < right) {
359 		cur = (left + right) >> 1;
360 		reg = &hyp_memory[cur];
361 		end = reg->base + reg->size;
362 		if (addr < reg->base) {
363 			right = cur;
364 			range->end = reg->base;
365 		} else if (addr >= end) {
366 			left = cur + 1;
367 			range->start = end;
368 		} else {
369 			range->start = reg->base;
370 			range->end = end;
371 			return reg;
372 		}
373 	}
374 
375 	return NULL;
376 }
377 
378 bool addr_is_memory(phys_addr_t phys)
379 {
380 	struct kvm_mem_range range;
381 
382 	return !!find_mem_range(phys, &range);
383 }
384 
385 static bool addr_is_allowed_memory(phys_addr_t phys)
386 {
387 	struct memblock_region *reg;
388 	struct kvm_mem_range range;
389 
390 	reg = find_mem_range(phys, &range);
391 
392 	return reg && !(reg->flags & MEMBLOCK_NOMAP);
393 }
394 
395 static bool is_in_mem_range(u64 addr, struct kvm_mem_range *range)
396 {
397 	return range->start <= addr && addr < range->end;
398 }
399 
400 static bool range_is_memory(u64 start, u64 end)
401 {
402 	struct kvm_mem_range r;
403 
404 	if (!find_mem_range(start, &r))
405 		return false;
406 
407 	return is_in_mem_range(end - 1, &r);
408 }
409 
410 static inline int __host_stage2_idmap(u64 start, u64 end,
411 				      enum kvm_pgtable_prot prot)
412 {
413 	return kvm_pgtable_stage2_map(&host_mmu.pgt, start, end - start, start,
414 				      prot, &host_s2_pool, 0);
415 }
416 
417 /*
418  * The pool has been provided with enough pages to cover all of memory with
419  * page granularity, but it is difficult to know how much of the MMIO range
420  * we will need to cover upfront, so we may need to 'recycle' the pages if we
421  * run out.
422  */
423 #define host_stage2_try(fn, ...)					\
424 	({								\
425 		int __ret;						\
426 		hyp_assert_lock_held(&host_mmu.lock);			\
427 		__ret = fn(__VA_ARGS__);				\
428 		if (__ret == -ENOMEM) {					\
429 			__ret = host_stage2_unmap_dev_all();		\
430 			if (!__ret)					\
431 				__ret = fn(__VA_ARGS__);		\
432 		}							\
433 		__ret;							\
434 	 })
435 
436 static inline bool range_included(struct kvm_mem_range *child,
437 				  struct kvm_mem_range *parent)
438 {
439 	return parent->start <= child->start && child->end <= parent->end;
440 }
441 
442 static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)
443 {
444 	struct kvm_mem_range cur;
445 	kvm_pte_t pte;
446 	s8 level;
447 	int ret;
448 
449 	hyp_assert_lock_held(&host_mmu.lock);
450 	ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr, &pte, &level);
451 	if (ret)
452 		return ret;
453 
454 	if (kvm_pte_valid(pte))
455 		return -EAGAIN;
456 
457 	if (pte)
458 		return -EPERM;
459 
460 	do {
461 		u64 granule = kvm_granule_size(level);
462 		cur.start = ALIGN_DOWN(addr, granule);
463 		cur.end = cur.start + granule;
464 		level++;
465 	} while ((level <= KVM_PGTABLE_LAST_LEVEL) &&
466 			!(kvm_level_supports_block_mapping(level) &&
467 			  range_included(&cur, range)));
468 
469 	*range = cur;
470 
471 	return 0;
472 }
473 
474 int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
475 			     enum kvm_pgtable_prot prot)
476 {
477 	return host_stage2_try(__host_stage2_idmap, addr, addr + size, prot);
478 }
479 
480 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
481 {
482 	return host_stage2_try(kvm_pgtable_stage2_set_owner, &host_mmu.pgt,
483 			       addr, size, &host_s2_pool, owner_id);
484 }
485 
486 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot)
487 {
488 	/*
489 	 * Block mappings must be used with care in the host stage-2 as a
490 	 * kvm_pgtable_stage2_map() operation targeting a page in the range of
491 	 * an existing block will delete the block under the assumption that
492 	 * mappings in the rest of the block range can always be rebuilt lazily.
493 	 * That assumption is correct for the host stage-2 with RWX mappings
494 	 * targeting memory or RW mappings targeting MMIO ranges (see
495 	 * host_stage2_idmap() below which implements some of the host memory
496 	 * abort logic). However, this is not safe for any other mappings where
497 	 * the host stage-2 page-table is in fact the only place where this
498 	 * state is stored. In all those cases, it is safer to use page-level
499 	 * mappings, hence avoiding to lose the state because of side-effects in
500 	 * kvm_pgtable_stage2_map().
501 	 */
502 	if (range_is_memory(addr, end))
503 		return prot != PKVM_HOST_MEM_PROT;
504 	else
505 		return prot != PKVM_HOST_MMIO_PROT;
506 }
507 
508 static int host_stage2_idmap(u64 addr)
509 {
510 	struct kvm_mem_range range;
511 	bool is_memory = !!find_mem_range(addr, &range);
512 	enum kvm_pgtable_prot prot;
513 	int ret;
514 
515 	prot = is_memory ? PKVM_HOST_MEM_PROT : PKVM_HOST_MMIO_PROT;
516 
517 	host_lock_component();
518 	ret = host_stage2_adjust_range(addr, &range);
519 	if (ret)
520 		goto unlock;
521 
522 	ret = host_stage2_idmap_locked(range.start, range.end - range.start, prot);
523 unlock:
524 	host_unlock_component();
525 
526 	return ret;
527 }
528 
529 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
530 {
531 	struct kvm_vcpu_fault_info fault;
532 	u64 esr, addr;
533 	int ret = 0;
534 
535 	esr = read_sysreg_el2(SYS_ESR);
536 	if (!__get_fault_info(esr, &fault)) {
537 		/*
538 		 * We've presumably raced with a page-table change which caused
539 		 * AT to fail, try again.
540 		 */
541 		return;
542 	}
543 
544 	addr = (fault.hpfar_el2 & HPFAR_MASK) << 8;
545 	ret = host_stage2_idmap(addr);
546 	BUG_ON(ret && ret != -EAGAIN);
547 }
548 
549 struct pkvm_mem_transition {
550 	u64				nr_pages;
551 
552 	struct {
553 		enum pkvm_component_id	id;
554 		/* Address in the initiator's address space */
555 		u64			addr;
556 
557 		union {
558 			struct {
559 				/* Address in the completer's address space */
560 				u64	completer_addr;
561 			} host;
562 			struct {
563 				u64	completer_addr;
564 			} hyp;
565 		};
566 	} initiator;
567 
568 	struct {
569 		enum pkvm_component_id	id;
570 	} completer;
571 };
572 
573 struct pkvm_mem_share {
574 	const struct pkvm_mem_transition	tx;
575 	const enum kvm_pgtable_prot		completer_prot;
576 };
577 
578 struct pkvm_mem_donation {
579 	const struct pkvm_mem_transition	tx;
580 };
581 
582 struct check_walk_data {
583 	enum pkvm_page_state	desired;
584 	enum pkvm_page_state	(*get_page_state)(kvm_pte_t pte, u64 addr);
585 };
586 
587 static int __check_page_state_visitor(const struct kvm_pgtable_visit_ctx *ctx,
588 				      enum kvm_pgtable_walk_flags visit)
589 {
590 	struct check_walk_data *d = ctx->arg;
591 
592 	return d->get_page_state(ctx->old, ctx->addr) == d->desired ? 0 : -EPERM;
593 }
594 
595 static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,
596 				  struct check_walk_data *data)
597 {
598 	struct kvm_pgtable_walker walker = {
599 		.cb	= __check_page_state_visitor,
600 		.arg	= data,
601 		.flags	= KVM_PGTABLE_WALK_LEAF,
602 	};
603 
604 	return kvm_pgtable_walk(pgt, addr, size, &walker);
605 }
606 
607 static enum pkvm_page_state host_get_page_state(kvm_pte_t pte, u64 addr)
608 {
609 	if (!addr_is_allowed_memory(addr))
610 		return PKVM_NOPAGE;
611 
612 	if (!kvm_pte_valid(pte) && pte)
613 		return PKVM_NOPAGE;
614 
615 	return pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte));
616 }
617 
618 static int __host_check_page_state_range(u64 addr, u64 size,
619 					 enum pkvm_page_state state)
620 {
621 	struct check_walk_data d = {
622 		.desired	= state,
623 		.get_page_state	= host_get_page_state,
624 	};
625 
626 	hyp_assert_lock_held(&host_mmu.lock);
627 	return check_page_state_range(&host_mmu.pgt, addr, size, &d);
628 }
629 
630 static int __host_set_page_state_range(u64 addr, u64 size,
631 				       enum pkvm_page_state state)
632 {
633 	enum kvm_pgtable_prot prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, state);
634 
635 	return host_stage2_idmap_locked(addr, size, prot);
636 }
637 
638 static int host_request_owned_transition(u64 *completer_addr,
639 					 const struct pkvm_mem_transition *tx)
640 {
641 	u64 size = tx->nr_pages * PAGE_SIZE;
642 	u64 addr = tx->initiator.addr;
643 
644 	*completer_addr = tx->initiator.host.completer_addr;
645 	return __host_check_page_state_range(addr, size, PKVM_PAGE_OWNED);
646 }
647 
648 static int host_request_unshare(u64 *completer_addr,
649 				const struct pkvm_mem_transition *tx)
650 {
651 	u64 size = tx->nr_pages * PAGE_SIZE;
652 	u64 addr = tx->initiator.addr;
653 
654 	*completer_addr = tx->initiator.host.completer_addr;
655 	return __host_check_page_state_range(addr, size, PKVM_PAGE_SHARED_OWNED);
656 }
657 
658 static int host_initiate_share(u64 *completer_addr,
659 			       const struct pkvm_mem_transition *tx)
660 {
661 	u64 size = tx->nr_pages * PAGE_SIZE;
662 	u64 addr = tx->initiator.addr;
663 
664 	*completer_addr = tx->initiator.host.completer_addr;
665 	return __host_set_page_state_range(addr, size, PKVM_PAGE_SHARED_OWNED);
666 }
667 
668 static int host_initiate_unshare(u64 *completer_addr,
669 				 const struct pkvm_mem_transition *tx)
670 {
671 	u64 size = tx->nr_pages * PAGE_SIZE;
672 	u64 addr = tx->initiator.addr;
673 
674 	*completer_addr = tx->initiator.host.completer_addr;
675 	return __host_set_page_state_range(addr, size, PKVM_PAGE_OWNED);
676 }
677 
678 static int host_initiate_donation(u64 *completer_addr,
679 				  const struct pkvm_mem_transition *tx)
680 {
681 	u8 owner_id = tx->completer.id;
682 	u64 size = tx->nr_pages * PAGE_SIZE;
683 
684 	*completer_addr = tx->initiator.host.completer_addr;
685 	return host_stage2_set_owner_locked(tx->initiator.addr, size, owner_id);
686 }
687 
688 static bool __host_ack_skip_pgtable_check(const struct pkvm_mem_transition *tx)
689 {
690 	return !(IS_ENABLED(CONFIG_NVHE_EL2_DEBUG) ||
691 		 tx->initiator.id != PKVM_ID_HYP);
692 }
693 
694 static int __host_ack_transition(u64 addr, const struct pkvm_mem_transition *tx,
695 				 enum pkvm_page_state state)
696 {
697 	u64 size = tx->nr_pages * PAGE_SIZE;
698 
699 	if (__host_ack_skip_pgtable_check(tx))
700 		return 0;
701 
702 	return __host_check_page_state_range(addr, size, state);
703 }
704 
705 static int host_ack_donation(u64 addr, const struct pkvm_mem_transition *tx)
706 {
707 	return __host_ack_transition(addr, tx, PKVM_NOPAGE);
708 }
709 
710 static int host_complete_donation(u64 addr, const struct pkvm_mem_transition *tx)
711 {
712 	u64 size = tx->nr_pages * PAGE_SIZE;
713 	u8 host_id = tx->completer.id;
714 
715 	return host_stage2_set_owner_locked(addr, size, host_id);
716 }
717 
718 static enum pkvm_page_state hyp_get_page_state(kvm_pte_t pte, u64 addr)
719 {
720 	if (!kvm_pte_valid(pte))
721 		return PKVM_NOPAGE;
722 
723 	return pkvm_getstate(kvm_pgtable_hyp_pte_prot(pte));
724 }
725 
726 static int __hyp_check_page_state_range(u64 addr, u64 size,
727 					enum pkvm_page_state state)
728 {
729 	struct check_walk_data d = {
730 		.desired	= state,
731 		.get_page_state	= hyp_get_page_state,
732 	};
733 
734 	hyp_assert_lock_held(&pkvm_pgd_lock);
735 	return check_page_state_range(&pkvm_pgtable, addr, size, &d);
736 }
737 
738 static int hyp_request_donation(u64 *completer_addr,
739 				const struct pkvm_mem_transition *tx)
740 {
741 	u64 size = tx->nr_pages * PAGE_SIZE;
742 	u64 addr = tx->initiator.addr;
743 
744 	*completer_addr = tx->initiator.hyp.completer_addr;
745 	return __hyp_check_page_state_range(addr, size, PKVM_PAGE_OWNED);
746 }
747 
748 static int hyp_initiate_donation(u64 *completer_addr,
749 				 const struct pkvm_mem_transition *tx)
750 {
751 	u64 size = tx->nr_pages * PAGE_SIZE;
752 	int ret;
753 
754 	*completer_addr = tx->initiator.hyp.completer_addr;
755 	ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, tx->initiator.addr, size);
756 	return (ret != size) ? -EFAULT : 0;
757 }
758 
759 static bool __hyp_ack_skip_pgtable_check(const struct pkvm_mem_transition *tx)
760 {
761 	return !(IS_ENABLED(CONFIG_NVHE_EL2_DEBUG) ||
762 		 tx->initiator.id != PKVM_ID_HOST);
763 }
764 
765 static int hyp_ack_share(u64 addr, const struct pkvm_mem_transition *tx,
766 			 enum kvm_pgtable_prot perms)
767 {
768 	u64 size = tx->nr_pages * PAGE_SIZE;
769 
770 	if (perms != PAGE_HYP)
771 		return -EPERM;
772 
773 	if (__hyp_ack_skip_pgtable_check(tx))
774 		return 0;
775 
776 	return __hyp_check_page_state_range(addr, size, PKVM_NOPAGE);
777 }
778 
779 static int hyp_ack_unshare(u64 addr, const struct pkvm_mem_transition *tx)
780 {
781 	u64 size = tx->nr_pages * PAGE_SIZE;
782 
783 	if (tx->initiator.id == PKVM_ID_HOST && hyp_page_count((void *)addr))
784 		return -EBUSY;
785 
786 	return __hyp_check_page_state_range(addr, size,
787 					    PKVM_PAGE_SHARED_BORROWED);
788 }
789 
790 static int hyp_ack_donation(u64 addr, const struct pkvm_mem_transition *tx)
791 {
792 	u64 size = tx->nr_pages * PAGE_SIZE;
793 
794 	if (__hyp_ack_skip_pgtable_check(tx))
795 		return 0;
796 
797 	return __hyp_check_page_state_range(addr, size, PKVM_NOPAGE);
798 }
799 
800 static int hyp_complete_share(u64 addr, const struct pkvm_mem_transition *tx,
801 			      enum kvm_pgtable_prot perms)
802 {
803 	void *start = (void *)addr, *end = start + (tx->nr_pages * PAGE_SIZE);
804 	enum kvm_pgtable_prot prot;
805 
806 	prot = pkvm_mkstate(perms, PKVM_PAGE_SHARED_BORROWED);
807 	return pkvm_create_mappings_locked(start, end, prot);
808 }
809 
810 static int hyp_complete_unshare(u64 addr, const struct pkvm_mem_transition *tx)
811 {
812 	u64 size = tx->nr_pages * PAGE_SIZE;
813 	int ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, addr, size);
814 
815 	return (ret != size) ? -EFAULT : 0;
816 }
817 
818 static int hyp_complete_donation(u64 addr,
819 				 const struct pkvm_mem_transition *tx)
820 {
821 	void *start = (void *)addr, *end = start + (tx->nr_pages * PAGE_SIZE);
822 	enum kvm_pgtable_prot prot = pkvm_mkstate(PAGE_HYP, PKVM_PAGE_OWNED);
823 
824 	return pkvm_create_mappings_locked(start, end, prot);
825 }
826 
827 static int check_share(struct pkvm_mem_share *share)
828 {
829 	const struct pkvm_mem_transition *tx = &share->tx;
830 	u64 completer_addr;
831 	int ret;
832 
833 	switch (tx->initiator.id) {
834 	case PKVM_ID_HOST:
835 		ret = host_request_owned_transition(&completer_addr, tx);
836 		break;
837 	default:
838 		ret = -EINVAL;
839 	}
840 
841 	if (ret)
842 		return ret;
843 
844 	switch (tx->completer.id) {
845 	case PKVM_ID_HYP:
846 		ret = hyp_ack_share(completer_addr, tx, share->completer_prot);
847 		break;
848 	case PKVM_ID_FFA:
849 		/*
850 		 * We only check the host; the secure side will check the other
851 		 * end when we forward the FFA call.
852 		 */
853 		ret = 0;
854 		break;
855 	default:
856 		ret = -EINVAL;
857 	}
858 
859 	return ret;
860 }
861 
862 static int __do_share(struct pkvm_mem_share *share)
863 {
864 	const struct pkvm_mem_transition *tx = &share->tx;
865 	u64 completer_addr;
866 	int ret;
867 
868 	switch (tx->initiator.id) {
869 	case PKVM_ID_HOST:
870 		ret = host_initiate_share(&completer_addr, tx);
871 		break;
872 	default:
873 		ret = -EINVAL;
874 	}
875 
876 	if (ret)
877 		return ret;
878 
879 	switch (tx->completer.id) {
880 	case PKVM_ID_HYP:
881 		ret = hyp_complete_share(completer_addr, tx, share->completer_prot);
882 		break;
883 	case PKVM_ID_FFA:
884 		/*
885 		 * We're not responsible for any secure page-tables, so there's
886 		 * nothing to do here.
887 		 */
888 		ret = 0;
889 		break;
890 	default:
891 		ret = -EINVAL;
892 	}
893 
894 	return ret;
895 }
896 
897 /*
898  * do_share():
899  *
900  * The page owner grants access to another component with a given set
901  * of permissions.
902  *
903  * Initiator: OWNED	=> SHARED_OWNED
904  * Completer: NOPAGE	=> SHARED_BORROWED
905  */
906 static int do_share(struct pkvm_mem_share *share)
907 {
908 	int ret;
909 
910 	ret = check_share(share);
911 	if (ret)
912 		return ret;
913 
914 	return WARN_ON(__do_share(share));
915 }
916 
917 static int check_unshare(struct pkvm_mem_share *share)
918 {
919 	const struct pkvm_mem_transition *tx = &share->tx;
920 	u64 completer_addr;
921 	int ret;
922 
923 	switch (tx->initiator.id) {
924 	case PKVM_ID_HOST:
925 		ret = host_request_unshare(&completer_addr, tx);
926 		break;
927 	default:
928 		ret = -EINVAL;
929 	}
930 
931 	if (ret)
932 		return ret;
933 
934 	switch (tx->completer.id) {
935 	case PKVM_ID_HYP:
936 		ret = hyp_ack_unshare(completer_addr, tx);
937 		break;
938 	case PKVM_ID_FFA:
939 		/* See check_share() */
940 		ret = 0;
941 		break;
942 	default:
943 		ret = -EINVAL;
944 	}
945 
946 	return ret;
947 }
948 
949 static int __do_unshare(struct pkvm_mem_share *share)
950 {
951 	const struct pkvm_mem_transition *tx = &share->tx;
952 	u64 completer_addr;
953 	int ret;
954 
955 	switch (tx->initiator.id) {
956 	case PKVM_ID_HOST:
957 		ret = host_initiate_unshare(&completer_addr, tx);
958 		break;
959 	default:
960 		ret = -EINVAL;
961 	}
962 
963 	if (ret)
964 		return ret;
965 
966 	switch (tx->completer.id) {
967 	case PKVM_ID_HYP:
968 		ret = hyp_complete_unshare(completer_addr, tx);
969 		break;
970 	case PKVM_ID_FFA:
971 		/* See __do_share() */
972 		ret = 0;
973 		break;
974 	default:
975 		ret = -EINVAL;
976 	}
977 
978 	return ret;
979 }
980 
981 /*
982  * do_unshare():
983  *
984  * The page owner revokes access from another component for a range of
985  * pages which were previously shared using do_share().
986  *
987  * Initiator: SHARED_OWNED	=> OWNED
988  * Completer: SHARED_BORROWED	=> NOPAGE
989  */
990 static int do_unshare(struct pkvm_mem_share *share)
991 {
992 	int ret;
993 
994 	ret = check_unshare(share);
995 	if (ret)
996 		return ret;
997 
998 	return WARN_ON(__do_unshare(share));
999 }
1000 
1001 static int check_donation(struct pkvm_mem_donation *donation)
1002 {
1003 	const struct pkvm_mem_transition *tx = &donation->tx;
1004 	u64 completer_addr;
1005 	int ret;
1006 
1007 	switch (tx->initiator.id) {
1008 	case PKVM_ID_HOST:
1009 		ret = host_request_owned_transition(&completer_addr, tx);
1010 		break;
1011 	case PKVM_ID_HYP:
1012 		ret = hyp_request_donation(&completer_addr, tx);
1013 		break;
1014 	default:
1015 		ret = -EINVAL;
1016 	}
1017 
1018 	if (ret)
1019 		return ret;
1020 
1021 	switch (tx->completer.id) {
1022 	case PKVM_ID_HOST:
1023 		ret = host_ack_donation(completer_addr, tx);
1024 		break;
1025 	case PKVM_ID_HYP:
1026 		ret = hyp_ack_donation(completer_addr, tx);
1027 		break;
1028 	default:
1029 		ret = -EINVAL;
1030 	}
1031 
1032 	return ret;
1033 }
1034 
1035 static int __do_donate(struct pkvm_mem_donation *donation)
1036 {
1037 	const struct pkvm_mem_transition *tx = &donation->tx;
1038 	u64 completer_addr;
1039 	int ret;
1040 
1041 	switch (tx->initiator.id) {
1042 	case PKVM_ID_HOST:
1043 		ret = host_initiate_donation(&completer_addr, tx);
1044 		break;
1045 	case PKVM_ID_HYP:
1046 		ret = hyp_initiate_donation(&completer_addr, tx);
1047 		break;
1048 	default:
1049 		ret = -EINVAL;
1050 	}
1051 
1052 	if (ret)
1053 		return ret;
1054 
1055 	switch (tx->completer.id) {
1056 	case PKVM_ID_HOST:
1057 		ret = host_complete_donation(completer_addr, tx);
1058 		break;
1059 	case PKVM_ID_HYP:
1060 		ret = hyp_complete_donation(completer_addr, tx);
1061 		break;
1062 	default:
1063 		ret = -EINVAL;
1064 	}
1065 
1066 	return ret;
1067 }
1068 
1069 /*
1070  * do_donate():
1071  *
1072  * The page owner transfers ownership to another component, losing access
1073  * as a consequence.
1074  *
1075  * Initiator: OWNED	=> NOPAGE
1076  * Completer: NOPAGE	=> OWNED
1077  */
1078 static int do_donate(struct pkvm_mem_donation *donation)
1079 {
1080 	int ret;
1081 
1082 	ret = check_donation(donation);
1083 	if (ret)
1084 		return ret;
1085 
1086 	return WARN_ON(__do_donate(donation));
1087 }
1088 
1089 int __pkvm_host_share_hyp(u64 pfn)
1090 {
1091 	int ret;
1092 	u64 host_addr = hyp_pfn_to_phys(pfn);
1093 	u64 hyp_addr = (u64)__hyp_va(host_addr);
1094 	struct pkvm_mem_share share = {
1095 		.tx	= {
1096 			.nr_pages	= 1,
1097 			.initiator	= {
1098 				.id	= PKVM_ID_HOST,
1099 				.addr	= host_addr,
1100 				.host	= {
1101 					.completer_addr = hyp_addr,
1102 				},
1103 			},
1104 			.completer	= {
1105 				.id	= PKVM_ID_HYP,
1106 			},
1107 		},
1108 		.completer_prot	= PAGE_HYP,
1109 	};
1110 
1111 	host_lock_component();
1112 	hyp_lock_component();
1113 
1114 	ret = do_share(&share);
1115 
1116 	hyp_unlock_component();
1117 	host_unlock_component();
1118 
1119 	return ret;
1120 }
1121 
1122 int __pkvm_host_unshare_hyp(u64 pfn)
1123 {
1124 	int ret;
1125 	u64 host_addr = hyp_pfn_to_phys(pfn);
1126 	u64 hyp_addr = (u64)__hyp_va(host_addr);
1127 	struct pkvm_mem_share share = {
1128 		.tx	= {
1129 			.nr_pages	= 1,
1130 			.initiator	= {
1131 				.id	= PKVM_ID_HOST,
1132 				.addr	= host_addr,
1133 				.host	= {
1134 					.completer_addr = hyp_addr,
1135 				},
1136 			},
1137 			.completer	= {
1138 				.id	= PKVM_ID_HYP,
1139 			},
1140 		},
1141 		.completer_prot	= PAGE_HYP,
1142 	};
1143 
1144 	host_lock_component();
1145 	hyp_lock_component();
1146 
1147 	ret = do_unshare(&share);
1148 
1149 	hyp_unlock_component();
1150 	host_unlock_component();
1151 
1152 	return ret;
1153 }
1154 
1155 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)
1156 {
1157 	int ret;
1158 	u64 host_addr = hyp_pfn_to_phys(pfn);
1159 	u64 hyp_addr = (u64)__hyp_va(host_addr);
1160 	struct pkvm_mem_donation donation = {
1161 		.tx	= {
1162 			.nr_pages	= nr_pages,
1163 			.initiator	= {
1164 				.id	= PKVM_ID_HOST,
1165 				.addr	= host_addr,
1166 				.host	= {
1167 					.completer_addr = hyp_addr,
1168 				},
1169 			},
1170 			.completer	= {
1171 				.id	= PKVM_ID_HYP,
1172 			},
1173 		},
1174 	};
1175 
1176 	host_lock_component();
1177 	hyp_lock_component();
1178 
1179 	ret = do_donate(&donation);
1180 
1181 	hyp_unlock_component();
1182 	host_unlock_component();
1183 
1184 	return ret;
1185 }
1186 
1187 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)
1188 {
1189 	int ret;
1190 	u64 host_addr = hyp_pfn_to_phys(pfn);
1191 	u64 hyp_addr = (u64)__hyp_va(host_addr);
1192 	struct pkvm_mem_donation donation = {
1193 		.tx	= {
1194 			.nr_pages	= nr_pages,
1195 			.initiator	= {
1196 				.id	= PKVM_ID_HYP,
1197 				.addr	= hyp_addr,
1198 				.hyp	= {
1199 					.completer_addr = host_addr,
1200 				},
1201 			},
1202 			.completer	= {
1203 				.id	= PKVM_ID_HOST,
1204 			},
1205 		},
1206 	};
1207 
1208 	host_lock_component();
1209 	hyp_lock_component();
1210 
1211 	ret = do_donate(&donation);
1212 
1213 	hyp_unlock_component();
1214 	host_unlock_component();
1215 
1216 	return ret;
1217 }
1218 
1219 int hyp_pin_shared_mem(void *from, void *to)
1220 {
1221 	u64 cur, start = ALIGN_DOWN((u64)from, PAGE_SIZE);
1222 	u64 end = PAGE_ALIGN((u64)to);
1223 	u64 size = end - start;
1224 	int ret;
1225 
1226 	host_lock_component();
1227 	hyp_lock_component();
1228 
1229 	ret = __host_check_page_state_range(__hyp_pa(start), size,
1230 					    PKVM_PAGE_SHARED_OWNED);
1231 	if (ret)
1232 		goto unlock;
1233 
1234 	ret = __hyp_check_page_state_range(start, size,
1235 					   PKVM_PAGE_SHARED_BORROWED);
1236 	if (ret)
1237 		goto unlock;
1238 
1239 	for (cur = start; cur < end; cur += PAGE_SIZE)
1240 		hyp_page_ref_inc(hyp_virt_to_page(cur));
1241 
1242 unlock:
1243 	hyp_unlock_component();
1244 	host_unlock_component();
1245 
1246 	return ret;
1247 }
1248 
1249 void hyp_unpin_shared_mem(void *from, void *to)
1250 {
1251 	u64 cur, start = ALIGN_DOWN((u64)from, PAGE_SIZE);
1252 	u64 end = PAGE_ALIGN((u64)to);
1253 
1254 	host_lock_component();
1255 	hyp_lock_component();
1256 
1257 	for (cur = start; cur < end; cur += PAGE_SIZE)
1258 		hyp_page_ref_dec(hyp_virt_to_page(cur));
1259 
1260 	hyp_unlock_component();
1261 	host_unlock_component();
1262 }
1263 
1264 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)
1265 {
1266 	int ret;
1267 	struct pkvm_mem_share share = {
1268 		.tx	= {
1269 			.nr_pages	= nr_pages,
1270 			.initiator	= {
1271 				.id	= PKVM_ID_HOST,
1272 				.addr	= hyp_pfn_to_phys(pfn),
1273 			},
1274 			.completer	= {
1275 				.id	= PKVM_ID_FFA,
1276 			},
1277 		},
1278 	};
1279 
1280 	host_lock_component();
1281 	ret = do_share(&share);
1282 	host_unlock_component();
1283 
1284 	return ret;
1285 }
1286 
1287 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)
1288 {
1289 	int ret;
1290 	struct pkvm_mem_share share = {
1291 		.tx	= {
1292 			.nr_pages	= nr_pages,
1293 			.initiator	= {
1294 				.id	= PKVM_ID_HOST,
1295 				.addr	= hyp_pfn_to_phys(pfn),
1296 			},
1297 			.completer	= {
1298 				.id	= PKVM_ID_FFA,
1299 			},
1300 		},
1301 	};
1302 
1303 	host_lock_component();
1304 	ret = do_unshare(&share);
1305 	host_unlock_component();
1306 
1307 	return ret;
1308 }
1309