spte.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel-based Virtual Machine driver for Linux
  4. *
  5. * Macros and functions to access KVM PTEs (also known as SPTEs)
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2020 Red Hat, Inc. and/or its affiliates.
  9. */
  10. #include <linux/kvm_host.h>
  11. #include "mmu.h"
  12. #include "mmu_internal.h"
  13. #include "x86.h"
  14. #include "spte.h"
  15. #include <asm/e820/api.h>
  16. #include <asm/memtype.h>
  17. #include <asm/vmx.h>
  18. bool __read_mostly enable_mmio_caching = true;
  19. static bool __ro_after_init allow_mmio_caching;
  20. module_param_named(mmio_caching, enable_mmio_caching, bool, 0444);
  21. EXPORT_SYMBOL_GPL(enable_mmio_caching);
  22. u64 __read_mostly shadow_host_writable_mask;
  23. u64 __read_mostly shadow_mmu_writable_mask;
  24. u64 __read_mostly shadow_nx_mask;
  25. u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
  26. u64 __read_mostly shadow_user_mask;
  27. u64 __read_mostly shadow_accessed_mask;
  28. u64 __read_mostly shadow_dirty_mask;
  29. u64 __read_mostly shadow_mmio_value;
  30. u64 __read_mostly shadow_mmio_mask;
  31. u64 __read_mostly shadow_mmio_access_mask;
  32. u64 __read_mostly shadow_present_mask;
  33. u64 __read_mostly shadow_memtype_mask;
  34. u64 __read_mostly shadow_me_value;
  35. u64 __read_mostly shadow_me_mask;
  36. u64 __read_mostly shadow_acc_track_mask;
  37. u64 __read_mostly shadow_nonpresent_or_rsvd_mask;
  38. u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;
  39. u8 __read_mostly shadow_phys_bits;
  40. void __init kvm_mmu_spte_module_init(void)
  41. {
  42. /*
  43. * Snapshot userspace's desire to allow MMIO caching. Whether or not
  44. * KVM can actually enable MMIO caching depends on vendor-specific
  45. * hardware capabilities and other module params that can't be resolved
  46. * until the vendor module is loaded, i.e. enable_mmio_caching can and
  47. * will change when the vendor module is (re)loaded.
  48. */
  49. allow_mmio_caching = enable_mmio_caching;
  50. }
  51. static u64 generation_mmio_spte_mask(u64 gen)
  52. {
  53. u64 mask;
  54. WARN_ON(gen & ~MMIO_SPTE_GEN_MASK);
  55. mask = (gen << MMIO_SPTE_GEN_LOW_SHIFT) & MMIO_SPTE_GEN_LOW_MASK;
  56. mask |= (gen << MMIO_SPTE_GEN_HIGH_SHIFT) & MMIO_SPTE_GEN_HIGH_MASK;
  57. return mask;
  58. }
  59. u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)
  60. {
  61. u64 gen = kvm_vcpu_memslots(vcpu)->generation & MMIO_SPTE_GEN_MASK;
  62. u64 spte = generation_mmio_spte_mask(gen);
  63. u64 gpa = gfn << PAGE_SHIFT;
  64. WARN_ON_ONCE(!shadow_mmio_value);
  65. access &= shadow_mmio_access_mask;
  66. spte |= shadow_mmio_value | access;
  67. spte |= gpa | shadow_nonpresent_or_rsvd_mask;
  68. spte |= (gpa & shadow_nonpresent_or_rsvd_mask)
  69. << SHADOW_NONPRESENT_OR_RSVD_MASK_LEN;
  70. return spte;
  71. }
  72. static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
  73. {
  74. if (pfn_valid(pfn))
  75. return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn)) &&
  76. /*
  77. * Some reserved pages, such as those from NVDIMM
  78. * DAX devices, are not for MMIO, and can be mapped
  79. * with cached memory type for better performance.
  80. * However, the above check misconceives those pages
  81. * as MMIO, and results in KVM mapping them with UC
  82. * memory type, which would hurt the performance.
  83. * Therefore, we check the host memory type in addition
  84. * and only treat UC/UC-/WC pages as MMIO.
  85. */
  86. (!pat_enabled() || pat_pfn_immune_to_uc_mtrr(pfn));
  87. return !e820__mapped_raw_any(pfn_to_hpa(pfn),
  88. pfn_to_hpa(pfn + 1) - 1,
  89. E820_TYPE_RAM);
  90. }
  91. /*
  92. * Returns true if the SPTE has bits that may be set without holding mmu_lock.
  93. * The caller is responsible for checking if the SPTE is shadow-present, and
  94. * for determining whether or not the caller cares about non-leaf SPTEs.
  95. */
  96. bool spte_has_volatile_bits(u64 spte)
  97. {
  98. /*
  99. * Always atomically update spte if it can be updated
  100. * out of mmu-lock, it can ensure dirty bit is not lost,
  101. * also, it can help us to get a stable is_writable_pte()
  102. * to ensure tlb flush is not missed.
  103. */
  104. if (!is_writable_pte(spte) && is_mmu_writable_spte(spte))
  105. return true;
  106. if (is_access_track_spte(spte))
  107. return true;
  108. if (spte_ad_enabled(spte)) {
  109. if (!(spte & shadow_accessed_mask) ||
  110. (is_writable_pte(spte) && !(spte & shadow_dirty_mask)))
  111. return true;
  112. }
  113. return false;
  114. }
  115. bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  116. const struct kvm_memory_slot *slot,
  117. unsigned int pte_access, gfn_t gfn, kvm_pfn_t pfn,
  118. u64 old_spte, bool prefetch, bool can_unsync,
  119. bool host_writable, u64 *new_spte)
  120. {
  121. int level = sp->role.level;
  122. u64 spte = SPTE_MMU_PRESENT_MASK;
  123. bool wrprot = false;
  124. WARN_ON_ONCE(!pte_access && !shadow_present_mask);
  125. if (sp->role.ad_disabled)
  126. spte |= SPTE_TDP_AD_DISABLED_MASK;
  127. else if (kvm_mmu_page_ad_need_write_protect(sp))
  128. spte |= SPTE_TDP_AD_WRPROT_ONLY_MASK;
  129. /*
  130. * For the EPT case, shadow_present_mask is 0 if hardware
  131. * supports exec-only page table entries. In that case,
  132. * ACC_USER_MASK and shadow_user_mask are used to represent
  133. * read access. See FNAME(gpte_access) in paging_tmpl.h.
  134. */
  135. spte |= shadow_present_mask;
  136. if (!prefetch)
  137. spte |= spte_shadow_accessed_mask(spte);
  138. if (level > PG_LEVEL_4K && (pte_access & ACC_EXEC_MASK) &&
  139. is_nx_huge_page_enabled(vcpu->kvm)) {
  140. pte_access &= ~ACC_EXEC_MASK;
  141. }
  142. if (pte_access & ACC_EXEC_MASK)
  143. spte |= shadow_x_mask;
  144. else
  145. spte |= shadow_nx_mask;
  146. if (pte_access & ACC_USER_MASK)
  147. spte |= shadow_user_mask;
  148. if (level > PG_LEVEL_4K)
  149. spte |= PT_PAGE_SIZE_MASK;
  150. if (shadow_memtype_mask)
  151. spte |= static_call(kvm_x86_get_mt_mask)(vcpu, gfn,
  152. kvm_is_mmio_pfn(pfn));
  153. if (host_writable)
  154. spte |= shadow_host_writable_mask;
  155. else
  156. pte_access &= ~ACC_WRITE_MASK;
  157. if (shadow_me_value && !kvm_is_mmio_pfn(pfn))
  158. spte |= shadow_me_value;
  159. spte |= (u64)pfn << PAGE_SHIFT;
  160. if (pte_access & ACC_WRITE_MASK) {
  161. spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask;
  162. /*
  163. * Optimization: for pte sync, if spte was writable the hash
  164. * lookup is unnecessary (and expensive). Write protection
  165. * is responsibility of kvm_mmu_get_page / kvm_mmu_sync_roots.
  166. * Same reasoning can be applied to dirty page accounting.
  167. */
  168. if (is_writable_pte(old_spte))
  169. goto out;
  170. /*
  171. * Unsync shadow pages that are reachable by the new, writable
  172. * SPTE. Write-protect the SPTE if the page can't be unsync'd,
  173. * e.g. it's write-tracked (upper-level SPs) or has one or more
  174. * shadow pages and unsync'ing pages is not allowed.
  175. */
  176. if (mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, can_unsync, prefetch)) {
  177. pgprintk("%s: found shadow page for %llx, marking ro\n",
  178. __func__, gfn);
  179. wrprot = true;
  180. pte_access &= ~ACC_WRITE_MASK;
  181. spte &= ~(PT_WRITABLE_MASK | shadow_mmu_writable_mask);
  182. }
  183. }
  184. if (pte_access & ACC_WRITE_MASK)
  185. spte |= spte_shadow_dirty_mask(spte);
  186. out:
  187. if (prefetch)
  188. spte = mark_spte_for_access_track(spte);
  189. WARN_ONCE(is_rsvd_spte(&vcpu->arch.mmu->shadow_zero_check, spte, level),
  190. "spte = 0x%llx, level = %d, rsvd bits = 0x%llx", spte, level,
  191. get_rsvd_bits(&vcpu->arch.mmu->shadow_zero_check, spte, level));
  192. if ((spte & PT_WRITABLE_MASK) && kvm_slot_dirty_track_enabled(slot)) {
  193. /* Enforced by kvm_mmu_hugepage_adjust. */
  194. WARN_ON(level > PG_LEVEL_4K);
  195. mark_page_dirty_in_slot(vcpu->kvm, slot, gfn);
  196. }
  197. *new_spte = spte;
  198. return wrprot;
  199. }
  200. static u64 make_spte_executable(u64 spte)
  201. {
  202. bool is_access_track = is_access_track_spte(spte);
  203. if (is_access_track)
  204. spte = restore_acc_track_spte(spte);
  205. spte &= ~shadow_nx_mask;
  206. spte |= shadow_x_mask;
  207. if (is_access_track)
  208. spte = mark_spte_for_access_track(spte);
  209. return spte;
  210. }
  211. /*
  212. * Construct an SPTE that maps a sub-page of the given huge page SPTE where
  213. * `index` identifies which sub-page.
  214. *
  215. * This is used during huge page splitting to build the SPTEs that make up the
  216. * new page table.
  217. */
  218. u64 make_huge_page_split_spte(struct kvm *kvm, u64 huge_spte, union kvm_mmu_page_role role,
  219. int index)
  220. {
  221. u64 child_spte;
  222. if (WARN_ON_ONCE(!is_shadow_present_pte(huge_spte)))
  223. return 0;
  224. if (WARN_ON_ONCE(!is_large_pte(huge_spte)))
  225. return 0;
  226. child_spte = huge_spte;
  227. /*
  228. * The child_spte already has the base address of the huge page being
  229. * split. So we just have to OR in the offset to the page at the next
  230. * lower level for the given index.
  231. */
  232. child_spte |= (index * KVM_PAGES_PER_HPAGE(role.level)) << PAGE_SHIFT;
  233. if (role.level == PG_LEVEL_4K) {
  234. child_spte &= ~PT_PAGE_SIZE_MASK;
  235. /*
  236. * When splitting to a 4K page where execution is allowed, mark
  237. * the page executable as the NX hugepage mitigation no longer
  238. * applies.
  239. */
  240. if ((role.access & ACC_EXEC_MASK) && is_nx_huge_page_enabled(kvm))
  241. child_spte = make_spte_executable(child_spte);
  242. }
  243. return child_spte;
  244. }
  245. u64 make_nonleaf_spte(u64 *child_pt, bool ad_disabled)
  246. {
  247. u64 spte = SPTE_MMU_PRESENT_MASK;
  248. spte |= __pa(child_pt) | shadow_present_mask | PT_WRITABLE_MASK |
  249. shadow_user_mask | shadow_x_mask | shadow_me_value;
  250. if (ad_disabled)
  251. spte |= SPTE_TDP_AD_DISABLED_MASK;
  252. else
  253. spte |= shadow_accessed_mask;
  254. return spte;
  255. }
  256. u64 kvm_mmu_changed_pte_notifier_make_spte(u64 old_spte, kvm_pfn_t new_pfn)
  257. {
  258. u64 new_spte;
  259. new_spte = old_spte & ~SPTE_BASE_ADDR_MASK;
  260. new_spte |= (u64)new_pfn << PAGE_SHIFT;
  261. new_spte &= ~PT_WRITABLE_MASK;
  262. new_spte &= ~shadow_host_writable_mask;
  263. new_spte &= ~shadow_mmu_writable_mask;
  264. new_spte = mark_spte_for_access_track(new_spte);
  265. return new_spte;
  266. }
  267. u64 mark_spte_for_access_track(u64 spte)
  268. {
  269. if (spte_ad_enabled(spte))
  270. return spte & ~shadow_accessed_mask;
  271. if (is_access_track_spte(spte))
  272. return spte;
  273. check_spte_writable_invariants(spte);
  274. WARN_ONCE(spte & (SHADOW_ACC_TRACK_SAVED_BITS_MASK <<
  275. SHADOW_ACC_TRACK_SAVED_BITS_SHIFT),
  276. "kvm: Access Tracking saved bit locations are not zero\n");
  277. spte |= (spte & SHADOW_ACC_TRACK_SAVED_BITS_MASK) <<
  278. SHADOW_ACC_TRACK_SAVED_BITS_SHIFT;
  279. spte &= ~shadow_acc_track_mask;
  280. return spte;
  281. }
  282. void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask)
  283. {
  284. BUG_ON((u64)(unsigned)access_mask != access_mask);
  285. WARN_ON(mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);
  286. /*
  287. * Reset to the original module param value to honor userspace's desire
  288. * to (dis)allow MMIO caching. Update the param itself so that
  289. * userspace can see whether or not KVM is actually using MMIO caching.
  290. */
  291. enable_mmio_caching = allow_mmio_caching;
  292. if (!enable_mmio_caching)
  293. mmio_value = 0;
  294. /*
  295. * The mask must contain only bits that are carved out specifically for
  296. * the MMIO SPTE mask, e.g. to ensure there's no overlap with the MMIO
  297. * generation.
  298. */
  299. if (WARN_ON(mmio_mask & ~SPTE_MMIO_ALLOWED_MASK))
  300. mmio_value = 0;
  301. /*
  302. * Disable MMIO caching if the MMIO value collides with the bits that
  303. * are used to hold the relocated GFN when the L1TF mitigation is
  304. * enabled. This should never fire as there is no known hardware that
  305. * can trigger this condition, e.g. SME/SEV CPUs that require a custom
  306. * MMIO value are not susceptible to L1TF.
  307. */
  308. if (WARN_ON(mmio_value & (shadow_nonpresent_or_rsvd_mask <<
  309. SHADOW_NONPRESENT_OR_RSVD_MASK_LEN)))
  310. mmio_value = 0;
  311. /*
  312. * The masked MMIO value must obviously match itself and a removed SPTE
  313. * must not get a false positive. Removed SPTEs and MMIO SPTEs should
  314. * never collide as MMIO must set some RWX bits, and removed SPTEs must
  315. * not set any RWX bits.
  316. */
  317. if (WARN_ON((mmio_value & mmio_mask) != mmio_value) ||
  318. WARN_ON(mmio_value && (REMOVED_SPTE & mmio_mask) == mmio_value))
  319. mmio_value = 0;
  320. if (!mmio_value)
  321. enable_mmio_caching = false;
  322. shadow_mmio_value = mmio_value;
  323. shadow_mmio_mask = mmio_mask;
  324. shadow_mmio_access_mask = access_mask;
  325. }
  326. EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
  327. void kvm_mmu_set_me_spte_mask(u64 me_value, u64 me_mask)
  328. {
  329. /* shadow_me_value must be a subset of shadow_me_mask */
  330. if (WARN_ON(me_value & ~me_mask))
  331. me_value = me_mask = 0;
  332. shadow_me_value = me_value;
  333. shadow_me_mask = me_mask;
  334. }
  335. EXPORT_SYMBOL_GPL(kvm_mmu_set_me_spte_mask);
  336. void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only)
  337. {
  338. shadow_user_mask = VMX_EPT_READABLE_MASK;
  339. shadow_accessed_mask = has_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull;
  340. shadow_dirty_mask = has_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull;
  341. shadow_nx_mask = 0ull;
  342. shadow_x_mask = VMX_EPT_EXECUTABLE_MASK;
  343. shadow_present_mask = has_exec_only ? 0ull : VMX_EPT_READABLE_MASK;
  344. /*
  345. * EPT overrides the host MTRRs, and so KVM must program the desired
  346. * memtype directly into the SPTEs. Note, this mask is just the mask
  347. * of all bits that factor into the memtype, the actual memtype must be
  348. * dynamically calculated, e.g. to ensure host MMIO is mapped UC.
  349. */
  350. shadow_memtype_mask = VMX_EPT_MT_MASK | VMX_EPT_IPAT_BIT;
  351. shadow_acc_track_mask = VMX_EPT_RWX_MASK;
  352. shadow_host_writable_mask = EPT_SPTE_HOST_WRITABLE;
  353. shadow_mmu_writable_mask = EPT_SPTE_MMU_WRITABLE;
  354. /*
  355. * EPT Misconfigurations are generated if the value of bits 2:0
  356. * of an EPT paging-structure entry is 110b (write/execute).
  357. */
  358. kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE,
  359. VMX_EPT_RWX_MASK, 0);
  360. }
  361. EXPORT_SYMBOL_GPL(kvm_mmu_set_ept_masks);
  362. void kvm_mmu_reset_all_pte_masks(void)
  363. {
  364. u8 low_phys_bits;
  365. u64 mask;
  366. shadow_phys_bits = kvm_get_shadow_phys_bits();
  367. /*
  368. * If the CPU has 46 or less physical address bits, then set an
  369. * appropriate mask to guard against L1TF attacks. Otherwise, it is
  370. * assumed that the CPU is not vulnerable to L1TF.
  371. *
  372. * Some Intel CPUs address the L1 cache using more PA bits than are
  373. * reported by CPUID. Use the PA width of the L1 cache when possible
  374. * to achieve more effective mitigation, e.g. if system RAM overlaps
  375. * the most significant bits of legal physical address space.
  376. */
  377. shadow_nonpresent_or_rsvd_mask = 0;
  378. low_phys_bits = boot_cpu_data.x86_phys_bits;
  379. if (boot_cpu_has_bug(X86_BUG_L1TF) &&
  380. !WARN_ON_ONCE(boot_cpu_data.x86_cache_bits >=
  381. 52 - SHADOW_NONPRESENT_OR_RSVD_MASK_LEN)) {
  382. low_phys_bits = boot_cpu_data.x86_cache_bits
  383. - SHADOW_NONPRESENT_OR_RSVD_MASK_LEN;
  384. shadow_nonpresent_or_rsvd_mask =
  385. rsvd_bits(low_phys_bits, boot_cpu_data.x86_cache_bits - 1);
  386. }
  387. shadow_nonpresent_or_rsvd_lower_gfn_mask =
  388. GENMASK_ULL(low_phys_bits - 1, PAGE_SHIFT);
  389. shadow_user_mask = PT_USER_MASK;
  390. shadow_accessed_mask = PT_ACCESSED_MASK;
  391. shadow_dirty_mask = PT_DIRTY_MASK;
  392. shadow_nx_mask = PT64_NX_MASK;
  393. shadow_x_mask = 0;
  394. shadow_present_mask = PT_PRESENT_MASK;
  395. /*
  396. * For shadow paging and NPT, KVM uses PAT entry '0' to encode WB
  397. * memtype in the SPTEs, i.e. relies on host MTRRs to provide the
  398. * correct memtype (WB is the "weakest" memtype).
  399. */
  400. shadow_memtype_mask = 0;
  401. shadow_acc_track_mask = 0;
  402. shadow_me_mask = 0;
  403. shadow_me_value = 0;
  404. shadow_host_writable_mask = DEFAULT_SPTE_HOST_WRITABLE;
  405. shadow_mmu_writable_mask = DEFAULT_SPTE_MMU_WRITABLE;
  406. /*
  407. * Set a reserved PA bit in MMIO SPTEs to generate page faults with
  408. * PFEC.RSVD=1 on MMIO accesses. 64-bit PTEs (PAE, x86-64, and EPT
  409. * paging) support a maximum of 52 bits of PA, i.e. if the CPU supports
  410. * 52-bit physical addresses then there are no reserved PA bits in the
  411. * PTEs and so the reserved PA approach must be disabled.
  412. */
  413. if (shadow_phys_bits < 52)
  414. mask = BIT_ULL(51) | PT_PRESENT_MASK;
  415. else
  416. mask = 0;
  417. kvm_mmu_set_mmio_spte_mask(mask, mask, ACC_WRITE_MASK | ACC_USER_MASK);
  418. }