]>
Commit | Line | Data |
---|---|---|
6aa8b732 AK |
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. | |
9611c187 | 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
6aa8b732 AK |
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 | */ | |
e495606d | 20 | |
af585b92 | 21 | #include "irq.h" |
1d737c8a | 22 | #include "mmu.h" |
836a1b3c | 23 | #include "x86.h" |
6de4f3ad | 24 | #include "kvm_cache_regs.h" |
5f7dde7b | 25 | #include "cpuid.h" |
e495606d | 26 | |
edf88417 | 27 | #include <linux/kvm_host.h> |
6aa8b732 AK |
28 | #include <linux/types.h> |
29 | #include <linux/string.h> | |
6aa8b732 AK |
30 | #include <linux/mm.h> |
31 | #include <linux/highmem.h> | |
32 | #include <linux/module.h> | |
448353ca | 33 | #include <linux/swap.h> |
05da4558 | 34 | #include <linux/hugetlb.h> |
2f333bcb | 35 | #include <linux/compiler.h> |
bc6678a3 | 36 | #include <linux/srcu.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
bf998156 | 38 | #include <linux/uaccess.h> |
6aa8b732 | 39 | |
e495606d AK |
40 | #include <asm/page.h> |
41 | #include <asm/cmpxchg.h> | |
4e542370 | 42 | #include <asm/io.h> |
13673a90 | 43 | #include <asm/vmx.h> |
6aa8b732 | 44 | |
18552672 JR |
45 | /* |
46 | * When setting this variable to true it enables Two-Dimensional-Paging | |
47 | * where the hardware walks 2 page tables: | |
48 | * 1. the guest-virtual to guest-physical | |
49 | * 2. while doing 1. it walks guest-physical to host-physical | |
50 | * If the hardware supports that we don't need to do shadow paging. | |
51 | */ | |
2f333bcb | 52 | bool tdp_enabled = false; |
18552672 | 53 | |
8b1fe17c XG |
54 | enum { |
55 | AUDIT_PRE_PAGE_FAULT, | |
56 | AUDIT_POST_PAGE_FAULT, | |
57 | AUDIT_PRE_PTE_WRITE, | |
6903074c XG |
58 | AUDIT_POST_PTE_WRITE, |
59 | AUDIT_PRE_SYNC, | |
60 | AUDIT_POST_SYNC | |
8b1fe17c | 61 | }; |
37a7d8b0 | 62 | |
8b1fe17c | 63 | #undef MMU_DEBUG |
37a7d8b0 AK |
64 | |
65 | #ifdef MMU_DEBUG | |
fa4a2c08 PB |
66 | static bool dbg = 0; |
67 | module_param(dbg, bool, 0644); | |
37a7d8b0 AK |
68 | |
69 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) | |
70 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) | |
fa4a2c08 | 71 | #define MMU_WARN_ON(x) WARN_ON(x) |
37a7d8b0 | 72 | #else |
37a7d8b0 AK |
73 | #define pgprintk(x...) do { } while (0) |
74 | #define rmap_printk(x...) do { } while (0) | |
fa4a2c08 | 75 | #define MMU_WARN_ON(x) do { } while (0) |
d6c69ee9 | 76 | #endif |
6aa8b732 | 77 | |
957ed9ef XG |
78 | #define PTE_PREFETCH_NUM 8 |
79 | ||
00763e41 | 80 | #define PT_FIRST_AVAIL_BITS_SHIFT 10 |
6aa8b732 AK |
81 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 |
82 | ||
6aa8b732 AK |
83 | #define PT64_LEVEL_BITS 9 |
84 | ||
85 | #define PT64_LEVEL_SHIFT(level) \ | |
d77c26fc | 86 | (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS) |
6aa8b732 | 87 | |
6aa8b732 AK |
88 | #define PT64_INDEX(address, level)\ |
89 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) | |
90 | ||
91 | ||
92 | #define PT32_LEVEL_BITS 10 | |
93 | ||
94 | #define PT32_LEVEL_SHIFT(level) \ | |
d77c26fc | 95 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
6aa8b732 | 96 | |
e04da980 JR |
97 | #define PT32_LVL_OFFSET_MASK(level) \ |
98 | (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
99 | * PT32_LEVEL_BITS))) - 1)) | |
6aa8b732 AK |
100 | |
101 | #define PT32_INDEX(address, level)\ | |
102 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) | |
103 | ||
104 | ||
27aba766 | 105 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
6aa8b732 AK |
106 | #define PT64_DIR_BASE_ADDR_MASK \ |
107 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) | |
e04da980 JR |
108 | #define PT64_LVL_ADDR_MASK(level) \ |
109 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
110 | * PT64_LEVEL_BITS))) - 1)) | |
111 | #define PT64_LVL_OFFSET_MASK(level) \ | |
112 | (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
113 | * PT64_LEVEL_BITS))) - 1)) | |
6aa8b732 AK |
114 | |
115 | #define PT32_BASE_ADDR_MASK PAGE_MASK | |
116 | #define PT32_DIR_BASE_ADDR_MASK \ | |
117 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) | |
e04da980 JR |
118 | #define PT32_LVL_ADDR_MASK(level) \ |
119 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
120 | * PT32_LEVEL_BITS))) - 1)) | |
6aa8b732 | 121 | |
53166229 GN |
122 | #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \ |
123 | | shadow_x_mask | shadow_nx_mask) | |
6aa8b732 | 124 | |
fe135d2c AK |
125 | #define ACC_EXEC_MASK 1 |
126 | #define ACC_WRITE_MASK PT_WRITABLE_MASK | |
127 | #define ACC_USER_MASK PT_USER_MASK | |
128 | #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) | |
129 | ||
90bb6fc5 AK |
130 | #include <trace/events/kvm.h> |
131 | ||
07420171 AK |
132 | #define CREATE_TRACE_POINTS |
133 | #include "mmutrace.h" | |
134 | ||
49fde340 XG |
135 | #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) |
136 | #define SPTE_MMU_WRITEABLE (1ULL << (PT_FIRST_AVAIL_BITS_SHIFT + 1)) | |
1403283a | 137 | |
135f8c2b AK |
138 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
139 | ||
220f773a TY |
140 | /* make pte_list_desc fit well in cache line */ |
141 | #define PTE_LIST_EXT 3 | |
142 | ||
53c07b18 XG |
143 | struct pte_list_desc { |
144 | u64 *sptes[PTE_LIST_EXT]; | |
145 | struct pte_list_desc *more; | |
cd4a4e53 AK |
146 | }; |
147 | ||
2d11123a AK |
148 | struct kvm_shadow_walk_iterator { |
149 | u64 addr; | |
150 | hpa_t shadow_addr; | |
2d11123a | 151 | u64 *sptep; |
dd3bfd59 | 152 | int level; |
2d11123a AK |
153 | unsigned index; |
154 | }; | |
155 | ||
156 | #define for_each_shadow_entry(_vcpu, _addr, _walker) \ | |
157 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ | |
158 | shadow_walk_okay(&(_walker)); \ | |
159 | shadow_walk_next(&(_walker))) | |
160 | ||
c2a2ac2b XG |
161 | #define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \ |
162 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ | |
163 | shadow_walk_okay(&(_walker)) && \ | |
164 | ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \ | |
165 | __shadow_walk_next(&(_walker), spte)) | |
166 | ||
53c07b18 | 167 | static struct kmem_cache *pte_list_desc_cache; |
d3d25b04 | 168 | static struct kmem_cache *mmu_page_header_cache; |
45221ab6 | 169 | static struct percpu_counter kvm_total_used_mmu_pages; |
b5a33a75 | 170 | |
7b52345e SY |
171 | static u64 __read_mostly shadow_nx_mask; |
172 | static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ | |
173 | static u64 __read_mostly shadow_user_mask; | |
174 | static u64 __read_mostly shadow_accessed_mask; | |
175 | static u64 __read_mostly shadow_dirty_mask; | |
ce88decf XG |
176 | static u64 __read_mostly shadow_mmio_mask; |
177 | ||
178 | static void mmu_spte_set(u64 *sptep, u64 spte); | |
e676505a | 179 | static void mmu_free_roots(struct kvm_vcpu *vcpu); |
ce88decf XG |
180 | |
181 | void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask) | |
182 | { | |
183 | shadow_mmio_mask = mmio_mask; | |
184 | } | |
185 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask); | |
186 | ||
f2fd125d | 187 | /* |
ee3d1570 DM |
188 | * the low bit of the generation number is always presumed to be zero. |
189 | * This disables mmio caching during memslot updates. The concept is | |
190 | * similar to a seqcount but instead of retrying the access we just punt | |
191 | * and ignore the cache. | |
192 | * | |
193 | * spte bits 3-11 are used as bits 1-9 of the generation number, | |
194 | * the bits 52-61 are used as bits 10-19 of the generation number. | |
f2fd125d | 195 | */ |
ee3d1570 | 196 | #define MMIO_SPTE_GEN_LOW_SHIFT 2 |
f2fd125d XG |
197 | #define MMIO_SPTE_GEN_HIGH_SHIFT 52 |
198 | ||
ee3d1570 DM |
199 | #define MMIO_GEN_SHIFT 20 |
200 | #define MMIO_GEN_LOW_SHIFT 10 | |
201 | #define MMIO_GEN_LOW_MASK ((1 << MMIO_GEN_LOW_SHIFT) - 2) | |
f8f55942 | 202 | #define MMIO_GEN_MASK ((1 << MMIO_GEN_SHIFT) - 1) |
f2fd125d XG |
203 | |
204 | static u64 generation_mmio_spte_mask(unsigned int gen) | |
205 | { | |
206 | u64 mask; | |
207 | ||
842bb26a | 208 | WARN_ON(gen & ~MMIO_GEN_MASK); |
f2fd125d XG |
209 | |
210 | mask = (gen & MMIO_GEN_LOW_MASK) << MMIO_SPTE_GEN_LOW_SHIFT; | |
211 | mask |= ((u64)gen >> MMIO_GEN_LOW_SHIFT) << MMIO_SPTE_GEN_HIGH_SHIFT; | |
212 | return mask; | |
213 | } | |
214 | ||
215 | static unsigned int get_mmio_spte_generation(u64 spte) | |
216 | { | |
217 | unsigned int gen; | |
218 | ||
219 | spte &= ~shadow_mmio_mask; | |
220 | ||
221 | gen = (spte >> MMIO_SPTE_GEN_LOW_SHIFT) & MMIO_GEN_LOW_MASK; | |
222 | gen |= (spte >> MMIO_SPTE_GEN_HIGH_SHIFT) << MMIO_GEN_LOW_SHIFT; | |
223 | return gen; | |
224 | } | |
225 | ||
54bf36aa | 226 | static unsigned int kvm_current_mmio_generation(struct kvm_vcpu *vcpu) |
f8f55942 | 227 | { |
54bf36aa | 228 | return kvm_vcpu_memslots(vcpu)->generation & MMIO_GEN_MASK; |
f8f55942 XG |
229 | } |
230 | ||
54bf36aa | 231 | static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn, |
f2fd125d | 232 | unsigned access) |
ce88decf | 233 | { |
54bf36aa | 234 | unsigned int gen = kvm_current_mmio_generation(vcpu); |
f8f55942 | 235 | u64 mask = generation_mmio_spte_mask(gen); |
95b0430d | 236 | |
ce88decf | 237 | access &= ACC_WRITE_MASK | ACC_USER_MASK; |
f2fd125d | 238 | mask |= shadow_mmio_mask | access | gfn << PAGE_SHIFT; |
f2fd125d | 239 | |
f8f55942 | 240 | trace_mark_mmio_spte(sptep, gfn, access, gen); |
f2fd125d | 241 | mmu_spte_set(sptep, mask); |
ce88decf XG |
242 | } |
243 | ||
244 | static bool is_mmio_spte(u64 spte) | |
245 | { | |
246 | return (spte & shadow_mmio_mask) == shadow_mmio_mask; | |
247 | } | |
248 | ||
249 | static gfn_t get_mmio_spte_gfn(u64 spte) | |
250 | { | |
842bb26a | 251 | u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask; |
f2fd125d | 252 | return (spte & ~mask) >> PAGE_SHIFT; |
ce88decf XG |
253 | } |
254 | ||
255 | static unsigned get_mmio_spte_access(u64 spte) | |
256 | { | |
842bb26a | 257 | u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask; |
f2fd125d | 258 | return (spte & ~mask) & ~PAGE_MASK; |
ce88decf XG |
259 | } |
260 | ||
54bf36aa | 261 | static bool set_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn, |
ba049e93 | 262 | kvm_pfn_t pfn, unsigned access) |
ce88decf XG |
263 | { |
264 | if (unlikely(is_noslot_pfn(pfn))) { | |
54bf36aa | 265 | mark_mmio_spte(vcpu, sptep, gfn, access); |
ce88decf XG |
266 | return true; |
267 | } | |
268 | ||
269 | return false; | |
270 | } | |
c7addb90 | 271 | |
54bf36aa | 272 | static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte) |
f8f55942 | 273 | { |
089504c0 XG |
274 | unsigned int kvm_gen, spte_gen; |
275 | ||
54bf36aa | 276 | kvm_gen = kvm_current_mmio_generation(vcpu); |
089504c0 XG |
277 | spte_gen = get_mmio_spte_generation(spte); |
278 | ||
279 | trace_check_mmio_spte(spte, kvm_gen, spte_gen); | |
280 | return likely(kvm_gen == spte_gen); | |
f8f55942 XG |
281 | } |
282 | ||
7b52345e | 283 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, |
4b12f0de | 284 | u64 dirty_mask, u64 nx_mask, u64 x_mask) |
7b52345e SY |
285 | { |
286 | shadow_user_mask = user_mask; | |
287 | shadow_accessed_mask = accessed_mask; | |
288 | shadow_dirty_mask = dirty_mask; | |
289 | shadow_nx_mask = nx_mask; | |
290 | shadow_x_mask = x_mask; | |
291 | } | |
292 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); | |
293 | ||
6aa8b732 AK |
294 | static int is_cpuid_PSE36(void) |
295 | { | |
296 | return 1; | |
297 | } | |
298 | ||
73b1087e AK |
299 | static int is_nx(struct kvm_vcpu *vcpu) |
300 | { | |
f6801dff | 301 | return vcpu->arch.efer & EFER_NX; |
73b1087e AK |
302 | } |
303 | ||
c7addb90 AK |
304 | static int is_shadow_present_pte(u64 pte) |
305 | { | |
ce88decf | 306 | return pte & PT_PRESENT_MASK && !is_mmio_spte(pte); |
c7addb90 AK |
307 | } |
308 | ||
05da4558 MT |
309 | static int is_large_pte(u64 pte) |
310 | { | |
311 | return pte & PT_PAGE_SIZE_MASK; | |
312 | } | |
313 | ||
776e6633 MT |
314 | static int is_last_spte(u64 pte, int level) |
315 | { | |
316 | if (level == PT_PAGE_TABLE_LEVEL) | |
317 | return 1; | |
852e3c19 | 318 | if (is_large_pte(pte)) |
776e6633 MT |
319 | return 1; |
320 | return 0; | |
321 | } | |
322 | ||
ba049e93 | 323 | static kvm_pfn_t spte_to_pfn(u64 pte) |
0b49ea86 | 324 | { |
35149e21 | 325 | return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
0b49ea86 AK |
326 | } |
327 | ||
da928521 AK |
328 | static gfn_t pse36_gfn_delta(u32 gpte) |
329 | { | |
330 | int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT; | |
331 | ||
332 | return (gpte & PT32_DIR_PSE36_MASK) << shift; | |
333 | } | |
334 | ||
603e0651 | 335 | #ifdef CONFIG_X86_64 |
d555c333 | 336 | static void __set_spte(u64 *sptep, u64 spte) |
e663ee64 | 337 | { |
603e0651 | 338 | *sptep = spte; |
e663ee64 AK |
339 | } |
340 | ||
603e0651 | 341 | static void __update_clear_spte_fast(u64 *sptep, u64 spte) |
a9221dd5 | 342 | { |
603e0651 XG |
343 | *sptep = spte; |
344 | } | |
345 | ||
346 | static u64 __update_clear_spte_slow(u64 *sptep, u64 spte) | |
347 | { | |
348 | return xchg(sptep, spte); | |
349 | } | |
c2a2ac2b XG |
350 | |
351 | static u64 __get_spte_lockless(u64 *sptep) | |
352 | { | |
353 | return ACCESS_ONCE(*sptep); | |
354 | } | |
a9221dd5 | 355 | #else |
603e0651 XG |
356 | union split_spte { |
357 | struct { | |
358 | u32 spte_low; | |
359 | u32 spte_high; | |
360 | }; | |
361 | u64 spte; | |
362 | }; | |
a9221dd5 | 363 | |
c2a2ac2b XG |
364 | static void count_spte_clear(u64 *sptep, u64 spte) |
365 | { | |
366 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); | |
367 | ||
368 | if (is_shadow_present_pte(spte)) | |
369 | return; | |
370 | ||
371 | /* Ensure the spte is completely set before we increase the count */ | |
372 | smp_wmb(); | |
373 | sp->clear_spte_count++; | |
374 | } | |
375 | ||
603e0651 XG |
376 | static void __set_spte(u64 *sptep, u64 spte) |
377 | { | |
378 | union split_spte *ssptep, sspte; | |
a9221dd5 | 379 | |
603e0651 XG |
380 | ssptep = (union split_spte *)sptep; |
381 | sspte = (union split_spte)spte; | |
382 | ||
383 | ssptep->spte_high = sspte.spte_high; | |
384 | ||
385 | /* | |
386 | * If we map the spte from nonpresent to present, We should store | |
387 | * the high bits firstly, then set present bit, so cpu can not | |
388 | * fetch this spte while we are setting the spte. | |
389 | */ | |
390 | smp_wmb(); | |
391 | ||
392 | ssptep->spte_low = sspte.spte_low; | |
a9221dd5 AK |
393 | } |
394 | ||
603e0651 XG |
395 | static void __update_clear_spte_fast(u64 *sptep, u64 spte) |
396 | { | |
397 | union split_spte *ssptep, sspte; | |
398 | ||
399 | ssptep = (union split_spte *)sptep; | |
400 | sspte = (union split_spte)spte; | |
401 | ||
402 | ssptep->spte_low = sspte.spte_low; | |
403 | ||
404 | /* | |
405 | * If we map the spte from present to nonpresent, we should clear | |
406 | * present bit firstly to avoid vcpu fetch the old high bits. | |
407 | */ | |
408 | smp_wmb(); | |
409 | ||
410 | ssptep->spte_high = sspte.spte_high; | |
c2a2ac2b | 411 | count_spte_clear(sptep, spte); |
603e0651 XG |
412 | } |
413 | ||
414 | static u64 __update_clear_spte_slow(u64 *sptep, u64 spte) | |
415 | { | |
416 | union split_spte *ssptep, sspte, orig; | |
417 | ||
418 | ssptep = (union split_spte *)sptep; | |
419 | sspte = (union split_spte)spte; | |
420 | ||
421 | /* xchg acts as a barrier before the setting of the high bits */ | |
422 | orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low); | |
41bc3186 ZJ |
423 | orig.spte_high = ssptep->spte_high; |
424 | ssptep->spte_high = sspte.spte_high; | |
c2a2ac2b | 425 | count_spte_clear(sptep, spte); |
603e0651 XG |
426 | |
427 | return orig.spte; | |
428 | } | |
c2a2ac2b XG |
429 | |
430 | /* | |
431 | * The idea using the light way get the spte on x86_32 guest is from | |
432 | * gup_get_pte(arch/x86/mm/gup.c). | |
accaefe0 XG |
433 | * |
434 | * An spte tlb flush may be pending, because kvm_set_pte_rmapp | |
435 | * coalesces them and we are running out of the MMU lock. Therefore | |
436 | * we need to protect against in-progress updates of the spte. | |
437 | * | |
438 | * Reading the spte while an update is in progress may get the old value | |
439 | * for the high part of the spte. The race is fine for a present->non-present | |
440 | * change (because the high part of the spte is ignored for non-present spte), | |
441 | * but for a present->present change we must reread the spte. | |
442 | * | |
443 | * All such changes are done in two steps (present->non-present and | |
444 | * non-present->present), hence it is enough to count the number of | |
445 | * present->non-present updates: if it changed while reading the spte, | |
446 | * we might have hit the race. This is done using clear_spte_count. | |
c2a2ac2b XG |
447 | */ |
448 | static u64 __get_spte_lockless(u64 *sptep) | |
449 | { | |
450 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); | |
451 | union split_spte spte, *orig = (union split_spte *)sptep; | |
452 | int count; | |
453 | ||
454 | retry: | |
455 | count = sp->clear_spte_count; | |
456 | smp_rmb(); | |
457 | ||
458 | spte.spte_low = orig->spte_low; | |
459 | smp_rmb(); | |
460 | ||
461 | spte.spte_high = orig->spte_high; | |
462 | smp_rmb(); | |
463 | ||
464 | if (unlikely(spte.spte_low != orig->spte_low || | |
465 | count != sp->clear_spte_count)) | |
466 | goto retry; | |
467 | ||
468 | return spte.spte; | |
469 | } | |
603e0651 XG |
470 | #endif |
471 | ||
c7ba5b48 XG |
472 | static bool spte_is_locklessly_modifiable(u64 spte) |
473 | { | |
feb3eb70 GN |
474 | return (spte & (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE)) == |
475 | (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE); | |
c7ba5b48 XG |
476 | } |
477 | ||
8672b721 XG |
478 | static bool spte_has_volatile_bits(u64 spte) |
479 | { | |
c7ba5b48 XG |
480 | /* |
481 | * Always atomicly update spte if it can be updated | |
482 | * out of mmu-lock, it can ensure dirty bit is not lost, | |
483 | * also, it can help us to get a stable is_writable_pte() | |
484 | * to ensure tlb flush is not missed. | |
485 | */ | |
486 | if (spte_is_locklessly_modifiable(spte)) | |
487 | return true; | |
488 | ||
8672b721 XG |
489 | if (!shadow_accessed_mask) |
490 | return false; | |
491 | ||
492 | if (!is_shadow_present_pte(spte)) | |
493 | return false; | |
494 | ||
4132779b XG |
495 | if ((spte & shadow_accessed_mask) && |
496 | (!is_writable_pte(spte) || (spte & shadow_dirty_mask))) | |
8672b721 XG |
497 | return false; |
498 | ||
499 | return true; | |
500 | } | |
501 | ||
4132779b XG |
502 | static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask) |
503 | { | |
504 | return (old_spte & bit_mask) && !(new_spte & bit_mask); | |
505 | } | |
506 | ||
7e71a59b KH |
507 | static bool spte_is_bit_changed(u64 old_spte, u64 new_spte, u64 bit_mask) |
508 | { | |
509 | return (old_spte & bit_mask) != (new_spte & bit_mask); | |
510 | } | |
511 | ||
1df9f2dc XG |
512 | /* Rules for using mmu_spte_set: |
513 | * Set the sptep from nonpresent to present. | |
514 | * Note: the sptep being assigned *must* be either not present | |
515 | * or in a state where the hardware will not attempt to update | |
516 | * the spte. | |
517 | */ | |
518 | static void mmu_spte_set(u64 *sptep, u64 new_spte) | |
519 | { | |
520 | WARN_ON(is_shadow_present_pte(*sptep)); | |
521 | __set_spte(sptep, new_spte); | |
522 | } | |
523 | ||
524 | /* Rules for using mmu_spte_update: | |
525 | * Update the state bits, it means the mapped pfn is not changged. | |
6e7d0354 XG |
526 | * |
527 | * Whenever we overwrite a writable spte with a read-only one we | |
528 | * should flush remote TLBs. Otherwise rmap_write_protect | |
529 | * will find a read-only spte, even though the writable spte | |
530 | * might be cached on a CPU's TLB, the return value indicates this | |
531 | * case. | |
1df9f2dc | 532 | */ |
6e7d0354 | 533 | static bool mmu_spte_update(u64 *sptep, u64 new_spte) |
b79b93f9 | 534 | { |
c7ba5b48 | 535 | u64 old_spte = *sptep; |
6e7d0354 | 536 | bool ret = false; |
4132779b | 537 | |
afd28fe1 | 538 | WARN_ON(!is_shadow_present_pte(new_spte)); |
b79b93f9 | 539 | |
6e7d0354 XG |
540 | if (!is_shadow_present_pte(old_spte)) { |
541 | mmu_spte_set(sptep, new_spte); | |
542 | return ret; | |
543 | } | |
4132779b | 544 | |
c7ba5b48 | 545 | if (!spte_has_volatile_bits(old_spte)) |
603e0651 | 546 | __update_clear_spte_fast(sptep, new_spte); |
4132779b | 547 | else |
603e0651 | 548 | old_spte = __update_clear_spte_slow(sptep, new_spte); |
4132779b | 549 | |
c7ba5b48 XG |
550 | /* |
551 | * For the spte updated out of mmu-lock is safe, since | |
552 | * we always atomicly update it, see the comments in | |
553 | * spte_has_volatile_bits(). | |
554 | */ | |
7f31c959 XG |
555 | if (spte_is_locklessly_modifiable(old_spte) && |
556 | !is_writable_pte(new_spte)) | |
6e7d0354 XG |
557 | ret = true; |
558 | ||
4132779b | 559 | if (!shadow_accessed_mask) |
6e7d0354 | 560 | return ret; |
4132779b | 561 | |
7e71a59b KH |
562 | /* |
563 | * Flush TLB when accessed/dirty bits are changed in the page tables, | |
564 | * to guarantee consistency between TLB and page tables. | |
565 | */ | |
566 | if (spte_is_bit_changed(old_spte, new_spte, | |
567 | shadow_accessed_mask | shadow_dirty_mask)) | |
568 | ret = true; | |
569 | ||
4132779b XG |
570 | if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask)) |
571 | kvm_set_pfn_accessed(spte_to_pfn(old_spte)); | |
572 | if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask)) | |
573 | kvm_set_pfn_dirty(spte_to_pfn(old_spte)); | |
6e7d0354 XG |
574 | |
575 | return ret; | |
b79b93f9 AK |
576 | } |
577 | ||
1df9f2dc XG |
578 | /* |
579 | * Rules for using mmu_spte_clear_track_bits: | |
580 | * It sets the sptep from present to nonpresent, and track the | |
581 | * state bits, it is used to clear the last level sptep. | |
582 | */ | |
583 | static int mmu_spte_clear_track_bits(u64 *sptep) | |
584 | { | |
ba049e93 | 585 | kvm_pfn_t pfn; |
1df9f2dc XG |
586 | u64 old_spte = *sptep; |
587 | ||
588 | if (!spte_has_volatile_bits(old_spte)) | |
603e0651 | 589 | __update_clear_spte_fast(sptep, 0ull); |
1df9f2dc | 590 | else |
603e0651 | 591 | old_spte = __update_clear_spte_slow(sptep, 0ull); |
1df9f2dc | 592 | |
afd28fe1 | 593 | if (!is_shadow_present_pte(old_spte)) |
1df9f2dc XG |
594 | return 0; |
595 | ||
596 | pfn = spte_to_pfn(old_spte); | |
86fde74c XG |
597 | |
598 | /* | |
599 | * KVM does not hold the refcount of the page used by | |
600 | * kvm mmu, before reclaiming the page, we should | |
601 | * unmap it from mmu first. | |
602 | */ | |
bf4bea8e | 603 | WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn))); |
86fde74c | 604 | |
1df9f2dc XG |
605 | if (!shadow_accessed_mask || old_spte & shadow_accessed_mask) |
606 | kvm_set_pfn_accessed(pfn); | |
607 | if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask)) | |
608 | kvm_set_pfn_dirty(pfn); | |
609 | return 1; | |
610 | } | |
611 | ||
612 | /* | |
613 | * Rules for using mmu_spte_clear_no_track: | |
614 | * Directly clear spte without caring the state bits of sptep, | |
615 | * it is used to set the upper level spte. | |
616 | */ | |
617 | static void mmu_spte_clear_no_track(u64 *sptep) | |
618 | { | |
603e0651 | 619 | __update_clear_spte_fast(sptep, 0ull); |
1df9f2dc XG |
620 | } |
621 | ||
c2a2ac2b XG |
622 | static u64 mmu_spte_get_lockless(u64 *sptep) |
623 | { | |
624 | return __get_spte_lockless(sptep); | |
625 | } | |
626 | ||
627 | static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu) | |
628 | { | |
c142786c AK |
629 | /* |
630 | * Prevent page table teardown by making any free-er wait during | |
631 | * kvm_flush_remote_tlbs() IPI to all active vcpus. | |
632 | */ | |
633 | local_irq_disable(); | |
634 | vcpu->mode = READING_SHADOW_PAGE_TABLES; | |
635 | /* | |
636 | * Make sure a following spte read is not reordered ahead of the write | |
637 | * to vcpu->mode. | |
638 | */ | |
639 | smp_mb(); | |
c2a2ac2b XG |
640 | } |
641 | ||
642 | static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) | |
643 | { | |
c142786c AK |
644 | /* |
645 | * Make sure the write to vcpu->mode is not reordered in front of | |
646 | * reads to sptes. If it does, kvm_commit_zap_page() can see us | |
647 | * OUTSIDE_GUEST_MODE and proceed to free the shadow page table. | |
648 | */ | |
649 | smp_mb(); | |
650 | vcpu->mode = OUTSIDE_GUEST_MODE; | |
651 | local_irq_enable(); | |
c2a2ac2b XG |
652 | } |
653 | ||
e2dec939 | 654 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
2e3e5882 | 655 | struct kmem_cache *base_cache, int min) |
714b93da AK |
656 | { |
657 | void *obj; | |
658 | ||
659 | if (cache->nobjs >= min) | |
e2dec939 | 660 | return 0; |
714b93da | 661 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
2e3e5882 | 662 | obj = kmem_cache_zalloc(base_cache, GFP_KERNEL); |
714b93da | 663 | if (!obj) |
e2dec939 | 664 | return -ENOMEM; |
714b93da AK |
665 | cache->objects[cache->nobjs++] = obj; |
666 | } | |
e2dec939 | 667 | return 0; |
714b93da AK |
668 | } |
669 | ||
f759e2b4 XG |
670 | static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache) |
671 | { | |
672 | return cache->nobjs; | |
673 | } | |
674 | ||
e8ad9a70 XG |
675 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc, |
676 | struct kmem_cache *cache) | |
714b93da AK |
677 | { |
678 | while (mc->nobjs) | |
e8ad9a70 | 679 | kmem_cache_free(cache, mc->objects[--mc->nobjs]); |
714b93da AK |
680 | } |
681 | ||
c1158e63 | 682 | static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache, |
2e3e5882 | 683 | int min) |
c1158e63 | 684 | { |
842f22ed | 685 | void *page; |
c1158e63 AK |
686 | |
687 | if (cache->nobjs >= min) | |
688 | return 0; | |
689 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { | |
842f22ed | 690 | page = (void *)__get_free_page(GFP_KERNEL); |
c1158e63 AK |
691 | if (!page) |
692 | return -ENOMEM; | |
842f22ed | 693 | cache->objects[cache->nobjs++] = page; |
c1158e63 AK |
694 | } |
695 | return 0; | |
696 | } | |
697 | ||
698 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) | |
699 | { | |
700 | while (mc->nobjs) | |
c4d198d5 | 701 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
c1158e63 AK |
702 | } |
703 | ||
2e3e5882 | 704 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
714b93da | 705 | { |
e2dec939 AK |
706 | int r; |
707 | ||
53c07b18 | 708 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache, |
67052b35 | 709 | pte_list_desc_cache, 8 + PTE_PREFETCH_NUM); |
d3d25b04 AK |
710 | if (r) |
711 | goto out; | |
ad312c7c | 712 | r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8); |
d3d25b04 AK |
713 | if (r) |
714 | goto out; | |
ad312c7c | 715 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
2e3e5882 | 716 | mmu_page_header_cache, 4); |
e2dec939 AK |
717 | out: |
718 | return r; | |
714b93da AK |
719 | } |
720 | ||
721 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) | |
722 | { | |
53c07b18 XG |
723 | mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache, |
724 | pte_list_desc_cache); | |
ad312c7c | 725 | mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache); |
e8ad9a70 XG |
726 | mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache, |
727 | mmu_page_header_cache); | |
714b93da AK |
728 | } |
729 | ||
80feb89a | 730 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc) |
714b93da AK |
731 | { |
732 | void *p; | |
733 | ||
734 | BUG_ON(!mc->nobjs); | |
735 | p = mc->objects[--mc->nobjs]; | |
714b93da AK |
736 | return p; |
737 | } | |
738 | ||
53c07b18 | 739 | static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu) |
714b93da | 740 | { |
80feb89a | 741 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache); |
714b93da AK |
742 | } |
743 | ||
53c07b18 | 744 | static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc) |
714b93da | 745 | { |
53c07b18 | 746 | kmem_cache_free(pte_list_desc_cache, pte_list_desc); |
714b93da AK |
747 | } |
748 | ||
2032a93d LJ |
749 | static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index) |
750 | { | |
751 | if (!sp->role.direct) | |
752 | return sp->gfns[index]; | |
753 | ||
754 | return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS)); | |
755 | } | |
756 | ||
757 | static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn) | |
758 | { | |
759 | if (sp->role.direct) | |
760 | BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index)); | |
761 | else | |
762 | sp->gfns[index] = gfn; | |
763 | } | |
764 | ||
05da4558 | 765 | /* |
d4dbf470 TY |
766 | * Return the pointer to the large page information for a given gfn, |
767 | * handling slots that are not large page aligned. | |
05da4558 | 768 | */ |
d4dbf470 TY |
769 | static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn, |
770 | struct kvm_memory_slot *slot, | |
771 | int level) | |
05da4558 MT |
772 | { |
773 | unsigned long idx; | |
774 | ||
fb03cb6f | 775 | idx = gfn_to_index(gfn, slot->base_gfn, level); |
db3fe4eb | 776 | return &slot->arch.lpage_info[level - 2][idx]; |
05da4558 MT |
777 | } |
778 | ||
3ed1a478 | 779 | static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) |
05da4558 | 780 | { |
699023e2 | 781 | struct kvm_memslots *slots; |
d25797b2 | 782 | struct kvm_memory_slot *slot; |
d4dbf470 | 783 | struct kvm_lpage_info *linfo; |
3ed1a478 | 784 | gfn_t gfn; |
d25797b2 | 785 | int i; |
05da4558 | 786 | |
3ed1a478 | 787 | gfn = sp->gfn; |
699023e2 PB |
788 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
789 | slot = __gfn_to_memslot(slots, gfn); | |
8a3d08f1 | 790 | for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) { |
d4dbf470 | 791 | linfo = lpage_info_slot(gfn, slot, i); |
92f94f1e | 792 | linfo->disallow_lpage += 1; |
d25797b2 | 793 | } |
332b207d | 794 | kvm->arch.indirect_shadow_pages++; |
05da4558 MT |
795 | } |
796 | ||
3ed1a478 | 797 | static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) |
05da4558 | 798 | { |
699023e2 | 799 | struct kvm_memslots *slots; |
d25797b2 | 800 | struct kvm_memory_slot *slot; |
d4dbf470 | 801 | struct kvm_lpage_info *linfo; |
3ed1a478 | 802 | gfn_t gfn; |
d25797b2 | 803 | int i; |
05da4558 | 804 | |
3ed1a478 | 805 | gfn = sp->gfn; |
699023e2 PB |
806 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
807 | slot = __gfn_to_memslot(slots, gfn); | |
8a3d08f1 | 808 | for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) { |
d4dbf470 | 809 | linfo = lpage_info_slot(gfn, slot, i); |
92f94f1e XG |
810 | linfo->disallow_lpage -= 1; |
811 | WARN_ON(linfo->disallow_lpage < 0); | |
d25797b2 | 812 | } |
332b207d | 813 | kvm->arch.indirect_shadow_pages--; |
05da4558 MT |
814 | } |
815 | ||
92f94f1e XG |
816 | static bool __mmu_gfn_lpage_is_disallowed(gfn_t gfn, int level, |
817 | struct kvm_memory_slot *slot) | |
05da4558 | 818 | { |
d4dbf470 | 819 | struct kvm_lpage_info *linfo; |
05da4558 MT |
820 | |
821 | if (slot) { | |
d4dbf470 | 822 | linfo = lpage_info_slot(gfn, slot, level); |
92f94f1e | 823 | return !!linfo->disallow_lpage; |
05da4558 MT |
824 | } |
825 | ||
92f94f1e | 826 | return true; |
05da4558 MT |
827 | } |
828 | ||
92f94f1e XG |
829 | static bool mmu_gfn_lpage_is_disallowed(struct kvm_vcpu *vcpu, gfn_t gfn, |
830 | int level) | |
5225fdf8 TY |
831 | { |
832 | struct kvm_memory_slot *slot; | |
833 | ||
834 | slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
92f94f1e | 835 | return __mmu_gfn_lpage_is_disallowed(gfn, level, slot); |
5225fdf8 TY |
836 | } |
837 | ||
d25797b2 | 838 | static int host_mapping_level(struct kvm *kvm, gfn_t gfn) |
05da4558 | 839 | { |
8f0b1ab6 | 840 | unsigned long page_size; |
d25797b2 | 841 | int i, ret = 0; |
05da4558 | 842 | |
8f0b1ab6 | 843 | page_size = kvm_host_page_size(kvm, gfn); |
05da4558 | 844 | |
8a3d08f1 | 845 | for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) { |
d25797b2 JR |
846 | if (page_size >= KVM_HPAGE_SIZE(i)) |
847 | ret = i; | |
848 | else | |
849 | break; | |
850 | } | |
851 | ||
4c2155ce | 852 | return ret; |
05da4558 MT |
853 | } |
854 | ||
d8aacf5d TY |
855 | static inline bool memslot_valid_for_gpte(struct kvm_memory_slot *slot, |
856 | bool no_dirty_log) | |
857 | { | |
858 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) | |
859 | return false; | |
860 | if (no_dirty_log && slot->dirty_bitmap) | |
861 | return false; | |
862 | ||
863 | return true; | |
864 | } | |
865 | ||
5d163b1c XG |
866 | static struct kvm_memory_slot * |
867 | gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn, | |
868 | bool no_dirty_log) | |
05da4558 MT |
869 | { |
870 | struct kvm_memory_slot *slot; | |
5d163b1c | 871 | |
54bf36aa | 872 | slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
d8aacf5d | 873 | if (!memslot_valid_for_gpte(slot, no_dirty_log)) |
5d163b1c XG |
874 | slot = NULL; |
875 | ||
876 | return slot; | |
877 | } | |
878 | ||
fd136902 TY |
879 | static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn, |
880 | bool *force_pt_level) | |
936a5fe6 AA |
881 | { |
882 | int host_level, level, max_level; | |
d8aacf5d TY |
883 | struct kvm_memory_slot *slot; |
884 | ||
8c85ac1c TY |
885 | if (unlikely(*force_pt_level)) |
886 | return PT_PAGE_TABLE_LEVEL; | |
05da4558 | 887 | |
8c85ac1c TY |
888 | slot = kvm_vcpu_gfn_to_memslot(vcpu, large_gfn); |
889 | *force_pt_level = !memslot_valid_for_gpte(slot, true); | |
fd136902 TY |
890 | if (unlikely(*force_pt_level)) |
891 | return PT_PAGE_TABLE_LEVEL; | |
892 | ||
d25797b2 JR |
893 | host_level = host_mapping_level(vcpu->kvm, large_gfn); |
894 | ||
895 | if (host_level == PT_PAGE_TABLE_LEVEL) | |
896 | return host_level; | |
897 | ||
55dd98c3 | 898 | max_level = min(kvm_x86_ops->get_lpage_level(), host_level); |
878403b7 SY |
899 | |
900 | for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level) | |
92f94f1e | 901 | if (__mmu_gfn_lpage_is_disallowed(large_gfn, level, slot)) |
d25797b2 | 902 | break; |
d25797b2 JR |
903 | |
904 | return level - 1; | |
05da4558 MT |
905 | } |
906 | ||
290fc38d | 907 | /* |
018aabb5 | 908 | * About rmap_head encoding: |
cd4a4e53 | 909 | * |
018aabb5 TY |
910 | * If the bit zero of rmap_head->val is clear, then it points to the only spte |
911 | * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct | |
53c07b18 | 912 | * pte_list_desc containing more mappings. |
018aabb5 TY |
913 | */ |
914 | ||
915 | /* | |
916 | * Returns the number of pointers in the rmap chain, not counting the new one. | |
cd4a4e53 | 917 | */ |
53c07b18 | 918 | static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte, |
018aabb5 | 919 | struct kvm_rmap_head *rmap_head) |
cd4a4e53 | 920 | { |
53c07b18 | 921 | struct pte_list_desc *desc; |
53a27b39 | 922 | int i, count = 0; |
cd4a4e53 | 923 | |
018aabb5 | 924 | if (!rmap_head->val) { |
53c07b18 | 925 | rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte); |
018aabb5 TY |
926 | rmap_head->val = (unsigned long)spte; |
927 | } else if (!(rmap_head->val & 1)) { | |
53c07b18 XG |
928 | rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte); |
929 | desc = mmu_alloc_pte_list_desc(vcpu); | |
018aabb5 | 930 | desc->sptes[0] = (u64 *)rmap_head->val; |
d555c333 | 931 | desc->sptes[1] = spte; |
018aabb5 | 932 | rmap_head->val = (unsigned long)desc | 1; |
cb16a7b3 | 933 | ++count; |
cd4a4e53 | 934 | } else { |
53c07b18 | 935 | rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte); |
018aabb5 | 936 | desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); |
53c07b18 | 937 | while (desc->sptes[PTE_LIST_EXT-1] && desc->more) { |
cd4a4e53 | 938 | desc = desc->more; |
53c07b18 | 939 | count += PTE_LIST_EXT; |
53a27b39 | 940 | } |
53c07b18 XG |
941 | if (desc->sptes[PTE_LIST_EXT-1]) { |
942 | desc->more = mmu_alloc_pte_list_desc(vcpu); | |
cd4a4e53 AK |
943 | desc = desc->more; |
944 | } | |
d555c333 | 945 | for (i = 0; desc->sptes[i]; ++i) |
cb16a7b3 | 946 | ++count; |
d555c333 | 947 | desc->sptes[i] = spte; |
cd4a4e53 | 948 | } |
53a27b39 | 949 | return count; |
cd4a4e53 AK |
950 | } |
951 | ||
53c07b18 | 952 | static void |
018aabb5 TY |
953 | pte_list_desc_remove_entry(struct kvm_rmap_head *rmap_head, |
954 | struct pte_list_desc *desc, int i, | |
955 | struct pte_list_desc *prev_desc) | |
cd4a4e53 AK |
956 | { |
957 | int j; | |
958 | ||
53c07b18 | 959 | for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j) |
cd4a4e53 | 960 | ; |
d555c333 AK |
961 | desc->sptes[i] = desc->sptes[j]; |
962 | desc->sptes[j] = NULL; | |
cd4a4e53 AK |
963 | if (j != 0) |
964 | return; | |
965 | if (!prev_desc && !desc->more) | |
018aabb5 | 966 | rmap_head->val = (unsigned long)desc->sptes[0]; |
cd4a4e53 AK |
967 | else |
968 | if (prev_desc) | |
969 | prev_desc->more = desc->more; | |
970 | else | |
018aabb5 | 971 | rmap_head->val = (unsigned long)desc->more | 1; |
53c07b18 | 972 | mmu_free_pte_list_desc(desc); |
cd4a4e53 AK |
973 | } |
974 | ||
018aabb5 | 975 | static void pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head) |
cd4a4e53 | 976 | { |
53c07b18 XG |
977 | struct pte_list_desc *desc; |
978 | struct pte_list_desc *prev_desc; | |
cd4a4e53 AK |
979 | int i; |
980 | ||
018aabb5 | 981 | if (!rmap_head->val) { |
53c07b18 | 982 | printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte); |
cd4a4e53 | 983 | BUG(); |
018aabb5 | 984 | } else if (!(rmap_head->val & 1)) { |
53c07b18 | 985 | rmap_printk("pte_list_remove: %p 1->0\n", spte); |
018aabb5 | 986 | if ((u64 *)rmap_head->val != spte) { |
53c07b18 | 987 | printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte); |
cd4a4e53 AK |
988 | BUG(); |
989 | } | |
018aabb5 | 990 | rmap_head->val = 0; |
cd4a4e53 | 991 | } else { |
53c07b18 | 992 | rmap_printk("pte_list_remove: %p many->many\n", spte); |
018aabb5 | 993 | desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); |
cd4a4e53 AK |
994 | prev_desc = NULL; |
995 | while (desc) { | |
018aabb5 | 996 | for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) { |
d555c333 | 997 | if (desc->sptes[i] == spte) { |
018aabb5 TY |
998 | pte_list_desc_remove_entry(rmap_head, |
999 | desc, i, prev_desc); | |
cd4a4e53 AK |
1000 | return; |
1001 | } | |
018aabb5 | 1002 | } |
cd4a4e53 AK |
1003 | prev_desc = desc; |
1004 | desc = desc->more; | |
1005 | } | |
53c07b18 | 1006 | pr_err("pte_list_remove: %p many->many\n", spte); |
cd4a4e53 AK |
1007 | BUG(); |
1008 | } | |
1009 | } | |
1010 | ||
018aabb5 TY |
1011 | static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, |
1012 | struct kvm_memory_slot *slot) | |
53c07b18 | 1013 | { |
77d11309 | 1014 | unsigned long idx; |
53c07b18 | 1015 | |
77d11309 | 1016 | idx = gfn_to_index(gfn, slot->base_gfn, level); |
d89cc617 | 1017 | return &slot->arch.rmap[level - PT_PAGE_TABLE_LEVEL][idx]; |
53c07b18 XG |
1018 | } |
1019 | ||
018aabb5 TY |
1020 | static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, |
1021 | struct kvm_mmu_page *sp) | |
9b9b1492 | 1022 | { |
699023e2 | 1023 | struct kvm_memslots *slots; |
9b9b1492 TY |
1024 | struct kvm_memory_slot *slot; |
1025 | ||
699023e2 PB |
1026 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
1027 | slot = __gfn_to_memslot(slots, gfn); | |
e4cd1da9 | 1028 | return __gfn_to_rmap(gfn, sp->role.level, slot); |
9b9b1492 TY |
1029 | } |
1030 | ||
f759e2b4 XG |
1031 | static bool rmap_can_add(struct kvm_vcpu *vcpu) |
1032 | { | |
1033 | struct kvm_mmu_memory_cache *cache; | |
1034 | ||
1035 | cache = &vcpu->arch.mmu_pte_list_desc_cache; | |
1036 | return mmu_memory_cache_free_objects(cache); | |
1037 | } | |
1038 | ||
53c07b18 XG |
1039 | static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
1040 | { | |
1041 | struct kvm_mmu_page *sp; | |
018aabb5 | 1042 | struct kvm_rmap_head *rmap_head; |
53c07b18 | 1043 | |
53c07b18 XG |
1044 | sp = page_header(__pa(spte)); |
1045 | kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn); | |
018aabb5 TY |
1046 | rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp); |
1047 | return pte_list_add(vcpu, spte, rmap_head); | |
53c07b18 XG |
1048 | } |
1049 | ||
53c07b18 XG |
1050 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
1051 | { | |
1052 | struct kvm_mmu_page *sp; | |
1053 | gfn_t gfn; | |
018aabb5 | 1054 | struct kvm_rmap_head *rmap_head; |
53c07b18 XG |
1055 | |
1056 | sp = page_header(__pa(spte)); | |
1057 | gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt); | |
018aabb5 TY |
1058 | rmap_head = gfn_to_rmap(kvm, gfn, sp); |
1059 | pte_list_remove(spte, rmap_head); | |
53c07b18 XG |
1060 | } |
1061 | ||
1e3f42f0 TY |
1062 | /* |
1063 | * Used by the following functions to iterate through the sptes linked by a | |
1064 | * rmap. All fields are private and not assumed to be used outside. | |
1065 | */ | |
1066 | struct rmap_iterator { | |
1067 | /* private fields */ | |
1068 | struct pte_list_desc *desc; /* holds the sptep if not NULL */ | |
1069 | int pos; /* index of the sptep */ | |
1070 | }; | |
1071 | ||
1072 | /* | |
1073 | * Iteration must be started by this function. This should also be used after | |
1074 | * removing/dropping sptes from the rmap link because in such cases the | |
1075 | * information in the itererator may not be valid. | |
1076 | * | |
1077 | * Returns sptep if found, NULL otherwise. | |
1078 | */ | |
018aabb5 TY |
1079 | static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head, |
1080 | struct rmap_iterator *iter) | |
1e3f42f0 | 1081 | { |
77fbbbd2 TY |
1082 | u64 *sptep; |
1083 | ||
018aabb5 | 1084 | if (!rmap_head->val) |
1e3f42f0 TY |
1085 | return NULL; |
1086 | ||
018aabb5 | 1087 | if (!(rmap_head->val & 1)) { |
1e3f42f0 | 1088 | iter->desc = NULL; |
77fbbbd2 TY |
1089 | sptep = (u64 *)rmap_head->val; |
1090 | goto out; | |
1e3f42f0 TY |
1091 | } |
1092 | ||
018aabb5 | 1093 | iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); |
1e3f42f0 | 1094 | iter->pos = 0; |
77fbbbd2 TY |
1095 | sptep = iter->desc->sptes[iter->pos]; |
1096 | out: | |
1097 | BUG_ON(!is_shadow_present_pte(*sptep)); | |
1098 | return sptep; | |
1e3f42f0 TY |
1099 | } |
1100 | ||
1101 | /* | |
1102 | * Must be used with a valid iterator: e.g. after rmap_get_first(). | |
1103 | * | |
1104 | * Returns sptep if found, NULL otherwise. | |
1105 | */ | |
1106 | static u64 *rmap_get_next(struct rmap_iterator *iter) | |
1107 | { | |
77fbbbd2 TY |
1108 | u64 *sptep; |
1109 | ||
1e3f42f0 TY |
1110 | if (iter->desc) { |
1111 | if (iter->pos < PTE_LIST_EXT - 1) { | |
1e3f42f0 TY |
1112 | ++iter->pos; |
1113 | sptep = iter->desc->sptes[iter->pos]; | |
1114 | if (sptep) | |
77fbbbd2 | 1115 | goto out; |
1e3f42f0 TY |
1116 | } |
1117 | ||
1118 | iter->desc = iter->desc->more; | |
1119 | ||
1120 | if (iter->desc) { | |
1121 | iter->pos = 0; | |
1122 | /* desc->sptes[0] cannot be NULL */ | |
77fbbbd2 TY |
1123 | sptep = iter->desc->sptes[iter->pos]; |
1124 | goto out; | |
1e3f42f0 TY |
1125 | } |
1126 | } | |
1127 | ||
1128 | return NULL; | |
77fbbbd2 TY |
1129 | out: |
1130 | BUG_ON(!is_shadow_present_pte(*sptep)); | |
1131 | return sptep; | |
1e3f42f0 TY |
1132 | } |
1133 | ||
018aabb5 TY |
1134 | #define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \ |
1135 | for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \ | |
77fbbbd2 | 1136 | _spte_; _spte_ = rmap_get_next(_iter_)) |
0d536790 | 1137 | |
c3707958 | 1138 | static void drop_spte(struct kvm *kvm, u64 *sptep) |
e4b502ea | 1139 | { |
1df9f2dc | 1140 | if (mmu_spte_clear_track_bits(sptep)) |
eb45fda4 | 1141 | rmap_remove(kvm, sptep); |
be38d276 AK |
1142 | } |
1143 | ||
8e22f955 XG |
1144 | |
1145 | static bool __drop_large_spte(struct kvm *kvm, u64 *sptep) | |
1146 | { | |
1147 | if (is_large_pte(*sptep)) { | |
1148 | WARN_ON(page_header(__pa(sptep))->role.level == | |
1149 | PT_PAGE_TABLE_LEVEL); | |
1150 | drop_spte(kvm, sptep); | |
1151 | --kvm->stat.lpages; | |
1152 | return true; | |
1153 | } | |
1154 | ||
1155 | return false; | |
1156 | } | |
1157 | ||
1158 | static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep) | |
1159 | { | |
1160 | if (__drop_large_spte(vcpu->kvm, sptep)) | |
1161 | kvm_flush_remote_tlbs(vcpu->kvm); | |
1162 | } | |
1163 | ||
1164 | /* | |
49fde340 | 1165 | * Write-protect on the specified @sptep, @pt_protect indicates whether |
c126d94f | 1166 | * spte write-protection is caused by protecting shadow page table. |
49fde340 | 1167 | * |
b4619660 | 1168 | * Note: write protection is difference between dirty logging and spte |
49fde340 XG |
1169 | * protection: |
1170 | * - for dirty logging, the spte can be set to writable at anytime if | |
1171 | * its dirty bitmap is properly set. | |
1172 | * - for spte protection, the spte can be writable only after unsync-ing | |
1173 | * shadow page. | |
8e22f955 | 1174 | * |
c126d94f | 1175 | * Return true if tlb need be flushed. |
8e22f955 | 1176 | */ |
c126d94f | 1177 | static bool spte_write_protect(struct kvm *kvm, u64 *sptep, bool pt_protect) |
d13bc5b5 XG |
1178 | { |
1179 | u64 spte = *sptep; | |
1180 | ||
49fde340 XG |
1181 | if (!is_writable_pte(spte) && |
1182 | !(pt_protect && spte_is_locklessly_modifiable(spte))) | |
d13bc5b5 XG |
1183 | return false; |
1184 | ||
1185 | rmap_printk("rmap_write_protect: spte %p %llx\n", sptep, *sptep); | |
1186 | ||
49fde340 XG |
1187 | if (pt_protect) |
1188 | spte &= ~SPTE_MMU_WRITEABLE; | |
d13bc5b5 | 1189 | spte = spte & ~PT_WRITABLE_MASK; |
49fde340 | 1190 | |
c126d94f | 1191 | return mmu_spte_update(sptep, spte); |
d13bc5b5 XG |
1192 | } |
1193 | ||
018aabb5 TY |
1194 | static bool __rmap_write_protect(struct kvm *kvm, |
1195 | struct kvm_rmap_head *rmap_head, | |
245c3912 | 1196 | bool pt_protect) |
98348e95 | 1197 | { |
1e3f42f0 TY |
1198 | u64 *sptep; |
1199 | struct rmap_iterator iter; | |
d13bc5b5 | 1200 | bool flush = false; |
374cbac0 | 1201 | |
018aabb5 | 1202 | for_each_rmap_spte(rmap_head, &iter, sptep) |
c126d94f | 1203 | flush |= spte_write_protect(kvm, sptep, pt_protect); |
855149aa | 1204 | |
d13bc5b5 | 1205 | return flush; |
a0ed4607 TY |
1206 | } |
1207 | ||
f4b4b180 KH |
1208 | static bool spte_clear_dirty(struct kvm *kvm, u64 *sptep) |
1209 | { | |
1210 | u64 spte = *sptep; | |
1211 | ||
1212 | rmap_printk("rmap_clear_dirty: spte %p %llx\n", sptep, *sptep); | |
1213 | ||
1214 | spte &= ~shadow_dirty_mask; | |
1215 | ||
1216 | return mmu_spte_update(sptep, spte); | |
1217 | } | |
1218 | ||
018aabb5 | 1219 | static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head) |
f4b4b180 KH |
1220 | { |
1221 | u64 *sptep; | |
1222 | struct rmap_iterator iter; | |
1223 | bool flush = false; | |
1224 | ||
018aabb5 | 1225 | for_each_rmap_spte(rmap_head, &iter, sptep) |
f4b4b180 | 1226 | flush |= spte_clear_dirty(kvm, sptep); |
f4b4b180 KH |
1227 | |
1228 | return flush; | |
1229 | } | |
1230 | ||
1231 | static bool spte_set_dirty(struct kvm *kvm, u64 *sptep) | |
1232 | { | |
1233 | u64 spte = *sptep; | |
1234 | ||
1235 | rmap_printk("rmap_set_dirty: spte %p %llx\n", sptep, *sptep); | |
1236 | ||
1237 | spte |= shadow_dirty_mask; | |
1238 | ||
1239 | return mmu_spte_update(sptep, spte); | |
1240 | } | |
1241 | ||
018aabb5 | 1242 | static bool __rmap_set_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head) |
f4b4b180 KH |
1243 | { |
1244 | u64 *sptep; | |
1245 | struct rmap_iterator iter; | |
1246 | bool flush = false; | |
1247 | ||
018aabb5 | 1248 | for_each_rmap_spte(rmap_head, &iter, sptep) |
f4b4b180 | 1249 | flush |= spte_set_dirty(kvm, sptep); |
f4b4b180 KH |
1250 | |
1251 | return flush; | |
1252 | } | |
1253 | ||
5dc99b23 | 1254 | /** |
3b0f1d01 | 1255 | * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages |
5dc99b23 TY |
1256 | * @kvm: kvm instance |
1257 | * @slot: slot to protect | |
1258 | * @gfn_offset: start of the BITS_PER_LONG pages we care about | |
1259 | * @mask: indicates which pages we should protect | |
1260 | * | |
1261 | * Used when we do not need to care about huge page mappings: e.g. during dirty | |
1262 | * logging we do not have any such mappings. | |
1263 | */ | |
3b0f1d01 | 1264 | static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm, |
5dc99b23 TY |
1265 | struct kvm_memory_slot *slot, |
1266 | gfn_t gfn_offset, unsigned long mask) | |
a0ed4607 | 1267 | { |
018aabb5 | 1268 | struct kvm_rmap_head *rmap_head; |
a0ed4607 | 1269 | |
5dc99b23 | 1270 | while (mask) { |
018aabb5 TY |
1271 | rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask), |
1272 | PT_PAGE_TABLE_LEVEL, slot); | |
1273 | __rmap_write_protect(kvm, rmap_head, false); | |
05da4558 | 1274 | |
5dc99b23 TY |
1275 | /* clear the first set bit */ |
1276 | mask &= mask - 1; | |
1277 | } | |
374cbac0 AK |
1278 | } |
1279 | ||
f4b4b180 KH |
1280 | /** |
1281 | * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages | |
1282 | * @kvm: kvm instance | |
1283 | * @slot: slot to clear D-bit | |
1284 | * @gfn_offset: start of the BITS_PER_LONG pages we care about | |
1285 | * @mask: indicates which pages we should clear D-bit | |
1286 | * | |
1287 | * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap. | |
1288 | */ | |
1289 | void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm, | |
1290 | struct kvm_memory_slot *slot, | |
1291 | gfn_t gfn_offset, unsigned long mask) | |
1292 | { | |
018aabb5 | 1293 | struct kvm_rmap_head *rmap_head; |
f4b4b180 KH |
1294 | |
1295 | while (mask) { | |
018aabb5 TY |
1296 | rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask), |
1297 | PT_PAGE_TABLE_LEVEL, slot); | |
1298 | __rmap_clear_dirty(kvm, rmap_head); | |
f4b4b180 KH |
1299 | |
1300 | /* clear the first set bit */ | |
1301 | mask &= mask - 1; | |
1302 | } | |
1303 | } | |
1304 | EXPORT_SYMBOL_GPL(kvm_mmu_clear_dirty_pt_masked); | |
1305 | ||
3b0f1d01 KH |
1306 | /** |
1307 | * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected | |
1308 | * PT level pages. | |
1309 | * | |
1310 | * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to | |
1311 | * enable dirty logging for them. | |
1312 | * | |
1313 | * Used when we do not need to care about huge page mappings: e.g. during dirty | |
1314 | * logging we do not have any such mappings. | |
1315 | */ | |
1316 | void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm, | |
1317 | struct kvm_memory_slot *slot, | |
1318 | gfn_t gfn_offset, unsigned long mask) | |
1319 | { | |
88178fd4 KH |
1320 | if (kvm_x86_ops->enable_log_dirty_pt_masked) |
1321 | kvm_x86_ops->enable_log_dirty_pt_masked(kvm, slot, gfn_offset, | |
1322 | mask); | |
1323 | else | |
1324 | kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask); | |
3b0f1d01 KH |
1325 | } |
1326 | ||
54bf36aa | 1327 | static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn) |
95d4c16c TY |
1328 | { |
1329 | struct kvm_memory_slot *slot; | |
018aabb5 | 1330 | struct kvm_rmap_head *rmap_head; |
5dc99b23 | 1331 | int i; |
2f84569f | 1332 | bool write_protected = false; |
95d4c16c | 1333 | |
54bf36aa | 1334 | slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
5dc99b23 | 1335 | |
8a3d08f1 | 1336 | for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) { |
018aabb5 TY |
1337 | rmap_head = __gfn_to_rmap(gfn, i, slot); |
1338 | write_protected |= __rmap_write_protect(vcpu->kvm, rmap_head, true); | |
5dc99b23 TY |
1339 | } |
1340 | ||
1341 | return write_protected; | |
95d4c16c TY |
1342 | } |
1343 | ||
018aabb5 | 1344 | static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head) |
e930bffe | 1345 | { |
1e3f42f0 TY |
1346 | u64 *sptep; |
1347 | struct rmap_iterator iter; | |
6a49f85c | 1348 | bool flush = false; |
e930bffe | 1349 | |
018aabb5 | 1350 | while ((sptep = rmap_get_first(rmap_head, &iter))) { |
6a49f85c | 1351 | rmap_printk("%s: spte %p %llx.\n", __func__, sptep, *sptep); |
1e3f42f0 TY |
1352 | |
1353 | drop_spte(kvm, sptep); | |
6a49f85c | 1354 | flush = true; |
e930bffe | 1355 | } |
1e3f42f0 | 1356 | |
6a49f85c XG |
1357 | return flush; |
1358 | } | |
1359 | ||
018aabb5 | 1360 | static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
6a49f85c XG |
1361 | struct kvm_memory_slot *slot, gfn_t gfn, int level, |
1362 | unsigned long data) | |
1363 | { | |
018aabb5 | 1364 | return kvm_zap_rmapp(kvm, rmap_head); |
e930bffe AA |
1365 | } |
1366 | ||
018aabb5 | 1367 | static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
8a9522d2 ALC |
1368 | struct kvm_memory_slot *slot, gfn_t gfn, int level, |
1369 | unsigned long data) | |
3da0dd43 | 1370 | { |
1e3f42f0 TY |
1371 | u64 *sptep; |
1372 | struct rmap_iterator iter; | |
3da0dd43 | 1373 | int need_flush = 0; |
1e3f42f0 | 1374 | u64 new_spte; |
3da0dd43 | 1375 | pte_t *ptep = (pte_t *)data; |
ba049e93 | 1376 | kvm_pfn_t new_pfn; |
3da0dd43 IE |
1377 | |
1378 | WARN_ON(pte_huge(*ptep)); | |
1379 | new_pfn = pte_pfn(*ptep); | |
1e3f42f0 | 1380 | |
0d536790 | 1381 | restart: |
018aabb5 | 1382 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
8a9522d2 ALC |
1383 | rmap_printk("kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n", |
1384 | sptep, *sptep, gfn, level); | |
1e3f42f0 | 1385 | |
3da0dd43 | 1386 | need_flush = 1; |
1e3f42f0 | 1387 | |
3da0dd43 | 1388 | if (pte_write(*ptep)) { |
1e3f42f0 | 1389 | drop_spte(kvm, sptep); |
0d536790 | 1390 | goto restart; |
3da0dd43 | 1391 | } else { |
1e3f42f0 | 1392 | new_spte = *sptep & ~PT64_BASE_ADDR_MASK; |
3da0dd43 IE |
1393 | new_spte |= (u64)new_pfn << PAGE_SHIFT; |
1394 | ||
1395 | new_spte &= ~PT_WRITABLE_MASK; | |
1396 | new_spte &= ~SPTE_HOST_WRITEABLE; | |
b79b93f9 | 1397 | new_spte &= ~shadow_accessed_mask; |
1e3f42f0 TY |
1398 | |
1399 | mmu_spte_clear_track_bits(sptep); | |
1400 | mmu_spte_set(sptep, new_spte); | |
3da0dd43 IE |
1401 | } |
1402 | } | |
1e3f42f0 | 1403 | |
3da0dd43 IE |
1404 | if (need_flush) |
1405 | kvm_flush_remote_tlbs(kvm); | |
1406 | ||
1407 | return 0; | |
1408 | } | |
1409 | ||
6ce1f4e2 XG |
1410 | struct slot_rmap_walk_iterator { |
1411 | /* input fields. */ | |
1412 | struct kvm_memory_slot *slot; | |
1413 | gfn_t start_gfn; | |
1414 | gfn_t end_gfn; | |
1415 | int start_level; | |
1416 | int end_level; | |
1417 | ||
1418 | /* output fields. */ | |
1419 | gfn_t gfn; | |
018aabb5 | 1420 | struct kvm_rmap_head *rmap; |
6ce1f4e2 XG |
1421 | int level; |
1422 | ||
1423 | /* private field. */ | |
018aabb5 | 1424 | struct kvm_rmap_head *end_rmap; |
6ce1f4e2 XG |
1425 | }; |
1426 | ||
1427 | static void | |
1428 | rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level) | |
1429 | { | |
1430 | iterator->level = level; | |
1431 | iterator->gfn = iterator->start_gfn; | |
1432 | iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot); | |
1433 | iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level, | |
1434 | iterator->slot); | |
1435 | } | |
1436 | ||
1437 | static void | |
1438 | slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator, | |
1439 | struct kvm_memory_slot *slot, int start_level, | |
1440 | int end_level, gfn_t start_gfn, gfn_t end_gfn) | |
1441 | { | |
1442 | iterator->slot = slot; | |
1443 | iterator->start_level = start_level; | |
1444 | iterator->end_level = end_level; | |
1445 | iterator->start_gfn = start_gfn; | |
1446 | iterator->end_gfn = end_gfn; | |
1447 | ||
1448 | rmap_walk_init_level(iterator, iterator->start_level); | |
1449 | } | |
1450 | ||
1451 | static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator) | |
1452 | { | |
1453 | return !!iterator->rmap; | |
1454 | } | |
1455 | ||
1456 | static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator) | |
1457 | { | |
1458 | if (++iterator->rmap <= iterator->end_rmap) { | |
1459 | iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level)); | |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | if (++iterator->level > iterator->end_level) { | |
1464 | iterator->rmap = NULL; | |
1465 | return; | |
1466 | } | |
1467 | ||
1468 | rmap_walk_init_level(iterator, iterator->level); | |
1469 | } | |
1470 | ||
1471 | #define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \ | |
1472 | _start_gfn, _end_gfn, _iter_) \ | |
1473 | for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \ | |
1474 | _end_level_, _start_gfn, _end_gfn); \ | |
1475 | slot_rmap_walk_okay(_iter_); \ | |
1476 | slot_rmap_walk_next(_iter_)) | |
1477 | ||
84504ef3 TY |
1478 | static int kvm_handle_hva_range(struct kvm *kvm, |
1479 | unsigned long start, | |
1480 | unsigned long end, | |
1481 | unsigned long data, | |
1482 | int (*handler)(struct kvm *kvm, | |
018aabb5 | 1483 | struct kvm_rmap_head *rmap_head, |
048212d0 | 1484 | struct kvm_memory_slot *slot, |
8a9522d2 ALC |
1485 | gfn_t gfn, |
1486 | int level, | |
84504ef3 | 1487 | unsigned long data)) |
e930bffe | 1488 | { |
bc6678a3 | 1489 | struct kvm_memslots *slots; |
be6ba0f0 | 1490 | struct kvm_memory_slot *memslot; |
6ce1f4e2 XG |
1491 | struct slot_rmap_walk_iterator iterator; |
1492 | int ret = 0; | |
9da0e4d5 | 1493 | int i; |
bc6678a3 | 1494 | |
9da0e4d5 PB |
1495 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
1496 | slots = __kvm_memslots(kvm, i); | |
1497 | kvm_for_each_memslot(memslot, slots) { | |
1498 | unsigned long hva_start, hva_end; | |
1499 | gfn_t gfn_start, gfn_end; | |
e930bffe | 1500 | |
9da0e4d5 PB |
1501 | hva_start = max(start, memslot->userspace_addr); |
1502 | hva_end = min(end, memslot->userspace_addr + | |
1503 | (memslot->npages << PAGE_SHIFT)); | |
1504 | if (hva_start >= hva_end) | |
1505 | continue; | |
1506 | /* | |
1507 | * {gfn(page) | page intersects with [hva_start, hva_end)} = | |
1508 | * {gfn_start, gfn_start+1, ..., gfn_end-1}. | |
1509 | */ | |
1510 | gfn_start = hva_to_gfn_memslot(hva_start, memslot); | |
1511 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); | |
1512 | ||
1513 | for_each_slot_rmap_range(memslot, PT_PAGE_TABLE_LEVEL, | |
1514 | PT_MAX_HUGEPAGE_LEVEL, | |
1515 | gfn_start, gfn_end - 1, | |
1516 | &iterator) | |
1517 | ret |= handler(kvm, iterator.rmap, memslot, | |
1518 | iterator.gfn, iterator.level, data); | |
1519 | } | |
e930bffe AA |
1520 | } |
1521 | ||
f395302e | 1522 | return ret; |
e930bffe AA |
1523 | } |
1524 | ||
84504ef3 TY |
1525 | static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, |
1526 | unsigned long data, | |
018aabb5 TY |
1527 | int (*handler)(struct kvm *kvm, |
1528 | struct kvm_rmap_head *rmap_head, | |
048212d0 | 1529 | struct kvm_memory_slot *slot, |
8a9522d2 | 1530 | gfn_t gfn, int level, |
84504ef3 TY |
1531 | unsigned long data)) |
1532 | { | |
1533 | return kvm_handle_hva_range(kvm, hva, hva + 1, data, handler); | |
e930bffe AA |
1534 | } |
1535 | ||
1536 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) | |
1537 | { | |
3da0dd43 IE |
1538 | return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp); |
1539 | } | |
1540 | ||
b3ae2096 TY |
1541 | int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end) |
1542 | { | |
1543 | return kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp); | |
1544 | } | |
1545 | ||
3da0dd43 IE |
1546 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) |
1547 | { | |
8a8365c5 | 1548 | kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp); |
e930bffe AA |
1549 | } |
1550 | ||
018aabb5 | 1551 | static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
8a9522d2 ALC |
1552 | struct kvm_memory_slot *slot, gfn_t gfn, int level, |
1553 | unsigned long data) | |
e930bffe | 1554 | { |
1e3f42f0 | 1555 | u64 *sptep; |
79f702a6 | 1556 | struct rmap_iterator uninitialized_var(iter); |
e930bffe AA |
1557 | int young = 0; |
1558 | ||
57128468 | 1559 | BUG_ON(!shadow_accessed_mask); |
534e38b4 | 1560 | |
018aabb5 | 1561 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
3f6d8c8a | 1562 | if (*sptep & shadow_accessed_mask) { |
e930bffe | 1563 | young = 1; |
3f6d8c8a XH |
1564 | clear_bit((ffs(shadow_accessed_mask) - 1), |
1565 | (unsigned long *)sptep); | |
e930bffe | 1566 | } |
018aabb5 | 1567 | } |
0d536790 | 1568 | |
8a9522d2 | 1569 | trace_kvm_age_page(gfn, level, slot, young); |
e930bffe AA |
1570 | return young; |
1571 | } | |
1572 | ||
018aabb5 | 1573 | static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
8a9522d2 ALC |
1574 | struct kvm_memory_slot *slot, gfn_t gfn, |
1575 | int level, unsigned long data) | |
8ee53820 | 1576 | { |
1e3f42f0 TY |
1577 | u64 *sptep; |
1578 | struct rmap_iterator iter; | |
8ee53820 AA |
1579 | int young = 0; |
1580 | ||
1581 | /* | |
1582 | * If there's no access bit in the secondary pte set by the | |
1583 | * hardware it's up to gup-fast/gup to set the access bit in | |
1584 | * the primary pte or in the page structure. | |
1585 | */ | |
1586 | if (!shadow_accessed_mask) | |
1587 | goto out; | |
1588 | ||
018aabb5 | 1589 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
3f6d8c8a | 1590 | if (*sptep & shadow_accessed_mask) { |
8ee53820 AA |
1591 | young = 1; |
1592 | break; | |
1593 | } | |
018aabb5 | 1594 | } |
8ee53820 AA |
1595 | out: |
1596 | return young; | |
1597 | } | |
1598 | ||
53a27b39 MT |
1599 | #define RMAP_RECYCLE_THRESHOLD 1000 |
1600 | ||
852e3c19 | 1601 | static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
53a27b39 | 1602 | { |
018aabb5 | 1603 | struct kvm_rmap_head *rmap_head; |
852e3c19 JR |
1604 | struct kvm_mmu_page *sp; |
1605 | ||
1606 | sp = page_header(__pa(spte)); | |
53a27b39 | 1607 | |
018aabb5 | 1608 | rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp); |
53a27b39 | 1609 | |
018aabb5 | 1610 | kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, 0); |
53a27b39 MT |
1611 | kvm_flush_remote_tlbs(vcpu->kvm); |
1612 | } | |
1613 | ||
57128468 | 1614 | int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end) |
e930bffe | 1615 | { |
57128468 ALC |
1616 | /* |
1617 | * In case of absence of EPT Access and Dirty Bits supports, | |
1618 | * emulate the accessed bit for EPT, by checking if this page has | |
1619 | * an EPT mapping, and clearing it if it does. On the next access, | |
1620 | * a new EPT mapping will be established. | |
1621 | * This has some overhead, but not as much as the cost of swapping | |
1622 | * out actively used pages or breaking up actively used hugepages. | |
1623 | */ | |
1624 | if (!shadow_accessed_mask) { | |
1625 | /* | |
1626 | * We are holding the kvm->mmu_lock, and we are blowing up | |
1627 | * shadow PTEs. MMU notifier consumers need to be kept at bay. | |
1628 | * This is correct as long as we don't decouple the mmu_lock | |
1629 | * protected regions (like invalidate_range_start|end does). | |
1630 | */ | |
1631 | kvm->mmu_notifier_seq++; | |
1632 | return kvm_handle_hva_range(kvm, start, end, 0, | |
1633 | kvm_unmap_rmapp); | |
1634 | } | |
1635 | ||
1636 | return kvm_handle_hva_range(kvm, start, end, 0, kvm_age_rmapp); | |
e930bffe AA |
1637 | } |
1638 | ||
8ee53820 AA |
1639 | int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) |
1640 | { | |
1641 | return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp); | |
1642 | } | |
1643 | ||
d6c69ee9 | 1644 | #ifdef MMU_DEBUG |
47ad8e68 | 1645 | static int is_empty_shadow_page(u64 *spt) |
6aa8b732 | 1646 | { |
139bdb2d AK |
1647 | u64 *pos; |
1648 | u64 *end; | |
1649 | ||
47ad8e68 | 1650 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
3c915510 | 1651 | if (is_shadow_present_pte(*pos)) { |
b8688d51 | 1652 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
139bdb2d | 1653 | pos, *pos); |
6aa8b732 | 1654 | return 0; |
139bdb2d | 1655 | } |
6aa8b732 AK |
1656 | return 1; |
1657 | } | |
d6c69ee9 | 1658 | #endif |
6aa8b732 | 1659 | |
45221ab6 DH |
1660 | /* |
1661 | * This value is the sum of all of the kvm instances's | |
1662 | * kvm->arch.n_used_mmu_pages values. We need a global, | |
1663 | * aggregate version in order to make the slab shrinker | |
1664 | * faster | |
1665 | */ | |
1666 | static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr) | |
1667 | { | |
1668 | kvm->arch.n_used_mmu_pages += nr; | |
1669 | percpu_counter_add(&kvm_total_used_mmu_pages, nr); | |
1670 | } | |
1671 | ||
834be0d8 | 1672 | static void kvm_mmu_free_page(struct kvm_mmu_page *sp) |
260746c0 | 1673 | { |
fa4a2c08 | 1674 | MMU_WARN_ON(!is_empty_shadow_page(sp->spt)); |
7775834a | 1675 | hlist_del(&sp->hash_link); |
bd4c86ea XG |
1676 | list_del(&sp->link); |
1677 | free_page((unsigned long)sp->spt); | |
834be0d8 GN |
1678 | if (!sp->role.direct) |
1679 | free_page((unsigned long)sp->gfns); | |
e8ad9a70 | 1680 | kmem_cache_free(mmu_page_header_cache, sp); |
260746c0 AK |
1681 | } |
1682 | ||
cea0f0e7 AK |
1683 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
1684 | { | |
1ae0a13d | 1685 | return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1); |
cea0f0e7 AK |
1686 | } |
1687 | ||
714b93da | 1688 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
4db35314 | 1689 | struct kvm_mmu_page *sp, u64 *parent_pte) |
cea0f0e7 | 1690 | { |
cea0f0e7 AK |
1691 | if (!parent_pte) |
1692 | return; | |
cea0f0e7 | 1693 | |
67052b35 | 1694 | pte_list_add(vcpu, parent_pte, &sp->parent_ptes); |
cea0f0e7 AK |
1695 | } |
1696 | ||
4db35314 | 1697 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
cea0f0e7 AK |
1698 | u64 *parent_pte) |
1699 | { | |
67052b35 | 1700 | pte_list_remove(parent_pte, &sp->parent_ptes); |
cea0f0e7 AK |
1701 | } |
1702 | ||
bcdd9a93 XG |
1703 | static void drop_parent_pte(struct kvm_mmu_page *sp, |
1704 | u64 *parent_pte) | |
1705 | { | |
1706 | mmu_page_remove_parent_pte(sp, parent_pte); | |
1df9f2dc | 1707 | mmu_spte_clear_no_track(parent_pte); |
bcdd9a93 XG |
1708 | } |
1709 | ||
47005792 | 1710 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct) |
ad8cfbe3 | 1711 | { |
67052b35 | 1712 | struct kvm_mmu_page *sp; |
7ddca7e4 | 1713 | |
80feb89a TY |
1714 | sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache); |
1715 | sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache); | |
67052b35 | 1716 | if (!direct) |
80feb89a | 1717 | sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache); |
67052b35 | 1718 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
5304b8d3 XG |
1719 | |
1720 | /* | |
1721 | * The active_mmu_pages list is the FIFO list, do not move the | |
1722 | * page until it is zapped. kvm_zap_obsolete_pages depends on | |
1723 | * this feature. See the comments in kvm_zap_obsolete_pages(). | |
1724 | */ | |
67052b35 | 1725 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
67052b35 XG |
1726 | kvm_mod_used_mmu_pages(vcpu->kvm, +1); |
1727 | return sp; | |
ad8cfbe3 MT |
1728 | } |
1729 | ||
67052b35 | 1730 | static void mark_unsync(u64 *spte); |
1047df1f | 1731 | static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp) |
0074ff63 | 1732 | { |
74c4e63a TY |
1733 | u64 *sptep; |
1734 | struct rmap_iterator iter; | |
1735 | ||
1736 | for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) { | |
1737 | mark_unsync(sptep); | |
1738 | } | |
0074ff63 MT |
1739 | } |
1740 | ||
67052b35 | 1741 | static void mark_unsync(u64 *spte) |
0074ff63 | 1742 | { |
67052b35 | 1743 | struct kvm_mmu_page *sp; |
1047df1f | 1744 | unsigned int index; |
0074ff63 | 1745 | |
67052b35 | 1746 | sp = page_header(__pa(spte)); |
1047df1f XG |
1747 | index = spte - sp->spt; |
1748 | if (__test_and_set_bit(index, sp->unsync_child_bitmap)) | |
0074ff63 | 1749 | return; |
1047df1f | 1750 | if (sp->unsync_children++) |
0074ff63 | 1751 | return; |
1047df1f | 1752 | kvm_mmu_mark_parents_unsync(sp); |
0074ff63 MT |
1753 | } |
1754 | ||
e8bc217a | 1755 | static int nonpaging_sync_page(struct kvm_vcpu *vcpu, |
a4a8e6f7 | 1756 | struct kvm_mmu_page *sp) |
e8bc217a MT |
1757 | { |
1758 | return 1; | |
1759 | } | |
1760 | ||
a7052897 MT |
1761 | static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
1762 | { | |
1763 | } | |
1764 | ||
0f53b5b1 XG |
1765 | static void nonpaging_update_pte(struct kvm_vcpu *vcpu, |
1766 | struct kvm_mmu_page *sp, u64 *spte, | |
7c562522 | 1767 | const void *pte) |
0f53b5b1 XG |
1768 | { |
1769 | WARN_ON(1); | |
1770 | } | |
1771 | ||
60c8aec6 MT |
1772 | #define KVM_PAGE_ARRAY_NR 16 |
1773 | ||
1774 | struct kvm_mmu_pages { | |
1775 | struct mmu_page_and_offset { | |
1776 | struct kvm_mmu_page *sp; | |
1777 | unsigned int idx; | |
1778 | } page[KVM_PAGE_ARRAY_NR]; | |
1779 | unsigned int nr; | |
1780 | }; | |
1781 | ||
cded19f3 HE |
1782 | static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp, |
1783 | int idx) | |
4731d4c7 | 1784 | { |
60c8aec6 | 1785 | int i; |
4731d4c7 | 1786 | |
60c8aec6 MT |
1787 | if (sp->unsync) |
1788 | for (i=0; i < pvec->nr; i++) | |
1789 | if (pvec->page[i].sp == sp) | |
1790 | return 0; | |
1791 | ||
1792 | pvec->page[pvec->nr].sp = sp; | |
1793 | pvec->page[pvec->nr].idx = idx; | |
1794 | pvec->nr++; | |
1795 | return (pvec->nr == KVM_PAGE_ARRAY_NR); | |
1796 | } | |
1797 | ||
fd951457 TY |
1798 | static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx) |
1799 | { | |
1800 | --sp->unsync_children; | |
1801 | WARN_ON((int)sp->unsync_children < 0); | |
1802 | __clear_bit(idx, sp->unsync_child_bitmap); | |
1803 | } | |
1804 | ||
60c8aec6 MT |
1805 | static int __mmu_unsync_walk(struct kvm_mmu_page *sp, |
1806 | struct kvm_mmu_pages *pvec) | |
1807 | { | |
1808 | int i, ret, nr_unsync_leaf = 0; | |
4731d4c7 | 1809 | |
37178b8b | 1810 | for_each_set_bit(i, sp->unsync_child_bitmap, 512) { |
7a8f1a74 | 1811 | struct kvm_mmu_page *child; |
4731d4c7 MT |
1812 | u64 ent = sp->spt[i]; |
1813 | ||
fd951457 TY |
1814 | if (!is_shadow_present_pte(ent) || is_large_pte(ent)) { |
1815 | clear_unsync_child_bit(sp, i); | |
1816 | continue; | |
1817 | } | |
7a8f1a74 XG |
1818 | |
1819 | child = page_header(ent & PT64_BASE_ADDR_MASK); | |
1820 | ||
1821 | if (child->unsync_children) { | |
1822 | if (mmu_pages_add(pvec, child, i)) | |
1823 | return -ENOSPC; | |
1824 | ||
1825 | ret = __mmu_unsync_walk(child, pvec); | |
fd951457 TY |
1826 | if (!ret) { |
1827 | clear_unsync_child_bit(sp, i); | |
1828 | continue; | |
1829 | } else if (ret > 0) { | |
7a8f1a74 | 1830 | nr_unsync_leaf += ret; |
fd951457 | 1831 | } else |
7a8f1a74 XG |
1832 | return ret; |
1833 | } else if (child->unsync) { | |
1834 | nr_unsync_leaf++; | |
1835 | if (mmu_pages_add(pvec, child, i)) | |
1836 | return -ENOSPC; | |
1837 | } else | |
fd951457 | 1838 | clear_unsync_child_bit(sp, i); |
4731d4c7 MT |
1839 | } |
1840 | ||
60c8aec6 MT |
1841 | return nr_unsync_leaf; |
1842 | } | |
1843 | ||
1844 | static int mmu_unsync_walk(struct kvm_mmu_page *sp, | |
1845 | struct kvm_mmu_pages *pvec) | |
1846 | { | |
1847 | if (!sp->unsync_children) | |
1848 | return 0; | |
1849 | ||
1850 | mmu_pages_add(pvec, sp, 0); | |
1851 | return __mmu_unsync_walk(sp, pvec); | |
4731d4c7 MT |
1852 | } |
1853 | ||
4731d4c7 MT |
1854 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
1855 | { | |
1856 | WARN_ON(!sp->unsync); | |
5e1b3ddb | 1857 | trace_kvm_mmu_sync_page(sp); |
4731d4c7 MT |
1858 | sp->unsync = 0; |
1859 | --kvm->stat.mmu_unsync; | |
1860 | } | |
1861 | ||
7775834a XG |
1862 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
1863 | struct list_head *invalid_list); | |
1864 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, | |
1865 | struct list_head *invalid_list); | |
4731d4c7 | 1866 | |
f34d251d XG |
1867 | /* |
1868 | * NOTE: we should pay more attention on the zapped-obsolete page | |
1869 | * (is_obsolete_sp(sp) && sp->role.invalid) when you do hash list walk | |
1870 | * since it has been deleted from active_mmu_pages but still can be found | |
1871 | * at hast list. | |
1872 | * | |
1873 | * for_each_gfn_indirect_valid_sp has skipped that kind of page and | |
1874 | * kvm_mmu_get_page(), the only user of for_each_gfn_sp(), has skipped | |
1875 | * all the obsolete pages. | |
1876 | */ | |
1044b030 TY |
1877 | #define for_each_gfn_sp(_kvm, _sp, _gfn) \ |
1878 | hlist_for_each_entry(_sp, \ | |
1879 | &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \ | |
1880 | if ((_sp)->gfn != (_gfn)) {} else | |
1881 | ||
1882 | #define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \ | |
1883 | for_each_gfn_sp(_kvm, _sp, _gfn) \ | |
1884 | if ((_sp)->role.direct || (_sp)->role.invalid) {} else | |
7ae680eb | 1885 | |
f918b443 | 1886 | /* @sp->gfn should be write-protected at the call site */ |
1d9dc7e0 | 1887 | static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
d98ba053 | 1888 | struct list_head *invalid_list, bool clear_unsync) |
4731d4c7 | 1889 | { |
5b7e0102 | 1890 | if (sp->role.cr4_pae != !!is_pae(vcpu)) { |
d98ba053 | 1891 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
4731d4c7 MT |
1892 | return 1; |
1893 | } | |
1894 | ||
f918b443 | 1895 | if (clear_unsync) |
1d9dc7e0 | 1896 | kvm_unlink_unsync_page(vcpu->kvm, sp); |
1d9dc7e0 | 1897 | |
a4a8e6f7 | 1898 | if (vcpu->arch.mmu.sync_page(vcpu, sp)) { |
d98ba053 | 1899 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
4731d4c7 MT |
1900 | return 1; |
1901 | } | |
1902 | ||
77c3913b | 1903 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
4731d4c7 MT |
1904 | return 0; |
1905 | } | |
1906 | ||
1d9dc7e0 XG |
1907 | static int kvm_sync_page_transient(struct kvm_vcpu *vcpu, |
1908 | struct kvm_mmu_page *sp) | |
1909 | { | |
d98ba053 | 1910 | LIST_HEAD(invalid_list); |
1d9dc7e0 XG |
1911 | int ret; |
1912 | ||
d98ba053 | 1913 | ret = __kvm_sync_page(vcpu, sp, &invalid_list, false); |
be71e061 | 1914 | if (ret) |
d98ba053 XG |
1915 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
1916 | ||
1d9dc7e0 XG |
1917 | return ret; |
1918 | } | |
1919 | ||
e37fa785 XG |
1920 | #ifdef CONFIG_KVM_MMU_AUDIT |
1921 | #include "mmu_audit.c" | |
1922 | #else | |
1923 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { } | |
1924 | static void mmu_audit_disable(void) { } | |
1925 | #endif | |
1926 | ||
d98ba053 XG |
1927 | static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
1928 | struct list_head *invalid_list) | |
1d9dc7e0 | 1929 | { |
d98ba053 | 1930 | return __kvm_sync_page(vcpu, sp, invalid_list, true); |
1d9dc7e0 XG |
1931 | } |
1932 | ||
9f1a122f XG |
1933 | /* @gfn should be write-protected at the call site */ |
1934 | static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) | |
1935 | { | |
9f1a122f | 1936 | struct kvm_mmu_page *s; |
d98ba053 | 1937 | LIST_HEAD(invalid_list); |
9f1a122f XG |
1938 | bool flush = false; |
1939 | ||
b67bfe0d | 1940 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) { |
7ae680eb | 1941 | if (!s->unsync) |
9f1a122f XG |
1942 | continue; |
1943 | ||
1944 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); | |
a4a8e6f7 | 1945 | kvm_unlink_unsync_page(vcpu->kvm, s); |
9f1a122f | 1946 | if ((s->role.cr4_pae != !!is_pae(vcpu)) || |
a4a8e6f7 | 1947 | (vcpu->arch.mmu.sync_page(vcpu, s))) { |
d98ba053 | 1948 | kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list); |
9f1a122f XG |
1949 | continue; |
1950 | } | |
9f1a122f XG |
1951 | flush = true; |
1952 | } | |
1953 | ||
d98ba053 | 1954 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
9f1a122f | 1955 | if (flush) |
77c3913b | 1956 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
9f1a122f XG |
1957 | } |
1958 | ||
60c8aec6 MT |
1959 | struct mmu_page_path { |
1960 | struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1]; | |
1961 | unsigned int idx[PT64_ROOT_LEVEL-1]; | |
4731d4c7 MT |
1962 | }; |
1963 | ||
60c8aec6 MT |
1964 | #define for_each_sp(pvec, sp, parents, i) \ |
1965 | for (i = mmu_pages_next(&pvec, &parents, -1), \ | |
1966 | sp = pvec.page[i].sp; \ | |
1967 | i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \ | |
1968 | i = mmu_pages_next(&pvec, &parents, i)) | |
1969 | ||
cded19f3 HE |
1970 | static int mmu_pages_next(struct kvm_mmu_pages *pvec, |
1971 | struct mmu_page_path *parents, | |
1972 | int i) | |
60c8aec6 MT |
1973 | { |
1974 | int n; | |
1975 | ||
1976 | for (n = i+1; n < pvec->nr; n++) { | |
1977 | struct kvm_mmu_page *sp = pvec->page[n].sp; | |
1978 | ||
1979 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { | |
1980 | parents->idx[0] = pvec->page[n].idx; | |
1981 | return n; | |
1982 | } | |
1983 | ||
1984 | parents->parent[sp->role.level-2] = sp; | |
1985 | parents->idx[sp->role.level-1] = pvec->page[n].idx; | |
1986 | } | |
1987 | ||
1988 | return n; | |
1989 | } | |
1990 | ||
cded19f3 | 1991 | static void mmu_pages_clear_parents(struct mmu_page_path *parents) |
4731d4c7 | 1992 | { |
60c8aec6 MT |
1993 | struct kvm_mmu_page *sp; |
1994 | unsigned int level = 0; | |
1995 | ||
1996 | do { | |
1997 | unsigned int idx = parents->idx[level]; | |
4731d4c7 | 1998 | |
60c8aec6 MT |
1999 | sp = parents->parent[level]; |
2000 | if (!sp) | |
2001 | return; | |
2002 | ||
fd951457 | 2003 | clear_unsync_child_bit(sp, idx); |
60c8aec6 MT |
2004 | level++; |
2005 | } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children); | |
4731d4c7 MT |
2006 | } |
2007 | ||
60c8aec6 MT |
2008 | static void kvm_mmu_pages_init(struct kvm_mmu_page *parent, |
2009 | struct mmu_page_path *parents, | |
2010 | struct kvm_mmu_pages *pvec) | |
4731d4c7 | 2011 | { |
60c8aec6 MT |
2012 | parents->parent[parent->role.level-1] = NULL; |
2013 | pvec->nr = 0; | |
2014 | } | |
4731d4c7 | 2015 | |
60c8aec6 MT |
2016 | static void mmu_sync_children(struct kvm_vcpu *vcpu, |
2017 | struct kvm_mmu_page *parent) | |
2018 | { | |
2019 | int i; | |
2020 | struct kvm_mmu_page *sp; | |
2021 | struct mmu_page_path parents; | |
2022 | struct kvm_mmu_pages pages; | |
d98ba053 | 2023 | LIST_HEAD(invalid_list); |
60c8aec6 MT |
2024 | |
2025 | kvm_mmu_pages_init(parent, &parents, &pages); | |
2026 | while (mmu_unsync_walk(parent, &pages)) { | |
2f84569f | 2027 | bool protected = false; |
b1a36821 MT |
2028 | |
2029 | for_each_sp(pages, sp, parents, i) | |
54bf36aa | 2030 | protected |= rmap_write_protect(vcpu, sp->gfn); |
b1a36821 MT |
2031 | |
2032 | if (protected) | |
2033 | kvm_flush_remote_tlbs(vcpu->kvm); | |
2034 | ||
60c8aec6 | 2035 | for_each_sp(pages, sp, parents, i) { |
d98ba053 | 2036 | kvm_sync_page(vcpu, sp, &invalid_list); |
60c8aec6 MT |
2037 | mmu_pages_clear_parents(&parents); |
2038 | } | |
d98ba053 | 2039 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
4731d4c7 | 2040 | cond_resched_lock(&vcpu->kvm->mmu_lock); |
60c8aec6 MT |
2041 | kvm_mmu_pages_init(parent, &parents, &pages); |
2042 | } | |
4731d4c7 MT |
2043 | } |
2044 | ||
a30f47cb XG |
2045 | static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp) |
2046 | { | |
2047 | sp->write_flooding_count = 0; | |
2048 | } | |
2049 | ||
2050 | static void clear_sp_write_flooding_count(u64 *spte) | |
2051 | { | |
2052 | struct kvm_mmu_page *sp = page_header(__pa(spte)); | |
2053 | ||
2054 | __clear_sp_write_flooding_count(sp); | |
2055 | } | |
2056 | ||
5304b8d3 XG |
2057 | static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp) |
2058 | { | |
2059 | return unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen); | |
2060 | } | |
2061 | ||
cea0f0e7 AK |
2062 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
2063 | gfn_t gfn, | |
2064 | gva_t gaddr, | |
2065 | unsigned level, | |
f6e2c02b | 2066 | int direct, |
bb11c6c9 | 2067 | unsigned access) |
cea0f0e7 AK |
2068 | { |
2069 | union kvm_mmu_page_role role; | |
cea0f0e7 | 2070 | unsigned quadrant; |
9f1a122f | 2071 | struct kvm_mmu_page *sp; |
9f1a122f | 2072 | bool need_sync = false; |
cea0f0e7 | 2073 | |
a770f6f2 | 2074 | role = vcpu->arch.mmu.base_role; |
cea0f0e7 | 2075 | role.level = level; |
f6e2c02b | 2076 | role.direct = direct; |
84b0c8c6 | 2077 | if (role.direct) |
5b7e0102 | 2078 | role.cr4_pae = 0; |
41074d07 | 2079 | role.access = access; |
c5a78f2b JR |
2080 | if (!vcpu->arch.mmu.direct_map |
2081 | && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { | |
cea0f0e7 AK |
2082 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
2083 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; | |
2084 | role.quadrant = quadrant; | |
2085 | } | |
b67bfe0d | 2086 | for_each_gfn_sp(vcpu->kvm, sp, gfn) { |
7f52af74 XG |
2087 | if (is_obsolete_sp(vcpu->kvm, sp)) |
2088 | continue; | |
2089 | ||
7ae680eb XG |
2090 | if (!need_sync && sp->unsync) |
2091 | need_sync = true; | |
4731d4c7 | 2092 | |
7ae680eb XG |
2093 | if (sp->role.word != role.word) |
2094 | continue; | |
4731d4c7 | 2095 | |
7ae680eb XG |
2096 | if (sp->unsync && kvm_sync_page_transient(vcpu, sp)) |
2097 | break; | |
e02aa901 | 2098 | |
98bba238 | 2099 | if (sp->unsync_children) |
a8eeb04a | 2100 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
e02aa901 | 2101 | |
a30f47cb | 2102 | __clear_sp_write_flooding_count(sp); |
7ae680eb XG |
2103 | trace_kvm_mmu_get_page(sp, false); |
2104 | return sp; | |
2105 | } | |
47005792 | 2106 | |
dfc5aa00 | 2107 | ++vcpu->kvm->stat.mmu_cache_miss; |
47005792 TY |
2108 | |
2109 | sp = kvm_mmu_alloc_page(vcpu, direct); | |
2110 | ||
4db35314 AK |
2111 | sp->gfn = gfn; |
2112 | sp->role = role; | |
7ae680eb XG |
2113 | hlist_add_head(&sp->hash_link, |
2114 | &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]); | |
f6e2c02b | 2115 | if (!direct) { |
54bf36aa | 2116 | if (rmap_write_protect(vcpu, gfn)) |
b1a36821 | 2117 | kvm_flush_remote_tlbs(vcpu->kvm); |
9f1a122f XG |
2118 | if (level > PT_PAGE_TABLE_LEVEL && need_sync) |
2119 | kvm_sync_pages(vcpu, gfn); | |
2120 | ||
3ed1a478 | 2121 | account_shadowed(vcpu->kvm, sp); |
4731d4c7 | 2122 | } |
5304b8d3 | 2123 | sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen; |
77492664 | 2124 | clear_page(sp->spt); |
f691fe1d | 2125 | trace_kvm_mmu_get_page(sp, true); |
4db35314 | 2126 | return sp; |
cea0f0e7 AK |
2127 | } |
2128 | ||
2d11123a AK |
2129 | static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator, |
2130 | struct kvm_vcpu *vcpu, u64 addr) | |
2131 | { | |
2132 | iterator->addr = addr; | |
2133 | iterator->shadow_addr = vcpu->arch.mmu.root_hpa; | |
2134 | iterator->level = vcpu->arch.mmu.shadow_root_level; | |
81407ca5 JR |
2135 | |
2136 | if (iterator->level == PT64_ROOT_LEVEL && | |
2137 | vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL && | |
2138 | !vcpu->arch.mmu.direct_map) | |
2139 | --iterator->level; | |
2140 | ||
2d11123a AK |
2141 | if (iterator->level == PT32E_ROOT_LEVEL) { |
2142 | iterator->shadow_addr | |
2143 | = vcpu->arch.mmu.pae_root[(addr >> 30) & 3]; | |
2144 | iterator->shadow_addr &= PT64_BASE_ADDR_MASK; | |
2145 | --iterator->level; | |
2146 | if (!iterator->shadow_addr) | |
2147 | iterator->level = 0; | |
2148 | } | |
2149 | } | |
2150 | ||
2151 | static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator) | |
2152 | { | |
2153 | if (iterator->level < PT_PAGE_TABLE_LEVEL) | |
2154 | return false; | |
4d88954d | 2155 | |
2d11123a AK |
2156 | iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level); |
2157 | iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index; | |
2158 | return true; | |
2159 | } | |
2160 | ||
c2a2ac2b XG |
2161 | static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator, |
2162 | u64 spte) | |
2d11123a | 2163 | { |
c2a2ac2b | 2164 | if (is_last_spte(spte, iterator->level)) { |
052331be XG |
2165 | iterator->level = 0; |
2166 | return; | |
2167 | } | |
2168 | ||
c2a2ac2b | 2169 | iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK; |
2d11123a AK |
2170 | --iterator->level; |
2171 | } | |
2172 | ||
c2a2ac2b XG |
2173 | static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator) |
2174 | { | |
2175 | return __shadow_walk_next(iterator, *iterator->sptep); | |
2176 | } | |
2177 | ||
98bba238 TY |
2178 | static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep, |
2179 | struct kvm_mmu_page *sp) | |
32ef26a3 AK |
2180 | { |
2181 | u64 spte; | |
2182 | ||
7a1638ce YZ |
2183 | BUILD_BUG_ON(VMX_EPT_READABLE_MASK != PT_PRESENT_MASK || |
2184 | VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK); | |
2185 | ||
24db2734 | 2186 | spte = __pa(sp->spt) | PT_PRESENT_MASK | PT_WRITABLE_MASK | |
0e3d0648 | 2187 | shadow_user_mask | shadow_x_mask | shadow_accessed_mask; |
24db2734 | 2188 | |
1df9f2dc | 2189 | mmu_spte_set(sptep, spte); |
98bba238 TY |
2190 | |
2191 | mmu_page_add_parent_pte(vcpu, sp, sptep); | |
2192 | ||
2193 | if (sp->unsync_children || sp->unsync) | |
2194 | mark_unsync(sptep); | |
32ef26a3 AK |
2195 | } |
2196 | ||
a357bd22 AK |
2197 | static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
2198 | unsigned direct_access) | |
2199 | { | |
2200 | if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) { | |
2201 | struct kvm_mmu_page *child; | |
2202 | ||
2203 | /* | |
2204 | * For the direct sp, if the guest pte's dirty bit | |
2205 | * changed form clean to dirty, it will corrupt the | |
2206 | * sp's access: allow writable in the read-only sp, | |
2207 | * so we should update the spte at this point to get | |
2208 | * a new sp with the correct access. | |
2209 | */ | |
2210 | child = page_header(*sptep & PT64_BASE_ADDR_MASK); | |
2211 | if (child->role.access == direct_access) | |
2212 | return; | |
2213 | ||
bcdd9a93 | 2214 | drop_parent_pte(child, sptep); |
a357bd22 AK |
2215 | kvm_flush_remote_tlbs(vcpu->kvm); |
2216 | } | |
2217 | } | |
2218 | ||
505aef8f | 2219 | static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, |
38e3b2b2 XG |
2220 | u64 *spte) |
2221 | { | |
2222 | u64 pte; | |
2223 | struct kvm_mmu_page *child; | |
2224 | ||
2225 | pte = *spte; | |
2226 | if (is_shadow_present_pte(pte)) { | |
505aef8f | 2227 | if (is_last_spte(pte, sp->role.level)) { |
c3707958 | 2228 | drop_spte(kvm, spte); |
505aef8f XG |
2229 | if (is_large_pte(pte)) |
2230 | --kvm->stat.lpages; | |
2231 | } else { | |
38e3b2b2 | 2232 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
bcdd9a93 | 2233 | drop_parent_pte(child, spte); |
38e3b2b2 | 2234 | } |
505aef8f XG |
2235 | return true; |
2236 | } | |
2237 | ||
2238 | if (is_mmio_spte(pte)) | |
ce88decf | 2239 | mmu_spte_clear_no_track(spte); |
c3707958 | 2240 | |
505aef8f | 2241 | return false; |
38e3b2b2 XG |
2242 | } |
2243 | ||
90cb0529 | 2244 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
4db35314 | 2245 | struct kvm_mmu_page *sp) |
a436036b | 2246 | { |
697fe2e2 | 2247 | unsigned i; |
697fe2e2 | 2248 | |
38e3b2b2 XG |
2249 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
2250 | mmu_page_zap_pte(kvm, sp, sp->spt + i); | |
a436036b AK |
2251 | } |
2252 | ||
31aa2b44 | 2253 | static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp) |
a436036b | 2254 | { |
1e3f42f0 TY |
2255 | u64 *sptep; |
2256 | struct rmap_iterator iter; | |
a436036b | 2257 | |
018aabb5 | 2258 | while ((sptep = rmap_get_first(&sp->parent_ptes, &iter))) |
1e3f42f0 | 2259 | drop_parent_pte(sp, sptep); |
31aa2b44 AK |
2260 | } |
2261 | ||
60c8aec6 | 2262 | static int mmu_zap_unsync_children(struct kvm *kvm, |
7775834a XG |
2263 | struct kvm_mmu_page *parent, |
2264 | struct list_head *invalid_list) | |
4731d4c7 | 2265 | { |
60c8aec6 MT |
2266 | int i, zapped = 0; |
2267 | struct mmu_page_path parents; | |
2268 | struct kvm_mmu_pages pages; | |
4731d4c7 | 2269 | |
60c8aec6 | 2270 | if (parent->role.level == PT_PAGE_TABLE_LEVEL) |
4731d4c7 | 2271 | return 0; |
60c8aec6 MT |
2272 | |
2273 | kvm_mmu_pages_init(parent, &parents, &pages); | |
2274 | while (mmu_unsync_walk(parent, &pages)) { | |
2275 | struct kvm_mmu_page *sp; | |
2276 | ||
2277 | for_each_sp(pages, sp, parents, i) { | |
7775834a | 2278 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
60c8aec6 | 2279 | mmu_pages_clear_parents(&parents); |
77662e00 | 2280 | zapped++; |
60c8aec6 | 2281 | } |
60c8aec6 MT |
2282 | kvm_mmu_pages_init(parent, &parents, &pages); |
2283 | } | |
2284 | ||
2285 | return zapped; | |
4731d4c7 MT |
2286 | } |
2287 | ||
7775834a XG |
2288 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
2289 | struct list_head *invalid_list) | |
31aa2b44 | 2290 | { |
4731d4c7 | 2291 | int ret; |
f691fe1d | 2292 | |
7775834a | 2293 | trace_kvm_mmu_prepare_zap_page(sp); |
31aa2b44 | 2294 | ++kvm->stat.mmu_shadow_zapped; |
7775834a | 2295 | ret = mmu_zap_unsync_children(kvm, sp, invalid_list); |
4db35314 | 2296 | kvm_mmu_page_unlink_children(kvm, sp); |
31aa2b44 | 2297 | kvm_mmu_unlink_parents(kvm, sp); |
5304b8d3 | 2298 | |
f6e2c02b | 2299 | if (!sp->role.invalid && !sp->role.direct) |
3ed1a478 | 2300 | unaccount_shadowed(kvm, sp); |
5304b8d3 | 2301 | |
4731d4c7 MT |
2302 | if (sp->unsync) |
2303 | kvm_unlink_unsync_page(kvm, sp); | |
4db35314 | 2304 | if (!sp->root_count) { |
54a4f023 GJ |
2305 | /* Count self */ |
2306 | ret++; | |
7775834a | 2307 | list_move(&sp->link, invalid_list); |
aa6bd187 | 2308 | kvm_mod_used_mmu_pages(kvm, -1); |
2e53d63a | 2309 | } else { |
5b5c6a5a | 2310 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
05988d72 GN |
2311 | |
2312 | /* | |
2313 | * The obsolete pages can not be used on any vcpus. | |
2314 | * See the comments in kvm_mmu_invalidate_zap_all_pages(). | |
2315 | */ | |
2316 | if (!sp->role.invalid && !is_obsolete_sp(kvm, sp)) | |
2317 | kvm_reload_remote_mmus(kvm); | |
2e53d63a | 2318 | } |
7775834a XG |
2319 | |
2320 | sp->role.invalid = 1; | |
4731d4c7 | 2321 | return ret; |
a436036b AK |
2322 | } |
2323 | ||
7775834a XG |
2324 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
2325 | struct list_head *invalid_list) | |
2326 | { | |
945315b9 | 2327 | struct kvm_mmu_page *sp, *nsp; |
7775834a XG |
2328 | |
2329 | if (list_empty(invalid_list)) | |
2330 | return; | |
2331 | ||
c142786c AK |
2332 | /* |
2333 | * wmb: make sure everyone sees our modifications to the page tables | |
2334 | * rmb: make sure we see changes to vcpu->mode | |
2335 | */ | |
2336 | smp_mb(); | |
4f022648 | 2337 | |
c142786c AK |
2338 | /* |
2339 | * Wait for all vcpus to exit guest mode and/or lockless shadow | |
2340 | * page table walks. | |
2341 | */ | |
2342 | kvm_flush_remote_tlbs(kvm); | |
c2a2ac2b | 2343 | |
945315b9 | 2344 | list_for_each_entry_safe(sp, nsp, invalid_list, link) { |
7775834a | 2345 | WARN_ON(!sp->role.invalid || sp->root_count); |
aa6bd187 | 2346 | kvm_mmu_free_page(sp); |
945315b9 | 2347 | } |
7775834a XG |
2348 | } |
2349 | ||
5da59607 TY |
2350 | static bool prepare_zap_oldest_mmu_page(struct kvm *kvm, |
2351 | struct list_head *invalid_list) | |
2352 | { | |
2353 | struct kvm_mmu_page *sp; | |
2354 | ||
2355 | if (list_empty(&kvm->arch.active_mmu_pages)) | |
2356 | return false; | |
2357 | ||
d74c0e6b GT |
2358 | sp = list_last_entry(&kvm->arch.active_mmu_pages, |
2359 | struct kvm_mmu_page, link); | |
5da59607 TY |
2360 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
2361 | ||
2362 | return true; | |
2363 | } | |
2364 | ||
82ce2c96 IE |
2365 | /* |
2366 | * Changing the number of mmu pages allocated to the vm | |
49d5ca26 | 2367 | * Note: if goal_nr_mmu_pages is too small, you will get dead lock |
82ce2c96 | 2368 | */ |
49d5ca26 | 2369 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages) |
82ce2c96 | 2370 | { |
d98ba053 | 2371 | LIST_HEAD(invalid_list); |
82ce2c96 | 2372 | |
b34cb590 TY |
2373 | spin_lock(&kvm->mmu_lock); |
2374 | ||
49d5ca26 | 2375 | if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { |
5da59607 TY |
2376 | /* Need to free some mmu pages to achieve the goal. */ |
2377 | while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) | |
2378 | if (!prepare_zap_oldest_mmu_page(kvm, &invalid_list)) | |
2379 | break; | |
82ce2c96 | 2380 | |
aa6bd187 | 2381 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
49d5ca26 | 2382 | goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; |
82ce2c96 | 2383 | } |
82ce2c96 | 2384 | |
49d5ca26 | 2385 | kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages; |
b34cb590 TY |
2386 | |
2387 | spin_unlock(&kvm->mmu_lock); | |
82ce2c96 IE |
2388 | } |
2389 | ||
1cb3f3ae | 2390 | int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
a436036b | 2391 | { |
4db35314 | 2392 | struct kvm_mmu_page *sp; |
d98ba053 | 2393 | LIST_HEAD(invalid_list); |
a436036b AK |
2394 | int r; |
2395 | ||
9ad17b10 | 2396 | pgprintk("%s: looking for gfn %llx\n", __func__, gfn); |
a436036b | 2397 | r = 0; |
1cb3f3ae | 2398 | spin_lock(&kvm->mmu_lock); |
b67bfe0d | 2399 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn) { |
9ad17b10 | 2400 | pgprintk("%s: gfn %llx role %x\n", __func__, gfn, |
7ae680eb XG |
2401 | sp->role.word); |
2402 | r = 1; | |
f41d335a | 2403 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
7ae680eb | 2404 | } |
d98ba053 | 2405 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
1cb3f3ae XG |
2406 | spin_unlock(&kvm->mmu_lock); |
2407 | ||
a436036b | 2408 | return r; |
cea0f0e7 | 2409 | } |
1cb3f3ae | 2410 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page); |
cea0f0e7 | 2411 | |
9cf5cf5a XG |
2412 | static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
2413 | { | |
2414 | trace_kvm_mmu_unsync_page(sp); | |
2415 | ++vcpu->kvm->stat.mmu_unsync; | |
2416 | sp->unsync = 1; | |
2417 | ||
2418 | kvm_mmu_mark_parents_unsync(sp); | |
9cf5cf5a XG |
2419 | } |
2420 | ||
2421 | static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) | |
4731d4c7 | 2422 | { |
4731d4c7 | 2423 | struct kvm_mmu_page *s; |
9cf5cf5a | 2424 | |
b67bfe0d | 2425 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) { |
7ae680eb | 2426 | if (s->unsync) |
4731d4c7 | 2427 | continue; |
9cf5cf5a XG |
2428 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); |
2429 | __kvm_unsync_page(vcpu, s); | |
4731d4c7 | 2430 | } |
4731d4c7 MT |
2431 | } |
2432 | ||
2433 | static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, | |
2434 | bool can_unsync) | |
2435 | { | |
9cf5cf5a | 2436 | struct kvm_mmu_page *s; |
9cf5cf5a XG |
2437 | bool need_unsync = false; |
2438 | ||
b67bfe0d | 2439 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) { |
36a2e677 XG |
2440 | if (!can_unsync) |
2441 | return 1; | |
2442 | ||
9cf5cf5a | 2443 | if (s->role.level != PT_PAGE_TABLE_LEVEL) |
4731d4c7 | 2444 | return 1; |
9cf5cf5a | 2445 | |
9bb4f6b1 | 2446 | if (!s->unsync) |
9cf5cf5a | 2447 | need_unsync = true; |
4731d4c7 | 2448 | } |
9cf5cf5a XG |
2449 | if (need_unsync) |
2450 | kvm_unsync_pages(vcpu, gfn); | |
4731d4c7 MT |
2451 | return 0; |
2452 | } | |
2453 | ||
ba049e93 | 2454 | static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) |
d1fe9219 PB |
2455 | { |
2456 | if (pfn_valid(pfn)) | |
2457 | return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn)); | |
2458 | ||
2459 | return true; | |
2460 | } | |
2461 | ||
d555c333 | 2462 | static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
c2288505 | 2463 | unsigned pte_access, int level, |
ba049e93 | 2464 | gfn_t gfn, kvm_pfn_t pfn, bool speculative, |
9bdbba13 | 2465 | bool can_unsync, bool host_writable) |
1c4f1fd6 | 2466 | { |
6e7d0354 | 2467 | u64 spte; |
1e73f9dd | 2468 | int ret = 0; |
64d4d521 | 2469 | |
54bf36aa | 2470 | if (set_mmio_spte(vcpu, sptep, gfn, pfn, pte_access)) |
ce88decf XG |
2471 | return 0; |
2472 | ||
982c2565 | 2473 | spte = PT_PRESENT_MASK; |
947da538 | 2474 | if (!speculative) |
3201b5d9 | 2475 | spte |= shadow_accessed_mask; |
640d9b0d | 2476 | |
7b52345e SY |
2477 | if (pte_access & ACC_EXEC_MASK) |
2478 | spte |= shadow_x_mask; | |
2479 | else | |
2480 | spte |= shadow_nx_mask; | |
49fde340 | 2481 | |
1c4f1fd6 | 2482 | if (pte_access & ACC_USER_MASK) |
7b52345e | 2483 | spte |= shadow_user_mask; |
49fde340 | 2484 | |
852e3c19 | 2485 | if (level > PT_PAGE_TABLE_LEVEL) |
05da4558 | 2486 | spte |= PT_PAGE_SIZE_MASK; |
b0bc3ee2 | 2487 | if (tdp_enabled) |
4b12f0de | 2488 | spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn, |
d1fe9219 | 2489 | kvm_is_mmio_pfn(pfn)); |
1c4f1fd6 | 2490 | |
9bdbba13 | 2491 | if (host_writable) |
1403283a | 2492 | spte |= SPTE_HOST_WRITEABLE; |
f8e453b0 XG |
2493 | else |
2494 | pte_access &= ~ACC_WRITE_MASK; | |
1403283a | 2495 | |
35149e21 | 2496 | spte |= (u64)pfn << PAGE_SHIFT; |
1c4f1fd6 | 2497 | |
c2288505 | 2498 | if (pte_access & ACC_WRITE_MASK) { |
1c4f1fd6 | 2499 | |
c2193463 | 2500 | /* |
7751babd XG |
2501 | * Other vcpu creates new sp in the window between |
2502 | * mapping_level() and acquiring mmu-lock. We can | |
2503 | * allow guest to retry the access, the mapping can | |
2504 | * be fixed if guest refault. | |
c2193463 | 2505 | */ |
852e3c19 | 2506 | if (level > PT_PAGE_TABLE_LEVEL && |
92f94f1e | 2507 | mmu_gfn_lpage_is_disallowed(vcpu, gfn, level)) |
be38d276 | 2508 | goto done; |
38187c83 | 2509 | |
49fde340 | 2510 | spte |= PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE; |
1c4f1fd6 | 2511 | |
ecc5589f MT |
2512 | /* |
2513 | * Optimization: for pte sync, if spte was writable the hash | |
2514 | * lookup is unnecessary (and expensive). Write protection | |
2515 | * is responsibility of mmu_get_page / kvm_sync_page. | |
2516 | * Same reasoning can be applied to dirty page accounting. | |
2517 | */ | |
8dae4445 | 2518 | if (!can_unsync && is_writable_pte(*sptep)) |
ecc5589f MT |
2519 | goto set_pte; |
2520 | ||
4731d4c7 | 2521 | if (mmu_need_write_protect(vcpu, gfn, can_unsync)) { |
9ad17b10 | 2522 | pgprintk("%s: found shadow page for %llx, marking ro\n", |
b8688d51 | 2523 | __func__, gfn); |
1e73f9dd | 2524 | ret = 1; |
1c4f1fd6 | 2525 | pte_access &= ~ACC_WRITE_MASK; |
49fde340 | 2526 | spte &= ~(PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE); |
1c4f1fd6 AK |
2527 | } |
2528 | } | |
2529 | ||
9b51a630 | 2530 | if (pte_access & ACC_WRITE_MASK) { |
54bf36aa | 2531 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
9b51a630 KH |
2532 | spte |= shadow_dirty_mask; |
2533 | } | |
1c4f1fd6 | 2534 | |
38187c83 | 2535 | set_pte: |
6e7d0354 | 2536 | if (mmu_spte_update(sptep, spte)) |
b330aa0c | 2537 | kvm_flush_remote_tlbs(vcpu->kvm); |
be38d276 | 2538 | done: |
1e73f9dd MT |
2539 | return ret; |
2540 | } | |
2541 | ||
029499b4 | 2542 | static bool mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access, |
ba049e93 | 2543 | int write_fault, int level, gfn_t gfn, kvm_pfn_t pfn, |
029499b4 | 2544 | bool speculative, bool host_writable) |
1e73f9dd MT |
2545 | { |
2546 | int was_rmapped = 0; | |
53a27b39 | 2547 | int rmap_count; |
029499b4 | 2548 | bool emulate = false; |
1e73f9dd | 2549 | |
f7616203 XG |
2550 | pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__, |
2551 | *sptep, write_fault, gfn); | |
1e73f9dd | 2552 | |
afd28fe1 | 2553 | if (is_shadow_present_pte(*sptep)) { |
1e73f9dd MT |
2554 | /* |
2555 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink | |
2556 | * the parent of the now unreachable PTE. | |
2557 | */ | |
852e3c19 JR |
2558 | if (level > PT_PAGE_TABLE_LEVEL && |
2559 | !is_large_pte(*sptep)) { | |
1e73f9dd | 2560 | struct kvm_mmu_page *child; |
d555c333 | 2561 | u64 pte = *sptep; |
1e73f9dd MT |
2562 | |
2563 | child = page_header(pte & PT64_BASE_ADDR_MASK); | |
bcdd9a93 | 2564 | drop_parent_pte(child, sptep); |
3be2264b | 2565 | kvm_flush_remote_tlbs(vcpu->kvm); |
d555c333 | 2566 | } else if (pfn != spte_to_pfn(*sptep)) { |
9ad17b10 | 2567 | pgprintk("hfn old %llx new %llx\n", |
d555c333 | 2568 | spte_to_pfn(*sptep), pfn); |
c3707958 | 2569 | drop_spte(vcpu->kvm, sptep); |
91546356 | 2570 | kvm_flush_remote_tlbs(vcpu->kvm); |
6bed6b9e JR |
2571 | } else |
2572 | was_rmapped = 1; | |
1e73f9dd | 2573 | } |
852e3c19 | 2574 | |
c2288505 XG |
2575 | if (set_spte(vcpu, sptep, pte_access, level, gfn, pfn, speculative, |
2576 | true, host_writable)) { | |
1e73f9dd | 2577 | if (write_fault) |
029499b4 | 2578 | emulate = true; |
77c3913b | 2579 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
a378b4e6 | 2580 | } |
1e73f9dd | 2581 | |
029499b4 TY |
2582 | if (unlikely(is_mmio_spte(*sptep))) |
2583 | emulate = true; | |
ce88decf | 2584 | |
d555c333 | 2585 | pgprintk("%s: setting spte %llx\n", __func__, *sptep); |
9ad17b10 | 2586 | pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n", |
d555c333 | 2587 | is_large_pte(*sptep)? "2MB" : "4kB", |
a205bc19 JR |
2588 | *sptep & PT_PRESENT_MASK ?"RW":"R", gfn, |
2589 | *sptep, sptep); | |
d555c333 | 2590 | if (!was_rmapped && is_large_pte(*sptep)) |
05da4558 MT |
2591 | ++vcpu->kvm->stat.lpages; |
2592 | ||
ffb61bb3 | 2593 | if (is_shadow_present_pte(*sptep)) { |
ffb61bb3 XG |
2594 | if (!was_rmapped) { |
2595 | rmap_count = rmap_add(vcpu, sptep, gfn); | |
2596 | if (rmap_count > RMAP_RECYCLE_THRESHOLD) | |
2597 | rmap_recycle(vcpu, sptep, gfn); | |
2598 | } | |
1c4f1fd6 | 2599 | } |
cb9aaa30 | 2600 | |
f3ac1a4b | 2601 | kvm_release_pfn_clean(pfn); |
029499b4 TY |
2602 | |
2603 | return emulate; | |
1c4f1fd6 AK |
2604 | } |
2605 | ||
ba049e93 | 2606 | static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, |
957ed9ef XG |
2607 | bool no_dirty_log) |
2608 | { | |
2609 | struct kvm_memory_slot *slot; | |
957ed9ef | 2610 | |
5d163b1c | 2611 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log); |
903816fa | 2612 | if (!slot) |
6c8ee57b | 2613 | return KVM_PFN_ERR_FAULT; |
957ed9ef | 2614 | |
037d92dc | 2615 | return gfn_to_pfn_memslot_atomic(slot, gfn); |
957ed9ef XG |
2616 | } |
2617 | ||
2618 | static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, | |
2619 | struct kvm_mmu_page *sp, | |
2620 | u64 *start, u64 *end) | |
2621 | { | |
2622 | struct page *pages[PTE_PREFETCH_NUM]; | |
d9ef13c2 | 2623 | struct kvm_memory_slot *slot; |
957ed9ef XG |
2624 | unsigned access = sp->role.access; |
2625 | int i, ret; | |
2626 | gfn_t gfn; | |
2627 | ||
2628 | gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt); | |
d9ef13c2 PB |
2629 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK); |
2630 | if (!slot) | |
957ed9ef XG |
2631 | return -1; |
2632 | ||
d9ef13c2 | 2633 | ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start); |
957ed9ef XG |
2634 | if (ret <= 0) |
2635 | return -1; | |
2636 | ||
2637 | for (i = 0; i < ret; i++, gfn++, start++) | |
029499b4 TY |
2638 | mmu_set_spte(vcpu, start, access, 0, sp->role.level, gfn, |
2639 | page_to_pfn(pages[i]), true, true); | |
957ed9ef XG |
2640 | |
2641 | return 0; | |
2642 | } | |
2643 | ||
2644 | static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, | |
2645 | struct kvm_mmu_page *sp, u64 *sptep) | |
2646 | { | |
2647 | u64 *spte, *start = NULL; | |
2648 | int i; | |
2649 | ||
2650 | WARN_ON(!sp->role.direct); | |
2651 | ||
2652 | i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1); | |
2653 | spte = sp->spt + i; | |
2654 | ||
2655 | for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) { | |
c3707958 | 2656 | if (is_shadow_present_pte(*spte) || spte == sptep) { |
957ed9ef XG |
2657 | if (!start) |
2658 | continue; | |
2659 | if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0) | |
2660 | break; | |
2661 | start = NULL; | |
2662 | } else if (!start) | |
2663 | start = spte; | |
2664 | } | |
2665 | } | |
2666 | ||
2667 | static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) | |
2668 | { | |
2669 | struct kvm_mmu_page *sp; | |
2670 | ||
2671 | /* | |
2672 | * Since it's no accessed bit on EPT, it's no way to | |
2673 | * distinguish between actually accessed translations | |
2674 | * and prefetched, so disable pte prefetch if EPT is | |
2675 | * enabled. | |
2676 | */ | |
2677 | if (!shadow_accessed_mask) | |
2678 | return; | |
2679 | ||
2680 | sp = page_header(__pa(sptep)); | |
2681 | if (sp->role.level > PT_PAGE_TABLE_LEVEL) | |
2682 | return; | |
2683 | ||
2684 | __direct_pte_prefetch(vcpu, sp, sptep); | |
2685 | } | |
2686 | ||
7ee0e5b2 | 2687 | static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable, |
ba049e93 | 2688 | int level, gfn_t gfn, kvm_pfn_t pfn, bool prefault) |
140754bc | 2689 | { |
9f652d21 | 2690 | struct kvm_shadow_walk_iterator iterator; |
140754bc | 2691 | struct kvm_mmu_page *sp; |
b90a0e6c | 2692 | int emulate = 0; |
140754bc | 2693 | gfn_t pseudo_gfn; |
6aa8b732 | 2694 | |
989c6b34 MT |
2695 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
2696 | return 0; | |
2697 | ||
9f652d21 | 2698 | for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) { |
852e3c19 | 2699 | if (iterator.level == level) { |
029499b4 TY |
2700 | emulate = mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, |
2701 | write, level, gfn, pfn, prefault, | |
2702 | map_writable); | |
957ed9ef | 2703 | direct_pte_prefetch(vcpu, iterator.sptep); |
9f652d21 AK |
2704 | ++vcpu->stat.pf_fixed; |
2705 | break; | |
6aa8b732 AK |
2706 | } |
2707 | ||
404381c5 | 2708 | drop_large_spte(vcpu, iterator.sptep); |
c3707958 | 2709 | if (!is_shadow_present_pte(*iterator.sptep)) { |
c9fa0b3b LJ |
2710 | u64 base_addr = iterator.addr; |
2711 | ||
2712 | base_addr &= PT64_LVL_ADDR_MASK(iterator.level); | |
2713 | pseudo_gfn = base_addr >> PAGE_SHIFT; | |
9f652d21 | 2714 | sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr, |
bb11c6c9 | 2715 | iterator.level - 1, 1, ACC_ALL); |
140754bc | 2716 | |
98bba238 | 2717 | link_shadow_page(vcpu, iterator.sptep, sp); |
9f652d21 AK |
2718 | } |
2719 | } | |
b90a0e6c | 2720 | return emulate; |
6aa8b732 AK |
2721 | } |
2722 | ||
77db5cbd | 2723 | static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk) |
bf998156 | 2724 | { |
77db5cbd HY |
2725 | siginfo_t info; |
2726 | ||
2727 | info.si_signo = SIGBUS; | |
2728 | info.si_errno = 0; | |
2729 | info.si_code = BUS_MCEERR_AR; | |
2730 | info.si_addr = (void __user *)address; | |
2731 | info.si_addr_lsb = PAGE_SHIFT; | |
bf998156 | 2732 | |
77db5cbd | 2733 | send_sig_info(SIGBUS, &info, tsk); |
bf998156 HY |
2734 | } |
2735 | ||
ba049e93 | 2736 | static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) |
bf998156 | 2737 | { |
4d8b81ab XG |
2738 | /* |
2739 | * Do not cache the mmio info caused by writing the readonly gfn | |
2740 | * into the spte otherwise read access on readonly gfn also can | |
2741 | * caused mmio page fault and treat it as mmio access. | |
2742 | * Return 1 to tell kvm to emulate it. | |
2743 | */ | |
2744 | if (pfn == KVM_PFN_ERR_RO_FAULT) | |
2745 | return 1; | |
2746 | ||
e6c1502b | 2747 | if (pfn == KVM_PFN_ERR_HWPOISON) { |
54bf36aa | 2748 | kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current); |
bf998156 | 2749 | return 0; |
d7c55201 | 2750 | } |
edba23e5 | 2751 | |
d7c55201 | 2752 | return -EFAULT; |
bf998156 HY |
2753 | } |
2754 | ||
936a5fe6 | 2755 | static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu, |
ba049e93 DW |
2756 | gfn_t *gfnp, kvm_pfn_t *pfnp, |
2757 | int *levelp) | |
936a5fe6 | 2758 | { |
ba049e93 | 2759 | kvm_pfn_t pfn = *pfnp; |
936a5fe6 AA |
2760 | gfn_t gfn = *gfnp; |
2761 | int level = *levelp; | |
2762 | ||
2763 | /* | |
2764 | * Check if it's a transparent hugepage. If this would be an | |
2765 | * hugetlbfs page, level wouldn't be set to | |
2766 | * PT_PAGE_TABLE_LEVEL and there would be no adjustment done | |
2767 | * here. | |
2768 | */ | |
bf4bea8e | 2769 | if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) && |
936a5fe6 AA |
2770 | level == PT_PAGE_TABLE_LEVEL && |
2771 | PageTransCompound(pfn_to_page(pfn)) && | |
92f94f1e | 2772 | !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) { |
936a5fe6 AA |
2773 | unsigned long mask; |
2774 | /* | |
2775 | * mmu_notifier_retry was successful and we hold the | |
2776 | * mmu_lock here, so the pmd can't become splitting | |
2777 | * from under us, and in turn | |
2778 | * __split_huge_page_refcount() can't run from under | |
2779 | * us and we can safely transfer the refcount from | |
2780 | * PG_tail to PG_head as we switch the pfn to tail to | |
2781 | * head. | |
2782 | */ | |
2783 | *levelp = level = PT_DIRECTORY_LEVEL; | |
2784 | mask = KVM_PAGES_PER_HPAGE(level) - 1; | |
2785 | VM_BUG_ON((gfn & mask) != (pfn & mask)); | |
2786 | if (pfn & mask) { | |
2787 | gfn &= ~mask; | |
2788 | *gfnp = gfn; | |
2789 | kvm_release_pfn_clean(pfn); | |
2790 | pfn &= ~mask; | |
c3586667 | 2791 | kvm_get_pfn(pfn); |
936a5fe6 AA |
2792 | *pfnp = pfn; |
2793 | } | |
2794 | } | |
2795 | } | |
2796 | ||
d7c55201 | 2797 | static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn, |
ba049e93 | 2798 | kvm_pfn_t pfn, unsigned access, int *ret_val) |
d7c55201 XG |
2799 | { |
2800 | bool ret = true; | |
2801 | ||
2802 | /* The pfn is invalid, report the error! */ | |
81c52c56 | 2803 | if (unlikely(is_error_pfn(pfn))) { |
d7c55201 XG |
2804 | *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn); |
2805 | goto exit; | |
2806 | } | |
2807 | ||
ce88decf | 2808 | if (unlikely(is_noslot_pfn(pfn))) |
d7c55201 | 2809 | vcpu_cache_mmio_info(vcpu, gva, gfn, access); |
d7c55201 XG |
2810 | |
2811 | ret = false; | |
2812 | exit: | |
2813 | return ret; | |
2814 | } | |
2815 | ||
e5552fd2 | 2816 | static bool page_fault_can_be_fast(u32 error_code) |
c7ba5b48 | 2817 | { |
1c118b82 XG |
2818 | /* |
2819 | * Do not fix the mmio spte with invalid generation number which | |
2820 | * need to be updated by slow page fault path. | |
2821 | */ | |
2822 | if (unlikely(error_code & PFERR_RSVD_MASK)) | |
2823 | return false; | |
2824 | ||
c7ba5b48 XG |
2825 | /* |
2826 | * #PF can be fast only if the shadow page table is present and it | |
2827 | * is caused by write-protect, that means we just need change the | |
2828 | * W bit of the spte which can be done out of mmu-lock. | |
2829 | */ | |
2830 | if (!(error_code & PFERR_PRESENT_MASK) || | |
2831 | !(error_code & PFERR_WRITE_MASK)) | |
2832 | return false; | |
2833 | ||
2834 | return true; | |
2835 | } | |
2836 | ||
2837 | static bool | |
92a476cb XG |
2838 | fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
2839 | u64 *sptep, u64 spte) | |
c7ba5b48 | 2840 | { |
c7ba5b48 XG |
2841 | gfn_t gfn; |
2842 | ||
2843 | WARN_ON(!sp->role.direct); | |
2844 | ||
2845 | /* | |
2846 | * The gfn of direct spte is stable since it is calculated | |
2847 | * by sp->gfn. | |
2848 | */ | |
2849 | gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt); | |
2850 | ||
9b51a630 KH |
2851 | /* |
2852 | * Theoretically we could also set dirty bit (and flush TLB) here in | |
2853 | * order to eliminate unnecessary PML logging. See comments in | |
2854 | * set_spte. But fast_page_fault is very unlikely to happen with PML | |
2855 | * enabled, so we do not do this. This might result in the same GPA | |
2856 | * to be logged in PML buffer again when the write really happens, and | |
2857 | * eventually to be called by mark_page_dirty twice. But it's also no | |
2858 | * harm. This also avoids the TLB flush needed after setting dirty bit | |
2859 | * so non-PML cases won't be impacted. | |
2860 | * | |
2861 | * Compare with set_spte where instead shadow_dirty_mask is set. | |
2862 | */ | |
c7ba5b48 | 2863 | if (cmpxchg64(sptep, spte, spte | PT_WRITABLE_MASK) == spte) |
54bf36aa | 2864 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
c7ba5b48 XG |
2865 | |
2866 | return true; | |
2867 | } | |
2868 | ||
2869 | /* | |
2870 | * Return value: | |
2871 | * - true: let the vcpu to access on the same address again. | |
2872 | * - false: let the real page fault path to fix it. | |
2873 | */ | |
2874 | static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level, | |
2875 | u32 error_code) | |
2876 | { | |
2877 | struct kvm_shadow_walk_iterator iterator; | |
92a476cb | 2878 | struct kvm_mmu_page *sp; |
c7ba5b48 XG |
2879 | bool ret = false; |
2880 | u64 spte = 0ull; | |
2881 | ||
37f6a4e2 MT |
2882 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
2883 | return false; | |
2884 | ||
e5552fd2 | 2885 | if (!page_fault_can_be_fast(error_code)) |
c7ba5b48 XG |
2886 | return false; |
2887 | ||
2888 | walk_shadow_page_lockless_begin(vcpu); | |
2889 | for_each_shadow_entry_lockless(vcpu, gva, iterator, spte) | |
2890 | if (!is_shadow_present_pte(spte) || iterator.level < level) | |
2891 | break; | |
2892 | ||
2893 | /* | |
2894 | * If the mapping has been changed, let the vcpu fault on the | |
2895 | * same address again. | |
2896 | */ | |
afd28fe1 | 2897 | if (!is_shadow_present_pte(spte)) { |
c7ba5b48 XG |
2898 | ret = true; |
2899 | goto exit; | |
2900 | } | |
2901 | ||
92a476cb XG |
2902 | sp = page_header(__pa(iterator.sptep)); |
2903 | if (!is_last_spte(spte, sp->role.level)) | |
c7ba5b48 XG |
2904 | goto exit; |
2905 | ||
2906 | /* | |
2907 | * Check if it is a spurious fault caused by TLB lazily flushed. | |
2908 | * | |
2909 | * Need not check the access of upper level table entries since | |
2910 | * they are always ACC_ALL. | |
2911 | */ | |
2912 | if (is_writable_pte(spte)) { | |
2913 | ret = true; | |
2914 | goto exit; | |
2915 | } | |
2916 | ||
2917 | /* | |
2918 | * Currently, to simplify the code, only the spte write-protected | |
2919 | * by dirty-log can be fast fixed. | |
2920 | */ | |
2921 | if (!spte_is_locklessly_modifiable(spte)) | |
2922 | goto exit; | |
2923 | ||
c126d94f XG |
2924 | /* |
2925 | * Do not fix write-permission on the large spte since we only dirty | |
2926 | * the first page into the dirty-bitmap in fast_pf_fix_direct_spte() | |
2927 | * that means other pages are missed if its slot is dirty-logged. | |
2928 | * | |
2929 | * Instead, we let the slow page fault path create a normal spte to | |
2930 | * fix the access. | |
2931 | * | |
2932 | * See the comments in kvm_arch_commit_memory_region(). | |
2933 | */ | |
2934 | if (sp->role.level > PT_PAGE_TABLE_LEVEL) | |
2935 | goto exit; | |
2936 | ||
c7ba5b48 XG |
2937 | /* |
2938 | * Currently, fast page fault only works for direct mapping since | |
2939 | * the gfn is not stable for indirect shadow page. | |
2940 | * See Documentation/virtual/kvm/locking.txt to get more detail. | |
2941 | */ | |
92a476cb | 2942 | ret = fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte); |
c7ba5b48 | 2943 | exit: |
a72faf25 XG |
2944 | trace_fast_page_fault(vcpu, gva, error_code, iterator.sptep, |
2945 | spte, ret); | |
c7ba5b48 XG |
2946 | walk_shadow_page_lockless_end(vcpu); |
2947 | ||
2948 | return ret; | |
2949 | } | |
2950 | ||
78b2c54a | 2951 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
ba049e93 | 2952 | gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable); |
450e0b41 | 2953 | static void make_mmu_pages_available(struct kvm_vcpu *vcpu); |
060c2abe | 2954 | |
c7ba5b48 XG |
2955 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, u32 error_code, |
2956 | gfn_t gfn, bool prefault) | |
10589a46 MT |
2957 | { |
2958 | int r; | |
852e3c19 | 2959 | int level; |
fd136902 | 2960 | bool force_pt_level = false; |
ba049e93 | 2961 | kvm_pfn_t pfn; |
e930bffe | 2962 | unsigned long mmu_seq; |
c7ba5b48 | 2963 | bool map_writable, write = error_code & PFERR_WRITE_MASK; |
aaee2c94 | 2964 | |
fd136902 | 2965 | level = mapping_level(vcpu, gfn, &force_pt_level); |
936a5fe6 | 2966 | if (likely(!force_pt_level)) { |
936a5fe6 AA |
2967 | /* |
2968 | * This path builds a PAE pagetable - so we can map | |
2969 | * 2mb pages at maximum. Therefore check if the level | |
2970 | * is larger than that. | |
2971 | */ | |
2972 | if (level > PT_DIRECTORY_LEVEL) | |
2973 | level = PT_DIRECTORY_LEVEL; | |
852e3c19 | 2974 | |
936a5fe6 | 2975 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
fd136902 | 2976 | } |
05da4558 | 2977 | |
c7ba5b48 XG |
2978 | if (fast_page_fault(vcpu, v, level, error_code)) |
2979 | return 0; | |
2980 | ||
e930bffe | 2981 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
4c2155ce | 2982 | smp_rmb(); |
060c2abe | 2983 | |
78b2c54a | 2984 | if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable)) |
060c2abe | 2985 | return 0; |
aaee2c94 | 2986 | |
d7c55201 XG |
2987 | if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r)) |
2988 | return r; | |
d196e343 | 2989 | |
aaee2c94 | 2990 | spin_lock(&vcpu->kvm->mmu_lock); |
8ca40a70 | 2991 | if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) |
e930bffe | 2992 | goto out_unlock; |
450e0b41 | 2993 | make_mmu_pages_available(vcpu); |
936a5fe6 AA |
2994 | if (likely(!force_pt_level)) |
2995 | transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level); | |
7ee0e5b2 | 2996 | r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault); |
aaee2c94 MT |
2997 | spin_unlock(&vcpu->kvm->mmu_lock); |
2998 | ||
10589a46 | 2999 | return r; |
e930bffe AA |
3000 | |
3001 | out_unlock: | |
3002 | spin_unlock(&vcpu->kvm->mmu_lock); | |
3003 | kvm_release_pfn_clean(pfn); | |
3004 | return 0; | |
10589a46 MT |
3005 | } |
3006 | ||
3007 | ||
17ac10ad AK |
3008 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
3009 | { | |
3010 | int i; | |
4db35314 | 3011 | struct kvm_mmu_page *sp; |
d98ba053 | 3012 | LIST_HEAD(invalid_list); |
17ac10ad | 3013 | |
ad312c7c | 3014 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
7b53aa56 | 3015 | return; |
35af577a | 3016 | |
81407ca5 JR |
3017 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL && |
3018 | (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL || | |
3019 | vcpu->arch.mmu.direct_map)) { | |
ad312c7c | 3020 | hpa_t root = vcpu->arch.mmu.root_hpa; |
17ac10ad | 3021 | |
35af577a | 3022 | spin_lock(&vcpu->kvm->mmu_lock); |
4db35314 AK |
3023 | sp = page_header(root); |
3024 | --sp->root_count; | |
d98ba053 XG |
3025 | if (!sp->root_count && sp->role.invalid) { |
3026 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); | |
3027 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); | |
3028 | } | |
aaee2c94 | 3029 | spin_unlock(&vcpu->kvm->mmu_lock); |
35af577a | 3030 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
17ac10ad AK |
3031 | return; |
3032 | } | |
35af577a GN |
3033 | |
3034 | spin_lock(&vcpu->kvm->mmu_lock); | |
17ac10ad | 3035 | for (i = 0; i < 4; ++i) { |
ad312c7c | 3036 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
17ac10ad | 3037 | |
417726a3 | 3038 | if (root) { |
417726a3 | 3039 | root &= PT64_BASE_ADDR_MASK; |
4db35314 AK |
3040 | sp = page_header(root); |
3041 | --sp->root_count; | |
2e53d63a | 3042 | if (!sp->root_count && sp->role.invalid) |
d98ba053 XG |
3043 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
3044 | &invalid_list); | |
417726a3 | 3045 | } |
ad312c7c | 3046 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
17ac10ad | 3047 | } |
d98ba053 | 3048 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
aaee2c94 | 3049 | spin_unlock(&vcpu->kvm->mmu_lock); |
ad312c7c | 3050 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
17ac10ad AK |
3051 | } |
3052 | ||
8986ecc0 MT |
3053 | static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn) |
3054 | { | |
3055 | int ret = 0; | |
3056 | ||
3057 | if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) { | |
a8eeb04a | 3058 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
8986ecc0 MT |
3059 | ret = 1; |
3060 | } | |
3061 | ||
3062 | return ret; | |
3063 | } | |
3064 | ||
651dd37a JR |
3065 | static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) |
3066 | { | |
3067 | struct kvm_mmu_page *sp; | |
7ebaf15e | 3068 | unsigned i; |
651dd37a JR |
3069 | |
3070 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { | |
3071 | spin_lock(&vcpu->kvm->mmu_lock); | |
450e0b41 | 3072 | make_mmu_pages_available(vcpu); |
bb11c6c9 | 3073 | sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL, 1, ACC_ALL); |
651dd37a JR |
3074 | ++sp->root_count; |
3075 | spin_unlock(&vcpu->kvm->mmu_lock); | |
3076 | vcpu->arch.mmu.root_hpa = __pa(sp->spt); | |
3077 | } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) { | |
3078 | for (i = 0; i < 4; ++i) { | |
3079 | hpa_t root = vcpu->arch.mmu.pae_root[i]; | |
3080 | ||
fa4a2c08 | 3081 | MMU_WARN_ON(VALID_PAGE(root)); |
651dd37a | 3082 | spin_lock(&vcpu->kvm->mmu_lock); |
450e0b41 | 3083 | make_mmu_pages_available(vcpu); |
649497d1 | 3084 | sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT), |
bb11c6c9 | 3085 | i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL); |
651dd37a JR |
3086 | root = __pa(sp->spt); |
3087 | ++sp->root_count; | |
3088 | spin_unlock(&vcpu->kvm->mmu_lock); | |
3089 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; | |
651dd37a | 3090 | } |
6292757f | 3091 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
651dd37a JR |
3092 | } else |
3093 | BUG(); | |
3094 | ||
3095 | return 0; | |
3096 | } | |
3097 | ||
3098 | static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) | |
17ac10ad | 3099 | { |
4db35314 | 3100 | struct kvm_mmu_page *sp; |
81407ca5 JR |
3101 | u64 pdptr, pm_mask; |
3102 | gfn_t root_gfn; | |
3103 | int i; | |
3bb65a22 | 3104 | |
5777ed34 | 3105 | root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT; |
17ac10ad | 3106 | |
651dd37a JR |
3107 | if (mmu_check_root(vcpu, root_gfn)) |
3108 | return 1; | |
3109 | ||
3110 | /* | |
3111 | * Do we shadow a long mode page table? If so we need to | |
3112 | * write-protect the guests page table root. | |
3113 | */ | |
3114 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { | |
ad312c7c | 3115 | hpa_t root = vcpu->arch.mmu.root_hpa; |
17ac10ad | 3116 | |
fa4a2c08 | 3117 | MMU_WARN_ON(VALID_PAGE(root)); |
651dd37a | 3118 | |
8facbbff | 3119 | spin_lock(&vcpu->kvm->mmu_lock); |
450e0b41 | 3120 | make_mmu_pages_available(vcpu); |
651dd37a | 3121 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL, |
bb11c6c9 | 3122 | 0, ACC_ALL); |
4db35314 AK |
3123 | root = __pa(sp->spt); |
3124 | ++sp->root_count; | |
8facbbff | 3125 | spin_unlock(&vcpu->kvm->mmu_lock); |
ad312c7c | 3126 | vcpu->arch.mmu.root_hpa = root; |
8986ecc0 | 3127 | return 0; |
17ac10ad | 3128 | } |
f87f9288 | 3129 | |
651dd37a JR |
3130 | /* |
3131 | * We shadow a 32 bit page table. This may be a legacy 2-level | |
81407ca5 JR |
3132 | * or a PAE 3-level page table. In either case we need to be aware that |
3133 | * the shadow page table may be a PAE or a long mode page table. | |
651dd37a | 3134 | */ |
81407ca5 JR |
3135 | pm_mask = PT_PRESENT_MASK; |
3136 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) | |
3137 | pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK; | |
3138 | ||
17ac10ad | 3139 | for (i = 0; i < 4; ++i) { |
ad312c7c | 3140 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
17ac10ad | 3141 | |
fa4a2c08 | 3142 | MMU_WARN_ON(VALID_PAGE(root)); |
ad312c7c | 3143 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
e4e517b4 | 3144 | pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i); |
43a3795a | 3145 | if (!is_present_gpte(pdptr)) { |
ad312c7c | 3146 | vcpu->arch.mmu.pae_root[i] = 0; |
417726a3 AK |
3147 | continue; |
3148 | } | |
6de4f3ad | 3149 | root_gfn = pdptr >> PAGE_SHIFT; |
f87f9288 JR |
3150 | if (mmu_check_root(vcpu, root_gfn)) |
3151 | return 1; | |
5a7388c2 | 3152 | } |
8facbbff | 3153 | spin_lock(&vcpu->kvm->mmu_lock); |
450e0b41 | 3154 | make_mmu_pages_available(vcpu); |
bb11c6c9 TY |
3155 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL, |
3156 | 0, ACC_ALL); | |
4db35314 AK |
3157 | root = __pa(sp->spt); |
3158 | ++sp->root_count; | |
8facbbff AK |
3159 | spin_unlock(&vcpu->kvm->mmu_lock); |
3160 | ||
81407ca5 | 3161 | vcpu->arch.mmu.pae_root[i] = root | pm_mask; |
17ac10ad | 3162 | } |
6292757f | 3163 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
81407ca5 JR |
3164 | |
3165 | /* | |
3166 | * If we shadow a 32 bit page table with a long mode page | |
3167 | * table we enter this path. | |
3168 | */ | |
3169 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { | |
3170 | if (vcpu->arch.mmu.lm_root == NULL) { | |
3171 | /* | |
3172 | * The additional page necessary for this is only | |
3173 | * allocated on demand. | |
3174 | */ | |
3175 | ||
3176 | u64 *lm_root; | |
3177 | ||
3178 | lm_root = (void*)get_zeroed_page(GFP_KERNEL); | |
3179 | if (lm_root == NULL) | |
3180 | return 1; | |
3181 | ||
3182 | lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask; | |
3183 | ||
3184 | vcpu->arch.mmu.lm_root = lm_root; | |
3185 | } | |
3186 | ||
3187 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root); | |
3188 | } | |
3189 | ||
8986ecc0 | 3190 | return 0; |
17ac10ad AK |
3191 | } |
3192 | ||
651dd37a JR |
3193 | static int mmu_alloc_roots(struct kvm_vcpu *vcpu) |
3194 | { | |
3195 | if (vcpu->arch.mmu.direct_map) | |
3196 | return mmu_alloc_direct_roots(vcpu); | |
3197 | else | |
3198 | return mmu_alloc_shadow_roots(vcpu); | |
3199 | } | |
3200 | ||
0ba73cda MT |
3201 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) |
3202 | { | |
3203 | int i; | |
3204 | struct kvm_mmu_page *sp; | |
3205 | ||
81407ca5 JR |
3206 | if (vcpu->arch.mmu.direct_map) |
3207 | return; | |
3208 | ||
0ba73cda MT |
3209 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
3210 | return; | |
6903074c | 3211 | |
56f17dd3 | 3212 | vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY); |
0375f7fa | 3213 | kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC); |
81407ca5 | 3214 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { |
0ba73cda MT |
3215 | hpa_t root = vcpu->arch.mmu.root_hpa; |
3216 | sp = page_header(root); | |
3217 | mmu_sync_children(vcpu, sp); | |
0375f7fa | 3218 | kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
0ba73cda MT |
3219 | return; |
3220 | } | |
3221 | for (i = 0; i < 4; ++i) { | |
3222 | hpa_t root = vcpu->arch.mmu.pae_root[i]; | |
3223 | ||
8986ecc0 | 3224 | if (root && VALID_PAGE(root)) { |
0ba73cda MT |
3225 | root &= PT64_BASE_ADDR_MASK; |
3226 | sp = page_header(root); | |
3227 | mmu_sync_children(vcpu, sp); | |
3228 | } | |
3229 | } | |
0375f7fa | 3230 | kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
0ba73cda MT |
3231 | } |
3232 | ||
3233 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) | |
3234 | { | |
3235 | spin_lock(&vcpu->kvm->mmu_lock); | |
3236 | mmu_sync_roots(vcpu); | |
6cffe8ca | 3237 | spin_unlock(&vcpu->kvm->mmu_lock); |
0ba73cda | 3238 | } |
bfd0a56b | 3239 | EXPORT_SYMBOL_GPL(kvm_mmu_sync_roots); |
0ba73cda | 3240 | |
1871c602 | 3241 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr, |
ab9ae313 | 3242 | u32 access, struct x86_exception *exception) |
6aa8b732 | 3243 | { |
ab9ae313 AK |
3244 | if (exception) |
3245 | exception->error_code = 0; | |
6aa8b732 AK |
3246 | return vaddr; |
3247 | } | |
3248 | ||
6539e738 | 3249 | static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr, |
ab9ae313 AK |
3250 | u32 access, |
3251 | struct x86_exception *exception) | |
6539e738 | 3252 | { |
ab9ae313 AK |
3253 | if (exception) |
3254 | exception->error_code = 0; | |
54987b7a | 3255 | return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception); |
6539e738 JR |
3256 | } |
3257 | ||
d625b155 XG |
3258 | static bool |
3259 | __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level) | |
3260 | { | |
3261 | int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f; | |
3262 | ||
3263 | return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) | | |
3264 | ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0); | |
3265 | } | |
3266 | ||
3267 | static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level) | |
3268 | { | |
3269 | return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level); | |
3270 | } | |
3271 | ||
3272 | static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level) | |
3273 | { | |
3274 | return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level); | |
3275 | } | |
3276 | ||
ded58749 | 3277 | static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct) |
ce88decf XG |
3278 | { |
3279 | if (direct) | |
3280 | return vcpu_match_mmio_gpa(vcpu, addr); | |
3281 | ||
3282 | return vcpu_match_mmio_gva(vcpu, addr); | |
3283 | } | |
3284 | ||
47ab8751 XG |
3285 | /* return true if reserved bit is detected on spte. */ |
3286 | static bool | |
3287 | walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep) | |
ce88decf XG |
3288 | { |
3289 | struct kvm_shadow_walk_iterator iterator; | |
47ab8751 XG |
3290 | u64 sptes[PT64_ROOT_LEVEL], spte = 0ull; |
3291 | int root, leaf; | |
3292 | bool reserved = false; | |
ce88decf | 3293 | |
37f6a4e2 | 3294 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
47ab8751 | 3295 | goto exit; |
37f6a4e2 | 3296 | |
ce88decf | 3297 | walk_shadow_page_lockless_begin(vcpu); |
47ab8751 | 3298 | |
29ecd660 PB |
3299 | for (shadow_walk_init(&iterator, vcpu, addr), |
3300 | leaf = root = iterator.level; | |
47ab8751 XG |
3301 | shadow_walk_okay(&iterator); |
3302 | __shadow_walk_next(&iterator, spte)) { | |
47ab8751 XG |
3303 | spte = mmu_spte_get_lockless(iterator.sptep); |
3304 | ||
3305 | sptes[leaf - 1] = spte; | |
29ecd660 | 3306 | leaf--; |
47ab8751 | 3307 | |
ce88decf XG |
3308 | if (!is_shadow_present_pte(spte)) |
3309 | break; | |
47ab8751 XG |
3310 | |
3311 | reserved |= is_shadow_zero_bits_set(&vcpu->arch.mmu, spte, | |
58c95070 | 3312 | iterator.level); |
47ab8751 XG |
3313 | } |
3314 | ||
ce88decf XG |
3315 | walk_shadow_page_lockless_end(vcpu); |
3316 | ||
47ab8751 XG |
3317 | if (reserved) { |
3318 | pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n", | |
3319 | __func__, addr); | |
29ecd660 | 3320 | while (root > leaf) { |
47ab8751 XG |
3321 | pr_err("------ spte 0x%llx level %d.\n", |
3322 | sptes[root - 1], root); | |
3323 | root--; | |
3324 | } | |
3325 | } | |
3326 | exit: | |
3327 | *sptep = spte; | |
3328 | return reserved; | |
ce88decf XG |
3329 | } |
3330 | ||
450869d6 | 3331 | int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct) |
ce88decf XG |
3332 | { |
3333 | u64 spte; | |
47ab8751 | 3334 | bool reserved; |
ce88decf | 3335 | |
ded58749 | 3336 | if (mmio_info_in_cache(vcpu, addr, direct)) |
b37fbea6 | 3337 | return RET_MMIO_PF_EMULATE; |
ce88decf | 3338 | |
47ab8751 | 3339 | reserved = walk_shadow_page_get_mmio_spte(vcpu, addr, &spte); |
450869d6 | 3340 | if (WARN_ON(reserved)) |
47ab8751 | 3341 | return RET_MMIO_PF_BUG; |
ce88decf XG |
3342 | |
3343 | if (is_mmio_spte(spte)) { | |
3344 | gfn_t gfn = get_mmio_spte_gfn(spte); | |
3345 | unsigned access = get_mmio_spte_access(spte); | |
3346 | ||
54bf36aa | 3347 | if (!check_mmio_spte(vcpu, spte)) |
f8f55942 XG |
3348 | return RET_MMIO_PF_INVALID; |
3349 | ||
ce88decf XG |
3350 | if (direct) |
3351 | addr = 0; | |
4f022648 XG |
3352 | |
3353 | trace_handle_mmio_page_fault(addr, gfn, access); | |
ce88decf | 3354 | vcpu_cache_mmio_info(vcpu, addr, gfn, access); |
b37fbea6 | 3355 | return RET_MMIO_PF_EMULATE; |
ce88decf XG |
3356 | } |
3357 | ||
ce88decf XG |
3358 | /* |
3359 | * If the page table is zapped by other cpus, let CPU fault again on | |
3360 | * the address. | |
3361 | */ | |
b37fbea6 | 3362 | return RET_MMIO_PF_RETRY; |
ce88decf | 3363 | } |
450869d6 | 3364 | EXPORT_SYMBOL_GPL(handle_mmio_page_fault); |
ce88decf | 3365 | |
6aa8b732 | 3366 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
78b2c54a | 3367 | u32 error_code, bool prefault) |
6aa8b732 | 3368 | { |
e833240f | 3369 | gfn_t gfn; |
e2dec939 | 3370 | int r; |
6aa8b732 | 3371 | |
b8688d51 | 3372 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
ce88decf | 3373 | |
e2dec939 AK |
3374 | r = mmu_topup_memory_caches(vcpu); |
3375 | if (r) | |
3376 | return r; | |
714b93da | 3377 | |
fa4a2c08 | 3378 | MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 | 3379 | |
e833240f | 3380 | gfn = gva >> PAGE_SHIFT; |
6aa8b732 | 3381 | |
e833240f | 3382 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
c7ba5b48 | 3383 | error_code, gfn, prefault); |
6aa8b732 AK |
3384 | } |
3385 | ||
7e1fbeac | 3386 | static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn) |
af585b92 GN |
3387 | { |
3388 | struct kvm_arch_async_pf arch; | |
fb67e14f | 3389 | |
7c90705b | 3390 | arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; |
af585b92 | 3391 | arch.gfn = gfn; |
c4806acd | 3392 | arch.direct_map = vcpu->arch.mmu.direct_map; |
fb67e14f | 3393 | arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu); |
af585b92 | 3394 | |
54bf36aa | 3395 | return kvm_setup_async_pf(vcpu, gva, kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch); |
af585b92 GN |
3396 | } |
3397 | ||
3398 | static bool can_do_async_pf(struct kvm_vcpu *vcpu) | |
3399 | { | |
35754c98 | 3400 | if (unlikely(!lapic_in_kernel(vcpu) || |
af585b92 GN |
3401 | kvm_event_needs_reinjection(vcpu))) |
3402 | return false; | |
3403 | ||
3404 | return kvm_x86_ops->interrupt_allowed(vcpu); | |
3405 | } | |
3406 | ||
78b2c54a | 3407 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
ba049e93 | 3408 | gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable) |
af585b92 | 3409 | { |
3520469d | 3410 | struct kvm_memory_slot *slot; |
af585b92 GN |
3411 | bool async; |
3412 | ||
54bf36aa | 3413 | slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
3520469d PB |
3414 | async = false; |
3415 | *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable); | |
af585b92 GN |
3416 | if (!async) |
3417 | return false; /* *pfn has correct page already */ | |
3418 | ||
78b2c54a | 3419 | if (!prefault && can_do_async_pf(vcpu)) { |
c9b263d2 | 3420 | trace_kvm_try_async_get_page(gva, gfn); |
af585b92 GN |
3421 | if (kvm_find_async_pf_gfn(vcpu, gfn)) { |
3422 | trace_kvm_async_pf_doublefault(gva, gfn); | |
3423 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); | |
3424 | return true; | |
3425 | } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn)) | |
3426 | return true; | |
3427 | } | |
3428 | ||
3520469d | 3429 | *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, write, writable); |
af585b92 GN |
3430 | return false; |
3431 | } | |
3432 | ||
6a39bbc5 XG |
3433 | static bool |
3434 | check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level) | |
3435 | { | |
3436 | int page_num = KVM_PAGES_PER_HPAGE(level); | |
3437 | ||
3438 | gfn &= ~(page_num - 1); | |
3439 | ||
3440 | return kvm_mtrr_check_gfn_range_consistency(vcpu, gfn, page_num); | |
3441 | } | |
3442 | ||
56028d08 | 3443 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code, |
78b2c54a | 3444 | bool prefault) |
fb72d167 | 3445 | { |
ba049e93 | 3446 | kvm_pfn_t pfn; |
fb72d167 | 3447 | int r; |
852e3c19 | 3448 | int level; |
cd1872f0 | 3449 | bool force_pt_level; |
05da4558 | 3450 | gfn_t gfn = gpa >> PAGE_SHIFT; |
e930bffe | 3451 | unsigned long mmu_seq; |
612819c3 MT |
3452 | int write = error_code & PFERR_WRITE_MASK; |
3453 | bool map_writable; | |
fb72d167 | 3454 | |
fa4a2c08 | 3455 | MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
fb72d167 JR |
3456 | |
3457 | r = mmu_topup_memory_caches(vcpu); | |
3458 | if (r) | |
3459 | return r; | |
3460 | ||
fd136902 TY |
3461 | force_pt_level = !check_hugepage_cache_consistency(vcpu, gfn, |
3462 | PT_DIRECTORY_LEVEL); | |
3463 | level = mapping_level(vcpu, gfn, &force_pt_level); | |
936a5fe6 | 3464 | if (likely(!force_pt_level)) { |
6a39bbc5 XG |
3465 | if (level > PT_DIRECTORY_LEVEL && |
3466 | !check_hugepage_cache_consistency(vcpu, gfn, level)) | |
3467 | level = PT_DIRECTORY_LEVEL; | |
936a5fe6 | 3468 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
fd136902 | 3469 | } |
852e3c19 | 3470 | |
c7ba5b48 XG |
3471 | if (fast_page_fault(vcpu, gpa, level, error_code)) |
3472 | return 0; | |
3473 | ||
e930bffe | 3474 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
4c2155ce | 3475 | smp_rmb(); |
af585b92 | 3476 | |
78b2c54a | 3477 | if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable)) |
af585b92 GN |
3478 | return 0; |
3479 | ||
d7c55201 XG |
3480 | if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r)) |
3481 | return r; | |
3482 | ||
fb72d167 | 3483 | spin_lock(&vcpu->kvm->mmu_lock); |
8ca40a70 | 3484 | if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) |
e930bffe | 3485 | goto out_unlock; |
450e0b41 | 3486 | make_mmu_pages_available(vcpu); |
936a5fe6 AA |
3487 | if (likely(!force_pt_level)) |
3488 | transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level); | |
7ee0e5b2 | 3489 | r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault); |
fb72d167 | 3490 | spin_unlock(&vcpu->kvm->mmu_lock); |
fb72d167 JR |
3491 | |
3492 | return r; | |
e930bffe AA |
3493 | |
3494 | out_unlock: | |
3495 | spin_unlock(&vcpu->kvm->mmu_lock); | |
3496 | kvm_release_pfn_clean(pfn); | |
3497 | return 0; | |
fb72d167 JR |
3498 | } |
3499 | ||
8a3c1a33 PB |
3500 | static void nonpaging_init_context(struct kvm_vcpu *vcpu, |
3501 | struct kvm_mmu *context) | |
6aa8b732 | 3502 | { |
6aa8b732 | 3503 | context->page_fault = nonpaging_page_fault; |
6aa8b732 | 3504 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
e8bc217a | 3505 | context->sync_page = nonpaging_sync_page; |
a7052897 | 3506 | context->invlpg = nonpaging_invlpg; |
0f53b5b1 | 3507 | context->update_pte = nonpaging_update_pte; |
cea0f0e7 | 3508 | context->root_level = 0; |
6aa8b732 | 3509 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
17c3ba9d | 3510 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 3511 | context->direct_map = true; |
2d48a985 | 3512 | context->nx = false; |
6aa8b732 AK |
3513 | } |
3514 | ||
d8d173da | 3515 | void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu) |
6aa8b732 | 3516 | { |
cea0f0e7 | 3517 | mmu_free_roots(vcpu); |
6aa8b732 AK |
3518 | } |
3519 | ||
5777ed34 JR |
3520 | static unsigned long get_cr3(struct kvm_vcpu *vcpu) |
3521 | { | |
9f8fe504 | 3522 | return kvm_read_cr3(vcpu); |
5777ed34 JR |
3523 | } |
3524 | ||
6389ee94 AK |
3525 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
3526 | struct x86_exception *fault) | |
6aa8b732 | 3527 | { |
6389ee94 | 3528 | vcpu->arch.mmu.inject_page_fault(vcpu, fault); |
6aa8b732 AK |
3529 | } |
3530 | ||
54bf36aa | 3531 | static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn, |
f2fd125d | 3532 | unsigned access, int *nr_present) |
ce88decf XG |
3533 | { |
3534 | if (unlikely(is_mmio_spte(*sptep))) { | |
3535 | if (gfn != get_mmio_spte_gfn(*sptep)) { | |
3536 | mmu_spte_clear_no_track(sptep); | |
3537 | return true; | |
3538 | } | |
3539 | ||
3540 | (*nr_present)++; | |
54bf36aa | 3541 | mark_mmio_spte(vcpu, sptep, gfn, access); |
ce88decf XG |
3542 | return true; |
3543 | } | |
3544 | ||
3545 | return false; | |
3546 | } | |
3547 | ||
6fd01b71 AK |
3548 | static inline bool is_last_gpte(struct kvm_mmu *mmu, unsigned level, unsigned gpte) |
3549 | { | |
3550 | unsigned index; | |
3551 | ||
3552 | index = level - 1; | |
3553 | index |= (gpte & PT_PAGE_SIZE_MASK) >> (PT_PAGE_SIZE_SHIFT - 2); | |
3554 | return mmu->last_pte_bitmap & (1 << index); | |
3555 | } | |
3556 | ||
37406aaa NHE |
3557 | #define PTTYPE_EPT 18 /* arbitrary */ |
3558 | #define PTTYPE PTTYPE_EPT | |
3559 | #include "paging_tmpl.h" | |
3560 | #undef PTTYPE | |
3561 | ||
6aa8b732 AK |
3562 | #define PTTYPE 64 |
3563 | #include "paging_tmpl.h" | |
3564 | #undef PTTYPE | |
3565 | ||
3566 | #define PTTYPE 32 | |
3567 | #include "paging_tmpl.h" | |
3568 | #undef PTTYPE | |
3569 | ||
6dc98b86 XG |
3570 | static void |
3571 | __reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, | |
3572 | struct rsvd_bits_validate *rsvd_check, | |
3573 | int maxphyaddr, int level, bool nx, bool gbpages, | |
6fec2144 | 3574 | bool pse, bool amd) |
82725b20 | 3575 | { |
82725b20 | 3576 | u64 exb_bit_rsvd = 0; |
5f7dde7b | 3577 | u64 gbpages_bit_rsvd = 0; |
a0c0feb5 | 3578 | u64 nonleaf_bit8_rsvd = 0; |
82725b20 | 3579 | |
a0a64f50 | 3580 | rsvd_check->bad_mt_xwr = 0; |
25d92081 | 3581 | |
6dc98b86 | 3582 | if (!nx) |
82725b20 | 3583 | exb_bit_rsvd = rsvd_bits(63, 63); |
6dc98b86 | 3584 | if (!gbpages) |
5f7dde7b | 3585 | gbpages_bit_rsvd = rsvd_bits(7, 7); |
a0c0feb5 PB |
3586 | |
3587 | /* | |
3588 | * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for | |
3589 | * leaf entries) on AMD CPUs only. | |
3590 | */ | |
6fec2144 | 3591 | if (amd) |
a0c0feb5 PB |
3592 | nonleaf_bit8_rsvd = rsvd_bits(8, 8); |
3593 | ||
6dc98b86 | 3594 | switch (level) { |
82725b20 DE |
3595 | case PT32_ROOT_LEVEL: |
3596 | /* no rsvd bits for 2 level 4K page table entries */ | |
a0a64f50 XG |
3597 | rsvd_check->rsvd_bits_mask[0][1] = 0; |
3598 | rsvd_check->rsvd_bits_mask[0][0] = 0; | |
3599 | rsvd_check->rsvd_bits_mask[1][0] = | |
3600 | rsvd_check->rsvd_bits_mask[0][0]; | |
f815bce8 | 3601 | |
6dc98b86 | 3602 | if (!pse) { |
a0a64f50 | 3603 | rsvd_check->rsvd_bits_mask[1][1] = 0; |
f815bce8 XG |
3604 | break; |
3605 | } | |
3606 | ||
82725b20 DE |
3607 | if (is_cpuid_PSE36()) |
3608 | /* 36bits PSE 4MB page */ | |
a0a64f50 | 3609 | rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); |
82725b20 DE |
3610 | else |
3611 | /* 32 bits PSE 4MB page */ | |
a0a64f50 | 3612 | rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); |
82725b20 DE |
3613 | break; |
3614 | case PT32E_ROOT_LEVEL: | |
a0a64f50 | 3615 | rsvd_check->rsvd_bits_mask[0][2] = |
20c466b5 | 3616 | rsvd_bits(maxphyaddr, 63) | |
cd9ae5fe | 3617 | rsvd_bits(5, 8) | rsvd_bits(1, 2); /* PDPTE */ |
a0a64f50 | 3618 | rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
4c26b4cd | 3619 | rsvd_bits(maxphyaddr, 62); /* PDE */ |
a0a64f50 | 3620 | rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
82725b20 | 3621 | rsvd_bits(maxphyaddr, 62); /* PTE */ |
a0a64f50 | 3622 | rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
82725b20 DE |
3623 | rsvd_bits(maxphyaddr, 62) | |
3624 | rsvd_bits(13, 20); /* large page */ | |
a0a64f50 XG |
3625 | rsvd_check->rsvd_bits_mask[1][0] = |
3626 | rsvd_check->rsvd_bits_mask[0][0]; | |
82725b20 DE |
3627 | break; |
3628 | case PT64_ROOT_LEVEL: | |
a0a64f50 XG |
3629 | rsvd_check->rsvd_bits_mask[0][3] = exb_bit_rsvd | |
3630 | nonleaf_bit8_rsvd | rsvd_bits(7, 7) | | |
4c26b4cd | 3631 | rsvd_bits(maxphyaddr, 51); |
a0a64f50 XG |
3632 | rsvd_check->rsvd_bits_mask[0][2] = exb_bit_rsvd | |
3633 | nonleaf_bit8_rsvd | gbpages_bit_rsvd | | |
82725b20 | 3634 | rsvd_bits(maxphyaddr, 51); |
a0a64f50 XG |
3635 | rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
3636 | rsvd_bits(maxphyaddr, 51); | |
3637 | rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd | | |
3638 | rsvd_bits(maxphyaddr, 51); | |
3639 | rsvd_check->rsvd_bits_mask[1][3] = | |
3640 | rsvd_check->rsvd_bits_mask[0][3]; | |
3641 | rsvd_check->rsvd_bits_mask[1][2] = exb_bit_rsvd | | |
5f7dde7b | 3642 | gbpages_bit_rsvd | rsvd_bits(maxphyaddr, 51) | |
e04da980 | 3643 | rsvd_bits(13, 29); |
a0a64f50 | 3644 | rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
4c26b4cd SY |
3645 | rsvd_bits(maxphyaddr, 51) | |
3646 | rsvd_bits(13, 20); /* large page */ | |
a0a64f50 XG |
3647 | rsvd_check->rsvd_bits_mask[1][0] = |
3648 | rsvd_check->rsvd_bits_mask[0][0]; | |
82725b20 DE |
3649 | break; |
3650 | } | |
3651 | } | |
3652 | ||
6dc98b86 XG |
3653 | static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, |
3654 | struct kvm_mmu *context) | |
3655 | { | |
3656 | __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check, | |
3657 | cpuid_maxphyaddr(vcpu), context->root_level, | |
3658 | context->nx, guest_cpuid_has_gbpages(vcpu), | |
6fec2144 | 3659 | is_pse(vcpu), guest_cpuid_is_amd(vcpu)); |
6dc98b86 XG |
3660 | } |
3661 | ||
81b8eebb XG |
3662 | static void |
3663 | __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check, | |
3664 | int maxphyaddr, bool execonly) | |
25d92081 | 3665 | { |
951f9fd7 | 3666 | u64 bad_mt_xwr; |
25d92081 | 3667 | |
a0a64f50 | 3668 | rsvd_check->rsvd_bits_mask[0][3] = |
25d92081 | 3669 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7); |
a0a64f50 | 3670 | rsvd_check->rsvd_bits_mask[0][2] = |
25d92081 | 3671 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6); |
a0a64f50 | 3672 | rsvd_check->rsvd_bits_mask[0][1] = |
25d92081 | 3673 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6); |
a0a64f50 | 3674 | rsvd_check->rsvd_bits_mask[0][0] = rsvd_bits(maxphyaddr, 51); |
25d92081 YZ |
3675 | |
3676 | /* large page */ | |
a0a64f50 XG |
3677 | rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3]; |
3678 | rsvd_check->rsvd_bits_mask[1][2] = | |
25d92081 | 3679 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 29); |
a0a64f50 | 3680 | rsvd_check->rsvd_bits_mask[1][1] = |
25d92081 | 3681 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 20); |
a0a64f50 | 3682 | rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0]; |
25d92081 | 3683 | |
951f9fd7 PB |
3684 | bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */ |
3685 | bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */ | |
3686 | bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */ | |
3687 | bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */ | |
3688 | bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */ | |
3689 | if (!execonly) { | |
3690 | /* bits 0..2 must not be 100 unless VMX capabilities allow it */ | |
3691 | bad_mt_xwr |= REPEAT_BYTE(1ull << 4); | |
25d92081 | 3692 | } |
951f9fd7 | 3693 | rsvd_check->bad_mt_xwr = bad_mt_xwr; |
25d92081 YZ |
3694 | } |
3695 | ||
81b8eebb XG |
3696 | static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu, |
3697 | struct kvm_mmu *context, bool execonly) | |
3698 | { | |
3699 | __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check, | |
3700 | cpuid_maxphyaddr(vcpu), execonly); | |
3701 | } | |
3702 | ||
c258b62b XG |
3703 | /* |
3704 | * the page table on host is the shadow page table for the page | |
3705 | * table in guest or amd nested guest, its mmu features completely | |
3706 | * follow the features in guest. | |
3707 | */ | |
3708 | void | |
3709 | reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context) | |
3710 | { | |
6fec2144 PB |
3711 | /* |
3712 | * Passing "true" to the last argument is okay; it adds a check | |
3713 | * on bit 8 of the SPTEs which KVM doesn't use anyway. | |
3714 | */ | |
c258b62b XG |
3715 | __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check, |
3716 | boot_cpu_data.x86_phys_bits, | |
3717 | context->shadow_root_level, context->nx, | |
6fec2144 PB |
3718 | guest_cpuid_has_gbpages(vcpu), is_pse(vcpu), |
3719 | true); | |
c258b62b XG |
3720 | } |
3721 | EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask); | |
3722 | ||
6fec2144 PB |
3723 | static inline bool boot_cpu_is_amd(void) |
3724 | { | |
3725 | WARN_ON_ONCE(!tdp_enabled); | |
3726 | return shadow_x_mask == 0; | |
3727 | } | |
3728 | ||
c258b62b XG |
3729 | /* |
3730 | * the direct page table on host, use as much mmu features as | |
3731 | * possible, however, kvm currently does not do execution-protection. | |
3732 | */ | |
3733 | static void | |
3734 | reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, | |
3735 | struct kvm_mmu *context) | |
3736 | { | |
6fec2144 | 3737 | if (boot_cpu_is_amd()) |
c258b62b XG |
3738 | __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check, |
3739 | boot_cpu_data.x86_phys_bits, | |
3740 | context->shadow_root_level, false, | |
6fec2144 | 3741 | cpu_has_gbpages, true, true); |
c258b62b XG |
3742 | else |
3743 | __reset_rsvds_bits_mask_ept(&context->shadow_zero_check, | |
3744 | boot_cpu_data.x86_phys_bits, | |
3745 | false); | |
3746 | ||
3747 | } | |
3748 | ||
3749 | /* | |
3750 | * as the comments in reset_shadow_zero_bits_mask() except it | |
3751 | * is the shadow page table for intel nested guest. | |
3752 | */ | |
3753 | static void | |
3754 | reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, | |
3755 | struct kvm_mmu *context, bool execonly) | |
3756 | { | |
3757 | __reset_rsvds_bits_mask_ept(&context->shadow_zero_check, | |
3758 | boot_cpu_data.x86_phys_bits, execonly); | |
3759 | } | |
3760 | ||
edc90b7d XG |
3761 | static void update_permission_bitmask(struct kvm_vcpu *vcpu, |
3762 | struct kvm_mmu *mmu, bool ept) | |
97d64b78 AK |
3763 | { |
3764 | unsigned bit, byte, pfec; | |
3765 | u8 map; | |
66386ade | 3766 | bool fault, x, w, u, wf, uf, ff, smapf, cr4_smap, cr4_smep, smap = 0; |
97d64b78 | 3767 | |
66386ade | 3768 | cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP); |
97ec8c06 | 3769 | cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP); |
97d64b78 AK |
3770 | for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) { |
3771 | pfec = byte << 1; | |
3772 | map = 0; | |
3773 | wf = pfec & PFERR_WRITE_MASK; | |
3774 | uf = pfec & PFERR_USER_MASK; | |
3775 | ff = pfec & PFERR_FETCH_MASK; | |
97ec8c06 FW |
3776 | /* |
3777 | * PFERR_RSVD_MASK bit is set in PFEC if the access is not | |
3778 | * subject to SMAP restrictions, and cleared otherwise. The | |
3779 | * bit is only meaningful if the SMAP bit is set in CR4. | |
3780 | */ | |
3781 | smapf = !(pfec & PFERR_RSVD_MASK); | |
97d64b78 AK |
3782 | for (bit = 0; bit < 8; ++bit) { |
3783 | x = bit & ACC_EXEC_MASK; | |
3784 | w = bit & ACC_WRITE_MASK; | |
3785 | u = bit & ACC_USER_MASK; | |
3786 | ||
25d92081 YZ |
3787 | if (!ept) { |
3788 | /* Not really needed: !nx will cause pte.nx to fault */ | |
3789 | x |= !mmu->nx; | |
3790 | /* Allow supervisor writes if !cr0.wp */ | |
3791 | w |= !is_write_protection(vcpu) && !uf; | |
3792 | /* Disallow supervisor fetches of user code if cr4.smep */ | |
66386ade | 3793 | x &= !(cr4_smep && u && !uf); |
97ec8c06 FW |
3794 | |
3795 | /* | |
3796 | * SMAP:kernel-mode data accesses from user-mode | |
3797 | * mappings should fault. A fault is considered | |
3798 | * as a SMAP violation if all of the following | |
3799 | * conditions are ture: | |
3800 | * - X86_CR4_SMAP is set in CR4 | |
3801 | * - An user page is accessed | |
3802 | * - Page fault in kernel mode | |
3803 | * - if CPL = 3 or X86_EFLAGS_AC is clear | |
3804 | * | |
3805 | * Here, we cover the first three conditions. | |
3806 | * The fourth is computed dynamically in | |
3807 | * permission_fault() and is in smapf. | |
3808 | * | |
3809 | * Also, SMAP does not affect instruction | |
3810 | * fetches, add the !ff check here to make it | |
3811 | * clearer. | |
3812 | */ | |
3813 | smap = cr4_smap && u && !uf && !ff; | |
25d92081 YZ |
3814 | } else |
3815 | /* Not really needed: no U/S accesses on ept */ | |
3816 | u = 1; | |
97d64b78 | 3817 | |
97ec8c06 FW |
3818 | fault = (ff && !x) || (uf && !u) || (wf && !w) || |
3819 | (smapf && smap); | |
97d64b78 AK |
3820 | map |= fault << bit; |
3821 | } | |
3822 | mmu->permissions[byte] = map; | |
3823 | } | |
3824 | } | |
3825 | ||
6fd01b71 AK |
3826 | static void update_last_pte_bitmap(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) |
3827 | { | |
3828 | u8 map; | |
3829 | unsigned level, root_level = mmu->root_level; | |
3830 | const unsigned ps_set_index = 1 << 2; /* bit 2 of index: ps */ | |
3831 | ||
3832 | if (root_level == PT32E_ROOT_LEVEL) | |
3833 | --root_level; | |
3834 | /* PT_PAGE_TABLE_LEVEL always terminates */ | |
3835 | map = 1 | (1 << ps_set_index); | |
3836 | for (level = PT_DIRECTORY_LEVEL; level <= root_level; ++level) { | |
3837 | if (level <= PT_PDPE_LEVEL | |
3838 | && (mmu->root_level >= PT32E_ROOT_LEVEL || is_pse(vcpu))) | |
3839 | map |= 1 << (ps_set_index | (level - 1)); | |
3840 | } | |
3841 | mmu->last_pte_bitmap = map; | |
3842 | } | |
3843 | ||
8a3c1a33 PB |
3844 | static void paging64_init_context_common(struct kvm_vcpu *vcpu, |
3845 | struct kvm_mmu *context, | |
3846 | int level) | |
6aa8b732 | 3847 | { |
2d48a985 | 3848 | context->nx = is_nx(vcpu); |
4d6931c3 | 3849 | context->root_level = level; |
2d48a985 | 3850 | |
4d6931c3 | 3851 | reset_rsvds_bits_mask(vcpu, context); |
25d92081 | 3852 | update_permission_bitmask(vcpu, context, false); |
6fd01b71 | 3853 | update_last_pte_bitmap(vcpu, context); |
6aa8b732 | 3854 | |
fa4a2c08 | 3855 | MMU_WARN_ON(!is_pae(vcpu)); |
6aa8b732 | 3856 | context->page_fault = paging64_page_fault; |
6aa8b732 | 3857 | context->gva_to_gpa = paging64_gva_to_gpa; |
e8bc217a | 3858 | context->sync_page = paging64_sync_page; |
a7052897 | 3859 | context->invlpg = paging64_invlpg; |
0f53b5b1 | 3860 | context->update_pte = paging64_update_pte; |
17ac10ad | 3861 | context->shadow_root_level = level; |
17c3ba9d | 3862 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 3863 | context->direct_map = false; |
6aa8b732 AK |
3864 | } |
3865 | ||
8a3c1a33 PB |
3866 | static void paging64_init_context(struct kvm_vcpu *vcpu, |
3867 | struct kvm_mmu *context) | |
17ac10ad | 3868 | { |
8a3c1a33 | 3869 | paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL); |
17ac10ad AK |
3870 | } |
3871 | ||
8a3c1a33 PB |
3872 | static void paging32_init_context(struct kvm_vcpu *vcpu, |
3873 | struct kvm_mmu *context) | |
6aa8b732 | 3874 | { |
2d48a985 | 3875 | context->nx = false; |
4d6931c3 | 3876 | context->root_level = PT32_ROOT_LEVEL; |
2d48a985 | 3877 | |
4d6931c3 | 3878 | reset_rsvds_bits_mask(vcpu, context); |
25d92081 | 3879 | update_permission_bitmask(vcpu, context, false); |
6fd01b71 | 3880 | update_last_pte_bitmap(vcpu, context); |
6aa8b732 | 3881 | |
6aa8b732 | 3882 | context->page_fault = paging32_page_fault; |
6aa8b732 | 3883 | context->gva_to_gpa = paging32_gva_to_gpa; |
e8bc217a | 3884 | context->sync_page = paging32_sync_page; |
a7052897 | 3885 | context->invlpg = paging32_invlpg; |
0f53b5b1 | 3886 | context->update_pte = paging32_update_pte; |
6aa8b732 | 3887 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
17c3ba9d | 3888 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 3889 | context->direct_map = false; |
6aa8b732 AK |
3890 | } |
3891 | ||
8a3c1a33 PB |
3892 | static void paging32E_init_context(struct kvm_vcpu *vcpu, |
3893 | struct kvm_mmu *context) | |
6aa8b732 | 3894 | { |
8a3c1a33 | 3895 | paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL); |
6aa8b732 AK |
3896 | } |
3897 | ||
8a3c1a33 | 3898 | static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
fb72d167 | 3899 | { |
ad896af0 | 3900 | struct kvm_mmu *context = &vcpu->arch.mmu; |
fb72d167 | 3901 | |
c445f8ef | 3902 | context->base_role.word = 0; |
699023e2 | 3903 | context->base_role.smm = is_smm(vcpu); |
fb72d167 | 3904 | context->page_fault = tdp_page_fault; |
e8bc217a | 3905 | context->sync_page = nonpaging_sync_page; |
a7052897 | 3906 | context->invlpg = nonpaging_invlpg; |
0f53b5b1 | 3907 | context->update_pte = nonpaging_update_pte; |
67253af5 | 3908 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); |
fb72d167 | 3909 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 3910 | context->direct_map = true; |
1c97f0a0 | 3911 | context->set_cr3 = kvm_x86_ops->set_tdp_cr3; |
5777ed34 | 3912 | context->get_cr3 = get_cr3; |
e4e517b4 | 3913 | context->get_pdptr = kvm_pdptr_read; |
cb659db8 | 3914 | context->inject_page_fault = kvm_inject_page_fault; |
fb72d167 JR |
3915 | |
3916 | if (!is_paging(vcpu)) { | |
2d48a985 | 3917 | context->nx = false; |
fb72d167 JR |
3918 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
3919 | context->root_level = 0; | |
3920 | } else if (is_long_mode(vcpu)) { | |
2d48a985 | 3921 | context->nx = is_nx(vcpu); |
fb72d167 | 3922 | context->root_level = PT64_ROOT_LEVEL; |
4d6931c3 DB |
3923 | reset_rsvds_bits_mask(vcpu, context); |
3924 | context->gva_to_gpa = paging64_gva_to_gpa; | |
fb72d167 | 3925 | } else if (is_pae(vcpu)) { |
2d48a985 | 3926 | context->nx = is_nx(vcpu); |
fb72d167 | 3927 | context->root_level = PT32E_ROOT_LEVEL; |
4d6931c3 DB |
3928 | reset_rsvds_bits_mask(vcpu, context); |
3929 | context->gva_to_gpa = paging64_gva_to_gpa; | |
fb72d167 | 3930 | } else { |
2d48a985 | 3931 | context->nx = false; |
fb72d167 | 3932 | context->root_level = PT32_ROOT_LEVEL; |
4d6931c3 DB |
3933 | reset_rsvds_bits_mask(vcpu, context); |
3934 | context->gva_to_gpa = paging32_gva_to_gpa; | |
fb72d167 JR |
3935 | } |
3936 | ||
25d92081 | 3937 | update_permission_bitmask(vcpu, context, false); |
6fd01b71 | 3938 | update_last_pte_bitmap(vcpu, context); |
c258b62b | 3939 | reset_tdp_shadow_zero_bits_mask(vcpu, context); |
fb72d167 JR |
3940 | } |
3941 | ||
ad896af0 | 3942 | void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu) |
6aa8b732 | 3943 | { |
411c588d | 3944 | bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP); |
edc90b7d | 3945 | bool smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP); |
ad896af0 PB |
3946 | struct kvm_mmu *context = &vcpu->arch.mmu; |
3947 | ||
fa4a2c08 | 3948 | MMU_WARN_ON(VALID_PAGE(context->root_hpa)); |
6aa8b732 AK |
3949 | |
3950 | if (!is_paging(vcpu)) | |
8a3c1a33 | 3951 | nonpaging_init_context(vcpu, context); |
a9058ecd | 3952 | else if (is_long_mode(vcpu)) |
8a3c1a33 | 3953 | paging64_init_context(vcpu, context); |
6aa8b732 | 3954 | else if (is_pae(vcpu)) |
8a3c1a33 | 3955 | paging32E_init_context(vcpu, context); |
6aa8b732 | 3956 | else |
8a3c1a33 | 3957 | paging32_init_context(vcpu, context); |
a770f6f2 | 3958 | |
ad896af0 PB |
3959 | context->base_role.nxe = is_nx(vcpu); |
3960 | context->base_role.cr4_pae = !!is_pae(vcpu); | |
3961 | context->base_role.cr0_wp = is_write_protection(vcpu); | |
3962 | context->base_role.smep_andnot_wp | |
411c588d | 3963 | = smep && !is_write_protection(vcpu); |
edc90b7d XG |
3964 | context->base_role.smap_andnot_wp |
3965 | = smap && !is_write_protection(vcpu); | |
699023e2 | 3966 | context->base_role.smm = is_smm(vcpu); |
c258b62b | 3967 | reset_shadow_zero_bits_mask(vcpu, context); |
52fde8df JR |
3968 | } |
3969 | EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu); | |
3970 | ||
ad896af0 | 3971 | void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly) |
155a97a3 | 3972 | { |
ad896af0 PB |
3973 | struct kvm_mmu *context = &vcpu->arch.mmu; |
3974 | ||
fa4a2c08 | 3975 | MMU_WARN_ON(VALID_PAGE(context->root_hpa)); |
155a97a3 NHE |
3976 | |
3977 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); | |
3978 | ||
3979 | context->nx = true; | |
155a97a3 NHE |
3980 | context->page_fault = ept_page_fault; |
3981 | context->gva_to_gpa = ept_gva_to_gpa; | |
3982 | context->sync_page = ept_sync_page; | |
3983 | context->invlpg = ept_invlpg; | |
3984 | context->update_pte = ept_update_pte; | |
155a97a3 NHE |
3985 | context->root_level = context->shadow_root_level; |
3986 | context->root_hpa = INVALID_PAGE; | |
3987 | context->direct_map = false; | |
3988 | ||
3989 | update_permission_bitmask(vcpu, context, true); | |
3990 | reset_rsvds_bits_mask_ept(vcpu, context, execonly); | |
c258b62b | 3991 | reset_ept_shadow_zero_bits_mask(vcpu, context, execonly); |
155a97a3 NHE |
3992 | } |
3993 | EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu); | |
3994 | ||
8a3c1a33 | 3995 | static void init_kvm_softmmu(struct kvm_vcpu *vcpu) |
52fde8df | 3996 | { |
ad896af0 PB |
3997 | struct kvm_mmu *context = &vcpu->arch.mmu; |
3998 | ||
3999 | kvm_init_shadow_mmu(vcpu); | |
4000 | context->set_cr3 = kvm_x86_ops->set_cr3; | |
4001 | context->get_cr3 = get_cr3; | |
4002 | context->get_pdptr = kvm_pdptr_read; | |
4003 | context->inject_page_fault = kvm_inject_page_fault; | |
6aa8b732 AK |
4004 | } |
4005 | ||
8a3c1a33 | 4006 | static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu) |
02f59dc9 JR |
4007 | { |
4008 | struct kvm_mmu *g_context = &vcpu->arch.nested_mmu; | |
4009 | ||
4010 | g_context->get_cr3 = get_cr3; | |
e4e517b4 | 4011 | g_context->get_pdptr = kvm_pdptr_read; |
02f59dc9 JR |
4012 | g_context->inject_page_fault = kvm_inject_page_fault; |
4013 | ||
4014 | /* | |
0af2593b DM |
4015 | * Note that arch.mmu.gva_to_gpa translates l2_gpa to l1_gpa using |
4016 | * L1's nested page tables (e.g. EPT12). The nested translation | |
4017 | * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using | |
4018 | * L2's page tables as the first level of translation and L1's | |
4019 | * nested page tables as the second level of translation. Basically | |
4020 | * the gva_to_gpa functions between mmu and nested_mmu are swapped. | |
02f59dc9 JR |
4021 | */ |
4022 | if (!is_paging(vcpu)) { | |
2d48a985 | 4023 | g_context->nx = false; |
02f59dc9 JR |
4024 | g_context->root_level = 0; |
4025 | g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested; | |
4026 | } else if (is_long_mode(vcpu)) { | |
2d48a985 | 4027 | g_context->nx = is_nx(vcpu); |
02f59dc9 | 4028 | g_context->root_level = PT64_ROOT_LEVEL; |
4d6931c3 | 4029 | reset_rsvds_bits_mask(vcpu, g_context); |
02f59dc9 JR |
4030 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; |
4031 | } else if (is_pae(vcpu)) { | |
2d48a985 | 4032 | g_context->nx = is_nx(vcpu); |
02f59dc9 | 4033 | g_context->root_level = PT32E_ROOT_LEVEL; |
4d6931c3 | 4034 | reset_rsvds_bits_mask(vcpu, g_context); |
02f59dc9 JR |
4035 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; |
4036 | } else { | |
2d48a985 | 4037 | g_context->nx = false; |
02f59dc9 | 4038 | g_context->root_level = PT32_ROOT_LEVEL; |
4d6931c3 | 4039 | reset_rsvds_bits_mask(vcpu, g_context); |
02f59dc9 JR |
4040 | g_context->gva_to_gpa = paging32_gva_to_gpa_nested; |
4041 | } | |
4042 | ||
25d92081 | 4043 | update_permission_bitmask(vcpu, g_context, false); |
6fd01b71 | 4044 | update_last_pte_bitmap(vcpu, g_context); |
02f59dc9 JR |
4045 | } |
4046 | ||
8a3c1a33 | 4047 | static void init_kvm_mmu(struct kvm_vcpu *vcpu) |
fb72d167 | 4048 | { |
02f59dc9 | 4049 | if (mmu_is_nested(vcpu)) |
e0c6db3e | 4050 | init_kvm_nested_mmu(vcpu); |
02f59dc9 | 4051 | else if (tdp_enabled) |
e0c6db3e | 4052 | init_kvm_tdp_mmu(vcpu); |
fb72d167 | 4053 | else |
e0c6db3e | 4054 | init_kvm_softmmu(vcpu); |
fb72d167 JR |
4055 | } |
4056 | ||
8a3c1a33 | 4057 | void kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
6aa8b732 | 4058 | { |
95f93af4 | 4059 | kvm_mmu_unload(vcpu); |
8a3c1a33 | 4060 | init_kvm_mmu(vcpu); |
17c3ba9d | 4061 | } |
8668a3c4 | 4062 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
17c3ba9d AK |
4063 | |
4064 | int kvm_mmu_load(struct kvm_vcpu *vcpu) | |
6aa8b732 | 4065 | { |
714b93da AK |
4066 | int r; |
4067 | ||
e2dec939 | 4068 | r = mmu_topup_memory_caches(vcpu); |
17c3ba9d AK |
4069 | if (r) |
4070 | goto out; | |
8986ecc0 | 4071 | r = mmu_alloc_roots(vcpu); |
e2858b4a | 4072 | kvm_mmu_sync_roots(vcpu); |
8986ecc0 MT |
4073 | if (r) |
4074 | goto out; | |
3662cb1c | 4075 | /* set_cr3() should ensure TLB has been flushed */ |
f43addd4 | 4076 | vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
714b93da AK |
4077 | out: |
4078 | return r; | |
6aa8b732 | 4079 | } |
17c3ba9d AK |
4080 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
4081 | ||
4082 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) | |
4083 | { | |
4084 | mmu_free_roots(vcpu); | |
95f93af4 | 4085 | WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
17c3ba9d | 4086 | } |
4b16184c | 4087 | EXPORT_SYMBOL_GPL(kvm_mmu_unload); |
6aa8b732 | 4088 | |
0028425f | 4089 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
7c562522 XG |
4090 | struct kvm_mmu_page *sp, u64 *spte, |
4091 | const void *new) | |
0028425f | 4092 | { |
30945387 | 4093 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) { |
7e4e4056 JR |
4094 | ++vcpu->kvm->stat.mmu_pde_zapped; |
4095 | return; | |
30945387 | 4096 | } |
0028425f | 4097 | |
4cee5764 | 4098 | ++vcpu->kvm->stat.mmu_pte_updated; |
7c562522 | 4099 | vcpu->arch.mmu.update_pte(vcpu, sp, spte, new); |
0028425f AK |
4100 | } |
4101 | ||
79539cec AK |
4102 | static bool need_remote_flush(u64 old, u64 new) |
4103 | { | |
4104 | if (!is_shadow_present_pte(old)) | |
4105 | return false; | |
4106 | if (!is_shadow_present_pte(new)) | |
4107 | return true; | |
4108 | if ((old ^ new) & PT64_BASE_ADDR_MASK) | |
4109 | return true; | |
53166229 GN |
4110 | old ^= shadow_nx_mask; |
4111 | new ^= shadow_nx_mask; | |
79539cec AK |
4112 | return (old & ~new & PT64_PERM_MASK) != 0; |
4113 | } | |
4114 | ||
0671a8e7 XG |
4115 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page, |
4116 | bool remote_flush, bool local_flush) | |
79539cec | 4117 | { |
0671a8e7 XG |
4118 | if (zap_page) |
4119 | return; | |
4120 | ||
4121 | if (remote_flush) | |
79539cec | 4122 | kvm_flush_remote_tlbs(vcpu->kvm); |
0671a8e7 | 4123 | else if (local_flush) |
77c3913b | 4124 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
79539cec AK |
4125 | } |
4126 | ||
889e5cbc XG |
4127 | static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa, |
4128 | const u8 *new, int *bytes) | |
da4a00f0 | 4129 | { |
889e5cbc XG |
4130 | u64 gentry; |
4131 | int r; | |
72016f3a | 4132 | |
72016f3a AK |
4133 | /* |
4134 | * Assume that the pte write on a page table of the same type | |
49b26e26 XG |
4135 | * as the current vcpu paging mode since we update the sptes only |
4136 | * when they have the same mode. | |
72016f3a | 4137 | */ |
889e5cbc | 4138 | if (is_pae(vcpu) && *bytes == 4) { |
72016f3a | 4139 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
889e5cbc XG |
4140 | *gpa &= ~(gpa_t)7; |
4141 | *bytes = 8; | |
54bf36aa | 4142 | r = kvm_vcpu_read_guest(vcpu, *gpa, &gentry, 8); |
72016f3a AK |
4143 | if (r) |
4144 | gentry = 0; | |
08e850c6 AK |
4145 | new = (const u8 *)&gentry; |
4146 | } | |
4147 | ||
889e5cbc | 4148 | switch (*bytes) { |
08e850c6 AK |
4149 | case 4: |
4150 | gentry = *(const u32 *)new; | |
4151 | break; | |
4152 | case 8: | |
4153 | gentry = *(const u64 *)new; | |
4154 | break; | |
4155 | default: | |
4156 | gentry = 0; | |
4157 | break; | |
72016f3a AK |
4158 | } |
4159 | ||
889e5cbc XG |
4160 | return gentry; |
4161 | } | |
4162 | ||
4163 | /* | |
4164 | * If we're seeing too many writes to a page, it may no longer be a page table, | |
4165 | * or we may be forking, in which case it is better to unmap the page. | |
4166 | */ | |
a138fe75 | 4167 | static bool detect_write_flooding(struct kvm_mmu_page *sp) |
889e5cbc | 4168 | { |
a30f47cb XG |
4169 | /* |
4170 | * Skip write-flooding detected for the sp whose level is 1, because | |
4171 | * it can become unsync, then the guest page is not write-protected. | |
4172 | */ | |
f71fa31f | 4173 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) |
a30f47cb | 4174 | return false; |
3246af0e | 4175 | |
a30f47cb | 4176 | return ++sp->write_flooding_count >= 3; |
889e5cbc XG |
4177 | } |
4178 | ||
4179 | /* | |
4180 | * Misaligned accesses are too much trouble to fix up; also, they usually | |
4181 | * indicate a page is not used as a page table. | |
4182 | */ | |
4183 | static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa, | |
4184 | int bytes) | |
4185 | { | |
4186 | unsigned offset, pte_size, misaligned; | |
4187 | ||
4188 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", | |
4189 | gpa, bytes, sp->role.word); | |
4190 | ||
4191 | offset = offset_in_page(gpa); | |
4192 | pte_size = sp->role.cr4_pae ? 8 : 4; | |
5d9ca30e XG |
4193 | |
4194 | /* | |
4195 | * Sometimes, the OS only writes the last one bytes to update status | |
4196 | * bits, for example, in linux, andb instruction is used in clear_bit(). | |
4197 | */ | |
4198 | if (!(offset & (pte_size - 1)) && bytes == 1) | |
4199 | return false; | |
4200 | ||
889e5cbc XG |
4201 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
4202 | misaligned |= bytes < 4; | |
4203 | ||
4204 | return misaligned; | |
4205 | } | |
4206 | ||
4207 | static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte) | |
4208 | { | |
4209 | unsigned page_offset, quadrant; | |
4210 | u64 *spte; | |
4211 | int level; | |
4212 | ||
4213 | page_offset = offset_in_page(gpa); | |
4214 | level = sp->role.level; | |
4215 | *nspte = 1; | |
4216 | if (!sp->role.cr4_pae) { | |
4217 | page_offset <<= 1; /* 32->64 */ | |
4218 | /* | |
4219 | * A 32-bit pde maps 4MB while the shadow pdes map | |
4220 | * only 2MB. So we need to double the offset again | |
4221 | * and zap two pdes instead of one. | |
4222 | */ | |
4223 | if (level == PT32_ROOT_LEVEL) { | |
4224 | page_offset &= ~7; /* kill rounding error */ | |
4225 | page_offset <<= 1; | |
4226 | *nspte = 2; | |
4227 | } | |
4228 | quadrant = page_offset >> PAGE_SHIFT; | |
4229 | page_offset &= ~PAGE_MASK; | |
4230 | if (quadrant != sp->role.quadrant) | |
4231 | return NULL; | |
4232 | } | |
4233 | ||
4234 | spte = &sp->spt[page_offset / sizeof(*spte)]; | |
4235 | return spte; | |
4236 | } | |
4237 | ||
4238 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, | |
4239 | const u8 *new, int bytes) | |
4240 | { | |
4241 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
889e5cbc | 4242 | struct kvm_mmu_page *sp; |
889e5cbc XG |
4243 | LIST_HEAD(invalid_list); |
4244 | u64 entry, gentry, *spte; | |
4245 | int npte; | |
a30f47cb | 4246 | bool remote_flush, local_flush, zap_page; |
4141259b AM |
4247 | union kvm_mmu_page_role mask = { }; |
4248 | ||
4249 | mask.cr0_wp = 1; | |
4250 | mask.cr4_pae = 1; | |
4251 | mask.nxe = 1; | |
4252 | mask.smep_andnot_wp = 1; | |
4253 | mask.smap_andnot_wp = 1; | |
699023e2 | 4254 | mask.smm = 1; |
889e5cbc XG |
4255 | |
4256 | /* | |
4257 | * If we don't have indirect shadow pages, it means no page is | |
4258 | * write-protected, so we can exit simply. | |
4259 | */ | |
4260 | if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages)) | |
4261 | return; | |
4262 | ||
4263 | zap_page = remote_flush = local_flush = false; | |
4264 | ||
4265 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); | |
4266 | ||
4267 | gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes); | |
4268 | ||
4269 | /* | |
4270 | * No need to care whether allocation memory is successful | |
4271 | * or not since pte prefetch is skiped if it does not have | |
4272 | * enough objects in the cache. | |
4273 | */ | |
4274 | mmu_topup_memory_caches(vcpu); | |
4275 | ||
4276 | spin_lock(&vcpu->kvm->mmu_lock); | |
4277 | ++vcpu->kvm->stat.mmu_pte_write; | |
0375f7fa | 4278 | kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE); |
889e5cbc | 4279 | |
b67bfe0d | 4280 | for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) { |
a30f47cb | 4281 | if (detect_write_misaligned(sp, gpa, bytes) || |
a138fe75 | 4282 | detect_write_flooding(sp)) { |
0671a8e7 | 4283 | zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
f41d335a | 4284 | &invalid_list); |
4cee5764 | 4285 | ++vcpu->kvm->stat.mmu_flooded; |
0e7bc4b9 AK |
4286 | continue; |
4287 | } | |
889e5cbc XG |
4288 | |
4289 | spte = get_written_sptes(sp, gpa, &npte); | |
4290 | if (!spte) | |
4291 | continue; | |
4292 | ||
0671a8e7 | 4293 | local_flush = true; |
ac1b714e | 4294 | while (npte--) { |
79539cec | 4295 | entry = *spte; |
38e3b2b2 | 4296 | mmu_page_zap_pte(vcpu->kvm, sp, spte); |
fa1de2bf XG |
4297 | if (gentry && |
4298 | !((sp->role.word ^ vcpu->arch.mmu.base_role.word) | |
f759e2b4 | 4299 | & mask.word) && rmap_can_add(vcpu)) |
7c562522 | 4300 | mmu_pte_write_new_pte(vcpu, sp, spte, &gentry); |
9bb4f6b1 | 4301 | if (need_remote_flush(entry, *spte)) |
0671a8e7 | 4302 | remote_flush = true; |
ac1b714e | 4303 | ++spte; |
9b7a0325 | 4304 | } |
9b7a0325 | 4305 | } |
0671a8e7 | 4306 | mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush); |
d98ba053 | 4307 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
0375f7fa | 4308 | kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE); |
aaee2c94 | 4309 | spin_unlock(&vcpu->kvm->mmu_lock); |
da4a00f0 AK |
4310 | } |
4311 | ||
a436036b AK |
4312 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
4313 | { | |
10589a46 MT |
4314 | gpa_t gpa; |
4315 | int r; | |
a436036b | 4316 | |
c5a78f2b | 4317 | if (vcpu->arch.mmu.direct_map) |
60f24784 AK |
4318 | return 0; |
4319 | ||
1871c602 | 4320 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL); |
10589a46 | 4321 | |
10589a46 | 4322 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
1cb3f3ae | 4323 | |
10589a46 | 4324 | return r; |
a436036b | 4325 | } |
577bdc49 | 4326 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt); |
a436036b | 4327 | |
81f4f76b | 4328 | static void make_mmu_pages_available(struct kvm_vcpu *vcpu) |
ebeace86 | 4329 | { |
d98ba053 | 4330 | LIST_HEAD(invalid_list); |
103ad25a | 4331 | |
81f4f76b TY |
4332 | if (likely(kvm_mmu_available_pages(vcpu->kvm) >= KVM_MIN_FREE_MMU_PAGES)) |
4333 | return; | |
4334 | ||
5da59607 TY |
4335 | while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES) { |
4336 | if (!prepare_zap_oldest_mmu_page(vcpu->kvm, &invalid_list)) | |
4337 | break; | |
ebeace86 | 4338 | |
4cee5764 | 4339 | ++vcpu->kvm->stat.mmu_recycled; |
ebeace86 | 4340 | } |
aa6bd187 | 4341 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
ebeace86 | 4342 | } |
ebeace86 | 4343 | |
dc25e89e AP |
4344 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code, |
4345 | void *insn, int insn_len) | |
3067714c | 4346 | { |
1cb3f3ae | 4347 | int r, emulation_type = EMULTYPE_RETRY; |
3067714c | 4348 | enum emulation_result er; |
ded58749 | 4349 | bool direct = vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu); |
3067714c | 4350 | |
e9ee956e TY |
4351 | if (unlikely(error_code & PFERR_RSVD_MASK)) { |
4352 | r = handle_mmio_page_fault(vcpu, cr2, direct); | |
4353 | if (r == RET_MMIO_PF_EMULATE) { | |
4354 | emulation_type = 0; | |
4355 | goto emulate; | |
4356 | } | |
4357 | if (r == RET_MMIO_PF_RETRY) | |
4358 | return 1; | |
4359 | if (r < 0) | |
4360 | return r; | |
4361 | } | |
4362 | ||
56028d08 | 4363 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false); |
3067714c | 4364 | if (r < 0) |
e9ee956e TY |
4365 | return r; |
4366 | if (!r) | |
4367 | return 1; | |
3067714c | 4368 | |
ded58749 | 4369 | if (mmio_info_in_cache(vcpu, cr2, direct)) |
1cb3f3ae | 4370 | emulation_type = 0; |
e9ee956e | 4371 | emulate: |
1cb3f3ae | 4372 | er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len); |
3067714c AK |
4373 | |
4374 | switch (er) { | |
4375 | case EMULATE_DONE: | |
4376 | return 1; | |
ac0a48c3 | 4377 | case EMULATE_USER_EXIT: |
3067714c | 4378 | ++vcpu->stat.mmio_exits; |
6d77dbfc | 4379 | /* fall through */ |
3067714c | 4380 | case EMULATE_FAIL: |
3f5d18a9 | 4381 | return 0; |
3067714c AK |
4382 | default: |
4383 | BUG(); | |
4384 | } | |
3067714c AK |
4385 | } |
4386 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); | |
4387 | ||
a7052897 MT |
4388 | void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
4389 | { | |
a7052897 | 4390 | vcpu->arch.mmu.invlpg(vcpu, gva); |
77c3913b | 4391 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
a7052897 MT |
4392 | ++vcpu->stat.invlpg; |
4393 | } | |
4394 | EXPORT_SYMBOL_GPL(kvm_mmu_invlpg); | |
4395 | ||
18552672 JR |
4396 | void kvm_enable_tdp(void) |
4397 | { | |
4398 | tdp_enabled = true; | |
4399 | } | |
4400 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); | |
4401 | ||
5f4cb662 JR |
4402 | void kvm_disable_tdp(void) |
4403 | { | |
4404 | tdp_enabled = false; | |
4405 | } | |
4406 | EXPORT_SYMBOL_GPL(kvm_disable_tdp); | |
4407 | ||
6aa8b732 AK |
4408 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
4409 | { | |
ad312c7c | 4410 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
81407ca5 JR |
4411 | if (vcpu->arch.mmu.lm_root != NULL) |
4412 | free_page((unsigned long)vcpu->arch.mmu.lm_root); | |
6aa8b732 AK |
4413 | } |
4414 | ||
4415 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) | |
4416 | { | |
17ac10ad | 4417 | struct page *page; |
6aa8b732 AK |
4418 | int i; |
4419 | ||
17ac10ad AK |
4420 | /* |
4421 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. | |
4422 | * Therefore we need to allocate shadow page tables in the first | |
4423 | * 4GB of memory, which happens to fit the DMA32 zone. | |
4424 | */ | |
4425 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); | |
4426 | if (!page) | |
d7fa6ab2 WY |
4427 | return -ENOMEM; |
4428 | ||
ad312c7c | 4429 | vcpu->arch.mmu.pae_root = page_address(page); |
17ac10ad | 4430 | for (i = 0; i < 4; ++i) |
ad312c7c | 4431 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
17ac10ad | 4432 | |
6aa8b732 | 4433 | return 0; |
6aa8b732 AK |
4434 | } |
4435 | ||
8018c27b | 4436 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
6aa8b732 | 4437 | { |
e459e322 XG |
4438 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
4439 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; | |
4440 | vcpu->arch.mmu.translate_gpa = translate_gpa; | |
4441 | vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa; | |
6aa8b732 | 4442 | |
8018c27b IM |
4443 | return alloc_mmu_pages(vcpu); |
4444 | } | |
6aa8b732 | 4445 | |
8a3c1a33 | 4446 | void kvm_mmu_setup(struct kvm_vcpu *vcpu) |
8018c27b | 4447 | { |
fa4a2c08 | 4448 | MMU_WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
2c264957 | 4449 | |
8a3c1a33 | 4450 | init_kvm_mmu(vcpu); |
6aa8b732 AK |
4451 | } |
4452 | ||
1bad2b2a | 4453 | /* The return value indicates if tlb flush on all vcpus is needed. */ |
018aabb5 | 4454 | typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head); |
1bad2b2a XG |
4455 | |
4456 | /* The caller should hold mmu-lock before calling this function. */ | |
4457 | static bool | |
4458 | slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot, | |
4459 | slot_level_handler fn, int start_level, int end_level, | |
4460 | gfn_t start_gfn, gfn_t end_gfn, bool lock_flush_tlb) | |
4461 | { | |
4462 | struct slot_rmap_walk_iterator iterator; | |
4463 | bool flush = false; | |
4464 | ||
4465 | for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn, | |
4466 | end_gfn, &iterator) { | |
4467 | if (iterator.rmap) | |
4468 | flush |= fn(kvm, iterator.rmap); | |
4469 | ||
4470 | if (need_resched() || spin_needbreak(&kvm->mmu_lock)) { | |
4471 | if (flush && lock_flush_tlb) { | |
4472 | kvm_flush_remote_tlbs(kvm); | |
4473 | flush = false; | |
4474 | } | |
4475 | cond_resched_lock(&kvm->mmu_lock); | |
4476 | } | |
4477 | } | |
4478 | ||
4479 | if (flush && lock_flush_tlb) { | |
4480 | kvm_flush_remote_tlbs(kvm); | |
4481 | flush = false; | |
4482 | } | |
4483 | ||
4484 | return flush; | |
4485 | } | |
4486 | ||
4487 | static bool | |
4488 | slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot, | |
4489 | slot_level_handler fn, int start_level, int end_level, | |
4490 | bool lock_flush_tlb) | |
4491 | { | |
4492 | return slot_handle_level_range(kvm, memslot, fn, start_level, | |
4493 | end_level, memslot->base_gfn, | |
4494 | memslot->base_gfn + memslot->npages - 1, | |
4495 | lock_flush_tlb); | |
4496 | } | |
4497 | ||
4498 | static bool | |
4499 | slot_handle_all_level(struct kvm *kvm, struct kvm_memory_slot *memslot, | |
4500 | slot_level_handler fn, bool lock_flush_tlb) | |
4501 | { | |
4502 | return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL, | |
4503 | PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb); | |
4504 | } | |
4505 | ||
4506 | static bool | |
4507 | slot_handle_large_level(struct kvm *kvm, struct kvm_memory_slot *memslot, | |
4508 | slot_level_handler fn, bool lock_flush_tlb) | |
4509 | { | |
4510 | return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL + 1, | |
4511 | PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb); | |
4512 | } | |
4513 | ||
4514 | static bool | |
4515 | slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot, | |
4516 | slot_level_handler fn, bool lock_flush_tlb) | |
4517 | { | |
4518 | return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL, | |
4519 | PT_PAGE_TABLE_LEVEL, lock_flush_tlb); | |
4520 | } | |
4521 | ||
efdfe536 XG |
4522 | void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) |
4523 | { | |
4524 | struct kvm_memslots *slots; | |
4525 | struct kvm_memory_slot *memslot; | |
9da0e4d5 | 4526 | int i; |
efdfe536 XG |
4527 | |
4528 | spin_lock(&kvm->mmu_lock); | |
9da0e4d5 PB |
4529 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
4530 | slots = __kvm_memslots(kvm, i); | |
4531 | kvm_for_each_memslot(memslot, slots) { | |
4532 | gfn_t start, end; | |
4533 | ||
4534 | start = max(gfn_start, memslot->base_gfn); | |
4535 | end = min(gfn_end, memslot->base_gfn + memslot->npages); | |
4536 | if (start >= end) | |
4537 | continue; | |
efdfe536 | 4538 | |
9da0e4d5 PB |
4539 | slot_handle_level_range(kvm, memslot, kvm_zap_rmapp, |
4540 | PT_PAGE_TABLE_LEVEL, PT_MAX_HUGEPAGE_LEVEL, | |
4541 | start, end - 1, true); | |
4542 | } | |
efdfe536 XG |
4543 | } |
4544 | ||
4545 | spin_unlock(&kvm->mmu_lock); | |
4546 | } | |
4547 | ||
018aabb5 TY |
4548 | static bool slot_rmap_write_protect(struct kvm *kvm, |
4549 | struct kvm_rmap_head *rmap_head) | |
d77aa73c | 4550 | { |
018aabb5 | 4551 | return __rmap_write_protect(kvm, rmap_head, false); |
d77aa73c XG |
4552 | } |
4553 | ||
1c91cad4 KH |
4554 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, |
4555 | struct kvm_memory_slot *memslot) | |
6aa8b732 | 4556 | { |
d77aa73c | 4557 | bool flush; |
6aa8b732 | 4558 | |
9d1beefb | 4559 | spin_lock(&kvm->mmu_lock); |
d77aa73c XG |
4560 | flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect, |
4561 | false); | |
9d1beefb | 4562 | spin_unlock(&kvm->mmu_lock); |
198c74f4 XG |
4563 | |
4564 | /* | |
4565 | * kvm_mmu_slot_remove_write_access() and kvm_vm_ioctl_get_dirty_log() | |
4566 | * which do tlb flush out of mmu-lock should be serialized by | |
4567 | * kvm->slots_lock otherwise tlb flush would be missed. | |
4568 | */ | |
4569 | lockdep_assert_held(&kvm->slots_lock); | |
4570 | ||
4571 | /* | |
4572 | * We can flush all the TLBs out of the mmu lock without TLB | |
4573 | * corruption since we just change the spte from writable to | |
4574 | * readonly so that we only need to care the case of changing | |
4575 | * spte from present to present (changing the spte from present | |
4576 | * to nonpresent will flush all the TLBs immediately), in other | |
4577 | * words, the only case we care is mmu_spte_update() where we | |
4578 | * haved checked SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE | |
4579 | * instead of PT_WRITABLE_MASK, that means it does not depend | |
4580 | * on PT_WRITABLE_MASK anymore. | |
4581 | */ | |
d91ffee9 KH |
4582 | if (flush) |
4583 | kvm_flush_remote_tlbs(kvm); | |
6aa8b732 | 4584 | } |
37a7d8b0 | 4585 | |
3ea3b7fa | 4586 | static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, |
018aabb5 | 4587 | struct kvm_rmap_head *rmap_head) |
3ea3b7fa WL |
4588 | { |
4589 | u64 *sptep; | |
4590 | struct rmap_iterator iter; | |
4591 | int need_tlb_flush = 0; | |
ba049e93 | 4592 | kvm_pfn_t pfn; |
3ea3b7fa WL |
4593 | struct kvm_mmu_page *sp; |
4594 | ||
0d536790 | 4595 | restart: |
018aabb5 | 4596 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
3ea3b7fa WL |
4597 | sp = page_header(__pa(sptep)); |
4598 | pfn = spte_to_pfn(*sptep); | |
4599 | ||
4600 | /* | |
decf6333 XG |
4601 | * We cannot do huge page mapping for indirect shadow pages, |
4602 | * which are found on the last rmap (level = 1) when not using | |
4603 | * tdp; such shadow pages are synced with the page table in | |
4604 | * the guest, and the guest page table is using 4K page size | |
4605 | * mapping if the indirect sp has level = 1. | |
3ea3b7fa WL |
4606 | */ |
4607 | if (sp->role.direct && | |
4608 | !kvm_is_reserved_pfn(pfn) && | |
4609 | PageTransCompound(pfn_to_page(pfn))) { | |
4610 | drop_spte(kvm, sptep); | |
3ea3b7fa | 4611 | need_tlb_flush = 1; |
0d536790 XG |
4612 | goto restart; |
4613 | } | |
3ea3b7fa WL |
4614 | } |
4615 | ||
4616 | return need_tlb_flush; | |
4617 | } | |
4618 | ||
4619 | void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm, | |
f36f3f28 | 4620 | const struct kvm_memory_slot *memslot) |
3ea3b7fa | 4621 | { |
f36f3f28 | 4622 | /* FIXME: const-ify all uses of struct kvm_memory_slot. */ |
3ea3b7fa | 4623 | spin_lock(&kvm->mmu_lock); |
f36f3f28 PB |
4624 | slot_handle_leaf(kvm, (struct kvm_memory_slot *)memslot, |
4625 | kvm_mmu_zap_collapsible_spte, true); | |
3ea3b7fa WL |
4626 | spin_unlock(&kvm->mmu_lock); |
4627 | } | |
4628 | ||
f4b4b180 KH |
4629 | void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, |
4630 | struct kvm_memory_slot *memslot) | |
4631 | { | |
d77aa73c | 4632 | bool flush; |
f4b4b180 KH |
4633 | |
4634 | spin_lock(&kvm->mmu_lock); | |
d77aa73c | 4635 | flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false); |
f4b4b180 KH |
4636 | spin_unlock(&kvm->mmu_lock); |
4637 | ||
4638 | lockdep_assert_held(&kvm->slots_lock); | |
4639 | ||
4640 | /* | |
4641 | * It's also safe to flush TLBs out of mmu lock here as currently this | |
4642 | * function is only used for dirty logging, in which case flushing TLB | |
4643 | * out of mmu lock also guarantees no dirty pages will be lost in | |
4644 | * dirty_bitmap. | |
4645 | */ | |
4646 | if (flush) | |
4647 | kvm_flush_remote_tlbs(kvm); | |
4648 | } | |
4649 | EXPORT_SYMBOL_GPL(kvm_mmu_slot_leaf_clear_dirty); | |
4650 | ||
4651 | void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm, | |
4652 | struct kvm_memory_slot *memslot) | |
4653 | { | |
d77aa73c | 4654 | bool flush; |
f4b4b180 KH |
4655 | |
4656 | spin_lock(&kvm->mmu_lock); | |
d77aa73c XG |
4657 | flush = slot_handle_large_level(kvm, memslot, slot_rmap_write_protect, |
4658 | false); | |
f4b4b180 KH |
4659 | spin_unlock(&kvm->mmu_lock); |
4660 | ||
4661 | /* see kvm_mmu_slot_remove_write_access */ | |
4662 | lockdep_assert_held(&kvm->slots_lock); | |
4663 | ||
4664 | if (flush) | |
4665 | kvm_flush_remote_tlbs(kvm); | |
4666 | } | |
4667 | EXPORT_SYMBOL_GPL(kvm_mmu_slot_largepage_remove_write_access); | |
4668 | ||
4669 | void kvm_mmu_slot_set_dirty(struct kvm *kvm, | |
4670 | struct kvm_memory_slot *memslot) | |
4671 | { | |
d77aa73c | 4672 | bool flush; |
f4b4b180 KH |
4673 | |
4674 | spin_lock(&kvm->mmu_lock); | |
d77aa73c | 4675 | flush = slot_handle_all_level(kvm, memslot, __rmap_set_dirty, false); |
f4b4b180 KH |
4676 | spin_unlock(&kvm->mmu_lock); |
4677 | ||
4678 | lockdep_assert_held(&kvm->slots_lock); | |
4679 | ||
4680 | /* see kvm_mmu_slot_leaf_clear_dirty */ | |
4681 | if (flush) | |
4682 | kvm_flush_remote_tlbs(kvm); | |
4683 | } | |
4684 | EXPORT_SYMBOL_GPL(kvm_mmu_slot_set_dirty); | |
4685 | ||
e7d11c7a | 4686 | #define BATCH_ZAP_PAGES 10 |
5304b8d3 XG |
4687 | static void kvm_zap_obsolete_pages(struct kvm *kvm) |
4688 | { | |
4689 | struct kvm_mmu_page *sp, *node; | |
e7d11c7a | 4690 | int batch = 0; |
5304b8d3 XG |
4691 | |
4692 | restart: | |
4693 | list_for_each_entry_safe_reverse(sp, node, | |
4694 | &kvm->arch.active_mmu_pages, link) { | |
e7d11c7a XG |
4695 | int ret; |
4696 | ||
5304b8d3 XG |
4697 | /* |
4698 | * No obsolete page exists before new created page since | |
4699 | * active_mmu_pages is the FIFO list. | |
4700 | */ | |
4701 | if (!is_obsolete_sp(kvm, sp)) | |
4702 | break; | |
4703 | ||
4704 | /* | |
5304b8d3 XG |
4705 | * Since we are reversely walking the list and the invalid |
4706 | * list will be moved to the head, skip the invalid page | |
4707 | * can help us to avoid the infinity list walking. | |
4708 | */ | |
4709 | if (sp->role.invalid) | |
4710 | continue; | |
4711 | ||
f34d251d XG |
4712 | /* |
4713 | * Need not flush tlb since we only zap the sp with invalid | |
4714 | * generation number. | |
4715 | */ | |
e7d11c7a | 4716 | if (batch >= BATCH_ZAP_PAGES && |
f34d251d | 4717 | cond_resched_lock(&kvm->mmu_lock)) { |
e7d11c7a | 4718 | batch = 0; |
5304b8d3 XG |
4719 | goto restart; |
4720 | } | |
4721 | ||
365c8868 XG |
4722 | ret = kvm_mmu_prepare_zap_page(kvm, sp, |
4723 | &kvm->arch.zapped_obsolete_pages); | |
e7d11c7a XG |
4724 | batch += ret; |
4725 | ||
4726 | if (ret) | |
5304b8d3 XG |
4727 | goto restart; |
4728 | } | |
4729 | ||
f34d251d XG |
4730 | /* |
4731 | * Should flush tlb before free page tables since lockless-walking | |
4732 | * may use the pages. | |
4733 | */ | |
365c8868 | 4734 | kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages); |
5304b8d3 XG |
4735 | } |
4736 | ||
4737 | /* | |
4738 | * Fast invalidate all shadow pages and use lock-break technique | |
4739 | * to zap obsolete pages. | |
4740 | * | |
4741 | * It's required when memslot is being deleted or VM is being | |
4742 | * destroyed, in these cases, we should ensure that KVM MMU does | |
4743 | * not use any resource of the being-deleted slot or all slots | |
4744 | * after calling the function. | |
4745 | */ | |
4746 | void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm) | |
4747 | { | |
4748 | spin_lock(&kvm->mmu_lock); | |
35006126 | 4749 | trace_kvm_mmu_invalidate_zap_all_pages(kvm); |
5304b8d3 XG |
4750 | kvm->arch.mmu_valid_gen++; |
4751 | ||
f34d251d XG |
4752 | /* |
4753 | * Notify all vcpus to reload its shadow page table | |
4754 | * and flush TLB. Then all vcpus will switch to new | |
4755 | * shadow page table with the new mmu_valid_gen. | |
4756 | * | |
4757 | * Note: we should do this under the protection of | |
4758 | * mmu-lock, otherwise, vcpu would purge shadow page | |
4759 | * but miss tlb flush. | |
4760 | */ | |
4761 | kvm_reload_remote_mmus(kvm); | |
4762 | ||
5304b8d3 XG |
4763 | kvm_zap_obsolete_pages(kvm); |
4764 | spin_unlock(&kvm->mmu_lock); | |
4765 | } | |
4766 | ||
365c8868 XG |
4767 | static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) |
4768 | { | |
4769 | return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages)); | |
4770 | } | |
4771 | ||
54bf36aa | 4772 | void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots) |
f8f55942 XG |
4773 | { |
4774 | /* | |
4775 | * The very rare case: if the generation-number is round, | |
4776 | * zap all shadow pages. | |
f8f55942 | 4777 | */ |
54bf36aa | 4778 | if (unlikely((slots->generation & MMIO_GEN_MASK) == 0)) { |
a629df7e | 4779 | printk_ratelimited(KERN_DEBUG "kvm: zapping shadow pages for mmio generation wraparound\n"); |
a8eca9dc | 4780 | kvm_mmu_invalidate_zap_all_pages(kvm); |
7a2e8aaf | 4781 | } |
f8f55942 XG |
4782 | } |
4783 | ||
70534a73 DC |
4784 | static unsigned long |
4785 | mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) | |
3ee16c81 IE |
4786 | { |
4787 | struct kvm *kvm; | |
1495f230 | 4788 | int nr_to_scan = sc->nr_to_scan; |
70534a73 | 4789 | unsigned long freed = 0; |
3ee16c81 | 4790 | |
2f303b74 | 4791 | spin_lock(&kvm_lock); |
3ee16c81 IE |
4792 | |
4793 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
3d56cbdf | 4794 | int idx; |
d98ba053 | 4795 | LIST_HEAD(invalid_list); |
3ee16c81 | 4796 | |
35f2d16b TY |
4797 | /* |
4798 | * Never scan more than sc->nr_to_scan VM instances. | |
4799 | * Will not hit this condition practically since we do not try | |
4800 | * to shrink more than one VM and it is very unlikely to see | |
4801 | * !n_used_mmu_pages so many times. | |
4802 | */ | |
4803 | if (!nr_to_scan--) | |
4804 | break; | |
19526396 GN |
4805 | /* |
4806 | * n_used_mmu_pages is accessed without holding kvm->mmu_lock | |
4807 | * here. We may skip a VM instance errorneosly, but we do not | |
4808 | * want to shrink a VM that only started to populate its MMU | |
4809 | * anyway. | |
4810 | */ | |
365c8868 XG |
4811 | if (!kvm->arch.n_used_mmu_pages && |
4812 | !kvm_has_zapped_obsolete_pages(kvm)) | |
19526396 | 4813 | continue; |
19526396 | 4814 | |
f656ce01 | 4815 | idx = srcu_read_lock(&kvm->srcu); |
3ee16c81 | 4816 | spin_lock(&kvm->mmu_lock); |
3ee16c81 | 4817 | |
365c8868 XG |
4818 | if (kvm_has_zapped_obsolete_pages(kvm)) { |
4819 | kvm_mmu_commit_zap_page(kvm, | |
4820 | &kvm->arch.zapped_obsolete_pages); | |
4821 | goto unlock; | |
4822 | } | |
4823 | ||
70534a73 DC |
4824 | if (prepare_zap_oldest_mmu_page(kvm, &invalid_list)) |
4825 | freed++; | |
d98ba053 | 4826 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
19526396 | 4827 | |
365c8868 | 4828 | unlock: |
3ee16c81 | 4829 | spin_unlock(&kvm->mmu_lock); |
f656ce01 | 4830 | srcu_read_unlock(&kvm->srcu, idx); |
19526396 | 4831 | |
70534a73 DC |
4832 | /* |
4833 | * unfair on small ones | |
4834 | * per-vm shrinkers cry out | |
4835 | * sadness comes quickly | |
4836 | */ | |
19526396 GN |
4837 | list_move_tail(&kvm->vm_list, &vm_list); |
4838 | break; | |
3ee16c81 | 4839 | } |
3ee16c81 | 4840 | |
2f303b74 | 4841 | spin_unlock(&kvm_lock); |
70534a73 | 4842 | return freed; |
70534a73 DC |
4843 | } |
4844 | ||
4845 | static unsigned long | |
4846 | mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc) | |
4847 | { | |
45221ab6 | 4848 | return percpu_counter_read_positive(&kvm_total_used_mmu_pages); |
3ee16c81 IE |
4849 | } |
4850 | ||
4851 | static struct shrinker mmu_shrinker = { | |
70534a73 DC |
4852 | .count_objects = mmu_shrink_count, |
4853 | .scan_objects = mmu_shrink_scan, | |
3ee16c81 IE |
4854 | .seeks = DEFAULT_SEEKS * 10, |
4855 | }; | |
4856 | ||
2ddfd20e | 4857 | static void mmu_destroy_caches(void) |
b5a33a75 | 4858 | { |
53c07b18 XG |
4859 | if (pte_list_desc_cache) |
4860 | kmem_cache_destroy(pte_list_desc_cache); | |
d3d25b04 AK |
4861 | if (mmu_page_header_cache) |
4862 | kmem_cache_destroy(mmu_page_header_cache); | |
b5a33a75 AK |
4863 | } |
4864 | ||
4865 | int kvm_mmu_module_init(void) | |
4866 | { | |
53c07b18 XG |
4867 | pte_list_desc_cache = kmem_cache_create("pte_list_desc", |
4868 | sizeof(struct pte_list_desc), | |
20c2df83 | 4869 | 0, 0, NULL); |
53c07b18 | 4870 | if (!pte_list_desc_cache) |
b5a33a75 AK |
4871 | goto nomem; |
4872 | ||
d3d25b04 AK |
4873 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
4874 | sizeof(struct kvm_mmu_page), | |
20c2df83 | 4875 | 0, 0, NULL); |
d3d25b04 AK |
4876 | if (!mmu_page_header_cache) |
4877 | goto nomem; | |
4878 | ||
908c7f19 | 4879 | if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL)) |
45bf21a8 WY |
4880 | goto nomem; |
4881 | ||
3ee16c81 IE |
4882 | register_shrinker(&mmu_shrinker); |
4883 | ||
b5a33a75 AK |
4884 | return 0; |
4885 | ||
4886 | nomem: | |
3ee16c81 | 4887 | mmu_destroy_caches(); |
b5a33a75 AK |
4888 | return -ENOMEM; |
4889 | } | |
4890 | ||
3ad82a7e ZX |
4891 | /* |
4892 | * Caculate mmu pages needed for kvm. | |
4893 | */ | |
4894 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) | |
4895 | { | |
3ad82a7e ZX |
4896 | unsigned int nr_mmu_pages; |
4897 | unsigned int nr_pages = 0; | |
bc6678a3 | 4898 | struct kvm_memslots *slots; |
be6ba0f0 | 4899 | struct kvm_memory_slot *memslot; |
9da0e4d5 | 4900 | int i; |
3ad82a7e | 4901 | |
9da0e4d5 PB |
4902 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
4903 | slots = __kvm_memslots(kvm, i); | |
90d83dc3 | 4904 | |
9da0e4d5 PB |
4905 | kvm_for_each_memslot(memslot, slots) |
4906 | nr_pages += memslot->npages; | |
4907 | } | |
3ad82a7e ZX |
4908 | |
4909 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; | |
4910 | nr_mmu_pages = max(nr_mmu_pages, | |
9da0e4d5 | 4911 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
3ad82a7e ZX |
4912 | |
4913 | return nr_mmu_pages; | |
4914 | } | |
4915 | ||
c42fffe3 XG |
4916 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
4917 | { | |
95f93af4 | 4918 | kvm_mmu_unload(vcpu); |
c42fffe3 XG |
4919 | free_mmu_pages(vcpu); |
4920 | mmu_free_memory_caches(vcpu); | |
b034cf01 XG |
4921 | } |
4922 | ||
b034cf01 XG |
4923 | void kvm_mmu_module_exit(void) |
4924 | { | |
4925 | mmu_destroy_caches(); | |
4926 | percpu_counter_destroy(&kvm_total_used_mmu_pages); | |
4927 | unregister_shrinker(&mmu_shrinker); | |
c42fffe3 XG |
4928 | mmu_audit_disable(); |
4929 | } |