paging_tmpl.h 25 KB

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