mmu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  26. {
  27. kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  28. }
  29. /**
  30. * kvm_pgd_init() - Initialise KVM GPA page directory.
  31. * @page: Pointer to page directory (PGD) for KVM GPA.
  32. *
  33. * Initialise a KVM GPA page directory with pointers to the invalid table, i.e.
  34. * representing no mappings. This is similar to pgd_init(), however it
  35. * initialises all the page directory pointers, not just the ones corresponding
  36. * to the userland address space (since it is for the guest physical address
  37. * space rather than a virtual address space).
  38. */
  39. static void kvm_pgd_init(void *page)
  40. {
  41. unsigned long *p, *end;
  42. unsigned long entry;
  43. #ifdef __PAGETABLE_PMD_FOLDED
  44. entry = (unsigned long)invalid_pte_table;
  45. #else
  46. entry = (unsigned long)invalid_pmd_table;
  47. #endif
  48. p = (unsigned long *)page;
  49. end = p + PTRS_PER_PGD;
  50. do {
  51. p[0] = entry;
  52. p[1] = entry;
  53. p[2] = entry;
  54. p[3] = entry;
  55. p[4] = entry;
  56. p += 8;
  57. p[-3] = entry;
  58. p[-2] = entry;
  59. p[-1] = entry;
  60. } while (p != end);
  61. }
  62. /**
  63. * kvm_pgd_alloc() - Allocate and initialise a KVM GPA page directory.
  64. *
  65. * Allocate a blank KVM GPA page directory (PGD) for representing guest physical
  66. * to host physical page mappings.
  67. *
  68. * Returns: Pointer to new KVM GPA page directory.
  69. * NULL on allocation failure.
  70. */
  71. pgd_t *kvm_pgd_alloc(void)
  72. {
  73. pgd_t *ret;
  74. ret = (pgd_t *)__get_free_pages(GFP_KERNEL, PGD_TABLE_ORDER);
  75. if (ret)
  76. kvm_pgd_init(ret);
  77. return ret;
  78. }
  79. /**
  80. * kvm_mips_walk_pgd() - Walk page table with optional allocation.
  81. * @pgd: Page directory pointer.
  82. * @addr: Address to index page table using.
  83. * @cache: MMU page cache to allocate new page tables from, or NULL.
  84. *
  85. * Walk the page tables pointed to by @pgd to find the PTE corresponding to the
  86. * address @addr. If page tables don't exist for @addr, they will be created
  87. * from the MMU cache if @cache is not NULL.
  88. *
  89. * Returns: Pointer to pte_t corresponding to @addr.
  90. * NULL if a page table doesn't exist for @addr and !@cache.
  91. * NULL if a page table allocation failed.
  92. */
  93. static pte_t *kvm_mips_walk_pgd(pgd_t *pgd, struct kvm_mmu_memory_cache *cache,
  94. unsigned long addr)
  95. {
  96. p4d_t *p4d;
  97. pud_t *pud;
  98. pmd_t *pmd;
  99. pgd += pgd_index(addr);
  100. if (pgd_none(*pgd)) {
  101. /* Not used on MIPS yet */
  102. BUG();
  103. return NULL;
  104. }
  105. p4d = p4d_offset(pgd, addr);
  106. pud = pud_offset(p4d, addr);
  107. if (pud_none(*pud)) {
  108. pmd_t *new_pmd;
  109. if (!cache)
  110. return NULL;
  111. new_pmd = kvm_mmu_memory_cache_alloc(cache);
  112. pmd_init(new_pmd);
  113. pud_populate(NULL, pud, new_pmd);
  114. }
  115. pmd = pmd_offset(pud, addr);
  116. if (pmd_none(*pmd)) {
  117. pte_t *new_pte;
  118. if (!cache)
  119. return NULL;
  120. new_pte = kvm_mmu_memory_cache_alloc(cache);
  121. clear_page(new_pte);
  122. pmd_populate_kernel(NULL, pmd, new_pte);
  123. }
  124. return pte_offset_kernel(pmd, addr);
  125. }
  126. /* Caller must hold kvm->mm_lock */
  127. static pte_t *kvm_mips_pte_for_gpa(struct kvm *kvm,
  128. struct kvm_mmu_memory_cache *cache,
  129. unsigned long addr)
  130. {
  131. return kvm_mips_walk_pgd(kvm->arch.gpa_mm.pgd, cache, addr);
  132. }
  133. /*
  134. * kvm_mips_flush_gpa_{pte,pmd,pud,pgd,pt}.
  135. * Flush a range of guest physical address space from the VM's GPA page tables.
  136. */
  137. static bool kvm_mips_flush_gpa_pte(pte_t *pte, unsigned long start_gpa,
  138. unsigned long end_gpa)
  139. {
  140. int i_min = pte_index(start_gpa);
  141. int i_max = pte_index(end_gpa);
  142. bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
  143. int i;
  144. for (i = i_min; i <= i_max; ++i) {
  145. if (!pte_present(pte[i]))
  146. continue;
  147. set_pte(pte + i, __pte(0));
  148. }
  149. return safe_to_remove;
  150. }
  151. static bool kvm_mips_flush_gpa_pmd(pmd_t *pmd, unsigned long start_gpa,
  152. unsigned long end_gpa)
  153. {
  154. pte_t *pte;
  155. unsigned long end = ~0ul;
  156. int i_min = pmd_index(start_gpa);
  157. int i_max = pmd_index(end_gpa);
  158. bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
  159. int i;
  160. for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
  161. if (!pmd_present(pmd[i]))
  162. continue;
  163. pte = pte_offset_kernel(pmd + i, 0);
  164. if (i == i_max)
  165. end = end_gpa;
  166. if (kvm_mips_flush_gpa_pte(pte, start_gpa, end)) {
  167. pmd_clear(pmd + i);
  168. pte_free_kernel(NULL, pte);
  169. } else {
  170. safe_to_remove = false;
  171. }
  172. }
  173. return safe_to_remove;
  174. }
  175. static bool kvm_mips_flush_gpa_pud(pud_t *pud, unsigned long start_gpa,
  176. unsigned long end_gpa)
  177. {
  178. pmd_t *pmd;
  179. unsigned long end = ~0ul;
  180. int i_min = pud_index(start_gpa);
  181. int i_max = pud_index(end_gpa);
  182. bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
  183. int i;
  184. for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
  185. if (!pud_present(pud[i]))
  186. continue;
  187. pmd = pmd_offset(pud + i, 0);
  188. if (i == i_max)
  189. end = end_gpa;
  190. if (kvm_mips_flush_gpa_pmd(pmd, start_gpa, end)) {
  191. pud_clear(pud + i);
  192. pmd_free(NULL, pmd);
  193. } else {
  194. safe_to_remove = false;
  195. }
  196. }
  197. return safe_to_remove;
  198. }
  199. static bool kvm_mips_flush_gpa_pgd(pgd_t *pgd, unsigned long start_gpa,
  200. unsigned long end_gpa)
  201. {
  202. p4d_t *p4d;
  203. pud_t *pud;
  204. unsigned long end = ~0ul;
  205. int i_min = pgd_index(start_gpa);
  206. int i_max = pgd_index(end_gpa);
  207. bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
  208. int i;
  209. for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
  210. if (!pgd_present(pgd[i]))
  211. continue;
  212. p4d = p4d_offset(pgd, 0);
  213. pud = pud_offset(p4d + i, 0);
  214. if (i == i_max)
  215. end = end_gpa;
  216. if (kvm_mips_flush_gpa_pud(pud, start_gpa, end)) {
  217. pgd_clear(pgd + i);
  218. pud_free(NULL, pud);
  219. } else {
  220. safe_to_remove = false;
  221. }
  222. }
  223. return safe_to_remove;
  224. }
  225. /**
  226. * kvm_mips_flush_gpa_pt() - Flush a range of guest physical addresses.
  227. * @kvm: KVM pointer.
  228. * @start_gfn: Guest frame number of first page in GPA range to flush.
  229. * @end_gfn: Guest frame number of last page in GPA range to flush.
  230. *
  231. * Flushes a range of GPA mappings from the GPA page tables.
  232. *
  233. * The caller must hold the @kvm->mmu_lock spinlock.
  234. *
  235. * Returns: Whether its safe to remove the top level page directory because
  236. * all lower levels have been removed.
  237. */
  238. bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
  239. {
  240. return kvm_mips_flush_gpa_pgd(kvm->arch.gpa_mm.pgd,
  241. start_gfn << PAGE_SHIFT,
  242. end_gfn << PAGE_SHIFT);
  243. }
  244. #define BUILD_PTE_RANGE_OP(name, op) \
  245. static int kvm_mips_##name##_pte(pte_t *pte, unsigned long start, \
  246. unsigned long end) \
  247. { \
  248. int ret = 0; \
  249. int i_min = pte_index(start); \
  250. int i_max = pte_index(end); \
  251. int i; \
  252. pte_t old, new; \
  253. \
  254. for (i = i_min; i <= i_max; ++i) { \
  255. if (!pte_present(pte[i])) \
  256. continue; \
  257. \
  258. old = pte[i]; \
  259. new = op(old); \
  260. if (pte_val(new) == pte_val(old)) \
  261. continue; \
  262. set_pte(pte + i, new); \
  263. ret = 1; \
  264. } \
  265. return ret; \
  266. } \
  267. \
  268. /* returns true if anything was done */ \
  269. static int kvm_mips_##name##_pmd(pmd_t *pmd, unsigned long start, \
  270. unsigned long end) \
  271. { \
  272. int ret = 0; \
  273. pte_t *pte; \
  274. unsigned long cur_end = ~0ul; \
  275. int i_min = pmd_index(start); \
  276. int i_max = pmd_index(end); \
  277. int i; \
  278. \
  279. for (i = i_min; i <= i_max; ++i, start = 0) { \
  280. if (!pmd_present(pmd[i])) \
  281. continue; \
  282. \
  283. pte = pte_offset_kernel(pmd + i, 0); \
  284. if (i == i_max) \
  285. cur_end = end; \
  286. \
  287. ret |= kvm_mips_##name##_pte(pte, start, cur_end); \
  288. } \
  289. return ret; \
  290. } \
  291. \
  292. static int kvm_mips_##name##_pud(pud_t *pud, unsigned long start, \
  293. unsigned long end) \
  294. { \
  295. int ret = 0; \
  296. pmd_t *pmd; \
  297. unsigned long cur_end = ~0ul; \
  298. int i_min = pud_index(start); \
  299. int i_max = pud_index(end); \
  300. int i; \
  301. \
  302. for (i = i_min; i <= i_max; ++i, start = 0) { \
  303. if (!pud_present(pud[i])) \
  304. continue; \
  305. \
  306. pmd = pmd_offset(pud + i, 0); \
  307. if (i == i_max) \
  308. cur_end = end; \
  309. \
  310. ret |= kvm_mips_##name##_pmd(pmd, start, cur_end); \
  311. } \
  312. return ret; \
  313. } \
  314. \
  315. static int kvm_mips_##name##_pgd(pgd_t *pgd, unsigned long start, \
  316. unsigned long end) \
  317. { \
  318. int ret = 0; \
  319. p4d_t *p4d; \
  320. pud_t *pud; \
  321. unsigned long cur_end = ~0ul; \
  322. int i_min = pgd_index(start); \
  323. int i_max = pgd_index(end); \
  324. int i; \
  325. \
  326. for (i = i_min; i <= i_max; ++i, start = 0) { \
  327. if (!pgd_present(pgd[i])) \
  328. continue; \
  329. \
  330. p4d = p4d_offset(pgd, 0); \
  331. pud = pud_offset(p4d + i, 0); \
  332. if (i == i_max) \
  333. cur_end = end; \
  334. \
  335. ret |= kvm_mips_##name##_pud(pud, start, cur_end); \
  336. } \
  337. return ret; \
  338. }
  339. /*
  340. * kvm_mips_mkclean_gpa_pt.
  341. * Mark a range of guest physical address space clean (writes fault) in the VM's
  342. * GPA page table to allow dirty page tracking.
  343. */
  344. BUILD_PTE_RANGE_OP(mkclean, pte_mkclean)
  345. /**
  346. * kvm_mips_mkclean_gpa_pt() - Make a range of guest physical addresses clean.
  347. * @kvm: KVM pointer.
  348. * @start_gfn: Guest frame number of first page in GPA range to flush.
  349. * @end_gfn: Guest frame number of last page in GPA range to flush.
  350. *
  351. * Make a range of GPA mappings clean so that guest writes will fault and
  352. * trigger dirty page logging.
  353. *
  354. * The caller must hold the @kvm->mmu_lock spinlock.
  355. *
  356. * Returns: Whether any GPA mappings were modified, which would require
  357. * derived mappings (GVA page tables & TLB enties) to be
  358. * invalidated.
  359. */
  360. int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
  361. {
  362. return kvm_mips_mkclean_pgd(kvm->arch.gpa_mm.pgd,
  363. start_gfn << PAGE_SHIFT,
  364. end_gfn << PAGE_SHIFT);
  365. }
  366. /**
  367. * kvm_arch_mmu_enable_log_dirty_pt_masked() - write protect dirty pages
  368. * @kvm: The KVM pointer
  369. * @slot: The memory slot associated with mask
  370. * @gfn_offset: The gfn offset in memory slot
  371. * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
  372. * slot to be write protected
  373. *
  374. * Walks bits set in mask write protects the associated pte's. Caller must
  375. * acquire @kvm->mmu_lock.
  376. */
  377. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  378. struct kvm_memory_slot *slot,
  379. gfn_t gfn_offset, unsigned long mask)
  380. {
  381. gfn_t base_gfn = slot->base_gfn + gfn_offset;
  382. gfn_t start = base_gfn + __ffs(mask);
  383. gfn_t end = base_gfn + __fls(mask);
  384. kvm_mips_mkclean_gpa_pt(kvm, start, end);
  385. }
  386. /*
  387. * kvm_mips_mkold_gpa_pt.
  388. * Mark a range of guest physical address space old (all accesses fault) in the
  389. * VM's GPA page table to allow detection of commonly used pages.
  390. */
  391. BUILD_PTE_RANGE_OP(mkold, pte_mkold)
  392. static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
  393. gfn_t end_gfn)
  394. {
  395. return kvm_mips_mkold_pgd(kvm->arch.gpa_mm.pgd,
  396. start_gfn << PAGE_SHIFT,
  397. end_gfn << PAGE_SHIFT);
  398. }
  399. bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
  400. {
  401. kvm_mips_flush_gpa_pt(kvm, range->start, range->end);
  402. return true;
  403. }
  404. bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  405. {
  406. return kvm_mips_mkold_gpa_pt(kvm, range->start, range->end);
  407. }
  408. bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  409. {
  410. gpa_t gpa = range->start << PAGE_SHIFT;
  411. pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
  412. if (!gpa_pte)
  413. return false;
  414. return pte_young(*gpa_pte);
  415. }
  416. /**
  417. * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
  418. * @vcpu: VCPU pointer.
  419. * @gpa: Guest physical address of fault.
  420. * @write_fault: Whether the fault was due to a write.
  421. * @out_entry: New PTE for @gpa (written on success unless NULL).
  422. * @out_buddy: New PTE for @gpa's buddy (written on success unless
  423. * NULL).
  424. *
  425. * Perform fast path GPA fault handling, doing all that can be done without
  426. * calling into KVM. This handles marking old pages young (for idle page
  427. * tracking), and dirtying of clean pages (for dirty page logging).
  428. *
  429. * Returns: 0 on success, in which case we can update derived mappings and
  430. * resume guest execution.
  431. * -EFAULT on failure due to absent GPA mapping or write to
  432. * read-only page, in which case KVM must be consulted.
  433. */
  434. static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa,
  435. bool write_fault,
  436. pte_t *out_entry, pte_t *out_buddy)
  437. {
  438. struct kvm *kvm = vcpu->kvm;
  439. gfn_t gfn = gpa >> PAGE_SHIFT;
  440. pte_t *ptep;
  441. int ret = 0;
  442. spin_lock(&kvm->mmu_lock);
  443. /* Fast path - just check GPA page table for an existing entry */
  444. ptep = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
  445. if (!ptep || !pte_present(*ptep)) {
  446. ret = -EFAULT;
  447. goto out;
  448. }
  449. /* Track access to pages marked old */
  450. if (!pte_young(*ptep))
  451. set_pte(ptep, pte_mkyoung(*ptep));
  452. if (write_fault && !pte_dirty(*ptep)) {
  453. if (!pte_write(*ptep)) {
  454. ret = -EFAULT;
  455. goto out;
  456. }
  457. /* Track dirtying of writeable pages */
  458. set_pte(ptep, pte_mkdirty(*ptep));
  459. mark_page_dirty(kvm, gfn);
  460. }
  461. if (out_entry)
  462. *out_entry = *ptep;
  463. if (out_buddy)
  464. *out_buddy = *ptep_buddy(ptep);
  465. out:
  466. spin_unlock(&kvm->mmu_lock);
  467. return ret;
  468. }
  469. /**
  470. * kvm_mips_map_page() - Map a guest physical page.
  471. * @vcpu: VCPU pointer.
  472. * @gpa: Guest physical address of fault.
  473. * @write_fault: Whether the fault was due to a write.
  474. * @out_entry: New PTE for @gpa (written on success unless NULL).
  475. * @out_buddy: New PTE for @gpa's buddy (written on success unless
  476. * NULL).
  477. *
  478. * Handle GPA faults by creating a new GPA mapping (or updating an existing
  479. * one).
  480. *
  481. * This takes care of marking pages young or dirty (idle/dirty page tracking),
  482. * asking KVM for the corresponding PFN, and creating a mapping in the GPA page
  483. * tables. Derived mappings (GVA page tables and TLBs) must be handled by the
  484. * caller.
  485. *
  486. * Returns: 0 on success, in which case the caller may use the @out_entry
  487. * and @out_buddy PTEs to update derived mappings and resume guest
  488. * execution.
  489. * -EFAULT if there is no memory region at @gpa or a write was
  490. * attempted to a read-only memory region. This is usually handled
  491. * as an MMIO access.
  492. */
  493. static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa,
  494. bool write_fault,
  495. pte_t *out_entry, pte_t *out_buddy)
  496. {
  497. struct kvm *kvm = vcpu->kvm;
  498. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  499. gfn_t gfn = gpa >> PAGE_SHIFT;
  500. int srcu_idx, err;
  501. kvm_pfn_t pfn;
  502. pte_t *ptep, entry;
  503. bool writeable;
  504. unsigned long prot_bits;
  505. unsigned long mmu_seq;
  506. struct page *page;
  507. /* Try the fast path to handle old / clean pages */
  508. srcu_idx = srcu_read_lock(&kvm->srcu);
  509. err = _kvm_mips_map_page_fast(vcpu, gpa, write_fault, out_entry,
  510. out_buddy);
  511. if (!err)
  512. goto out;
  513. /* We need a minimum of cached pages ready for page table creation */
  514. err = kvm_mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES);
  515. if (err)
  516. goto out;
  517. retry:
  518. /*
  519. * Used to check for invalidations in progress, of the pfn that is
  520. * returned by pfn_to_pfn_prot below.
  521. */
  522. mmu_seq = kvm->mmu_invalidate_seq;
  523. /*
  524. * Ensure the read of mmu_invalidate_seq isn't reordered with PTE reads
  525. * in kvm_faultin_pfn() (which calls get_user_pages()), so that we don't
  526. * risk the page we get a reference to getting unmapped before we have a
  527. * chance to grab the mmu_lock without mmu_invalidate_retry() noticing.
  528. *
  529. * This smp_rmb() pairs with the effective smp_wmb() of the combination
  530. * of the pte_unmap_unlock() after the PTE is zapped, and the
  531. * spin_lock() in kvm_mmu_notifier_invalidate_<page|range_end>() before
  532. * mmu_invalidate_seq is incremented.
  533. */
  534. smp_rmb();
  535. /* Slow path - ask KVM core whether we can access this GPA */
  536. pfn = kvm_faultin_pfn(vcpu, gfn, write_fault, &writeable, &page);
  537. if (is_error_noslot_pfn(pfn)) {
  538. err = -EFAULT;
  539. goto out;
  540. }
  541. spin_lock(&kvm->mmu_lock);
  542. /* Check if an invalidation has taken place since we got pfn */
  543. if (mmu_invalidate_retry(kvm, mmu_seq)) {
  544. /*
  545. * This can happen when mappings are changed asynchronously, but
  546. * also synchronously if a COW is triggered by
  547. * kvm_faultin_pfn().
  548. */
  549. spin_unlock(&kvm->mmu_lock);
  550. kvm_release_page_unused(page);
  551. goto retry;
  552. }
  553. /* Ensure page tables are allocated */
  554. ptep = kvm_mips_pte_for_gpa(kvm, memcache, gpa);
  555. /* Set up the PTE */
  556. prot_bits = _PAGE_PRESENT | __READABLE | _page_cachable_default;
  557. if (writeable) {
  558. prot_bits |= _PAGE_WRITE;
  559. if (write_fault) {
  560. prot_bits |= __WRITEABLE;
  561. mark_page_dirty(kvm, gfn);
  562. }
  563. }
  564. entry = pfn_pte(pfn, __pgprot(prot_bits));
  565. /* Write the PTE */
  566. set_pte(ptep, entry);
  567. err = 0;
  568. if (out_entry)
  569. *out_entry = *ptep;
  570. if (out_buddy)
  571. *out_buddy = *ptep_buddy(ptep);
  572. kvm_release_faultin_page(kvm, page, false, writeable);
  573. spin_unlock(&kvm->mmu_lock);
  574. out:
  575. srcu_read_unlock(&kvm->srcu, srcu_idx);
  576. return err;
  577. }
  578. int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr,
  579. struct kvm_vcpu *vcpu,
  580. bool write_fault)
  581. {
  582. int ret;
  583. ret = kvm_mips_map_page(vcpu, badvaddr, write_fault, NULL, NULL);
  584. if (ret)
  585. return ret;
  586. /* Invalidate this entry in the TLB */
  587. return kvm_vz_host_tlb_inv(vcpu, badvaddr);
  588. }
  589. /**
  590. * kvm_mips_migrate_count() - Migrate timer.
  591. * @vcpu: Virtual CPU.
  592. *
  593. * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
  594. * if it was running prior to being cancelled.
  595. *
  596. * Must be called when the VCPU is migrated to a different CPU to ensure that
  597. * timer expiry during guest execution interrupts the guest and causes the
  598. * interrupt to be delivered in a timely manner.
  599. */
  600. static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
  601. {
  602. if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
  603. hrtimer_restart(&vcpu->arch.comparecount_timer);
  604. }
  605. /* Restore ASID once we are scheduled back after preemption */
  606. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  607. {
  608. unsigned long flags;
  609. kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
  610. local_irq_save(flags);
  611. vcpu->cpu = cpu;
  612. if (vcpu->arch.last_sched_cpu != cpu) {
  613. kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
  614. vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
  615. /*
  616. * Migrate the timer interrupt to the current CPU so that it
  617. * always interrupts the guest and synchronously triggers a
  618. * guest timer interrupt.
  619. */
  620. kvm_mips_migrate_count(vcpu);
  621. }
  622. /* restore guest state to registers */
  623. kvm_mips_callbacks->vcpu_load(vcpu, cpu);
  624. local_irq_restore(flags);
  625. }
  626. /* ASID can change if another task is scheduled during preemption */
  627. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  628. {
  629. unsigned long flags;
  630. int cpu;
  631. local_irq_save(flags);
  632. cpu = smp_processor_id();
  633. vcpu->arch.last_sched_cpu = cpu;
  634. vcpu->cpu = -1;
  635. /* save guest state in registers */
  636. kvm_mips_callbacks->vcpu_put(vcpu, cpu);
  637. local_irq_restore(flags);
  638. }