1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Google LLC
4  * Author: Quentin Perret <qperret@google.com>
5  */
6 
7 #include <linux/kvm_host.h>
8 #include <asm/kvm_hyp.h>
9 #include <asm/kvm_mmu.h>
10 #include <asm/kvm_pgtable.h>
11 #include <asm/kvm_pkvm.h>
12 
13 #include <nvhe/early_alloc.h>
14 #include <nvhe/ffa.h>
15 #include <nvhe/fixed_config.h>
16 #include <nvhe/gfp.h>
17 #include <nvhe/memory.h>
18 #include <nvhe/mem_protect.h>
19 #include <nvhe/mm.h>
20 #include <nvhe/pkvm.h>
21 #include <nvhe/trap_handler.h>
22 
23 unsigned long hyp_nr_cpus;
24 
25 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
26 			 (unsigned long)__per_cpu_start)
27 
28 static void *vmemmap_base;
29 static void *vm_table_base;
30 static void *hyp_pgt_base;
31 static void *host_s2_pgt_base;
32 static void *ffa_proxy_pages;
33 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
34 static struct hyp_pool hpool;
35 
divide_memory_pool(void * virt,unsigned long size)36 static int divide_memory_pool(void *virt, unsigned long size)
37 {
38 	unsigned long nr_pages;
39 
40 	hyp_early_alloc_init(virt, size);
41 
42 	nr_pages = hyp_vmemmap_pages(sizeof(struct hyp_page));
43 	vmemmap_base = hyp_early_alloc_contig(nr_pages);
44 	if (!vmemmap_base)
45 		return -ENOMEM;
46 
47 	nr_pages = hyp_vm_table_pages();
48 	vm_table_base = hyp_early_alloc_contig(nr_pages);
49 	if (!vm_table_base)
50 		return -ENOMEM;
51 
52 	nr_pages = hyp_s1_pgtable_pages();
53 	hyp_pgt_base = hyp_early_alloc_contig(nr_pages);
54 	if (!hyp_pgt_base)
55 		return -ENOMEM;
56 
57 	nr_pages = host_s2_pgtable_pages();
58 	host_s2_pgt_base = hyp_early_alloc_contig(nr_pages);
59 	if (!host_s2_pgt_base)
60 		return -ENOMEM;
61 
62 	nr_pages = hyp_ffa_proxy_pages();
63 	ffa_proxy_pages = hyp_early_alloc_contig(nr_pages);
64 	if (!ffa_proxy_pages)
65 		return -ENOMEM;
66 
67 	return 0;
68 }
69 
pkvm_create_host_sve_mappings(void)70 static int pkvm_create_host_sve_mappings(void)
71 {
72 	void *start, *end;
73 	int ret, i;
74 
75 	if (!system_supports_sve())
76 		return 0;
77 
78 	for (i = 0; i < hyp_nr_cpus; i++) {
79 		struct kvm_host_data *host_data = per_cpu_ptr(&kvm_host_data, i);
80 		struct cpu_sve_state *sve_state = host_data->sve_state;
81 
82 		start = kern_hyp_va(sve_state);
83 		end = start + PAGE_ALIGN(pkvm_host_sve_state_size());
84 		ret = pkvm_create_mappings(start, end, PAGE_HYP);
85 		if (ret)
86 			return ret;
87 	}
88 
89 	return 0;
90 }
91 
recreate_hyp_mappings(phys_addr_t phys,unsigned long size,unsigned long * per_cpu_base,u32 hyp_va_bits)92 static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
93 				 unsigned long *per_cpu_base,
94 				 u32 hyp_va_bits)
95 {
96 	void *start, *end, *virt = hyp_phys_to_virt(phys);
97 	unsigned long pgt_size = hyp_s1_pgtable_pages() << PAGE_SHIFT;
98 	enum kvm_pgtable_prot prot;
99 	int ret, i;
100 
101 	/* Recreate the hyp page-table using the early page allocator */
102 	hyp_early_alloc_init(hyp_pgt_base, pgt_size);
103 	ret = kvm_pgtable_hyp_init(&pkvm_pgtable, hyp_va_bits,
104 				   &hyp_early_alloc_mm_ops);
105 	if (ret)
106 		return ret;
107 
108 	ret = hyp_create_idmap(hyp_va_bits);
109 	if (ret)
110 		return ret;
111 
112 	ret = hyp_map_vectors();
113 	if (ret)
114 		return ret;
115 
116 	ret = hyp_back_vmemmap(hyp_virt_to_phys(vmemmap_base));
117 	if (ret)
118 		return ret;
119 
120 	ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, PAGE_HYP_EXEC);
121 	if (ret)
122 		return ret;
123 
124 	ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO);
125 	if (ret)
126 		return ret;
127 
128 	ret = pkvm_create_mappings(__hyp_bss_start, __hyp_bss_end, PAGE_HYP);
129 	if (ret)
130 		return ret;
131 
132 	ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP);
133 	if (ret)
134 		return ret;
135 
136 	for (i = 0; i < hyp_nr_cpus; i++) {
137 		struct kvm_nvhe_init_params *params = per_cpu_ptr(&kvm_init_params, i);
138 
139 		start = (void *)kern_hyp_va(per_cpu_base[i]);
140 		end = start + PAGE_ALIGN(hyp_percpu_size);
141 		ret = pkvm_create_mappings(start, end, PAGE_HYP);
142 		if (ret)
143 			return ret;
144 
145 		ret = pkvm_create_stack(params->stack_pa, &params->stack_hyp_va);
146 		if (ret)
147 			return ret;
148 	}
149 
150 	pkvm_create_host_sve_mappings();
151 
152 	/*
153 	 * Map the host sections RO in the hypervisor, but transfer the
154 	 * ownership from the host to the hypervisor itself to make sure they
155 	 * can't be donated or shared with another entity.
156 	 *
157 	 * The ownership transition requires matching changes in the host
158 	 * stage-2. This will be done later (see finalize_host_mappings()) once
159 	 * the hyp_vmemmap is addressable.
160 	 */
161 	prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED);
162 	ret = pkvm_create_mappings(&kvm_vgic_global_state,
163 				   &kvm_vgic_global_state + 1, prot);
164 	if (ret)
165 		return ret;
166 
167 	return 0;
168 }
169 
update_nvhe_init_params(void)170 static void update_nvhe_init_params(void)
171 {
172 	struct kvm_nvhe_init_params *params;
173 	unsigned long i;
174 
175 	for (i = 0; i < hyp_nr_cpus; i++) {
176 		params = per_cpu_ptr(&kvm_init_params, i);
177 		params->pgd_pa = __hyp_pa(pkvm_pgtable.pgd);
178 		dcache_clean_inval_poc((unsigned long)params,
179 				    (unsigned long)params + sizeof(*params));
180 	}
181 }
182 
hyp_zalloc_hyp_page(void * arg)183 static void *hyp_zalloc_hyp_page(void *arg)
184 {
185 	return hyp_alloc_pages(&hpool, 0);
186 }
187 
hpool_get_page(void * addr)188 static void hpool_get_page(void *addr)
189 {
190 	hyp_get_page(&hpool, addr);
191 }
192 
hpool_put_page(void * addr)193 static void hpool_put_page(void *addr)
194 {
195 	hyp_put_page(&hpool, addr);
196 }
197 
fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx * ctx,enum kvm_pgtable_walk_flags visit)198 static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
199 				     enum kvm_pgtable_walk_flags visit)
200 {
201 	enum kvm_pgtable_prot prot;
202 	enum pkvm_page_state state;
203 	phys_addr_t phys;
204 
205 	if (!kvm_pte_valid(ctx->old))
206 		return 0;
207 
208 	if (ctx->level != KVM_PGTABLE_LAST_LEVEL)
209 		return -EINVAL;
210 
211 	phys = kvm_pte_to_phys(ctx->old);
212 	if (!addr_is_memory(phys))
213 		return -EINVAL;
214 
215 	/*
216 	 * Adjust the host stage-2 mappings to match the ownership attributes
217 	 * configured in the hypervisor stage-1.
218 	 */
219 	state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(ctx->old));
220 	switch (state) {
221 	case PKVM_PAGE_OWNED:
222 		return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
223 	case PKVM_PAGE_SHARED_OWNED:
224 		prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_BORROWED);
225 		break;
226 	case PKVM_PAGE_SHARED_BORROWED:
227 		prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_OWNED);
228 		break;
229 	default:
230 		return -EINVAL;
231 	}
232 
233 	return host_stage2_idmap_locked(phys, PAGE_SIZE, prot);
234 }
235 
fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx * ctx,enum kvm_pgtable_walk_flags visit)236 static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx,
237 					 enum kvm_pgtable_walk_flags visit)
238 {
239 	/*
240 	 * Fix-up the refcount for the page-table pages as the early allocator
241 	 * was unable to access the hyp_vmemmap and so the buddy allocator has
242 	 * initialised the refcount to '1'.
243 	 */
244 	if (kvm_pte_valid(ctx->old))
245 		ctx->mm_ops->get_page(ctx->ptep);
246 
247 	return 0;
248 }
249 
fix_host_ownership(void)250 static int fix_host_ownership(void)
251 {
252 	struct kvm_pgtable_walker walker = {
253 		.cb	= fix_host_ownership_walker,
254 		.flags	= KVM_PGTABLE_WALK_LEAF,
255 	};
256 	int i, ret;
257 
258 	for (i = 0; i < hyp_memblock_nr; i++) {
259 		struct memblock_region *reg = &hyp_memory[i];
260 		u64 start = (u64)hyp_phys_to_virt(reg->base);
261 
262 		ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
263 		if (ret)
264 			return ret;
265 	}
266 
267 	return 0;
268 }
269 
fix_hyp_pgtable_refcnt(void)270 static int fix_hyp_pgtable_refcnt(void)
271 {
272 	struct kvm_pgtable_walker walker = {
273 		.cb	= fix_hyp_pgtable_refcnt_walker,
274 		.flags	= KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
275 		.arg	= pkvm_pgtable.mm_ops,
276 	};
277 
278 	return kvm_pgtable_walk(&pkvm_pgtable, 0, BIT(pkvm_pgtable.ia_bits),
279 				&walker);
280 }
281 
__pkvm_init_finalise(void)282 void __noreturn __pkvm_init_finalise(void)
283 {
284 	struct kvm_cpu_context *host_ctxt = host_data_ptr(host_ctxt);
285 	unsigned long nr_pages, reserved_pages, pfn;
286 	int ret;
287 
288 	/* Now that the vmemmap is backed, install the full-fledged allocator */
289 	pfn = hyp_virt_to_pfn(hyp_pgt_base);
290 	nr_pages = hyp_s1_pgtable_pages();
291 	reserved_pages = hyp_early_alloc_nr_used_pages();
292 	ret = hyp_pool_init(&hpool, pfn, nr_pages, reserved_pages);
293 	if (ret)
294 		goto out;
295 
296 	ret = kvm_host_prepare_stage2(host_s2_pgt_base);
297 	if (ret)
298 		goto out;
299 
300 	pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) {
301 		.zalloc_page = hyp_zalloc_hyp_page,
302 		.phys_to_virt = hyp_phys_to_virt,
303 		.virt_to_phys = hyp_virt_to_phys,
304 		.get_page = hpool_get_page,
305 		.put_page = hpool_put_page,
306 		.page_count = hyp_page_count,
307 	};
308 	pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;
309 
310 	ret = fix_host_ownership();
311 	if (ret)
312 		goto out;
313 
314 	ret = fix_hyp_pgtable_refcnt();
315 	if (ret)
316 		goto out;
317 
318 	ret = hyp_create_pcpu_fixmap();
319 	if (ret)
320 		goto out;
321 
322 	ret = hyp_ffa_init(ffa_proxy_pages);
323 	if (ret)
324 		goto out;
325 
326 	pkvm_hyp_vm_table_init(vm_table_base);
327 out:
328 	/*
329 	 * We tail-called to here from handle___pkvm_init() and will not return,
330 	 * so make sure to propagate the return value to the host.
331 	 */
332 	cpu_reg(host_ctxt, 1) = ret;
333 
334 	__host_enter(host_ctxt);
335 }
336 
__pkvm_init(phys_addr_t phys,unsigned long size,unsigned long nr_cpus,unsigned long * per_cpu_base,u32 hyp_va_bits)337 int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus,
338 		unsigned long *per_cpu_base, u32 hyp_va_bits)
339 {
340 	struct kvm_nvhe_init_params *params;
341 	void *virt = hyp_phys_to_virt(phys);
342 	typeof(__pkvm_init_switch_pgd) *fn;
343 	int ret;
344 
345 	BUG_ON(kvm_check_pvm_sysreg_table());
346 
347 	if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size))
348 		return -EINVAL;
349 
350 	hyp_spin_lock_init(&pkvm_pgd_lock);
351 	hyp_nr_cpus = nr_cpus;
352 
353 	ret = divide_memory_pool(virt, size);
354 	if (ret)
355 		return ret;
356 
357 	ret = recreate_hyp_mappings(phys, size, per_cpu_base, hyp_va_bits);
358 	if (ret)
359 		return ret;
360 
361 	update_nvhe_init_params();
362 
363 	/* Jump in the idmap page to switch to the new page-tables */
364 	params = this_cpu_ptr(&kvm_init_params);
365 	fn = (typeof(fn))__hyp_pa(__pkvm_init_switch_pgd);
366 	fn(params->pgd_pa, params->stack_hyp_va, __pkvm_init_finalise);
367 
368 	unreachable();
369 }
370