paging_tmpl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. /*
  263. * Fetch a shadow pte for a specific level in the paging hierarchy.
  264. */
  265. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  266. struct guest_walker *gw,
  267. int user_fault, int write_fault, int hlevel,
  268. int *ptwrite, pfn_t pfn)
  269. {
  270. unsigned access = gw->pt_access;
  271. struct kvm_mmu_page *sp;
  272. u64 spte, *sptep = NULL;
  273. int direct;
  274. gfn_t table_gfn;
  275. int r;
  276. int level;
  277. bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
  278. unsigned direct_access;
  279. pt_element_t curr_pte;
  280. struct kvm_shadow_walk_iterator iterator;
  281. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  282. return NULL;
  283. direct_access = gw->pt_access & gw->pte_access;
  284. if (!dirty)
  285. direct_access &= ~ACC_WRITE_MASK;
  286. for_each_shadow_entry(vcpu, addr, iterator) {
  287. level = iterator.level;
  288. sptep = iterator.sptep;
  289. if (iterator.level == hlevel) {
  290. mmu_set_spte(vcpu, sptep, access,
  291. gw->pte_access & access,
  292. user_fault, write_fault,
  293. dirty, ptwrite, level,
  294. gw->gfn, pfn, false, true);
  295. break;
  296. }
  297. if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
  298. struct kvm_mmu_page *child;
  299. if (level != gw->level)
  300. continue;
  301. /*
  302. * For the direct sp, if the guest pte's dirty bit
  303. * changed form clean to dirty, it will corrupt the
  304. * sp's access: allow writable in the read-only sp,
  305. * so we should update the spte at this point to get
  306. * a new sp with the correct access.
  307. */
  308. child = page_header(*sptep & PT64_BASE_ADDR_MASK);
  309. if (child->role.access == direct_access)
  310. continue;
  311. mmu_page_remove_parent_pte(child, sptep);
  312. __set_spte(sptep, shadow_trap_nonpresent_pte);
  313. kvm_flush_remote_tlbs(vcpu->kvm);
  314. }
  315. if (is_large_pte(*sptep)) {
  316. drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
  317. kvm_flush_remote_tlbs(vcpu->kvm);
  318. }
  319. if (level <= gw->level) {
  320. direct = 1;
  321. access = direct_access;
  322. /*
  323. * It is a large guest pages backed by small host pages,
  324. * So we set @direct(@sp->role.direct)=1, and set
  325. * @table_gfn(@sp->gfn)=the base page frame for linear
  326. * translations.
  327. */
  328. table_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  329. access &= gw->pte_access;
  330. } else {
  331. direct = 0;
  332. table_gfn = gw->table_gfn[level - 2];
  333. }
  334. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
  335. direct, access, sptep);
  336. if (!direct) {
  337. r = kvm_read_guest_atomic(vcpu->kvm,
  338. gw->pte_gpa[level - 2],
  339. &curr_pte, sizeof(curr_pte));
  340. if (r || curr_pte != gw->ptes[level - 2]) {
  341. kvm_mmu_put_page(sp, sptep);
  342. kvm_release_pfn_clean(pfn);
  343. sptep = NULL;
  344. break;
  345. }
  346. }
  347. spte = __pa(sp->spt)
  348. | PT_PRESENT_MASK | PT_ACCESSED_MASK
  349. | PT_WRITABLE_MASK | PT_USER_MASK;
  350. *sptep = spte;
  351. }
  352. return sptep;
  353. }
  354. /*
  355. * Page fault handler. There are several causes for a page fault:
  356. * - there is no shadow pte for the guest pte
  357. * - write access through a shadow pte marked read only so that we can set
  358. * the dirty bit
  359. * - write access to a shadow pte marked read only so we can update the page
  360. * dirty bitmap, when userspace requests it
  361. * - mmio access; in this case we will never install a present shadow pte
  362. * - normal guest page fault due to the guest pte marked not present, not
  363. * writable, or not executable
  364. *
  365. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  366. * a negative value on error.
  367. */
  368. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  369. u32 error_code)
  370. {
  371. int write_fault = error_code & PFERR_WRITE_MASK;
  372. int user_fault = error_code & PFERR_USER_MASK;
  373. int fetch_fault = error_code & PFERR_FETCH_MASK;
  374. struct guest_walker walker;
  375. u64 *sptep;
  376. int write_pt = 0;
  377. int r;
  378. pfn_t pfn;
  379. int level = PT_PAGE_TABLE_LEVEL;
  380. unsigned long mmu_seq;
  381. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  382. kvm_mmu_audit(vcpu, "pre page fault");
  383. r = mmu_topup_memory_caches(vcpu);
  384. if (r)
  385. return r;
  386. /*
  387. * Look up the guest pte for the faulting address.
  388. */
  389. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  390. fetch_fault);
  391. /*
  392. * The page is not mapped by the guest. Let the guest handle it.
  393. */
  394. if (!r) {
  395. pgprintk("%s: guest page fault\n", __func__);
  396. inject_page_fault(vcpu, addr, walker.error_code);
  397. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  398. return 0;
  399. }
  400. if (walker.level >= PT_DIRECTORY_LEVEL) {
  401. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  402. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  403. }
  404. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  405. smp_rmb();
  406. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  407. /* mmio */
  408. if (is_error_pfn(pfn))
  409. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  410. spin_lock(&vcpu->kvm->mmu_lock);
  411. if (mmu_notifier_retry(vcpu, mmu_seq))
  412. goto out_unlock;
  413. kvm_mmu_free_some_pages(vcpu);
  414. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  415. level, &write_pt, pfn);
  416. (void)sptep;
  417. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  418. sptep, *sptep, write_pt);
  419. if (!write_pt)
  420. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  421. ++vcpu->stat.pf_fixed;
  422. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  423. spin_unlock(&vcpu->kvm->mmu_lock);
  424. return write_pt;
  425. out_unlock:
  426. spin_unlock(&vcpu->kvm->mmu_lock);
  427. kvm_release_pfn_clean(pfn);
  428. return 0;
  429. }
  430. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  431. {
  432. struct kvm_shadow_walk_iterator iterator;
  433. struct kvm_mmu_page *sp;
  434. gpa_t pte_gpa = -1;
  435. int level;
  436. u64 *sptep;
  437. int need_flush = 0;
  438. spin_lock(&vcpu->kvm->mmu_lock);
  439. for_each_shadow_entry(vcpu, gva, iterator) {
  440. level = iterator.level;
  441. sptep = iterator.sptep;
  442. sp = page_header(__pa(sptep));
  443. if (is_last_spte(*sptep, level)) {
  444. int offset, shift;
  445. if (!sp->unsync)
  446. break;
  447. shift = PAGE_SHIFT -
  448. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  449. offset = sp->role.quadrant << shift;
  450. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  451. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  452. if (is_shadow_present_pte(*sptep)) {
  453. if (is_large_pte(*sptep))
  454. --vcpu->kvm->stat.lpages;
  455. drop_spte(vcpu->kvm, sptep,
  456. shadow_trap_nonpresent_pte);
  457. need_flush = 1;
  458. } else
  459. __set_spte(sptep, shadow_trap_nonpresent_pte);
  460. break;
  461. }
  462. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  463. break;
  464. }
  465. if (need_flush)
  466. kvm_flush_remote_tlbs(vcpu->kvm);
  467. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  468. spin_unlock(&vcpu->kvm->mmu_lock);
  469. if (pte_gpa == -1)
  470. return;
  471. if (mmu_topup_memory_caches(vcpu))
  472. return;
  473. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  474. }
  475. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  476. u32 *error)
  477. {
  478. struct guest_walker walker;
  479. gpa_t gpa = UNMAPPED_GVA;
  480. int r;
  481. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  482. !!(access & PFERR_WRITE_MASK),
  483. !!(access & PFERR_USER_MASK),
  484. !!(access & PFERR_FETCH_MASK));
  485. if (r) {
  486. gpa = gfn_to_gpa(walker.gfn);
  487. gpa |= vaddr & ~PAGE_MASK;
  488. } else if (error)
  489. *error = walker.error_code;
  490. return gpa;
  491. }
  492. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  493. struct kvm_mmu_page *sp)
  494. {
  495. int i, j, offset, r;
  496. pt_element_t pt[256 / sizeof(pt_element_t)];
  497. gpa_t pte_gpa;
  498. if (sp->role.direct
  499. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  500. nonpaging_prefetch_page(vcpu, sp);
  501. return;
  502. }
  503. pte_gpa = gfn_to_gpa(sp->gfn);
  504. if (PTTYPE == 32) {
  505. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  506. pte_gpa += offset * sizeof(pt_element_t);
  507. }
  508. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  509. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  510. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  511. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  512. if (r || is_present_gpte(pt[j]))
  513. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  514. else
  515. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  516. }
  517. }
  518. /*
  519. * Using the cached information from sp->gfns is safe because:
  520. * - The spte has a reference to the struct page, so the pfn for a given gfn
  521. * can't change unless all sptes pointing to it are nuked first.
  522. */
  523. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  524. bool clear_unsync)
  525. {
  526. int i, offset, nr_present;
  527. bool reset_host_protection;
  528. gpa_t first_pte_gpa;
  529. offset = nr_present = 0;
  530. /* direct kvm_mmu_page can not be unsync. */
  531. BUG_ON(sp->role.direct);
  532. if (PTTYPE == 32)
  533. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  534. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  535. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  536. unsigned pte_access;
  537. pt_element_t gpte;
  538. gpa_t pte_gpa;
  539. gfn_t gfn;
  540. if (!is_shadow_present_pte(sp->spt[i]))
  541. continue;
  542. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  543. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  544. sizeof(pt_element_t)))
  545. return -EINVAL;
  546. gfn = gpte_to_gfn(gpte);
  547. if (gfn != sp->gfns[i] ||
  548. !is_present_gpte(gpte) || !(gpte & PT_ACCESSED_MASK)) {
  549. u64 nonpresent;
  550. if (is_present_gpte(gpte) || !clear_unsync)
  551. nonpresent = shadow_trap_nonpresent_pte;
  552. else
  553. nonpresent = shadow_notrap_nonpresent_pte;
  554. drop_spte(vcpu->kvm, &sp->spt[i], nonpresent);
  555. continue;
  556. }
  557. nr_present++;
  558. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  559. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  560. pte_access &= ~ACC_WRITE_MASK;
  561. reset_host_protection = 0;
  562. } else {
  563. reset_host_protection = 1;
  564. }
  565. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  566. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  567. spte_to_pfn(sp->spt[i]), true, false,
  568. reset_host_protection);
  569. }
  570. return !nr_present;
  571. }
  572. #undef pt_element_t
  573. #undef guest_walker
  574. #undef FNAME
  575. #undef PT_BASE_ADDR_MASK
  576. #undef PT_INDEX
  577. #undef PT_LEVEL_MASK
  578. #undef PT_LVL_ADDR_MASK
  579. #undef PT_LVL_OFFSET_MASK
  580. #undef PT_LEVEL_BITS
  581. #undef PT_MAX_FULL_LEVELS
  582. #undef gpte_to_gfn
  583. #undef gpte_to_gfn_lvl
  584. #undef CMPXCHG