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