mmu.c 33 KB

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