paging_tmpl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 && is_nx(vcpu) && (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. (pte & PT_PAGE_SIZE_MASK) &&
  166. (PTTYPE == 64 || is_pse(vcpu))) ||
  167. ((walker->level == PT_PDPE_LEVEL) &&
  168. (pte & PT_PAGE_SIZE_MASK) &&
  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. gpte = *(const pt_element_t *)pte;
  223. if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
  224. if (!is_present_gpte(gpte))
  225. __set_spte(spte, shadow_notrap_nonpresent_pte);
  226. return;
  227. }
  228. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  229. pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte);
  230. if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
  231. return;
  232. pfn = vcpu->arch.update_pte.pfn;
  233. if (is_error_pfn(pfn))
  234. return;
  235. if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq))
  236. return;
  237. kvm_get_pfn(pfn);
  238. /*
  239. * we call mmu_set_spte() with reset_host_protection = true beacuse that
  240. * vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
  241. */
  242. mmu_set_spte(vcpu, spte, page->role.access, pte_access, 0, 0,
  243. gpte & PT_DIRTY_MASK, NULL, PT_PAGE_TABLE_LEVEL,
  244. gpte_to_gfn(gpte), pfn, true, true);
  245. }
  246. /*
  247. * Fetch a shadow pte for a specific level in the paging hierarchy.
  248. */
  249. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  250. struct guest_walker *gw,
  251. int user_fault, int write_fault, int hlevel,
  252. int *ptwrite, pfn_t pfn)
  253. {
  254. unsigned access = gw->pt_access;
  255. struct kvm_mmu_page *shadow_page;
  256. u64 spte, *sptep = NULL;
  257. int direct;
  258. gfn_t table_gfn;
  259. int r;
  260. int level;
  261. pt_element_t curr_pte;
  262. struct kvm_shadow_walk_iterator iterator;
  263. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  264. return NULL;
  265. for_each_shadow_entry(vcpu, addr, iterator) {
  266. level = iterator.level;
  267. sptep = iterator.sptep;
  268. if (iterator.level == hlevel) {
  269. mmu_set_spte(vcpu, sptep, access,
  270. gw->pte_access & access,
  271. user_fault, write_fault,
  272. gw->ptes[gw->level-1] & PT_DIRTY_MASK,
  273. ptwrite, level,
  274. gw->gfn, pfn, false, true);
  275. break;
  276. }
  277. if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep))
  278. continue;
  279. if (is_large_pte(*sptep)) {
  280. rmap_remove(vcpu->kvm, sptep);
  281. __set_spte(sptep, shadow_trap_nonpresent_pte);
  282. kvm_flush_remote_tlbs(vcpu->kvm);
  283. }
  284. if (level <= gw->level) {
  285. int delta = level - gw->level + 1;
  286. direct = 1;
  287. if (!is_dirty_gpte(gw->ptes[level - delta]))
  288. access &= ~ACC_WRITE_MASK;
  289. table_gfn = gpte_to_gfn(gw->ptes[level - delta]);
  290. /* advance table_gfn when emulating 1gb pages with 4k */
  291. if (delta == 0)
  292. table_gfn += PT_INDEX(addr, level);
  293. } else {
  294. direct = 0;
  295. table_gfn = gw->table_gfn[level - 2];
  296. }
  297. shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
  298. direct, access, sptep);
  299. if (!direct) {
  300. r = kvm_read_guest_atomic(vcpu->kvm,
  301. gw->pte_gpa[level - 2],
  302. &curr_pte, sizeof(curr_pte));
  303. if (r || curr_pte != gw->ptes[level - 2]) {
  304. kvm_mmu_put_page(shadow_page, sptep);
  305. kvm_release_pfn_clean(pfn);
  306. sptep = NULL;
  307. break;
  308. }
  309. }
  310. spte = __pa(shadow_page->spt)
  311. | PT_PRESENT_MASK | PT_ACCESSED_MASK
  312. | PT_WRITABLE_MASK | PT_USER_MASK;
  313. *sptep = spte;
  314. }
  315. return sptep;
  316. }
  317. /*
  318. * Page fault handler. There are several causes for a page fault:
  319. * - there is no shadow pte for the guest pte
  320. * - write access through a shadow pte marked read only so that we can set
  321. * the dirty bit
  322. * - write access to a shadow pte marked read only so we can update the page
  323. * dirty bitmap, when userspace requests it
  324. * - mmio access; in this case we will never install a present shadow pte
  325. * - normal guest page fault due to the guest pte marked not present, not
  326. * writable, or not executable
  327. *
  328. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  329. * a negative value on error.
  330. */
  331. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  332. u32 error_code)
  333. {
  334. int write_fault = error_code & PFERR_WRITE_MASK;
  335. int user_fault = error_code & PFERR_USER_MASK;
  336. int fetch_fault = error_code & PFERR_FETCH_MASK;
  337. struct guest_walker walker;
  338. u64 *sptep;
  339. int write_pt = 0;
  340. int r;
  341. pfn_t pfn;
  342. int level = PT_PAGE_TABLE_LEVEL;
  343. unsigned long mmu_seq;
  344. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  345. kvm_mmu_audit(vcpu, "pre page fault");
  346. r = mmu_topup_memory_caches(vcpu);
  347. if (r)
  348. return r;
  349. /*
  350. * Look up the guest pte for the faulting address.
  351. */
  352. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  353. fetch_fault);
  354. /*
  355. * The page is not mapped by the guest. Let the guest handle it.
  356. */
  357. if (!r) {
  358. pgprintk("%s: guest page fault\n", __func__);
  359. inject_page_fault(vcpu, addr, walker.error_code);
  360. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  361. return 0;
  362. }
  363. if (walker.level >= PT_DIRECTORY_LEVEL) {
  364. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  365. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  366. }
  367. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  368. smp_rmb();
  369. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  370. /* mmio */
  371. if (is_error_pfn(pfn)) {
  372. pgprintk("gfn %lx is mmio\n", walker.gfn);
  373. kvm_release_pfn_clean(pfn);
  374. return 1;
  375. }
  376. spin_lock(&vcpu->kvm->mmu_lock);
  377. if (mmu_notifier_retry(vcpu, mmu_seq))
  378. goto out_unlock;
  379. kvm_mmu_free_some_pages(vcpu);
  380. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  381. level, &write_pt, pfn);
  382. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  383. sptep, *sptep, write_pt);
  384. if (!write_pt)
  385. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  386. ++vcpu->stat.pf_fixed;
  387. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  388. spin_unlock(&vcpu->kvm->mmu_lock);
  389. return write_pt;
  390. out_unlock:
  391. spin_unlock(&vcpu->kvm->mmu_lock);
  392. kvm_release_pfn_clean(pfn);
  393. return 0;
  394. }
  395. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  396. {
  397. struct kvm_shadow_walk_iterator iterator;
  398. int level;
  399. u64 *sptep;
  400. int need_flush = 0;
  401. spin_lock(&vcpu->kvm->mmu_lock);
  402. for_each_shadow_entry(vcpu, gva, iterator) {
  403. level = iterator.level;
  404. sptep = iterator.sptep;
  405. if (level == PT_PAGE_TABLE_LEVEL ||
  406. ((level == PT_DIRECTORY_LEVEL && is_large_pte(*sptep))) ||
  407. ((level == PT_PDPE_LEVEL && is_large_pte(*sptep)))) {
  408. if (is_shadow_present_pte(*sptep)) {
  409. rmap_remove(vcpu->kvm, sptep);
  410. if (is_large_pte(*sptep))
  411. --vcpu->kvm->stat.lpages;
  412. need_flush = 1;
  413. }
  414. __set_spte(sptep, shadow_trap_nonpresent_pte);
  415. break;
  416. }
  417. if (!is_shadow_present_pte(*sptep))
  418. break;
  419. }
  420. if (need_flush)
  421. kvm_flush_remote_tlbs(vcpu->kvm);
  422. spin_unlock(&vcpu->kvm->mmu_lock);
  423. }
  424. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  425. u32 *error)
  426. {
  427. struct guest_walker walker;
  428. gpa_t gpa = UNMAPPED_GVA;
  429. int r;
  430. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  431. !!(access & PFERR_WRITE_MASK),
  432. !!(access & PFERR_USER_MASK),
  433. !!(access & PFERR_FETCH_MASK));
  434. if (r) {
  435. gpa = gfn_to_gpa(walker.gfn);
  436. gpa |= vaddr & ~PAGE_MASK;
  437. } else if (error)
  438. *error = walker.error_code;
  439. return gpa;
  440. }
  441. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  442. struct kvm_mmu_page *sp)
  443. {
  444. int i, j, offset, r;
  445. pt_element_t pt[256 / sizeof(pt_element_t)];
  446. gpa_t pte_gpa;
  447. if (sp->role.direct
  448. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  449. nonpaging_prefetch_page(vcpu, sp);
  450. return;
  451. }
  452. pte_gpa = gfn_to_gpa(sp->gfn);
  453. if (PTTYPE == 32) {
  454. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  455. pte_gpa += offset * sizeof(pt_element_t);
  456. }
  457. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  458. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  459. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  460. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  461. if (r || is_present_gpte(pt[j]))
  462. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  463. else
  464. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  465. }
  466. }
  467. /*
  468. * Using the cached information from sp->gfns is safe because:
  469. * - The spte has a reference to the struct page, so the pfn for a given gfn
  470. * can't change unless all sptes pointing to it are nuked first.
  471. * - Alias changes zap the entire shadow cache.
  472. */
  473. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  474. {
  475. int i, offset, nr_present;
  476. bool reset_host_protection;
  477. offset = nr_present = 0;
  478. if (PTTYPE == 32)
  479. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  480. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  481. unsigned pte_access;
  482. pt_element_t gpte;
  483. gpa_t pte_gpa;
  484. gfn_t gfn = sp->gfns[i];
  485. if (!is_shadow_present_pte(sp->spt[i]))
  486. continue;
  487. pte_gpa = gfn_to_gpa(sp->gfn);
  488. pte_gpa += (i+offset) * sizeof(pt_element_t);
  489. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  490. sizeof(pt_element_t)))
  491. return -EINVAL;
  492. if (gpte_to_gfn(gpte) != gfn || !is_present_gpte(gpte) ||
  493. !(gpte & PT_ACCESSED_MASK)) {
  494. u64 nonpresent;
  495. rmap_remove(vcpu->kvm, &sp->spt[i]);
  496. if (is_present_gpte(gpte))
  497. nonpresent = shadow_trap_nonpresent_pte;
  498. else
  499. nonpresent = shadow_notrap_nonpresent_pte;
  500. __set_spte(&sp->spt[i], nonpresent);
  501. continue;
  502. }
  503. nr_present++;
  504. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  505. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  506. pte_access &= ~ACC_WRITE_MASK;
  507. reset_host_protection = 0;
  508. } else {
  509. reset_host_protection = 1;
  510. }
  511. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  512. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  513. spte_to_pfn(sp->spt[i]), true, false,
  514. reset_host_protection);
  515. }
  516. return !nr_present;
  517. }
  518. #undef pt_element_t
  519. #undef guest_walker
  520. #undef FNAME
  521. #undef PT_BASE_ADDR_MASK
  522. #undef PT_INDEX
  523. #undef PT_LEVEL_MASK
  524. #undef PT_LVL_ADDR_MASK
  525. #undef PT_LVL_OFFSET_MASK
  526. #undef PT_LEVEL_BITS
  527. #undef PT_MAX_FULL_LEVELS
  528. #undef gpte_to_gfn
  529. #undef gpte_to_gfn_lvl
  530. #undef CMPXCHG