xref: /openbmc/linux/arch/arm64/kvm/mmu.c (revision d2825fa9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5  */
6 
7 #include <linux/mman.h>
8 #include <linux/kvm_host.h>
9 #include <linux/io.h>
10 #include <linux/hugetlb.h>
11 #include <linux/sched/signal.h>
12 #include <trace/events/kvm.h>
13 #include <asm/pgalloc.h>
14 #include <asm/cacheflush.h>
15 #include <asm/kvm_arm.h>
16 #include <asm/kvm_mmu.h>
17 #include <asm/kvm_pgtable.h>
18 #include <asm/kvm_ras.h>
19 #include <asm/kvm_asm.h>
20 #include <asm/kvm_emulate.h>
21 #include <asm/virt.h>
22 
23 #include "trace.h"
24 
25 static struct kvm_pgtable *hyp_pgtable;
26 static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
27 
28 static unsigned long hyp_idmap_start;
29 static unsigned long hyp_idmap_end;
30 static phys_addr_t hyp_idmap_vector;
31 
32 static unsigned long io_map_base;
33 
34 
35 /*
36  * Release kvm_mmu_lock periodically if the memory region is large. Otherwise,
37  * we may see kernel panics with CONFIG_DETECT_HUNG_TASK,
38  * CONFIG_LOCKUP_DETECTOR, CONFIG_LOCKDEP. Additionally, holding the lock too
39  * long will also starve other vCPUs. We have to also make sure that the page
40  * tables are not freed while we released the lock.
41  */
42 static int stage2_apply_range(struct kvm *kvm, phys_addr_t addr,
43 			      phys_addr_t end,
44 			      int (*fn)(struct kvm_pgtable *, u64, u64),
45 			      bool resched)
46 {
47 	int ret;
48 	u64 next;
49 
50 	do {
51 		struct kvm_pgtable *pgt = kvm->arch.mmu.pgt;
52 		if (!pgt)
53 			return -EINVAL;
54 
55 		next = stage2_pgd_addr_end(kvm, addr, end);
56 		ret = fn(pgt, addr, next - addr);
57 		if (ret)
58 			break;
59 
60 		if (resched && next != end)
61 			cond_resched_rwlock_write(&kvm->mmu_lock);
62 	} while (addr = next, addr != end);
63 
64 	return ret;
65 }
66 
67 #define stage2_apply_range_resched(kvm, addr, end, fn)			\
68 	stage2_apply_range(kvm, addr, end, fn, true)
69 
70 static bool memslot_is_logging(struct kvm_memory_slot *memslot)
71 {
72 	return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
73 }
74 
75 /**
76  * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
77  * @kvm:	pointer to kvm structure.
78  *
79  * Interface to HYP function to flush all VM TLB entries
80  */
81 void kvm_flush_remote_tlbs(struct kvm *kvm)
82 {
83 	++kvm->stat.generic.remote_tlb_flush_requests;
84 	kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
85 }
86 
87 static bool kvm_is_device_pfn(unsigned long pfn)
88 {
89 	return !pfn_is_map_memory(pfn);
90 }
91 
92 static void *stage2_memcache_zalloc_page(void *arg)
93 {
94 	struct kvm_mmu_memory_cache *mc = arg;
95 
96 	/* Allocated with __GFP_ZERO, so no need to zero */
97 	return kvm_mmu_memory_cache_alloc(mc);
98 }
99 
100 static void *kvm_host_zalloc_pages_exact(size_t size)
101 {
102 	return alloc_pages_exact(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
103 }
104 
105 static void kvm_host_get_page(void *addr)
106 {
107 	get_page(virt_to_page(addr));
108 }
109 
110 static void kvm_host_put_page(void *addr)
111 {
112 	put_page(virt_to_page(addr));
113 }
114 
115 static int kvm_host_page_count(void *addr)
116 {
117 	return page_count(virt_to_page(addr));
118 }
119 
120 static phys_addr_t kvm_host_pa(void *addr)
121 {
122 	return __pa(addr);
123 }
124 
125 static void *kvm_host_va(phys_addr_t phys)
126 {
127 	return __va(phys);
128 }
129 
130 static void clean_dcache_guest_page(void *va, size_t size)
131 {
132 	__clean_dcache_guest_page(va, size);
133 }
134 
135 static void invalidate_icache_guest_page(void *va, size_t size)
136 {
137 	__invalidate_icache_guest_page(va, size);
138 }
139 
140 /*
141  * Unmapping vs dcache management:
142  *
143  * If a guest maps certain memory pages as uncached, all writes will
144  * bypass the data cache and go directly to RAM.  However, the CPUs
145  * can still speculate reads (not writes) and fill cache lines with
146  * data.
147  *
148  * Those cache lines will be *clean* cache lines though, so a
149  * clean+invalidate operation is equivalent to an invalidate
150  * operation, because no cache lines are marked dirty.
151  *
152  * Those clean cache lines could be filled prior to an uncached write
153  * by the guest, and the cache coherent IO subsystem would therefore
154  * end up writing old data to disk.
155  *
156  * This is why right after unmapping a page/section and invalidating
157  * the corresponding TLBs, we flush to make sure the IO subsystem will
158  * never hit in the cache.
159  *
160  * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
161  * we then fully enforce cacheability of RAM, no matter what the guest
162  * does.
163  */
164 /**
165  * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
166  * @mmu:   The KVM stage-2 MMU pointer
167  * @start: The intermediate physical base address of the range to unmap
168  * @size:  The size of the area to unmap
169  * @may_block: Whether or not we are permitted to block
170  *
171  * Clear a range of stage-2 mappings, lowering the various ref-counts.  Must
172  * be called while holding mmu_lock (unless for freeing the stage2 pgd before
173  * destroying the VM), otherwise another faulting VCPU may come in and mess
174  * with things behind our backs.
175  */
176 static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
177 				 bool may_block)
178 {
179 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
180 	phys_addr_t end = start + size;
181 
182 	lockdep_assert_held_write(&kvm->mmu_lock);
183 	WARN_ON(size & ~PAGE_MASK);
184 	WARN_ON(stage2_apply_range(kvm, start, end, kvm_pgtable_stage2_unmap,
185 				   may_block));
186 }
187 
188 static void unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size)
189 {
190 	__unmap_stage2_range(mmu, start, size, true);
191 }
192 
193 static void stage2_flush_memslot(struct kvm *kvm,
194 				 struct kvm_memory_slot *memslot)
195 {
196 	phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
197 	phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
198 
199 	stage2_apply_range_resched(kvm, addr, end, kvm_pgtable_stage2_flush);
200 }
201 
202 /**
203  * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
204  * @kvm: The struct kvm pointer
205  *
206  * Go through the stage 2 page tables and invalidate any cache lines
207  * backing memory already mapped to the VM.
208  */
209 static void stage2_flush_vm(struct kvm *kvm)
210 {
211 	struct kvm_memslots *slots;
212 	struct kvm_memory_slot *memslot;
213 	int idx, bkt;
214 
215 	idx = srcu_read_lock(&kvm->srcu);
216 	write_lock(&kvm->mmu_lock);
217 
218 	slots = kvm_memslots(kvm);
219 	kvm_for_each_memslot(memslot, bkt, slots)
220 		stage2_flush_memslot(kvm, memslot);
221 
222 	write_unlock(&kvm->mmu_lock);
223 	srcu_read_unlock(&kvm->srcu, idx);
224 }
225 
226 /**
227  * free_hyp_pgds - free Hyp-mode page tables
228  */
229 void free_hyp_pgds(void)
230 {
231 	mutex_lock(&kvm_hyp_pgd_mutex);
232 	if (hyp_pgtable) {
233 		kvm_pgtable_hyp_destroy(hyp_pgtable);
234 		kfree(hyp_pgtable);
235 		hyp_pgtable = NULL;
236 	}
237 	mutex_unlock(&kvm_hyp_pgd_mutex);
238 }
239 
240 static bool kvm_host_owns_hyp_mappings(void)
241 {
242 	if (is_kernel_in_hyp_mode())
243 		return false;
244 
245 	if (static_branch_likely(&kvm_protected_mode_initialized))
246 		return false;
247 
248 	/*
249 	 * This can happen at boot time when __create_hyp_mappings() is called
250 	 * after the hyp protection has been enabled, but the static key has
251 	 * not been flipped yet.
252 	 */
253 	if (!hyp_pgtable && is_protected_kvm_enabled())
254 		return false;
255 
256 	WARN_ON(!hyp_pgtable);
257 
258 	return true;
259 }
260 
261 static int __create_hyp_mappings(unsigned long start, unsigned long size,
262 				 unsigned long phys, enum kvm_pgtable_prot prot)
263 {
264 	int err;
265 
266 	if (WARN_ON(!kvm_host_owns_hyp_mappings()))
267 		return -EINVAL;
268 
269 	mutex_lock(&kvm_hyp_pgd_mutex);
270 	err = kvm_pgtable_hyp_map(hyp_pgtable, start, size, phys, prot);
271 	mutex_unlock(&kvm_hyp_pgd_mutex);
272 
273 	return err;
274 }
275 
276 static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
277 {
278 	if (!is_vmalloc_addr(kaddr)) {
279 		BUG_ON(!virt_addr_valid(kaddr));
280 		return __pa(kaddr);
281 	} else {
282 		return page_to_phys(vmalloc_to_page(kaddr)) +
283 		       offset_in_page(kaddr);
284 	}
285 }
286 
287 struct hyp_shared_pfn {
288 	u64 pfn;
289 	int count;
290 	struct rb_node node;
291 };
292 
293 static DEFINE_MUTEX(hyp_shared_pfns_lock);
294 static struct rb_root hyp_shared_pfns = RB_ROOT;
295 
296 static struct hyp_shared_pfn *find_shared_pfn(u64 pfn, struct rb_node ***node,
297 					      struct rb_node **parent)
298 {
299 	struct hyp_shared_pfn *this;
300 
301 	*node = &hyp_shared_pfns.rb_node;
302 	*parent = NULL;
303 	while (**node) {
304 		this = container_of(**node, struct hyp_shared_pfn, node);
305 		*parent = **node;
306 		if (this->pfn < pfn)
307 			*node = &((**node)->rb_left);
308 		else if (this->pfn > pfn)
309 			*node = &((**node)->rb_right);
310 		else
311 			return this;
312 	}
313 
314 	return NULL;
315 }
316 
317 static int share_pfn_hyp(u64 pfn)
318 {
319 	struct rb_node **node, *parent;
320 	struct hyp_shared_pfn *this;
321 	int ret = 0;
322 
323 	mutex_lock(&hyp_shared_pfns_lock);
324 	this = find_shared_pfn(pfn, &node, &parent);
325 	if (this) {
326 		this->count++;
327 		goto unlock;
328 	}
329 
330 	this = kzalloc(sizeof(*this), GFP_KERNEL);
331 	if (!this) {
332 		ret = -ENOMEM;
333 		goto unlock;
334 	}
335 
336 	this->pfn = pfn;
337 	this->count = 1;
338 	rb_link_node(&this->node, parent, node);
339 	rb_insert_color(&this->node, &hyp_shared_pfns);
340 	ret = kvm_call_hyp_nvhe(__pkvm_host_share_hyp, pfn, 1);
341 unlock:
342 	mutex_unlock(&hyp_shared_pfns_lock);
343 
344 	return ret;
345 }
346 
347 static int unshare_pfn_hyp(u64 pfn)
348 {
349 	struct rb_node **node, *parent;
350 	struct hyp_shared_pfn *this;
351 	int ret = 0;
352 
353 	mutex_lock(&hyp_shared_pfns_lock);
354 	this = find_shared_pfn(pfn, &node, &parent);
355 	if (WARN_ON(!this)) {
356 		ret = -ENOENT;
357 		goto unlock;
358 	}
359 
360 	this->count--;
361 	if (this->count)
362 		goto unlock;
363 
364 	rb_erase(&this->node, &hyp_shared_pfns);
365 	kfree(this);
366 	ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, pfn, 1);
367 unlock:
368 	mutex_unlock(&hyp_shared_pfns_lock);
369 
370 	return ret;
371 }
372 
373 int kvm_share_hyp(void *from, void *to)
374 {
375 	phys_addr_t start, end, cur;
376 	u64 pfn;
377 	int ret;
378 
379 	if (is_kernel_in_hyp_mode())
380 		return 0;
381 
382 	/*
383 	 * The share hcall maps things in the 'fixed-offset' region of the hyp
384 	 * VA space, so we can only share physically contiguous data-structures
385 	 * for now.
386 	 */
387 	if (is_vmalloc_or_module_addr(from) || is_vmalloc_or_module_addr(to))
388 		return -EINVAL;
389 
390 	if (kvm_host_owns_hyp_mappings())
391 		return create_hyp_mappings(from, to, PAGE_HYP);
392 
393 	start = ALIGN_DOWN(__pa(from), PAGE_SIZE);
394 	end = PAGE_ALIGN(__pa(to));
395 	for (cur = start; cur < end; cur += PAGE_SIZE) {
396 		pfn = __phys_to_pfn(cur);
397 		ret = share_pfn_hyp(pfn);
398 		if (ret)
399 			return ret;
400 	}
401 
402 	return 0;
403 }
404 
405 void kvm_unshare_hyp(void *from, void *to)
406 {
407 	phys_addr_t start, end, cur;
408 	u64 pfn;
409 
410 	if (is_kernel_in_hyp_mode() || kvm_host_owns_hyp_mappings() || !from)
411 		return;
412 
413 	start = ALIGN_DOWN(__pa(from), PAGE_SIZE);
414 	end = PAGE_ALIGN(__pa(to));
415 	for (cur = start; cur < end; cur += PAGE_SIZE) {
416 		pfn = __phys_to_pfn(cur);
417 		WARN_ON(unshare_pfn_hyp(pfn));
418 	}
419 }
420 
421 /**
422  * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
423  * @from:	The virtual kernel start address of the range
424  * @to:		The virtual kernel end address of the range (exclusive)
425  * @prot:	The protection to be applied to this range
426  *
427  * The same virtual address as the kernel virtual address is also used
428  * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
429  * physical pages.
430  */
431 int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
432 {
433 	phys_addr_t phys_addr;
434 	unsigned long virt_addr;
435 	unsigned long start = kern_hyp_va((unsigned long)from);
436 	unsigned long end = kern_hyp_va((unsigned long)to);
437 
438 	if (is_kernel_in_hyp_mode())
439 		return 0;
440 
441 	if (!kvm_host_owns_hyp_mappings())
442 		return -EPERM;
443 
444 	start = start & PAGE_MASK;
445 	end = PAGE_ALIGN(end);
446 
447 	for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
448 		int err;
449 
450 		phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
451 		err = __create_hyp_mappings(virt_addr, PAGE_SIZE, phys_addr,
452 					    prot);
453 		if (err)
454 			return err;
455 	}
456 
457 	return 0;
458 }
459 
460 static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
461 					unsigned long *haddr,
462 					enum kvm_pgtable_prot prot)
463 {
464 	unsigned long base;
465 	int ret = 0;
466 
467 	if (!kvm_host_owns_hyp_mappings()) {
468 		base = kvm_call_hyp_nvhe(__pkvm_create_private_mapping,
469 					 phys_addr, size, prot);
470 		if (IS_ERR_OR_NULL((void *)base))
471 			return PTR_ERR((void *)base);
472 		*haddr = base;
473 
474 		return 0;
475 	}
476 
477 	mutex_lock(&kvm_hyp_pgd_mutex);
478 
479 	/*
480 	 * This assumes that we have enough space below the idmap
481 	 * page to allocate our VAs. If not, the check below will
482 	 * kick. A potential alternative would be to detect that
483 	 * overflow and switch to an allocation above the idmap.
484 	 *
485 	 * The allocated size is always a multiple of PAGE_SIZE.
486 	 */
487 	size = PAGE_ALIGN(size + offset_in_page(phys_addr));
488 	base = io_map_base - size;
489 
490 	/*
491 	 * Verify that BIT(VA_BITS - 1) hasn't been flipped by
492 	 * allocating the new area, as it would indicate we've
493 	 * overflowed the idmap/IO address range.
494 	 */
495 	if ((base ^ io_map_base) & BIT(VA_BITS - 1))
496 		ret = -ENOMEM;
497 	else
498 		io_map_base = base;
499 
500 	mutex_unlock(&kvm_hyp_pgd_mutex);
501 
502 	if (ret)
503 		goto out;
504 
505 	ret = __create_hyp_mappings(base, size, phys_addr, prot);
506 	if (ret)
507 		goto out;
508 
509 	*haddr = base + offset_in_page(phys_addr);
510 out:
511 	return ret;
512 }
513 
514 /**
515  * create_hyp_io_mappings - Map IO into both kernel and HYP
516  * @phys_addr:	The physical start address which gets mapped
517  * @size:	Size of the region being mapped
518  * @kaddr:	Kernel VA for this mapping
519  * @haddr:	HYP VA for this mapping
520  */
521 int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
522 			   void __iomem **kaddr,
523 			   void __iomem **haddr)
524 {
525 	unsigned long addr;
526 	int ret;
527 
528 	if (is_protected_kvm_enabled())
529 		return -EPERM;
530 
531 	*kaddr = ioremap(phys_addr, size);
532 	if (!*kaddr)
533 		return -ENOMEM;
534 
535 	if (is_kernel_in_hyp_mode()) {
536 		*haddr = *kaddr;
537 		return 0;
538 	}
539 
540 	ret = __create_hyp_private_mapping(phys_addr, size,
541 					   &addr, PAGE_HYP_DEVICE);
542 	if (ret) {
543 		iounmap(*kaddr);
544 		*kaddr = NULL;
545 		*haddr = NULL;
546 		return ret;
547 	}
548 
549 	*haddr = (void __iomem *)addr;
550 	return 0;
551 }
552 
553 /**
554  * create_hyp_exec_mappings - Map an executable range into HYP
555  * @phys_addr:	The physical start address which gets mapped
556  * @size:	Size of the region being mapped
557  * @haddr:	HYP VA for this mapping
558  */
559 int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
560 			     void **haddr)
561 {
562 	unsigned long addr;
563 	int ret;
564 
565 	BUG_ON(is_kernel_in_hyp_mode());
566 
567 	ret = __create_hyp_private_mapping(phys_addr, size,
568 					   &addr, PAGE_HYP_EXEC);
569 	if (ret) {
570 		*haddr = NULL;
571 		return ret;
572 	}
573 
574 	*haddr = (void *)addr;
575 	return 0;
576 }
577 
578 static struct kvm_pgtable_mm_ops kvm_user_mm_ops = {
579 	/* We shouldn't need any other callback to walk the PT */
580 	.phys_to_virt		= kvm_host_va,
581 };
582 
583 static int get_user_mapping_size(struct kvm *kvm, u64 addr)
584 {
585 	struct kvm_pgtable pgt = {
586 		.pgd		= (kvm_pte_t *)kvm->mm->pgd,
587 		.ia_bits	= VA_BITS,
588 		.start_level	= (KVM_PGTABLE_MAX_LEVELS -
589 				   CONFIG_PGTABLE_LEVELS),
590 		.mm_ops		= &kvm_user_mm_ops,
591 	};
592 	kvm_pte_t pte = 0;	/* Keep GCC quiet... */
593 	u32 level = ~0;
594 	int ret;
595 
596 	ret = kvm_pgtable_get_leaf(&pgt, addr, &pte, &level);
597 	VM_BUG_ON(ret);
598 	VM_BUG_ON(level >= KVM_PGTABLE_MAX_LEVELS);
599 	VM_BUG_ON(!(pte & PTE_VALID));
600 
601 	return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
602 }
603 
604 static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
605 	.zalloc_page		= stage2_memcache_zalloc_page,
606 	.zalloc_pages_exact	= kvm_host_zalloc_pages_exact,
607 	.free_pages_exact	= free_pages_exact,
608 	.get_page		= kvm_host_get_page,
609 	.put_page		= kvm_host_put_page,
610 	.page_count		= kvm_host_page_count,
611 	.phys_to_virt		= kvm_host_va,
612 	.virt_to_phys		= kvm_host_pa,
613 	.dcache_clean_inval_poc	= clean_dcache_guest_page,
614 	.icache_inval_pou	= invalidate_icache_guest_page,
615 };
616 
617 /**
618  * kvm_init_stage2_mmu - Initialise a S2 MMU structure
619  * @kvm:	The pointer to the KVM structure
620  * @mmu:	The pointer to the s2 MMU structure
621  *
622  * Allocates only the stage-2 HW PGD level table(s).
623  * Note we don't need locking here as this is only called when the VM is
624  * created, which can only be done once.
625  */
626 int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)
627 {
628 	int cpu, err;
629 	struct kvm_pgtable *pgt;
630 
631 	if (mmu->pgt != NULL) {
632 		kvm_err("kvm_arch already initialized?\n");
633 		return -EINVAL;
634 	}
635 
636 	pgt = kzalloc(sizeof(*pgt), GFP_KERNEL_ACCOUNT);
637 	if (!pgt)
638 		return -ENOMEM;
639 
640 	mmu->arch = &kvm->arch;
641 	err = kvm_pgtable_stage2_init(pgt, mmu, &kvm_s2_mm_ops);
642 	if (err)
643 		goto out_free_pgtable;
644 
645 	mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
646 	if (!mmu->last_vcpu_ran) {
647 		err = -ENOMEM;
648 		goto out_destroy_pgtable;
649 	}
650 
651 	for_each_possible_cpu(cpu)
652 		*per_cpu_ptr(mmu->last_vcpu_ran, cpu) = -1;
653 
654 	mmu->pgt = pgt;
655 	mmu->pgd_phys = __pa(pgt->pgd);
656 	return 0;
657 
658 out_destroy_pgtable:
659 	kvm_pgtable_stage2_destroy(pgt);
660 out_free_pgtable:
661 	kfree(pgt);
662 	return err;
663 }
664 
665 static void stage2_unmap_memslot(struct kvm *kvm,
666 				 struct kvm_memory_slot *memslot)
667 {
668 	hva_t hva = memslot->userspace_addr;
669 	phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
670 	phys_addr_t size = PAGE_SIZE * memslot->npages;
671 	hva_t reg_end = hva + size;
672 
673 	/*
674 	 * A memory region could potentially cover multiple VMAs, and any holes
675 	 * between them, so iterate over all of them to find out if we should
676 	 * unmap any of them.
677 	 *
678 	 *     +--------------------------------------------+
679 	 * +---------------+----------------+   +----------------+
680 	 * |   : VMA 1     |      VMA 2     |   |    VMA 3  :    |
681 	 * +---------------+----------------+   +----------------+
682 	 *     |               memory region                |
683 	 *     +--------------------------------------------+
684 	 */
685 	do {
686 		struct vm_area_struct *vma;
687 		hva_t vm_start, vm_end;
688 
689 		vma = find_vma_intersection(current->mm, hva, reg_end);
690 		if (!vma)
691 			break;
692 
693 		/*
694 		 * Take the intersection of this VMA with the memory region
695 		 */
696 		vm_start = max(hva, vma->vm_start);
697 		vm_end = min(reg_end, vma->vm_end);
698 
699 		if (!(vma->vm_flags & VM_PFNMAP)) {
700 			gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
701 			unmap_stage2_range(&kvm->arch.mmu, gpa, vm_end - vm_start);
702 		}
703 		hva = vm_end;
704 	} while (hva < reg_end);
705 }
706 
707 /**
708  * stage2_unmap_vm - Unmap Stage-2 RAM mappings
709  * @kvm: The struct kvm pointer
710  *
711  * Go through the memregions and unmap any regular RAM
712  * backing memory already mapped to the VM.
713  */
714 void stage2_unmap_vm(struct kvm *kvm)
715 {
716 	struct kvm_memslots *slots;
717 	struct kvm_memory_slot *memslot;
718 	int idx, bkt;
719 
720 	idx = srcu_read_lock(&kvm->srcu);
721 	mmap_read_lock(current->mm);
722 	write_lock(&kvm->mmu_lock);
723 
724 	slots = kvm_memslots(kvm);
725 	kvm_for_each_memslot(memslot, bkt, slots)
726 		stage2_unmap_memslot(kvm, memslot);
727 
728 	write_unlock(&kvm->mmu_lock);
729 	mmap_read_unlock(current->mm);
730 	srcu_read_unlock(&kvm->srcu, idx);
731 }
732 
733 void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
734 {
735 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
736 	struct kvm_pgtable *pgt = NULL;
737 
738 	write_lock(&kvm->mmu_lock);
739 	pgt = mmu->pgt;
740 	if (pgt) {
741 		mmu->pgd_phys = 0;
742 		mmu->pgt = NULL;
743 		free_percpu(mmu->last_vcpu_ran);
744 	}
745 	write_unlock(&kvm->mmu_lock);
746 
747 	if (pgt) {
748 		kvm_pgtable_stage2_destroy(pgt);
749 		kfree(pgt);
750 	}
751 }
752 
753 /**
754  * kvm_phys_addr_ioremap - map a device range to guest IPA
755  *
756  * @kvm:	The KVM pointer
757  * @guest_ipa:	The IPA at which to insert the mapping
758  * @pa:		The physical address of the device
759  * @size:	The size of the mapping
760  * @writable:   Whether or not to create a writable mapping
761  */
762 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
763 			  phys_addr_t pa, unsigned long size, bool writable)
764 {
765 	phys_addr_t addr;
766 	int ret = 0;
767 	struct kvm_mmu_memory_cache cache = { 0, __GFP_ZERO, NULL, };
768 	struct kvm_pgtable *pgt = kvm->arch.mmu.pgt;
769 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_DEVICE |
770 				     KVM_PGTABLE_PROT_R |
771 				     (writable ? KVM_PGTABLE_PROT_W : 0);
772 
773 	if (is_protected_kvm_enabled())
774 		return -EPERM;
775 
776 	size += offset_in_page(guest_ipa);
777 	guest_ipa &= PAGE_MASK;
778 
779 	for (addr = guest_ipa; addr < guest_ipa + size; addr += PAGE_SIZE) {
780 		ret = kvm_mmu_topup_memory_cache(&cache,
781 						 kvm_mmu_cache_min_pages(kvm));
782 		if (ret)
783 			break;
784 
785 		write_lock(&kvm->mmu_lock);
786 		ret = kvm_pgtable_stage2_map(pgt, addr, PAGE_SIZE, pa, prot,
787 					     &cache);
788 		write_unlock(&kvm->mmu_lock);
789 		if (ret)
790 			break;
791 
792 		pa += PAGE_SIZE;
793 	}
794 
795 	kvm_mmu_free_memory_cache(&cache);
796 	return ret;
797 }
798 
799 /**
800  * stage2_wp_range() - write protect stage2 memory region range
801  * @mmu:        The KVM stage-2 MMU pointer
802  * @addr:	Start address of range
803  * @end:	End address of range
804  */
805 static void stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
806 {
807 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
808 	stage2_apply_range_resched(kvm, addr, end, kvm_pgtable_stage2_wrprotect);
809 }
810 
811 /**
812  * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
813  * @kvm:	The KVM pointer
814  * @slot:	The memory slot to write protect
815  *
816  * Called to start logging dirty pages after memory region
817  * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
818  * all present PUD, PMD and PTEs are write protected in the memory region.
819  * Afterwards read of dirty page log can be called.
820  *
821  * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
822  * serializing operations for VM memory regions.
823  */
824 static void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
825 {
826 	struct kvm_memslots *slots = kvm_memslots(kvm);
827 	struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
828 	phys_addr_t start, end;
829 
830 	if (WARN_ON_ONCE(!memslot))
831 		return;
832 
833 	start = memslot->base_gfn << PAGE_SHIFT;
834 	end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
835 
836 	write_lock(&kvm->mmu_lock);
837 	stage2_wp_range(&kvm->arch.mmu, start, end);
838 	write_unlock(&kvm->mmu_lock);
839 	kvm_flush_remote_tlbs(kvm);
840 }
841 
842 /**
843  * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
844  * @kvm:	The KVM pointer
845  * @slot:	The memory slot associated with mask
846  * @gfn_offset:	The gfn offset in memory slot
847  * @mask:	The mask of dirty pages at offset 'gfn_offset' in this memory
848  *		slot to be write protected
849  *
850  * Walks bits set in mask write protects the associated pte's. Caller must
851  * acquire kvm_mmu_lock.
852  */
853 static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
854 		struct kvm_memory_slot *slot,
855 		gfn_t gfn_offset, unsigned long mask)
856 {
857 	phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
858 	phys_addr_t start = (base_gfn +  __ffs(mask)) << PAGE_SHIFT;
859 	phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
860 
861 	stage2_wp_range(&kvm->arch.mmu, start, end);
862 }
863 
864 /*
865  * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
866  * dirty pages.
867  *
868  * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
869  * enable dirty logging for them.
870  */
871 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
872 		struct kvm_memory_slot *slot,
873 		gfn_t gfn_offset, unsigned long mask)
874 {
875 	kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
876 }
877 
878 static void kvm_send_hwpoison_signal(unsigned long address, short lsb)
879 {
880 	send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, current);
881 }
882 
883 static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,
884 					       unsigned long hva,
885 					       unsigned long map_size)
886 {
887 	gpa_t gpa_start;
888 	hva_t uaddr_start, uaddr_end;
889 	size_t size;
890 
891 	/* The memslot and the VMA are guaranteed to be aligned to PAGE_SIZE */
892 	if (map_size == PAGE_SIZE)
893 		return true;
894 
895 	size = memslot->npages * PAGE_SIZE;
896 
897 	gpa_start = memslot->base_gfn << PAGE_SHIFT;
898 
899 	uaddr_start = memslot->userspace_addr;
900 	uaddr_end = uaddr_start + size;
901 
902 	/*
903 	 * Pages belonging to memslots that don't have the same alignment
904 	 * within a PMD/PUD for userspace and IPA cannot be mapped with stage-2
905 	 * PMD/PUD entries, because we'll end up mapping the wrong pages.
906 	 *
907 	 * Consider a layout like the following:
908 	 *
909 	 *    memslot->userspace_addr:
910 	 *    +-----+--------------------+--------------------+---+
911 	 *    |abcde|fgh  Stage-1 block  |    Stage-1 block tv|xyz|
912 	 *    +-----+--------------------+--------------------+---+
913 	 *
914 	 *    memslot->base_gfn << PAGE_SHIFT:
915 	 *      +---+--------------------+--------------------+-----+
916 	 *      |abc|def  Stage-2 block  |    Stage-2 block   |tvxyz|
917 	 *      +---+--------------------+--------------------+-----+
918 	 *
919 	 * If we create those stage-2 blocks, we'll end up with this incorrect
920 	 * mapping:
921 	 *   d -> f
922 	 *   e -> g
923 	 *   f -> h
924 	 */
925 	if ((gpa_start & (map_size - 1)) != (uaddr_start & (map_size - 1)))
926 		return false;
927 
928 	/*
929 	 * Next, let's make sure we're not trying to map anything not covered
930 	 * by the memslot. This means we have to prohibit block size mappings
931 	 * for the beginning and end of a non-block aligned and non-block sized
932 	 * memory slot (illustrated by the head and tail parts of the
933 	 * userspace view above containing pages 'abcde' and 'xyz',
934 	 * respectively).
935 	 *
936 	 * Note that it doesn't matter if we do the check using the
937 	 * userspace_addr or the base_gfn, as both are equally aligned (per
938 	 * the check above) and equally sized.
939 	 */
940 	return (hva & ~(map_size - 1)) >= uaddr_start &&
941 	       (hva & ~(map_size - 1)) + map_size <= uaddr_end;
942 }
943 
944 /*
945  * Check if the given hva is backed by a transparent huge page (THP) and
946  * whether it can be mapped using block mapping in stage2. If so, adjust
947  * the stage2 PFN and IPA accordingly. Only PMD_SIZE THPs are currently
948  * supported. This will need to be updated to support other THP sizes.
949  *
950  * Returns the size of the mapping.
951  */
952 static unsigned long
953 transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot,
954 			    unsigned long hva, kvm_pfn_t *pfnp,
955 			    phys_addr_t *ipap)
956 {
957 	kvm_pfn_t pfn = *pfnp;
958 
959 	/*
960 	 * Make sure the adjustment is done only for THP pages. Also make
961 	 * sure that the HVA and IPA are sufficiently aligned and that the
962 	 * block map is contained within the memslot.
963 	 */
964 	if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE) &&
965 	    get_user_mapping_size(kvm, hva) >= PMD_SIZE) {
966 		/*
967 		 * The address we faulted on is backed by a transparent huge
968 		 * page.  However, because we map the compound huge page and
969 		 * not the individual tail page, we need to transfer the
970 		 * refcount to the head page.  We have to be careful that the
971 		 * THP doesn't start to split while we are adjusting the
972 		 * refcounts.
973 		 *
974 		 * We are sure this doesn't happen, because mmu_notifier_retry
975 		 * was successful and we are holding the mmu_lock, so if this
976 		 * THP is trying to split, it will be blocked in the mmu
977 		 * notifier before touching any of the pages, specifically
978 		 * before being able to call __split_huge_page_refcount().
979 		 *
980 		 * We can therefore safely transfer the refcount from PG_tail
981 		 * to PG_head and switch the pfn from a tail page to the head
982 		 * page accordingly.
983 		 */
984 		*ipap &= PMD_MASK;
985 		kvm_release_pfn_clean(pfn);
986 		pfn &= ~(PTRS_PER_PMD - 1);
987 		get_page(pfn_to_page(pfn));
988 		*pfnp = pfn;
989 
990 		return PMD_SIZE;
991 	}
992 
993 	/* Use page mapping if we cannot use block mapping. */
994 	return PAGE_SIZE;
995 }
996 
997 static int get_vma_page_shift(struct vm_area_struct *vma, unsigned long hva)
998 {
999 	unsigned long pa;
1000 
1001 	if (is_vm_hugetlb_page(vma) && !(vma->vm_flags & VM_PFNMAP))
1002 		return huge_page_shift(hstate_vma(vma));
1003 
1004 	if (!(vma->vm_flags & VM_PFNMAP))
1005 		return PAGE_SHIFT;
1006 
1007 	VM_BUG_ON(is_vm_hugetlb_page(vma));
1008 
1009 	pa = (vma->vm_pgoff << PAGE_SHIFT) + (hva - vma->vm_start);
1010 
1011 #ifndef __PAGETABLE_PMD_FOLDED
1012 	if ((hva & (PUD_SIZE - 1)) == (pa & (PUD_SIZE - 1)) &&
1013 	    ALIGN_DOWN(hva, PUD_SIZE) >= vma->vm_start &&
1014 	    ALIGN(hva, PUD_SIZE) <= vma->vm_end)
1015 		return PUD_SHIFT;
1016 #endif
1017 
1018 	if ((hva & (PMD_SIZE - 1)) == (pa & (PMD_SIZE - 1)) &&
1019 	    ALIGN_DOWN(hva, PMD_SIZE) >= vma->vm_start &&
1020 	    ALIGN(hva, PMD_SIZE) <= vma->vm_end)
1021 		return PMD_SHIFT;
1022 
1023 	return PAGE_SHIFT;
1024 }
1025 
1026 /*
1027  * The page will be mapped in stage 2 as Normal Cacheable, so the VM will be
1028  * able to see the page's tags and therefore they must be initialised first. If
1029  * PG_mte_tagged is set, tags have already been initialised.
1030  *
1031  * The race in the test/set of the PG_mte_tagged flag is handled by:
1032  * - preventing VM_SHARED mappings in a memslot with MTE preventing two VMs
1033  *   racing to santise the same page
1034  * - mmap_lock protects between a VM faulting a page in and the VMM performing
1035  *   an mprotect() to add VM_MTE
1036  */
1037 static int sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
1038 			     unsigned long size)
1039 {
1040 	unsigned long i, nr_pages = size >> PAGE_SHIFT;
1041 	struct page *page;
1042 
1043 	if (!kvm_has_mte(kvm))
1044 		return 0;
1045 
1046 	/*
1047 	 * pfn_to_online_page() is used to reject ZONE_DEVICE pages
1048 	 * that may not support tags.
1049 	 */
1050 	page = pfn_to_online_page(pfn);
1051 
1052 	if (!page)
1053 		return -EFAULT;
1054 
1055 	for (i = 0; i < nr_pages; i++, page++) {
1056 		if (!test_bit(PG_mte_tagged, &page->flags)) {
1057 			mte_clear_page_tags(page_address(page));
1058 			set_bit(PG_mte_tagged, &page->flags);
1059 		}
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1066 			  struct kvm_memory_slot *memslot, unsigned long hva,
1067 			  unsigned long fault_status)
1068 {
1069 	int ret = 0;
1070 	bool write_fault, writable, force_pte = false;
1071 	bool exec_fault;
1072 	bool device = false;
1073 	bool shared;
1074 	unsigned long mmu_seq;
1075 	struct kvm *kvm = vcpu->kvm;
1076 	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
1077 	struct vm_area_struct *vma;
1078 	short vma_shift;
1079 	gfn_t gfn;
1080 	kvm_pfn_t pfn;
1081 	bool logging_active = memslot_is_logging(memslot);
1082 	bool logging_perm_fault = false;
1083 	unsigned long fault_level = kvm_vcpu_trap_get_fault_level(vcpu);
1084 	unsigned long vma_pagesize, fault_granule;
1085 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
1086 	struct kvm_pgtable *pgt;
1087 
1088 	fault_granule = 1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(fault_level);
1089 	write_fault = kvm_is_write_fault(vcpu);
1090 	exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu);
1091 	VM_BUG_ON(write_fault && exec_fault);
1092 
1093 	if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
1094 		kvm_err("Unexpected L2 read permission error\n");
1095 		return -EFAULT;
1096 	}
1097 
1098 	/*
1099 	 * Let's check if we will get back a huge page backed by hugetlbfs, or
1100 	 * get block mapping for device MMIO region.
1101 	 */
1102 	mmap_read_lock(current->mm);
1103 	vma = vma_lookup(current->mm, hva);
1104 	if (unlikely(!vma)) {
1105 		kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1106 		mmap_read_unlock(current->mm);
1107 		return -EFAULT;
1108 	}
1109 
1110 	/*
1111 	 * logging_active is guaranteed to never be true for VM_PFNMAP
1112 	 * memslots.
1113 	 */
1114 	if (logging_active) {
1115 		force_pte = true;
1116 		vma_shift = PAGE_SHIFT;
1117 		logging_perm_fault = (fault_status == FSC_PERM && write_fault);
1118 	} else {
1119 		vma_shift = get_vma_page_shift(vma, hva);
1120 	}
1121 
1122 	shared = (vma->vm_flags & VM_SHARED);
1123 
1124 	switch (vma_shift) {
1125 #ifndef __PAGETABLE_PMD_FOLDED
1126 	case PUD_SHIFT:
1127 		if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
1128 			break;
1129 		fallthrough;
1130 #endif
1131 	case CONT_PMD_SHIFT:
1132 		vma_shift = PMD_SHIFT;
1133 		fallthrough;
1134 	case PMD_SHIFT:
1135 		if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
1136 			break;
1137 		fallthrough;
1138 	case CONT_PTE_SHIFT:
1139 		vma_shift = PAGE_SHIFT;
1140 		force_pte = true;
1141 		fallthrough;
1142 	case PAGE_SHIFT:
1143 		break;
1144 	default:
1145 		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
1146 	}
1147 
1148 	vma_pagesize = 1UL << vma_shift;
1149 	if (vma_pagesize == PMD_SIZE || vma_pagesize == PUD_SIZE)
1150 		fault_ipa &= ~(vma_pagesize - 1);
1151 
1152 	gfn = fault_ipa >> PAGE_SHIFT;
1153 	mmap_read_unlock(current->mm);
1154 
1155 	/*
1156 	 * Permission faults just need to update the existing leaf entry,
1157 	 * and so normally don't require allocations from the memcache. The
1158 	 * only exception to this is when dirty logging is enabled at runtime
1159 	 * and a write fault needs to collapse a block entry into a table.
1160 	 */
1161 	if (fault_status != FSC_PERM || (logging_active && write_fault)) {
1162 		ret = kvm_mmu_topup_memory_cache(memcache,
1163 						 kvm_mmu_cache_min_pages(kvm));
1164 		if (ret)
1165 			return ret;
1166 	}
1167 
1168 	mmu_seq = vcpu->kvm->mmu_notifier_seq;
1169 	/*
1170 	 * Ensure the read of mmu_notifier_seq happens before we call
1171 	 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1172 	 * the page we just got a reference to gets unmapped before we have a
1173 	 * chance to grab the mmu_lock, which ensure that if the page gets
1174 	 * unmapped afterwards, the call to kvm_unmap_gfn will take it away
1175 	 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1176 	 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1177 	 *
1178 	 * Besides, __gfn_to_pfn_memslot() instead of gfn_to_pfn_prot() is
1179 	 * used to avoid unnecessary overhead introduced to locate the memory
1180 	 * slot because it's always fixed even @gfn is adjusted for huge pages.
1181 	 */
1182 	smp_rmb();
1183 
1184 	pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
1185 				   write_fault, &writable, NULL);
1186 	if (pfn == KVM_PFN_ERR_HWPOISON) {
1187 		kvm_send_hwpoison_signal(hva, vma_shift);
1188 		return 0;
1189 	}
1190 	if (is_error_noslot_pfn(pfn))
1191 		return -EFAULT;
1192 
1193 	if (kvm_is_device_pfn(pfn)) {
1194 		/*
1195 		 * If the page was identified as device early by looking at
1196 		 * the VMA flags, vma_pagesize is already representing the
1197 		 * largest quantity we can map.  If instead it was mapped
1198 		 * via gfn_to_pfn_prot(), vma_pagesize is set to PAGE_SIZE
1199 		 * and must not be upgraded.
1200 		 *
1201 		 * In both cases, we don't let transparent_hugepage_adjust()
1202 		 * change things at the last minute.
1203 		 */
1204 		device = true;
1205 	} else if (logging_active && !write_fault) {
1206 		/*
1207 		 * Only actually map the page as writable if this was a write
1208 		 * fault.
1209 		 */
1210 		writable = false;
1211 	}
1212 
1213 	if (exec_fault && device)
1214 		return -ENOEXEC;
1215 
1216 	/*
1217 	 * To reduce MMU contentions and enhance concurrency during dirty
1218 	 * logging dirty logging, only acquire read lock for permission
1219 	 * relaxation.
1220 	 */
1221 	if (logging_perm_fault)
1222 		read_lock(&kvm->mmu_lock);
1223 	else
1224 		write_lock(&kvm->mmu_lock);
1225 	pgt = vcpu->arch.hw_mmu->pgt;
1226 	if (mmu_notifier_retry(kvm, mmu_seq))
1227 		goto out_unlock;
1228 
1229 	/*
1230 	 * If we are not forced to use page mapping, check if we are
1231 	 * backed by a THP and thus use block mapping if possible.
1232 	 */
1233 	if (vma_pagesize == PAGE_SIZE && !(force_pte || device)) {
1234 		if (fault_status == FSC_PERM && fault_granule > PAGE_SIZE)
1235 			vma_pagesize = fault_granule;
1236 		else
1237 			vma_pagesize = transparent_hugepage_adjust(kvm, memslot,
1238 								   hva, &pfn,
1239 								   &fault_ipa);
1240 	}
1241 
1242 	if (fault_status != FSC_PERM && !device && kvm_has_mte(kvm)) {
1243 		/* Check the VMM hasn't introduced a new VM_SHARED VMA */
1244 		if (!shared)
1245 			ret = sanitise_mte_tags(kvm, pfn, vma_pagesize);
1246 		else
1247 			ret = -EFAULT;
1248 		if (ret)
1249 			goto out_unlock;
1250 	}
1251 
1252 	if (writable)
1253 		prot |= KVM_PGTABLE_PROT_W;
1254 
1255 	if (exec_fault)
1256 		prot |= KVM_PGTABLE_PROT_X;
1257 
1258 	if (device)
1259 		prot |= KVM_PGTABLE_PROT_DEVICE;
1260 	else if (cpus_have_const_cap(ARM64_HAS_CACHE_DIC))
1261 		prot |= KVM_PGTABLE_PROT_X;
1262 
1263 	/*
1264 	 * Under the premise of getting a FSC_PERM fault, we just need to relax
1265 	 * permissions only if vma_pagesize equals fault_granule. Otherwise,
1266 	 * kvm_pgtable_stage2_map() should be called to change block size.
1267 	 */
1268 	if (fault_status == FSC_PERM && vma_pagesize == fault_granule) {
1269 		ret = kvm_pgtable_stage2_relax_perms(pgt, fault_ipa, prot);
1270 	} else {
1271 		ret = kvm_pgtable_stage2_map(pgt, fault_ipa, vma_pagesize,
1272 					     __pfn_to_phys(pfn), prot,
1273 					     memcache);
1274 	}
1275 
1276 	/* Mark the page dirty only if the fault is handled successfully */
1277 	if (writable && !ret) {
1278 		kvm_set_pfn_dirty(pfn);
1279 		mark_page_dirty_in_slot(kvm, memslot, gfn);
1280 	}
1281 
1282 out_unlock:
1283 	if (logging_perm_fault)
1284 		read_unlock(&kvm->mmu_lock);
1285 	else
1286 		write_unlock(&kvm->mmu_lock);
1287 	kvm_set_pfn_accessed(pfn);
1288 	kvm_release_pfn_clean(pfn);
1289 	return ret != -EAGAIN ? ret : 0;
1290 }
1291 
1292 /* Resolve the access fault by making the page young again. */
1293 static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1294 {
1295 	pte_t pte;
1296 	kvm_pte_t kpte;
1297 	struct kvm_s2_mmu *mmu;
1298 
1299 	trace_kvm_access_fault(fault_ipa);
1300 
1301 	write_lock(&vcpu->kvm->mmu_lock);
1302 	mmu = vcpu->arch.hw_mmu;
1303 	kpte = kvm_pgtable_stage2_mkyoung(mmu->pgt, fault_ipa);
1304 	write_unlock(&vcpu->kvm->mmu_lock);
1305 
1306 	pte = __pte(kpte);
1307 	if (pte_valid(pte))
1308 		kvm_set_pfn_accessed(pte_pfn(pte));
1309 }
1310 
1311 /**
1312  * kvm_handle_guest_abort - handles all 2nd stage aborts
1313  * @vcpu:	the VCPU pointer
1314  *
1315  * Any abort that gets to the host is almost guaranteed to be caused by a
1316  * missing second stage translation table entry, which can mean that either the
1317  * guest simply needs more memory and we must allocate an appropriate page or it
1318  * can mean that the guest tried to access I/O memory, which is emulated by user
1319  * space. The distinction is based on the IPA causing the fault and whether this
1320  * memory region has been registered as standard RAM by user space.
1321  */
1322 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
1323 {
1324 	unsigned long fault_status;
1325 	phys_addr_t fault_ipa;
1326 	struct kvm_memory_slot *memslot;
1327 	unsigned long hva;
1328 	bool is_iabt, write_fault, writable;
1329 	gfn_t gfn;
1330 	int ret, idx;
1331 
1332 	fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
1333 
1334 	fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
1335 	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
1336 
1337 	/* Synchronous External Abort? */
1338 	if (kvm_vcpu_abt_issea(vcpu)) {
1339 		/*
1340 		 * For RAS the host kernel may handle this abort.
1341 		 * There is no need to pass the error into the guest.
1342 		 */
1343 		if (kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_esr(vcpu)))
1344 			kvm_inject_vabt(vcpu);
1345 
1346 		return 1;
1347 	}
1348 
1349 	trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_esr(vcpu),
1350 			      kvm_vcpu_get_hfar(vcpu), fault_ipa);
1351 
1352 	/* Check the stage-2 fault is trans. fault or write fault */
1353 	if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1354 	    fault_status != FSC_ACCESS) {
1355 		kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1356 			kvm_vcpu_trap_get_class(vcpu),
1357 			(unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1358 			(unsigned long)kvm_vcpu_get_esr(vcpu));
1359 		return -EFAULT;
1360 	}
1361 
1362 	idx = srcu_read_lock(&vcpu->kvm->srcu);
1363 
1364 	gfn = fault_ipa >> PAGE_SHIFT;
1365 	memslot = gfn_to_memslot(vcpu->kvm, gfn);
1366 	hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
1367 	write_fault = kvm_is_write_fault(vcpu);
1368 	if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
1369 		/*
1370 		 * The guest has put either its instructions or its page-tables
1371 		 * somewhere it shouldn't have. Userspace won't be able to do
1372 		 * anything about this (there's no syndrome for a start), so
1373 		 * re-inject the abort back into the guest.
1374 		 */
1375 		if (is_iabt) {
1376 			ret = -ENOEXEC;
1377 			goto out;
1378 		}
1379 
1380 		if (kvm_vcpu_abt_iss1tw(vcpu)) {
1381 			kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
1382 			ret = 1;
1383 			goto out_unlock;
1384 		}
1385 
1386 		/*
1387 		 * Check for a cache maintenance operation. Since we
1388 		 * ended-up here, we know it is outside of any memory
1389 		 * slot. But we can't find out if that is for a device,
1390 		 * or if the guest is just being stupid. The only thing
1391 		 * we know for sure is that this range cannot be cached.
1392 		 *
1393 		 * So let's assume that the guest is just being
1394 		 * cautious, and skip the instruction.
1395 		 */
1396 		if (kvm_is_error_hva(hva) && kvm_vcpu_dabt_is_cm(vcpu)) {
1397 			kvm_incr_pc(vcpu);
1398 			ret = 1;
1399 			goto out_unlock;
1400 		}
1401 
1402 		/*
1403 		 * The IPA is reported as [MAX:12], so we need to
1404 		 * complement it with the bottom 12 bits from the
1405 		 * faulting VA. This is always 12 bits, irrespective
1406 		 * of the page size.
1407 		 */
1408 		fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
1409 		ret = io_mem_abort(vcpu, fault_ipa);
1410 		goto out_unlock;
1411 	}
1412 
1413 	/* Userspace should not be able to register out-of-bounds IPAs */
1414 	VM_BUG_ON(fault_ipa >= kvm_phys_size(vcpu->kvm));
1415 
1416 	if (fault_status == FSC_ACCESS) {
1417 		handle_access_fault(vcpu, fault_ipa);
1418 		ret = 1;
1419 		goto out_unlock;
1420 	}
1421 
1422 	ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
1423 	if (ret == 0)
1424 		ret = 1;
1425 out:
1426 	if (ret == -ENOEXEC) {
1427 		kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
1428 		ret = 1;
1429 	}
1430 out_unlock:
1431 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
1432 	return ret;
1433 }
1434 
1435 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
1436 {
1437 	if (!kvm->arch.mmu.pgt)
1438 		return false;
1439 
1440 	__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
1441 			     (range->end - range->start) << PAGE_SHIFT,
1442 			     range->may_block);
1443 
1444 	return false;
1445 }
1446 
1447 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1448 {
1449 	kvm_pfn_t pfn = pte_pfn(range->pte);
1450 	int ret;
1451 
1452 	if (!kvm->arch.mmu.pgt)
1453 		return false;
1454 
1455 	WARN_ON(range->end - range->start != 1);
1456 
1457 	ret = sanitise_mte_tags(kvm, pfn, PAGE_SIZE);
1458 	if (ret)
1459 		return false;
1460 
1461 	/*
1462 	 * We've moved a page around, probably through CoW, so let's treat
1463 	 * it just like a translation fault and the map handler will clean
1464 	 * the cache to the PoC.
1465 	 *
1466 	 * The MMU notifiers will have unmapped a huge PMD before calling
1467 	 * ->change_pte() (which in turn calls kvm_set_spte_gfn()) and
1468 	 * therefore we never need to clear out a huge PMD through this
1469 	 * calling path and a memcache is not required.
1470 	 */
1471 	kvm_pgtable_stage2_map(kvm->arch.mmu.pgt, range->start << PAGE_SHIFT,
1472 			       PAGE_SIZE, __pfn_to_phys(pfn),
1473 			       KVM_PGTABLE_PROT_R, NULL);
1474 
1475 	return false;
1476 }
1477 
1478 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1479 {
1480 	u64 size = (range->end - range->start) << PAGE_SHIFT;
1481 	kvm_pte_t kpte;
1482 	pte_t pte;
1483 
1484 	if (!kvm->arch.mmu.pgt)
1485 		return false;
1486 
1487 	WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
1488 
1489 	kpte = kvm_pgtable_stage2_mkold(kvm->arch.mmu.pgt,
1490 					range->start << PAGE_SHIFT);
1491 	pte = __pte(kpte);
1492 	return pte_valid(pte) && pte_young(pte);
1493 }
1494 
1495 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1496 {
1497 	if (!kvm->arch.mmu.pgt)
1498 		return false;
1499 
1500 	return kvm_pgtable_stage2_is_young(kvm->arch.mmu.pgt,
1501 					   range->start << PAGE_SHIFT);
1502 }
1503 
1504 phys_addr_t kvm_mmu_get_httbr(void)
1505 {
1506 	return __pa(hyp_pgtable->pgd);
1507 }
1508 
1509 phys_addr_t kvm_get_idmap_vector(void)
1510 {
1511 	return hyp_idmap_vector;
1512 }
1513 
1514 static int kvm_map_idmap_text(void)
1515 {
1516 	unsigned long size = hyp_idmap_end - hyp_idmap_start;
1517 	int err = __create_hyp_mappings(hyp_idmap_start, size, hyp_idmap_start,
1518 					PAGE_HYP_EXEC);
1519 	if (err)
1520 		kvm_err("Failed to idmap %lx-%lx\n",
1521 			hyp_idmap_start, hyp_idmap_end);
1522 
1523 	return err;
1524 }
1525 
1526 static void *kvm_hyp_zalloc_page(void *arg)
1527 {
1528 	return (void *)get_zeroed_page(GFP_KERNEL);
1529 }
1530 
1531 static struct kvm_pgtable_mm_ops kvm_hyp_mm_ops = {
1532 	.zalloc_page		= kvm_hyp_zalloc_page,
1533 	.get_page		= kvm_host_get_page,
1534 	.put_page		= kvm_host_put_page,
1535 	.phys_to_virt		= kvm_host_va,
1536 	.virt_to_phys		= kvm_host_pa,
1537 };
1538 
1539 int kvm_mmu_init(u32 *hyp_va_bits)
1540 {
1541 	int err;
1542 
1543 	hyp_idmap_start = __pa_symbol(__hyp_idmap_text_start);
1544 	hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
1545 	hyp_idmap_end = __pa_symbol(__hyp_idmap_text_end);
1546 	hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
1547 	hyp_idmap_vector = __pa_symbol(__kvm_hyp_init);
1548 
1549 	/*
1550 	 * We rely on the linker script to ensure at build time that the HYP
1551 	 * init code does not cross a page boundary.
1552 	 */
1553 	BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
1554 
1555 	*hyp_va_bits = 64 - ((idmap_t0sz & TCR_T0SZ_MASK) >> TCR_T0SZ_OFFSET);
1556 	kvm_debug("Using %u-bit virtual addresses at EL2\n", *hyp_va_bits);
1557 	kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
1558 	kvm_debug("HYP VA range: %lx:%lx\n",
1559 		  kern_hyp_va(PAGE_OFFSET),
1560 		  kern_hyp_va((unsigned long)high_memory - 1));
1561 
1562 	if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
1563 	    hyp_idmap_start <  kern_hyp_va((unsigned long)high_memory - 1) &&
1564 	    hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
1565 		/*
1566 		 * The idmap page is intersecting with the VA space,
1567 		 * it is not safe to continue further.
1568 		 */
1569 		kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
1570 		err = -EINVAL;
1571 		goto out;
1572 	}
1573 
1574 	hyp_pgtable = kzalloc(sizeof(*hyp_pgtable), GFP_KERNEL);
1575 	if (!hyp_pgtable) {
1576 		kvm_err("Hyp mode page-table not allocated\n");
1577 		err = -ENOMEM;
1578 		goto out;
1579 	}
1580 
1581 	err = kvm_pgtable_hyp_init(hyp_pgtable, *hyp_va_bits, &kvm_hyp_mm_ops);
1582 	if (err)
1583 		goto out_free_pgtable;
1584 
1585 	err = kvm_map_idmap_text();
1586 	if (err)
1587 		goto out_destroy_pgtable;
1588 
1589 	io_map_base = hyp_idmap_start;
1590 	return 0;
1591 
1592 out_destroy_pgtable:
1593 	kvm_pgtable_hyp_destroy(hyp_pgtable);
1594 out_free_pgtable:
1595 	kfree(hyp_pgtable);
1596 	hyp_pgtable = NULL;
1597 out:
1598 	return err;
1599 }
1600 
1601 void kvm_arch_commit_memory_region(struct kvm *kvm,
1602 				   struct kvm_memory_slot *old,
1603 				   const struct kvm_memory_slot *new,
1604 				   enum kvm_mr_change change)
1605 {
1606 	/*
1607 	 * At this point memslot has been committed and there is an
1608 	 * allocated dirty_bitmap[], dirty pages will be tracked while the
1609 	 * memory slot is write protected.
1610 	 */
1611 	if (change != KVM_MR_DELETE && new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
1612 		/*
1613 		 * If we're with initial-all-set, we don't need to write
1614 		 * protect any pages because they're all reported as dirty.
1615 		 * Huge pages and normal pages will be write protect gradually.
1616 		 */
1617 		if (!kvm_dirty_log_manual_protect_and_init_set(kvm)) {
1618 			kvm_mmu_wp_memory_region(kvm, new->id);
1619 		}
1620 	}
1621 }
1622 
1623 int kvm_arch_prepare_memory_region(struct kvm *kvm,
1624 				   const struct kvm_memory_slot *old,
1625 				   struct kvm_memory_slot *new,
1626 				   enum kvm_mr_change change)
1627 {
1628 	hva_t hva, reg_end;
1629 	int ret = 0;
1630 
1631 	if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
1632 			change != KVM_MR_FLAGS_ONLY)
1633 		return 0;
1634 
1635 	/*
1636 	 * Prevent userspace from creating a memory region outside of the IPA
1637 	 * space addressable by the KVM guest IPA space.
1638 	 */
1639 	if ((new->base_gfn + new->npages) > (kvm_phys_size(kvm) >> PAGE_SHIFT))
1640 		return -EFAULT;
1641 
1642 	hva = new->userspace_addr;
1643 	reg_end = hva + (new->npages << PAGE_SHIFT);
1644 
1645 	mmap_read_lock(current->mm);
1646 	/*
1647 	 * A memory region could potentially cover multiple VMAs, and any holes
1648 	 * between them, so iterate over all of them.
1649 	 *
1650 	 *     +--------------------------------------------+
1651 	 * +---------------+----------------+   +----------------+
1652 	 * |   : VMA 1     |      VMA 2     |   |    VMA 3  :    |
1653 	 * +---------------+----------------+   +----------------+
1654 	 *     |               memory region                |
1655 	 *     +--------------------------------------------+
1656 	 */
1657 	do {
1658 		struct vm_area_struct *vma;
1659 
1660 		vma = find_vma_intersection(current->mm, hva, reg_end);
1661 		if (!vma)
1662 			break;
1663 
1664 		/*
1665 		 * VM_SHARED mappings are not allowed with MTE to avoid races
1666 		 * when updating the PG_mte_tagged page flag, see
1667 		 * sanitise_mte_tags for more details.
1668 		 */
1669 		if (kvm_has_mte(kvm) && vma->vm_flags & VM_SHARED) {
1670 			ret = -EINVAL;
1671 			break;
1672 		}
1673 
1674 		if (vma->vm_flags & VM_PFNMAP) {
1675 			/* IO region dirty page logging not allowed */
1676 			if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
1677 				ret = -EINVAL;
1678 				break;
1679 			}
1680 		}
1681 		hva = min(reg_end, vma->vm_end);
1682 	} while (hva < reg_end);
1683 
1684 	mmap_read_unlock(current->mm);
1685 	return ret;
1686 }
1687 
1688 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
1689 {
1690 }
1691 
1692 void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
1693 {
1694 }
1695 
1696 void kvm_arch_flush_shadow_all(struct kvm *kvm)
1697 {
1698 	kvm_free_stage2_pgd(&kvm->arch.mmu);
1699 }
1700 
1701 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1702 				   struct kvm_memory_slot *slot)
1703 {
1704 	gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1705 	phys_addr_t size = slot->npages << PAGE_SHIFT;
1706 
1707 	write_lock(&kvm->mmu_lock);
1708 	unmap_stage2_range(&kvm->arch.mmu, gpa, size);
1709 	write_unlock(&kvm->mmu_lock);
1710 }
1711 
1712 /*
1713  * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1714  *
1715  * Main problems:
1716  * - S/W ops are local to a CPU (not broadcast)
1717  * - We have line migration behind our back (speculation)
1718  * - System caches don't support S/W at all (damn!)
1719  *
1720  * In the face of the above, the best we can do is to try and convert
1721  * S/W ops to VA ops. Because the guest is not allowed to infer the
1722  * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1723  * which is a rather good thing for us.
1724  *
1725  * Also, it is only used when turning caches on/off ("The expected
1726  * usage of the cache maintenance instructions that operate by set/way
1727  * is associated with the cache maintenance instructions associated
1728  * with the powerdown and powerup of caches, if this is required by
1729  * the implementation.").
1730  *
1731  * We use the following policy:
1732  *
1733  * - If we trap a S/W operation, we enable VM trapping to detect
1734  *   caches being turned on/off, and do a full clean.
1735  *
1736  * - We flush the caches on both caches being turned on and off.
1737  *
1738  * - Once the caches are enabled, we stop trapping VM ops.
1739  */
1740 void kvm_set_way_flush(struct kvm_vcpu *vcpu)
1741 {
1742 	unsigned long hcr = *vcpu_hcr(vcpu);
1743 
1744 	/*
1745 	 * If this is the first time we do a S/W operation
1746 	 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1747 	 * VM trapping.
1748 	 *
1749 	 * Otherwise, rely on the VM trapping to wait for the MMU +
1750 	 * Caches to be turned off. At that point, we'll be able to
1751 	 * clean the caches again.
1752 	 */
1753 	if (!(hcr & HCR_TVM)) {
1754 		trace_kvm_set_way_flush(*vcpu_pc(vcpu),
1755 					vcpu_has_cache_enabled(vcpu));
1756 		stage2_flush_vm(vcpu->kvm);
1757 		*vcpu_hcr(vcpu) = hcr | HCR_TVM;
1758 	}
1759 }
1760 
1761 void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
1762 {
1763 	bool now_enabled = vcpu_has_cache_enabled(vcpu);
1764 
1765 	/*
1766 	 * If switching the MMU+caches on, need to invalidate the caches.
1767 	 * If switching it off, need to clean the caches.
1768 	 * Clean + invalidate does the trick always.
1769 	 */
1770 	if (now_enabled != was_enabled)
1771 		stage2_flush_vm(vcpu->kvm);
1772 
1773 	/* Caches are now on, stop trapping VM ops (until a S/W op) */
1774 	if (now_enabled)
1775 		*vcpu_hcr(vcpu) &= ~HCR_TVM;
1776 
1777 	trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
1778 }
1779