blob: c23a1b323aad6ca2480c031a498b74854d9db485 [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
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 Dall342cd0a2013-01-20 18:28:06 -050018
19#include <linux/mman.h>
20#include <linux/kvm_host.h>
21#include <linux/io.h>
Christoffer Dallad361f02012-11-01 17:14:45 +010022#include <linux/hugetlb.h>
James Morse196f8782017-06-20 17:11:48 +010023#include <linux/sched/signal.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050024#include <trace/events/kvm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050025#include <asm/pgalloc.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050026#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050027#include <asm/kvm_arm.h>
28#include <asm/kvm_mmu.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050029#include <asm/kvm_mmio.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050030#include <asm/kvm_asm.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050031#include <asm/kvm_emulate.h>
Marc Zyngier1e947ba2015-01-29 11:59:54 +000032#include <asm/virt.h>
Tyler Baicar621f48e2017-06-21 12:17:14 -060033#include <asm/system_misc.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050034
35#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050036
Marc Zyngier5a677ce2013-04-12 19:12:06 +010037static pgd_t *boot_hyp_pgd;
Marc Zyngier2fb41052013-04-12 19:12:03 +010038static pgd_t *hyp_pgd;
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000039static pgd_t *merged_hyp_pgd;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050040static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
41
Marc Zyngier5a677ce2013-04-12 19:12:06 +010042static unsigned long hyp_idmap_start;
43static unsigned long hyp_idmap_end;
44static phys_addr_t hyp_idmap_vector;
45
Marc Zyngiere3f019b2017-12-04 17:04:38 +000046static unsigned long io_map_base;
47
Christoffer Dall38f791a2014-10-10 12:14:28 +020048#define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
Mark Salter5d4e08c2014-03-28 14:25:19 +000049
Mario Smarduch15a49a42015-01-15 15:58:58 -080050#define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
51#define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
52
53static bool memslot_is_logging(struct kvm_memory_slot *memslot)
54{
Mario Smarduch15a49a42015-01-15 15:58:58 -080055 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
Mario Smarduch72760302015-01-15 15:59:01 -080056}
57
58/**
59 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
60 * @kvm: pointer to kvm structure.
61 *
62 * Interface to HYP function to flush all VM TLB entries
63 */
64void kvm_flush_remote_tlbs(struct kvm *kvm)
65{
66 kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
Mario Smarduch15a49a42015-01-15 15:58:58 -080067}
Christoffer Dallad361f02012-11-01 17:14:45 +010068
Marc Zyngier48762762013-01-28 15:27:00 +000069static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050070{
Suzuki K Poulose8684e702016-03-22 17:14:25 +000071 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050072}
73
Marc Zyngier363ef892014-12-19 16:48:06 +000074/*
75 * D-Cache management functions. They take the page table entries by
76 * value, as they are flushing the cache using the kernel mapping (or
77 * kmap on 32bit).
78 */
79static void kvm_flush_dcache_pte(pte_t pte)
80{
81 __kvm_flush_dcache_pte(pte);
82}
83
84static void kvm_flush_dcache_pmd(pmd_t pmd)
85{
86 __kvm_flush_dcache_pmd(pmd);
87}
88
89static void kvm_flush_dcache_pud(pud_t pud)
90{
91 __kvm_flush_dcache_pud(pud);
92}
93
Ard Biesheuvele6fab542015-11-10 15:11:20 +010094static bool kvm_is_device_pfn(unsigned long pfn)
95{
96 return !pfn_valid(pfn);
97}
98
Mario Smarduch15a49a42015-01-15 15:58:58 -080099/**
100 * stage2_dissolve_pmd() - clear and flush huge PMD entry
101 * @kvm: pointer to kvm structure.
102 * @addr: IPA
103 * @pmd: pmd pointer for IPA
104 *
105 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
106 * pages in the range dirty.
107 */
108static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
109{
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000110 if (!pmd_thp_or_huge(*pmd))
Mario Smarduch15a49a42015-01-15 15:58:58 -0800111 return;
112
113 pmd_clear(pmd);
114 kvm_tlb_flush_vmid_ipa(kvm, addr);
115 put_page(virt_to_page(pmd));
116}
117
Christoffer Dalld5d81842013-01-20 18:28:07 -0500118static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
119 int min, int max)
120{
121 void *page;
122
123 BUG_ON(max > KVM_NR_MEM_OBJS);
124 if (cache->nobjs >= min)
125 return 0;
126 while (cache->nobjs < max) {
127 page = (void *)__get_free_page(PGALLOC_GFP);
128 if (!page)
129 return -ENOMEM;
130 cache->objects[cache->nobjs++] = page;
131 }
132 return 0;
133}
134
135static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
136{
137 while (mc->nobjs)
138 free_page((unsigned long)mc->objects[--mc->nobjs]);
139}
140
141static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
142{
143 void *p;
144
145 BUG_ON(!mc || !mc->nobjs);
146 p = mc->objects[--mc->nobjs];
147 return p;
148}
149
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000150static void clear_stage2_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100151{
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100152 pud_t *pud_table __maybe_unused = stage2_pud_offset(kvm, pgd, 0UL);
153 stage2_pgd_clear(kvm, pgd);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200154 kvm_tlb_flush_vmid_ipa(kvm, addr);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100155 stage2_pud_free(kvm, pud_table);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200156 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100157}
158
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000159static void clear_stage2_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500160{
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100161 pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(kvm, pud, 0);
162 VM_BUG_ON(stage2_pud_huge(kvm, *pud));
163 stage2_pud_clear(kvm, pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200164 kvm_tlb_flush_vmid_ipa(kvm, addr);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100165 stage2_pmd_free(kvm, pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100166 put_page(virt_to_page(pud));
167}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500168
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000169static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100170{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200171 pte_t *pte_table = pte_offset_kernel(pmd, 0);
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000172 VM_BUG_ON(pmd_thp_or_huge(*pmd));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200173 pmd_clear(pmd);
174 kvm_tlb_flush_vmid_ipa(kvm, addr);
175 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100176 put_page(virt_to_page(pmd));
177}
178
Marc Zyngier88dc25e82018-05-25 12:23:11 +0100179static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte)
180{
181 WRITE_ONCE(*ptep, new_pte);
182 dsb(ishst);
183}
184
185static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd)
186{
187 WRITE_ONCE(*pmdp, new_pmd);
188 dsb(ishst);
189}
190
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100191static inline void kvm_pmd_populate(pmd_t *pmdp, pte_t *ptep)
192{
193 kvm_set_pmd(pmdp, kvm_mk_pmd(ptep));
194}
195
196static inline void kvm_pud_populate(pud_t *pudp, pmd_t *pmdp)
197{
198 WRITE_ONCE(*pudp, kvm_mk_pud(pmdp));
199 dsb(ishst);
200}
201
202static inline void kvm_pgd_populate(pgd_t *pgdp, pud_t *pudp)
203{
204 WRITE_ONCE(*pgdp, kvm_mk_pgd(pudp));
205 dsb(ishst);
206}
207
Marc Zyngier363ef892014-12-19 16:48:06 +0000208/*
209 * Unmapping vs dcache management:
210 *
211 * If a guest maps certain memory pages as uncached, all writes will
212 * bypass the data cache and go directly to RAM. However, the CPUs
213 * can still speculate reads (not writes) and fill cache lines with
214 * data.
215 *
216 * Those cache lines will be *clean* cache lines though, so a
217 * clean+invalidate operation is equivalent to an invalidate
218 * operation, because no cache lines are marked dirty.
219 *
220 * Those clean cache lines could be filled prior to an uncached write
221 * by the guest, and the cache coherent IO subsystem would therefore
222 * end up writing old data to disk.
223 *
224 * This is why right after unmapping a page/section and invalidating
225 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
226 * the IO subsystem will never hit in the cache.
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100227 *
228 * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
229 * we then fully enforce cacheability of RAM, no matter what the guest
230 * does.
Marc Zyngier363ef892014-12-19 16:48:06 +0000231 */
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000232static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200233 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100234{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200235 phys_addr_t start_addr = addr;
236 pte_t *pte, *start_pte;
237
238 start_pte = pte = pte_offset_kernel(pmd, addr);
239 do {
240 if (!pte_none(*pte)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000241 pte_t old_pte = *pte;
242
Christoffer Dall4f853a72014-05-09 23:31:31 +0200243 kvm_set_pte(pte, __pte(0));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200244 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000245
246 /* No need to invalidate the cache for device mappings */
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100247 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000248 kvm_flush_dcache_pte(old_pte);
249
250 put_page(virt_to_page(pte));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200251 }
252 } while (pte++, addr += PAGE_SIZE, addr != end);
253
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100254 if (stage2_pte_table_empty(kvm, start_pte))
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000255 clear_stage2_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500256}
257
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000258static void unmap_stage2_pmds(struct kvm *kvm, pud_t *pud,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200259 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500260{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200261 phys_addr_t next, start_addr = addr;
262 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000263
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100264 start_pmd = pmd = stage2_pmd_offset(kvm, pud, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200265 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100266 next = stage2_pmd_addr_end(kvm, addr, end);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200267 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000268 if (pmd_thp_or_huge(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000269 pmd_t old_pmd = *pmd;
270
Christoffer Dall4f853a72014-05-09 23:31:31 +0200271 pmd_clear(pmd);
272 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000273
274 kvm_flush_dcache_pmd(old_pmd);
275
Christoffer Dall4f853a72014-05-09 23:31:31 +0200276 put_page(virt_to_page(pmd));
277 } else {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000278 unmap_stage2_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100279 }
280 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200281 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100282
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100283 if (stage2_pmd_table_empty(kvm, start_pmd))
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000284 clear_stage2_pud_entry(kvm, pud, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200285}
286
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000287static void unmap_stage2_puds(struct kvm *kvm, pgd_t *pgd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200288 phys_addr_t addr, phys_addr_t end)
289{
290 phys_addr_t next, start_addr = addr;
291 pud_t *pud, *start_pud;
292
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100293 start_pud = pud = stage2_pud_offset(kvm, pgd, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200294 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100295 next = stage2_pud_addr_end(kvm, addr, end);
296 if (!stage2_pud_none(kvm, *pud)) {
297 if (stage2_pud_huge(kvm, *pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000298 pud_t old_pud = *pud;
299
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100300 stage2_pud_clear(kvm, pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200301 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000302 kvm_flush_dcache_pud(old_pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200303 put_page(virt_to_page(pud));
304 } else {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000305 unmap_stage2_pmds(kvm, pud, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200306 }
307 }
308 } while (pud++, addr = next, addr != end);
309
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100310 if (stage2_pud_table_empty(kvm, start_pud))
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000311 clear_stage2_pgd_entry(kvm, pgd, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200312}
313
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000314/**
315 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
316 * @kvm: The VM pointer
317 * @start: The intermediate physical base address of the range to unmap
318 * @size: The size of the area to unmap
319 *
320 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
321 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
322 * destroying the VM), otherwise another faulting VCPU may come in and mess
323 * with things behind our backs.
324 */
325static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
Christoffer Dall4f853a72014-05-09 23:31:31 +0200326{
327 pgd_t *pgd;
328 phys_addr_t addr = start, end = start + size;
329 phys_addr_t next;
330
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100331 assert_spin_locked(&kvm->mmu_lock);
Jia He47a91b72018-05-21 11:05:30 +0800332 WARN_ON(size & ~PAGE_MASK);
333
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100334 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200335 do {
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +0100336 /*
337 * Make sure the page table is still active, as another thread
338 * could have possibly freed the page table, while we released
339 * the lock.
340 */
341 if (!READ_ONCE(kvm->arch.pgd))
342 break;
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100343 next = stage2_pgd_addr_end(kvm, addr, end);
344 if (!stage2_pgd_none(kvm, *pgd))
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000345 unmap_stage2_puds(kvm, pgd, addr, next);
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100346 /*
347 * If the range is too large, release the kvm->mmu_lock
348 * to prevent starvation and lockup detector warnings.
349 */
350 if (next != end)
351 cond_resched_lock(&kvm->mmu_lock);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200352 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000353}
354
Marc Zyngier9d218a12014-01-15 12:50:23 +0000355static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
356 phys_addr_t addr, phys_addr_t end)
357{
358 pte_t *pte;
359
360 pte = pte_offset_kernel(pmd, addr);
361 do {
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100362 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000363 kvm_flush_dcache_pte(*pte);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000364 } while (pte++, addr += PAGE_SIZE, addr != end);
365}
366
367static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
368 phys_addr_t addr, phys_addr_t end)
369{
370 pmd_t *pmd;
371 phys_addr_t next;
372
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100373 pmd = stage2_pmd_offset(kvm, pud, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000374 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100375 next = stage2_pmd_addr_end(kvm, addr, end);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000376 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000377 if (pmd_thp_or_huge(*pmd))
Marc Zyngier363ef892014-12-19 16:48:06 +0000378 kvm_flush_dcache_pmd(*pmd);
379 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000380 stage2_flush_ptes(kvm, pmd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000381 }
382 } while (pmd++, addr = next, addr != end);
383}
384
385static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
386 phys_addr_t addr, phys_addr_t end)
387{
388 pud_t *pud;
389 phys_addr_t next;
390
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100391 pud = stage2_pud_offset(kvm, pgd, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000392 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100393 next = stage2_pud_addr_end(kvm, addr, end);
394 if (!stage2_pud_none(kvm, *pud)) {
395 if (stage2_pud_huge(kvm, *pud))
Marc Zyngier363ef892014-12-19 16:48:06 +0000396 kvm_flush_dcache_pud(*pud);
397 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000398 stage2_flush_pmds(kvm, pud, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000399 }
400 } while (pud++, addr = next, addr != end);
401}
402
403static void stage2_flush_memslot(struct kvm *kvm,
404 struct kvm_memory_slot *memslot)
405{
406 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
407 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
408 phys_addr_t next;
409 pgd_t *pgd;
410
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100411 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000412 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100413 next = stage2_pgd_addr_end(kvm, addr, end);
414 if (!stage2_pgd_none(kvm, *pgd))
Suzuki K Poulosed2db7772018-09-26 17:32:37 +0100415 stage2_flush_puds(kvm, pgd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000416 } while (pgd++, addr = next, addr != end);
417}
418
419/**
420 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
421 * @kvm: The struct kvm pointer
422 *
423 * Go through the stage 2 page tables and invalidate any cache lines
424 * backing memory already mapped to the VM.
425 */
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000426static void stage2_flush_vm(struct kvm *kvm)
Marc Zyngier9d218a12014-01-15 12:50:23 +0000427{
428 struct kvm_memslots *slots;
429 struct kvm_memory_slot *memslot;
430 int idx;
431
432 idx = srcu_read_lock(&kvm->srcu);
433 spin_lock(&kvm->mmu_lock);
434
435 slots = kvm_memslots(kvm);
436 kvm_for_each_memslot(memslot, slots)
437 stage2_flush_memslot(kvm, memslot);
438
439 spin_unlock(&kvm->mmu_lock);
440 srcu_read_unlock(&kvm->srcu, idx);
441}
442
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000443static void clear_hyp_pgd_entry(pgd_t *pgd)
444{
445 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0UL);
446 pgd_clear(pgd);
447 pud_free(NULL, pud_table);
448 put_page(virt_to_page(pgd));
449}
450
451static void clear_hyp_pud_entry(pud_t *pud)
452{
453 pmd_t *pmd_table __maybe_unused = pmd_offset(pud, 0);
454 VM_BUG_ON(pud_huge(*pud));
455 pud_clear(pud);
456 pmd_free(NULL, pmd_table);
457 put_page(virt_to_page(pud));
458}
459
460static void clear_hyp_pmd_entry(pmd_t *pmd)
461{
462 pte_t *pte_table = pte_offset_kernel(pmd, 0);
463 VM_BUG_ON(pmd_thp_or_huge(*pmd));
464 pmd_clear(pmd);
465 pte_free_kernel(NULL, pte_table);
466 put_page(virt_to_page(pmd));
467}
468
469static void unmap_hyp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
470{
471 pte_t *pte, *start_pte;
472
473 start_pte = pte = pte_offset_kernel(pmd, addr);
474 do {
475 if (!pte_none(*pte)) {
476 kvm_set_pte(pte, __pte(0));
477 put_page(virt_to_page(pte));
478 }
479 } while (pte++, addr += PAGE_SIZE, addr != end);
480
481 if (hyp_pte_table_empty(start_pte))
482 clear_hyp_pmd_entry(pmd);
483}
484
485static void unmap_hyp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
486{
487 phys_addr_t next;
488 pmd_t *pmd, *start_pmd;
489
490 start_pmd = pmd = pmd_offset(pud, addr);
491 do {
492 next = pmd_addr_end(addr, end);
493 /* Hyp doesn't use huge pmds */
494 if (!pmd_none(*pmd))
495 unmap_hyp_ptes(pmd, addr, next);
496 } while (pmd++, addr = next, addr != end);
497
498 if (hyp_pmd_table_empty(start_pmd))
499 clear_hyp_pud_entry(pud);
500}
501
502static void unmap_hyp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
503{
504 phys_addr_t next;
505 pud_t *pud, *start_pud;
506
507 start_pud = pud = pud_offset(pgd, addr);
508 do {
509 next = pud_addr_end(addr, end);
510 /* Hyp doesn't use huge puds */
511 if (!pud_none(*pud))
512 unmap_hyp_pmds(pud, addr, next);
513 } while (pud++, addr = next, addr != end);
514
515 if (hyp_pud_table_empty(start_pud))
516 clear_hyp_pgd_entry(pgd);
517}
518
Marc Zyngier3ddd4552018-03-14 15:17:33 +0000519static unsigned int kvm_pgd_index(unsigned long addr, unsigned int ptrs_per_pgd)
520{
521 return (addr >> PGDIR_SHIFT) & (ptrs_per_pgd - 1);
522}
523
524static void __unmap_hyp_range(pgd_t *pgdp, unsigned long ptrs_per_pgd,
525 phys_addr_t start, u64 size)
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000526{
527 pgd_t *pgd;
528 phys_addr_t addr = start, end = start + size;
529 phys_addr_t next;
530
531 /*
532 * We don't unmap anything from HYP, except at the hyp tear down.
533 * Hence, we don't have to invalidate the TLBs here.
534 */
Marc Zyngier3ddd4552018-03-14 15:17:33 +0000535 pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000536 do {
537 next = pgd_addr_end(addr, end);
538 if (!pgd_none(*pgd))
539 unmap_hyp_puds(pgd, addr, next);
540 } while (pgd++, addr = next, addr != end);
541}
542
Marc Zyngier3ddd4552018-03-14 15:17:33 +0000543static void unmap_hyp_range(pgd_t *pgdp, phys_addr_t start, u64 size)
544{
545 __unmap_hyp_range(pgdp, PTRS_PER_PGD, start, size);
546}
547
548static void unmap_hyp_idmap_range(pgd_t *pgdp, phys_addr_t start, u64 size)
549{
550 __unmap_hyp_range(pgdp, __kvm_idmap_ptrs_per_pgd(), start, size);
551}
552
Marc Zyngier000d3992013-03-05 02:43:17 +0000553/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100554 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000555 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100556 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
557 * therefore contains either mappings in the kernel memory area (above
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000558 * PAGE_OFFSET), or device mappings in the idmap range.
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100559 *
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000560 * boot_hyp_pgd should only map the idmap range, and is only used in
561 * the extended idmap case.
Marc Zyngier000d3992013-03-05 02:43:17 +0000562 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100563void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000564{
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000565 pgd_t *id_pgd;
566
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100567 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100568
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000569 id_pgd = boot_hyp_pgd ? boot_hyp_pgd : hyp_pgd;
570
571 if (id_pgd) {
572 /* In case we never called hyp_mmu_init() */
573 if (!io_map_base)
574 io_map_base = hyp_idmap_start;
575 unmap_hyp_idmap_range(id_pgd, io_map_base,
576 hyp_idmap_start + PAGE_SIZE - io_map_base);
577 }
578
Marc Zyngier26781f9c2016-06-30 18:40:46 +0100579 if (boot_hyp_pgd) {
Marc Zyngier26781f9c2016-06-30 18:40:46 +0100580 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
581 boot_hyp_pgd = NULL;
582 }
583
Marc Zyngier4f728272013-04-12 19:12:05 +0100584 if (hyp_pgd) {
Marc Zyngier7839c672017-12-07 11:45:45 +0000585 unmap_hyp_range(hyp_pgd, kern_hyp_va(PAGE_OFFSET),
586 (uintptr_t)high_memory - PAGE_OFFSET);
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100587
Christoffer Dall38f791a2014-10-10 12:14:28 +0200588 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100589 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100590 }
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000591 if (merged_hyp_pgd) {
592 clear_page(merged_hyp_pgd);
593 free_page((unsigned long)merged_hyp_pgd);
594 merged_hyp_pgd = NULL;
595 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100596
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500597 mutex_unlock(&kvm_hyp_pgd_mutex);
598}
599
600static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100601 unsigned long end, unsigned long pfn,
602 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500603{
604 pte_t *pte;
605 unsigned long addr;
606
Marc Zyngier3562c762013-04-12 19:12:02 +0100607 addr = start;
608 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100609 pte = pte_offset_kernel(pmd, addr);
610 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100611 get_page(virt_to_page(pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100612 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100613 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500614}
615
616static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100617 unsigned long end, unsigned long pfn,
618 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500619{
620 pmd_t *pmd;
621 pte_t *pte;
622 unsigned long addr, next;
623
Marc Zyngier3562c762013-04-12 19:12:02 +0100624 addr = start;
625 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100626 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500627
628 BUG_ON(pmd_sect(*pmd));
629
630 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100631 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500632 if (!pte) {
633 kvm_err("Cannot allocate Hyp pte\n");
634 return -ENOMEM;
635 }
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100636 kvm_pmd_populate(pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100637 get_page(virt_to_page(pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500638 }
639
640 next = pmd_addr_end(addr, end);
641
Marc Zyngier6060df82013-04-12 19:12:01 +0100642 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
643 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100644 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500645
646 return 0;
647}
648
Christoffer Dall38f791a2014-10-10 12:14:28 +0200649static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
650 unsigned long end, unsigned long pfn,
651 pgprot_t prot)
652{
653 pud_t *pud;
654 pmd_t *pmd;
655 unsigned long addr, next;
656 int ret;
657
658 addr = start;
659 do {
660 pud = pud_offset(pgd, addr);
661
662 if (pud_none_or_clear_bad(pud)) {
663 pmd = pmd_alloc_one(NULL, addr);
664 if (!pmd) {
665 kvm_err("Cannot allocate Hyp pmd\n");
666 return -ENOMEM;
667 }
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100668 kvm_pud_populate(pud, pmd);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200669 get_page(virt_to_page(pud));
Christoffer Dall38f791a2014-10-10 12:14:28 +0200670 }
671
672 next = pud_addr_end(addr, end);
673 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
674 if (ret)
675 return ret;
676 pfn += (next - addr) >> PAGE_SHIFT;
677 } while (addr = next, addr != end);
678
679 return 0;
680}
681
Kristina Martsenko98732d12018-01-15 15:23:49 +0000682static int __create_hyp_mappings(pgd_t *pgdp, unsigned long ptrs_per_pgd,
Marc Zyngier6060df82013-04-12 19:12:01 +0100683 unsigned long start, unsigned long end,
684 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500685{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500686 pgd_t *pgd;
687 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500688 unsigned long addr, next;
689 int err = 0;
690
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500691 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100692 addr = start & PAGE_MASK;
693 end = PAGE_ALIGN(end);
694 do {
Marc Zyngier3ddd4552018-03-14 15:17:33 +0000695 pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500696
Christoffer Dall38f791a2014-10-10 12:14:28 +0200697 if (pgd_none(*pgd)) {
698 pud = pud_alloc_one(NULL, addr);
699 if (!pud) {
700 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500701 err = -ENOMEM;
702 goto out;
703 }
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100704 kvm_pgd_populate(pgd, pud);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200705 get_page(virt_to_page(pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500706 }
707
708 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200709 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500710 if (err)
711 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100712 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100713 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500714out:
715 mutex_unlock(&kvm_hyp_pgd_mutex);
716 return err;
717}
718
Christoffer Dall40c27292013-11-15 13:14:12 -0800719static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
720{
721 if (!is_vmalloc_addr(kaddr)) {
722 BUG_ON(!virt_addr_valid(kaddr));
723 return __pa(kaddr);
724 } else {
725 return page_to_phys(vmalloc_to_page(kaddr)) +
726 offset_in_page(kaddr);
727 }
728}
729
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500730/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100731 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500732 * @from: The virtual kernel start address of the range
733 * @to: The virtual kernel end address of the range (exclusive)
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100734 * @prot: The protection to be applied to this range
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500735 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100736 * The same virtual address as the kernel virtual address is also used
737 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
738 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500739 */
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100740int create_hyp_mappings(void *from, void *to, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500741{
Christoffer Dall40c27292013-11-15 13:14:12 -0800742 phys_addr_t phys_addr;
743 unsigned long virt_addr;
Marc Zyngier6c41a412016-06-30 18:40:51 +0100744 unsigned long start = kern_hyp_va((unsigned long)from);
745 unsigned long end = kern_hyp_va((unsigned long)to);
Marc Zyngier6060df82013-04-12 19:12:01 +0100746
Marc Zyngier1e947ba2015-01-29 11:59:54 +0000747 if (is_kernel_in_hyp_mode())
748 return 0;
749
Christoffer Dall40c27292013-11-15 13:14:12 -0800750 start = start & PAGE_MASK;
751 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100752
Christoffer Dall40c27292013-11-15 13:14:12 -0800753 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
754 int err;
755
756 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
Kristina Martsenko98732d12018-01-15 15:23:49 +0000757 err = __create_hyp_mappings(hyp_pgd, PTRS_PER_PGD,
758 virt_addr, virt_addr + PAGE_SIZE,
Christoffer Dall40c27292013-11-15 13:14:12 -0800759 __phys_to_pfn(phys_addr),
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100760 prot);
Christoffer Dall40c27292013-11-15 13:14:12 -0800761 if (err)
762 return err;
763 }
764
765 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500766}
767
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000768static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
769 unsigned long *haddr, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500770{
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000771 pgd_t *pgd = hyp_pgd;
772 unsigned long base;
773 int ret = 0;
Marc Zyngier6060df82013-04-12 19:12:01 +0100774
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000775 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier6060df82013-04-12 19:12:01 +0100776
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000777 /*
778 * This assumes that we we have enough space below the idmap
779 * page to allocate our VAs. If not, the check below will
780 * kick. A potential alternative would be to detect that
781 * overflow and switch to an allocation above the idmap.
782 *
783 * The allocated size is always a multiple of PAGE_SIZE.
784 */
785 size = PAGE_ALIGN(size + offset_in_page(phys_addr));
786 base = io_map_base - size;
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000787
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000788 /*
789 * Verify that BIT(VA_BITS - 1) hasn't been flipped by
790 * allocating the new area, as it would indicate we've
791 * overflowed the idmap/IO address range.
792 */
793 if ((base ^ io_map_base) & BIT(VA_BITS - 1))
794 ret = -ENOMEM;
795 else
796 io_map_base = base;
797
798 mutex_unlock(&kvm_hyp_pgd_mutex);
799
800 if (ret)
801 goto out;
802
803 if (__kvm_cpu_uses_extended_idmap())
804 pgd = boot_hyp_pgd;
805
806 ret = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
807 base, base + size,
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000808 __phys_to_pfn(phys_addr), prot);
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000809 if (ret)
810 goto out;
811
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000812 *haddr = base + offset_in_page(phys_addr);
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000813
814out:
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000815 return ret;
816}
817
818/**
819 * create_hyp_io_mappings - Map IO into both kernel and HYP
820 * @phys_addr: The physical start address which gets mapped
821 * @size: Size of the region being mapped
822 * @kaddr: Kernel VA for this mapping
823 * @haddr: HYP VA for this mapping
824 */
825int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
826 void __iomem **kaddr,
827 void __iomem **haddr)
828{
829 unsigned long addr;
830 int ret;
831
832 *kaddr = ioremap(phys_addr, size);
833 if (!*kaddr)
834 return -ENOMEM;
835
836 if (is_kernel_in_hyp_mode()) {
837 *haddr = *kaddr;
838 return 0;
839 }
840
841 ret = __create_hyp_private_mapping(phys_addr, size,
842 &addr, PAGE_HYP_DEVICE);
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000843 if (ret) {
844 iounmap(*kaddr);
845 *kaddr = NULL;
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000846 *haddr = NULL;
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000847 return ret;
848 }
849
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000850 *haddr = (void __iomem *)addr;
851 return 0;
852}
853
854/**
855 * create_hyp_exec_mappings - Map an executable range into HYP
856 * @phys_addr: The physical start address which gets mapped
857 * @size: Size of the region being mapped
858 * @haddr: HYP VA for this mapping
859 */
860int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
861 void **haddr)
862{
863 unsigned long addr;
864 int ret;
865
866 BUG_ON(is_kernel_in_hyp_mode());
867
868 ret = __create_hyp_private_mapping(phys_addr, size,
869 &addr, PAGE_HYP_EXEC);
870 if (ret) {
871 *haddr = NULL;
872 return ret;
873 }
874
875 *haddr = (void *)addr;
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000876 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500877}
878
Christoffer Dalld5d81842013-01-20 18:28:07 -0500879/**
880 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
881 * @kvm: The KVM struct pointer for the VM.
882 *
Vladimir Murzin9d4dc6882015-11-16 11:28:16 +0000883 * Allocates only the stage-2 HW PGD level table(s) (can support either full
884 * 40-bit input addresses or limited to 32-bit input addresses). Clears the
885 * allocated pages.
Christoffer Dalld5d81842013-01-20 18:28:07 -0500886 *
887 * Note we don't need locking here as this is only called when the VM is
888 * created, which can only be done once.
889 */
890int kvm_alloc_stage2_pgd(struct kvm *kvm)
891{
892 pgd_t *pgd;
893
894 if (kvm->arch.pgd != NULL) {
895 kvm_err("kvm_arch already initialized?\n");
896 return -EINVAL;
897 }
898
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000899 /* Allocate the HW PGD, making sure that each page gets its own refcount */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100900 pgd = alloc_pages_exact(stage2_pgd_size(kvm), GFP_KERNEL | __GFP_ZERO);
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000901 if (!pgd)
Marc Zyngiera9873702015-03-10 19:06:59 +0000902 return -ENOMEM;
903
Christoffer Dalld5d81842013-01-20 18:28:07 -0500904 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500905 return 0;
906}
907
Christoffer Dall957db102014-11-27 10:35:03 +0100908static void stage2_unmap_memslot(struct kvm *kvm,
909 struct kvm_memory_slot *memslot)
910{
911 hva_t hva = memslot->userspace_addr;
912 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
913 phys_addr_t size = PAGE_SIZE * memslot->npages;
914 hva_t reg_end = hva + size;
915
916 /*
917 * A memory region could potentially cover multiple VMAs, and any holes
918 * between them, so iterate over all of them to find out if we should
919 * unmap any of them.
920 *
921 * +--------------------------------------------+
922 * +---------------+----------------+ +----------------+
923 * | : VMA 1 | VMA 2 | | VMA 3 : |
924 * +---------------+----------------+ +----------------+
925 * | memory region |
926 * +--------------------------------------------+
927 */
928 do {
929 struct vm_area_struct *vma = find_vma(current->mm, hva);
930 hva_t vm_start, vm_end;
931
932 if (!vma || vma->vm_start >= reg_end)
933 break;
934
935 /*
936 * Take the intersection of this VMA with the memory region
937 */
938 vm_start = max(hva, vma->vm_start);
939 vm_end = min(reg_end, vma->vm_end);
940
941 if (!(vma->vm_flags & VM_PFNMAP)) {
942 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
943 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
944 }
945 hva = vm_end;
946 } while (hva < reg_end);
947}
948
949/**
950 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
951 * @kvm: The struct kvm pointer
952 *
953 * Go through the memregions and unmap any reguler RAM
954 * backing memory already mapped to the VM.
955 */
956void stage2_unmap_vm(struct kvm *kvm)
957{
958 struct kvm_memslots *slots;
959 struct kvm_memory_slot *memslot;
960 int idx;
961
962 idx = srcu_read_lock(&kvm->srcu);
Marc Zyngier90f6e152017-03-16 18:20:49 +0000963 down_read(&current->mm->mmap_sem);
Christoffer Dall957db102014-11-27 10:35:03 +0100964 spin_lock(&kvm->mmu_lock);
965
966 slots = kvm_memslots(kvm);
967 kvm_for_each_memslot(memslot, slots)
968 stage2_unmap_memslot(kvm, memslot);
969
970 spin_unlock(&kvm->mmu_lock);
Marc Zyngier90f6e152017-03-16 18:20:49 +0000971 up_read(&current->mm->mmap_sem);
Christoffer Dall957db102014-11-27 10:35:03 +0100972 srcu_read_unlock(&kvm->srcu, idx);
973}
974
Christoffer Dalld5d81842013-01-20 18:28:07 -0500975/**
976 * kvm_free_stage2_pgd - free all stage-2 tables
977 * @kvm: The KVM struct pointer for the VM.
978 *
979 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
980 * underlying level-2 and level-3 tables before freeing the actual level-1 table
981 * and setting the struct pointer to NULL.
Christoffer Dalld5d81842013-01-20 18:28:07 -0500982 */
983void kvm_free_stage2_pgd(struct kvm *kvm)
984{
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100985 void *pgd = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500986
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100987 spin_lock(&kvm->mmu_lock);
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100988 if (kvm->arch.pgd) {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100989 unmap_stage2_range(kvm, 0, kvm_phys_size(kvm));
Suzuki K Poulose2952a602017-05-16 10:34:54 +0100990 pgd = READ_ONCE(kvm->arch.pgd);
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100991 kvm->arch.pgd = NULL;
992 }
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100993 spin_unlock(&kvm->mmu_lock);
994
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000995 /* Free the HW pgd, one page at a time */
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100996 if (pgd)
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100997 free_pages_exact(pgd, stage2_pgd_size(kvm));
Christoffer Dalld5d81842013-01-20 18:28:07 -0500998}
999
Christoffer Dall38f791a2014-10-10 12:14:28 +02001000static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1001 phys_addr_t addr)
1002{
1003 pgd_t *pgd;
1004 pud_t *pud;
1005
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001006 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
1007 if (stage2_pgd_none(kvm, *pgd)) {
Christoffer Dall38f791a2014-10-10 12:14:28 +02001008 if (!cache)
1009 return NULL;
1010 pud = mmu_memory_cache_alloc(cache);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001011 stage2_pgd_populate(kvm, pgd, pud);
Christoffer Dall38f791a2014-10-10 12:14:28 +02001012 get_page(virt_to_page(pgd));
1013 }
1014
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001015 return stage2_pud_offset(kvm, pgd, addr);
Christoffer Dall38f791a2014-10-10 12:14:28 +02001016}
1017
Christoffer Dallad361f02012-11-01 17:14:45 +01001018static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1019 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001020{
Christoffer Dalld5d81842013-01-20 18:28:07 -05001021 pud_t *pud;
1022 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001023
Christoffer Dall38f791a2014-10-10 12:14:28 +02001024 pud = stage2_get_pud(kvm, cache, addr);
Marc Zyngierd6dbdd32017-06-05 19:17:18 +01001025 if (!pud)
1026 return NULL;
1027
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001028 if (stage2_pud_none(kvm, *pud)) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001029 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +01001030 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001031 pmd = mmu_memory_cache_alloc(cache);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001032 stage2_pud_populate(kvm, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001033 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +01001034 }
1035
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001036 return stage2_pmd_offset(kvm, pud, addr);
Christoffer Dallad361f02012-11-01 17:14:45 +01001037}
Christoffer Dalld5d81842013-01-20 18:28:07 -05001038
Christoffer Dallad361f02012-11-01 17:14:45 +01001039static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
1040 *cache, phys_addr_t addr, const pmd_t *new_pmd)
1041{
1042 pmd_t *pmd, old_pmd;
1043
1044 pmd = stage2_get_pmd(kvm, cache, addr);
1045 VM_BUG_ON(!pmd);
1046
Christoffer Dallad361f02012-11-01 17:14:45 +01001047 old_pmd = *pmd;
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001048 if (pmd_present(old_pmd)) {
Punit Agrawal86658b82018-08-13 11:43:50 +01001049 /*
1050 * Multiple vcpus faulting on the same PMD entry, can
1051 * lead to them sequentially updating the PMD with the
1052 * same value. Following the break-before-make
1053 * (pmd_clear() followed by tlb_flush()) process can
1054 * hinder forward progress due to refaults generated
1055 * on missing translations.
1056 *
1057 * Skip updating the page table if the entry is
1058 * unchanged.
1059 */
1060 if (pmd_val(old_pmd) == pmd_val(*new_pmd))
1061 return 0;
1062
1063 /*
1064 * Mapping in huge pages should only happen through a
1065 * fault. If a page is merged into a transparent huge
1066 * page, the individual subpages of that huge page
1067 * should be unmapped through MMU notifiers before we
1068 * get here.
1069 *
1070 * Merging of CompoundPages is not supported; they
1071 * should become splitting first, unmapped, merged,
1072 * and mapped back in on-demand.
1073 */
1074 VM_BUG_ON(pmd_pfn(old_pmd) != pmd_pfn(*new_pmd));
1075
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001076 pmd_clear(pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +01001077 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001078 } else {
Christoffer Dallad361f02012-11-01 17:14:45 +01001079 get_page(virt_to_page(pmd));
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001080 }
1081
1082 kvm_set_pmd(pmd, *new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +01001083 return 0;
1084}
1085
Marc Zyngier7a3796d2017-10-23 17:11:21 +01001086static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)
1087{
1088 pmd_t *pmdp;
1089 pte_t *ptep;
1090
1091 pmdp = stage2_get_pmd(kvm, NULL, addr);
1092 if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp))
1093 return false;
1094
1095 if (pmd_thp_or_huge(*pmdp))
1096 return kvm_s2pmd_exec(pmdp);
1097
1098 ptep = pte_offset_kernel(pmdp, addr);
1099 if (!ptep || pte_none(*ptep) || !pte_present(*ptep))
1100 return false;
1101
1102 return kvm_s2pte_exec(ptep);
1103}
1104
Christoffer Dallad361f02012-11-01 17:14:45 +01001105static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
Mario Smarduch15a49a42015-01-15 15:58:58 -08001106 phys_addr_t addr, const pte_t *new_pte,
1107 unsigned long flags)
Christoffer Dallad361f02012-11-01 17:14:45 +01001108{
1109 pmd_t *pmd;
1110 pte_t *pte, old_pte;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001111 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
1112 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
1113
1114 VM_BUG_ON(logging_active && !cache);
Christoffer Dallad361f02012-11-01 17:14:45 +01001115
Christoffer Dall38f791a2014-10-10 12:14:28 +02001116 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +01001117 pmd = stage2_get_pmd(kvm, cache, addr);
1118 if (!pmd) {
1119 /*
1120 * Ignore calls from kvm_set_spte_hva for unallocated
1121 * address ranges.
1122 */
1123 return 0;
1124 }
1125
Mario Smarduch15a49a42015-01-15 15:58:58 -08001126 /*
1127 * While dirty page logging - dissolve huge PMD, then continue on to
1128 * allocate page.
1129 */
1130 if (logging_active)
1131 stage2_dissolve_pmd(kvm, addr, pmd);
1132
Christoffer Dallad361f02012-11-01 17:14:45 +01001133 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -05001134 if (pmd_none(*pmd)) {
1135 if (!cache)
1136 return 0; /* ignore calls from kvm_set_spte_hva */
1137 pte = mmu_memory_cache_alloc(cache);
Marc Zyngier0db9dd82018-06-27 15:51:05 +01001138 kvm_pmd_populate(pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001139 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +01001140 }
1141
1142 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001143
1144 if (iomap && pte_present(*pte))
1145 return -EFAULT;
1146
1147 /* Create 2nd stage page table mapping - Level 3 */
1148 old_pte = *pte;
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001149 if (pte_present(old_pte)) {
Punit Agrawal976d34e2018-08-13 11:43:51 +01001150 /* Skip page table update if there is no change */
1151 if (pte_val(old_pte) == pte_val(*new_pte))
1152 return 0;
1153
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001154 kvm_set_pte(pte, __pte(0));
Marc Zyngier48762762013-01-28 15:27:00 +00001155 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001156 } else {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001157 get_page(virt_to_page(pte));
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001158 }
Christoffer Dalld5d81842013-01-20 18:28:07 -05001159
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001160 kvm_set_pte(pte, *new_pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001161 return 0;
1162}
1163
Catalin Marinas06485052016-04-13 17:57:37 +01001164#ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1165static int stage2_ptep_test_and_clear_young(pte_t *pte)
1166{
1167 if (pte_young(*pte)) {
1168 *pte = pte_mkold(*pte);
1169 return 1;
1170 }
1171 return 0;
1172}
1173#else
1174static int stage2_ptep_test_and_clear_young(pte_t *pte)
1175{
1176 return __ptep_test_and_clear_young(pte);
1177}
1178#endif
1179
1180static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
1181{
1182 return stage2_ptep_test_and_clear_young((pte_t *)pmd);
1183}
1184
Christoffer Dalld5d81842013-01-20 18:28:07 -05001185/**
1186 * kvm_phys_addr_ioremap - map a device range to guest IPA
1187 *
1188 * @kvm: The KVM pointer
1189 * @guest_ipa: The IPA at which to insert the mapping
1190 * @pa: The physical address of the device
1191 * @size: The size of the mapping
1192 */
1193int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001194 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001195{
1196 phys_addr_t addr, end;
1197 int ret = 0;
1198 unsigned long pfn;
1199 struct kvm_mmu_memory_cache cache = { 0, };
1200
1201 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
1202 pfn = __phys_to_pfn(pa);
1203
1204 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +01001205 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001206
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001207 if (writable)
Catalin Marinas06485052016-04-13 17:57:37 +01001208 pte = kvm_s2pte_mkwrite(pte);
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001209
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001210 ret = mmu_topup_memory_cache(&cache,
1211 kvm_mmu_cache_min_pages(kvm),
1212 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001213 if (ret)
1214 goto out;
1215 spin_lock(&kvm->mmu_lock);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001216 ret = stage2_set_pte(kvm, &cache, addr, &pte,
1217 KVM_S2PTE_FLAG_IS_IOMAP);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001218 spin_unlock(&kvm->mmu_lock);
1219 if (ret)
1220 goto out;
1221
1222 pfn++;
1223 }
1224
1225out:
1226 mmu_free_memory_cache(&cache);
1227 return ret;
1228}
1229
Dan Williamsba049e92016-01-15 16:56:11 -08001230static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001231{
Dan Williamsba049e92016-01-15 16:56:11 -08001232 kvm_pfn_t pfn = *pfnp;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001233 gfn_t gfn = *ipap >> PAGE_SHIFT;
Punit Agrawalfd2ef352018-10-01 16:54:35 +01001234 struct page *page = pfn_to_page(pfn);
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001235
Punit Agrawalfd2ef352018-10-01 16:54:35 +01001236 /*
1237 * PageTransCompoungMap() returns true for THP and
1238 * hugetlbfs. Make sure the adjustment is done only for THP
1239 * pages.
1240 */
1241 if (!PageHuge(page) && PageTransCompoundMap(page)) {
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001242 unsigned long mask;
1243 /*
1244 * The address we faulted on is backed by a transparent huge
1245 * page. However, because we map the compound huge page and
1246 * not the individual tail page, we need to transfer the
1247 * refcount to the head page. We have to be careful that the
1248 * THP doesn't start to split while we are adjusting the
1249 * refcounts.
1250 *
1251 * We are sure this doesn't happen, because mmu_notifier_retry
1252 * was successful and we are holding the mmu_lock, so if this
1253 * THP is trying to split, it will be blocked in the mmu
1254 * notifier before touching any of the pages, specifically
1255 * before being able to call __split_huge_page_refcount().
1256 *
1257 * We can therefore safely transfer the refcount from PG_tail
1258 * to PG_head and switch the pfn from a tail page to the head
1259 * page accordingly.
1260 */
1261 mask = PTRS_PER_PMD - 1;
1262 VM_BUG_ON((gfn & mask) != (pfn & mask));
1263 if (pfn & mask) {
1264 *ipap &= PMD_MASK;
1265 kvm_release_pfn_clean(pfn);
1266 pfn &= ~mask;
1267 kvm_get_pfn(pfn);
1268 *pfnp = pfn;
1269 }
1270
1271 return true;
1272 }
1273
1274 return false;
1275}
1276
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001277static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
1278{
1279 if (kvm_vcpu_trap_is_iabt(vcpu))
1280 return false;
1281
1282 return kvm_vcpu_dabt_iswrite(vcpu);
1283}
1284
Mario Smarduchc6473552015-01-15 15:58:56 -08001285/**
1286 * stage2_wp_ptes - write protect PMD range
1287 * @pmd: pointer to pmd entry
1288 * @addr: range start address
1289 * @end: range end address
1290 */
1291static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1292{
1293 pte_t *pte;
1294
1295 pte = pte_offset_kernel(pmd, addr);
1296 do {
1297 if (!pte_none(*pte)) {
1298 if (!kvm_s2pte_readonly(pte))
1299 kvm_set_s2pte_readonly(pte);
1300 }
1301 } while (pte++, addr += PAGE_SIZE, addr != end);
1302}
1303
1304/**
1305 * stage2_wp_pmds - write protect PUD range
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001306 * kvm: kvm instance for the VM
Mario Smarduchc6473552015-01-15 15:58:56 -08001307 * @pud: pointer to pud entry
1308 * @addr: range start address
1309 * @end: range end address
1310 */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001311static void stage2_wp_pmds(struct kvm *kvm, pud_t *pud,
1312 phys_addr_t addr, phys_addr_t end)
Mario Smarduchc6473552015-01-15 15:58:56 -08001313{
1314 pmd_t *pmd;
1315 phys_addr_t next;
1316
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001317 pmd = stage2_pmd_offset(kvm, pud, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001318
1319 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001320 next = stage2_pmd_addr_end(kvm, addr, end);
Mario Smarduchc6473552015-01-15 15:58:56 -08001321 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001322 if (pmd_thp_or_huge(*pmd)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001323 if (!kvm_s2pmd_readonly(pmd))
1324 kvm_set_s2pmd_readonly(pmd);
1325 } else {
1326 stage2_wp_ptes(pmd, addr, next);
1327 }
1328 }
1329 } while (pmd++, addr = next, addr != end);
1330}
1331
1332/**
1333 * stage2_wp_puds - write protect PGD range
1334 * @pgd: pointer to pgd entry
1335 * @addr: range start address
1336 * @end: range end address
1337 *
1338 * Process PUD entries, for a huge PUD we cause a panic.
1339 */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001340static void stage2_wp_puds(struct kvm *kvm, pgd_t *pgd,
1341 phys_addr_t addr, phys_addr_t end)
Mario Smarduchc6473552015-01-15 15:58:56 -08001342{
1343 pud_t *pud;
1344 phys_addr_t next;
1345
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001346 pud = stage2_pud_offset(kvm, pgd, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001347 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001348 next = stage2_pud_addr_end(kvm, addr, end);
1349 if (!stage2_pud_none(kvm, *pud)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001350 /* TODO:PUD not supported, revisit later if supported */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001351 BUG_ON(stage2_pud_huge(kvm, *pud));
1352 stage2_wp_pmds(kvm, pud, addr, next);
Mario Smarduchc6473552015-01-15 15:58:56 -08001353 }
1354 } while (pud++, addr = next, addr != end);
1355}
1356
1357/**
1358 * stage2_wp_range() - write protect stage2 memory region range
1359 * @kvm: The KVM pointer
1360 * @addr: Start address of range
1361 * @end: End address of range
1362 */
1363static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1364{
1365 pgd_t *pgd;
1366 phys_addr_t next;
1367
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001368 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001369 do {
1370 /*
1371 * Release kvm_mmu_lock periodically if the memory region is
1372 * large. Otherwise, we may see kernel panics with
Christoffer Dall227ea812015-01-23 10:49:31 +01001373 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1374 * CONFIG_LOCKDEP. Additionally, holding the lock too long
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001375 * will also starve other vCPUs. We have to also make sure
1376 * that the page tables are not freed while we released
1377 * the lock.
Mario Smarduchc6473552015-01-15 15:58:56 -08001378 */
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001379 cond_resched_lock(&kvm->mmu_lock);
1380 if (!READ_ONCE(kvm->arch.pgd))
1381 break;
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001382 next = stage2_pgd_addr_end(kvm, addr, end);
1383 if (stage2_pgd_present(kvm, *pgd))
1384 stage2_wp_puds(kvm, pgd, addr, next);
Mario Smarduchc6473552015-01-15 15:58:56 -08001385 } while (pgd++, addr = next, addr != end);
1386}
1387
1388/**
1389 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1390 * @kvm: The KVM pointer
1391 * @slot: The memory slot to write protect
1392 *
1393 * Called to start logging dirty pages after memory region
1394 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1395 * all present PMD and PTEs are write protected in the memory region.
1396 * Afterwards read of dirty page log can be called.
1397 *
1398 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1399 * serializing operations for VM memory regions.
1400 */
1401void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1402{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001403 struct kvm_memslots *slots = kvm_memslots(kvm);
1404 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
Mario Smarduchc6473552015-01-15 15:58:56 -08001405 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1406 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1407
1408 spin_lock(&kvm->mmu_lock);
1409 stage2_wp_range(kvm, start, end);
1410 spin_unlock(&kvm->mmu_lock);
1411 kvm_flush_remote_tlbs(kvm);
1412}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001413
1414/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001415 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
Mario Smarduch53c810c2015-01-15 15:58:57 -08001416 * @kvm: The KVM pointer
1417 * @slot: The memory slot associated with mask
1418 * @gfn_offset: The gfn offset in memory slot
1419 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1420 * slot to be write protected
1421 *
1422 * Walks bits set in mask write protects the associated pte's. Caller must
1423 * acquire kvm_mmu_lock.
1424 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001425static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Mario Smarduch53c810c2015-01-15 15:58:57 -08001426 struct kvm_memory_slot *slot,
1427 gfn_t gfn_offset, unsigned long mask)
1428{
1429 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1430 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1431 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1432
1433 stage2_wp_range(kvm, start, end);
1434}
Mario Smarduchc6473552015-01-15 15:58:56 -08001435
Kai Huang3b0f1d02015-01-28 10:54:23 +08001436/*
1437 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1438 * dirty pages.
1439 *
1440 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1441 * enable dirty logging for them.
1442 */
1443void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1444 struct kvm_memory_slot *slot,
1445 gfn_t gfn_offset, unsigned long mask)
1446{
1447 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1448}
1449
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001450static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001451{
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001452 __clean_dcache_guest_page(pfn, size);
Marc Zyngiera15f6932017-10-23 17:11:15 +01001453}
1454
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001455static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
Marc Zyngiera15f6932017-10-23 17:11:15 +01001456{
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001457 __invalidate_icache_guest_page(pfn, size);
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001458}
1459
James Morse196f8782017-06-20 17:11:48 +01001460static void kvm_send_hwpoison_signal(unsigned long address,
1461 struct vm_area_struct *vma)
1462{
1463 siginfo_t info;
1464
Eric W. Biederman3eb0f512018-04-17 15:26:37 -05001465 clear_siginfo(&info);
James Morse196f8782017-06-20 17:11:48 +01001466 info.si_signo = SIGBUS;
1467 info.si_errno = 0;
1468 info.si_code = BUS_MCEERR_AR;
1469 info.si_addr = (void __user *)address;
1470
1471 if (is_vm_hugetlb_page(vma))
1472 info.si_addr_lsb = huge_page_shift(hstate_vma(vma));
1473 else
1474 info.si_addr_lsb = PAGE_SHIFT;
1475
1476 send_sig_info(SIGBUS, &info, current);
1477}
1478
Christoffer Dall94f8e642013-01-20 18:28:12 -05001479static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001480 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001481 unsigned long fault_status)
1482{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001483 int ret;
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001484 bool write_fault, exec_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001485 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001486 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001487 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001488 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001489 struct vm_area_struct *vma;
Dan Williamsba049e92016-01-15 16:56:11 -08001490 kvm_pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001491 pgprot_t mem_type = PAGE_S2;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001492 bool logging_active = memslot_is_logging(memslot);
1493 unsigned long flags = 0;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001494
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001495 write_fault = kvm_is_write_fault(vcpu);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001496 exec_fault = kvm_vcpu_trap_is_iabt(vcpu);
1497 VM_BUG_ON(write_fault && exec_fault);
1498
1499 if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001500 kvm_err("Unexpected L2 read permission error\n");
1501 return -EFAULT;
1502 }
1503
Christoffer Dallad361f02012-11-01 17:14:45 +01001504 /* Let's check if we will get back a huge page backed by hugetlbfs */
1505 down_read(&current->mm->mmap_sem);
1506 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001507 if (unlikely(!vma)) {
1508 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1509 up_read(&current->mm->mmap_sem);
1510 return -EFAULT;
1511 }
1512
Punit Agrawalc507bab2018-01-04 18:24:33 +00001513 if (vma_kernel_pagesize(vma) == PMD_SIZE && !logging_active) {
Christoffer Dallad361f02012-11-01 17:14:45 +01001514 hugetlb = true;
1515 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001516 } else {
1517 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001518 * Pages belonging to memslots that don't have the same
1519 * alignment for userspace and IPA cannot be mapped using
1520 * block descriptors even if the pages belong to a THP for
1521 * the process, because the stage-2 block descriptor will
1522 * cover more than a single THP and we loose atomicity for
1523 * unmapping, updates, and splits of the THP or other pages
1524 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001525 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001526 if ((memslot->userspace_addr & ~PMD_MASK) !=
1527 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001528 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001529 }
1530 up_read(&current->mm->mmap_sem);
1531
Christoffer Dall94f8e642013-01-20 18:28:12 -05001532 /* We need minimum second+third level pages */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001533 ret = mmu_topup_memory_cache(memcache, kvm_mmu_cache_min_pages(kvm),
Christoffer Dall38f791a2014-10-10 12:14:28 +02001534 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001535 if (ret)
1536 return ret;
1537
1538 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1539 /*
1540 * Ensure the read of mmu_notifier_seq happens before we call
1541 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1542 * the page we just got a reference to gets unmapped before we have a
1543 * chance to grab the mmu_lock, which ensure that if the page gets
1544 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1545 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1546 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1547 */
1548 smp_rmb();
1549
Christoffer Dallad361f02012-11-01 17:14:45 +01001550 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
James Morse196f8782017-06-20 17:11:48 +01001551 if (pfn == KVM_PFN_ERR_HWPOISON) {
1552 kvm_send_hwpoison_signal(hva, vma);
1553 return 0;
1554 }
Christoffer Dall9ac71592016-08-17 10:46:10 +02001555 if (is_error_noslot_pfn(pfn))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001556 return -EFAULT;
1557
Mario Smarduch15a49a42015-01-15 15:58:58 -08001558 if (kvm_is_device_pfn(pfn)) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001559 mem_type = PAGE_S2_DEVICE;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001560 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1561 } else if (logging_active) {
1562 /*
1563 * Faults on pages in a memslot with logging enabled
1564 * should not be mapped with huge pages (it introduces churn
1565 * and performance degradation), so force a pte mapping.
1566 */
1567 force_pte = true;
1568 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1569
1570 /*
1571 * Only actually map the page as writable if this was a write
1572 * fault.
1573 */
1574 if (!write_fault)
1575 writable = false;
1576 }
Kim Phillipsb8865762014-06-26 01:45:51 +01001577
Christoffer Dallad361f02012-11-01 17:14:45 +01001578 spin_lock(&kvm->mmu_lock);
1579 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001580 goto out_unlock;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001581
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001582 if (!hugetlb && !force_pte)
1583 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001584
1585 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001586 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001587 new_pmd = pmd_mkhuge(new_pmd);
1588 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001589 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +01001590 kvm_set_pfn_dirty(pfn);
1591 }
Marc Zyngiera9c0e122017-10-23 17:11:20 +01001592
1593 if (fault_status != FSC_PERM)
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001594 clean_dcache_guest_page(pfn, PMD_SIZE);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001595
1596 if (exec_fault) {
1597 new_pmd = kvm_s2pmd_mkexec(new_pmd);
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001598 invalidate_icache_guest_page(pfn, PMD_SIZE);
Marc Zyngier7a3796d2017-10-23 17:11:21 +01001599 } else if (fault_status == FSC_PERM) {
1600 /* Preserve execute if XN was already cleared */
1601 if (stage2_is_exec(kvm, fault_ipa))
1602 new_pmd = kvm_s2pmd_mkexec(new_pmd);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001603 }
Marc Zyngiera15f6932017-10-23 17:11:15 +01001604
Christoffer Dallad361f02012-11-01 17:14:45 +01001605 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1606 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001607 pte_t new_pte = pfn_pte(pfn, mem_type);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001608
Christoffer Dallad361f02012-11-01 17:14:45 +01001609 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001610 new_pte = kvm_s2pte_mkwrite(new_pte);
Christoffer Dallad361f02012-11-01 17:14:45 +01001611 kvm_set_pfn_dirty(pfn);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001612 mark_page_dirty(kvm, gfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001613 }
Marc Zyngiera9c0e122017-10-23 17:11:20 +01001614
1615 if (fault_status != FSC_PERM)
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001616 clean_dcache_guest_page(pfn, PAGE_SIZE);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001617
1618 if (exec_fault) {
1619 new_pte = kvm_s2pte_mkexec(new_pte);
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001620 invalidate_icache_guest_page(pfn, PAGE_SIZE);
Marc Zyngier7a3796d2017-10-23 17:11:21 +01001621 } else if (fault_status == FSC_PERM) {
1622 /* Preserve execute if XN was already cleared */
1623 if (stage2_is_exec(kvm, fault_ipa))
1624 new_pte = kvm_s2pte_mkexec(new_pte);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001625 }
Marc Zyngiera15f6932017-10-23 17:11:15 +01001626
Mario Smarduch15a49a42015-01-15 15:58:58 -08001627 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001628 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001629
Christoffer Dall94f8e642013-01-20 18:28:12 -05001630out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001631 spin_unlock(&kvm->mmu_lock);
Marc Zyngier35307b92015-03-12 18:16:51 +00001632 kvm_set_pfn_accessed(pfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001633 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001634 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001635}
1636
Marc Zyngieraeda9132015-03-12 18:16:52 +00001637/*
1638 * Resolve the access fault by making the page young again.
1639 * Note that because the faulting entry is guaranteed not to be
1640 * cached in the TLB, we don't need to invalidate anything.
Catalin Marinas06485052016-04-13 17:57:37 +01001641 * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
1642 * so there is no need for atomic (pte|pmd)_mkyoung operations.
Marc Zyngieraeda9132015-03-12 18:16:52 +00001643 */
1644static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1645{
1646 pmd_t *pmd;
1647 pte_t *pte;
Dan Williamsba049e92016-01-15 16:56:11 -08001648 kvm_pfn_t pfn;
Marc Zyngieraeda9132015-03-12 18:16:52 +00001649 bool pfn_valid = false;
1650
1651 trace_kvm_access_fault(fault_ipa);
1652
1653 spin_lock(&vcpu->kvm->mmu_lock);
1654
1655 pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
1656 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1657 goto out;
1658
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001659 if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
Marc Zyngieraeda9132015-03-12 18:16:52 +00001660 *pmd = pmd_mkyoung(*pmd);
1661 pfn = pmd_pfn(*pmd);
1662 pfn_valid = true;
1663 goto out;
1664 }
1665
1666 pte = pte_offset_kernel(pmd, fault_ipa);
1667 if (pte_none(*pte)) /* Nothing there either */
1668 goto out;
1669
1670 *pte = pte_mkyoung(*pte); /* Just a page... */
1671 pfn = pte_pfn(*pte);
1672 pfn_valid = true;
1673out:
1674 spin_unlock(&vcpu->kvm->mmu_lock);
1675 if (pfn_valid)
1676 kvm_set_pfn_accessed(pfn);
1677}
1678
Christoffer Dall94f8e642013-01-20 18:28:12 -05001679/**
1680 * kvm_handle_guest_abort - handles all 2nd stage aborts
1681 * @vcpu: the VCPU pointer
1682 * @run: the kvm_run structure
1683 *
1684 * Any abort that gets to the host is almost guaranteed to be caused by a
1685 * missing second stage translation table entry, which can mean that either the
1686 * guest simply needs more memory and we must allocate an appropriate page or it
1687 * can mean that the guest tried to access I/O memory, which is emulated by user
1688 * space. The distinction is based on the IPA causing the fault and whether this
1689 * memory region has been registered as standard RAM by user space.
1690 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001691int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1692{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001693 unsigned long fault_status;
1694 phys_addr_t fault_ipa;
1695 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001696 unsigned long hva;
1697 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001698 gfn_t gfn;
1699 int ret, idx;
1700
Tyler Baicar621f48e2017-06-21 12:17:14 -06001701 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
1702
1703 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
James Morsebb428922017-07-18 13:37:41 +01001704 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Tyler Baicar621f48e2017-06-21 12:17:14 -06001705
James Morsebb428922017-07-18 13:37:41 +01001706 /* Synchronous External Abort? */
1707 if (kvm_vcpu_dabt_isextabt(vcpu)) {
1708 /*
1709 * For RAS the host kernel may handle this abort.
1710 * There is no need to pass the error into the guest.
1711 */
Tyler Baicar621f48e2017-06-21 12:17:14 -06001712 if (!handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
1713 return 1;
Tyler Baicar621f48e2017-06-21 12:17:14 -06001714
James Morsebb428922017-07-18 13:37:41 +01001715 if (unlikely(!is_iabt)) {
1716 kvm_inject_vabt(vcpu);
1717 return 1;
1718 }
Marc Zyngier40557102016-09-06 14:02:15 +01001719 }
1720
Marc Zyngier7393b592012-09-17 19:27:09 +01001721 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1722 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001723
1724 /* Check the stage-2 fault is trans. fault or write fault */
Marc Zyngier35307b92015-03-12 18:16:51 +00001725 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1726 fault_status != FSC_ACCESS) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001727 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1728 kvm_vcpu_trap_get_class(vcpu),
1729 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1730 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001731 return -EFAULT;
1732 }
1733
1734 idx = srcu_read_lock(&vcpu->kvm->srcu);
1735
1736 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001737 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1738 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001739 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001740 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001741 if (is_iabt) {
1742 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001743 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001744 ret = 1;
1745 goto out_unlock;
1746 }
1747
Marc Zyngiercfe39502012-12-12 14:42:09 +00001748 /*
Marc Zyngier57c841f2016-01-29 15:01:28 +00001749 * Check for a cache maintenance operation. Since we
1750 * ended-up here, we know it is outside of any memory
1751 * slot. But we can't find out if that is for a device,
1752 * or if the guest is just being stupid. The only thing
1753 * we know for sure is that this range cannot be cached.
1754 *
1755 * So let's assume that the guest is just being
1756 * cautious, and skip the instruction.
1757 */
1758 if (kvm_vcpu_dabt_is_cm(vcpu)) {
1759 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1760 ret = 1;
1761 goto out_unlock;
1762 }
1763
1764 /*
Marc Zyngiercfe39502012-12-12 14:42:09 +00001765 * The IPA is reported as [MAX:12], so we need to
1766 * complement it with the bottom 12 bits from the
1767 * faulting VA. This is always 12 bits, irrespective
1768 * of the page size.
1769 */
1770 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001771 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001772 goto out_unlock;
1773 }
1774
Christoffer Dallc3058d52014-10-10 12:14:29 +02001775 /* Userspace should not be able to register out-of-bounds IPAs */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001776 VM_BUG_ON(fault_ipa >= kvm_phys_size(vcpu->kvm));
Christoffer Dallc3058d52014-10-10 12:14:29 +02001777
Marc Zyngieraeda9132015-03-12 18:16:52 +00001778 if (fault_status == FSC_ACCESS) {
1779 handle_access_fault(vcpu, fault_ipa);
1780 ret = 1;
1781 goto out_unlock;
1782 }
1783
Christoffer Dall98047882014-08-19 12:18:04 +02001784 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001785 if (ret == 0)
1786 ret = 1;
1787out_unlock:
1788 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1789 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001790}
1791
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001792static int handle_hva_to_gpa(struct kvm *kvm,
1793 unsigned long start,
1794 unsigned long end,
1795 int (*handler)(struct kvm *kvm,
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001796 gpa_t gpa, u64 size,
1797 void *data),
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001798 void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001799{
1800 struct kvm_memslots *slots;
1801 struct kvm_memory_slot *memslot;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001802 int ret = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001803
1804 slots = kvm_memslots(kvm);
1805
1806 /* we only care about the pages that the guest sees */
1807 kvm_for_each_memslot(memslot, slots) {
1808 unsigned long hva_start, hva_end;
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001809 gfn_t gpa;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001810
1811 hva_start = max(start, memslot->userspace_addr);
1812 hva_end = min(end, memslot->userspace_addr +
1813 (memslot->npages << PAGE_SHIFT));
1814 if (hva_start >= hva_end)
1815 continue;
1816
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001817 gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
1818 ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001819 }
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001820
1821 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001822}
1823
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001824static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001825{
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001826 unmap_stage2_range(kvm, gpa, size);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001827 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001828}
1829
Christoffer Dalld5d81842013-01-20 18:28:07 -05001830int kvm_unmap_hva_range(struct kvm *kvm,
1831 unsigned long start, unsigned long end)
1832{
1833 if (!kvm->arch.pgd)
1834 return 0;
1835
1836 trace_kvm_unmap_hva_range(start, end);
1837 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1838 return 0;
1839}
1840
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001841static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001842{
1843 pte_t *pte = (pte_t *)data;
1844
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001845 WARN_ON(size != PAGE_SIZE);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001846 /*
1847 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1848 * flag clear because MMU notifiers will have unmapped a huge PMD before
1849 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1850 * therefore stage2_set_pte() never needs to clear out a huge PMD
1851 * through this calling path.
1852 */
1853 stage2_set_pte(kvm, NULL, gpa, pte, 0);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001854 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001855}
1856
1857
1858void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1859{
1860 unsigned long end = hva + PAGE_SIZE;
Marc Zyngier694556d2018-08-23 09:58:27 +01001861 kvm_pfn_t pfn = pte_pfn(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001862 pte_t stage2_pte;
1863
1864 if (!kvm->arch.pgd)
1865 return;
1866
1867 trace_kvm_set_spte_hva(hva);
Marc Zyngier694556d2018-08-23 09:58:27 +01001868
1869 /*
1870 * We've moved a page around, probably through CoW, so let's treat it
1871 * just like a translation fault and clean the cache to the PoC.
1872 */
1873 clean_dcache_guest_page(pfn, PAGE_SIZE);
1874 stage2_pte = pfn_pte(pfn, PAGE_S2);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001875 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1876}
1877
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001878static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Marc Zyngier35307b92015-03-12 18:16:51 +00001879{
1880 pmd_t *pmd;
1881 pte_t *pte;
1882
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001883 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
Marc Zyngier35307b92015-03-12 18:16:51 +00001884 pmd = stage2_get_pmd(kvm, NULL, gpa);
1885 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1886 return 0;
1887
Catalin Marinas06485052016-04-13 17:57:37 +01001888 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
1889 return stage2_pmdp_test_and_clear_young(pmd);
Marc Zyngier35307b92015-03-12 18:16:51 +00001890
1891 pte = pte_offset_kernel(pmd, gpa);
1892 if (pte_none(*pte))
1893 return 0;
1894
Catalin Marinas06485052016-04-13 17:57:37 +01001895 return stage2_ptep_test_and_clear_young(pte);
Marc Zyngier35307b92015-03-12 18:16:51 +00001896}
1897
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001898static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Marc Zyngier35307b92015-03-12 18:16:51 +00001899{
1900 pmd_t *pmd;
1901 pte_t *pte;
1902
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001903 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
Marc Zyngier35307b92015-03-12 18:16:51 +00001904 pmd = stage2_get_pmd(kvm, NULL, gpa);
1905 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1906 return 0;
1907
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001908 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
Marc Zyngier35307b92015-03-12 18:16:51 +00001909 return pmd_young(*pmd);
1910
1911 pte = pte_offset_kernel(pmd, gpa);
1912 if (!pte_none(*pte)) /* Just a page... */
1913 return pte_young(*pte);
1914
1915 return 0;
1916}
1917
1918int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
1919{
Suzuki K Poulose7e5a6722017-07-05 09:57:00 +01001920 if (!kvm->arch.pgd)
1921 return 0;
Marc Zyngier35307b92015-03-12 18:16:51 +00001922 trace_kvm_age_hva(start, end);
1923 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
1924}
1925
1926int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1927{
Suzuki K Poulose7e5a6722017-07-05 09:57:00 +01001928 if (!kvm->arch.pgd)
1929 return 0;
Marc Zyngier35307b92015-03-12 18:16:51 +00001930 trace_kvm_test_age_hva(hva);
1931 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
1932}
1933
Christoffer Dalld5d81842013-01-20 18:28:07 -05001934void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1935{
1936 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1937}
1938
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001939phys_addr_t kvm_mmu_get_httbr(void)
1940{
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001941 if (__kvm_cpu_uses_extended_idmap())
1942 return virt_to_phys(merged_hyp_pgd);
1943 else
1944 return virt_to_phys(hyp_pgd);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001945}
1946
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001947phys_addr_t kvm_get_idmap_vector(void)
1948{
1949 return hyp_idmap_vector;
1950}
1951
Marc Zyngier0535a3e2016-06-30 18:40:43 +01001952static int kvm_map_idmap_text(pgd_t *pgd)
1953{
1954 int err;
1955
1956 /* Create the idmap in the boot page tables */
Kristina Martsenko98732d12018-01-15 15:23:49 +00001957 err = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
Marc Zyngier0535a3e2016-06-30 18:40:43 +01001958 hyp_idmap_start, hyp_idmap_end,
1959 __phys_to_pfn(hyp_idmap_start),
1960 PAGE_HYP_EXEC);
1961 if (err)
1962 kvm_err("Failed to idmap %lx-%lx\n",
1963 hyp_idmap_start, hyp_idmap_end);
1964
1965 return err;
1966}
1967
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001968int kvm_mmu_init(void)
1969{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001970 int err;
1971
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001972 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
Marc Zyngier46fef152018-03-12 14:25:10 +00001973 hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001974 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
Marc Zyngier46fef152018-03-12 14:25:10 +00001975 hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001976 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001977
Ard Biesheuvel06f75a12015-03-19 16:42:26 +00001978 /*
1979 * We rely on the linker script to ensure at build time that the HYP
1980 * init code does not cross a page boundary.
1981 */
1982 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001983
Marc Zyngierb4ef0492017-12-03 20:04:51 +00001984 kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
1985 kvm_debug("HYP VA range: %lx:%lx\n",
1986 kern_hyp_va(PAGE_OFFSET),
1987 kern_hyp_va((unsigned long)high_memory - 1));
Marc Zyngiereac378a2016-06-30 18:40:50 +01001988
Marc Zyngier6c41a412016-06-30 18:40:51 +01001989 if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
Marc Zyngiered57cac2017-12-03 18:22:49 +00001990 hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
Marc Zyngierd2896d42016-08-22 09:01:17 +01001991 hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
Marc Zyngiereac378a2016-06-30 18:40:50 +01001992 /*
1993 * The idmap page is intersecting with the VA space,
1994 * it is not safe to continue further.
1995 */
1996 kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
1997 err = -EINVAL;
1998 goto out;
1999 }
2000
Christoffer Dall38f791a2014-10-10 12:14:28 +02002001 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Marc Zyngier0535a3e2016-06-30 18:40:43 +01002002 if (!hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05002003 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01002004 err = -ENOMEM;
2005 goto out;
2006 }
2007
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00002008 if (__kvm_cpu_uses_extended_idmap()) {
Marc Zyngier0535a3e2016-06-30 18:40:43 +01002009 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2010 hyp_pgd_order);
2011 if (!boot_hyp_pgd) {
2012 kvm_err("Hyp boot PGD not allocated\n");
2013 err = -ENOMEM;
2014 goto out;
2015 }
2016
2017 err = kvm_map_idmap_text(boot_hyp_pgd);
2018 if (err)
2019 goto out;
2020
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00002021 merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
2022 if (!merged_hyp_pgd) {
2023 kvm_err("Failed to allocate extra HYP pgd\n");
2024 goto out;
2025 }
2026 __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
2027 hyp_idmap_start);
Marc Zyngier0535a3e2016-06-30 18:40:43 +01002028 } else {
2029 err = kvm_map_idmap_text(hyp_pgd);
2030 if (err)
2031 goto out;
Marc Zyngier5a677ce2013-04-12 19:12:06 +01002032 }
2033
Marc Zyngiere3f019b2017-12-04 17:04:38 +00002034 io_map_base = hyp_idmap_start;
Christoffer Dalld5d81842013-01-20 18:28:07 -05002035 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01002036out:
Marc Zyngier4f728272013-04-12 19:12:05 +01002037 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01002038 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05002039}
Eric Augerdf6ce242014-06-06 11:10:23 +02002040
2041void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002042 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02002043 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002044 const struct kvm_memory_slot *new,
Eric Augerdf6ce242014-06-06 11:10:23 +02002045 enum kvm_mr_change change)
2046{
Mario Smarduchc6473552015-01-15 15:58:56 -08002047 /*
2048 * At this point memslot has been committed and there is an
2049 * allocated dirty_bitmap[], dirty pages will be be tracked while the
2050 * memory slot is write protected.
2051 */
2052 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
2053 kvm_mmu_wp_memory_region(kvm, mem->slot);
Eric Augerdf6ce242014-06-06 11:10:23 +02002054}
2055
2056int kvm_arch_prepare_memory_region(struct kvm *kvm,
2057 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002058 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02002059 enum kvm_mr_change change)
2060{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002061 hva_t hva = mem->userspace_addr;
2062 hva_t reg_end = hva + mem->memory_size;
2063 bool writable = !(mem->flags & KVM_MEM_READONLY);
2064 int ret = 0;
2065
Mario Smarduch15a49a42015-01-15 15:58:58 -08002066 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
2067 change != KVM_MR_FLAGS_ONLY)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002068 return 0;
2069
2070 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02002071 * Prevent userspace from creating a memory region outside of the IPA
2072 * space addressable by the KVM guest IPA space.
2073 */
2074 if (memslot->base_gfn + memslot->npages >=
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01002075 (kvm_phys_size(kvm) >> PAGE_SHIFT))
Christoffer Dallc3058d52014-10-10 12:14:29 +02002076 return -EFAULT;
2077
Marc Zyngier72f31042017-03-16 18:20:50 +00002078 down_read(&current->mm->mmap_sem);
Christoffer Dallc3058d52014-10-10 12:14:29 +02002079 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002080 * A memory region could potentially cover multiple VMAs, and any holes
2081 * between them, so iterate over all of them to find out if we can map
2082 * any of them right now.
2083 *
2084 * +--------------------------------------------+
2085 * +---------------+----------------+ +----------------+
2086 * | : VMA 1 | VMA 2 | | VMA 3 : |
2087 * +---------------+----------------+ +----------------+
2088 * | memory region |
2089 * +--------------------------------------------+
2090 */
2091 do {
2092 struct vm_area_struct *vma = find_vma(current->mm, hva);
2093 hva_t vm_start, vm_end;
2094
2095 if (!vma || vma->vm_start >= reg_end)
2096 break;
2097
2098 /*
2099 * Mapping a read-only VMA is only allowed if the
2100 * memory region is configured as read-only.
2101 */
2102 if (writable && !(vma->vm_flags & VM_WRITE)) {
2103 ret = -EPERM;
2104 break;
2105 }
2106
2107 /*
2108 * Take the intersection of this VMA with the memory region
2109 */
2110 vm_start = max(hva, vma->vm_start);
2111 vm_end = min(reg_end, vma->vm_end);
2112
2113 if (vma->vm_flags & VM_PFNMAP) {
2114 gpa_t gpa = mem->guest_phys_addr +
2115 (vm_start - mem->userspace_addr);
Marek Majtykaca09f022015-09-16 12:04:55 +02002116 phys_addr_t pa;
2117
2118 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
2119 pa += vm_start - vma->vm_start;
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002120
Mario Smarduch15a49a42015-01-15 15:58:58 -08002121 /* IO region dirty page logging not allowed */
Marc Zyngier72f31042017-03-16 18:20:50 +00002122 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
2123 ret = -EINVAL;
2124 goto out;
2125 }
Mario Smarduch15a49a42015-01-15 15:58:58 -08002126
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002127 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
2128 vm_end - vm_start,
2129 writable);
2130 if (ret)
2131 break;
2132 }
2133 hva = vm_end;
2134 } while (hva < reg_end);
2135
Mario Smarduch15a49a42015-01-15 15:58:58 -08002136 if (change == KVM_MR_FLAGS_ONLY)
Marc Zyngier72f31042017-03-16 18:20:50 +00002137 goto out;
Mario Smarduch15a49a42015-01-15 15:58:58 -08002138
Ard Biesheuvel849260c2014-11-17 14:58:53 +00002139 spin_lock(&kvm->mmu_lock);
2140 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002141 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00002142 else
2143 stage2_flush_memslot(kvm, memslot);
2144 spin_unlock(&kvm->mmu_lock);
Marc Zyngier72f31042017-03-16 18:20:50 +00002145out:
2146 up_read(&current->mm->mmap_sem);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002147 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02002148}
2149
2150void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
2151 struct kvm_memory_slot *dont)
2152{
2153}
2154
2155int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2156 unsigned long npages)
2157{
2158 return 0;
2159}
2160
Paolo Bonzini15f46012015-05-17 21:26:08 +02002161void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Eric Augerdf6ce242014-06-06 11:10:23 +02002162{
2163}
2164
2165void kvm_arch_flush_shadow_all(struct kvm *kvm)
2166{
Suzuki K Poulose293f2932016-09-08 16:25:49 +01002167 kvm_free_stage2_pgd(kvm);
Eric Augerdf6ce242014-06-06 11:10:23 +02002168}
2169
2170void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
2171 struct kvm_memory_slot *slot)
2172{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002173 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
2174 phys_addr_t size = slot->npages << PAGE_SHIFT;
2175
2176 spin_lock(&kvm->mmu_lock);
2177 unmap_stage2_range(kvm, gpa, size);
2178 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02002179}
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002180
2181/*
2182 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
2183 *
2184 * Main problems:
2185 * - S/W ops are local to a CPU (not broadcast)
2186 * - We have line migration behind our back (speculation)
2187 * - System caches don't support S/W at all (damn!)
2188 *
2189 * In the face of the above, the best we can do is to try and convert
2190 * S/W ops to VA ops. Because the guest is not allowed to infer the
2191 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
2192 * which is a rather good thing for us.
2193 *
2194 * Also, it is only used when turning caches on/off ("The expected
2195 * usage of the cache maintenance instructions that operate by set/way
2196 * is associated with the cache maintenance instructions associated
2197 * with the powerdown and powerup of caches, if this is required by
2198 * the implementation.").
2199 *
2200 * We use the following policy:
2201 *
2202 * - If we trap a S/W operation, we enable VM trapping to detect
2203 * caches being turned on/off, and do a full clean.
2204 *
2205 * - We flush the caches on both caches being turned on and off.
2206 *
2207 * - Once the caches are enabled, we stop trapping VM ops.
2208 */
2209void kvm_set_way_flush(struct kvm_vcpu *vcpu)
2210{
Christoffer Dall3df59d82017-08-03 12:09:05 +02002211 unsigned long hcr = *vcpu_hcr(vcpu);
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002212
2213 /*
2214 * If this is the first time we do a S/W operation
2215 * (i.e. HCR_TVM not set) flush the whole memory, and set the
2216 * VM trapping.
2217 *
2218 * Otherwise, rely on the VM trapping to wait for the MMU +
2219 * Caches to be turned off. At that point, we'll be able to
2220 * clean the caches again.
2221 */
2222 if (!(hcr & HCR_TVM)) {
2223 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
2224 vcpu_has_cache_enabled(vcpu));
2225 stage2_flush_vm(vcpu->kvm);
Christoffer Dall3df59d82017-08-03 12:09:05 +02002226 *vcpu_hcr(vcpu) = hcr | HCR_TVM;
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002227 }
2228}
2229
2230void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
2231{
2232 bool now_enabled = vcpu_has_cache_enabled(vcpu);
2233
2234 /*
2235 * If switching the MMU+caches on, need to invalidate the caches.
2236 * If switching it off, need to clean the caches.
2237 * Clean + invalidate does the trick always.
2238 */
2239 if (now_enabled != was_enabled)
2240 stage2_flush_vm(vcpu->kvm);
2241
2242 /* Caches are now on, stop trapping VM ops (until a S/W op) */
2243 if (now_enabled)
Christoffer Dall3df59d82017-08-03 12:09:05 +02002244 *vcpu_hcr(vcpu) &= ~HCR_TVM;
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002245
2246 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
2247}