paging_tmpl.h 27 KB

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