Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / arm64 / include / asm / kvm_pgtable.h
blobaab04097b5054a8a48a237d8e79971f57034c7e1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020 Google LLC
4 * Author: Will Deacon <will@kernel.org>
5 */
7 #ifndef __ARM64_KVM_PGTABLE_H__
8 #define __ARM64_KVM_PGTABLE_H__
10 #include <linux/bits.h>
11 #include <linux/kvm_host.h>
12 #include <linux/types.h>
14 #define KVM_PGTABLE_FIRST_LEVEL -1
15 #define KVM_PGTABLE_LAST_LEVEL 3
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
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
29 #define kvm_lpa2_is_enabled() system_supports_lpa2()
31 static inline u64 kvm_get_parange_max(void)
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;
40 static inline u64 kvm_get_parange(u64 mmfr0)
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;
48 return parange;
51 typedef u64 kvm_pte_t;
53 #define KVM_PTE_VALID BIT(0)
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)
60 #define KVM_PHYS_INVALID (-1ULL)
62 #define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2)
64 #define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2)
65 #define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6)
66 #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \
67 ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; })
68 #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \
69 ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; })
70 #define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8)
71 #define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3
72 #define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10)
74 #define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2)
75 #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6)
76 #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7)
77 #define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8)
78 #define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3
79 #define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10)
81 #define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50)
83 #define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55)
85 #define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54)
87 #define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54)
89 #define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50)
91 #define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
92 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
93 KVM_PTE_LEAF_ATTR_HI_S2_XN)
95 #define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2)
96 #define KVM_MAX_OWNER_ID 1
99 * Used to indicate a pte for which a 'break-before-make' sequence is in
100 * progress.
102 #define KVM_INVALID_PTE_LOCKED BIT(10)
104 static inline bool kvm_pte_valid(kvm_pte_t pte)
106 return pte & KVM_PTE_VALID;
109 static inline u64 kvm_pte_to_phys(kvm_pte_t pte)
111 u64 pa;
113 if (kvm_lpa2_is_enabled()) {
114 pa = pte & KVM_PTE_ADDR_MASK_LPA2;
115 pa |= FIELD_GET(KVM_PTE_ADDR_51_50_LPA2, pte) << 50;
116 } else {
117 pa = pte & KVM_PTE_ADDR_MASK;
118 if (PAGE_SHIFT == 16)
119 pa |= FIELD_GET(KVM_PTE_ADDR_51_48, pte) << 48;
122 return pa;
125 static inline kvm_pte_t kvm_phys_to_pte(u64 pa)
127 kvm_pte_t pte;
129 if (kvm_lpa2_is_enabled()) {
130 pte = pa & KVM_PTE_ADDR_MASK_LPA2;
131 pa &= GENMASK(51, 50);
132 pte |= FIELD_PREP(KVM_PTE_ADDR_51_50_LPA2, pa >> 50);
133 } else {
134 pte = pa & KVM_PTE_ADDR_MASK;
135 if (PAGE_SHIFT == 16) {
136 pa &= GENMASK(51, 48);
137 pte |= FIELD_PREP(KVM_PTE_ADDR_51_48, pa >> 48);
141 return pte;
144 static inline kvm_pfn_t kvm_pte_to_pfn(kvm_pte_t pte)
146 return __phys_to_pfn(kvm_pte_to_phys(pte));
149 static inline u64 kvm_granule_shift(s8 level)
151 /* Assumes KVM_PGTABLE_LAST_LEVEL is 3 */
152 return ARM64_HW_PGTABLE_LEVEL_SHIFT(level);
155 static inline u64 kvm_granule_size(s8 level)
157 return BIT(kvm_granule_shift(level));
160 static inline bool kvm_level_supports_block_mapping(s8 level)
162 return level >= KVM_PGTABLE_MIN_BLOCK_LEVEL;
165 static inline u32 kvm_supported_block_sizes(void)
167 s8 level = KVM_PGTABLE_MIN_BLOCK_LEVEL;
168 u32 r = 0;
170 for (; level <= KVM_PGTABLE_LAST_LEVEL; level++)
171 r |= BIT(kvm_granule_shift(level));
173 return r;
176 static inline bool kvm_is_block_size_supported(u64 size)
178 bool is_power_of_two = IS_ALIGNED(size, size);
180 return is_power_of_two && (size & kvm_supported_block_sizes());
184 * struct kvm_pgtable_mm_ops - Memory management callbacks.
185 * @zalloc_page: Allocate a single zeroed memory page.
186 * The @arg parameter can be used by the walker
187 * to pass a memcache. The initial refcount of
188 * the page is 1.
189 * @zalloc_pages_exact: Allocate an exact number of zeroed memory pages.
190 * The @size parameter is in bytes, and is rounded
191 * up to the next page boundary. The resulting
192 * allocation is physically contiguous.
193 * @free_pages_exact: Free an exact number of memory pages previously
194 * allocated by zalloc_pages_exact.
195 * @free_unlinked_table: Free an unlinked paging structure by unlinking and
196 * dropping references.
197 * @get_page: Increment the refcount on a page.
198 * @put_page: Decrement the refcount on a page. When the
199 * refcount reaches 0 the page is automatically
200 * freed.
201 * @page_count: Return the refcount of a page.
202 * @phys_to_virt: Convert a physical address into a virtual
203 * address mapped in the current context.
204 * @virt_to_phys: Convert a virtual address mapped in the current
205 * context into a physical address.
206 * @dcache_clean_inval_poc: Clean and invalidate the data cache to the PoC
207 * for the specified memory address range.
208 * @icache_inval_pou: Invalidate the instruction cache to the PoU
209 * for the specified memory address range.
211 struct kvm_pgtable_mm_ops {
212 void* (*zalloc_page)(void *arg);
213 void* (*zalloc_pages_exact)(size_t size);
214 void (*free_pages_exact)(void *addr, size_t size);
215 void (*free_unlinked_table)(void *addr, s8 level);
216 void (*get_page)(void *addr);
217 void (*put_page)(void *addr);
218 int (*page_count)(void *addr);
219 void* (*phys_to_virt)(phys_addr_t phys);
220 phys_addr_t (*virt_to_phys)(void *addr);
221 void (*dcache_clean_inval_poc)(void *addr, size_t size);
222 void (*icache_inval_pou)(void *addr, size_t size);
226 * enum kvm_pgtable_stage2_flags - Stage-2 page-table flags.
227 * @KVM_PGTABLE_S2_NOFWB: Don't enforce Normal-WB even if the CPUs have
228 * ARM64_HAS_STAGE2_FWB.
229 * @KVM_PGTABLE_S2_IDMAP: Only use identity mappings.
231 enum kvm_pgtable_stage2_flags {
232 KVM_PGTABLE_S2_NOFWB = BIT(0),
233 KVM_PGTABLE_S2_IDMAP = BIT(1),
237 * enum kvm_pgtable_prot - Page-table permissions and attributes.
238 * @KVM_PGTABLE_PROT_X: Execute permission.
239 * @KVM_PGTABLE_PROT_W: Write permission.
240 * @KVM_PGTABLE_PROT_R: Read permission.
241 * @KVM_PGTABLE_PROT_DEVICE: Device attributes.
242 * @KVM_PGTABLE_PROT_NORMAL_NC: Normal noncacheable attributes.
243 * @KVM_PGTABLE_PROT_SW0: Software bit 0.
244 * @KVM_PGTABLE_PROT_SW1: Software bit 1.
245 * @KVM_PGTABLE_PROT_SW2: Software bit 2.
246 * @KVM_PGTABLE_PROT_SW3: Software bit 3.
248 enum kvm_pgtable_prot {
249 KVM_PGTABLE_PROT_X = BIT(0),
250 KVM_PGTABLE_PROT_W = BIT(1),
251 KVM_PGTABLE_PROT_R = BIT(2),
253 KVM_PGTABLE_PROT_DEVICE = BIT(3),
254 KVM_PGTABLE_PROT_NORMAL_NC = BIT(4),
256 KVM_PGTABLE_PROT_SW0 = BIT(55),
257 KVM_PGTABLE_PROT_SW1 = BIT(56),
258 KVM_PGTABLE_PROT_SW2 = BIT(57),
259 KVM_PGTABLE_PROT_SW3 = BIT(58),
262 #define KVM_PGTABLE_PROT_RW (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W)
263 #define KVM_PGTABLE_PROT_RWX (KVM_PGTABLE_PROT_RW | KVM_PGTABLE_PROT_X)
265 #define PKVM_HOST_MEM_PROT KVM_PGTABLE_PROT_RWX
266 #define PKVM_HOST_MMIO_PROT KVM_PGTABLE_PROT_RW
268 #define PAGE_HYP KVM_PGTABLE_PROT_RW
269 #define PAGE_HYP_EXEC (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_X)
270 #define PAGE_HYP_RO (KVM_PGTABLE_PROT_R)
271 #define PAGE_HYP_DEVICE (PAGE_HYP | KVM_PGTABLE_PROT_DEVICE)
273 typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,
274 enum kvm_pgtable_prot prot);
277 * enum kvm_pgtable_walk_flags - Flags to control a depth-first page-table walk.
278 * @KVM_PGTABLE_WALK_LEAF: Visit leaf entries, including invalid
279 * entries.
280 * @KVM_PGTABLE_WALK_TABLE_PRE: Visit table entries before their
281 * children.
282 * @KVM_PGTABLE_WALK_TABLE_POST: Visit table entries after their
283 * children.
284 * @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared
285 * with other software walkers.
286 * @KVM_PGTABLE_WALK_HANDLE_FAULT: Indicates the page-table walk was
287 * invoked from a fault handler.
288 * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries
289 * without Break-before-make's
290 * TLB invalidation.
291 * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries
292 * without Cache maintenance
293 * operations required.
295 enum kvm_pgtable_walk_flags {
296 KVM_PGTABLE_WALK_LEAF = BIT(0),
297 KVM_PGTABLE_WALK_TABLE_PRE = BIT(1),
298 KVM_PGTABLE_WALK_TABLE_POST = BIT(2),
299 KVM_PGTABLE_WALK_SHARED = BIT(3),
300 KVM_PGTABLE_WALK_HANDLE_FAULT = BIT(4),
301 KVM_PGTABLE_WALK_SKIP_BBM_TLBI = BIT(5),
302 KVM_PGTABLE_WALK_SKIP_CMO = BIT(6),
305 struct kvm_pgtable_visit_ctx {
306 kvm_pte_t *ptep;
307 kvm_pte_t old;
308 void *arg;
309 struct kvm_pgtable_mm_ops *mm_ops;
310 u64 start;
311 u64 addr;
312 u64 end;
313 s8 level;
314 enum kvm_pgtable_walk_flags flags;
317 typedef int (*kvm_pgtable_visitor_fn_t)(const struct kvm_pgtable_visit_ctx *ctx,
318 enum kvm_pgtable_walk_flags visit);
320 static inline bool kvm_pgtable_walk_shared(const struct kvm_pgtable_visit_ctx *ctx)
322 return ctx->flags & KVM_PGTABLE_WALK_SHARED;
326 * struct kvm_pgtable_walker - Hook into a page-table walk.
327 * @cb: Callback function to invoke during the walk.
328 * @arg: Argument passed to the callback function.
329 * @flags: Bitwise-OR of flags to identify the entry types on which to
330 * invoke the callback function.
332 struct kvm_pgtable_walker {
333 const kvm_pgtable_visitor_fn_t cb;
334 void * const arg;
335 const enum kvm_pgtable_walk_flags flags;
339 * RCU cannot be used in a non-kernel context such as the hyp. As such, page
340 * table walkers used in hyp do not call into RCU and instead use other
341 * synchronization mechanisms (such as a spinlock).
343 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
345 typedef kvm_pte_t *kvm_pteref_t;
347 static inline kvm_pte_t *kvm_dereference_pteref(struct kvm_pgtable_walker *walker,
348 kvm_pteref_t pteref)
350 return pteref;
353 static inline int kvm_pgtable_walk_begin(struct kvm_pgtable_walker *walker)
356 * Due to the lack of RCU (or a similar protection scheme), only
357 * non-shared table walkers are allowed in the hypervisor.
359 if (walker->flags & KVM_PGTABLE_WALK_SHARED)
360 return -EPERM;
362 return 0;
365 static inline void kvm_pgtable_walk_end(struct kvm_pgtable_walker *walker) {}
367 static inline bool kvm_pgtable_walk_lock_held(void)
369 return true;
372 #else
374 typedef kvm_pte_t __rcu *kvm_pteref_t;
376 static inline kvm_pte_t *kvm_dereference_pteref(struct kvm_pgtable_walker *walker,
377 kvm_pteref_t pteref)
379 return rcu_dereference_check(pteref, !(walker->flags & KVM_PGTABLE_WALK_SHARED));
382 static inline int kvm_pgtable_walk_begin(struct kvm_pgtable_walker *walker)
384 if (walker->flags & KVM_PGTABLE_WALK_SHARED)
385 rcu_read_lock();
387 return 0;
390 static inline void kvm_pgtable_walk_end(struct kvm_pgtable_walker *walker)
392 if (walker->flags & KVM_PGTABLE_WALK_SHARED)
393 rcu_read_unlock();
396 static inline bool kvm_pgtable_walk_lock_held(void)
398 return rcu_read_lock_held();
401 #endif
404 * struct kvm_pgtable - KVM page-table.
405 * @ia_bits: Maximum input address size, in bits.
406 * @start_level: Level at which the page-table walk starts.
407 * @pgd: Pointer to the first top-level entry of the page-table.
408 * @mm_ops: Memory management callbacks.
409 * @mmu: Stage-2 KVM MMU struct. Unused for stage-1 page-tables.
410 * @flags: Stage-2 page-table flags.
411 * @force_pte_cb: Function that returns true if page level mappings must
412 * be used instead of block mappings.
414 struct kvm_pgtable {
415 u32 ia_bits;
416 s8 start_level;
417 kvm_pteref_t pgd;
418 struct kvm_pgtable_mm_ops *mm_ops;
420 /* Stage-2 only */
421 struct kvm_s2_mmu *mmu;
422 enum kvm_pgtable_stage2_flags flags;
423 kvm_pgtable_force_pte_cb_t force_pte_cb;
427 * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table.
428 * @pgt: Uninitialised page-table structure to initialise.
429 * @va_bits: Maximum virtual address bits.
430 * @mm_ops: Memory management callbacks.
432 * Return: 0 on success, negative error code on failure.
434 int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
435 struct kvm_pgtable_mm_ops *mm_ops);
438 * kvm_pgtable_hyp_destroy() - Destroy an unused hypervisor stage-1 page-table.
439 * @pgt: Page-table structure initialised by kvm_pgtable_hyp_init().
441 * The page-table is assumed to be unreachable by any hardware walkers prior
442 * to freeing and therefore no TLB invalidation is performed.
444 void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt);
447 * kvm_pgtable_hyp_map() - Install a mapping in a hypervisor stage-1 page-table.
448 * @pgt: Page-table structure initialised by kvm_pgtable_hyp_init().
449 * @addr: Virtual address at which to place the mapping.
450 * @size: Size of the mapping.
451 * @phys: Physical address of the memory to map.
452 * @prot: Permissions and attributes for the mapping.
454 * The offset of @addr within a page is ignored, @size is rounded-up to
455 * the next page boundary and @phys is rounded-down to the previous page
456 * boundary.
458 * If device attributes are not explicitly requested in @prot, then the
459 * mapping will be normal, cacheable. Attempts to install a new mapping
460 * for a virtual address that is already mapped will be rejected with an
461 * error and a WARN().
463 * Return: 0 on success, negative error code on failure.
465 int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
466 enum kvm_pgtable_prot prot);
469 * kvm_pgtable_hyp_unmap() - Remove a mapping from a hypervisor stage-1 page-table.
470 * @pgt: Page-table structure initialised by kvm_pgtable_hyp_init().
471 * @addr: Virtual address from which to remove the mapping.
472 * @size: Size of the mapping.
474 * The offset of @addr within a page is ignored, @size is rounded-up to
475 * the next page boundary and @phys is rounded-down to the previous page
476 * boundary.
478 * TLB invalidation is performed for each page-table entry cleared during the
479 * unmapping operation and the reference count for the page-table page
480 * containing the cleared entry is decremented, with unreferenced pages being
481 * freed. The unmapping operation will stop early if it encounters either an
482 * invalid page-table entry or a valid block mapping which maps beyond the range
483 * being unmapped.
485 * Return: Number of bytes unmapped, which may be 0.
487 u64 kvm_pgtable_hyp_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
490 * kvm_get_vtcr() - Helper to construct VTCR_EL2
491 * @mmfr0: Sanitized value of SYS_ID_AA64MMFR0_EL1 register.
492 * @mmfr1: Sanitized value of SYS_ID_AA64MMFR1_EL1 register.
493 * @phys_shfit: Value to set in VTCR_EL2.T0SZ.
495 * The VTCR value is common across all the physical CPUs on the system.
496 * We use system wide sanitised values to fill in different fields,
497 * except for Hardware Management of Access Flags. HA Flag is set
498 * unconditionally on all CPUs, as it is safe to run with or without
499 * the feature and the bit is RES0 on CPUs that don't support it.
501 * Return: VTCR_EL2 value
503 u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift);
506 * kvm_pgtable_stage2_pgd_size() - Helper to compute size of a stage-2 PGD
507 * @vtcr: Content of the VTCR register.
509 * Return: the size (in bytes) of the stage-2 PGD
511 size_t kvm_pgtable_stage2_pgd_size(u64 vtcr);
514 * __kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table.
515 * @pgt: Uninitialised page-table structure to initialise.
516 * @mmu: S2 MMU context for this S2 translation
517 * @mm_ops: Memory management callbacks.
518 * @flags: Stage-2 configuration flags.
519 * @force_pte_cb: Function that returns true if page level mappings must
520 * be used instead of block mappings.
522 * Return: 0 on success, negative error code on failure.
524 int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
525 struct kvm_pgtable_mm_ops *mm_ops,
526 enum kvm_pgtable_stage2_flags flags,
527 kvm_pgtable_force_pte_cb_t force_pte_cb);
529 #define kvm_pgtable_stage2_init(pgt, mmu, mm_ops) \
530 __kvm_pgtable_stage2_init(pgt, mmu, mm_ops, 0, NULL)
533 * kvm_pgtable_stage2_destroy() - Destroy an unused guest stage-2 page-table.
534 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
536 * The page-table is assumed to be unreachable by any hardware walkers prior
537 * to freeing and therefore no TLB invalidation is performed.
539 void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);
542 * kvm_pgtable_stage2_free_unlinked() - Free an unlinked stage-2 paging structure.
543 * @mm_ops: Memory management callbacks.
544 * @pgtable: Unlinked stage-2 paging structure to be freed.
545 * @level: Level of the stage-2 paging structure to be freed.
547 * The page-table is assumed to be unreachable by any hardware walkers prior to
548 * freeing and therefore no TLB invalidation is performed.
550 void kvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, s8 level);
553 * kvm_pgtable_stage2_create_unlinked() - Create an unlinked stage-2 paging structure.
554 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
555 * @phys: Physical address of the memory to map.
556 * @level: Starting level of the stage-2 paging structure to be created.
557 * @prot: Permissions and attributes for the mapping.
558 * @mc: Cache of pre-allocated and zeroed memory from which to allocate
559 * page-table pages.
560 * @force_pte: Force mappings to PAGE_SIZE granularity.
562 * Returns an unlinked page-table tree. This new page-table tree is
563 * not reachable (i.e., it is unlinked) from the root pgd and it's
564 * therefore unreachableby the hardware page-table walker. No TLB
565 * invalidation or CMOs are performed.
567 * If device attributes are not explicitly requested in @prot, then the
568 * mapping will be normal, cacheable.
570 * Return: The fully populated (unlinked) stage-2 paging structure, or
571 * an ERR_PTR(error) on failure.
573 kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
574 u64 phys, s8 level,
575 enum kvm_pgtable_prot prot,
576 void *mc, bool force_pte);
579 * kvm_pgtable_stage2_map() - Install a mapping in a guest stage-2 page-table.
580 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
581 * @addr: Intermediate physical address at which to place the mapping.
582 * @size: Size of the mapping.
583 * @phys: Physical address of the memory to map.
584 * @prot: Permissions and attributes for the mapping.
585 * @mc: Cache of pre-allocated and zeroed memory from which to allocate
586 * page-table pages.
587 * @flags: Flags to control the page-table walk (ex. a shared walk)
589 * The offset of @addr within a page is ignored, @size is rounded-up to
590 * the next page boundary and @phys is rounded-down to the previous page
591 * boundary.
593 * If device attributes are not explicitly requested in @prot, then the
594 * mapping will be normal, cacheable.
596 * Note that the update of a valid leaf PTE in this function will be aborted,
597 * if it's trying to recreate the exact same mapping or only change the access
598 * permissions. Instead, the vCPU will exit one more time from guest if still
599 * needed and then go through the path of relaxing permissions.
601 * Note that this function will both coalesce existing table entries and split
602 * existing block mappings, relying on page-faults to fault back areas outside
603 * of the new mapping lazily.
605 * Return: 0 on success, negative error code on failure.
607 int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
608 u64 phys, enum kvm_pgtable_prot prot,
609 void *mc, enum kvm_pgtable_walk_flags flags);
612 * kvm_pgtable_stage2_set_owner() - Unmap and annotate pages in the IPA space to
613 * track ownership.
614 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
615 * @addr: Base intermediate physical address to annotate.
616 * @size: Size of the annotated range.
617 * @mc: Cache of pre-allocated and zeroed memory from which to allocate
618 * page-table pages.
619 * @owner_id: Unique identifier for the owner of the page.
621 * By default, all page-tables are owned by identifier 0. This function can be
622 * used to mark portions of the IPA space as owned by other entities. When a
623 * stage 2 is used with identity-mappings, these annotations allow to use the
624 * page-table data structure as a simple rmap.
626 * Return: 0 on success, negative error code on failure.
628 int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size,
629 void *mc, u8 owner_id);
632 * kvm_pgtable_stage2_unmap() - Remove a mapping from a guest stage-2 page-table.
633 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
634 * @addr: Intermediate physical address from which to remove the mapping.
635 * @size: Size of the mapping.
637 * The offset of @addr within a page is ignored and @size is rounded-up to
638 * the next page boundary.
640 * TLB invalidation is performed for each page-table entry cleared during the
641 * unmapping operation and the reference count for the page-table page
642 * containing the cleared entry is decremented, with unreferenced pages being
643 * freed. Unmapping a cacheable page will ensure that it is clean to the PoC if
644 * FWB is not supported by the CPU.
646 * Return: 0 on success, negative error code on failure.
648 int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
651 * kvm_pgtable_stage2_wrprotect() - Write-protect guest stage-2 address range
652 * without TLB invalidation.
653 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
654 * @addr: Intermediate physical address from which to write-protect,
655 * @size: Size of the range.
657 * The offset of @addr within a page is ignored and @size is rounded-up to
658 * the next page boundary.
660 * Note that it is the caller's responsibility to invalidate the TLB after
661 * calling this function to ensure that the updated permissions are visible
662 * to the CPUs.
664 * Return: 0 on success, negative error code on failure.
666 int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);
669 * kvm_pgtable_stage2_mkyoung() - Set the access flag in a page-table entry.
670 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
671 * @addr: Intermediate physical address to identify the page-table entry.
673 * The offset of @addr within a page is ignored.
675 * If there is a valid, leaf page-table entry used to translate @addr, then
676 * set the access flag in that entry.
678 void kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr);
681 * kvm_pgtable_stage2_test_clear_young() - Test and optionally clear the access
682 * 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 * @size: Size of the address range to visit.
686 * @mkold: True if the access flag should be cleared.
688 * The offset of @addr within a page is ignored.
690 * Tests and conditionally clears the access flag for every valid, leaf
691 * page-table entry used to translate the range [@addr, @addr + @size).
693 * Note that it is the caller's responsibility to invalidate the TLB after
694 * calling this function to ensure that the updated permissions are visible
695 * to the CPUs.
697 * Return: True if any of the visited PTEs had the access flag set.
699 bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr,
700 u64 size, bool mkold);
703 * kvm_pgtable_stage2_relax_perms() - Relax the permissions enforced by a
704 * page-table entry.
705 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
706 * @addr: Intermediate physical address to identify the page-table entry.
707 * @prot: Additional permissions to grant for the mapping.
709 * The offset of @addr within a page is ignored.
711 * If there is a valid, leaf page-table entry used to translate @addr, then
712 * relax the permissions in that entry according to the read, write and
713 * execute permissions specified by @prot. No permissions are removed, and
714 * TLB invalidation is performed after updating the entry. Software bits cannot
715 * be set or cleared using kvm_pgtable_stage2_relax_perms().
717 * Return: 0 on success, negative error code on failure.
719 int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
720 enum kvm_pgtable_prot prot);
723 * kvm_pgtable_stage2_flush_range() - Clean and invalidate data cache to Point
724 * of Coherency for guest stage-2 address
725 * range.
726 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*().
727 * @addr: Intermediate physical address from which to flush.
728 * @size: Size of the range.
730 * The offset of @addr within a page is ignored and @size is rounded-up to
731 * the next page boundary.
733 * Return: 0 on success, negative error code on failure.
735 int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size);
738 * kvm_pgtable_stage2_split() - Split a range of huge pages into leaf PTEs pointing
739 * to PAGE_SIZE guest pages.
740 * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init().
741 * @addr: Intermediate physical address from which to split.
742 * @size: Size of the range.
743 * @mc: Cache of pre-allocated and zeroed memory from which to allocate
744 * page-table pages.
746 * The function tries to split any level 1 or 2 entry that overlaps
747 * with the input range (given by @addr and @size).
749 * Return: 0 on success, negative error code on failure. Note that
750 * kvm_pgtable_stage2_split() is best effort: it tries to break as many
751 * blocks in the input range as allowed by @mc_capacity.
753 int kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
754 struct kvm_mmu_memory_cache *mc);
757 * kvm_pgtable_walk() - Walk a page-table.
758 * @pgt: Page-table structure initialised by kvm_pgtable_*_init().
759 * @addr: Input address for the start of the walk.
760 * @size: Size of the range to walk.
761 * @walker: Walker callback description.
763 * The offset of @addr within a page is ignored and @size is rounded-up to
764 * the next page boundary.
766 * The walker will walk the page-table entries corresponding to the input
767 * address range specified, visiting entries according to the walker flags.
768 * Invalid entries are treated as leaf entries. The visited page table entry is
769 * reloaded after invoking the walker callback, allowing the walker to descend
770 * into a newly installed table.
772 * Returning a negative error code from the walker callback function will
773 * terminate the walk immediately with the same error code.
775 * Return: 0 on success, negative error code on failure.
777 int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,
778 struct kvm_pgtable_walker *walker);
781 * kvm_pgtable_get_leaf() - Walk a page-table and retrieve the leaf entry
782 * with its level.
783 * @pgt: Page-table structure initialised by kvm_pgtable_*_init()
784 * or a similar initialiser.
785 * @addr: Input address for the start of the walk.
786 * @ptep: Pointer to storage for the retrieved PTE.
787 * @level: Pointer to storage for the level of the retrieved PTE.
789 * The offset of @addr within a page is ignored.
791 * The walker will walk the page-table entries corresponding to the input
792 * address specified, retrieving the leaf corresponding to this address.
793 * Invalid entries are treated as leaf entries.
795 * Return: 0 on success, negative error code on failure.
797 int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr,
798 kvm_pte_t *ptep, s8 *level);
801 * kvm_pgtable_stage2_pte_prot() - Retrieve the protection attributes of a
802 * stage-2 Page-Table Entry.
803 * @pte: Page-table entry
805 * Return: protection attributes of the page-table entry in the enum
806 * kvm_pgtable_prot format.
808 enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte);
811 * kvm_pgtable_hyp_pte_prot() - Retrieve the protection attributes of a stage-1
812 * Page-Table Entry.
813 * @pte: Page-table entry
815 * Return: protection attributes of the page-table entry in the enum
816 * kvm_pgtable_prot format.
818 enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte);
821 * kvm_tlb_flush_vmid_range() - Invalidate/flush a range of TLB entries
823 * @mmu: Stage-2 KVM MMU struct
824 * @addr: The base Intermediate physical address from which to invalidate
825 * @size: Size of the range from the base to invalidate
827 void kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
828 phys_addr_t addr, size_t size);
829 #endif /* __ARM64_KVM_PGTABLE_H__ */