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_mmu_page * sp)190 static inline bool kvm_mmu_page_ad_need_write_protect(struct kvm_mmu_page *sp)
191 {
192 /*
193 * When using the EPT page-modification log, the GPAs in the CPU dirty
194 * log would come from L2 rather than L1. Therefore, we need to rely
195 * on write protection to record dirty pages, which bypasses PML, since
196 * writes now result in a vmexit. Note, the check on CPU dirty logging
197 * being enabled is mandatory as the bits used to denote WP-only SPTEs
198 * are reserved for PAE paging (32-bit KVM).
199 */
200 return kvm_x86_ops.cpu_dirty_log_size && sp->role.guest_mode;
201 }
202
gfn_round_for_level(gfn_t gfn,int level)203 static inline gfn_t gfn_round_for_level(gfn_t gfn, int level)
204 {
205 return gfn & -KVM_PAGES_PER_HPAGE(level);
206 }
207
208 int mmu_try_to_unsync_pages(struct kvm *kvm, const struct kvm_memory_slot *slot,
209 gfn_t gfn, bool synchronizing, bool prefetch);
210
211 void kvm_mmu_gfn_disallow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn);
212 void kvm_mmu_gfn_allow_lpage(const struct kvm_memory_slot *slot, gfn_t gfn);
213 bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
214 struct kvm_memory_slot *slot, u64 gfn,
215 int min_level);
216
217 /* Flush the given page (huge or not) of guest memory. */
kvm_flush_remote_tlbs_gfn(struct kvm * kvm,gfn_t gfn,int level)218 static inline void kvm_flush_remote_tlbs_gfn(struct kvm *kvm, gfn_t gfn, int level)
219 {
220 kvm_flush_remote_tlbs_range(kvm, gfn_round_for_level(gfn, level),
221 KVM_PAGES_PER_HPAGE(level));
222 }
223
224 unsigned int pte_list_count(struct kvm_rmap_head *rmap_head);
225
226 extern int nx_huge_pages;
is_nx_huge_page_enabled(struct kvm * kvm)227 static inline bool is_nx_huge_page_enabled(struct kvm *kvm)
228 {
229 return READ_ONCE(nx_huge_pages) && !kvm->arch.disable_nx_huge_pages;
230 }
231
232 struct kvm_page_fault {
233 /* arguments to kvm_mmu_do_page_fault. */
234 const gpa_t addr;
235 const u64 error_code;
236 const bool prefetch;
237
238 /* Derived from error_code. */
239 const bool exec;
240 const bool write;
241 const bool present;
242 const bool rsvd;
243 const bool user;
244
245 /* Derived from mmu and global state. */
246 const bool is_tdp;
247 const bool is_private;
248 const bool nx_huge_page_workaround_enabled;
249
250 /*
251 * Whether a >4KB mapping can be created or is forbidden due to NX
252 * hugepages.
253 */
254 bool huge_page_disallowed;
255
256 /*
257 * Maximum page size that can be created for this fault; input to
258 * FNAME(fetch), direct_map() and kvm_tdp_mmu_map().
259 */
260 u8 max_level;
261
262 /*
263 * Page size that can be created based on the max_level and the
264 * page size used by the host mapping.
265 */
266 u8 req_level;
267
268 /*
269 * Page size that will be created based on the req_level and
270 * huge_page_disallowed.
271 */
272 u8 goal_level;
273
274 /*
275 * Shifted addr, or result of guest page table walk if addr is a gva. In
276 * the case of VM where memslot's can be mapped at multiple GPA aliases
277 * (i.e. TDX), the gfn field does not contain the bit that selects between
278 * the aliases (i.e. the shared bit for TDX).
279 */
280 gfn_t gfn;
281
282 /* The memslot containing gfn. May be NULL. */
283 struct kvm_memory_slot *slot;
284
285 /* Outputs of kvm_mmu_faultin_pfn(). */
286 unsigned long mmu_seq;
287 kvm_pfn_t pfn;
288 struct page *refcounted_page;
289 bool map_writable;
290
291 /*
292 * Indicates the guest is trying to write a gfn that contains one or
293 * more of the PTEs used to translate the write itself, i.e. the access
294 * is changing its own translation in the guest page tables.
295 */
296 bool write_fault_to_shadow_pgtable;
297 };
298
299 int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
300
301 /*
302 * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(),
303 * and of course kvm_mmu_do_page_fault().
304 *
305 * RET_PF_CONTINUE: So far, so good, keep handling the page fault.
306 * RET_PF_RETRY: let CPU fault again on the address.
307 * RET_PF_EMULATE: mmio page fault, emulate the instruction directly.
308 * RET_PF_WRITE_PROTECTED: the gfn is write-protected, either unprotected the
309 * gfn and retry, or emulate the instruction directly.
310 * RET_PF_INVALID: the spte is invalid, let the real page fault path update it.
311 * RET_PF_FIXED: The faulting entry has been fixed.
312 * RET_PF_SPURIOUS: The faulting entry was already fixed, e.g. by another vCPU.
313 *
314 * Any names added to this enum should be exported to userspace for use in
315 * tracepoints via TRACE_DEFINE_ENUM() in mmutrace.h
316 *
317 * Note, all values must be greater than or equal to zero so as not to encroach
318 * on -errno return values.
319 */
320 enum {
321 RET_PF_CONTINUE = 0,
322 RET_PF_RETRY,
323 RET_PF_EMULATE,
324 RET_PF_WRITE_PROTECTED,
325 RET_PF_INVALID,
326 RET_PF_FIXED,
327 RET_PF_SPURIOUS,
328 };
329
330 /*
331 * Define RET_PF_CONTINUE as 0 to allow for
332 * - efficient machine code when checking for CONTINUE, e.g.
333 * "TEST %rax, %rax, JNZ", as all "stop!" values are non-zero,
334 * - kvm_mmu_do_page_fault() to return other RET_PF_* as a positive value.
335 */
336 static_assert(RET_PF_CONTINUE == 0);
337
kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu * vcpu,struct kvm_page_fault * fault)338 static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
339 struct kvm_page_fault *fault)
340 {
341 kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT,
342 PAGE_SIZE, fault->write, fault->exec,
343 fault->is_private);
344 }
345
kvm_mmu_do_page_fault(struct kvm_vcpu * vcpu,gpa_t cr2_or_gpa,u64 err,bool prefetch,int * emulation_type,u8 * level)346 static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
347 u64 err, bool prefetch,
348 int *emulation_type, u8 *level)
349 {
350 struct kvm_page_fault fault = {
351 .addr = cr2_or_gpa,
352 .error_code = err,
353 .exec = err & PFERR_FETCH_MASK,
354 .write = err & PFERR_WRITE_MASK,
355 .present = err & PFERR_PRESENT_MASK,
356 .rsvd = err & PFERR_RSVD_MASK,
357 .user = err & PFERR_USER_MASK,
358 .prefetch = prefetch,
359 .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault),
360 .nx_huge_page_workaround_enabled =
361 is_nx_huge_page_enabled(vcpu->kvm),
362
363 .max_level = KVM_MAX_HUGEPAGE_LEVEL,
364 .req_level = PG_LEVEL_4K,
365 .goal_level = PG_LEVEL_4K,
366 .is_private = err & PFERR_PRIVATE_ACCESS,
367
368 .pfn = KVM_PFN_ERR_FAULT,
369 };
370 int r;
371
372 if (vcpu->arch.mmu->root_role.direct) {
373 /*
374 * Things like memslots don't understand the concept of a shared
375 * bit. Strip it so that the GFN can be used like normal, and the
376 * fault.addr can be used when the shared bit is needed.
377 */
378 fault.gfn = gpa_to_gfn(fault.addr) & ~kvm_gfn_direct_bits(vcpu->kvm);
379 fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
380 }
381
382 /*
383 * With retpoline being active an indirect call is rather expensive,
384 * so do a direct call in the most common case.
385 */
386 if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && fault.is_tdp)
387 r = kvm_tdp_page_fault(vcpu, &fault);
388 else
389 r = vcpu->arch.mmu->page_fault(vcpu, &fault);
390
391 /*
392 * Not sure what's happening, but punt to userspace and hope that
393 * they can fix it by changing memory to shared, or they can
394 * provide a better error.
395 */
396 if (r == RET_PF_EMULATE && fault.is_private) {
397 pr_warn_ratelimited("kvm: unexpected emulation request on private memory\n");
398 kvm_mmu_prepare_memory_fault_exit(vcpu, &fault);
399 return -EFAULT;
400 }
401
402 if (fault.write_fault_to_shadow_pgtable && emulation_type)
403 *emulation_type |= EMULTYPE_WRITE_PF_TO_SP;
404 if (level)
405 *level = fault.goal_level;
406
407 return r;
408 }
409
410 int kvm_mmu_max_mapping_level(struct kvm *kvm,
411 const struct kvm_memory_slot *slot, gfn_t gfn);
412 void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
413 void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_level);
414
415 void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
416 void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
417
418 #endif /* __KVM_X86_MMU_INTERNAL_H */
419