paging_tmpl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  67. unsigned pt_access;
  68. unsigned pte_access;
  69. gfn_t gfn;
  70. u32 error_code;
  71. };
  72. static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
  73. {
  74. return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
  75. }
  76. static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
  77. gfn_t table_gfn, unsigned index,
  78. pt_element_t orig_pte, pt_element_t new_pte)
  79. {
  80. pt_element_t ret;
  81. pt_element_t *table;
  82. struct page *page;
  83. page = gfn_to_page(kvm, table_gfn);
  84. table = kmap_atomic(page, KM_USER0);
  85. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  86. kunmap_atomic(table, KM_USER0);
  87. kvm_release_page_dirty(page);
  88. return (ret != orig_pte);
  89. }
  90. static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
  91. {
  92. unsigned access;
  93. access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
  94. #if PTTYPE == 64
  95. if (is_nx(vcpu))
  96. access &= ~(gpte >> PT64_NX_SHIFT);
  97. #endif
  98. return access;
  99. }
  100. /*
  101. * Fetch a guest pte for a guest virtual address
  102. */
  103. static int FNAME(walk_addr)(struct guest_walker *walker,
  104. struct kvm_vcpu *vcpu, gva_t addr,
  105. int write_fault, int user_fault, int fetch_fault)
  106. {
  107. pt_element_t pte;
  108. gfn_t table_gfn;
  109. unsigned index, pt_access, uninitialized_var(pte_access);
  110. gpa_t pte_gpa;
  111. bool eperm, present, rsvd_fault;
  112. trace_kvm_mmu_pagetable_walk(addr, write_fault, user_fault,
  113. fetch_fault);
  114. walk:
  115. present = true;
  116. eperm = rsvd_fault = false;
  117. walker->level = vcpu->arch.mmu.root_level;
  118. pte = vcpu->arch.cr3;
  119. #if PTTYPE == 64
  120. if (!is_long_mode(vcpu)) {
  121. pte = kvm_pdptr_read(vcpu, (addr >> 30) & 3);
  122. trace_kvm_mmu_paging_element(pte, walker->level);
  123. if (!is_present_gpte(pte)) {
  124. present = false;
  125. goto error;
  126. }
  127. --walker->level;
  128. }
  129. #endif
  130. ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
  131. (vcpu->arch.cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
  132. pt_access = ACC_ALL;
  133. for (;;) {
  134. index = PT_INDEX(addr, walker->level);
  135. table_gfn = gpte_to_gfn(pte);
  136. pte_gpa = gfn_to_gpa(table_gfn);
  137. pte_gpa += index * sizeof(pt_element_t);
  138. walker->table_gfn[walker->level - 1] = table_gfn;
  139. walker->pte_gpa[walker->level - 1] = pte_gpa;
  140. if (kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte))) {
  141. present = false;
  142. break;
  143. }
  144. trace_kvm_mmu_paging_element(pte, walker->level);
  145. if (!is_present_gpte(pte)) {
  146. present = false;
  147. break;
  148. }
  149. if (is_rsvd_bits_set(vcpu, pte, walker->level)) {
  150. rsvd_fault = true;
  151. break;
  152. }
  153. if (write_fault && !is_writable_pte(pte))
  154. if (user_fault || is_write_protection(vcpu))
  155. eperm = true;
  156. if (user_fault && !(pte & PT_USER_MASK))
  157. eperm = true;
  158. #if PTTYPE == 64
  159. if (fetch_fault && (pte & PT64_NX_MASK))
  160. eperm = true;
  161. #endif
  162. if (!eperm && !rsvd_fault && !(pte & PT_ACCESSED_MASK)) {
  163. trace_kvm_mmu_set_accessed_bit(table_gfn, index,
  164. sizeof(pte));
  165. if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
  166. index, pte, pte|PT_ACCESSED_MASK))
  167. goto walk;
  168. mark_page_dirty(vcpu->kvm, table_gfn);
  169. pte |= PT_ACCESSED_MASK;
  170. }
  171. pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
  172. walker->ptes[walker->level - 1] = pte;
  173. if ((walker->level == PT_PAGE_TABLE_LEVEL) ||
  174. ((walker->level == PT_DIRECTORY_LEVEL) &&
  175. is_large_pte(pte) &&
  176. (PTTYPE == 64 || is_pse(vcpu))) ||
  177. ((walker->level == PT_PDPE_LEVEL) &&
  178. is_large_pte(pte) &&
  179. is_long_mode(vcpu))) {
  180. int lvl = walker->level;
  181. walker->gfn = gpte_to_gfn_lvl(pte, lvl);
  182. walker->gfn += (addr & PT_LVL_OFFSET_MASK(lvl))
  183. >> PAGE_SHIFT;
  184. if (PTTYPE == 32 &&
  185. walker->level == PT_DIRECTORY_LEVEL &&
  186. is_cpuid_PSE36())
  187. walker->gfn += pse36_gfn_delta(pte);
  188. break;
  189. }
  190. pt_access = pte_access;
  191. --walker->level;
  192. }
  193. if (!present || eperm || rsvd_fault)
  194. goto error;
  195. if (write_fault && !is_dirty_gpte(pte)) {
  196. bool ret;
  197. trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
  198. ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
  199. pte|PT_DIRTY_MASK);
  200. if (ret)
  201. goto walk;
  202. mark_page_dirty(vcpu->kvm, table_gfn);
  203. pte |= PT_DIRTY_MASK;
  204. walker->ptes[walker->level - 1] = pte;
  205. }
  206. walker->pt_access = pt_access;
  207. walker->pte_access = pte_access;
  208. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  209. __func__, (u64)pte, pte_access, pt_access);
  210. return 1;
  211. error:
  212. walker->error_code = 0;
  213. if (present)
  214. walker->error_code |= PFERR_PRESENT_MASK;
  215. if (write_fault)
  216. walker->error_code |= PFERR_WRITE_MASK;
  217. if (user_fault)
  218. walker->error_code |= PFERR_USER_MASK;
  219. if (fetch_fault && is_nx(vcpu))
  220. walker->error_code |= PFERR_FETCH_MASK;
  221. if (rsvd_fault)
  222. walker->error_code |= PFERR_RSVD_MASK;
  223. trace_kvm_mmu_walker_error(walker->error_code);
  224. return 0;
  225. }
  226. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  227. u64 *spte, const void *pte)
  228. {
  229. pt_element_t gpte;
  230. unsigned pte_access;
  231. pfn_t pfn;
  232. u64 new_spte;
  233. gpte = *(const pt_element_t *)pte;
  234. if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
  235. if (!is_present_gpte(gpte)) {
  236. if (sp->unsync)
  237. new_spte = shadow_trap_nonpresent_pte;
  238. else
  239. new_spte = shadow_notrap_nonpresent_pte;
  240. __set_spte(spte, new_spte);
  241. }
  242. return;
  243. }
  244. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  245. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  246. if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
  247. return;
  248. pfn = vcpu->arch.update_pte.pfn;
  249. if (is_error_pfn(pfn))
  250. return;
  251. if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq))
  252. return;
  253. kvm_get_pfn(pfn);
  254. /*
  255. * we call mmu_set_spte() with reset_host_protection = true beacuse that
  256. * vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
  257. */
  258. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  259. is_dirty_gpte(gpte), NULL, PT_PAGE_TABLE_LEVEL,
  260. gpte_to_gfn(gpte), pfn, true, true);
  261. }
  262. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  263. struct guest_walker *gw, int level)
  264. {
  265. int r;
  266. pt_element_t curr_pte;
  267. r = kvm_read_guest_atomic(vcpu->kvm, gw->pte_gpa[level - 1],
  268. &curr_pte, sizeof(curr_pte));
  269. return r || curr_pte != gw->ptes[level - 1];
  270. }
  271. /*
  272. * Fetch a shadow pte for a specific level in the paging hierarchy.
  273. */
  274. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  275. struct guest_walker *gw,
  276. int user_fault, int write_fault, int hlevel,
  277. int *ptwrite, pfn_t pfn)
  278. {
  279. unsigned access = gw->pt_access;
  280. struct kvm_mmu_page *sp = NULL;
  281. bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
  282. int top_level;
  283. unsigned direct_access;
  284. struct kvm_shadow_walk_iterator it;
  285. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  286. return NULL;
  287. direct_access = gw->pt_access & gw->pte_access;
  288. if (!dirty)
  289. direct_access &= ~ACC_WRITE_MASK;
  290. top_level = vcpu->arch.mmu.root_level;
  291. if (top_level == PT32E_ROOT_LEVEL)
  292. top_level = PT32_ROOT_LEVEL;
  293. /*
  294. * Verify that the top-level gpte is still there. Since the page
  295. * is a root page, it is either write protected (and cannot be
  296. * changed from now on) or it is invalid (in which case, we don't
  297. * really care if it changes underneath us after this point).
  298. */
  299. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  300. goto out_gpte_changed;
  301. for (shadow_walk_init(&it, vcpu, addr);
  302. shadow_walk_okay(&it) && it.level > gw->level;
  303. shadow_walk_next(&it)) {
  304. gfn_t table_gfn;
  305. drop_large_spte(vcpu, it.sptep);
  306. sp = NULL;
  307. if (!is_shadow_present_pte(*it.sptep)) {
  308. table_gfn = gw->table_gfn[it.level - 2];
  309. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  310. false, access, it.sptep);
  311. }
  312. /*
  313. * Verify that the gpte in the page we've just write
  314. * protected is still there.
  315. */
  316. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  317. goto out_gpte_changed;
  318. if (sp)
  319. link_shadow_page(it.sptep, sp);
  320. }
  321. for (;
  322. shadow_walk_okay(&it) && it.level > hlevel;
  323. shadow_walk_next(&it)) {
  324. gfn_t direct_gfn;
  325. validate_direct_spte(vcpu, it.sptep, direct_access);
  326. drop_large_spte(vcpu, it.sptep);
  327. if (is_shadow_present_pte(*it.sptep))
  328. continue;
  329. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  330. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  331. true, direct_access, it.sptep);
  332. link_shadow_page(it.sptep, sp);
  333. }
  334. mmu_set_spte(vcpu, it.sptep, access, gw->pte_access & access,
  335. user_fault, write_fault, dirty, ptwrite, it.level,
  336. gw->gfn, pfn, false, true);
  337. return it.sptep;
  338. out_gpte_changed:
  339. if (sp)
  340. kvm_mmu_put_page(sp, it.sptep);
  341. kvm_release_pfn_clean(pfn);
  342. return NULL;
  343. }
  344. /*
  345. * Page fault handler. There are several causes for a page fault:
  346. * - there is no shadow pte for the guest pte
  347. * - write access through a shadow pte marked read only so that we can set
  348. * the dirty bit
  349. * - write access to a shadow pte marked read only so we can update the page
  350. * dirty bitmap, when userspace requests it
  351. * - mmio access; in this case we will never install a present shadow pte
  352. * - normal guest page fault due to the guest pte marked not present, not
  353. * writable, or not executable
  354. *
  355. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  356. * a negative value on error.
  357. */
  358. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  359. u32 error_code)
  360. {
  361. int write_fault = error_code & PFERR_WRITE_MASK;
  362. int user_fault = error_code & PFERR_USER_MASK;
  363. int fetch_fault = error_code & PFERR_FETCH_MASK;
  364. struct guest_walker walker;
  365. u64 *sptep;
  366. int write_pt = 0;
  367. int r;
  368. pfn_t pfn;
  369. int level = PT_PAGE_TABLE_LEVEL;
  370. unsigned long mmu_seq;
  371. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  372. kvm_mmu_audit(vcpu, "pre page fault");
  373. r = mmu_topup_memory_caches(vcpu);
  374. if (r)
  375. return r;
  376. /*
  377. * Look up the guest pte for the faulting address.
  378. */
  379. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  380. fetch_fault);
  381. /*
  382. * The page is not mapped by the guest. Let the guest handle it.
  383. */
  384. if (!r) {
  385. pgprintk("%s: guest page fault\n", __func__);
  386. inject_page_fault(vcpu, addr, walker.error_code);
  387. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  388. return 0;
  389. }
  390. if (walker.level >= PT_DIRECTORY_LEVEL) {
  391. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  392. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  393. }
  394. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  395. smp_rmb();
  396. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  397. /* mmio */
  398. if (is_error_pfn(pfn))
  399. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  400. spin_lock(&vcpu->kvm->mmu_lock);
  401. if (mmu_notifier_retry(vcpu, mmu_seq))
  402. goto out_unlock;
  403. kvm_mmu_free_some_pages(vcpu);
  404. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  405. level, &write_pt, pfn);
  406. (void)sptep;
  407. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  408. sptep, *sptep, write_pt);
  409. if (!write_pt)
  410. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  411. ++vcpu->stat.pf_fixed;
  412. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  413. spin_unlock(&vcpu->kvm->mmu_lock);
  414. return write_pt;
  415. out_unlock:
  416. spin_unlock(&vcpu->kvm->mmu_lock);
  417. kvm_release_pfn_clean(pfn);
  418. return 0;
  419. }
  420. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  421. {
  422. struct kvm_shadow_walk_iterator iterator;
  423. struct kvm_mmu_page *sp;
  424. gpa_t pte_gpa = -1;
  425. int level;
  426. u64 *sptep;
  427. int need_flush = 0;
  428. spin_lock(&vcpu->kvm->mmu_lock);
  429. for_each_shadow_entry(vcpu, gva, iterator) {
  430. level = iterator.level;
  431. sptep = iterator.sptep;
  432. sp = page_header(__pa(sptep));
  433. if (is_last_spte(*sptep, level)) {
  434. int offset, shift;
  435. if (!sp->unsync)
  436. break;
  437. shift = PAGE_SHIFT -
  438. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  439. offset = sp->role.quadrant << shift;
  440. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  441. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  442. if (is_shadow_present_pte(*sptep)) {
  443. if (is_large_pte(*sptep))
  444. --vcpu->kvm->stat.lpages;
  445. drop_spte(vcpu->kvm, sptep,
  446. shadow_trap_nonpresent_pte);
  447. need_flush = 1;
  448. } else
  449. __set_spte(sptep, shadow_trap_nonpresent_pte);
  450. break;
  451. }
  452. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  453. break;
  454. }
  455. if (need_flush)
  456. kvm_flush_remote_tlbs(vcpu->kvm);
  457. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  458. spin_unlock(&vcpu->kvm->mmu_lock);
  459. if (pte_gpa == -1)
  460. return;
  461. if (mmu_topup_memory_caches(vcpu))
  462. return;
  463. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  464. }
  465. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  466. u32 *error)
  467. {
  468. struct guest_walker walker;
  469. gpa_t gpa = UNMAPPED_GVA;
  470. int r;
  471. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  472. !!(access & PFERR_WRITE_MASK),
  473. !!(access & PFERR_USER_MASK),
  474. !!(access & PFERR_FETCH_MASK));
  475. if (r) {
  476. gpa = gfn_to_gpa(walker.gfn);
  477. gpa |= vaddr & ~PAGE_MASK;
  478. } else if (error)
  479. *error = walker.error_code;
  480. return gpa;
  481. }
  482. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  483. struct kvm_mmu_page *sp)
  484. {
  485. int i, j, offset, r;
  486. pt_element_t pt[256 / sizeof(pt_element_t)];
  487. gpa_t pte_gpa;
  488. if (sp->role.direct
  489. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  490. nonpaging_prefetch_page(vcpu, sp);
  491. return;
  492. }
  493. pte_gpa = gfn_to_gpa(sp->gfn);
  494. if (PTTYPE == 32) {
  495. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  496. pte_gpa += offset * sizeof(pt_element_t);
  497. }
  498. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  499. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  500. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  501. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  502. if (r || is_present_gpte(pt[j]))
  503. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  504. else
  505. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  506. }
  507. }
  508. /*
  509. * Using the cached information from sp->gfns is safe because:
  510. * - The spte has a reference to the struct page, so the pfn for a given gfn
  511. * can't change unless all sptes pointing to it are nuked first.
  512. */
  513. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  514. bool clear_unsync)
  515. {
  516. int i, offset, nr_present;
  517. bool reset_host_protection;
  518. gpa_t first_pte_gpa;
  519. offset = nr_present = 0;
  520. /* direct kvm_mmu_page can not be unsync. */
  521. BUG_ON(sp->role.direct);
  522. if (PTTYPE == 32)
  523. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  524. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  525. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  526. unsigned pte_access;
  527. pt_element_t gpte;
  528. gpa_t pte_gpa;
  529. gfn_t gfn;
  530. if (!is_shadow_present_pte(sp->spt[i]))
  531. continue;
  532. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  533. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  534. sizeof(pt_element_t)))
  535. return -EINVAL;
  536. gfn = gpte_to_gfn(gpte);
  537. if (is_rsvd_bits_set(vcpu, gpte, PT_PAGE_TABLE_LEVEL)
  538. || gfn != sp->gfns[i] || !is_present_gpte(gpte)
  539. || !(gpte & PT_ACCESSED_MASK)) {
  540. u64 nonpresent;
  541. if (is_present_gpte(gpte) || !clear_unsync)
  542. nonpresent = shadow_trap_nonpresent_pte;
  543. else
  544. nonpresent = shadow_notrap_nonpresent_pte;
  545. drop_spte(vcpu->kvm, &sp->spt[i], nonpresent);
  546. continue;
  547. }
  548. nr_present++;
  549. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  550. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  551. pte_access &= ~ACC_WRITE_MASK;
  552. reset_host_protection = 0;
  553. } else {
  554. reset_host_protection = 1;
  555. }
  556. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  557. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  558. spte_to_pfn(sp->spt[i]), true, false,
  559. reset_host_protection);
  560. }
  561. return !nr_present;
  562. }
  563. #undef pt_element_t
  564. #undef guest_walker
  565. #undef FNAME
  566. #undef PT_BASE_ADDR_MASK
  567. #undef PT_INDEX
  568. #undef PT_LEVEL_MASK
  569. #undef PT_LVL_ADDR_MASK
  570. #undef PT_LVL_OFFSET_MASK
  571. #undef PT_LEVEL_BITS
  572. #undef PT_MAX_FULL_LEVELS
  573. #undef gpte_to_gfn
  574. #undef gpte_to_gfn_lvl
  575. #undef CMPXCHG