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