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