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