setup.c 8.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2020 Google LLC
 * Author: Quentin Perret <qperret@google.com>
 */

#include <linux/kvm_host.h>
#include <asm/kvm_hyp.h>
#include <asm/kvm_mmu.h>
#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 17
#include <nvhe/gfp.h>
#include <nvhe/memory.h>
18
#include <nvhe/mem_protect.h>
19
#include <nvhe/mm.h>
20
#include <nvhe/pkvm.h>
21 22 23 24 25 26 27 28
#include <nvhe/trap_handler.h>

unsigned long hyp_nr_cpus;

#define hyp_percpu_size ((unsigned long)__per_cpu_end - \
			 (unsigned long)__per_cpu_start)

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 36 37

static int divide_memory_pool(void *virt, unsigned long size)
{
38
	unsigned long nr_pages;
39 40 41

	hyp_early_alloc_init(virt, size);

42
	nr_pages = hyp_vmemmap_pages(sizeof(struct hyp_page));
43 44 45 46
	vmemmap_base = hyp_early_alloc_contig(nr_pages);
	if (!vmemmap_base)
		return -ENOMEM;

47 48 49 50 51
	nr_pages = hyp_vm_table_pages();
	vm_table_base = hyp_early_alloc_contig(nr_pages);
	if (!vm_table_base)
		return -ENOMEM;

52 53 54 55 56
	nr_pages = hyp_s1_pgtable_pages();
	hyp_pgt_base = hyp_early_alloc_contig(nr_pages);
	if (!hyp_pgt_base)
		return -ENOMEM;

57 58 59
	nr_pages = host_s2_pgtable_pages();
	host_s2_pgt_base = hyp_early_alloc_contig(nr_pages);
	if (!host_s2_pgt_base)
60 61
		return -ENOMEM;

62 63 64 65 66
	nr_pages = hyp_ffa_proxy_pages();
	ffa_proxy_pages = hyp_early_alloc_contig(nr_pages);
	if (!ffa_proxy_pages)
		return -ENOMEM;

67 68 69 70 71 72 73 74 75
	return 0;
}

static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
				 unsigned long *per_cpu_base,
				 u32 hyp_va_bits)
{
	void *start, *end, *virt = hyp_phys_to_virt(phys);
	unsigned long pgt_size = hyp_s1_pgtable_pages() << PAGE_SHIFT;
76
	enum kvm_pgtable_prot prot;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	int ret, i;

	/* Recreate the hyp page-table using the early page allocator */
	hyp_early_alloc_init(hyp_pgt_base, pgt_size);
	ret = kvm_pgtable_hyp_init(&pkvm_pgtable, hyp_va_bits,
				   &hyp_early_alloc_mm_ops);
	if (ret)
		return ret;

	ret = hyp_create_idmap(hyp_va_bits);
	if (ret)
		return ret;

	ret = hyp_map_vectors();
	if (ret)
		return ret;

94
	ret = hyp_back_vmemmap(hyp_virt_to_phys(vmemmap_base));
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	if (ret)
		return ret;

	ret = pkvm_create_mappings(__hyp_text_start, __hyp_text_end, PAGE_HYP_EXEC);
	if (ret)
		return ret;

	ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO);
	if (ret)
		return ret;

	ret = pkvm_create_mappings(__hyp_bss_start, __hyp_bss_end, PAGE_HYP);
	if (ret)
		return ret;

	ret = pkvm_create_mappings(virt, virt + size, PAGE_HYP);
	if (ret)
		return ret;

	for (i = 0; i < hyp_nr_cpus; i++) {
115 116
		struct kvm_nvhe_init_params *params = per_cpu_ptr(&kvm_init_params, i);

117 118 119 120 121 122
		start = (void *)kern_hyp_va(per_cpu_base[i]);
		end = start + PAGE_ALIGN(hyp_percpu_size);
		ret = pkvm_create_mappings(start, end, PAGE_HYP);
		if (ret)
			return ret;

123
		ret = pkvm_create_stack(params->stack_pa, &params->stack_hyp_va);
124 125
		if (ret)
			return ret;
126 127
	}

128
	/*
129 130 131
	 * Map the host sections RO in the hypervisor, but transfer the
	 * ownership from the host to the hypervisor itself to make sure they
	 * can't be donated or shared with another entity.
132 133 134 135 136 137
	 *
	 * The ownership transition requires matching changes in the host
	 * stage-2. This will be done later (see finalize_host_mappings()) once
	 * the hyp_vmemmap is addressable.
	 */
	prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED);
138 139
	ret = pkvm_create_mappings(&kvm_vgic_global_state,
				   &kvm_vgic_global_state + 1, prot);
140 141 142
	if (ret)
		return ret;

143 144 145 146 147 148 149 150 151 152 153
	return 0;
}

static void update_nvhe_init_params(void)
{
	struct kvm_nvhe_init_params *params;
	unsigned long i;

	for (i = 0; i < hyp_nr_cpus; i++) {
		params = per_cpu_ptr(&kvm_init_params, i);
		params->pgd_pa = __hyp_pa(pkvm_pgtable.pgd);
154
		dcache_clean_inval_poc((unsigned long)params,
155
				    (unsigned long)params + sizeof(*params));
156 157 158 159 160 161 162 163
	}
}

static void *hyp_zalloc_hyp_page(void *arg)
{
	return hyp_alloc_pages(&hpool, 0);
}

164 165 166 167 168 169 170 171 172 173
static void hpool_get_page(void *addr)
{
	hyp_get_page(&hpool, addr);
}

static void hpool_put_page(void *addr)
{
	hyp_put_page(&hpool, addr);
}

174 175
static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
				     enum kvm_pgtable_walk_flags visit)
176 177 178 179 180
{
	enum kvm_pgtable_prot prot;
	enum pkvm_page_state state;
	phys_addr_t phys;

181
	if (!kvm_pte_valid(ctx->old))
182 183
		return 0;

184
	if (ctx->level != KVM_PGTABLE_LAST_LEVEL)
185 186
		return -EINVAL;

187
	phys = kvm_pte_to_phys(ctx->old);
188
	if (!addr_is_memory(phys))
189
		return -EINVAL;
190 191 192 193 194

	/*
	 * Adjust the host stage-2 mappings to match the ownership attributes
	 * configured in the hypervisor stage-1.
	 */
195
	state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(ctx->old));
196 197
	switch (state) {
	case PKVM_PAGE_OWNED:
198
		return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
199 200 201 202 203 204 205 206 207 208 209 210 211
	case PKVM_PAGE_SHARED_OWNED:
		prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_BORROWED);
		break;
	case PKVM_PAGE_SHARED_BORROWED:
		prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_OWNED);
		break;
	default:
		return -EINVAL;
	}

	return host_stage2_idmap_locked(phys, PAGE_SIZE, prot);
}

212 213
static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx,
					 enum kvm_pgtable_walk_flags visit)
214 215 216 217 218 219
{
	/*
	 * Fix-up the refcount for the page-table pages as the early allocator
	 * was unable to access the hyp_vmemmap and so the buddy allocator has
	 * initialised the refcount to '1'.
	 */
220 221
	if (kvm_pte_valid(ctx->old))
		ctx->mm_ops->get_page(ctx->ptep);
222 223 224 225 226

	return 0;
}

static int fix_host_ownership(void)
227 228
{
	struct kvm_pgtable_walker walker = {
229 230
		.cb	= fix_host_ownership_walker,
		.flags	= KVM_PGTABLE_WALK_LEAF,
231
	};
232 233 234 235 236 237 238 239 240 241
	int i, ret;

	for (i = 0; i < hyp_memblock_nr; i++) {
		struct memblock_region *reg = &hyp_memory[i];
		u64 start = (u64)hyp_phys_to_virt(reg->base);

		ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
		if (ret)
			return ret;
	}
242

243
	return 0;
244 245
}

246 247 248 249 250 251 252 253 254 255 256 257
static int fix_hyp_pgtable_refcnt(void)
{
	struct kvm_pgtable_walker walker = {
		.cb	= fix_hyp_pgtable_refcnt_walker,
		.flags	= KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
		.arg	= pkvm_pgtable.mm_ops,
	};

	return kvm_pgtable_walk(&pkvm_pgtable, 0, BIT(pkvm_pgtable.ia_bits),
				&walker);
}

258 259
void __noreturn __pkvm_init_finalise(void)
{
260
	struct kvm_cpu_context *host_ctxt = host_data_ptr(host_ctxt);
261 262 263 264 265 266 267 268 269 270 271
	unsigned long nr_pages, reserved_pages, pfn;
	int ret;

	/* Now that the vmemmap is backed, install the full-fledged allocator */
	pfn = hyp_virt_to_pfn(hyp_pgt_base);
	nr_pages = hyp_s1_pgtable_pages();
	reserved_pages = hyp_early_alloc_nr_used_pages();
	ret = hyp_pool_init(&hpool, pfn, nr_pages, reserved_pages);
	if (ret)
		goto out;

272
	ret = kvm_host_prepare_stage2(host_s2_pgt_base);
273 274 275
	if (ret)
		goto out;

276 277 278 279
	pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) {
		.zalloc_page = hyp_zalloc_hyp_page,
		.phys_to_virt = hyp_phys_to_virt,
		.virt_to_phys = hyp_virt_to_phys,
280 281
		.get_page = hpool_get_page,
		.put_page = hpool_put_page,
282
		.page_count = hyp_page_count,
283 284 285
	};
	pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;

286 287 288 289 290
	ret = fix_host_ownership();
	if (ret)
		goto out;

	ret = fix_hyp_pgtable_refcnt();
291 292 293
	if (ret)
		goto out;

294
	ret = hyp_create_pcpu_fixmap();
295 296 297
	if (ret)
		goto out;

298
	ret = hyp_ffa_init(ffa_proxy_pages);
299 300 301
	if (ret)
		goto out;

302
	pkvm_hyp_vm_table_init(vm_table_base);
303
	pkvm_host_fpsimd_state_init();
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
out:
	/*
	 * We tail-called to here from handle___pkvm_init() and will not return,
	 * so make sure to propagate the return value to the host.
	 */
	cpu_reg(host_ctxt, 1) = ret;

	__host_enter(host_ctxt);
}

int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long nr_cpus,
		unsigned long *per_cpu_base, u32 hyp_va_bits)
{
	struct kvm_nvhe_init_params *params;
	void *virt = hyp_phys_to_virt(phys);
	void (*fn)(phys_addr_t params_pa, void *finalize_fn_va);
	int ret;

322 323
	BUG_ON(kvm_check_pvm_sysreg_table());

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size))
		return -EINVAL;

	hyp_spin_lock_init(&pkvm_pgd_lock);
	hyp_nr_cpus = nr_cpus;

	ret = divide_memory_pool(virt, size);
	if (ret)
		return ret;

	ret = recreate_hyp_mappings(phys, size, per_cpu_base, hyp_va_bits);
	if (ret)
		return ret;

	update_nvhe_init_params();

	/* Jump in the idmap page to switch to the new page-tables */
	params = this_cpu_ptr(&kvm_init_params);
	fn = (typeof(fn))__hyp_pa(__pkvm_init_switch_pgd);
	fn(__hyp_pa(params), __pkvm_init_finalise);

	unreachable();
}