blob: bf330b493c1e7be80308e2347e1d5c812a6cc46a [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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 */
18
19#include <linux/mman.h>
20#include <linux/kvm_host.h>
21#include <linux/io.h>
22#include <linux/hugetlb.h>
23#include <linux/sched/signal.h>
24#include <trace/events/kvm.h>
25#include <asm/pgalloc.h>
26#include <asm/cacheflush.h>
27#include <asm/kvm_arm.h>
28#include <asm/kvm_mmu.h>
29#include <asm/kvm_mmio.h>
30#include <asm/kvm_asm.h>
31#include <asm/kvm_emulate.h>
32#include <asm/virt.h>
33#include <asm/system_misc.h>
34
35#include "trace.h"
36
37static pgd_t *boot_hyp_pgd;
38static pgd_t *hyp_pgd;
39static pgd_t *merged_hyp_pgd;
40static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
41
42static unsigned long hyp_idmap_start;
43static unsigned long hyp_idmap_end;
44static phys_addr_t hyp_idmap_vector;
45
46static unsigned long io_map_base;
47
48#define S2_PGD_SIZE (PTRS_PER_S2_PGD * sizeof(pgd_t))
49#define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
50
51#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{
56 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
57}
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);
68}
69
70static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
71{
72 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
73}
74
75/*
76 * D-Cache management functions. They take the page table entries by
77 * value, as they are flushing the cache using the kernel mapping (or
78 * kmap on 32bit).
79 */
80static void kvm_flush_dcache_pte(pte_t pte)
81{
82 __kvm_flush_dcache_pte(pte);
83}
84
85static void kvm_flush_dcache_pmd(pmd_t pmd)
86{
87 __kvm_flush_dcache_pmd(pmd);
88}
89
90static void kvm_flush_dcache_pud(pud_t pud)
91{
92 __kvm_flush_dcache_pud(pud);
93}
94
95static bool kvm_is_device_pfn(unsigned long pfn)
96{
97 return !pfn_valid(pfn);
98}
99
100/**
101 * stage2_dissolve_pmd() - clear and flush huge PMD entry
102 * @kvm: pointer to kvm structure.
103 * @addr: IPA
104 * @pmd: pmd pointer for IPA
105 *
106 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
107 * pages in the range dirty.
108 */
109static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
110{
111 if (!pmd_thp_or_huge(*pmd))
112 return;
113
114 pmd_clear(pmd);
115 kvm_tlb_flush_vmid_ipa(kvm, addr);
116 put_page(virt_to_page(pmd));
117}
118
119static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
120 int min, int max)
121{
122 void *page;
123
124 BUG_ON(max > KVM_NR_MEM_OBJS);
125 if (cache->nobjs >= min)
126 return 0;
127 while (cache->nobjs < max) {
128 page = (void *)__get_free_page(PGALLOC_GFP);
129 if (!page)
130 return -ENOMEM;
131 cache->objects[cache->nobjs++] = page;
132 }
133 return 0;
134}
135
136static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
137{
138 while (mc->nobjs)
139 free_page((unsigned long)mc->objects[--mc->nobjs]);
140}
141
142static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
143{
144 void *p;
145
146 BUG_ON(!mc || !mc->nobjs);
147 p = mc->objects[--mc->nobjs];
148 return p;
149}
150
151static void clear_stage2_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
152{
153 pud_t *pud_table __maybe_unused = stage2_pud_offset(pgd, 0UL);
154 stage2_pgd_clear(pgd);
155 kvm_tlb_flush_vmid_ipa(kvm, addr);
156 stage2_pud_free(pud_table);
157 put_page(virt_to_page(pgd));
158}
159
160static void clear_stage2_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
161{
162 pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(pud, 0);
163 VM_BUG_ON(stage2_pud_huge(*pud));
164 stage2_pud_clear(pud);
165 kvm_tlb_flush_vmid_ipa(kvm, addr);
166 stage2_pmd_free(pmd_table);
167 put_page(virt_to_page(pud));
168}
169
170static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
171{
172 pte_t *pte_table = pte_offset_kernel(pmd, 0);
173 VM_BUG_ON(pmd_thp_or_huge(*pmd));
174 pmd_clear(pmd);
175 kvm_tlb_flush_vmid_ipa(kvm, addr);
176 pte_free_kernel(NULL, pte_table);
177 put_page(virt_to_page(pmd));
178}
179
180static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte)
181{
182 WRITE_ONCE(*ptep, new_pte);
183 dsb(ishst);
184}
185
186static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd)
187{
188 WRITE_ONCE(*pmdp, new_pmd);
189 dsb(ishst);
190}
191
192static inline void kvm_pmd_populate(pmd_t *pmdp, pte_t *ptep)
193{
194 kvm_set_pmd(pmdp, kvm_mk_pmd(ptep));
195}
196
197static inline void kvm_pud_populate(pud_t *pudp, pmd_t *pmdp)
198{
199 WRITE_ONCE(*pudp, kvm_mk_pud(pmdp));
200 dsb(ishst);
201}
202
203static inline void kvm_pgd_populate(pgd_t *pgdp, pud_t *pudp)
204{
205 WRITE_ONCE(*pgdp, kvm_mk_pgd(pudp));
206 dsb(ishst);
207}
208
209/*
210 * Unmapping vs dcache management:
211 *
212 * If a guest maps certain memory pages as uncached, all writes will
213 * bypass the data cache and go directly to RAM. However, the CPUs
214 * can still speculate reads (not writes) and fill cache lines with
215 * data.
216 *
217 * Those cache lines will be *clean* cache lines though, so a
218 * clean+invalidate operation is equivalent to an invalidate
219 * operation, because no cache lines are marked dirty.
220 *
221 * Those clean cache lines could be filled prior to an uncached write
222 * by the guest, and the cache coherent IO subsystem would therefore
223 * end up writing old data to disk.
224 *
225 * This is why right after unmapping a page/section and invalidating
226 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
227 * the IO subsystem will never hit in the cache.
228 *
229 * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
230 * we then fully enforce cacheability of RAM, no matter what the guest
231 * does.
232 */
233static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd,
234 phys_addr_t addr, phys_addr_t end)
235{
236 phys_addr_t start_addr = addr;
237 pte_t *pte, *start_pte;
238
239 start_pte = pte = pte_offset_kernel(pmd, addr);
240 do {
241 if (!pte_none(*pte)) {
242 pte_t old_pte = *pte;
243
244 kvm_set_pte(pte, __pte(0));
245 kvm_tlb_flush_vmid_ipa(kvm, addr);
246
247 /* No need to invalidate the cache for device mappings */
248 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
249 kvm_flush_dcache_pte(old_pte);
250
251 put_page(virt_to_page(pte));
252 }
253 } while (pte++, addr += PAGE_SIZE, addr != end);
254
255 if (stage2_pte_table_empty(start_pte))
256 clear_stage2_pmd_entry(kvm, pmd, start_addr);
257}
258
259static void unmap_stage2_pmds(struct kvm *kvm, pud_t *pud,
260 phys_addr_t addr, phys_addr_t end)
261{
262 phys_addr_t next, start_addr = addr;
263 pmd_t *pmd, *start_pmd;
264
265 start_pmd = pmd = stage2_pmd_offset(pud, addr);
266 do {
267 next = stage2_pmd_addr_end(addr, end);
268 if (!pmd_none(*pmd)) {
269 if (pmd_thp_or_huge(*pmd)) {
270 pmd_t old_pmd = *pmd;
271
272 pmd_clear(pmd);
273 kvm_tlb_flush_vmid_ipa(kvm, addr);
274
275 kvm_flush_dcache_pmd(old_pmd);
276
277 put_page(virt_to_page(pmd));
278 } else {
279 unmap_stage2_ptes(kvm, pmd, addr, next);
280 }
281 }
282 } while (pmd++, addr = next, addr != end);
283
284 if (stage2_pmd_table_empty(start_pmd))
285 clear_stage2_pud_entry(kvm, pud, start_addr);
286}
287
288static void unmap_stage2_puds(struct kvm *kvm, pgd_t *pgd,
289 phys_addr_t addr, phys_addr_t end)
290{
291 phys_addr_t next, start_addr = addr;
292 pud_t *pud, *start_pud;
293
294 start_pud = pud = stage2_pud_offset(pgd, addr);
295 do {
296 next = stage2_pud_addr_end(addr, end);
297 if (!stage2_pud_none(*pud)) {
298 if (stage2_pud_huge(*pud)) {
299 pud_t old_pud = *pud;
300
301 stage2_pud_clear(pud);
302 kvm_tlb_flush_vmid_ipa(kvm, addr);
303 kvm_flush_dcache_pud(old_pud);
304 put_page(virt_to_page(pud));
305 } else {
306 unmap_stage2_pmds(kvm, pud, addr, next);
307 }
308 }
309 } while (pud++, addr = next, addr != end);
310
311 if (stage2_pud_table_empty(start_pud))
312 clear_stage2_pgd_entry(kvm, pgd, start_addr);
313}
314
315/**
316 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
317 * @kvm: The VM pointer
318 * @start: The intermediate physical base address of the range to unmap
319 * @size: The size of the area to unmap
320 *
321 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
322 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
323 * destroying the VM), otherwise another faulting VCPU may come in and mess
324 * with things behind our backs.
325 */
326static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
327{
328 pgd_t *pgd;
329 phys_addr_t addr = start, end = start + size;
330 phys_addr_t next;
331
332 assert_spin_locked(&kvm->mmu_lock);
333 WARN_ON(size & ~PAGE_MASK);
334
335 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
336 do {
337 /*
338 * Make sure the page table is still active, as another thread
339 * could have possibly freed the page table, while we released
340 * the lock.
341 */
342 if (!READ_ONCE(kvm->arch.pgd))
343 break;
344 next = stage2_pgd_addr_end(addr, end);
345 if (!stage2_pgd_none(*pgd))
346 unmap_stage2_puds(kvm, pgd, addr, next);
347 /*
348 * If the range is too large, release the kvm->mmu_lock
349 * to prevent starvation and lockup detector warnings.
350 */
351 if (next != end)
352 cond_resched_lock(&kvm->mmu_lock);
353 } while (pgd++, addr = next, addr != end);
354}
355
356static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
357 phys_addr_t addr, phys_addr_t end)
358{
359 pte_t *pte;
360
361 pte = pte_offset_kernel(pmd, addr);
362 do {
363 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
364 kvm_flush_dcache_pte(*pte);
365 } while (pte++, addr += PAGE_SIZE, addr != end);
366}
367
368static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
369 phys_addr_t addr, phys_addr_t end)
370{
371 pmd_t *pmd;
372 phys_addr_t next;
373
374 pmd = stage2_pmd_offset(pud, addr);
375 do {
376 next = stage2_pmd_addr_end(addr, end);
377 if (!pmd_none(*pmd)) {
378 if (pmd_thp_or_huge(*pmd))
379 kvm_flush_dcache_pmd(*pmd);
380 else
381 stage2_flush_ptes(kvm, pmd, addr, next);
382 }
383 } while (pmd++, addr = next, addr != end);
384}
385
386static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
387 phys_addr_t addr, phys_addr_t end)
388{
389 pud_t *pud;
390 phys_addr_t next;
391
392 pud = stage2_pud_offset(pgd, addr);
393 do {
394 next = stage2_pud_addr_end(addr, end);
395 if (!stage2_pud_none(*pud)) {
396 if (stage2_pud_huge(*pud))
397 kvm_flush_dcache_pud(*pud);
398 else
399 stage2_flush_pmds(kvm, pud, addr, next);
400 }
401 } while (pud++, addr = next, addr != end);
402}
403
404static void stage2_flush_memslot(struct kvm *kvm,
405 struct kvm_memory_slot *memslot)
406{
407 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
408 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
409 phys_addr_t next;
410 pgd_t *pgd;
411
412 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
413 do {
414 next = stage2_pgd_addr_end(addr, end);
415 if (!stage2_pgd_none(*pgd))
416 stage2_flush_puds(kvm, pgd, addr, next);
417 } while (pgd++, addr = next, addr != end);
418}
419
420/**
421 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
422 * @kvm: The struct kvm pointer
423 *
424 * Go through the stage 2 page tables and invalidate any cache lines
425 * backing memory already mapped to the VM.
426 */
427static void stage2_flush_vm(struct kvm *kvm)
428{
429 struct kvm_memslots *slots;
430 struct kvm_memory_slot *memslot;
431 int idx;
432
433 idx = srcu_read_lock(&kvm->srcu);
434 spin_lock(&kvm->mmu_lock);
435
436 slots = kvm_memslots(kvm);
437 kvm_for_each_memslot(memslot, slots)
438 stage2_flush_memslot(kvm, memslot);
439
440 spin_unlock(&kvm->mmu_lock);
441 srcu_read_unlock(&kvm->srcu, idx);
442}
443
444static void clear_hyp_pgd_entry(pgd_t *pgd)
445{
446 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0UL);
447 pgd_clear(pgd);
448 pud_free(NULL, pud_table);
449 put_page(virt_to_page(pgd));
450}
451
452static void clear_hyp_pud_entry(pud_t *pud)
453{
454 pmd_t *pmd_table __maybe_unused = pmd_offset(pud, 0);
455 VM_BUG_ON(pud_huge(*pud));
456 pud_clear(pud);
457 pmd_free(NULL, pmd_table);
458 put_page(virt_to_page(pud));
459}
460
461static void clear_hyp_pmd_entry(pmd_t *pmd)
462{
463 pte_t *pte_table = pte_offset_kernel(pmd, 0);
464 VM_BUG_ON(pmd_thp_or_huge(*pmd));
465 pmd_clear(pmd);
466 pte_free_kernel(NULL, pte_table);
467 put_page(virt_to_page(pmd));
468}
469
470static void unmap_hyp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
471{
472 pte_t *pte, *start_pte;
473
474 start_pte = pte = pte_offset_kernel(pmd, addr);
475 do {
476 if (!pte_none(*pte)) {
477 kvm_set_pte(pte, __pte(0));
478 put_page(virt_to_page(pte));
479 }
480 } while (pte++, addr += PAGE_SIZE, addr != end);
481
482 if (hyp_pte_table_empty(start_pte))
483 clear_hyp_pmd_entry(pmd);
484}
485
486static void unmap_hyp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
487{
488 phys_addr_t next;
489 pmd_t *pmd, *start_pmd;
490
491 start_pmd = pmd = pmd_offset(pud, addr);
492 do {
493 next = pmd_addr_end(addr, end);
494 /* Hyp doesn't use huge pmds */
495 if (!pmd_none(*pmd))
496 unmap_hyp_ptes(pmd, addr, next);
497 } while (pmd++, addr = next, addr != end);
498
499 if (hyp_pmd_table_empty(start_pmd))
500 clear_hyp_pud_entry(pud);
501}
502
503static void unmap_hyp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
504{
505 phys_addr_t next;
506 pud_t *pud, *start_pud;
507
508 start_pud = pud = pud_offset(pgd, addr);
509 do {
510 next = pud_addr_end(addr, end);
511 /* Hyp doesn't use huge puds */
512 if (!pud_none(*pud))
513 unmap_hyp_pmds(pud, addr, next);
514 } while (pud++, addr = next, addr != end);
515
516 if (hyp_pud_table_empty(start_pud))
517 clear_hyp_pgd_entry(pgd);
518}
519
520static unsigned int kvm_pgd_index(unsigned long addr, unsigned int ptrs_per_pgd)
521{
522 return (addr >> PGDIR_SHIFT) & (ptrs_per_pgd - 1);
523}
524
525static void __unmap_hyp_range(pgd_t *pgdp, unsigned long ptrs_per_pgd,
526 phys_addr_t start, u64 size)
527{
528 pgd_t *pgd;
529 phys_addr_t addr = start, end = start + size;
530 phys_addr_t next;
531
532 /*
533 * We don't unmap anything from HYP, except at the hyp tear down.
534 * Hence, we don't have to invalidate the TLBs here.
535 */
536 pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
537 do {
538 next = pgd_addr_end(addr, end);
539 if (!pgd_none(*pgd))
540 unmap_hyp_puds(pgd, addr, next);
541 } while (pgd++, addr = next, addr != end);
542}
543
544static void unmap_hyp_range(pgd_t *pgdp, phys_addr_t start, u64 size)
545{
546 __unmap_hyp_range(pgdp, PTRS_PER_PGD, start, size);
547}
548
549static void unmap_hyp_idmap_range(pgd_t *pgdp, phys_addr_t start, u64 size)
550{
551 __unmap_hyp_range(pgdp, __kvm_idmap_ptrs_per_pgd(), start, size);
552}
553
554/**
555 * free_hyp_pgds - free Hyp-mode page tables
556 *
557 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
558 * therefore contains either mappings in the kernel memory area (above
559 * PAGE_OFFSET), or device mappings in the idmap range.
560 *
561 * boot_hyp_pgd should only map the idmap range, and is only used in
562 * the extended idmap case.
563 */
564void free_hyp_pgds(void)
565{
566 pgd_t *id_pgd;
567
568 mutex_lock(&kvm_hyp_pgd_mutex);
569
570 id_pgd = boot_hyp_pgd ? boot_hyp_pgd : hyp_pgd;
571
572 if (id_pgd) {
573 /* In case we never called hyp_mmu_init() */
574 if (!io_map_base)
575 io_map_base = hyp_idmap_start;
576 unmap_hyp_idmap_range(id_pgd, io_map_base,
577 hyp_idmap_start + PAGE_SIZE - io_map_base);
578 }
579
580 if (boot_hyp_pgd) {
581 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
582 boot_hyp_pgd = NULL;
583 }
584
585 if (hyp_pgd) {
586 unmap_hyp_range(hyp_pgd, kern_hyp_va(PAGE_OFFSET),
587 (uintptr_t)high_memory - PAGE_OFFSET);
588
589 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
590 hyp_pgd = NULL;
591 }
592 if (merged_hyp_pgd) {
593 clear_page(merged_hyp_pgd);
594 free_page((unsigned long)merged_hyp_pgd);
595 merged_hyp_pgd = NULL;
596 }
597
598 mutex_unlock(&kvm_hyp_pgd_mutex);
599}
600
601static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
602 unsigned long end, unsigned long pfn,
603 pgprot_t prot)
604{
605 pte_t *pte;
606 unsigned long addr;
607
608 addr = start;
609 do {
610 pte = pte_offset_kernel(pmd, addr);
611 kvm_set_pte(pte, pfn_pte(pfn, prot));
612 get_page(virt_to_page(pte));
613 pfn++;
614 } while (addr += PAGE_SIZE, addr != end);
615}
616
617static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
618 unsigned long end, unsigned long pfn,
619 pgprot_t prot)
620{
621 pmd_t *pmd;
622 pte_t *pte;
623 unsigned long addr, next;
624
625 addr = start;
626 do {
627 pmd = pmd_offset(pud, addr);
628
629 BUG_ON(pmd_sect(*pmd));
630
631 if (pmd_none(*pmd)) {
632 pte = pte_alloc_one_kernel(NULL, addr);
633 if (!pte) {
634 kvm_err("Cannot allocate Hyp pte\n");
635 return -ENOMEM;
636 }
637 kvm_pmd_populate(pmd, pte);
638 get_page(virt_to_page(pmd));
639 }
640
641 next = pmd_addr_end(addr, end);
642
643 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
644 pfn += (next - addr) >> PAGE_SHIFT;
645 } while (addr = next, addr != end);
646
647 return 0;
648}
649
650static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
651 unsigned long end, unsigned long pfn,
652 pgprot_t prot)
653{
654 pud_t *pud;
655 pmd_t *pmd;
656 unsigned long addr, next;
657 int ret;
658
659 addr = start;
660 do {
661 pud = pud_offset(pgd, addr);
662
663 if (pud_none_or_clear_bad(pud)) {
664 pmd = pmd_alloc_one(NULL, addr);
665 if (!pmd) {
666 kvm_err("Cannot allocate Hyp pmd\n");
667 return -ENOMEM;
668 }
669 kvm_pud_populate(pud, pmd);
670 get_page(virt_to_page(pud));
671 }
672
673 next = pud_addr_end(addr, end);
674 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
675 if (ret)
676 return ret;
677 pfn += (next - addr) >> PAGE_SHIFT;
678 } while (addr = next, addr != end);
679
680 return 0;
681}
682
683static int __create_hyp_mappings(pgd_t *pgdp, unsigned long ptrs_per_pgd,
684 unsigned long start, unsigned long end,
685 unsigned long pfn, pgprot_t prot)
686{
687 pgd_t *pgd;
688 pud_t *pud;
689 unsigned long addr, next;
690 int err = 0;
691
692 mutex_lock(&kvm_hyp_pgd_mutex);
693 addr = start & PAGE_MASK;
694 end = PAGE_ALIGN(end);
695 do {
696 pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
697
698 if (pgd_none(*pgd)) {
699 pud = pud_alloc_one(NULL, addr);
700 if (!pud) {
701 kvm_err("Cannot allocate Hyp pud\n");
702 err = -ENOMEM;
703 goto out;
704 }
705 kvm_pgd_populate(pgd, pud);
706 get_page(virt_to_page(pgd));
707 }
708
709 next = pgd_addr_end(addr, end);
710 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
711 if (err)
712 goto out;
713 pfn += (next - addr) >> PAGE_SHIFT;
714 } while (addr = next, addr != end);
715out:
716 mutex_unlock(&kvm_hyp_pgd_mutex);
717 return err;
718}
719
720static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
721{
722 if (!is_vmalloc_addr(kaddr)) {
723 BUG_ON(!virt_addr_valid(kaddr));
724 return __pa(kaddr);
725 } else {
726 return page_to_phys(vmalloc_to_page(kaddr)) +
727 offset_in_page(kaddr);
728 }
729}
730
731/**
732 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
733 * @from: The virtual kernel start address of the range
734 * @to: The virtual kernel end address of the range (exclusive)
735 * @prot: The protection to be applied to this range
736 *
737 * The same virtual address as the kernel virtual address is also used
738 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
739 * physical pages.
740 */
741int create_hyp_mappings(void *from, void *to, pgprot_t prot)
742{
743 phys_addr_t phys_addr;
744 unsigned long virt_addr;
745 unsigned long start = kern_hyp_va((unsigned long)from);
746 unsigned long end = kern_hyp_va((unsigned long)to);
747
748 if (is_kernel_in_hyp_mode())
749 return 0;
750
751 start = start & PAGE_MASK;
752 end = PAGE_ALIGN(end);
753
754 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
755 int err;
756
757 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
758 err = __create_hyp_mappings(hyp_pgd, PTRS_PER_PGD,
759 virt_addr, virt_addr + PAGE_SIZE,
760 __phys_to_pfn(phys_addr),
761 prot);
762 if (err)
763 return err;
764 }
765
766 return 0;
767}
768
769static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
770 unsigned long *haddr, pgprot_t prot)
771{
772 pgd_t *pgd = hyp_pgd;
773 unsigned long base;
774 int ret = 0;
775
776 mutex_lock(&kvm_hyp_pgd_mutex);
777
778 /*
779 * This assumes that we we have enough space below the idmap
780 * page to allocate our VAs. If not, the check below will
781 * kick. A potential alternative would be to detect that
782 * overflow and switch to an allocation above the idmap.
783 *
784 * The allocated size is always a multiple of PAGE_SIZE.
785 */
786 size = PAGE_ALIGN(size + offset_in_page(phys_addr));
787 base = io_map_base - size;
788
789 /*
790 * Verify that BIT(VA_BITS - 1) hasn't been flipped by
791 * allocating the new area, as it would indicate we've
792 * overflowed the idmap/IO address range.
793 */
794 if ((base ^ io_map_base) & BIT(VA_BITS - 1))
795 ret = -ENOMEM;
796 else
797 io_map_base = base;
798
799 mutex_unlock(&kvm_hyp_pgd_mutex);
800
801 if (ret)
802 goto out;
803
804 if (__kvm_cpu_uses_extended_idmap())
805 pgd = boot_hyp_pgd;
806
807 ret = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
808 base, base + size,
809 __phys_to_pfn(phys_addr), prot);
810 if (ret)
811 goto out;
812
813 *haddr = base + offset_in_page(phys_addr);
814
815out:
816 return ret;
817}
818
819/**
820 * create_hyp_io_mappings - Map IO into both kernel and HYP
821 * @phys_addr: The physical start address which gets mapped
822 * @size: Size of the region being mapped
823 * @kaddr: Kernel VA for this mapping
824 * @haddr: HYP VA for this mapping
825 */
826int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
827 void __iomem **kaddr,
828 void __iomem **haddr)
829{
830 unsigned long addr;
831 int ret;
832
833 *kaddr = ioremap(phys_addr, size);
834 if (!*kaddr)
835 return -ENOMEM;
836
837 if (is_kernel_in_hyp_mode()) {
838 *haddr = *kaddr;
839 return 0;
840 }
841
842 ret = __create_hyp_private_mapping(phys_addr, size,
843 &addr, PAGE_HYP_DEVICE);
844 if (ret) {
845 iounmap(*kaddr);
846 *kaddr = NULL;
847 *haddr = NULL;
848 return ret;
849 }
850
851 *haddr = (void __iomem *)addr;
852 return 0;
853}
854
855/**
856 * create_hyp_exec_mappings - Map an executable range into HYP
857 * @phys_addr: The physical start address which gets mapped
858 * @size: Size of the region being mapped
859 * @haddr: HYP VA for this mapping
860 */
861int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
862 void **haddr)
863{
864 unsigned long addr;
865 int ret;
866
867 BUG_ON(is_kernel_in_hyp_mode());
868
869 ret = __create_hyp_private_mapping(phys_addr, size,
870 &addr, PAGE_HYP_EXEC);
871 if (ret) {
872 *haddr = NULL;
873 return ret;
874 }
875
876 *haddr = (void *)addr;
877 return 0;
878}
879
880/**
881 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
882 * @kvm: The KVM struct pointer for the VM.
883 *
884 * Allocates only the stage-2 HW PGD level table(s) (can support either full
885 * 40-bit input addresses or limited to 32-bit input addresses). Clears the
886 * allocated pages.
887 *
888 * Note we don't need locking here as this is only called when the VM is
889 * created, which can only be done once.
890 */
891int kvm_alloc_stage2_pgd(struct kvm *kvm)
892{
893 pgd_t *pgd;
894
895 if (kvm->arch.pgd != NULL) {
896 kvm_err("kvm_arch already initialized?\n");
897 return -EINVAL;
898 }
899
900 /* Allocate the HW PGD, making sure that each page gets its own refcount */
901 pgd = alloc_pages_exact(S2_PGD_SIZE, GFP_KERNEL | __GFP_ZERO);
902 if (!pgd)
903 return -ENOMEM;
904
905 kvm->arch.pgd = pgd;
906 return 0;
907}
908
909static void stage2_unmap_memslot(struct kvm *kvm,
910 struct kvm_memory_slot *memslot)
911{
912 hva_t hva = memslot->userspace_addr;
913 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
914 phys_addr_t size = PAGE_SIZE * memslot->npages;
915 hva_t reg_end = hva + size;
916
917 /*
918 * A memory region could potentially cover multiple VMAs, and any holes
919 * between them, so iterate over all of them to find out if we should
920 * unmap any of them.
921 *
922 * +--------------------------------------------+
923 * +---------------+----------------+ +----------------+
924 * | : VMA 1 | VMA 2 | | VMA 3 : |
925 * +---------------+----------------+ +----------------+
926 * | memory region |
927 * +--------------------------------------------+
928 */
929 do {
930 struct vm_area_struct *vma = find_vma(current->mm, hva);
931 hva_t vm_start, vm_end;
932
933 if (!vma || vma->vm_start >= reg_end)
934 break;
935
936 /*
937 * Take the intersection of this VMA with the memory region
938 */
939 vm_start = max(hva, vma->vm_start);
940 vm_end = min(reg_end, vma->vm_end);
941
942 if (!(vma->vm_flags & VM_PFNMAP)) {
943 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
944 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
945 }
946 hva = vm_end;
947 } while (hva < reg_end);
948}
949
950/**
951 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
952 * @kvm: The struct kvm pointer
953 *
954 * Go through the memregions and unmap any reguler RAM
955 * backing memory already mapped to the VM.
956 */
957void stage2_unmap_vm(struct kvm *kvm)
958{
959 struct kvm_memslots *slots;
960 struct kvm_memory_slot *memslot;
961 int idx;
962
963 idx = srcu_read_lock(&kvm->srcu);
964 down_read(&current->mm->mmap_sem);
965 spin_lock(&kvm->mmu_lock);
966
967 slots = kvm_memslots(kvm);
968 kvm_for_each_memslot(memslot, slots)
969 stage2_unmap_memslot(kvm, memslot);
970
971 spin_unlock(&kvm->mmu_lock);
972 up_read(&current->mm->mmap_sem);
973 srcu_read_unlock(&kvm->srcu, idx);
974}
975
976/**
977 * kvm_free_stage2_pgd - free all stage-2 tables
978 * @kvm: The KVM struct pointer for the VM.
979 *
980 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
981 * underlying level-2 and level-3 tables before freeing the actual level-1 table
982 * and setting the struct pointer to NULL.
983 */
984void kvm_free_stage2_pgd(struct kvm *kvm)
985{
986 void *pgd = NULL;
987
988 spin_lock(&kvm->mmu_lock);
989 if (kvm->arch.pgd) {
990 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
991 pgd = READ_ONCE(kvm->arch.pgd);
992 kvm->arch.pgd = NULL;
993 }
994 spin_unlock(&kvm->mmu_lock);
995
996 /* Free the HW pgd, one page at a time */
997 if (pgd)
998 free_pages_exact(pgd, S2_PGD_SIZE);
999}
1000
1001static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1002 phys_addr_t addr)
1003{
1004 pgd_t *pgd;
1005 pud_t *pud;
1006
1007 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
1008 if (WARN_ON(stage2_pgd_none(*pgd))) {
1009 if (!cache)
1010 return NULL;
1011 pud = mmu_memory_cache_alloc(cache);
1012 stage2_pgd_populate(pgd, pud);
1013 get_page(virt_to_page(pgd));
1014 }
1015
1016 return stage2_pud_offset(pgd, addr);
1017}
1018
1019static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1020 phys_addr_t addr)
1021{
1022 pud_t *pud;
1023 pmd_t *pmd;
1024
1025 pud = stage2_get_pud(kvm, cache, addr);
1026 if (!pud)
1027 return NULL;
1028
1029 if (stage2_pud_none(*pud)) {
1030 if (!cache)
1031 return NULL;
1032 pmd = mmu_memory_cache_alloc(cache);
1033 stage2_pud_populate(pud, pmd);
1034 get_page(virt_to_page(pud));
1035 }
1036
1037 return stage2_pmd_offset(pud, addr);
1038}
1039
1040static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
1041 *cache, phys_addr_t addr, const pmd_t *new_pmd)
1042{
1043 pmd_t *pmd, old_pmd;
1044
1045 pmd = stage2_get_pmd(kvm, cache, addr);
1046 VM_BUG_ON(!pmd);
1047
1048 old_pmd = *pmd;
1049 if (pmd_present(old_pmd)) {
1050 /*
1051 * Multiple vcpus faulting on the same PMD entry, can
1052 * lead to them sequentially updating the PMD with the
1053 * same value. Following the break-before-make
1054 * (pmd_clear() followed by tlb_flush()) process can
1055 * hinder forward progress due to refaults generated
1056 * on missing translations.
1057 *
1058 * Skip updating the page table if the entry is
1059 * unchanged.
1060 */
1061 if (pmd_val(old_pmd) == pmd_val(*new_pmd))
1062 return 0;
1063
1064 /*
1065 * Mapping in huge pages should only happen through a
1066 * fault. If a page is merged into a transparent huge
1067 * page, the individual subpages of that huge page
1068 * should be unmapped through MMU notifiers before we
1069 * get here.
1070 *
1071 * Merging of CompoundPages is not supported; they
1072 * should become splitting first, unmapped, merged,
1073 * and mapped back in on-demand.
1074 */
1075 VM_BUG_ON(pmd_pfn(old_pmd) != pmd_pfn(*new_pmd));
1076
1077 pmd_clear(pmd);
1078 kvm_tlb_flush_vmid_ipa(kvm, addr);
1079 } else {
1080 get_page(virt_to_page(pmd));
1081 }
1082
1083 kvm_set_pmd(pmd, *new_pmd);
1084 return 0;
1085}
1086
1087static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)
1088{
1089 pmd_t *pmdp;
1090 pte_t *ptep;
1091
1092 pmdp = stage2_get_pmd(kvm, NULL, addr);
1093 if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp))
1094 return false;
1095
1096 if (pmd_thp_or_huge(*pmdp))
1097 return kvm_s2pmd_exec(pmdp);
1098
1099 ptep = pte_offset_kernel(pmdp, addr);
1100 if (!ptep || pte_none(*ptep) || !pte_present(*ptep))
1101 return false;
1102
1103 return kvm_s2pte_exec(ptep);
1104}
1105
1106static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1107 phys_addr_t addr, const pte_t *new_pte,
1108 unsigned long flags)
1109{
1110 pmd_t *pmd;
1111 pte_t *pte, old_pte;
1112 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
1113 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
1114
1115 VM_BUG_ON(logging_active && !cache);
1116
1117 /* Create stage-2 page table mapping - Levels 0 and 1 */
1118 pmd = stage2_get_pmd(kvm, cache, addr);
1119 if (!pmd) {
1120 /*
1121 * Ignore calls from kvm_set_spte_hva for unallocated
1122 * address ranges.
1123 */
1124 return 0;
1125 }
1126
1127 /*
1128 * While dirty page logging - dissolve huge PMD, then continue on to
1129 * allocate page.
1130 */
1131 if (logging_active)
1132 stage2_dissolve_pmd(kvm, addr, pmd);
1133
1134 /* Create stage-2 page mappings - Level 2 */
1135 if (pmd_none(*pmd)) {
1136 if (!cache)
1137 return 0; /* ignore calls from kvm_set_spte_hva */
1138 pte = mmu_memory_cache_alloc(cache);
1139 kvm_pmd_populate(pmd, pte);
1140 get_page(virt_to_page(pmd));
1141 }
1142
1143 pte = pte_offset_kernel(pmd, addr);
1144
1145 if (iomap && pte_present(*pte))
1146 return -EFAULT;
1147
1148 /* Create 2nd stage page table mapping - Level 3 */
1149 old_pte = *pte;
1150 if (pte_present(old_pte)) {
1151 /* Skip page table update if there is no change */
1152 if (pte_val(old_pte) == pte_val(*new_pte))
1153 return 0;
1154
1155 kvm_set_pte(pte, __pte(0));
1156 kvm_tlb_flush_vmid_ipa(kvm, addr);
1157 } else {
1158 get_page(virt_to_page(pte));
1159 }
1160
1161 kvm_set_pte(pte, *new_pte);
1162 return 0;
1163}
1164
1165#ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1166static int stage2_ptep_test_and_clear_young(pte_t *pte)
1167{
1168 if (pte_young(*pte)) {
1169 *pte = pte_mkold(*pte);
1170 return 1;
1171 }
1172 return 0;
1173}
1174#else
1175static int stage2_ptep_test_and_clear_young(pte_t *pte)
1176{
1177 return __ptep_test_and_clear_young(pte);
1178}
1179#endif
1180
1181static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
1182{
1183 return stage2_ptep_test_and_clear_young((pte_t *)pmd);
1184}
1185
1186/**
1187 * kvm_phys_addr_ioremap - map a device range to guest IPA
1188 *
1189 * @kvm: The KVM pointer
1190 * @guest_ipa: The IPA at which to insert the mapping
1191 * @pa: The physical address of the device
1192 * @size: The size of the mapping
1193 */
1194int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
1195 phys_addr_t pa, unsigned long size, bool writable)
1196{
1197 phys_addr_t addr, end;
1198 int ret = 0;
1199 unsigned long pfn;
1200 struct kvm_mmu_memory_cache cache = { 0, };
1201
1202 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
1203 pfn = __phys_to_pfn(pa);
1204
1205 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
1206 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
1207
1208 if (writable)
1209 pte = kvm_s2pte_mkwrite(pte);
1210
1211 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
1212 KVM_NR_MEM_OBJS);
1213 if (ret)
1214 goto out;
1215 spin_lock(&kvm->mmu_lock);
1216 ret = stage2_set_pte(kvm, &cache, addr, &pte,
1217 KVM_S2PTE_FLAG_IS_IOMAP);
1218 spin_unlock(&kvm->mmu_lock);
1219 if (ret)
1220 goto out;
1221
1222 pfn++;
1223 }
1224
1225out:
1226 mmu_free_memory_cache(&cache);
1227 return ret;
1228}
1229
1230static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
1231{
1232 kvm_pfn_t pfn = *pfnp;
1233 gfn_t gfn = *ipap >> PAGE_SHIFT;
1234 struct page *page = pfn_to_page(pfn);
1235
1236 /*
1237 * PageTransCompoungMap() returns true for THP and
1238 * hugetlbfs. Make sure the adjustment is done only for THP
1239 * pages.
1240 */
1241 if (!PageHuge(page) && PageTransCompoundMap(page)) {
1242 unsigned long mask;
1243 /*
1244 * The address we faulted on is backed by a transparent huge
1245 * page. However, because we map the compound huge page and
1246 * not the individual tail page, we need to transfer the
1247 * refcount to the head page. We have to be careful that the
1248 * THP doesn't start to split while we are adjusting the
1249 * refcounts.
1250 *
1251 * We are sure this doesn't happen, because mmu_notifier_retry
1252 * was successful and we are holding the mmu_lock, so if this
1253 * THP is trying to split, it will be blocked in the mmu
1254 * notifier before touching any of the pages, specifically
1255 * before being able to call __split_huge_page_refcount().
1256 *
1257 * We can therefore safely transfer the refcount from PG_tail
1258 * to PG_head and switch the pfn from a tail page to the head
1259 * page accordingly.
1260 */
1261 mask = PTRS_PER_PMD - 1;
1262 VM_BUG_ON((gfn & mask) != (pfn & mask));
1263 if (pfn & mask) {
1264 *ipap &= PMD_MASK;
1265 kvm_release_pfn_clean(pfn);
1266 pfn &= ~mask;
1267 kvm_get_pfn(pfn);
1268 *pfnp = pfn;
1269 }
1270
1271 return true;
1272 }
1273
1274 return false;
1275}
1276
1277static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
1278{
1279 if (kvm_vcpu_trap_is_iabt(vcpu))
1280 return false;
1281
1282 return kvm_vcpu_dabt_iswrite(vcpu);
1283}
1284
1285/**
1286 * stage2_wp_ptes - write protect PMD range
1287 * @pmd: pointer to pmd entry
1288 * @addr: range start address
1289 * @end: range end address
1290 */
1291static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1292{
1293 pte_t *pte;
1294
1295 pte = pte_offset_kernel(pmd, addr);
1296 do {
1297 if (!pte_none(*pte)) {
1298 if (!kvm_s2pte_readonly(pte))
1299 kvm_set_s2pte_readonly(pte);
1300 }
1301 } while (pte++, addr += PAGE_SIZE, addr != end);
1302}
1303
1304/**
1305 * stage2_wp_pmds - write protect PUD range
1306 * @pud: pointer to pud entry
1307 * @addr: range start address
1308 * @end: range end address
1309 */
1310static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
1311{
1312 pmd_t *pmd;
1313 phys_addr_t next;
1314
1315 pmd = stage2_pmd_offset(pud, addr);
1316
1317 do {
1318 next = stage2_pmd_addr_end(addr, end);
1319 if (!pmd_none(*pmd)) {
1320 if (pmd_thp_or_huge(*pmd)) {
1321 if (!kvm_s2pmd_readonly(pmd))
1322 kvm_set_s2pmd_readonly(pmd);
1323 } else {
1324 stage2_wp_ptes(pmd, addr, next);
1325 }
1326 }
1327 } while (pmd++, addr = next, addr != end);
1328}
1329
1330/**
1331 * stage2_wp_puds - write protect PGD range
1332 * @pgd: pointer to pgd entry
1333 * @addr: range start address
1334 * @end: range end address
1335 *
1336 * Process PUD entries, for a huge PUD we cause a panic.
1337 */
1338static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
1339{
1340 pud_t *pud;
1341 phys_addr_t next;
1342
1343 pud = stage2_pud_offset(pgd, addr);
1344 do {
1345 next = stage2_pud_addr_end(addr, end);
1346 if (!stage2_pud_none(*pud)) {
1347 /* TODO:PUD not supported, revisit later if supported */
1348 BUG_ON(stage2_pud_huge(*pud));
1349 stage2_wp_pmds(pud, addr, next);
1350 }
1351 } while (pud++, addr = next, addr != end);
1352}
1353
1354/**
1355 * stage2_wp_range() - write protect stage2 memory region range
1356 * @kvm: The KVM pointer
1357 * @addr: Start address of range
1358 * @end: End address of range
1359 */
1360static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1361{
1362 pgd_t *pgd;
1363 phys_addr_t next;
1364
1365 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
1366 do {
1367 /*
1368 * Release kvm_mmu_lock periodically if the memory region is
1369 * large. Otherwise, we may see kernel panics with
1370 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1371 * CONFIG_LOCKDEP. Additionally, holding the lock too long
1372 * will also starve other vCPUs. We have to also make sure
1373 * that the page tables are not freed while we released
1374 * the lock.
1375 */
1376 cond_resched_lock(&kvm->mmu_lock);
1377 if (!READ_ONCE(kvm->arch.pgd))
1378 break;
1379 next = stage2_pgd_addr_end(addr, end);
1380 if (stage2_pgd_present(*pgd))
1381 stage2_wp_puds(pgd, addr, next);
1382 } while (pgd++, addr = next, addr != end);
1383}
1384
1385/**
1386 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1387 * @kvm: The KVM pointer
1388 * @slot: The memory slot to write protect
1389 *
1390 * Called to start logging dirty pages after memory region
1391 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1392 * all present PMD and PTEs are write protected in the memory region.
1393 * Afterwards read of dirty page log can be called.
1394 *
1395 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1396 * serializing operations for VM memory regions.
1397 */
1398void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1399{
1400 struct kvm_memslots *slots = kvm_memslots(kvm);
1401 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
1402 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1403 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1404
1405 spin_lock(&kvm->mmu_lock);
1406 stage2_wp_range(kvm, start, end);
1407 spin_unlock(&kvm->mmu_lock);
1408 kvm_flush_remote_tlbs(kvm);
1409}
1410
1411/**
1412 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
1413 * @kvm: The KVM pointer
1414 * @slot: The memory slot associated with mask
1415 * @gfn_offset: The gfn offset in memory slot
1416 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1417 * slot to be write protected
1418 *
1419 * Walks bits set in mask write protects the associated pte's. Caller must
1420 * acquire kvm_mmu_lock.
1421 */
1422static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
1423 struct kvm_memory_slot *slot,
1424 gfn_t gfn_offset, unsigned long mask)
1425{
1426 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1427 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1428 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1429
1430 stage2_wp_range(kvm, start, end);
1431}
1432
1433/*
1434 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1435 * dirty pages.
1436 *
1437 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1438 * enable dirty logging for them.
1439 */
1440void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1441 struct kvm_memory_slot *slot,
1442 gfn_t gfn_offset, unsigned long mask)
1443{
1444 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1445}
1446
1447static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
1448{
1449 __clean_dcache_guest_page(pfn, size);
1450}
1451
1452static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
1453{
1454 __invalidate_icache_guest_page(pfn, size);
1455}
1456
1457static void kvm_send_hwpoison_signal(unsigned long address,
1458 struct vm_area_struct *vma)
1459{
1460 siginfo_t info;
1461
1462 clear_siginfo(&info);
1463 info.si_signo = SIGBUS;
1464 info.si_errno = 0;
1465 info.si_code = BUS_MCEERR_AR;
1466 info.si_addr = (void __user *)address;
1467
1468 if (is_vm_hugetlb_page(vma))
1469 info.si_addr_lsb = huge_page_shift(hstate_vma(vma));
1470 else
1471 info.si_addr_lsb = PAGE_SHIFT;
1472
1473 send_sig_info(SIGBUS, &info, current);
1474}
1475
1476static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1477 struct kvm_memory_slot *memslot, unsigned long hva,
1478 unsigned long fault_status)
1479{
1480 int ret;
1481 bool write_fault, exec_fault, writable, hugetlb = false, force_pte = false;
1482 unsigned long mmu_seq;
1483 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
1484 struct kvm *kvm = vcpu->kvm;
1485 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
1486 struct vm_area_struct *vma;
1487 kvm_pfn_t pfn;
1488 pgprot_t mem_type = PAGE_S2;
1489 bool logging_active = memslot_is_logging(memslot);
1490 unsigned long flags = 0;
1491
1492 write_fault = kvm_is_write_fault(vcpu);
1493 exec_fault = kvm_vcpu_trap_is_iabt(vcpu);
1494 VM_BUG_ON(write_fault && exec_fault);
1495
1496 if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
1497 kvm_err("Unexpected L2 read permission error\n");
1498 return -EFAULT;
1499 }
1500
1501 /* Let's check if we will get back a huge page backed by hugetlbfs */
1502 down_read(&current->mm->mmap_sem);
1503 vma = find_vma_intersection(current->mm, hva, hva + 1);
1504 if (unlikely(!vma)) {
1505 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1506 up_read(&current->mm->mmap_sem);
1507 return -EFAULT;
1508 }
1509
1510 if (vma_kernel_pagesize(vma) == PMD_SIZE && !logging_active) {
1511 hugetlb = true;
1512 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
1513 } else {
1514 /*
1515 * Pages belonging to memslots that don't have the same
1516 * alignment for userspace and IPA cannot be mapped using
1517 * block descriptors even if the pages belong to a THP for
1518 * the process, because the stage-2 block descriptor will
1519 * cover more than a single THP and we loose atomicity for
1520 * unmapping, updates, and splits of the THP or other pages
1521 * in the stage-2 block range.
1522 */
1523 if ((memslot->userspace_addr & ~PMD_MASK) !=
1524 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
1525 force_pte = true;
1526 }
1527 up_read(&current->mm->mmap_sem);
1528
1529 /* We need minimum second+third level pages */
1530 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1531 KVM_NR_MEM_OBJS);
1532 if (ret)
1533 return ret;
1534
1535 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1536 /*
1537 * Ensure the read of mmu_notifier_seq happens before we call
1538 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1539 * the page we just got a reference to gets unmapped before we have a
1540 * chance to grab the mmu_lock, which ensure that if the page gets
1541 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1542 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1543 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1544 */
1545 smp_rmb();
1546
1547 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
1548 if (pfn == KVM_PFN_ERR_HWPOISON) {
1549 kvm_send_hwpoison_signal(hva, vma);
1550 return 0;
1551 }
1552 if (is_error_noslot_pfn(pfn))
1553 return -EFAULT;
1554
1555 if (kvm_is_device_pfn(pfn)) {
1556 mem_type = PAGE_S2_DEVICE;
1557 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1558 } else if (logging_active) {
1559 /*
1560 * Faults on pages in a memslot with logging enabled
1561 * should not be mapped with huge pages (it introduces churn
1562 * and performance degradation), so force a pte mapping.
1563 */
1564 force_pte = true;
1565 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1566
1567 /*
1568 * Only actually map the page as writable if this was a write
1569 * fault.
1570 */
1571 if (!write_fault)
1572 writable = false;
1573 }
1574
1575 spin_lock(&kvm->mmu_lock);
1576 if (mmu_notifier_retry(kvm, mmu_seq))
1577 goto out_unlock;
1578
1579 if (!hugetlb && !force_pte)
1580 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
1581
1582 if (hugetlb) {
1583 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
1584 new_pmd = pmd_mkhuge(new_pmd);
1585 if (writable) {
1586 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
1587 kvm_set_pfn_dirty(pfn);
1588 }
1589
1590 if (fault_status != FSC_PERM)
1591 clean_dcache_guest_page(pfn, PMD_SIZE);
1592
1593 if (exec_fault) {
1594 new_pmd = kvm_s2pmd_mkexec(new_pmd);
1595 invalidate_icache_guest_page(pfn, PMD_SIZE);
1596 } else if (fault_status == FSC_PERM) {
1597 /* Preserve execute if XN was already cleared */
1598 if (stage2_is_exec(kvm, fault_ipa))
1599 new_pmd = kvm_s2pmd_mkexec(new_pmd);
1600 }
1601
1602 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1603 } else {
1604 pte_t new_pte = pfn_pte(pfn, mem_type);
1605
1606 if (writable) {
1607 new_pte = kvm_s2pte_mkwrite(new_pte);
1608 kvm_set_pfn_dirty(pfn);
1609 mark_page_dirty(kvm, gfn);
1610 }
1611
1612 if (fault_status != FSC_PERM)
1613 clean_dcache_guest_page(pfn, PAGE_SIZE);
1614
1615 if (exec_fault) {
1616 new_pte = kvm_s2pte_mkexec(new_pte);
1617 invalidate_icache_guest_page(pfn, PAGE_SIZE);
1618 } else if (fault_status == FSC_PERM) {
1619 /* Preserve execute if XN was already cleared */
1620 if (stage2_is_exec(kvm, fault_ipa))
1621 new_pte = kvm_s2pte_mkexec(new_pte);
1622 }
1623
1624 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
1625 }
1626
1627out_unlock:
1628 spin_unlock(&kvm->mmu_lock);
1629 kvm_set_pfn_accessed(pfn);
1630 kvm_release_pfn_clean(pfn);
1631 return ret;
1632}
1633
1634/*
1635 * Resolve the access fault by making the page young again.
1636 * Note that because the faulting entry is guaranteed not to be
1637 * cached in the TLB, we don't need to invalidate anything.
1638 * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
1639 * so there is no need for atomic (pte|pmd)_mkyoung operations.
1640 */
1641static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1642{
1643 pmd_t *pmd;
1644 pte_t *pte;
1645 kvm_pfn_t pfn;
1646 bool pfn_valid = false;
1647
1648 trace_kvm_access_fault(fault_ipa);
1649
1650 spin_lock(&vcpu->kvm->mmu_lock);
1651
1652 pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
1653 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1654 goto out;
1655
1656 if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
1657 *pmd = pmd_mkyoung(*pmd);
1658 pfn = pmd_pfn(*pmd);
1659 pfn_valid = true;
1660 goto out;
1661 }
1662
1663 pte = pte_offset_kernel(pmd, fault_ipa);
1664 if (pte_none(*pte)) /* Nothing there either */
1665 goto out;
1666
1667 *pte = pte_mkyoung(*pte); /* Just a page... */
1668 pfn = pte_pfn(*pte);
1669 pfn_valid = true;
1670out:
1671 spin_unlock(&vcpu->kvm->mmu_lock);
1672 if (pfn_valid)
1673 kvm_set_pfn_accessed(pfn);
1674}
1675
1676/**
1677 * kvm_handle_guest_abort - handles all 2nd stage aborts
1678 * @vcpu: the VCPU pointer
1679 * @run: the kvm_run structure
1680 *
1681 * Any abort that gets to the host is almost guaranteed to be caused by a
1682 * missing second stage translation table entry, which can mean that either the
1683 * guest simply needs more memory and we must allocate an appropriate page or it
1684 * can mean that the guest tried to access I/O memory, which is emulated by user
1685 * space. The distinction is based on the IPA causing the fault and whether this
1686 * memory region has been registered as standard RAM by user space.
1687 */
1688int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1689{
1690 unsigned long fault_status;
1691 phys_addr_t fault_ipa;
1692 struct kvm_memory_slot *memslot;
1693 unsigned long hva;
1694 bool is_iabt, write_fault, writable;
1695 gfn_t gfn;
1696 int ret, idx;
1697
1698 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
1699
1700 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
1701 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
1702
1703 /* Synchronous External Abort? */
1704 if (kvm_vcpu_dabt_isextabt(vcpu)) {
1705 /*
1706 * For RAS the host kernel may handle this abort.
1707 * There is no need to pass the error into the guest.
1708 */
1709 if (!handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
1710 return 1;
1711
1712 if (unlikely(!is_iabt)) {
1713 kvm_inject_vabt(vcpu);
1714 return 1;
1715 }
1716 }
1717
1718 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1719 kvm_vcpu_get_hfar(vcpu), fault_ipa);
1720
1721 /* Check the stage-2 fault is trans. fault or write fault */
1722 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1723 fault_status != FSC_ACCESS) {
1724 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1725 kvm_vcpu_trap_get_class(vcpu),
1726 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1727 (unsigned long)kvm_vcpu_get_hsr(vcpu));
1728 return -EFAULT;
1729 }
1730
1731 idx = srcu_read_lock(&vcpu->kvm->srcu);
1732
1733 gfn = fault_ipa >> PAGE_SHIFT;
1734 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1735 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
1736 write_fault = kvm_is_write_fault(vcpu);
1737 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
1738 if (is_iabt) {
1739 /* Prefetch Abort on I/O address */
1740 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
1741 ret = 1;
1742 goto out_unlock;
1743 }
1744
1745 /*
1746 * Check for a cache maintenance operation. Since we
1747 * ended-up here, we know it is outside of any memory
1748 * slot. But we can't find out if that is for a device,
1749 * or if the guest is just being stupid. The only thing
1750 * we know for sure is that this range cannot be cached.
1751 *
1752 * So let's assume that the guest is just being
1753 * cautious, and skip the instruction.
1754 */
1755 if (kvm_vcpu_dabt_is_cm(vcpu)) {
1756 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1757 ret = 1;
1758 goto out_unlock;
1759 }
1760
1761 /*
1762 * The IPA is reported as [MAX:12], so we need to
1763 * complement it with the bottom 12 bits from the
1764 * faulting VA. This is always 12 bits, irrespective
1765 * of the page size.
1766 */
1767 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
1768 ret = io_mem_abort(vcpu, run, fault_ipa);
1769 goto out_unlock;
1770 }
1771
1772 /* Userspace should not be able to register out-of-bounds IPAs */
1773 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1774
1775 if (fault_status == FSC_ACCESS) {
1776 handle_access_fault(vcpu, fault_ipa);
1777 ret = 1;
1778 goto out_unlock;
1779 }
1780
1781 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
1782 if (ret == 0)
1783 ret = 1;
1784out_unlock:
1785 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1786 return ret;
1787}
1788
1789static int handle_hva_to_gpa(struct kvm *kvm,
1790 unsigned long start,
1791 unsigned long end,
1792 int (*handler)(struct kvm *kvm,
1793 gpa_t gpa, u64 size,
1794 void *data),
1795 void *data)
1796{
1797 struct kvm_memslots *slots;
1798 struct kvm_memory_slot *memslot;
1799 int ret = 0;
1800
1801 slots = kvm_memslots(kvm);
1802
1803 /* we only care about the pages that the guest sees */
1804 kvm_for_each_memslot(memslot, slots) {
1805 unsigned long hva_start, hva_end;
1806 gfn_t gpa;
1807
1808 hva_start = max(start, memslot->userspace_addr);
1809 hva_end = min(end, memslot->userspace_addr +
1810 (memslot->npages << PAGE_SHIFT));
1811 if (hva_start >= hva_end)
1812 continue;
1813
1814 gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
1815 ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
1816 }
1817
1818 return ret;
1819}
1820
1821static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
1822{
1823 unmap_stage2_range(kvm, gpa, size);
1824 return 0;
1825}
1826
1827int kvm_unmap_hva_range(struct kvm *kvm,
1828 unsigned long start, unsigned long end)
1829{
1830 if (!kvm->arch.pgd)
1831 return 0;
1832
1833 trace_kvm_unmap_hva_range(start, end);
1834 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1835 return 0;
1836}
1837
1838static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
1839{
1840 pte_t *pte = (pte_t *)data;
1841
1842 WARN_ON(size != PAGE_SIZE);
1843 /*
1844 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1845 * flag clear because MMU notifiers will have unmapped a huge PMD before
1846 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1847 * therefore stage2_set_pte() never needs to clear out a huge PMD
1848 * through this calling path.
1849 */
1850 stage2_set_pte(kvm, NULL, gpa, pte, 0);
1851 return 0;
1852}
1853
1854
1855void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1856{
1857 unsigned long end = hva + PAGE_SIZE;
1858 kvm_pfn_t pfn = pte_pfn(pte);
1859 pte_t stage2_pte;
1860
1861 if (!kvm->arch.pgd)
1862 return;
1863
1864 trace_kvm_set_spte_hva(hva);
1865
1866 /*
1867 * We've moved a page around, probably through CoW, so let's treat it
1868 * just like a translation fault and clean the cache to the PoC.
1869 */
1870 clean_dcache_guest_page(pfn, PAGE_SIZE);
1871 stage2_pte = pfn_pte(pfn, PAGE_S2);
1872 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1873}
1874
1875static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
1876{
1877 pmd_t *pmd;
1878 pte_t *pte;
1879
1880 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
1881 pmd = stage2_get_pmd(kvm, NULL, gpa);
1882 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1883 return 0;
1884
1885 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
1886 return stage2_pmdp_test_and_clear_young(pmd);
1887
1888 pte = pte_offset_kernel(pmd, gpa);
1889 if (pte_none(*pte))
1890 return 0;
1891
1892 return stage2_ptep_test_and_clear_young(pte);
1893}
1894
1895static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
1896{
1897 pmd_t *pmd;
1898 pte_t *pte;
1899
1900 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
1901 pmd = stage2_get_pmd(kvm, NULL, gpa);
1902 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1903 return 0;
1904
1905 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
1906 return pmd_young(*pmd);
1907
1908 pte = pte_offset_kernel(pmd, gpa);
1909 if (!pte_none(*pte)) /* Just a page... */
1910 return pte_young(*pte);
1911
1912 return 0;
1913}
1914
1915int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
1916{
1917 if (!kvm->arch.pgd)
1918 return 0;
1919 trace_kvm_age_hva(start, end);
1920 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
1921}
1922
1923int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1924{
1925 if (!kvm->arch.pgd)
1926 return 0;
1927 trace_kvm_test_age_hva(hva);
1928 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
1929}
1930
1931void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1932{
1933 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1934}
1935
1936phys_addr_t kvm_mmu_get_httbr(void)
1937{
1938 if (__kvm_cpu_uses_extended_idmap())
1939 return virt_to_phys(merged_hyp_pgd);
1940 else
1941 return virt_to_phys(hyp_pgd);
1942}
1943
1944phys_addr_t kvm_get_idmap_vector(void)
1945{
1946 return hyp_idmap_vector;
1947}
1948
1949static int kvm_map_idmap_text(pgd_t *pgd)
1950{
1951 int err;
1952
1953 /* Create the idmap in the boot page tables */
1954 err = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
1955 hyp_idmap_start, hyp_idmap_end,
1956 __phys_to_pfn(hyp_idmap_start),
1957 PAGE_HYP_EXEC);
1958 if (err)
1959 kvm_err("Failed to idmap %lx-%lx\n",
1960 hyp_idmap_start, hyp_idmap_end);
1961
1962 return err;
1963}
1964
1965int kvm_mmu_init(void)
1966{
1967 int err;
1968
1969 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1970 hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
1971 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1972 hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
1973 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
1974
1975 /*
1976 * We rely on the linker script to ensure at build time that the HYP
1977 * init code does not cross a page boundary.
1978 */
1979 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
1980
1981 kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
1982 kvm_debug("HYP VA range: %lx:%lx\n",
1983 kern_hyp_va(PAGE_OFFSET),
1984 kern_hyp_va((unsigned long)high_memory - 1));
1985
1986 if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
1987 hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
1988 hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
1989 /*
1990 * The idmap page is intersecting with the VA space,
1991 * it is not safe to continue further.
1992 */
1993 kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
1994 err = -EINVAL;
1995 goto out;
1996 }
1997
1998 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1999 if (!hyp_pgd) {
2000 kvm_err("Hyp mode PGD not allocated\n");
2001 err = -ENOMEM;
2002 goto out;
2003 }
2004
2005 if (__kvm_cpu_uses_extended_idmap()) {
2006 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2007 hyp_pgd_order);
2008 if (!boot_hyp_pgd) {
2009 kvm_err("Hyp boot PGD not allocated\n");
2010 err = -ENOMEM;
2011 goto out;
2012 }
2013
2014 err = kvm_map_idmap_text(boot_hyp_pgd);
2015 if (err)
2016 goto out;
2017
2018 merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
2019 if (!merged_hyp_pgd) {
2020 kvm_err("Failed to allocate extra HYP pgd\n");
2021 goto out;
2022 }
2023 __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
2024 hyp_idmap_start);
2025 } else {
2026 err = kvm_map_idmap_text(hyp_pgd);
2027 if (err)
2028 goto out;
2029 }
2030
2031 io_map_base = hyp_idmap_start;
2032 return 0;
2033out:
2034 free_hyp_pgds();
2035 return err;
2036}
2037
2038void kvm_arch_commit_memory_region(struct kvm *kvm,
2039 const struct kvm_userspace_memory_region *mem,
2040 const struct kvm_memory_slot *old,
2041 const struct kvm_memory_slot *new,
2042 enum kvm_mr_change change)
2043{
2044 /*
2045 * At this point memslot has been committed and there is an
2046 * allocated dirty_bitmap[], dirty pages will be be tracked while the
2047 * memory slot is write protected.
2048 */
2049 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
2050 kvm_mmu_wp_memory_region(kvm, mem->slot);
2051}
2052
2053int kvm_arch_prepare_memory_region(struct kvm *kvm,
2054 struct kvm_memory_slot *memslot,
2055 const struct kvm_userspace_memory_region *mem,
2056 enum kvm_mr_change change)
2057{
2058 hva_t hva = mem->userspace_addr;
2059 hva_t reg_end = hva + mem->memory_size;
2060 bool writable = !(mem->flags & KVM_MEM_READONLY);
2061 int ret = 0;
2062
2063 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
2064 change != KVM_MR_FLAGS_ONLY)
2065 return 0;
2066
2067 /*
2068 * Prevent userspace from creating a memory region outside of the IPA
2069 * space addressable by the KVM guest IPA space.
2070 */
2071 if (memslot->base_gfn + memslot->npages >=
2072 (KVM_PHYS_SIZE >> PAGE_SHIFT))
2073 return -EFAULT;
2074
2075 down_read(&current->mm->mmap_sem);
2076 /*
2077 * A memory region could potentially cover multiple VMAs, and any holes
2078 * between them, so iterate over all of them to find out if we can map
2079 * any of them right now.
2080 *
2081 * +--------------------------------------------+
2082 * +---------------+----------------+ +----------------+
2083 * | : VMA 1 | VMA 2 | | VMA 3 : |
2084 * +---------------+----------------+ +----------------+
2085 * | memory region |
2086 * +--------------------------------------------+
2087 */
2088 do {
2089 struct vm_area_struct *vma = find_vma(current->mm, hva);
2090 hva_t vm_start, vm_end;
2091
2092 if (!vma || vma->vm_start >= reg_end)
2093 break;
2094
2095 /*
2096 * Mapping a read-only VMA is only allowed if the
2097 * memory region is configured as read-only.
2098 */
2099 if (writable && !(vma->vm_flags & VM_WRITE)) {
2100 ret = -EPERM;
2101 break;
2102 }
2103
2104 /*
2105 * Take the intersection of this VMA with the memory region
2106 */
2107 vm_start = max(hva, vma->vm_start);
2108 vm_end = min(reg_end, vma->vm_end);
2109
2110 if (vma->vm_flags & VM_PFNMAP) {
2111 gpa_t gpa = mem->guest_phys_addr +
2112 (vm_start - mem->userspace_addr);
2113 phys_addr_t pa;
2114
2115 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
2116 pa += vm_start - vma->vm_start;
2117
2118 /* IO region dirty page logging not allowed */
2119 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
2120 ret = -EINVAL;
2121 goto out;
2122 }
2123
2124 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
2125 vm_end - vm_start,
2126 writable);
2127 if (ret)
2128 break;
2129 }
2130 hva = vm_end;
2131 } while (hva < reg_end);
2132
2133 if (change == KVM_MR_FLAGS_ONLY)
2134 goto out;
2135
2136 spin_lock(&kvm->mmu_lock);
2137 if (ret)
2138 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
2139 else
2140 stage2_flush_memslot(kvm, memslot);
2141 spin_unlock(&kvm->mmu_lock);
2142out:
2143 up_read(&current->mm->mmap_sem);
2144 return ret;
2145}
2146
2147void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
2148 struct kvm_memory_slot *dont)
2149{
2150}
2151
2152int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2153 unsigned long npages)
2154{
2155 return 0;
2156}
2157
2158void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
2159{
2160}
2161
2162void kvm_arch_flush_shadow_all(struct kvm *kvm)
2163{
2164 kvm_free_stage2_pgd(kvm);
2165}
2166
2167void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
2168 struct kvm_memory_slot *slot)
2169{
2170 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
2171 phys_addr_t size = slot->npages << PAGE_SHIFT;
2172
2173 spin_lock(&kvm->mmu_lock);
2174 unmap_stage2_range(kvm, gpa, size);
2175 spin_unlock(&kvm->mmu_lock);
2176}
2177
2178/*
2179 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
2180 *
2181 * Main problems:
2182 * - S/W ops are local to a CPU (not broadcast)
2183 * - We have line migration behind our back (speculation)
2184 * - System caches don't support S/W at all (damn!)
2185 *
2186 * In the face of the above, the best we can do is to try and convert
2187 * S/W ops to VA ops. Because the guest is not allowed to infer the
2188 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
2189 * which is a rather good thing for us.
2190 *
2191 * Also, it is only used when turning caches on/off ("The expected
2192 * usage of the cache maintenance instructions that operate by set/way
2193 * is associated with the cache maintenance instructions associated
2194 * with the powerdown and powerup of caches, if this is required by
2195 * the implementation.").
2196 *
2197 * We use the following policy:
2198 *
2199 * - If we trap a S/W operation, we enable VM trapping to detect
2200 * caches being turned on/off, and do a full clean.
2201 *
2202 * - We flush the caches on both caches being turned on and off.
2203 *
2204 * - Once the caches are enabled, we stop trapping VM ops.
2205 */
2206void kvm_set_way_flush(struct kvm_vcpu *vcpu)
2207{
2208 unsigned long hcr = *vcpu_hcr(vcpu);
2209
2210 /*
2211 * If this is the first time we do a S/W operation
2212 * (i.e. HCR_TVM not set) flush the whole memory, and set the
2213 * VM trapping.
2214 *
2215 * Otherwise, rely on the VM trapping to wait for the MMU +
2216 * Caches to be turned off. At that point, we'll be able to
2217 * clean the caches again.
2218 */
2219 if (!(hcr & HCR_TVM)) {
2220 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
2221 vcpu_has_cache_enabled(vcpu));
2222 stage2_flush_vm(vcpu->kvm);
2223 *vcpu_hcr(vcpu) = hcr | HCR_TVM;
2224 }
2225}
2226
2227void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
2228{
2229 bool now_enabled = vcpu_has_cache_enabled(vcpu);
2230
2231 /*
2232 * If switching the MMU+caches on, need to invalidate the caches.
2233 * If switching it off, need to clean the caches.
2234 * Clean + invalidate does the trick always.
2235 */
2236 if (now_enabled != was_enabled)
2237 stage2_flush_vm(vcpu->kvm);
2238
2239 /* Caches are now on, stop trapping VM ops (until a S/W op) */
2240 if (now_enabled)
2241 *vcpu_hcr(vcpu) &= ~HCR_TVM;
2242
2243 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
2244}