paging_tmpl.h 17 KB

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