xref: /linux/arch/arm64/include/asm/kvm_pgtable.h (revision 43db1111073049220381944af4a3b8a5400eda71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Google LLC
4  * Author: Will Deacon <will@kernel.org>
5  */
6 
7 #ifndef __ARM64_KVM_PGTABLE_H__
8 #define __ARM64_KVM_PGTABLE_H__
9 
10 #include <linux/bits.h>
11 #include <linux/kvm_host.h>
12 #include <linux/types.h>
13 
14 #define KVM_PGTABLE_FIRST_LEVEL		-1
15 #define KVM_PGTABLE_LAST_LEVEL		3
16 
17 /*
18  * The largest supported block sizes for KVM (no 52-bit PA support):
19  *  - 4K (level 1):	1GB
20  *  - 16K (level 2):	32MB
21  *  - 64K (level 2):	512MB
22  */
23 #ifdef CONFIG_ARM64_4K_PAGES
24 #define KVM_PGTABLE_MIN_BLOCK_LEVEL	1
25 #else
26 #define KVM_PGTABLE_MIN_BLOCK_LEVEL	2
27 #endif
28 
29 #define kvm_lpa2_is_enabled()		system_supports_lpa2()
30 
kvm_get_parange_max(void)31 static inline u64 kvm_get_parange_max(void)
32 {
33 	if (kvm_lpa2_is_enabled() ||
34 	   (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && PAGE_SHIFT == 16))
35 		return ID_AA64MMFR0_EL1_PARANGE_52;
36 	else
37 		return ID_AA64MMFR0_EL1_PARANGE_48;
38 }
39 
kvm_get_parange(u64 mmfr0)40 static inline u64 kvm_get_parange(u64 mmfr0)
41 {
42 	u64 parange_max = kvm_get_parange_max();
43 	u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
44 				ID_AA64MMFR0_EL1_PARANGE_SHIFT);
45 	if (parange > parange_max)
46 		parange = parange_max;
47 
48 	return parange;
49 }
50 
51 typedef u64 kvm_pte_t;
52 
53 #define KVM_PTE_VALID			BIT(0)
54 
55 #define KVM_PTE_ADDR_MASK		GENMASK(47, PAGE_SHIFT)
56 #define KVM_PTE_ADDR_51_48		GENMASK(15, 12)
57 #define KVM_PTE_ADDR_MASK_LPA2		GENMASK(49, PAGE_SHIFT)
58 #define KVM_PTE_ADDR_51_50_LPA2		GENMASK(9, 8)
59 
60 #define KVM_PHYS_INVALID		(-1ULL)
61 
62 #define KVM_PTE_TYPE			BIT(1)
63 #define KVM_PTE_TYPE_BLOCK		0
64 #define KVM_PTE_TYPE_PAGE		1
65 #define KVM_PTE_TYPE_TABLE		1
66 
67 #define KVM_PTE_LEAF_ATTR_LO		GENMASK(11, 2)
68 
69 #define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX	GENMASK(4, 2)
70 #define KVM_PTE_LEAF_ATTR_LO_S1_AP	GENMASK(7, 6)
71 #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO		\
72 	({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; })
73 #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW		\
74 	({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; })
75 #define KVM_PTE_LEAF_ATTR_LO_S1_SH	GENMASK(9, 8)
76 #define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS	3
77 #define KVM_PTE_LEAF_ATTR_LO_S1_AF	BIT(10)
78 
79 #define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR	GENMASK(5, 2)
80 #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R	BIT(6)
81 #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W	BIT(7)
82 #define KVM_PTE_LEAF_ATTR_LO_S2_SH	GENMASK(9, 8)
83 #define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS	3
84 #define KVM_PTE_LEAF_ATTR_LO_S2_AF	BIT(10)
85 
86 #define KVM_PTE_LEAF_ATTR_HI		GENMASK(63, 50)
87 
88 #define KVM_PTE_LEAF_ATTR_HI_SW		GENMASK(58, 55)
89 
90 #define KVM_PTE_LEAF_ATTR_HI_S1_XN	BIT(54)
91 
92 #define KVM_PTE_LEAF_ATTR_HI_S2_XN	BIT(54)
93 
94 #define KVM_PTE_LEAF_ATTR_HI_S1_GP	BIT(50)
95 
96 #define KVM_PTE_LEAF_ATTR_S2_PERMS	(KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
97 					 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
98 					 KVM_PTE_LEAF_ATTR_HI_S2_XN)
99 
100 #define KVM_INVALID_PTE_OWNER_MASK	GENMASK(9, 2)
101 #define KVM_MAX_OWNER_ID		1
102 
103 /*
104  * Used to indicate a pte for which a 'break-before-make' sequence is in
105  * progress.
106  */
107 #define KVM_INVALID_PTE_LOCKED		BIT(10)
108 
kvm_pte_valid(kvm_pte_t pte)109 static inline bool kvm_pte_valid(kvm_pte_t pte)
110 {
111 	return pte & KVM_PTE_VALID;
112 }
113 
kvm_pte_to_phys(kvm_pte_t pte)114 static inline u64 kvm_pte_to_phys(kvm_pte_t pte)
115 {
116 	u64 pa;
117 
118 	if (kvm_lpa2_is_enabled()) {
119 		pa = pte & KVM_PTE_ADDR_MASK_LPA2;
120 		pa |= FIELD_GET(KVM_PTE_ADDR_51_50_LPA2, pte) << 50;
121 	} else {
122 		pa = pte & KVM_PTE_ADDR_MASK;
123 		if (PAGE_SHIFT == 16)
124 			pa |= FIELD_GET(KVM_PTE_ADDR_51_48, pte) << 48;
125 	}
126 
127 	return pa;
128 }
129 
kvm_phys_to_pte(u64 pa)130 static inline kvm_pte_t kvm_phys_to_pte(u64 pa)
131 {
132 	kvm_pte_t pte;
133 
134 	if (kvm_lpa2_is_enabled()) {
135 		pte = pa & KVM_PTE_ADDR_MASK_LPA2;
136 		pa &= GENMASK(51, 50);
137 		pte |= FIELD_PREP(KVM_PTE_ADDR_51_50_LPA2, pa >> 50);
138 	} else {
139 		pte = pa & KVM_PTE_ADDR_MASK;
140 		if (PAGE_SHIFT == 16) {
141 			pa &= GENMASK(51, 48);
142 			pte |= FIELD_PREP(KVM_PTE_ADDR_51_48, pa >> 48);
143 		}
144 	}
145 
146 	return pte;
147 }
148 
kvm_pte_to_pfn(kvm_pte_t pte)149 static inline kvm_pfn_t kvm_pte_to_pfn(kvm_pte_t pte)
150 {
151 	return __phys_to_pfn(kvm_pte_to_phys(pte));
152 }
153 
kvm_granule_shift(s8 level)154 static inline u64 kvm_granule_shift(s8 level)
155 {
156 	/* Assumes KVM_PGTABLE_LAST_LEVEL is 3 */
157 	return ARM64_HW_PGTABLE_LEVEL_SHIFT(level);
158 }
159 
kvm_granule_size(s8 level)160 static inline u64 kvm_granule_size(s8 level)
161 {
162 	return BIT(kvm_granule_shift(level));
163 }
164 
kvm_level_supports_block_mapping(s8 level)165 static inline bool kvm_level_supports_block_mapping(s8 level)
166 {
167 	return level >= KVM_PGTABLE_MIN_BLOCK_LEVEL;
168 }
169 
kvm_supported_block_sizes(void)170 static inline u32 kvm_supported_block_sizes(void)
171 {
172 	s8 level = KVM_PGTABLE_MIN_BLOCK_LEVEL;
173 	u32 r = 0;
174 
175 	for (; level <= KVM_PGTABLE_LAST_LEVEL; level++)
176 		r |= BIT(kvm_granule_shift(level));
177 
178 	return r;
179 }
180 
kvm_is_block_size_supported(u64 size)181 static inline bool kvm_is_block_size_supported(u64 size)
182 {
183 	bool is_power_of_two = IS_ALIGNED(size, size);
184 
185 	return is_power_of_two && (size & kvm_supported_block_sizes());
186 }
187 
188 /**
189  * struct kvm_pgtable_mm_ops - Memory management callbacks.
190  * @zalloc_page:		Allocate a single zeroed memory page.
191  *				The @arg parameter can be used by the walker
192  *				to pass a memcache. The initial refcount of
193  *				the page is 1.
194  * @zalloc_pages_exact:		Allocate an exact number of zeroed memory pages.
195  *				The @size parameter is in bytes, and is rounded
196  *				up to the next page boundary. The resulting
197  *				allocation is physically contiguous.
198  * @free_pages_exact:		Free an exact number of memory pages previously
199  *				allocated by zalloc_pages_exact.
200  * @free_unlinked_table:	Free an unlinked paging structure by unlinking and
201  *				dropping references.
202  * @get_page:			Increment the refcount on a page.
203  * @put_page:			Decrement the refcount on a page. When the
204  *				refcount reaches 0 the page is automatically
205  *				freed.
206  * @page_count:			Return the refcount of a page.
207  * @phys_to_virt:		Convert a physical address into a virtual
208  *				address	mapped in the current context.
209  * @virt_to_phys:		Convert a virtual address mapped in the current
210  *				context into a physical address.
211  * @dcache_clean_inval_poc:	Clean and invalidate the data cache to the PoC
212  *				for the	specified memory address range.
213  * @icache_inval_pou:		Invalidate the instruction cache to the PoU
214  *				for the specified memory address range.
215  */
216 struct kvm_pgtable_mm_ops {
217 	void*		(*zalloc_page)(void *arg);
218 	void*		(*zalloc_pages_exact)(size_t size);
219 	void		(*free_pages_exact)(void *addr, size_t size);
220 	void		(*free_unlinked_table)(void *addr, s8 level);
221 	void		(*get_page)(void *addr);
222 	void		(*put_page)(void *addr);
223 	int		(*page_count)(void *addr);
224 	void*		(*phys_to_virt)(phys_addr_t phys);
225 	phys_addr_t	(*virt_to_phys)(void *addr);
226 	void		(*dcache_clean_inval_poc)(void *addr, size_t size);
227 	void		(*icache_inval_pou)(void *addr, size_t size);
228 };
229 
230 /**
231  * enum kvm_pgtable_stage2_flags - Stage-2 page-table flags.
232  * @KVM_PGTABLE_S2_NOFWB:	Don't enforce Normal-WB even if the CPUs have
233  *				ARM64_HAS_STAGE2_FWB.
234  * @KVM_PGTABLE_S2_IDMAP:	Only use identity mappings.
235  */
236 enum kvm_pgtable_stage2_flags {
237 	KVM_PGTABLE_S2_NOFWB			= BIT(0),
238 	KVM_PGTABLE_S2_IDMAP			= BIT(1),
239 };
240 
241 /**
242  * enum kvm_pgtable_prot - Page-table permissions and attributes.
243  * @KVM_PGTABLE_PROT_X:		Execute permission.
244  * @KVM_PGTABLE_PROT_W:		Write permission.
245  * @KVM_PGTABLE_PROT_R:		Read permission.
246  * @KVM_PGTABLE_PROT_DEVICE:	Device attributes.
247  * @KVM_PGTABLE_PROT_NORMAL_NC:	Normal noncacheable attributes.
248  * @KVM_PGTABLE_PROT_SW0:	Software bit 0.
249  * @KVM_PGTABLE_PROT_SW1:	Software bit 1.
250  * @KVM_PGTABLE_PROT_SW2:	Software bit 2.
251  * @KVM_PGTABLE_PROT_SW3:	Software bit 3.
252  */
253 enum kvm_pgtable_prot {
254 	KVM_PGTABLE_PROT_X			= BIT(0),
255 	KVM_PGTABLE_PROT_W			= BIT(1),
256 	KVM_PGTABLE_PROT_R			= BIT(2),
257 
258 	KVM_PGTABLE_PROT_DEVICE			= BIT(3),
259 	KVM_PGTABLE_PROT_NORMAL_NC		= BIT(4),
260 
261 	KVM_PGTABLE_PROT_SW0			= BIT(55),
262 	KVM_PGTABLE_PROT_SW1			= BIT(56),
263 	KVM_PGTABLE_PROT_SW2			= BIT(57),
264 	KVM_PGTABLE_PROT_SW3			= BIT(58),
265 };
266 
267 #define KVM_PGTABLE_PROT_RW	(KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W)
268 #define KVM_PGTABLE_PROT_RWX	(KVM_PGTABLE_PROT_RW | KVM_PGTABLE_PROT_X)
269 
270 #define PKVM_HOST_MEM_PROT	KVM_PGTABLE_PROT_RWX
271 #define PKVM_HOST_MMIO_PROT	KVM_PGTABLE_PROT_RW
272 
273 #define PAGE_HYP		KVM_PGTABLE_PROT_RW
274 #define PAGE_HYP_EXEC		(KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_X)
275 #define PAGE_HYP_RO		(KVM_PGTABLE_PROT_R)
276 #define PAGE_HYP_DEVICE		(PAGE_HYP | KVM_PGTABLE_PROT_DEVICE)
277 
278 typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,
279 					   enum kvm_pgtable_prot prot);
280 
281 /**
282  * enum kvm_pgtable_walk_flags - Flags to control a depth-first page-table walk.
283  * @KVM_PGTABLE_WALK_LEAF:		Visit leaf entries, including invalid
284  *					entries.
285  * @KVM_PGTABLE_WALK_TABLE_PRE:		Visit table entries before their
286  *					children.
287  * @KVM_PGTABLE_WALK_TABLE_POST:	Visit table entries after their
288  *					children.
289  * @KVM_PGTABLE_WALK_SHARED:		Indicates the page-tables may be shared
290  *					with other software walkers.
291  * @KVM_PGTABLE_WALK_HANDLE_FAULT:	Indicates the page-table walk was
292  *					invoked from a fault handler.
293  * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI:	Visit and update table entries
294  *					without Break-before-make's
295  *					TLB invalidation.
296  * @KVM_PGTABLE_WALK_SKIP_CMO:		Visit and update table entries
297  *					without Cache maintenance
298  *					operations required.
299  */
300 enum kvm_pgtable_walk_flags {
301 	KVM_PGTABLE_WALK_LEAF			= BIT(0),
302 	KVM_PGTABLE_WALK_TABLE_PRE		= BIT(1),
303 	KVM_PGTABLE_WALK_TABLE_POST		= BIT(2),
304 	KVM_PGTABLE_WALK_SHARED			= BIT(3),
305 	KVM_PGTABLE_WALK_HANDLE_FAULT		= BIT(4),
306 	KVM_PGTABLE_WALK_SKIP_BBM_TLBI		= BIT(5),
307 	KVM_PGTABLE_WALK_SKIP_CMO		= BIT(6),
308 };
309 
310 struct kvm_pgtable_visit_ctx {
311 	kvm_pte_t				*ptep;
312 	kvm_pte_t				old;
313 	void					*arg;
314 	struct kvm_pgtable_mm_ops		*mm_ops;
315 	u64					start;
316 	u64					addr;
317 	u64					end;
318 	s8					level;
319 	enum kvm_pgtable_walk_flags		flags;
320 };
321 
322 typedef int (*kvm_pgtable_visitor_fn_t)(const struct kvm_pgtable_visit_ctx *ctx,
323 					enum kvm_pgtable_walk_flags visit);
324 
kvm_pgtable_walk_shared(const struct kvm_pgtable_visit_ctx * ctx)325 static inline bool kvm_pgtable_walk_shared(const struct kvm_pgtable_visit_ctx *ctx)
326 {
327 	return ctx->flags & KVM_PGTABLE_WALK_SHARED;
328 }
329 
330 /**
331  * struct kvm_pgtable_walker - Hook into a page-table walk.
332  * @cb:		Callback function to invoke during the walk.
333  * @arg:	Argument passed to the callback function.
334  * @flags:	Bitwise-OR of flags to identify the entry types on which to
335  *		invoke the callback function.
336  */
337 struct kvm_pgtable_walker {
338 	const kvm_pgtable_visitor_fn_t		cb;
339 	void * const				arg;
340 	const enum kvm_pgtable_walk_flags	flags;
341 };
342 
343 /*
344  * RCU cannot be used in a non-kernel context such as the hyp. As such, page
345  * table walkers used in hyp do not call into RCU and instead use other
346  * synchronization mechanisms (such as a spinlock).
347  */
348 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
349 
350 typedef kvm_pte_t *kvm_pteref_t;
351 
kvm_dereference_pteref(struct kvm_pgtable_walker * walker,kvm_pteref_t pteref)352 static inline kvm_pte_t *kvm_dereference_pteref(struct kvm_pgtable_walker *walker,
353 						kvm_pteref_t pteref)
354 {
355 	return pteref;
356 }
357 
kvm_pgtable_walk_begin(struct kvm_pgtable_walker * walker)358 static inline int kvm_pgtable_walk_begin(struct kvm_pgtable_walker *walker)
359 {
360 	/*
361 	 * Due to the lack of RCU (or a similar protection scheme), only
362 	 * non-shared table walkers are allowed in the hypervisor.
363 	 */
364 	if (walker->flags & KVM_PGTABLE_WALK_SHARED)
365 		return -EPERM;
366 
367 	return 0;
368 }
369 
kvm_pgtable_walk_end(struct kvm_pgtable_walker * walker)370 static inline void kvm_pgtable_walk_end(struct kvm_pgtable_walker *walker) {}
371 
kvm_pgtable_walk_lock_held(void)372 static inline bool kvm_pgtable_walk_lock_held(void)
373 {
374 	return true;
375 }
376 
377 #else
378 
379 typedef kvm_pte_t __rcu *kvm_pteref_t;
380 
kvm_dereference_pteref(struct kvm_pgtable_walker * walker,kvm_pteref_t pteref)381 static inline kvm_pte_t *kvm_dereference_pteref(struct kvm_pgtable_walker *walker,
382 						kvm_pteref_t pteref)
383 {
384 	return rcu_dereference_check(pteref, !(walker->flags & KVM_PGTABLE_WALK_SHARED));
385 }
386 
kvm_pgtable_walk_begin(struct kvm_pgtable_walker * walker)387 static inline int kvm_pgtable_walk_begin(struct kvm_pgtable_walker *walker)
388 {
389 	if (walker->flags & KVM_PGTABLE_WALK_SHARED)
390 		rcu_read_lock();
391 
392 	return 0;
393 }
394 
kvm_pgtable_walk_end(struct kvm_pgtable_walker * walker)395 static inline void kvm_pgtable_walk_end(struct kvm_pgtable_walker *walker)
396 {
397 	if (walker->flags & KVM_PGTABLE_WALK_SHARED)
398 		rcu_read_unlock();
399 }
400 
kvm_pgtable_walk_lock_held(void)401 static inline bool kvm_pgtable_walk_lock_held(void)
402 {
403 	return rcu_read_lock_held();
404 }
405 
406 #endif
407 
408 /**
409  * struct kvm_pgtable - KVM page-table.
410  * @ia_bits:		Maximum input address size, in bits.
411  * @start_level:	Level at which the page-table walk starts.
412  * @pgd:		Pointer to the first top-level entry of the page-table.
413  * @mm_ops:		Memory management callbacks.
414  * @mmu:		Stage-2 KVM MMU struct. Unused for stage-1 page-tables.
415  * @flags:		Stage-2 page-table flags.
416  * @force_pte_cb:	Function that returns true if page level mappings must
417  *			be used instead of block mappings.
418  */
419 struct kvm_pgtable {
420 	union {
421 		struct rb_root_cached				pkvm_mappings;
422 		struct {
423 			u32					ia_bits;
424 			s8					start_level;
425 			kvm_pteref_t				pgd;
426 			struct kvm_pgtable_mm_ops		*mm_ops;
427 
428 			/* Stage-2 only */
429 			enum kvm_pgtable_stage2_flags		flags;
430 			kvm_pgtable_force_pte_cb_t		force_pte_cb;
431 		};
432 	};
433 	struct kvm_s2_mmu					*mmu;
434 };
435 
436 /**
437  * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table.
438  * @pgt:	Uninitialised page-table structure to initialise.
439  * @va_bits:	Maximum virtual address bits.
440  * @mm_ops:	Memory management callbacks.
441  *
442  * Return: 0 on success, negative error code on failure.
443  */
444 int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
445 			 struct kvm_pgtable_mm_ops *mm_ops);
446 
447 /**
448  * kvm_pgtable_hyp_destroy() - Destroy an unused hypervisor stage-1 page-table.
449  * @pgt:	Page-table structure initialised by kvm_pgtable_hyp_init().
450  *
451  * The page-table is assumed to be unreachable by any hardware walkers prior
452  * to freeing and therefore no TLB invalidation is performed.
453  */
454 void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt);
455 
456 /**
457  * kvm_pgtable_hyp_map() - Install a mapping in a hypervisor stage-1 page-table.
458  * @pgt:	Page-table structure initialised by kvm_pgtable_hyp_init().
459  * @addr:	Virtual address at which to place the mapping.
460  * @size:	Size of the mapping.
461  * @phys:	Physical address of the memory to map.
462  * @prot:	Permissions and attributes for the mapping.
463  *
464  * The offset of @addr within a page is ignored, @size is rounded-up to
465  * the next page boundary and @phys is rounded-down to the previous page
466  * boundary.
467  *
468  * If device attributes are not explicitly requested in @prot, then the
469  * mapping will be normal, cacheable. Attempts to install a new mapping
470  * for a virtual address that is already mapped will be rejected with an
471  * error and a WARN().
472  *
473  * Return: 0 on success, negative error code on failure.
474  */
475 int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
476 			enum kvm_pgtable_prot prot);
477 
478 /**
479  * kvm_pgtable_hyp_unmap() - Remove a mapping from a hypervisor stage-1 page-table.
480  * @pgt:	Page-table structure initialised by kvm_pgtable_hyp_init().
481  * @addr:	Virtual address from which to remove the mapping.
482  * @size:	Size of the mapping.
483  *
484  * The offset of @addr within a page is ignored, @size is rounded-up to
485  * the next page boundary and @phys is rounded-down to the previous page
486  * boundary.
487  *
488  * TLB invalidation is performed for each page-table entry cleared during the
489  * unmapping operation and the reference count for the page-table page
490  * containing the cleared entry is decremented, with unreferenced pages being
491  * freed. The unmapping operation will stop early if it encounters either an
492  * invalid page-table entry or a valid block mapping which maps beyond the range
493  * being unmapped.
494  *
495  * Return: Number of bytes unmapped, which may be 0.
496  */
497 u64 kvm_pgtable_hyp_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
498 
499 /**
500  * kvm_get_vtcr() - Helper to construct VTCR_EL2
501  * @mmfr0:	Sanitized value of SYS_ID_AA64MMFR0_EL1 register.
502  * @mmfr1:	Sanitized value of SYS_ID_AA64MMFR1_EL1 register.
503  * @phys_shfit:	Value to set in VTCR_EL2.T0SZ.
504  *
505  * The VTCR value is common across all the physical CPUs on the system.
506  * We use system wide sanitised values to fill in different fields,
507  * except for Hardware Management of Access Flags. HA Flag is set
508  * unconditionally on all CPUs, as it is safe to run with or without
509  * the feature and the bit is RES0 on CPUs that don't support it.
510  *
511  * Return: VTCR_EL2 value
512  */
513 u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift);
514 
515 /**
516  * kvm_pgtable_stage2_pgd_size() - Helper to compute size of a stage-2 PGD
517  * @vtcr:	Content of the VTCR register.
518  *
519  * Return: the size (in bytes) of the stage-2 PGD
520  */
521 size_t kvm_pgtable_stage2_pgd_size(u64 vtcr);
522 
523 /**
524  * __kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table.
525  * @pgt:	Uninitialised page-table structure to initialise.
526  * @mmu:	S2 MMU context for this S2 translation
527  * @mm_ops:	Memory management callbacks.
528  * @flags:	Stage-2 configuration flags.
529  * @force_pte_cb: Function that returns true if page level mappings must
530  *		be used instead of block mappings.
531  *
532  * Return: 0 on success, negative error code on failure.
533  */
534 int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
535 			      struct kvm_pgtable_mm_ops *mm_ops,
536 			      enum kvm_pgtable_stage2_flags flags,
537 			      kvm_pgtable_force_pte_cb_t force_pte_cb);
538 
kvm_pgtable_stage2_init(struct kvm_pgtable * pgt,struct kvm_s2_mmu * mmu,struct kvm_pgtable_mm_ops * mm_ops)539 static inline int kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
540 					  struct kvm_pgtable_mm_ops *mm_ops)
541 {
542 	return __kvm_pgtable_stage2_init(pgt, mmu, mm_ops, 0, NULL);
543 }
544 
545 /**
546  * kvm_pgtable_stage2_destroy() - Destroy an unused guest stage-2 page-table.
547  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
548  *
549  * The page-table is assumed to be unreachable by any hardware walkers prior
550  * to freeing and therefore no TLB invalidation is performed.
551  */
552 void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);
553 
554 /**
555  * kvm_pgtable_stage2_free_unlinked() - Free an unlinked stage-2 paging structure.
556  * @mm_ops:	Memory management callbacks.
557  * @pgtable:	Unlinked stage-2 paging structure to be freed.
558  * @level:	Level of the stage-2 paging structure to be freed.
559  *
560  * The page-table is assumed to be unreachable by any hardware walkers prior to
561  * freeing and therefore no TLB invalidation is performed.
562  */
563 void kvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, s8 level);
564 
565 /**
566  * kvm_pgtable_stage2_create_unlinked() - Create an unlinked stage-2 paging structure.
567  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
568  * @phys:	Physical address of the memory to map.
569  * @level:	Starting level of the stage-2 paging structure to be created.
570  * @prot:	Permissions and attributes for the mapping.
571  * @mc:		Cache of pre-allocated and zeroed memory from which to allocate
572  *		page-table pages.
573  * @force_pte:  Force mappings to PAGE_SIZE granularity.
574  *
575  * Returns an unlinked page-table tree.  This new page-table tree is
576  * not reachable (i.e., it is unlinked) from the root pgd and it's
577  * therefore unreachableby the hardware page-table walker. No TLB
578  * invalidation or CMOs are performed.
579  *
580  * If device attributes are not explicitly requested in @prot, then the
581  * mapping will be normal, cacheable.
582  *
583  * Return: The fully populated (unlinked) stage-2 paging structure, or
584  * an ERR_PTR(error) on failure.
585  */
586 kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
587 					      u64 phys, s8 level,
588 					      enum kvm_pgtable_prot prot,
589 					      void *mc, bool force_pte);
590 
591 /**
592  * kvm_pgtable_stage2_map() - Install a mapping in a guest stage-2 page-table.
593  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
594  * @addr:	Intermediate physical address at which to place the mapping.
595  * @size:	Size of the mapping.
596  * @phys:	Physical address of the memory to map.
597  * @prot:	Permissions and attributes for the mapping.
598  * @mc:		Cache of pre-allocated and zeroed memory from which to allocate
599  *		page-table pages.
600  * @flags:	Flags to control the page-table walk (ex. a shared walk)
601  *
602  * The offset of @addr within a page is ignored, @size is rounded-up to
603  * the next page boundary and @phys is rounded-down to the previous page
604  * boundary.
605  *
606  * If device attributes are not explicitly requested in @prot, then the
607  * mapping will be normal, cacheable.
608  *
609  * Note that the update of a valid leaf PTE in this function will be aborted,
610  * if it's trying to recreate the exact same mapping or only change the access
611  * permissions. Instead, the vCPU will exit one more time from guest if still
612  * needed and then go through the path of relaxing permissions.
613  *
614  * Note that this function will both coalesce existing table entries and split
615  * existing block mappings, relying on page-faults to fault back areas outside
616  * of the new mapping lazily.
617  *
618  * Return: 0 on success, negative error code on failure.
619  */
620 int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
621 			   u64 phys, enum kvm_pgtable_prot prot,
622 			   void *mc, enum kvm_pgtable_walk_flags flags);
623 
624 /**
625  * kvm_pgtable_stage2_set_owner() - Unmap and annotate pages in the IPA space to
626  *				    track ownership.
627  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
628  * @addr:	Base intermediate physical address to annotate.
629  * @size:	Size of the annotated range.
630  * @mc:		Cache of pre-allocated and zeroed memory from which to allocate
631  *		page-table pages.
632  * @owner_id:	Unique identifier for the owner of the page.
633  *
634  * By default, all page-tables are owned by identifier 0. This function can be
635  * used to mark portions of the IPA space as owned by other entities. When a
636  * stage 2 is used with identity-mappings, these annotations allow to use the
637  * page-table data structure as a simple rmap.
638  *
639  * Return: 0 on success, negative error code on failure.
640  */
641 int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size,
642 				 void *mc, u8 owner_id);
643 
644 /**
645  * kvm_pgtable_stage2_unmap() - Remove a mapping from a guest stage-2 page-table.
646  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
647  * @addr:	Intermediate physical address from which to remove the mapping.
648  * @size:	Size of the mapping.
649  *
650  * The offset of @addr within a page is ignored and @size is rounded-up to
651  * the next page boundary.
652  *
653  * TLB invalidation is performed for each page-table entry cleared during the
654  * unmapping operation and the reference count for the page-table page
655  * containing the cleared entry is decremented, with unreferenced pages being
656  * freed. Unmapping a cacheable page will ensure that it is clean to the PoC if
657  * FWB is not supported by the CPU.
658  *
659  * Return: 0 on success, negative error code on failure.
660  */
661 int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
662 
663 /**
664  * kvm_pgtable_stage2_wrprotect() - Write-protect guest stage-2 address range
665  *                                  without TLB invalidation.
666  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
667  * @addr:	Intermediate physical address from which to write-protect,
668  * @size:	Size of the range.
669  *
670  * The offset of @addr within a page is ignored and @size is rounded-up to
671  * the next page boundary.
672  *
673  * Note that it is the caller's responsibility to invalidate the TLB after
674  * calling this function to ensure that the updated permissions are visible
675  * to the CPUs.
676  *
677  * Return: 0 on success, negative error code on failure.
678  */
679 int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);
680 
681 /**
682  * kvm_pgtable_stage2_mkyoung() - Set the access flag in a page-table entry.
683  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
684  * @addr:	Intermediate physical address to identify the page-table entry.
685  * @flags:	Flags to control the page-table walk (ex. a shared walk)
686  *
687  * The offset of @addr within a page is ignored.
688  *
689  * If there is a valid, leaf page-table entry used to translate @addr, then
690  * set the access flag in that entry.
691  */
692 void kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr,
693 				enum kvm_pgtable_walk_flags flags);
694 
695 /**
696  * kvm_pgtable_stage2_test_clear_young() - Test and optionally clear the access
697  *					   flag in a page-table entry.
698  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
699  * @addr:	Intermediate physical address to identify the page-table entry.
700  * @size:	Size of the address range to visit.
701  * @mkold:	True if the access flag should be cleared.
702  *
703  * The offset of @addr within a page is ignored.
704  *
705  * Tests and conditionally clears the access flag for every valid, leaf
706  * page-table entry used to translate the range [@addr, @addr + @size).
707  *
708  * Note that it is the caller's responsibility to invalidate the TLB after
709  * calling this function to ensure that the updated permissions are visible
710  * to the CPUs.
711  *
712  * Return: True if any of the visited PTEs had the access flag set.
713  */
714 bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr,
715 					 u64 size, bool mkold);
716 
717 /**
718  * kvm_pgtable_stage2_relax_perms() - Relax the permissions enforced by a
719  *				      page-table entry.
720  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
721  * @addr:	Intermediate physical address to identify the page-table entry.
722  * @prot:	Additional permissions to grant for the mapping.
723  * @flags:	Flags to control the page-table walk (ex. a shared walk)
724  *
725  * The offset of @addr within a page is ignored.
726  *
727  * If there is a valid, leaf page-table entry used to translate @addr, then
728  * relax the permissions in that entry according to the read, write and
729  * execute permissions specified by @prot. No permissions are removed, and
730  * TLB invalidation is performed after updating the entry. Software bits cannot
731  * be set or cleared using kvm_pgtable_stage2_relax_perms().
732  *
733  * Return: 0 on success, negative error code on failure.
734  */
735 int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
736 				   enum kvm_pgtable_prot prot,
737 				   enum kvm_pgtable_walk_flags flags);
738 
739 /**
740  * kvm_pgtable_stage2_flush_range() - Clean and invalidate data cache to Point
741  * 				      of Coherency for guest stage-2 address
742  *				      range.
743  * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
744  * @addr:	Intermediate physical address from which to flush.
745  * @size:	Size of the range.
746  *
747  * The offset of @addr within a page is ignored and @size is rounded-up to
748  * the next page boundary.
749  *
750  * Return: 0 on success, negative error code on failure.
751  */
752 int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size);
753 
754 /**
755  * kvm_pgtable_stage2_split() - Split a range of huge pages into leaf PTEs pointing
756  *				to PAGE_SIZE guest pages.
757  * @pgt:	 Page-table structure initialised by kvm_pgtable_stage2_init().
758  * @addr:	 Intermediate physical address from which to split.
759  * @size:	 Size of the range.
760  * @mc:		 Cache of pre-allocated and zeroed memory from which to allocate
761  *		 page-table pages.
762  *
763  * The function tries to split any level 1 or 2 entry that overlaps
764  * with the input range (given by @addr and @size).
765  *
766  * Return: 0 on success, negative error code on failure. Note that
767  * kvm_pgtable_stage2_split() is best effort: it tries to break as many
768  * blocks in the input range as allowed by @mc_capacity.
769  */
770 int kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
771 			     struct kvm_mmu_memory_cache *mc);
772 
773 /**
774  * kvm_pgtable_walk() - Walk a page-table.
775  * @pgt:	Page-table structure initialised by kvm_pgtable_*_init().
776  * @addr:	Input address for the start of the walk.
777  * @size:	Size of the range to walk.
778  * @walker:	Walker callback description.
779  *
780  * The offset of @addr within a page is ignored and @size is rounded-up to
781  * the next page boundary.
782  *
783  * The walker will walk the page-table entries corresponding to the input
784  * address range specified, visiting entries according to the walker flags.
785  * Invalid entries are treated as leaf entries. The visited page table entry is
786  * reloaded after invoking the walker callback, allowing the walker to descend
787  * into a newly installed table.
788  *
789  * Returning a negative error code from the walker callback function will
790  * terminate the walk immediately with the same error code.
791  *
792  * Return: 0 on success, negative error code on failure.
793  */
794 int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,
795 		     struct kvm_pgtable_walker *walker);
796 
797 /**
798  * kvm_pgtable_get_leaf() - Walk a page-table and retrieve the leaf entry
799  *			    with its level.
800  * @pgt:	Page-table structure initialised by kvm_pgtable_*_init()
801  *		or a similar initialiser.
802  * @addr:	Input address for the start of the walk.
803  * @ptep:	Pointer to storage for the retrieved PTE.
804  * @level:	Pointer to storage for the level of the retrieved PTE.
805  *
806  * The offset of @addr within a page is ignored.
807  *
808  * The walker will walk the page-table entries corresponding to the input
809  * address specified, retrieving the leaf corresponding to this address.
810  * Invalid entries are treated as leaf entries.
811  *
812  * Return: 0 on success, negative error code on failure.
813  */
814 int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr,
815 			 kvm_pte_t *ptep, s8 *level);
816 
817 /**
818  * kvm_pgtable_stage2_pte_prot() - Retrieve the protection attributes of a
819  *				   stage-2 Page-Table Entry.
820  * @pte:	Page-table entry
821  *
822  * Return: protection attributes of the page-table entry in the enum
823  *	   kvm_pgtable_prot format.
824  */
825 enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte);
826 
827 /**
828  * kvm_pgtable_hyp_pte_prot() - Retrieve the protection attributes of a stage-1
829  *				Page-Table Entry.
830  * @pte:	Page-table entry
831  *
832  * Return: protection attributes of the page-table entry in the enum
833  *	   kvm_pgtable_prot format.
834  */
835 enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte);
836 
837 /**
838  * kvm_tlb_flush_vmid_range() - Invalidate/flush a range of TLB entries
839  *
840  * @mmu:	Stage-2 KVM MMU struct
841  * @addr:	The base Intermediate physical address from which to invalidate
842  * @size:	Size of the range from the base to invalidate
843  */
844 void kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
845 				phys_addr_t addr, size_t size);
846 #endif	/* __ARM64_KVM_PGTABLE_H__ */
847