paging_tmpl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
  156. index, pte, pte|PT_ACCESSED_MASK))
  157. goto walk;
  158. mark_page_dirty(vcpu->kvm, table_gfn);
  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. ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
  187. pte|PT_DIRTY_MASK);
  188. if (ret)
  189. goto walk;
  190. mark_page_dirty(vcpu->kvm, table_gfn);
  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, pte_access, pt_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. access &= gw->pte_access;
  300. } else {
  301. direct = 0;
  302. table_gfn = gw->table_gfn[level - 2];
  303. }
  304. shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
  305. direct, access, sptep);
  306. if (!direct) {
  307. r = kvm_read_guest_atomic(vcpu->kvm,
  308. gw->pte_gpa[level - 2],
  309. &curr_pte, sizeof(curr_pte));
  310. if (r || curr_pte != gw->ptes[level - 2]) {
  311. kvm_mmu_put_page(shadow_page, sptep);
  312. kvm_release_pfn_clean(pfn);
  313. sptep = NULL;
  314. break;
  315. }
  316. }
  317. spte = __pa(shadow_page->spt)
  318. | PT_PRESENT_MASK | PT_ACCESSED_MASK
  319. | PT_WRITABLE_MASK | PT_USER_MASK;
  320. *sptep = spte;
  321. }
  322. return sptep;
  323. }
  324. /*
  325. * Page fault handler. There are several causes for a page fault:
  326. * - there is no shadow pte for the guest pte
  327. * - write access through a shadow pte marked read only so that we can set
  328. * the dirty bit
  329. * - write access to a shadow pte marked read only so we can update the page
  330. * dirty bitmap, when userspace requests it
  331. * - mmio access; in this case we will never install a present shadow pte
  332. * - normal guest page fault due to the guest pte marked not present, not
  333. * writable, or not executable
  334. *
  335. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  336. * a negative value on error.
  337. */
  338. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  339. u32 error_code)
  340. {
  341. int write_fault = error_code & PFERR_WRITE_MASK;
  342. int user_fault = error_code & PFERR_USER_MASK;
  343. int fetch_fault = error_code & PFERR_FETCH_MASK;
  344. struct guest_walker walker;
  345. u64 *sptep;
  346. int write_pt = 0;
  347. int r;
  348. pfn_t pfn;
  349. int level = PT_PAGE_TABLE_LEVEL;
  350. unsigned long mmu_seq;
  351. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  352. kvm_mmu_audit(vcpu, "pre page fault");
  353. r = mmu_topup_memory_caches(vcpu);
  354. if (r)
  355. return r;
  356. /*
  357. * Look up the guest pte for the faulting address.
  358. */
  359. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  360. fetch_fault);
  361. /*
  362. * The page is not mapped by the guest. Let the guest handle it.
  363. */
  364. if (!r) {
  365. pgprintk("%s: guest page fault\n", __func__);
  366. inject_page_fault(vcpu, addr, walker.error_code);
  367. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  368. return 0;
  369. }
  370. if (walker.level >= PT_DIRECTORY_LEVEL) {
  371. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  372. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  373. }
  374. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  375. smp_rmb();
  376. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  377. /* mmio */
  378. if (is_error_pfn(pfn))
  379. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  380. spin_lock(&vcpu->kvm->mmu_lock);
  381. if (mmu_notifier_retry(vcpu, mmu_seq))
  382. goto out_unlock;
  383. kvm_mmu_free_some_pages(vcpu);
  384. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  385. level, &write_pt, pfn);
  386. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  387. sptep, *sptep, write_pt);
  388. if (!write_pt)
  389. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  390. ++vcpu->stat.pf_fixed;
  391. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  392. spin_unlock(&vcpu->kvm->mmu_lock);
  393. return write_pt;
  394. out_unlock:
  395. spin_unlock(&vcpu->kvm->mmu_lock);
  396. kvm_release_pfn_clean(pfn);
  397. return 0;
  398. }
  399. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  400. {
  401. struct kvm_shadow_walk_iterator iterator;
  402. gpa_t pte_gpa = -1;
  403. int level;
  404. u64 *sptep;
  405. int need_flush = 0;
  406. spin_lock(&vcpu->kvm->mmu_lock);
  407. for_each_shadow_entry(vcpu, gva, iterator) {
  408. level = iterator.level;
  409. sptep = iterator.sptep;
  410. if (is_last_spte(*sptep, level)) {
  411. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  412. int offset, shift;
  413. shift = PAGE_SHIFT -
  414. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  415. offset = sp->role.quadrant << shift;
  416. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  417. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  418. if (is_shadow_present_pte(*sptep)) {
  419. rmap_remove(vcpu->kvm, sptep);
  420. if (is_large_pte(*sptep))
  421. --vcpu->kvm->stat.lpages;
  422. need_flush = 1;
  423. }
  424. __set_spte(sptep, shadow_trap_nonpresent_pte);
  425. break;
  426. }
  427. if (!is_shadow_present_pte(*sptep))
  428. break;
  429. }
  430. if (need_flush)
  431. kvm_flush_remote_tlbs(vcpu->kvm);
  432. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  433. spin_unlock(&vcpu->kvm->mmu_lock);
  434. if (pte_gpa == -1)
  435. return;
  436. if (mmu_topup_memory_caches(vcpu))
  437. return;
  438. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  439. }
  440. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  441. u32 *error)
  442. {
  443. struct guest_walker walker;
  444. gpa_t gpa = UNMAPPED_GVA;
  445. int r;
  446. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  447. !!(access & PFERR_WRITE_MASK),
  448. !!(access & PFERR_USER_MASK),
  449. !!(access & PFERR_FETCH_MASK));
  450. if (r) {
  451. gpa = gfn_to_gpa(walker.gfn);
  452. gpa |= vaddr & ~PAGE_MASK;
  453. } else if (error)
  454. *error = walker.error_code;
  455. return gpa;
  456. }
  457. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  458. struct kvm_mmu_page *sp)
  459. {
  460. int i, j, offset, r;
  461. pt_element_t pt[256 / sizeof(pt_element_t)];
  462. gpa_t pte_gpa;
  463. if (sp->role.direct
  464. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  465. nonpaging_prefetch_page(vcpu, sp);
  466. return;
  467. }
  468. pte_gpa = gfn_to_gpa(sp->gfn);
  469. if (PTTYPE == 32) {
  470. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  471. pte_gpa += offset * sizeof(pt_element_t);
  472. }
  473. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  474. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  475. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  476. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  477. if (r || is_present_gpte(pt[j]))
  478. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  479. else
  480. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  481. }
  482. }
  483. /*
  484. * Using the cached information from sp->gfns is safe because:
  485. * - The spte has a reference to the struct page, so the pfn for a given gfn
  486. * can't change unless all sptes pointing to it are nuked first.
  487. * - Alias changes zap the entire shadow cache.
  488. */
  489. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  490. {
  491. int i, offset, nr_present;
  492. bool reset_host_protection;
  493. gpa_t first_pte_gpa;
  494. offset = nr_present = 0;
  495. if (PTTYPE == 32)
  496. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  497. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  498. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  499. unsigned pte_access;
  500. pt_element_t gpte;
  501. gpa_t pte_gpa;
  502. gfn_t gfn;
  503. if (!is_shadow_present_pte(sp->spt[i]))
  504. continue;
  505. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  506. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  507. sizeof(pt_element_t)))
  508. return -EINVAL;
  509. gfn = gpte_to_gfn(gpte);
  510. if (unalias_gfn(vcpu->kvm, gfn) != sp->gfns[i] ||
  511. !is_present_gpte(gpte) || !(gpte & PT_ACCESSED_MASK)) {
  512. u64 nonpresent;
  513. rmap_remove(vcpu->kvm, &sp->spt[i]);
  514. if (is_present_gpte(gpte))
  515. nonpresent = shadow_trap_nonpresent_pte;
  516. else
  517. nonpresent = shadow_notrap_nonpresent_pte;
  518. __set_spte(&sp->spt[i], nonpresent);
  519. continue;
  520. }
  521. nr_present++;
  522. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  523. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  524. pte_access &= ~ACC_WRITE_MASK;
  525. reset_host_protection = 0;
  526. } else {
  527. reset_host_protection = 1;
  528. }
  529. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  530. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  531. spte_to_pfn(sp->spt[i]), true, false,
  532. reset_host_protection);
  533. }
  534. return !nr_present;
  535. }
  536. #undef pt_element_t
  537. #undef guest_walker
  538. #undef FNAME
  539. #undef PT_BASE_ADDR_MASK
  540. #undef PT_INDEX
  541. #undef PT_LEVEL_MASK
  542. #undef PT_LVL_ADDR_MASK
  543. #undef PT_LVL_OFFSET_MASK
  544. #undef PT_LEVEL_BITS
  545. #undef PT_MAX_FULL_LEVELS
  546. #undef gpte_to_gfn
  547. #undef gpte_to_gfn_lvl
  548. #undef CMPXCHG