Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Stand-alone page-table allocator for hyp stage-1 and guest stage-2. |
| 4 | * No bombay mix was harmed in the writing of this file. |
| 5 | * |
| 6 | * Copyright (C) 2020 Google LLC |
| 7 | * Author: Will Deacon <will@kernel.org> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/bitfield.h> |
| 11 | #include <asm/kvm_pgtable.h> |
Quentin Perret | bcb25a2 | 2021-03-19 10:01:30 +0000 | [diff] [blame] | 12 | #include <asm/stage2_pgtable.h> |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 13 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 14 | |
| 15 | #define KVM_PTE_TYPE BIT(1) |
| 16 | #define KVM_PTE_TYPE_BLOCK 0 |
| 17 | #define KVM_PTE_TYPE_PAGE 1 |
| 18 | #define KVM_PTE_TYPE_TABLE 1 |
| 19 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 20 | #define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) |
| 21 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 22 | #define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) |
| 23 | #define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) |
| 24 | #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO 3 |
| 25 | #define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW 1 |
| 26 | #define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) |
| 27 | #define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 |
| 28 | #define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) |
| 29 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 30 | #define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) |
| 31 | #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) |
| 32 | #define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) |
| 33 | #define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) |
| 34 | #define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 |
| 35 | #define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) |
| 36 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 37 | #define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 51) |
| 38 | |
Quentin Perret | 178cac0 | 2021-08-09 16:24:34 +0100 | [diff] [blame] | 39 | #define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) |
| 40 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 41 | #define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) |
| 42 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 43 | #define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) |
| 44 | |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 45 | #define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ |
| 46 | KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ |
| 47 | KVM_PTE_LEAF_ATTR_HI_S2_XN) |
| 48 | |
Quentin Perret | 8a0282c | 2021-08-09 16:24:35 +0100 | [diff] [blame] | 49 | #define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 50 | #define KVM_MAX_OWNER_ID 1 |
| 51 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 52 | struct kvm_pgtable_walk_data { |
| 53 | struct kvm_pgtable *pgt; |
| 54 | struct kvm_pgtable_walker *walker; |
| 55 | |
| 56 | u64 addr; |
| 57 | u64 end; |
| 58 | }; |
| 59 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 60 | #define KVM_PHYS_INVALID (-1ULL) |
| 61 | |
| 62 | static bool kvm_phys_is_valid(u64 phys) |
| 63 | { |
| 64 | return phys < BIT(id_aa64mmfr0_parange_to_phys_shift(ID_AA64MMFR0_PARANGE_MAX)); |
| 65 | } |
| 66 | |
Quentin Perret | 2fcb3a5 | 2021-03-19 10:01:39 +0000 | [diff] [blame] | 67 | static bool kvm_block_mapping_supported(u64 addr, u64 end, u64 phys, u32 level) |
| 68 | { |
| 69 | u64 granule = kvm_granule_size(level); |
| 70 | |
| 71 | if (!kvm_level_supports_block_mapping(level)) |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 72 | return false; |
| 73 | |
| 74 | if (granule > (end - addr)) |
| 75 | return false; |
| 76 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 77 | if (kvm_phys_is_valid(phys) && !IS_ALIGNED(phys, granule)) |
| 78 | return false; |
| 79 | |
| 80 | return IS_ALIGNED(addr, granule); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static u32 kvm_pgtable_idx(struct kvm_pgtable_walk_data *data, u32 level) |
| 84 | { |
| 85 | u64 shift = kvm_granule_shift(level); |
| 86 | u64 mask = BIT(PAGE_SHIFT - 3) - 1; |
| 87 | |
| 88 | return (data->addr >> shift) & mask; |
| 89 | } |
| 90 | |
| 91 | static u32 __kvm_pgd_page_idx(struct kvm_pgtable *pgt, u64 addr) |
| 92 | { |
| 93 | u64 shift = kvm_granule_shift(pgt->start_level - 1); /* May underflow */ |
| 94 | u64 mask = BIT(pgt->ia_bits) - 1; |
| 95 | |
| 96 | return (addr & mask) >> shift; |
| 97 | } |
| 98 | |
| 99 | static u32 kvm_pgd_page_idx(struct kvm_pgtable_walk_data *data) |
| 100 | { |
| 101 | return __kvm_pgd_page_idx(data->pgt, data->addr); |
| 102 | } |
| 103 | |
| 104 | static u32 kvm_pgd_pages(u32 ia_bits, u32 start_level) |
| 105 | { |
| 106 | struct kvm_pgtable pgt = { |
| 107 | .ia_bits = ia_bits, |
| 108 | .start_level = start_level, |
| 109 | }; |
| 110 | |
| 111 | return __kvm_pgd_page_idx(&pgt, -1ULL) + 1; |
| 112 | } |
| 113 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 114 | static bool kvm_pte_table(kvm_pte_t pte, u32 level) |
| 115 | { |
| 116 | if (level == KVM_PGTABLE_MAX_LEVELS - 1) |
| 117 | return false; |
| 118 | |
| 119 | if (!kvm_pte_valid(pte)) |
| 120 | return false; |
| 121 | |
| 122 | return FIELD_GET(KVM_PTE_TYPE, pte) == KVM_PTE_TYPE_TABLE; |
| 123 | } |
| 124 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 125 | static kvm_pte_t kvm_phys_to_pte(u64 pa) |
| 126 | { |
| 127 | kvm_pte_t pte = pa & KVM_PTE_ADDR_MASK; |
| 128 | |
| 129 | if (PAGE_SHIFT == 16) |
| 130 | pte |= FIELD_PREP(KVM_PTE_ADDR_51_48, pa >> 48); |
| 131 | |
| 132 | return pte; |
| 133 | } |
| 134 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 135 | static kvm_pte_t *kvm_pte_follow(kvm_pte_t pte, struct kvm_pgtable_mm_ops *mm_ops) |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 136 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 137 | return mm_ops->phys_to_virt(kvm_pte_to_phys(pte)); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 140 | static void kvm_clear_pte(kvm_pte_t *ptep) |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 141 | { |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 142 | WRITE_ONCE(*ptep, 0); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 145 | static void kvm_set_table_pte(kvm_pte_t *ptep, kvm_pte_t *childp, |
| 146 | struct kvm_pgtable_mm_ops *mm_ops) |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 147 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 148 | kvm_pte_t old = *ptep, pte = kvm_phys_to_pte(mm_ops->virt_to_phys(childp)); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 149 | |
| 150 | pte |= FIELD_PREP(KVM_PTE_TYPE, KVM_PTE_TYPE_TABLE); |
| 151 | pte |= KVM_PTE_VALID; |
| 152 | |
| 153 | WARN_ON(kvm_pte_valid(old)); |
| 154 | smp_store_release(ptep, pte); |
| 155 | } |
| 156 | |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 157 | static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level) |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 158 | { |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 159 | kvm_pte_t pte = kvm_phys_to_pte(pa); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 160 | u64 type = (level == KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYPE_PAGE : |
| 161 | KVM_PTE_TYPE_BLOCK; |
| 162 | |
| 163 | pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI); |
| 164 | pte |= FIELD_PREP(KVM_PTE_TYPE, type); |
| 165 | pte |= KVM_PTE_VALID; |
| 166 | |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 167 | return pte; |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 170 | static kvm_pte_t kvm_init_invalid_leaf_owner(u8 owner_id) |
| 171 | { |
| 172 | return FIELD_PREP(KVM_INVALID_PTE_OWNER_MASK, owner_id); |
| 173 | } |
| 174 | |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 175 | static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr, |
| 176 | u32 level, kvm_pte_t *ptep, |
| 177 | enum kvm_pgtable_walk_flags flag) |
| 178 | { |
| 179 | struct kvm_pgtable_walker *walker = data->walker; |
| 180 | return walker->cb(addr, data->end, level, ptep, flag, walker->arg); |
| 181 | } |
| 182 | |
| 183 | static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data, |
| 184 | kvm_pte_t *pgtable, u32 level); |
| 185 | |
| 186 | static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, |
| 187 | kvm_pte_t *ptep, u32 level) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | u64 addr = data->addr; |
| 191 | kvm_pte_t *childp, pte = *ptep; |
| 192 | bool table = kvm_pte_table(pte, level); |
| 193 | enum kvm_pgtable_walk_flags flags = data->walker->flags; |
| 194 | |
| 195 | if (table && (flags & KVM_PGTABLE_WALK_TABLE_PRE)) { |
| 196 | ret = kvm_pgtable_visitor_cb(data, addr, level, ptep, |
| 197 | KVM_PGTABLE_WALK_TABLE_PRE); |
| 198 | } |
| 199 | |
| 200 | if (!table && (flags & KVM_PGTABLE_WALK_LEAF)) { |
| 201 | ret = kvm_pgtable_visitor_cb(data, addr, level, ptep, |
| 202 | KVM_PGTABLE_WALK_LEAF); |
| 203 | pte = *ptep; |
| 204 | table = kvm_pte_table(pte, level); |
| 205 | } |
| 206 | |
| 207 | if (ret) |
| 208 | goto out; |
| 209 | |
| 210 | if (!table) { |
Jia He | 357ad20 | 2021-03-05 18:52:54 +0000 | [diff] [blame] | 211 | data->addr = ALIGN_DOWN(data->addr, kvm_granule_size(level)); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 212 | data->addr += kvm_granule_size(level); |
| 213 | goto out; |
| 214 | } |
| 215 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 216 | childp = kvm_pte_follow(pte, data->pgt->mm_ops); |
Will Deacon | b1e57de | 2020-09-11 14:25:10 +0100 | [diff] [blame] | 217 | ret = __kvm_pgtable_walk(data, childp, level + 1); |
| 218 | if (ret) |
| 219 | goto out; |
| 220 | |
| 221 | if (flags & KVM_PGTABLE_WALK_TABLE_POST) { |
| 222 | ret = kvm_pgtable_visitor_cb(data, addr, level, ptep, |
| 223 | KVM_PGTABLE_WALK_TABLE_POST); |
| 224 | } |
| 225 | |
| 226 | out: |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data, |
| 231 | kvm_pte_t *pgtable, u32 level) |
| 232 | { |
| 233 | u32 idx; |
| 234 | int ret = 0; |
| 235 | |
| 236 | if (WARN_ON_ONCE(level >= KVM_PGTABLE_MAX_LEVELS)) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | for (idx = kvm_pgtable_idx(data, level); idx < PTRS_PER_PTE; ++idx) { |
| 240 | kvm_pte_t *ptep = &pgtable[idx]; |
| 241 | |
| 242 | if (data->addr >= data->end) |
| 243 | break; |
| 244 | |
| 245 | ret = __kvm_pgtable_visit(data, ptep, level); |
| 246 | if (ret) |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static int _kvm_pgtable_walk(struct kvm_pgtable_walk_data *data) |
| 254 | { |
| 255 | u32 idx; |
| 256 | int ret = 0; |
| 257 | struct kvm_pgtable *pgt = data->pgt; |
| 258 | u64 limit = BIT(pgt->ia_bits); |
| 259 | |
| 260 | if (data->addr > limit || data->end > limit) |
| 261 | return -ERANGE; |
| 262 | |
| 263 | if (!pgt->pgd) |
| 264 | return -EINVAL; |
| 265 | |
| 266 | for (idx = kvm_pgd_page_idx(data); data->addr < data->end; ++idx) { |
| 267 | kvm_pte_t *ptep = &pgt->pgd[idx * PTRS_PER_PTE]; |
| 268 | |
| 269 | ret = __kvm_pgtable_walk(data, ptep, pgt->start_level); |
| 270 | if (ret) |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size, |
| 278 | struct kvm_pgtable_walker *walker) |
| 279 | { |
| 280 | struct kvm_pgtable_walk_data walk_data = { |
| 281 | .pgt = pgt, |
| 282 | .addr = ALIGN_DOWN(addr, PAGE_SIZE), |
| 283 | .end = PAGE_ALIGN(walk_data.addr + size), |
| 284 | .walker = walker, |
| 285 | }; |
| 286 | |
| 287 | return _kvm_pgtable_walk(&walk_data); |
| 288 | } |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 289 | |
Marc Zyngier | 63db506 | 2021-07-26 16:35:47 +0100 | [diff] [blame] | 290 | struct leaf_walk_data { |
| 291 | kvm_pte_t pte; |
| 292 | u32 level; |
| 293 | }; |
| 294 | |
| 295 | static int leaf_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 296 | enum kvm_pgtable_walk_flags flag, void * const arg) |
| 297 | { |
| 298 | struct leaf_walk_data *data = arg; |
| 299 | |
| 300 | data->pte = *ptep; |
| 301 | data->level = level; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr, |
| 307 | kvm_pte_t *ptep, u32 *level) |
| 308 | { |
| 309 | struct leaf_walk_data data; |
| 310 | struct kvm_pgtable_walker walker = { |
| 311 | .cb = leaf_walker, |
| 312 | .flags = KVM_PGTABLE_WALK_LEAF, |
| 313 | .arg = &data, |
| 314 | }; |
| 315 | int ret; |
| 316 | |
| 317 | ret = kvm_pgtable_walk(pgt, ALIGN_DOWN(addr, PAGE_SIZE), |
| 318 | PAGE_SIZE, &walker); |
| 319 | if (!ret) { |
| 320 | if (ptep) |
| 321 | *ptep = data.pte; |
| 322 | if (level) |
| 323 | *level = data.level; |
| 324 | } |
| 325 | |
| 326 | return ret; |
| 327 | } |
| 328 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 329 | struct hyp_map_data { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 330 | u64 phys; |
| 331 | kvm_pte_t attr; |
| 332 | struct kvm_pgtable_mm_ops *mm_ops; |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 333 | }; |
| 334 | |
Quentin Perret | 3fab823 | 2021-03-19 10:01:38 +0000 | [diff] [blame] | 335 | static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep) |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 336 | { |
| 337 | bool device = prot & KVM_PGTABLE_PROT_DEVICE; |
| 338 | u32 mtype = device ? MT_DEVICE_nGnRE : MT_NORMAL; |
| 339 | kvm_pte_t attr = FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX, mtype); |
| 340 | u32 sh = KVM_PTE_LEAF_ATTR_LO_S1_SH_IS; |
| 341 | u32 ap = (prot & KVM_PGTABLE_PROT_W) ? KVM_PTE_LEAF_ATTR_LO_S1_AP_RW : |
| 342 | KVM_PTE_LEAF_ATTR_LO_S1_AP_RO; |
| 343 | |
| 344 | if (!(prot & KVM_PGTABLE_PROT_R)) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | if (prot & KVM_PGTABLE_PROT_X) { |
| 348 | if (prot & KVM_PGTABLE_PROT_W) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | if (device) |
| 352 | return -EINVAL; |
| 353 | } else { |
| 354 | attr |= KVM_PTE_LEAF_ATTR_HI_S1_XN; |
| 355 | } |
| 356 | |
| 357 | attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_AP, ap); |
| 358 | attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_SH, sh); |
| 359 | attr |= KVM_PTE_LEAF_ATTR_LO_S1_AF; |
Quentin Perret | 4505e9b | 2021-08-09 16:24:38 +0100 | [diff] [blame^] | 360 | attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW; |
Quentin Perret | 3fab823 | 2021-03-19 10:01:38 +0000 | [diff] [blame] | 361 | *ptep = attr; |
| 362 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Quentin Perret | b53846c | 2021-08-09 16:24:36 +0100 | [diff] [blame] | 366 | static bool hyp_pte_needs_update(kvm_pte_t old, kvm_pte_t new) |
| 367 | { |
| 368 | /* |
| 369 | * Tolerate KVM recreating the exact same mapping, or changing software |
| 370 | * bits if the existing mapping was valid. |
| 371 | */ |
| 372 | if (old == new) |
| 373 | return false; |
| 374 | |
| 375 | if (!kvm_pte_valid(old)) |
| 376 | return true; |
| 377 | |
| 378 | return !WARN_ON((old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW); |
| 379 | } |
| 380 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 381 | static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level, |
| 382 | kvm_pte_t *ptep, struct hyp_map_data *data) |
| 383 | { |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 384 | kvm_pte_t new, old = *ptep; |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 385 | u64 granule = kvm_granule_size(level), phys = data->phys; |
| 386 | |
| 387 | if (!kvm_block_mapping_supported(addr, end, phys, level)) |
| 388 | return false; |
| 389 | |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 390 | new = kvm_init_valid_leaf_pte(phys, data->attr, level); |
Quentin Perret | b53846c | 2021-08-09 16:24:36 +0100 | [diff] [blame] | 391 | if (hyp_pte_needs_update(old, new)) |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 392 | smp_store_release(ptep, new); |
| 393 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 394 | data->phys += granule; |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | static int hyp_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 399 | enum kvm_pgtable_walk_flags flag, void * const arg) |
| 400 | { |
| 401 | kvm_pte_t *childp; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 402 | struct hyp_map_data *data = arg; |
| 403 | struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 404 | |
| 405 | if (hyp_map_walker_try_leaf(addr, end, level, ptep, arg)) |
| 406 | return 0; |
| 407 | |
| 408 | if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1)) |
| 409 | return -EINVAL; |
| 410 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 411 | childp = (kvm_pte_t *)mm_ops->zalloc_page(NULL); |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 412 | if (!childp) |
| 413 | return -ENOMEM; |
| 414 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 415 | kvm_set_table_pte(ptep, childp, mm_ops); |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys, |
| 420 | enum kvm_pgtable_prot prot) |
| 421 | { |
| 422 | int ret; |
| 423 | struct hyp_map_data map_data = { |
| 424 | .phys = ALIGN_DOWN(phys, PAGE_SIZE), |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 425 | .mm_ops = pgt->mm_ops, |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 426 | }; |
| 427 | struct kvm_pgtable_walker walker = { |
| 428 | .cb = hyp_map_walker, |
| 429 | .flags = KVM_PGTABLE_WALK_LEAF, |
| 430 | .arg = &map_data, |
| 431 | }; |
| 432 | |
Quentin Perret | 3fab823 | 2021-03-19 10:01:38 +0000 | [diff] [blame] | 433 | ret = hyp_set_prot_attr(prot, &map_data.attr); |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 434 | if (ret) |
| 435 | return ret; |
| 436 | |
| 437 | ret = kvm_pgtable_walk(pgt, addr, size, &walker); |
| 438 | dsb(ishst); |
| 439 | isb(); |
| 440 | return ret; |
| 441 | } |
| 442 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 443 | int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits, |
| 444 | struct kvm_pgtable_mm_ops *mm_ops) |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 445 | { |
| 446 | u64 levels = ARM64_HW_PGTABLE_LEVELS(va_bits); |
| 447 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 448 | pgt->pgd = (kvm_pte_t *)mm_ops->zalloc_page(NULL); |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 449 | if (!pgt->pgd) |
| 450 | return -ENOMEM; |
| 451 | |
| 452 | pgt->ia_bits = va_bits; |
| 453 | pgt->start_level = KVM_PGTABLE_MAX_LEVELS - levels; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 454 | pgt->mm_ops = mm_ops; |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 455 | pgt->mmu = NULL; |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 456 | pgt->force_pte_cb = NULL; |
| 457 | |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int hyp_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 462 | enum kvm_pgtable_walk_flags flag, void * const arg) |
| 463 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 464 | struct kvm_pgtable_mm_ops *mm_ops = arg; |
| 465 | |
| 466 | mm_ops->put_page((void *)kvm_pte_follow(*ptep, mm_ops)); |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt) |
| 471 | { |
| 472 | struct kvm_pgtable_walker walker = { |
| 473 | .cb = hyp_free_walker, |
| 474 | .flags = KVM_PGTABLE_WALK_TABLE_POST, |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 475 | .arg = pgt->mm_ops, |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker)); |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 479 | pgt->mm_ops->put_page(pgt->pgd); |
Will Deacon | bb0e92c | 2020-09-11 14:25:11 +0100 | [diff] [blame] | 480 | pgt->pgd = NULL; |
| 481 | } |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 482 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 483 | struct stage2_map_data { |
| 484 | u64 phys; |
| 485 | kvm_pte_t attr; |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 486 | u8 owner_id; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 487 | |
| 488 | kvm_pte_t *anchor; |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 489 | kvm_pte_t *childp; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 490 | |
| 491 | struct kvm_s2_mmu *mmu; |
Quentin Perret | e37f37a | 2021-03-19 10:01:33 +0000 | [diff] [blame] | 492 | void *memcache; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 493 | |
| 494 | struct kvm_pgtable_mm_ops *mm_ops; |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 495 | |
| 496 | /* Force mappings to page granularity */ |
| 497 | bool force_pte; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 498 | }; |
| 499 | |
Quentin Perret | bcb25a2 | 2021-03-19 10:01:30 +0000 | [diff] [blame] | 500 | u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift) |
| 501 | { |
| 502 | u64 vtcr = VTCR_EL2_FLAGS; |
| 503 | u8 lvls; |
| 504 | |
| 505 | vtcr |= kvm_get_parange(mmfr0) << VTCR_EL2_PS_SHIFT; |
| 506 | vtcr |= VTCR_EL2_T0SZ(phys_shift); |
| 507 | /* |
| 508 | * Use a minimum 2 level page table to prevent splitting |
| 509 | * host PMD huge pages at stage2. |
| 510 | */ |
| 511 | lvls = stage2_pgtable_levels(phys_shift); |
| 512 | if (lvls < 2) |
| 513 | lvls = 2; |
| 514 | vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls); |
| 515 | |
| 516 | /* |
| 517 | * Enable the Hardware Access Flag management, unconditionally |
| 518 | * on all CPUs. The features is RES0 on CPUs without the support |
| 519 | * and must be ignored by the CPUs. |
| 520 | */ |
| 521 | vtcr |= VTCR_EL2_HA; |
| 522 | |
| 523 | /* Set the vmid bits */ |
| 524 | vtcr |= (get_vmid_bits(mmfr1) == 16) ? |
| 525 | VTCR_EL2_VS_16BIT : |
| 526 | VTCR_EL2_VS_8BIT; |
| 527 | |
| 528 | return vtcr; |
| 529 | } |
| 530 | |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 531 | static bool stage2_has_fwb(struct kvm_pgtable *pgt) |
| 532 | { |
| 533 | if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) |
| 534 | return false; |
| 535 | |
| 536 | return !(pgt->flags & KVM_PGTABLE_S2_NOFWB); |
| 537 | } |
| 538 | |
| 539 | #define KVM_S2_MEMATTR(pgt, attr) PAGE_S2_MEMATTR(attr, stage2_has_fwb(pgt)) |
| 540 | |
| 541 | static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot prot, |
| 542 | kvm_pte_t *ptep) |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 543 | { |
| 544 | bool device = prot & KVM_PGTABLE_PROT_DEVICE; |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 545 | kvm_pte_t attr = device ? KVM_S2_MEMATTR(pgt, DEVICE_nGnRE) : |
| 546 | KVM_S2_MEMATTR(pgt, NORMAL); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 547 | u32 sh = KVM_PTE_LEAF_ATTR_LO_S2_SH_IS; |
| 548 | |
| 549 | if (!(prot & KVM_PGTABLE_PROT_X)) |
| 550 | attr |= KVM_PTE_LEAF_ATTR_HI_S2_XN; |
| 551 | else if (device) |
| 552 | return -EINVAL; |
| 553 | |
| 554 | if (prot & KVM_PGTABLE_PROT_R) |
| 555 | attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R; |
| 556 | |
| 557 | if (prot & KVM_PGTABLE_PROT_W) |
| 558 | attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W; |
| 559 | |
| 560 | attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh); |
| 561 | attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF; |
Quentin Perret | 4505e9b | 2021-08-09 16:24:38 +0100 | [diff] [blame^] | 562 | attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW; |
Quentin Perret | 3fab823 | 2021-03-19 10:01:38 +0000 | [diff] [blame] | 563 | *ptep = attr; |
| 564 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 568 | static bool stage2_pte_needs_update(kvm_pte_t old, kvm_pte_t new) |
| 569 | { |
| 570 | if (!kvm_pte_valid(old) || !kvm_pte_valid(new)) |
| 571 | return true; |
| 572 | |
| 573 | return ((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS)); |
| 574 | } |
| 575 | |
| 576 | static bool stage2_pte_is_counted(kvm_pte_t pte) |
| 577 | { |
| 578 | /* |
| 579 | * The refcount tracks valid entries as well as invalid entries if they |
| 580 | * encode ownership of a page to another entity than the page-table |
| 581 | * owner, whose id is 0. |
| 582 | */ |
| 583 | return !!pte; |
| 584 | } |
| 585 | |
| 586 | static void stage2_put_pte(kvm_pte_t *ptep, struct kvm_s2_mmu *mmu, u64 addr, |
| 587 | u32 level, struct kvm_pgtable_mm_ops *mm_ops) |
| 588 | { |
| 589 | /* |
| 590 | * Clear the existing PTE, and perform break-before-make with |
| 591 | * TLB maintenance if it was valid. |
| 592 | */ |
| 593 | if (kvm_pte_valid(*ptep)) { |
| 594 | kvm_clear_pte(ptep); |
| 595 | kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, addr, level); |
| 596 | } |
| 597 | |
| 598 | mm_ops->put_page(ptep); |
| 599 | } |
| 600 | |
Yanan Wang | 25aa286 | 2021-06-17 18:58:24 +0800 | [diff] [blame] | 601 | static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte) |
| 602 | { |
| 603 | u64 memattr = pte & KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR; |
| 604 | return memattr == KVM_S2_MEMATTR(pgt, NORMAL); |
| 605 | } |
| 606 | |
| 607 | static bool stage2_pte_executable(kvm_pte_t pte) |
| 608 | { |
| 609 | return !(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN); |
| 610 | } |
| 611 | |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 612 | static bool stage2_leaf_mapping_allowed(u64 addr, u64 end, u32 level, |
| 613 | struct stage2_map_data *data) |
| 614 | { |
| 615 | if (data->force_pte && (level < (KVM_PGTABLE_MAX_LEVELS - 1))) |
| 616 | return false; |
| 617 | |
| 618 | return kvm_block_mapping_supported(addr, end, data->phys, level); |
| 619 | } |
| 620 | |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 621 | static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, |
| 622 | kvm_pte_t *ptep, |
| 623 | struct stage2_map_data *data) |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 624 | { |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 625 | kvm_pte_t new, old = *ptep; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 626 | u64 granule = kvm_granule_size(level), phys = data->phys; |
Yanan Wang | 25aa286 | 2021-06-17 18:58:24 +0800 | [diff] [blame] | 627 | struct kvm_pgtable *pgt = data->mmu->pgt; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 628 | struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 629 | |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 630 | if (!stage2_leaf_mapping_allowed(addr, end, level, data)) |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 631 | return -E2BIG; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 632 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 633 | if (kvm_phys_is_valid(phys)) |
| 634 | new = kvm_init_valid_leaf_pte(phys, data->attr, level); |
| 635 | else |
| 636 | new = kvm_init_invalid_leaf_owner(data->owner_id); |
| 637 | |
| 638 | if (stage2_pte_is_counted(old)) { |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 639 | /* |
| 640 | * Skip updating the PTE if we are trying to recreate the exact |
| 641 | * same mapping or only change the access permissions. Instead, |
| 642 | * the vCPU will exit one more time from guest if still needed |
| 643 | * and then go through the path of relaxing permissions. |
| 644 | */ |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 645 | if (!stage2_pte_needs_update(old, new)) |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 646 | return -EAGAIN; |
Yanan Wang | 5c646b7 | 2020-12-02 04:10:32 +0800 | [diff] [blame] | 647 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 648 | stage2_put_pte(ptep, data->mmu, addr, level, mm_ops); |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 649 | } |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 650 | |
Yanan Wang | 25aa286 | 2021-06-17 18:58:24 +0800 | [diff] [blame] | 651 | /* Perform CMOs before installation of the guest stage-2 PTE */ |
| 652 | if (mm_ops->dcache_clean_inval_poc && stage2_pte_cacheable(pgt, new)) |
| 653 | mm_ops->dcache_clean_inval_poc(kvm_pte_follow(new, mm_ops), |
| 654 | granule); |
| 655 | |
| 656 | if (mm_ops->icache_inval_pou && stage2_pte_executable(new)) |
| 657 | mm_ops->icache_inval_pou(kvm_pte_follow(new, mm_ops), granule); |
| 658 | |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 659 | smp_store_release(ptep, new); |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 660 | if (stage2_pte_is_counted(new)) |
| 661 | mm_ops->get_page(ptep); |
| 662 | if (kvm_phys_is_valid(phys)) |
| 663 | data->phys += granule; |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 664 | return 0; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static int stage2_map_walk_table_pre(u64 addr, u64 end, u32 level, |
| 668 | kvm_pte_t *ptep, |
| 669 | struct stage2_map_data *data) |
| 670 | { |
| 671 | if (data->anchor) |
| 672 | return 0; |
| 673 | |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 674 | if (!stage2_leaf_mapping_allowed(addr, end, level, data)) |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 675 | return 0; |
| 676 | |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 677 | data->childp = kvm_pte_follow(*ptep, data->mm_ops); |
| 678 | kvm_clear_pte(ptep); |
Yanan Wang | 3a0b870 | 2020-12-02 04:10:33 +0800 | [diff] [blame] | 679 | |
| 680 | /* |
| 681 | * Invalidate the whole stage-2, as we may have numerous leaf |
| 682 | * entries below us which would otherwise need invalidating |
| 683 | * individually. |
| 684 | */ |
| 685 | kvm_call_hyp(__kvm_tlb_flush_vmid, data->mmu); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 686 | data->anchor = ptep; |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 691 | struct stage2_map_data *data) |
| 692 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 693 | struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 694 | kvm_pte_t *childp, pte = *ptep; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 695 | int ret; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 696 | |
| 697 | if (data->anchor) { |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 698 | if (stage2_pte_is_counted(pte)) |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 699 | mm_ops->put_page(ptep); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Yanan Wang | 694d071 | 2021-01-14 20:13:49 +0800 | [diff] [blame] | 704 | ret = stage2_map_walker_try_leaf(addr, end, level, ptep, data); |
| 705 | if (ret != -E2BIG) |
| 706 | return ret; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 707 | |
| 708 | if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1)) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | if (!data->memcache) |
| 712 | return -ENOMEM; |
| 713 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 714 | childp = mm_ops->zalloc_page(data->memcache); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 715 | if (!childp) |
| 716 | return -ENOMEM; |
| 717 | |
| 718 | /* |
| 719 | * If we've run into an existing block mapping then replace it with |
| 720 | * a table. Accesses beyond 'end' that fall within the new table |
| 721 | * will be mapped lazily. |
| 722 | */ |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 723 | if (stage2_pte_is_counted(pte)) |
| 724 | stage2_put_pte(ptep, data->mmu, addr, level, mm_ops); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 725 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 726 | kvm_set_table_pte(ptep, childp, mm_ops); |
| 727 | mm_ops->get_page(ptep); |
Yanan Wang | 8ed8005 | 2021-01-14 20:13:48 +0800 | [diff] [blame] | 728 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int stage2_map_walk_table_post(u64 addr, u64 end, u32 level, |
| 733 | kvm_pte_t *ptep, |
| 734 | struct stage2_map_data *data) |
| 735 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 736 | struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 737 | kvm_pte_t *childp; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 738 | int ret = 0; |
| 739 | |
| 740 | if (!data->anchor) |
| 741 | return 0; |
| 742 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 743 | if (data->anchor == ptep) { |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 744 | childp = data->childp; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 745 | data->anchor = NULL; |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 746 | data->childp = NULL; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 747 | ret = stage2_map_walk_leaf(addr, end, level, ptep, data); |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 748 | } else { |
| 749 | childp = kvm_pte_follow(*ptep, mm_ops); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Quentin Perret | f60ca2f | 2021-03-19 10:01:36 +0000 | [diff] [blame] | 752 | mm_ops->put_page(childp); |
| 753 | mm_ops->put_page(ptep); |
| 754 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * This is a little fiddly, as we use all three of the walk flags. The idea |
| 760 | * is that the TABLE_PRE callback runs for table entries on the way down, |
| 761 | * looking for table entries which we could conceivably replace with a |
| 762 | * block entry for this mapping. If it finds one, then it sets the 'anchor' |
| 763 | * field in 'struct stage2_map_data' to point at the table entry, before |
| 764 | * clearing the entry to zero and descending into the now detached table. |
| 765 | * |
| 766 | * The behaviour of the LEAF callback then depends on whether or not the |
| 767 | * anchor has been set. If not, then we're not using a block mapping higher |
| 768 | * up the table and we perform the mapping at the existing leaves instead. |
| 769 | * If, on the other hand, the anchor _is_ set, then we drop references to |
| 770 | * all valid leaves so that the pages beneath the anchor can be freed. |
| 771 | * |
| 772 | * Finally, the TABLE_POST callback does nothing if the anchor has not |
| 773 | * been set, but otherwise frees the page-table pages while walking back up |
| 774 | * the page-table, installing the block entry when it revisits the anchor |
| 775 | * pointer and clearing the anchor to NULL. |
| 776 | */ |
| 777 | static int stage2_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 778 | enum kvm_pgtable_walk_flags flag, void * const arg) |
| 779 | { |
| 780 | struct stage2_map_data *data = arg; |
| 781 | |
| 782 | switch (flag) { |
| 783 | case KVM_PGTABLE_WALK_TABLE_PRE: |
| 784 | return stage2_map_walk_table_pre(addr, end, level, ptep, data); |
| 785 | case KVM_PGTABLE_WALK_LEAF: |
| 786 | return stage2_map_walk_leaf(addr, end, level, ptep, data); |
| 787 | case KVM_PGTABLE_WALK_TABLE_POST: |
| 788 | return stage2_map_walk_table_post(addr, end, level, ptep, data); |
| 789 | } |
| 790 | |
| 791 | return -EINVAL; |
| 792 | } |
| 793 | |
| 794 | int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, |
| 795 | u64 phys, enum kvm_pgtable_prot prot, |
Quentin Perret | e37f37a | 2021-03-19 10:01:33 +0000 | [diff] [blame] | 796 | void *mc) |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 797 | { |
| 798 | int ret; |
| 799 | struct stage2_map_data map_data = { |
| 800 | .phys = ALIGN_DOWN(phys, PAGE_SIZE), |
| 801 | .mmu = pgt->mmu, |
| 802 | .memcache = mc, |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 803 | .mm_ops = pgt->mm_ops, |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 804 | .force_pte = pgt->force_pte_cb && pgt->force_pte_cb(addr, addr + size, prot), |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 805 | }; |
| 806 | struct kvm_pgtable_walker walker = { |
| 807 | .cb = stage2_map_walker, |
| 808 | .flags = KVM_PGTABLE_WALK_TABLE_PRE | |
| 809 | KVM_PGTABLE_WALK_LEAF | |
| 810 | KVM_PGTABLE_WALK_TABLE_POST, |
| 811 | .arg = &map_data, |
| 812 | }; |
| 813 | |
Quentin Perret | 8942a23 | 2021-03-19 10:01:41 +0000 | [diff] [blame] | 814 | if (WARN_ON((pgt->flags & KVM_PGTABLE_S2_IDMAP) && (addr != phys))) |
| 815 | return -EINVAL; |
| 816 | |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 817 | ret = stage2_set_prot_attr(pgt, prot, &map_data.attr); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 818 | if (ret) |
| 819 | return ret; |
| 820 | |
| 821 | ret = kvm_pgtable_walk(pgt, addr, size, &walker); |
| 822 | dsb(ishst); |
| 823 | return ret; |
| 824 | } |
| 825 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 826 | int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, |
| 827 | void *mc, u8 owner_id) |
| 828 | { |
| 829 | int ret; |
| 830 | struct stage2_map_data map_data = { |
| 831 | .phys = KVM_PHYS_INVALID, |
| 832 | .mmu = pgt->mmu, |
| 833 | .memcache = mc, |
| 834 | .mm_ops = pgt->mm_ops, |
| 835 | .owner_id = owner_id, |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 836 | .force_pte = true, |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 837 | }; |
| 838 | struct kvm_pgtable_walker walker = { |
| 839 | .cb = stage2_map_walker, |
| 840 | .flags = KVM_PGTABLE_WALK_TABLE_PRE | |
| 841 | KVM_PGTABLE_WALK_LEAF | |
| 842 | KVM_PGTABLE_WALK_TABLE_POST, |
| 843 | .arg = &map_data, |
| 844 | }; |
| 845 | |
| 846 | if (owner_id > KVM_MAX_OWNER_ID) |
| 847 | return -EINVAL; |
| 848 | |
| 849 | ret = kvm_pgtable_walk(pgt, addr, size, &walker); |
| 850 | return ret; |
| 851 | } |
| 852 | |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 853 | static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 854 | enum kvm_pgtable_walk_flags flag, |
| 855 | void * const arg) |
| 856 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 857 | struct kvm_pgtable *pgt = arg; |
| 858 | struct kvm_s2_mmu *mmu = pgt->mmu; |
| 859 | struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops; |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 860 | kvm_pte_t pte = *ptep, *childp = NULL; |
| 861 | bool need_flush = false; |
| 862 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 863 | if (!kvm_pte_valid(pte)) { |
| 864 | if (stage2_pte_is_counted(pte)) { |
| 865 | kvm_clear_pte(ptep); |
| 866 | mm_ops->put_page(ptep); |
| 867 | } |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 868 | return 0; |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 869 | } |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 870 | |
| 871 | if (kvm_pte_table(pte, level)) { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 872 | childp = kvm_pte_follow(pte, mm_ops); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 873 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 874 | if (mm_ops->page_count(childp) != 1) |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 875 | return 0; |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 876 | } else if (stage2_pte_cacheable(pgt, pte)) { |
| 877 | need_flush = !stage2_has_fwb(pgt); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
| 881 | * This is similar to the map() path in that we unmap the entire |
| 882 | * block entry and rely on the remaining portions being faulted |
| 883 | * back lazily. |
| 884 | */ |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 885 | stage2_put_pte(ptep, mmu, addr, level, mm_ops); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 886 | |
| 887 | if (need_flush) { |
Fuad Tabba | 814b186 | 2021-05-24 09:29:55 +0100 | [diff] [blame] | 888 | kvm_pte_t *pte_follow = kvm_pte_follow(pte, mm_ops); |
| 889 | |
Fuad Tabba | fade9c2 | 2021-05-24 09:30:01 +0100 | [diff] [blame] | 890 | dcache_clean_inval_poc((unsigned long)pte_follow, |
Fuad Tabba | 814b186 | 2021-05-24 09:29:55 +0100 | [diff] [blame] | 891 | (unsigned long)pte_follow + |
| 892 | kvm_granule_size(level)); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | if (childp) |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 896 | mm_ops->put_page(childp); |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size) |
| 902 | { |
| 903 | struct kvm_pgtable_walker walker = { |
| 904 | .cb = stage2_unmap_walker, |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 905 | .arg = pgt, |
Will Deacon | 6d9d211 | 2020-09-11 14:25:14 +0100 | [diff] [blame] | 906 | .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST, |
| 907 | }; |
| 908 | |
| 909 | return kvm_pgtable_walk(pgt, addr, size, &walker); |
| 910 | } |
| 911 | |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 912 | struct stage2_attr_data { |
Yanan Wang | a4d5ca5 | 2021-06-17 18:58:22 +0800 | [diff] [blame] | 913 | kvm_pte_t attr_set; |
| 914 | kvm_pte_t attr_clr; |
| 915 | kvm_pte_t pte; |
| 916 | u32 level; |
| 917 | struct kvm_pgtable_mm_ops *mm_ops; |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 918 | }; |
| 919 | |
| 920 | static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 921 | enum kvm_pgtable_walk_flags flag, |
| 922 | void * const arg) |
| 923 | { |
| 924 | kvm_pte_t pte = *ptep; |
| 925 | struct stage2_attr_data *data = arg; |
Yanan Wang | 25aa286 | 2021-06-17 18:58:24 +0800 | [diff] [blame] | 926 | struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 927 | |
| 928 | if (!kvm_pte_valid(pte)) |
| 929 | return 0; |
| 930 | |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 931 | data->level = level; |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 932 | data->pte = pte; |
| 933 | pte &= ~data->attr_clr; |
| 934 | pte |= data->attr_set; |
| 935 | |
| 936 | /* |
| 937 | * We may race with the CPU trying to set the access flag here, |
| 938 | * but worst-case the access flag update gets lost and will be |
| 939 | * set on the next access instead. |
| 940 | */ |
Yanan Wang | 25aa286 | 2021-06-17 18:58:24 +0800 | [diff] [blame] | 941 | if (data->pte != pte) { |
| 942 | /* |
| 943 | * Invalidate instruction cache before updating the guest |
| 944 | * stage-2 PTE if we are going to add executable permission. |
| 945 | */ |
| 946 | if (mm_ops->icache_inval_pou && |
| 947 | stage2_pte_executable(pte) && !stage2_pte_executable(*ptep)) |
| 948 | mm_ops->icache_inval_pou(kvm_pte_follow(pte, mm_ops), |
| 949 | kvm_granule_size(level)); |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 950 | WRITE_ONCE(*ptep, pte); |
Yanan Wang | 25aa286 | 2021-06-17 18:58:24 +0800 | [diff] [blame] | 951 | } |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr, |
| 957 | u64 size, kvm_pte_t attr_set, |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 958 | kvm_pte_t attr_clr, kvm_pte_t *orig_pte, |
| 959 | u32 *level) |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 960 | { |
| 961 | int ret; |
| 962 | kvm_pte_t attr_mask = KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI; |
| 963 | struct stage2_attr_data data = { |
| 964 | .attr_set = attr_set & attr_mask, |
| 965 | .attr_clr = attr_clr & attr_mask, |
Yanan Wang | a4d5ca5 | 2021-06-17 18:58:22 +0800 | [diff] [blame] | 966 | .mm_ops = pgt->mm_ops, |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 967 | }; |
| 968 | struct kvm_pgtable_walker walker = { |
| 969 | .cb = stage2_attr_walker, |
| 970 | .arg = &data, |
| 971 | .flags = KVM_PGTABLE_WALK_LEAF, |
| 972 | }; |
| 973 | |
| 974 | ret = kvm_pgtable_walk(pgt, addr, size, &walker); |
| 975 | if (ret) |
| 976 | return ret; |
| 977 | |
| 978 | if (orig_pte) |
| 979 | *orig_pte = data.pte; |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 980 | |
| 981 | if (level) |
| 982 | *level = data.level; |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 983 | return 0; |
| 984 | } |
| 985 | |
Quentin Perret | 73d49df | 2020-09-11 14:25:20 +0100 | [diff] [blame] | 986 | int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) |
| 987 | { |
| 988 | return stage2_update_leaf_attrs(pgt, addr, size, 0, |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 989 | KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W, |
| 990 | NULL, NULL); |
Quentin Perret | 73d49df | 2020-09-11 14:25:20 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 993 | kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr) |
| 994 | { |
| 995 | kvm_pte_t pte = 0; |
| 996 | stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0, |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 997 | &pte, NULL); |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 998 | dsb(ishst); |
| 999 | return pte; |
| 1000 | } |
| 1001 | |
| 1002 | kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr) |
| 1003 | { |
| 1004 | kvm_pte_t pte = 0; |
| 1005 | stage2_update_leaf_attrs(pgt, addr, 1, 0, KVM_PTE_LEAF_ATTR_LO_S2_AF, |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 1006 | &pte, NULL); |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 1007 | /* |
| 1008 | * "But where's the TLBI?!", you scream. |
| 1009 | * "Over in the core code", I sigh. |
| 1010 | * |
| 1011 | * See the '->clear_flush_young()' callback on the KVM mmu notifier. |
| 1012 | */ |
| 1013 | return pte; |
| 1014 | } |
| 1015 | |
| 1016 | bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr) |
| 1017 | { |
| 1018 | kvm_pte_t pte = 0; |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 1019 | stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte, NULL); |
Will Deacon | e0e5a07 | 2020-09-11 14:25:18 +0100 | [diff] [blame] | 1020 | return pte & KVM_PTE_LEAF_ATTR_LO_S2_AF; |
| 1021 | } |
| 1022 | |
Will Deacon | adcd4e2 | 2020-09-11 14:25:24 +0100 | [diff] [blame] | 1023 | int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr, |
| 1024 | enum kvm_pgtable_prot prot) |
| 1025 | { |
| 1026 | int ret; |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 1027 | u32 level; |
Will Deacon | adcd4e2 | 2020-09-11 14:25:24 +0100 | [diff] [blame] | 1028 | kvm_pte_t set = 0, clr = 0; |
| 1029 | |
Quentin Perret | 4505e9b | 2021-08-09 16:24:38 +0100 | [diff] [blame^] | 1030 | if (prot & KVM_PTE_LEAF_ATTR_HI_SW) |
| 1031 | return -EINVAL; |
| 1032 | |
Will Deacon | adcd4e2 | 2020-09-11 14:25:24 +0100 | [diff] [blame] | 1033 | if (prot & KVM_PGTABLE_PROT_R) |
| 1034 | set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R; |
| 1035 | |
| 1036 | if (prot & KVM_PGTABLE_PROT_W) |
| 1037 | set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W; |
| 1038 | |
| 1039 | if (prot & KVM_PGTABLE_PROT_X) |
| 1040 | clr |= KVM_PTE_LEAF_ATTR_HI_S2_XN; |
| 1041 | |
Will Deacon | b259d13 | 2020-09-30 14:18:01 +0100 | [diff] [blame] | 1042 | ret = stage2_update_leaf_attrs(pgt, addr, 1, set, clr, NULL, &level); |
| 1043 | if (!ret) |
| 1044 | kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, pgt->mmu, addr, level); |
Will Deacon | adcd4e2 | 2020-09-11 14:25:24 +0100 | [diff] [blame] | 1045 | return ret; |
| 1046 | } |
| 1047 | |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1048 | static int stage2_flush_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 1049 | enum kvm_pgtable_walk_flags flag, |
| 1050 | void * const arg) |
| 1051 | { |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 1052 | struct kvm_pgtable *pgt = arg; |
| 1053 | struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops; |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1054 | kvm_pte_t pte = *ptep; |
Fuad Tabba | 814b186 | 2021-05-24 09:29:55 +0100 | [diff] [blame] | 1055 | kvm_pte_t *pte_follow; |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1056 | |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 1057 | if (!kvm_pte_valid(pte) || !stage2_pte_cacheable(pgt, pte)) |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1058 | return 0; |
| 1059 | |
Fuad Tabba | 814b186 | 2021-05-24 09:29:55 +0100 | [diff] [blame] | 1060 | pte_follow = kvm_pte_follow(pte, mm_ops); |
Fuad Tabba | fade9c2 | 2021-05-24 09:30:01 +0100 | [diff] [blame] | 1061 | dcache_clean_inval_poc((unsigned long)pte_follow, |
Fuad Tabba | 814b186 | 2021-05-24 09:29:55 +0100 | [diff] [blame] | 1062 | (unsigned long)pte_follow + |
| 1063 | kvm_granule_size(level)); |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size) |
| 1068 | { |
| 1069 | struct kvm_pgtable_walker walker = { |
| 1070 | .cb = stage2_flush_walker, |
| 1071 | .flags = KVM_PGTABLE_WALK_LEAF, |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 1072 | .arg = pgt, |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1073 | }; |
| 1074 | |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 1075 | if (stage2_has_fwb(pgt)) |
Quentin Perret | 93c66b4 | 2020-09-11 14:25:22 +0100 | [diff] [blame] | 1076 | return 0; |
| 1077 | |
| 1078 | return kvm_pgtable_walk(pgt, addr, size, &walker); |
| 1079 | } |
| 1080 | |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 1081 | |
| 1082 | int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_arch *arch, |
| 1083 | struct kvm_pgtable_mm_ops *mm_ops, |
| 1084 | enum kvm_pgtable_stage2_flags flags, |
| 1085 | kvm_pgtable_force_pte_cb_t force_pte_cb) |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1086 | { |
| 1087 | size_t pgd_sz; |
Quentin Perret | 834cd93 | 2021-03-19 10:01:27 +0000 | [diff] [blame] | 1088 | u64 vtcr = arch->vtcr; |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1089 | u32 ia_bits = VTCR_EL2_IPA(vtcr); |
| 1090 | u32 sl0 = FIELD_GET(VTCR_EL2_SL0_MASK, vtcr); |
| 1091 | u32 start_level = VTCR_EL2_TGRAN_SL0_BASE - sl0; |
| 1092 | |
| 1093 | pgd_sz = kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1094 | pgt->pgd = mm_ops->zalloc_pages_exact(pgd_sz); |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1095 | if (!pgt->pgd) |
| 1096 | return -ENOMEM; |
| 1097 | |
| 1098 | pgt->ia_bits = ia_bits; |
| 1099 | pgt->start_level = start_level; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1100 | pgt->mm_ops = mm_ops; |
Quentin Perret | 834cd93 | 2021-03-19 10:01:27 +0000 | [diff] [blame] | 1101 | pgt->mmu = &arch->mmu; |
Quentin Perret | bc224df | 2021-03-19 10:01:40 +0000 | [diff] [blame] | 1102 | pgt->flags = flags; |
Quentin Perret | 5651311 | 2021-08-09 16:24:37 +0100 | [diff] [blame] | 1103 | pgt->force_pte_cb = force_pte_cb; |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1104 | |
| 1105 | /* Ensure zeroed PGD pages are visible to the hardware walker */ |
| 1106 | dsb(ishst); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | static int stage2_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, |
| 1111 | enum kvm_pgtable_walk_flags flag, |
| 1112 | void * const arg) |
| 1113 | { |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1114 | struct kvm_pgtable_mm_ops *mm_ops = arg; |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1115 | kvm_pte_t pte = *ptep; |
| 1116 | |
Quentin Perret | 807923e | 2021-03-19 10:01:37 +0000 | [diff] [blame] | 1117 | if (!stage2_pte_is_counted(pte)) |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1118 | return 0; |
| 1119 | |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1120 | mm_ops->put_page(ptep); |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1121 | |
| 1122 | if (kvm_pte_table(pte, level)) |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1123 | mm_ops->put_page(kvm_pte_follow(pte, mm_ops)); |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1124 | |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt) |
| 1129 | { |
| 1130 | size_t pgd_sz; |
| 1131 | struct kvm_pgtable_walker walker = { |
| 1132 | .cb = stage2_free_walker, |
| 1133 | .flags = KVM_PGTABLE_WALK_LEAF | |
| 1134 | KVM_PGTABLE_WALK_TABLE_POST, |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1135 | .arg = pgt->mm_ops, |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1136 | }; |
| 1137 | |
| 1138 | WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker)); |
| 1139 | pgd_sz = kvm_pgd_pages(pgt->ia_bits, pgt->start_level) * PAGE_SIZE; |
Quentin Perret | 7aef0cb | 2021-03-19 10:01:14 +0000 | [diff] [blame] | 1140 | pgt->mm_ops->free_pages_exact(pgt->pgd, pgd_sz); |
Will Deacon | 71233d0 | 2020-09-11 14:25:13 +0100 | [diff] [blame] | 1141 | pgt->pgd = NULL; |
| 1142 | } |