paging_tmpl.h 28 KB

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