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