xref: /linux/arch/arm64/include/asm/kvm_pkvm.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 - Google LLC
4  * Author: Quentin Perret <qperret@google.com>
5  */
6 #ifndef __ARM64_KVM_PKVM_H__
7 #define __ARM64_KVM_PKVM_H__
8 
9 #include <linux/arm_ffa.h>
10 #include <linux/kvm_host.h>
11 #include <linux/memblock.h>
12 #include <linux/scatterlist.h>
13 #include <asm/kvm_pgtable.h>
14 
15 /* Maximum number of VMs that can co-exist under pKVM. */
16 #define KVM_MAX_PVMS 255
17 
18 #define HYP_MEMBLOCK_REGIONS 128
19 
20 int pkvm_init_host_vm(struct kvm *kvm);
21 int pkvm_create_hyp_vm(struct kvm *kvm);
22 bool pkvm_hyp_vm_is_created(struct kvm *kvm);
23 void pkvm_destroy_hyp_vm(struct kvm *kvm);
24 int pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu);
25 
26 /*
27  * Check whether the specific capability is allowed in pKVM.
28  *
29  * Certain features are allowed only for non-protected VMs in pKVM, which is why
30  * this takes the VM (kvm) as a parameter.
31  */
32 static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
33 {
34 	switch (ext) {
35 	case KVM_CAP_IRQCHIP:
36 	case KVM_CAP_ARM_PSCI:
37 	case KVM_CAP_ARM_PSCI_0_2:
38 	case KVM_CAP_NR_VCPUS:
39 	case KVM_CAP_MAX_VCPUS:
40 	case KVM_CAP_MAX_VCPU_ID:
41 	case KVM_CAP_MSI_DEVID:
42 	case KVM_CAP_ARM_VM_IPA_SIZE:
43 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
44 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
45 	case KVM_CAP_ONE_REG:
46 	case KVM_CAP_MP_STATE:
47 	case KVM_CAP_VCPU_EVENTS:
48 	case KVM_CAP_VCPU_ATTRIBUTES:
49 	case KVM_CAP_IMMEDIATE_EXIT:
50 	case KVM_CAP_IOEVENTFD:
51 	case KVM_CAP_IRQFD_RESAMPLE:
52 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
53 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
54 		return true;
55 	case KVM_CAP_ARM_MTE:
56 	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
57 	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
58 	case KVM_CAP_PRE_FAULT_MEMORY:
59 		return false;
60 	default:
61 		return !kvm || (kvm->arch.vm_flavor == VM_PKVM);
62 	}
63 }
64 
65 /*
66  * The vCPU features a protected VM may use: checked by the host at
67  * KVM_ARM_VCPU_INIT, applied by EL2 when the hyp VM is created.
68  */
69 static inline void kvm_pkvm_vcpu_allowed_features(struct kvm *kvm,
70 						  unsigned long *allowed)
71 {
72 	bitmap_zero(allowed, KVM_VCPU_MAX_FEATURES);
73 
74 	set_bit(KVM_ARM_VCPU_PSCI_0_2, allowed);
75 
76 	if (kvm_pkvm_ext_allowed(kvm, KVM_CAP_ARM_PMU_V3))
77 		set_bit(KVM_ARM_VCPU_PMU_V3, allowed);
78 
79 	if (kvm_pkvm_ext_allowed(kvm, KVM_CAP_ARM_PTRAUTH_ADDRESS))
80 		set_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, allowed);
81 
82 	if (kvm_pkvm_ext_allowed(kvm, KVM_CAP_ARM_PTRAUTH_GENERIC))
83 		set_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, allowed);
84 
85 	if (kvm_pkvm_ext_allowed(kvm, KVM_CAP_ARM_SVE))
86 		set_bit(KVM_ARM_VCPU_SVE, allowed);
87 }
88 
89 /*
90  * Check whether the KVM VM IOCTL is allowed in pKVM.
91  *
92  * Certain features are allowed only for non-protected VMs in pKVM, which is why
93  * this takes the VM (kvm) as a parameter.
94  */
95 static inline bool kvm_pkvm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
96 {
97 	long ext;
98 	int r;
99 
100 	r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
101 	if (r < 0)
102 		return false;
103 
104 	return kvm_pkvm_ext_allowed(kvm, ext);
105 }
106 
107 extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
108 extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
109 
110 static inline unsigned long
111 hyp_vmemmap_memblock_size(struct memblock_region *reg, size_t vmemmap_entry_size)
112 {
113 	unsigned long nr_pages = reg->size >> PAGE_SHIFT;
114 	unsigned long start, end;
115 
116 	start = (reg->base >> PAGE_SHIFT) * vmemmap_entry_size;
117 	end = start + nr_pages * vmemmap_entry_size;
118 	start = ALIGN_DOWN(start, PAGE_SIZE);
119 	end = ALIGN(end, PAGE_SIZE);
120 
121 	return end - start;
122 }
123 
124 static inline unsigned long hyp_vmemmap_pages(size_t vmemmap_entry_size)
125 {
126 	unsigned long res = 0, i;
127 
128 	for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
129 		res += hyp_vmemmap_memblock_size(&kvm_nvhe_sym(hyp_memory)[i],
130 						 vmemmap_entry_size);
131 	}
132 
133 	return res >> PAGE_SHIFT;
134 }
135 
136 static inline unsigned long hyp_vm_table_pages(void)
137 {
138 	return PAGE_ALIGN(KVM_MAX_PVMS * sizeof(void *)) >> PAGE_SHIFT;
139 }
140 
141 static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)
142 {
143 	unsigned long total = 0;
144 	int i;
145 
146 	/* Provision the worst case scenario */
147 	for (i = KVM_PGTABLE_FIRST_LEVEL; i <= KVM_PGTABLE_LAST_LEVEL; i++) {
148 		nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE);
149 		total += nr_pages;
150 	}
151 
152 	return total;
153 }
154 
155 static inline unsigned long __hyp_pgtable_total_pages(void)
156 {
157 	unsigned long res = 0, i;
158 
159 	/* Cover all of memory with page-granularity */
160 	for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
161 		struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i];
162 		res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT);
163 	}
164 
165 	return res;
166 }
167 
168 static inline unsigned long hyp_s1_pgtable_pages(void)
169 {
170 	unsigned long res;
171 
172 	res = __hyp_pgtable_total_pages();
173 
174 	/* Allow 1 GiB for private mappings */
175 	res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
176 
177 	return res;
178 }
179 
180 static inline unsigned long host_s2_pgtable_pages(void)
181 {
182 	unsigned long res;
183 
184 	/*
185 	 * Include an extra 16 pages to safely upper-bound the worst case of
186 	 * concatenated pgds.
187 	 */
188 	res = __hyp_pgtable_total_pages() + 16;
189 
190 	/* Allow 1 GiB for MMIO mappings */
191 	res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
192 
193 	return res;
194 }
195 
196 #ifdef CONFIG_NVHE_EL2_DEBUG
197 static inline unsigned long pkvm_selftest_pages(void) { return 32; }
198 #else
199 static inline unsigned long pkvm_selftest_pages(void) { return 0; }
200 #endif
201 
202 #define KVM_FFA_MBOX_NR_PAGES	1
203 
204 static inline unsigned long hyp_ffa_proxy_pages(void)
205 {
206 	size_t desc_max;
207 
208 	/*
209 	 * The hypervisor FFA proxy needs enough memory to buffer a fragmented
210 	 * descriptor returned from EL3 in response to a RETRIEVE_REQ call.
211 	 */
212 	desc_max = sizeof(struct ffa_mem_region) +
213 		   sizeof(struct ffa_mem_region_attributes) +
214 		   sizeof(struct ffa_composite_mem_region) +
215 		   SG_MAX_SEGMENTS * sizeof(struct ffa_mem_region_addr_range);
216 
217 	/* Plus a page each for the hypervisor's RX and TX mailboxes. */
218 	return (2 * KVM_FFA_MBOX_NR_PAGES) + DIV_ROUND_UP(desc_max, PAGE_SIZE);
219 }
220 
221 static inline size_t pkvm_host_sve_state_size(void)
222 {
223 	if (!system_supports_sve())
224 		return 0;
225 
226 	return SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl));
227 }
228 
229 struct pkvm_mapping {
230 	struct rb_node node;
231 	u64 gfn;
232 	u64 pfn;
233 	struct {
234 		u64 nr_pages:48;
235 		u64 nc:1;
236 	};
237 	u64 __subtree_last;	/* Internal member for interval tree */
238 };
239 
240 enum pkvm_hyp_req_type {
241 	PKVM_HYP_NO_REQ = 0,
242 	PKVM_HYP_REQ_HYP_ALLOC,
243 	PKVM_HYP_REQ_HYP_ALLOC_SELFTEST,
244 	__PKVM_HYP_REQ_TYPE_MAX,
245 };
246 
247 #define PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX \
248 	(sizeof(struct arm_smccc_res) - offsetof(struct arm_smccc_res, a2) - 1)
249 
250 struct pkvm_hyp_req {
251 	u8 type;
252 	union {
253 		struct {
254 			u32	nr_pages;
255 		} mem;
256 		struct {
257 			/* Helper for SMCCC encoding/decoding */
258 			u8	args[PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX];
259 		} args;
260 	};
261 };
262 
263 static inline size_t pkvm_hyp_req_arg_size(u8 type)
264 {
265 	struct pkvm_hyp_req *req;
266 
267 	switch (type) {
268 	case PKVM_HYP_NO_REQ:
269 		return 0;
270 	case PKVM_HYP_REQ_HYP_ALLOC:
271 	case PKVM_HYP_REQ_HYP_ALLOC_SELFTEST:
272 		return sizeof(req->mem);
273 	default:
274 		WARN_ON(1);
275 	}
276 
277 	return 0;
278 }
279 
280 /* Encode the pending pkvm_hyp_req type into the SMCCC args */
281 static inline void
282 pkvm_hyp_req_to_smccc(struct kvm_cpu_context *host_ctxt, struct pkvm_hyp_req *req)
283 {
284 	u8 *dst, type = req->type;
285 	size_t size;
286 
287 	if (type == PKVM_HYP_NO_REQ || type >= __PKVM_HYP_REQ_TYPE_MAX) {
288 		host_ctxt->regs.regs[2] = 0;
289 		return;
290 	}
291 
292 	size = pkvm_hyp_req_arg_size(type);
293 	if (WARN_ON(size > PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX))
294 		return;
295 
296 	dst = (u8 *)&host_ctxt->regs.regs[2];
297 	*dst = type;
298 
299 	memcpy(dst + 1, &req->args, size);
300 }
301 
302 /* Return true if a pkvm_hyp_req has been decoded from the SMCCC args */
303 static inline bool smccc_to_pkvm_hyp_req(struct pkvm_hyp_req *req, struct arm_smccc_res *res)
304 {
305 	u8 *src = (u8 *)res + offsetof(struct arm_smccc_res, a2);
306 	u8 type = *src;
307 
308 	if (type == PKVM_HYP_NO_REQ || type >= __PKVM_HYP_REQ_TYPE_MAX)
309 		return false;
310 
311 	req->type = type;
312 	memcpy(&req->args, src + 1, pkvm_hyp_req_arg_size(type));
313 
314 	return true;
315 }
316 
317 int __pkvm_handle_smccc_req(struct arm_smccc_res *res);
318 
319 /**
320  * pkvm_call_hyp_req() - Issue an HVC that can return hypervisor requests
321  * @f: Hypervisor function symbol to call.
322  * @...: Arguments to pass to the hypercall.
323  *
324  * Re-issue an HVC and process any pending hypervisor request until completion
325  * or error.
326  *
327  * Only use this helper for HVCs whose hypervisor handlers format their return
328  * registers with pkvm_hyp_req_to_smccc().
329  *
330  * Return: Result of the hypercall or a negative error if the hyp request
331  * handling failed.
332  */
333 #define pkvm_call_hyp_req(f, ...)								\
334 ({												\
335 	struct arm_smccc_res __res;								\
336 	int __ret;										\
337 	do {											\
338 		__ret = -1;									\
339 		arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f), ##__VA_ARGS__, &__res);		\
340 		if (WARN_ON(__res.a0 != SMCCC_RET_SUCCESS))					\
341 			break;									\
342 		__ret = __res.a1;								\
343 		if (!__ret)									\
344 			break;									\
345 		__ret = __pkvm_handle_smccc_req(&__res);					\
346 	} while (!__ret);									\
347 	__ret;											\
348 })
349 
350 int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
351 			     struct kvm_pgtable_mm_ops *mm_ops);
352 void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
353 					u64 addr, u64 size);
354 void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt);
355 int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
356 			    enum kvm_pgtable_prot prot, void *mc,
357 			    enum kvm_pgtable_walk_flags flags);
358 int pkvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
359 int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);
360 int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size);
361 bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 size, bool mkold);
362 int pkvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr, enum kvm_pgtable_prot prot,
363 				    enum kvm_pgtable_walk_flags flags);
364 void pkvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr,
365 				 enum kvm_pgtable_walk_flags flags);
366 int pkvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
367 			      struct kvm_mmu_memory_cache *mc);
368 void pkvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, s8 level);
369 kvm_pte_t *pkvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt, u64 phys, s8 level,
370 					       enum kvm_pgtable_prot prot, void *mc,
371 					       bool force_pte);
372 #endif	/* __ARM64_KVM_PKVM_H__ */
373