paging_tmpl.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. accessed_dirty = PT_GUEST_ACCESSED_MASK;
  269. pt_access = pte_access = ACC_ALL;
  270. ++walker->level;
  271. do {
  272. gfn_t real_gfn;
  273. unsigned long host_addr;
  274. pt_access &= pte_access;
  275. --walker->level;
  276. index = PT_INDEX(addr, walker->level);
  277. table_gfn = gpte_to_gfn(pte);
  278. offset = index * sizeof(pt_element_t);
  279. pte_gpa = gfn_to_gpa(table_gfn) + offset;
  280. walker->table_gfn[walker->level - 1] = table_gfn;
  281. walker->pte_gpa[walker->level - 1] = pte_gpa;
  282. real_gfn = mmu->translate_gpa(vcpu, gfn_to_gpa(table_gfn),
  283. PFERR_USER_MASK|PFERR_WRITE_MASK,
  284. &walker->fault);
  285. /*
  286. * FIXME: This can happen if emulation (for of an INS/OUTS
  287. * instruction) triggers a nested page fault. The exit
  288. * qualification / exit info field will incorrectly have
  289. * "guest page access" as the nested page fault's cause,
  290. * instead of "guest page structure access". To fix this,
  291. * the x86_exception struct should be augmented with enough
  292. * information to fix the exit_qualification or exit_info_1
  293. * fields.
  294. */
  295. if (unlikely(real_gfn == UNMAPPED_GVA))
  296. return 0;
  297. real_gfn = gpa_to_gfn(real_gfn);
  298. host_addr = gfn_to_hva_prot(vcpu->kvm, real_gfn,
  299. &walker->pte_writable[walker->level - 1]);
  300. if (unlikely(kvm_is_error_hva(host_addr)))
  301. goto error;
  302. ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
  303. if (unlikely(__copy_from_user(&pte, ptep_user, sizeof(pte))))
  304. goto error;
  305. walker->ptep_user[walker->level - 1] = ptep_user;
  306. trace_kvm_mmu_paging_element(pte, walker->level);
  307. if (unlikely(!FNAME(is_present_gpte)(pte)))
  308. goto error;
  309. if (unlikely(FNAME(is_rsvd_bits_set)(mmu, pte,
  310. walker->level))) {
  311. errcode |= PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
  312. goto error;
  313. }
  314. accessed_dirty &= pte;
  315. pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
  316. walker->ptes[walker->level - 1] = pte;
  317. } while (!is_last_gpte(mmu, walker->level, pte));
  318. if (unlikely(permission_fault(vcpu, mmu, pte_access, access))) {
  319. errcode |= PFERR_PRESENT_MASK;
  320. goto error;
  321. }
  322. gfn = gpte_to_gfn_lvl(pte, walker->level);
  323. gfn += (addr & PT_LVL_OFFSET_MASK(walker->level)) >> PAGE_SHIFT;
  324. if (PTTYPE == 32 && walker->level == PT_DIRECTORY_LEVEL && is_cpuid_PSE36())
  325. gfn += pse36_gfn_delta(pte);
  326. real_gpa = mmu->translate_gpa(vcpu, gfn_to_gpa(gfn), access, &walker->fault);
  327. if (real_gpa == UNMAPPED_GVA)
  328. return 0;
  329. walker->gfn = real_gpa >> PAGE_SHIFT;
  330. if (!write_fault)
  331. FNAME(protect_clean_gpte)(&pte_access, pte);
  332. else
  333. /*
  334. * On a write fault, fold the dirty bit into accessed_dirty.
  335. * For modes without A/D bits support accessed_dirty will be
  336. * always clear.
  337. */
  338. accessed_dirty &= pte >>
  339. (PT_GUEST_DIRTY_SHIFT - PT_GUEST_ACCESSED_SHIFT);
  340. if (unlikely(!accessed_dirty)) {
  341. ret = FNAME(update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault);
  342. if (unlikely(ret < 0))
  343. goto error;
  344. else if (ret)
  345. goto retry_walk;
  346. }
  347. walker->pt_access = pt_access;
  348. walker->pte_access = pte_access;
  349. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  350. __func__, (u64)pte, pte_access, pt_access);
  351. return 1;
  352. error:
  353. errcode |= write_fault | user_fault;
  354. if (fetch_fault && (mmu->nx ||
  355. kvm_read_cr4_bits(vcpu, X86_CR4_SMEP)))
  356. errcode |= PFERR_FETCH_MASK;
  357. walker->fault.vector = PF_VECTOR;
  358. walker->fault.error_code_valid = true;
  359. walker->fault.error_code = errcode;
  360. #if PTTYPE == PTTYPE_EPT
  361. /*
  362. * Use PFERR_RSVD_MASK in error_code to to tell if EPT
  363. * misconfiguration requires to be injected. The detection is
  364. * done by is_rsvd_bits_set() above.
  365. *
  366. * We set up the value of exit_qualification to inject:
  367. * [2:0] - Derive from [2:0] of real exit_qualification at EPT violation
  368. * [5:3] - Calculated by the page walk of the guest EPT page tables
  369. * [7:8] - Derived from [7:8] of real exit_qualification
  370. *
  371. * The other bits are set to 0.
  372. */
  373. if (!(errcode & PFERR_RSVD_MASK)) {
  374. vcpu->arch.exit_qualification &= 0x187;
  375. vcpu->arch.exit_qualification |= ((pt_access & pte) & 0x7) << 3;
  376. }
  377. #endif
  378. walker->fault.address = addr;
  379. walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
  380. trace_kvm_mmu_walker_error(walker->fault.error_code);
  381. return 0;
  382. }
  383. static int FNAME(walk_addr)(struct guest_walker *walker,
  384. struct kvm_vcpu *vcpu, gva_t addr, u32 access)
  385. {
  386. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr,
  387. access);
  388. }
  389. #if PTTYPE != PTTYPE_EPT
  390. static int FNAME(walk_addr_nested)(struct guest_walker *walker,
  391. struct kvm_vcpu *vcpu, gva_t addr,
  392. u32 access)
  393. {
  394. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
  395. addr, access);
  396. }
  397. #endif
  398. static bool
  399. FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  400. u64 *spte, pt_element_t gpte, bool no_dirty_log)
  401. {
  402. unsigned pte_access;
  403. gfn_t gfn;
  404. pfn_t pfn;
  405. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
  406. return false;
  407. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  408. gfn = gpte_to_gfn(gpte);
  409. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  410. FNAME(protect_clean_gpte)(&pte_access, gpte);
  411. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  412. no_dirty_log && (pte_access & ACC_WRITE_MASK));
  413. if (is_error_pfn(pfn))
  414. return false;
  415. /*
  416. * we call mmu_set_spte() with host_writable = true because
  417. * pte_prefetch_gfn_to_pfn always gets a writable pfn.
  418. */
  419. mmu_set_spte(vcpu, spte, pte_access, 0, NULL, PT_PAGE_TABLE_LEVEL,
  420. gfn, pfn, true, true);
  421. return true;
  422. }
  423. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  424. u64 *spte, const void *pte)
  425. {
  426. pt_element_t gpte = *(const pt_element_t *)pte;
  427. FNAME(prefetch_gpte)(vcpu, sp, spte, gpte, false);
  428. }
  429. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  430. struct guest_walker *gw, int level)
  431. {
  432. pt_element_t curr_pte;
  433. gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
  434. u64 mask;
  435. int r, index;
  436. if (level == PT_PAGE_TABLE_LEVEL) {
  437. mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
  438. base_gpa = pte_gpa & ~mask;
  439. index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
  440. r = kvm_read_guest_atomic(vcpu->kvm, base_gpa,
  441. gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
  442. curr_pte = gw->prefetch_ptes[index];
  443. } else
  444. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa,
  445. &curr_pte, sizeof(curr_pte));
  446. return r || curr_pte != gw->ptes[level - 1];
  447. }
  448. static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
  449. u64 *sptep)
  450. {
  451. struct kvm_mmu_page *sp;
  452. pt_element_t *gptep = gw->prefetch_ptes;
  453. u64 *spte;
  454. int i;
  455. sp = page_header(__pa(sptep));
  456. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  457. return;
  458. if (sp->role.direct)
  459. return __direct_pte_prefetch(vcpu, sp, sptep);
  460. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  461. spte = sp->spt + i;
  462. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  463. if (spte == sptep)
  464. continue;
  465. if (is_shadow_present_pte(*spte))
  466. continue;
  467. if (!FNAME(prefetch_gpte)(vcpu, sp, spte, gptep[i], true))
  468. break;
  469. }
  470. }
  471. /*
  472. * Fetch a shadow pte for a specific level in the paging hierarchy.
  473. * If the guest tries to write a write-protected page, we need to
  474. * emulate this operation, return 1 to indicate this case.
  475. */
  476. static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  477. struct guest_walker *gw,
  478. int write_fault, int hlevel,
  479. pfn_t pfn, bool map_writable, bool prefault)
  480. {
  481. struct kvm_mmu_page *sp = NULL;
  482. struct kvm_shadow_walk_iterator it;
  483. unsigned direct_access, access = gw->pt_access;
  484. int top_level, emulate = 0;
  485. direct_access = gw->pte_access;
  486. top_level = vcpu->arch.mmu.root_level;
  487. if (top_level == PT32E_ROOT_LEVEL)
  488. top_level = PT32_ROOT_LEVEL;
  489. /*
  490. * Verify that the top-level gpte is still there. Since the page
  491. * is a root page, it is either write protected (and cannot be
  492. * changed from now on) or it is invalid (in which case, we don't
  493. * really care if it changes underneath us after this point).
  494. */
  495. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  496. goto out_gpte_changed;
  497. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  498. goto out_gpte_changed;
  499. for (shadow_walk_init(&it, vcpu, addr);
  500. shadow_walk_okay(&it) && it.level > gw->level;
  501. shadow_walk_next(&it)) {
  502. gfn_t table_gfn;
  503. clear_sp_write_flooding_count(it.sptep);
  504. drop_large_spte(vcpu, it.sptep);
  505. sp = NULL;
  506. if (!is_shadow_present_pte(*it.sptep)) {
  507. table_gfn = gw->table_gfn[it.level - 2];
  508. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  509. false, access, it.sptep);
  510. }
  511. /*
  512. * Verify that the gpte in the page we've just write
  513. * protected is still there.
  514. */
  515. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  516. goto out_gpte_changed;
  517. if (sp)
  518. link_shadow_page(it.sptep, sp, PT_GUEST_ACCESSED_MASK);
  519. }
  520. for (;
  521. shadow_walk_okay(&it) && it.level > hlevel;
  522. shadow_walk_next(&it)) {
  523. gfn_t direct_gfn;
  524. clear_sp_write_flooding_count(it.sptep);
  525. validate_direct_spte(vcpu, it.sptep, direct_access);
  526. drop_large_spte(vcpu, it.sptep);
  527. if (is_shadow_present_pte(*it.sptep))
  528. continue;
  529. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  530. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  531. true, direct_access, it.sptep);
  532. link_shadow_page(it.sptep, sp, PT_GUEST_ACCESSED_MASK);
  533. }
  534. clear_sp_write_flooding_count(it.sptep);
  535. mmu_set_spte(vcpu, it.sptep, gw->pte_access, write_fault, &emulate,
  536. it.level, gw->gfn, pfn, prefault, map_writable);
  537. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  538. return emulate;
  539. out_gpte_changed:
  540. if (sp)
  541. kvm_mmu_put_page(sp, it.sptep);
  542. kvm_release_pfn_clean(pfn);
  543. return 0;
  544. }
  545. /*
  546. * To see whether the mapped gfn can write its page table in the current
  547. * mapping.
  548. *
  549. * It is the helper function of FNAME(page_fault). When guest uses large page
  550. * size to map the writable gfn which is used as current page table, we should
  551. * force kvm to use small page size to map it because new shadow page will be
  552. * created when kvm establishes shadow page table that stop kvm using large
  553. * page size. Do it early can avoid unnecessary #PF and emulation.
  554. *
  555. * @write_fault_to_shadow_pgtable will return true if the fault gfn is
  556. * currently used as its page table.
  557. *
  558. * Note: the PDPT page table is not checked for PAE-32 bit guest. It is ok
  559. * since the PDPT is always shadowed, that means, we can not use large page
  560. * size to map the gfn which is used as PDPT.
  561. */
  562. static bool
  563. FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
  564. struct guest_walker *walker, int user_fault,
  565. bool *write_fault_to_shadow_pgtable)
  566. {
  567. int level;
  568. gfn_t mask = ~(KVM_PAGES_PER_HPAGE(walker->level) - 1);
  569. bool self_changed = false;
  570. if (!(walker->pte_access & ACC_WRITE_MASK ||
  571. (!is_write_protection(vcpu) && !user_fault)))
  572. return false;
  573. for (level = walker->level; level <= walker->max_level; level++) {
  574. gfn_t gfn = walker->gfn ^ walker->table_gfn[level - 1];
  575. self_changed |= !(gfn & mask);
  576. *write_fault_to_shadow_pgtable |= !gfn;
  577. }
  578. return self_changed;
  579. }
  580. /*
  581. * Page fault handler. There are several causes for a page fault:
  582. * - there is no shadow pte for the guest pte
  583. * - write access through a shadow pte marked read only so that we can set
  584. * the dirty bit
  585. * - write access to a shadow pte marked read only so we can update the page
  586. * dirty bitmap, when userspace requests it
  587. * - mmio access; in this case we will never install a present shadow pte
  588. * - normal guest page fault due to the guest pte marked not present, not
  589. * writable, or not executable
  590. *
  591. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  592. * a negative value on error.
  593. */
  594. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
  595. bool prefault)
  596. {
  597. int write_fault = error_code & PFERR_WRITE_MASK;
  598. int user_fault = error_code & PFERR_USER_MASK;
  599. struct guest_walker walker;
  600. int r;
  601. pfn_t pfn;
  602. int level = PT_PAGE_TABLE_LEVEL;
  603. int force_pt_level;
  604. unsigned long mmu_seq;
  605. bool map_writable, is_self_change_mapping;
  606. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  607. if (unlikely(error_code & PFERR_RSVD_MASK)) {
  608. r = handle_mmio_page_fault(vcpu, addr, error_code,
  609. mmu_is_nested(vcpu));
  610. if (likely(r != RET_MMIO_PF_INVALID))
  611. return r;
  612. };
  613. r = mmu_topup_memory_caches(vcpu);
  614. if (r)
  615. return r;
  616. /*
  617. * Look up the guest pte for the faulting address.
  618. */
  619. r = FNAME(walk_addr)(&walker, vcpu, addr, error_code);
  620. /*
  621. * The page is not mapped by the guest. Let the guest handle it.
  622. */
  623. if (!r) {
  624. pgprintk("%s: guest page fault\n", __func__);
  625. if (!prefault)
  626. inject_page_fault(vcpu, &walker.fault);
  627. return 0;
  628. }
  629. vcpu->arch.write_fault_to_shadow_pgtable = false;
  630. is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,
  631. &walker, user_fault, &vcpu->arch.write_fault_to_shadow_pgtable);
  632. if (walker.level >= PT_DIRECTORY_LEVEL)
  633. force_pt_level = mapping_level_dirty_bitmap(vcpu, walker.gfn)
  634. || is_self_change_mapping;
  635. else
  636. force_pt_level = 1;
  637. if (!force_pt_level) {
  638. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  639. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  640. }
  641. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  642. smp_rmb();
  643. if (try_async_pf(vcpu, prefault, walker.gfn, addr, &pfn, write_fault,
  644. &map_writable))
  645. return 0;
  646. if (handle_abnormal_pfn(vcpu, mmu_is_nested(vcpu) ? 0 : addr,
  647. walker.gfn, pfn, walker.pte_access, &r))
  648. return r;
  649. /*
  650. * Do not change pte_access if the pfn is a mmio page, otherwise
  651. * we will cache the incorrect access into mmio spte.
  652. */
  653. if (write_fault && !(walker.pte_access & ACC_WRITE_MASK) &&
  654. !is_write_protection(vcpu) && !user_fault &&
  655. !is_noslot_pfn(pfn)) {
  656. walker.pte_access |= ACC_WRITE_MASK;
  657. walker.pte_access &= ~ACC_USER_MASK;
  658. /*
  659. * If we converted a user page to a kernel page,
  660. * so that the kernel can write to it when cr0.wp=0,
  661. * then we should prevent the kernel from executing it
  662. * if SMEP is enabled.
  663. */
  664. if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
  665. walker.pte_access &= ~ACC_EXEC_MASK;
  666. }
  667. spin_lock(&vcpu->kvm->mmu_lock);
  668. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  669. goto out_unlock;
  670. kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  671. make_mmu_pages_available(vcpu);
  672. if (!force_pt_level)
  673. transparent_hugepage_adjust(vcpu, &walker.gfn, &pfn, &level);
  674. r = FNAME(fetch)(vcpu, addr, &walker, write_fault,
  675. level, pfn, map_writable, prefault);
  676. ++vcpu->stat.pf_fixed;
  677. kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  678. spin_unlock(&vcpu->kvm->mmu_lock);
  679. return r;
  680. out_unlock:
  681. spin_unlock(&vcpu->kvm->mmu_lock);
  682. kvm_release_pfn_clean(pfn);
  683. return 0;
  684. }
  685. static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp)
  686. {
  687. int offset = 0;
  688. WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
  689. if (PTTYPE == 32)
  690. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  691. return gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  692. }
  693. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  694. {
  695. struct kvm_shadow_walk_iterator iterator;
  696. struct kvm_mmu_page *sp;
  697. int level;
  698. u64 *sptep;
  699. vcpu_clear_mmio_info(vcpu, gva);
  700. /*
  701. * No need to check return value here, rmap_can_add() can
  702. * help us to skip pte prefetch later.
  703. */
  704. mmu_topup_memory_caches(vcpu);
  705. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
  706. WARN_ON(1);
  707. return;
  708. }
  709. spin_lock(&vcpu->kvm->mmu_lock);
  710. for_each_shadow_entry(vcpu, gva, iterator) {
  711. level = iterator.level;
  712. sptep = iterator.sptep;
  713. sp = page_header(__pa(sptep));
  714. if (is_last_spte(*sptep, level)) {
  715. pt_element_t gpte;
  716. gpa_t pte_gpa;
  717. if (!sp->unsync)
  718. break;
  719. pte_gpa = FNAME(get_level1_sp_gpa)(sp);
  720. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  721. if (mmu_page_zap_pte(vcpu->kvm, sp, sptep))
  722. kvm_flush_remote_tlbs(vcpu->kvm);
  723. if (!rmap_can_add(vcpu))
  724. break;
  725. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  726. sizeof(pt_element_t)))
  727. break;
  728. FNAME(update_pte)(vcpu, sp, sptep, &gpte);
  729. }
  730. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  731. break;
  732. }
  733. spin_unlock(&vcpu->kvm->mmu_lock);
  734. }
  735. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, 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)(&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. #if PTTYPE != PTTYPE_EPT
  750. static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
  751. u32 access,
  752. struct x86_exception *exception)
  753. {
  754. struct guest_walker walker;
  755. gpa_t gpa = UNMAPPED_GVA;
  756. int r;
  757. r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr, access);
  758. if (r) {
  759. gpa = gfn_to_gpa(walker.gfn);
  760. gpa |= vaddr & ~PAGE_MASK;
  761. } else if (exception)
  762. *exception = walker.fault;
  763. return gpa;
  764. }
  765. #endif
  766. /*
  767. * Using the cached information from sp->gfns is safe because:
  768. * - The spte has a reference to the struct page, so the pfn for a given gfn
  769. * can't change unless all sptes pointing to it are nuked first.
  770. *
  771. * Note:
  772. * We should flush all tlbs if spte is dropped even though guest is
  773. * responsible for it. Since if we don't, kvm_mmu_notifier_invalidate_page
  774. * and kvm_mmu_notifier_invalidate_range_start detect the mapping page isn't
  775. * used by guest then tlbs are not flushed, so guest is allowed to access the
  776. * freed pages.
  777. * And we increase kvm->tlbs_dirty to delay tlbs flush in this case.
  778. */
  779. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  780. {
  781. int i, nr_present = 0;
  782. bool host_writable;
  783. gpa_t first_pte_gpa;
  784. /* direct kvm_mmu_page can not be unsync. */
  785. BUG_ON(sp->role.direct);
  786. first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);
  787. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  788. unsigned pte_access;
  789. pt_element_t gpte;
  790. gpa_t pte_gpa;
  791. gfn_t gfn;
  792. if (!sp->spt[i])
  793. continue;
  794. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  795. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  796. sizeof(pt_element_t)))
  797. return -EINVAL;
  798. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, &sp->spt[i], gpte)) {
  799. vcpu->kvm->tlbs_dirty++;
  800. continue;
  801. }
  802. gfn = gpte_to_gfn(gpte);
  803. pte_access = sp->role.access;
  804. pte_access &= FNAME(gpte_access)(vcpu, gpte);
  805. FNAME(protect_clean_gpte)(&pte_access, gpte);
  806. if (sync_mmio_spte(vcpu->kvm, &sp->spt[i], gfn, pte_access,
  807. &nr_present))
  808. continue;
  809. if (gfn != sp->gfns[i]) {
  810. drop_spte(vcpu->kvm, &sp->spt[i]);
  811. vcpu->kvm->tlbs_dirty++;
  812. continue;
  813. }
  814. nr_present++;
  815. host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE;
  816. set_spte(vcpu, &sp->spt[i], pte_access,
  817. PT_PAGE_TABLE_LEVEL, gfn,
  818. spte_to_pfn(sp->spt[i]), true, false,
  819. host_writable);
  820. }
  821. return !nr_present;
  822. }
  823. #undef pt_element_t
  824. #undef guest_walker
  825. #undef FNAME
  826. #undef PT_BASE_ADDR_MASK
  827. #undef PT_INDEX
  828. #undef PT_LVL_ADDR_MASK
  829. #undef PT_LVL_OFFSET_MASK
  830. #undef PT_LEVEL_BITS
  831. #undef PT_MAX_FULL_LEVELS
  832. #undef gpte_to_gfn
  833. #undef gpte_to_gfn_lvl
  834. #undef CMPXCHG
  835. #undef PT_GUEST_ACCESSED_MASK
  836. #undef PT_GUEST_DIRTY_MASK
  837. #undef PT_GUEST_DIRTY_SHIFT
  838. #undef PT_GUEST_ACCESSED_SHIFT