xref: /openbmc/linux/arch/arm64/kvm/hyp/nvhe/mm.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1f320bc74SQuentin Perret // SPDX-License-Identifier: GPL-2.0-only
2f320bc74SQuentin Perret /*
3f320bc74SQuentin Perret  * Copyright (C) 2020 Google LLC
4f320bc74SQuentin Perret  * Author: Quentin Perret <qperret@google.com>
5f320bc74SQuentin Perret  */
6f320bc74SQuentin Perret 
7f320bc74SQuentin Perret #include <linux/kvm_host.h>
8f320bc74SQuentin Perret #include <asm/kvm_hyp.h>
9f320bc74SQuentin Perret #include <asm/kvm_mmu.h>
10f320bc74SQuentin Perret #include <asm/kvm_pgtable.h>
119429f4b0SWill Deacon #include <asm/kvm_pkvm.h>
12f320bc74SQuentin Perret #include <asm/spectre.h>
13f320bc74SQuentin Perret 
14f320bc74SQuentin Perret #include <nvhe/early_alloc.h>
15f320bc74SQuentin Perret #include <nvhe/gfp.h>
16f320bc74SQuentin Perret #include <nvhe/memory.h>
17aa6948f8SQuentin Perret #include <nvhe/mem_protect.h>
18f320bc74SQuentin Perret #include <nvhe/mm.h>
19f320bc74SQuentin Perret #include <nvhe/spinlock.h>
20f320bc74SQuentin Perret 
21f320bc74SQuentin Perret struct kvm_pgtable pkvm_pgtable;
22f320bc74SQuentin Perret hyp_spinlock_t pkvm_pgd_lock;
23f320bc74SQuentin Perret 
24f320bc74SQuentin Perret struct memblock_region hyp_memory[HYP_MEMBLOCK_REGIONS];
25f320bc74SQuentin Perret unsigned int hyp_memblock_nr;
26f320bc74SQuentin Perret 
27473a3efbSQuentin Perret static u64 __io_map_base;
28473a3efbSQuentin Perret 
29aa6948f8SQuentin Perret struct hyp_fixmap_slot {
30aa6948f8SQuentin Perret 	u64 addr;
31aa6948f8SQuentin Perret 	kvm_pte_t *ptep;
32aa6948f8SQuentin Perret };
33aa6948f8SQuentin Perret static DEFINE_PER_CPU(struct hyp_fixmap_slot, fixmap_slots);
34aa6948f8SQuentin Perret 
__pkvm_create_mappings(unsigned long start,unsigned long size,unsigned long phys,enum kvm_pgtable_prot prot)3564a80fb7SQuentin Perret static int __pkvm_create_mappings(unsigned long start, unsigned long size,
36f320bc74SQuentin Perret 				  unsigned long phys, enum kvm_pgtable_prot prot)
37f320bc74SQuentin Perret {
38f320bc74SQuentin Perret 	int err;
39f320bc74SQuentin Perret 
40f320bc74SQuentin Perret 	hyp_spin_lock(&pkvm_pgd_lock);
41f320bc74SQuentin Perret 	err = kvm_pgtable_hyp_map(&pkvm_pgtable, start, size, phys, prot);
42f320bc74SQuentin Perret 	hyp_spin_unlock(&pkvm_pgd_lock);
43f320bc74SQuentin Perret 
44f320bc74SQuentin Perret 	return err;
45f320bc74SQuentin Perret }
46f320bc74SQuentin Perret 
__pkvm_alloc_private_va_range(unsigned long start,size_t size)47*f156a7d1SVincent Donnefort static int __pkvm_alloc_private_va_range(unsigned long start, size_t size)
48*f156a7d1SVincent Donnefort {
49*f156a7d1SVincent Donnefort 	unsigned long cur;
50*f156a7d1SVincent Donnefort 
51*f156a7d1SVincent Donnefort 	hyp_assert_lock_held(&pkvm_pgd_lock);
52*f156a7d1SVincent Donnefort 
53*f156a7d1SVincent Donnefort 	if (!start || start < __io_map_base)
54*f156a7d1SVincent Donnefort 		return -EINVAL;
55*f156a7d1SVincent Donnefort 
56*f156a7d1SVincent Donnefort 	/* The allocated size is always a multiple of PAGE_SIZE */
57*f156a7d1SVincent Donnefort 	cur = start + PAGE_ALIGN(size);
58*f156a7d1SVincent Donnefort 
59*f156a7d1SVincent Donnefort 	/* Are we overflowing on the vmemmap ? */
60*f156a7d1SVincent Donnefort 	if (cur > __hyp_vmemmap)
61*f156a7d1SVincent Donnefort 		return -ENOMEM;
62*f156a7d1SVincent Donnefort 
63*f156a7d1SVincent Donnefort 	__io_map_base = cur;
64*f156a7d1SVincent Donnefort 
65*f156a7d1SVincent Donnefort 	return 0;
66*f156a7d1SVincent Donnefort }
67*f156a7d1SVincent Donnefort 
68f922c13eSKalesh Singh /**
69f922c13eSKalesh Singh  * pkvm_alloc_private_va_range - Allocates a private VA range.
70f922c13eSKalesh Singh  * @size:	The size of the VA range to reserve.
71f922c13eSKalesh Singh  * @haddr:	The hypervisor virtual start address of the allocation.
72f922c13eSKalesh Singh  *
73f922c13eSKalesh Singh  * The private virtual address (VA) range is allocated above __io_map_base
74f922c13eSKalesh Singh  * and aligned based on the order of @size.
75f922c13eSKalesh Singh  *
76f922c13eSKalesh Singh  * Return: 0 on success or negative error code on failure.
77f922c13eSKalesh Singh  */
pkvm_alloc_private_va_range(size_t size,unsigned long * haddr)78f922c13eSKalesh Singh int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr)
79f922c13eSKalesh Singh {
80*f156a7d1SVincent Donnefort 	unsigned long addr;
81*f156a7d1SVincent Donnefort 	int ret;
82f922c13eSKalesh Singh 
83f922c13eSKalesh Singh 	hyp_spin_lock(&pkvm_pgd_lock);
84*f156a7d1SVincent Donnefort 	addr = __io_map_base;
85*f156a7d1SVincent Donnefort 	ret = __pkvm_alloc_private_va_range(addr, size);
86f922c13eSKalesh Singh 	hyp_spin_unlock(&pkvm_pgd_lock);
87f922c13eSKalesh Singh 
88*f156a7d1SVincent Donnefort 	*haddr = addr;
89*f156a7d1SVincent Donnefort 
90f922c13eSKalesh Singh 	return ret;
91f922c13eSKalesh Singh }
92f922c13eSKalesh Singh 
__pkvm_create_private_mapping(phys_addr_t phys,size_t size,enum kvm_pgtable_prot prot,unsigned long * haddr)93f922c13eSKalesh Singh int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
94f922c13eSKalesh Singh 				  enum kvm_pgtable_prot prot,
95f922c13eSKalesh Singh 				  unsigned long *haddr)
96f320bc74SQuentin Perret {
97f320bc74SQuentin Perret 	unsigned long addr;
98f320bc74SQuentin Perret 	int err;
99f320bc74SQuentin Perret 
100f320bc74SQuentin Perret 	size = PAGE_ALIGN(size + offset_in_page(phys));
101f922c13eSKalesh Singh 	err = pkvm_alloc_private_va_range(size, &addr);
102f922c13eSKalesh Singh 	if (err)
103f922c13eSKalesh Singh 		return err;
104f320bc74SQuentin Perret 
105f922c13eSKalesh Singh 	err = __pkvm_create_mappings(addr, size, phys, prot);
106f922c13eSKalesh Singh 	if (err)
107f922c13eSKalesh Singh 		return err;
108f320bc74SQuentin Perret 
109f922c13eSKalesh Singh 	*haddr = addr + offset_in_page(phys);
110f922c13eSKalesh Singh 	return err;
111f320bc74SQuentin Perret }
112f320bc74SQuentin Perret 
pkvm_create_mappings_locked(void * from,void * to,enum kvm_pgtable_prot prot)113f9370010SQuentin Perret int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_prot prot)
114f320bc74SQuentin Perret {
115f320bc74SQuentin Perret 	unsigned long start = (unsigned long)from;
116f320bc74SQuentin Perret 	unsigned long end = (unsigned long)to;
117f320bc74SQuentin Perret 	unsigned long virt_addr;
118f320bc74SQuentin Perret 	phys_addr_t phys;
119f320bc74SQuentin Perret 
120f9370010SQuentin Perret 	hyp_assert_lock_held(&pkvm_pgd_lock);
121f9370010SQuentin Perret 
122f320bc74SQuentin Perret 	start = start & PAGE_MASK;
123f320bc74SQuentin Perret 	end = PAGE_ALIGN(end);
124f320bc74SQuentin Perret 
125f320bc74SQuentin Perret 	for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
126f320bc74SQuentin Perret 		int err;
127f320bc74SQuentin Perret 
128f320bc74SQuentin Perret 		phys = hyp_virt_to_phys((void *)virt_addr);
129f9370010SQuentin Perret 		err = kvm_pgtable_hyp_map(&pkvm_pgtable, virt_addr, PAGE_SIZE,
130f9370010SQuentin Perret 					  phys, prot);
131f320bc74SQuentin Perret 		if (err)
132f320bc74SQuentin Perret 			return err;
133f320bc74SQuentin Perret 	}
134f320bc74SQuentin Perret 
135f320bc74SQuentin Perret 	return 0;
136f320bc74SQuentin Perret }
137f320bc74SQuentin Perret 
pkvm_create_mappings(void * from,void * to,enum kvm_pgtable_prot prot)138f9370010SQuentin Perret int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
139f9370010SQuentin Perret {
140f9370010SQuentin Perret 	int ret;
141f9370010SQuentin Perret 
142f9370010SQuentin Perret 	hyp_spin_lock(&pkvm_pgd_lock);
143f9370010SQuentin Perret 	ret = pkvm_create_mappings_locked(from, to, prot);
144f9370010SQuentin Perret 	hyp_spin_unlock(&pkvm_pgd_lock);
145f9370010SQuentin Perret 
146f9370010SQuentin Perret 	return ret;
147f9370010SQuentin Perret }
148f9370010SQuentin Perret 
hyp_back_vmemmap(phys_addr_t back)1498e6bcc3aSQuentin Perret int hyp_back_vmemmap(phys_addr_t back)
150f320bc74SQuentin Perret {
1518e6bcc3aSQuentin Perret 	unsigned long i, start, size, end = 0;
1528e6bcc3aSQuentin Perret 	int ret;
153f320bc74SQuentin Perret 
1548e6bcc3aSQuentin Perret 	for (i = 0; i < hyp_memblock_nr; i++) {
1558e6bcc3aSQuentin Perret 		start = hyp_memory[i].base;
1568e6bcc3aSQuentin Perret 		start = ALIGN_DOWN((u64)hyp_phys_to_page(start), PAGE_SIZE);
1578e6bcc3aSQuentin Perret 		/*
1588e6bcc3aSQuentin Perret 		 * The begining of the hyp_vmemmap region for the current
1598e6bcc3aSQuentin Perret 		 * memblock may already be backed by the page backing the end
1608e6bcc3aSQuentin Perret 		 * the previous region, so avoid mapping it twice.
1618e6bcc3aSQuentin Perret 		 */
1628e6bcc3aSQuentin Perret 		start = max(start, end);
163f320bc74SQuentin Perret 
1648e6bcc3aSQuentin Perret 		end = hyp_memory[i].base + hyp_memory[i].size;
1658e6bcc3aSQuentin Perret 		end = PAGE_ALIGN((u64)hyp_phys_to_page(end));
1668e6bcc3aSQuentin Perret 		if (start >= end)
1678e6bcc3aSQuentin Perret 			continue;
1688e6bcc3aSQuentin Perret 
1698e6bcc3aSQuentin Perret 		size = end - start;
1708e6bcc3aSQuentin Perret 		ret = __pkvm_create_mappings(start, size, back, PAGE_HYP);
1718e6bcc3aSQuentin Perret 		if (ret)
1728e6bcc3aSQuentin Perret 			return ret;
1738e6bcc3aSQuentin Perret 
1748e6bcc3aSQuentin Perret 		memset(hyp_phys_to_virt(back), 0, size);
1758e6bcc3aSQuentin Perret 		back += size;
1768e6bcc3aSQuentin Perret 	}
1778e6bcc3aSQuentin Perret 
1788e6bcc3aSQuentin Perret 	return 0;
179f320bc74SQuentin Perret }
180f320bc74SQuentin Perret 
181f320bc74SQuentin Perret static void *__hyp_bp_vect_base;
pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot)182f320bc74SQuentin Perret int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot)
183f320bc74SQuentin Perret {
184f320bc74SQuentin Perret 	void *vector;
185f320bc74SQuentin Perret 
186f320bc74SQuentin Perret 	switch (slot) {
187f320bc74SQuentin Perret 	case HYP_VECTOR_DIRECT: {
188f320bc74SQuentin Perret 		vector = __kvm_hyp_vector;
189f320bc74SQuentin Perret 		break;
190f320bc74SQuentin Perret 	}
191f320bc74SQuentin Perret 	case HYP_VECTOR_SPECTRE_DIRECT: {
192f320bc74SQuentin Perret 		vector = __bp_harden_hyp_vecs;
193f320bc74SQuentin Perret 		break;
194f320bc74SQuentin Perret 	}
195f320bc74SQuentin Perret 	case HYP_VECTOR_INDIRECT:
196f320bc74SQuentin Perret 	case HYP_VECTOR_SPECTRE_INDIRECT: {
197f320bc74SQuentin Perret 		vector = (void *)__hyp_bp_vect_base;
198f320bc74SQuentin Perret 		break;
199f320bc74SQuentin Perret 	}
200f320bc74SQuentin Perret 	default:
201f320bc74SQuentin Perret 		return -EINVAL;
202f320bc74SQuentin Perret 	}
203f320bc74SQuentin Perret 
204f320bc74SQuentin Perret 	vector = __kvm_vector_slot2addr(vector, slot);
205f320bc74SQuentin Perret 	*this_cpu_ptr(&kvm_hyp_vector) = (unsigned long)vector;
206f320bc74SQuentin Perret 
207f320bc74SQuentin Perret 	return 0;
208f320bc74SQuentin Perret }
209f320bc74SQuentin Perret 
hyp_map_vectors(void)210f320bc74SQuentin Perret int hyp_map_vectors(void)
211f320bc74SQuentin Perret {
212f320bc74SQuentin Perret 	phys_addr_t phys;
213f922c13eSKalesh Singh 	unsigned long bp_base;
214f922c13eSKalesh Singh 	int ret;
215f320bc74SQuentin Perret 
2165bdf3437SJames Morse 	if (!kvm_system_needs_idmapped_vectors()) {
2175bdf3437SJames Morse 		__hyp_bp_vect_base = __bp_harden_hyp_vecs;
218f320bc74SQuentin Perret 		return 0;
2195bdf3437SJames Morse 	}
220f320bc74SQuentin Perret 
221f320bc74SQuentin Perret 	phys = __hyp_pa(__bp_harden_hyp_vecs);
222f922c13eSKalesh Singh 	ret = __pkvm_create_private_mapping(phys, __BP_HARDEN_HYP_VECS_SZ,
223f922c13eSKalesh Singh 					    PAGE_HYP_EXEC, &bp_base);
224f922c13eSKalesh Singh 	if (ret)
225f922c13eSKalesh Singh 		return ret;
226f320bc74SQuentin Perret 
227f922c13eSKalesh Singh 	__hyp_bp_vect_base = (void *)bp_base;
228f320bc74SQuentin Perret 
229f320bc74SQuentin Perret 	return 0;
230f320bc74SQuentin Perret }
231f320bc74SQuentin Perret 
hyp_fixmap_map(phys_addr_t phys)232aa6948f8SQuentin Perret void *hyp_fixmap_map(phys_addr_t phys)
233aa6948f8SQuentin Perret {
234aa6948f8SQuentin Perret 	struct hyp_fixmap_slot *slot = this_cpu_ptr(&fixmap_slots);
235aa6948f8SQuentin Perret 	kvm_pte_t pte, *ptep = slot->ptep;
236aa6948f8SQuentin Perret 
237aa6948f8SQuentin Perret 	pte = *ptep;
238aa6948f8SQuentin Perret 	pte &= ~kvm_phys_to_pte(KVM_PHYS_INVALID);
239aa6948f8SQuentin Perret 	pte |= kvm_phys_to_pte(phys) | KVM_PTE_VALID;
240aa6948f8SQuentin Perret 	WRITE_ONCE(*ptep, pte);
241aa6948f8SQuentin Perret 	dsb(ishst);
242aa6948f8SQuentin Perret 
243aa6948f8SQuentin Perret 	return (void *)slot->addr;
244aa6948f8SQuentin Perret }
245aa6948f8SQuentin Perret 
fixmap_clear_slot(struct hyp_fixmap_slot * slot)246aa6948f8SQuentin Perret static void fixmap_clear_slot(struct hyp_fixmap_slot *slot)
247aa6948f8SQuentin Perret {
248aa6948f8SQuentin Perret 	kvm_pte_t *ptep = slot->ptep;
249aa6948f8SQuentin Perret 	u64 addr = slot->addr;
250aa6948f8SQuentin Perret 
251aa6948f8SQuentin Perret 	WRITE_ONCE(*ptep, *ptep & ~KVM_PTE_VALID);
252aa6948f8SQuentin Perret 
253aa6948f8SQuentin Perret 	/*
254aa6948f8SQuentin Perret 	 * Irritatingly, the architecture requires that we use inner-shareable
255aa6948f8SQuentin Perret 	 * broadcast TLB invalidation here in case another CPU speculates
256aa6948f8SQuentin Perret 	 * through our fixmap and decides to create an "amalagamation of the
257aa6948f8SQuentin Perret 	 * values held in the TLB" due to the apparent lack of a
258aa6948f8SQuentin Perret 	 * break-before-make sequence.
259aa6948f8SQuentin Perret 	 *
260aa6948f8SQuentin Perret 	 * https://lore.kernel.org/kvm/20221017115209.2099-1-will@kernel.org/T/#mf10dfbaf1eaef9274c581b81c53758918c1d0f03
261aa6948f8SQuentin Perret 	 */
262aa6948f8SQuentin Perret 	dsb(ishst);
263aa6948f8SQuentin Perret 	__tlbi_level(vale2is, __TLBI_VADDR(addr, 0), (KVM_PGTABLE_MAX_LEVELS - 1));
264aa6948f8SQuentin Perret 	dsb(ish);
265aa6948f8SQuentin Perret 	isb();
266aa6948f8SQuentin Perret }
267aa6948f8SQuentin Perret 
hyp_fixmap_unmap(void)268aa6948f8SQuentin Perret void hyp_fixmap_unmap(void)
269aa6948f8SQuentin Perret {
270aa6948f8SQuentin Perret 	fixmap_clear_slot(this_cpu_ptr(&fixmap_slots));
271aa6948f8SQuentin Perret }
272aa6948f8SQuentin Perret 
__create_fixmap_slot_cb(const struct kvm_pgtable_visit_ctx * ctx,enum kvm_pgtable_walk_flags visit)273cfa72993SMarc Zyngier static int __create_fixmap_slot_cb(const struct kvm_pgtable_visit_ctx *ctx,
274cfa72993SMarc Zyngier 				   enum kvm_pgtable_walk_flags visit)
275aa6948f8SQuentin Perret {
276cfa72993SMarc Zyngier 	struct hyp_fixmap_slot *slot = per_cpu_ptr(&fixmap_slots, (u64)ctx->arg);
277aa6948f8SQuentin Perret 
278cfa72993SMarc Zyngier 	if (!kvm_pte_valid(ctx->old) || ctx->level != KVM_PGTABLE_MAX_LEVELS - 1)
279aa6948f8SQuentin Perret 		return -EINVAL;
280aa6948f8SQuentin Perret 
281cfa72993SMarc Zyngier 	slot->addr = ctx->addr;
282cfa72993SMarc Zyngier 	slot->ptep = ctx->ptep;
283aa6948f8SQuentin Perret 
284aa6948f8SQuentin Perret 	/*
285aa6948f8SQuentin Perret 	 * Clear the PTE, but keep the page-table page refcount elevated to
286aa6948f8SQuentin Perret 	 * prevent it from ever being freed. This lets us manipulate the PTEs
287aa6948f8SQuentin Perret 	 * by hand safely without ever needing to allocate memory.
288aa6948f8SQuentin Perret 	 */
289aa6948f8SQuentin Perret 	fixmap_clear_slot(slot);
290aa6948f8SQuentin Perret 
291aa6948f8SQuentin Perret 	return 0;
292aa6948f8SQuentin Perret }
293aa6948f8SQuentin Perret 
create_fixmap_slot(u64 addr,u64 cpu)294aa6948f8SQuentin Perret static int create_fixmap_slot(u64 addr, u64 cpu)
295aa6948f8SQuentin Perret {
296aa6948f8SQuentin Perret 	struct kvm_pgtable_walker walker = {
297aa6948f8SQuentin Perret 		.cb	= __create_fixmap_slot_cb,
298aa6948f8SQuentin Perret 		.flags	= KVM_PGTABLE_WALK_LEAF,
299aa6948f8SQuentin Perret 		.arg = (void *)cpu,
300aa6948f8SQuentin Perret 	};
301aa6948f8SQuentin Perret 
302aa6948f8SQuentin Perret 	return kvm_pgtable_walk(&pkvm_pgtable, addr, PAGE_SIZE, &walker);
303aa6948f8SQuentin Perret }
304aa6948f8SQuentin Perret 
hyp_create_pcpu_fixmap(void)305aa6948f8SQuentin Perret int hyp_create_pcpu_fixmap(void)
306aa6948f8SQuentin Perret {
307aa6948f8SQuentin Perret 	unsigned long addr, i;
308aa6948f8SQuentin Perret 	int ret;
309aa6948f8SQuentin Perret 
310aa6948f8SQuentin Perret 	for (i = 0; i < hyp_nr_cpus; i++) {
311aa6948f8SQuentin Perret 		ret = pkvm_alloc_private_va_range(PAGE_SIZE, &addr);
312aa6948f8SQuentin Perret 		if (ret)
313aa6948f8SQuentin Perret 			return ret;
314aa6948f8SQuentin Perret 
315aa6948f8SQuentin Perret 		ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr, PAGE_SIZE,
316aa6948f8SQuentin Perret 					  __hyp_pa(__hyp_bss_start), PAGE_HYP);
317aa6948f8SQuentin Perret 		if (ret)
318aa6948f8SQuentin Perret 			return ret;
319aa6948f8SQuentin Perret 
320aa6948f8SQuentin Perret 		ret = create_fixmap_slot(addr, i);
321aa6948f8SQuentin Perret 		if (ret)
322aa6948f8SQuentin Perret 			return ret;
323aa6948f8SQuentin Perret 	}
324aa6948f8SQuentin Perret 
325aa6948f8SQuentin Perret 	return 0;
326aa6948f8SQuentin Perret }
327aa6948f8SQuentin Perret 
hyp_create_idmap(u32 hyp_va_bits)328f320bc74SQuentin Perret int hyp_create_idmap(u32 hyp_va_bits)
329f320bc74SQuentin Perret {
330f320bc74SQuentin Perret 	unsigned long start, end;
331f320bc74SQuentin Perret 
332f320bc74SQuentin Perret 	start = hyp_virt_to_phys((void *)__hyp_idmap_text_start);
333f320bc74SQuentin Perret 	start = ALIGN_DOWN(start, PAGE_SIZE);
334f320bc74SQuentin Perret 
335f320bc74SQuentin Perret 	end = hyp_virt_to_phys((void *)__hyp_idmap_text_end);
336f320bc74SQuentin Perret 	end = ALIGN(end, PAGE_SIZE);
337f320bc74SQuentin Perret 
338f320bc74SQuentin Perret 	/*
339f320bc74SQuentin Perret 	 * One half of the VA space is reserved to linearly map portions of
340f320bc74SQuentin Perret 	 * memory -- see va_layout.c for more details. The other half of the VA
341f320bc74SQuentin Perret 	 * space contains the trampoline page, and needs some care. Split that
342f320bc74SQuentin Perret 	 * second half in two and find the quarter of VA space not conflicting
343f320bc74SQuentin Perret 	 * with the idmap to place the IOs and the vmemmap. IOs use the lower
344f320bc74SQuentin Perret 	 * half of the quarter and the vmemmap the upper half.
345f320bc74SQuentin Perret 	 */
346f320bc74SQuentin Perret 	__io_map_base = start & BIT(hyp_va_bits - 2);
347f320bc74SQuentin Perret 	__io_map_base ^= BIT(hyp_va_bits - 2);
348f320bc74SQuentin Perret 	__hyp_vmemmap = __io_map_base | BIT(hyp_va_bits - 3);
349f320bc74SQuentin Perret 
350f320bc74SQuentin Perret 	return __pkvm_create_mappings(start, end - start, start, PAGE_HYP_EXEC);
351f320bc74SQuentin Perret }
352717a7eebSQuentin Perret 
pkvm_create_stack(phys_addr_t phys,unsigned long * haddr)353*f156a7d1SVincent Donnefort int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr)
354*f156a7d1SVincent Donnefort {
355*f156a7d1SVincent Donnefort 	unsigned long addr, prev_base;
356*f156a7d1SVincent Donnefort 	size_t size;
357*f156a7d1SVincent Donnefort 	int ret;
358*f156a7d1SVincent Donnefort 
359*f156a7d1SVincent Donnefort 	hyp_spin_lock(&pkvm_pgd_lock);
360*f156a7d1SVincent Donnefort 
361*f156a7d1SVincent Donnefort 	prev_base = __io_map_base;
362*f156a7d1SVincent Donnefort 	/*
363*f156a7d1SVincent Donnefort 	 * Efficient stack verification using the PAGE_SHIFT bit implies
364*f156a7d1SVincent Donnefort 	 * an alignment of our allocation on the order of the size.
365*f156a7d1SVincent Donnefort 	 */
366*f156a7d1SVincent Donnefort 	size = PAGE_SIZE * 2;
367*f156a7d1SVincent Donnefort 	addr = ALIGN(__io_map_base, size);
368*f156a7d1SVincent Donnefort 
369*f156a7d1SVincent Donnefort 	ret = __pkvm_alloc_private_va_range(addr, size);
370*f156a7d1SVincent Donnefort 	if (!ret) {
371*f156a7d1SVincent Donnefort 		/*
372*f156a7d1SVincent Donnefort 		 * Since the stack grows downwards, map the stack to the page
373*f156a7d1SVincent Donnefort 		 * at the higher address and leave the lower guard page
374*f156a7d1SVincent Donnefort 		 * unbacked.
375*f156a7d1SVincent Donnefort 		 *
376*f156a7d1SVincent Donnefort 		 * Any valid stack address now has the PAGE_SHIFT bit as 1
377*f156a7d1SVincent Donnefort 		 * and addresses corresponding to the guard page have the
378*f156a7d1SVincent Donnefort 		 * PAGE_SHIFT bit as 0 - this is used for overflow detection.
379*f156a7d1SVincent Donnefort 		 */
380*f156a7d1SVincent Donnefort 		ret = kvm_pgtable_hyp_map(&pkvm_pgtable, addr + PAGE_SIZE,
381*f156a7d1SVincent Donnefort 					  PAGE_SIZE, phys, PAGE_HYP);
382*f156a7d1SVincent Donnefort 		if (ret)
383*f156a7d1SVincent Donnefort 			__io_map_base = prev_base;
384*f156a7d1SVincent Donnefort 	}
385*f156a7d1SVincent Donnefort 	hyp_spin_unlock(&pkvm_pgd_lock);
386*f156a7d1SVincent Donnefort 
387*f156a7d1SVincent Donnefort 	*haddr = addr + size;
388*f156a7d1SVincent Donnefort 
389*f156a7d1SVincent Donnefort 	return ret;
390*f156a7d1SVincent Donnefort }
391*f156a7d1SVincent Donnefort 
admit_host_page(void * arg)392717a7eebSQuentin Perret static void *admit_host_page(void *arg)
393717a7eebSQuentin Perret {
394717a7eebSQuentin Perret 	struct kvm_hyp_memcache *host_mc = arg;
395717a7eebSQuentin Perret 
396717a7eebSQuentin Perret 	if (!host_mc->nr_pages)
397717a7eebSQuentin Perret 		return NULL;
398717a7eebSQuentin Perret 
399717a7eebSQuentin Perret 	/*
400717a7eebSQuentin Perret 	 * The host still owns the pages in its memcache, so we need to go
401717a7eebSQuentin Perret 	 * through a full host-to-hyp donation cycle to change it. Fortunately,
402717a7eebSQuentin Perret 	 * __pkvm_host_donate_hyp() takes care of races for us, so if it
403717a7eebSQuentin Perret 	 * succeeds we're good to go.
404717a7eebSQuentin Perret 	 */
405717a7eebSQuentin Perret 	if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(host_mc->head), 1))
406717a7eebSQuentin Perret 		return NULL;
407717a7eebSQuentin Perret 
408717a7eebSQuentin Perret 	return pop_hyp_memcache(host_mc, hyp_phys_to_virt);
409717a7eebSQuentin Perret }
410717a7eebSQuentin Perret 
411717a7eebSQuentin Perret /* Refill our local memcache by poping pages from the one provided by the host. */
refill_memcache(struct kvm_hyp_memcache * mc,unsigned long min_pages,struct kvm_hyp_memcache * host_mc)412717a7eebSQuentin Perret int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
413717a7eebSQuentin Perret 		    struct kvm_hyp_memcache *host_mc)
414717a7eebSQuentin Perret {
415717a7eebSQuentin Perret 	struct kvm_hyp_memcache tmp = *host_mc;
416717a7eebSQuentin Perret 	int ret;
417717a7eebSQuentin Perret 
418717a7eebSQuentin Perret 	ret =  __topup_hyp_memcache(mc, min_pages, admit_host_page,
419717a7eebSQuentin Perret 				    hyp_virt_to_phys, &tmp);
420717a7eebSQuentin Perret 	*host_mc = tmp;
421717a7eebSQuentin Perret 
422717a7eebSQuentin Perret 	return ret;
423717a7eebSQuentin Perret }
424