paging_tmpl.h 21 KB

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