blob: 667e93304033062deec99f5d2f8a41eae9afead9 [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 Zyngier5aa75c82015-01-29 11:59:54 +000031#include <asm/virt.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050032
33#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050034
35extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
36
Marc Zyngier5a677ce2013-04-12 19:12:06 +010037static pgd_t *boot_hyp_pgd;
Marc Zyngier2fb41052013-04-12 19:12:03 +010038static pgd_t *hyp_pgd;
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000039static pgd_t *merged_hyp_pgd;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050040static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
41
Marc Zyngier5a677ce2013-04-12 19:12:06 +010042static unsigned long hyp_idmap_start;
43static unsigned long hyp_idmap_end;
44static phys_addr_t hyp_idmap_vector;
45
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
Christoffer Dall9b5fdb92013-10-02 15:32:01 -070048#define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
Mario Smarduchc6473552015-01-15 15:58:56 -080049#define kvm_pud_huge(_x) pud_huge(_x)
Christoffer Dallad361f02012-11-01 17:14:45 +010050
Mario Smarduch15a49a42015-01-15 15:58:58 -080051#define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
52#define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
53
54static bool memslot_is_logging(struct kvm_memory_slot *memslot)
55{
Mario Smarduch15a49a42015-01-15 15:58:58 -080056 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
Mario Smarduch72760302015-01-15 15:59:01 -080057}
58
59/**
60 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
61 * @kvm: pointer to kvm structure.
62 *
63 * Interface to HYP function to flush all VM TLB entries
64 */
65void kvm_flush_remote_tlbs(struct kvm *kvm)
66{
67 kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
Mario Smarduch15a49a42015-01-15 15:58:58 -080068}
Christoffer Dall342cd0a2013-01-20 18:28:06 -050069
Marc Zyngier48762762013-01-28 15:27:00 +000070static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050071{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +010072 /*
73 * This function also gets called when dealing with HYP page
74 * tables. As HYP doesn't have an associated struct kvm (and
75 * the HYP page tables are fairly static), we don't do
76 * anything there.
77 */
78 if (kvm)
79 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050080}
81
Marc Zyngier363ef892014-12-19 16:48:06 +000082/*
83 * D-Cache management functions. They take the page table entries by
84 * value, as they are flushing the cache using the kernel mapping (or
85 * kmap on 32bit).
86 */
87static void kvm_flush_dcache_pte(pte_t pte)
88{
89 __kvm_flush_dcache_pte(pte);
90}
91
92static void kvm_flush_dcache_pmd(pmd_t pmd)
93{
94 __kvm_flush_dcache_pmd(pmd);
95}
96
97static void kvm_flush_dcache_pud(pud_t pud)
98{
99 __kvm_flush_dcache_pud(pud);
100}
101
Ard Biesheuvel959cad32015-11-10 15:11:20 +0100102static bool kvm_is_device_pfn(unsigned long pfn)
103{
104 return !pfn_valid(pfn);
105}
106
Mario Smarduch15a49a42015-01-15 15:58:58 -0800107/**
108 * stage2_dissolve_pmd() - clear and flush huge PMD entry
109 * @kvm: pointer to kvm structure.
110 * @addr: IPA
111 * @pmd: pmd pointer for IPA
112 *
113 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
114 * pages in the range dirty.
115 */
116static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
117{
118 if (!kvm_pmd_huge(*pmd))
119 return;
120
121 pmd_clear(pmd);
122 kvm_tlb_flush_vmid_ipa(kvm, addr);
123 put_page(virt_to_page(pmd));
124}
125
Christoffer Dalld5d81842013-01-20 18:28:07 -0500126static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
127 int min, int max)
128{
129 void *page;
130
131 BUG_ON(max > KVM_NR_MEM_OBJS);
132 if (cache->nobjs >= min)
133 return 0;
134 while (cache->nobjs < max) {
135 page = (void *)__get_free_page(PGALLOC_GFP);
136 if (!page)
137 return -ENOMEM;
138 cache->objects[cache->nobjs++] = page;
139 }
140 return 0;
141}
142
143static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
144{
145 while (mc->nobjs)
146 free_page((unsigned long)mc->objects[--mc->nobjs]);
147}
148
149static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
150{
151 void *p;
152
153 BUG_ON(!mc || !mc->nobjs);
154 p = mc->objects[--mc->nobjs];
155 return p;
156}
157
Christoffer Dall4f853a72014-05-09 23:31:31 +0200158static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100159{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200160 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
161 pgd_clear(pgd);
162 kvm_tlb_flush_vmid_ipa(kvm, addr);
163 pud_free(NULL, pud_table);
164 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100165}
166
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100167static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500168{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200169 pmd_t *pmd_table = pmd_offset(pud, 0);
170 VM_BUG_ON(pud_huge(*pud));
171 pud_clear(pud);
172 kvm_tlb_flush_vmid_ipa(kvm, addr);
173 pmd_free(NULL, pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100174 put_page(virt_to_page(pud));
175}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500176
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100177static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100178{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200179 pte_t *pte_table = pte_offset_kernel(pmd, 0);
180 VM_BUG_ON(kvm_pmd_huge(*pmd));
181 pmd_clear(pmd);
182 kvm_tlb_flush_vmid_ipa(kvm, addr);
183 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100184 put_page(virt_to_page(pmd));
185}
186
Marc Zyngier363ef892014-12-19 16:48:06 +0000187/*
188 * Unmapping vs dcache management:
189 *
190 * If a guest maps certain memory pages as uncached, all writes will
191 * bypass the data cache and go directly to RAM. However, the CPUs
192 * can still speculate reads (not writes) and fill cache lines with
193 * data.
194 *
195 * Those cache lines will be *clean* cache lines though, so a
196 * clean+invalidate operation is equivalent to an invalidate
197 * operation, because no cache lines are marked dirty.
198 *
199 * Those clean cache lines could be filled prior to an uncached write
200 * by the guest, and the cache coherent IO subsystem would therefore
201 * end up writing old data to disk.
202 *
203 * This is why right after unmapping a page/section and invalidating
204 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
205 * the IO subsystem will never hit in the cache.
206 */
Christoffer Dall4f853a72014-05-09 23:31:31 +0200207static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
208 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100209{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200210 phys_addr_t start_addr = addr;
211 pte_t *pte, *start_pte;
212
213 start_pte = pte = pte_offset_kernel(pmd, addr);
214 do {
215 if (!pte_none(*pte)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000216 pte_t old_pte = *pte;
217
Christoffer Dall4f853a72014-05-09 23:31:31 +0200218 kvm_set_pte(pte, __pte(0));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200219 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000220
221 /* No need to invalidate the cache for device mappings */
Ard Biesheuvel426bfb62015-12-03 09:25:22 +0100222 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000223 kvm_flush_dcache_pte(old_pte);
224
225 put_page(virt_to_page(pte));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200226 }
227 } while (pte++, addr += PAGE_SIZE, addr != end);
228
Christoffer Dall38f791a2014-10-10 12:14:28 +0200229 if (kvm_pte_table_empty(kvm, start_pte))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200230 clear_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500231}
232
Christoffer Dall4f853a72014-05-09 23:31:31 +0200233static void unmap_pmds(struct kvm *kvm, pud_t *pud,
234 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500235{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200236 phys_addr_t next, start_addr = addr;
237 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000238
Christoffer Dall4f853a72014-05-09 23:31:31 +0200239 start_pmd = pmd = pmd_offset(pud, addr);
240 do {
241 next = kvm_pmd_addr_end(addr, end);
242 if (!pmd_none(*pmd)) {
243 if (kvm_pmd_huge(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000244 pmd_t old_pmd = *pmd;
245
Christoffer Dall4f853a72014-05-09 23:31:31 +0200246 pmd_clear(pmd);
247 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000248
249 kvm_flush_dcache_pmd(old_pmd);
250
Christoffer Dall4f853a72014-05-09 23:31:31 +0200251 put_page(virt_to_page(pmd));
252 } else {
253 unmap_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100254 }
255 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200256 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100257
Christoffer Dall38f791a2014-10-10 12:14:28 +0200258 if (kvm_pmd_table_empty(kvm, start_pmd))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200259 clear_pud_entry(kvm, pud, start_addr);
260}
261
262static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
263 phys_addr_t addr, phys_addr_t end)
264{
265 phys_addr_t next, start_addr = addr;
266 pud_t *pud, *start_pud;
267
268 start_pud = pud = pud_offset(pgd, addr);
269 do {
270 next = kvm_pud_addr_end(addr, end);
271 if (!pud_none(*pud)) {
272 if (pud_huge(*pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000273 pud_t old_pud = *pud;
274
Christoffer Dall4f853a72014-05-09 23:31:31 +0200275 pud_clear(pud);
276 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000277
278 kvm_flush_dcache_pud(old_pud);
279
Christoffer Dall4f853a72014-05-09 23:31:31 +0200280 put_page(virt_to_page(pud));
281 } else {
282 unmap_pmds(kvm, pud, addr, next);
283 }
284 }
285 } while (pud++, addr = next, addr != end);
286
Christoffer Dall38f791a2014-10-10 12:14:28 +0200287 if (kvm_pud_table_empty(kvm, start_pud))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200288 clear_pgd_entry(kvm, pgd, start_addr);
289}
290
291
292static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
293 phys_addr_t start, u64 size)
294{
295 pgd_t *pgd;
296 phys_addr_t addr = start, end = start + size;
297 phys_addr_t next;
298
Marc Zyngier04b8dc82015-03-10 19:07:00 +0000299 pgd = pgdp + kvm_pgd_index(addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200300 do {
301 next = kvm_pgd_addr_end(addr, end);
Mark Rutland7cbb87d2014-10-28 19:36:45 +0000302 if (!pgd_none(*pgd))
303 unmap_puds(kvm, pgd, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200304 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000305}
306
Marc Zyngier9d218a12014-01-15 12:50:23 +0000307static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
308 phys_addr_t addr, phys_addr_t end)
309{
310 pte_t *pte;
311
312 pte = pte_offset_kernel(pmd, addr);
313 do {
Ard Biesheuvel426bfb62015-12-03 09:25:22 +0100314 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000315 kvm_flush_dcache_pte(*pte);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000316 } while (pte++, addr += PAGE_SIZE, addr != end);
317}
318
319static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
320 phys_addr_t addr, phys_addr_t end)
321{
322 pmd_t *pmd;
323 phys_addr_t next;
324
325 pmd = pmd_offset(pud, addr);
326 do {
327 next = kvm_pmd_addr_end(addr, end);
328 if (!pmd_none(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000329 if (kvm_pmd_huge(*pmd))
330 kvm_flush_dcache_pmd(*pmd);
331 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000332 stage2_flush_ptes(kvm, pmd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000333 }
334 } while (pmd++, addr = next, addr != end);
335}
336
337static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
338 phys_addr_t addr, phys_addr_t end)
339{
340 pud_t *pud;
341 phys_addr_t next;
342
343 pud = pud_offset(pgd, addr);
344 do {
345 next = kvm_pud_addr_end(addr, end);
346 if (!pud_none(*pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000347 if (pud_huge(*pud))
348 kvm_flush_dcache_pud(*pud);
349 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000350 stage2_flush_pmds(kvm, pud, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000351 }
352 } while (pud++, addr = next, addr != end);
353}
354
355static void stage2_flush_memslot(struct kvm *kvm,
356 struct kvm_memory_slot *memslot)
357{
358 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
359 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
360 phys_addr_t next;
361 pgd_t *pgd;
362
Marc Zyngier04b8dc82015-03-10 19:07:00 +0000363 pgd = kvm->arch.pgd + kvm_pgd_index(addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000364 do {
365 next = kvm_pgd_addr_end(addr, end);
366 stage2_flush_puds(kvm, pgd, addr, next);
367 } while (pgd++, addr = next, addr != end);
368}
369
370/**
371 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
372 * @kvm: The struct kvm pointer
373 *
374 * Go through the stage 2 page tables and invalidate any cache lines
375 * backing memory already mapped to the VM.
376 */
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000377static void stage2_flush_vm(struct kvm *kvm)
Marc Zyngier9d218a12014-01-15 12:50:23 +0000378{
379 struct kvm_memslots *slots;
380 struct kvm_memory_slot *memslot;
381 int idx;
382
383 idx = srcu_read_lock(&kvm->srcu);
384 spin_lock(&kvm->mmu_lock);
385
386 slots = kvm_memslots(kvm);
387 kvm_for_each_memslot(memslot, slots)
388 stage2_flush_memslot(kvm, memslot);
389
390 spin_unlock(&kvm->mmu_lock);
391 srcu_read_unlock(&kvm->srcu, idx);
392}
393
Marc Zyngier000d3992013-03-05 02:43:17 +0000394/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100395 * free_boot_hyp_pgd - free HYP boot page tables
396 *
397 * Free the HYP boot page tables. The bounce page is also freed.
398 */
399void free_boot_hyp_pgd(void)
400{
401 mutex_lock(&kvm_hyp_pgd_mutex);
402
403 if (boot_hyp_pgd) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100404 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
405 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200406 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100407 boot_hyp_pgd = NULL;
408 }
409
410 if (hyp_pgd)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100411 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100412
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100413 mutex_unlock(&kvm_hyp_pgd_mutex);
414}
415
416/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100417 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000418 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100419 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
420 * therefore contains either mappings in the kernel memory area (above
421 * PAGE_OFFSET), or device mappings in the vmalloc range (from
422 * VMALLOC_START to VMALLOC_END).
423 *
424 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000425 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100426void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000427{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500428 unsigned long addr;
429
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100430 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100431
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100432 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100433
Marc Zyngier4f728272013-04-12 19:12:05 +0100434 if (hyp_pgd) {
435 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100436 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100437 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100438 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
439
Christoffer Dall38f791a2014-10-10 12:14:28 +0200440 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100441 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100442 }
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000443 if (merged_hyp_pgd) {
444 clear_page(merged_hyp_pgd);
445 free_page((unsigned long)merged_hyp_pgd);
446 merged_hyp_pgd = NULL;
447 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100448
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500449 mutex_unlock(&kvm_hyp_pgd_mutex);
450}
451
452static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100453 unsigned long end, unsigned long pfn,
454 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500455{
456 pte_t *pte;
457 unsigned long addr;
458
Marc Zyngier3562c762013-04-12 19:12:02 +0100459 addr = start;
460 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100461 pte = pte_offset_kernel(pmd, addr);
462 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100463 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100464 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100465 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100466 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500467}
468
469static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100470 unsigned long end, unsigned long pfn,
471 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500472{
473 pmd_t *pmd;
474 pte_t *pte;
475 unsigned long addr, next;
476
Marc Zyngier3562c762013-04-12 19:12:02 +0100477 addr = start;
478 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100479 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500480
481 BUG_ON(pmd_sect(*pmd));
482
483 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100484 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500485 if (!pte) {
486 kvm_err("Cannot allocate Hyp pte\n");
487 return -ENOMEM;
488 }
489 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100490 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100491 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500492 }
493
494 next = pmd_addr_end(addr, end);
495
Marc Zyngier6060df82013-04-12 19:12:01 +0100496 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
497 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100498 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500499
500 return 0;
501}
502
Christoffer Dall38f791a2014-10-10 12:14:28 +0200503static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
504 unsigned long end, unsigned long pfn,
505 pgprot_t prot)
506{
507 pud_t *pud;
508 pmd_t *pmd;
509 unsigned long addr, next;
510 int ret;
511
512 addr = start;
513 do {
514 pud = pud_offset(pgd, addr);
515
516 if (pud_none_or_clear_bad(pud)) {
517 pmd = pmd_alloc_one(NULL, addr);
518 if (!pmd) {
519 kvm_err("Cannot allocate Hyp pmd\n");
520 return -ENOMEM;
521 }
522 pud_populate(NULL, pud, pmd);
523 get_page(virt_to_page(pud));
524 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
525 }
526
527 next = pud_addr_end(addr, end);
528 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
529 if (ret)
530 return ret;
531 pfn += (next - addr) >> PAGE_SHIFT;
532 } while (addr = next, addr != end);
533
534 return 0;
535}
536
Marc Zyngier6060df82013-04-12 19:12:01 +0100537static int __create_hyp_mappings(pgd_t *pgdp,
538 unsigned long start, unsigned long end,
539 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500540{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500541 pgd_t *pgd;
542 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500543 unsigned long addr, next;
544 int err = 0;
545
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500546 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100547 addr = start & PAGE_MASK;
548 end = PAGE_ALIGN(end);
549 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100550 pgd = pgdp + pgd_index(addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500551
Christoffer Dall38f791a2014-10-10 12:14:28 +0200552 if (pgd_none(*pgd)) {
553 pud = pud_alloc_one(NULL, addr);
554 if (!pud) {
555 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500556 err = -ENOMEM;
557 goto out;
558 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200559 pgd_populate(NULL, pgd, pud);
560 get_page(virt_to_page(pgd));
561 kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500562 }
563
564 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200565 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500566 if (err)
567 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100568 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100569 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500570out:
571 mutex_unlock(&kvm_hyp_pgd_mutex);
572 return err;
573}
574
Christoffer Dall40c27292013-11-15 13:14:12 -0800575static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
576{
577 if (!is_vmalloc_addr(kaddr)) {
578 BUG_ON(!virt_addr_valid(kaddr));
579 return __pa(kaddr);
580 } else {
581 return page_to_phys(vmalloc_to_page(kaddr)) +
582 offset_in_page(kaddr);
583 }
584}
585
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500586/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100587 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500588 * @from: The virtual kernel start address of the range
589 * @to: The virtual kernel end address of the range (exclusive)
590 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100591 * The same virtual address as the kernel virtual address is also used
592 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
593 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500594 */
595int create_hyp_mappings(void *from, void *to)
596{
Christoffer Dall40c27292013-11-15 13:14:12 -0800597 phys_addr_t phys_addr;
598 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100599 unsigned long start = KERN_TO_HYP((unsigned long)from);
600 unsigned long end = KERN_TO_HYP((unsigned long)to);
601
Marc Zyngier5aa75c82015-01-29 11:59:54 +0000602 if (is_kernel_in_hyp_mode())
603 return 0;
604
Christoffer Dall40c27292013-11-15 13:14:12 -0800605 start = start & PAGE_MASK;
606 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100607
Christoffer Dall40c27292013-11-15 13:14:12 -0800608 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
609 int err;
610
611 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
612 err = __create_hyp_mappings(hyp_pgd, virt_addr,
613 virt_addr + PAGE_SIZE,
614 __phys_to_pfn(phys_addr),
615 PAGE_HYP);
616 if (err)
617 return err;
618 }
619
620 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500621}
622
623/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100624 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
625 * @from: The kernel start VA of the range
626 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100627 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100628 *
629 * The resulting HYP VA is the same as the kernel VA, modulo
630 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500631 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100632int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500633{
Marc Zyngier6060df82013-04-12 19:12:01 +0100634 unsigned long start = KERN_TO_HYP((unsigned long)from);
635 unsigned long end = KERN_TO_HYP((unsigned long)to);
636
Marc Zyngier5aa75c82015-01-29 11:59:54 +0000637 if (is_kernel_in_hyp_mode())
638 return 0;
639
Marc Zyngier6060df82013-04-12 19:12:01 +0100640 /* Check for a valid kernel IO mapping */
641 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
642 return -EINVAL;
643
644 return __create_hyp_mappings(hyp_pgd, start, end,
645 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500646}
647
Marc Zyngiera9873702015-03-10 19:06:59 +0000648/* Free the HW pgd, one page at a time */
649static void kvm_free_hwpgd(void *hwpgd)
650{
651 free_pages_exact(hwpgd, kvm_get_hwpgd_size());
652}
653
654/* Allocate the HW PGD, making sure that each page gets its own refcount */
655static void *kvm_alloc_hwpgd(void)
656{
657 unsigned int size = kvm_get_hwpgd_size();
658
659 return alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
660}
661
Christoffer Dalld5d81842013-01-20 18:28:07 -0500662/**
663 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
664 * @kvm: The KVM struct pointer for the VM.
665 *
Vladimir Murzin30494392015-11-16 11:28:16 +0000666 * Allocates only the stage-2 HW PGD level table(s) (can support either full
667 * 40-bit input addresses or limited to 32-bit input addresses). Clears the
668 * allocated pages.
Christoffer Dalld5d81842013-01-20 18:28:07 -0500669 *
670 * Note we don't need locking here as this is only called when the VM is
671 * created, which can only be done once.
672 */
673int kvm_alloc_stage2_pgd(struct kvm *kvm)
674{
675 pgd_t *pgd;
Marc Zyngiera9873702015-03-10 19:06:59 +0000676 void *hwpgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500677
678 if (kvm->arch.pgd != NULL) {
679 kvm_err("kvm_arch already initialized?\n");
680 return -EINVAL;
681 }
682
Marc Zyngiera9873702015-03-10 19:06:59 +0000683 hwpgd = kvm_alloc_hwpgd();
684 if (!hwpgd)
685 return -ENOMEM;
686
687 /* When the kernel uses more levels of page tables than the
688 * guest, we allocate a fake PGD and pre-populate it to point
689 * to the next-level page table, which will be the real
690 * initial page table pointed to by the VTTBR.
691 *
692 * When KVM_PREALLOC_LEVEL==2, we allocate a single page for
693 * the PMD and the kernel will use folded pud.
694 * When KVM_PREALLOC_LEVEL==1, we allocate 2 consecutive PUD
695 * pages.
696 */
Christoffer Dall38f791a2014-10-10 12:14:28 +0200697 if (KVM_PREALLOC_LEVEL > 0) {
Marc Zyngiera9873702015-03-10 19:06:59 +0000698 int i;
699
Christoffer Dall38f791a2014-10-10 12:14:28 +0200700 /*
701 * Allocate fake pgd for the page table manipulation macros to
702 * work. This is not used by the hardware and we have no
703 * alignment requirement for this allocation.
704 */
705 pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
706 GFP_KERNEL | __GFP_ZERO);
Marc Zyngiera9873702015-03-10 19:06:59 +0000707
708 if (!pgd) {
709 kvm_free_hwpgd(hwpgd);
710 return -ENOMEM;
711 }
712
713 /* Plug the HW PGD into the fake one. */
714 for (i = 0; i < PTRS_PER_S2_PGD; i++) {
715 if (KVM_PREALLOC_LEVEL == 1)
716 pgd_populate(NULL, pgd + i,
717 (pud_t *)hwpgd + i * PTRS_PER_PUD);
718 else if (KVM_PREALLOC_LEVEL == 2)
719 pud_populate(NULL, pud_offset(pgd, 0) + i,
720 (pmd_t *)hwpgd + i * PTRS_PER_PMD);
721 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200722 } else {
723 /*
724 * Allocate actual first-level Stage-2 page table used by the
725 * hardware for Stage-2 page table walks.
726 */
Marc Zyngiera9873702015-03-10 19:06:59 +0000727 pgd = (pgd_t *)hwpgd;
Christoffer Dall38f791a2014-10-10 12:14:28 +0200728 }
729
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100730 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500731 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500732 return 0;
733}
734
Christoffer Dalld5d81842013-01-20 18:28:07 -0500735/**
736 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
737 * @kvm: The VM pointer
738 * @start: The intermediate physical base address of the range to unmap
739 * @size: The size of the area to unmap
740 *
741 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
742 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
743 * destroying the VM), otherwise another faulting VCPU may come in and mess
744 * with things behind our backs.
745 */
746static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
747{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100748 unmap_range(kvm, kvm->arch.pgd, start, size);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500749}
750
Christoffer Dall957db102014-11-27 10:35:03 +0100751static void stage2_unmap_memslot(struct kvm *kvm,
752 struct kvm_memory_slot *memslot)
753{
754 hva_t hva = memslot->userspace_addr;
755 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
756 phys_addr_t size = PAGE_SIZE * memslot->npages;
757 hva_t reg_end = hva + size;
758
759 /*
760 * A memory region could potentially cover multiple VMAs, and any holes
761 * between them, so iterate over all of them to find out if we should
762 * unmap any of them.
763 *
764 * +--------------------------------------------+
765 * +---------------+----------------+ +----------------+
766 * | : VMA 1 | VMA 2 | | VMA 3 : |
767 * +---------------+----------------+ +----------------+
768 * | memory region |
769 * +--------------------------------------------+
770 */
771 do {
772 struct vm_area_struct *vma = find_vma(current->mm, hva);
773 hva_t vm_start, vm_end;
774
775 if (!vma || vma->vm_start >= reg_end)
776 break;
777
778 /*
779 * Take the intersection of this VMA with the memory region
780 */
781 vm_start = max(hva, vma->vm_start);
782 vm_end = min(reg_end, vma->vm_end);
783
784 if (!(vma->vm_flags & VM_PFNMAP)) {
785 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
786 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
787 }
788 hva = vm_end;
789 } while (hva < reg_end);
790}
791
792/**
793 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
794 * @kvm: The struct kvm pointer
795 *
796 * Go through the memregions and unmap any reguler RAM
797 * backing memory already mapped to the VM.
798 */
799void stage2_unmap_vm(struct kvm *kvm)
800{
801 struct kvm_memslots *slots;
802 struct kvm_memory_slot *memslot;
803 int idx;
804
805 idx = srcu_read_lock(&kvm->srcu);
806 spin_lock(&kvm->mmu_lock);
807
808 slots = kvm_memslots(kvm);
809 kvm_for_each_memslot(memslot, slots)
810 stage2_unmap_memslot(kvm, memslot);
811
812 spin_unlock(&kvm->mmu_lock);
813 srcu_read_unlock(&kvm->srcu, idx);
814}
815
Christoffer Dalld5d81842013-01-20 18:28:07 -0500816/**
817 * kvm_free_stage2_pgd - free all stage-2 tables
818 * @kvm: The KVM struct pointer for the VM.
819 *
820 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
821 * underlying level-2 and level-3 tables before freeing the actual level-1 table
822 * and setting the struct pointer to NULL.
823 *
824 * Note we don't need locking here as this is only called when the VM is
825 * destroyed, which can only be done once.
826 */
827void kvm_free_stage2_pgd(struct kvm *kvm)
828{
829 if (kvm->arch.pgd == NULL)
830 return;
831
832 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
Marc Zyngiera9873702015-03-10 19:06:59 +0000833 kvm_free_hwpgd(kvm_get_hwpgd(kvm));
Christoffer Dall38f791a2014-10-10 12:14:28 +0200834 if (KVM_PREALLOC_LEVEL > 0)
835 kfree(kvm->arch.pgd);
Marc Zyngiera9873702015-03-10 19:06:59 +0000836
Christoffer Dalld5d81842013-01-20 18:28:07 -0500837 kvm->arch.pgd = NULL;
838}
839
Christoffer Dall38f791a2014-10-10 12:14:28 +0200840static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
841 phys_addr_t addr)
842{
843 pgd_t *pgd;
844 pud_t *pud;
845
Marc Zyngier04b8dc82015-03-10 19:07:00 +0000846 pgd = kvm->arch.pgd + kvm_pgd_index(addr);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200847 if (WARN_ON(pgd_none(*pgd))) {
848 if (!cache)
849 return NULL;
850 pud = mmu_memory_cache_alloc(cache);
851 pgd_populate(NULL, pgd, pud);
852 get_page(virt_to_page(pgd));
853 }
854
855 return pud_offset(pgd, addr);
856}
857
Christoffer Dallad361f02012-11-01 17:14:45 +0100858static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
859 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500860{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500861 pud_t *pud;
862 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500863
Christoffer Dall38f791a2014-10-10 12:14:28 +0200864 pud = stage2_get_pud(kvm, cache, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500865 if (pud_none(*pud)) {
866 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100867 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500868 pmd = mmu_memory_cache_alloc(cache);
869 pud_populate(NULL, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500870 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100871 }
872
Christoffer Dallad361f02012-11-01 17:14:45 +0100873 return pmd_offset(pud, addr);
874}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500875
Christoffer Dallad361f02012-11-01 17:14:45 +0100876static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
877 *cache, phys_addr_t addr, const pmd_t *new_pmd)
878{
879 pmd_t *pmd, old_pmd;
880
881 pmd = stage2_get_pmd(kvm, cache, addr);
882 VM_BUG_ON(!pmd);
883
884 /*
885 * Mapping in huge pages should only happen through a fault. If a
886 * page is merged into a transparent huge page, the individual
887 * subpages of that huge page should be unmapped through MMU
888 * notifiers before we get here.
889 *
890 * Merging of CompoundPages is not supported; they should become
891 * splitting first, unmapped, merged, and mapped back in on-demand.
892 */
893 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
894
895 old_pmd = *pmd;
896 kvm_set_pmd(pmd, *new_pmd);
897 if (pmd_present(old_pmd))
898 kvm_tlb_flush_vmid_ipa(kvm, addr);
899 else
900 get_page(virt_to_page(pmd));
901 return 0;
902}
903
904static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
Mario Smarduch15a49a42015-01-15 15:58:58 -0800905 phys_addr_t addr, const pte_t *new_pte,
906 unsigned long flags)
Christoffer Dallad361f02012-11-01 17:14:45 +0100907{
908 pmd_t *pmd;
909 pte_t *pte, old_pte;
Mario Smarduch15a49a42015-01-15 15:58:58 -0800910 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
911 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
912
913 VM_BUG_ON(logging_active && !cache);
Christoffer Dallad361f02012-11-01 17:14:45 +0100914
Christoffer Dall38f791a2014-10-10 12:14:28 +0200915 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +0100916 pmd = stage2_get_pmd(kvm, cache, addr);
917 if (!pmd) {
918 /*
919 * Ignore calls from kvm_set_spte_hva for unallocated
920 * address ranges.
921 */
922 return 0;
923 }
924
Mario Smarduch15a49a42015-01-15 15:58:58 -0800925 /*
926 * While dirty page logging - dissolve huge PMD, then continue on to
927 * allocate page.
928 */
929 if (logging_active)
930 stage2_dissolve_pmd(kvm, addr, pmd);
931
Christoffer Dallad361f02012-11-01 17:14:45 +0100932 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500933 if (pmd_none(*pmd)) {
934 if (!cache)
935 return 0; /* ignore calls from kvm_set_spte_hva */
936 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100937 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500938 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500939 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100940 }
941
942 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500943
944 if (iomap && pte_present(*pte))
945 return -EFAULT;
946
947 /* Create 2nd stage page table mapping - Level 3 */
948 old_pte = *pte;
949 kvm_set_pte(pte, *new_pte);
950 if (pte_present(old_pte))
Marc Zyngier48762762013-01-28 15:27:00 +0000951 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500952 else
953 get_page(virt_to_page(pte));
954
955 return 0;
956}
957
958/**
959 * kvm_phys_addr_ioremap - map a device range to guest IPA
960 *
961 * @kvm: The KVM pointer
962 * @guest_ipa: The IPA at which to insert the mapping
963 * @pa: The physical address of the device
964 * @size: The size of the mapping
965 */
966int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700967 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500968{
969 phys_addr_t addr, end;
970 int ret = 0;
971 unsigned long pfn;
972 struct kvm_mmu_memory_cache cache = { 0, };
973
974 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
975 pfn = __phys_to_pfn(pa);
976
977 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100978 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500979
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700980 if (writable)
981 kvm_set_s2pte_writable(&pte);
982
Christoffer Dall38f791a2014-10-10 12:14:28 +0200983 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
984 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500985 if (ret)
986 goto out;
987 spin_lock(&kvm->mmu_lock);
Mario Smarduch15a49a42015-01-15 15:58:58 -0800988 ret = stage2_set_pte(kvm, &cache, addr, &pte,
989 KVM_S2PTE_FLAG_IS_IOMAP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500990 spin_unlock(&kvm->mmu_lock);
991 if (ret)
992 goto out;
993
994 pfn++;
995 }
996
997out:
998 mmu_free_memory_cache(&cache);
999 return ret;
1000}
1001
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001002static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
1003{
1004 pfn_t pfn = *pfnp;
1005 gfn_t gfn = *ipap >> PAGE_SHIFT;
1006
1007 if (PageTransCompound(pfn_to_page(pfn))) {
1008 unsigned long mask;
1009 /*
1010 * The address we faulted on is backed by a transparent huge
1011 * page. However, because we map the compound huge page and
1012 * not the individual tail page, we need to transfer the
1013 * refcount to the head page. We have to be careful that the
1014 * THP doesn't start to split while we are adjusting the
1015 * refcounts.
1016 *
1017 * We are sure this doesn't happen, because mmu_notifier_retry
1018 * was successful and we are holding the mmu_lock, so if this
1019 * THP is trying to split, it will be blocked in the mmu
1020 * notifier before touching any of the pages, specifically
1021 * before being able to call __split_huge_page_refcount().
1022 *
1023 * We can therefore safely transfer the refcount from PG_tail
1024 * to PG_head and switch the pfn from a tail page to the head
1025 * page accordingly.
1026 */
1027 mask = PTRS_PER_PMD - 1;
1028 VM_BUG_ON((gfn & mask) != (pfn & mask));
1029 if (pfn & mask) {
1030 *ipap &= PMD_MASK;
1031 kvm_release_pfn_clean(pfn);
1032 pfn &= ~mask;
1033 kvm_get_pfn(pfn);
1034 *pfnp = pfn;
1035 }
1036
1037 return true;
1038 }
1039
1040 return false;
1041}
1042
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001043static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
1044{
1045 if (kvm_vcpu_trap_is_iabt(vcpu))
1046 return false;
1047
1048 return kvm_vcpu_dabt_iswrite(vcpu);
1049}
1050
Mario Smarduchc6473552015-01-15 15:58:56 -08001051/**
1052 * stage2_wp_ptes - write protect PMD range
1053 * @pmd: pointer to pmd entry
1054 * @addr: range start address
1055 * @end: range end address
1056 */
1057static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1058{
1059 pte_t *pte;
1060
1061 pte = pte_offset_kernel(pmd, addr);
1062 do {
1063 if (!pte_none(*pte)) {
1064 if (!kvm_s2pte_readonly(pte))
1065 kvm_set_s2pte_readonly(pte);
1066 }
1067 } while (pte++, addr += PAGE_SIZE, addr != end);
1068}
1069
1070/**
1071 * stage2_wp_pmds - write protect PUD range
1072 * @pud: pointer to pud entry
1073 * @addr: range start address
1074 * @end: range end address
1075 */
1076static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
1077{
1078 pmd_t *pmd;
1079 phys_addr_t next;
1080
1081 pmd = pmd_offset(pud, addr);
1082
1083 do {
1084 next = kvm_pmd_addr_end(addr, end);
1085 if (!pmd_none(*pmd)) {
1086 if (kvm_pmd_huge(*pmd)) {
1087 if (!kvm_s2pmd_readonly(pmd))
1088 kvm_set_s2pmd_readonly(pmd);
1089 } else {
1090 stage2_wp_ptes(pmd, addr, next);
1091 }
1092 }
1093 } while (pmd++, addr = next, addr != end);
1094}
1095
1096/**
1097 * stage2_wp_puds - write protect PGD range
1098 * @pgd: pointer to pgd entry
1099 * @addr: range start address
1100 * @end: range end address
1101 *
1102 * Process PUD entries, for a huge PUD we cause a panic.
1103 */
1104static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
1105{
1106 pud_t *pud;
1107 phys_addr_t next;
1108
1109 pud = pud_offset(pgd, addr);
1110 do {
1111 next = kvm_pud_addr_end(addr, end);
1112 if (!pud_none(*pud)) {
1113 /* TODO:PUD not supported, revisit later if supported */
1114 BUG_ON(kvm_pud_huge(*pud));
1115 stage2_wp_pmds(pud, addr, next);
1116 }
1117 } while (pud++, addr = next, addr != end);
1118}
1119
1120/**
1121 * stage2_wp_range() - write protect stage2 memory region range
1122 * @kvm: The KVM pointer
1123 * @addr: Start address of range
1124 * @end: End address of range
1125 */
1126static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1127{
1128 pgd_t *pgd;
1129 phys_addr_t next;
1130
Marc Zyngier04b8dc82015-03-10 19:07:00 +00001131 pgd = kvm->arch.pgd + kvm_pgd_index(addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001132 do {
1133 /*
1134 * Release kvm_mmu_lock periodically if the memory region is
1135 * large. Otherwise, we may see kernel panics with
Christoffer Dall227ea8182015-01-23 10:49:31 +01001136 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1137 * CONFIG_LOCKDEP. Additionally, holding the lock too long
Mario Smarduchc6473552015-01-15 15:58:56 -08001138 * will also starve other vCPUs.
1139 */
1140 if (need_resched() || spin_needbreak(&kvm->mmu_lock))
1141 cond_resched_lock(&kvm->mmu_lock);
1142
1143 next = kvm_pgd_addr_end(addr, end);
1144 if (pgd_present(*pgd))
1145 stage2_wp_puds(pgd, addr, next);
1146 } while (pgd++, addr = next, addr != end);
1147}
1148
1149/**
1150 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1151 * @kvm: The KVM pointer
1152 * @slot: The memory slot to write protect
1153 *
1154 * Called to start logging dirty pages after memory region
1155 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1156 * all present PMD and PTEs are write protected in the memory region.
1157 * Afterwards read of dirty page log can be called.
1158 *
1159 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1160 * serializing operations for VM memory regions.
1161 */
1162void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1163{
1164 struct kvm_memory_slot *memslot = id_to_memslot(kvm->memslots, slot);
1165 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1166 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1167
1168 spin_lock(&kvm->mmu_lock);
1169 stage2_wp_range(kvm, start, end);
1170 spin_unlock(&kvm->mmu_lock);
1171 kvm_flush_remote_tlbs(kvm);
1172}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001173
1174/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001175 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
Mario Smarduch53c810c2015-01-15 15:58:57 -08001176 * @kvm: The KVM pointer
1177 * @slot: The memory slot associated with mask
1178 * @gfn_offset: The gfn offset in memory slot
1179 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1180 * slot to be write protected
1181 *
1182 * Walks bits set in mask write protects the associated pte's. Caller must
1183 * acquire kvm_mmu_lock.
1184 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001185static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Mario Smarduch53c810c2015-01-15 15:58:57 -08001186 struct kvm_memory_slot *slot,
1187 gfn_t gfn_offset, unsigned long mask)
1188{
1189 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1190 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1191 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1192
1193 stage2_wp_range(kvm, start, end);
1194}
Mario Smarduchc6473552015-01-15 15:58:56 -08001195
Kai Huang3b0f1d02015-01-28 10:54:23 +08001196/*
1197 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1198 * dirty pages.
1199 *
1200 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1201 * enable dirty logging for them.
1202 */
1203void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1204 struct kvm_memory_slot *slot,
1205 gfn_t gfn_offset, unsigned long mask)
1206{
1207 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1208}
1209
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001210static void coherent_cache_guest_page(struct kvm_vcpu *vcpu, pfn_t pfn,
1211 unsigned long size, bool uncached)
1212{
1213 __coherent_cache_guest_page(vcpu, pfn, size, uncached);
1214}
1215
Christoffer Dall94f8e642013-01-20 18:28:12 -05001216static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001217 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001218 unsigned long fault_status)
1219{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001220 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001221 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001222 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001223 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001224 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001225 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001226 struct vm_area_struct *vma;
1227 pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001228 pgprot_t mem_type = PAGE_S2;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001229 bool fault_ipa_uncached;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001230 bool logging_active = memslot_is_logging(memslot);
1231 unsigned long flags = 0;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001232
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001233 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001234 if (fault_status == FSC_PERM && !write_fault) {
1235 kvm_err("Unexpected L2 read permission error\n");
1236 return -EFAULT;
1237 }
1238
Christoffer Dallad361f02012-11-01 17:14:45 +01001239 /* Let's check if we will get back a huge page backed by hugetlbfs */
1240 down_read(&current->mm->mmap_sem);
1241 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001242 if (unlikely(!vma)) {
1243 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1244 up_read(&current->mm->mmap_sem);
1245 return -EFAULT;
1246 }
1247
Mario Smarduch15a49a42015-01-15 15:58:58 -08001248 if (is_vm_hugetlb_page(vma) && !logging_active) {
Christoffer Dallad361f02012-11-01 17:14:45 +01001249 hugetlb = true;
1250 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001251 } else {
1252 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001253 * Pages belonging to memslots that don't have the same
1254 * alignment for userspace and IPA cannot be mapped using
1255 * block descriptors even if the pages belong to a THP for
1256 * the process, because the stage-2 block descriptor will
1257 * cover more than a single THP and we loose atomicity for
1258 * unmapping, updates, and splits of the THP or other pages
1259 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001260 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001261 if ((memslot->userspace_addr & ~PMD_MASK) !=
1262 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001263 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001264 }
1265 up_read(&current->mm->mmap_sem);
1266
Christoffer Dall94f8e642013-01-20 18:28:12 -05001267 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001268 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1269 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001270 if (ret)
1271 return ret;
1272
1273 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1274 /*
1275 * Ensure the read of mmu_notifier_seq happens before we call
1276 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1277 * the page we just got a reference to gets unmapped before we have a
1278 * chance to grab the mmu_lock, which ensure that if the page gets
1279 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1280 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1281 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1282 */
1283 smp_rmb();
1284
Christoffer Dallad361f02012-11-01 17:14:45 +01001285 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001286 if (is_error_pfn(pfn))
1287 return -EFAULT;
1288
Mario Smarduch15a49a42015-01-15 15:58:58 -08001289 if (kvm_is_device_pfn(pfn)) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001290 mem_type = PAGE_S2_DEVICE;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001291 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1292 } else if (logging_active) {
1293 /*
1294 * Faults on pages in a memslot with logging enabled
1295 * should not be mapped with huge pages (it introduces churn
1296 * and performance degradation), so force a pte mapping.
1297 */
1298 force_pte = true;
1299 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1300
1301 /*
1302 * Only actually map the page as writable if this was a write
1303 * fault.
1304 */
1305 if (!write_fault)
1306 writable = false;
1307 }
Kim Phillipsb8865762014-06-26 01:45:51 +01001308
Christoffer Dallad361f02012-11-01 17:14:45 +01001309 spin_lock(&kvm->mmu_lock);
1310 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001311 goto out_unlock;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001312
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001313 if (!hugetlb && !force_pte)
1314 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001315
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001316 fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001317
Christoffer Dallad361f02012-11-01 17:14:45 +01001318 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001319 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001320 new_pmd = pmd_mkhuge(new_pmd);
1321 if (writable) {
1322 kvm_set_s2pmd_writable(&new_pmd);
1323 kvm_set_pfn_dirty(pfn);
1324 }
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001325 coherent_cache_guest_page(vcpu, pfn, PMD_SIZE, fault_ipa_uncached);
Christoffer Dallad361f02012-11-01 17:14:45 +01001326 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1327 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001328 pte_t new_pte = pfn_pte(pfn, mem_type);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001329
Christoffer Dallad361f02012-11-01 17:14:45 +01001330 if (writable) {
1331 kvm_set_s2pte_writable(&new_pte);
1332 kvm_set_pfn_dirty(pfn);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001333 mark_page_dirty(kvm, gfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001334 }
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001335 coherent_cache_guest_page(vcpu, pfn, PAGE_SIZE, fault_ipa_uncached);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001336 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001337 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001338
Christoffer Dall94f8e642013-01-20 18:28:12 -05001339out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001340 spin_unlock(&kvm->mmu_lock);
Marc Zyngier35307b92015-03-12 18:16:51 +00001341 kvm_set_pfn_accessed(pfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001342 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001343 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001344}
1345
Marc Zyngieraeda9132015-03-12 18:16:52 +00001346/*
1347 * Resolve the access fault by making the page young again.
1348 * Note that because the faulting entry is guaranteed not to be
1349 * cached in the TLB, we don't need to invalidate anything.
1350 */
1351static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1352{
1353 pmd_t *pmd;
1354 pte_t *pte;
1355 pfn_t pfn;
1356 bool pfn_valid = false;
1357
1358 trace_kvm_access_fault(fault_ipa);
1359
1360 spin_lock(&vcpu->kvm->mmu_lock);
1361
1362 pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
1363 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1364 goto out;
1365
1366 if (kvm_pmd_huge(*pmd)) { /* THP, HugeTLB */
1367 *pmd = pmd_mkyoung(*pmd);
1368 pfn = pmd_pfn(*pmd);
1369 pfn_valid = true;
1370 goto out;
1371 }
1372
1373 pte = pte_offset_kernel(pmd, fault_ipa);
1374 if (pte_none(*pte)) /* Nothing there either */
1375 goto out;
1376
1377 *pte = pte_mkyoung(*pte); /* Just a page... */
1378 pfn = pte_pfn(*pte);
1379 pfn_valid = true;
1380out:
1381 spin_unlock(&vcpu->kvm->mmu_lock);
1382 if (pfn_valid)
1383 kvm_set_pfn_accessed(pfn);
1384}
1385
Christoffer Dall94f8e642013-01-20 18:28:12 -05001386/**
1387 * kvm_handle_guest_abort - handles all 2nd stage aborts
1388 * @vcpu: the VCPU pointer
1389 * @run: the kvm_run structure
1390 *
1391 * Any abort that gets to the host is almost guaranteed to be caused by a
1392 * missing second stage translation table entry, which can mean that either the
1393 * guest simply needs more memory and we must allocate an appropriate page or it
1394 * can mean that the guest tried to access I/O memory, which is emulated by user
1395 * space. The distinction is based on the IPA causing the fault and whether this
1396 * memory region has been registered as standard RAM by user space.
1397 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001398int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1399{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001400 unsigned long fault_status;
1401 phys_addr_t fault_ipa;
1402 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001403 unsigned long hva;
1404 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001405 gfn_t gfn;
1406 int ret, idx;
1407
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001408 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +01001409 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001410
Marc Zyngier7393b592012-09-17 19:27:09 +01001411 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1412 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001413
1414 /* Check the stage-2 fault is trans. fault or write fault */
Christoffer Dall0496daa52014-09-26 12:29:34 +02001415 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
Marc Zyngier35307b92015-03-12 18:16:51 +00001416 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1417 fault_status != FSC_ACCESS) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001418 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1419 kvm_vcpu_trap_get_class(vcpu),
1420 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1421 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001422 return -EFAULT;
1423 }
1424
1425 idx = srcu_read_lock(&vcpu->kvm->srcu);
1426
1427 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001428 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1429 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001430 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001431 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001432 if (is_iabt) {
1433 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001434 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001435 ret = 1;
1436 goto out_unlock;
1437 }
1438
Marc Zyngiercfe39502012-12-12 14:42:09 +00001439 /*
1440 * The IPA is reported as [MAX:12], so we need to
1441 * complement it with the bottom 12 bits from the
1442 * faulting VA. This is always 12 bits, irrespective
1443 * of the page size.
1444 */
1445 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001446 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001447 goto out_unlock;
1448 }
1449
Christoffer Dallc3058d52014-10-10 12:14:29 +02001450 /* Userspace should not be able to register out-of-bounds IPAs */
1451 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1452
Marc Zyngieraeda9132015-03-12 18:16:52 +00001453 if (fault_status == FSC_ACCESS) {
1454 handle_access_fault(vcpu, fault_ipa);
1455 ret = 1;
1456 goto out_unlock;
1457 }
1458
Christoffer Dall98047882014-08-19 12:18:04 +02001459 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001460 if (ret == 0)
1461 ret = 1;
1462out_unlock:
1463 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1464 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001465}
1466
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001467static int handle_hva_to_gpa(struct kvm *kvm,
1468 unsigned long start,
1469 unsigned long end,
1470 int (*handler)(struct kvm *kvm,
1471 gpa_t gpa, void *data),
1472 void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001473{
1474 struct kvm_memslots *slots;
1475 struct kvm_memory_slot *memslot;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001476 int ret = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001477
1478 slots = kvm_memslots(kvm);
1479
1480 /* we only care about the pages that the guest sees */
1481 kvm_for_each_memslot(memslot, slots) {
1482 unsigned long hva_start, hva_end;
1483 gfn_t gfn, gfn_end;
1484
1485 hva_start = max(start, memslot->userspace_addr);
1486 hva_end = min(end, memslot->userspace_addr +
1487 (memslot->npages << PAGE_SHIFT));
1488 if (hva_start >= hva_end)
1489 continue;
1490
1491 /*
1492 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1493 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1494 */
1495 gfn = hva_to_gfn_memslot(hva_start, memslot);
1496 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
1497
1498 for (; gfn < gfn_end; ++gfn) {
1499 gpa_t gpa = gfn << PAGE_SHIFT;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001500 ret |= handler(kvm, gpa, data);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001501 }
1502 }
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001503
1504 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001505}
1506
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001507static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001508{
1509 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001510 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001511}
1512
1513int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1514{
1515 unsigned long end = hva + PAGE_SIZE;
1516
1517 if (!kvm->arch.pgd)
1518 return 0;
1519
1520 trace_kvm_unmap_hva(hva);
1521 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1522 return 0;
1523}
1524
1525int kvm_unmap_hva_range(struct kvm *kvm,
1526 unsigned long start, unsigned long end)
1527{
1528 if (!kvm->arch.pgd)
1529 return 0;
1530
1531 trace_kvm_unmap_hva_range(start, end);
1532 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1533 return 0;
1534}
1535
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001536static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001537{
1538 pte_t *pte = (pte_t *)data;
1539
Mario Smarduch15a49a42015-01-15 15:58:58 -08001540 /*
1541 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1542 * flag clear because MMU notifiers will have unmapped a huge PMD before
1543 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1544 * therefore stage2_set_pte() never needs to clear out a huge PMD
1545 * through this calling path.
1546 */
1547 stage2_set_pte(kvm, NULL, gpa, pte, 0);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001548 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001549}
1550
1551
1552void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1553{
1554 unsigned long end = hva + PAGE_SIZE;
1555 pte_t stage2_pte;
1556
1557 if (!kvm->arch.pgd)
1558 return;
1559
1560 trace_kvm_set_spte_hva(hva);
1561 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1562 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1563}
1564
Marc Zyngier35307b92015-03-12 18:16:51 +00001565static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1566{
1567 pmd_t *pmd;
1568 pte_t *pte;
1569
1570 pmd = stage2_get_pmd(kvm, NULL, gpa);
1571 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1572 return 0;
1573
1574 if (kvm_pmd_huge(*pmd)) { /* THP, HugeTLB */
1575 if (pmd_young(*pmd)) {
1576 *pmd = pmd_mkold(*pmd);
1577 return 1;
1578 }
1579
1580 return 0;
1581 }
1582
1583 pte = pte_offset_kernel(pmd, gpa);
1584 if (pte_none(*pte))
1585 return 0;
1586
1587 if (pte_young(*pte)) {
1588 *pte = pte_mkold(*pte); /* Just a page... */
1589 return 1;
1590 }
1591
1592 return 0;
1593}
1594
1595static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1596{
1597 pmd_t *pmd;
1598 pte_t *pte;
1599
1600 pmd = stage2_get_pmd(kvm, NULL, gpa);
1601 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1602 return 0;
1603
1604 if (kvm_pmd_huge(*pmd)) /* THP, HugeTLB */
1605 return pmd_young(*pmd);
1606
1607 pte = pte_offset_kernel(pmd, gpa);
1608 if (!pte_none(*pte)) /* Just a page... */
1609 return pte_young(*pte);
1610
1611 return 0;
1612}
1613
1614int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
1615{
1616 trace_kvm_age_hva(start, end);
1617 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
1618}
1619
1620int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1621{
1622 trace_kvm_test_age_hva(hva);
1623 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
1624}
1625
Christoffer Dalld5d81842013-01-20 18:28:07 -05001626void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1627{
1628 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1629}
1630
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001631phys_addr_t kvm_mmu_get_httbr(void)
1632{
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001633 if (__kvm_cpu_uses_extended_idmap())
1634 return virt_to_phys(merged_hyp_pgd);
1635 else
1636 return virt_to_phys(hyp_pgd);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001637}
1638
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001639phys_addr_t kvm_mmu_get_boot_httbr(void)
1640{
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001641 if (__kvm_cpu_uses_extended_idmap())
1642 return virt_to_phys(merged_hyp_pgd);
1643 else
1644 return virt_to_phys(boot_hyp_pgd);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001645}
1646
1647phys_addr_t kvm_get_idmap_vector(void)
1648{
1649 return hyp_idmap_vector;
1650}
1651
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001652phys_addr_t kvm_get_idmap_start(void)
1653{
1654 return hyp_idmap_start;
1655}
1656
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001657int kvm_mmu_init(void)
1658{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001659 int err;
1660
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001661 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1662 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1663 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001664
Ard Biesheuvel06f75a12015-03-19 16:42:26 +00001665 /*
1666 * We rely on the linker script to ensure at build time that the HYP
1667 * init code does not cross a page boundary.
1668 */
1669 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001670
Christoffer Dall38f791a2014-10-10 12:14:28 +02001671 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1672 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Mark Salter5d4e08c2014-03-28 14:25:19 +00001673
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001674 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001675 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001676 err = -ENOMEM;
1677 goto out;
1678 }
1679
1680 /* Create the idmap in the boot page tables */
1681 err = __create_hyp_mappings(boot_hyp_pgd,
1682 hyp_idmap_start, hyp_idmap_end,
1683 __phys_to_pfn(hyp_idmap_start),
1684 PAGE_HYP);
1685
1686 if (err) {
1687 kvm_err("Failed to idmap %lx-%lx\n",
1688 hyp_idmap_start, hyp_idmap_end);
1689 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001690 }
1691
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001692 if (__kvm_cpu_uses_extended_idmap()) {
1693 merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1694 if (!merged_hyp_pgd) {
1695 kvm_err("Failed to allocate extra HYP pgd\n");
1696 goto out;
1697 }
1698 __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
1699 hyp_idmap_start);
1700 return 0;
1701 }
1702
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001703 /* Map the very same page at the trampoline VA */
1704 err = __create_hyp_mappings(boot_hyp_pgd,
1705 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1706 __phys_to_pfn(hyp_idmap_start),
1707 PAGE_HYP);
1708 if (err) {
1709 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1710 TRAMPOLINE_VA);
1711 goto out;
1712 }
1713
1714 /* Map the same page again into the runtime page tables */
1715 err = __create_hyp_mappings(hyp_pgd,
1716 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1717 __phys_to_pfn(hyp_idmap_start),
1718 PAGE_HYP);
1719 if (err) {
1720 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1721 TRAMPOLINE_VA);
1722 goto out;
1723 }
1724
Christoffer Dalld5d81842013-01-20 18:28:07 -05001725 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001726out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001727 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001728 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001729}
Eric Augerdf6ce242014-06-06 11:10:23 +02001730
1731void kvm_arch_commit_memory_region(struct kvm *kvm,
1732 struct kvm_userspace_memory_region *mem,
1733 const struct kvm_memory_slot *old,
1734 enum kvm_mr_change change)
1735{
Mario Smarduchc6473552015-01-15 15:58:56 -08001736 /*
1737 * At this point memslot has been committed and there is an
1738 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1739 * memory slot is write protected.
1740 */
1741 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
1742 kvm_mmu_wp_memory_region(kvm, mem->slot);
Eric Augerdf6ce242014-06-06 11:10:23 +02001743}
1744
1745int kvm_arch_prepare_memory_region(struct kvm *kvm,
1746 struct kvm_memory_slot *memslot,
1747 struct kvm_userspace_memory_region *mem,
1748 enum kvm_mr_change change)
1749{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001750 hva_t hva = mem->userspace_addr;
1751 hva_t reg_end = hva + mem->memory_size;
1752 bool writable = !(mem->flags & KVM_MEM_READONLY);
1753 int ret = 0;
1754
Mario Smarduch15a49a42015-01-15 15:58:58 -08001755 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
1756 change != KVM_MR_FLAGS_ONLY)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001757 return 0;
1758
1759 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001760 * Prevent userspace from creating a memory region outside of the IPA
1761 * space addressable by the KVM guest IPA space.
1762 */
1763 if (memslot->base_gfn + memslot->npages >=
1764 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1765 return -EFAULT;
1766
1767 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001768 * A memory region could potentially cover multiple VMAs, and any holes
1769 * between them, so iterate over all of them to find out if we can map
1770 * any of them right now.
1771 *
1772 * +--------------------------------------------+
1773 * +---------------+----------------+ +----------------+
1774 * | : VMA 1 | VMA 2 | | VMA 3 : |
1775 * +---------------+----------------+ +----------------+
1776 * | memory region |
1777 * +--------------------------------------------+
1778 */
1779 do {
1780 struct vm_area_struct *vma = find_vma(current->mm, hva);
1781 hva_t vm_start, vm_end;
1782
1783 if (!vma || vma->vm_start >= reg_end)
1784 break;
1785
1786 /*
1787 * Mapping a read-only VMA is only allowed if the
1788 * memory region is configured as read-only.
1789 */
1790 if (writable && !(vma->vm_flags & VM_WRITE)) {
1791 ret = -EPERM;
1792 break;
1793 }
1794
1795 /*
1796 * Take the intersection of this VMA with the memory region
1797 */
1798 vm_start = max(hva, vma->vm_start);
1799 vm_end = min(reg_end, vma->vm_end);
1800
1801 if (vma->vm_flags & VM_PFNMAP) {
1802 gpa_t gpa = mem->guest_phys_addr +
1803 (vm_start - mem->userspace_addr);
Marek Majtyka10e259a2015-09-16 12:04:55 +02001804 phys_addr_t pa;
1805
1806 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
1807 pa += vm_start - vma->vm_start;
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001808
Mario Smarduch15a49a42015-01-15 15:58:58 -08001809 /* IO region dirty page logging not allowed */
1810 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
1811 return -EINVAL;
1812
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001813 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1814 vm_end - vm_start,
1815 writable);
1816 if (ret)
1817 break;
1818 }
1819 hva = vm_end;
1820 } while (hva < reg_end);
1821
Mario Smarduch15a49a42015-01-15 15:58:58 -08001822 if (change == KVM_MR_FLAGS_ONLY)
1823 return ret;
1824
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001825 spin_lock(&kvm->mmu_lock);
1826 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001827 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001828 else
1829 stage2_flush_memslot(kvm, memslot);
1830 spin_unlock(&kvm->mmu_lock);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001831 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001832}
1833
1834void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1835 struct kvm_memory_slot *dont)
1836{
1837}
1838
1839int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1840 unsigned long npages)
1841{
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001842 /*
1843 * Readonly memslots are not incoherent with the caches by definition,
1844 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1845 * that the guest may consider devices and hence map as uncached.
1846 * To prevent incoherency issues in these cases, tag all readonly
1847 * regions as incoherent.
1848 */
1849 if (slot->flags & KVM_MEM_READONLY)
1850 slot->flags |= KVM_MEMSLOT_INCOHERENT;
Eric Augerdf6ce242014-06-06 11:10:23 +02001851 return 0;
1852}
1853
1854void kvm_arch_memslots_updated(struct kvm *kvm)
1855{
1856}
1857
1858void kvm_arch_flush_shadow_all(struct kvm *kvm)
1859{
1860}
1861
1862void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1863 struct kvm_memory_slot *slot)
1864{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001865 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1866 phys_addr_t size = slot->npages << PAGE_SHIFT;
1867
1868 spin_lock(&kvm->mmu_lock);
1869 unmap_stage2_range(kvm, gpa, size);
1870 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001871}
Marc Zyngier3c1e7162014-12-19 16:05:31 +00001872
1873/*
1874 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1875 *
1876 * Main problems:
1877 * - S/W ops are local to a CPU (not broadcast)
1878 * - We have line migration behind our back (speculation)
1879 * - System caches don't support S/W at all (damn!)
1880 *
1881 * In the face of the above, the best we can do is to try and convert
1882 * S/W ops to VA ops. Because the guest is not allowed to infer the
1883 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1884 * which is a rather good thing for us.
1885 *
1886 * Also, it is only used when turning caches on/off ("The expected
1887 * usage of the cache maintenance instructions that operate by set/way
1888 * is associated with the cache maintenance instructions associated
1889 * with the powerdown and powerup of caches, if this is required by
1890 * the implementation.").
1891 *
1892 * We use the following policy:
1893 *
1894 * - If we trap a S/W operation, we enable VM trapping to detect
1895 * caches being turned on/off, and do a full clean.
1896 *
1897 * - We flush the caches on both caches being turned on and off.
1898 *
1899 * - Once the caches are enabled, we stop trapping VM ops.
1900 */
1901void kvm_set_way_flush(struct kvm_vcpu *vcpu)
1902{
1903 unsigned long hcr = vcpu_get_hcr(vcpu);
1904
1905 /*
1906 * If this is the first time we do a S/W operation
1907 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1908 * VM trapping.
1909 *
1910 * Otherwise, rely on the VM trapping to wait for the MMU +
1911 * Caches to be turned off. At that point, we'll be able to
1912 * clean the caches again.
1913 */
1914 if (!(hcr & HCR_TVM)) {
1915 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
1916 vcpu_has_cache_enabled(vcpu));
1917 stage2_flush_vm(vcpu->kvm);
1918 vcpu_set_hcr(vcpu, hcr | HCR_TVM);
1919 }
1920}
1921
1922void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
1923{
1924 bool now_enabled = vcpu_has_cache_enabled(vcpu);
1925
1926 /*
1927 * If switching the MMU+caches on, need to invalidate the caches.
1928 * If switching it off, need to clean the caches.
1929 * Clean + invalidate does the trick always.
1930 */
1931 if (now_enabled != was_enabled)
1932 stage2_flush_vm(vcpu->kvm);
1933
1934 /* Caches are now on, stop trapping VM ops (until a S/W op) */
1935 if (now_enabled)
1936 vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
1937
1938 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
1939}