paging_tmpl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 *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. link_shadow_page(sptep, sp);
  348. }
  349. return sptep;
  350. }
  351. /*
  352. * Page fault handler. There are several causes for a page fault:
  353. * - there is no shadow pte for the guest pte
  354. * - write access through a shadow pte marked read only so that we can set
  355. * the dirty bit
  356. * - write access to a shadow pte marked read only so we can update the page
  357. * dirty bitmap, when userspace requests it
  358. * - mmio access; in this case we will never install a present shadow pte
  359. * - normal guest page fault due to the guest pte marked not present, not
  360. * writable, or not executable
  361. *
  362. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  363. * a negative value on error.
  364. */
  365. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  366. u32 error_code)
  367. {
  368. int write_fault = error_code & PFERR_WRITE_MASK;
  369. int user_fault = error_code & PFERR_USER_MASK;
  370. int fetch_fault = error_code & PFERR_FETCH_MASK;
  371. struct guest_walker walker;
  372. u64 *sptep;
  373. int write_pt = 0;
  374. int r;
  375. pfn_t pfn;
  376. int level = PT_PAGE_TABLE_LEVEL;
  377. unsigned long mmu_seq;
  378. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  379. kvm_mmu_audit(vcpu, "pre page fault");
  380. r = mmu_topup_memory_caches(vcpu);
  381. if (r)
  382. return r;
  383. /*
  384. * Look up the guest pte for the faulting address.
  385. */
  386. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  387. fetch_fault);
  388. /*
  389. * The page is not mapped by the guest. Let the guest handle it.
  390. */
  391. if (!r) {
  392. pgprintk("%s: guest page fault\n", __func__);
  393. inject_page_fault(vcpu, addr, walker.error_code);
  394. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  395. return 0;
  396. }
  397. if (walker.level >= PT_DIRECTORY_LEVEL) {
  398. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  399. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  400. }
  401. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  402. smp_rmb();
  403. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  404. /* mmio */
  405. if (is_error_pfn(pfn))
  406. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  407. spin_lock(&vcpu->kvm->mmu_lock);
  408. if (mmu_notifier_retry(vcpu, mmu_seq))
  409. goto out_unlock;
  410. kvm_mmu_free_some_pages(vcpu);
  411. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  412. level, &write_pt, pfn);
  413. (void)sptep;
  414. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  415. sptep, *sptep, write_pt);
  416. if (!write_pt)
  417. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  418. ++vcpu->stat.pf_fixed;
  419. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  420. spin_unlock(&vcpu->kvm->mmu_lock);
  421. return write_pt;
  422. out_unlock:
  423. spin_unlock(&vcpu->kvm->mmu_lock);
  424. kvm_release_pfn_clean(pfn);
  425. return 0;
  426. }
  427. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  428. {
  429. struct kvm_shadow_walk_iterator iterator;
  430. struct kvm_mmu_page *sp;
  431. gpa_t pte_gpa = -1;
  432. int level;
  433. u64 *sptep;
  434. int need_flush = 0;
  435. spin_lock(&vcpu->kvm->mmu_lock);
  436. for_each_shadow_entry(vcpu, gva, iterator) {
  437. level = iterator.level;
  438. sptep = iterator.sptep;
  439. sp = page_header(__pa(sptep));
  440. if (is_last_spte(*sptep, level)) {
  441. int offset, shift;
  442. if (!sp->unsync)
  443. break;
  444. shift = PAGE_SHIFT -
  445. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  446. offset = sp->role.quadrant << shift;
  447. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  448. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  449. if (is_shadow_present_pte(*sptep)) {
  450. if (is_large_pte(*sptep))
  451. --vcpu->kvm->stat.lpages;
  452. drop_spte(vcpu->kvm, sptep,
  453. shadow_trap_nonpresent_pte);
  454. need_flush = 1;
  455. } else
  456. __set_spte(sptep, shadow_trap_nonpresent_pte);
  457. break;
  458. }
  459. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  460. break;
  461. }
  462. if (need_flush)
  463. kvm_flush_remote_tlbs(vcpu->kvm);
  464. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  465. spin_unlock(&vcpu->kvm->mmu_lock);
  466. if (pte_gpa == -1)
  467. return;
  468. if (mmu_topup_memory_caches(vcpu))
  469. return;
  470. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  471. }
  472. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  473. u32 *error)
  474. {
  475. struct guest_walker walker;
  476. gpa_t gpa = UNMAPPED_GVA;
  477. int r;
  478. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  479. !!(access & PFERR_WRITE_MASK),
  480. !!(access & PFERR_USER_MASK),
  481. !!(access & PFERR_FETCH_MASK));
  482. if (r) {
  483. gpa = gfn_to_gpa(walker.gfn);
  484. gpa |= vaddr & ~PAGE_MASK;
  485. } else if (error)
  486. *error = walker.error_code;
  487. return gpa;
  488. }
  489. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  490. struct kvm_mmu_page *sp)
  491. {
  492. int i, j, offset, r;
  493. pt_element_t pt[256 / sizeof(pt_element_t)];
  494. gpa_t pte_gpa;
  495. if (sp->role.direct
  496. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  497. nonpaging_prefetch_page(vcpu, sp);
  498. return;
  499. }
  500. pte_gpa = gfn_to_gpa(sp->gfn);
  501. if (PTTYPE == 32) {
  502. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  503. pte_gpa += offset * sizeof(pt_element_t);
  504. }
  505. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  506. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  507. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  508. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  509. if (r || is_present_gpte(pt[j]))
  510. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  511. else
  512. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  513. }
  514. }
  515. /*
  516. * Using the cached information from sp->gfns is safe because:
  517. * - The spte has a reference to the struct page, so the pfn for a given gfn
  518. * can't change unless all sptes pointing to it are nuked first.
  519. */
  520. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  521. bool clear_unsync)
  522. {
  523. int i, offset, nr_present;
  524. bool reset_host_protection;
  525. gpa_t first_pte_gpa;
  526. offset = nr_present = 0;
  527. /* direct kvm_mmu_page can not be unsync. */
  528. BUG_ON(sp->role.direct);
  529. if (PTTYPE == 32)
  530. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  531. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  532. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  533. unsigned pte_access;
  534. pt_element_t gpte;
  535. gpa_t pte_gpa;
  536. gfn_t gfn;
  537. if (!is_shadow_present_pte(sp->spt[i]))
  538. continue;
  539. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  540. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  541. sizeof(pt_element_t)))
  542. return -EINVAL;
  543. gfn = gpte_to_gfn(gpte);
  544. if (gfn != sp->gfns[i] ||
  545. !is_present_gpte(gpte) || !(gpte & PT_ACCESSED_MASK)) {
  546. u64 nonpresent;
  547. if (is_present_gpte(gpte) || !clear_unsync)
  548. nonpresent = shadow_trap_nonpresent_pte;
  549. else
  550. nonpresent = shadow_notrap_nonpresent_pte;
  551. drop_spte(vcpu->kvm, &sp->spt[i], nonpresent);
  552. continue;
  553. }
  554. nr_present++;
  555. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  556. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  557. pte_access &= ~ACC_WRITE_MASK;
  558. reset_host_protection = 0;
  559. } else {
  560. reset_host_protection = 1;
  561. }
  562. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  563. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  564. spte_to_pfn(sp->spt[i]), true, false,
  565. reset_host_protection);
  566. }
  567. return !nr_present;
  568. }
  569. #undef pt_element_t
  570. #undef guest_walker
  571. #undef FNAME
  572. #undef PT_BASE_ADDR_MASK
  573. #undef PT_INDEX
  574. #undef PT_LEVEL_MASK
  575. #undef PT_LVL_ADDR_MASK
  576. #undef PT_LVL_OFFSET_MASK
  577. #undef PT_LEVEL_BITS
  578. #undef PT_MAX_FULL_LEVELS
  579. #undef gpte_to_gfn
  580. #undef gpte_to_gfn_lvl
  581. #undef CMPXCHG