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 | } |
Mario Smarduch | 53c810c | 2015-01-15 15:58:57 -0800 | [diff] [blame^] | 1032 | |
| 1033 | /** |
| 1034 | * kvm_arch_mmu_write_protect_pt_masked() - write protect dirty pages |
| 1035 | * @kvm: The KVM pointer |
| 1036 | * @slot: The memory slot associated with mask |
| 1037 | * @gfn_offset: The gfn offset in memory slot |
| 1038 | * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory |
| 1039 | * slot to be write protected |
| 1040 | * |
| 1041 | * Walks bits set in mask write protects the associated pte's. Caller must |
| 1042 | * acquire kvm_mmu_lock. |
| 1043 | */ |
| 1044 | void kvm_arch_mmu_write_protect_pt_masked(struct kvm *kvm, |
| 1045 | struct kvm_memory_slot *slot, |
| 1046 | gfn_t gfn_offset, unsigned long mask) |
| 1047 | { |
| 1048 | phys_addr_t base_gfn = slot->base_gfn + gfn_offset; |
| 1049 | phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT; |
| 1050 | phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT; |
| 1051 | |
| 1052 | stage2_wp_range(kvm, start, end); |
| 1053 | } |
Mario Smarduch | c647355 | 2015-01-15 15:58:56 -0800 | [diff] [blame] | 1054 | #endif |
| 1055 | |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1056 | 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] | 1057 | struct kvm_memory_slot *memslot, unsigned long hva, |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1058 | unsigned long fault_status) |
| 1059 | { |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1060 | int ret; |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1061 | bool write_fault, writable, hugetlb = false, force_pte = false; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1062 | unsigned long mmu_seq; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1063 | gfn_t gfn = fault_ipa >> PAGE_SHIFT; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1064 | struct kvm *kvm = vcpu->kvm; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1065 | struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1066 | struct vm_area_struct *vma; |
| 1067 | pfn_t pfn; |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1068 | pgprot_t mem_type = PAGE_S2; |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1069 | bool fault_ipa_uncached; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1070 | |
Ard Biesheuvel | a7d079c | 2014-09-09 11:27:09 +0100 | [diff] [blame] | 1071 | write_fault = kvm_is_write_fault(vcpu); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1072 | if (fault_status == FSC_PERM && !write_fault) { |
| 1073 | kvm_err("Unexpected L2 read permission error\n"); |
| 1074 | return -EFAULT; |
| 1075 | } |
| 1076 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1077 | /* Let's check if we will get back a huge page backed by hugetlbfs */ |
| 1078 | down_read(¤t->mm->mmap_sem); |
| 1079 | vma = find_vma_intersection(current->mm, hva, hva + 1); |
Ard Biesheuvel | 37b5440 | 2014-09-17 14:56:17 -0700 | [diff] [blame] | 1080 | if (unlikely(!vma)) { |
| 1081 | kvm_err("Failed to find VMA for hva 0x%lx\n", hva); |
| 1082 | up_read(¤t->mm->mmap_sem); |
| 1083 | return -EFAULT; |
| 1084 | } |
| 1085 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1086 | if (is_vm_hugetlb_page(vma)) { |
| 1087 | hugetlb = true; |
| 1088 | gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT; |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1089 | } else { |
| 1090 | /* |
Marc Zyngier | 136d737 | 2013-12-13 16:56:06 +0000 | [diff] [blame] | 1091 | * Pages belonging to memslots that don't have the same |
| 1092 | * alignment for userspace and IPA cannot be mapped using |
| 1093 | * block descriptors even if the pages belong to a THP for |
| 1094 | * the process, because the stage-2 block descriptor will |
| 1095 | * cover more than a single THP and we loose atomicity for |
| 1096 | * unmapping, updates, and splits of the THP or other pages |
| 1097 | * in the stage-2 block range. |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1098 | */ |
Marc Zyngier | 136d737 | 2013-12-13 16:56:06 +0000 | [diff] [blame] | 1099 | if ((memslot->userspace_addr & ~PMD_MASK) != |
| 1100 | ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK)) |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1101 | force_pte = true; |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1102 | } |
| 1103 | up_read(¤t->mm->mmap_sem); |
| 1104 | |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1105 | /* We need minimum second+third level pages */ |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 1106 | ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES, |
| 1107 | KVM_NR_MEM_OBJS); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1108 | if (ret) |
| 1109 | return ret; |
| 1110 | |
| 1111 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
| 1112 | /* |
| 1113 | * Ensure the read of mmu_notifier_seq happens before we call |
| 1114 | * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk |
| 1115 | * the page we just got a reference to gets unmapped before we have a |
| 1116 | * chance to grab the mmu_lock, which ensure that if the page gets |
| 1117 | * unmapped afterwards, the call to kvm_unmap_hva will take it away |
| 1118 | * from us again properly. This smp_rmb() interacts with the smp_wmb() |
| 1119 | * in kvm_mmu_notifier_invalidate_<page|range_end>. |
| 1120 | */ |
| 1121 | smp_rmb(); |
| 1122 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1123 | pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1124 | if (is_error_pfn(pfn)) |
| 1125 | return -EFAULT; |
| 1126 | |
Ard Biesheuvel | bb55e9b | 2014-11-10 09:33:55 +0100 | [diff] [blame] | 1127 | if (kvm_is_device_pfn(pfn)) |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1128 | mem_type = PAGE_S2_DEVICE; |
| 1129 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1130 | spin_lock(&kvm->mmu_lock); |
| 1131 | if (mmu_notifier_retry(kvm, mmu_seq)) |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1132 | goto out_unlock; |
Christoffer Dall | 9b5fdb9 | 2013-10-02 15:32:01 -0700 | [diff] [blame] | 1133 | if (!hugetlb && !force_pte) |
| 1134 | hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1135 | |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1136 | fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT; |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1137 | |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1138 | if (hugetlb) { |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1139 | pmd_t new_pmd = pfn_pmd(pfn, mem_type); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1140 | new_pmd = pmd_mkhuge(new_pmd); |
| 1141 | if (writable) { |
| 1142 | kvm_set_s2pmd_writable(&new_pmd); |
| 1143 | kvm_set_pfn_dirty(pfn); |
| 1144 | } |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1145 | coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE, |
| 1146 | fault_ipa_uncached); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1147 | ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd); |
| 1148 | } else { |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1149 | pte_t new_pte = pfn_pte(pfn, mem_type); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1150 | if (writable) { |
| 1151 | kvm_set_s2pte_writable(&new_pte); |
| 1152 | kvm_set_pfn_dirty(pfn); |
| 1153 | } |
Laszlo Ersek | 840f4bf | 2014-11-17 14:58:52 +0000 | [diff] [blame] | 1154 | coherent_cache_guest_page(vcpu, hva, PAGE_SIZE, |
| 1155 | fault_ipa_uncached); |
Kim Phillips | b886576 | 2014-06-26 01:45:51 +0100 | [diff] [blame] | 1156 | ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, |
Steve Capper | 3d08c62 | 2014-10-14 15:02:15 +0100 | [diff] [blame] | 1157 | pgprot_val(mem_type) == pgprot_val(PAGE_S2_DEVICE)); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1158 | } |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1159 | |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1160 | |
| 1161 | out_unlock: |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1162 | spin_unlock(&kvm->mmu_lock); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1163 | kvm_release_pfn_clean(pfn); |
Christoffer Dall | ad361f0 | 2012-11-01 17:14:45 +0100 | [diff] [blame] | 1164 | return ret; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * kvm_handle_guest_abort - handles all 2nd stage aborts |
| 1169 | * @vcpu: the VCPU pointer |
| 1170 | * @run: the kvm_run structure |
| 1171 | * |
| 1172 | * Any abort that gets to the host is almost guaranteed to be caused by a |
| 1173 | * missing second stage translation table entry, which can mean that either the |
| 1174 | * guest simply needs more memory and we must allocate an appropriate page or it |
| 1175 | * can mean that the guest tried to access I/O memory, which is emulated by user |
| 1176 | * space. The distinction is based on the IPA causing the fault and whether this |
| 1177 | * memory region has been registered as standard RAM by user space. |
| 1178 | */ |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1179 | int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 1180 | { |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1181 | unsigned long fault_status; |
| 1182 | phys_addr_t fault_ipa; |
| 1183 | struct kvm_memory_slot *memslot; |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1184 | unsigned long hva; |
| 1185 | bool is_iabt, write_fault, writable; |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1186 | gfn_t gfn; |
| 1187 | int ret, idx; |
| 1188 | |
Marc Zyngier | 52d1dba | 2012-10-15 10:33:38 +0100 | [diff] [blame] | 1189 | is_iabt = kvm_vcpu_trap_is_iabt(vcpu); |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 1190 | fault_ipa = kvm_vcpu_get_fault_ipa(vcpu); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1191 | |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 1192 | trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu), |
| 1193 | kvm_vcpu_get_hfar(vcpu), fault_ipa); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1194 | |
| 1195 | /* Check the stage-2 fault is trans. fault or write fault */ |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 1196 | fault_status = kvm_vcpu_trap_get_fault_type(vcpu); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1197 | if (fault_status != FSC_FAULT && fault_status != FSC_PERM) { |
Christoffer Dall | 0496daa5 | 2014-09-26 12:29:34 +0200 | [diff] [blame] | 1198 | kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n", |
| 1199 | kvm_vcpu_trap_get_class(vcpu), |
| 1200 | (unsigned long)kvm_vcpu_trap_get_fault(vcpu), |
| 1201 | (unsigned long)kvm_vcpu_get_hsr(vcpu)); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1202 | return -EFAULT; |
| 1203 | } |
| 1204 | |
| 1205 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 1206 | |
| 1207 | gfn = fault_ipa >> PAGE_SHIFT; |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1208 | memslot = gfn_to_memslot(vcpu->kvm, gfn); |
| 1209 | hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable); |
Ard Biesheuvel | a7d079c | 2014-09-09 11:27:09 +0100 | [diff] [blame] | 1210 | write_fault = kvm_is_write_fault(vcpu); |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1211 | if (kvm_is_error_hva(hva) || (write_fault && !writable)) { |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1212 | if (is_iabt) { |
| 1213 | /* Prefetch Abort on I/O address */ |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 1214 | kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu)); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1215 | ret = 1; |
| 1216 | goto out_unlock; |
| 1217 | } |
| 1218 | |
Marc Zyngier | cfe3950 | 2012-12-12 14:42:09 +0000 | [diff] [blame] | 1219 | /* |
| 1220 | * The IPA is reported as [MAX:12], so we need to |
| 1221 | * complement it with the bottom 12 bits from the |
| 1222 | * faulting VA. This is always 12 bits, irrespective |
| 1223 | * of the page size. |
| 1224 | */ |
| 1225 | fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 1226 | ret = io_mem_abort(vcpu, run, fault_ipa); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1227 | goto out_unlock; |
| 1228 | } |
| 1229 | |
Christoffer Dall | c3058d5 | 2014-10-10 12:14:29 +0200 | [diff] [blame] | 1230 | /* Userspace should not be able to register out-of-bounds IPAs */ |
| 1231 | VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE); |
| 1232 | |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 1233 | ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status); |
Christoffer Dall | 94f8e64 | 2013-01-20 18:28:12 -0500 | [diff] [blame] | 1234 | if (ret == 0) |
| 1235 | ret = 1; |
| 1236 | out_unlock: |
| 1237 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 1238 | return ret; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1239 | } |
| 1240 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1241 | static void handle_hva_to_gpa(struct kvm *kvm, |
| 1242 | unsigned long start, |
| 1243 | unsigned long end, |
| 1244 | void (*handler)(struct kvm *kvm, |
| 1245 | gpa_t gpa, void *data), |
| 1246 | void *data) |
| 1247 | { |
| 1248 | struct kvm_memslots *slots; |
| 1249 | struct kvm_memory_slot *memslot; |
| 1250 | |
| 1251 | slots = kvm_memslots(kvm); |
| 1252 | |
| 1253 | /* we only care about the pages that the guest sees */ |
| 1254 | kvm_for_each_memslot(memslot, slots) { |
| 1255 | unsigned long hva_start, hva_end; |
| 1256 | gfn_t gfn, gfn_end; |
| 1257 | |
| 1258 | hva_start = max(start, memslot->userspace_addr); |
| 1259 | hva_end = min(end, memslot->userspace_addr + |
| 1260 | (memslot->npages << PAGE_SHIFT)); |
| 1261 | if (hva_start >= hva_end) |
| 1262 | continue; |
| 1263 | |
| 1264 | /* |
| 1265 | * {gfn(page) | page intersects with [hva_start, hva_end)} = |
| 1266 | * {gfn_start, gfn_start+1, ..., gfn_end-1}. |
| 1267 | */ |
| 1268 | gfn = hva_to_gfn_memslot(hva_start, memslot); |
| 1269 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); |
| 1270 | |
| 1271 | for (; gfn < gfn_end; ++gfn) { |
| 1272 | gpa_t gpa = gfn << PAGE_SHIFT; |
| 1273 | handler(kvm, gpa, data); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data) |
| 1279 | { |
| 1280 | unmap_stage2_range(kvm, gpa, PAGE_SIZE); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) |
| 1284 | { |
| 1285 | unsigned long end = hva + PAGE_SIZE; |
| 1286 | |
| 1287 | if (!kvm->arch.pgd) |
| 1288 | return 0; |
| 1289 | |
| 1290 | trace_kvm_unmap_hva(hva); |
| 1291 | handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL); |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | int kvm_unmap_hva_range(struct kvm *kvm, |
| 1296 | unsigned long start, unsigned long end) |
| 1297 | { |
| 1298 | if (!kvm->arch.pgd) |
| 1299 | return 0; |
| 1300 | |
| 1301 | trace_kvm_unmap_hva_range(start, end); |
| 1302 | handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL); |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data) |
| 1307 | { |
| 1308 | pte_t *pte = (pte_t *)data; |
| 1309 | |
| 1310 | stage2_set_pte(kvm, NULL, gpa, pte, false); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) |
| 1315 | { |
| 1316 | unsigned long end = hva + PAGE_SIZE; |
| 1317 | pte_t stage2_pte; |
| 1318 | |
| 1319 | if (!kvm->arch.pgd) |
| 1320 | return; |
| 1321 | |
| 1322 | trace_kvm_set_spte_hva(hva); |
| 1323 | stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2); |
| 1324 | handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte); |
| 1325 | } |
| 1326 | |
| 1327 | void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 1328 | { |
| 1329 | mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); |
| 1330 | } |
| 1331 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1332 | phys_addr_t kvm_mmu_get_httbr(void) |
| 1333 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1334 | return virt_to_phys(hyp_pgd); |
| 1335 | } |
| 1336 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1337 | phys_addr_t kvm_mmu_get_boot_httbr(void) |
| 1338 | { |
| 1339 | return virt_to_phys(boot_hyp_pgd); |
| 1340 | } |
| 1341 | |
| 1342 | phys_addr_t kvm_get_idmap_vector(void) |
| 1343 | { |
| 1344 | return hyp_idmap_vector; |
| 1345 | } |
| 1346 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1347 | int kvm_mmu_init(void) |
| 1348 | { |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1349 | int err; |
| 1350 | |
Santosh Shilimkar | 4fda342 | 2013-11-19 14:59:12 -0500 | [diff] [blame] | 1351 | hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start); |
| 1352 | hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end); |
| 1353 | hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1354 | |
| 1355 | if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) { |
| 1356 | /* |
| 1357 | * Our init code is crossing a page boundary. Allocate |
| 1358 | * a bounce page, copy the code over and use that. |
| 1359 | */ |
| 1360 | size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start; |
| 1361 | phys_addr_t phys_base; |
| 1362 | |
Mark Salter | 5d4e08c | 2014-03-28 14:25:19 +0000 | [diff] [blame] | 1363 | init_bounce_page = (void *)__get_free_page(GFP_KERNEL); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1364 | if (!init_bounce_page) { |
| 1365 | kvm_err("Couldn't allocate HYP init bounce page\n"); |
| 1366 | err = -ENOMEM; |
| 1367 | goto out; |
| 1368 | } |
| 1369 | |
| 1370 | memcpy(init_bounce_page, __hyp_idmap_text_start, len); |
| 1371 | /* |
| 1372 | * Warning: the code we just copied to the bounce page |
| 1373 | * must be flushed to the point of coherency. |
| 1374 | * Otherwise, the data may be sitting in L2, and HYP |
| 1375 | * mode won't be able to observe it as it runs with |
| 1376 | * caches off at that point. |
| 1377 | */ |
| 1378 | kvm_flush_dcache_to_poc(init_bounce_page, len); |
| 1379 | |
Santosh Shilimkar | 4fda342 | 2013-11-19 14:59:12 -0500 | [diff] [blame] | 1380 | phys_base = kvm_virt_to_phys(init_bounce_page); |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1381 | hyp_idmap_vector += phys_base - hyp_idmap_start; |
| 1382 | hyp_idmap_start = phys_base; |
| 1383 | hyp_idmap_end = phys_base + len; |
| 1384 | |
| 1385 | kvm_info("Using HYP init bounce page @%lx\n", |
| 1386 | (unsigned long)phys_base); |
| 1387 | } |
| 1388 | |
Christoffer Dall | 38f791a | 2014-10-10 12:14:28 +0200 | [diff] [blame] | 1389 | hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order); |
| 1390 | 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] | 1391 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1392 | if (!hyp_pgd || !boot_hyp_pgd) { |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1393 | kvm_err("Hyp mode PGD not allocated\n"); |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1394 | err = -ENOMEM; |
| 1395 | goto out; |
| 1396 | } |
| 1397 | |
| 1398 | /* Create the idmap in the boot page tables */ |
| 1399 | err = __create_hyp_mappings(boot_hyp_pgd, |
| 1400 | hyp_idmap_start, hyp_idmap_end, |
| 1401 | __phys_to_pfn(hyp_idmap_start), |
| 1402 | PAGE_HYP); |
| 1403 | |
| 1404 | if (err) { |
| 1405 | kvm_err("Failed to idmap %lx-%lx\n", |
| 1406 | hyp_idmap_start, hyp_idmap_end); |
| 1407 | goto out; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1408 | } |
| 1409 | |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1410 | /* Map the very same page at the trampoline VA */ |
| 1411 | err = __create_hyp_mappings(boot_hyp_pgd, |
| 1412 | TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE, |
| 1413 | __phys_to_pfn(hyp_idmap_start), |
| 1414 | PAGE_HYP); |
| 1415 | if (err) { |
| 1416 | kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n", |
| 1417 | TRAMPOLINE_VA); |
| 1418 | goto out; |
| 1419 | } |
| 1420 | |
| 1421 | /* Map the same page again into the runtime page tables */ |
| 1422 | err = __create_hyp_mappings(hyp_pgd, |
| 1423 | TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE, |
| 1424 | __phys_to_pfn(hyp_idmap_start), |
| 1425 | PAGE_HYP); |
| 1426 | if (err) { |
| 1427 | kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n", |
| 1428 | TRAMPOLINE_VA); |
| 1429 | goto out; |
| 1430 | } |
| 1431 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 1432 | return 0; |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1433 | out: |
Marc Zyngier | 4f72827 | 2013-04-12 19:12:05 +0100 | [diff] [blame] | 1434 | free_hyp_pgds(); |
Marc Zyngier | 2fb4105 | 2013-04-12 19:12:03 +0100 | [diff] [blame] | 1435 | return err; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1436 | } |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1437 | |
| 1438 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 1439 | struct kvm_userspace_memory_region *mem, |
| 1440 | const struct kvm_memory_slot *old, |
| 1441 | enum kvm_mr_change change) |
| 1442 | { |
Mario Smarduch | c647355 | 2015-01-15 15:58:56 -0800 | [diff] [blame] | 1443 | #ifdef CONFIG_ARM |
| 1444 | /* |
| 1445 | * At this point memslot has been committed and there is an |
| 1446 | * allocated dirty_bitmap[], dirty pages will be be tracked while the |
| 1447 | * memory slot is write protected. |
| 1448 | */ |
| 1449 | if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) |
| 1450 | kvm_mmu_wp_memory_region(kvm, mem->slot); |
| 1451 | #endif |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 1455 | struct kvm_memory_slot *memslot, |
| 1456 | struct kvm_userspace_memory_region *mem, |
| 1457 | enum kvm_mr_change change) |
| 1458 | { |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1459 | hva_t hva = mem->userspace_addr; |
| 1460 | hva_t reg_end = hva + mem->memory_size; |
| 1461 | bool writable = !(mem->flags & KVM_MEM_READONLY); |
| 1462 | int ret = 0; |
| 1463 | |
| 1464 | if (change != KVM_MR_CREATE && change != KVM_MR_MOVE) |
| 1465 | return 0; |
| 1466 | |
| 1467 | /* |
Christoffer Dall | c3058d5 | 2014-10-10 12:14:29 +0200 | [diff] [blame] | 1468 | * Prevent userspace from creating a memory region outside of the IPA |
| 1469 | * space addressable by the KVM guest IPA space. |
| 1470 | */ |
| 1471 | if (memslot->base_gfn + memslot->npages >= |
| 1472 | (KVM_PHYS_SIZE >> PAGE_SHIFT)) |
| 1473 | return -EFAULT; |
| 1474 | |
| 1475 | /* |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1476 | * A memory region could potentially cover multiple VMAs, and any holes |
| 1477 | * between them, so iterate over all of them to find out if we can map |
| 1478 | * any of them right now. |
| 1479 | * |
| 1480 | * +--------------------------------------------+ |
| 1481 | * +---------------+----------------+ +----------------+ |
| 1482 | * | : VMA 1 | VMA 2 | | VMA 3 : | |
| 1483 | * +---------------+----------------+ +----------------+ |
| 1484 | * | memory region | |
| 1485 | * +--------------------------------------------+ |
| 1486 | */ |
| 1487 | do { |
| 1488 | struct vm_area_struct *vma = find_vma(current->mm, hva); |
| 1489 | hva_t vm_start, vm_end; |
| 1490 | |
| 1491 | if (!vma || vma->vm_start >= reg_end) |
| 1492 | break; |
| 1493 | |
| 1494 | /* |
| 1495 | * Mapping a read-only VMA is only allowed if the |
| 1496 | * memory region is configured as read-only. |
| 1497 | */ |
| 1498 | if (writable && !(vma->vm_flags & VM_WRITE)) { |
| 1499 | ret = -EPERM; |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * Take the intersection of this VMA with the memory region |
| 1505 | */ |
| 1506 | vm_start = max(hva, vma->vm_start); |
| 1507 | vm_end = min(reg_end, vma->vm_end); |
| 1508 | |
| 1509 | if (vma->vm_flags & VM_PFNMAP) { |
| 1510 | gpa_t gpa = mem->guest_phys_addr + |
| 1511 | (vm_start - mem->userspace_addr); |
| 1512 | phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) + |
| 1513 | vm_start - vma->vm_start; |
| 1514 | |
| 1515 | ret = kvm_phys_addr_ioremap(kvm, gpa, pa, |
| 1516 | vm_end - vm_start, |
| 1517 | writable); |
| 1518 | if (ret) |
| 1519 | break; |
| 1520 | } |
| 1521 | hva = vm_end; |
| 1522 | } while (hva < reg_end); |
| 1523 | |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1524 | spin_lock(&kvm->mmu_lock); |
| 1525 | if (ret) |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1526 | unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size); |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1527 | else |
| 1528 | stage2_flush_memslot(kvm, memslot); |
| 1529 | spin_unlock(&kvm->mmu_lock); |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1530 | return ret; |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
| 1534 | struct kvm_memory_slot *dont) |
| 1535 | { |
| 1536 | } |
| 1537 | |
| 1538 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 1539 | unsigned long npages) |
| 1540 | { |
Ard Biesheuvel | 849260c | 2014-11-17 14:58:53 +0000 | [diff] [blame] | 1541 | /* |
| 1542 | * Readonly memslots are not incoherent with the caches by definition, |
| 1543 | * but in practice, they are used mostly to emulate ROMs or NOR flashes |
| 1544 | * that the guest may consider devices and hence map as uncached. |
| 1545 | * To prevent incoherency issues in these cases, tag all readonly |
| 1546 | * regions as incoherent. |
| 1547 | */ |
| 1548 | if (slot->flags & KVM_MEM_READONLY) |
| 1549 | slot->flags |= KVM_MEMSLOT_INCOHERENT; |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | void kvm_arch_memslots_updated(struct kvm *kvm) |
| 1554 | { |
| 1555 | } |
| 1556 | |
| 1557 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 1558 | { |
| 1559 | } |
| 1560 | |
| 1561 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 1562 | struct kvm_memory_slot *slot) |
| 1563 | { |
Ard Biesheuvel | 8eef912 | 2014-10-10 17:00:32 +0200 | [diff] [blame] | 1564 | gpa_t gpa = slot->base_gfn << PAGE_SHIFT; |
| 1565 | phys_addr_t size = slot->npages << PAGE_SHIFT; |
| 1566 | |
| 1567 | spin_lock(&kvm->mmu_lock); |
| 1568 | unmap_stage2_range(kvm, gpa, size); |
| 1569 | spin_unlock(&kvm->mmu_lock); |
Eric Auger | df6ce24 | 2014-06-06 11:10:23 +0200 | [diff] [blame] | 1570 | } |