paging_tmpl.h 22 KB

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