blob: 4607e9ca60a277b27cf2d162d5cdf9b5d2efb6e5 [file] [log] [blame]
Thomas Gleixnerd94d71c2019-05-29 07:12:40 -07001// SPDX-License-Identifier: GPL-2.0-only
Christoffer Dall749cf76c2013-01-20 18:28:06 -05002/*
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
Christoffer Dall749cf76c2013-01-20 18:28:06 -05005 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05006
7#include <linux/mman.h>
8#include <linux/kvm_host.h>
9#include <linux/io.h>
Christoffer Dallad361f02012-11-01 17:14:45 +010010#include <linux/hugetlb.h>
James Morse196f8782017-06-20 17:11:48 +010011#include <linux/sched/signal.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050012#include <trace/events/kvm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050013#include <asm/pgalloc.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050014#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050015#include <asm/kvm_arm.h>
16#include <asm/kvm_mmu.h>
Will Deacon0f9d09b2020-09-11 14:25:12 +010017#include <asm/kvm_pgtable.h>
James Morse0db5e022019-01-29 18:48:49 +000018#include <asm/kvm_ras.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050019#include <asm/kvm_asm.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050020#include <asm/kvm_emulate.h>
Marc Zyngier1e947ba2015-01-29 11:59:54 +000021#include <asm/virt.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050022
23#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050024
Will Deacon0f9d09b2020-09-11 14:25:12 +010025static struct kvm_pgtable *hyp_pgtable;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050026static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
27
Marc Zyngier5a677ce2013-04-12 19:12:06 +010028static unsigned long hyp_idmap_start;
29static unsigned long hyp_idmap_end;
30static phys_addr_t hyp_idmap_vector;
31
Marc Zyngiere3f019b2017-12-04 17:04:38 +000032static unsigned long io_map_base;
33
Mario Smarduch15a49a42015-01-15 15:58:58 -080034#define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
35#define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
36
Marc Zyngier6d674e22019-12-11 16:56:48 +000037static bool is_iomap(unsigned long flags)
38{
39 return flags & KVM_S2PTE_FLAG_IS_IOMAP;
40}
41
Mario Smarduch15a49a42015-01-15 15:58:58 -080042static bool memslot_is_logging(struct kvm_memory_slot *memslot)
43{
Mario Smarduch15a49a42015-01-15 15:58:58 -080044 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
Mario Smarduch72760302015-01-15 15:59:01 -080045}
46
47/**
48 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
49 * @kvm: pointer to kvm structure.
50 *
51 * Interface to HYP function to flush all VM TLB entries
52 */
53void kvm_flush_remote_tlbs(struct kvm *kvm)
54{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +010055 kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
Mario Smarduch15a49a42015-01-15 15:58:58 -080056}
Christoffer Dallad361f02012-11-01 17:14:45 +010057
Marc Zyngierefaa5b92019-01-02 12:34:25 +000058static void kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa,
59 int level)
Christoffer Dalld5d81842013-01-20 18:28:07 -050060{
Marc Zyngierefaa5b92019-01-02 12:34:25 +000061 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ipa, level);
Christoffer Dalld5d81842013-01-20 18:28:07 -050062}
63
Marc Zyngier363ef892014-12-19 16:48:06 +000064/*
65 * D-Cache management functions. They take the page table entries by
66 * value, as they are flushing the cache using the kernel mapping (or
67 * kmap on 32bit).
68 */
69static void kvm_flush_dcache_pte(pte_t pte)
70{
71 __kvm_flush_dcache_pte(pte);
72}
73
74static void kvm_flush_dcache_pmd(pmd_t pmd)
75{
76 __kvm_flush_dcache_pmd(pmd);
77}
78
79static void kvm_flush_dcache_pud(pud_t pud)
80{
81 __kvm_flush_dcache_pud(pud);
82}
83
Ard Biesheuvele6fab542015-11-10 15:11:20 +010084static bool kvm_is_device_pfn(unsigned long pfn)
85{
86 return !pfn_valid(pfn);
87}
88
Mario Smarduch15a49a42015-01-15 15:58:58 -080089/**
90 * stage2_dissolve_pmd() - clear and flush huge PMD entry
Christoffer Dalla0e50aa2019-01-04 21:09:05 +010091 * @mmu: pointer to mmu structure to operate on
Mario Smarduch15a49a42015-01-15 15:58:58 -080092 * @addr: IPA
93 * @pmd: pmd pointer for IPA
94 *
Zenghui Yu8324c3d2019-03-25 08:02:05 +000095 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs.
Mario Smarduch15a49a42015-01-15 15:58:58 -080096 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +010097static void stage2_dissolve_pmd(struct kvm_s2_mmu *mmu, phys_addr_t addr, pmd_t *pmd)
Mario Smarduch15a49a42015-01-15 15:58:58 -080098{
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +000099 if (!pmd_thp_or_huge(*pmd))
Mario Smarduch15a49a42015-01-15 15:58:58 -0800100 return;
101
102 pmd_clear(pmd);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000103 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PMD_LEVEL);
Mario Smarduch15a49a42015-01-15 15:58:58 -0800104 put_page(virt_to_page(pmd));
105}
106
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000107/**
108 * stage2_dissolve_pud() - clear and flush huge PUD entry
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100109 * @mmu: pointer to mmu structure to operate on
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000110 * @addr: IPA
111 * @pud: pud pointer for IPA
112 *
Zenghui Yu8324c3d2019-03-25 08:02:05 +0000113 * Function clears a PUD entry, flushes addr 1st and 2nd stage TLBs.
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000114 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100115static void stage2_dissolve_pud(struct kvm_s2_mmu *mmu, phys_addr_t addr, pud_t *pudp)
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000116{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100117 struct kvm *kvm = mmu->kvm;
118
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000119 if (!stage2_pud_huge(kvm, *pudp))
120 return;
121
122 stage2_pud_clear(kvm, pudp);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000123 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PUD_LEVEL);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000124 put_page(virt_to_page(pudp));
125}
126
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100127static void clear_stage2_pgd_entry(struct kvm_s2_mmu *mmu, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100128{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100129 struct kvm *kvm = mmu->kvm;
Mike Rapoporte9f63762020-06-04 16:46:23 -0700130 p4d_t *p4d_table __maybe_unused = stage2_p4d_offset(kvm, pgd, 0UL);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100131 stage2_pgd_clear(kvm, pgd);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000132 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700133 stage2_p4d_free(kvm, p4d_table);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200134 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100135}
136
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100137static void clear_stage2_p4d_entry(struct kvm_s2_mmu *mmu, p4d_t *p4d, phys_addr_t addr)
Mike Rapoporte9f63762020-06-04 16:46:23 -0700138{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100139 struct kvm *kvm = mmu->kvm;
Mike Rapoporte9f63762020-06-04 16:46:23 -0700140 pud_t *pud_table __maybe_unused = stage2_pud_offset(kvm, p4d, 0);
141 stage2_p4d_clear(kvm, p4d);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000142 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700143 stage2_pud_free(kvm, pud_table);
144 put_page(virt_to_page(p4d));
145}
146
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100147static void clear_stage2_pud_entry(struct kvm_s2_mmu *mmu, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500148{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100149 struct kvm *kvm = mmu->kvm;
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100150 pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(kvm, pud, 0);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100151
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100152 VM_BUG_ON(stage2_pud_huge(kvm, *pud));
153 stage2_pud_clear(kvm, pud);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000154 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100155 stage2_pmd_free(kvm, pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100156 put_page(virt_to_page(pud));
157}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500158
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100159static void clear_stage2_pmd_entry(struct kvm_s2_mmu *mmu, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100160{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200161 pte_t *pte_table = pte_offset_kernel(pmd, 0);
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000162 VM_BUG_ON(pmd_thp_or_huge(*pmd));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200163 pmd_clear(pmd);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000164 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_NO_LEVEL_HINT);
Anshuman Khandual14b94d02019-03-12 18:55:45 +0530165 free_page((unsigned long)pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100166 put_page(virt_to_page(pmd));
167}
168
Marc Zyngier88dc25e82018-05-25 12:23:11 +0100169static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte)
170{
171 WRITE_ONCE(*ptep, new_pte);
172 dsb(ishst);
173}
174
175static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd)
176{
177 WRITE_ONCE(*pmdp, new_pmd);
178 dsb(ishst);
179}
180
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100181static inline void kvm_pmd_populate(pmd_t *pmdp, pte_t *ptep)
182{
183 kvm_set_pmd(pmdp, kvm_mk_pmd(ptep));
184}
185
186static inline void kvm_pud_populate(pud_t *pudp, pmd_t *pmdp)
187{
188 WRITE_ONCE(*pudp, kvm_mk_pud(pmdp));
189 dsb(ishst);
190}
191
Mike Rapoporte9f63762020-06-04 16:46:23 -0700192static inline void kvm_p4d_populate(p4d_t *p4dp, pud_t *pudp)
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100193{
Mike Rapoporte9f63762020-06-04 16:46:23 -0700194 WRITE_ONCE(*p4dp, kvm_mk_p4d(pudp));
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100195 dsb(ishst);
196}
197
Mike Rapoporte9f63762020-06-04 16:46:23 -0700198static inline void kvm_pgd_populate(pgd_t *pgdp, p4d_t *p4dp)
199{
200#ifndef __PAGETABLE_P4D_FOLDED
201 WRITE_ONCE(*pgdp, kvm_mk_pgd(p4dp));
202 dsb(ishst);
203#endif
204}
205
Marc Zyngier363ef892014-12-19 16:48:06 +0000206/*
207 * Unmapping vs dcache management:
208 *
209 * If a guest maps certain memory pages as uncached, all writes will
210 * bypass the data cache and go directly to RAM. However, the CPUs
211 * can still speculate reads (not writes) and fill cache lines with
212 * data.
213 *
214 * Those cache lines will be *clean* cache lines though, so a
215 * clean+invalidate operation is equivalent to an invalidate
216 * operation, because no cache lines are marked dirty.
217 *
218 * Those clean cache lines could be filled prior to an uncached write
219 * by the guest, and the cache coherent IO subsystem would therefore
220 * end up writing old data to disk.
221 *
222 * This is why right after unmapping a page/section and invalidating
223 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
224 * the IO subsystem will never hit in the cache.
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100225 *
226 * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
227 * we then fully enforce cacheability of RAM, no matter what the guest
228 * does.
Marc Zyngier363ef892014-12-19 16:48:06 +0000229 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100230static void unmap_stage2_ptes(struct kvm_s2_mmu *mmu, pmd_t *pmd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200231 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100232{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200233 phys_addr_t start_addr = addr;
234 pte_t *pte, *start_pte;
235
236 start_pte = pte = pte_offset_kernel(pmd, addr);
237 do {
238 if (!pte_none(*pte)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000239 pte_t old_pte = *pte;
240
Christoffer Dall4f853a72014-05-09 23:31:31 +0200241 kvm_set_pte(pte, __pte(0));
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000242 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PTE_LEVEL);
Marc Zyngier363ef892014-12-19 16:48:06 +0000243
244 /* No need to invalidate the cache for device mappings */
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100245 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000246 kvm_flush_dcache_pte(old_pte);
247
248 put_page(virt_to_page(pte));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200249 }
250 } while (pte++, addr += PAGE_SIZE, addr != end);
251
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100252 if (stage2_pte_table_empty(mmu->kvm, start_pte))
253 clear_stage2_pmd_entry(mmu, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500254}
255
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100256static void unmap_stage2_pmds(struct kvm_s2_mmu *mmu, pud_t *pud,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200257 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500258{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100259 struct kvm *kvm = mmu->kvm;
Christoffer Dall4f853a72014-05-09 23:31:31 +0200260 phys_addr_t next, start_addr = addr;
261 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000262
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100263 start_pmd = pmd = stage2_pmd_offset(kvm, pud, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200264 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100265 next = stage2_pmd_addr_end(kvm, addr, end);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200266 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000267 if (pmd_thp_or_huge(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000268 pmd_t old_pmd = *pmd;
269
Christoffer Dall4f853a72014-05-09 23:31:31 +0200270 pmd_clear(pmd);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000271 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PMD_LEVEL);
Marc Zyngier363ef892014-12-19 16:48:06 +0000272
273 kvm_flush_dcache_pmd(old_pmd);
274
Christoffer Dall4f853a72014-05-09 23:31:31 +0200275 put_page(virt_to_page(pmd));
276 } else {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100277 unmap_stage2_ptes(mmu, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100278 }
279 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200280 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100281
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100282 if (stage2_pmd_table_empty(kvm, start_pmd))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100283 clear_stage2_pud_entry(mmu, pud, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200284}
285
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100286static void unmap_stage2_puds(struct kvm_s2_mmu *mmu, p4d_t *p4d,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200287 phys_addr_t addr, phys_addr_t end)
288{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100289 struct kvm *kvm = mmu->kvm;
Christoffer Dall4f853a72014-05-09 23:31:31 +0200290 phys_addr_t next, start_addr = addr;
291 pud_t *pud, *start_pud;
292
Mike Rapoporte9f63762020-06-04 16:46:23 -0700293 start_pud = pud = stage2_pud_offset(kvm, p4d, 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);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000301 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PUD_LEVEL);
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 {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100305 unmap_stage2_pmds(mmu, 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))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100311 clear_stage2_p4d_entry(mmu, p4d, start_addr);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700312}
313
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100314static void unmap_stage2_p4ds(struct kvm_s2_mmu *mmu, pgd_t *pgd,
Mike Rapoporte9f63762020-06-04 16:46:23 -0700315 phys_addr_t addr, phys_addr_t end)
316{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100317 struct kvm *kvm = mmu->kvm;
Mike Rapoporte9f63762020-06-04 16:46:23 -0700318 phys_addr_t next, start_addr = addr;
319 p4d_t *p4d, *start_p4d;
320
321 start_p4d = p4d = stage2_p4d_offset(kvm, pgd, addr);
322 do {
323 next = stage2_p4d_addr_end(kvm, addr, end);
324 if (!stage2_p4d_none(kvm, *p4d))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100325 unmap_stage2_puds(mmu, p4d, addr, next);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700326 } while (p4d++, addr = next, addr != end);
327
328 if (stage2_p4d_table_empty(kvm, start_p4d))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100329 clear_stage2_pgd_entry(mmu, pgd, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200330}
331
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000332/**
333 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
334 * @kvm: The VM pointer
335 * @start: The intermediate physical base address of the range to unmap
336 * @size: The size of the area to unmap
337 *
338 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
339 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
340 * destroying the VM), otherwise another faulting VCPU may come in and mess
341 * with things behind our backs.
342 */
Will Deaconb5331372020-08-11 11:27:25 +0100343static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
344 bool may_block)
Christoffer Dall4f853a72014-05-09 23:31:31 +0200345{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100346 struct kvm *kvm = mmu->kvm;
Christoffer Dall4f853a72014-05-09 23:31:31 +0200347 pgd_t *pgd;
348 phys_addr_t addr = start, end = start + size;
349 phys_addr_t next;
350
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100351 assert_spin_locked(&kvm->mmu_lock);
Jia He47a91b72018-05-21 11:05:30 +0800352 WARN_ON(size & ~PAGE_MASK);
353
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100354 pgd = mmu->pgd + stage2_pgd_index(kvm, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200355 do {
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +0100356 /*
357 * Make sure the page table is still active, as another thread
358 * could have possibly freed the page table, while we released
359 * the lock.
360 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100361 if (!READ_ONCE(mmu->pgd))
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +0100362 break;
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100363 next = stage2_pgd_addr_end(kvm, addr, end);
364 if (!stage2_pgd_none(kvm, *pgd))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100365 unmap_stage2_p4ds(mmu, pgd, addr, next);
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100366 /*
367 * If the range is too large, release the kvm->mmu_lock
368 * to prevent starvation and lockup detector warnings.
369 */
Will Deaconb5331372020-08-11 11:27:25 +0100370 if (may_block && next != end)
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100371 cond_resched_lock(&kvm->mmu_lock);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200372 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000373}
374
Will Deaconb5331372020-08-11 11:27:25 +0100375static void unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size)
376{
377 __unmap_stage2_range(mmu, start, size, true);
378}
379
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100380static void stage2_flush_ptes(struct kvm_s2_mmu *mmu, pmd_t *pmd,
Marc Zyngier9d218a12014-01-15 12:50:23 +0000381 phys_addr_t addr, phys_addr_t end)
382{
383 pte_t *pte;
384
385 pte = pte_offset_kernel(pmd, addr);
386 do {
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100387 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000388 kvm_flush_dcache_pte(*pte);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000389 } while (pte++, addr += PAGE_SIZE, addr != end);
390}
391
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100392static void stage2_flush_pmds(struct kvm_s2_mmu *mmu, pud_t *pud,
Marc Zyngier9d218a12014-01-15 12:50:23 +0000393 phys_addr_t addr, phys_addr_t end)
394{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100395 struct kvm *kvm = mmu->kvm;
Marc Zyngier9d218a12014-01-15 12:50:23 +0000396 pmd_t *pmd;
397 phys_addr_t next;
398
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100399 pmd = stage2_pmd_offset(kvm, pud, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000400 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100401 next = stage2_pmd_addr_end(kvm, addr, end);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000402 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000403 if (pmd_thp_or_huge(*pmd))
Marc Zyngier363ef892014-12-19 16:48:06 +0000404 kvm_flush_dcache_pmd(*pmd);
405 else
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100406 stage2_flush_ptes(mmu, pmd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000407 }
408 } while (pmd++, addr = next, addr != end);
409}
410
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100411static void stage2_flush_puds(struct kvm_s2_mmu *mmu, p4d_t *p4d,
Marc Zyngier9d218a12014-01-15 12:50:23 +0000412 phys_addr_t addr, phys_addr_t end)
413{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100414 struct kvm *kvm = mmu->kvm;
Marc Zyngier9d218a12014-01-15 12:50:23 +0000415 pud_t *pud;
416 phys_addr_t next;
417
Mike Rapoporte9f63762020-06-04 16:46:23 -0700418 pud = stage2_pud_offset(kvm, p4d, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000419 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100420 next = stage2_pud_addr_end(kvm, addr, end);
421 if (!stage2_pud_none(kvm, *pud)) {
422 if (stage2_pud_huge(kvm, *pud))
Marc Zyngier363ef892014-12-19 16:48:06 +0000423 kvm_flush_dcache_pud(*pud);
424 else
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100425 stage2_flush_pmds(mmu, pud, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000426 }
427 } while (pud++, addr = next, addr != end);
428}
429
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100430static void stage2_flush_p4ds(struct kvm_s2_mmu *mmu, pgd_t *pgd,
Mike Rapoporte9f63762020-06-04 16:46:23 -0700431 phys_addr_t addr, phys_addr_t end)
432{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100433 struct kvm *kvm = mmu->kvm;
Mike Rapoporte9f63762020-06-04 16:46:23 -0700434 p4d_t *p4d;
435 phys_addr_t next;
436
437 p4d = stage2_p4d_offset(kvm, pgd, addr);
438 do {
439 next = stage2_p4d_addr_end(kvm, addr, end);
440 if (!stage2_p4d_none(kvm, *p4d))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100441 stage2_flush_puds(mmu, p4d, addr, next);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700442 } while (p4d++, addr = next, addr != end);
443}
444
Marc Zyngier9d218a12014-01-15 12:50:23 +0000445static void stage2_flush_memslot(struct kvm *kvm,
446 struct kvm_memory_slot *memslot)
447{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100448 struct kvm_s2_mmu *mmu = &kvm->arch.mmu;
Marc Zyngier9d218a12014-01-15 12:50:23 +0000449 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
450 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
451 phys_addr_t next;
452 pgd_t *pgd;
453
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100454 pgd = mmu->pgd + stage2_pgd_index(kvm, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000455 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100456 next = stage2_pgd_addr_end(kvm, addr, end);
457 if (!stage2_pgd_none(kvm, *pgd))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100458 stage2_flush_p4ds(mmu, pgd, addr, next);
Jiang Yi48c963e2020-04-15 10:42:29 +0200459
460 if (next != end)
461 cond_resched_lock(&kvm->mmu_lock);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000462 } while (pgd++, addr = next, addr != end);
463}
464
465/**
466 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
467 * @kvm: The struct kvm pointer
468 *
469 * Go through the stage 2 page tables and invalidate any cache lines
470 * backing memory already mapped to the VM.
471 */
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000472static void stage2_flush_vm(struct kvm *kvm)
Marc Zyngier9d218a12014-01-15 12:50:23 +0000473{
474 struct kvm_memslots *slots;
475 struct kvm_memory_slot *memslot;
476 int idx;
477
478 idx = srcu_read_lock(&kvm->srcu);
479 spin_lock(&kvm->mmu_lock);
480
481 slots = kvm_memslots(kvm);
482 kvm_for_each_memslot(memslot, slots)
483 stage2_flush_memslot(kvm, memslot);
484
485 spin_unlock(&kvm->mmu_lock);
486 srcu_read_unlock(&kvm->srcu, idx);
487}
488
Marc Zyngier000d3992013-03-05 02:43:17 +0000489/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100490 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000491 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100492void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000493{
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100494 mutex_lock(&kvm_hyp_pgd_mutex);
Will Deacon0f9d09b2020-09-11 14:25:12 +0100495 if (hyp_pgtable) {
496 kvm_pgtable_hyp_destroy(hyp_pgtable);
497 kfree(hyp_pgtable);
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000498 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500499 mutex_unlock(&kvm_hyp_pgd_mutex);
500}
501
Will Deacon0f9d09b2020-09-11 14:25:12 +0100502static int __create_hyp_mappings(unsigned long start, unsigned long size,
503 unsigned long phys, enum kvm_pgtable_prot prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500504{
Will Deacon0f9d09b2020-09-11 14:25:12 +0100505 int err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500506
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500507 mutex_lock(&kvm_hyp_pgd_mutex);
Will Deacon0f9d09b2020-09-11 14:25:12 +0100508 err = kvm_pgtable_hyp_map(hyp_pgtable, start, size, phys, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500509 mutex_unlock(&kvm_hyp_pgd_mutex);
Will Deacon0f9d09b2020-09-11 14:25:12 +0100510
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500511 return err;
512}
513
Christoffer Dall40c27292013-11-15 13:14:12 -0800514static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
515{
516 if (!is_vmalloc_addr(kaddr)) {
517 BUG_ON(!virt_addr_valid(kaddr));
518 return __pa(kaddr);
519 } else {
520 return page_to_phys(vmalloc_to_page(kaddr)) +
521 offset_in_page(kaddr);
522 }
523}
524
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500525/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100526 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500527 * @from: The virtual kernel start address of the range
528 * @to: The virtual kernel end address of the range (exclusive)
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100529 * @prot: The protection to be applied to this range
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500530 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100531 * The same virtual address as the kernel virtual address is also used
532 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
533 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500534 */
Will Deacon0f9d09b2020-09-11 14:25:12 +0100535int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500536{
Christoffer Dall40c27292013-11-15 13:14:12 -0800537 phys_addr_t phys_addr;
538 unsigned long virt_addr;
Marc Zyngier6c41a412016-06-30 18:40:51 +0100539 unsigned long start = kern_hyp_va((unsigned long)from);
540 unsigned long end = kern_hyp_va((unsigned long)to);
Marc Zyngier6060df82013-04-12 19:12:01 +0100541
Marc Zyngier1e947ba2015-01-29 11:59:54 +0000542 if (is_kernel_in_hyp_mode())
543 return 0;
544
Christoffer Dall40c27292013-11-15 13:14:12 -0800545 start = start & PAGE_MASK;
546 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100547
Christoffer Dall40c27292013-11-15 13:14:12 -0800548 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
549 int err;
550
551 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
Will Deacon0f9d09b2020-09-11 14:25:12 +0100552 err = __create_hyp_mappings(virt_addr, PAGE_SIZE, phys_addr,
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100553 prot);
Christoffer Dall40c27292013-11-15 13:14:12 -0800554 if (err)
555 return err;
556 }
557
558 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500559}
560
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000561static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
Will Deacon0f9d09b2020-09-11 14:25:12 +0100562 unsigned long *haddr,
563 enum kvm_pgtable_prot prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500564{
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000565 unsigned long base;
566 int ret = 0;
Marc Zyngier6060df82013-04-12 19:12:01 +0100567
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000568 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier6060df82013-04-12 19:12:01 +0100569
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000570 /*
Fuad Tabba656012c2020-04-01 15:03:10 +0100571 * This assumes that we have enough space below the idmap
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000572 * page to allocate our VAs. If not, the check below will
573 * kick. A potential alternative would be to detect that
574 * overflow and switch to an allocation above the idmap.
575 *
576 * The allocated size is always a multiple of PAGE_SIZE.
577 */
578 size = PAGE_ALIGN(size + offset_in_page(phys_addr));
579 base = io_map_base - size;
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000580
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000581 /*
582 * Verify that BIT(VA_BITS - 1) hasn't been flipped by
583 * allocating the new area, as it would indicate we've
584 * overflowed the idmap/IO address range.
585 */
586 if ((base ^ io_map_base) & BIT(VA_BITS - 1))
587 ret = -ENOMEM;
588 else
589 io_map_base = base;
590
591 mutex_unlock(&kvm_hyp_pgd_mutex);
592
593 if (ret)
594 goto out;
595
Will Deacon0f9d09b2020-09-11 14:25:12 +0100596 ret = __create_hyp_mappings(base, size, phys_addr, prot);
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000597 if (ret)
598 goto out;
599
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000600 *haddr = base + offset_in_page(phys_addr);
Marc Zyngiere3f019b2017-12-04 17:04:38 +0000601out:
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000602 return ret;
603}
604
605/**
606 * create_hyp_io_mappings - Map IO into both kernel and HYP
607 * @phys_addr: The physical start address which gets mapped
608 * @size: Size of the region being mapped
609 * @kaddr: Kernel VA for this mapping
610 * @haddr: HYP VA for this mapping
611 */
612int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
613 void __iomem **kaddr,
614 void __iomem **haddr)
615{
616 unsigned long addr;
617 int ret;
618
619 *kaddr = ioremap(phys_addr, size);
620 if (!*kaddr)
621 return -ENOMEM;
622
623 if (is_kernel_in_hyp_mode()) {
624 *haddr = *kaddr;
625 return 0;
626 }
627
628 ret = __create_hyp_private_mapping(phys_addr, size,
629 &addr, PAGE_HYP_DEVICE);
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000630 if (ret) {
631 iounmap(*kaddr);
632 *kaddr = NULL;
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000633 *haddr = NULL;
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000634 return ret;
635 }
636
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000637 *haddr = (void __iomem *)addr;
638 return 0;
639}
640
641/**
642 * create_hyp_exec_mappings - Map an executable range into HYP
643 * @phys_addr: The physical start address which gets mapped
644 * @size: Size of the region being mapped
645 * @haddr: HYP VA for this mapping
646 */
647int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
648 void **haddr)
649{
650 unsigned long addr;
651 int ret;
652
653 BUG_ON(is_kernel_in_hyp_mode());
654
655 ret = __create_hyp_private_mapping(phys_addr, size,
656 &addr, PAGE_HYP_EXEC);
657 if (ret) {
658 *haddr = NULL;
659 return ret;
660 }
661
662 *haddr = (void *)addr;
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000663 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500664}
665
Christoffer Dalld5d81842013-01-20 18:28:07 -0500666/**
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100667 * kvm_init_stage2_mmu - Initialise a S2 MMU strucrure
668 * @kvm: The pointer to the KVM structure
669 * @mmu: The pointer to the s2 MMU structure
Christoffer Dalld5d81842013-01-20 18:28:07 -0500670 *
Will Deacon71233d02020-09-11 14:25:13 +0100671 * Allocates only the stage-2 HW PGD level table(s).
Christoffer Dalld5d81842013-01-20 18:28:07 -0500672 * Note we don't need locking here as this is only called when the VM is
673 * created, which can only be done once.
674 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100675int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500676{
Will Deacon71233d02020-09-11 14:25:13 +0100677 int cpu, err;
678 struct kvm_pgtable *pgt;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500679
Will Deacon71233d02020-09-11 14:25:13 +0100680 if (mmu->pgt != NULL) {
Christoffer Dalld5d81842013-01-20 18:28:07 -0500681 kvm_err("kvm_arch already initialized?\n");
682 return -EINVAL;
683 }
684
Will Deacon71233d02020-09-11 14:25:13 +0100685 pgt = kzalloc(sizeof(*pgt), GFP_KERNEL);
686 if (!pgt)
Marc Zyngiera9873702015-03-10 19:06:59 +0000687 return -ENOMEM;
688
Will Deacon71233d02020-09-11 14:25:13 +0100689 err = kvm_pgtable_stage2_init(pgt, kvm);
690 if (err)
691 goto out_free_pgtable;
Christoffer Dalle329fb72018-12-11 15:26:31 +0100692
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100693 mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
694 if (!mmu->last_vcpu_ran) {
Will Deacon71233d02020-09-11 14:25:13 +0100695 err = -ENOMEM;
696 goto out_destroy_pgtable;
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100697 }
698
699 for_each_possible_cpu(cpu)
700 *per_cpu_ptr(mmu->last_vcpu_ran, cpu) = -1;
701
702 mmu->kvm = kvm;
Will Deacon71233d02020-09-11 14:25:13 +0100703 mmu->pgt = pgt;
704 mmu->pgd_phys = __pa(pgt->pgd);
705 mmu->pgd = (void *)pgt->pgd;
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100706 mmu->vmid.vmid_gen = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500707 return 0;
Will Deacon71233d02020-09-11 14:25:13 +0100708
709out_destroy_pgtable:
710 kvm_pgtable_stage2_destroy(pgt);
711out_free_pgtable:
712 kfree(pgt);
713 return err;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500714}
715
Christoffer Dall957db102014-11-27 10:35:03 +0100716static void stage2_unmap_memslot(struct kvm *kvm,
717 struct kvm_memory_slot *memslot)
718{
719 hva_t hva = memslot->userspace_addr;
720 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
721 phys_addr_t size = PAGE_SIZE * memslot->npages;
722 hva_t reg_end = hva + size;
723
724 /*
725 * A memory region could potentially cover multiple VMAs, and any holes
726 * between them, so iterate over all of them to find out if we should
727 * unmap any of them.
728 *
729 * +--------------------------------------------+
730 * +---------------+----------------+ +----------------+
731 * | : VMA 1 | VMA 2 | | VMA 3 : |
732 * +---------------+----------------+ +----------------+
733 * | memory region |
734 * +--------------------------------------------+
735 */
736 do {
737 struct vm_area_struct *vma = find_vma(current->mm, hva);
738 hva_t vm_start, vm_end;
739
740 if (!vma || vma->vm_start >= reg_end)
741 break;
742
743 /*
744 * Take the intersection of this VMA with the memory region
745 */
746 vm_start = max(hva, vma->vm_start);
747 vm_end = min(reg_end, vma->vm_end);
748
749 if (!(vma->vm_flags & VM_PFNMAP)) {
750 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100751 unmap_stage2_range(&kvm->arch.mmu, gpa, vm_end - vm_start);
Christoffer Dall957db102014-11-27 10:35:03 +0100752 }
753 hva = vm_end;
754 } while (hva < reg_end);
755}
756
757/**
758 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
759 * @kvm: The struct kvm pointer
760 *
Fuad Tabba656012c2020-04-01 15:03:10 +0100761 * Go through the memregions and unmap any regular RAM
Christoffer Dall957db102014-11-27 10:35:03 +0100762 * backing memory already mapped to the VM.
763 */
764void stage2_unmap_vm(struct kvm *kvm)
765{
766 struct kvm_memslots *slots;
767 struct kvm_memory_slot *memslot;
768 int idx;
769
770 idx = srcu_read_lock(&kvm->srcu);
Michel Lespinasse89154dd2020-06-08 21:33:29 -0700771 mmap_read_lock(current->mm);
Christoffer Dall957db102014-11-27 10:35:03 +0100772 spin_lock(&kvm->mmu_lock);
773
774 slots = kvm_memslots(kvm);
775 kvm_for_each_memslot(memslot, slots)
776 stage2_unmap_memslot(kvm, memslot);
777
778 spin_unlock(&kvm->mmu_lock);
Michel Lespinasse89154dd2020-06-08 21:33:29 -0700779 mmap_read_unlock(current->mm);
Christoffer Dall957db102014-11-27 10:35:03 +0100780 srcu_read_unlock(&kvm->srcu, idx);
781}
782
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100783void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500784{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100785 struct kvm *kvm = mmu->kvm;
Will Deacon71233d02020-09-11 14:25:13 +0100786 struct kvm_pgtable *pgt = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500787
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100788 spin_lock(&kvm->mmu_lock);
Will Deacon71233d02020-09-11 14:25:13 +0100789 pgt = mmu->pgt;
790 if (pgt) {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100791 mmu->pgd = NULL;
Will Deacon71233d02020-09-11 14:25:13 +0100792 mmu->pgd_phys = 0;
793 mmu->pgt = NULL;
794 free_percpu(mmu->last_vcpu_ran);
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100795 }
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100796 spin_unlock(&kvm->mmu_lock);
797
Will Deacon71233d02020-09-11 14:25:13 +0100798 if (pgt) {
799 kvm_pgtable_stage2_destroy(pgt);
800 kfree(pgt);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100801 }
Christoffer Dalld5d81842013-01-20 18:28:07 -0500802}
803
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100804static p4d_t *stage2_get_p4d(struct kvm_s2_mmu *mmu, struct kvm_mmu_memory_cache *cache,
Christoffer Dall38f791a2014-10-10 12:14:28 +0200805 phys_addr_t addr)
806{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100807 struct kvm *kvm = mmu->kvm;
Christoffer Dall38f791a2014-10-10 12:14:28 +0200808 pgd_t *pgd;
Mike Rapoporte9f63762020-06-04 16:46:23 -0700809 p4d_t *p4d;
Christoffer Dall38f791a2014-10-10 12:14:28 +0200810
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100811 pgd = mmu->pgd + stage2_pgd_index(kvm, addr);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100812 if (stage2_pgd_none(kvm, *pgd)) {
Christoffer Dall38f791a2014-10-10 12:14:28 +0200813 if (!cache)
814 return NULL;
Sean Christophersonc1a33ae2020-07-02 19:35:42 -0700815 p4d = kvm_mmu_memory_cache_alloc(cache);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700816 stage2_pgd_populate(kvm, pgd, p4d);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200817 get_page(virt_to_page(pgd));
818 }
819
Mike Rapoporte9f63762020-06-04 16:46:23 -0700820 return stage2_p4d_offset(kvm, pgd, addr);
821}
822
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100823static pud_t *stage2_get_pud(struct kvm_s2_mmu *mmu, struct kvm_mmu_memory_cache *cache,
Mike Rapoporte9f63762020-06-04 16:46:23 -0700824 phys_addr_t addr)
825{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100826 struct kvm *kvm = mmu->kvm;
Mike Rapoporte9f63762020-06-04 16:46:23 -0700827 p4d_t *p4d;
828 pud_t *pud;
829
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100830 p4d = stage2_get_p4d(mmu, cache, addr);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700831 if (stage2_p4d_none(kvm, *p4d)) {
832 if (!cache)
833 return NULL;
Sean Christophersonc1a33ae2020-07-02 19:35:42 -0700834 pud = kvm_mmu_memory_cache_alloc(cache);
Mike Rapoporte9f63762020-06-04 16:46:23 -0700835 stage2_p4d_populate(kvm, p4d, pud);
836 get_page(virt_to_page(p4d));
837 }
838
839 return stage2_pud_offset(kvm, p4d, addr);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200840}
841
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100842static pmd_t *stage2_get_pmd(struct kvm_s2_mmu *mmu, struct kvm_mmu_memory_cache *cache,
Christoffer Dallad361f02012-11-01 17:14:45 +0100843 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500844{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100845 struct kvm *kvm = mmu->kvm;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500846 pud_t *pud;
847 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500848
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100849 pud = stage2_get_pud(mmu, cache, addr);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000850 if (!pud || stage2_pud_huge(kvm, *pud))
Marc Zyngierd6dbdd32017-06-05 19:17:18 +0100851 return NULL;
852
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100853 if (stage2_pud_none(kvm, *pud)) {
Christoffer Dalld5d81842013-01-20 18:28:07 -0500854 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100855 return NULL;
Sean Christophersonc1a33ae2020-07-02 19:35:42 -0700856 pmd = kvm_mmu_memory_cache_alloc(cache);
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100857 stage2_pud_populate(kvm, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500858 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100859 }
860
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100861 return stage2_pmd_offset(kvm, pud, addr);
Christoffer Dallad361f02012-11-01 17:14:45 +0100862}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500863
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100864static int stage2_set_pmd_huge(struct kvm_s2_mmu *mmu,
865 struct kvm_mmu_memory_cache *cache,
866 phys_addr_t addr, const pmd_t *new_pmd)
Christoffer Dallad361f02012-11-01 17:14:45 +0100867{
868 pmd_t *pmd, old_pmd;
869
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000870retry:
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100871 pmd = stage2_get_pmd(mmu, cache, addr);
Christoffer Dallad361f02012-11-01 17:14:45 +0100872 VM_BUG_ON(!pmd);
873
Christoffer Dallad361f02012-11-01 17:14:45 +0100874 old_pmd = *pmd;
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000875 /*
876 * Multiple vcpus faulting on the same PMD entry, can
877 * lead to them sequentially updating the PMD with the
878 * same value. Following the break-before-make
879 * (pmd_clear() followed by tlb_flush()) process can
880 * hinder forward progress due to refaults generated
881 * on missing translations.
882 *
883 * Skip updating the page table if the entry is
884 * unchanged.
885 */
886 if (pmd_val(old_pmd) == pmd_val(*new_pmd))
887 return 0;
888
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100889 if (pmd_present(old_pmd)) {
Punit Agrawal86658b82018-08-13 11:43:50 +0100890 /*
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000891 * If we already have PTE level mapping for this block,
892 * we must unmap it to avoid inconsistent TLB state and
893 * leaking the table page. We could end up in this situation
894 * if the memory slot was marked for dirty logging and was
895 * reverted, leaving PTE level mappings for the pages accessed
896 * during the period. So, unmap the PTE level mapping for this
897 * block and retry, as we could have released the upper level
898 * table in the process.
Punit Agrawal86658b82018-08-13 11:43:50 +0100899 *
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000900 * Normal THP split/merge follows mmu_notifier callbacks and do
901 * get handled accordingly.
Punit Agrawal86658b82018-08-13 11:43:50 +0100902 */
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000903 if (!pmd_thp_or_huge(old_pmd)) {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100904 unmap_stage2_range(mmu, addr & S2_PMD_MASK, S2_PMD_SIZE);
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000905 goto retry;
906 }
Punit Agrawal86658b82018-08-13 11:43:50 +0100907 /*
908 * Mapping in huge pages should only happen through a
909 * fault. If a page is merged into a transparent huge
910 * page, the individual subpages of that huge page
911 * should be unmapped through MMU notifiers before we
912 * get here.
913 *
914 * Merging of CompoundPages is not supported; they
915 * should become splitting first, unmapped, merged,
916 * and mapped back in on-demand.
917 */
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000918 WARN_ON_ONCE(pmd_pfn(old_pmd) != pmd_pfn(*new_pmd));
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100919 pmd_clear(pmd);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000920 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PMD_LEVEL);
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100921 } else {
Christoffer Dallad361f02012-11-01 17:14:45 +0100922 get_page(virt_to_page(pmd));
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100923 }
924
925 kvm_set_pmd(pmd, *new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +0100926 return 0;
927}
928
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100929static int stage2_set_pud_huge(struct kvm_s2_mmu *mmu,
930 struct kvm_mmu_memory_cache *cache,
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000931 phys_addr_t addr, const pud_t *new_pudp)
932{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100933 struct kvm *kvm = mmu->kvm;
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000934 pud_t *pudp, old_pud;
935
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000936retry:
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100937 pudp = stage2_get_pud(mmu, cache, addr);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000938 VM_BUG_ON(!pudp);
939
940 old_pud = *pudp;
941
942 /*
943 * A large number of vcpus faulting on the same stage 2 entry,
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000944 * can lead to a refault due to the stage2_pud_clear()/tlb_flush().
945 * Skip updating the page tables if there is no change.
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000946 */
947 if (pud_val(old_pud) == pud_val(*new_pudp))
948 return 0;
949
950 if (stage2_pud_present(kvm, old_pud)) {
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000951 /*
952 * If we already have table level mapping for this block, unmap
953 * the range for this block and retry.
954 */
955 if (!stage2_pud_huge(kvm, old_pud)) {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100956 unmap_stage2_range(mmu, addr & S2_PUD_MASK, S2_PUD_SIZE);
Suzuki K Poulose3c3736c2019-03-20 14:57:19 +0000957 goto retry;
958 }
959
960 WARN_ON_ONCE(kvm_pud_pfn(old_pud) != kvm_pud_pfn(*new_pudp));
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000961 stage2_pud_clear(kvm, pudp);
Marc Zyngierefaa5b92019-01-02 12:34:25 +0000962 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PUD_LEVEL);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000963 } else {
964 get_page(virt_to_page(pudp));
965 }
966
967 kvm_set_pud(pudp, *new_pudp);
968 return 0;
969}
970
Punit Agrawal86d1c552018-12-11 17:10:38 +0000971/*
972 * stage2_get_leaf_entry - walk the stage2 VM page tables and return
973 * true if a valid and present leaf-entry is found. A pointer to the
974 * leaf-entry is returned in the appropriate level variable - pudpp,
975 * pmdpp, ptepp.
976 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100977static bool stage2_get_leaf_entry(struct kvm_s2_mmu *mmu, phys_addr_t addr,
Punit Agrawal86d1c552018-12-11 17:10:38 +0000978 pud_t **pudpp, pmd_t **pmdpp, pte_t **ptepp)
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100979{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100980 struct kvm *kvm = mmu->kvm;
Punit Agrawal86d1c552018-12-11 17:10:38 +0000981 pud_t *pudp;
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100982 pmd_t *pmdp;
983 pte_t *ptep;
984
Punit Agrawal86d1c552018-12-11 17:10:38 +0000985 *pudpp = NULL;
986 *pmdpp = NULL;
987 *ptepp = NULL;
988
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100989 pudp = stage2_get_pud(mmu, NULL, addr);
Punit Agrawal86d1c552018-12-11 17:10:38 +0000990 if (!pudp || stage2_pud_none(kvm, *pudp) || !stage2_pud_present(kvm, *pudp))
991 return false;
992
993 if (stage2_pud_huge(kvm, *pudp)) {
994 *pudpp = pudp;
995 return true;
996 }
997
998 pmdp = stage2_pmd_offset(kvm, pudp, addr);
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100999 if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp))
1000 return false;
1001
Punit Agrawal86d1c552018-12-11 17:10:38 +00001002 if (pmd_thp_or_huge(*pmdp)) {
1003 *pmdpp = pmdp;
1004 return true;
1005 }
Marc Zyngier7a3796d2017-10-23 17:11:21 +01001006
1007 ptep = pte_offset_kernel(pmdp, addr);
1008 if (!ptep || pte_none(*ptep) || !pte_present(*ptep))
1009 return false;
1010
Punit Agrawal86d1c552018-12-11 17:10:38 +00001011 *ptepp = ptep;
1012 return true;
1013}
1014
Paolo Bonzini0378dae2020-08-09 12:58:23 -04001015static bool stage2_is_exec(struct kvm_s2_mmu *mmu, phys_addr_t addr, unsigned long sz)
Punit Agrawal86d1c552018-12-11 17:10:38 +00001016{
1017 pud_t *pudp;
1018 pmd_t *pmdp;
1019 pte_t *ptep;
1020 bool found;
1021
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001022 found = stage2_get_leaf_entry(mmu, addr, &pudp, &pmdp, &ptep);
Punit Agrawal86d1c552018-12-11 17:10:38 +00001023 if (!found)
1024 return false;
1025
1026 if (pudp)
Will Deaconb757b472020-07-23 11:17:14 +01001027 return sz <= PUD_SIZE && kvm_s2pud_exec(pudp);
Punit Agrawal86d1c552018-12-11 17:10:38 +00001028 else if (pmdp)
Will Deaconb757b472020-07-23 11:17:14 +01001029 return sz <= PMD_SIZE && kvm_s2pmd_exec(pmdp);
Punit Agrawal86d1c552018-12-11 17:10:38 +00001030 else
Will Deaconb757b472020-07-23 11:17:14 +01001031 return sz == PAGE_SIZE && kvm_s2pte_exec(ptep);
Marc Zyngier7a3796d2017-10-23 17:11:21 +01001032}
1033
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001034static int stage2_set_pte(struct kvm_s2_mmu *mmu,
1035 struct kvm_mmu_memory_cache *cache,
Mario Smarduch15a49a42015-01-15 15:58:58 -08001036 phys_addr_t addr, const pte_t *new_pte,
1037 unsigned long flags)
Christoffer Dallad361f02012-11-01 17:14:45 +01001038{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001039 struct kvm *kvm = mmu->kvm;
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001040 pud_t *pud;
Christoffer Dallad361f02012-11-01 17:14:45 +01001041 pmd_t *pmd;
1042 pte_t *pte, old_pte;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001043 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
1044 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
1045
1046 VM_BUG_ON(logging_active && !cache);
Christoffer Dallad361f02012-11-01 17:14:45 +01001047
Christoffer Dall38f791a2014-10-10 12:14:28 +02001048 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001049 pud = stage2_get_pud(mmu, cache, addr);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001050 if (!pud) {
1051 /*
1052 * Ignore calls from kvm_set_spte_hva for unallocated
1053 * address ranges.
1054 */
1055 return 0;
1056 }
1057
1058 /*
1059 * While dirty page logging - dissolve huge PUD, then continue
1060 * on to allocate page.
1061 */
1062 if (logging_active)
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001063 stage2_dissolve_pud(mmu, addr, pud);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001064
1065 if (stage2_pud_none(kvm, *pud)) {
1066 if (!cache)
1067 return 0; /* ignore calls from kvm_set_spte_hva */
Sean Christophersonc1a33ae2020-07-02 19:35:42 -07001068 pmd = kvm_mmu_memory_cache_alloc(cache);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001069 stage2_pud_populate(kvm, pud, pmd);
1070 get_page(virt_to_page(pud));
1071 }
1072
1073 pmd = stage2_pmd_offset(kvm, pud, addr);
Christoffer Dallad361f02012-11-01 17:14:45 +01001074 if (!pmd) {
1075 /*
1076 * Ignore calls from kvm_set_spte_hva for unallocated
1077 * address ranges.
1078 */
1079 return 0;
1080 }
1081
Mario Smarduch15a49a42015-01-15 15:58:58 -08001082 /*
1083 * While dirty page logging - dissolve huge PMD, then continue on to
1084 * allocate page.
1085 */
1086 if (logging_active)
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001087 stage2_dissolve_pmd(mmu, addr, pmd);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001088
Christoffer Dallad361f02012-11-01 17:14:45 +01001089 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -05001090 if (pmd_none(*pmd)) {
1091 if (!cache)
1092 return 0; /* ignore calls from kvm_set_spte_hva */
Sean Christophersonc1a33ae2020-07-02 19:35:42 -07001093 pte = kvm_mmu_memory_cache_alloc(cache);
Marc Zyngier0db9dd82018-06-27 15:51:05 +01001094 kvm_pmd_populate(pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001095 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +01001096 }
1097
1098 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001099
1100 if (iomap && pte_present(*pte))
1101 return -EFAULT;
1102
1103 /* Create 2nd stage page table mapping - Level 3 */
1104 old_pte = *pte;
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001105 if (pte_present(old_pte)) {
Punit Agrawal976d34e2018-08-13 11:43:51 +01001106 /* Skip page table update if there is no change */
1107 if (pte_val(old_pte) == pte_val(*new_pte))
1108 return 0;
1109
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001110 kvm_set_pte(pte, __pte(0));
Marc Zyngierefaa5b92019-01-02 12:34:25 +00001111 kvm_tlb_flush_vmid_ipa(mmu, addr, S2_PTE_LEVEL);
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001112 } else {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001113 get_page(virt_to_page(pte));
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001114 }
Christoffer Dalld5d81842013-01-20 18:28:07 -05001115
Marc Zyngierd4b9e072016-04-28 16:16:31 +01001116 kvm_set_pte(pte, *new_pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001117 return 0;
1118}
1119
Catalin Marinas06485052016-04-13 17:57:37 +01001120#ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1121static int stage2_ptep_test_and_clear_young(pte_t *pte)
1122{
1123 if (pte_young(*pte)) {
1124 *pte = pte_mkold(*pte);
1125 return 1;
1126 }
1127 return 0;
1128}
1129#else
1130static int stage2_ptep_test_and_clear_young(pte_t *pte)
1131{
1132 return __ptep_test_and_clear_young(pte);
1133}
1134#endif
1135
1136static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
1137{
1138 return stage2_ptep_test_and_clear_young((pte_t *)pmd);
1139}
1140
Punit Agrawal35a63962018-12-11 17:10:40 +00001141static int stage2_pudp_test_and_clear_young(pud_t *pud)
1142{
1143 return stage2_ptep_test_and_clear_young((pte_t *)pud);
1144}
1145
Christoffer Dalld5d81842013-01-20 18:28:07 -05001146/**
1147 * kvm_phys_addr_ioremap - map a device range to guest IPA
1148 *
1149 * @kvm: The KVM pointer
1150 * @guest_ipa: The IPA at which to insert the mapping
1151 * @pa: The physical address of the device
1152 * @size: The size of the mapping
1153 */
1154int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001155 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001156{
1157 phys_addr_t addr, end;
1158 int ret = 0;
1159 unsigned long pfn;
Sean Christophersonc1a33ae2020-07-02 19:35:42 -07001160 struct kvm_mmu_memory_cache cache = { 0, __GFP_ZERO, NULL, };
Christoffer Dalld5d81842013-01-20 18:28:07 -05001161
1162 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
1163 pfn = __phys_to_pfn(pa);
1164
1165 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Punit Agrawalf8df7332018-12-11 17:10:36 +00001166 pte_t pte = kvm_pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001167
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001168 if (writable)
Catalin Marinas06485052016-04-13 17:57:37 +01001169 pte = kvm_s2pte_mkwrite(pte);
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001170
Sean Christophersonc1a33ae2020-07-02 19:35:42 -07001171 ret = kvm_mmu_topup_memory_cache(&cache,
1172 kvm_mmu_cache_min_pages(kvm));
Christoffer Dalld5d81842013-01-20 18:28:07 -05001173 if (ret)
1174 goto out;
1175 spin_lock(&kvm->mmu_lock);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001176 ret = stage2_set_pte(&kvm->arch.mmu, &cache, addr, &pte,
1177 KVM_S2PTE_FLAG_IS_IOMAP);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001178 spin_unlock(&kvm->mmu_lock);
1179 if (ret)
1180 goto out;
1181
1182 pfn++;
1183 }
1184
1185out:
Sean Christophersonc1a33ae2020-07-02 19:35:42 -07001186 kvm_mmu_free_memory_cache(&cache);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001187 return ret;
1188}
1189
Mario Smarduchc6473552015-01-15 15:58:56 -08001190/**
1191 * stage2_wp_ptes - write protect PMD range
1192 * @pmd: pointer to pmd entry
1193 * @addr: range start address
1194 * @end: range end address
1195 */
1196static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1197{
1198 pte_t *pte;
1199
1200 pte = pte_offset_kernel(pmd, addr);
1201 do {
1202 if (!pte_none(*pte)) {
1203 if (!kvm_s2pte_readonly(pte))
1204 kvm_set_s2pte_readonly(pte);
1205 }
1206 } while (pte++, addr += PAGE_SIZE, addr != end);
1207}
1208
1209/**
1210 * stage2_wp_pmds - write protect PUD range
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001211 * kvm: kvm instance for the VM
Mario Smarduchc6473552015-01-15 15:58:56 -08001212 * @pud: pointer to pud entry
1213 * @addr: range start address
1214 * @end: range end address
1215 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001216static void stage2_wp_pmds(struct kvm_s2_mmu *mmu, pud_t *pud,
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001217 phys_addr_t addr, phys_addr_t end)
Mario Smarduchc6473552015-01-15 15:58:56 -08001218{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001219 struct kvm *kvm = mmu->kvm;
Mario Smarduchc6473552015-01-15 15:58:56 -08001220 pmd_t *pmd;
1221 phys_addr_t next;
1222
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001223 pmd = stage2_pmd_offset(kvm, pud, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001224
1225 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001226 next = stage2_pmd_addr_end(kvm, addr, end);
Mario Smarduchc6473552015-01-15 15:58:56 -08001227 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001228 if (pmd_thp_or_huge(*pmd)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001229 if (!kvm_s2pmd_readonly(pmd))
1230 kvm_set_s2pmd_readonly(pmd);
1231 } else {
1232 stage2_wp_ptes(pmd, addr, next);
1233 }
1234 }
1235 } while (pmd++, addr = next, addr != end);
1236}
1237
1238/**
Mike Rapoporte9f63762020-06-04 16:46:23 -07001239 * stage2_wp_puds - write protect P4D range
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001240 * @p4d: pointer to p4d entry
Zenghui Yu8324c3d2019-03-25 08:02:05 +00001241 * @addr: range start address
1242 * @end: range end address
1243 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001244static void stage2_wp_puds(struct kvm_s2_mmu *mmu, p4d_t *p4d,
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001245 phys_addr_t addr, phys_addr_t end)
Mario Smarduchc6473552015-01-15 15:58:56 -08001246{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001247 struct kvm *kvm = mmu->kvm;
Mario Smarduchc6473552015-01-15 15:58:56 -08001248 pud_t *pud;
1249 phys_addr_t next;
1250
Mike Rapoporte9f63762020-06-04 16:46:23 -07001251 pud = stage2_pud_offset(kvm, p4d, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001252 do {
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001253 next = stage2_pud_addr_end(kvm, addr, end);
1254 if (!stage2_pud_none(kvm, *pud)) {
Punit Agrawal4ea5af52018-12-11 17:10:37 +00001255 if (stage2_pud_huge(kvm, *pud)) {
1256 if (!kvm_s2pud_readonly(pud))
1257 kvm_set_s2pud_readonly(pud);
1258 } else {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001259 stage2_wp_pmds(mmu, pud, addr, next);
Punit Agrawal4ea5af52018-12-11 17:10:37 +00001260 }
Mario Smarduchc6473552015-01-15 15:58:56 -08001261 }
1262 } while (pud++, addr = next, addr != end);
1263}
1264
1265/**
Mike Rapoporte9f63762020-06-04 16:46:23 -07001266 * stage2_wp_p4ds - write protect PGD range
1267 * @pgd: pointer to pgd entry
1268 * @addr: range start address
1269 * @end: range end address
1270 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001271static void stage2_wp_p4ds(struct kvm_s2_mmu *mmu, pgd_t *pgd,
Mike Rapoporte9f63762020-06-04 16:46:23 -07001272 phys_addr_t addr, phys_addr_t end)
1273{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001274 struct kvm *kvm = mmu->kvm;
Mike Rapoporte9f63762020-06-04 16:46:23 -07001275 p4d_t *p4d;
1276 phys_addr_t next;
1277
1278 p4d = stage2_p4d_offset(kvm, pgd, addr);
1279 do {
1280 next = stage2_p4d_addr_end(kvm, addr, end);
1281 if (!stage2_p4d_none(kvm, *p4d))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001282 stage2_wp_puds(mmu, p4d, addr, next);
Mike Rapoporte9f63762020-06-04 16:46:23 -07001283 } while (p4d++, addr = next, addr != end);
1284}
1285
1286/**
Mario Smarduchc6473552015-01-15 15:58:56 -08001287 * stage2_wp_range() - write protect stage2 memory region range
1288 * @kvm: The KVM pointer
1289 * @addr: Start address of range
1290 * @end: End address of range
1291 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001292static void stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
Mario Smarduchc6473552015-01-15 15:58:56 -08001293{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001294 struct kvm *kvm = mmu->kvm;
Mario Smarduchc6473552015-01-15 15:58:56 -08001295 pgd_t *pgd;
1296 phys_addr_t next;
1297
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001298 pgd = mmu->pgd + stage2_pgd_index(kvm, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001299 do {
1300 /*
1301 * Release kvm_mmu_lock periodically if the memory region is
1302 * large. Otherwise, we may see kernel panics with
Christoffer Dall227ea812015-01-23 10:49:31 +01001303 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1304 * CONFIG_LOCKDEP. Additionally, holding the lock too long
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001305 * will also starve other vCPUs. We have to also make sure
1306 * that the page tables are not freed while we released
1307 * the lock.
Mario Smarduchc6473552015-01-15 15:58:56 -08001308 */
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001309 cond_resched_lock(&kvm->mmu_lock);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001310 if (!READ_ONCE(mmu->pgd))
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001311 break;
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001312 next = stage2_pgd_addr_end(kvm, addr, end);
1313 if (stage2_pgd_present(kvm, *pgd))
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001314 stage2_wp_p4ds(mmu, pgd, addr, next);
Mario Smarduchc6473552015-01-15 15:58:56 -08001315 } while (pgd++, addr = next, addr != end);
1316}
1317
1318/**
1319 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1320 * @kvm: The KVM pointer
1321 * @slot: The memory slot to write protect
1322 *
1323 * Called to start logging dirty pages after memory region
1324 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
Punit Agrawal4ea5af52018-12-11 17:10:37 +00001325 * all present PUD, PMD and PTEs are write protected in the memory region.
Mario Smarduchc6473552015-01-15 15:58:56 -08001326 * Afterwards read of dirty page log can be called.
1327 *
1328 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1329 * serializing operations for VM memory regions.
1330 */
1331void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1332{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001333 struct kvm_memslots *slots = kvm_memslots(kvm);
1334 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001335 phys_addr_t start, end;
1336
1337 if (WARN_ON_ONCE(!memslot))
1338 return;
1339
1340 start = memslot->base_gfn << PAGE_SHIFT;
1341 end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
Mario Smarduchc6473552015-01-15 15:58:56 -08001342
1343 spin_lock(&kvm->mmu_lock);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001344 stage2_wp_range(&kvm->arch.mmu, start, end);
Mario Smarduchc6473552015-01-15 15:58:56 -08001345 spin_unlock(&kvm->mmu_lock);
1346 kvm_flush_remote_tlbs(kvm);
1347}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001348
1349/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001350 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
Mario Smarduch53c810c2015-01-15 15:58:57 -08001351 * @kvm: The KVM pointer
1352 * @slot: The memory slot associated with mask
1353 * @gfn_offset: The gfn offset in memory slot
1354 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1355 * slot to be write protected
1356 *
1357 * Walks bits set in mask write protects the associated pte's. Caller must
1358 * acquire kvm_mmu_lock.
1359 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001360static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Mario Smarduch53c810c2015-01-15 15:58:57 -08001361 struct kvm_memory_slot *slot,
1362 gfn_t gfn_offset, unsigned long mask)
1363{
1364 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1365 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1366 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1367
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001368 stage2_wp_range(&kvm->arch.mmu, start, end);
Mario Smarduch53c810c2015-01-15 15:58:57 -08001369}
Mario Smarduchc6473552015-01-15 15:58:56 -08001370
Kai Huang3b0f1d02015-01-28 10:54:23 +08001371/*
1372 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1373 * dirty pages.
1374 *
1375 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1376 * enable dirty logging for them.
1377 */
1378void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1379 struct kvm_memory_slot *slot,
1380 gfn_t gfn_offset, unsigned long mask)
1381{
1382 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1383}
1384
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001385static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001386{
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001387 __clean_dcache_guest_page(pfn, size);
Marc Zyngiera15f6932017-10-23 17:11:15 +01001388}
1389
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001390static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
Marc Zyngiera15f6932017-10-23 17:11:15 +01001391{
Marc Zyngier17ab9d52017-10-23 17:11:22 +01001392 __invalidate_icache_guest_page(pfn, size);
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001393}
1394
James Morse1559b752019-12-17 12:38:09 +00001395static void kvm_send_hwpoison_signal(unsigned long address, short lsb)
James Morse196f8782017-06-20 17:11:48 +01001396{
Eric W. Biederman795a8372018-04-16 13:39:10 -05001397 send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, current);
James Morse196f8782017-06-20 17:11:48 +01001398}
1399
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001400static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,
1401 unsigned long hva,
1402 unsigned long map_size)
Christoffer Dall6794ad52018-11-02 08:53:22 +01001403{
Shaokun Zhangc2be79a2019-02-19 17:22:21 +08001404 gpa_t gpa_start;
Christoffer Dall6794ad52018-11-02 08:53:22 +01001405 hva_t uaddr_start, uaddr_end;
1406 size_t size;
1407
Suzuki K Poulose9f283612020-05-07 20:35:45 +08001408 /* The memslot and the VMA are guaranteed to be aligned to PAGE_SIZE */
1409 if (map_size == PAGE_SIZE)
1410 return true;
1411
Christoffer Dall6794ad52018-11-02 08:53:22 +01001412 size = memslot->npages * PAGE_SIZE;
1413
1414 gpa_start = memslot->base_gfn << PAGE_SHIFT;
Christoffer Dall6794ad52018-11-02 08:53:22 +01001415
1416 uaddr_start = memslot->userspace_addr;
1417 uaddr_end = uaddr_start + size;
1418
1419 /*
1420 * Pages belonging to memslots that don't have the same alignment
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001421 * within a PMD/PUD for userspace and IPA cannot be mapped with stage-2
1422 * PMD/PUD entries, because we'll end up mapping the wrong pages.
Christoffer Dall6794ad52018-11-02 08:53:22 +01001423 *
1424 * Consider a layout like the following:
1425 *
1426 * memslot->userspace_addr:
1427 * +-----+--------------------+--------------------+---+
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001428 * |abcde|fgh Stage-1 block | Stage-1 block tv|xyz|
Christoffer Dall6794ad52018-11-02 08:53:22 +01001429 * +-----+--------------------+--------------------+---+
1430 *
Suzuki K Poulose9f283612020-05-07 20:35:45 +08001431 * memslot->base_gfn << PAGE_SHIFT:
Christoffer Dall6794ad52018-11-02 08:53:22 +01001432 * +---+--------------------+--------------------+-----+
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001433 * |abc|def Stage-2 block | Stage-2 block |tvxyz|
Christoffer Dall6794ad52018-11-02 08:53:22 +01001434 * +---+--------------------+--------------------+-----+
1435 *
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001436 * If we create those stage-2 blocks, we'll end up with this incorrect
Christoffer Dall6794ad52018-11-02 08:53:22 +01001437 * mapping:
1438 * d -> f
1439 * e -> g
1440 * f -> h
1441 */
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001442 if ((gpa_start & (map_size - 1)) != (uaddr_start & (map_size - 1)))
Christoffer Dall6794ad52018-11-02 08:53:22 +01001443 return false;
1444
1445 /*
1446 * Next, let's make sure we're not trying to map anything not covered
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001447 * by the memslot. This means we have to prohibit block size mappings
1448 * for the beginning and end of a non-block aligned and non-block sized
Christoffer Dall6794ad52018-11-02 08:53:22 +01001449 * memory slot (illustrated by the head and tail parts of the
1450 * userspace view above containing pages 'abcde' and 'xyz',
1451 * respectively).
1452 *
1453 * Note that it doesn't matter if we do the check using the
1454 * userspace_addr or the base_gfn, as both are equally aligned (per
1455 * the check above) and equally sized.
1456 */
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001457 return (hva & ~(map_size - 1)) >= uaddr_start &&
1458 (hva & ~(map_size - 1)) + map_size <= uaddr_end;
Christoffer Dall6794ad52018-11-02 08:53:22 +01001459}
1460
Suzuki K Poulose0529c902020-05-07 20:35:46 +08001461/*
1462 * Check if the given hva is backed by a transparent huge page (THP) and
1463 * whether it can be mapped using block mapping in stage2. If so, adjust
1464 * the stage2 PFN and IPA accordingly. Only PMD_SIZE THPs are currently
1465 * supported. This will need to be updated to support other THP sizes.
1466 *
1467 * Returns the size of the mapping.
1468 */
1469static unsigned long
1470transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
1471 unsigned long hva, kvm_pfn_t *pfnp,
1472 phys_addr_t *ipap)
1473{
1474 kvm_pfn_t pfn = *pfnp;
1475
1476 /*
1477 * Make sure the adjustment is done only for THP pages. Also make
1478 * sure that the HVA and IPA are sufficiently aligned and that the
1479 * block map is contained within the memslot.
1480 */
1481 if (kvm_is_transparent_hugepage(pfn) &&
1482 fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE)) {
1483 /*
1484 * The address we faulted on is backed by a transparent huge
1485 * page. However, because we map the compound huge page and
1486 * not the individual tail page, we need to transfer the
1487 * refcount to the head page. We have to be careful that the
1488 * THP doesn't start to split while we are adjusting the
1489 * refcounts.
1490 *
1491 * We are sure this doesn't happen, because mmu_notifier_retry
1492 * was successful and we are holding the mmu_lock, so if this
1493 * THP is trying to split, it will be blocked in the mmu
1494 * notifier before touching any of the pages, specifically
1495 * before being able to call __split_huge_page_refcount().
1496 *
1497 * We can therefore safely transfer the refcount from PG_tail
1498 * to PG_head and switch the pfn from a tail page to the head
1499 * page accordingly.
1500 */
1501 *ipap &= PMD_MASK;
1502 kvm_release_pfn_clean(pfn);
1503 pfn &= ~(PTRS_PER_PMD - 1);
1504 kvm_get_pfn(pfn);
1505 *pfnp = pfn;
1506
1507 return PMD_SIZE;
1508 }
1509
1510 /* Use page mapping if we cannot use block mapping. */
1511 return PAGE_SIZE;
1512}
1513
Christoffer Dall94f8e642013-01-20 18:28:12 -05001514static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001515 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001516 unsigned long fault_status)
1517{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001518 int ret;
Punit Agrawal6396b852018-12-11 17:10:35 +00001519 bool write_fault, writable, force_pte = false;
1520 bool exec_fault, needs_exec;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001521 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001522 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001523 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001524 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001525 struct vm_area_struct *vma;
James Morse1559b752019-12-17 12:38:09 +00001526 short vma_shift;
Dan Williamsba049e92016-01-15 16:56:11 -08001527 kvm_pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001528 pgprot_t mem_type = PAGE_S2;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001529 bool logging_active = memslot_is_logging(memslot);
Punit Agrawal3f58bf62018-12-11 17:10:34 +00001530 unsigned long vma_pagesize, flags = 0;
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001531 struct kvm_s2_mmu *mmu = vcpu->arch.hw_mmu;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001532
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001533 write_fault = kvm_is_write_fault(vcpu);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001534 exec_fault = kvm_vcpu_trap_is_iabt(vcpu);
1535 VM_BUG_ON(write_fault && exec_fault);
1536
1537 if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001538 kvm_err("Unexpected L2 read permission error\n");
1539 return -EFAULT;
1540 }
1541
Christoffer Dallad361f02012-11-01 17:14:45 +01001542 /* Let's check if we will get back a huge page backed by hugetlbfs */
Michel Lespinasse89154dd2020-06-08 21:33:29 -07001543 mmap_read_lock(current->mm);
Christoffer Dallad361f02012-11-01 17:14:45 +01001544 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001545 if (unlikely(!vma)) {
1546 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
Michel Lespinasse89154dd2020-06-08 21:33:29 -07001547 mmap_read_unlock(current->mm);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001548 return -EFAULT;
1549 }
1550
James Morse1559b752019-12-17 12:38:09 +00001551 if (is_vm_hugetlb_page(vma))
1552 vma_shift = huge_page_shift(hstate_vma(vma));
1553 else
1554 vma_shift = PAGE_SHIFT;
1555
1556 vma_pagesize = 1ULL << vma_shift;
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001557 if (logging_active ||
Marc Zyngier6d674e22019-12-11 16:56:48 +00001558 (vma->vm_flags & VM_PFNMAP) ||
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001559 !fault_supports_stage2_huge_mapping(memslot, hva, vma_pagesize)) {
1560 force_pte = true;
1561 vma_pagesize = PAGE_SIZE;
1562 }
1563
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001564 /*
Suzuki K Poulose280cebf2019-01-29 19:12:17 +00001565 * The stage2 has a minimum of 2 level table (For arm64 see
1566 * kvm_arm_setup_stage2()). Hence, we are guaranteed that we can
1567 * use PMD_SIZE huge mappings (even when the PMD is folded into PGD).
1568 * As for PUD huge maps, we must make sure that we have at least
1569 * 3 levels, i.e, PMD is not folded.
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001570 */
Suzuki K Poulosea80868f2019-03-12 09:52:51 +00001571 if (vma_pagesize == PMD_SIZE ||
1572 (vma_pagesize == PUD_SIZE && kvm_stage2_has_pmd(kvm)))
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001573 gfn = (fault_ipa & huge_page_mask(hstate_vma(vma))) >> PAGE_SHIFT;
Michel Lespinasse89154dd2020-06-08 21:33:29 -07001574 mmap_read_unlock(current->mm);
Christoffer Dallad361f02012-11-01 17:14:45 +01001575
Christoffer Dall94f8e642013-01-20 18:28:12 -05001576 /* We need minimum second+third level pages */
Sean Christophersonc1a33ae2020-07-02 19:35:42 -07001577 ret = kvm_mmu_topup_memory_cache(memcache, kvm_mmu_cache_min_pages(kvm));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001578 if (ret)
1579 return ret;
1580
1581 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1582 /*
1583 * Ensure the read of mmu_notifier_seq happens before we call
1584 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1585 * the page we just got a reference to gets unmapped before we have a
1586 * chance to grab the mmu_lock, which ensure that if the page gets
1587 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1588 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1589 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1590 */
1591 smp_rmb();
1592
Christoffer Dallad361f02012-11-01 17:14:45 +01001593 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
James Morse196f8782017-06-20 17:11:48 +01001594 if (pfn == KVM_PFN_ERR_HWPOISON) {
James Morse1559b752019-12-17 12:38:09 +00001595 kvm_send_hwpoison_signal(hva, vma_shift);
James Morse196f8782017-06-20 17:11:48 +01001596 return 0;
1597 }
Christoffer Dall9ac71592016-08-17 10:46:10 +02001598 if (is_error_noslot_pfn(pfn))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001599 return -EFAULT;
1600
Mario Smarduch15a49a42015-01-15 15:58:58 -08001601 if (kvm_is_device_pfn(pfn)) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001602 mem_type = PAGE_S2_DEVICE;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001603 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1604 } else if (logging_active) {
1605 /*
1606 * Faults on pages in a memslot with logging enabled
1607 * should not be mapped with huge pages (it introduces churn
1608 * and performance degradation), so force a pte mapping.
1609 */
Mario Smarduch15a49a42015-01-15 15:58:58 -08001610 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1611
1612 /*
1613 * Only actually map the page as writable if this was a write
1614 * fault.
1615 */
1616 if (!write_fault)
1617 writable = false;
1618 }
Kim Phillipsb8865762014-06-26 01:45:51 +01001619
Marc Zyngier6d674e22019-12-11 16:56:48 +00001620 if (exec_fault && is_iomap(flags))
1621 return -ENOEXEC;
1622
Christoffer Dallad361f02012-11-01 17:14:45 +01001623 spin_lock(&kvm->mmu_lock);
1624 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001625 goto out_unlock;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001626
Suzuki K Poulose0529c902020-05-07 20:35:46 +08001627 /*
1628 * If we are not forced to use page mapping, check if we are
1629 * backed by a THP and thus use block mapping if possible.
1630 */
1631 if (vma_pagesize == PAGE_SIZE && !force_pte)
1632 vma_pagesize = transparent_hugepage_adjust(memslot, hva,
1633 &pfn, &fault_ipa);
Punit Agrawal3f58bf62018-12-11 17:10:34 +00001634 if (writable)
1635 kvm_set_pfn_dirty(pfn);
1636
Marc Zyngier6d674e22019-12-11 16:56:48 +00001637 if (fault_status != FSC_PERM && !is_iomap(flags))
Punit Agrawal3f58bf62018-12-11 17:10:34 +00001638 clean_dcache_guest_page(pfn, vma_pagesize);
1639
1640 if (exec_fault)
1641 invalidate_icache_guest_page(pfn, vma_pagesize);
1642
Punit Agrawal6396b852018-12-11 17:10:35 +00001643 /*
1644 * If we took an execution fault we have made the
1645 * icache/dcache coherent above and should now let the s2
1646 * mapping be executable.
1647 *
1648 * Write faults (!exec_fault && FSC_PERM) are orthogonal to
1649 * execute permissions, and we preserve whatever we have.
1650 */
1651 needs_exec = exec_fault ||
Will Deaconb757b472020-07-23 11:17:14 +01001652 (fault_status == FSC_PERM &&
Paolo Bonzini0378dae2020-08-09 12:58:23 -04001653 stage2_is_exec(mmu, fault_ipa, vma_pagesize));
Punit Agrawal6396b852018-12-11 17:10:35 +00001654
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001655 if (vma_pagesize == PUD_SIZE) {
1656 pud_t new_pud = kvm_pfn_pud(pfn, mem_type);
1657
1658 new_pud = kvm_pud_mkhuge(new_pud);
1659 if (writable)
1660 new_pud = kvm_s2pud_mkwrite(new_pud);
1661
1662 if (needs_exec)
1663 new_pud = kvm_s2pud_mkexec(new_pud);
1664
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001665 ret = stage2_set_pud_huge(mmu, memcache, fault_ipa, &new_pud);
Punit Agrawalb8e0ba72018-12-11 17:10:41 +00001666 } else if (vma_pagesize == PMD_SIZE) {
Punit Agrawalf8df7332018-12-11 17:10:36 +00001667 pmd_t new_pmd = kvm_pfn_pmd(pfn, mem_type);
1668
1669 new_pmd = kvm_pmd_mkhuge(new_pmd);
1670
Punit Agrawal3f58bf62018-12-11 17:10:34 +00001671 if (writable)
Catalin Marinas06485052016-04-13 17:57:37 +01001672 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001673
Punit Agrawal6396b852018-12-11 17:10:35 +00001674 if (needs_exec)
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001675 new_pmd = kvm_s2pmd_mkexec(new_pmd);
Marc Zyngiera15f6932017-10-23 17:11:15 +01001676
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001677 ret = stage2_set_pmd_huge(mmu, memcache, fault_ipa, &new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +01001678 } else {
Punit Agrawalf8df7332018-12-11 17:10:36 +00001679 pte_t new_pte = kvm_pfn_pte(pfn, mem_type);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001680
Christoffer Dallad361f02012-11-01 17:14:45 +01001681 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001682 new_pte = kvm_s2pte_mkwrite(new_pte);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001683 mark_page_dirty(kvm, gfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001684 }
Marc Zyngiera9c0e122017-10-23 17:11:20 +01001685
Punit Agrawal6396b852018-12-11 17:10:35 +00001686 if (needs_exec)
Marc Zyngierd0e22b42017-10-23 17:11:19 +01001687 new_pte = kvm_s2pte_mkexec(new_pte);
Marc Zyngiera15f6932017-10-23 17:11:15 +01001688
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001689 ret = stage2_set_pte(mmu, memcache, fault_ipa, &new_pte, flags);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001690 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001691
Christoffer Dall94f8e642013-01-20 18:28:12 -05001692out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001693 spin_unlock(&kvm->mmu_lock);
Marc Zyngier35307b92015-03-12 18:16:51 +00001694 kvm_set_pfn_accessed(pfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001695 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001696 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001697}
1698
Marc Zyngieraeda9132015-03-12 18:16:52 +00001699/*
1700 * Resolve the access fault by making the page young again.
1701 * Note that because the faulting entry is guaranteed not to be
1702 * cached in the TLB, we don't need to invalidate anything.
Catalin Marinas06485052016-04-13 17:57:37 +01001703 * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
1704 * so there is no need for atomic (pte|pmd)_mkyoung operations.
Marc Zyngieraeda9132015-03-12 18:16:52 +00001705 */
1706static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1707{
Punit Agrawaleb3f06242018-12-11 17:10:39 +00001708 pud_t *pud;
Marc Zyngieraeda9132015-03-12 18:16:52 +00001709 pmd_t *pmd;
1710 pte_t *pte;
Dan Williamsba049e92016-01-15 16:56:11 -08001711 kvm_pfn_t pfn;
Marc Zyngieraeda9132015-03-12 18:16:52 +00001712 bool pfn_valid = false;
1713
1714 trace_kvm_access_fault(fault_ipa);
1715
1716 spin_lock(&vcpu->kvm->mmu_lock);
1717
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001718 if (!stage2_get_leaf_entry(vcpu->arch.hw_mmu, fault_ipa, &pud, &pmd, &pte))
Marc Zyngieraeda9132015-03-12 18:16:52 +00001719 goto out;
1720
Punit Agrawaleb3f06242018-12-11 17:10:39 +00001721 if (pud) { /* HugeTLB */
1722 *pud = kvm_s2pud_mkyoung(*pud);
1723 pfn = kvm_pud_pfn(*pud);
1724 pfn_valid = true;
1725 } else if (pmd) { /* THP, HugeTLB */
Marc Zyngieraeda9132015-03-12 18:16:52 +00001726 *pmd = pmd_mkyoung(*pmd);
1727 pfn = pmd_pfn(*pmd);
1728 pfn_valid = true;
Punit Agrawaleb3f06242018-12-11 17:10:39 +00001729 } else {
1730 *pte = pte_mkyoung(*pte); /* Just a page... */
1731 pfn = pte_pfn(*pte);
1732 pfn_valid = true;
Marc Zyngieraeda9132015-03-12 18:16:52 +00001733 }
1734
Marc Zyngieraeda9132015-03-12 18:16:52 +00001735out:
1736 spin_unlock(&vcpu->kvm->mmu_lock);
1737 if (pfn_valid)
1738 kvm_set_pfn_accessed(pfn);
1739}
1740
Christoffer Dall94f8e642013-01-20 18:28:12 -05001741/**
1742 * kvm_handle_guest_abort - handles all 2nd stage aborts
1743 * @vcpu: the VCPU pointer
Christoffer Dall94f8e642013-01-20 18:28:12 -05001744 *
1745 * Any abort that gets to the host is almost guaranteed to be caused by a
1746 * missing second stage translation table entry, which can mean that either the
1747 * guest simply needs more memory and we must allocate an appropriate page or it
1748 * can mean that the guest tried to access I/O memory, which is emulated by user
1749 * space. The distinction is based on the IPA causing the fault and whether this
1750 * memory region has been registered as standard RAM by user space.
1751 */
Tianjia Zhang74cc7e02020-06-23 21:14:15 +08001752int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001753{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001754 unsigned long fault_status;
1755 phys_addr_t fault_ipa;
1756 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001757 unsigned long hva;
1758 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001759 gfn_t gfn;
1760 int ret, idx;
1761
Tyler Baicar621f48e2017-06-21 12:17:14 -06001762 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
1763
1764 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
James Morsebb428922017-07-18 13:37:41 +01001765 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Tyler Baicar621f48e2017-06-21 12:17:14 -06001766
James Morsebb428922017-07-18 13:37:41 +01001767 /* Synchronous External Abort? */
Will Deaconc9a636f2020-07-29 11:28:18 +01001768 if (kvm_vcpu_abt_issea(vcpu)) {
James Morsebb428922017-07-18 13:37:41 +01001769 /*
1770 * For RAS the host kernel may handle this abort.
1771 * There is no need to pass the error into the guest.
1772 */
Will Deacon84b951a2020-07-29 11:28:19 +01001773 if (kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_esr(vcpu)))
James Morsebb428922017-07-18 13:37:41 +01001774 kvm_inject_vabt(vcpu);
Will Deacon84b951a2020-07-29 11:28:19 +01001775
1776 return 1;
Marc Zyngier40557102016-09-06 14:02:15 +01001777 }
1778
Gavin Shan3a949f42020-06-30 11:57:05 +10001779 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_esr(vcpu),
Marc Zyngier7393b592012-09-17 19:27:09 +01001780 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001781
1782 /* Check the stage-2 fault is trans. fault or write fault */
Marc Zyngier35307b92015-03-12 18:16:51 +00001783 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1784 fault_status != FSC_ACCESS) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001785 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1786 kvm_vcpu_trap_get_class(vcpu),
1787 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
Gavin Shan3a949f42020-06-30 11:57:05 +10001788 (unsigned long)kvm_vcpu_get_esr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001789 return -EFAULT;
1790 }
1791
1792 idx = srcu_read_lock(&vcpu->kvm->srcu);
1793
1794 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001795 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1796 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001797 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001798 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Will Deacon022c8322020-07-29 11:28:21 +01001799 /*
1800 * The guest has put either its instructions or its page-tables
1801 * somewhere it shouldn't have. Userspace won't be able to do
1802 * anything about this (there's no syndrome for a start), so
1803 * re-inject the abort back into the guest.
1804 */
Christoffer Dall94f8e642013-01-20 18:28:12 -05001805 if (is_iabt) {
Marc Zyngier6d674e22019-12-11 16:56:48 +00001806 ret = -ENOEXEC;
1807 goto out;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001808 }
1809
Will Deacon022c8322020-07-29 11:28:21 +01001810 if (kvm_vcpu_dabt_iss1tw(vcpu)) {
1811 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
1812 ret = 1;
1813 goto out_unlock;
1814 }
1815
Marc Zyngiercfe39502012-12-12 14:42:09 +00001816 /*
Marc Zyngier57c841f2016-01-29 15:01:28 +00001817 * Check for a cache maintenance operation. Since we
1818 * ended-up here, we know it is outside of any memory
1819 * slot. But we can't find out if that is for a device,
1820 * or if the guest is just being stupid. The only thing
1821 * we know for sure is that this range cannot be cached.
1822 *
1823 * So let's assume that the guest is just being
1824 * cautious, and skip the instruction.
1825 */
Will Deacon54dc0d22020-07-29 11:28:20 +01001826 if (kvm_is_error_hva(hva) && kvm_vcpu_dabt_is_cm(vcpu)) {
Marc Zyngier57c841f2016-01-29 15:01:28 +00001827 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1828 ret = 1;
1829 goto out_unlock;
1830 }
1831
1832 /*
Marc Zyngiercfe39502012-12-12 14:42:09 +00001833 * The IPA is reported as [MAX:12], so we need to
1834 * complement it with the bottom 12 bits from the
1835 * faulting VA. This is always 12 bits, irrespective
1836 * of the page size.
1837 */
1838 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Tianjia Zhang74cc7e02020-06-23 21:14:15 +08001839 ret = io_mem_abort(vcpu, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001840 goto out_unlock;
1841 }
1842
Christoffer Dallc3058d52014-10-10 12:14:29 +02001843 /* Userspace should not be able to register out-of-bounds IPAs */
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01001844 VM_BUG_ON(fault_ipa >= kvm_phys_size(vcpu->kvm));
Christoffer Dallc3058d52014-10-10 12:14:29 +02001845
Marc Zyngieraeda9132015-03-12 18:16:52 +00001846 if (fault_status == FSC_ACCESS) {
1847 handle_access_fault(vcpu, fault_ipa);
1848 ret = 1;
1849 goto out_unlock;
1850 }
1851
Christoffer Dall98047882014-08-19 12:18:04 +02001852 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001853 if (ret == 0)
1854 ret = 1;
Marc Zyngier6d674e22019-12-11 16:56:48 +00001855out:
1856 if (ret == -ENOEXEC) {
1857 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
1858 ret = 1;
1859 }
Christoffer Dall94f8e642013-01-20 18:28:12 -05001860out_unlock:
1861 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1862 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001863}
1864
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001865static int handle_hva_to_gpa(struct kvm *kvm,
1866 unsigned long start,
1867 unsigned long end,
1868 int (*handler)(struct kvm *kvm,
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001869 gpa_t gpa, u64 size,
1870 void *data),
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001871 void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001872{
1873 struct kvm_memslots *slots;
1874 struct kvm_memory_slot *memslot;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001875 int ret = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001876
1877 slots = kvm_memslots(kvm);
1878
1879 /* we only care about the pages that the guest sees */
1880 kvm_for_each_memslot(memslot, slots) {
1881 unsigned long hva_start, hva_end;
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001882 gfn_t gpa;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001883
1884 hva_start = max(start, memslot->userspace_addr);
1885 hva_end = min(end, memslot->userspace_addr +
1886 (memslot->npages << PAGE_SHIFT));
1887 if (hva_start >= hva_end)
1888 continue;
1889
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001890 gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
1891 ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001892 }
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001893
1894 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001895}
1896
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001897static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001898{
Will Deaconb5331372020-08-11 11:27:25 +01001899 unsigned flags = *(unsigned *)data;
1900 bool may_block = flags & MMU_NOTIFIER_RANGE_BLOCKABLE;
1901
1902 __unmap_stage2_range(&kvm->arch.mmu, gpa, size, may_block);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001903 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001904}
1905
Christoffer Dalld5d81842013-01-20 18:28:07 -05001906int kvm_unmap_hva_range(struct kvm *kvm,
Will Deaconfdfe7cb2020-08-11 11:27:24 +01001907 unsigned long start, unsigned long end, unsigned flags)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001908{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001909 if (!kvm->arch.mmu.pgd)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001910 return 0;
1911
1912 trace_kvm_unmap_hva_range(start, end);
Will Deaconb5331372020-08-11 11:27:25 +01001913 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, &flags);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001914 return 0;
1915}
1916
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001917static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001918{
1919 pte_t *pte = (pte_t *)data;
1920
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001921 WARN_ON(size != PAGE_SIZE);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001922 /*
1923 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1924 * flag clear because MMU notifiers will have unmapped a huge PMD before
1925 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1926 * therefore stage2_set_pte() never needs to clear out a huge PMD
1927 * through this calling path.
1928 */
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001929 stage2_set_pte(&kvm->arch.mmu, NULL, gpa, pte, 0);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001930 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001931}
1932
1933
Lan Tianyu748c0e32018-12-06 21:21:10 +08001934int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001935{
1936 unsigned long end = hva + PAGE_SIZE;
Marc Zyngier694556d2018-08-23 09:58:27 +01001937 kvm_pfn_t pfn = pte_pfn(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001938 pte_t stage2_pte;
1939
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001940 if (!kvm->arch.mmu.pgd)
Lan Tianyu748c0e32018-12-06 21:21:10 +08001941 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001942
1943 trace_kvm_set_spte_hva(hva);
Marc Zyngier694556d2018-08-23 09:58:27 +01001944
1945 /*
1946 * We've moved a page around, probably through CoW, so let's treat it
1947 * just like a translation fault and clean the cache to the PoC.
1948 */
1949 clean_dcache_guest_page(pfn, PAGE_SIZE);
Punit Agrawalf8df7332018-12-11 17:10:36 +00001950 stage2_pte = kvm_pfn_pte(pfn, PAGE_S2);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001951 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
Lan Tianyu748c0e32018-12-06 21:21:10 +08001952
1953 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001954}
1955
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001956static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Marc Zyngier35307b92015-03-12 18:16:51 +00001957{
Punit Agrawal35a63962018-12-11 17:10:40 +00001958 pud_t *pud;
Marc Zyngier35307b92015-03-12 18:16:51 +00001959 pmd_t *pmd;
1960 pte_t *pte;
1961
Punit Agrawal35a63962018-12-11 17:10:40 +00001962 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001963 if (!stage2_get_leaf_entry(&kvm->arch.mmu, gpa, &pud, &pmd, &pte))
Marc Zyngier35307b92015-03-12 18:16:51 +00001964 return 0;
1965
Punit Agrawal35a63962018-12-11 17:10:40 +00001966 if (pud)
1967 return stage2_pudp_test_and_clear_young(pud);
1968 else if (pmd)
Catalin Marinas06485052016-04-13 17:57:37 +01001969 return stage2_pmdp_test_and_clear_young(pmd);
Punit Agrawal35a63962018-12-11 17:10:40 +00001970 else
1971 return stage2_ptep_test_and_clear_young(pte);
Marc Zyngier35307b92015-03-12 18:16:51 +00001972}
1973
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001974static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Marc Zyngier35307b92015-03-12 18:16:51 +00001975{
Punit Agrawal35a63962018-12-11 17:10:40 +00001976 pud_t *pud;
Marc Zyngier35307b92015-03-12 18:16:51 +00001977 pmd_t *pmd;
1978 pte_t *pte;
1979
Punit Agrawal35a63962018-12-11 17:10:40 +00001980 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001981 if (!stage2_get_leaf_entry(&kvm->arch.mmu, gpa, &pud, &pmd, &pte))
Marc Zyngier35307b92015-03-12 18:16:51 +00001982 return 0;
1983
Punit Agrawal35a63962018-12-11 17:10:40 +00001984 if (pud)
1985 return kvm_s2pud_young(*pud);
1986 else if (pmd)
Marc Zyngier35307b92015-03-12 18:16:51 +00001987 return pmd_young(*pmd);
Punit Agrawal35a63962018-12-11 17:10:40 +00001988 else
Marc Zyngier35307b92015-03-12 18:16:51 +00001989 return pte_young(*pte);
Marc Zyngier35307b92015-03-12 18:16:51 +00001990}
1991
1992int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
1993{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01001994 if (!kvm->arch.mmu.pgd)
Suzuki K Poulose7e5a6722017-07-05 09:57:00 +01001995 return 0;
Marc Zyngier35307b92015-03-12 18:16:51 +00001996 trace_kvm_age_hva(start, end);
1997 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
1998}
1999
2000int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
2001{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01002002 if (!kvm->arch.mmu.pgd)
Suzuki K Poulose7e5a6722017-07-05 09:57:00 +01002003 return 0;
Marc Zyngier35307b92015-03-12 18:16:51 +00002004 trace_kvm_test_age_hva(hva);
Gavin Shancf2d23e2020-01-21 16:56:59 +11002005 return handle_hva_to_gpa(kvm, hva, hva + PAGE_SIZE,
2006 kvm_test_age_hva_handler, NULL);
Marc Zyngier35307b92015-03-12 18:16:51 +00002007}
2008
Christoffer Dall342cd0a2013-01-20 18:28:06 -05002009phys_addr_t kvm_mmu_get_httbr(void)
2010{
Will Deacon0f9d09b2020-09-11 14:25:12 +01002011 return __pa(hyp_pgtable->pgd);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05002012}
2013
Marc Zyngier5a677ce2013-04-12 19:12:06 +01002014phys_addr_t kvm_get_idmap_vector(void)
2015{
2016 return hyp_idmap_vector;
2017}
2018
Will Deacon0f9d09b2020-09-11 14:25:12 +01002019static int kvm_map_idmap_text(void)
Marc Zyngier0535a3e2016-06-30 18:40:43 +01002020{
Will Deacon0f9d09b2020-09-11 14:25:12 +01002021 unsigned long size = hyp_idmap_end - hyp_idmap_start;
2022 int err = __create_hyp_mappings(hyp_idmap_start, size, hyp_idmap_start,
2023 PAGE_HYP_EXEC);
Marc Zyngier0535a3e2016-06-30 18:40:43 +01002024 if (err)
2025 kvm_err("Failed to idmap %lx-%lx\n",
2026 hyp_idmap_start, hyp_idmap_end);
2027
2028 return err;
2029}
2030
Christoffer Dall342cd0a2013-01-20 18:28:06 -05002031int kvm_mmu_init(void)
2032{
Marc Zyngier2fb41052013-04-12 19:12:03 +01002033 int err;
Will Deacon0f9d09b2020-09-11 14:25:12 +01002034 u32 hyp_va_bits;
Marc Zyngier2fb41052013-04-12 19:12:03 +01002035
Andrew Scull0a787912020-05-19 11:40:36 +01002036 hyp_idmap_start = __pa_symbol(__hyp_idmap_text_start);
Marc Zyngier46fef152018-03-12 14:25:10 +00002037 hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
Andrew Scull0a787912020-05-19 11:40:36 +01002038 hyp_idmap_end = __pa_symbol(__hyp_idmap_text_end);
Marc Zyngier46fef152018-03-12 14:25:10 +00002039 hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
Andrew Scull0a787912020-05-19 11:40:36 +01002040 hyp_idmap_vector = __pa_symbol(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01002041
Ard Biesheuvel06f75a12015-03-19 16:42:26 +00002042 /*
2043 * We rely on the linker script to ensure at build time that the HYP
2044 * init code does not cross a page boundary.
2045 */
2046 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01002047
Will Deacon0f9d09b2020-09-11 14:25:12 +01002048 hyp_va_bits = 64 - ((idmap_t0sz & TCR_T0SZ_MASK) >> TCR_T0SZ_OFFSET);
2049 kvm_debug("Using %u-bit virtual addresses at EL2\n", hyp_va_bits);
Marc Zyngierb4ef0492017-12-03 20:04:51 +00002050 kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
2051 kvm_debug("HYP VA range: %lx:%lx\n",
2052 kern_hyp_va(PAGE_OFFSET),
2053 kern_hyp_va((unsigned long)high_memory - 1));
Marc Zyngiereac378a2016-06-30 18:40:50 +01002054
Marc Zyngier6c41a412016-06-30 18:40:51 +01002055 if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
Marc Zyngiered57cac2017-12-03 18:22:49 +00002056 hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
Marc Zyngierd2896d42016-08-22 09:01:17 +01002057 hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
Marc Zyngiereac378a2016-06-30 18:40:50 +01002058 /*
2059 * The idmap page is intersecting with the VA space,
2060 * it is not safe to continue further.
2061 */
2062 kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
2063 err = -EINVAL;
2064 goto out;
2065 }
2066
Will Deacon0f9d09b2020-09-11 14:25:12 +01002067 hyp_pgtable = kzalloc(sizeof(*hyp_pgtable), GFP_KERNEL);
2068 if (!hyp_pgtable) {
2069 kvm_err("Hyp mode page-table not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01002070 err = -ENOMEM;
2071 goto out;
2072 }
2073
Will Deacon0f9d09b2020-09-11 14:25:12 +01002074 err = kvm_pgtable_hyp_init(hyp_pgtable, hyp_va_bits);
2075 if (err)
2076 goto out_free_pgtable;
Marc Zyngier0535a3e2016-06-30 18:40:43 +01002077
Will Deacon0f9d09b2020-09-11 14:25:12 +01002078 err = kvm_map_idmap_text();
2079 if (err)
2080 goto out_destroy_pgtable;
Marc Zyngier5a677ce2013-04-12 19:12:06 +01002081
Marc Zyngiere3f019b2017-12-04 17:04:38 +00002082 io_map_base = hyp_idmap_start;
Christoffer Dalld5d81842013-01-20 18:28:07 -05002083 return 0;
Will Deacon0f9d09b2020-09-11 14:25:12 +01002084
2085out_destroy_pgtable:
2086 kvm_pgtable_hyp_destroy(hyp_pgtable);
2087out_free_pgtable:
2088 kfree(hyp_pgtable);
2089 hyp_pgtable = NULL;
Marc Zyngier2fb41052013-04-12 19:12:03 +01002090out:
Marc Zyngier2fb41052013-04-12 19:12:03 +01002091 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05002092}
Eric Augerdf6ce242014-06-06 11:10:23 +02002093
2094void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002095 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -08002096 struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002097 const struct kvm_memory_slot *new,
Eric Augerdf6ce242014-06-06 11:10:23 +02002098 enum kvm_mr_change change)
2099{
Mario Smarduchc6473552015-01-15 15:58:56 -08002100 /*
2101 * At this point memslot has been committed and there is an
Fuad Tabba656012c2020-04-01 15:03:10 +01002102 * allocated dirty_bitmap[], dirty pages will be tracked while the
Mario Smarduchc6473552015-01-15 15:58:56 -08002103 * memory slot is write protected.
2104 */
Keqian Zhuc8626262020-04-13 20:20:23 +08002105 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) {
2106 /*
2107 * If we're with initial-all-set, we don't need to write
2108 * protect any pages because they're all reported as dirty.
2109 * Huge pages and normal pages will be write protect gradually.
2110 */
2111 if (!kvm_dirty_log_manual_protect_and_init_set(kvm)) {
2112 kvm_mmu_wp_memory_region(kvm, mem->slot);
2113 }
2114 }
Eric Augerdf6ce242014-06-06 11:10:23 +02002115}
2116
2117int kvm_arch_prepare_memory_region(struct kvm *kvm,
2118 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002119 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02002120 enum kvm_mr_change change)
2121{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002122 hva_t hva = mem->userspace_addr;
2123 hva_t reg_end = hva + mem->memory_size;
2124 bool writable = !(mem->flags & KVM_MEM_READONLY);
2125 int ret = 0;
2126
Mario Smarduch15a49a42015-01-15 15:58:58 -08002127 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
2128 change != KVM_MR_FLAGS_ONLY)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002129 return 0;
2130
2131 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02002132 * Prevent userspace from creating a memory region outside of the IPA
2133 * space addressable by the KVM guest IPA space.
2134 */
2135 if (memslot->base_gfn + memslot->npages >=
Suzuki K Poulosee55cac52018-09-26 17:32:44 +01002136 (kvm_phys_size(kvm) >> PAGE_SHIFT))
Christoffer Dallc3058d52014-10-10 12:14:29 +02002137 return -EFAULT;
2138
Michel Lespinasse89154dd2020-06-08 21:33:29 -07002139 mmap_read_lock(current->mm);
Christoffer Dallc3058d52014-10-10 12:14:29 +02002140 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002141 * A memory region could potentially cover multiple VMAs, and any holes
2142 * between them, so iterate over all of them to find out if we can map
2143 * any of them right now.
2144 *
2145 * +--------------------------------------------+
2146 * +---------------+----------------+ +----------------+
2147 * | : VMA 1 | VMA 2 | | VMA 3 : |
2148 * +---------------+----------------+ +----------------+
2149 * | memory region |
2150 * +--------------------------------------------+
2151 */
2152 do {
2153 struct vm_area_struct *vma = find_vma(current->mm, hva);
2154 hva_t vm_start, vm_end;
2155
2156 if (!vma || vma->vm_start >= reg_end)
2157 break;
2158
2159 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002160 * Take the intersection of this VMA with the memory region
2161 */
2162 vm_start = max(hva, vma->vm_start);
2163 vm_end = min(reg_end, vma->vm_end);
2164
2165 if (vma->vm_flags & VM_PFNMAP) {
2166 gpa_t gpa = mem->guest_phys_addr +
2167 (vm_start - mem->userspace_addr);
Marek Majtykaca09f022015-09-16 12:04:55 +02002168 phys_addr_t pa;
2169
2170 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
2171 pa += vm_start - vma->vm_start;
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002172
Mario Smarduch15a49a42015-01-15 15:58:58 -08002173 /* IO region dirty page logging not allowed */
Marc Zyngier72f31042017-03-16 18:20:50 +00002174 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
2175 ret = -EINVAL;
2176 goto out;
2177 }
Mario Smarduch15a49a42015-01-15 15:58:58 -08002178
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002179 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
2180 vm_end - vm_start,
2181 writable);
2182 if (ret)
2183 break;
2184 }
2185 hva = vm_end;
2186 } while (hva < reg_end);
2187
Mario Smarduch15a49a42015-01-15 15:58:58 -08002188 if (change == KVM_MR_FLAGS_ONLY)
Marc Zyngier72f31042017-03-16 18:20:50 +00002189 goto out;
Mario Smarduch15a49a42015-01-15 15:58:58 -08002190
Ard Biesheuvel849260c2014-11-17 14:58:53 +00002191 spin_lock(&kvm->mmu_lock);
2192 if (ret)
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01002193 unmap_stage2_range(&kvm->arch.mmu, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00002194 else
2195 stage2_flush_memslot(kvm, memslot);
2196 spin_unlock(&kvm->mmu_lock);
Marc Zyngier72f31042017-03-16 18:20:50 +00002197out:
Michel Lespinasse89154dd2020-06-08 21:33:29 -07002198 mmap_read_unlock(current->mm);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002199 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02002200}
2201
Sean Christophersone96c81e2020-02-18 13:07:27 -08002202void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Eric Augerdf6ce242014-06-06 11:10:23 +02002203{
2204}
2205
Sean Christopherson15248252019-02-05 12:54:17 -08002206void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
Eric Augerdf6ce242014-06-06 11:10:23 +02002207{
2208}
2209
2210void kvm_arch_flush_shadow_all(struct kvm *kvm)
2211{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01002212 kvm_free_stage2_pgd(&kvm->arch.mmu);
Eric Augerdf6ce242014-06-06 11:10:23 +02002213}
2214
2215void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
2216 struct kvm_memory_slot *slot)
2217{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002218 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
2219 phys_addr_t size = slot->npages << PAGE_SHIFT;
2220
2221 spin_lock(&kvm->mmu_lock);
Christoffer Dalla0e50aa2019-01-04 21:09:05 +01002222 unmap_stage2_range(&kvm->arch.mmu, gpa, size);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02002223 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02002224}
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002225
2226/*
2227 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
2228 *
2229 * Main problems:
2230 * - S/W ops are local to a CPU (not broadcast)
2231 * - We have line migration behind our back (speculation)
2232 * - System caches don't support S/W at all (damn!)
2233 *
2234 * In the face of the above, the best we can do is to try and convert
2235 * S/W ops to VA ops. Because the guest is not allowed to infer the
2236 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
2237 * which is a rather good thing for us.
2238 *
2239 * Also, it is only used when turning caches on/off ("The expected
2240 * usage of the cache maintenance instructions that operate by set/way
2241 * is associated with the cache maintenance instructions associated
2242 * with the powerdown and powerup of caches, if this is required by
2243 * the implementation.").
2244 *
2245 * We use the following policy:
2246 *
2247 * - If we trap a S/W operation, we enable VM trapping to detect
2248 * caches being turned on/off, and do a full clean.
2249 *
2250 * - We flush the caches on both caches being turned on and off.
2251 *
2252 * - Once the caches are enabled, we stop trapping VM ops.
2253 */
2254void kvm_set_way_flush(struct kvm_vcpu *vcpu)
2255{
Christoffer Dall3df59d82017-08-03 12:09:05 +02002256 unsigned long hcr = *vcpu_hcr(vcpu);
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002257
2258 /*
2259 * If this is the first time we do a S/W operation
2260 * (i.e. HCR_TVM not set) flush the whole memory, and set the
2261 * VM trapping.
2262 *
2263 * Otherwise, rely on the VM trapping to wait for the MMU +
2264 * Caches to be turned off. At that point, we'll be able to
2265 * clean the caches again.
2266 */
2267 if (!(hcr & HCR_TVM)) {
2268 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
2269 vcpu_has_cache_enabled(vcpu));
2270 stage2_flush_vm(vcpu->kvm);
Christoffer Dall3df59d82017-08-03 12:09:05 +02002271 *vcpu_hcr(vcpu) = hcr | HCR_TVM;
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002272 }
2273}
2274
2275void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
2276{
2277 bool now_enabled = vcpu_has_cache_enabled(vcpu);
2278
2279 /*
2280 * If switching the MMU+caches on, need to invalidate the caches.
2281 * If switching it off, need to clean the caches.
2282 * Clean + invalidate does the trick always.
2283 */
2284 if (now_enabled != was_enabled)
2285 stage2_flush_vm(vcpu->kvm);
2286
2287 /* Caches are now on, stop trapping VM ops (until a S/W op) */
2288 if (now_enabled)
Christoffer Dall3df59d82017-08-03 12:09:05 +02002289 *vcpu_hcr(vcpu) &= ~HCR_TVM;
Marc Zyngier3c1e7162014-12-19 16:05:31 +00002290
2291 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
2292}