paging_tmpl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * MMU support
  8. *
  9. * Copyright (C) 2006 Qumranet, Inc.
  10. * Copyright 2010 Red Hat, Inc. and/or its affilates.
  11. *
  12. * Authors:
  13. * Yaniv Kamay <yaniv@qumranet.com>
  14. * Avi Kivity <avi@qumranet.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. /*
  21. * We need the mmu code to access both 32-bit and 64-bit guest ptes,
  22. * so the code in this file is compiled twice, once per pte size.
  23. */
  24. #if PTTYPE == 64
  25. #define pt_element_t u64
  26. #define guest_walker guest_walker64
  27. #define FNAME(name) paging##64_##name
  28. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  29. #define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
  30. #define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
  31. #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
  32. #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level)
  33. #define PT_LEVEL_BITS PT64_LEVEL_BITS
  34. #ifdef CONFIG_X86_64
  35. #define PT_MAX_FULL_LEVELS 4
  36. #define CMPXCHG cmpxchg
  37. #else
  38. #define CMPXCHG cmpxchg64
  39. #define PT_MAX_FULL_LEVELS 2
  40. #endif
  41. #elif PTTYPE == 32
  42. #define pt_element_t u32
  43. #define guest_walker guest_walker32
  44. #define FNAME(name) paging##32_##name
  45. #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
  46. #define PT_LVL_ADDR_MASK(lvl) PT32_LVL_ADDR_MASK(lvl)
  47. #define PT_LVL_OFFSET_MASK(lvl) PT32_LVL_OFFSET_MASK(lvl)
  48. #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
  49. #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
  50. #define PT_LEVEL_BITS PT32_LEVEL_BITS
  51. #define PT_MAX_FULL_LEVELS 2
  52. #define CMPXCHG cmpxchg
  53. #else
  54. #error Invalid PTTYPE value
  55. #endif
  56. #define gpte_to_gfn_lvl FNAME(gpte_to_gfn_lvl)
  57. #define gpte_to_gfn(pte) gpte_to_gfn_lvl((pte), PT_PAGE_TABLE_LEVEL)
  58. /*
  59. * The guest_walker structure emulates the behavior of the hardware page
  60. * table walker.
  61. */
  62. struct guest_walker {
  63. int level;
  64. gfn_t table_gfn[PT_MAX_FULL_LEVELS];
  65. pt_element_t ptes[PT_MAX_FULL_LEVELS];
  66. pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
  67. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  68. unsigned pt_access;
  69. unsigned pte_access;
  70. gfn_t gfn;
  71. u32 error_code;
  72. };
  73. static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
  74. {
  75. return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
  76. }
  77. static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
  78. gfn_t table_gfn, unsigned index,
  79. pt_element_t orig_pte, pt_element_t new_pte)
  80. {
  81. pt_element_t ret;
  82. pt_element_t *table;
  83. struct page *page;
  84. page = gfn_to_page(kvm, table_gfn);
  85. table = kmap_atomic(page, KM_USER0);
  86. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  87. kunmap_atomic(table, KM_USER0);
  88. kvm_release_page_dirty(page);
  89. return (ret != orig_pte);
  90. }
  91. static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
  92. {
  93. unsigned access;
  94. access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
  95. #if PTTYPE == 64
  96. if (is_nx(vcpu))
  97. access &= ~(gpte >> PT64_NX_SHIFT);
  98. #endif
  99. return access;
  100. }
  101. /*
  102. * Fetch a guest pte for a guest virtual address
  103. */
  104. static int FNAME(walk_addr)(struct guest_walker *walker,
  105. struct kvm_vcpu *vcpu, gva_t addr,
  106. int write_fault, int user_fault, int fetch_fault)
  107. {
  108. pt_element_t pte;
  109. gfn_t table_gfn;
  110. unsigned index, pt_access, uninitialized_var(pte_access);
  111. gpa_t pte_gpa;
  112. bool eperm, present, rsvd_fault;
  113. trace_kvm_mmu_pagetable_walk(addr, write_fault, user_fault,
  114. fetch_fault);
  115. walk:
  116. present = true;
  117. eperm = rsvd_fault = false;
  118. walker->level = vcpu->arch.mmu.root_level;
  119. pte = vcpu->arch.mmu.get_cr3(vcpu);
  120. #if PTTYPE == 64
  121. if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
  122. pte = kvm_pdptr_read(vcpu, (addr >> 30) & 3);
  123. trace_kvm_mmu_paging_element(pte, walker->level);
  124. if (!is_present_gpte(pte)) {
  125. present = false;
  126. goto error;
  127. }
  128. --walker->level;
  129. }
  130. #endif
  131. ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
  132. (vcpu->arch.mmu.get_cr3(vcpu) & CR3_NONPAE_RESERVED_BITS) == 0);
  133. pt_access = ACC_ALL;
  134. for (;;) {
  135. index = PT_INDEX(addr, walker->level);
  136. table_gfn = gpte_to_gfn(pte);
  137. pte_gpa = gfn_to_gpa(table_gfn);
  138. pte_gpa += index * sizeof(pt_element_t);
  139. walker->table_gfn[walker->level - 1] = table_gfn;
  140. walker->pte_gpa[walker->level - 1] = pte_gpa;
  141. if (kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte))) {
  142. present = false;
  143. break;
  144. }
  145. trace_kvm_mmu_paging_element(pte, walker->level);
  146. if (!is_present_gpte(pte)) {
  147. present = false;
  148. break;
  149. }
  150. if (is_rsvd_bits_set(&vcpu->arch.mmu, pte, walker->level)) {
  151. rsvd_fault = true;
  152. break;
  153. }
  154. if (write_fault && !is_writable_pte(pte))
  155. if (user_fault || is_write_protection(vcpu))
  156. eperm = true;
  157. if (user_fault && !(pte & PT_USER_MASK))
  158. eperm = true;
  159. #if PTTYPE == 64
  160. if (fetch_fault && (pte & PT64_NX_MASK))
  161. eperm = true;
  162. #endif
  163. if (!eperm && !rsvd_fault && !(pte & PT_ACCESSED_MASK)) {
  164. trace_kvm_mmu_set_accessed_bit(table_gfn, index,
  165. sizeof(pte));
  166. if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
  167. index, pte, pte|PT_ACCESSED_MASK))
  168. goto walk;
  169. mark_page_dirty(vcpu->kvm, table_gfn);
  170. pte |= PT_ACCESSED_MASK;
  171. }
  172. pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
  173. walker->ptes[walker->level - 1] = pte;
  174. if ((walker->level == PT_PAGE_TABLE_LEVEL) ||
  175. ((walker->level == PT_DIRECTORY_LEVEL) &&
  176. is_large_pte(pte) &&
  177. (PTTYPE == 64 || is_pse(vcpu))) ||
  178. ((walker->level == PT_PDPE_LEVEL) &&
  179. is_large_pte(pte) &&
  180. vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL)) {
  181. int lvl = walker->level;
  182. walker->gfn = gpte_to_gfn_lvl(pte, lvl);
  183. walker->gfn += (addr & PT_LVL_OFFSET_MASK(lvl))
  184. >> PAGE_SHIFT;
  185. if (PTTYPE == 32 &&
  186. walker->level == PT_DIRECTORY_LEVEL &&
  187. is_cpuid_PSE36())
  188. walker->gfn += pse36_gfn_delta(pte);
  189. break;
  190. }
  191. pt_access = pte_access;
  192. --walker->level;
  193. }
  194. if (!present || eperm || rsvd_fault)
  195. goto error;
  196. if (write_fault && !is_dirty_gpte(pte)) {
  197. bool ret;
  198. trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
  199. ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
  200. pte|PT_DIRTY_MASK);
  201. if (ret)
  202. goto walk;
  203. mark_page_dirty(vcpu->kvm, table_gfn);
  204. pte |= PT_DIRTY_MASK;
  205. walker->ptes[walker->level - 1] = pte;
  206. }
  207. walker->pt_access = pt_access;
  208. walker->pte_access = pte_access;
  209. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  210. __func__, (u64)pte, pte_access, pt_access);
  211. return 1;
  212. error:
  213. walker->error_code = 0;
  214. if (present)
  215. walker->error_code |= PFERR_PRESENT_MASK;
  216. if (write_fault)
  217. walker->error_code |= PFERR_WRITE_MASK;
  218. if (user_fault)
  219. walker->error_code |= PFERR_USER_MASK;
  220. if (fetch_fault && is_nx(vcpu))
  221. walker->error_code |= PFERR_FETCH_MASK;
  222. if (rsvd_fault)
  223. walker->error_code |= PFERR_RSVD_MASK;
  224. trace_kvm_mmu_walker_error(walker->error_code);
  225. return 0;
  226. }
  227. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  228. u64 *spte, const void *pte)
  229. {
  230. pt_element_t gpte;
  231. unsigned pte_access;
  232. pfn_t pfn;
  233. u64 new_spte;
  234. gpte = *(const pt_element_t *)pte;
  235. if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
  236. if (!is_present_gpte(gpte)) {
  237. if (sp->unsync)
  238. new_spte = shadow_trap_nonpresent_pte;
  239. else
  240. new_spte = shadow_notrap_nonpresent_pte;
  241. __set_spte(spte, new_spte);
  242. }
  243. return;
  244. }
  245. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  246. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  247. if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
  248. return;
  249. pfn = vcpu->arch.update_pte.pfn;
  250. if (is_error_pfn(pfn))
  251. return;
  252. if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq))
  253. return;
  254. kvm_get_pfn(pfn);
  255. /*
  256. * we call mmu_set_spte() with reset_host_protection = true beacuse that
  257. * vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
  258. */
  259. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  260. is_dirty_gpte(gpte), NULL, PT_PAGE_TABLE_LEVEL,
  261. gpte_to_gfn(gpte), pfn, true, true);
  262. }
  263. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  264. struct guest_walker *gw, int level)
  265. {
  266. pt_element_t curr_pte;
  267. gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
  268. u64 mask;
  269. int r, index;
  270. if (level == PT_PAGE_TABLE_LEVEL) {
  271. mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
  272. base_gpa = pte_gpa & ~mask;
  273. index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
  274. r = kvm_read_guest_atomic(vcpu->kvm, base_gpa,
  275. gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
  276. curr_pte = gw->prefetch_ptes[index];
  277. } else
  278. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa,
  279. &curr_pte, sizeof(curr_pte));
  280. return r || curr_pte != gw->ptes[level - 1];
  281. }
  282. static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
  283. u64 *sptep)
  284. {
  285. struct kvm_mmu_page *sp;
  286. struct kvm_mmu *mmu = &vcpu->arch.mmu;
  287. pt_element_t *gptep = gw->prefetch_ptes;
  288. u64 *spte;
  289. int i;
  290. sp = page_header(__pa(sptep));
  291. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  292. return;
  293. if (sp->role.direct)
  294. return __direct_pte_prefetch(vcpu, sp, sptep);
  295. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  296. spte = sp->spt + i;
  297. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  298. pt_element_t gpte;
  299. unsigned pte_access;
  300. gfn_t gfn;
  301. pfn_t pfn;
  302. bool dirty;
  303. if (spte == sptep)
  304. continue;
  305. if (*spte != shadow_trap_nonpresent_pte)
  306. continue;
  307. gpte = gptep[i];
  308. if (!is_present_gpte(gpte) ||
  309. is_rsvd_bits_set(mmu, gpte, PT_PAGE_TABLE_LEVEL)) {
  310. if (!sp->unsync)
  311. __set_spte(spte, shadow_notrap_nonpresent_pte);
  312. continue;
  313. }
  314. if (!(gpte & PT_ACCESSED_MASK))
  315. continue;
  316. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  317. gfn = gpte_to_gfn(gpte);
  318. dirty = is_dirty_gpte(gpte);
  319. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  320. (pte_access & ACC_WRITE_MASK) && dirty);
  321. if (is_error_pfn(pfn)) {
  322. kvm_release_pfn_clean(pfn);
  323. break;
  324. }
  325. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  326. dirty, NULL, PT_PAGE_TABLE_LEVEL, gfn,
  327. pfn, true, true);
  328. }
  329. }
  330. /*
  331. * Fetch a shadow pte for a specific level in the paging hierarchy.
  332. */
  333. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  334. struct guest_walker *gw,
  335. int user_fault, int write_fault, int hlevel,
  336. int *ptwrite, pfn_t pfn)
  337. {
  338. unsigned access = gw->pt_access;
  339. struct kvm_mmu_page *sp = NULL;
  340. bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
  341. int top_level;
  342. unsigned direct_access;
  343. struct kvm_shadow_walk_iterator it;
  344. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  345. return NULL;
  346. direct_access = gw->pt_access & gw->pte_access;
  347. if (!dirty)
  348. direct_access &= ~ACC_WRITE_MASK;
  349. top_level = vcpu->arch.mmu.root_level;
  350. if (top_level == PT32E_ROOT_LEVEL)
  351. top_level = PT32_ROOT_LEVEL;
  352. /*
  353. * Verify that the top-level gpte is still there. Since the page
  354. * is a root page, it is either write protected (and cannot be
  355. * changed from now on) or it is invalid (in which case, we don't
  356. * really care if it changes underneath us after this point).
  357. */
  358. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  359. goto out_gpte_changed;
  360. for (shadow_walk_init(&it, vcpu, addr);
  361. shadow_walk_okay(&it) && it.level > gw->level;
  362. shadow_walk_next(&it)) {
  363. gfn_t table_gfn;
  364. drop_large_spte(vcpu, it.sptep);
  365. sp = NULL;
  366. if (!is_shadow_present_pte(*it.sptep)) {
  367. table_gfn = gw->table_gfn[it.level - 2];
  368. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  369. false, access, it.sptep);
  370. }
  371. /*
  372. * Verify that the gpte in the page we've just write
  373. * protected is still there.
  374. */
  375. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  376. goto out_gpte_changed;
  377. if (sp)
  378. link_shadow_page(it.sptep, sp);
  379. }
  380. for (;
  381. shadow_walk_okay(&it) && it.level > hlevel;
  382. shadow_walk_next(&it)) {
  383. gfn_t direct_gfn;
  384. validate_direct_spte(vcpu, it.sptep, direct_access);
  385. drop_large_spte(vcpu, it.sptep);
  386. if (is_shadow_present_pte(*it.sptep))
  387. continue;
  388. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  389. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  390. true, direct_access, it.sptep);
  391. link_shadow_page(it.sptep, sp);
  392. }
  393. mmu_set_spte(vcpu, it.sptep, access, gw->pte_access & access,
  394. user_fault, write_fault, dirty, ptwrite, it.level,
  395. gw->gfn, pfn, false, true);
  396. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  397. return it.sptep;
  398. out_gpte_changed:
  399. if (sp)
  400. kvm_mmu_put_page(sp, it.sptep);
  401. kvm_release_pfn_clean(pfn);
  402. return NULL;
  403. }
  404. /*
  405. * Page fault handler. There are several causes for a page fault:
  406. * - there is no shadow pte for the guest pte
  407. * - write access through a shadow pte marked read only so that we can set
  408. * the dirty bit
  409. * - write access to a shadow pte marked read only so we can update the page
  410. * dirty bitmap, when userspace requests it
  411. * - mmio access; in this case we will never install a present shadow pte
  412. * - normal guest page fault due to the guest pte marked not present, not
  413. * writable, or not executable
  414. *
  415. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  416. * a negative value on error.
  417. */
  418. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  419. u32 error_code)
  420. {
  421. int write_fault = error_code & PFERR_WRITE_MASK;
  422. int user_fault = error_code & PFERR_USER_MASK;
  423. int fetch_fault = error_code & PFERR_FETCH_MASK;
  424. struct guest_walker walker;
  425. u64 *sptep;
  426. int write_pt = 0;
  427. int r;
  428. pfn_t pfn;
  429. int level = PT_PAGE_TABLE_LEVEL;
  430. unsigned long mmu_seq;
  431. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  432. r = mmu_topup_memory_caches(vcpu);
  433. if (r)
  434. return r;
  435. /*
  436. * Look up the guest pte for the faulting address.
  437. */
  438. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  439. fetch_fault);
  440. /*
  441. * The page is not mapped by the guest. Let the guest handle it.
  442. */
  443. if (!r) {
  444. pgprintk("%s: guest page fault\n", __func__);
  445. inject_page_fault(vcpu, addr, walker.error_code);
  446. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  447. return 0;
  448. }
  449. if (walker.level >= PT_DIRECTORY_LEVEL) {
  450. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  451. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  452. }
  453. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  454. smp_rmb();
  455. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  456. /* mmio */
  457. if (is_error_pfn(pfn))
  458. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  459. spin_lock(&vcpu->kvm->mmu_lock);
  460. if (mmu_notifier_retry(vcpu, mmu_seq))
  461. goto out_unlock;
  462. trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  463. kvm_mmu_free_some_pages(vcpu);
  464. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  465. level, &write_pt, pfn);
  466. (void)sptep;
  467. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  468. sptep, *sptep, write_pt);
  469. if (!write_pt)
  470. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  471. ++vcpu->stat.pf_fixed;
  472. trace_kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  473. spin_unlock(&vcpu->kvm->mmu_lock);
  474. return write_pt;
  475. out_unlock:
  476. spin_unlock(&vcpu->kvm->mmu_lock);
  477. kvm_release_pfn_clean(pfn);
  478. return 0;
  479. }
  480. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  481. {
  482. struct kvm_shadow_walk_iterator iterator;
  483. struct kvm_mmu_page *sp;
  484. gpa_t pte_gpa = -1;
  485. int level;
  486. u64 *sptep;
  487. int need_flush = 0;
  488. spin_lock(&vcpu->kvm->mmu_lock);
  489. for_each_shadow_entry(vcpu, gva, iterator) {
  490. level = iterator.level;
  491. sptep = iterator.sptep;
  492. sp = page_header(__pa(sptep));
  493. if (is_last_spte(*sptep, level)) {
  494. int offset, shift;
  495. if (!sp->unsync)
  496. break;
  497. shift = PAGE_SHIFT -
  498. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  499. offset = sp->role.quadrant << shift;
  500. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  501. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  502. if (is_shadow_present_pte(*sptep)) {
  503. if (is_large_pte(*sptep))
  504. --vcpu->kvm->stat.lpages;
  505. drop_spte(vcpu->kvm, sptep,
  506. shadow_trap_nonpresent_pte);
  507. need_flush = 1;
  508. } else
  509. __set_spte(sptep, shadow_trap_nonpresent_pte);
  510. break;
  511. }
  512. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  513. break;
  514. }
  515. if (need_flush)
  516. kvm_flush_remote_tlbs(vcpu->kvm);
  517. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  518. spin_unlock(&vcpu->kvm->mmu_lock);
  519. if (pte_gpa == -1)
  520. return;
  521. if (mmu_topup_memory_caches(vcpu))
  522. return;
  523. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  524. }
  525. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  526. u32 *error)
  527. {
  528. struct guest_walker walker;
  529. gpa_t gpa = UNMAPPED_GVA;
  530. int r;
  531. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  532. !!(access & PFERR_WRITE_MASK),
  533. !!(access & PFERR_USER_MASK),
  534. !!(access & PFERR_FETCH_MASK));
  535. if (r) {
  536. gpa = gfn_to_gpa(walker.gfn);
  537. gpa |= vaddr & ~PAGE_MASK;
  538. } else if (error)
  539. *error = walker.error_code;
  540. return gpa;
  541. }
  542. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  543. struct kvm_mmu_page *sp)
  544. {
  545. int i, j, offset, r;
  546. pt_element_t pt[256 / sizeof(pt_element_t)];
  547. gpa_t pte_gpa;
  548. if (sp->role.direct
  549. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  550. nonpaging_prefetch_page(vcpu, sp);
  551. return;
  552. }
  553. pte_gpa = gfn_to_gpa(sp->gfn);
  554. if (PTTYPE == 32) {
  555. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  556. pte_gpa += offset * sizeof(pt_element_t);
  557. }
  558. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  559. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  560. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  561. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  562. if (r || is_present_gpte(pt[j]))
  563. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  564. else
  565. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  566. }
  567. }
  568. /*
  569. * Using the cached information from sp->gfns is safe because:
  570. * - The spte has a reference to the struct page, so the pfn for a given gfn
  571. * can't change unless all sptes pointing to it are nuked first.
  572. */
  573. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  574. bool clear_unsync)
  575. {
  576. int i, offset, nr_present;
  577. bool reset_host_protection;
  578. gpa_t first_pte_gpa;
  579. offset = nr_present = 0;
  580. /* direct kvm_mmu_page can not be unsync. */
  581. BUG_ON(sp->role.direct);
  582. if (PTTYPE == 32)
  583. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  584. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  585. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  586. unsigned pte_access;
  587. pt_element_t gpte;
  588. gpa_t pte_gpa;
  589. gfn_t gfn;
  590. if (!is_shadow_present_pte(sp->spt[i]))
  591. continue;
  592. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  593. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  594. sizeof(pt_element_t)))
  595. return -EINVAL;
  596. gfn = gpte_to_gfn(gpte);
  597. if (is_rsvd_bits_set(&vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL)
  598. || gfn != sp->gfns[i] || !is_present_gpte(gpte)
  599. || !(gpte & PT_ACCESSED_MASK)) {
  600. u64 nonpresent;
  601. if (is_present_gpte(gpte) || !clear_unsync)
  602. nonpresent = shadow_trap_nonpresent_pte;
  603. else
  604. nonpresent = shadow_notrap_nonpresent_pte;
  605. drop_spte(vcpu->kvm, &sp->spt[i], nonpresent);
  606. continue;
  607. }
  608. nr_present++;
  609. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  610. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  611. pte_access &= ~ACC_WRITE_MASK;
  612. reset_host_protection = 0;
  613. } else {
  614. reset_host_protection = 1;
  615. }
  616. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  617. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  618. spte_to_pfn(sp->spt[i]), true, false,
  619. reset_host_protection);
  620. }
  621. return !nr_present;
  622. }
  623. #undef pt_element_t
  624. #undef guest_walker
  625. #undef FNAME
  626. #undef PT_BASE_ADDR_MASK
  627. #undef PT_INDEX
  628. #undef PT_LEVEL_MASK
  629. #undef PT_LVL_ADDR_MASK
  630. #undef PT_LVL_OFFSET_MASK
  631. #undef PT_LEVEL_BITS
  632. #undef PT_MAX_FULL_LEVELS
  633. #undef gpte_to_gfn
  634. #undef gpte_to_gfn_lvl
  635. #undef CMPXCHG