Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 3 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 18 | |
| 19 | #include <linux/mman.h> |
| 20 | #include <linux/kvm_host.h> |
| 21 | #include <linux/io.h> |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 22 | #include <linux/hugetlb.h> |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 23 | #include <trace/events/kvm.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 24 | #include <asm/pgalloc.h> |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 25 | #include <asm/cacheflush.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 26 | #include <asm/kvm_arm.h> |
| 27 | #include <asm/kvm_mmu.h> |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 28 | #include <asm/kvm_mmio.h> |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 29 | #include <asm/kvm_asm.h> |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 30 | #include <asm/kvm_emulate.h> |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 31 | |
| 32 | #include "trace.h" |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 33 | |
| 34 | extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[]; |
| 35 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 36 | static pgd_t *boot_hyp_pgd; |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 37 | static pgd_t *hyp_pgd; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 38 | static DEFINE_MUTEX(kvm_hyp_pgd_mutex); |
| 39 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 40 | static void *init_bounce_page; |
| 41 | static unsigned long hyp_idmap_start; |
| 42 | static unsigned long hyp_idmap_end; |
| 43 | static phys_addr_t hyp_idmap_vector; |
| 44 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 45 | #define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t)) |
Mark Salter | 5d4e08c | 2014-03-28 14:25:19 +0000 | [diff] [blame] | 46 | |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 47 | #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x)) |
Mario Smarduch | c647355 | 2015-01-15 15:58:56 -0800 | [diff] [blame] | 48 | #define kvm_pud_huge(_x) pud_huge(_x) |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 49 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 50 | #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0) |
| 51 | #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1) |
| 52 | |
| 53 | static bool memslot_is_logging(struct kvm_memory_slot *memslot) |
| 54 | { |
| 55 | #ifdef CONFIG_ARM |
| 56 | return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY); |
| 57 | #else |
| 58 | return false; |
| 59 | #endif |
| 60 | } |
| 61 | |
Marc Zyngier | 4876276 | 2013-01-28 15:27:00 +0000 | [diff] [blame] | 62 | static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa) |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 63 | { |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 64 | /* |
| 65 | * This function also gets called when dealing with HYP page |
| 66 | * tables. As HYP doesn't have an associated struct kvm (and |
| 67 | * the HYP page tables are fairly static), we don't do |
| 68 | * anything there. |
| 69 | */ |
| 70 | if (kvm) |
| 71 | kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 74 | /** |
| 75 | * stage2_dissolve_pmd() - clear and flush huge PMD entry |
| 76 | * @kvm: pointer to kvm structure. |
| 77 | * @addr: IPA |
| 78 | * @pmd: pmd pointer for IPA |
| 79 | * |
| 80 | * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all |
| 81 | * pages in the range dirty. |
| 82 | */ |
| 83 | static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd) |
| 84 | { |
| 85 | if (!kvm_pmd_huge(*pmd)) |
| 86 | return; |
| 87 | |
| 88 | pmd_clear(pmd); |
| 89 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 90 | put_page(virt_to_page(pmd)); |
| 91 | } |
| 92 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 93 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
| 94 | int min, int max) |
| 95 | { |
| 96 | void *page; |
| 97 | |
| 98 | BUG_ON(max > KVM_NR_MEM_OBJS); |
| 99 | if (cache->nobjs >= min) |
| 100 | return 0; |
| 101 | while (cache->nobjs < max) { |
| 102 | page = (void *)__get_free_page(PGALLOC_GFP); |
| 103 | if (!page) |
| 104 | return -ENOMEM; |
| 105 | cache->objects[cache->nobjs++] = page; |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) |
| 111 | { |
| 112 | while (mc->nobjs) |
| 113 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
| 114 | } |
| 115 | |
| 116 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc) |
| 117 | { |
| 118 | void *p; |
| 119 | |
| 120 | BUG_ON(!mc || !mc->nobjs); |
| 121 | p = mc->objects[--mc->nobjs]; |
| 122 | return p; |
| 123 | } |
| 124 | |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 125 | static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr) |
Marc Zyngier | 979acd5 | 2013-08-06 13:05:48 +0100 | [diff] [blame] | 126 | { |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 127 | pud_t *pud_table __maybe_unused = pud_offset(pgd, 0); |
| 128 | pgd_clear(pgd); |
| 129 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 130 | pud_free(NULL, pud_table); |
| 131 | put_page(virt_to_page(pgd)); |
Marc Zyngier | 979acd5 | 2013-08-06 13:05:48 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 134 | static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 135 | { |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 136 | pmd_t *pmd_table = pmd_offset(pud, 0); |
| 137 | VM_BUG_ON(pud_huge(*pud)); |
| 138 | pud_clear(pud); |
| 139 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 140 | pmd_free(NULL, pmd_table); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 141 | put_page(virt_to_page(pud)); |
| 142 | } |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 143 | |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 144 | static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr) |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 145 | { |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 146 | pte_t *pte_table = pte_offset_kernel(pmd, 0); |
| 147 | VM_BUG_ON(kvm_pmd_huge(*pmd)); |
| 148 | pmd_clear(pmd); |
| 149 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 150 | pte_free_kernel(NULL, pte_table); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 151 | put_page(virt_to_page(pmd)); |
| 152 | } |
| 153 | |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 154 | static void unmap_ptes(struct kvm *kvm, pmd_t *pmd, |
| 155 | phys_addr_t addr, phys_addr_t end) |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 156 | { |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 157 | phys_addr_t start_addr = addr; |
| 158 | pte_t *pte, *start_pte; |
| 159 | |
| 160 | start_pte = pte = pte_offset_kernel(pmd, addr); |
| 161 | do { |
| 162 | if (!pte_none(*pte)) { |
| 163 | kvm_set_pte(pte, __pte(0)); |
| 164 | put_page(virt_to_page(pte)); |
| 165 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 166 | } |
| 167 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 168 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 169 | if (kvm_pte_table_empty(kvm, start_pte)) |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 170 | clear_pmd_entry(kvm, pmd, start_addr); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 173 | static void unmap_pmds(struct kvm *kvm, pud_t *pud, |
| 174 | phys_addr_t addr, phys_addr_t end) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 175 | { |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 176 | phys_addr_t next, start_addr = addr; |
| 177 | pmd_t *pmd, *start_pmd; |
Marc Zyngier | 000d399 | 2013-03-05 02:43:17 +0000 | [diff] [blame] | 178 | |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 179 | start_pmd = pmd = pmd_offset(pud, addr); |
| 180 | do { |
| 181 | next = kvm_pmd_addr_end(addr, end); |
| 182 | if (!pmd_none(*pmd)) { |
| 183 | if (kvm_pmd_huge(*pmd)) { |
| 184 | pmd_clear(pmd); |
| 185 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 186 | put_page(virt_to_page(pmd)); |
| 187 | } else { |
| 188 | unmap_ptes(kvm, pmd, addr, next); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 191 | } while (pmd++, addr = next, addr != end); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 192 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 193 | if (kvm_pmd_table_empty(kvm, start_pmd)) |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 194 | clear_pud_entry(kvm, pud, start_addr); |
| 195 | } |
| 196 | |
| 197 | static void unmap_puds(struct kvm *kvm, pgd_t *pgd, |
| 198 | phys_addr_t addr, phys_addr_t end) |
| 199 | { |
| 200 | phys_addr_t next, start_addr = addr; |
| 201 | pud_t *pud, *start_pud; |
| 202 | |
| 203 | start_pud = pud = pud_offset(pgd, addr); |
| 204 | do { |
| 205 | next = kvm_pud_addr_end(addr, end); |
| 206 | if (!pud_none(*pud)) { |
| 207 | if (pud_huge(*pud)) { |
| 208 | pud_clear(pud); |
| 209 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 210 | put_page(virt_to_page(pud)); |
| 211 | } else { |
| 212 | unmap_pmds(kvm, pud, addr, next); |
| 213 | } |
| 214 | } |
| 215 | } while (pud++, addr = next, addr != end); |
| 216 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 217 | if (kvm_pud_table_empty(kvm, start_pud)) |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 218 | clear_pgd_entry(kvm, pgd, start_addr); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static void unmap_range(struct kvm *kvm, pgd_t *pgdp, |
| 223 | phys_addr_t start, u64 size) |
| 224 | { |
| 225 | pgd_t *pgd; |
| 226 | phys_addr_t addr = start, end = start + size; |
| 227 | phys_addr_t next; |
| 228 | |
| 229 | pgd = pgdp + pgd_index(addr); |
| 230 | do { |
| 231 | next = kvm_pgd_addr_end(addr, end); |
Mark Rutland | 7cbb87d | 2014-10-28 19:36:45 +0000 | [diff] [blame] | 232 | if (!pgd_none(*pgd)) |
| 233 | unmap_puds(kvm, pgd, addr, next); |
Christoffer Dall | 4f853a7 | 2014-05-09 23:31:31 +0200 | [diff] [blame] | 234 | } while (pgd++, addr = next, addr != end); |
Marc Zyngier | 000d399 | 2013-03-05 02:43:17 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Marc Zyngier | 9d218a1 | 2014-01-15 12:50:23 +0000 | [diff] [blame] | 237 | static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd, |
| 238 | phys_addr_t addr, phys_addr_t end) |
| 239 | { |
| 240 | pte_t *pte; |
| 241 | |
| 242 | pte = pte_offset_kernel(pmd, addr); |
| 243 | do { |
| 244 | if (!pte_none(*pte)) { |
| 245 | hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT); |
| 246 | kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE); |
| 247 | } |
| 248 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 249 | } |
| 250 | |
| 251 | static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud, |
| 252 | phys_addr_t addr, phys_addr_t end) |
| 253 | { |
| 254 | pmd_t *pmd; |
| 255 | phys_addr_t next; |
| 256 | |
| 257 | pmd = pmd_offset(pud, addr); |
| 258 | do { |
| 259 | next = kvm_pmd_addr_end(addr, end); |
| 260 | if (!pmd_none(*pmd)) { |
| 261 | if (kvm_pmd_huge(*pmd)) { |
| 262 | hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT); |
| 263 | kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE); |
| 264 | } else { |
| 265 | stage2_flush_ptes(kvm, pmd, addr, next); |
| 266 | } |
| 267 | } |
| 268 | } while (pmd++, addr = next, addr != end); |
| 269 | } |
| 270 | |
| 271 | static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd, |
| 272 | phys_addr_t addr, phys_addr_t end) |
| 273 | { |
| 274 | pud_t *pud; |
| 275 | phys_addr_t next; |
| 276 | |
| 277 | pud = pud_offset(pgd, addr); |
| 278 | do { |
| 279 | next = kvm_pud_addr_end(addr, end); |
| 280 | if (!pud_none(*pud)) { |
| 281 | if (pud_huge(*pud)) { |
| 282 | hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT); |
| 283 | kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE); |
| 284 | } else { |
| 285 | stage2_flush_pmds(kvm, pud, addr, next); |
| 286 | } |
| 287 | } |
| 288 | } while (pud++, addr = next, addr != end); |
| 289 | } |
| 290 | |
| 291 | static void stage2_flush_memslot(struct kvm *kvm, |
| 292 | struct kvm_memory_slot *memslot) |
| 293 | { |
| 294 | phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT; |
| 295 | phys_addr_t end = addr + PAGE_SIZE * memslot->npages; |
| 296 | phys_addr_t next; |
| 297 | pgd_t *pgd; |
| 298 | |
| 299 | pgd = kvm->arch.pgd + pgd_index(addr); |
| 300 | do { |
| 301 | next = kvm_pgd_addr_end(addr, end); |
| 302 | stage2_flush_puds(kvm, pgd, addr, next); |
| 303 | } while (pgd++, addr = next, addr != end); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * stage2_flush_vm - Invalidate cache for pages mapped in stage 2 |
| 308 | * @kvm: The struct kvm pointer |
| 309 | * |
| 310 | * Go through the stage 2 page tables and invalidate any cache lines |
| 311 | * backing memory already mapped to the VM. |
| 312 | */ |
| 313 | void stage2_flush_vm(struct kvm *kvm) |
| 314 | { |
| 315 | struct kvm_memslots *slots; |
| 316 | struct kvm_memory_slot *memslot; |
| 317 | int idx; |
| 318 | |
| 319 | idx = srcu_read_lock(&kvm->srcu); |
| 320 | spin_lock(&kvm->mmu_lock); |
| 321 | |
| 322 | slots = kvm_memslots(kvm); |
| 323 | kvm_for_each_memslot(memslot, slots) |
| 324 | stage2_flush_memslot(kvm, memslot); |
| 325 | |
| 326 | spin_unlock(&kvm->mmu_lock); |
| 327 | srcu_read_unlock(&kvm->srcu, idx); |
| 328 | } |
| 329 | |
Marc Zyngier | 000d399 | 2013-03-05 02:43:17 +0000 | [diff] [blame] | 330 | /** |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 331 | * free_boot_hyp_pgd - free HYP boot page tables |
| 332 | * |
| 333 | * Free the HYP boot page tables. The bounce page is also freed. |
| 334 | */ |
| 335 | void free_boot_hyp_pgd(void) |
| 336 | { |
| 337 | mutex_lock(&kvm_hyp_pgd_mutex); |
| 338 | |
| 339 | if (boot_hyp_pgd) { |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 340 | unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE); |
| 341 | unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE); |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 342 | free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order); |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 343 | boot_hyp_pgd = NULL; |
| 344 | } |
| 345 | |
| 346 | if (hyp_pgd) |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 347 | unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE); |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 348 | |
Mark Salter | 5d4e08c | 2014-03-28 14:25:19 +0000 | [diff] [blame] | 349 | free_page((unsigned long)init_bounce_page); |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 350 | init_bounce_page = NULL; |
| 351 | |
| 352 | mutex_unlock(&kvm_hyp_pgd_mutex); |
| 353 | } |
| 354 | |
| 355 | /** |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 356 | * free_hyp_pgds - free Hyp-mode page tables |
Marc Zyngier | 000d399 | 2013-03-05 02:43:17 +0000 | [diff] [blame] | 357 | * |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 358 | * Assumes hyp_pgd is a page table used strictly in Hyp-mode and |
| 359 | * therefore contains either mappings in the kernel memory area (above |
| 360 | * PAGE_OFFSET), or device mappings in the vmalloc range (from |
| 361 | * VMALLOC_START to VMALLOC_END). |
| 362 | * |
| 363 | * boot_hyp_pgd should only map two pages for the init code. |
Marc Zyngier | 000d399 | 2013-03-05 02:43:17 +0000 | [diff] [blame] | 364 | */ |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 365 | void free_hyp_pgds(void) |
Marc Zyngier | 000d399 | 2013-03-05 02:43:17 +0000 | [diff] [blame] | 366 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 367 | unsigned long addr; |
| 368 | |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 369 | free_boot_hyp_pgd(); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 370 | |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 371 | mutex_lock(&kvm_hyp_pgd_mutex); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 372 | |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 373 | if (hyp_pgd) { |
| 374 | for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE) |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 375 | unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 376 | for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE) |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 377 | unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE); |
| 378 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 379 | free_pages((unsigned long)hyp_pgd, hyp_pgd_order); |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 380 | hyp_pgd = NULL; |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 383 | mutex_unlock(&kvm_hyp_pgd_mutex); |
| 384 | } |
| 385 | |
| 386 | static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start, |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 387 | unsigned long end, unsigned long pfn, |
| 388 | pgprot_t prot) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 389 | { |
| 390 | pte_t *pte; |
| 391 | unsigned long addr; |
| 392 | |
Marc Zyngier | 3562c76 | 2013-04-12 19:12:02 +0100 | [diff] [blame] | 393 | addr = start; |
| 394 | do { |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 395 | pte = pte_offset_kernel(pmd, addr); |
| 396 | kvm_set_pte(pte, pfn_pte(pfn, prot)); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 397 | get_page(virt_to_page(pte)); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 398 | kvm_flush_dcache_to_poc(pte, sizeof(*pte)); |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 399 | pfn++; |
Marc Zyngier | 3562c76 | 2013-04-12 19:12:02 +0100 | [diff] [blame] | 400 | } while (addr += PAGE_SIZE, addr != end); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start, |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 404 | unsigned long end, unsigned long pfn, |
| 405 | pgprot_t prot) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 406 | { |
| 407 | pmd_t *pmd; |
| 408 | pte_t *pte; |
| 409 | unsigned long addr, next; |
| 410 | |
Marc Zyngier | 3562c76 | 2013-04-12 19:12:02 +0100 | [diff] [blame] | 411 | addr = start; |
| 412 | do { |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 413 | pmd = pmd_offset(pud, addr); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 414 | |
| 415 | BUG_ON(pmd_sect(*pmd)); |
| 416 | |
| 417 | if (pmd_none(*pmd)) { |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 418 | pte = pte_alloc_one_kernel(NULL, addr); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 419 | if (!pte) { |
| 420 | kvm_err("Cannot allocate Hyp pte\n"); |
| 421 | return -ENOMEM; |
| 422 | } |
| 423 | pmd_populate_kernel(NULL, pmd, pte); |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 424 | get_page(virt_to_page(pmd)); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 425 | kvm_flush_dcache_to_poc(pmd, sizeof(*pmd)); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | next = pmd_addr_end(addr, end); |
| 429 | |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 430 | create_hyp_pte_mappings(pmd, addr, next, pfn, prot); |
| 431 | pfn += (next - addr) >> PAGE_SHIFT; |
Marc Zyngier | 3562c76 | 2013-04-12 19:12:02 +0100 | [diff] [blame] | 432 | } while (addr = next, addr != end); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 437 | static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start, |
| 438 | unsigned long end, unsigned long pfn, |
| 439 | pgprot_t prot) |
| 440 | { |
| 441 | pud_t *pud; |
| 442 | pmd_t *pmd; |
| 443 | unsigned long addr, next; |
| 444 | int ret; |
| 445 | |
| 446 | addr = start; |
| 447 | do { |
| 448 | pud = pud_offset(pgd, addr); |
| 449 | |
| 450 | if (pud_none_or_clear_bad(pud)) { |
| 451 | pmd = pmd_alloc_one(NULL, addr); |
| 452 | if (!pmd) { |
| 453 | kvm_err("Cannot allocate Hyp pmd\n"); |
| 454 | return -ENOMEM; |
| 455 | } |
| 456 | pud_populate(NULL, pud, pmd); |
| 457 | get_page(virt_to_page(pud)); |
| 458 | kvm_flush_dcache_to_poc(pud, sizeof(*pud)); |
| 459 | } |
| 460 | |
| 461 | next = pud_addr_end(addr, end); |
| 462 | ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot); |
| 463 | if (ret) |
| 464 | return ret; |
| 465 | pfn += (next - addr) >> PAGE_SHIFT; |
| 466 | } while (addr = next, addr != end); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 471 | static int __create_hyp_mappings(pgd_t *pgdp, |
| 472 | unsigned long start, unsigned long end, |
| 473 | unsigned long pfn, pgprot_t prot) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 474 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 475 | pgd_t *pgd; |
| 476 | pud_t *pud; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 477 | unsigned long addr, next; |
| 478 | int err = 0; |
| 479 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 480 | mutex_lock(&kvm_hyp_pgd_mutex); |
Marc Zyngier | 3562c76 | 2013-04-12 19:12:02 +0100 | [diff] [blame] | 481 | addr = start & PAGE_MASK; |
| 482 | end = PAGE_ALIGN(end); |
| 483 | do { |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 484 | pgd = pgdp + pgd_index(addr); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 485 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 486 | if (pgd_none(*pgd)) { |
| 487 | pud = pud_alloc_one(NULL, addr); |
| 488 | if (!pud) { |
| 489 | kvm_err("Cannot allocate Hyp pud\n"); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 490 | err = -ENOMEM; |
| 491 | goto out; |
| 492 | } |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 493 | pgd_populate(NULL, pgd, pud); |
| 494 | get_page(virt_to_page(pgd)); |
| 495 | kvm_flush_dcache_to_poc(pgd, sizeof(*pgd)); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | next = pgd_addr_end(addr, end); |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 499 | err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 500 | if (err) |
| 501 | goto out; |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 502 | pfn += (next - addr) >> PAGE_SHIFT; |
Marc Zyngier | 3562c76 | 2013-04-12 19:12:02 +0100 | [diff] [blame] | 503 | } while (addr = next, addr != end); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 504 | out: |
| 505 | mutex_unlock(&kvm_hyp_pgd_mutex); |
| 506 | return err; |
| 507 | } |
| 508 | |
Christoffer Dall | 40c2729 | 2013-11-15 13:14:12 -0800 | [diff] [blame] | 509 | static phys_addr_t kvm_kaddr_to_phys(void *kaddr) |
| 510 | { |
| 511 | if (!is_vmalloc_addr(kaddr)) { |
| 512 | BUG_ON(!virt_addr_valid(kaddr)); |
| 513 | return __pa(kaddr); |
| 514 | } else { |
| 515 | return page_to_phys(vmalloc_to_page(kaddr)) + |
| 516 | offset_in_page(kaddr); |
| 517 | } |
| 518 | } |
| 519 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 520 | /** |
Marc Zyngier | 06e8c3b | 2012-10-28 01:09:14 +0100 | [diff] [blame] | 521 | * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 522 | * @from: The virtual kernel start address of the range |
| 523 | * @to: The virtual kernel end address of the range (exclusive) |
| 524 | * |
Marc Zyngier | 06e8c3b | 2012-10-28 01:09:14 +0100 | [diff] [blame] | 525 | * The same virtual address as the kernel virtual address is also used |
| 526 | * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying |
| 527 | * physical pages. |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 528 | */ |
| 529 | int create_hyp_mappings(void *from, void *to) |
| 530 | { |
Christoffer Dall | 40c2729 | 2013-11-15 13:14:12 -0800 | [diff] [blame] | 531 | phys_addr_t phys_addr; |
| 532 | unsigned long virt_addr; |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 533 | unsigned long start = KERN_TO_HYP((unsigned long)from); |
| 534 | unsigned long end = KERN_TO_HYP((unsigned long)to); |
| 535 | |
Christoffer Dall | 40c2729 | 2013-11-15 13:14:12 -0800 | [diff] [blame] | 536 | start = start & PAGE_MASK; |
| 537 | end = PAGE_ALIGN(end); |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 538 | |
Christoffer Dall | 40c2729 | 2013-11-15 13:14:12 -0800 | [diff] [blame] | 539 | for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) { |
| 540 | int err; |
| 541 | |
| 542 | phys_addr = kvm_kaddr_to_phys(from + virt_addr - start); |
| 543 | err = __create_hyp_mappings(hyp_pgd, virt_addr, |
| 544 | virt_addr + PAGE_SIZE, |
| 545 | __phys_to_pfn(phys_addr), |
| 546 | PAGE_HYP); |
| 547 | if (err) |
| 548 | return err; |
| 549 | } |
| 550 | |
| 551 | return 0; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
Marc Zyngier | 06e8c3b | 2012-10-28 01:09:14 +0100 | [diff] [blame] | 555 | * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode |
| 556 | * @from: The kernel start VA of the range |
| 557 | * @to: The kernel end VA of the range (exclusive) |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 558 | * @phys_addr: The physical start address which gets mapped |
Marc Zyngier | 06e8c3b | 2012-10-28 01:09:14 +0100 | [diff] [blame] | 559 | * |
| 560 | * The resulting HYP VA is the same as the kernel VA, modulo |
| 561 | * HYP_PAGE_OFFSET. |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 562 | */ |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 563 | int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 564 | { |
Marc Zyngier | 6060df8 | 2013-04-12 19:12:01 +0100 | [diff] [blame] | 565 | unsigned long start = KERN_TO_HYP((unsigned long)from); |
| 566 | unsigned long end = KERN_TO_HYP((unsigned long)to); |
| 567 | |
| 568 | /* Check for a valid kernel IO mapping */ |
| 569 | if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1)) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | return __create_hyp_mappings(hyp_pgd, start, end, |
| 573 | __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 574 | } |
| 575 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 576 | /** |
| 577 | * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation. |
| 578 | * @kvm: The KVM struct pointer for the VM. |
| 579 | * |
| 580 | * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can |
| 581 | * support either full 40-bit input addresses or limited to 32-bit input |
| 582 | * addresses). Clears the allocated pages. |
| 583 | * |
| 584 | * Note we don't need locking here as this is only called when the VM is |
| 585 | * created, which can only be done once. |
| 586 | */ |
| 587 | int kvm_alloc_stage2_pgd(struct kvm *kvm) |
| 588 | { |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 589 | int ret; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 590 | pgd_t *pgd; |
| 591 | |
| 592 | if (kvm->arch.pgd != NULL) { |
| 593 | kvm_err("kvm_arch already initialized?\n"); |
| 594 | return -EINVAL; |
| 595 | } |
| 596 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 597 | if (KVM_PREALLOC_LEVEL > 0) { |
| 598 | /* |
| 599 | * Allocate fake pgd for the page table manipulation macros to |
| 600 | * work. This is not used by the hardware and we have no |
| 601 | * alignment requirement for this allocation. |
| 602 | */ |
| 603 | pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t), |
| 604 | GFP_KERNEL | __GFP_ZERO); |
| 605 | } else { |
| 606 | /* |
| 607 | * Allocate actual first-level Stage-2 page table used by the |
| 608 | * hardware for Stage-2 page table walks. |
| 609 | */ |
| 610 | pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER); |
| 611 | } |
| 612 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 613 | if (!pgd) |
| 614 | return -ENOMEM; |
| 615 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 616 | ret = kvm_prealloc_hwpgd(kvm, pgd); |
| 617 | if (ret) |
| 618 | goto out_err; |
| 619 | |
Marc Zyngier | c62ee2b | 2012-10-15 11:27:37 +0100 | [diff] [blame] | 620 | kvm_clean_pgd(pgd); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 621 | kvm->arch.pgd = pgd; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 622 | return 0; |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 623 | out_err: |
| 624 | if (KVM_PREALLOC_LEVEL > 0) |
| 625 | kfree(pgd); |
| 626 | else |
| 627 | free_pages((unsigned long)pgd, S2_PGD_ORDER); |
| 628 | return ret; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 631 | /** |
| 632 | * unmap_stage2_range -- Clear stage2 page table entries to unmap a range |
| 633 | * @kvm: The VM pointer |
| 634 | * @start: The intermediate physical base address of the range to unmap |
| 635 | * @size: The size of the area to unmap |
| 636 | * |
| 637 | * Clear a range of stage-2 mappings, lowering the various ref-counts. Must |
| 638 | * be called while holding mmu_lock (unless for freeing the stage2 pgd before |
| 639 | * destroying the VM), otherwise another faulting VCPU may come in and mess |
| 640 | * with things behind our backs. |
| 641 | */ |
| 642 | static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size) |
| 643 | { |
Marc Zyngier | d4cb9df5 | 2013-05-14 12:11:34 +0100 | [diff] [blame] | 644 | unmap_range(kvm, kvm->arch.pgd, start, size); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Christoffer Dall | 957db10 | 2014-11-27 10:35:03 +0100 | [diff] [blame] | 647 | static void stage2_unmap_memslot(struct kvm *kvm, |
| 648 | struct kvm_memory_slot *memslot) |
| 649 | { |
| 650 | hva_t hva = memslot->userspace_addr; |
| 651 | phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT; |
| 652 | phys_addr_t size = PAGE_SIZE * memslot->npages; |
| 653 | hva_t reg_end = hva + size; |
| 654 | |
| 655 | /* |
| 656 | * A memory region could potentially cover multiple VMAs, and any holes |
| 657 | * between them, so iterate over all of them to find out if we should |
| 658 | * unmap any of them. |
| 659 | * |
| 660 | * +--------------------------------------------+ |
| 661 | * +---------------+----------------+ +----------------+ |
| 662 | * | : VMA 1 | VMA 2 | | VMA 3 : | |
| 663 | * +---------------+----------------+ +----------------+ |
| 664 | * | memory region | |
| 665 | * +--------------------------------------------+ |
| 666 | */ |
| 667 | do { |
| 668 | struct vm_area_struct *vma = find_vma(current->mm, hva); |
| 669 | hva_t vm_start, vm_end; |
| 670 | |
| 671 | if (!vma || vma->vm_start >= reg_end) |
| 672 | break; |
| 673 | |
| 674 | /* |
| 675 | * Take the intersection of this VMA with the memory region |
| 676 | */ |
| 677 | vm_start = max(hva, vma->vm_start); |
| 678 | vm_end = min(reg_end, vma->vm_end); |
| 679 | |
| 680 | if (!(vma->vm_flags & VM_PFNMAP)) { |
| 681 | gpa_t gpa = addr + (vm_start - memslot->userspace_addr); |
| 682 | unmap_stage2_range(kvm, gpa, vm_end - vm_start); |
| 683 | } |
| 684 | hva = vm_end; |
| 685 | } while (hva < reg_end); |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * stage2_unmap_vm - Unmap Stage-2 RAM mappings |
| 690 | * @kvm: The struct kvm pointer |
| 691 | * |
| 692 | * Go through the memregions and unmap any reguler RAM |
| 693 | * backing memory already mapped to the VM. |
| 694 | */ |
| 695 | void stage2_unmap_vm(struct kvm *kvm) |
| 696 | { |
| 697 | struct kvm_memslots *slots; |
| 698 | struct kvm_memory_slot *memslot; |
| 699 | int idx; |
| 700 | |
| 701 | idx = srcu_read_lock(&kvm->srcu); |
| 702 | spin_lock(&kvm->mmu_lock); |
| 703 | |
| 704 | slots = kvm_memslots(kvm); |
| 705 | kvm_for_each_memslot(memslot, slots) |
| 706 | stage2_unmap_memslot(kvm, memslot); |
| 707 | |
| 708 | spin_unlock(&kvm->mmu_lock); |
| 709 | srcu_read_unlock(&kvm->srcu, idx); |
| 710 | } |
| 711 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 712 | /** |
| 713 | * kvm_free_stage2_pgd - free all stage-2 tables |
| 714 | * @kvm: The KVM struct pointer for the VM. |
| 715 | * |
| 716 | * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all |
| 717 | * underlying level-2 and level-3 tables before freeing the actual level-1 table |
| 718 | * and setting the struct pointer to NULL. |
| 719 | * |
| 720 | * Note we don't need locking here as this is only called when the VM is |
| 721 | * destroyed, which can only be done once. |
| 722 | */ |
| 723 | void kvm_free_stage2_pgd(struct kvm *kvm) |
| 724 | { |
| 725 | if (kvm->arch.pgd == NULL) |
| 726 | return; |
| 727 | |
| 728 | unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE); |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 729 | kvm_free_hwpgd(kvm); |
| 730 | if (KVM_PREALLOC_LEVEL > 0) |
| 731 | kfree(kvm->arch.pgd); |
| 732 | else |
| 733 | free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 734 | kvm->arch.pgd = NULL; |
| 735 | } |
| 736 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 737 | static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, |
| 738 | phys_addr_t addr) |
| 739 | { |
| 740 | pgd_t *pgd; |
| 741 | pud_t *pud; |
| 742 | |
| 743 | pgd = kvm->arch.pgd + pgd_index(addr); |
| 744 | if (WARN_ON(pgd_none(*pgd))) { |
| 745 | if (!cache) |
| 746 | return NULL; |
| 747 | pud = mmu_memory_cache_alloc(cache); |
| 748 | pgd_populate(NULL, pgd, pud); |
| 749 | get_page(virt_to_page(pgd)); |
| 750 | } |
| 751 | |
| 752 | return pud_offset(pgd, addr); |
| 753 | } |
| 754 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 755 | static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, |
| 756 | phys_addr_t addr) |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 757 | { |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 758 | pud_t *pud; |
| 759 | pmd_t *pmd; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 760 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 761 | pud = stage2_get_pud(kvm, cache, addr); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 762 | if (pud_none(*pud)) { |
| 763 | if (!cache) |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 764 | return NULL; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 765 | pmd = mmu_memory_cache_alloc(cache); |
| 766 | pud_populate(NULL, pud, pmd); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 767 | get_page(virt_to_page(pud)); |
Marc Zyngier | c62ee2b | 2012-10-15 11:27:37 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 770 | return pmd_offset(pud, addr); |
| 771 | } |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 772 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 773 | static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache |
| 774 | *cache, phys_addr_t addr, const pmd_t *new_pmd) |
| 775 | { |
| 776 | pmd_t *pmd, old_pmd; |
| 777 | |
| 778 | pmd = stage2_get_pmd(kvm, cache, addr); |
| 779 | VM_BUG_ON(!pmd); |
| 780 | |
| 781 | /* |
| 782 | * Mapping in huge pages should only happen through a fault. If a |
| 783 | * page is merged into a transparent huge page, the individual |
| 784 | * subpages of that huge page should be unmapped through MMU |
| 785 | * notifiers before we get here. |
| 786 | * |
| 787 | * Merging of CompoundPages is not supported; they should become |
| 788 | * splitting first, unmapped, merged, and mapped back in on-demand. |
| 789 | */ |
| 790 | VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd)); |
| 791 | |
| 792 | old_pmd = *pmd; |
| 793 | kvm_set_pmd(pmd, *new_pmd); |
| 794 | if (pmd_present(old_pmd)) |
| 795 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
| 796 | else |
| 797 | get_page(virt_to_page(pmd)); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 802 | phys_addr_t addr, const pte_t *new_pte, |
| 803 | unsigned long flags) |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 804 | { |
| 805 | pmd_t *pmd; |
| 806 | pte_t *pte, old_pte; |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 807 | bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP; |
| 808 | bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE; |
| 809 | |
| 810 | VM_BUG_ON(logging_active && !cache); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 811 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 812 | /* Create stage-2 page table mapping - Levels 0 and 1 */ |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 813 | pmd = stage2_get_pmd(kvm, cache, addr); |
| 814 | if (!pmd) { |
| 815 | /* |
| 816 | * Ignore calls from kvm_set_spte_hva for unallocated |
| 817 | * address ranges. |
| 818 | */ |
| 819 | return 0; |
| 820 | } |
| 821 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 822 | /* |
| 823 | * While dirty page logging - dissolve huge PMD, then continue on to |
| 824 | * allocate page. |
| 825 | */ |
| 826 | if (logging_active) |
| 827 | stage2_dissolve_pmd(kvm, addr, pmd); |
| 828 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 829 | /* Create stage-2 page mappings - Level 2 */ |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 830 | if (pmd_none(*pmd)) { |
| 831 | if (!cache) |
| 832 | return 0; /* ignore calls from kvm_set_spte_hva */ |
| 833 | pte = mmu_memory_cache_alloc(cache); |
Marc Zyngier | c62ee2b | 2012-10-15 11:27:37 +0100 | [diff] [blame] | 834 | kvm_clean_pte(pte); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 835 | pmd_populate_kernel(NULL, pmd, pte); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 836 | get_page(virt_to_page(pmd)); |
Marc Zyngier | c62ee2b | 2012-10-15 11:27:37 +0100 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | pte = pte_offset_kernel(pmd, addr); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 840 | |
| 841 | if (iomap && pte_present(*pte)) |
| 842 | return -EFAULT; |
| 843 | |
| 844 | /* Create 2nd stage page table mapping - Level 3 */ |
| 845 | old_pte = *pte; |
| 846 | kvm_set_pte(pte, *new_pte); |
| 847 | if (pte_present(old_pte)) |
Marc Zyngier | 4876276 | 2013-01-28 15:27:00 +0000 | [diff] [blame] | 848 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 849 | else |
| 850 | get_page(virt_to_page(pte)); |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * kvm_phys_addr_ioremap - map a device range to guest IPA |
| 857 | * |
| 858 | * @kvm: The KVM pointer |
| 859 | * @guest_ipa: The IPA at which to insert the mapping |
| 860 | * @pa: The physical address of the device |
| 861 | * @size: The size of the mapping |
| 862 | */ |
| 863 | 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] | 864 | phys_addr_t pa, unsigned long size, bool writable) |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 865 | { |
| 866 | phys_addr_t addr, end; |
| 867 | int ret = 0; |
| 868 | unsigned long pfn; |
| 869 | struct kvm_mmu_memory_cache cache = { 0, }; |
| 870 | |
| 871 | end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK; |
| 872 | pfn = __phys_to_pfn(pa); |
| 873 | |
| 874 | for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) { |
Marc Zyngier | c62ee2b | 2012-10-15 11:27:37 +0100 | [diff] [blame] | 875 | pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 876 | |
Ard Biesheuvel | c40f2f8 | 2014-09-17 14:56:18 -0700 | [diff] [blame] | 877 | if (writable) |
| 878 | kvm_set_s2pte_writable(&pte); |
| 879 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 880 | ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES, |
| 881 | KVM_NR_MEM_OBJS); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 882 | if (ret) |
| 883 | goto out; |
| 884 | spin_lock(&kvm->mmu_lock); |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 885 | ret = stage2_set_pte(kvm, &cache, addr, &pte, |
| 886 | KVM_S2PTE_FLAG_IS_IOMAP); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 887 | spin_unlock(&kvm->mmu_lock); |
| 888 | if (ret) |
| 889 | goto out; |
| 890 | |
| 891 | pfn++; |
| 892 | } |
| 893 | |
| 894 | out: |
| 895 | mmu_free_memory_cache(&cache); |
| 896 | return ret; |
| 897 | } |
| 898 | |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 899 | static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap) |
| 900 | { |
| 901 | pfn_t pfn = *pfnp; |
| 902 | gfn_t gfn = *ipap >> PAGE_SHIFT; |
| 903 | |
| 904 | if (PageTransCompound(pfn_to_page(pfn))) { |
| 905 | unsigned long mask; |
| 906 | /* |
| 907 | * The address we faulted on is backed by a transparent huge |
| 908 | * page. However, because we map the compound huge page and |
| 909 | * not the individual tail page, we need to transfer the |
| 910 | * refcount to the head page. We have to be careful that the |
| 911 | * THP doesn't start to split while we are adjusting the |
| 912 | * refcounts. |
| 913 | * |
| 914 | * We are sure this doesn't happen, because mmu_notifier_retry |
| 915 | * was successful and we are holding the mmu_lock, so if this |
| 916 | * THP is trying to split, it will be blocked in the mmu |
| 917 | * notifier before touching any of the pages, specifically |
| 918 | * before being able to call __split_huge_page_refcount(). |
| 919 | * |
| 920 | * We can therefore safely transfer the refcount from PG_tail |
| 921 | * to PG_head and switch the pfn from a tail page to the head |
| 922 | * page accordingly. |
| 923 | */ |
| 924 | mask = PTRS_PER_PMD - 1; |
| 925 | VM_BUG_ON((gfn & mask) != (pfn & mask)); |
| 926 | if (pfn & mask) { |
| 927 | *ipap &= PMD_MASK; |
| 928 | kvm_release_pfn_clean(pfn); |
| 929 | pfn &= ~mask; |
| 930 | kvm_get_pfn(pfn); |
| 931 | *pfnp = pfn; |
| 932 | } |
| 933 | |
| 934 | return true; |
| 935 | } |
| 936 | |
| 937 | return false; |
| 938 | } |
| 939 | |
Ard Biesheuvel | a7d079c | 2014-09-09 11:27:09 +0100 | [diff] [blame] | 940 | static bool kvm_is_write_fault(struct kvm_vcpu *vcpu) |
| 941 | { |
| 942 | if (kvm_vcpu_trap_is_iabt(vcpu)) |
| 943 | return false; |
| 944 | |
| 945 | return kvm_vcpu_dabt_iswrite(vcpu); |
| 946 | } |
| 947 | |
Ard Biesheuvel | bb55e9b | 2014-11-10 09:33:55 +0100 | [diff] [blame] | 948 | static bool kvm_is_device_pfn(unsigned long pfn) |
| 949 | { |
| 950 | return !pfn_valid(pfn); |
| 951 | } |
| 952 | |
Mario Smarduch | c647355 | 2015-01-15 15:58:56 -0800 | [diff] [blame] | 953 | #ifdef CONFIG_ARM |
| 954 | /** |
| 955 | * stage2_wp_ptes - write protect PMD range |
| 956 | * @pmd: pointer to pmd entry |
| 957 | * @addr: range start address |
| 958 | * @end: range end address |
| 959 | */ |
| 960 | static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end) |
| 961 | { |
| 962 | pte_t *pte; |
| 963 | |
| 964 | pte = pte_offset_kernel(pmd, addr); |
| 965 | do { |
| 966 | if (!pte_none(*pte)) { |
| 967 | if (!kvm_s2pte_readonly(pte)) |
| 968 | kvm_set_s2pte_readonly(pte); |
| 969 | } |
| 970 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * stage2_wp_pmds - write protect PUD range |
| 975 | * @pud: pointer to pud entry |
| 976 | * @addr: range start address |
| 977 | * @end: range end address |
| 978 | */ |
| 979 | static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end) |
| 980 | { |
| 981 | pmd_t *pmd; |
| 982 | phys_addr_t next; |
| 983 | |
| 984 | pmd = pmd_offset(pud, addr); |
| 985 | |
| 986 | do { |
| 987 | next = kvm_pmd_addr_end(addr, end); |
| 988 | if (!pmd_none(*pmd)) { |
| 989 | if (kvm_pmd_huge(*pmd)) { |
| 990 | if (!kvm_s2pmd_readonly(pmd)) |
| 991 | kvm_set_s2pmd_readonly(pmd); |
| 992 | } else { |
| 993 | stage2_wp_ptes(pmd, addr, next); |
| 994 | } |
| 995 | } |
| 996 | } while (pmd++, addr = next, addr != end); |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * stage2_wp_puds - write protect PGD range |
| 1001 | * @pgd: pointer to pgd entry |
| 1002 | * @addr: range start address |
| 1003 | * @end: range end address |
| 1004 | * |
| 1005 | * Process PUD entries, for a huge PUD we cause a panic. |
| 1006 | */ |
| 1007 | static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end) |
| 1008 | { |
| 1009 | pud_t *pud; |
| 1010 | phys_addr_t next; |
| 1011 | |
| 1012 | pud = pud_offset(pgd, addr); |
| 1013 | do { |
| 1014 | next = kvm_pud_addr_end(addr, end); |
| 1015 | if (!pud_none(*pud)) { |
| 1016 | /* TODO:PUD not supported, revisit later if supported */ |
| 1017 | BUG_ON(kvm_pud_huge(*pud)); |
| 1018 | stage2_wp_pmds(pud, addr, next); |
| 1019 | } |
| 1020 | } while (pud++, addr = next, addr != end); |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * stage2_wp_range() - write protect stage2 memory region range |
| 1025 | * @kvm: The KVM pointer |
| 1026 | * @addr: Start address of range |
| 1027 | * @end: End address of range |
| 1028 | */ |
| 1029 | static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end) |
| 1030 | { |
| 1031 | pgd_t *pgd; |
| 1032 | phys_addr_t next; |
| 1033 | |
| 1034 | pgd = kvm->arch.pgd + pgd_index(addr); |
| 1035 | do { |
| 1036 | /* |
| 1037 | * Release kvm_mmu_lock periodically if the memory region is |
| 1038 | * large. Otherwise, we may see kernel panics with |
| 1039 | * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCK_DETECTOR, |
| 1040 | * CONFIG_LOCK_DEP. Additionally, holding the lock too long |
| 1041 | * will also starve other vCPUs. |
| 1042 | */ |
| 1043 | if (need_resched() || spin_needbreak(&kvm->mmu_lock)) |
| 1044 | cond_resched_lock(&kvm->mmu_lock); |
| 1045 | |
| 1046 | next = kvm_pgd_addr_end(addr, end); |
| 1047 | if (pgd_present(*pgd)) |
| 1048 | stage2_wp_puds(pgd, addr, next); |
| 1049 | } while (pgd++, addr = next, addr != end); |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot |
| 1054 | * @kvm: The KVM pointer |
| 1055 | * @slot: The memory slot to write protect |
| 1056 | * |
| 1057 | * Called to start logging dirty pages after memory region |
| 1058 | * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns |
| 1059 | * all present PMD and PTEs are write protected in the memory region. |
| 1060 | * Afterwards read of dirty page log can be called. |
| 1061 | * |
| 1062 | * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired, |
| 1063 | * serializing operations for VM memory regions. |
| 1064 | */ |
| 1065 | void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot) |
| 1066 | { |
| 1067 | struct kvm_memory_slot *memslot = id_to_memslot(kvm->memslots, slot); |
| 1068 | phys_addr_t start = memslot->base_gfn << PAGE_SHIFT; |
| 1069 | phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT; |
| 1070 | |
| 1071 | spin_lock(&kvm->mmu_lock); |
| 1072 | stage2_wp_range(kvm, start, end); |
| 1073 | spin_unlock(&kvm->mmu_lock); |
| 1074 | kvm_flush_remote_tlbs(kvm); |
| 1075 | } |
Mario Smarduch | 53c810c | 2015-01-15 15:58:57 -0800 | [diff] [blame] | 1076 | |
| 1077 | /** |
| 1078 | * kvm_arch_mmu_write_protect_pt_masked() - write protect dirty pages |
| 1079 | * @kvm: The KVM pointer |
| 1080 | * @slot: The memory slot associated with mask |
| 1081 | * @gfn_offset: The gfn offset in memory slot |
| 1082 | * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory |
| 1083 | * slot to be write protected |
| 1084 | * |
| 1085 | * Walks bits set in mask write protects the associated pte's. Caller must |
| 1086 | * acquire kvm_mmu_lock. |
| 1087 | */ |
| 1088 | void kvm_arch_mmu_write_protect_pt_masked(struct kvm *kvm, |
| 1089 | struct kvm_memory_slot *slot, |
| 1090 | gfn_t gfn_offset, unsigned long mask) |
| 1091 | { |
| 1092 | phys_addr_t base_gfn = slot->base_gfn + gfn_offset; |
| 1093 | phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT; |
| 1094 | phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT; |
| 1095 | |
| 1096 | stage2_wp_range(kvm, start, end); |
| 1097 | } |
Mario Smarduch | c647355 | 2015-01-15 15:58:56 -0800 | [diff] [blame] | 1098 | #endif |
| 1099 | |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1100 | static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1101 | struct kvm_memory_slot *memslot, unsigned long hva, |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1102 | unsigned long fault_status) |
| 1103 | { |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1104 | int ret; |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1105 | bool write_fault, writable, hugetlb = false, force_pte = false; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1106 | unsigned long mmu_seq; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1107 | gfn_t gfn = fault_ipa >> PAGE_SHIFT; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1108 | struct kvm *kvm = vcpu->kvm; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1109 | struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1110 | struct vm_area_struct *vma; |
| 1111 | pfn_t pfn; |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1112 | pgprot_t mem_type = PAGE_S2; |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1113 | bool fault_ipa_uncached; |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1114 | bool logging_active = memslot_is_logging(memslot); |
| 1115 | unsigned long flags = 0; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1116 | |
Ard Biesheuvel | a7d079c | 2014-09-09 11:27:09 +0100 | [diff] [blame] | 1117 | write_fault = kvm_is_write_fault(vcpu); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1118 | if (fault_status == FSC_PERM && !write_fault) { |
| 1119 | kvm_err("Unexpected L2 read permission error\n"); |
| 1120 | return -EFAULT; |
| 1121 | } |
| 1122 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1123 | /* Let's check if we will get back a huge page backed by hugetlbfs */ |
| 1124 | down_read(¤t->mm->mmap_sem); |
| 1125 | vma = find_vma_intersection(current->mm, hva, hva + 1); |
Ard Biesheuvel | 37b5440 | 2014-09-17 14:56:17 -0700 | [diff] [blame] | 1126 | if (unlikely(!vma)) { |
| 1127 | kvm_err("Failed to find VMA for hva 0x%lx\n", hva); |
| 1128 | up_read(¤t->mm->mmap_sem); |
| 1129 | return -EFAULT; |
| 1130 | } |
| 1131 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1132 | if (is_vm_hugetlb_page(vma) && !logging_active) { |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1133 | hugetlb = true; |
| 1134 | gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT; |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1135 | } else { |
| 1136 | /* |
Marc Zyngier | 136d737 | 2013-12-13 16:56:06 +0000 | [diff] [blame] | 1137 | * Pages belonging to memslots that don't have the same |
| 1138 | * alignment for userspace and IPA cannot be mapped using |
| 1139 | * block descriptors even if the pages belong to a THP for |
| 1140 | * the process, because the stage-2 block descriptor will |
| 1141 | * cover more than a single THP and we loose atomicity for |
| 1142 | * unmapping, updates, and splits of the THP or other pages |
| 1143 | * in the stage-2 block range. |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1144 | */ |
Marc Zyngier | 136d737 | 2013-12-13 16:56:06 +0000 | [diff] [blame] | 1145 | if ((memslot->userspace_addr & ~PMD_MASK) != |
| 1146 | ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK)) |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1147 | force_pte = true; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1148 | } |
| 1149 | up_read(¤t->mm->mmap_sem); |
| 1150 | |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1151 | /* We need minimum second+third level pages */ |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 1152 | ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES, |
| 1153 | KVM_NR_MEM_OBJS); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1154 | if (ret) |
| 1155 | return ret; |
| 1156 | |
| 1157 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
| 1158 | /* |
| 1159 | * Ensure the read of mmu_notifier_seq happens before we call |
| 1160 | * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk |
| 1161 | * the page we just got a reference to gets unmapped before we have a |
| 1162 | * chance to grab the mmu_lock, which ensure that if the page gets |
| 1163 | * unmapped afterwards, the call to kvm_unmap_hva will take it away |
| 1164 | * from us again properly. This smp_rmb() interacts with the smp_wmb() |
| 1165 | * in kvm_mmu_notifier_invalidate_<page|range_end>. |
| 1166 | */ |
| 1167 | smp_rmb(); |
| 1168 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1169 | pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1170 | if (is_error_pfn(pfn)) |
| 1171 | return -EFAULT; |
| 1172 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1173 | if (kvm_is_device_pfn(pfn)) { |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1174 | mem_type = PAGE_S2_DEVICE; |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1175 | flags |= KVM_S2PTE_FLAG_IS_IOMAP; |
| 1176 | } else if (logging_active) { |
| 1177 | /* |
| 1178 | * Faults on pages in a memslot with logging enabled |
| 1179 | * should not be mapped with huge pages (it introduces churn |
| 1180 | * and performance degradation), so force a pte mapping. |
| 1181 | */ |
| 1182 | force_pte = true; |
| 1183 | flags |= KVM_S2_FLAG_LOGGING_ACTIVE; |
| 1184 | |
| 1185 | /* |
| 1186 | * Only actually map the page as writable if this was a write |
| 1187 | * fault. |
| 1188 | */ |
| 1189 | if (!write_fault) |
| 1190 | writable = false; |
| 1191 | } |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1192 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1193 | spin_lock(&kvm->mmu_lock); |
| 1194 | if (mmu_notifier_retry(kvm, mmu_seq)) |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1195 | goto out_unlock; |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1196 | |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1197 | if (!hugetlb && !force_pte) |
| 1198 | hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1199 | |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1200 | fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT; |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1201 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1202 | if (hugetlb) { |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1203 | pmd_t new_pmd = pfn_pmd(pfn, mem_type); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1204 | new_pmd = pmd_mkhuge(new_pmd); |
| 1205 | if (writable) { |
| 1206 | kvm_set_s2pmd_writable(&new_pmd); |
| 1207 | kvm_set_pfn_dirty(pfn); |
| 1208 | } |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1209 | coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE, |
| 1210 | fault_ipa_uncached); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1211 | ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd); |
| 1212 | } else { |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1213 | pte_t new_pte = pfn_pte(pfn, mem_type); |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1214 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1215 | if (writable) { |
| 1216 | kvm_set_s2pte_writable(&new_pte); |
| 1217 | kvm_set_pfn_dirty(pfn); |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1218 | mark_page_dirty(kvm, gfn); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1219 | } |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1220 | coherent_cache_guest_page(vcpu, hva, PAGE_SIZE, |
| 1221 | fault_ipa_uncached); |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1222 | ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1223 | } |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1224 | |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1225 | out_unlock: |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1226 | spin_unlock(&kvm->mmu_lock); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1227 | kvm_release_pfn_clean(pfn); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1228 | return ret; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * kvm_handle_guest_abort - handles all 2nd stage aborts |
| 1233 | * @vcpu: the VCPU pointer |
| 1234 | * @run: the kvm_run structure |
| 1235 | * |
| 1236 | * Any abort that gets to the host is almost guaranteed to be caused by a |
| 1237 | * missing second stage translation table entry, which can mean that either the |
| 1238 | * guest simply needs more memory and we must allocate an appropriate page or it |
| 1239 | * can mean that the guest tried to access I/O memory, which is emulated by user |
| 1240 | * space. The distinction is based on the IPA causing the fault and whether this |
| 1241 | * memory region has been registered as standard RAM by user space. |
| 1242 | */ |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1243 | int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 1244 | { |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1245 | unsigned long fault_status; |
| 1246 | phys_addr_t fault_ipa; |
| 1247 | struct kvm_memory_slot *memslot; |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1248 | unsigned long hva; |
| 1249 | bool is_iabt, write_fault, writable; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1250 | gfn_t gfn; |
| 1251 | int ret, idx; |
| 1252 | |
Marc Zyngier | 52d1dba | 2012-10-15 10:33:38 +0100 | [diff] [blame] | 1253 | is_iabt = kvm_vcpu_trap_is_iabt(vcpu); |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 1254 | fault_ipa = kvm_vcpu_get_fault_ipa(vcpu); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1255 | |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 1256 | trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu), |
| 1257 | kvm_vcpu_get_hfar(vcpu), fault_ipa); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1258 | |
| 1259 | /* Check the stage-2 fault is trans. fault or write fault */ |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 1260 | fault_status = kvm_vcpu_trap_get_fault_type(vcpu); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1261 | if (fault_status != FSC_FAULT && fault_status != FSC_PERM) { |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 1262 | kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n", |
| 1263 | kvm_vcpu_trap_get_class(vcpu), |
| 1264 | (unsigned long)kvm_vcpu_trap_get_fault(vcpu), |
| 1265 | (unsigned long)kvm_vcpu_get_hsr(vcpu)); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1266 | return -EFAULT; |
| 1267 | } |
| 1268 | |
| 1269 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 1270 | |
| 1271 | gfn = fault_ipa >> PAGE_SHIFT; |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1272 | memslot = gfn_to_memslot(vcpu->kvm, gfn); |
| 1273 | hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable); |
Ard Biesheuvel | a7d079c | 2014-09-09 11:27:09 +0100 | [diff] [blame] | 1274 | write_fault = kvm_is_write_fault(vcpu); |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1275 | if (kvm_is_error_hva(hva) || (write_fault && !writable)) { |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1276 | if (is_iabt) { |
| 1277 | /* Prefetch Abort on I/O address */ |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 1278 | kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu)); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1279 | ret = 1; |
| 1280 | goto out_unlock; |
| 1281 | } |
| 1282 | |
Marc Zyngier | cfe3950 | 2012-12-12 14:42:09 +0000 | [diff] [blame] | 1283 | /* |
| 1284 | * The IPA is reported as [MAX:12], so we need to |
| 1285 | * complement it with the bottom 12 bits from the |
| 1286 | * faulting VA. This is always 12 bits, irrespective |
| 1287 | * of the page size. |
| 1288 | */ |
| 1289 | fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 1290 | ret = io_mem_abort(vcpu, run, fault_ipa); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1291 | goto out_unlock; |
| 1292 | } |
| 1293 | |
Christoffer Dall | c3058d5 | 2014-10-10 12:14:29 +0200 | [diff] [blame] | 1294 | /* Userspace should not be able to register out-of-bounds IPAs */ |
| 1295 | VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE); |
| 1296 | |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1297 | ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1298 | if (ret == 0) |
| 1299 | ret = 1; |
| 1300 | out_unlock: |
| 1301 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 1302 | return ret; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1303 | } |
| 1304 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1305 | static void handle_hva_to_gpa(struct kvm *kvm, |
| 1306 | unsigned long start, |
| 1307 | unsigned long end, |
| 1308 | void (*handler)(struct kvm *kvm, |
| 1309 | gpa_t gpa, void *data), |
| 1310 | void *data) |
| 1311 | { |
| 1312 | struct kvm_memslots *slots; |
| 1313 | struct kvm_memory_slot *memslot; |
| 1314 | |
| 1315 | slots = kvm_memslots(kvm); |
| 1316 | |
| 1317 | /* we only care about the pages that the guest sees */ |
| 1318 | kvm_for_each_memslot(memslot, slots) { |
| 1319 | unsigned long hva_start, hva_end; |
| 1320 | gfn_t gfn, gfn_end; |
| 1321 | |
| 1322 | hva_start = max(start, memslot->userspace_addr); |
| 1323 | hva_end = min(end, memslot->userspace_addr + |
| 1324 | (memslot->npages << PAGE_SHIFT)); |
| 1325 | if (hva_start >= hva_end) |
| 1326 | continue; |
| 1327 | |
| 1328 | /* |
| 1329 | * {gfn(page) | page intersects with [hva_start, hva_end)} = |
| 1330 | * {gfn_start, gfn_start+1, ..., gfn_end-1}. |
| 1331 | */ |
| 1332 | gfn = hva_to_gfn_memslot(hva_start, memslot); |
| 1333 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); |
| 1334 | |
| 1335 | for (; gfn < gfn_end; ++gfn) { |
| 1336 | gpa_t gpa = gfn << PAGE_SHIFT; |
| 1337 | handler(kvm, gpa, data); |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data) |
| 1343 | { |
| 1344 | unmap_stage2_range(kvm, gpa, PAGE_SIZE); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) |
| 1348 | { |
| 1349 | unsigned long end = hva + PAGE_SIZE; |
| 1350 | |
| 1351 | if (!kvm->arch.pgd) |
| 1352 | return 0; |
| 1353 | |
| 1354 | trace_kvm_unmap_hva(hva); |
| 1355 | handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL); |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | int kvm_unmap_hva_range(struct kvm *kvm, |
| 1360 | unsigned long start, unsigned long end) |
| 1361 | { |
| 1362 | if (!kvm->arch.pgd) |
| 1363 | return 0; |
| 1364 | |
| 1365 | trace_kvm_unmap_hva_range(start, end); |
| 1366 | handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL); |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data) |
| 1371 | { |
| 1372 | pte_t *pte = (pte_t *)data; |
| 1373 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1374 | /* |
| 1375 | * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE |
| 1376 | * flag clear because MMU notifiers will have unmapped a huge PMD before |
| 1377 | * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and |
| 1378 | * therefore stage2_set_pte() never needs to clear out a huge PMD |
| 1379 | * through this calling path. |
| 1380 | */ |
| 1381 | stage2_set_pte(kvm, NULL, gpa, pte, 0); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) |
| 1386 | { |
| 1387 | unsigned long end = hva + PAGE_SIZE; |
| 1388 | pte_t stage2_pte; |
| 1389 | |
| 1390 | if (!kvm->arch.pgd) |
| 1391 | return; |
| 1392 | |
| 1393 | trace_kvm_set_spte_hva(hva); |
| 1394 | stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2); |
| 1395 | handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte); |
| 1396 | } |
| 1397 | |
| 1398 | void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 1399 | { |
| 1400 | mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); |
| 1401 | } |
| 1402 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1403 | phys_addr_t kvm_mmu_get_httbr(void) |
| 1404 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1405 | return virt_to_phys(hyp_pgd); |
| 1406 | } |
| 1407 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1408 | phys_addr_t kvm_mmu_get_boot_httbr(void) |
| 1409 | { |
| 1410 | return virt_to_phys(boot_hyp_pgd); |
| 1411 | } |
| 1412 | |
| 1413 | phys_addr_t kvm_get_idmap_vector(void) |
| 1414 | { |
| 1415 | return hyp_idmap_vector; |
| 1416 | } |
| 1417 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1418 | int kvm_mmu_init(void) |
| 1419 | { |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1420 | int err; |
| 1421 | |
Santosh Shilimkar | 4fda342 | 2013-11-19 14:59:12 -0500 | [diff] [blame] | 1422 | hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start); |
| 1423 | hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end); |
| 1424 | hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1425 | |
| 1426 | if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) { |
| 1427 | /* |
| 1428 | * Our init code is crossing a page boundary. Allocate |
| 1429 | * a bounce page, copy the code over and use that. |
| 1430 | */ |
| 1431 | size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start; |
| 1432 | phys_addr_t phys_base; |
| 1433 | |
Mark Salter | 5d4e08c | 2014-03-28 14:25:19 +0000 | [diff] [blame] | 1434 | init_bounce_page = (void *)__get_free_page(GFP_KERNEL); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1435 | if (!init_bounce_page) { |
| 1436 | kvm_err("Couldn't allocate HYP init bounce page\n"); |
| 1437 | err = -ENOMEM; |
| 1438 | goto out; |
| 1439 | } |
| 1440 | |
| 1441 | memcpy(init_bounce_page, __hyp_idmap_text_start, len); |
| 1442 | /* |
| 1443 | * Warning: the code we just copied to the bounce page |
| 1444 | * must be flushed to the point of coherency. |
| 1445 | * Otherwise, the data may be sitting in L2, and HYP |
| 1446 | * mode won't be able to observe it as it runs with |
| 1447 | * caches off at that point. |
| 1448 | */ |
| 1449 | kvm_flush_dcache_to_poc(init_bounce_page, len); |
| 1450 | |
Santosh Shilimkar | 4fda342 | 2013-11-19 14:59:12 -0500 | [diff] [blame] | 1451 | phys_base = kvm_virt_to_phys(init_bounce_page); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1452 | hyp_idmap_vector += phys_base - hyp_idmap_start; |
| 1453 | hyp_idmap_start = phys_base; |
| 1454 | hyp_idmap_end = phys_base + len; |
| 1455 | |
| 1456 | kvm_info("Using HYP init bounce page @%lx\n", |
| 1457 | (unsigned long)phys_base); |
| 1458 | } |
| 1459 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 1460 | hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order); |
| 1461 | boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order); |
Mark Salter | 5d4e08c | 2014-03-28 14:25:19 +0000 | [diff] [blame] | 1462 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1463 | if (!hyp_pgd || !boot_hyp_pgd) { |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1464 | kvm_err("Hyp mode PGD not allocated\n"); |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1465 | err = -ENOMEM; |
| 1466 | goto out; |
| 1467 | } |
| 1468 | |
| 1469 | /* Create the idmap in the boot page tables */ |
| 1470 | err = __create_hyp_mappings(boot_hyp_pgd, |
| 1471 | hyp_idmap_start, hyp_idmap_end, |
| 1472 | __phys_to_pfn(hyp_idmap_start), |
| 1473 | PAGE_HYP); |
| 1474 | |
| 1475 | if (err) { |
| 1476 | kvm_err("Failed to idmap %lx-%lx\n", |
| 1477 | hyp_idmap_start, hyp_idmap_end); |
| 1478 | goto out; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1479 | } |
| 1480 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1481 | /* Map the very same page at the trampoline VA */ |
| 1482 | err = __create_hyp_mappings(boot_hyp_pgd, |
| 1483 | TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE, |
| 1484 | __phys_to_pfn(hyp_idmap_start), |
| 1485 | PAGE_HYP); |
| 1486 | if (err) { |
| 1487 | kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n", |
| 1488 | TRAMPOLINE_VA); |
| 1489 | goto out; |
| 1490 | } |
| 1491 | |
| 1492 | /* Map the same page again into the runtime page tables */ |
| 1493 | err = __create_hyp_mappings(hyp_pgd, |
| 1494 | TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE, |
| 1495 | __phys_to_pfn(hyp_idmap_start), |
| 1496 | PAGE_HYP); |
| 1497 | if (err) { |
| 1498 | kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n", |
| 1499 | TRAMPOLINE_VA); |
| 1500 | goto out; |
| 1501 | } |
| 1502 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1503 | return 0; |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1504 | out: |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 1505 | free_hyp_pgds(); |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1506 | return err; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1507 | } |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1508 | |
| 1509 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 1510 | struct kvm_userspace_memory_region *mem, |
| 1511 | const struct kvm_memory_slot *old, |
| 1512 | enum kvm_mr_change change) |
| 1513 | { |
Mario Smarduch | c647355 | 2015-01-15 15:58:56 -0800 | [diff] [blame] | 1514 | #ifdef CONFIG_ARM |
| 1515 | /* |
| 1516 | * At this point memslot has been committed and there is an |
| 1517 | * allocated dirty_bitmap[], dirty pages will be be tracked while the |
| 1518 | * memory slot is write protected. |
| 1519 | */ |
| 1520 | if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) |
| 1521 | kvm_mmu_wp_memory_region(kvm, mem->slot); |
| 1522 | #endif |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 1526 | struct kvm_memory_slot *memslot, |
| 1527 | struct kvm_userspace_memory_region *mem, |
| 1528 | enum kvm_mr_change change) |
| 1529 | { |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1530 | hva_t hva = mem->userspace_addr; |
| 1531 | hva_t reg_end = hva + mem->memory_size; |
| 1532 | bool writable = !(mem->flags & KVM_MEM_READONLY); |
| 1533 | int ret = 0; |
| 1534 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1535 | if (change != KVM_MR_CREATE && change != KVM_MR_MOVE && |
| 1536 | change != KVM_MR_FLAGS_ONLY) |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1537 | return 0; |
| 1538 | |
| 1539 | /* |
Christoffer Dall | c3058d5 | 2014-10-10 12:14:29 +0200 | [diff] [blame] | 1540 | * Prevent userspace from creating a memory region outside of the IPA |
| 1541 | * space addressable by the KVM guest IPA space. |
| 1542 | */ |
| 1543 | if (memslot->base_gfn + memslot->npages >= |
| 1544 | (KVM_PHYS_SIZE >> PAGE_SHIFT)) |
| 1545 | return -EFAULT; |
| 1546 | |
| 1547 | /* |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1548 | * A memory region could potentially cover multiple VMAs, and any holes |
| 1549 | * between them, so iterate over all of them to find out if we can map |
| 1550 | * any of them right now. |
| 1551 | * |
| 1552 | * +--------------------------------------------+ |
| 1553 | * +---------------+----------------+ +----------------+ |
| 1554 | * | : VMA 1 | VMA 2 | | VMA 3 : | |
| 1555 | * +---------------+----------------+ +----------------+ |
| 1556 | * | memory region | |
| 1557 | * +--------------------------------------------+ |
| 1558 | */ |
| 1559 | do { |
| 1560 | struct vm_area_struct *vma = find_vma(current->mm, hva); |
| 1561 | hva_t vm_start, vm_end; |
| 1562 | |
| 1563 | if (!vma || vma->vm_start >= reg_end) |
| 1564 | break; |
| 1565 | |
| 1566 | /* |
| 1567 | * Mapping a read-only VMA is only allowed if the |
| 1568 | * memory region is configured as read-only. |
| 1569 | */ |
| 1570 | if (writable && !(vma->vm_flags & VM_WRITE)) { |
| 1571 | ret = -EPERM; |
| 1572 | break; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * Take the intersection of this VMA with the memory region |
| 1577 | */ |
| 1578 | vm_start = max(hva, vma->vm_start); |
| 1579 | vm_end = min(reg_end, vma->vm_end); |
| 1580 | |
| 1581 | if (vma->vm_flags & VM_PFNMAP) { |
| 1582 | gpa_t gpa = mem->guest_phys_addr + |
| 1583 | (vm_start - mem->userspace_addr); |
| 1584 | phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) + |
| 1585 | vm_start - vma->vm_start; |
| 1586 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1587 | /* IO region dirty page logging not allowed */ |
| 1588 | if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) |
| 1589 | return -EINVAL; |
| 1590 | |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1591 | ret = kvm_phys_addr_ioremap(kvm, gpa, pa, |
| 1592 | vm_end - vm_start, |
| 1593 | writable); |
| 1594 | if (ret) |
| 1595 | break; |
| 1596 | } |
| 1597 | hva = vm_end; |
| 1598 | } while (hva < reg_end); |
| 1599 | |
Mario Smarduch | 15a49a4 | 2015-01-15 15:58:58 -0800 | [diff] [blame^] | 1600 | if (change == KVM_MR_FLAGS_ONLY) |
| 1601 | return ret; |
| 1602 | |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1603 | spin_lock(&kvm->mmu_lock); |
| 1604 | if (ret) |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1605 | unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size); |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1606 | else |
| 1607 | stage2_flush_memslot(kvm, memslot); |
| 1608 | spin_unlock(&kvm->mmu_lock); |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1609 | return ret; |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
| 1613 | struct kvm_memory_slot *dont) |
| 1614 | { |
| 1615 | } |
| 1616 | |
| 1617 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 1618 | unsigned long npages) |
| 1619 | { |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1620 | /* |
| 1621 | * Readonly memslots are not incoherent with the caches by definition, |
| 1622 | * but in practice, they are used mostly to emulate ROMs or NOR flashes |
| 1623 | * that the guest may consider devices and hence map as uncached. |
| 1624 | * To prevent incoherency issues in these cases, tag all readonly |
| 1625 | * regions as incoherent. |
| 1626 | */ |
| 1627 | if (slot->flags & KVM_MEM_READONLY) |
| 1628 | slot->flags |= KVM_MEMSLOT_INCOHERENT; |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | void kvm_arch_memslots_updated(struct kvm *kvm) |
| 1633 | { |
| 1634 | } |
| 1635 | |
| 1636 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 1637 | { |
| 1638 | } |
| 1639 | |
| 1640 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 1641 | struct kvm_memory_slot *slot) |
| 1642 | { |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1643 | gpa_t gpa = slot->base_gfn << PAGE_SHIFT; |
| 1644 | phys_addr_t size = slot->npages << PAGE_SHIFT; |
| 1645 | |
| 1646 | spin_lock(&kvm->mmu_lock); |
| 1647 | unmap_stage2_range(kvm, gpa, size); |
| 1648 | spin_unlock(&kvm->mmu_lock); |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1649 | } |