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