xref: /openbmc/linux/arch/mips/kvm/mmu.c (revision f125e2d4)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * KVM/MIPS MMU handling in the KVM module.
7  *
8  * Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
9  * Authors: Sanjay Lal <sanjayl@kymasys.com>
10  */
11 
12 #include <linux/highmem.h>
13 #include <linux/kvm_host.h>
14 #include <linux/uaccess.h>
15 #include <asm/mmu_context.h>
16 #include <asm/pgalloc.h>
17 
18 /*
19  * KVM_MMU_CACHE_MIN_PAGES is the number of GPA page table translation levels
20  * for which pages need to be cached.
21  */
22 #if defined(__PAGETABLE_PMD_FOLDED)
23 #define KVM_MMU_CACHE_MIN_PAGES 1
24 #else
25 #define KVM_MMU_CACHE_MIN_PAGES 2
26 #endif
27 
28 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
29 				  int min, int max)
30 {
31 	void *page;
32 
33 	BUG_ON(max > KVM_NR_MEM_OBJS);
34 	if (cache->nobjs >= min)
35 		return 0;
36 	while (cache->nobjs < max) {
37 		page = (void *)__get_free_page(GFP_KERNEL);
38 		if (!page)
39 			return -ENOMEM;
40 		cache->objects[cache->nobjs++] = page;
41 	}
42 	return 0;
43 }
44 
45 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
46 {
47 	while (mc->nobjs)
48 		free_page((unsigned long)mc->objects[--mc->nobjs]);
49 }
50 
51 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
52 {
53 	void *p;
54 
55 	BUG_ON(!mc || !mc->nobjs);
56 	p = mc->objects[--mc->nobjs];
57 	return p;
58 }
59 
60 void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
61 {
62 	mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
63 }
64 
65 /**
66  * kvm_pgd_init() - Initialise KVM GPA page directory.
67  * @page:	Pointer to page directory (PGD) for KVM GPA.
68  *
69  * Initialise a KVM GPA page directory with pointers to the invalid table, i.e.
70  * representing no mappings. This is similar to pgd_init(), however it
71  * initialises all the page directory pointers, not just the ones corresponding
72  * to the userland address space (since it is for the guest physical address
73  * space rather than a virtual address space).
74  */
75 static void kvm_pgd_init(void *page)
76 {
77 	unsigned long *p, *end;
78 	unsigned long entry;
79 
80 #ifdef __PAGETABLE_PMD_FOLDED
81 	entry = (unsigned long)invalid_pte_table;
82 #else
83 	entry = (unsigned long)invalid_pmd_table;
84 #endif
85 
86 	p = (unsigned long *)page;
87 	end = p + PTRS_PER_PGD;
88 
89 	do {
90 		p[0] = entry;
91 		p[1] = entry;
92 		p[2] = entry;
93 		p[3] = entry;
94 		p[4] = entry;
95 		p += 8;
96 		p[-3] = entry;
97 		p[-2] = entry;
98 		p[-1] = entry;
99 	} while (p != end);
100 }
101 
102 /**
103  * kvm_pgd_alloc() - Allocate and initialise a KVM GPA page directory.
104  *
105  * Allocate a blank KVM GPA page directory (PGD) for representing guest physical
106  * to host physical page mappings.
107  *
108  * Returns:	Pointer to new KVM GPA page directory.
109  *		NULL on allocation failure.
110  */
111 pgd_t *kvm_pgd_alloc(void)
112 {
113 	pgd_t *ret;
114 
115 	ret = (pgd_t *)__get_free_pages(GFP_KERNEL, PGD_ORDER);
116 	if (ret)
117 		kvm_pgd_init(ret);
118 
119 	return ret;
120 }
121 
122 /**
123  * kvm_mips_walk_pgd() - Walk page table with optional allocation.
124  * @pgd:	Page directory pointer.
125  * @addr:	Address to index page table using.
126  * @cache:	MMU page cache to allocate new page tables from, or NULL.
127  *
128  * Walk the page tables pointed to by @pgd to find the PTE corresponding to the
129  * address @addr. If page tables don't exist for @addr, they will be created
130  * from the MMU cache if @cache is not NULL.
131  *
132  * Returns:	Pointer to pte_t corresponding to @addr.
133  *		NULL if a page table doesn't exist for @addr and !@cache.
134  *		NULL if a page table allocation failed.
135  */
136 static pte_t *kvm_mips_walk_pgd(pgd_t *pgd, struct kvm_mmu_memory_cache *cache,
137 				unsigned long addr)
138 {
139 	p4d_t *p4d;
140 	pud_t *pud;
141 	pmd_t *pmd;
142 
143 	pgd += pgd_index(addr);
144 	if (pgd_none(*pgd)) {
145 		/* Not used on MIPS yet */
146 		BUG();
147 		return NULL;
148 	}
149 	p4d = p4d_offset(pgd, addr);
150 	pud = pud_offset(p4d, addr);
151 	if (pud_none(*pud)) {
152 		pmd_t *new_pmd;
153 
154 		if (!cache)
155 			return NULL;
156 		new_pmd = mmu_memory_cache_alloc(cache);
157 		pmd_init((unsigned long)new_pmd,
158 			 (unsigned long)invalid_pte_table);
159 		pud_populate(NULL, pud, new_pmd);
160 	}
161 	pmd = pmd_offset(pud, addr);
162 	if (pmd_none(*pmd)) {
163 		pte_t *new_pte;
164 
165 		if (!cache)
166 			return NULL;
167 		new_pte = mmu_memory_cache_alloc(cache);
168 		clear_page(new_pte);
169 		pmd_populate_kernel(NULL, pmd, new_pte);
170 	}
171 	return pte_offset(pmd, addr);
172 }
173 
174 /* Caller must hold kvm->mm_lock */
175 static pte_t *kvm_mips_pte_for_gpa(struct kvm *kvm,
176 				   struct kvm_mmu_memory_cache *cache,
177 				   unsigned long addr)
178 {
179 	return kvm_mips_walk_pgd(kvm->arch.gpa_mm.pgd, cache, addr);
180 }
181 
182 /*
183  * kvm_mips_flush_gpa_{pte,pmd,pud,pgd,pt}.
184  * Flush a range of guest physical address space from the VM's GPA page tables.
185  */
186 
187 static bool kvm_mips_flush_gpa_pte(pte_t *pte, unsigned long start_gpa,
188 				   unsigned long end_gpa)
189 {
190 	int i_min = __pte_offset(start_gpa);
191 	int i_max = __pte_offset(end_gpa);
192 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
193 	int i;
194 
195 	for (i = i_min; i <= i_max; ++i) {
196 		if (!pte_present(pte[i]))
197 			continue;
198 
199 		set_pte(pte + i, __pte(0));
200 	}
201 	return safe_to_remove;
202 }
203 
204 static bool kvm_mips_flush_gpa_pmd(pmd_t *pmd, unsigned long start_gpa,
205 				   unsigned long end_gpa)
206 {
207 	pte_t *pte;
208 	unsigned long end = ~0ul;
209 	int i_min = pmd_index(start_gpa);
210 	int i_max = pmd_index(end_gpa);
211 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
212 	int i;
213 
214 	for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
215 		if (!pmd_present(pmd[i]))
216 			continue;
217 
218 		pte = pte_offset(pmd + i, 0);
219 		if (i == i_max)
220 			end = end_gpa;
221 
222 		if (kvm_mips_flush_gpa_pte(pte, start_gpa, end)) {
223 			pmd_clear(pmd + i);
224 			pte_free_kernel(NULL, pte);
225 		} else {
226 			safe_to_remove = false;
227 		}
228 	}
229 	return safe_to_remove;
230 }
231 
232 static bool kvm_mips_flush_gpa_pud(pud_t *pud, unsigned long start_gpa,
233 				   unsigned long end_gpa)
234 {
235 	pmd_t *pmd;
236 	unsigned long end = ~0ul;
237 	int i_min = pud_index(start_gpa);
238 	int i_max = pud_index(end_gpa);
239 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
240 	int i;
241 
242 	for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
243 		if (!pud_present(pud[i]))
244 			continue;
245 
246 		pmd = pmd_offset(pud + i, 0);
247 		if (i == i_max)
248 			end = end_gpa;
249 
250 		if (kvm_mips_flush_gpa_pmd(pmd, start_gpa, end)) {
251 			pud_clear(pud + i);
252 			pmd_free(NULL, pmd);
253 		} else {
254 			safe_to_remove = false;
255 		}
256 	}
257 	return safe_to_remove;
258 }
259 
260 static bool kvm_mips_flush_gpa_pgd(pgd_t *pgd, unsigned long start_gpa,
261 				   unsigned long end_gpa)
262 {
263 	p4d_t *p4d;
264 	pud_t *pud;
265 	unsigned long end = ~0ul;
266 	int i_min = pgd_index(start_gpa);
267 	int i_max = pgd_index(end_gpa);
268 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
269 	int i;
270 
271 	for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
272 		if (!pgd_present(pgd[i]))
273 			continue;
274 
275 		p4d = p4d_offset(pgd, 0);
276 		pud = pud_offset(p4d + i, 0);
277 		if (i == i_max)
278 			end = end_gpa;
279 
280 		if (kvm_mips_flush_gpa_pud(pud, start_gpa, end)) {
281 			pgd_clear(pgd + i);
282 			pud_free(NULL, pud);
283 		} else {
284 			safe_to_remove = false;
285 		}
286 	}
287 	return safe_to_remove;
288 }
289 
290 /**
291  * kvm_mips_flush_gpa_pt() - Flush a range of guest physical addresses.
292  * @kvm:	KVM pointer.
293  * @start_gfn:	Guest frame number of first page in GPA range to flush.
294  * @end_gfn:	Guest frame number of last page in GPA range to flush.
295  *
296  * Flushes a range of GPA mappings from the GPA page tables.
297  *
298  * The caller must hold the @kvm->mmu_lock spinlock.
299  *
300  * Returns:	Whether its safe to remove the top level page directory because
301  *		all lower levels have been removed.
302  */
303 bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
304 {
305 	return kvm_mips_flush_gpa_pgd(kvm->arch.gpa_mm.pgd,
306 				      start_gfn << PAGE_SHIFT,
307 				      end_gfn << PAGE_SHIFT);
308 }
309 
310 #define BUILD_PTE_RANGE_OP(name, op)					\
311 static int kvm_mips_##name##_pte(pte_t *pte, unsigned long start,	\
312 				 unsigned long end)			\
313 {									\
314 	int ret = 0;							\
315 	int i_min = __pte_offset(start);				\
316 	int i_max = __pte_offset(end);					\
317 	int i;								\
318 	pte_t old, new;							\
319 									\
320 	for (i = i_min; i <= i_max; ++i) {				\
321 		if (!pte_present(pte[i]))				\
322 			continue;					\
323 									\
324 		old = pte[i];						\
325 		new = op(old);						\
326 		if (pte_val(new) == pte_val(old))			\
327 			continue;					\
328 		set_pte(pte + i, new);					\
329 		ret = 1;						\
330 	}								\
331 	return ret;							\
332 }									\
333 									\
334 /* returns true if anything was done */					\
335 static int kvm_mips_##name##_pmd(pmd_t *pmd, unsigned long start,	\
336 				 unsigned long end)			\
337 {									\
338 	int ret = 0;							\
339 	pte_t *pte;							\
340 	unsigned long cur_end = ~0ul;					\
341 	int i_min = pmd_index(start);				\
342 	int i_max = pmd_index(end);					\
343 	int i;								\
344 									\
345 	for (i = i_min; i <= i_max; ++i, start = 0) {			\
346 		if (!pmd_present(pmd[i]))				\
347 			continue;					\
348 									\
349 		pte = pte_offset(pmd + i, 0);				\
350 		if (i == i_max)						\
351 			cur_end = end;					\
352 									\
353 		ret |= kvm_mips_##name##_pte(pte, start, cur_end);	\
354 	}								\
355 	return ret;							\
356 }									\
357 									\
358 static int kvm_mips_##name##_pud(pud_t *pud, unsigned long start,	\
359 				 unsigned long end)			\
360 {									\
361 	int ret = 0;							\
362 	pmd_t *pmd;							\
363 	unsigned long cur_end = ~0ul;					\
364 	int i_min = pud_index(start);				\
365 	int i_max = pud_index(end);					\
366 	int i;								\
367 									\
368 	for (i = i_min; i <= i_max; ++i, start = 0) {			\
369 		if (!pud_present(pud[i]))				\
370 			continue;					\
371 									\
372 		pmd = pmd_offset(pud + i, 0);				\
373 		if (i == i_max)						\
374 			cur_end = end;					\
375 									\
376 		ret |= kvm_mips_##name##_pmd(pmd, start, cur_end);	\
377 	}								\
378 	return ret;							\
379 }									\
380 									\
381 static int kvm_mips_##name##_pgd(pgd_t *pgd, unsigned long start,	\
382 				 unsigned long end)			\
383 {									\
384 	int ret = 0;							\
385 	p4d_t *p4d;							\
386 	pud_t *pud;							\
387 	unsigned long cur_end = ~0ul;					\
388 	int i_min = pgd_index(start);					\
389 	int i_max = pgd_index(end);					\
390 	int i;								\
391 									\
392 	for (i = i_min; i <= i_max; ++i, start = 0) {			\
393 		if (!pgd_present(pgd[i]))				\
394 			continue;					\
395 									\
396 		p4d = p4d_offset(pgd, 0);				\
397 		pud = pud_offset(p4d + i, 0);				\
398 		if (i == i_max)						\
399 			cur_end = end;					\
400 									\
401 		ret |= kvm_mips_##name##_pud(pud, start, cur_end);	\
402 	}								\
403 	return ret;							\
404 }
405 
406 /*
407  * kvm_mips_mkclean_gpa_pt.
408  * Mark a range of guest physical address space clean (writes fault) in the VM's
409  * GPA page table to allow dirty page tracking.
410  */
411 
412 BUILD_PTE_RANGE_OP(mkclean, pte_mkclean)
413 
414 /**
415  * kvm_mips_mkclean_gpa_pt() - Make a range of guest physical addresses clean.
416  * @kvm:	KVM pointer.
417  * @start_gfn:	Guest frame number of first page in GPA range to flush.
418  * @end_gfn:	Guest frame number of last page in GPA range to flush.
419  *
420  * Make a range of GPA mappings clean so that guest writes will fault and
421  * trigger dirty page logging.
422  *
423  * The caller must hold the @kvm->mmu_lock spinlock.
424  *
425  * Returns:	Whether any GPA mappings were modified, which would require
426  *		derived mappings (GVA page tables & TLB enties) to be
427  *		invalidated.
428  */
429 int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
430 {
431 	return kvm_mips_mkclean_pgd(kvm->arch.gpa_mm.pgd,
432 				    start_gfn << PAGE_SHIFT,
433 				    end_gfn << PAGE_SHIFT);
434 }
435 
436 /**
437  * kvm_arch_mmu_enable_log_dirty_pt_masked() - write protect dirty pages
438  * @kvm:	The KVM pointer
439  * @slot:	The memory slot associated with mask
440  * @gfn_offset:	The gfn offset in memory slot
441  * @mask:	The mask of dirty pages at offset 'gfn_offset' in this memory
442  *		slot to be write protected
443  *
444  * Walks bits set in mask write protects the associated pte's. Caller must
445  * acquire @kvm->mmu_lock.
446  */
447 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
448 		struct kvm_memory_slot *slot,
449 		gfn_t gfn_offset, unsigned long mask)
450 {
451 	gfn_t base_gfn = slot->base_gfn + gfn_offset;
452 	gfn_t start = base_gfn +  __ffs(mask);
453 	gfn_t end = base_gfn + __fls(mask);
454 
455 	kvm_mips_mkclean_gpa_pt(kvm, start, end);
456 }
457 
458 /*
459  * kvm_mips_mkold_gpa_pt.
460  * Mark a range of guest physical address space old (all accesses fault) in the
461  * VM's GPA page table to allow detection of commonly used pages.
462  */
463 
464 BUILD_PTE_RANGE_OP(mkold, pte_mkold)
465 
466 static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
467 				 gfn_t end_gfn)
468 {
469 	return kvm_mips_mkold_pgd(kvm->arch.gpa_mm.pgd,
470 				  start_gfn << PAGE_SHIFT,
471 				  end_gfn << PAGE_SHIFT);
472 }
473 
474 static int handle_hva_to_gpa(struct kvm *kvm,
475 			     unsigned long start,
476 			     unsigned long end,
477 			     int (*handler)(struct kvm *kvm, gfn_t gfn,
478 					    gpa_t gfn_end,
479 					    struct kvm_memory_slot *memslot,
480 					    void *data),
481 			     void *data)
482 {
483 	struct kvm_memslots *slots;
484 	struct kvm_memory_slot *memslot;
485 	int ret = 0;
486 
487 	slots = kvm_memslots(kvm);
488 
489 	/* we only care about the pages that the guest sees */
490 	kvm_for_each_memslot(memslot, slots) {
491 		unsigned long hva_start, hva_end;
492 		gfn_t gfn, gfn_end;
493 
494 		hva_start = max(start, memslot->userspace_addr);
495 		hva_end = min(end, memslot->userspace_addr +
496 					(memslot->npages << PAGE_SHIFT));
497 		if (hva_start >= hva_end)
498 			continue;
499 
500 		/*
501 		 * {gfn(page) | page intersects with [hva_start, hva_end)} =
502 		 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
503 		 */
504 		gfn = hva_to_gfn_memslot(hva_start, memslot);
505 		gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
506 
507 		ret |= handler(kvm, gfn, gfn_end, memslot, data);
508 	}
509 
510 	return ret;
511 }
512 
513 
514 static int kvm_unmap_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
515 				 struct kvm_memory_slot *memslot, void *data)
516 {
517 	kvm_mips_flush_gpa_pt(kvm, gfn, gfn_end);
518 	return 1;
519 }
520 
521 int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
522 {
523 	handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
524 
525 	kvm_mips_callbacks->flush_shadow_all(kvm);
526 	return 0;
527 }
528 
529 static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
530 				struct kvm_memory_slot *memslot, void *data)
531 {
532 	gpa_t gpa = gfn << PAGE_SHIFT;
533 	pte_t hva_pte = *(pte_t *)data;
534 	pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
535 	pte_t old_pte;
536 
537 	if (!gpa_pte)
538 		return 0;
539 
540 	/* Mapping may need adjusting depending on memslot flags */
541 	old_pte = *gpa_pte;
542 	if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
543 		hva_pte = pte_mkclean(hva_pte);
544 	else if (memslot->flags & KVM_MEM_READONLY)
545 		hva_pte = pte_wrprotect(hva_pte);
546 
547 	set_pte(gpa_pte, hva_pte);
548 
549 	/* Replacing an absent or old page doesn't need flushes */
550 	if (!pte_present(old_pte) || !pte_young(old_pte))
551 		return 0;
552 
553 	/* Pages swapped, aged, moved, or cleaned require flushes */
554 	return !pte_present(hva_pte) ||
555 	       !pte_young(hva_pte) ||
556 	       pte_pfn(old_pte) != pte_pfn(hva_pte) ||
557 	       (pte_dirty(old_pte) && !pte_dirty(hva_pte));
558 }
559 
560 int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
561 {
562 	unsigned long end = hva + PAGE_SIZE;
563 	int ret;
564 
565 	ret = handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &pte);
566 	if (ret)
567 		kvm_mips_callbacks->flush_shadow_all(kvm);
568 	return 0;
569 }
570 
571 static int kvm_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
572 			       struct kvm_memory_slot *memslot, void *data)
573 {
574 	return kvm_mips_mkold_gpa_pt(kvm, gfn, gfn_end);
575 }
576 
577 static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
578 				    struct kvm_memory_slot *memslot, void *data)
579 {
580 	gpa_t gpa = gfn << PAGE_SHIFT;
581 	pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
582 
583 	if (!gpa_pte)
584 		return 0;
585 	return pte_young(*gpa_pte);
586 }
587 
588 int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
589 {
590 	return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
591 }
592 
593 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
594 {
595 	return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
596 }
597 
598 /**
599  * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
600  * @vcpu:		VCPU pointer.
601  * @gpa:		Guest physical address of fault.
602  * @write_fault:	Whether the fault was due to a write.
603  * @out_entry:		New PTE for @gpa (written on success unless NULL).
604  * @out_buddy:		New PTE for @gpa's buddy (written on success unless
605  *			NULL).
606  *
607  * Perform fast path GPA fault handling, doing all that can be done without
608  * calling into KVM. This handles marking old pages young (for idle page
609  * tracking), and dirtying of clean pages (for dirty page logging).
610  *
611  * Returns:	0 on success, in which case we can update derived mappings and
612  *		resume guest execution.
613  *		-EFAULT on failure due to absent GPA mapping or write to
614  *		read-only page, in which case KVM must be consulted.
615  */
616 static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa,
617 				   bool write_fault,
618 				   pte_t *out_entry, pte_t *out_buddy)
619 {
620 	struct kvm *kvm = vcpu->kvm;
621 	gfn_t gfn = gpa >> PAGE_SHIFT;
622 	pte_t *ptep;
623 	kvm_pfn_t pfn = 0;	/* silence bogus GCC warning */
624 	bool pfn_valid = false;
625 	int ret = 0;
626 
627 	spin_lock(&kvm->mmu_lock);
628 
629 	/* Fast path - just check GPA page table for an existing entry */
630 	ptep = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
631 	if (!ptep || !pte_present(*ptep)) {
632 		ret = -EFAULT;
633 		goto out;
634 	}
635 
636 	/* Track access to pages marked old */
637 	if (!pte_young(*ptep)) {
638 		set_pte(ptep, pte_mkyoung(*ptep));
639 		pfn = pte_pfn(*ptep);
640 		pfn_valid = true;
641 		/* call kvm_set_pfn_accessed() after unlock */
642 	}
643 	if (write_fault && !pte_dirty(*ptep)) {
644 		if (!pte_write(*ptep)) {
645 			ret = -EFAULT;
646 			goto out;
647 		}
648 
649 		/* Track dirtying of writeable pages */
650 		set_pte(ptep, pte_mkdirty(*ptep));
651 		pfn = pte_pfn(*ptep);
652 		mark_page_dirty(kvm, gfn);
653 		kvm_set_pfn_dirty(pfn);
654 	}
655 
656 	if (out_entry)
657 		*out_entry = *ptep;
658 	if (out_buddy)
659 		*out_buddy = *ptep_buddy(ptep);
660 
661 out:
662 	spin_unlock(&kvm->mmu_lock);
663 	if (pfn_valid)
664 		kvm_set_pfn_accessed(pfn);
665 	return ret;
666 }
667 
668 /**
669  * kvm_mips_map_page() - Map a guest physical page.
670  * @vcpu:		VCPU pointer.
671  * @gpa:		Guest physical address of fault.
672  * @write_fault:	Whether the fault was due to a write.
673  * @out_entry:		New PTE for @gpa (written on success unless NULL).
674  * @out_buddy:		New PTE for @gpa's buddy (written on success unless
675  *			NULL).
676  *
677  * Handle GPA faults by creating a new GPA mapping (or updating an existing
678  * one).
679  *
680  * This takes care of marking pages young or dirty (idle/dirty page tracking),
681  * asking KVM for the corresponding PFN, and creating a mapping in the GPA page
682  * tables. Derived mappings (GVA page tables and TLBs) must be handled by the
683  * caller.
684  *
685  * Returns:	0 on success, in which case the caller may use the @out_entry
686  *		and @out_buddy PTEs to update derived mappings and resume guest
687  *		execution.
688  *		-EFAULT if there is no memory region at @gpa or a write was
689  *		attempted to a read-only memory region. This is usually handled
690  *		as an MMIO access.
691  */
692 static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa,
693 			     bool write_fault,
694 			     pte_t *out_entry, pte_t *out_buddy)
695 {
696 	struct kvm *kvm = vcpu->kvm;
697 	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
698 	gfn_t gfn = gpa >> PAGE_SHIFT;
699 	int srcu_idx, err;
700 	kvm_pfn_t pfn;
701 	pte_t *ptep, entry, old_pte;
702 	bool writeable;
703 	unsigned long prot_bits;
704 	unsigned long mmu_seq;
705 
706 	/* Try the fast path to handle old / clean pages */
707 	srcu_idx = srcu_read_lock(&kvm->srcu);
708 	err = _kvm_mips_map_page_fast(vcpu, gpa, write_fault, out_entry,
709 				      out_buddy);
710 	if (!err)
711 		goto out;
712 
713 	/* We need a minimum of cached pages ready for page table creation */
714 	err = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
715 				     KVM_NR_MEM_OBJS);
716 	if (err)
717 		goto out;
718 
719 retry:
720 	/*
721 	 * Used to check for invalidations in progress, of the pfn that is
722 	 * returned by pfn_to_pfn_prot below.
723 	 */
724 	mmu_seq = kvm->mmu_notifier_seq;
725 	/*
726 	 * Ensure the read of mmu_notifier_seq isn't reordered with PTE reads in
727 	 * gfn_to_pfn_prot() (which calls get_user_pages()), so that we don't
728 	 * risk the page we get a reference to getting unmapped before we have a
729 	 * chance to grab the mmu_lock without mmu_notifier_retry() noticing.
730 	 *
731 	 * This smp_rmb() pairs with the effective smp_wmb() of the combination
732 	 * of the pte_unmap_unlock() after the PTE is zapped, and the
733 	 * spin_lock() in kvm_mmu_notifier_invalidate_<page|range_end>() before
734 	 * mmu_notifier_seq is incremented.
735 	 */
736 	smp_rmb();
737 
738 	/* Slow path - ask KVM core whether we can access this GPA */
739 	pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writeable);
740 	if (is_error_noslot_pfn(pfn)) {
741 		err = -EFAULT;
742 		goto out;
743 	}
744 
745 	spin_lock(&kvm->mmu_lock);
746 	/* Check if an invalidation has taken place since we got pfn */
747 	if (mmu_notifier_retry(kvm, mmu_seq)) {
748 		/*
749 		 * This can happen when mappings are changed asynchronously, but
750 		 * also synchronously if a COW is triggered by
751 		 * gfn_to_pfn_prot().
752 		 */
753 		spin_unlock(&kvm->mmu_lock);
754 		kvm_release_pfn_clean(pfn);
755 		goto retry;
756 	}
757 
758 	/* Ensure page tables are allocated */
759 	ptep = kvm_mips_pte_for_gpa(kvm, memcache, gpa);
760 
761 	/* Set up the PTE */
762 	prot_bits = _PAGE_PRESENT | __READABLE | _page_cachable_default;
763 	if (writeable) {
764 		prot_bits |= _PAGE_WRITE;
765 		if (write_fault) {
766 			prot_bits |= __WRITEABLE;
767 			mark_page_dirty(kvm, gfn);
768 			kvm_set_pfn_dirty(pfn);
769 		}
770 	}
771 	entry = pfn_pte(pfn, __pgprot(prot_bits));
772 
773 	/* Write the PTE */
774 	old_pte = *ptep;
775 	set_pte(ptep, entry);
776 
777 	err = 0;
778 	if (out_entry)
779 		*out_entry = *ptep;
780 	if (out_buddy)
781 		*out_buddy = *ptep_buddy(ptep);
782 
783 	spin_unlock(&kvm->mmu_lock);
784 	kvm_release_pfn_clean(pfn);
785 	kvm_set_pfn_accessed(pfn);
786 out:
787 	srcu_read_unlock(&kvm->srcu, srcu_idx);
788 	return err;
789 }
790 
791 static pte_t *kvm_trap_emul_pte_for_gva(struct kvm_vcpu *vcpu,
792 					unsigned long addr)
793 {
794 	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
795 	pgd_t *pgdp;
796 	int ret;
797 
798 	/* We need a minimum of cached pages ready for page table creation */
799 	ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
800 				     KVM_NR_MEM_OBJS);
801 	if (ret)
802 		return NULL;
803 
804 	if (KVM_GUEST_KERNEL_MODE(vcpu))
805 		pgdp = vcpu->arch.guest_kernel_mm.pgd;
806 	else
807 		pgdp = vcpu->arch.guest_user_mm.pgd;
808 
809 	return kvm_mips_walk_pgd(pgdp, memcache, addr);
810 }
811 
812 void kvm_trap_emul_invalidate_gva(struct kvm_vcpu *vcpu, unsigned long addr,
813 				  bool user)
814 {
815 	pgd_t *pgdp;
816 	pte_t *ptep;
817 
818 	addr &= PAGE_MASK << 1;
819 
820 	pgdp = vcpu->arch.guest_kernel_mm.pgd;
821 	ptep = kvm_mips_walk_pgd(pgdp, NULL, addr);
822 	if (ptep) {
823 		ptep[0] = pfn_pte(0, __pgprot(0));
824 		ptep[1] = pfn_pte(0, __pgprot(0));
825 	}
826 
827 	if (user) {
828 		pgdp = vcpu->arch.guest_user_mm.pgd;
829 		ptep = kvm_mips_walk_pgd(pgdp, NULL, addr);
830 		if (ptep) {
831 			ptep[0] = pfn_pte(0, __pgprot(0));
832 			ptep[1] = pfn_pte(0, __pgprot(0));
833 		}
834 	}
835 }
836 
837 /*
838  * kvm_mips_flush_gva_{pte,pmd,pud,pgd,pt}.
839  * Flush a range of guest physical address space from the VM's GPA page tables.
840  */
841 
842 static bool kvm_mips_flush_gva_pte(pte_t *pte, unsigned long start_gva,
843 				   unsigned long end_gva)
844 {
845 	int i_min = __pte_offset(start_gva);
846 	int i_max = __pte_offset(end_gva);
847 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
848 	int i;
849 
850 	/*
851 	 * There's no freeing to do, so there's no point clearing individual
852 	 * entries unless only part of the last level page table needs flushing.
853 	 */
854 	if (safe_to_remove)
855 		return true;
856 
857 	for (i = i_min; i <= i_max; ++i) {
858 		if (!pte_present(pte[i]))
859 			continue;
860 
861 		set_pte(pte + i, __pte(0));
862 	}
863 	return false;
864 }
865 
866 static bool kvm_mips_flush_gva_pmd(pmd_t *pmd, unsigned long start_gva,
867 				   unsigned long end_gva)
868 {
869 	pte_t *pte;
870 	unsigned long end = ~0ul;
871 	int i_min = pmd_index(start_gva);
872 	int i_max = pmd_index(end_gva);
873 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
874 	int i;
875 
876 	for (i = i_min; i <= i_max; ++i, start_gva = 0) {
877 		if (!pmd_present(pmd[i]))
878 			continue;
879 
880 		pte = pte_offset(pmd + i, 0);
881 		if (i == i_max)
882 			end = end_gva;
883 
884 		if (kvm_mips_flush_gva_pte(pte, start_gva, end)) {
885 			pmd_clear(pmd + i);
886 			pte_free_kernel(NULL, pte);
887 		} else {
888 			safe_to_remove = false;
889 		}
890 	}
891 	return safe_to_remove;
892 }
893 
894 static bool kvm_mips_flush_gva_pud(pud_t *pud, unsigned long start_gva,
895 				   unsigned long end_gva)
896 {
897 	pmd_t *pmd;
898 	unsigned long end = ~0ul;
899 	int i_min = pud_index(start_gva);
900 	int i_max = pud_index(end_gva);
901 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
902 	int i;
903 
904 	for (i = i_min; i <= i_max; ++i, start_gva = 0) {
905 		if (!pud_present(pud[i]))
906 			continue;
907 
908 		pmd = pmd_offset(pud + i, 0);
909 		if (i == i_max)
910 			end = end_gva;
911 
912 		if (kvm_mips_flush_gva_pmd(pmd, start_gva, end)) {
913 			pud_clear(pud + i);
914 			pmd_free(NULL, pmd);
915 		} else {
916 			safe_to_remove = false;
917 		}
918 	}
919 	return safe_to_remove;
920 }
921 
922 static bool kvm_mips_flush_gva_pgd(pgd_t *pgd, unsigned long start_gva,
923 				   unsigned long end_gva)
924 {
925 	p4d_t *p4d;
926 	pud_t *pud;
927 	unsigned long end = ~0ul;
928 	int i_min = pgd_index(start_gva);
929 	int i_max = pgd_index(end_gva);
930 	bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
931 	int i;
932 
933 	for (i = i_min; i <= i_max; ++i, start_gva = 0) {
934 		if (!pgd_present(pgd[i]))
935 			continue;
936 
937 		p4d = p4d_offset(pgd, 0);
938 		pud = pud_offset(p4d + i, 0);
939 		if (i == i_max)
940 			end = end_gva;
941 
942 		if (kvm_mips_flush_gva_pud(pud, start_gva, end)) {
943 			pgd_clear(pgd + i);
944 			pud_free(NULL, pud);
945 		} else {
946 			safe_to_remove = false;
947 		}
948 	}
949 	return safe_to_remove;
950 }
951 
952 void kvm_mips_flush_gva_pt(pgd_t *pgd, enum kvm_mips_flush flags)
953 {
954 	if (flags & KMF_GPA) {
955 		/* all of guest virtual address space could be affected */
956 		if (flags & KMF_KERN)
957 			/* useg, kseg0, seg2/3 */
958 			kvm_mips_flush_gva_pgd(pgd, 0, 0x7fffffff);
959 		else
960 			/* useg */
961 			kvm_mips_flush_gva_pgd(pgd, 0, 0x3fffffff);
962 	} else {
963 		/* useg */
964 		kvm_mips_flush_gva_pgd(pgd, 0, 0x3fffffff);
965 
966 		/* kseg2/3 */
967 		if (flags & KMF_KERN)
968 			kvm_mips_flush_gva_pgd(pgd, 0x60000000, 0x7fffffff);
969 	}
970 }
971 
972 static pte_t kvm_mips_gpa_pte_to_gva_unmapped(pte_t pte)
973 {
974 	/*
975 	 * Don't leak writeable but clean entries from GPA page tables. We don't
976 	 * want the normal Linux tlbmod handler to handle dirtying when KVM
977 	 * accesses guest memory.
978 	 */
979 	if (!pte_dirty(pte))
980 		pte = pte_wrprotect(pte);
981 
982 	return pte;
983 }
984 
985 static pte_t kvm_mips_gpa_pte_to_gva_mapped(pte_t pte, long entrylo)
986 {
987 	/* Guest EntryLo overrides host EntryLo */
988 	if (!(entrylo & ENTRYLO_D))
989 		pte = pte_mkclean(pte);
990 
991 	return kvm_mips_gpa_pte_to_gva_unmapped(pte);
992 }
993 
994 #ifdef CONFIG_KVM_MIPS_VZ
995 int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr,
996 				      struct kvm_vcpu *vcpu,
997 				      bool write_fault)
998 {
999 	int ret;
1000 
1001 	ret = kvm_mips_map_page(vcpu, badvaddr, write_fault, NULL, NULL);
1002 	if (ret)
1003 		return ret;
1004 
1005 	/* Invalidate this entry in the TLB */
1006 	return kvm_vz_host_tlb_inv(vcpu, badvaddr);
1007 }
1008 #endif
1009 
1010 /* XXXKYMA: Must be called with interrupts disabled */
1011 int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
1012 				    struct kvm_vcpu *vcpu,
1013 				    bool write_fault)
1014 {
1015 	unsigned long gpa;
1016 	pte_t pte_gpa[2], *ptep_gva;
1017 	int idx;
1018 
1019 	if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
1020 		kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
1021 		kvm_mips_dump_host_tlbs();
1022 		return -1;
1023 	}
1024 
1025 	/* Get the GPA page table entry */
1026 	gpa = KVM_GUEST_CPHYSADDR(badvaddr);
1027 	idx = (badvaddr >> PAGE_SHIFT) & 1;
1028 	if (kvm_mips_map_page(vcpu, gpa, write_fault, &pte_gpa[idx],
1029 			      &pte_gpa[!idx]) < 0)
1030 		return -1;
1031 
1032 	/* Get the GVA page table entry */
1033 	ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, badvaddr & ~PAGE_SIZE);
1034 	if (!ptep_gva) {
1035 		kvm_err("No ptep for gva %lx\n", badvaddr);
1036 		return -1;
1037 	}
1038 
1039 	/* Copy a pair of entries from GPA page table to GVA page table */
1040 	ptep_gva[0] = kvm_mips_gpa_pte_to_gva_unmapped(pte_gpa[0]);
1041 	ptep_gva[1] = kvm_mips_gpa_pte_to_gva_unmapped(pte_gpa[1]);
1042 
1043 	/* Invalidate this entry in the TLB, guest kernel ASID only */
1044 	kvm_mips_host_tlb_inv(vcpu, badvaddr, false, true);
1045 	return 0;
1046 }
1047 
1048 int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
1049 					 struct kvm_mips_tlb *tlb,
1050 					 unsigned long gva,
1051 					 bool write_fault)
1052 {
1053 	struct kvm *kvm = vcpu->kvm;
1054 	long tlb_lo[2];
1055 	pte_t pte_gpa[2], *ptep_buddy, *ptep_gva;
1056 	unsigned int idx = TLB_LO_IDX(*tlb, gva);
1057 	bool kernel = KVM_GUEST_KERNEL_MODE(vcpu);
1058 
1059 	tlb_lo[0] = tlb->tlb_lo[0];
1060 	tlb_lo[1] = tlb->tlb_lo[1];
1061 
1062 	/*
1063 	 * The commpage address must not be mapped to anything else if the guest
1064 	 * TLB contains entries nearby, or commpage accesses will break.
1065 	 */
1066 	if (!((gva ^ KVM_GUEST_COMMPAGE_ADDR) & VPN2_MASK & (PAGE_MASK << 1)))
1067 		tlb_lo[TLB_LO_IDX(*tlb, KVM_GUEST_COMMPAGE_ADDR)] = 0;
1068 
1069 	/* Get the GPA page table entry */
1070 	if (kvm_mips_map_page(vcpu, mips3_tlbpfn_to_paddr(tlb_lo[idx]),
1071 			      write_fault, &pte_gpa[idx], NULL) < 0)
1072 		return -1;
1073 
1074 	/* And its GVA buddy's GPA page table entry if it also exists */
1075 	pte_gpa[!idx] = pfn_pte(0, __pgprot(0));
1076 	if (tlb_lo[!idx] & ENTRYLO_V) {
1077 		spin_lock(&kvm->mmu_lock);
1078 		ptep_buddy = kvm_mips_pte_for_gpa(kvm, NULL,
1079 					mips3_tlbpfn_to_paddr(tlb_lo[!idx]));
1080 		if (ptep_buddy)
1081 			pte_gpa[!idx] = *ptep_buddy;
1082 		spin_unlock(&kvm->mmu_lock);
1083 	}
1084 
1085 	/* Get the GVA page table entry pair */
1086 	ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, gva & ~PAGE_SIZE);
1087 	if (!ptep_gva) {
1088 		kvm_err("No ptep for gva %lx\n", gva);
1089 		return -1;
1090 	}
1091 
1092 	/* Copy a pair of entries from GPA page table to GVA page table */
1093 	ptep_gva[0] = kvm_mips_gpa_pte_to_gva_mapped(pte_gpa[0], tlb_lo[0]);
1094 	ptep_gva[1] = kvm_mips_gpa_pte_to_gva_mapped(pte_gpa[1], tlb_lo[1]);
1095 
1096 	/* Invalidate this entry in the TLB, current guest mode ASID only */
1097 	kvm_mips_host_tlb_inv(vcpu, gva, !kernel, kernel);
1098 
1099 	kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
1100 		  tlb->tlb_lo[0], tlb->tlb_lo[1]);
1101 
1102 	return 0;
1103 }
1104 
1105 int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
1106 				       struct kvm_vcpu *vcpu)
1107 {
1108 	kvm_pfn_t pfn;
1109 	pte_t *ptep;
1110 
1111 	ptep = kvm_trap_emul_pte_for_gva(vcpu, badvaddr);
1112 	if (!ptep) {
1113 		kvm_err("No ptep for commpage %lx\n", badvaddr);
1114 		return -1;
1115 	}
1116 
1117 	pfn = PFN_DOWN(virt_to_phys(vcpu->arch.kseg0_commpage));
1118 	/* Also set valid and dirty, so refill handler doesn't have to */
1119 	*ptep = pte_mkyoung(pte_mkdirty(pfn_pte(pfn, PAGE_SHARED)));
1120 
1121 	/* Invalidate this entry in the TLB, guest kernel ASID only */
1122 	kvm_mips_host_tlb_inv(vcpu, badvaddr, false, true);
1123 	return 0;
1124 }
1125 
1126 /**
1127  * kvm_mips_migrate_count() - Migrate timer.
1128  * @vcpu:	Virtual CPU.
1129  *
1130  * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
1131  * if it was running prior to being cancelled.
1132  *
1133  * Must be called when the VCPU is migrated to a different CPU to ensure that
1134  * timer expiry during guest execution interrupts the guest and causes the
1135  * interrupt to be delivered in a timely manner.
1136  */
1137 static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
1138 {
1139 	if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
1140 		hrtimer_restart(&vcpu->arch.comparecount_timer);
1141 }
1142 
1143 /* Restore ASID once we are scheduled back after preemption */
1144 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1145 {
1146 	unsigned long flags;
1147 
1148 	kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
1149 
1150 	local_irq_save(flags);
1151 
1152 	vcpu->cpu = cpu;
1153 	if (vcpu->arch.last_sched_cpu != cpu) {
1154 		kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
1155 			  vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
1156 		/*
1157 		 * Migrate the timer interrupt to the current CPU so that it
1158 		 * always interrupts the guest and synchronously triggers a
1159 		 * guest timer interrupt.
1160 		 */
1161 		kvm_mips_migrate_count(vcpu);
1162 	}
1163 
1164 	/* restore guest state to registers */
1165 	kvm_mips_callbacks->vcpu_load(vcpu, cpu);
1166 
1167 	local_irq_restore(flags);
1168 }
1169 
1170 /* ASID can change if another task is scheduled during preemption */
1171 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1172 {
1173 	unsigned long flags;
1174 	int cpu;
1175 
1176 	local_irq_save(flags);
1177 
1178 	cpu = smp_processor_id();
1179 	vcpu->arch.last_sched_cpu = cpu;
1180 	vcpu->cpu = -1;
1181 
1182 	/* save guest state in registers */
1183 	kvm_mips_callbacks->vcpu_put(vcpu, cpu);
1184 
1185 	local_irq_restore(flags);
1186 }
1187 
1188 /**
1189  * kvm_trap_emul_gva_fault() - Safely attempt to handle a GVA access fault.
1190  * @vcpu:	Virtual CPU.
1191  * @gva:	Guest virtual address to be accessed.
1192  * @write:	True if write attempted (must be dirtied and made writable).
1193  *
1194  * Safely attempt to handle a GVA fault, mapping GVA pages if necessary, and
1195  * dirtying the page if @write so that guest instructions can be modified.
1196  *
1197  * Returns:	KVM_MIPS_MAPPED on success.
1198  *		KVM_MIPS_GVA if bad guest virtual address.
1199  *		KVM_MIPS_GPA if bad guest physical address.
1200  *		KVM_MIPS_TLB if guest TLB not present.
1201  *		KVM_MIPS_TLBINV if guest TLB present but not valid.
1202  *		KVM_MIPS_TLBMOD if guest TLB read only.
1203  */
1204 enum kvm_mips_fault_result kvm_trap_emul_gva_fault(struct kvm_vcpu *vcpu,
1205 						   unsigned long gva,
1206 						   bool write)
1207 {
1208 	struct mips_coproc *cop0 = vcpu->arch.cop0;
1209 	struct kvm_mips_tlb *tlb;
1210 	int index;
1211 
1212 	if (KVM_GUEST_KSEGX(gva) == KVM_GUEST_KSEG0) {
1213 		if (kvm_mips_handle_kseg0_tlb_fault(gva, vcpu, write) < 0)
1214 			return KVM_MIPS_GPA;
1215 	} else if ((KVM_GUEST_KSEGX(gva) < KVM_GUEST_KSEG0) ||
1216 		   KVM_GUEST_KSEGX(gva) == KVM_GUEST_KSEG23) {
1217 		/* Address should be in the guest TLB */
1218 		index = kvm_mips_guest_tlb_lookup(vcpu, (gva & VPN2_MASK) |
1219 			  (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID));
1220 		if (index < 0)
1221 			return KVM_MIPS_TLB;
1222 		tlb = &vcpu->arch.guest_tlb[index];
1223 
1224 		/* Entry should be valid, and dirty for writes */
1225 		if (!TLB_IS_VALID(*tlb, gva))
1226 			return KVM_MIPS_TLBINV;
1227 		if (write && !TLB_IS_DIRTY(*tlb, gva))
1228 			return KVM_MIPS_TLBMOD;
1229 
1230 		if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, gva, write))
1231 			return KVM_MIPS_GPA;
1232 	} else {
1233 		return KVM_MIPS_GVA;
1234 	}
1235 
1236 	return KVM_MIPS_MAPPED;
1237 }
1238 
1239 int kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
1240 {
1241 	int err;
1242 
1243 	if (WARN(IS_ENABLED(CONFIG_KVM_MIPS_VZ),
1244 		 "Expect BadInstr/BadInstrP registers to be used with VZ\n"))
1245 		return -EINVAL;
1246 
1247 retry:
1248 	kvm_trap_emul_gva_lockless_begin(vcpu);
1249 	err = get_user(*out, opc);
1250 	kvm_trap_emul_gva_lockless_end(vcpu);
1251 
1252 	if (unlikely(err)) {
1253 		/*
1254 		 * Try to handle the fault, maybe we just raced with a GVA
1255 		 * invalidation.
1256 		 */
1257 		err = kvm_trap_emul_gva_fault(vcpu, (unsigned long)opc,
1258 					      false);
1259 		if (unlikely(err)) {
1260 			kvm_err("%s: illegal address: %p\n",
1261 				__func__, opc);
1262 			return -EFAULT;
1263 		}
1264 
1265 		/* Hopefully it'll work now */
1266 		goto retry;
1267 	}
1268 	return 0;
1269 }
1270