blob: bde0cdd60997038b45d88bf004b858d36e26de89 [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -050018
19#include <linux/mman.h>
20#include <linux/kvm_host.h>
21#include <linux/io.h>
Christoffer Dallad361f02012-11-01 17:14:45 +010022#include <linux/hugetlb.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050023#include <trace/events/kvm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050024#include <asm/pgalloc.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050025#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050026#include <asm/kvm_arm.h>
27#include <asm/kvm_mmu.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050028#include <asm/kvm_mmio.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050029#include <asm/kvm_asm.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050030#include <asm/kvm_emulate.h>
Marc Zyngier1e947ba2015-01-29 11:59:54 +000031#include <asm/virt.h>
Tyler Baicar621f48e2017-06-21 12:17:14 -060032#include <asm/system_misc.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050033
34#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050035
Marc Zyngier5a677ce2013-04-12 19:12:06 +010036static pgd_t *boot_hyp_pgd;
Marc Zyngier2fb41052013-04-12 19:12:03 +010037static pgd_t *hyp_pgd;
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000038static pgd_t *merged_hyp_pgd;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050039static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
40
Marc Zyngier5a677ce2013-04-12 19:12:06 +010041static unsigned long hyp_idmap_start;
42static unsigned long hyp_idmap_end;
43static phys_addr_t hyp_idmap_vector;
44
Suzuki K Poulose9163ee232016-03-22 17:01:21 +000045#define S2_PGD_SIZE (PTRS_PER_S2_PGD * sizeof(pgd_t))
Christoffer Dall38f791a2014-10-10 12:14:28 +020046#define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
Mark Salter5d4e08c2014-03-28 14:25:19 +000047
Mario Smarduch15a49a42015-01-15 15:58:58 -080048#define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
49#define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
50
51static bool memslot_is_logging(struct kvm_memory_slot *memslot)
52{
Mario Smarduch15a49a42015-01-15 15:58:58 -080053 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
Mario Smarduch72760302015-01-15 15:59:01 -080054}
55
56/**
57 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
58 * @kvm: pointer to kvm structure.
59 *
60 * Interface to HYP function to flush all VM TLB entries
61 */
62void kvm_flush_remote_tlbs(struct kvm *kvm)
63{
64 kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
Mario Smarduch15a49a42015-01-15 15:58:58 -080065}
Christoffer Dallad361f02012-11-01 17:14:45 +010066
Marc Zyngier48762762013-01-28 15:27:00 +000067static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050068{
Suzuki K Poulose8684e702016-03-22 17:14:25 +000069 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050070}
71
Marc Zyngier363ef892014-12-19 16:48:06 +000072/*
73 * D-Cache management functions. They take the page table entries by
74 * value, as they are flushing the cache using the kernel mapping (or
75 * kmap on 32bit).
76 */
77static void kvm_flush_dcache_pte(pte_t pte)
78{
79 __kvm_flush_dcache_pte(pte);
80}
81
82static void kvm_flush_dcache_pmd(pmd_t pmd)
83{
84 __kvm_flush_dcache_pmd(pmd);
85}
86
87static void kvm_flush_dcache_pud(pud_t pud)
88{
89 __kvm_flush_dcache_pud(pud);
90}
91
Ard Biesheuvele6fab542015-11-10 15:11:20 +010092static bool kvm_is_device_pfn(unsigned long pfn)
93{
94 return !pfn_valid(pfn);
95}
96
Mario Smarduch15a49a42015-01-15 15:58:58 -080097/**
98 * stage2_dissolve_pmd() - clear and flush huge PMD entry
99 * @kvm: pointer to kvm structure.
100 * @addr: IPA
101 * @pmd: pmd pointer for IPA
102 *
103 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
104 * pages in the range dirty.
105 */
106static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
107{
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000108 if (!pmd_thp_or_huge(*pmd))
Mario Smarduch15a49a42015-01-15 15:58:58 -0800109 return;
110
111 pmd_clear(pmd);
112 kvm_tlb_flush_vmid_ipa(kvm, addr);
113 put_page(virt_to_page(pmd));
114}
115
Christoffer Dalld5d81842013-01-20 18:28:07 -0500116static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
117 int min, int max)
118{
119 void *page;
120
121 BUG_ON(max > KVM_NR_MEM_OBJS);
122 if (cache->nobjs >= min)
123 return 0;
124 while (cache->nobjs < max) {
125 page = (void *)__get_free_page(PGALLOC_GFP);
126 if (!page)
127 return -ENOMEM;
128 cache->objects[cache->nobjs++] = page;
129 }
130 return 0;
131}
132
133static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
134{
135 while (mc->nobjs)
136 free_page((unsigned long)mc->objects[--mc->nobjs]);
137}
138
139static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
140{
141 void *p;
142
143 BUG_ON(!mc || !mc->nobjs);
144 p = mc->objects[--mc->nobjs];
145 return p;
146}
147
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000148static void clear_stage2_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100149{
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000150 pud_t *pud_table __maybe_unused = stage2_pud_offset(pgd, 0UL);
151 stage2_pgd_clear(pgd);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200152 kvm_tlb_flush_vmid_ipa(kvm, addr);
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000153 stage2_pud_free(pud_table);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200154 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100155}
156
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000157static void clear_stage2_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500158{
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000159 pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(pud, 0);
160 VM_BUG_ON(stage2_pud_huge(*pud));
161 stage2_pud_clear(pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200162 kvm_tlb_flush_vmid_ipa(kvm, addr);
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000163 stage2_pmd_free(pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100164 put_page(virt_to_page(pud));
165}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500166
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000167static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100168{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200169 pte_t *pte_table = pte_offset_kernel(pmd, 0);
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000170 VM_BUG_ON(pmd_thp_or_huge(*pmd));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200171 pmd_clear(pmd);
172 kvm_tlb_flush_vmid_ipa(kvm, addr);
173 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100174 put_page(virt_to_page(pmd));
175}
176
Marc Zyngier363ef892014-12-19 16:48:06 +0000177/*
178 * Unmapping vs dcache management:
179 *
180 * If a guest maps certain memory pages as uncached, all writes will
181 * bypass the data cache and go directly to RAM. However, the CPUs
182 * can still speculate reads (not writes) and fill cache lines with
183 * data.
184 *
185 * Those cache lines will be *clean* cache lines though, so a
186 * clean+invalidate operation is equivalent to an invalidate
187 * operation, because no cache lines are marked dirty.
188 *
189 * Those clean cache lines could be filled prior to an uncached write
190 * by the guest, and the cache coherent IO subsystem would therefore
191 * end up writing old data to disk.
192 *
193 * This is why right after unmapping a page/section and invalidating
194 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
195 * the IO subsystem will never hit in the cache.
196 */
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000197static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200198 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100199{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200200 phys_addr_t start_addr = addr;
201 pte_t *pte, *start_pte;
202
203 start_pte = pte = pte_offset_kernel(pmd, addr);
204 do {
205 if (!pte_none(*pte)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000206 pte_t old_pte = *pte;
207
Christoffer Dall4f853a72014-05-09 23:31:31 +0200208 kvm_set_pte(pte, __pte(0));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200209 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000210
211 /* No need to invalidate the cache for device mappings */
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100212 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000213 kvm_flush_dcache_pte(old_pte);
214
215 put_page(virt_to_page(pte));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200216 }
217 } while (pte++, addr += PAGE_SIZE, addr != end);
218
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000219 if (stage2_pte_table_empty(start_pte))
220 clear_stage2_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500221}
222
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000223static void unmap_stage2_pmds(struct kvm *kvm, pud_t *pud,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200224 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500225{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200226 phys_addr_t next, start_addr = addr;
227 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000228
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000229 start_pmd = pmd = stage2_pmd_offset(pud, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200230 do {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000231 next = stage2_pmd_addr_end(addr, end);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200232 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000233 if (pmd_thp_or_huge(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000234 pmd_t old_pmd = *pmd;
235
Christoffer Dall4f853a72014-05-09 23:31:31 +0200236 pmd_clear(pmd);
237 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000238
239 kvm_flush_dcache_pmd(old_pmd);
240
Christoffer Dall4f853a72014-05-09 23:31:31 +0200241 put_page(virt_to_page(pmd));
242 } else {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000243 unmap_stage2_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100244 }
245 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200246 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100247
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000248 if (stage2_pmd_table_empty(start_pmd))
249 clear_stage2_pud_entry(kvm, pud, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200250}
251
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000252static void unmap_stage2_puds(struct kvm *kvm, pgd_t *pgd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200253 phys_addr_t addr, phys_addr_t end)
254{
255 phys_addr_t next, start_addr = addr;
256 pud_t *pud, *start_pud;
257
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000258 start_pud = pud = stage2_pud_offset(pgd, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200259 do {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000260 next = stage2_pud_addr_end(addr, end);
261 if (!stage2_pud_none(*pud)) {
262 if (stage2_pud_huge(*pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000263 pud_t old_pud = *pud;
264
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000265 stage2_pud_clear(pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200266 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000267 kvm_flush_dcache_pud(old_pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200268 put_page(virt_to_page(pud));
269 } else {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000270 unmap_stage2_pmds(kvm, pud, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200271 }
272 }
273 } while (pud++, addr = next, addr != end);
274
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000275 if (stage2_pud_table_empty(start_pud))
276 clear_stage2_pgd_entry(kvm, pgd, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200277}
278
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000279/**
280 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
281 * @kvm: The VM pointer
282 * @start: The intermediate physical base address of the range to unmap
283 * @size: The size of the area to unmap
284 *
285 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
286 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
287 * destroying the VM), otherwise another faulting VCPU may come in and mess
288 * with things behind our backs.
289 */
290static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
Christoffer Dall4f853a72014-05-09 23:31:31 +0200291{
292 pgd_t *pgd;
293 phys_addr_t addr = start, end = start + size;
294 phys_addr_t next;
295
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100296 assert_spin_locked(&kvm->mmu_lock);
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000297 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200298 do {
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +0100299 /*
300 * Make sure the page table is still active, as another thread
301 * could have possibly freed the page table, while we released
302 * the lock.
303 */
304 if (!READ_ONCE(kvm->arch.pgd))
305 break;
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000306 next = stage2_pgd_addr_end(addr, end);
307 if (!stage2_pgd_none(*pgd))
308 unmap_stage2_puds(kvm, pgd, addr, next);
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100309 /*
310 * If the range is too large, release the kvm->mmu_lock
311 * to prevent starvation and lockup detector warnings.
312 */
313 if (next != end)
314 cond_resched_lock(&kvm->mmu_lock);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200315 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000316}
317
Marc Zyngier9d218a12014-01-15 12:50:23 +0000318static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
319 phys_addr_t addr, phys_addr_t end)
320{
321 pte_t *pte;
322
323 pte = pte_offset_kernel(pmd, addr);
324 do {
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100325 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000326 kvm_flush_dcache_pte(*pte);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000327 } while (pte++, addr += PAGE_SIZE, addr != end);
328}
329
330static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
331 phys_addr_t addr, phys_addr_t end)
332{
333 pmd_t *pmd;
334 phys_addr_t next;
335
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000336 pmd = stage2_pmd_offset(pud, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000337 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000338 next = stage2_pmd_addr_end(addr, end);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000339 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000340 if (pmd_thp_or_huge(*pmd))
Marc Zyngier363ef892014-12-19 16:48:06 +0000341 kvm_flush_dcache_pmd(*pmd);
342 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000343 stage2_flush_ptes(kvm, pmd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000344 }
345 } while (pmd++, addr = next, addr != end);
346}
347
348static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
349 phys_addr_t addr, phys_addr_t end)
350{
351 pud_t *pud;
352 phys_addr_t next;
353
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000354 pud = stage2_pud_offset(pgd, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000355 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000356 next = stage2_pud_addr_end(addr, end);
357 if (!stage2_pud_none(*pud)) {
358 if (stage2_pud_huge(*pud))
Marc Zyngier363ef892014-12-19 16:48:06 +0000359 kvm_flush_dcache_pud(*pud);
360 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000361 stage2_flush_pmds(kvm, pud, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000362 }
363 } while (pud++, addr = next, addr != end);
364}
365
366static void stage2_flush_memslot(struct kvm *kvm,
367 struct kvm_memory_slot *memslot)
368{
369 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
370 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
371 phys_addr_t next;
372 pgd_t *pgd;
373
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000374 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000375 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000376 next = stage2_pgd_addr_end(addr, end);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000377 stage2_flush_puds(kvm, pgd, addr, next);
378 } while (pgd++, addr = next, addr != end);
379}
380
381/**
382 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
383 * @kvm: The struct kvm pointer
384 *
385 * Go through the stage 2 page tables and invalidate any cache lines
386 * backing memory already mapped to the VM.
387 */
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000388static void stage2_flush_vm(struct kvm *kvm)
Marc Zyngier9d218a12014-01-15 12:50:23 +0000389{
390 struct kvm_memslots *slots;
391 struct kvm_memory_slot *memslot;
392 int idx;
393
394 idx = srcu_read_lock(&kvm->srcu);
395 spin_lock(&kvm->mmu_lock);
396
397 slots = kvm_memslots(kvm);
398 kvm_for_each_memslot(memslot, slots)
399 stage2_flush_memslot(kvm, memslot);
400
401 spin_unlock(&kvm->mmu_lock);
402 srcu_read_unlock(&kvm->srcu, idx);
403}
404
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000405static void clear_hyp_pgd_entry(pgd_t *pgd)
406{
407 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0UL);
408 pgd_clear(pgd);
409 pud_free(NULL, pud_table);
410 put_page(virt_to_page(pgd));
411}
412
413static void clear_hyp_pud_entry(pud_t *pud)
414{
415 pmd_t *pmd_table __maybe_unused = pmd_offset(pud, 0);
416 VM_BUG_ON(pud_huge(*pud));
417 pud_clear(pud);
418 pmd_free(NULL, pmd_table);
419 put_page(virt_to_page(pud));
420}
421
422static void clear_hyp_pmd_entry(pmd_t *pmd)
423{
424 pte_t *pte_table = pte_offset_kernel(pmd, 0);
425 VM_BUG_ON(pmd_thp_or_huge(*pmd));
426 pmd_clear(pmd);
427 pte_free_kernel(NULL, pte_table);
428 put_page(virt_to_page(pmd));
429}
430
431static void unmap_hyp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
432{
433 pte_t *pte, *start_pte;
434
435 start_pte = pte = pte_offset_kernel(pmd, addr);
436 do {
437 if (!pte_none(*pte)) {
438 kvm_set_pte(pte, __pte(0));
439 put_page(virt_to_page(pte));
440 }
441 } while (pte++, addr += PAGE_SIZE, addr != end);
442
443 if (hyp_pte_table_empty(start_pte))
444 clear_hyp_pmd_entry(pmd);
445}
446
447static void unmap_hyp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
448{
449 phys_addr_t next;
450 pmd_t *pmd, *start_pmd;
451
452 start_pmd = pmd = pmd_offset(pud, addr);
453 do {
454 next = pmd_addr_end(addr, end);
455 /* Hyp doesn't use huge pmds */
456 if (!pmd_none(*pmd))
457 unmap_hyp_ptes(pmd, addr, next);
458 } while (pmd++, addr = next, addr != end);
459
460 if (hyp_pmd_table_empty(start_pmd))
461 clear_hyp_pud_entry(pud);
462}
463
464static void unmap_hyp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
465{
466 phys_addr_t next;
467 pud_t *pud, *start_pud;
468
469 start_pud = pud = pud_offset(pgd, addr);
470 do {
471 next = pud_addr_end(addr, end);
472 /* Hyp doesn't use huge puds */
473 if (!pud_none(*pud))
474 unmap_hyp_pmds(pud, addr, next);
475 } while (pud++, addr = next, addr != end);
476
477 if (hyp_pud_table_empty(start_pud))
478 clear_hyp_pgd_entry(pgd);
479}
480
481static void unmap_hyp_range(pgd_t *pgdp, phys_addr_t start, u64 size)
482{
483 pgd_t *pgd;
484 phys_addr_t addr = start, end = start + size;
485 phys_addr_t next;
486
487 /*
488 * We don't unmap anything from HYP, except at the hyp tear down.
489 * Hence, we don't have to invalidate the TLBs here.
490 */
491 pgd = pgdp + pgd_index(addr);
492 do {
493 next = pgd_addr_end(addr, end);
494 if (!pgd_none(*pgd))
495 unmap_hyp_puds(pgd, addr, next);
496 } while (pgd++, addr = next, addr != end);
497}
498
Marc Zyngier000d3992013-03-05 02:43:17 +0000499/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100500 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000501 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100502 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
503 * therefore contains either mappings in the kernel memory area (above
504 * PAGE_OFFSET), or device mappings in the vmalloc range (from
505 * VMALLOC_START to VMALLOC_END).
506 *
507 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000508 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100509void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000510{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500511 unsigned long addr;
512
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100513 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100514
Marc Zyngier26781f9c2016-06-30 18:40:46 +0100515 if (boot_hyp_pgd) {
516 unmap_hyp_range(boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
517 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
518 boot_hyp_pgd = NULL;
519 }
520
Marc Zyngier4f728272013-04-12 19:12:05 +0100521 if (hyp_pgd) {
Marc Zyngier26781f9c2016-06-30 18:40:46 +0100522 unmap_hyp_range(hyp_pgd, hyp_idmap_start, PAGE_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100523 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngier6c41a412016-06-30 18:40:51 +0100524 unmap_hyp_range(hyp_pgd, kern_hyp_va(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100525 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngier6c41a412016-06-30 18:40:51 +0100526 unmap_hyp_range(hyp_pgd, kern_hyp_va(addr), PGDIR_SIZE);
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100527
Christoffer Dall38f791a2014-10-10 12:14:28 +0200528 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100529 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100530 }
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000531 if (merged_hyp_pgd) {
532 clear_page(merged_hyp_pgd);
533 free_page((unsigned long)merged_hyp_pgd);
534 merged_hyp_pgd = NULL;
535 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100536
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500537 mutex_unlock(&kvm_hyp_pgd_mutex);
538}
539
540static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100541 unsigned long end, unsigned long pfn,
542 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500543{
544 pte_t *pte;
545 unsigned long addr;
546
Marc Zyngier3562c762013-04-12 19:12:02 +0100547 addr = start;
548 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100549 pte = pte_offset_kernel(pmd, addr);
550 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100551 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100552 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100553 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100554 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500555}
556
557static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100558 unsigned long end, unsigned long pfn,
559 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500560{
561 pmd_t *pmd;
562 pte_t *pte;
563 unsigned long addr, next;
564
Marc Zyngier3562c762013-04-12 19:12:02 +0100565 addr = start;
566 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100567 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500568
569 BUG_ON(pmd_sect(*pmd));
570
571 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100572 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500573 if (!pte) {
574 kvm_err("Cannot allocate Hyp pte\n");
575 return -ENOMEM;
576 }
577 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100578 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100579 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500580 }
581
582 next = pmd_addr_end(addr, end);
583
Marc Zyngier6060df82013-04-12 19:12:01 +0100584 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
585 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100586 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500587
588 return 0;
589}
590
Christoffer Dall38f791a2014-10-10 12:14:28 +0200591static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
592 unsigned long end, unsigned long pfn,
593 pgprot_t prot)
594{
595 pud_t *pud;
596 pmd_t *pmd;
597 unsigned long addr, next;
598 int ret;
599
600 addr = start;
601 do {
602 pud = pud_offset(pgd, addr);
603
604 if (pud_none_or_clear_bad(pud)) {
605 pmd = pmd_alloc_one(NULL, addr);
606 if (!pmd) {
607 kvm_err("Cannot allocate Hyp pmd\n");
608 return -ENOMEM;
609 }
610 pud_populate(NULL, pud, pmd);
611 get_page(virt_to_page(pud));
612 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
613 }
614
615 next = pud_addr_end(addr, end);
616 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
617 if (ret)
618 return ret;
619 pfn += (next - addr) >> PAGE_SHIFT;
620 } while (addr = next, addr != end);
621
622 return 0;
623}
624
Marc Zyngier6060df82013-04-12 19:12:01 +0100625static int __create_hyp_mappings(pgd_t *pgdp,
626 unsigned long start, unsigned long end,
627 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500628{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500629 pgd_t *pgd;
630 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500631 unsigned long addr, next;
632 int err = 0;
633
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500634 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100635 addr = start & PAGE_MASK;
636 end = PAGE_ALIGN(end);
637 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100638 pgd = pgdp + pgd_index(addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500639
Christoffer Dall38f791a2014-10-10 12:14:28 +0200640 if (pgd_none(*pgd)) {
641 pud = pud_alloc_one(NULL, addr);
642 if (!pud) {
643 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500644 err = -ENOMEM;
645 goto out;
646 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200647 pgd_populate(NULL, pgd, pud);
648 get_page(virt_to_page(pgd));
649 kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500650 }
651
652 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200653 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500654 if (err)
655 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100656 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100657 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500658out:
659 mutex_unlock(&kvm_hyp_pgd_mutex);
660 return err;
661}
662
Christoffer Dall40c27292013-11-15 13:14:12 -0800663static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
664{
665 if (!is_vmalloc_addr(kaddr)) {
666 BUG_ON(!virt_addr_valid(kaddr));
667 return __pa(kaddr);
668 } else {
669 return page_to_phys(vmalloc_to_page(kaddr)) +
670 offset_in_page(kaddr);
671 }
672}
673
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500674/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100675 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500676 * @from: The virtual kernel start address of the range
677 * @to: The virtual kernel end address of the range (exclusive)
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100678 * @prot: The protection to be applied to this range
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500679 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100680 * The same virtual address as the kernel virtual address is also used
681 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
682 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500683 */
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100684int create_hyp_mappings(void *from, void *to, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500685{
Christoffer Dall40c27292013-11-15 13:14:12 -0800686 phys_addr_t phys_addr;
687 unsigned long virt_addr;
Marc Zyngier6c41a412016-06-30 18:40:51 +0100688 unsigned long start = kern_hyp_va((unsigned long)from);
689 unsigned long end = kern_hyp_va((unsigned long)to);
Marc Zyngier6060df82013-04-12 19:12:01 +0100690
Marc Zyngier1e947ba2015-01-29 11:59:54 +0000691 if (is_kernel_in_hyp_mode())
692 return 0;
693
Christoffer Dall40c27292013-11-15 13:14:12 -0800694 start = start & PAGE_MASK;
695 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100696
Christoffer Dall40c27292013-11-15 13:14:12 -0800697 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
698 int err;
699
700 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
701 err = __create_hyp_mappings(hyp_pgd, virt_addr,
702 virt_addr + PAGE_SIZE,
703 __phys_to_pfn(phys_addr),
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100704 prot);
Christoffer Dall40c27292013-11-15 13:14:12 -0800705 if (err)
706 return err;
707 }
708
709 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500710}
711
712/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100713 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
714 * @from: The kernel start VA of the range
715 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100716 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100717 *
718 * The resulting HYP VA is the same as the kernel VA, modulo
719 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500720 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100721int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500722{
Marc Zyngier6c41a412016-06-30 18:40:51 +0100723 unsigned long start = kern_hyp_va((unsigned long)from);
724 unsigned long end = kern_hyp_va((unsigned long)to);
Marc Zyngier6060df82013-04-12 19:12:01 +0100725
Marc Zyngier1e947ba2015-01-29 11:59:54 +0000726 if (is_kernel_in_hyp_mode())
727 return 0;
728
Marc Zyngier6060df82013-04-12 19:12:01 +0100729 /* Check for a valid kernel IO mapping */
730 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
731 return -EINVAL;
732
733 return __create_hyp_mappings(hyp_pgd, start, end,
734 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500735}
736
Christoffer Dalld5d81842013-01-20 18:28:07 -0500737/**
738 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
739 * @kvm: The KVM struct pointer for the VM.
740 *
Vladimir Murzin9d4dc6882015-11-16 11:28:16 +0000741 * Allocates only the stage-2 HW PGD level table(s) (can support either full
742 * 40-bit input addresses or limited to 32-bit input addresses). Clears the
743 * allocated pages.
Christoffer Dalld5d81842013-01-20 18:28:07 -0500744 *
745 * Note we don't need locking here as this is only called when the VM is
746 * created, which can only be done once.
747 */
748int kvm_alloc_stage2_pgd(struct kvm *kvm)
749{
750 pgd_t *pgd;
751
752 if (kvm->arch.pgd != NULL) {
753 kvm_err("kvm_arch already initialized?\n");
754 return -EINVAL;
755 }
756
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000757 /* Allocate the HW PGD, making sure that each page gets its own refcount */
758 pgd = alloc_pages_exact(S2_PGD_SIZE, GFP_KERNEL | __GFP_ZERO);
759 if (!pgd)
Marc Zyngiera9873702015-03-10 19:06:59 +0000760 return -ENOMEM;
761
Christoffer Dalld5d81842013-01-20 18:28:07 -0500762 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500763 return 0;
764}
765
Christoffer Dall957db102014-11-27 10:35:03 +0100766static void stage2_unmap_memslot(struct kvm *kvm,
767 struct kvm_memory_slot *memslot)
768{
769 hva_t hva = memslot->userspace_addr;
770 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
771 phys_addr_t size = PAGE_SIZE * memslot->npages;
772 hva_t reg_end = hva + size;
773
774 /*
775 * A memory region could potentially cover multiple VMAs, and any holes
776 * between them, so iterate over all of them to find out if we should
777 * unmap any of them.
778 *
779 * +--------------------------------------------+
780 * +---------------+----------------+ +----------------+
781 * | : VMA 1 | VMA 2 | | VMA 3 : |
782 * +---------------+----------------+ +----------------+
783 * | memory region |
784 * +--------------------------------------------+
785 */
786 do {
787 struct vm_area_struct *vma = find_vma(current->mm, hva);
788 hva_t vm_start, vm_end;
789
790 if (!vma || vma->vm_start >= reg_end)
791 break;
792
793 /*
794 * Take the intersection of this VMA with the memory region
795 */
796 vm_start = max(hva, vma->vm_start);
797 vm_end = min(reg_end, vma->vm_end);
798
799 if (!(vma->vm_flags & VM_PFNMAP)) {
800 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
801 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
802 }
803 hva = vm_end;
804 } while (hva < reg_end);
805}
806
807/**
808 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
809 * @kvm: The struct kvm pointer
810 *
811 * Go through the memregions and unmap any reguler RAM
812 * backing memory already mapped to the VM.
813 */
814void stage2_unmap_vm(struct kvm *kvm)
815{
816 struct kvm_memslots *slots;
817 struct kvm_memory_slot *memslot;
818 int idx;
819
820 idx = srcu_read_lock(&kvm->srcu);
Marc Zyngier90f6e152017-03-16 18:20:49 +0000821 down_read(&current->mm->mmap_sem);
Christoffer Dall957db102014-11-27 10:35:03 +0100822 spin_lock(&kvm->mmu_lock);
823
824 slots = kvm_memslots(kvm);
825 kvm_for_each_memslot(memslot, slots)
826 stage2_unmap_memslot(kvm, memslot);
827
828 spin_unlock(&kvm->mmu_lock);
Marc Zyngier90f6e152017-03-16 18:20:49 +0000829 up_read(&current->mm->mmap_sem);
Christoffer Dall957db102014-11-27 10:35:03 +0100830 srcu_read_unlock(&kvm->srcu, idx);
831}
832
Christoffer Dalld5d81842013-01-20 18:28:07 -0500833/**
834 * kvm_free_stage2_pgd - free all stage-2 tables
835 * @kvm: The KVM struct pointer for the VM.
836 *
837 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
838 * underlying level-2 and level-3 tables before freeing the actual level-1 table
839 * and setting the struct pointer to NULL.
Christoffer Dalld5d81842013-01-20 18:28:07 -0500840 */
841void kvm_free_stage2_pgd(struct kvm *kvm)
842{
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100843 void *pgd = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500844
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100845 spin_lock(&kvm->mmu_lock);
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100846 if (kvm->arch.pgd) {
847 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
Suzuki K Poulose2952a602017-05-16 10:34:54 +0100848 pgd = READ_ONCE(kvm->arch.pgd);
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100849 kvm->arch.pgd = NULL;
850 }
Suzuki K Poulose8b3405e2017-04-03 15:12:43 +0100851 spin_unlock(&kvm->mmu_lock);
852
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000853 /* Free the HW pgd, one page at a time */
Suzuki K Poulose6c0d7062017-05-03 15:17:51 +0100854 if (pgd)
855 free_pages_exact(pgd, S2_PGD_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500856}
857
Christoffer Dall38f791a2014-10-10 12:14:28 +0200858static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
859 phys_addr_t addr)
860{
861 pgd_t *pgd;
862 pud_t *pud;
863
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000864 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
865 if (WARN_ON(stage2_pgd_none(*pgd))) {
Christoffer Dall38f791a2014-10-10 12:14:28 +0200866 if (!cache)
867 return NULL;
868 pud = mmu_memory_cache_alloc(cache);
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000869 stage2_pgd_populate(pgd, pud);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200870 get_page(virt_to_page(pgd));
871 }
872
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000873 return stage2_pud_offset(pgd, addr);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200874}
875
Christoffer Dallad361f02012-11-01 17:14:45 +0100876static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
877 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500878{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500879 pud_t *pud;
880 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500881
Christoffer Dall38f791a2014-10-10 12:14:28 +0200882 pud = stage2_get_pud(kvm, cache, addr);
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000883 if (stage2_pud_none(*pud)) {
Christoffer Dalld5d81842013-01-20 18:28:07 -0500884 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100885 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500886 pmd = mmu_memory_cache_alloc(cache);
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000887 stage2_pud_populate(pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500888 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100889 }
890
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000891 return stage2_pmd_offset(pud, addr);
Christoffer Dallad361f02012-11-01 17:14:45 +0100892}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500893
Christoffer Dallad361f02012-11-01 17:14:45 +0100894static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
895 *cache, phys_addr_t addr, const pmd_t *new_pmd)
896{
897 pmd_t *pmd, old_pmd;
898
899 pmd = stage2_get_pmd(kvm, cache, addr);
900 VM_BUG_ON(!pmd);
901
902 /*
903 * Mapping in huge pages should only happen through a fault. If a
904 * page is merged into a transparent huge page, the individual
905 * subpages of that huge page should be unmapped through MMU
906 * notifiers before we get here.
907 *
908 * Merging of CompoundPages is not supported; they should become
909 * splitting first, unmapped, merged, and mapped back in on-demand.
910 */
911 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
912
913 old_pmd = *pmd;
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100914 if (pmd_present(old_pmd)) {
915 pmd_clear(pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +0100916 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100917 } else {
Christoffer Dallad361f02012-11-01 17:14:45 +0100918 get_page(virt_to_page(pmd));
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100919 }
920
921 kvm_set_pmd(pmd, *new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +0100922 return 0;
923}
924
925static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
Mario Smarduch15a49a42015-01-15 15:58:58 -0800926 phys_addr_t addr, const pte_t *new_pte,
927 unsigned long flags)
Christoffer Dallad361f02012-11-01 17:14:45 +0100928{
929 pmd_t *pmd;
930 pte_t *pte, old_pte;
Mario Smarduch15a49a42015-01-15 15:58:58 -0800931 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
932 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
933
934 VM_BUG_ON(logging_active && !cache);
Christoffer Dallad361f02012-11-01 17:14:45 +0100935
Christoffer Dall38f791a2014-10-10 12:14:28 +0200936 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +0100937 pmd = stage2_get_pmd(kvm, cache, addr);
938 if (!pmd) {
939 /*
940 * Ignore calls from kvm_set_spte_hva for unallocated
941 * address ranges.
942 */
943 return 0;
944 }
945
Mario Smarduch15a49a42015-01-15 15:58:58 -0800946 /*
947 * While dirty page logging - dissolve huge PMD, then continue on to
948 * allocate page.
949 */
950 if (logging_active)
951 stage2_dissolve_pmd(kvm, addr, pmd);
952
Christoffer Dallad361f02012-11-01 17:14:45 +0100953 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500954 if (pmd_none(*pmd)) {
955 if (!cache)
956 return 0; /* ignore calls from kvm_set_spte_hva */
957 pte = mmu_memory_cache_alloc(cache);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500958 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500959 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100960 }
961
962 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500963
964 if (iomap && pte_present(*pte))
965 return -EFAULT;
966
967 /* Create 2nd stage page table mapping - Level 3 */
968 old_pte = *pte;
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100969 if (pte_present(old_pte)) {
970 kvm_set_pte(pte, __pte(0));
Marc Zyngier48762762013-01-28 15:27:00 +0000971 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100972 } else {
Christoffer Dalld5d81842013-01-20 18:28:07 -0500973 get_page(virt_to_page(pte));
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100974 }
Christoffer Dalld5d81842013-01-20 18:28:07 -0500975
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100976 kvm_set_pte(pte, *new_pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500977 return 0;
978}
979
Catalin Marinas06485052016-04-13 17:57:37 +0100980#ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
981static int stage2_ptep_test_and_clear_young(pte_t *pte)
982{
983 if (pte_young(*pte)) {
984 *pte = pte_mkold(*pte);
985 return 1;
986 }
987 return 0;
988}
989#else
990static int stage2_ptep_test_and_clear_young(pte_t *pte)
991{
992 return __ptep_test_and_clear_young(pte);
993}
994#endif
995
996static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
997{
998 return stage2_ptep_test_and_clear_young((pte_t *)pmd);
999}
1000
Christoffer Dalld5d81842013-01-20 18:28:07 -05001001/**
1002 * kvm_phys_addr_ioremap - map a device range to guest IPA
1003 *
1004 * @kvm: The KVM pointer
1005 * @guest_ipa: The IPA at which to insert the mapping
1006 * @pa: The physical address of the device
1007 * @size: The size of the mapping
1008 */
1009int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001010 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001011{
1012 phys_addr_t addr, end;
1013 int ret = 0;
1014 unsigned long pfn;
1015 struct kvm_mmu_memory_cache cache = { 0, };
1016
1017 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
1018 pfn = __phys_to_pfn(pa);
1019
1020 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +01001021 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001022
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001023 if (writable)
Catalin Marinas06485052016-04-13 17:57:37 +01001024 pte = kvm_s2pte_mkwrite(pte);
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001025
Christoffer Dall38f791a2014-10-10 12:14:28 +02001026 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
1027 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001028 if (ret)
1029 goto out;
1030 spin_lock(&kvm->mmu_lock);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001031 ret = stage2_set_pte(kvm, &cache, addr, &pte,
1032 KVM_S2PTE_FLAG_IS_IOMAP);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001033 spin_unlock(&kvm->mmu_lock);
1034 if (ret)
1035 goto out;
1036
1037 pfn++;
1038 }
1039
1040out:
1041 mmu_free_memory_cache(&cache);
1042 return ret;
1043}
1044
Dan Williamsba049e92016-01-15 16:56:11 -08001045static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001046{
Dan Williamsba049e92016-01-15 16:56:11 -08001047 kvm_pfn_t pfn = *pfnp;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001048 gfn_t gfn = *ipap >> PAGE_SHIFT;
1049
Andrea Arcangeli127393f2016-05-05 16:22:20 -07001050 if (PageTransCompoundMap(pfn_to_page(pfn))) {
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001051 unsigned long mask;
1052 /*
1053 * The address we faulted on is backed by a transparent huge
1054 * page. However, because we map the compound huge page and
1055 * not the individual tail page, we need to transfer the
1056 * refcount to the head page. We have to be careful that the
1057 * THP doesn't start to split while we are adjusting the
1058 * refcounts.
1059 *
1060 * We are sure this doesn't happen, because mmu_notifier_retry
1061 * was successful and we are holding the mmu_lock, so if this
1062 * THP is trying to split, it will be blocked in the mmu
1063 * notifier before touching any of the pages, specifically
1064 * before being able to call __split_huge_page_refcount().
1065 *
1066 * We can therefore safely transfer the refcount from PG_tail
1067 * to PG_head and switch the pfn from a tail page to the head
1068 * page accordingly.
1069 */
1070 mask = PTRS_PER_PMD - 1;
1071 VM_BUG_ON((gfn & mask) != (pfn & mask));
1072 if (pfn & mask) {
1073 *ipap &= PMD_MASK;
1074 kvm_release_pfn_clean(pfn);
1075 pfn &= ~mask;
1076 kvm_get_pfn(pfn);
1077 *pfnp = pfn;
1078 }
1079
1080 return true;
1081 }
1082
1083 return false;
1084}
1085
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001086static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
1087{
1088 if (kvm_vcpu_trap_is_iabt(vcpu))
1089 return false;
1090
1091 return kvm_vcpu_dabt_iswrite(vcpu);
1092}
1093
Mario Smarduchc6473552015-01-15 15:58:56 -08001094/**
1095 * stage2_wp_ptes - write protect PMD range
1096 * @pmd: pointer to pmd entry
1097 * @addr: range start address
1098 * @end: range end address
1099 */
1100static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1101{
1102 pte_t *pte;
1103
1104 pte = pte_offset_kernel(pmd, addr);
1105 do {
1106 if (!pte_none(*pte)) {
1107 if (!kvm_s2pte_readonly(pte))
1108 kvm_set_s2pte_readonly(pte);
1109 }
1110 } while (pte++, addr += PAGE_SIZE, addr != end);
1111}
1112
1113/**
1114 * stage2_wp_pmds - write protect PUD range
1115 * @pud: pointer to pud entry
1116 * @addr: range start address
1117 * @end: range end address
1118 */
1119static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
1120{
1121 pmd_t *pmd;
1122 phys_addr_t next;
1123
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001124 pmd = stage2_pmd_offset(pud, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001125
1126 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001127 next = stage2_pmd_addr_end(addr, end);
Mario Smarduchc6473552015-01-15 15:58:56 -08001128 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001129 if (pmd_thp_or_huge(*pmd)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001130 if (!kvm_s2pmd_readonly(pmd))
1131 kvm_set_s2pmd_readonly(pmd);
1132 } else {
1133 stage2_wp_ptes(pmd, addr, next);
1134 }
1135 }
1136 } while (pmd++, addr = next, addr != end);
1137}
1138
1139/**
1140 * stage2_wp_puds - write protect PGD range
1141 * @pgd: pointer to pgd entry
1142 * @addr: range start address
1143 * @end: range end address
1144 *
1145 * Process PUD entries, for a huge PUD we cause a panic.
1146 */
1147static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
1148{
1149 pud_t *pud;
1150 phys_addr_t next;
1151
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001152 pud = stage2_pud_offset(pgd, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001153 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001154 next = stage2_pud_addr_end(addr, end);
1155 if (!stage2_pud_none(*pud)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001156 /* TODO:PUD not supported, revisit later if supported */
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001157 BUG_ON(stage2_pud_huge(*pud));
Mario Smarduchc6473552015-01-15 15:58:56 -08001158 stage2_wp_pmds(pud, addr, next);
1159 }
1160 } while (pud++, addr = next, addr != end);
1161}
1162
1163/**
1164 * stage2_wp_range() - write protect stage2 memory region range
1165 * @kvm: The KVM pointer
1166 * @addr: Start address of range
1167 * @end: End address of range
1168 */
1169static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1170{
1171 pgd_t *pgd;
1172 phys_addr_t next;
1173
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001174 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001175 do {
1176 /*
1177 * Release kvm_mmu_lock periodically if the memory region is
1178 * large. Otherwise, we may see kernel panics with
Christoffer Dall227ea812015-01-23 10:49:31 +01001179 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1180 * CONFIG_LOCKDEP. Additionally, holding the lock too long
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001181 * will also starve other vCPUs. We have to also make sure
1182 * that the page tables are not freed while we released
1183 * the lock.
Mario Smarduchc6473552015-01-15 15:58:56 -08001184 */
Suzuki K Poulose0c428a6a2017-05-16 10:34:55 +01001185 cond_resched_lock(&kvm->mmu_lock);
1186 if (!READ_ONCE(kvm->arch.pgd))
1187 break;
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001188 next = stage2_pgd_addr_end(addr, end);
1189 if (stage2_pgd_present(*pgd))
Mario Smarduchc6473552015-01-15 15:58:56 -08001190 stage2_wp_puds(pgd, addr, next);
1191 } while (pgd++, addr = next, addr != end);
1192}
1193
1194/**
1195 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1196 * @kvm: The KVM pointer
1197 * @slot: The memory slot to write protect
1198 *
1199 * Called to start logging dirty pages after memory region
1200 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1201 * all present PMD and PTEs are write protected in the memory region.
1202 * Afterwards read of dirty page log can be called.
1203 *
1204 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1205 * serializing operations for VM memory regions.
1206 */
1207void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1208{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001209 struct kvm_memslots *slots = kvm_memslots(kvm);
1210 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
Mario Smarduchc6473552015-01-15 15:58:56 -08001211 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1212 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1213
1214 spin_lock(&kvm->mmu_lock);
1215 stage2_wp_range(kvm, start, end);
1216 spin_unlock(&kvm->mmu_lock);
1217 kvm_flush_remote_tlbs(kvm);
1218}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001219
1220/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001221 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
Mario Smarduch53c810c2015-01-15 15:58:57 -08001222 * @kvm: The KVM pointer
1223 * @slot: The memory slot associated with mask
1224 * @gfn_offset: The gfn offset in memory slot
1225 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1226 * slot to be write protected
1227 *
1228 * Walks bits set in mask write protects the associated pte's. Caller must
1229 * acquire kvm_mmu_lock.
1230 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001231static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Mario Smarduch53c810c2015-01-15 15:58:57 -08001232 struct kvm_memory_slot *slot,
1233 gfn_t gfn_offset, unsigned long mask)
1234{
1235 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1236 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1237 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1238
1239 stage2_wp_range(kvm, start, end);
1240}
Mario Smarduchc6473552015-01-15 15:58:56 -08001241
Kai Huang3b0f1d02015-01-28 10:54:23 +08001242/*
1243 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1244 * dirty pages.
1245 *
1246 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1247 * enable dirty logging for them.
1248 */
1249void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1250 struct kvm_memory_slot *slot,
1251 gfn_t gfn_offset, unsigned long mask)
1252{
1253 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1254}
1255
Dan Williamsba049e92016-01-15 16:56:11 -08001256static void coherent_cache_guest_page(struct kvm_vcpu *vcpu, kvm_pfn_t pfn,
Marc Zyngier13b77562017-01-25 13:33:11 +00001257 unsigned long size)
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001258{
Marc Zyngier13b77562017-01-25 13:33:11 +00001259 __coherent_cache_guest_page(vcpu, pfn, size);
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001260}
1261
Christoffer Dall94f8e642013-01-20 18:28:12 -05001262static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001263 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001264 unsigned long fault_status)
1265{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001266 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001267 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001268 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001269 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001270 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001271 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001272 struct vm_area_struct *vma;
Dan Williamsba049e92016-01-15 16:56:11 -08001273 kvm_pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001274 pgprot_t mem_type = PAGE_S2;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001275 bool logging_active = memslot_is_logging(memslot);
1276 unsigned long flags = 0;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001277
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001278 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001279 if (fault_status == FSC_PERM && !write_fault) {
1280 kvm_err("Unexpected L2 read permission error\n");
1281 return -EFAULT;
1282 }
1283
Christoffer Dallad361f02012-11-01 17:14:45 +01001284 /* Let's check if we will get back a huge page backed by hugetlbfs */
1285 down_read(&current->mm->mmap_sem);
1286 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001287 if (unlikely(!vma)) {
1288 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1289 up_read(&current->mm->mmap_sem);
1290 return -EFAULT;
1291 }
1292
Mario Smarduch15a49a42015-01-15 15:58:58 -08001293 if (is_vm_hugetlb_page(vma) && !logging_active) {
Christoffer Dallad361f02012-11-01 17:14:45 +01001294 hugetlb = true;
1295 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001296 } else {
1297 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001298 * Pages belonging to memslots that don't have the same
1299 * alignment for userspace and IPA cannot be mapped using
1300 * block descriptors even if the pages belong to a THP for
1301 * the process, because the stage-2 block descriptor will
1302 * cover more than a single THP and we loose atomicity for
1303 * unmapping, updates, and splits of the THP or other pages
1304 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001305 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001306 if ((memslot->userspace_addr & ~PMD_MASK) !=
1307 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001308 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001309 }
1310 up_read(&current->mm->mmap_sem);
1311
Christoffer Dall94f8e642013-01-20 18:28:12 -05001312 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001313 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1314 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001315 if (ret)
1316 return ret;
1317
1318 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1319 /*
1320 * Ensure the read of mmu_notifier_seq happens before we call
1321 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1322 * the page we just got a reference to gets unmapped before we have a
1323 * chance to grab the mmu_lock, which ensure that if the page gets
1324 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1325 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1326 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1327 */
1328 smp_rmb();
1329
Christoffer Dallad361f02012-11-01 17:14:45 +01001330 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall9ac71592016-08-17 10:46:10 +02001331 if (is_error_noslot_pfn(pfn))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001332 return -EFAULT;
1333
Mario Smarduch15a49a42015-01-15 15:58:58 -08001334 if (kvm_is_device_pfn(pfn)) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001335 mem_type = PAGE_S2_DEVICE;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001336 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1337 } else if (logging_active) {
1338 /*
1339 * Faults on pages in a memslot with logging enabled
1340 * should not be mapped with huge pages (it introduces churn
1341 * and performance degradation), so force a pte mapping.
1342 */
1343 force_pte = true;
1344 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1345
1346 /*
1347 * Only actually map the page as writable if this was a write
1348 * fault.
1349 */
1350 if (!write_fault)
1351 writable = false;
1352 }
Kim Phillipsb8865762014-06-26 01:45:51 +01001353
Christoffer Dallad361f02012-11-01 17:14:45 +01001354 spin_lock(&kvm->mmu_lock);
1355 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001356 goto out_unlock;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001357
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001358 if (!hugetlb && !force_pte)
1359 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001360
1361 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001362 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001363 new_pmd = pmd_mkhuge(new_pmd);
1364 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001365 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +01001366 kvm_set_pfn_dirty(pfn);
1367 }
Marc Zyngier13b77562017-01-25 13:33:11 +00001368 coherent_cache_guest_page(vcpu, pfn, PMD_SIZE);
Christoffer Dallad361f02012-11-01 17:14:45 +01001369 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1370 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001371 pte_t new_pte = pfn_pte(pfn, mem_type);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001372
Christoffer Dallad361f02012-11-01 17:14:45 +01001373 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001374 new_pte = kvm_s2pte_mkwrite(new_pte);
Christoffer Dallad361f02012-11-01 17:14:45 +01001375 kvm_set_pfn_dirty(pfn);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001376 mark_page_dirty(kvm, gfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001377 }
Marc Zyngier13b77562017-01-25 13:33:11 +00001378 coherent_cache_guest_page(vcpu, pfn, PAGE_SIZE);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001379 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001380 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001381
Christoffer Dall94f8e642013-01-20 18:28:12 -05001382out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001383 spin_unlock(&kvm->mmu_lock);
Marc Zyngier35307b92015-03-12 18:16:51 +00001384 kvm_set_pfn_accessed(pfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001385 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001386 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001387}
1388
Marc Zyngieraeda9132015-03-12 18:16:52 +00001389/*
1390 * Resolve the access fault by making the page young again.
1391 * Note that because the faulting entry is guaranteed not to be
1392 * cached in the TLB, we don't need to invalidate anything.
Catalin Marinas06485052016-04-13 17:57:37 +01001393 * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
1394 * so there is no need for atomic (pte|pmd)_mkyoung operations.
Marc Zyngieraeda9132015-03-12 18:16:52 +00001395 */
1396static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1397{
1398 pmd_t *pmd;
1399 pte_t *pte;
Dan Williamsba049e92016-01-15 16:56:11 -08001400 kvm_pfn_t pfn;
Marc Zyngieraeda9132015-03-12 18:16:52 +00001401 bool pfn_valid = false;
1402
1403 trace_kvm_access_fault(fault_ipa);
1404
1405 spin_lock(&vcpu->kvm->mmu_lock);
1406
1407 pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
1408 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1409 goto out;
1410
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001411 if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
Marc Zyngieraeda9132015-03-12 18:16:52 +00001412 *pmd = pmd_mkyoung(*pmd);
1413 pfn = pmd_pfn(*pmd);
1414 pfn_valid = true;
1415 goto out;
1416 }
1417
1418 pte = pte_offset_kernel(pmd, fault_ipa);
1419 if (pte_none(*pte)) /* Nothing there either */
1420 goto out;
1421
1422 *pte = pte_mkyoung(*pte); /* Just a page... */
1423 pfn = pte_pfn(*pte);
1424 pfn_valid = true;
1425out:
1426 spin_unlock(&vcpu->kvm->mmu_lock);
1427 if (pfn_valid)
1428 kvm_set_pfn_accessed(pfn);
1429}
1430
Tyler Baicar621f48e2017-06-21 12:17:14 -06001431static bool is_abort_sea(unsigned long fault_status)
1432{
1433 switch (fault_status) {
1434 case FSC_SEA:
1435 case FSC_SEA_TTW0:
1436 case FSC_SEA_TTW1:
1437 case FSC_SEA_TTW2:
1438 case FSC_SEA_TTW3:
1439 case FSC_SECC:
1440 case FSC_SECC_TTW0:
1441 case FSC_SECC_TTW1:
1442 case FSC_SECC_TTW2:
1443 case FSC_SECC_TTW3:
1444 return true;
1445 default:
1446 return false;
1447 }
1448}
1449
Christoffer Dall94f8e642013-01-20 18:28:12 -05001450/**
1451 * kvm_handle_guest_abort - handles all 2nd stage aborts
1452 * @vcpu: the VCPU pointer
1453 * @run: the kvm_run structure
1454 *
1455 * Any abort that gets to the host is almost guaranteed to be caused by a
1456 * missing second stage translation table entry, which can mean that either the
1457 * guest simply needs more memory and we must allocate an appropriate page or it
1458 * can mean that the guest tried to access I/O memory, which is emulated by user
1459 * space. The distinction is based on the IPA causing the fault and whether this
1460 * memory region has been registered as standard RAM by user space.
1461 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001462int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1463{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001464 unsigned long fault_status;
1465 phys_addr_t fault_ipa;
1466 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001467 unsigned long hva;
1468 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001469 gfn_t gfn;
1470 int ret, idx;
1471
Tyler Baicar621f48e2017-06-21 12:17:14 -06001472 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
1473
1474 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
1475
1476 /*
1477 * The host kernel will handle the synchronous external abort. There
1478 * is no need to pass the error into the guest.
1479 */
1480 if (is_abort_sea(fault_status)) {
1481 if (!handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
1482 return 1;
1483 }
1484
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001485 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier40557102016-09-06 14:02:15 +01001486 if (unlikely(!is_iabt && kvm_vcpu_dabt_isextabt(vcpu))) {
1487 kvm_inject_vabt(vcpu);
1488 return 1;
1489 }
1490
Marc Zyngier7393b592012-09-17 19:27:09 +01001491 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1492 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001493
1494 /* Check the stage-2 fault is trans. fault or write fault */
Marc Zyngier35307b92015-03-12 18:16:51 +00001495 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1496 fault_status != FSC_ACCESS) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001497 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1498 kvm_vcpu_trap_get_class(vcpu),
1499 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1500 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001501 return -EFAULT;
1502 }
1503
1504 idx = srcu_read_lock(&vcpu->kvm->srcu);
1505
1506 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001507 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1508 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001509 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001510 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001511 if (is_iabt) {
1512 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001513 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001514 ret = 1;
1515 goto out_unlock;
1516 }
1517
Marc Zyngiercfe39502012-12-12 14:42:09 +00001518 /*
Marc Zyngier57c841f2016-01-29 15:01:28 +00001519 * Check for a cache maintenance operation. Since we
1520 * ended-up here, we know it is outside of any memory
1521 * slot. But we can't find out if that is for a device,
1522 * or if the guest is just being stupid. The only thing
1523 * we know for sure is that this range cannot be cached.
1524 *
1525 * So let's assume that the guest is just being
1526 * cautious, and skip the instruction.
1527 */
1528 if (kvm_vcpu_dabt_is_cm(vcpu)) {
1529 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1530 ret = 1;
1531 goto out_unlock;
1532 }
1533
1534 /*
Marc Zyngiercfe39502012-12-12 14:42:09 +00001535 * The IPA is reported as [MAX:12], so we need to
1536 * complement it with the bottom 12 bits from the
1537 * faulting VA. This is always 12 bits, irrespective
1538 * of the page size.
1539 */
1540 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001541 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001542 goto out_unlock;
1543 }
1544
Christoffer Dallc3058d52014-10-10 12:14:29 +02001545 /* Userspace should not be able to register out-of-bounds IPAs */
1546 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1547
Marc Zyngieraeda9132015-03-12 18:16:52 +00001548 if (fault_status == FSC_ACCESS) {
1549 handle_access_fault(vcpu, fault_ipa);
1550 ret = 1;
1551 goto out_unlock;
1552 }
1553
Christoffer Dall98047882014-08-19 12:18:04 +02001554 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001555 if (ret == 0)
1556 ret = 1;
1557out_unlock:
1558 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1559 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001560}
1561
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001562static int handle_hva_to_gpa(struct kvm *kvm,
1563 unsigned long start,
1564 unsigned long end,
1565 int (*handler)(struct kvm *kvm,
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001566 gpa_t gpa, u64 size,
1567 void *data),
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001568 void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001569{
1570 struct kvm_memslots *slots;
1571 struct kvm_memory_slot *memslot;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001572 int ret = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001573
1574 slots = kvm_memslots(kvm);
1575
1576 /* we only care about the pages that the guest sees */
1577 kvm_for_each_memslot(memslot, slots) {
1578 unsigned long hva_start, hva_end;
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001579 gfn_t gpa;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001580
1581 hva_start = max(start, memslot->userspace_addr);
1582 hva_end = min(end, memslot->userspace_addr +
1583 (memslot->npages << PAGE_SHIFT));
1584 if (hva_start >= hva_end)
1585 continue;
1586
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001587 gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
1588 ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001589 }
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001590
1591 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001592}
1593
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001594static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001595{
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001596 unmap_stage2_range(kvm, gpa, size);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001597 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001598}
1599
1600int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1601{
1602 unsigned long end = hva + PAGE_SIZE;
1603
1604 if (!kvm->arch.pgd)
1605 return 0;
1606
1607 trace_kvm_unmap_hva(hva);
1608 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1609 return 0;
1610}
1611
1612int kvm_unmap_hva_range(struct kvm *kvm,
1613 unsigned long start, unsigned long end)
1614{
1615 if (!kvm->arch.pgd)
1616 return 0;
1617
1618 trace_kvm_unmap_hva_range(start, end);
1619 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1620 return 0;
1621}
1622
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001623static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001624{
1625 pte_t *pte = (pte_t *)data;
1626
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001627 WARN_ON(size != PAGE_SIZE);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001628 /*
1629 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1630 * flag clear because MMU notifiers will have unmapped a huge PMD before
1631 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1632 * therefore stage2_set_pte() never needs to clear out a huge PMD
1633 * through this calling path.
1634 */
1635 stage2_set_pte(kvm, NULL, gpa, pte, 0);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001636 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001637}
1638
1639
1640void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1641{
1642 unsigned long end = hva + PAGE_SIZE;
1643 pte_t stage2_pte;
1644
1645 if (!kvm->arch.pgd)
1646 return;
1647
1648 trace_kvm_set_spte_hva(hva);
1649 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1650 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1651}
1652
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001653static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Marc Zyngier35307b92015-03-12 18:16:51 +00001654{
1655 pmd_t *pmd;
1656 pte_t *pte;
1657
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001658 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
Marc Zyngier35307b92015-03-12 18:16:51 +00001659 pmd = stage2_get_pmd(kvm, NULL, gpa);
1660 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1661 return 0;
1662
Catalin Marinas06485052016-04-13 17:57:37 +01001663 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
1664 return stage2_pmdp_test_and_clear_young(pmd);
Marc Zyngier35307b92015-03-12 18:16:51 +00001665
1666 pte = pte_offset_kernel(pmd, gpa);
1667 if (pte_none(*pte))
1668 return 0;
1669
Catalin Marinas06485052016-04-13 17:57:37 +01001670 return stage2_ptep_test_and_clear_young(pte);
Marc Zyngier35307b92015-03-12 18:16:51 +00001671}
1672
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001673static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
Marc Zyngier35307b92015-03-12 18:16:51 +00001674{
1675 pmd_t *pmd;
1676 pte_t *pte;
1677
Suzuki K Poulose056aad62017-03-20 18:26:42 +00001678 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
Marc Zyngier35307b92015-03-12 18:16:51 +00001679 pmd = stage2_get_pmd(kvm, NULL, gpa);
1680 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1681 return 0;
1682
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001683 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
Marc Zyngier35307b92015-03-12 18:16:51 +00001684 return pmd_young(*pmd);
1685
1686 pte = pte_offset_kernel(pmd, gpa);
1687 if (!pte_none(*pte)) /* Just a page... */
1688 return pte_young(*pte);
1689
1690 return 0;
1691}
1692
1693int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
1694{
1695 trace_kvm_age_hva(start, end);
1696 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
1697}
1698
1699int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1700{
1701 trace_kvm_test_age_hva(hva);
1702 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
1703}
1704
Christoffer Dalld5d81842013-01-20 18:28:07 -05001705void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1706{
1707 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1708}
1709
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001710phys_addr_t kvm_mmu_get_httbr(void)
1711{
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001712 if (__kvm_cpu_uses_extended_idmap())
1713 return virt_to_phys(merged_hyp_pgd);
1714 else
1715 return virt_to_phys(hyp_pgd);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001716}
1717
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001718phys_addr_t kvm_get_idmap_vector(void)
1719{
1720 return hyp_idmap_vector;
1721}
1722
Marc Zyngier0535a3e2016-06-30 18:40:43 +01001723static int kvm_map_idmap_text(pgd_t *pgd)
1724{
1725 int err;
1726
1727 /* Create the idmap in the boot page tables */
1728 err = __create_hyp_mappings(pgd,
1729 hyp_idmap_start, hyp_idmap_end,
1730 __phys_to_pfn(hyp_idmap_start),
1731 PAGE_HYP_EXEC);
1732 if (err)
1733 kvm_err("Failed to idmap %lx-%lx\n",
1734 hyp_idmap_start, hyp_idmap_end);
1735
1736 return err;
1737}
1738
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001739int kvm_mmu_init(void)
1740{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001741 int err;
1742
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001743 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1744 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1745 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001746
Ard Biesheuvel06f75a12015-03-19 16:42:26 +00001747 /*
1748 * We rely on the linker script to ensure at build time that the HYP
1749 * init code does not cross a page boundary.
1750 */
1751 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001752
Marc Zyngiereac378a2016-06-30 18:40:50 +01001753 kvm_info("IDMAP page: %lx\n", hyp_idmap_start);
1754 kvm_info("HYP VA range: %lx:%lx\n",
Marc Zyngier6c41a412016-06-30 18:40:51 +01001755 kern_hyp_va(PAGE_OFFSET), kern_hyp_va(~0UL));
Marc Zyngiereac378a2016-06-30 18:40:50 +01001756
Marc Zyngier6c41a412016-06-30 18:40:51 +01001757 if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
Marc Zyngierd2896d42016-08-22 09:01:17 +01001758 hyp_idmap_start < kern_hyp_va(~0UL) &&
1759 hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
Marc Zyngiereac378a2016-06-30 18:40:50 +01001760 /*
1761 * The idmap page is intersecting with the VA space,
1762 * it is not safe to continue further.
1763 */
1764 kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
1765 err = -EINVAL;
1766 goto out;
1767 }
1768
Christoffer Dall38f791a2014-10-10 12:14:28 +02001769 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Marc Zyngier0535a3e2016-06-30 18:40:43 +01001770 if (!hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001771 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001772 err = -ENOMEM;
1773 goto out;
1774 }
1775
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001776 if (__kvm_cpu_uses_extended_idmap()) {
Marc Zyngier0535a3e2016-06-30 18:40:43 +01001777 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1778 hyp_pgd_order);
1779 if (!boot_hyp_pgd) {
1780 kvm_err("Hyp boot PGD not allocated\n");
1781 err = -ENOMEM;
1782 goto out;
1783 }
1784
1785 err = kvm_map_idmap_text(boot_hyp_pgd);
1786 if (err)
1787 goto out;
1788
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001789 merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1790 if (!merged_hyp_pgd) {
1791 kvm_err("Failed to allocate extra HYP pgd\n");
1792 goto out;
1793 }
1794 __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
1795 hyp_idmap_start);
Marc Zyngier0535a3e2016-06-30 18:40:43 +01001796 } else {
1797 err = kvm_map_idmap_text(hyp_pgd);
1798 if (err)
1799 goto out;
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001800 }
1801
Christoffer Dalld5d81842013-01-20 18:28:07 -05001802 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001803out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001804 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001805 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001806}
Eric Augerdf6ce242014-06-06 11:10:23 +02001807
1808void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001809 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02001810 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02001811 const struct kvm_memory_slot *new,
Eric Augerdf6ce242014-06-06 11:10:23 +02001812 enum kvm_mr_change change)
1813{
Mario Smarduchc6473552015-01-15 15:58:56 -08001814 /*
1815 * At this point memslot has been committed and there is an
1816 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1817 * memory slot is write protected.
1818 */
1819 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
1820 kvm_mmu_wp_memory_region(kvm, mem->slot);
Eric Augerdf6ce242014-06-06 11:10:23 +02001821}
1822
1823int kvm_arch_prepare_memory_region(struct kvm *kvm,
1824 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001825 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02001826 enum kvm_mr_change change)
1827{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001828 hva_t hva = mem->userspace_addr;
1829 hva_t reg_end = hva + mem->memory_size;
1830 bool writable = !(mem->flags & KVM_MEM_READONLY);
1831 int ret = 0;
1832
Mario Smarduch15a49a42015-01-15 15:58:58 -08001833 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
1834 change != KVM_MR_FLAGS_ONLY)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001835 return 0;
1836
1837 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001838 * Prevent userspace from creating a memory region outside of the IPA
1839 * space addressable by the KVM guest IPA space.
1840 */
1841 if (memslot->base_gfn + memslot->npages >=
1842 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1843 return -EFAULT;
1844
Marc Zyngier72f31042017-03-16 18:20:50 +00001845 down_read(&current->mm->mmap_sem);
Christoffer Dallc3058d52014-10-10 12:14:29 +02001846 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001847 * A memory region could potentially cover multiple VMAs, and any holes
1848 * between them, so iterate over all of them to find out if we can map
1849 * any of them right now.
1850 *
1851 * +--------------------------------------------+
1852 * +---------------+----------------+ +----------------+
1853 * | : VMA 1 | VMA 2 | | VMA 3 : |
1854 * +---------------+----------------+ +----------------+
1855 * | memory region |
1856 * +--------------------------------------------+
1857 */
1858 do {
1859 struct vm_area_struct *vma = find_vma(current->mm, hva);
1860 hva_t vm_start, vm_end;
1861
1862 if (!vma || vma->vm_start >= reg_end)
1863 break;
1864
1865 /*
1866 * Mapping a read-only VMA is only allowed if the
1867 * memory region is configured as read-only.
1868 */
1869 if (writable && !(vma->vm_flags & VM_WRITE)) {
1870 ret = -EPERM;
1871 break;
1872 }
1873
1874 /*
1875 * Take the intersection of this VMA with the memory region
1876 */
1877 vm_start = max(hva, vma->vm_start);
1878 vm_end = min(reg_end, vma->vm_end);
1879
1880 if (vma->vm_flags & VM_PFNMAP) {
1881 gpa_t gpa = mem->guest_phys_addr +
1882 (vm_start - mem->userspace_addr);
Marek Majtykaca09f022015-09-16 12:04:55 +02001883 phys_addr_t pa;
1884
1885 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
1886 pa += vm_start - vma->vm_start;
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001887
Mario Smarduch15a49a42015-01-15 15:58:58 -08001888 /* IO region dirty page logging not allowed */
Marc Zyngier72f31042017-03-16 18:20:50 +00001889 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
1890 ret = -EINVAL;
1891 goto out;
1892 }
Mario Smarduch15a49a42015-01-15 15:58:58 -08001893
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001894 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1895 vm_end - vm_start,
1896 writable);
1897 if (ret)
1898 break;
1899 }
1900 hva = vm_end;
1901 } while (hva < reg_end);
1902
Mario Smarduch15a49a42015-01-15 15:58:58 -08001903 if (change == KVM_MR_FLAGS_ONLY)
Marc Zyngier72f31042017-03-16 18:20:50 +00001904 goto out;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001905
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001906 spin_lock(&kvm->mmu_lock);
1907 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001908 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001909 else
1910 stage2_flush_memslot(kvm, memslot);
1911 spin_unlock(&kvm->mmu_lock);
Marc Zyngier72f31042017-03-16 18:20:50 +00001912out:
1913 up_read(&current->mm->mmap_sem);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001914 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001915}
1916
1917void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1918 struct kvm_memory_slot *dont)
1919{
1920}
1921
1922int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1923 unsigned long npages)
1924{
1925 return 0;
1926}
1927
Paolo Bonzini15f46012015-05-17 21:26:08 +02001928void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Eric Augerdf6ce242014-06-06 11:10:23 +02001929{
1930}
1931
1932void kvm_arch_flush_shadow_all(struct kvm *kvm)
1933{
Suzuki K Poulose293f2932016-09-08 16:25:49 +01001934 kvm_free_stage2_pgd(kvm);
Eric Augerdf6ce242014-06-06 11:10:23 +02001935}
1936
1937void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1938 struct kvm_memory_slot *slot)
1939{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001940 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1941 phys_addr_t size = slot->npages << PAGE_SHIFT;
1942
1943 spin_lock(&kvm->mmu_lock);
1944 unmap_stage2_range(kvm, gpa, size);
1945 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001946}
Marc Zyngier3c1e7162014-12-19 16:05:31 +00001947
1948/*
1949 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1950 *
1951 * Main problems:
1952 * - S/W ops are local to a CPU (not broadcast)
1953 * - We have line migration behind our back (speculation)
1954 * - System caches don't support S/W at all (damn!)
1955 *
1956 * In the face of the above, the best we can do is to try and convert
1957 * S/W ops to VA ops. Because the guest is not allowed to infer the
1958 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1959 * which is a rather good thing for us.
1960 *
1961 * Also, it is only used when turning caches on/off ("The expected
1962 * usage of the cache maintenance instructions that operate by set/way
1963 * is associated with the cache maintenance instructions associated
1964 * with the powerdown and powerup of caches, if this is required by
1965 * the implementation.").
1966 *
1967 * We use the following policy:
1968 *
1969 * - If we trap a S/W operation, we enable VM trapping to detect
1970 * caches being turned on/off, and do a full clean.
1971 *
1972 * - We flush the caches on both caches being turned on and off.
1973 *
1974 * - Once the caches are enabled, we stop trapping VM ops.
1975 */
1976void kvm_set_way_flush(struct kvm_vcpu *vcpu)
1977{
1978 unsigned long hcr = vcpu_get_hcr(vcpu);
1979
1980 /*
1981 * If this is the first time we do a S/W operation
1982 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1983 * VM trapping.
1984 *
1985 * Otherwise, rely on the VM trapping to wait for the MMU +
1986 * Caches to be turned off. At that point, we'll be able to
1987 * clean the caches again.
1988 */
1989 if (!(hcr & HCR_TVM)) {
1990 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
1991 vcpu_has_cache_enabled(vcpu));
1992 stage2_flush_vm(vcpu->kvm);
1993 vcpu_set_hcr(vcpu, hcr | HCR_TVM);
1994 }
1995}
1996
1997void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
1998{
1999 bool now_enabled = vcpu_has_cache_enabled(vcpu);
2000
2001 /*
2002 * If switching the MMU+caches on, need to invalidate the caches.
2003 * If switching it off, need to clean the caches.
2004 * Clean + invalidate does the trick always.
2005 */
2006 if (now_enabled != was_enabled)
2007 stage2_flush_vm(vcpu->kvm);
2008
2009 /* Caches are now on, stop trapping VM ops (until a S/W op) */
2010 if (now_enabled)
2011 vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
2012
2013 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
2014}