paging_tmpl.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * MMU support
  8. *
  9. * Copyright (C) 2006 Qumranet, Inc.
  10. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  11. *
  12. * Authors:
  13. * Yaniv Kamay <yaniv@qumranet.com>
  14. * Avi Kivity <avi@qumranet.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. /*
  21. * We need the mmu code to access both 32-bit and 64-bit guest ptes,
  22. * so the code in this file is compiled twice, once per pte size.
  23. */
  24. #if PTTYPE == 64
  25. #define pt_element_t u64
  26. #define guest_walker guest_walker64
  27. #define FNAME(name) paging##64_##name
  28. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  29. #define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
  30. #define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
  31. #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
  32. #define PT_LEVEL_BITS PT64_LEVEL_BITS
  33. #define PT_GUEST_DIRTY_SHIFT PT_DIRTY_SHIFT
  34. #define PT_GUEST_ACCESSED_SHIFT PT_ACCESSED_SHIFT
  35. #define PT_HAVE_ACCESSED_DIRTY(mmu) true
  36. #ifdef CONFIG_X86_64
  37. #define PT_MAX_FULL_LEVELS PT64_ROOT_MAX_LEVEL
  38. #define CMPXCHG cmpxchg
  39. #else
  40. #define CMPXCHG cmpxchg64
  41. #define PT_MAX_FULL_LEVELS 2
  42. #endif
  43. #elif PTTYPE == 32
  44. #define pt_element_t u32
  45. #define guest_walker guest_walker32
  46. #define FNAME(name) paging##32_##name
  47. #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
  48. #define PT_LVL_ADDR_MASK(lvl) PT32_LVL_ADDR_MASK(lvl)
  49. #define PT_LVL_OFFSET_MASK(lvl) PT32_LVL_OFFSET_MASK(lvl)
  50. #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
  51. #define PT_LEVEL_BITS PT32_LEVEL_BITS
  52. #define PT_MAX_FULL_LEVELS 2
  53. #define PT_GUEST_DIRTY_SHIFT PT_DIRTY_SHIFT
  54. #define PT_GUEST_ACCESSED_SHIFT PT_ACCESSED_SHIFT
  55. #define PT_HAVE_ACCESSED_DIRTY(mmu) true
  56. #define CMPXCHG cmpxchg
  57. #elif PTTYPE == PTTYPE_EPT
  58. #define pt_element_t u64
  59. #define guest_walker guest_walkerEPT
  60. #define FNAME(name) ept_##name
  61. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  62. #define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
  63. #define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
  64. #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
  65. #define PT_LEVEL_BITS PT64_LEVEL_BITS
  66. #define PT_GUEST_DIRTY_SHIFT 9
  67. #define PT_GUEST_ACCESSED_SHIFT 8
  68. #define PT_HAVE_ACCESSED_DIRTY(mmu) ((mmu)->ept_ad)
  69. #define CMPXCHG cmpxchg64
  70. #define PT_MAX_FULL_LEVELS 4
  71. #else
  72. #error Invalid PTTYPE value
  73. #endif
  74. #define PT_GUEST_DIRTY_MASK (1 << PT_GUEST_DIRTY_SHIFT)
  75. #define PT_GUEST_ACCESSED_MASK (1 << PT_GUEST_ACCESSED_SHIFT)
  76. #define gpte_to_gfn_lvl FNAME(gpte_to_gfn_lvl)
  77. #define gpte_to_gfn(pte) gpte_to_gfn_lvl((pte), PT_PAGE_TABLE_LEVEL)
  78. /*
  79. * The guest_walker structure emulates the behavior of the hardware page
  80. * table walker.
  81. */
  82. struct guest_walker {
  83. int level;
  84. unsigned max_level;
  85. gfn_t table_gfn[PT_MAX_FULL_LEVELS];
  86. pt_element_t ptes[PT_MAX_FULL_LEVELS];
  87. pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
  88. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  89. pt_element_t __user *ptep_user[PT_MAX_FULL_LEVELS];
  90. bool pte_writable[PT_MAX_FULL_LEVELS];
  91. unsigned pt_access;
  92. unsigned pte_access;
  93. gfn_t gfn;
  94. struct x86_exception fault;
  95. };
  96. static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
  97. {
  98. return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
  99. }
  100. static inline void FNAME(protect_clean_gpte)(struct kvm_mmu *mmu, unsigned *access,
  101. unsigned gpte)
  102. {
  103. unsigned mask;
  104. /* dirty bit is not supported, so no need to track it */
  105. if (!PT_HAVE_ACCESSED_DIRTY(mmu))
  106. return;
  107. BUILD_BUG_ON(PT_WRITABLE_MASK != ACC_WRITE_MASK);
  108. mask = (unsigned)~ACC_WRITE_MASK;
  109. /* Allow write access to dirty gptes */
  110. mask |= (gpte >> (PT_GUEST_DIRTY_SHIFT - PT_WRITABLE_SHIFT)) &
  111. PT_WRITABLE_MASK;
  112. *access &= mask;
  113. }
  114. static inline int FNAME(is_present_gpte)(unsigned long pte)
  115. {
  116. #if PTTYPE != PTTYPE_EPT
  117. return pte & PT_PRESENT_MASK;
  118. #else
  119. return pte & 7;
  120. #endif
  121. }
  122. static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  123. pt_element_t __user *ptep_user, unsigned index,
  124. pt_element_t orig_pte, pt_element_t new_pte)
  125. {
  126. int npages;
  127. pt_element_t ret;
  128. pt_element_t *table;
  129. struct page *page;
  130. npages = get_user_pages_fast((unsigned long)ptep_user, 1, 1, &page);
  131. /* Check if the user is doing something meaningless. */
  132. if (unlikely(npages != 1))
  133. return -EFAULT;
  134. table = kmap_atomic(page);
  135. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  136. kunmap_atomic(table);
  137. kvm_release_page_dirty(page);
  138. return (ret != orig_pte);
  139. }
  140. static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
  141. struct kvm_mmu_page *sp, u64 *spte,
  142. u64 gpte)
  143. {
  144. if (is_rsvd_bits_set(&vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL))
  145. goto no_present;
  146. if (!FNAME(is_present_gpte)(gpte))
  147. goto no_present;
  148. /* if accessed bit is not supported prefetch non accessed gpte */
  149. if (PT_HAVE_ACCESSED_DIRTY(&vcpu->arch.mmu) && !(gpte & PT_GUEST_ACCESSED_MASK))
  150. goto no_present;
  151. return false;
  152. no_present:
  153. drop_spte(vcpu->kvm, spte);
  154. return true;
  155. }
  156. /*
  157. * For PTTYPE_EPT, a page table can be executable but not readable
  158. * on supported processors. Therefore, set_spte does not automatically
  159. * set bit 0 if execute only is supported. Here, we repurpose ACC_USER_MASK
  160. * to signify readability since it isn't used in the EPT case
  161. */
  162. static inline unsigned FNAME(gpte_access)(u64 gpte)
  163. {
  164. unsigned access;
  165. #if PTTYPE == PTTYPE_EPT
  166. access = ((gpte & VMX_EPT_WRITABLE_MASK) ? ACC_WRITE_MASK : 0) |
  167. ((gpte & VMX_EPT_EXECUTABLE_MASK) ? ACC_EXEC_MASK : 0) |
  168. ((gpte & VMX_EPT_READABLE_MASK) ? ACC_USER_MASK : 0);
  169. #else
  170. BUILD_BUG_ON(ACC_EXEC_MASK != PT_PRESENT_MASK);
  171. BUILD_BUG_ON(ACC_EXEC_MASK != 1);
  172. access = gpte & (PT_WRITABLE_MASK | PT_USER_MASK | PT_PRESENT_MASK);
  173. /* Combine NX with P (which is set here) to get ACC_EXEC_MASK. */
  174. access ^= (gpte >> PT64_NX_SHIFT);
  175. #endif
  176. return access;
  177. }
  178. static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,
  179. struct kvm_mmu *mmu,
  180. struct guest_walker *walker,
  181. gpa_t addr, int write_fault)
  182. {
  183. unsigned level, index;
  184. pt_element_t pte, orig_pte;
  185. pt_element_t __user *ptep_user;
  186. gfn_t table_gfn;
  187. int ret;
  188. /* dirty/accessed bits are not supported, so no need to update them */
  189. if (!PT_HAVE_ACCESSED_DIRTY(mmu))
  190. return 0;
  191. for (level = walker->max_level; level >= walker->level; --level) {
  192. pte = orig_pte = walker->ptes[level - 1];
  193. table_gfn = walker->table_gfn[level - 1];
  194. ptep_user = walker->ptep_user[level - 1];
  195. index = offset_in_page(ptep_user) / sizeof(pt_element_t);
  196. if (!(pte & PT_GUEST_ACCESSED_MASK)) {
  197. trace_kvm_mmu_set_accessed_bit(table_gfn, index, sizeof(pte));
  198. pte |= PT_GUEST_ACCESSED_MASK;
  199. }
  200. if (level == walker->level && write_fault &&
  201. !(pte & PT_GUEST_DIRTY_MASK)) {
  202. trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
  203. #if PTTYPE == PTTYPE_EPT
  204. if (kvm_arch_write_log_dirty(vcpu, addr))
  205. return -EINVAL;
  206. #endif
  207. pte |= PT_GUEST_DIRTY_MASK;
  208. }
  209. if (pte == orig_pte)
  210. continue;
  211. /*
  212. * If the slot is read-only, simply do not process the accessed
  213. * and dirty bits. This is the correct thing to do if the slot
  214. * is ROM, and page tables in read-as-ROM/write-as-MMIO slots
  215. * are only supported if the accessed and dirty bits are already
  216. * set in the ROM (so that MMIO writes are never needed).
  217. *
  218. * Note that NPT does not allow this at all and faults, since
  219. * it always wants nested page table entries for the guest
  220. * page tables to be writable. And EPT works but will simply
  221. * overwrite the read-only memory to set the accessed and dirty
  222. * bits.
  223. */
  224. if (unlikely(!walker->pte_writable[level - 1]))
  225. continue;
  226. ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index, orig_pte, pte);
  227. if (ret)
  228. return ret;
  229. kvm_vcpu_mark_page_dirty(vcpu, table_gfn);
  230. walker->ptes[level - 1] = pte;
  231. }
  232. return 0;
  233. }
  234. static inline unsigned FNAME(gpte_pkeys)(struct kvm_vcpu *vcpu, u64 gpte)
  235. {
  236. unsigned pkeys = 0;
  237. #if PTTYPE == 64
  238. pte_t pte = {.pte = gpte};
  239. pkeys = pte_flags_pkey(pte_flags(pte));
  240. #endif
  241. return pkeys;
  242. }
  243. /*
  244. * Fetch a guest pte for a guest virtual address, or for an L2's GPA.
  245. */
  246. static int FNAME(walk_addr_generic)(struct guest_walker *walker,
  247. struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  248. gpa_t addr, u32 access)
  249. {
  250. int ret;
  251. pt_element_t pte;
  252. pt_element_t __user *uninitialized_var(ptep_user);
  253. gfn_t table_gfn;
  254. u64 pt_access, pte_access;
  255. unsigned index, accessed_dirty, pte_pkey;
  256. unsigned nested_access;
  257. gpa_t pte_gpa;
  258. bool have_ad;
  259. int offset;
  260. u64 walk_nx_mask = 0;
  261. const int write_fault = access & PFERR_WRITE_MASK;
  262. const int user_fault = access & PFERR_USER_MASK;
  263. const int fetch_fault = access & PFERR_FETCH_MASK;
  264. u16 errcode = 0;
  265. gpa_t real_gpa;
  266. gfn_t gfn;
  267. trace_kvm_mmu_pagetable_walk(addr, access);
  268. retry_walk:
  269. walker->level = mmu->root_level;
  270. pte = mmu->get_cr3(vcpu);
  271. have_ad = PT_HAVE_ACCESSED_DIRTY(mmu);
  272. #if PTTYPE == 64
  273. walk_nx_mask = 1ULL << PT64_NX_SHIFT;
  274. if (walker->level == PT32E_ROOT_LEVEL) {
  275. pte = mmu->get_pdptr(vcpu, (addr >> 30) & 3);
  276. trace_kvm_mmu_paging_element(pte, walker->level);
  277. if (!FNAME(is_present_gpte)(pte))
  278. goto error;
  279. --walker->level;
  280. }
  281. #endif
  282. walker->max_level = walker->level;
  283. ASSERT(!(is_long_mode(vcpu) && !is_pae(vcpu)));
  284. /*
  285. * FIXME: on Intel processors, loads of the PDPTE registers for PAE paging
  286. * by the MOV to CR instruction are treated as reads and do not cause the
  287. * processor to set the dirty flag in any EPT paging-structure entry.
  288. */
  289. nested_access = (have_ad ? PFERR_WRITE_MASK : 0) | PFERR_USER_MASK;
  290. pte_access = ~0;
  291. ++walker->level;
  292. do {
  293. gfn_t real_gfn;
  294. unsigned long host_addr;
  295. pt_access = pte_access;
  296. --walker->level;
  297. index = PT_INDEX(addr, walker->level);
  298. table_gfn = gpte_to_gfn(pte);
  299. offset = index * sizeof(pt_element_t);
  300. pte_gpa = gfn_to_gpa(table_gfn) + offset;
  301. BUG_ON(walker->level < 1);
  302. walker->table_gfn[walker->level - 1] = table_gfn;
  303. walker->pte_gpa[walker->level - 1] = pte_gpa;
  304. real_gfn = mmu->translate_gpa(vcpu, gfn_to_gpa(table_gfn),
  305. nested_access,
  306. &walker->fault);
  307. /*
  308. * FIXME: This can happen if emulation (for of an INS/OUTS
  309. * instruction) triggers a nested page fault. The exit
  310. * qualification / exit info field will incorrectly have
  311. * "guest page access" as the nested page fault's cause,
  312. * instead of "guest page structure access". To fix this,
  313. * the x86_exception struct should be augmented with enough
  314. * information to fix the exit_qualification or exit_info_1
  315. * fields.
  316. */
  317. if (unlikely(real_gfn == UNMAPPED_GVA))
  318. return 0;
  319. real_gfn = gpa_to_gfn(real_gfn);
  320. host_addr = kvm_vcpu_gfn_to_hva_prot(vcpu, real_gfn,
  321. &walker->pte_writable[walker->level - 1]);
  322. if (unlikely(kvm_is_error_hva(host_addr)))
  323. goto error;
  324. ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
  325. if (unlikely(__copy_from_user(&pte, ptep_user, sizeof(pte))))
  326. goto error;
  327. walker->ptep_user[walker->level - 1] = ptep_user;
  328. trace_kvm_mmu_paging_element(pte, walker->level);
  329. /*
  330. * Inverting the NX it lets us AND it like other
  331. * permission bits.
  332. */
  333. pte_access = pt_access & (pte ^ walk_nx_mask);
  334. if (unlikely(!FNAME(is_present_gpte)(pte)))
  335. goto error;
  336. if (unlikely(is_rsvd_bits_set(mmu, pte, walker->level))) {
  337. errcode = PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
  338. goto error;
  339. }
  340. walker->ptes[walker->level - 1] = pte;
  341. } while (!is_last_gpte(mmu, walker->level, pte));
  342. pte_pkey = FNAME(gpte_pkeys)(vcpu, pte);
  343. accessed_dirty = have_ad ? pte_access & PT_GUEST_ACCESSED_MASK : 0;
  344. /* Convert to ACC_*_MASK flags for struct guest_walker. */
  345. walker->pt_access = FNAME(gpte_access)(pt_access ^ walk_nx_mask);
  346. walker->pte_access = FNAME(gpte_access)(pte_access ^ walk_nx_mask);
  347. errcode = permission_fault(vcpu, mmu, walker->pte_access, pte_pkey, access);
  348. if (unlikely(errcode))
  349. goto error;
  350. gfn = gpte_to_gfn_lvl(pte, walker->level);
  351. gfn += (addr & PT_LVL_OFFSET_MASK(walker->level)) >> PAGE_SHIFT;
  352. if (PTTYPE == 32 && walker->level == PT_DIRECTORY_LEVEL && is_cpuid_PSE36())
  353. gfn += pse36_gfn_delta(pte);
  354. real_gpa = mmu->translate_gpa(vcpu, gfn_to_gpa(gfn), access, &walker->fault);
  355. if (real_gpa == UNMAPPED_GVA)
  356. return 0;
  357. walker->gfn = real_gpa >> PAGE_SHIFT;
  358. if (!write_fault)
  359. FNAME(protect_clean_gpte)(mmu, &walker->pte_access, pte);
  360. else
  361. /*
  362. * On a write fault, fold the dirty bit into accessed_dirty.
  363. * For modes without A/D bits support accessed_dirty will be
  364. * always clear.
  365. */
  366. accessed_dirty &= pte >>
  367. (PT_GUEST_DIRTY_SHIFT - PT_GUEST_ACCESSED_SHIFT);
  368. if (unlikely(!accessed_dirty)) {
  369. ret = FNAME(update_accessed_dirty_bits)(vcpu, mmu, walker,
  370. addr, write_fault);
  371. if (unlikely(ret < 0))
  372. goto error;
  373. else if (ret)
  374. goto retry_walk;
  375. }
  376. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  377. __func__, (u64)pte, walker->pte_access, walker->pt_access);
  378. return 1;
  379. error:
  380. errcode |= write_fault | user_fault;
  381. if (fetch_fault && (mmu->nx ||
  382. kvm_read_cr4_bits(vcpu, X86_CR4_SMEP)))
  383. errcode |= PFERR_FETCH_MASK;
  384. walker->fault.vector = PF_VECTOR;
  385. walker->fault.error_code_valid = true;
  386. walker->fault.error_code = errcode;
  387. #if PTTYPE == PTTYPE_EPT
  388. /*
  389. * Use PFERR_RSVD_MASK in error_code to to tell if EPT
  390. * misconfiguration requires to be injected. The detection is
  391. * done by is_rsvd_bits_set() above.
  392. *
  393. * We set up the value of exit_qualification to inject:
  394. * [2:0] - Derive from the access bits. The exit_qualification might be
  395. * out of date if it is serving an EPT misconfiguration.
  396. * [5:3] - Calculated by the page walk of the guest EPT page tables
  397. * [7:8] - Derived from [7:8] of real exit_qualification
  398. *
  399. * The other bits are set to 0.
  400. */
  401. if (!(errcode & PFERR_RSVD_MASK)) {
  402. vcpu->arch.exit_qualification &= 0x180;
  403. if (write_fault)
  404. vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_WRITE;
  405. if (user_fault)
  406. vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_READ;
  407. if (fetch_fault)
  408. vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_INSTR;
  409. vcpu->arch.exit_qualification |= (pte_access & 0x7) << 3;
  410. }
  411. #endif
  412. walker->fault.address = addr;
  413. walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
  414. trace_kvm_mmu_walker_error(walker->fault.error_code);
  415. return 0;
  416. }
  417. static int FNAME(walk_addr)(struct guest_walker *walker,
  418. struct kvm_vcpu *vcpu, gpa_t addr, u32 access)
  419. {
  420. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr,
  421. access);
  422. }
  423. #if PTTYPE != PTTYPE_EPT
  424. static int FNAME(walk_addr_nested)(struct guest_walker *walker,
  425. struct kvm_vcpu *vcpu, gva_t addr,
  426. u32 access)
  427. {
  428. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
  429. addr, access);
  430. }
  431. #endif
  432. static bool
  433. FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  434. u64 *spte, pt_element_t gpte, bool no_dirty_log)
  435. {
  436. unsigned pte_access;
  437. gfn_t gfn;
  438. kvm_pfn_t pfn;
  439. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
  440. return false;
  441. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  442. gfn = gpte_to_gfn(gpte);
  443. pte_access = sp->role.access & FNAME(gpte_access)(gpte);
  444. FNAME(protect_clean_gpte)(&vcpu->arch.mmu, &pte_access, gpte);
  445. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  446. no_dirty_log && (pte_access & ACC_WRITE_MASK));
  447. if (is_error_pfn(pfn))
  448. return false;
  449. /*
  450. * we call mmu_set_spte() with host_writable = true because
  451. * pte_prefetch_gfn_to_pfn always gets a writable pfn.
  452. */
  453. mmu_set_spte(vcpu, spte, pte_access, 0, PT_PAGE_TABLE_LEVEL, gfn, pfn,
  454. true, true);
  455. kvm_release_pfn_clean(pfn);
  456. return true;
  457. }
  458. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  459. u64 *spte, const void *pte)
  460. {
  461. pt_element_t gpte = *(const pt_element_t *)pte;
  462. FNAME(prefetch_gpte)(vcpu, sp, spte, gpte, false);
  463. }
  464. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  465. struct guest_walker *gw, int level)
  466. {
  467. pt_element_t curr_pte;
  468. gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
  469. u64 mask;
  470. int r, index;
  471. if (level == PT_PAGE_TABLE_LEVEL) {
  472. mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
  473. base_gpa = pte_gpa & ~mask;
  474. index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
  475. r = kvm_vcpu_read_guest_atomic(vcpu, base_gpa,
  476. gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
  477. curr_pte = gw->prefetch_ptes[index];
  478. } else
  479. r = kvm_vcpu_read_guest_atomic(vcpu, pte_gpa,
  480. &curr_pte, sizeof(curr_pte));
  481. return r || curr_pte != gw->ptes[level - 1];
  482. }
  483. static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
  484. u64 *sptep)
  485. {
  486. struct kvm_mmu_page *sp;
  487. pt_element_t *gptep = gw->prefetch_ptes;
  488. u64 *spte;
  489. int i;
  490. sp = page_header(__pa(sptep));
  491. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  492. return;
  493. if (sp->role.direct)
  494. return __direct_pte_prefetch(vcpu, sp, sptep);
  495. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  496. spte = sp->spt + i;
  497. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  498. if (spte == sptep)
  499. continue;
  500. if (is_shadow_present_pte(*spte))
  501. continue;
  502. if (!FNAME(prefetch_gpte)(vcpu, sp, spte, gptep[i], true))
  503. break;
  504. }
  505. }
  506. /*
  507. * Fetch a shadow pte for a specific level in the paging hierarchy.
  508. * If the guest tries to write a write-protected page, we need to
  509. * emulate this operation, return 1 to indicate this case.
  510. */
  511. static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,
  512. struct guest_walker *gw,
  513. int write_fault, int hlevel,
  514. kvm_pfn_t pfn, bool map_writable, bool prefault,
  515. bool lpage_disallowed)
  516. {
  517. struct kvm_mmu_page *sp = NULL;
  518. struct kvm_shadow_walk_iterator it;
  519. unsigned direct_access, access = gw->pt_access;
  520. int top_level, ret;
  521. gfn_t gfn, base_gfn;
  522. direct_access = gw->pte_access;
  523. top_level = vcpu->arch.mmu.root_level;
  524. if (top_level == PT32E_ROOT_LEVEL)
  525. top_level = PT32_ROOT_LEVEL;
  526. /*
  527. * Verify that the top-level gpte is still there. Since the page
  528. * is a root page, it is either write protected (and cannot be
  529. * changed from now on) or it is invalid (in which case, we don't
  530. * really care if it changes underneath us after this point).
  531. */
  532. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  533. goto out_gpte_changed;
  534. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  535. goto out_gpte_changed;
  536. for (shadow_walk_init(&it, vcpu, addr);
  537. shadow_walk_okay(&it) && it.level > gw->level;
  538. shadow_walk_next(&it)) {
  539. gfn_t table_gfn;
  540. clear_sp_write_flooding_count(it.sptep);
  541. drop_large_spte(vcpu, it.sptep);
  542. sp = NULL;
  543. if (!is_shadow_present_pte(*it.sptep)) {
  544. table_gfn = gw->table_gfn[it.level - 2];
  545. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  546. false, access);
  547. }
  548. /*
  549. * Verify that the gpte in the page we've just write
  550. * protected is still there.
  551. */
  552. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  553. goto out_gpte_changed;
  554. if (sp)
  555. link_shadow_page(vcpu, it.sptep, sp);
  556. }
  557. /*
  558. * FNAME(page_fault) might have clobbered the bottom bits of
  559. * gw->gfn, restore them from the virtual address.
  560. */
  561. gfn = gw->gfn | ((addr & PT_LVL_OFFSET_MASK(gw->level)) >> PAGE_SHIFT);
  562. base_gfn = gfn;
  563. trace_kvm_mmu_spte_requested(addr, gw->level, pfn);
  564. for (; shadow_walk_okay(&it); shadow_walk_next(&it)) {
  565. clear_sp_write_flooding_count(it.sptep);
  566. /*
  567. * We cannot overwrite existing page tables with an NX
  568. * large page, as the leaf could be executable.
  569. */
  570. disallowed_hugepage_adjust(it, gfn, &pfn, &hlevel);
  571. base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  572. if (it.level == hlevel)
  573. break;
  574. validate_direct_spte(vcpu, it.sptep, direct_access);
  575. drop_large_spte(vcpu, it.sptep);
  576. if (!is_shadow_present_pte(*it.sptep)) {
  577. sp = kvm_mmu_get_page(vcpu, base_gfn, addr,
  578. it.level - 1, true, direct_access);
  579. link_shadow_page(vcpu, it.sptep, sp);
  580. if (lpage_disallowed)
  581. account_huge_nx_page(vcpu->kvm, sp);
  582. }
  583. }
  584. ret = mmu_set_spte(vcpu, it.sptep, gw->pte_access, write_fault,
  585. it.level, base_gfn, pfn, prefault, map_writable);
  586. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  587. ++vcpu->stat.pf_fixed;
  588. return ret;
  589. out_gpte_changed:
  590. return RET_PF_RETRY;
  591. }
  592. /*
  593. * To see whether the mapped gfn can write its page table in the current
  594. * mapping.
  595. *
  596. * It is the helper function of FNAME(page_fault). When guest uses large page
  597. * size to map the writable gfn which is used as current page table, we should
  598. * force kvm to use small page size to map it because new shadow page will be
  599. * created when kvm establishes shadow page table that stop kvm using large
  600. * page size. Do it early can avoid unnecessary #PF and emulation.
  601. *
  602. * @write_fault_to_shadow_pgtable will return true if the fault gfn is
  603. * currently used as its page table.
  604. *
  605. * Note: the PDPT page table is not checked for PAE-32 bit guest. It is ok
  606. * since the PDPT is always shadowed, that means, we can not use large page
  607. * size to map the gfn which is used as PDPT.
  608. */
  609. static bool
  610. FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
  611. struct guest_walker *walker, int user_fault,
  612. bool *write_fault_to_shadow_pgtable)
  613. {
  614. int level;
  615. gfn_t mask = ~(KVM_PAGES_PER_HPAGE(walker->level) - 1);
  616. bool self_changed = false;
  617. if (!(walker->pte_access & ACC_WRITE_MASK ||
  618. (!is_write_protection(vcpu) && !user_fault)))
  619. return false;
  620. for (level = walker->level; level <= walker->max_level; level++) {
  621. gfn_t gfn = walker->gfn ^ walker->table_gfn[level - 1];
  622. self_changed |= !(gfn & mask);
  623. *write_fault_to_shadow_pgtable |= !gfn;
  624. }
  625. return self_changed;
  626. }
  627. /*
  628. * Page fault handler. There are several causes for a page fault:
  629. * - there is no shadow pte for the guest pte
  630. * - write access through a shadow pte marked read only so that we can set
  631. * the dirty bit
  632. * - write access to a shadow pte marked read only so we can update the page
  633. * dirty bitmap, when userspace requests it
  634. * - mmio access; in this case we will never install a present shadow pte
  635. * - normal guest page fault due to the guest pte marked not present, not
  636. * writable, or not executable
  637. *
  638. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  639. * a negative value on error.
  640. */
  641. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code,
  642. bool prefault)
  643. {
  644. int write_fault = error_code & PFERR_WRITE_MASK;
  645. int user_fault = error_code & PFERR_USER_MASK;
  646. struct guest_walker walker;
  647. int r;
  648. kvm_pfn_t pfn;
  649. int level = PT_PAGE_TABLE_LEVEL;
  650. unsigned long mmu_seq;
  651. bool map_writable, is_self_change_mapping;
  652. bool lpage_disallowed = (error_code & PFERR_FETCH_MASK) &&
  653. is_nx_huge_page_enabled();
  654. bool force_pt_level = lpage_disallowed;
  655. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  656. r = mmu_topup_memory_caches(vcpu);
  657. if (r)
  658. return r;
  659. /*
  660. * If PFEC.RSVD is set, this is a shadow page fault.
  661. * The bit needs to be cleared before walking guest page tables.
  662. */
  663. error_code &= ~PFERR_RSVD_MASK;
  664. /*
  665. * Look up the guest pte for the faulting address.
  666. */
  667. r = FNAME(walk_addr)(&walker, vcpu, addr, error_code);
  668. /*
  669. * The page is not mapped by the guest. Let the guest handle it.
  670. */
  671. if (!r) {
  672. pgprintk("%s: guest page fault\n", __func__);
  673. if (!prefault)
  674. inject_page_fault(vcpu, &walker.fault);
  675. return RET_PF_RETRY;
  676. }
  677. if (page_fault_handle_page_track(vcpu, error_code, walker.gfn)) {
  678. shadow_page_table_clear_flood(vcpu, addr);
  679. return RET_PF_EMULATE;
  680. }
  681. vcpu->arch.write_fault_to_shadow_pgtable = false;
  682. is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,
  683. &walker, user_fault, &vcpu->arch.write_fault_to_shadow_pgtable);
  684. if (walker.level >= PT_DIRECTORY_LEVEL && !is_self_change_mapping) {
  685. level = mapping_level(vcpu, walker.gfn, &force_pt_level);
  686. if (likely(!force_pt_level)) {
  687. level = min(walker.level, level);
  688. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  689. }
  690. } else
  691. force_pt_level = true;
  692. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  693. smp_rmb();
  694. if (try_async_pf(vcpu, prefault, walker.gfn, addr, &pfn, write_fault,
  695. &map_writable))
  696. return RET_PF_RETRY;
  697. if (handle_abnormal_pfn(vcpu, addr, walker.gfn, pfn, walker.pte_access, &r))
  698. return r;
  699. /*
  700. * Do not change pte_access if the pfn is a mmio page, otherwise
  701. * we will cache the incorrect access into mmio spte.
  702. */
  703. if (write_fault && !(walker.pte_access & ACC_WRITE_MASK) &&
  704. !is_write_protection(vcpu) && !user_fault &&
  705. !is_noslot_pfn(pfn)) {
  706. walker.pte_access |= ACC_WRITE_MASK;
  707. walker.pte_access &= ~ACC_USER_MASK;
  708. /*
  709. * If we converted a user page to a kernel page,
  710. * so that the kernel can write to it when cr0.wp=0,
  711. * then we should prevent the kernel from executing it
  712. * if SMEP is enabled.
  713. */
  714. if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
  715. walker.pte_access &= ~ACC_EXEC_MASK;
  716. }
  717. r = RET_PF_RETRY;
  718. spin_lock(&vcpu->kvm->mmu_lock);
  719. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  720. goto out_unlock;
  721. kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  722. if (make_mmu_pages_available(vcpu) < 0)
  723. goto out_unlock;
  724. if (!force_pt_level)
  725. transparent_hugepage_adjust(vcpu, walker.gfn, &pfn, &level);
  726. r = FNAME(fetch)(vcpu, addr, &walker, write_fault,
  727. level, pfn, map_writable, prefault, lpage_disallowed);
  728. kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  729. out_unlock:
  730. spin_unlock(&vcpu->kvm->mmu_lock);
  731. kvm_release_pfn_clean(pfn);
  732. return r;
  733. }
  734. static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp)
  735. {
  736. int offset = 0;
  737. WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
  738. if (PTTYPE == 32)
  739. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  740. return gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  741. }
  742. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa)
  743. {
  744. struct kvm_shadow_walk_iterator iterator;
  745. struct kvm_mmu_page *sp;
  746. int level;
  747. u64 *sptep;
  748. vcpu_clear_mmio_info(vcpu, gva);
  749. /*
  750. * No need to check return value here, rmap_can_add() can
  751. * help us to skip pte prefetch later.
  752. */
  753. mmu_topup_memory_caches(vcpu);
  754. if (!VALID_PAGE(root_hpa)) {
  755. WARN_ON(1);
  756. return;
  757. }
  758. spin_lock(&vcpu->kvm->mmu_lock);
  759. for_each_shadow_entry_using_root(vcpu, root_hpa, gva, iterator) {
  760. level = iterator.level;
  761. sptep = iterator.sptep;
  762. sp = page_header(__pa(sptep));
  763. if (is_last_spte(*sptep, level)) {
  764. pt_element_t gpte;
  765. gpa_t pte_gpa;
  766. if (!sp->unsync)
  767. break;
  768. pte_gpa = FNAME(get_level1_sp_gpa)(sp);
  769. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  770. if (mmu_page_zap_pte(vcpu->kvm, sp, sptep))
  771. kvm_flush_remote_tlbs(vcpu->kvm);
  772. if (!rmap_can_add(vcpu))
  773. break;
  774. if (kvm_vcpu_read_guest_atomic(vcpu, pte_gpa, &gpte,
  775. sizeof(pt_element_t)))
  776. break;
  777. FNAME(update_pte)(vcpu, sp, sptep, &gpte);
  778. }
  779. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  780. break;
  781. }
  782. spin_unlock(&vcpu->kvm->mmu_lock);
  783. }
  784. /* Note, @addr is a GPA when gva_to_gpa() translates an L2 GPA to an L1 GPA. */
  785. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gpa_t addr, u32 access,
  786. struct x86_exception *exception)
  787. {
  788. struct guest_walker walker;
  789. gpa_t gpa = UNMAPPED_GVA;
  790. int r;
  791. r = FNAME(walk_addr)(&walker, vcpu, addr, access);
  792. if (r) {
  793. gpa = gfn_to_gpa(walker.gfn);
  794. gpa |= addr & ~PAGE_MASK;
  795. } else if (exception)
  796. *exception = walker.fault;
  797. return gpa;
  798. }
  799. #if PTTYPE != PTTYPE_EPT
  800. /* Note, gva_to_gpa_nested() is only used to translate L2 GVAs. */
  801. static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gpa_t vaddr,
  802. u32 access,
  803. struct x86_exception *exception)
  804. {
  805. struct guest_walker walker;
  806. gpa_t gpa = UNMAPPED_GVA;
  807. int r;
  808. #ifndef CONFIG_X86_64
  809. /* A 64-bit GVA should be impossible on 32-bit KVM. */
  810. WARN_ON_ONCE(vaddr >> 32);
  811. #endif
  812. r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr, access);
  813. if (r) {
  814. gpa = gfn_to_gpa(walker.gfn);
  815. gpa |= vaddr & ~PAGE_MASK;
  816. } else if (exception)
  817. *exception = walker.fault;
  818. return gpa;
  819. }
  820. #endif
  821. /*
  822. * Using the cached information from sp->gfns is safe because:
  823. * - The spte has a reference to the struct page, so the pfn for a given gfn
  824. * can't change unless all sptes pointing to it are nuked first.
  825. *
  826. * Note:
  827. * We should flush all tlbs if spte is dropped even though guest is
  828. * responsible for it. Since if we don't, kvm_mmu_notifier_invalidate_page
  829. * and kvm_mmu_notifier_invalidate_range_start detect the mapping page isn't
  830. * used by guest then tlbs are not flushed, so guest is allowed to access the
  831. * freed pages.
  832. * And we increase kvm->tlbs_dirty to delay tlbs flush in this case.
  833. */
  834. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  835. {
  836. int i, nr_present = 0;
  837. bool host_writable;
  838. gpa_t first_pte_gpa;
  839. int set_spte_ret = 0;
  840. /* direct kvm_mmu_page can not be unsync. */
  841. BUG_ON(sp->role.direct);
  842. first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);
  843. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  844. unsigned pte_access;
  845. pt_element_t gpte;
  846. gpa_t pte_gpa;
  847. gfn_t gfn;
  848. if (!sp->spt[i])
  849. continue;
  850. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  851. if (kvm_vcpu_read_guest_atomic(vcpu, pte_gpa, &gpte,
  852. sizeof(pt_element_t)))
  853. return 0;
  854. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, &sp->spt[i], gpte)) {
  855. /*
  856. * Update spte before increasing tlbs_dirty to make
  857. * sure no tlb flush is lost after spte is zapped; see
  858. * the comments in kvm_flush_remote_tlbs().
  859. */
  860. smp_wmb();
  861. vcpu->kvm->tlbs_dirty++;
  862. continue;
  863. }
  864. gfn = gpte_to_gfn(gpte);
  865. pte_access = sp->role.access;
  866. pte_access &= FNAME(gpte_access)(gpte);
  867. FNAME(protect_clean_gpte)(&vcpu->arch.mmu, &pte_access, gpte);
  868. if (sync_mmio_spte(vcpu, &sp->spt[i], gfn, pte_access,
  869. &nr_present))
  870. continue;
  871. if (gfn != sp->gfns[i]) {
  872. drop_spte(vcpu->kvm, &sp->spt[i]);
  873. /*
  874. * The same as above where we are doing
  875. * prefetch_invalid_gpte().
  876. */
  877. smp_wmb();
  878. vcpu->kvm->tlbs_dirty++;
  879. continue;
  880. }
  881. nr_present++;
  882. host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE;
  883. set_spte_ret |= set_spte(vcpu, &sp->spt[i],
  884. pte_access, PT_PAGE_TABLE_LEVEL,
  885. gfn, spte_to_pfn(sp->spt[i]),
  886. true, false, host_writable);
  887. }
  888. if (set_spte_ret & SET_SPTE_NEED_REMOTE_TLB_FLUSH)
  889. kvm_flush_remote_tlbs(vcpu->kvm);
  890. return nr_present;
  891. }
  892. #undef pt_element_t
  893. #undef guest_walker
  894. #undef FNAME
  895. #undef PT_BASE_ADDR_MASK
  896. #undef PT_INDEX
  897. #undef PT_LVL_ADDR_MASK
  898. #undef PT_LVL_OFFSET_MASK
  899. #undef PT_LEVEL_BITS
  900. #undef PT_MAX_FULL_LEVELS
  901. #undef gpte_to_gfn
  902. #undef gpte_to_gfn_lvl
  903. #undef CMPXCHG
  904. #undef PT_GUEST_ACCESSED_MASK
  905. #undef PT_GUEST_DIRTY_MASK
  906. #undef PT_GUEST_DIRTY_SHIFT
  907. #undef PT_GUEST_ACCESSED_SHIFT
  908. #undef PT_HAVE_ACCESSED_DIRTY