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