paging_tmpl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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.cr3;
  120. #if PTTYPE == 64
  121. if (!is_long_mode(vcpu)) {
  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.cr3 & 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, 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. is_long_mode(vcpu))) {
  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. pt_element_t *gptep = gw->prefetch_ptes;
  287. u64 *spte;
  288. int i;
  289. sp = page_header(__pa(sptep));
  290. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  291. return;
  292. if (sp->role.direct)
  293. return __direct_pte_prefetch(vcpu, sp, sptep);
  294. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  295. spte = sp->spt + i;
  296. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  297. pt_element_t gpte;
  298. unsigned pte_access;
  299. gfn_t gfn;
  300. pfn_t pfn;
  301. bool dirty;
  302. if (spte == sptep)
  303. continue;
  304. if (*spte != shadow_trap_nonpresent_pte)
  305. continue;
  306. gpte = gptep[i];
  307. if (!is_present_gpte(gpte) ||
  308. is_rsvd_bits_set(vcpu, gpte, PT_PAGE_TABLE_LEVEL)) {
  309. if (!sp->unsync)
  310. __set_spte(spte, shadow_notrap_nonpresent_pte);
  311. continue;
  312. }
  313. if (!(gpte & PT_ACCESSED_MASK))
  314. continue;
  315. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  316. gfn = gpte_to_gfn(gpte);
  317. dirty = is_dirty_gpte(gpte);
  318. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  319. (pte_access & ACC_WRITE_MASK) && dirty);
  320. if (is_error_pfn(pfn)) {
  321. kvm_release_pfn_clean(pfn);
  322. break;
  323. }
  324. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  325. dirty, NULL, PT_PAGE_TABLE_LEVEL, gfn,
  326. pfn, true, true);
  327. }
  328. }
  329. /*
  330. * Fetch a shadow pte for a specific level in the paging hierarchy.
  331. */
  332. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  333. struct guest_walker *gw,
  334. int user_fault, int write_fault, int hlevel,
  335. int *ptwrite, pfn_t pfn)
  336. {
  337. unsigned access = gw->pt_access;
  338. struct kvm_mmu_page *sp = NULL;
  339. bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
  340. int top_level;
  341. unsigned direct_access;
  342. struct kvm_shadow_walk_iterator it;
  343. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  344. return NULL;
  345. direct_access = gw->pt_access & gw->pte_access;
  346. if (!dirty)
  347. direct_access &= ~ACC_WRITE_MASK;
  348. top_level = vcpu->arch.mmu.root_level;
  349. if (top_level == PT32E_ROOT_LEVEL)
  350. top_level = PT32_ROOT_LEVEL;
  351. /*
  352. * Verify that the top-level gpte is still there. Since the page
  353. * is a root page, it is either write protected (and cannot be
  354. * changed from now on) or it is invalid (in which case, we don't
  355. * really care if it changes underneath us after this point).
  356. */
  357. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  358. goto out_gpte_changed;
  359. for (shadow_walk_init(&it, vcpu, addr);
  360. shadow_walk_okay(&it) && it.level > gw->level;
  361. shadow_walk_next(&it)) {
  362. gfn_t table_gfn;
  363. drop_large_spte(vcpu, it.sptep);
  364. sp = NULL;
  365. if (!is_shadow_present_pte(*it.sptep)) {
  366. table_gfn = gw->table_gfn[it.level - 2];
  367. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  368. false, access, it.sptep);
  369. }
  370. /*
  371. * Verify that the gpte in the page we've just write
  372. * protected is still there.
  373. */
  374. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  375. goto out_gpte_changed;
  376. if (sp)
  377. link_shadow_page(it.sptep, sp);
  378. }
  379. for (;
  380. shadow_walk_okay(&it) && it.level > hlevel;
  381. shadow_walk_next(&it)) {
  382. gfn_t direct_gfn;
  383. validate_direct_spte(vcpu, it.sptep, direct_access);
  384. drop_large_spte(vcpu, it.sptep);
  385. if (is_shadow_present_pte(*it.sptep))
  386. continue;
  387. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  388. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  389. true, direct_access, it.sptep);
  390. link_shadow_page(it.sptep, sp);
  391. }
  392. mmu_set_spte(vcpu, it.sptep, access, gw->pte_access & access,
  393. user_fault, write_fault, dirty, ptwrite, it.level,
  394. gw->gfn, pfn, false, true);
  395. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  396. return it.sptep;
  397. out_gpte_changed:
  398. if (sp)
  399. kvm_mmu_put_page(sp, it.sptep);
  400. kvm_release_pfn_clean(pfn);
  401. return NULL;
  402. }
  403. /*
  404. * Page fault handler. There are several causes for a page fault:
  405. * - there is no shadow pte for the guest pte
  406. * - write access through a shadow pte marked read only so that we can set
  407. * the dirty bit
  408. * - write access to a shadow pte marked read only so we can update the page
  409. * dirty bitmap, when userspace requests it
  410. * - mmio access; in this case we will never install a present shadow pte
  411. * - normal guest page fault due to the guest pte marked not present, not
  412. * writable, or not executable
  413. *
  414. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  415. * a negative value on error.
  416. */
  417. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  418. u32 error_code)
  419. {
  420. int write_fault = error_code & PFERR_WRITE_MASK;
  421. int user_fault = error_code & PFERR_USER_MASK;
  422. int fetch_fault = error_code & PFERR_FETCH_MASK;
  423. struct guest_walker walker;
  424. u64 *sptep;
  425. int write_pt = 0;
  426. int r;
  427. pfn_t pfn;
  428. int level = PT_PAGE_TABLE_LEVEL;
  429. unsigned long mmu_seq;
  430. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  431. r = mmu_topup_memory_caches(vcpu);
  432. if (r)
  433. return r;
  434. /*
  435. * Look up the guest pte for the faulting address.
  436. */
  437. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  438. fetch_fault);
  439. /*
  440. * The page is not mapped by the guest. Let the guest handle it.
  441. */
  442. if (!r) {
  443. pgprintk("%s: guest page fault\n", __func__);
  444. inject_page_fault(vcpu, addr, walker.error_code);
  445. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  446. return 0;
  447. }
  448. if (walker.level >= PT_DIRECTORY_LEVEL) {
  449. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  450. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  451. }
  452. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  453. smp_rmb();
  454. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  455. /* mmio */
  456. if (is_error_pfn(pfn))
  457. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  458. spin_lock(&vcpu->kvm->mmu_lock);
  459. if (mmu_notifier_retry(vcpu, mmu_seq))
  460. goto out_unlock;
  461. trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  462. kvm_mmu_free_some_pages(vcpu);
  463. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  464. level, &write_pt, pfn);
  465. (void)sptep;
  466. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  467. sptep, *sptep, write_pt);
  468. if (!write_pt)
  469. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  470. ++vcpu->stat.pf_fixed;
  471. trace_kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  472. spin_unlock(&vcpu->kvm->mmu_lock);
  473. return write_pt;
  474. out_unlock:
  475. spin_unlock(&vcpu->kvm->mmu_lock);
  476. kvm_release_pfn_clean(pfn);
  477. return 0;
  478. }
  479. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  480. {
  481. struct kvm_shadow_walk_iterator iterator;
  482. struct kvm_mmu_page *sp;
  483. gpa_t pte_gpa = -1;
  484. int level;
  485. u64 *sptep;
  486. int need_flush = 0;
  487. spin_lock(&vcpu->kvm->mmu_lock);
  488. for_each_shadow_entry(vcpu, gva, iterator) {
  489. level = iterator.level;
  490. sptep = iterator.sptep;
  491. sp = page_header(__pa(sptep));
  492. if (is_last_spte(*sptep, level)) {
  493. int offset, shift;
  494. if (!sp->unsync)
  495. break;
  496. shift = PAGE_SHIFT -
  497. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  498. offset = sp->role.quadrant << shift;
  499. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  500. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  501. if (is_shadow_present_pte(*sptep)) {
  502. if (is_large_pte(*sptep))
  503. --vcpu->kvm->stat.lpages;
  504. drop_spte(vcpu->kvm, sptep,
  505. shadow_trap_nonpresent_pte);
  506. need_flush = 1;
  507. } else
  508. __set_spte(sptep, shadow_trap_nonpresent_pte);
  509. break;
  510. }
  511. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  512. break;
  513. }
  514. if (need_flush)
  515. kvm_flush_remote_tlbs(vcpu->kvm);
  516. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  517. spin_unlock(&vcpu->kvm->mmu_lock);
  518. if (pte_gpa == -1)
  519. return;
  520. if (mmu_topup_memory_caches(vcpu))
  521. return;
  522. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  523. }
  524. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  525. u32 *error)
  526. {
  527. struct guest_walker walker;
  528. gpa_t gpa = UNMAPPED_GVA;
  529. int r;
  530. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  531. !!(access & PFERR_WRITE_MASK),
  532. !!(access & PFERR_USER_MASK),
  533. !!(access & PFERR_FETCH_MASK));
  534. if (r) {
  535. gpa = gfn_to_gpa(walker.gfn);
  536. gpa |= vaddr & ~PAGE_MASK;
  537. } else if (error)
  538. *error = walker.error_code;
  539. return gpa;
  540. }
  541. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  542. struct kvm_mmu_page *sp)
  543. {
  544. int i, j, offset, r;
  545. pt_element_t pt[256 / sizeof(pt_element_t)];
  546. gpa_t pte_gpa;
  547. if (sp->role.direct
  548. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  549. nonpaging_prefetch_page(vcpu, sp);
  550. return;
  551. }
  552. pte_gpa = gfn_to_gpa(sp->gfn);
  553. if (PTTYPE == 32) {
  554. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  555. pte_gpa += offset * sizeof(pt_element_t);
  556. }
  557. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  558. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  559. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  560. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  561. if (r || is_present_gpte(pt[j]))
  562. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  563. else
  564. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  565. }
  566. }
  567. /*
  568. * Using the cached information from sp->gfns is safe because:
  569. * - The spte has a reference to the struct page, so the pfn for a given gfn
  570. * can't change unless all sptes pointing to it are nuked first.
  571. */
  572. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  573. bool clear_unsync)
  574. {
  575. int i, offset, nr_present;
  576. bool reset_host_protection;
  577. gpa_t first_pte_gpa;
  578. offset = nr_present = 0;
  579. /* direct kvm_mmu_page can not be unsync. */
  580. BUG_ON(sp->role.direct);
  581. if (PTTYPE == 32)
  582. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  583. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  584. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  585. unsigned pte_access;
  586. pt_element_t gpte;
  587. gpa_t pte_gpa;
  588. gfn_t gfn;
  589. if (!is_shadow_present_pte(sp->spt[i]))
  590. continue;
  591. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  592. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  593. sizeof(pt_element_t)))
  594. return -EINVAL;
  595. gfn = gpte_to_gfn(gpte);
  596. if (is_rsvd_bits_set(vcpu, gpte, PT_PAGE_TABLE_LEVEL)
  597. || gfn != sp->gfns[i] || !is_present_gpte(gpte)
  598. || !(gpte & PT_ACCESSED_MASK)) {
  599. u64 nonpresent;
  600. if (is_present_gpte(gpte) || !clear_unsync)
  601. nonpresent = shadow_trap_nonpresent_pte;
  602. else
  603. nonpresent = shadow_notrap_nonpresent_pte;
  604. drop_spte(vcpu->kvm, &sp->spt[i], nonpresent);
  605. continue;
  606. }
  607. nr_present++;
  608. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  609. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  610. pte_access &= ~ACC_WRITE_MASK;
  611. reset_host_protection = 0;
  612. } else {
  613. reset_host_protection = 1;
  614. }
  615. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  616. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  617. spte_to_pfn(sp->spt[i]), true, false,
  618. reset_host_protection);
  619. }
  620. return !nr_present;
  621. }
  622. #undef pt_element_t
  623. #undef guest_walker
  624. #undef FNAME
  625. #undef PT_BASE_ADDR_MASK
  626. #undef PT_INDEX
  627. #undef PT_LEVEL_MASK
  628. #undef PT_LVL_ADDR_MASK
  629. #undef PT_LVL_OFFSET_MASK
  630. #undef PT_LEVEL_BITS
  631. #undef PT_MAX_FULL_LEVELS
  632. #undef gpte_to_gfn
  633. #undef gpte_to_gfn_lvl
  634. #undef CMPXCHG