paging_tmpl.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
  67. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  68. unsigned pt_access;
  69. unsigned pte_access;
  70. gfn_t gfn;
  71. u32 error_code;
  72. };
  73. static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
  74. {
  75. return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
  76. }
  77. static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
  78. gfn_t table_gfn, unsigned index,
  79. pt_element_t orig_pte, pt_element_t new_pte)
  80. {
  81. pt_element_t ret;
  82. pt_element_t *table;
  83. struct page *page;
  84. page = gfn_to_page(kvm, table_gfn);
  85. table = kmap_atomic(page, KM_USER0);
  86. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  87. kunmap_atomic(table, KM_USER0);
  88. kvm_release_page_dirty(page);
  89. return (ret != orig_pte);
  90. }
  91. static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
  92. {
  93. unsigned access;
  94. access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
  95. #if PTTYPE == 64
  96. if (is_nx(vcpu))
  97. access &= ~(gpte >> PT64_NX_SHIFT);
  98. #endif
  99. return access;
  100. }
  101. /*
  102. * Fetch a guest pte for a guest virtual address
  103. */
  104. static int FNAME(walk_addr_generic)(struct guest_walker *walker,
  105. struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  106. gva_t addr, int write_fault,
  107. int user_fault, int fetch_fault)
  108. {
  109. pt_element_t pte;
  110. gfn_t table_gfn;
  111. unsigned index, pt_access, uninitialized_var(pte_access);
  112. gpa_t pte_gpa;
  113. bool eperm, present, rsvd_fault;
  114. trace_kvm_mmu_pagetable_walk(addr, write_fault, user_fault,
  115. fetch_fault);
  116. walk:
  117. present = true;
  118. eperm = rsvd_fault = false;
  119. walker->level = mmu->root_level;
  120. pte = mmu->get_cr3(vcpu);
  121. #if PTTYPE == 64
  122. if (walker->level == PT32E_ROOT_LEVEL) {
  123. pte = kvm_pdptr_read(vcpu, (addr >> 30) & 3);
  124. trace_kvm_mmu_paging_element(pte, walker->level);
  125. if (!is_present_gpte(pte)) {
  126. present = false;
  127. goto error;
  128. }
  129. --walker->level;
  130. }
  131. #endif
  132. ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
  133. (mmu->get_cr3(vcpu) & CR3_NONPAE_RESERVED_BITS) == 0);
  134. pt_access = ACC_ALL;
  135. for (;;) {
  136. index = PT_INDEX(addr, walker->level);
  137. table_gfn = gpte_to_gfn(pte);
  138. pte_gpa = gfn_to_gpa(table_gfn);
  139. pte_gpa += index * sizeof(pt_element_t);
  140. walker->table_gfn[walker->level - 1] = table_gfn;
  141. walker->pte_gpa[walker->level - 1] = pte_gpa;
  142. if (kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte))) {
  143. present = false;
  144. break;
  145. }
  146. trace_kvm_mmu_paging_element(pte, walker->level);
  147. if (!is_present_gpte(pte)) {
  148. present = false;
  149. break;
  150. }
  151. if (is_rsvd_bits_set(&vcpu->arch.mmu, pte, walker->level)) {
  152. rsvd_fault = true;
  153. break;
  154. }
  155. if (write_fault && !is_writable_pte(pte))
  156. if (user_fault || is_write_protection(vcpu))
  157. eperm = true;
  158. if (user_fault && !(pte & PT_USER_MASK))
  159. eperm = true;
  160. #if PTTYPE == 64
  161. if (fetch_fault && (pte & PT64_NX_MASK))
  162. eperm = true;
  163. #endif
  164. if (!eperm && !rsvd_fault && !(pte & PT_ACCESSED_MASK)) {
  165. trace_kvm_mmu_set_accessed_bit(table_gfn, index,
  166. sizeof(pte));
  167. if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
  168. index, pte, pte|PT_ACCESSED_MASK))
  169. goto walk;
  170. mark_page_dirty(vcpu->kvm, table_gfn);
  171. pte |= PT_ACCESSED_MASK;
  172. }
  173. pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
  174. walker->ptes[walker->level - 1] = pte;
  175. if ((walker->level == PT_PAGE_TABLE_LEVEL) ||
  176. ((walker->level == PT_DIRECTORY_LEVEL) &&
  177. is_large_pte(pte) &&
  178. (PTTYPE == 64 || is_pse(vcpu))) ||
  179. ((walker->level == PT_PDPE_LEVEL) &&
  180. is_large_pte(pte) &&
  181. mmu->root_level == PT64_ROOT_LEVEL)) {
  182. int lvl = walker->level;
  183. walker->gfn = gpte_to_gfn_lvl(pte, lvl);
  184. walker->gfn += (addr & PT_LVL_OFFSET_MASK(lvl))
  185. >> PAGE_SHIFT;
  186. if (PTTYPE == 32 &&
  187. walker->level == PT_DIRECTORY_LEVEL &&
  188. is_cpuid_PSE36())
  189. walker->gfn += pse36_gfn_delta(pte);
  190. break;
  191. }
  192. pt_access = pte_access;
  193. --walker->level;
  194. }
  195. if (!present || eperm || rsvd_fault)
  196. goto error;
  197. if (write_fault && !is_dirty_gpte(pte)) {
  198. bool ret;
  199. trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
  200. ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
  201. pte|PT_DIRTY_MASK);
  202. if (ret)
  203. goto walk;
  204. mark_page_dirty(vcpu->kvm, table_gfn);
  205. pte |= PT_DIRTY_MASK;
  206. walker->ptes[walker->level - 1] = pte;
  207. }
  208. walker->pt_access = pt_access;
  209. walker->pte_access = pte_access;
  210. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  211. __func__, (u64)pte, pte_access, pt_access);
  212. return 1;
  213. error:
  214. walker->error_code = 0;
  215. if (present)
  216. walker->error_code |= PFERR_PRESENT_MASK;
  217. if (write_fault)
  218. walker->error_code |= PFERR_WRITE_MASK;
  219. if (user_fault)
  220. walker->error_code |= PFERR_USER_MASK;
  221. if (fetch_fault && is_nx(vcpu))
  222. walker->error_code |= PFERR_FETCH_MASK;
  223. if (rsvd_fault)
  224. walker->error_code |= PFERR_RSVD_MASK;
  225. vcpu->arch.fault.address = addr;
  226. vcpu->arch.fault.error_code = walker->error_code;
  227. trace_kvm_mmu_walker_error(walker->error_code);
  228. return 0;
  229. }
  230. static int FNAME(walk_addr)(struct guest_walker *walker,
  231. struct kvm_vcpu *vcpu, gva_t addr,
  232. int write_fault, int user_fault, int fetch_fault)
  233. {
  234. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr,
  235. write_fault, user_fault, fetch_fault);
  236. }
  237. static int FNAME(walk_addr_nested)(struct guest_walker *walker,
  238. struct kvm_vcpu *vcpu, gva_t addr,
  239. int write_fault, int user_fault,
  240. int fetch_fault)
  241. {
  242. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
  243. addr, write_fault, user_fault,
  244. fetch_fault);
  245. }
  246. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  247. u64 *spte, const void *pte)
  248. {
  249. pt_element_t gpte;
  250. unsigned pte_access;
  251. pfn_t pfn;
  252. u64 new_spte;
  253. gpte = *(const pt_element_t *)pte;
  254. if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
  255. if (!is_present_gpte(gpte)) {
  256. if (sp->unsync)
  257. new_spte = shadow_trap_nonpresent_pte;
  258. else
  259. new_spte = shadow_notrap_nonpresent_pte;
  260. __set_spte(spte, new_spte);
  261. }
  262. return;
  263. }
  264. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  265. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  266. if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
  267. return;
  268. pfn = vcpu->arch.update_pte.pfn;
  269. if (is_error_pfn(pfn))
  270. return;
  271. if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq))
  272. return;
  273. kvm_get_pfn(pfn);
  274. /*
  275. * we call mmu_set_spte() with reset_host_protection = true beacuse that
  276. * vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
  277. */
  278. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  279. is_dirty_gpte(gpte), NULL, PT_PAGE_TABLE_LEVEL,
  280. gpte_to_gfn(gpte), pfn, true, true);
  281. }
  282. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  283. struct guest_walker *gw, int level)
  284. {
  285. pt_element_t curr_pte;
  286. gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
  287. u64 mask;
  288. int r, index;
  289. if (level == PT_PAGE_TABLE_LEVEL) {
  290. mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
  291. base_gpa = pte_gpa & ~mask;
  292. index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
  293. r = kvm_read_guest_atomic(vcpu->kvm, base_gpa,
  294. gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
  295. curr_pte = gw->prefetch_ptes[index];
  296. } else
  297. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa,
  298. &curr_pte, sizeof(curr_pte));
  299. return r || curr_pte != gw->ptes[level - 1];
  300. }
  301. static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
  302. u64 *sptep)
  303. {
  304. struct kvm_mmu_page *sp;
  305. struct kvm_mmu *mmu = &vcpu->arch.mmu;
  306. pt_element_t *gptep = gw->prefetch_ptes;
  307. u64 *spte;
  308. int i;
  309. sp = page_header(__pa(sptep));
  310. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  311. return;
  312. if (sp->role.direct)
  313. return __direct_pte_prefetch(vcpu, sp, sptep);
  314. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  315. spte = sp->spt + i;
  316. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  317. pt_element_t gpte;
  318. unsigned pte_access;
  319. gfn_t gfn;
  320. pfn_t pfn;
  321. bool dirty;
  322. if (spte == sptep)
  323. continue;
  324. if (*spte != shadow_trap_nonpresent_pte)
  325. continue;
  326. gpte = gptep[i];
  327. if (!is_present_gpte(gpte) ||
  328. is_rsvd_bits_set(mmu, gpte, PT_PAGE_TABLE_LEVEL)) {
  329. if (!sp->unsync)
  330. __set_spte(spte, shadow_notrap_nonpresent_pte);
  331. continue;
  332. }
  333. if (!(gpte & PT_ACCESSED_MASK))
  334. continue;
  335. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  336. gfn = gpte_to_gfn(gpte);
  337. dirty = is_dirty_gpte(gpte);
  338. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  339. (pte_access & ACC_WRITE_MASK) && dirty);
  340. if (is_error_pfn(pfn)) {
  341. kvm_release_pfn_clean(pfn);
  342. break;
  343. }
  344. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  345. dirty, NULL, PT_PAGE_TABLE_LEVEL, gfn,
  346. pfn, true, true);
  347. }
  348. }
  349. /*
  350. * Fetch a shadow pte for a specific level in the paging hierarchy.
  351. */
  352. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  353. struct guest_walker *gw,
  354. int user_fault, int write_fault, int hlevel,
  355. int *ptwrite, pfn_t pfn)
  356. {
  357. unsigned access = gw->pt_access;
  358. struct kvm_mmu_page *sp = NULL;
  359. bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
  360. int top_level;
  361. unsigned direct_access;
  362. struct kvm_shadow_walk_iterator it;
  363. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  364. return NULL;
  365. direct_access = gw->pt_access & gw->pte_access;
  366. if (!dirty)
  367. direct_access &= ~ACC_WRITE_MASK;
  368. top_level = vcpu->arch.mmu.root_level;
  369. if (top_level == PT32E_ROOT_LEVEL)
  370. top_level = PT32_ROOT_LEVEL;
  371. /*
  372. * Verify that the top-level gpte is still there. Since the page
  373. * is a root page, it is either write protected (and cannot be
  374. * changed from now on) or it is invalid (in which case, we don't
  375. * really care if it changes underneath us after this point).
  376. */
  377. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  378. goto out_gpte_changed;
  379. for (shadow_walk_init(&it, vcpu, addr);
  380. shadow_walk_okay(&it) && it.level > gw->level;
  381. shadow_walk_next(&it)) {
  382. gfn_t table_gfn;
  383. drop_large_spte(vcpu, it.sptep);
  384. sp = NULL;
  385. if (!is_shadow_present_pte(*it.sptep)) {
  386. table_gfn = gw->table_gfn[it.level - 2];
  387. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  388. false, access, it.sptep);
  389. }
  390. /*
  391. * Verify that the gpte in the page we've just write
  392. * protected is still there.
  393. */
  394. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  395. goto out_gpte_changed;
  396. if (sp)
  397. link_shadow_page(it.sptep, sp);
  398. }
  399. for (;
  400. shadow_walk_okay(&it) && it.level > hlevel;
  401. shadow_walk_next(&it)) {
  402. gfn_t direct_gfn;
  403. validate_direct_spte(vcpu, it.sptep, direct_access);
  404. drop_large_spte(vcpu, it.sptep);
  405. if (is_shadow_present_pte(*it.sptep))
  406. continue;
  407. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  408. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  409. true, direct_access, it.sptep);
  410. link_shadow_page(it.sptep, sp);
  411. }
  412. mmu_set_spte(vcpu, it.sptep, access, gw->pte_access & access,
  413. user_fault, write_fault, dirty, ptwrite, it.level,
  414. gw->gfn, pfn, false, true);
  415. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  416. return it.sptep;
  417. out_gpte_changed:
  418. if (sp)
  419. kvm_mmu_put_page(sp, it.sptep);
  420. kvm_release_pfn_clean(pfn);
  421. return NULL;
  422. }
  423. /*
  424. * Page fault handler. There are several causes for a page fault:
  425. * - there is no shadow pte for the guest pte
  426. * - write access through a shadow pte marked read only so that we can set
  427. * the dirty bit
  428. * - write access to a shadow pte marked read only so we can update the page
  429. * dirty bitmap, when userspace requests it
  430. * - mmio access; in this case we will never install a present shadow pte
  431. * - normal guest page fault due to the guest pte marked not present, not
  432. * writable, or not executable
  433. *
  434. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  435. * a negative value on error.
  436. */
  437. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  438. u32 error_code)
  439. {
  440. int write_fault = error_code & PFERR_WRITE_MASK;
  441. int user_fault = error_code & PFERR_USER_MASK;
  442. int fetch_fault = error_code & PFERR_FETCH_MASK;
  443. struct guest_walker walker;
  444. u64 *sptep;
  445. int write_pt = 0;
  446. int r;
  447. pfn_t pfn;
  448. int level = PT_PAGE_TABLE_LEVEL;
  449. unsigned long mmu_seq;
  450. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  451. r = mmu_topup_memory_caches(vcpu);
  452. if (r)
  453. return r;
  454. /*
  455. * Look up the guest pte for the faulting address.
  456. */
  457. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  458. fetch_fault);
  459. /*
  460. * The page is not mapped by the guest. Let the guest handle it.
  461. */
  462. if (!r) {
  463. pgprintk("%s: guest page fault\n", __func__);
  464. inject_page_fault(vcpu);
  465. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  466. return 0;
  467. }
  468. if (walker.level >= PT_DIRECTORY_LEVEL) {
  469. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  470. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  471. }
  472. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  473. smp_rmb();
  474. pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
  475. /* mmio */
  476. if (is_error_pfn(pfn))
  477. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  478. spin_lock(&vcpu->kvm->mmu_lock);
  479. if (mmu_notifier_retry(vcpu, mmu_seq))
  480. goto out_unlock;
  481. trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  482. kvm_mmu_free_some_pages(vcpu);
  483. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  484. level, &write_pt, pfn);
  485. (void)sptep;
  486. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  487. sptep, *sptep, write_pt);
  488. if (!write_pt)
  489. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  490. ++vcpu->stat.pf_fixed;
  491. trace_kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  492. spin_unlock(&vcpu->kvm->mmu_lock);
  493. return write_pt;
  494. out_unlock:
  495. spin_unlock(&vcpu->kvm->mmu_lock);
  496. kvm_release_pfn_clean(pfn);
  497. return 0;
  498. }
  499. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  500. {
  501. struct kvm_shadow_walk_iterator iterator;
  502. struct kvm_mmu_page *sp;
  503. gpa_t pte_gpa = -1;
  504. int level;
  505. u64 *sptep;
  506. int need_flush = 0;
  507. spin_lock(&vcpu->kvm->mmu_lock);
  508. for_each_shadow_entry(vcpu, gva, iterator) {
  509. level = iterator.level;
  510. sptep = iterator.sptep;
  511. sp = page_header(__pa(sptep));
  512. if (is_last_spte(*sptep, level)) {
  513. int offset, shift;
  514. if (!sp->unsync)
  515. break;
  516. shift = PAGE_SHIFT -
  517. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  518. offset = sp->role.quadrant << shift;
  519. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  520. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  521. if (is_shadow_present_pte(*sptep)) {
  522. if (is_large_pte(*sptep))
  523. --vcpu->kvm->stat.lpages;
  524. drop_spte(vcpu->kvm, sptep,
  525. shadow_trap_nonpresent_pte);
  526. need_flush = 1;
  527. } else
  528. __set_spte(sptep, shadow_trap_nonpresent_pte);
  529. break;
  530. }
  531. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  532. break;
  533. }
  534. if (need_flush)
  535. kvm_flush_remote_tlbs(vcpu->kvm);
  536. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  537. spin_unlock(&vcpu->kvm->mmu_lock);
  538. if (pte_gpa == -1)
  539. return;
  540. if (mmu_topup_memory_caches(vcpu))
  541. return;
  542. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  543. }
  544. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  545. u32 *error)
  546. {
  547. struct guest_walker walker;
  548. gpa_t gpa = UNMAPPED_GVA;
  549. int r;
  550. r = FNAME(walk_addr)(&walker, vcpu, vaddr,
  551. !!(access & PFERR_WRITE_MASK),
  552. !!(access & PFERR_USER_MASK),
  553. !!(access & PFERR_FETCH_MASK));
  554. if (r) {
  555. gpa = gfn_to_gpa(walker.gfn);
  556. gpa |= vaddr & ~PAGE_MASK;
  557. } else if (error)
  558. *error = walker.error_code;
  559. return gpa;
  560. }
  561. static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
  562. u32 access, u32 *error)
  563. {
  564. struct guest_walker walker;
  565. gpa_t gpa = UNMAPPED_GVA;
  566. int r;
  567. r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr,
  568. access & PFERR_WRITE_MASK,
  569. access & PFERR_USER_MASK,
  570. access & PFERR_FETCH_MASK);
  571. if (r) {
  572. gpa = gfn_to_gpa(walker.gfn);
  573. gpa |= vaddr & ~PAGE_MASK;
  574. } else if (error)
  575. *error = walker.error_code;
  576. return gpa;
  577. }
  578. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  579. struct kvm_mmu_page *sp)
  580. {
  581. int i, j, offset, r;
  582. pt_element_t pt[256 / sizeof(pt_element_t)];
  583. gpa_t pte_gpa;
  584. if (sp->role.direct
  585. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  586. nonpaging_prefetch_page(vcpu, sp);
  587. return;
  588. }
  589. pte_gpa = gfn_to_gpa(sp->gfn);
  590. if (PTTYPE == 32) {
  591. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  592. pte_gpa += offset * sizeof(pt_element_t);
  593. }
  594. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  595. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  596. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  597. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  598. if (r || is_present_gpte(pt[j]))
  599. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  600. else
  601. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  602. }
  603. }
  604. /*
  605. * Using the cached information from sp->gfns is safe because:
  606. * - The spte has a reference to the struct page, so the pfn for a given gfn
  607. * can't change unless all sptes pointing to it are nuked first.
  608. */
  609. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  610. bool clear_unsync)
  611. {
  612. int i, offset, nr_present;
  613. bool reset_host_protection;
  614. gpa_t first_pte_gpa;
  615. offset = nr_present = 0;
  616. /* direct kvm_mmu_page can not be unsync. */
  617. BUG_ON(sp->role.direct);
  618. if (PTTYPE == 32)
  619. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  620. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  621. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  622. unsigned pte_access;
  623. pt_element_t gpte;
  624. gpa_t pte_gpa;
  625. gfn_t gfn;
  626. if (!is_shadow_present_pte(sp->spt[i]))
  627. continue;
  628. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  629. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  630. sizeof(pt_element_t)))
  631. return -EINVAL;
  632. gfn = gpte_to_gfn(gpte);
  633. if (is_rsvd_bits_set(&vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL)
  634. || gfn != sp->gfns[i] || !is_present_gpte(gpte)
  635. || !(gpte & PT_ACCESSED_MASK)) {
  636. u64 nonpresent;
  637. if (is_present_gpte(gpte) || !clear_unsync)
  638. nonpresent = shadow_trap_nonpresent_pte;
  639. else
  640. nonpresent = shadow_notrap_nonpresent_pte;
  641. drop_spte(vcpu->kvm, &sp->spt[i], nonpresent);
  642. continue;
  643. }
  644. nr_present++;
  645. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  646. if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
  647. pte_access &= ~ACC_WRITE_MASK;
  648. reset_host_protection = 0;
  649. } else {
  650. reset_host_protection = 1;
  651. }
  652. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  653. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  654. spte_to_pfn(sp->spt[i]), true, false,
  655. reset_host_protection);
  656. }
  657. return !nr_present;
  658. }
  659. #undef pt_element_t
  660. #undef guest_walker
  661. #undef FNAME
  662. #undef PT_BASE_ADDR_MASK
  663. #undef PT_INDEX
  664. #undef PT_LEVEL_MASK
  665. #undef PT_LVL_ADDR_MASK
  666. #undef PT_LVL_OFFSET_MASK
  667. #undef PT_LEVEL_BITS
  668. #undef PT_MAX_FULL_LEVELS
  669. #undef gpte_to_gfn
  670. #undef gpte_to_gfn_lvl
  671. #undef CMPXCHG