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