paging_tmpl.h 24 KB

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