1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_MMU_INTERNAL_H
3 #define __KVM_X86_MMU_INTERNAL_H
4
5 #include <linux/types.h>
6 #include <linux/kvm_host.h>
7 #include <asm/kvm_host.h>
8
9 #include "mmu.h"
10
11 #ifdef CONFIG_KVM_PROVE_MMU
12 #define KVM_MMU_WARN_ON(x) WARN_ON_ONCE(x)
13 #else
14 #define KVM_MMU_WARN_ON(x) BUILD_BUG_ON_INVALID(x)
15 #endif
16
17 /* Page table builder macros common to shadow (host) PTEs and guest PTEs. */
18 #define __PT_BASE_ADDR_MASK GENMASK_ULL(51, 12)
19 #define __PT_LEVEL_SHIFT(level, bits_per_level) \
20 (PAGE_SHIFT + ((level) - 1) * (bits_per_level))
21 #define __PT_INDEX(address, level, bits_per_level) \
22 (((address) >> __PT_LEVEL_SHIFT(level, bits_per_level)) & ((1 << (bits_per_level)) - 1))
23
24 #define __PT_LVL_ADDR_MASK(base_addr_mask, level, bits_per_level) \
25 ((base_addr_mask) & ~((1ULL << (PAGE_SHIFT + (((level) - 1) * (bits_per_level)))) - 1))
26
27 #define __PT_LVL_OFFSET_MASK(base_addr_mask, level, bits_per_level) \
28 ((base_addr_mask) & ((1ULL << (PAGE_SHIFT + (((level) - 1) * (bits_per_level)))) - 1))
29
30 #define __PT_ENT_PER_PAGE(bits_per_level) (1 << (bits_per_level))
31
32 /*
33 * Unlike regular MMU roots, PAE "roots", a.k.a. PDPTEs/PDPTRs, have a PRESENT
34 * bit, and thus are guaranteed to be non-zero when valid. And, when a guest
35 * PDPTR is !PRESENT, its corresponding PAE root cannot be set to INVALID_PAGE,
36 * as the CPU would treat that as PRESENT PDPTR with reserved bits set. Use
37 * '0' instead of INVALID_PAGE to indicate an invalid PAE root.
38 */
39 #define INVALID_PAE_ROOT 0
40 #define IS_VALID_PAE_ROOT(x) (!!(x))
41
kvm_mmu_get_dummy_root(void)42 static inline hpa_t kvm_mmu_get_dummy_root(void)
43 {
44 return my_zero_pfn(0) << PAGE_SHIFT;
45 }
46
kvm_mmu_is_dummy_root(hpa_t shadow_page)47 static inline bool kvm_mmu_is_dummy_root(hpa_t shadow_page)
48 {
49 return is_zero_pfn(shadow_page >> PAGE_SHIFT);
50 }
51
52 typedef u64 __rcu *tdp_ptep_t;
53
54 struct kvm_mmu_page {
55 /*
56 * Note, "link" through "spt" fit in a single 64 byte cache line on
57 * 64-bit kernels, keep it that way unless there's a reason not to.
58 */
59 struct list_head link;
60 struct hlist_node hash_link;
61
62 bool tdp_mmu_page;
63 bool unsync;
64 union {
65 u8 mmu_valid_gen;
66
67 /* Only accessed under slots_lock. */
68 bool tdp_mmu_scheduled_root_to_zap;
69 };
70
71 /*
72 * The shadow page can't be replaced by an equivalent huge page
73 * because it is being used to map an executable page in the guest
74 * and the NX huge page mitigation is enabled.
75 */
76 bool nx_huge_page_disallowed;
77
78 /*
79 * The following two entries are used to key the shadow page in the
80 * hash table.
81 */
82 union kvm_mmu_page_role role;
83 gfn_t gfn;
84
85 u64 *spt;
86
87 /*
88 * Stores the result of the guest translation being shadowed by each
89 * SPTE. KVM shadows two types of guest translations: nGPA -> GPA
90 * (shadow EPT/NPT) and GVA -> GPA (traditional shadow paging). In both
91 * cases the result of the translation is a GPA and a set of access
92 * constraints.
93 *
94 * The GFN is stored in the upper bits (PAGE_SHIFT) and the shadowed
95 * access permissions are stored in the lower bits. Note, for
96 * convenience and uniformity across guests, the access permissions are
97 * stored in KVM format (e.g. ACC_EXEC_MASK) not the raw guest format.
98 */
99 u64 *shadowed_translation;
100
101 /* Currently serving as active root */
102 union {
103 int root_count;
104 refcount_t tdp_mmu_root_count;
105 };
106 union {
107 /* These two members aren't used for TDP MMU */
108 struct {
109 unsigned int unsync_children;
110 /*
111 * Number of writes since the last time traversal
112 * visited this page.
113 */
114 atomic_t write_flooding_count;
115 };
116 /*
117 * Page table page of external PT.
118 * Passed to TDX module, not accessed by KVM.
119 */
120 void *external_spt;
121 };
122 union {
123 struct kvm_rmap_head parent_ptes; /* rmap pointers to parent sptes */
124 tdp_ptep_t ptep;
125 };
126 DECLARE_BITMAP(unsync_child_bitmap, 512);
127
128 /*
129 * Tracks shadow pages that, if zapped, would allow KVM to create an NX
130 * huge page. A shadow page will have nx_huge_page_disallowed set but
131 * not be on the list if a huge page is disallowed for other reasons,
132 * e.g. because KVM is shadowing a PTE at the same gfn, the memslot
133 * isn't properly aligned, etc...
134 */
135 struct list_head possible_nx_huge_page_link;
136 #ifdef CONFIG_X86_32
137 /*
138 * Used out of the mmu-lock to avoid reading spte values while an
139 * update is in progress; see the comments in __get_spte_lockless().
140 */
141 int clear_spte_count;
142 #endif
143
144 #ifdef CONFIG_X86_64
145 /* Used for freeing the page asynchronously if it is a TDP MMU page. */
146 struct rcu_head rcu_head;
147 #endif
148 };
149
150 extern struct kmem_cache *mmu_page_header_cache;
151
kvm_mmu_role_as_id(union kvm_mmu_page_role role)152 static inline int kvm_mmu_role_as_id(union kvm_mmu_page_role role)
153 {
154 return role.smm ? 1 : 0;
155 }
156
kvm_mmu_page_as_id(struct kvm_mmu_page * sp)157 static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)
158 {
159 return kvm_mmu_role_as_id(sp->role);
160 }
161
is_mirror_sp(const struct kvm_mmu_page * sp)162 static inline bool is_mirror_sp(const struct kvm_mmu_page *sp)
163 {
164 return sp->role.is_mirror;
165 }
166
kvm_mmu_alloc_external_spt(struct kvm_vcpu * vcpu,struct kvm_mmu_page * sp)167 static inline void kvm_mmu_alloc_external_spt(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
168 {
169 /*
170 * external_spt is allocated for TDX module to hold private EPT mappings,
171 * TDX module will initialize the page by itself.
172 * Therefore, KVM does not need to initialize or access external_spt.
173 * KVM only interacts with sp->spt for private EPT operations.
174 */
175 sp->external_spt = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_external_spt_cache);
176 }
177
kvm_gfn_root_bits(const struct kvm * kvm,const struct kvm_mmu_page * root)178 static inline gfn_t kvm_gfn_root_bits(const struct kvm *kvm, const struct kvm_mmu_page *root)
179 {
180 /*
181 * Since mirror SPs are used only for TDX, which maps private memory
182 * at its "natural" GFN, no mask needs to be applied to them - and, dually,
183 * we expect that the bits is only used for the shared PT.
184 */
185 if (is_mirror_sp(root))
186 return 0;
187 return kvm_gfn_direct_bits(kvm);
188 }
189
kvm_mmu_page_ad_need_write_protect(struct kvm * kvm,struct kvm_mmu_page * sp)190 static inline bool kvm_mmu_page_ad_need_write_protect(struct kvm *kvm,
191 struct kvm_mmu_page *sp)
192 {
193 /*
194 * When using the EPT page-modification log, the GPAs in the CPU dirty
195 * log would come from L2 rather than L1. Therefore, we need to rely
196 * on write protection to record dirty pages, which bypasses PML, since
197 * writes now result in a vmexit. Note, the check on CPU dirty logging
198 * being enabled is mandatory as the bits used to denote WP-only SPTEs
199 * are reserved for PAE paging (32-bit KVM).
200 */
201 return kvm->arch.cpu_dirty_log_size && sp->role.guest_mode;
202 }
203
gfn_round_for_level(gfn_t gfn,int level)204 static inline gfn_t gfn_round_for_level(gfn_t gfn, int level)
205 {
206 return gfn & -KVM_PAGES_PER_HPAGE(level);
207 }
208
209 int mmu_try_to_unsync_pages(struct kvm *kvm, const struct kvm_memory_slot *slot,
210 gfn_t gfn, bool synchronizing, bool prefetch);
211
212 void kvm_mmu_gfn_disallow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn);
213 void kvm_mmu_gfn_allow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn);
214 bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
215 struct kvm_memory_slot *slot, u64 gfn,
216 int min_level);
217
218 /* Flush the given page (huge or not) of guest memory. */
kvm_flush_remote_tlbs_gfn(struct kvm * kvm,gfn_t gfn,int level)219 static inline void kvm_flush_remote_tlbs_gfn(struct kvm *kvm, gfn_t gfn, int level)
220 {
221 kvm_flush_remote_tlbs_range(kvm, gfn_round_for_level(gfn, level),
222 KVM_PAGES_PER_HPAGE(level));
223 }
224
225 unsigned int pte_list_count(struct kvm_rmap_head *rmap_head);
226
227 extern int nx_huge_pages;
is_nx_huge_page_enabled(struct kvm * kvm)228 static inline bool is_nx_huge_page_enabled(struct kvm *kvm)
229 {
230 return READ_ONCE(nx_huge_pages) && !kvm->arch.disable_nx_huge_pages;
231 }
232
233 struct kvm_page_fault {
234 /* arguments to kvm_mmu_do_page_fault. */
235 const gpa_t addr;
236 const u64 error_code;
237 const bool prefetch;
238
239 /* Derived from error_code. */
240 const bool exec;
241 const bool write;
242 const bool present;
243 const bool rsvd;
244 const bool user;
245
246 /* Derived from mmu and global state. */
247 const bool is_tdp;
248 const bool is_private;
249 const bool nx_huge_page_workaround_enabled;
250
251 /*
252 * Whether a >4KB mapping can be created or is forbidden due to NX
253 * hugepages.
254 */
255 bool huge_page_disallowed;
256
257 /*
258 * Maximum page size that can be created for this fault; input to
259 * FNAME(fetch), direct_map() and kvm_tdp_mmu_map().
260 */
261 u8 max_level;
262
263 /*
264 * Page size that can be created based on the max_level and the
265 * page size used by the host mapping.
266 */
267 u8 req_level;
268
269 /*
270 * Page size that will be created based on the req_level and
271 * huge_page_disallowed.
272 */
273 u8 goal_level;
274
275 /*
276 * Shifted addr, or result of guest page table walk if addr is a gva. In
277 * the case of VM where memslot's can be mapped at multiple GPA aliases
278 * (i.e. TDX), the gfn field does not contain the bit that selects between
279 * the aliases (i.e. the shared bit for TDX).
280 */
281 gfn_t gfn;
282
283 /* The memslot containing gfn. May be NULL. */
284 struct kvm_memory_slot *slot;
285
286 /* Outputs of kvm_mmu_faultin_pfn(). */
287 unsigned long mmu_seq;
288 kvm_pfn_t pfn;
289 struct page *refcounted_page;
290 bool map_writable;
291
292 /*
293 * Indicates the guest is trying to write a gfn that contains one or
294 * more of the PTEs used to translate the write itself, i.e. the access
295 * is changing its own translation in the guest page tables.
296 */
297 bool write_fault_to_shadow_pgtable;
298 };
299
300 int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
301
302 /*
303 * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(),
304 * and of course kvm_mmu_do_page_fault().
305 *
306 * RET_PF_CONTINUE: So far, so good, keep handling the page fault.
307 * RET_PF_RETRY: let CPU fault again on the address.
308 * RET_PF_EMULATE: mmio page fault, emulate the instruction directly.
309 * RET_PF_WRITE_PROTECTED: the gfn is write-protected, either unprotected the
310 * gfn and retry, or emulate the instruction directly.
311 * RET_PF_INVALID: the spte is invalid, let the real page fault path update it.
312 * RET_PF_FIXED: The faulting entry has been fixed.
313 * RET_PF_SPURIOUS: The faulting entry was already fixed, e.g. by another vCPU.
314 *
315 * Any names added to this enum should be exported to userspace for use in
316 * tracepoints via TRACE_DEFINE_ENUM() in mmutrace.h
317 *
318 * Note, all values must be greater than or equal to zero so as not to encroach
319 * on -errno return values.
320 */
321 enum {
322 RET_PF_CONTINUE = 0,
323 RET_PF_RETRY,
324 RET_PF_EMULATE,
325 RET_PF_WRITE_PROTECTED,
326 RET_PF_INVALID,
327 RET_PF_FIXED,
328 RET_PF_SPURIOUS,
329 };
330
331 /*
332 * Define RET_PF_CONTINUE as 0 to allow for
333 * - efficient machine code when checking for CONTINUE, e.g.
334 * "TEST %rax, %rax, JNZ", as all "stop!" values are non-zero,
335 * - kvm_mmu_do_page_fault() to return other RET_PF_* as a positive value.
336 */
337 static_assert(RET_PF_CONTINUE == 0);
338
kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu * vcpu,struct kvm_page_fault * fault)339 static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
340 struct kvm_page_fault *fault)
341 {
342 kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT,
343 PAGE_SIZE, fault->write, fault->exec,
344 fault->is_private);
345 }
346
kvm_mmu_do_page_fault(struct kvm_vcpu * vcpu,gpa_t cr2_or_gpa,u64 err,bool prefetch,int * emulation_type,u8 * level)347 static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
348 u64 err, bool prefetch,
349 int *emulation_type, u8 *level)
350 {
351 struct kvm_page_fault fault = {
352 .addr = cr2_or_gpa,
353 .error_code = err,
354 .exec = err & PFERR_FETCH_MASK,
355 .write = err & PFERR_WRITE_MASK,
356 .present = err & PFERR_PRESENT_MASK,
357 .rsvd = err & PFERR_RSVD_MASK,
358 .user = err & PFERR_USER_MASK,
359 .prefetch = prefetch,
360 .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault),
361 .nx_huge_page_workaround_enabled =
362 is_nx_huge_page_enabled(vcpu->kvm),
363
364 .max_level = KVM_MAX_HUGEPAGE_LEVEL,
365 .req_level = PG_LEVEL_4K,
366 .goal_level = PG_LEVEL_4K,
367 .is_private = err & PFERR_PRIVATE_ACCESS,
368
369 .pfn = KVM_PFN_ERR_FAULT,
370 };
371 int r;
372
373 if (vcpu->arch.mmu->root_role.direct) {
374 /*
375 * Things like memslots don't understand the concept of a shared
376 * bit. Strip it so that the GFN can be used like normal, and the
377 * fault.addr can be used when the shared bit is needed.
378 */
379 fault.gfn = gpa_to_gfn(fault.addr) & ~kvm_gfn_direct_bits(vcpu->kvm);
380 fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
381 }
382
383 /*
384 * With retpoline being active an indirect call is rather expensive,
385 * so do a direct call in the most common case.
386 */
387 if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && fault.is_tdp)
388 r = kvm_tdp_page_fault(vcpu, &fault);
389 else
390 r = vcpu->arch.mmu->page_fault(vcpu, &fault);
391
392 /*
393 * Not sure what's happening, but punt to userspace and hope that
394 * they can fix it by changing memory to shared, or they can
395 * provide a better error.
396 */
397 if (r == RET_PF_EMULATE && fault.is_private) {
398 pr_warn_ratelimited("kvm: unexpected emulation request on private memory\n");
399 kvm_mmu_prepare_memory_fault_exit(vcpu, &fault);
400 return -EFAULT;
401 }
402
403 if (fault.write_fault_to_shadow_pgtable && emulation_type)
404 *emulation_type |= EMULTYPE_WRITE_PF_TO_SP;
405 if (level)
406 *level = fault.goal_level;
407
408 return r;
409 }
410
411 int kvm_mmu_max_mapping_level(struct kvm *kvm,
412 const struct kvm_memory_slot *slot, gfn_t gfn);
413 void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
414 void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_level);
415
416 void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
417 void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
418
419 #endif /* __KVM_X86_MMU_INTERNAL_H */
420