paging_tmpl.h 16 KB

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