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