1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012,2013 - ARM Ltd 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 */ 6 7 #ifndef __ARM64_KVM_MMU_H__ 8 #define __ARM64_KVM_MMU_H__ 9 10 #include <asm/page.h> 11 #include <asm/memory.h> 12 #include <asm/mmu.h> 13 #include <asm/cpufeature.h> 14 15 /* 16 * As ARMv8.0 only has the TTBR0_EL2 register, we cannot express 17 * "negative" addresses. This makes it impossible to directly share 18 * mappings with the kernel. 19 * 20 * Instead, give the HYP mode its own VA region at a fixed offset from 21 * the kernel by just masking the top bits (which are all ones for a 22 * kernel address). We need to find out how many bits to mask. 23 * 24 * We want to build a set of page tables that cover both parts of the 25 * idmap (the trampoline page used to initialize EL2), and our normal 26 * runtime VA space, at the same time. 27 * 28 * Given that the kernel uses VA_BITS for its entire address space, 29 * and that half of that space (VA_BITS - 1) is used for the linear 30 * mapping, we can also limit the EL2 space to (VA_BITS - 1). 31 * 32 * The main question is "Within the VA_BITS space, does EL2 use the 33 * top or the bottom half of that space to shadow the kernel's linear 34 * mapping?". As we need to idmap the trampoline page, this is 35 * determined by the range in which this page lives. 36 * 37 * If the page is in the bottom half, we have to use the top half. If 38 * the page is in the top half, we have to use the bottom half: 39 * 40 * T = __pa_symbol(__hyp_idmap_text_start) 41 * if (T & BIT(VA_BITS - 1)) 42 * HYP_VA_MIN = 0 //idmap in upper half 43 * else 44 * HYP_VA_MIN = 1 << (VA_BITS - 1) 45 * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1 46 * 47 * This of course assumes that the trampoline page exists within the 48 * VA_BITS range. If it doesn't, then it means we're in the odd case 49 * where the kernel idmap (as well as HYP) uses more levels than the 50 * kernel runtime page tables (as seen when the kernel is configured 51 * for 4k pages, 39bits VA, and yet memory lives just above that 52 * limit, forcing the idmap to use 4 levels of page tables while the 53 * kernel itself only uses 3). In this particular case, it doesn't 54 * matter which side of VA_BITS we use, as we're guaranteed not to 55 * conflict with anything. 56 * 57 * When using VHE, there are no separate hyp mappings and all KVM 58 * functionality is already mapped as part of the main kernel 59 * mappings, and none of this applies in that case. 60 */ 61 62 #ifdef __ASSEMBLY__ 63 64 #include <asm/alternative.h> 65 66 /* 67 * Convert a kernel VA into a HYP VA. 68 * reg: VA to be converted. 69 * 70 * The actual code generation takes place in kvm_update_va_mask, and 71 * the instructions below are only there to reserve the space and 72 * perform the register allocation (kvm_update_va_mask uses the 73 * specific registers encoded in the instructions). 74 */ 75 .macro kern_hyp_va reg 76 alternative_cb kvm_update_va_mask 77 and \reg, \reg, #1 /* mask with va_mask */ 78 ror \reg, \reg, #1 /* rotate to the first tag bit */ 79 add \reg, \reg, #0 /* insert the low 12 bits of the tag */ 80 add \reg, \reg, #0, lsl 12 /* insert the top 12 bits of the tag */ 81 ror \reg, \reg, #63 /* rotate back */ 82 alternative_cb_end 83 .endm 84 85 #else 86 87 #include <linux/pgtable.h> 88 #include <asm/pgalloc.h> 89 #include <asm/cache.h> 90 #include <asm/cacheflush.h> 91 #include <asm/mmu_context.h> 92 93 void kvm_update_va_mask(struct alt_instr *alt, 94 __le32 *origptr, __le32 *updptr, int nr_inst); 95 void kvm_compute_layout(void); 96 97 static __always_inline unsigned long __kern_hyp_va(unsigned long v) 98 { 99 asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n" 100 "ror %0, %0, #1\n" 101 "add %0, %0, #0\n" 102 "add %0, %0, #0, lsl 12\n" 103 "ror %0, %0, #63\n", 104 kvm_update_va_mask) 105 : "+r" (v)); 106 return v; 107 } 108 109 #define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v)))) 110 111 /* 112 * We currently support using a VM-specified IPA size. For backward 113 * compatibility, the default IPA size is fixed to 40bits. 114 */ 115 #define KVM_PHYS_SHIFT (40) 116 117 #define kvm_phys_shift(kvm) VTCR_EL2_IPA(kvm->arch.vtcr) 118 #define kvm_phys_size(kvm) (_AC(1, ULL) << kvm_phys_shift(kvm)) 119 #define kvm_phys_mask(kvm) (kvm_phys_size(kvm) - _AC(1, ULL)) 120 121 static inline bool kvm_page_empty(void *ptr) 122 { 123 struct page *ptr_page = virt_to_page(ptr); 124 return page_count(ptr_page) == 1; 125 } 126 127 #include <asm/stage2_pgtable.h> 128 129 int create_hyp_mappings(void *from, void *to, pgprot_t prot); 130 int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, 131 void __iomem **kaddr, 132 void __iomem **haddr); 133 int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, 134 void **haddr); 135 void free_hyp_pgds(void); 136 137 void stage2_unmap_vm(struct kvm *kvm); 138 int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu); 139 void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu); 140 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, 141 phys_addr_t pa, unsigned long size, bool writable); 142 143 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu); 144 145 void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); 146 147 phys_addr_t kvm_mmu_get_httbr(void); 148 phys_addr_t kvm_get_idmap_vector(void); 149 int kvm_mmu_init(void); 150 void kvm_clear_hyp_idmap(void); 151 152 #define kvm_mk_pmd(ptep) \ 153 __pmd(__phys_to_pmd_val(__pa(ptep)) | PMD_TYPE_TABLE) 154 #define kvm_mk_pud(pmdp) \ 155 __pud(__phys_to_pud_val(__pa(pmdp)) | PMD_TYPE_TABLE) 156 #define kvm_mk_p4d(pmdp) \ 157 __p4d(__phys_to_p4d_val(__pa(pmdp)) | PUD_TYPE_TABLE) 158 159 #define kvm_set_pud(pudp, pud) set_pud(pudp, pud) 160 161 #define kvm_pfn_pte(pfn, prot) pfn_pte(pfn, prot) 162 #define kvm_pfn_pmd(pfn, prot) pfn_pmd(pfn, prot) 163 #define kvm_pfn_pud(pfn, prot) pfn_pud(pfn, prot) 164 165 #define kvm_pud_pfn(pud) pud_pfn(pud) 166 167 #define kvm_pmd_mkhuge(pmd) pmd_mkhuge(pmd) 168 #define kvm_pud_mkhuge(pud) pud_mkhuge(pud) 169 170 static inline pte_t kvm_s2pte_mkwrite(pte_t pte) 171 { 172 pte_val(pte) |= PTE_S2_RDWR; 173 return pte; 174 } 175 176 static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd) 177 { 178 pmd_val(pmd) |= PMD_S2_RDWR; 179 return pmd; 180 } 181 182 static inline pud_t kvm_s2pud_mkwrite(pud_t pud) 183 { 184 pud_val(pud) |= PUD_S2_RDWR; 185 return pud; 186 } 187 188 static inline pte_t kvm_s2pte_mkexec(pte_t pte) 189 { 190 pte_val(pte) &= ~PTE_S2_XN; 191 return pte; 192 } 193 194 static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd) 195 { 196 pmd_val(pmd) &= ~PMD_S2_XN; 197 return pmd; 198 } 199 200 static inline pud_t kvm_s2pud_mkexec(pud_t pud) 201 { 202 pud_val(pud) &= ~PUD_S2_XN; 203 return pud; 204 } 205 206 static inline void kvm_set_s2pte_readonly(pte_t *ptep) 207 { 208 pteval_t old_pteval, pteval; 209 210 pteval = READ_ONCE(pte_val(*ptep)); 211 do { 212 old_pteval = pteval; 213 pteval &= ~PTE_S2_RDWR; 214 pteval |= PTE_S2_RDONLY; 215 pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval); 216 } while (pteval != old_pteval); 217 } 218 219 static inline bool kvm_s2pte_readonly(pte_t *ptep) 220 { 221 return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY; 222 } 223 224 static inline bool kvm_s2pte_exec(pte_t *ptep) 225 { 226 return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN); 227 } 228 229 static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp) 230 { 231 kvm_set_s2pte_readonly((pte_t *)pmdp); 232 } 233 234 static inline bool kvm_s2pmd_readonly(pmd_t *pmdp) 235 { 236 return kvm_s2pte_readonly((pte_t *)pmdp); 237 } 238 239 static inline bool kvm_s2pmd_exec(pmd_t *pmdp) 240 { 241 return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN); 242 } 243 244 static inline void kvm_set_s2pud_readonly(pud_t *pudp) 245 { 246 kvm_set_s2pte_readonly((pte_t *)pudp); 247 } 248 249 static inline bool kvm_s2pud_readonly(pud_t *pudp) 250 { 251 return kvm_s2pte_readonly((pte_t *)pudp); 252 } 253 254 static inline bool kvm_s2pud_exec(pud_t *pudp) 255 { 256 return !(READ_ONCE(pud_val(*pudp)) & PUD_S2_XN); 257 } 258 259 static inline pud_t kvm_s2pud_mkyoung(pud_t pud) 260 { 261 return pud_mkyoung(pud); 262 } 263 264 static inline bool kvm_s2pud_young(pud_t pud) 265 { 266 return pud_young(pud); 267 } 268 269 #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep) 270 271 #ifdef __PAGETABLE_PMD_FOLDED 272 #define hyp_pmd_table_empty(pmdp) (0) 273 #else 274 #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp) 275 #endif 276 277 #ifdef __PAGETABLE_PUD_FOLDED 278 #define hyp_pud_table_empty(pudp) (0) 279 #else 280 #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp) 281 #endif 282 283 #ifdef __PAGETABLE_P4D_FOLDED 284 #define hyp_p4d_table_empty(p4dp) (0) 285 #else 286 #define hyp_p4d_table_empty(p4dp) kvm_page_empty(p4dp) 287 #endif 288 289 struct kvm; 290 291 #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l)) 292 293 static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) 294 { 295 return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; 296 } 297 298 static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size) 299 { 300 void *va = page_address(pfn_to_page(pfn)); 301 302 /* 303 * With FWB, we ensure that the guest always accesses memory using 304 * cacheable attributes, and we don't have to clean to PoC when 305 * faulting in pages. Furthermore, FWB implies IDC, so cleaning to 306 * PoU is not required either in this case. 307 */ 308 if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) 309 return; 310 311 kvm_flush_dcache_to_poc(va, size); 312 } 313 314 static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn, 315 unsigned long size) 316 { 317 if (icache_is_aliasing()) { 318 /* any kind of VIPT cache */ 319 __flush_icache_all(); 320 } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) { 321 /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */ 322 void *va = page_address(pfn_to_page(pfn)); 323 324 invalidate_icache_range((unsigned long)va, 325 (unsigned long)va + size); 326 } 327 } 328 329 static inline void __kvm_flush_dcache_pte(pte_t pte) 330 { 331 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 332 struct page *page = pte_page(pte); 333 kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE); 334 } 335 } 336 337 static inline void __kvm_flush_dcache_pmd(pmd_t pmd) 338 { 339 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 340 struct page *page = pmd_page(pmd); 341 kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE); 342 } 343 } 344 345 static inline void __kvm_flush_dcache_pud(pud_t pud) 346 { 347 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) { 348 struct page *page = pud_page(pud); 349 kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE); 350 } 351 } 352 353 void kvm_set_way_flush(struct kvm_vcpu *vcpu); 354 void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); 355 356 static inline bool __kvm_cpu_uses_extended_idmap(void) 357 { 358 return __cpu_uses_extended_idmap_level(); 359 } 360 361 static inline unsigned long __kvm_idmap_ptrs_per_pgd(void) 362 { 363 return idmap_ptrs_per_pgd; 364 } 365 366 /* 367 * Can't use pgd_populate here, because the extended idmap adds an extra level 368 * above CONFIG_PGTABLE_LEVELS (which is 2 or 3 if we're using the extended 369 * idmap), and pgd_populate is only available if CONFIG_PGTABLE_LEVELS = 4. 370 */ 371 static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd, 372 pgd_t *hyp_pgd, 373 pgd_t *merged_hyp_pgd, 374 unsigned long hyp_idmap_start) 375 { 376 int idmap_idx; 377 u64 pgd_addr; 378 379 /* 380 * Use the first entry to access the HYP mappings. It is 381 * guaranteed to be free, otherwise we wouldn't use an 382 * extended idmap. 383 */ 384 VM_BUG_ON(pgd_val(merged_hyp_pgd[0])); 385 pgd_addr = __phys_to_pgd_val(__pa(hyp_pgd)); 386 merged_hyp_pgd[0] = __pgd(pgd_addr | PMD_TYPE_TABLE); 387 388 /* 389 * Create another extended level entry that points to the boot HYP map, 390 * which contains an ID mapping of the HYP init code. We essentially 391 * merge the boot and runtime HYP maps by doing so, but they don't 392 * overlap anyway, so this is fine. 393 */ 394 idmap_idx = hyp_idmap_start >> VA_BITS; 395 VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx])); 396 pgd_addr = __phys_to_pgd_val(__pa(boot_hyp_pgd)); 397 merged_hyp_pgd[idmap_idx] = __pgd(pgd_addr | PMD_TYPE_TABLE); 398 } 399 400 static inline unsigned int kvm_get_vmid_bits(void) 401 { 402 int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 403 404 return get_vmid_bits(reg); 405 } 406 407 /* 408 * We are not in the kvm->srcu critical section most of the time, so we take 409 * the SRCU read lock here. Since we copy the data from the user page, we 410 * can immediately drop the lock again. 411 */ 412 static inline int kvm_read_guest_lock(struct kvm *kvm, 413 gpa_t gpa, void *data, unsigned long len) 414 { 415 int srcu_idx = srcu_read_lock(&kvm->srcu); 416 int ret = kvm_read_guest(kvm, gpa, data, len); 417 418 srcu_read_unlock(&kvm->srcu, srcu_idx); 419 420 return ret; 421 } 422 423 static inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa, 424 const void *data, unsigned long len) 425 { 426 int srcu_idx = srcu_read_lock(&kvm->srcu); 427 int ret = kvm_write_guest(kvm, gpa, data, len); 428 429 srcu_read_unlock(&kvm->srcu, srcu_idx); 430 431 return ret; 432 } 433 434 /* 435 * EL2 vectors can be mapped and rerouted in a number of ways, 436 * depending on the kernel configuration and CPU present: 437 * 438 * - If the CPU is affected by Spectre-v2, the hardening sequence is 439 * placed in one of the vector slots, which is executed before jumping 440 * to the real vectors. 441 * 442 * - If the CPU also has the ARM64_HARDEN_EL2_VECTORS cap, the slot 443 * containing the hardening sequence is mapped next to the idmap page, 444 * and executed before jumping to the real vectors. 445 * 446 * - If the CPU only has the ARM64_HARDEN_EL2_VECTORS cap, then an 447 * empty slot is selected, mapped next to the idmap page, and 448 * executed before jumping to the real vectors. 449 * 450 * Note that ARM64_HARDEN_EL2_VECTORS is somewhat incompatible with 451 * VHE, as we don't have hypervisor-specific mappings. If the system 452 * is VHE and yet selects this capability, it will be ignored. 453 */ 454 extern void *__kvm_bp_vect_base; 455 extern int __kvm_harden_el2_vector_slot; 456 457 static inline void *kvm_get_hyp_vector(void) 458 { 459 struct bp_hardening_data *data = arm64_get_bp_hardening_data(); 460 void *vect = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 461 int slot = -1; 462 463 if (cpus_have_const_cap(ARM64_SPECTRE_V2) && data->fn) { 464 vect = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 465 slot = data->hyp_vectors_slot; 466 } 467 468 if (this_cpu_has_cap(ARM64_HARDEN_EL2_VECTORS) && !has_vhe()) { 469 vect = __kvm_bp_vect_base; 470 if (slot == -1) 471 slot = __kvm_harden_el2_vector_slot; 472 } 473 474 if (slot != -1) 475 vect += slot * SZ_2K; 476 477 return vect; 478 } 479 480 #define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr) 481 482 /* 483 * Get the magic number 'x' for VTTBR:BADDR of this KVM instance. 484 * With v8.2 LVA extensions, 'x' should be a minimum of 6 with 485 * 52bit IPS. 486 */ 487 static inline int arm64_vttbr_x(u32 ipa_shift, u32 levels) 488 { 489 int x = ARM64_VTTBR_X(ipa_shift, levels); 490 491 return (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && x < 6) ? 6 : x; 492 } 493 494 static inline u64 vttbr_baddr_mask(u32 ipa_shift, u32 levels) 495 { 496 unsigned int x = arm64_vttbr_x(ipa_shift, levels); 497 498 return GENMASK_ULL(PHYS_MASK_SHIFT - 1, x); 499 } 500 501 static inline u64 kvm_vttbr_baddr_mask(struct kvm *kvm) 502 { 503 return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm)); 504 } 505 506 static __always_inline u64 kvm_get_vttbr(struct kvm_s2_mmu *mmu) 507 { 508 struct kvm_vmid *vmid = &mmu->vmid; 509 u64 vmid_field, baddr; 510 u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0; 511 512 baddr = mmu->pgd_phys; 513 vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT; 514 return kvm_phys_to_vttbr(baddr) | vmid_field | cnp; 515 } 516 517 /* 518 * Must be called from hyp code running at EL2 with an updated VTTBR 519 * and interrupts disabled. 520 */ 521 static __always_inline void __load_guest_stage2(struct kvm_s2_mmu *mmu) 522 { 523 write_sysreg(kern_hyp_va(mmu->kvm)->arch.vtcr, vtcr_el2); 524 write_sysreg(kvm_get_vttbr(mmu), vttbr_el2); 525 526 /* 527 * ARM errata 1165522 and 1530923 require the actual execution of the 528 * above before we can switch to the EL1/EL0 translation regime used by 529 * the guest. 530 */ 531 asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); 532 } 533 534 #endif /* __ASSEMBLY__ */ 535 #endif /* __ARM64_KVM_MMU_H__ */ 536