paging_tmpl.h 21 KB

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