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" |
af585b92 | 25 | #include "x86.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 XG |
63 | char *audit_point_name[] = { |
64 | "pre page fault", | |
65 | "post page fault", | |
66 | "pre pte write", | |
6903074c XG |
67 | "post pte write", |
68 | "pre sync", | |
69 | "post sync" | |
8b1fe17c | 70 | }; |
37a7d8b0 | 71 | |
8b1fe17c | 72 | #undef MMU_DEBUG |
37a7d8b0 AK |
73 | |
74 | #ifdef MMU_DEBUG | |
75 | ||
76 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) | |
77 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) | |
78 | ||
79 | #else | |
80 | ||
81 | #define pgprintk(x...) do { } while (0) | |
82 | #define rmap_printk(x...) do { } while (0) | |
83 | ||
84 | #endif | |
85 | ||
8b1fe17c | 86 | #ifdef MMU_DEBUG |
6ada8cca AK |
87 | static int dbg = 0; |
88 | module_param(dbg, bool, 0644); | |
37a7d8b0 | 89 | #endif |
6aa8b732 | 90 | |
582801a9 MT |
91 | static int oos_shadow = 1; |
92 | module_param(oos_shadow, bool, 0644); | |
93 | ||
d6c69ee9 YD |
94 | #ifndef MMU_DEBUG |
95 | #define ASSERT(x) do { } while (0) | |
96 | #else | |
6aa8b732 AK |
97 | #define ASSERT(x) \ |
98 | if (!(x)) { \ | |
99 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ | |
100 | __FILE__, __LINE__, #x); \ | |
101 | } | |
d6c69ee9 | 102 | #endif |
6aa8b732 | 103 | |
957ed9ef XG |
104 | #define PTE_PREFETCH_NUM 8 |
105 | ||
6aa8b732 AK |
106 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
107 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 | |
108 | ||
6aa8b732 AK |
109 | #define PT64_LEVEL_BITS 9 |
110 | ||
111 | #define PT64_LEVEL_SHIFT(level) \ | |
d77c26fc | 112 | (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS) |
6aa8b732 | 113 | |
6aa8b732 AK |
114 | #define PT64_INDEX(address, level)\ |
115 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) | |
116 | ||
117 | ||
118 | #define PT32_LEVEL_BITS 10 | |
119 | ||
120 | #define PT32_LEVEL_SHIFT(level) \ | |
d77c26fc | 121 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
6aa8b732 | 122 | |
e04da980 JR |
123 | #define PT32_LVL_OFFSET_MASK(level) \ |
124 | (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
125 | * PT32_LEVEL_BITS))) - 1)) | |
6aa8b732 AK |
126 | |
127 | #define PT32_INDEX(address, level)\ | |
128 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) | |
129 | ||
130 | ||
27aba766 | 131 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
6aa8b732 AK |
132 | #define PT64_DIR_BASE_ADDR_MASK \ |
133 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) | |
e04da980 JR |
134 | #define PT64_LVL_ADDR_MASK(level) \ |
135 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
136 | * PT64_LEVEL_BITS))) - 1)) | |
137 | #define PT64_LVL_OFFSET_MASK(level) \ | |
138 | (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
139 | * PT64_LEVEL_BITS))) - 1)) | |
6aa8b732 AK |
140 | |
141 | #define PT32_BASE_ADDR_MASK PAGE_MASK | |
142 | #define PT32_DIR_BASE_ADDR_MASK \ | |
143 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) | |
e04da980 JR |
144 | #define PT32_LVL_ADDR_MASK(level) \ |
145 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ | |
146 | * PT32_LEVEL_BITS))) - 1)) | |
6aa8b732 | 147 | |
79539cec AK |
148 | #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \ |
149 | | PT64_NX_MASK) | |
6aa8b732 | 150 | |
cd4a4e53 AK |
151 | #define RMAP_EXT 4 |
152 | ||
fe135d2c AK |
153 | #define ACC_EXEC_MASK 1 |
154 | #define ACC_WRITE_MASK PT_WRITABLE_MASK | |
155 | #define ACC_USER_MASK PT_USER_MASK | |
156 | #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) | |
157 | ||
90bb6fc5 AK |
158 | #include <trace/events/kvm.h> |
159 | ||
07420171 AK |
160 | #define CREATE_TRACE_POINTS |
161 | #include "mmutrace.h" | |
162 | ||
1403283a IE |
163 | #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) |
164 | ||
135f8c2b AK |
165 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
166 | ||
cd4a4e53 | 167 | struct kvm_rmap_desc { |
d555c333 | 168 | u64 *sptes[RMAP_EXT]; |
cd4a4e53 AK |
169 | struct kvm_rmap_desc *more; |
170 | }; | |
171 | ||
2d11123a AK |
172 | struct kvm_shadow_walk_iterator { |
173 | u64 addr; | |
174 | hpa_t shadow_addr; | |
175 | int level; | |
176 | u64 *sptep; | |
177 | unsigned index; | |
178 | }; | |
179 | ||
180 | #define for_each_shadow_entry(_vcpu, _addr, _walker) \ | |
181 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ | |
182 | shadow_walk_okay(&(_walker)); \ | |
183 | shadow_walk_next(&(_walker))) | |
184 | ||
1047df1f | 185 | typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte); |
ad8cfbe3 | 186 | |
b5a33a75 AK |
187 | static struct kmem_cache *pte_chain_cache; |
188 | static struct kmem_cache *rmap_desc_cache; | |
d3d25b04 | 189 | static struct kmem_cache *mmu_page_header_cache; |
45221ab6 | 190 | static struct percpu_counter kvm_total_used_mmu_pages; |
b5a33a75 | 191 | |
c7addb90 AK |
192 | static u64 __read_mostly shadow_trap_nonpresent_pte; |
193 | static u64 __read_mostly shadow_notrap_nonpresent_pte; | |
7b52345e SY |
194 | static u64 __read_mostly shadow_nx_mask; |
195 | static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ | |
196 | static u64 __read_mostly shadow_user_mask; | |
197 | static u64 __read_mostly shadow_accessed_mask; | |
198 | static u64 __read_mostly shadow_dirty_mask; | |
c7addb90 | 199 | |
82725b20 DE |
200 | static inline u64 rsvd_bits(int s, int e) |
201 | { | |
202 | return ((1ULL << (e - s + 1)) - 1) << s; | |
203 | } | |
204 | ||
c7addb90 AK |
205 | void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte) |
206 | { | |
207 | shadow_trap_nonpresent_pte = trap_pte; | |
208 | shadow_notrap_nonpresent_pte = notrap_pte; | |
209 | } | |
210 | EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes); | |
211 | ||
7b52345e | 212 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, |
4b12f0de | 213 | u64 dirty_mask, u64 nx_mask, u64 x_mask) |
7b52345e SY |
214 | { |
215 | shadow_user_mask = user_mask; | |
216 | shadow_accessed_mask = accessed_mask; | |
217 | shadow_dirty_mask = dirty_mask; | |
218 | shadow_nx_mask = nx_mask; | |
219 | shadow_x_mask = x_mask; | |
220 | } | |
221 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); | |
222 | ||
3dbe1415 | 223 | static bool is_write_protection(struct kvm_vcpu *vcpu) |
6aa8b732 | 224 | { |
4d4ec087 | 225 | return kvm_read_cr0_bits(vcpu, X86_CR0_WP); |
6aa8b732 AK |
226 | } |
227 | ||
228 | static int is_cpuid_PSE36(void) | |
229 | { | |
230 | return 1; | |
231 | } | |
232 | ||
73b1087e AK |
233 | static int is_nx(struct kvm_vcpu *vcpu) |
234 | { | |
f6801dff | 235 | return vcpu->arch.efer & EFER_NX; |
73b1087e AK |
236 | } |
237 | ||
c7addb90 AK |
238 | static int is_shadow_present_pte(u64 pte) |
239 | { | |
c7addb90 AK |
240 | return pte != shadow_trap_nonpresent_pte |
241 | && pte != shadow_notrap_nonpresent_pte; | |
242 | } | |
243 | ||
05da4558 MT |
244 | static int is_large_pte(u64 pte) |
245 | { | |
246 | return pte & PT_PAGE_SIZE_MASK; | |
247 | } | |
248 | ||
8dae4445 | 249 | static int is_writable_pte(unsigned long pte) |
6aa8b732 AK |
250 | { |
251 | return pte & PT_WRITABLE_MASK; | |
252 | } | |
253 | ||
43a3795a | 254 | static int is_dirty_gpte(unsigned long pte) |
e3c5e7ec | 255 | { |
439e218a | 256 | return pte & PT_DIRTY_MASK; |
e3c5e7ec AK |
257 | } |
258 | ||
43a3795a | 259 | static int is_rmap_spte(u64 pte) |
cd4a4e53 | 260 | { |
4b1a80fa | 261 | return is_shadow_present_pte(pte); |
cd4a4e53 AK |
262 | } |
263 | ||
776e6633 MT |
264 | static int is_last_spte(u64 pte, int level) |
265 | { | |
266 | if (level == PT_PAGE_TABLE_LEVEL) | |
267 | return 1; | |
852e3c19 | 268 | if (is_large_pte(pte)) |
776e6633 MT |
269 | return 1; |
270 | return 0; | |
271 | } | |
272 | ||
35149e21 | 273 | static pfn_t spte_to_pfn(u64 pte) |
0b49ea86 | 274 | { |
35149e21 | 275 | return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
0b49ea86 AK |
276 | } |
277 | ||
da928521 AK |
278 | static gfn_t pse36_gfn_delta(u32 gpte) |
279 | { | |
280 | int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT; | |
281 | ||
282 | return (gpte & PT32_DIR_PSE36_MASK) << shift; | |
283 | } | |
284 | ||
d555c333 | 285 | static void __set_spte(u64 *sptep, u64 spte) |
e663ee64 | 286 | { |
7645e432 | 287 | set_64bit(sptep, spte); |
e663ee64 AK |
288 | } |
289 | ||
a9221dd5 AK |
290 | static u64 __xchg_spte(u64 *sptep, u64 new_spte) |
291 | { | |
292 | #ifdef CONFIG_X86_64 | |
293 | return xchg(sptep, new_spte); | |
294 | #else | |
295 | u64 old_spte; | |
296 | ||
297 | do { | |
298 | old_spte = *sptep; | |
299 | } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte); | |
300 | ||
301 | return old_spte; | |
302 | #endif | |
303 | } | |
304 | ||
8672b721 XG |
305 | static bool spte_has_volatile_bits(u64 spte) |
306 | { | |
307 | if (!shadow_accessed_mask) | |
308 | return false; | |
309 | ||
310 | if (!is_shadow_present_pte(spte)) | |
311 | return false; | |
312 | ||
4132779b XG |
313 | if ((spte & shadow_accessed_mask) && |
314 | (!is_writable_pte(spte) || (spte & shadow_dirty_mask))) | |
8672b721 XG |
315 | return false; |
316 | ||
317 | return true; | |
318 | } | |
319 | ||
4132779b XG |
320 | static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask) |
321 | { | |
322 | return (old_spte & bit_mask) && !(new_spte & bit_mask); | |
323 | } | |
324 | ||
b79b93f9 AK |
325 | static void update_spte(u64 *sptep, u64 new_spte) |
326 | { | |
4132779b XG |
327 | u64 mask, old_spte = *sptep; |
328 | ||
329 | WARN_ON(!is_rmap_spte(new_spte)); | |
b79b93f9 | 330 | |
4132779b XG |
331 | new_spte |= old_spte & shadow_dirty_mask; |
332 | ||
333 | mask = shadow_accessed_mask; | |
334 | if (is_writable_pte(old_spte)) | |
335 | mask |= shadow_dirty_mask; | |
336 | ||
337 | if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask) | |
b79b93f9 | 338 | __set_spte(sptep, new_spte); |
4132779b | 339 | else |
b79b93f9 | 340 | old_spte = __xchg_spte(sptep, new_spte); |
4132779b XG |
341 | |
342 | if (!shadow_accessed_mask) | |
343 | return; | |
344 | ||
345 | if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask)) | |
346 | kvm_set_pfn_accessed(spte_to_pfn(old_spte)); | |
347 | if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask)) | |
348 | kvm_set_pfn_dirty(spte_to_pfn(old_spte)); | |
b79b93f9 AK |
349 | } |
350 | ||
e2dec939 | 351 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
2e3e5882 | 352 | struct kmem_cache *base_cache, int min) |
714b93da AK |
353 | { |
354 | void *obj; | |
355 | ||
356 | if (cache->nobjs >= min) | |
e2dec939 | 357 | return 0; |
714b93da | 358 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
2e3e5882 | 359 | obj = kmem_cache_zalloc(base_cache, GFP_KERNEL); |
714b93da | 360 | if (!obj) |
e2dec939 | 361 | return -ENOMEM; |
714b93da AK |
362 | cache->objects[cache->nobjs++] = obj; |
363 | } | |
e2dec939 | 364 | return 0; |
714b93da AK |
365 | } |
366 | ||
e8ad9a70 XG |
367 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc, |
368 | struct kmem_cache *cache) | |
714b93da AK |
369 | { |
370 | while (mc->nobjs) | |
e8ad9a70 | 371 | kmem_cache_free(cache, mc->objects[--mc->nobjs]); |
714b93da AK |
372 | } |
373 | ||
c1158e63 | 374 | static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache, |
2e3e5882 | 375 | int min) |
c1158e63 | 376 | { |
842f22ed | 377 | void *page; |
c1158e63 AK |
378 | |
379 | if (cache->nobjs >= min) | |
380 | return 0; | |
381 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { | |
842f22ed | 382 | page = (void *)__get_free_page(GFP_KERNEL); |
c1158e63 AK |
383 | if (!page) |
384 | return -ENOMEM; | |
842f22ed | 385 | cache->objects[cache->nobjs++] = page; |
c1158e63 AK |
386 | } |
387 | return 0; | |
388 | } | |
389 | ||
390 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) | |
391 | { | |
392 | while (mc->nobjs) | |
c4d198d5 | 393 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
c1158e63 AK |
394 | } |
395 | ||
2e3e5882 | 396 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
714b93da | 397 | { |
e2dec939 AK |
398 | int r; |
399 | ||
ad312c7c | 400 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache, |
2e3e5882 | 401 | pte_chain_cache, 4); |
e2dec939 AK |
402 | if (r) |
403 | goto out; | |
ad312c7c | 404 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, |
957ed9ef | 405 | rmap_desc_cache, 4 + PTE_PREFETCH_NUM); |
d3d25b04 AK |
406 | if (r) |
407 | goto out; | |
ad312c7c | 408 | r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8); |
d3d25b04 AK |
409 | if (r) |
410 | goto out; | |
ad312c7c | 411 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
2e3e5882 | 412 | mmu_page_header_cache, 4); |
e2dec939 AK |
413 | out: |
414 | return r; | |
714b93da AK |
415 | } |
416 | ||
417 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) | |
418 | { | |
e8ad9a70 XG |
419 | mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache); |
420 | mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache); | |
ad312c7c | 421 | mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache); |
e8ad9a70 XG |
422 | mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache, |
423 | mmu_page_header_cache); | |
714b93da AK |
424 | } |
425 | ||
426 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc, | |
427 | size_t size) | |
428 | { | |
429 | void *p; | |
430 | ||
431 | BUG_ON(!mc->nobjs); | |
432 | p = mc->objects[--mc->nobjs]; | |
714b93da AK |
433 | return p; |
434 | } | |
435 | ||
714b93da AK |
436 | static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu) |
437 | { | |
ad312c7c | 438 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache, |
714b93da AK |
439 | sizeof(struct kvm_pte_chain)); |
440 | } | |
441 | ||
90cb0529 | 442 | static void mmu_free_pte_chain(struct kvm_pte_chain *pc) |
714b93da | 443 | { |
e8ad9a70 | 444 | kmem_cache_free(pte_chain_cache, pc); |
714b93da AK |
445 | } |
446 | ||
447 | static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu) | |
448 | { | |
ad312c7c | 449 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache, |
714b93da AK |
450 | sizeof(struct kvm_rmap_desc)); |
451 | } | |
452 | ||
90cb0529 | 453 | static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd) |
714b93da | 454 | { |
e8ad9a70 | 455 | kmem_cache_free(rmap_desc_cache, rd); |
714b93da AK |
456 | } |
457 | ||
2032a93d LJ |
458 | static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index) |
459 | { | |
460 | if (!sp->role.direct) | |
461 | return sp->gfns[index]; | |
462 | ||
463 | return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS)); | |
464 | } | |
465 | ||
466 | static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn) | |
467 | { | |
468 | if (sp->role.direct) | |
469 | BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index)); | |
470 | else | |
471 | sp->gfns[index] = gfn; | |
472 | } | |
473 | ||
05da4558 | 474 | /* |
d4dbf470 TY |
475 | * Return the pointer to the large page information for a given gfn, |
476 | * handling slots that are not large page aligned. | |
05da4558 | 477 | */ |
d4dbf470 TY |
478 | static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn, |
479 | struct kvm_memory_slot *slot, | |
480 | int level) | |
05da4558 MT |
481 | { |
482 | unsigned long idx; | |
483 | ||
82855413 JR |
484 | idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) - |
485 | (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level)); | |
d4dbf470 | 486 | return &slot->lpage_info[level - 2][idx]; |
05da4558 MT |
487 | } |
488 | ||
489 | static void account_shadowed(struct kvm *kvm, gfn_t gfn) | |
490 | { | |
d25797b2 | 491 | struct kvm_memory_slot *slot; |
d4dbf470 | 492 | struct kvm_lpage_info *linfo; |
d25797b2 | 493 | int i; |
05da4558 | 494 | |
a1f4d395 | 495 | slot = gfn_to_memslot(kvm, gfn); |
d25797b2 JR |
496 | for (i = PT_DIRECTORY_LEVEL; |
497 | i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) { | |
d4dbf470 TY |
498 | linfo = lpage_info_slot(gfn, slot, i); |
499 | linfo->write_count += 1; | |
d25797b2 | 500 | } |
332b207d | 501 | kvm->arch.indirect_shadow_pages++; |
05da4558 MT |
502 | } |
503 | ||
504 | static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn) | |
505 | { | |
d25797b2 | 506 | struct kvm_memory_slot *slot; |
d4dbf470 | 507 | struct kvm_lpage_info *linfo; |
d25797b2 | 508 | int i; |
05da4558 | 509 | |
a1f4d395 | 510 | slot = gfn_to_memslot(kvm, gfn); |
d25797b2 JR |
511 | for (i = PT_DIRECTORY_LEVEL; |
512 | i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) { | |
d4dbf470 TY |
513 | linfo = lpage_info_slot(gfn, slot, i); |
514 | linfo->write_count -= 1; | |
515 | WARN_ON(linfo->write_count < 0); | |
d25797b2 | 516 | } |
332b207d | 517 | kvm->arch.indirect_shadow_pages--; |
05da4558 MT |
518 | } |
519 | ||
d25797b2 JR |
520 | static int has_wrprotected_page(struct kvm *kvm, |
521 | gfn_t gfn, | |
522 | int level) | |
05da4558 | 523 | { |
2843099f | 524 | struct kvm_memory_slot *slot; |
d4dbf470 | 525 | struct kvm_lpage_info *linfo; |
05da4558 | 526 | |
a1f4d395 | 527 | slot = gfn_to_memslot(kvm, gfn); |
05da4558 | 528 | if (slot) { |
d4dbf470 TY |
529 | linfo = lpage_info_slot(gfn, slot, level); |
530 | return linfo->write_count; | |
05da4558 MT |
531 | } |
532 | ||
533 | return 1; | |
534 | } | |
535 | ||
d25797b2 | 536 | static int host_mapping_level(struct kvm *kvm, gfn_t gfn) |
05da4558 | 537 | { |
8f0b1ab6 | 538 | unsigned long page_size; |
d25797b2 | 539 | int i, ret = 0; |
05da4558 | 540 | |
8f0b1ab6 | 541 | page_size = kvm_host_page_size(kvm, gfn); |
05da4558 | 542 | |
d25797b2 JR |
543 | for (i = PT_PAGE_TABLE_LEVEL; |
544 | i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) { | |
545 | if (page_size >= KVM_HPAGE_SIZE(i)) | |
546 | ret = i; | |
547 | else | |
548 | break; | |
549 | } | |
550 | ||
4c2155ce | 551 | return ret; |
05da4558 MT |
552 | } |
553 | ||
5d163b1c XG |
554 | static struct kvm_memory_slot * |
555 | gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn, | |
556 | bool no_dirty_log) | |
05da4558 MT |
557 | { |
558 | struct kvm_memory_slot *slot; | |
5d163b1c XG |
559 | |
560 | slot = gfn_to_memslot(vcpu->kvm, gfn); | |
561 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID || | |
562 | (no_dirty_log && slot->dirty_bitmap)) | |
563 | slot = NULL; | |
564 | ||
565 | return slot; | |
566 | } | |
567 | ||
568 | static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn) | |
569 | { | |
a0a8eaba | 570 | return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true); |
936a5fe6 AA |
571 | } |
572 | ||
573 | static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn) | |
574 | { | |
575 | int host_level, level, max_level; | |
05da4558 | 576 | |
d25797b2 JR |
577 | host_level = host_mapping_level(vcpu->kvm, large_gfn); |
578 | ||
579 | if (host_level == PT_PAGE_TABLE_LEVEL) | |
580 | return host_level; | |
581 | ||
878403b7 SY |
582 | max_level = kvm_x86_ops->get_lpage_level() < host_level ? |
583 | kvm_x86_ops->get_lpage_level() : host_level; | |
584 | ||
585 | for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level) | |
d25797b2 JR |
586 | if (has_wrprotected_page(vcpu->kvm, large_gfn, level)) |
587 | break; | |
d25797b2 JR |
588 | |
589 | return level - 1; | |
05da4558 MT |
590 | } |
591 | ||
290fc38d IE |
592 | /* |
593 | * Take gfn and return the reverse mapping to it. | |
290fc38d IE |
594 | */ |
595 | ||
44ad9944 | 596 | static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level) |
290fc38d IE |
597 | { |
598 | struct kvm_memory_slot *slot; | |
d4dbf470 | 599 | struct kvm_lpage_info *linfo; |
290fc38d IE |
600 | |
601 | slot = gfn_to_memslot(kvm, gfn); | |
44ad9944 | 602 | if (likely(level == PT_PAGE_TABLE_LEVEL)) |
05da4558 MT |
603 | return &slot->rmap[gfn - slot->base_gfn]; |
604 | ||
d4dbf470 | 605 | linfo = lpage_info_slot(gfn, slot, level); |
05da4558 | 606 | |
d4dbf470 | 607 | return &linfo->rmap_pde; |
290fc38d IE |
608 | } |
609 | ||
cd4a4e53 AK |
610 | /* |
611 | * Reverse mapping data structures: | |
612 | * | |
290fc38d IE |
613 | * If rmapp bit zero is zero, then rmapp point to the shadw page table entry |
614 | * that points to page_address(page). | |
cd4a4e53 | 615 | * |
290fc38d IE |
616 | * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc |
617 | * containing more mappings. | |
53a27b39 MT |
618 | * |
619 | * Returns the number of rmap entries before the spte was added or zero if | |
620 | * the spte was not added. | |
621 | * | |
cd4a4e53 | 622 | */ |
44ad9944 | 623 | static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
cd4a4e53 | 624 | { |
4db35314 | 625 | struct kvm_mmu_page *sp; |
cd4a4e53 | 626 | struct kvm_rmap_desc *desc; |
290fc38d | 627 | unsigned long *rmapp; |
53a27b39 | 628 | int i, count = 0; |
cd4a4e53 | 629 | |
43a3795a | 630 | if (!is_rmap_spte(*spte)) |
53a27b39 | 631 | return count; |
4db35314 | 632 | sp = page_header(__pa(spte)); |
2032a93d | 633 | kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn); |
44ad9944 | 634 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level); |
290fc38d | 635 | if (!*rmapp) { |
cd4a4e53 | 636 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); |
290fc38d IE |
637 | *rmapp = (unsigned long)spte; |
638 | } else if (!(*rmapp & 1)) { | |
cd4a4e53 | 639 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); |
714b93da | 640 | desc = mmu_alloc_rmap_desc(vcpu); |
d555c333 AK |
641 | desc->sptes[0] = (u64 *)*rmapp; |
642 | desc->sptes[1] = spte; | |
290fc38d | 643 | *rmapp = (unsigned long)desc | 1; |
cb16a7b3 | 644 | ++count; |
cd4a4e53 AK |
645 | } else { |
646 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); | |
290fc38d | 647 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
d555c333 | 648 | while (desc->sptes[RMAP_EXT-1] && desc->more) { |
cd4a4e53 | 649 | desc = desc->more; |
53a27b39 MT |
650 | count += RMAP_EXT; |
651 | } | |
d555c333 | 652 | if (desc->sptes[RMAP_EXT-1]) { |
714b93da | 653 | desc->more = mmu_alloc_rmap_desc(vcpu); |
cd4a4e53 AK |
654 | desc = desc->more; |
655 | } | |
d555c333 | 656 | for (i = 0; desc->sptes[i]; ++i) |
cb16a7b3 | 657 | ++count; |
d555c333 | 658 | desc->sptes[i] = spte; |
cd4a4e53 | 659 | } |
53a27b39 | 660 | return count; |
cd4a4e53 AK |
661 | } |
662 | ||
290fc38d | 663 | static void rmap_desc_remove_entry(unsigned long *rmapp, |
cd4a4e53 AK |
664 | struct kvm_rmap_desc *desc, |
665 | int i, | |
666 | struct kvm_rmap_desc *prev_desc) | |
667 | { | |
668 | int j; | |
669 | ||
d555c333 | 670 | for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j) |
cd4a4e53 | 671 | ; |
d555c333 AK |
672 | desc->sptes[i] = desc->sptes[j]; |
673 | desc->sptes[j] = NULL; | |
cd4a4e53 AK |
674 | if (j != 0) |
675 | return; | |
676 | if (!prev_desc && !desc->more) | |
d555c333 | 677 | *rmapp = (unsigned long)desc->sptes[0]; |
cd4a4e53 AK |
678 | else |
679 | if (prev_desc) | |
680 | prev_desc->more = desc->more; | |
681 | else | |
290fc38d | 682 | *rmapp = (unsigned long)desc->more | 1; |
90cb0529 | 683 | mmu_free_rmap_desc(desc); |
cd4a4e53 AK |
684 | } |
685 | ||
290fc38d | 686 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
cd4a4e53 | 687 | { |
cd4a4e53 AK |
688 | struct kvm_rmap_desc *desc; |
689 | struct kvm_rmap_desc *prev_desc; | |
4db35314 | 690 | struct kvm_mmu_page *sp; |
2032a93d | 691 | gfn_t gfn; |
290fc38d | 692 | unsigned long *rmapp; |
cd4a4e53 AK |
693 | int i; |
694 | ||
4db35314 | 695 | sp = page_header(__pa(spte)); |
2032a93d LJ |
696 | gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt); |
697 | rmapp = gfn_to_rmap(kvm, gfn, sp->role.level); | |
290fc38d | 698 | if (!*rmapp) { |
19ada5c4 | 699 | printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte); |
cd4a4e53 | 700 | BUG(); |
290fc38d | 701 | } else if (!(*rmapp & 1)) { |
19ada5c4 | 702 | rmap_printk("rmap_remove: %p 1->0\n", spte); |
290fc38d | 703 | if ((u64 *)*rmapp != spte) { |
19ada5c4 | 704 | printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte); |
cd4a4e53 AK |
705 | BUG(); |
706 | } | |
290fc38d | 707 | *rmapp = 0; |
cd4a4e53 | 708 | } else { |
19ada5c4 | 709 | rmap_printk("rmap_remove: %p many->many\n", spte); |
290fc38d | 710 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
cd4a4e53 AK |
711 | prev_desc = NULL; |
712 | while (desc) { | |
d555c333 AK |
713 | for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) |
714 | if (desc->sptes[i] == spte) { | |
290fc38d | 715 | rmap_desc_remove_entry(rmapp, |
714b93da | 716 | desc, i, |
cd4a4e53 AK |
717 | prev_desc); |
718 | return; | |
719 | } | |
720 | prev_desc = desc; | |
721 | desc = desc->more; | |
722 | } | |
19ada5c4 | 723 | pr_err("rmap_remove: %p many->many\n", spte); |
cd4a4e53 AK |
724 | BUG(); |
725 | } | |
726 | } | |
727 | ||
eb45fda4 | 728 | static int set_spte_track_bits(u64 *sptep, u64 new_spte) |
be38d276 | 729 | { |
ce061867 | 730 | pfn_t pfn; |
9a3aad70 XG |
731 | u64 old_spte = *sptep; |
732 | ||
8672b721 | 733 | if (!spte_has_volatile_bits(old_spte)) |
9a3aad70 | 734 | __set_spte(sptep, new_spte); |
8672b721 | 735 | else |
9a3aad70 | 736 | old_spte = __xchg_spte(sptep, new_spte); |
ce061867 | 737 | |
a9221dd5 | 738 | if (!is_rmap_spte(old_spte)) |
eb45fda4 | 739 | return 0; |
8672b721 | 740 | |
a9221dd5 | 741 | pfn = spte_to_pfn(old_spte); |
daa3db69 | 742 | if (!shadow_accessed_mask || old_spte & shadow_accessed_mask) |
ce061867 | 743 | kvm_set_pfn_accessed(pfn); |
4132779b | 744 | if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask)) |
ce061867 | 745 | kvm_set_pfn_dirty(pfn); |
eb45fda4 | 746 | return 1; |
e4b502ea XG |
747 | } |
748 | ||
749 | static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte) | |
750 | { | |
eb45fda4 MT |
751 | if (set_spte_track_bits(sptep, new_spte)) |
752 | rmap_remove(kvm, sptep); | |
be38d276 AK |
753 | } |
754 | ||
98348e95 | 755 | static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte) |
374cbac0 | 756 | { |
374cbac0 | 757 | struct kvm_rmap_desc *desc; |
98348e95 IE |
758 | u64 *prev_spte; |
759 | int i; | |
760 | ||
761 | if (!*rmapp) | |
762 | return NULL; | |
763 | else if (!(*rmapp & 1)) { | |
764 | if (!spte) | |
765 | return (u64 *)*rmapp; | |
766 | return NULL; | |
767 | } | |
768 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); | |
98348e95 IE |
769 | prev_spte = NULL; |
770 | while (desc) { | |
d555c333 | 771 | for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) { |
98348e95 | 772 | if (prev_spte == spte) |
d555c333 AK |
773 | return desc->sptes[i]; |
774 | prev_spte = desc->sptes[i]; | |
98348e95 IE |
775 | } |
776 | desc = desc->more; | |
777 | } | |
778 | return NULL; | |
779 | } | |
780 | ||
b1a36821 | 781 | static int rmap_write_protect(struct kvm *kvm, u64 gfn) |
98348e95 | 782 | { |
290fc38d | 783 | unsigned long *rmapp; |
374cbac0 | 784 | u64 *spte; |
44ad9944 | 785 | int i, write_protected = 0; |
374cbac0 | 786 | |
44ad9944 | 787 | rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL); |
374cbac0 | 788 | |
98348e95 IE |
789 | spte = rmap_next(kvm, rmapp, NULL); |
790 | while (spte) { | |
374cbac0 | 791 | BUG_ON(!spte); |
374cbac0 | 792 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
374cbac0 | 793 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
8dae4445 | 794 | if (is_writable_pte(*spte)) { |
b79b93f9 | 795 | update_spte(spte, *spte & ~PT_WRITABLE_MASK); |
caa5b8a5 ED |
796 | write_protected = 1; |
797 | } | |
9647c14c | 798 | spte = rmap_next(kvm, rmapp, spte); |
374cbac0 | 799 | } |
855149aa | 800 | |
05da4558 | 801 | /* check for huge page mappings */ |
44ad9944 JR |
802 | for (i = PT_DIRECTORY_LEVEL; |
803 | i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) { | |
804 | rmapp = gfn_to_rmap(kvm, gfn, i); | |
805 | spte = rmap_next(kvm, rmapp, NULL); | |
806 | while (spte) { | |
807 | BUG_ON(!spte); | |
808 | BUG_ON(!(*spte & PT_PRESENT_MASK)); | |
809 | BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)); | |
810 | pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn); | |
8dae4445 | 811 | if (is_writable_pte(*spte)) { |
be38d276 AK |
812 | drop_spte(kvm, spte, |
813 | shadow_trap_nonpresent_pte); | |
44ad9944 | 814 | --kvm->stat.lpages; |
44ad9944 JR |
815 | spte = NULL; |
816 | write_protected = 1; | |
817 | } | |
818 | spte = rmap_next(kvm, rmapp, spte); | |
05da4558 | 819 | } |
05da4558 MT |
820 | } |
821 | ||
b1a36821 | 822 | return write_protected; |
374cbac0 AK |
823 | } |
824 | ||
8a8365c5 FD |
825 | static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, |
826 | unsigned long data) | |
e930bffe AA |
827 | { |
828 | u64 *spte; | |
829 | int need_tlb_flush = 0; | |
830 | ||
831 | while ((spte = rmap_next(kvm, rmapp, NULL))) { | |
832 | BUG_ON(!(*spte & PT_PRESENT_MASK)); | |
833 | rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte); | |
be38d276 | 834 | drop_spte(kvm, spte, shadow_trap_nonpresent_pte); |
e930bffe AA |
835 | need_tlb_flush = 1; |
836 | } | |
837 | return need_tlb_flush; | |
838 | } | |
839 | ||
8a8365c5 FD |
840 | static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, |
841 | unsigned long data) | |
3da0dd43 IE |
842 | { |
843 | int need_flush = 0; | |
e4b502ea | 844 | u64 *spte, new_spte; |
3da0dd43 IE |
845 | pte_t *ptep = (pte_t *)data; |
846 | pfn_t new_pfn; | |
847 | ||
848 | WARN_ON(pte_huge(*ptep)); | |
849 | new_pfn = pte_pfn(*ptep); | |
850 | spte = rmap_next(kvm, rmapp, NULL); | |
851 | while (spte) { | |
852 | BUG_ON(!is_shadow_present_pte(*spte)); | |
853 | rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte); | |
854 | need_flush = 1; | |
855 | if (pte_write(*ptep)) { | |
be38d276 | 856 | drop_spte(kvm, spte, shadow_trap_nonpresent_pte); |
3da0dd43 IE |
857 | spte = rmap_next(kvm, rmapp, NULL); |
858 | } else { | |
859 | new_spte = *spte &~ (PT64_BASE_ADDR_MASK); | |
860 | new_spte |= (u64)new_pfn << PAGE_SHIFT; | |
861 | ||
862 | new_spte &= ~PT_WRITABLE_MASK; | |
863 | new_spte &= ~SPTE_HOST_WRITEABLE; | |
b79b93f9 | 864 | new_spte &= ~shadow_accessed_mask; |
e4b502ea | 865 | set_spte_track_bits(spte, new_spte); |
3da0dd43 IE |
866 | spte = rmap_next(kvm, rmapp, spte); |
867 | } | |
868 | } | |
869 | if (need_flush) | |
870 | kvm_flush_remote_tlbs(kvm); | |
871 | ||
872 | return 0; | |
873 | } | |
874 | ||
8a8365c5 FD |
875 | static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, |
876 | unsigned long data, | |
3da0dd43 | 877 | int (*handler)(struct kvm *kvm, unsigned long *rmapp, |
8a8365c5 | 878 | unsigned long data)) |
e930bffe | 879 | { |
852e3c19 | 880 | int i, j; |
90bb6fc5 | 881 | int ret; |
e930bffe | 882 | int retval = 0; |
bc6678a3 MT |
883 | struct kvm_memslots *slots; |
884 | ||
90d83dc3 | 885 | slots = kvm_memslots(kvm); |
e930bffe | 886 | |
46a26bf5 MT |
887 | for (i = 0; i < slots->nmemslots; i++) { |
888 | struct kvm_memory_slot *memslot = &slots->memslots[i]; | |
e930bffe AA |
889 | unsigned long start = memslot->userspace_addr; |
890 | unsigned long end; | |
891 | ||
e930bffe AA |
892 | end = start + (memslot->npages << PAGE_SHIFT); |
893 | if (hva >= start && hva < end) { | |
894 | gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT; | |
d4dbf470 | 895 | gfn_t gfn = memslot->base_gfn + gfn_offset; |
852e3c19 | 896 | |
90bb6fc5 | 897 | ret = handler(kvm, &memslot->rmap[gfn_offset], data); |
852e3c19 JR |
898 | |
899 | for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) { | |
d4dbf470 TY |
900 | struct kvm_lpage_info *linfo; |
901 | ||
902 | linfo = lpage_info_slot(gfn, memslot, | |
903 | PT_DIRECTORY_LEVEL + j); | |
904 | ret |= handler(kvm, &linfo->rmap_pde, data); | |
852e3c19 | 905 | } |
90bb6fc5 AK |
906 | trace_kvm_age_page(hva, memslot, ret); |
907 | retval |= ret; | |
e930bffe AA |
908 | } |
909 | } | |
910 | ||
911 | return retval; | |
912 | } | |
913 | ||
914 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) | |
915 | { | |
3da0dd43 IE |
916 | return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp); |
917 | } | |
918 | ||
919 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) | |
920 | { | |
8a8365c5 | 921 | kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp); |
e930bffe AA |
922 | } |
923 | ||
8a8365c5 FD |
924 | static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, |
925 | unsigned long data) | |
e930bffe AA |
926 | { |
927 | u64 *spte; | |
928 | int young = 0; | |
929 | ||
6316e1c8 RR |
930 | /* |
931 | * Emulate the accessed bit for EPT, by checking if this page has | |
932 | * an EPT mapping, and clearing it if it does. On the next access, | |
933 | * a new EPT mapping will be established. | |
934 | * This has some overhead, but not as much as the cost of swapping | |
935 | * out actively used pages or breaking up actively used hugepages. | |
936 | */ | |
534e38b4 | 937 | if (!shadow_accessed_mask) |
6316e1c8 | 938 | return kvm_unmap_rmapp(kvm, rmapp, data); |
534e38b4 | 939 | |
e930bffe AA |
940 | spte = rmap_next(kvm, rmapp, NULL); |
941 | while (spte) { | |
942 | int _young; | |
943 | u64 _spte = *spte; | |
944 | BUG_ON(!(_spte & PT_PRESENT_MASK)); | |
945 | _young = _spte & PT_ACCESSED_MASK; | |
946 | if (_young) { | |
947 | young = 1; | |
948 | clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); | |
949 | } | |
950 | spte = rmap_next(kvm, rmapp, spte); | |
951 | } | |
952 | return young; | |
953 | } | |
954 | ||
8ee53820 AA |
955 | static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp, |
956 | unsigned long data) | |
957 | { | |
958 | u64 *spte; | |
959 | int young = 0; | |
960 | ||
961 | /* | |
962 | * If there's no access bit in the secondary pte set by the | |
963 | * hardware it's up to gup-fast/gup to set the access bit in | |
964 | * the primary pte or in the page structure. | |
965 | */ | |
966 | if (!shadow_accessed_mask) | |
967 | goto out; | |
968 | ||
969 | spte = rmap_next(kvm, rmapp, NULL); | |
970 | while (spte) { | |
971 | u64 _spte = *spte; | |
972 | BUG_ON(!(_spte & PT_PRESENT_MASK)); | |
973 | young = _spte & PT_ACCESSED_MASK; | |
974 | if (young) { | |
975 | young = 1; | |
976 | break; | |
977 | } | |
978 | spte = rmap_next(kvm, rmapp, spte); | |
979 | } | |
980 | out: | |
981 | return young; | |
982 | } | |
983 | ||
53a27b39 MT |
984 | #define RMAP_RECYCLE_THRESHOLD 1000 |
985 | ||
852e3c19 | 986 | static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
53a27b39 MT |
987 | { |
988 | unsigned long *rmapp; | |
852e3c19 JR |
989 | struct kvm_mmu_page *sp; |
990 | ||
991 | sp = page_header(__pa(spte)); | |
53a27b39 | 992 | |
852e3c19 | 993 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level); |
53a27b39 | 994 | |
3da0dd43 | 995 | kvm_unmap_rmapp(vcpu->kvm, rmapp, 0); |
53a27b39 MT |
996 | kvm_flush_remote_tlbs(vcpu->kvm); |
997 | } | |
998 | ||
e930bffe AA |
999 | int kvm_age_hva(struct kvm *kvm, unsigned long hva) |
1000 | { | |
3da0dd43 | 1001 | return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp); |
e930bffe AA |
1002 | } |
1003 | ||
8ee53820 AA |
1004 | int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) |
1005 | { | |
1006 | return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp); | |
1007 | } | |
1008 | ||
d6c69ee9 | 1009 | #ifdef MMU_DEBUG |
47ad8e68 | 1010 | static int is_empty_shadow_page(u64 *spt) |
6aa8b732 | 1011 | { |
139bdb2d AK |
1012 | u64 *pos; |
1013 | u64 *end; | |
1014 | ||
47ad8e68 | 1015 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
3c915510 | 1016 | if (is_shadow_present_pte(*pos)) { |
b8688d51 | 1017 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
139bdb2d | 1018 | pos, *pos); |
6aa8b732 | 1019 | return 0; |
139bdb2d | 1020 | } |
6aa8b732 AK |
1021 | return 1; |
1022 | } | |
d6c69ee9 | 1023 | #endif |
6aa8b732 | 1024 | |
45221ab6 DH |
1025 | /* |
1026 | * This value is the sum of all of the kvm instances's | |
1027 | * kvm->arch.n_used_mmu_pages values. We need a global, | |
1028 | * aggregate version in order to make the slab shrinker | |
1029 | * faster | |
1030 | */ | |
1031 | static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr) | |
1032 | { | |
1033 | kvm->arch.n_used_mmu_pages += nr; | |
1034 | percpu_counter_add(&kvm_total_used_mmu_pages, nr); | |
1035 | } | |
1036 | ||
4db35314 | 1037 | static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
260746c0 | 1038 | { |
4db35314 | 1039 | ASSERT(is_empty_shadow_page(sp->spt)); |
7775834a | 1040 | hlist_del(&sp->hash_link); |
4db35314 | 1041 | list_del(&sp->link); |
842f22ed | 1042 | free_page((unsigned long)sp->spt); |
2032a93d | 1043 | if (!sp->role.direct) |
842f22ed | 1044 | free_page((unsigned long)sp->gfns); |
e8ad9a70 | 1045 | kmem_cache_free(mmu_page_header_cache, sp); |
45221ab6 | 1046 | kvm_mod_used_mmu_pages(kvm, -1); |
260746c0 AK |
1047 | } |
1048 | ||
cea0f0e7 AK |
1049 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
1050 | { | |
1ae0a13d | 1051 | return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1); |
cea0f0e7 AK |
1052 | } |
1053 | ||
25c0de2c | 1054 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
2032a93d | 1055 | u64 *parent_pte, int direct) |
6aa8b732 | 1056 | { |
4db35314 | 1057 | struct kvm_mmu_page *sp; |
6aa8b732 | 1058 | |
ad312c7c ZX |
1059 | sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp); |
1060 | sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); | |
2032a93d LJ |
1061 | if (!direct) |
1062 | sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, | |
1063 | PAGE_SIZE); | |
4db35314 | 1064 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
f05e70ac | 1065 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
291f26bc | 1066 | bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS); |
4db35314 AK |
1067 | sp->multimapped = 0; |
1068 | sp->parent_pte = parent_pte; | |
45221ab6 | 1069 | kvm_mod_used_mmu_pages(vcpu->kvm, +1); |
4db35314 | 1070 | return sp; |
6aa8b732 AK |
1071 | } |
1072 | ||
714b93da | 1073 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
4db35314 | 1074 | struct kvm_mmu_page *sp, u64 *parent_pte) |
cea0f0e7 AK |
1075 | { |
1076 | struct kvm_pte_chain *pte_chain; | |
1077 | struct hlist_node *node; | |
1078 | int i; | |
1079 | ||
1080 | if (!parent_pte) | |
1081 | return; | |
4db35314 AK |
1082 | if (!sp->multimapped) { |
1083 | u64 *old = sp->parent_pte; | |
cea0f0e7 AK |
1084 | |
1085 | if (!old) { | |
4db35314 | 1086 | sp->parent_pte = parent_pte; |
cea0f0e7 AK |
1087 | return; |
1088 | } | |
4db35314 | 1089 | sp->multimapped = 1; |
714b93da | 1090 | pte_chain = mmu_alloc_pte_chain(vcpu); |
4db35314 AK |
1091 | INIT_HLIST_HEAD(&sp->parent_ptes); |
1092 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); | |
cea0f0e7 AK |
1093 | pte_chain->parent_ptes[0] = old; |
1094 | } | |
4db35314 | 1095 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) { |
cea0f0e7 AK |
1096 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) |
1097 | continue; | |
1098 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) | |
1099 | if (!pte_chain->parent_ptes[i]) { | |
1100 | pte_chain->parent_ptes[i] = parent_pte; | |
1101 | return; | |
1102 | } | |
1103 | } | |
714b93da | 1104 | pte_chain = mmu_alloc_pte_chain(vcpu); |
cea0f0e7 | 1105 | BUG_ON(!pte_chain); |
4db35314 | 1106 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); |
cea0f0e7 AK |
1107 | pte_chain->parent_ptes[0] = parent_pte; |
1108 | } | |
1109 | ||
4db35314 | 1110 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
cea0f0e7 AK |
1111 | u64 *parent_pte) |
1112 | { | |
1113 | struct kvm_pte_chain *pte_chain; | |
1114 | struct hlist_node *node; | |
1115 | int i; | |
1116 | ||
4db35314 AK |
1117 | if (!sp->multimapped) { |
1118 | BUG_ON(sp->parent_pte != parent_pte); | |
1119 | sp->parent_pte = NULL; | |
cea0f0e7 AK |
1120 | return; |
1121 | } | |
4db35314 | 1122 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
cea0f0e7 AK |
1123 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
1124 | if (!pte_chain->parent_ptes[i]) | |
1125 | break; | |
1126 | if (pte_chain->parent_ptes[i] != parent_pte) | |
1127 | continue; | |
697fe2e2 AK |
1128 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
1129 | && pte_chain->parent_ptes[i + 1]) { | |
cea0f0e7 AK |
1130 | pte_chain->parent_ptes[i] |
1131 | = pte_chain->parent_ptes[i + 1]; | |
1132 | ++i; | |
1133 | } | |
1134 | pte_chain->parent_ptes[i] = NULL; | |
697fe2e2 AK |
1135 | if (i == 0) { |
1136 | hlist_del(&pte_chain->link); | |
90cb0529 | 1137 | mmu_free_pte_chain(pte_chain); |
4db35314 AK |
1138 | if (hlist_empty(&sp->parent_ptes)) { |
1139 | sp->multimapped = 0; | |
1140 | sp->parent_pte = NULL; | |
697fe2e2 AK |
1141 | } |
1142 | } | |
cea0f0e7 AK |
1143 | return; |
1144 | } | |
1145 | BUG(); | |
1146 | } | |
1147 | ||
6b18493d | 1148 | static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn) |
ad8cfbe3 MT |
1149 | { |
1150 | struct kvm_pte_chain *pte_chain; | |
1151 | struct hlist_node *node; | |
1152 | struct kvm_mmu_page *parent_sp; | |
1153 | int i; | |
1154 | ||
1155 | if (!sp->multimapped && sp->parent_pte) { | |
1156 | parent_sp = page_header(__pa(sp->parent_pte)); | |
1047df1f | 1157 | fn(parent_sp, sp->parent_pte); |
ad8cfbe3 MT |
1158 | return; |
1159 | } | |
1047df1f | 1160 | |
ad8cfbe3 MT |
1161 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
1162 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { | |
1047df1f XG |
1163 | u64 *spte = pte_chain->parent_ptes[i]; |
1164 | ||
1165 | if (!spte) | |
ad8cfbe3 | 1166 | break; |
1047df1f XG |
1167 | parent_sp = page_header(__pa(spte)); |
1168 | fn(parent_sp, spte); | |
ad8cfbe3 MT |
1169 | } |
1170 | } | |
1171 | ||
1047df1f XG |
1172 | static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte); |
1173 | static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp) | |
0074ff63 | 1174 | { |
1047df1f | 1175 | mmu_parent_walk(sp, mark_unsync); |
0074ff63 MT |
1176 | } |
1177 | ||
1047df1f | 1178 | static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte) |
0074ff63 | 1179 | { |
1047df1f | 1180 | unsigned int index; |
0074ff63 | 1181 | |
1047df1f XG |
1182 | index = spte - sp->spt; |
1183 | if (__test_and_set_bit(index, sp->unsync_child_bitmap)) | |
0074ff63 | 1184 | return; |
1047df1f | 1185 | if (sp->unsync_children++) |
0074ff63 | 1186 | return; |
1047df1f | 1187 | kvm_mmu_mark_parents_unsync(sp); |
0074ff63 MT |
1188 | } |
1189 | ||
d761a501 AK |
1190 | static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu, |
1191 | struct kvm_mmu_page *sp) | |
1192 | { | |
1193 | int i; | |
1194 | ||
1195 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) | |
1196 | sp->spt[i] = shadow_trap_nonpresent_pte; | |
1197 | } | |
1198 | ||
e8bc217a | 1199 | static int nonpaging_sync_page(struct kvm_vcpu *vcpu, |
a4a8e6f7 | 1200 | struct kvm_mmu_page *sp) |
e8bc217a MT |
1201 | { |
1202 | return 1; | |
1203 | } | |
1204 | ||
a7052897 MT |
1205 | static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
1206 | { | |
1207 | } | |
1208 | ||
0f53b5b1 XG |
1209 | static void nonpaging_update_pte(struct kvm_vcpu *vcpu, |
1210 | struct kvm_mmu_page *sp, u64 *spte, | |
7c562522 | 1211 | const void *pte) |
0f53b5b1 XG |
1212 | { |
1213 | WARN_ON(1); | |
1214 | } | |
1215 | ||
60c8aec6 MT |
1216 | #define KVM_PAGE_ARRAY_NR 16 |
1217 | ||
1218 | struct kvm_mmu_pages { | |
1219 | struct mmu_page_and_offset { | |
1220 | struct kvm_mmu_page *sp; | |
1221 | unsigned int idx; | |
1222 | } page[KVM_PAGE_ARRAY_NR]; | |
1223 | unsigned int nr; | |
1224 | }; | |
1225 | ||
0074ff63 MT |
1226 | #define for_each_unsync_children(bitmap, idx) \ |
1227 | for (idx = find_first_bit(bitmap, 512); \ | |
1228 | idx < 512; \ | |
1229 | idx = find_next_bit(bitmap, 512, idx+1)) | |
1230 | ||
cded19f3 HE |
1231 | static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp, |
1232 | int idx) | |
4731d4c7 | 1233 | { |
60c8aec6 | 1234 | int i; |
4731d4c7 | 1235 | |
60c8aec6 MT |
1236 | if (sp->unsync) |
1237 | for (i=0; i < pvec->nr; i++) | |
1238 | if (pvec->page[i].sp == sp) | |
1239 | return 0; | |
1240 | ||
1241 | pvec->page[pvec->nr].sp = sp; | |
1242 | pvec->page[pvec->nr].idx = idx; | |
1243 | pvec->nr++; | |
1244 | return (pvec->nr == KVM_PAGE_ARRAY_NR); | |
1245 | } | |
1246 | ||
1247 | static int __mmu_unsync_walk(struct kvm_mmu_page *sp, | |
1248 | struct kvm_mmu_pages *pvec) | |
1249 | { | |
1250 | int i, ret, nr_unsync_leaf = 0; | |
4731d4c7 | 1251 | |
0074ff63 | 1252 | for_each_unsync_children(sp->unsync_child_bitmap, i) { |
7a8f1a74 | 1253 | struct kvm_mmu_page *child; |
4731d4c7 MT |
1254 | u64 ent = sp->spt[i]; |
1255 | ||
7a8f1a74 XG |
1256 | if (!is_shadow_present_pte(ent) || is_large_pte(ent)) |
1257 | goto clear_child_bitmap; | |
1258 | ||
1259 | child = page_header(ent & PT64_BASE_ADDR_MASK); | |
1260 | ||
1261 | if (child->unsync_children) { | |
1262 | if (mmu_pages_add(pvec, child, i)) | |
1263 | return -ENOSPC; | |
1264 | ||
1265 | ret = __mmu_unsync_walk(child, pvec); | |
1266 | if (!ret) | |
1267 | goto clear_child_bitmap; | |
1268 | else if (ret > 0) | |
1269 | nr_unsync_leaf += ret; | |
1270 | else | |
1271 | return ret; | |
1272 | } else if (child->unsync) { | |
1273 | nr_unsync_leaf++; | |
1274 | if (mmu_pages_add(pvec, child, i)) | |
1275 | return -ENOSPC; | |
1276 | } else | |
1277 | goto clear_child_bitmap; | |
1278 | ||
1279 | continue; | |
1280 | ||
1281 | clear_child_bitmap: | |
1282 | __clear_bit(i, sp->unsync_child_bitmap); | |
1283 | sp->unsync_children--; | |
1284 | WARN_ON((int)sp->unsync_children < 0); | |
4731d4c7 MT |
1285 | } |
1286 | ||
4731d4c7 | 1287 | |
60c8aec6 MT |
1288 | return nr_unsync_leaf; |
1289 | } | |
1290 | ||
1291 | static int mmu_unsync_walk(struct kvm_mmu_page *sp, | |
1292 | struct kvm_mmu_pages *pvec) | |
1293 | { | |
1294 | if (!sp->unsync_children) | |
1295 | return 0; | |
1296 | ||
1297 | mmu_pages_add(pvec, sp, 0); | |
1298 | return __mmu_unsync_walk(sp, pvec); | |
4731d4c7 MT |
1299 | } |
1300 | ||
4731d4c7 MT |
1301 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
1302 | { | |
1303 | WARN_ON(!sp->unsync); | |
5e1b3ddb | 1304 | trace_kvm_mmu_sync_page(sp); |
4731d4c7 MT |
1305 | sp->unsync = 0; |
1306 | --kvm->stat.mmu_unsync; | |
1307 | } | |
1308 | ||
7775834a XG |
1309 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
1310 | struct list_head *invalid_list); | |
1311 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, | |
1312 | struct list_head *invalid_list); | |
4731d4c7 | 1313 | |
f41d335a XG |
1314 | #define for_each_gfn_sp(kvm, sp, gfn, pos) \ |
1315 | hlist_for_each_entry(sp, pos, \ | |
7ae680eb XG |
1316 | &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \ |
1317 | if ((sp)->gfn != (gfn)) {} else | |
1318 | ||
f41d335a XG |
1319 | #define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \ |
1320 | hlist_for_each_entry(sp, pos, \ | |
7ae680eb XG |
1321 | &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \ |
1322 | if ((sp)->gfn != (gfn) || (sp)->role.direct || \ | |
1323 | (sp)->role.invalid) {} else | |
1324 | ||
f918b443 | 1325 | /* @sp->gfn should be write-protected at the call site */ |
1d9dc7e0 | 1326 | static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
d98ba053 | 1327 | struct list_head *invalid_list, bool clear_unsync) |
4731d4c7 | 1328 | { |
5b7e0102 | 1329 | if (sp->role.cr4_pae != !!is_pae(vcpu)) { |
d98ba053 | 1330 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
4731d4c7 MT |
1331 | return 1; |
1332 | } | |
1333 | ||
f918b443 | 1334 | if (clear_unsync) |
1d9dc7e0 | 1335 | kvm_unlink_unsync_page(vcpu->kvm, sp); |
1d9dc7e0 | 1336 | |
a4a8e6f7 | 1337 | if (vcpu->arch.mmu.sync_page(vcpu, sp)) { |
d98ba053 | 1338 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
4731d4c7 MT |
1339 | return 1; |
1340 | } | |
1341 | ||
1342 | kvm_mmu_flush_tlb(vcpu); | |
4731d4c7 MT |
1343 | return 0; |
1344 | } | |
1345 | ||
1d9dc7e0 XG |
1346 | static int kvm_sync_page_transient(struct kvm_vcpu *vcpu, |
1347 | struct kvm_mmu_page *sp) | |
1348 | { | |
d98ba053 | 1349 | LIST_HEAD(invalid_list); |
1d9dc7e0 XG |
1350 | int ret; |
1351 | ||
d98ba053 | 1352 | ret = __kvm_sync_page(vcpu, sp, &invalid_list, false); |
be71e061 | 1353 | if (ret) |
d98ba053 XG |
1354 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
1355 | ||
1d9dc7e0 XG |
1356 | return ret; |
1357 | } | |
1358 | ||
d98ba053 XG |
1359 | static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
1360 | struct list_head *invalid_list) | |
1d9dc7e0 | 1361 | { |
d98ba053 | 1362 | return __kvm_sync_page(vcpu, sp, invalid_list, true); |
1d9dc7e0 XG |
1363 | } |
1364 | ||
9f1a122f XG |
1365 | /* @gfn should be write-protected at the call site */ |
1366 | static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) | |
1367 | { | |
9f1a122f | 1368 | struct kvm_mmu_page *s; |
f41d335a | 1369 | struct hlist_node *node; |
d98ba053 | 1370 | LIST_HEAD(invalid_list); |
9f1a122f XG |
1371 | bool flush = false; |
1372 | ||
f41d335a | 1373 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
7ae680eb | 1374 | if (!s->unsync) |
9f1a122f XG |
1375 | continue; |
1376 | ||
1377 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); | |
a4a8e6f7 | 1378 | kvm_unlink_unsync_page(vcpu->kvm, s); |
9f1a122f | 1379 | if ((s->role.cr4_pae != !!is_pae(vcpu)) || |
a4a8e6f7 | 1380 | (vcpu->arch.mmu.sync_page(vcpu, s))) { |
d98ba053 | 1381 | kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list); |
9f1a122f XG |
1382 | continue; |
1383 | } | |
9f1a122f XG |
1384 | flush = true; |
1385 | } | |
1386 | ||
d98ba053 | 1387 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
9f1a122f XG |
1388 | if (flush) |
1389 | kvm_mmu_flush_tlb(vcpu); | |
1390 | } | |
1391 | ||
60c8aec6 MT |
1392 | struct mmu_page_path { |
1393 | struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1]; | |
1394 | unsigned int idx[PT64_ROOT_LEVEL-1]; | |
4731d4c7 MT |
1395 | }; |
1396 | ||
60c8aec6 MT |
1397 | #define for_each_sp(pvec, sp, parents, i) \ |
1398 | for (i = mmu_pages_next(&pvec, &parents, -1), \ | |
1399 | sp = pvec.page[i].sp; \ | |
1400 | i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \ | |
1401 | i = mmu_pages_next(&pvec, &parents, i)) | |
1402 | ||
cded19f3 HE |
1403 | static int mmu_pages_next(struct kvm_mmu_pages *pvec, |
1404 | struct mmu_page_path *parents, | |
1405 | int i) | |
60c8aec6 MT |
1406 | { |
1407 | int n; | |
1408 | ||
1409 | for (n = i+1; n < pvec->nr; n++) { | |
1410 | struct kvm_mmu_page *sp = pvec->page[n].sp; | |
1411 | ||
1412 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { | |
1413 | parents->idx[0] = pvec->page[n].idx; | |
1414 | return n; | |
1415 | } | |
1416 | ||
1417 | parents->parent[sp->role.level-2] = sp; | |
1418 | parents->idx[sp->role.level-1] = pvec->page[n].idx; | |
1419 | } | |
1420 | ||
1421 | return n; | |
1422 | } | |
1423 | ||
cded19f3 | 1424 | static void mmu_pages_clear_parents(struct mmu_page_path *parents) |
4731d4c7 | 1425 | { |
60c8aec6 MT |
1426 | struct kvm_mmu_page *sp; |
1427 | unsigned int level = 0; | |
1428 | ||
1429 | do { | |
1430 | unsigned int idx = parents->idx[level]; | |
4731d4c7 | 1431 | |
60c8aec6 MT |
1432 | sp = parents->parent[level]; |
1433 | if (!sp) | |
1434 | return; | |
1435 | ||
1436 | --sp->unsync_children; | |
1437 | WARN_ON((int)sp->unsync_children < 0); | |
1438 | __clear_bit(idx, sp->unsync_child_bitmap); | |
1439 | level++; | |
1440 | } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children); | |
4731d4c7 MT |
1441 | } |
1442 | ||
60c8aec6 MT |
1443 | static void kvm_mmu_pages_init(struct kvm_mmu_page *parent, |
1444 | struct mmu_page_path *parents, | |
1445 | struct kvm_mmu_pages *pvec) | |
4731d4c7 | 1446 | { |
60c8aec6 MT |
1447 | parents->parent[parent->role.level-1] = NULL; |
1448 | pvec->nr = 0; | |
1449 | } | |
4731d4c7 | 1450 | |
60c8aec6 MT |
1451 | static void mmu_sync_children(struct kvm_vcpu *vcpu, |
1452 | struct kvm_mmu_page *parent) | |
1453 | { | |
1454 | int i; | |
1455 | struct kvm_mmu_page *sp; | |
1456 | struct mmu_page_path parents; | |
1457 | struct kvm_mmu_pages pages; | |
d98ba053 | 1458 | LIST_HEAD(invalid_list); |
60c8aec6 MT |
1459 | |
1460 | kvm_mmu_pages_init(parent, &parents, &pages); | |
1461 | while (mmu_unsync_walk(parent, &pages)) { | |
b1a36821 MT |
1462 | int protected = 0; |
1463 | ||
1464 | for_each_sp(pages, sp, parents, i) | |
1465 | protected |= rmap_write_protect(vcpu->kvm, sp->gfn); | |
1466 | ||
1467 | if (protected) | |
1468 | kvm_flush_remote_tlbs(vcpu->kvm); | |
1469 | ||
60c8aec6 | 1470 | for_each_sp(pages, sp, parents, i) { |
d98ba053 | 1471 | kvm_sync_page(vcpu, sp, &invalid_list); |
60c8aec6 MT |
1472 | mmu_pages_clear_parents(&parents); |
1473 | } | |
d98ba053 | 1474 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
4731d4c7 | 1475 | cond_resched_lock(&vcpu->kvm->mmu_lock); |
60c8aec6 MT |
1476 | kvm_mmu_pages_init(parent, &parents, &pages); |
1477 | } | |
4731d4c7 MT |
1478 | } |
1479 | ||
cea0f0e7 AK |
1480 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
1481 | gfn_t gfn, | |
1482 | gva_t gaddr, | |
1483 | unsigned level, | |
f6e2c02b | 1484 | int direct, |
41074d07 | 1485 | unsigned access, |
f7d9c7b7 | 1486 | u64 *parent_pte) |
cea0f0e7 AK |
1487 | { |
1488 | union kvm_mmu_page_role role; | |
cea0f0e7 | 1489 | unsigned quadrant; |
9f1a122f | 1490 | struct kvm_mmu_page *sp; |
f41d335a | 1491 | struct hlist_node *node; |
9f1a122f | 1492 | bool need_sync = false; |
cea0f0e7 | 1493 | |
a770f6f2 | 1494 | role = vcpu->arch.mmu.base_role; |
cea0f0e7 | 1495 | role.level = level; |
f6e2c02b | 1496 | role.direct = direct; |
84b0c8c6 | 1497 | if (role.direct) |
5b7e0102 | 1498 | role.cr4_pae = 0; |
41074d07 | 1499 | role.access = access; |
c5a78f2b JR |
1500 | if (!vcpu->arch.mmu.direct_map |
1501 | && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { | |
cea0f0e7 AK |
1502 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
1503 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; | |
1504 | role.quadrant = quadrant; | |
1505 | } | |
f41d335a | 1506 | for_each_gfn_sp(vcpu->kvm, sp, gfn, node) { |
7ae680eb XG |
1507 | if (!need_sync && sp->unsync) |
1508 | need_sync = true; | |
4731d4c7 | 1509 | |
7ae680eb XG |
1510 | if (sp->role.word != role.word) |
1511 | continue; | |
4731d4c7 | 1512 | |
7ae680eb XG |
1513 | if (sp->unsync && kvm_sync_page_transient(vcpu, sp)) |
1514 | break; | |
e02aa901 | 1515 | |
7ae680eb XG |
1516 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); |
1517 | if (sp->unsync_children) { | |
a8eeb04a | 1518 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
7ae680eb XG |
1519 | kvm_mmu_mark_parents_unsync(sp); |
1520 | } else if (sp->unsync) | |
1521 | kvm_mmu_mark_parents_unsync(sp); | |
e02aa901 | 1522 | |
7ae680eb XG |
1523 | trace_kvm_mmu_get_page(sp, false); |
1524 | return sp; | |
1525 | } | |
dfc5aa00 | 1526 | ++vcpu->kvm->stat.mmu_cache_miss; |
2032a93d | 1527 | sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct); |
4db35314 AK |
1528 | if (!sp) |
1529 | return sp; | |
4db35314 AK |
1530 | sp->gfn = gfn; |
1531 | sp->role = role; | |
7ae680eb XG |
1532 | hlist_add_head(&sp->hash_link, |
1533 | &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]); | |
f6e2c02b | 1534 | if (!direct) { |
b1a36821 MT |
1535 | if (rmap_write_protect(vcpu->kvm, gfn)) |
1536 | kvm_flush_remote_tlbs(vcpu->kvm); | |
9f1a122f XG |
1537 | if (level > PT_PAGE_TABLE_LEVEL && need_sync) |
1538 | kvm_sync_pages(vcpu, gfn); | |
1539 | ||
4731d4c7 MT |
1540 | account_shadowed(vcpu->kvm, gfn); |
1541 | } | |
131d8279 AK |
1542 | if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte) |
1543 | vcpu->arch.mmu.prefetch_page(vcpu, sp); | |
1544 | else | |
1545 | nonpaging_prefetch_page(vcpu, sp); | |
f691fe1d | 1546 | trace_kvm_mmu_get_page(sp, true); |
4db35314 | 1547 | return sp; |
cea0f0e7 AK |
1548 | } |
1549 | ||
2d11123a AK |
1550 | static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator, |
1551 | struct kvm_vcpu *vcpu, u64 addr) | |
1552 | { | |
1553 | iterator->addr = addr; | |
1554 | iterator->shadow_addr = vcpu->arch.mmu.root_hpa; | |
1555 | iterator->level = vcpu->arch.mmu.shadow_root_level; | |
81407ca5 JR |
1556 | |
1557 | if (iterator->level == PT64_ROOT_LEVEL && | |
1558 | vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL && | |
1559 | !vcpu->arch.mmu.direct_map) | |
1560 | --iterator->level; | |
1561 | ||
2d11123a AK |
1562 | if (iterator->level == PT32E_ROOT_LEVEL) { |
1563 | iterator->shadow_addr | |
1564 | = vcpu->arch.mmu.pae_root[(addr >> 30) & 3]; | |
1565 | iterator->shadow_addr &= PT64_BASE_ADDR_MASK; | |
1566 | --iterator->level; | |
1567 | if (!iterator->shadow_addr) | |
1568 | iterator->level = 0; | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator) | |
1573 | { | |
1574 | if (iterator->level < PT_PAGE_TABLE_LEVEL) | |
1575 | return false; | |
4d88954d MT |
1576 | |
1577 | if (iterator->level == PT_PAGE_TABLE_LEVEL) | |
1578 | if (is_large_pte(*iterator->sptep)) | |
1579 | return false; | |
1580 | ||
2d11123a AK |
1581 | iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level); |
1582 | iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index; | |
1583 | return true; | |
1584 | } | |
1585 | ||
1586 | static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator) | |
1587 | { | |
1588 | iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK; | |
1589 | --iterator->level; | |
1590 | } | |
1591 | ||
32ef26a3 AK |
1592 | static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp) |
1593 | { | |
1594 | u64 spte; | |
1595 | ||
1596 | spte = __pa(sp->spt) | |
1597 | | PT_PRESENT_MASK | PT_ACCESSED_MASK | |
1598 | | PT_WRITABLE_MASK | PT_USER_MASK; | |
121eee97 | 1599 | __set_spte(sptep, spte); |
32ef26a3 AK |
1600 | } |
1601 | ||
a3aa51cf AK |
1602 | static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep) |
1603 | { | |
1604 | if (is_large_pte(*sptep)) { | |
1605 | drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); | |
1606 | kvm_flush_remote_tlbs(vcpu->kvm); | |
1607 | } | |
1608 | } | |
1609 | ||
a357bd22 AK |
1610 | static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
1611 | unsigned direct_access) | |
1612 | { | |
1613 | if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) { | |
1614 | struct kvm_mmu_page *child; | |
1615 | ||
1616 | /* | |
1617 | * For the direct sp, if the guest pte's dirty bit | |
1618 | * changed form clean to dirty, it will corrupt the | |
1619 | * sp's access: allow writable in the read-only sp, | |
1620 | * so we should update the spte at this point to get | |
1621 | * a new sp with the correct access. | |
1622 | */ | |
1623 | child = page_header(*sptep & PT64_BASE_ADDR_MASK); | |
1624 | if (child->role.access == direct_access) | |
1625 | return; | |
1626 | ||
1627 | mmu_page_remove_parent_pte(child, sptep); | |
1628 | __set_spte(sptep, shadow_trap_nonpresent_pte); | |
1629 | kvm_flush_remote_tlbs(vcpu->kvm); | |
1630 | } | |
1631 | } | |
1632 | ||
90cb0529 | 1633 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
4db35314 | 1634 | struct kvm_mmu_page *sp) |
a436036b | 1635 | { |
697fe2e2 AK |
1636 | unsigned i; |
1637 | u64 *pt; | |
1638 | u64 ent; | |
1639 | ||
4db35314 | 1640 | pt = sp->spt; |
697fe2e2 | 1641 | |
697fe2e2 AK |
1642 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
1643 | ent = pt[i]; | |
1644 | ||
05da4558 | 1645 | if (is_shadow_present_pte(ent)) { |
776e6633 | 1646 | if (!is_last_spte(ent, sp->role.level)) { |
05da4558 MT |
1647 | ent &= PT64_BASE_ADDR_MASK; |
1648 | mmu_page_remove_parent_pte(page_header(ent), | |
1649 | &pt[i]); | |
1650 | } else { | |
776e6633 MT |
1651 | if (is_large_pte(ent)) |
1652 | --kvm->stat.lpages; | |
be38d276 AK |
1653 | drop_spte(kvm, &pt[i], |
1654 | shadow_trap_nonpresent_pte); | |
05da4558 MT |
1655 | } |
1656 | } | |
c7addb90 | 1657 | pt[i] = shadow_trap_nonpresent_pte; |
697fe2e2 | 1658 | } |
a436036b AK |
1659 | } |
1660 | ||
4db35314 | 1661 | static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte) |
cea0f0e7 | 1662 | { |
4db35314 | 1663 | mmu_page_remove_parent_pte(sp, parent_pte); |
a436036b AK |
1664 | } |
1665 | ||
12b7d28f AK |
1666 | static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm) |
1667 | { | |
1668 | int i; | |
988a2cae | 1669 | struct kvm_vcpu *vcpu; |
12b7d28f | 1670 | |
988a2cae GN |
1671 | kvm_for_each_vcpu(i, vcpu, kvm) |
1672 | vcpu->arch.last_pte_updated = NULL; | |
12b7d28f AK |
1673 | } |
1674 | ||
31aa2b44 | 1675 | static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp) |
a436036b AK |
1676 | { |
1677 | u64 *parent_pte; | |
1678 | ||
4db35314 AK |
1679 | while (sp->multimapped || sp->parent_pte) { |
1680 | if (!sp->multimapped) | |
1681 | parent_pte = sp->parent_pte; | |
a436036b AK |
1682 | else { |
1683 | struct kvm_pte_chain *chain; | |
1684 | ||
4db35314 | 1685 | chain = container_of(sp->parent_ptes.first, |
a436036b AK |
1686 | struct kvm_pte_chain, link); |
1687 | parent_pte = chain->parent_ptes[0]; | |
1688 | } | |
697fe2e2 | 1689 | BUG_ON(!parent_pte); |
4db35314 | 1690 | kvm_mmu_put_page(sp, parent_pte); |
d555c333 | 1691 | __set_spte(parent_pte, shadow_trap_nonpresent_pte); |
a436036b | 1692 | } |
31aa2b44 AK |
1693 | } |
1694 | ||
60c8aec6 | 1695 | static int mmu_zap_unsync_children(struct kvm *kvm, |
7775834a XG |
1696 | struct kvm_mmu_page *parent, |
1697 | struct list_head *invalid_list) | |
4731d4c7 | 1698 | { |
60c8aec6 MT |
1699 | int i, zapped = 0; |
1700 | struct mmu_page_path parents; | |
1701 | struct kvm_mmu_pages pages; | |
4731d4c7 | 1702 | |
60c8aec6 | 1703 | if (parent->role.level == PT_PAGE_TABLE_LEVEL) |
4731d4c7 | 1704 | return 0; |
60c8aec6 MT |
1705 | |
1706 | kvm_mmu_pages_init(parent, &parents, &pages); | |
1707 | while (mmu_unsync_walk(parent, &pages)) { | |
1708 | struct kvm_mmu_page *sp; | |
1709 | ||
1710 | for_each_sp(pages, sp, parents, i) { | |
7775834a | 1711 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
60c8aec6 | 1712 | mmu_pages_clear_parents(&parents); |
77662e00 | 1713 | zapped++; |
60c8aec6 | 1714 | } |
60c8aec6 MT |
1715 | kvm_mmu_pages_init(parent, &parents, &pages); |
1716 | } | |
1717 | ||
1718 | return zapped; | |
4731d4c7 MT |
1719 | } |
1720 | ||
7775834a XG |
1721 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
1722 | struct list_head *invalid_list) | |
31aa2b44 | 1723 | { |
4731d4c7 | 1724 | int ret; |
f691fe1d | 1725 | |
7775834a | 1726 | trace_kvm_mmu_prepare_zap_page(sp); |
31aa2b44 | 1727 | ++kvm->stat.mmu_shadow_zapped; |
7775834a | 1728 | ret = mmu_zap_unsync_children(kvm, sp, invalid_list); |
4db35314 | 1729 | kvm_mmu_page_unlink_children(kvm, sp); |
31aa2b44 | 1730 | kvm_mmu_unlink_parents(kvm, sp); |
f6e2c02b | 1731 | if (!sp->role.invalid && !sp->role.direct) |
5b5c6a5a | 1732 | unaccount_shadowed(kvm, sp->gfn); |
4731d4c7 MT |
1733 | if (sp->unsync) |
1734 | kvm_unlink_unsync_page(kvm, sp); | |
4db35314 | 1735 | if (!sp->root_count) { |
54a4f023 GJ |
1736 | /* Count self */ |
1737 | ret++; | |
7775834a | 1738 | list_move(&sp->link, invalid_list); |
2e53d63a | 1739 | } else { |
5b5c6a5a | 1740 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
2e53d63a MT |
1741 | kvm_reload_remote_mmus(kvm); |
1742 | } | |
7775834a XG |
1743 | |
1744 | sp->role.invalid = 1; | |
12b7d28f | 1745 | kvm_mmu_reset_last_pte_updated(kvm); |
4731d4c7 | 1746 | return ret; |
a436036b AK |
1747 | } |
1748 | ||
7775834a XG |
1749 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
1750 | struct list_head *invalid_list) | |
1751 | { | |
1752 | struct kvm_mmu_page *sp; | |
1753 | ||
1754 | if (list_empty(invalid_list)) | |
1755 | return; | |
1756 | ||
1757 | kvm_flush_remote_tlbs(kvm); | |
1758 | ||
1759 | do { | |
1760 | sp = list_first_entry(invalid_list, struct kvm_mmu_page, link); | |
1761 | WARN_ON(!sp->role.invalid || sp->root_count); | |
1762 | kvm_mmu_free_page(kvm, sp); | |
1763 | } while (!list_empty(invalid_list)); | |
1764 | ||
1765 | } | |
1766 | ||
82ce2c96 IE |
1767 | /* |
1768 | * Changing the number of mmu pages allocated to the vm | |
49d5ca26 | 1769 | * Note: if goal_nr_mmu_pages is too small, you will get dead lock |
82ce2c96 | 1770 | */ |
49d5ca26 | 1771 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages) |
82ce2c96 | 1772 | { |
d98ba053 | 1773 | LIST_HEAD(invalid_list); |
82ce2c96 IE |
1774 | /* |
1775 | * If we set the number of mmu pages to be smaller be than the | |
1776 | * number of actived pages , we must to free some mmu pages before we | |
1777 | * change the value | |
1778 | */ | |
1779 | ||
49d5ca26 DH |
1780 | if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { |
1781 | while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages && | |
77662e00 | 1782 | !list_empty(&kvm->arch.active_mmu_pages)) { |
82ce2c96 IE |
1783 | struct kvm_mmu_page *page; |
1784 | ||
f05e70ac | 1785 | page = container_of(kvm->arch.active_mmu_pages.prev, |
82ce2c96 | 1786 | struct kvm_mmu_page, link); |
80b63faf XF |
1787 | kvm_mmu_prepare_zap_page(kvm, page, &invalid_list); |
1788 | kvm_mmu_commit_zap_page(kvm, &invalid_list); | |
82ce2c96 | 1789 | } |
49d5ca26 | 1790 | goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; |
82ce2c96 | 1791 | } |
82ce2c96 | 1792 | |
49d5ca26 | 1793 | kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages; |
82ce2c96 IE |
1794 | } |
1795 | ||
f67a46f4 | 1796 | static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
a436036b | 1797 | { |
4db35314 | 1798 | struct kvm_mmu_page *sp; |
f41d335a | 1799 | struct hlist_node *node; |
d98ba053 | 1800 | LIST_HEAD(invalid_list); |
a436036b AK |
1801 | int r; |
1802 | ||
9ad17b10 | 1803 | pgprintk("%s: looking for gfn %llx\n", __func__, gfn); |
a436036b | 1804 | r = 0; |
f41d335a XG |
1805 | |
1806 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) { | |
9ad17b10 | 1807 | pgprintk("%s: gfn %llx role %x\n", __func__, gfn, |
7ae680eb XG |
1808 | sp->role.word); |
1809 | r = 1; | |
f41d335a | 1810 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
7ae680eb | 1811 | } |
d98ba053 | 1812 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
a436036b | 1813 | return r; |
cea0f0e7 AK |
1814 | } |
1815 | ||
f67a46f4 | 1816 | static void mmu_unshadow(struct kvm *kvm, gfn_t gfn) |
97a0a01e | 1817 | { |
4db35314 | 1818 | struct kvm_mmu_page *sp; |
f41d335a | 1819 | struct hlist_node *node; |
d98ba053 | 1820 | LIST_HEAD(invalid_list); |
97a0a01e | 1821 | |
f41d335a | 1822 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) { |
9ad17b10 | 1823 | pgprintk("%s: zap %llx %x\n", |
7ae680eb | 1824 | __func__, gfn, sp->role.word); |
f41d335a | 1825 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
97a0a01e | 1826 | } |
d98ba053 | 1827 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
97a0a01e AK |
1828 | } |
1829 | ||
38c335f1 | 1830 | static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn) |
6aa8b732 | 1831 | { |
bc6678a3 | 1832 | int slot = memslot_id(kvm, gfn); |
4db35314 | 1833 | struct kvm_mmu_page *sp = page_header(__pa(pte)); |
6aa8b732 | 1834 | |
291f26bc | 1835 | __set_bit(slot, sp->slot_bitmap); |
6aa8b732 AK |
1836 | } |
1837 | ||
6844dec6 MT |
1838 | static void mmu_convert_notrap(struct kvm_mmu_page *sp) |
1839 | { | |
1840 | int i; | |
1841 | u64 *pt = sp->spt; | |
1842 | ||
1843 | if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte) | |
1844 | return; | |
1845 | ||
1846 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { | |
1847 | if (pt[i] == shadow_notrap_nonpresent_pte) | |
d555c333 | 1848 | __set_spte(&pt[i], shadow_trap_nonpresent_pte); |
6844dec6 MT |
1849 | } |
1850 | } | |
1851 | ||
74be52e3 SY |
1852 | /* |
1853 | * The function is based on mtrr_type_lookup() in | |
1854 | * arch/x86/kernel/cpu/mtrr/generic.c | |
1855 | */ | |
1856 | static int get_mtrr_type(struct mtrr_state_type *mtrr_state, | |
1857 | u64 start, u64 end) | |
1858 | { | |
1859 | int i; | |
1860 | u64 base, mask; | |
1861 | u8 prev_match, curr_match; | |
1862 | int num_var_ranges = KVM_NR_VAR_MTRR; | |
1863 | ||
1864 | if (!mtrr_state->enabled) | |
1865 | return 0xFF; | |
1866 | ||
1867 | /* Make end inclusive end, instead of exclusive */ | |
1868 | end--; | |
1869 | ||
1870 | /* Look in fixed ranges. Just return the type as per start */ | |
1871 | if (mtrr_state->have_fixed && (start < 0x100000)) { | |
1872 | int idx; | |
1873 | ||
1874 | if (start < 0x80000) { | |
1875 | idx = 0; | |
1876 | idx += (start >> 16); | |
1877 | return mtrr_state->fixed_ranges[idx]; | |
1878 | } else if (start < 0xC0000) { | |
1879 | idx = 1 * 8; | |
1880 | idx += ((start - 0x80000) >> 14); | |
1881 | return mtrr_state->fixed_ranges[idx]; | |
1882 | } else if (start < 0x1000000) { | |
1883 | idx = 3 * 8; | |
1884 | idx += ((start - 0xC0000) >> 12); | |
1885 | return mtrr_state->fixed_ranges[idx]; | |
1886 | } | |
1887 | } | |
1888 | ||
1889 | /* | |
1890 | * Look in variable ranges | |
1891 | * Look of multiple ranges matching this address and pick type | |
1892 | * as per MTRR precedence | |
1893 | */ | |
1894 | if (!(mtrr_state->enabled & 2)) | |
1895 | return mtrr_state->def_type; | |
1896 | ||
1897 | prev_match = 0xFF; | |
1898 | for (i = 0; i < num_var_ranges; ++i) { | |
1899 | unsigned short start_state, end_state; | |
1900 | ||
1901 | if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11))) | |
1902 | continue; | |
1903 | ||
1904 | base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) + | |
1905 | (mtrr_state->var_ranges[i].base_lo & PAGE_MASK); | |
1906 | mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) + | |
1907 | (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK); | |
1908 | ||
1909 | start_state = ((start & mask) == (base & mask)); | |
1910 | end_state = ((end & mask) == (base & mask)); | |
1911 | if (start_state != end_state) | |
1912 | return 0xFE; | |
1913 | ||
1914 | if ((start & mask) != (base & mask)) | |
1915 | continue; | |
1916 | ||
1917 | curr_match = mtrr_state->var_ranges[i].base_lo & 0xff; | |
1918 | if (prev_match == 0xFF) { | |
1919 | prev_match = curr_match; | |
1920 | continue; | |
1921 | } | |
1922 | ||
1923 | if (prev_match == MTRR_TYPE_UNCACHABLE || | |
1924 | curr_match == MTRR_TYPE_UNCACHABLE) | |
1925 | return MTRR_TYPE_UNCACHABLE; | |
1926 | ||
1927 | if ((prev_match == MTRR_TYPE_WRBACK && | |
1928 | curr_match == MTRR_TYPE_WRTHROUGH) || | |
1929 | (prev_match == MTRR_TYPE_WRTHROUGH && | |
1930 | curr_match == MTRR_TYPE_WRBACK)) { | |
1931 | prev_match = MTRR_TYPE_WRTHROUGH; | |
1932 | curr_match = MTRR_TYPE_WRTHROUGH; | |
1933 | } | |
1934 | ||
1935 | if (prev_match != curr_match) | |
1936 | return MTRR_TYPE_UNCACHABLE; | |
1937 | } | |
1938 | ||
1939 | if (prev_match != 0xFF) | |
1940 | return prev_match; | |
1941 | ||
1942 | return mtrr_state->def_type; | |
1943 | } | |
1944 | ||
4b12f0de | 1945 | u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) |
74be52e3 SY |
1946 | { |
1947 | u8 mtrr; | |
1948 | ||
1949 | mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT, | |
1950 | (gfn << PAGE_SHIFT) + PAGE_SIZE); | |
1951 | if (mtrr == 0xfe || mtrr == 0xff) | |
1952 | mtrr = MTRR_TYPE_WRBACK; | |
1953 | return mtrr; | |
1954 | } | |
4b12f0de | 1955 | EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type); |
74be52e3 | 1956 | |
9cf5cf5a XG |
1957 | static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
1958 | { | |
1959 | trace_kvm_mmu_unsync_page(sp); | |
1960 | ++vcpu->kvm->stat.mmu_unsync; | |
1961 | sp->unsync = 1; | |
1962 | ||
1963 | kvm_mmu_mark_parents_unsync(sp); | |
1964 | mmu_convert_notrap(sp); | |
1965 | } | |
1966 | ||
1967 | static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) | |
4731d4c7 | 1968 | { |
4731d4c7 | 1969 | struct kvm_mmu_page *s; |
f41d335a | 1970 | struct hlist_node *node; |
9cf5cf5a | 1971 | |
f41d335a | 1972 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
7ae680eb | 1973 | if (s->unsync) |
4731d4c7 | 1974 | continue; |
9cf5cf5a XG |
1975 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); |
1976 | __kvm_unsync_page(vcpu, s); | |
4731d4c7 | 1977 | } |
4731d4c7 MT |
1978 | } |
1979 | ||
1980 | static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, | |
1981 | bool can_unsync) | |
1982 | { | |
9cf5cf5a | 1983 | struct kvm_mmu_page *s; |
f41d335a | 1984 | struct hlist_node *node; |
9cf5cf5a XG |
1985 | bool need_unsync = false; |
1986 | ||
f41d335a | 1987 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
36a2e677 XG |
1988 | if (!can_unsync) |
1989 | return 1; | |
1990 | ||
9cf5cf5a | 1991 | if (s->role.level != PT_PAGE_TABLE_LEVEL) |
4731d4c7 | 1992 | return 1; |
9cf5cf5a XG |
1993 | |
1994 | if (!need_unsync && !s->unsync) { | |
36a2e677 | 1995 | if (!oos_shadow) |
9cf5cf5a XG |
1996 | return 1; |
1997 | need_unsync = true; | |
1998 | } | |
4731d4c7 | 1999 | } |
9cf5cf5a XG |
2000 | if (need_unsync) |
2001 | kvm_unsync_pages(vcpu, gfn); | |
4731d4c7 MT |
2002 | return 0; |
2003 | } | |
2004 | ||
d555c333 | 2005 | static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
1e73f9dd | 2006 | unsigned pte_access, int user_fault, |
852e3c19 | 2007 | int write_fault, int dirty, int level, |
c2d0ee46 | 2008 | gfn_t gfn, pfn_t pfn, bool speculative, |
9bdbba13 | 2009 | bool can_unsync, bool host_writable) |
1c4f1fd6 | 2010 | { |
b330aa0c | 2011 | u64 spte, entry = *sptep; |
1e73f9dd | 2012 | int ret = 0; |
64d4d521 | 2013 | |
1c4f1fd6 AK |
2014 | /* |
2015 | * We don't set the accessed bit, since we sometimes want to see | |
2016 | * whether the guest actually used the pte (in order to detect | |
2017 | * demand paging). | |
2018 | */ | |
982c2565 | 2019 | spte = PT_PRESENT_MASK; |
947da538 | 2020 | if (!speculative) |
3201b5d9 | 2021 | spte |= shadow_accessed_mask; |
1c4f1fd6 AK |
2022 | if (!dirty) |
2023 | pte_access &= ~ACC_WRITE_MASK; | |
7b52345e SY |
2024 | if (pte_access & ACC_EXEC_MASK) |
2025 | spte |= shadow_x_mask; | |
2026 | else | |
2027 | spte |= shadow_nx_mask; | |
1c4f1fd6 | 2028 | if (pte_access & ACC_USER_MASK) |
7b52345e | 2029 | spte |= shadow_user_mask; |
852e3c19 | 2030 | if (level > PT_PAGE_TABLE_LEVEL) |
05da4558 | 2031 | spte |= PT_PAGE_SIZE_MASK; |
b0bc3ee2 | 2032 | if (tdp_enabled) |
4b12f0de SY |
2033 | spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn, |
2034 | kvm_is_mmio_pfn(pfn)); | |
1c4f1fd6 | 2035 | |
9bdbba13 | 2036 | if (host_writable) |
1403283a | 2037 | spte |= SPTE_HOST_WRITEABLE; |
f8e453b0 XG |
2038 | else |
2039 | pte_access &= ~ACC_WRITE_MASK; | |
1403283a | 2040 | |
35149e21 | 2041 | spte |= (u64)pfn << PAGE_SHIFT; |
1c4f1fd6 AK |
2042 | |
2043 | if ((pte_access & ACC_WRITE_MASK) | |
c5a78f2b JR |
2044 | || (!vcpu->arch.mmu.direct_map && write_fault |
2045 | && !is_write_protection(vcpu) && !user_fault)) { | |
1c4f1fd6 | 2046 | |
852e3c19 JR |
2047 | if (level > PT_PAGE_TABLE_LEVEL && |
2048 | has_wrprotected_page(vcpu->kvm, gfn, level)) { | |
38187c83 | 2049 | ret = 1; |
be38d276 AK |
2050 | drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); |
2051 | goto done; | |
38187c83 MT |
2052 | } |
2053 | ||
1c4f1fd6 | 2054 | spte |= PT_WRITABLE_MASK; |
1c4f1fd6 | 2055 | |
c5a78f2b JR |
2056 | if (!vcpu->arch.mmu.direct_map |
2057 | && !(pte_access & ACC_WRITE_MASK)) | |
69325a12 AK |
2058 | spte &= ~PT_USER_MASK; |
2059 | ||
ecc5589f MT |
2060 | /* |
2061 | * Optimization: for pte sync, if spte was writable the hash | |
2062 | * lookup is unnecessary (and expensive). Write protection | |
2063 | * is responsibility of mmu_get_page / kvm_sync_page. | |
2064 | * Same reasoning can be applied to dirty page accounting. | |
2065 | */ | |
8dae4445 | 2066 | if (!can_unsync && is_writable_pte(*sptep)) |
ecc5589f MT |
2067 | goto set_pte; |
2068 | ||
4731d4c7 | 2069 | if (mmu_need_write_protect(vcpu, gfn, can_unsync)) { |
9ad17b10 | 2070 | pgprintk("%s: found shadow page for %llx, marking ro\n", |
b8688d51 | 2071 | __func__, gfn); |
1e73f9dd | 2072 | ret = 1; |
1c4f1fd6 | 2073 | pte_access &= ~ACC_WRITE_MASK; |
8dae4445 | 2074 | if (is_writable_pte(spte)) |
1c4f1fd6 | 2075 | spte &= ~PT_WRITABLE_MASK; |
1c4f1fd6 AK |
2076 | } |
2077 | } | |
2078 | ||
1c4f1fd6 AK |
2079 | if (pte_access & ACC_WRITE_MASK) |
2080 | mark_page_dirty(vcpu->kvm, gfn); | |
2081 | ||
38187c83 | 2082 | set_pte: |
b79b93f9 | 2083 | update_spte(sptep, spte); |
b330aa0c XG |
2084 | /* |
2085 | * If we overwrite a writable spte with a read-only one we | |
2086 | * should flush remote TLBs. Otherwise rmap_write_protect | |
2087 | * will find a read-only spte, even though the writable spte | |
2088 | * might be cached on a CPU's TLB. | |
2089 | */ | |
2090 | if (is_writable_pte(entry) && !is_writable_pte(*sptep)) | |
2091 | kvm_flush_remote_tlbs(vcpu->kvm); | |
be38d276 | 2092 | done: |
1e73f9dd MT |
2093 | return ret; |
2094 | } | |
2095 | ||
d555c333 | 2096 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
1e73f9dd MT |
2097 | unsigned pt_access, unsigned pte_access, |
2098 | int user_fault, int write_fault, int dirty, | |
852e3c19 | 2099 | int *ptwrite, int level, gfn_t gfn, |
1403283a | 2100 | pfn_t pfn, bool speculative, |
9bdbba13 | 2101 | bool host_writable) |
1e73f9dd MT |
2102 | { |
2103 | int was_rmapped = 0; | |
53a27b39 | 2104 | int rmap_count; |
1e73f9dd MT |
2105 | |
2106 | pgprintk("%s: spte %llx access %x write_fault %d" | |
9ad17b10 | 2107 | " user_fault %d gfn %llx\n", |
d555c333 | 2108 | __func__, *sptep, pt_access, |
1e73f9dd MT |
2109 | write_fault, user_fault, gfn); |
2110 | ||
d555c333 | 2111 | if (is_rmap_spte(*sptep)) { |
1e73f9dd MT |
2112 | /* |
2113 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink | |
2114 | * the parent of the now unreachable PTE. | |
2115 | */ | |
852e3c19 JR |
2116 | if (level > PT_PAGE_TABLE_LEVEL && |
2117 | !is_large_pte(*sptep)) { | |
1e73f9dd | 2118 | struct kvm_mmu_page *child; |
d555c333 | 2119 | u64 pte = *sptep; |
1e73f9dd MT |
2120 | |
2121 | child = page_header(pte & PT64_BASE_ADDR_MASK); | |
d555c333 | 2122 | mmu_page_remove_parent_pte(child, sptep); |
3be2264b MT |
2123 | __set_spte(sptep, shadow_trap_nonpresent_pte); |
2124 | kvm_flush_remote_tlbs(vcpu->kvm); | |
d555c333 | 2125 | } else if (pfn != spte_to_pfn(*sptep)) { |
9ad17b10 | 2126 | pgprintk("hfn old %llx new %llx\n", |
d555c333 | 2127 | spte_to_pfn(*sptep), pfn); |
be38d276 | 2128 | drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); |
91546356 | 2129 | kvm_flush_remote_tlbs(vcpu->kvm); |
6bed6b9e JR |
2130 | } else |
2131 | was_rmapped = 1; | |
1e73f9dd | 2132 | } |
852e3c19 | 2133 | |
d555c333 | 2134 | if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault, |
1403283a | 2135 | dirty, level, gfn, pfn, speculative, true, |
9bdbba13 | 2136 | host_writable)) { |
1e73f9dd MT |
2137 | if (write_fault) |
2138 | *ptwrite = 1; | |
5304efde | 2139 | kvm_mmu_flush_tlb(vcpu); |
a378b4e6 | 2140 | } |
1e73f9dd | 2141 | |
d555c333 | 2142 | pgprintk("%s: setting spte %llx\n", __func__, *sptep); |
9ad17b10 | 2143 | pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n", |
d555c333 | 2144 | is_large_pte(*sptep)? "2MB" : "4kB", |
a205bc19 JR |
2145 | *sptep & PT_PRESENT_MASK ?"RW":"R", gfn, |
2146 | *sptep, sptep); | |
d555c333 | 2147 | if (!was_rmapped && is_large_pte(*sptep)) |
05da4558 MT |
2148 | ++vcpu->kvm->stat.lpages; |
2149 | ||
d555c333 | 2150 | page_header_update_slot(vcpu->kvm, sptep, gfn); |
1c4f1fd6 | 2151 | if (!was_rmapped) { |
44ad9944 | 2152 | rmap_count = rmap_add(vcpu, sptep, gfn); |
53a27b39 | 2153 | if (rmap_count > RMAP_RECYCLE_THRESHOLD) |
852e3c19 | 2154 | rmap_recycle(vcpu, sptep, gfn); |
1c4f1fd6 | 2155 | } |
9ed5520d | 2156 | kvm_release_pfn_clean(pfn); |
1b7fcd32 | 2157 | if (speculative) { |
d555c333 | 2158 | vcpu->arch.last_pte_updated = sptep; |
1b7fcd32 AK |
2159 | vcpu->arch.last_pte_gfn = gfn; |
2160 | } | |
1c4f1fd6 AK |
2161 | } |
2162 | ||
6aa8b732 AK |
2163 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
2164 | { | |
2165 | } | |
2166 | ||
957ed9ef XG |
2167 | static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, |
2168 | bool no_dirty_log) | |
2169 | { | |
2170 | struct kvm_memory_slot *slot; | |
2171 | unsigned long hva; | |
2172 | ||
5d163b1c | 2173 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log); |
957ed9ef XG |
2174 | if (!slot) { |
2175 | get_page(bad_page); | |
2176 | return page_to_pfn(bad_page); | |
2177 | } | |
2178 | ||
2179 | hva = gfn_to_hva_memslot(slot, gfn); | |
2180 | ||
2181 | return hva_to_pfn_atomic(vcpu->kvm, hva); | |
2182 | } | |
2183 | ||
2184 | static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, | |
2185 | struct kvm_mmu_page *sp, | |
2186 | u64 *start, u64 *end) | |
2187 | { | |
2188 | struct page *pages[PTE_PREFETCH_NUM]; | |
2189 | unsigned access = sp->role.access; | |
2190 | int i, ret; | |
2191 | gfn_t gfn; | |
2192 | ||
2193 | gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt); | |
5d163b1c | 2194 | if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK)) |
957ed9ef XG |
2195 | return -1; |
2196 | ||
2197 | ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start); | |
2198 | if (ret <= 0) | |
2199 | return -1; | |
2200 | ||
2201 | for (i = 0; i < ret; i++, gfn++, start++) | |
2202 | mmu_set_spte(vcpu, start, ACC_ALL, | |
2203 | access, 0, 0, 1, NULL, | |
2204 | sp->role.level, gfn, | |
2205 | page_to_pfn(pages[i]), true, true); | |
2206 | ||
2207 | return 0; | |
2208 | } | |
2209 | ||
2210 | static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, | |
2211 | struct kvm_mmu_page *sp, u64 *sptep) | |
2212 | { | |
2213 | u64 *spte, *start = NULL; | |
2214 | int i; | |
2215 | ||
2216 | WARN_ON(!sp->role.direct); | |
2217 | ||
2218 | i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1); | |
2219 | spte = sp->spt + i; | |
2220 | ||
2221 | for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) { | |
2222 | if (*spte != shadow_trap_nonpresent_pte || spte == sptep) { | |
2223 | if (!start) | |
2224 | continue; | |
2225 | if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0) | |
2226 | break; | |
2227 | start = NULL; | |
2228 | } else if (!start) | |
2229 | start = spte; | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) | |
2234 | { | |
2235 | struct kvm_mmu_page *sp; | |
2236 | ||
2237 | /* | |
2238 | * Since it's no accessed bit on EPT, it's no way to | |
2239 | * distinguish between actually accessed translations | |
2240 | * and prefetched, so disable pte prefetch if EPT is | |
2241 | * enabled. | |
2242 | */ | |
2243 | if (!shadow_accessed_mask) | |
2244 | return; | |
2245 | ||
2246 | sp = page_header(__pa(sptep)); | |
2247 | if (sp->role.level > PT_PAGE_TABLE_LEVEL) | |
2248 | return; | |
2249 | ||
2250 | __direct_pte_prefetch(vcpu, sp, sptep); | |
2251 | } | |
2252 | ||
9f652d21 | 2253 | static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, |
2ec4739d XG |
2254 | int map_writable, int level, gfn_t gfn, pfn_t pfn, |
2255 | bool prefault) | |
140754bc | 2256 | { |
9f652d21 | 2257 | struct kvm_shadow_walk_iterator iterator; |
140754bc | 2258 | struct kvm_mmu_page *sp; |
9f652d21 | 2259 | int pt_write = 0; |
140754bc | 2260 | gfn_t pseudo_gfn; |
6aa8b732 | 2261 | |
9f652d21 | 2262 | for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) { |
852e3c19 | 2263 | if (iterator.level == level) { |
612819c3 MT |
2264 | unsigned pte_access = ACC_ALL; |
2265 | ||
612819c3 | 2266 | mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access, |
9f652d21 | 2267 | 0, write, 1, &pt_write, |
2ec4739d | 2268 | level, gfn, pfn, prefault, map_writable); |
957ed9ef | 2269 | direct_pte_prefetch(vcpu, iterator.sptep); |
9f652d21 AK |
2270 | ++vcpu->stat.pf_fixed; |
2271 | break; | |
6aa8b732 AK |
2272 | } |
2273 | ||
9f652d21 | 2274 | if (*iterator.sptep == shadow_trap_nonpresent_pte) { |
c9fa0b3b LJ |
2275 | u64 base_addr = iterator.addr; |
2276 | ||
2277 | base_addr &= PT64_LVL_ADDR_MASK(iterator.level); | |
2278 | pseudo_gfn = base_addr >> PAGE_SHIFT; | |
9f652d21 AK |
2279 | sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr, |
2280 | iterator.level - 1, | |
2281 | 1, ACC_ALL, iterator.sptep); | |
2282 | if (!sp) { | |
2283 | pgprintk("nonpaging_map: ENOMEM\n"); | |
2284 | kvm_release_pfn_clean(pfn); | |
2285 | return -ENOMEM; | |
2286 | } | |
140754bc | 2287 | |
d555c333 AK |
2288 | __set_spte(iterator.sptep, |
2289 | __pa(sp->spt) | |
2290 | | PT_PRESENT_MASK | PT_WRITABLE_MASK | |
33f91edb XG |
2291 | | shadow_user_mask | shadow_x_mask |
2292 | | shadow_accessed_mask); | |
9f652d21 AK |
2293 | } |
2294 | } | |
2295 | return pt_write; | |
6aa8b732 AK |
2296 | } |
2297 | ||
77db5cbd | 2298 | static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk) |
bf998156 | 2299 | { |
77db5cbd HY |
2300 | siginfo_t info; |
2301 | ||
2302 | info.si_signo = SIGBUS; | |
2303 | info.si_errno = 0; | |
2304 | info.si_code = BUS_MCEERR_AR; | |
2305 | info.si_addr = (void __user *)address; | |
2306 | info.si_addr_lsb = PAGE_SHIFT; | |
bf998156 | 2307 | |
77db5cbd | 2308 | send_sig_info(SIGBUS, &info, tsk); |
bf998156 HY |
2309 | } |
2310 | ||
2311 | static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn) | |
2312 | { | |
2313 | kvm_release_pfn_clean(pfn); | |
2314 | if (is_hwpoison_pfn(pfn)) { | |
77db5cbd | 2315 | kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current); |
bf998156 | 2316 | return 0; |
edba23e5 GN |
2317 | } else if (is_fault_pfn(pfn)) |
2318 | return -EFAULT; | |
2319 | ||
bf998156 HY |
2320 | return 1; |
2321 | } | |
2322 | ||
936a5fe6 AA |
2323 | static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu, |
2324 | gfn_t *gfnp, pfn_t *pfnp, int *levelp) | |
2325 | { | |
2326 | pfn_t pfn = *pfnp; | |
2327 | gfn_t gfn = *gfnp; | |
2328 | int level = *levelp; | |
2329 | ||
2330 | /* | |
2331 | * Check if it's a transparent hugepage. If this would be an | |
2332 | * hugetlbfs page, level wouldn't be set to | |
2333 | * PT_PAGE_TABLE_LEVEL and there would be no adjustment done | |
2334 | * here. | |
2335 | */ | |
2336 | if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) && | |
2337 | level == PT_PAGE_TABLE_LEVEL && | |
2338 | PageTransCompound(pfn_to_page(pfn)) && | |
2339 | !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) { | |
2340 | unsigned long mask; | |
2341 | /* | |
2342 | * mmu_notifier_retry was successful and we hold the | |
2343 | * mmu_lock here, so the pmd can't become splitting | |
2344 | * from under us, and in turn | |
2345 | * __split_huge_page_refcount() can't run from under | |
2346 | * us and we can safely transfer the refcount from | |
2347 | * PG_tail to PG_head as we switch the pfn to tail to | |
2348 | * head. | |
2349 | */ | |
2350 | *levelp = level = PT_DIRECTORY_LEVEL; | |
2351 | mask = KVM_PAGES_PER_HPAGE(level) - 1; | |
2352 | VM_BUG_ON((gfn & mask) != (pfn & mask)); | |
2353 | if (pfn & mask) { | |
2354 | gfn &= ~mask; | |
2355 | *gfnp = gfn; | |
2356 | kvm_release_pfn_clean(pfn); | |
2357 | pfn &= ~mask; | |
2358 | if (!get_page_unless_zero(pfn_to_page(pfn))) | |
2359 | BUG(); | |
2360 | *pfnp = pfn; | |
2361 | } | |
2362 | } | |
2363 | } | |
2364 | ||
78b2c54a | 2365 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
060c2abe XG |
2366 | gva_t gva, pfn_t *pfn, bool write, bool *writable); |
2367 | ||
2368 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn, | |
78b2c54a | 2369 | bool prefault) |
10589a46 MT |
2370 | { |
2371 | int r; | |
852e3c19 | 2372 | int level; |
936a5fe6 | 2373 | int force_pt_level; |
35149e21 | 2374 | pfn_t pfn; |
e930bffe | 2375 | unsigned long mmu_seq; |
612819c3 | 2376 | bool map_writable; |
aaee2c94 | 2377 | |
936a5fe6 AA |
2378 | force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn); |
2379 | if (likely(!force_pt_level)) { | |
2380 | level = mapping_level(vcpu, gfn); | |
2381 | /* | |
2382 | * This path builds a PAE pagetable - so we can map | |
2383 | * 2mb pages at maximum. Therefore check if the level | |
2384 | * is larger than that. | |
2385 | */ | |
2386 | if (level > PT_DIRECTORY_LEVEL) | |
2387 | level = PT_DIRECTORY_LEVEL; | |
852e3c19 | 2388 | |
936a5fe6 AA |
2389 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
2390 | } else | |
2391 | level = PT_PAGE_TABLE_LEVEL; | |
05da4558 | 2392 | |
e930bffe | 2393 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
4c2155ce | 2394 | smp_rmb(); |
060c2abe | 2395 | |
78b2c54a | 2396 | if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable)) |
060c2abe | 2397 | return 0; |
aaee2c94 | 2398 | |
d196e343 | 2399 | /* mmio */ |
bf998156 HY |
2400 | if (is_error_pfn(pfn)) |
2401 | return kvm_handle_bad_page(vcpu->kvm, gfn, pfn); | |
d196e343 | 2402 | |
aaee2c94 | 2403 | spin_lock(&vcpu->kvm->mmu_lock); |
e930bffe AA |
2404 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
2405 | goto out_unlock; | |
eb787d10 | 2406 | kvm_mmu_free_some_pages(vcpu); |
936a5fe6 AA |
2407 | if (likely(!force_pt_level)) |
2408 | transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level); | |
2ec4739d XG |
2409 | r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn, |
2410 | prefault); | |
aaee2c94 MT |
2411 | spin_unlock(&vcpu->kvm->mmu_lock); |
2412 | ||
aaee2c94 | 2413 | |
10589a46 | 2414 | return r; |
e930bffe AA |
2415 | |
2416 | out_unlock: | |
2417 | spin_unlock(&vcpu->kvm->mmu_lock); | |
2418 | kvm_release_pfn_clean(pfn); | |
2419 | return 0; | |
10589a46 MT |
2420 | } |
2421 | ||
2422 | ||
17ac10ad AK |
2423 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
2424 | { | |
2425 | int i; | |
4db35314 | 2426 | struct kvm_mmu_page *sp; |
d98ba053 | 2427 | LIST_HEAD(invalid_list); |
17ac10ad | 2428 | |
ad312c7c | 2429 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
7b53aa56 | 2430 | return; |
aaee2c94 | 2431 | spin_lock(&vcpu->kvm->mmu_lock); |
81407ca5 JR |
2432 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL && |
2433 | (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL || | |
2434 | vcpu->arch.mmu.direct_map)) { | |
ad312c7c | 2435 | hpa_t root = vcpu->arch.mmu.root_hpa; |
17ac10ad | 2436 | |
4db35314 AK |
2437 | sp = page_header(root); |
2438 | --sp->root_count; | |
d98ba053 XG |
2439 | if (!sp->root_count && sp->role.invalid) { |
2440 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); | |
2441 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); | |
2442 | } | |
ad312c7c | 2443 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
aaee2c94 | 2444 | spin_unlock(&vcpu->kvm->mmu_lock); |
17ac10ad AK |
2445 | return; |
2446 | } | |
17ac10ad | 2447 | for (i = 0; i < 4; ++i) { |
ad312c7c | 2448 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
17ac10ad | 2449 | |
417726a3 | 2450 | if (root) { |
417726a3 | 2451 | root &= PT64_BASE_ADDR_MASK; |
4db35314 AK |
2452 | sp = page_header(root); |
2453 | --sp->root_count; | |
2e53d63a | 2454 | if (!sp->root_count && sp->role.invalid) |
d98ba053 XG |
2455 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
2456 | &invalid_list); | |
417726a3 | 2457 | } |
ad312c7c | 2458 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
17ac10ad | 2459 | } |
d98ba053 | 2460 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
aaee2c94 | 2461 | spin_unlock(&vcpu->kvm->mmu_lock); |
ad312c7c | 2462 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
17ac10ad AK |
2463 | } |
2464 | ||
8986ecc0 MT |
2465 | static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn) |
2466 | { | |
2467 | int ret = 0; | |
2468 | ||
2469 | if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) { | |
a8eeb04a | 2470 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
8986ecc0 MT |
2471 | ret = 1; |
2472 | } | |
2473 | ||
2474 | return ret; | |
2475 | } | |
2476 | ||
651dd37a JR |
2477 | static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) |
2478 | { | |
2479 | struct kvm_mmu_page *sp; | |
7ebaf15e | 2480 | unsigned i; |
651dd37a JR |
2481 | |
2482 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { | |
2483 | spin_lock(&vcpu->kvm->mmu_lock); | |
2484 | kvm_mmu_free_some_pages(vcpu); | |
2485 | sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL, | |
2486 | 1, ACC_ALL, NULL); | |
2487 | ++sp->root_count; | |
2488 | spin_unlock(&vcpu->kvm->mmu_lock); | |
2489 | vcpu->arch.mmu.root_hpa = __pa(sp->spt); | |
2490 | } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) { | |
2491 | for (i = 0; i < 4; ++i) { | |
2492 | hpa_t root = vcpu->arch.mmu.pae_root[i]; | |
2493 | ||
2494 | ASSERT(!VALID_PAGE(root)); | |
2495 | spin_lock(&vcpu->kvm->mmu_lock); | |
2496 | kvm_mmu_free_some_pages(vcpu); | |
649497d1 AK |
2497 | sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT), |
2498 | i << 30, | |
651dd37a JR |
2499 | PT32_ROOT_LEVEL, 1, ACC_ALL, |
2500 | NULL); | |
2501 | root = __pa(sp->spt); | |
2502 | ++sp->root_count; | |
2503 | spin_unlock(&vcpu->kvm->mmu_lock); | |
2504 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; | |
651dd37a | 2505 | } |
6292757f | 2506 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
651dd37a JR |
2507 | } else |
2508 | BUG(); | |
2509 | ||
2510 | return 0; | |
2511 | } | |
2512 | ||
2513 | static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) | |
17ac10ad | 2514 | { |
4db35314 | 2515 | struct kvm_mmu_page *sp; |
81407ca5 JR |
2516 | u64 pdptr, pm_mask; |
2517 | gfn_t root_gfn; | |
2518 | int i; | |
3bb65a22 | 2519 | |
5777ed34 | 2520 | root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT; |
17ac10ad | 2521 | |
651dd37a JR |
2522 | if (mmu_check_root(vcpu, root_gfn)) |
2523 | return 1; | |
2524 | ||
2525 | /* | |
2526 | * Do we shadow a long mode page table? If so we need to | |
2527 | * write-protect the guests page table root. | |
2528 | */ | |
2529 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { | |
ad312c7c | 2530 | hpa_t root = vcpu->arch.mmu.root_hpa; |
17ac10ad AK |
2531 | |
2532 | ASSERT(!VALID_PAGE(root)); | |
651dd37a | 2533 | |
8facbbff | 2534 | spin_lock(&vcpu->kvm->mmu_lock); |
24955b6c | 2535 | kvm_mmu_free_some_pages(vcpu); |
651dd37a JR |
2536 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL, |
2537 | 0, ACC_ALL, NULL); | |
4db35314 AK |
2538 | root = __pa(sp->spt); |
2539 | ++sp->root_count; | |
8facbbff | 2540 | spin_unlock(&vcpu->kvm->mmu_lock); |
ad312c7c | 2541 | vcpu->arch.mmu.root_hpa = root; |
8986ecc0 | 2542 | return 0; |
17ac10ad | 2543 | } |
f87f9288 | 2544 | |
651dd37a JR |
2545 | /* |
2546 | * We shadow a 32 bit page table. This may be a legacy 2-level | |
81407ca5 JR |
2547 | * or a PAE 3-level page table. In either case we need to be aware that |
2548 | * the shadow page table may be a PAE or a long mode page table. | |
651dd37a | 2549 | */ |
81407ca5 JR |
2550 | pm_mask = PT_PRESENT_MASK; |
2551 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) | |
2552 | pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK; | |
2553 | ||
17ac10ad | 2554 | for (i = 0; i < 4; ++i) { |
ad312c7c | 2555 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
17ac10ad AK |
2556 | |
2557 | ASSERT(!VALID_PAGE(root)); | |
ad312c7c | 2558 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
d41d1895 | 2559 | pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i); |
43a3795a | 2560 | if (!is_present_gpte(pdptr)) { |
ad312c7c | 2561 | vcpu->arch.mmu.pae_root[i] = 0; |
417726a3 AK |
2562 | continue; |
2563 | } | |
6de4f3ad | 2564 | root_gfn = pdptr >> PAGE_SHIFT; |
f87f9288 JR |
2565 | if (mmu_check_root(vcpu, root_gfn)) |
2566 | return 1; | |
5a7388c2 | 2567 | } |
8facbbff | 2568 | spin_lock(&vcpu->kvm->mmu_lock); |
24955b6c | 2569 | kvm_mmu_free_some_pages(vcpu); |
4db35314 | 2570 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
651dd37a | 2571 | PT32_ROOT_LEVEL, 0, |
f7d9c7b7 | 2572 | ACC_ALL, NULL); |
4db35314 AK |
2573 | root = __pa(sp->spt); |
2574 | ++sp->root_count; | |
8facbbff AK |
2575 | spin_unlock(&vcpu->kvm->mmu_lock); |
2576 | ||
81407ca5 | 2577 | vcpu->arch.mmu.pae_root[i] = root | pm_mask; |
17ac10ad | 2578 | } |
6292757f | 2579 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
81407ca5 JR |
2580 | |
2581 | /* | |
2582 | * If we shadow a 32 bit page table with a long mode page | |
2583 | * table we enter this path. | |
2584 | */ | |
2585 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { | |
2586 | if (vcpu->arch.mmu.lm_root == NULL) { | |
2587 | /* | |
2588 | * The additional page necessary for this is only | |
2589 | * allocated on demand. | |
2590 | */ | |
2591 | ||
2592 | u64 *lm_root; | |
2593 | ||
2594 | lm_root = (void*)get_zeroed_page(GFP_KERNEL); | |
2595 | if (lm_root == NULL) | |
2596 | return 1; | |
2597 | ||
2598 | lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask; | |
2599 | ||
2600 | vcpu->arch.mmu.lm_root = lm_root; | |
2601 | } | |
2602 | ||
2603 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root); | |
2604 | } | |
2605 | ||
8986ecc0 | 2606 | return 0; |
17ac10ad AK |
2607 | } |
2608 | ||
651dd37a JR |
2609 | static int mmu_alloc_roots(struct kvm_vcpu *vcpu) |
2610 | { | |
2611 | if (vcpu->arch.mmu.direct_map) | |
2612 | return mmu_alloc_direct_roots(vcpu); | |
2613 | else | |
2614 | return mmu_alloc_shadow_roots(vcpu); | |
2615 | } | |
2616 | ||
0ba73cda MT |
2617 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) |
2618 | { | |
2619 | int i; | |
2620 | struct kvm_mmu_page *sp; | |
2621 | ||
81407ca5 JR |
2622 | if (vcpu->arch.mmu.direct_map) |
2623 | return; | |
2624 | ||
0ba73cda MT |
2625 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
2626 | return; | |
6903074c XG |
2627 | |
2628 | trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC); | |
81407ca5 | 2629 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { |
0ba73cda MT |
2630 | hpa_t root = vcpu->arch.mmu.root_hpa; |
2631 | sp = page_header(root); | |
2632 | mmu_sync_children(vcpu, sp); | |
5054c0de | 2633 | trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
0ba73cda MT |
2634 | return; |
2635 | } | |
2636 | for (i = 0; i < 4; ++i) { | |
2637 | hpa_t root = vcpu->arch.mmu.pae_root[i]; | |
2638 | ||
8986ecc0 | 2639 | if (root && VALID_PAGE(root)) { |
0ba73cda MT |
2640 | root &= PT64_BASE_ADDR_MASK; |
2641 | sp = page_header(root); | |
2642 | mmu_sync_children(vcpu, sp); | |
2643 | } | |
2644 | } | |
6903074c | 2645 | trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
0ba73cda MT |
2646 | } |
2647 | ||
2648 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) | |
2649 | { | |
2650 | spin_lock(&vcpu->kvm->mmu_lock); | |
2651 | mmu_sync_roots(vcpu); | |
6cffe8ca | 2652 | spin_unlock(&vcpu->kvm->mmu_lock); |
0ba73cda MT |
2653 | } |
2654 | ||
1871c602 | 2655 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr, |
ab9ae313 | 2656 | u32 access, struct x86_exception *exception) |
6aa8b732 | 2657 | { |
ab9ae313 AK |
2658 | if (exception) |
2659 | exception->error_code = 0; | |
6aa8b732 AK |
2660 | return vaddr; |
2661 | } | |
2662 | ||
6539e738 | 2663 | static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr, |
ab9ae313 AK |
2664 | u32 access, |
2665 | struct x86_exception *exception) | |
6539e738 | 2666 | { |
ab9ae313 AK |
2667 | if (exception) |
2668 | exception->error_code = 0; | |
6539e738 JR |
2669 | return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access); |
2670 | } | |
2671 | ||
6aa8b732 | 2672 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
78b2c54a | 2673 | u32 error_code, bool prefault) |
6aa8b732 | 2674 | { |
e833240f | 2675 | gfn_t gfn; |
e2dec939 | 2676 | int r; |
6aa8b732 | 2677 | |
b8688d51 | 2678 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
e2dec939 AK |
2679 | r = mmu_topup_memory_caches(vcpu); |
2680 | if (r) | |
2681 | return r; | |
714b93da | 2682 | |
6aa8b732 | 2683 | ASSERT(vcpu); |
ad312c7c | 2684 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 | 2685 | |
e833240f | 2686 | gfn = gva >> PAGE_SHIFT; |
6aa8b732 | 2687 | |
e833240f | 2688 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
78b2c54a | 2689 | error_code & PFERR_WRITE_MASK, gfn, prefault); |
6aa8b732 AK |
2690 | } |
2691 | ||
7e1fbeac | 2692 | static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn) |
af585b92 GN |
2693 | { |
2694 | struct kvm_arch_async_pf arch; | |
fb67e14f | 2695 | |
7c90705b | 2696 | arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; |
af585b92 | 2697 | arch.gfn = gfn; |
c4806acd | 2698 | arch.direct_map = vcpu->arch.mmu.direct_map; |
fb67e14f | 2699 | arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu); |
af585b92 GN |
2700 | |
2701 | return kvm_setup_async_pf(vcpu, gva, gfn, &arch); | |
2702 | } | |
2703 | ||
2704 | static bool can_do_async_pf(struct kvm_vcpu *vcpu) | |
2705 | { | |
2706 | if (unlikely(!irqchip_in_kernel(vcpu->kvm) || | |
2707 | kvm_event_needs_reinjection(vcpu))) | |
2708 | return false; | |
2709 | ||
2710 | return kvm_x86_ops->interrupt_allowed(vcpu); | |
2711 | } | |
2712 | ||
78b2c54a | 2713 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
612819c3 | 2714 | gva_t gva, pfn_t *pfn, bool write, bool *writable) |
af585b92 GN |
2715 | { |
2716 | bool async; | |
2717 | ||
612819c3 | 2718 | *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable); |
af585b92 GN |
2719 | |
2720 | if (!async) | |
2721 | return false; /* *pfn has correct page already */ | |
2722 | ||
2723 | put_page(pfn_to_page(*pfn)); | |
2724 | ||
78b2c54a | 2725 | if (!prefault && can_do_async_pf(vcpu)) { |
c9b263d2 | 2726 | trace_kvm_try_async_get_page(gva, gfn); |
af585b92 GN |
2727 | if (kvm_find_async_pf_gfn(vcpu, gfn)) { |
2728 | trace_kvm_async_pf_doublefault(gva, gfn); | |
2729 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); | |
2730 | return true; | |
2731 | } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn)) | |
2732 | return true; | |
2733 | } | |
2734 | ||
612819c3 | 2735 | *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable); |
af585b92 GN |
2736 | |
2737 | return false; | |
2738 | } | |
2739 | ||
56028d08 | 2740 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code, |
78b2c54a | 2741 | bool prefault) |
fb72d167 | 2742 | { |
35149e21 | 2743 | pfn_t pfn; |
fb72d167 | 2744 | int r; |
852e3c19 | 2745 | int level; |
936a5fe6 | 2746 | int force_pt_level; |
05da4558 | 2747 | gfn_t gfn = gpa >> PAGE_SHIFT; |
e930bffe | 2748 | unsigned long mmu_seq; |
612819c3 MT |
2749 | int write = error_code & PFERR_WRITE_MASK; |
2750 | bool map_writable; | |
fb72d167 JR |
2751 | |
2752 | ASSERT(vcpu); | |
2753 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); | |
2754 | ||
2755 | r = mmu_topup_memory_caches(vcpu); | |
2756 | if (r) | |
2757 | return r; | |
2758 | ||
936a5fe6 AA |
2759 | force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn); |
2760 | if (likely(!force_pt_level)) { | |
2761 | level = mapping_level(vcpu, gfn); | |
2762 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); | |
2763 | } else | |
2764 | level = PT_PAGE_TABLE_LEVEL; | |
852e3c19 | 2765 | |
e930bffe | 2766 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
4c2155ce | 2767 | smp_rmb(); |
af585b92 | 2768 | |
78b2c54a | 2769 | if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable)) |
af585b92 GN |
2770 | return 0; |
2771 | ||
2772 | /* mmio */ | |
bf998156 HY |
2773 | if (is_error_pfn(pfn)) |
2774 | return kvm_handle_bad_page(vcpu->kvm, gfn, pfn); | |
fb72d167 | 2775 | spin_lock(&vcpu->kvm->mmu_lock); |
e930bffe AA |
2776 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
2777 | goto out_unlock; | |
fb72d167 | 2778 | kvm_mmu_free_some_pages(vcpu); |
936a5fe6 AA |
2779 | if (likely(!force_pt_level)) |
2780 | transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level); | |
612819c3 | 2781 | r = __direct_map(vcpu, gpa, write, map_writable, |
2ec4739d | 2782 | level, gfn, pfn, prefault); |
fb72d167 | 2783 | spin_unlock(&vcpu->kvm->mmu_lock); |
fb72d167 JR |
2784 | |
2785 | return r; | |
e930bffe AA |
2786 | |
2787 | out_unlock: | |
2788 | spin_unlock(&vcpu->kvm->mmu_lock); | |
2789 | kvm_release_pfn_clean(pfn); | |
2790 | return 0; | |
fb72d167 JR |
2791 | } |
2792 | ||
6aa8b732 AK |
2793 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
2794 | { | |
17ac10ad | 2795 | mmu_free_roots(vcpu); |
6aa8b732 AK |
2796 | } |
2797 | ||
52fde8df JR |
2798 | static int nonpaging_init_context(struct kvm_vcpu *vcpu, |
2799 | struct kvm_mmu *context) | |
6aa8b732 | 2800 | { |
6aa8b732 AK |
2801 | context->new_cr3 = nonpaging_new_cr3; |
2802 | context->page_fault = nonpaging_page_fault; | |
6aa8b732 AK |
2803 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
2804 | context->free = nonpaging_free; | |
c7addb90 | 2805 | context->prefetch_page = nonpaging_prefetch_page; |
e8bc217a | 2806 | context->sync_page = nonpaging_sync_page; |
a7052897 | 2807 | context->invlpg = nonpaging_invlpg; |
0f53b5b1 | 2808 | context->update_pte = nonpaging_update_pte; |
cea0f0e7 | 2809 | context->root_level = 0; |
6aa8b732 | 2810 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
17c3ba9d | 2811 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 2812 | context->direct_map = true; |
2d48a985 | 2813 | context->nx = false; |
6aa8b732 AK |
2814 | return 0; |
2815 | } | |
2816 | ||
d835dfec | 2817 | void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
6aa8b732 | 2818 | { |
1165f5fe | 2819 | ++vcpu->stat.tlb_flush; |
a8eeb04a | 2820 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
6aa8b732 AK |
2821 | } |
2822 | ||
2823 | static void paging_new_cr3(struct kvm_vcpu *vcpu) | |
2824 | { | |
9f8fe504 | 2825 | pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu)); |
cea0f0e7 | 2826 | mmu_free_roots(vcpu); |
6aa8b732 AK |
2827 | } |
2828 | ||
5777ed34 JR |
2829 | static unsigned long get_cr3(struct kvm_vcpu *vcpu) |
2830 | { | |
9f8fe504 | 2831 | return kvm_read_cr3(vcpu); |
5777ed34 JR |
2832 | } |
2833 | ||
6389ee94 AK |
2834 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
2835 | struct x86_exception *fault) | |
6aa8b732 | 2836 | { |
6389ee94 | 2837 | vcpu->arch.mmu.inject_page_fault(vcpu, fault); |
6aa8b732 AK |
2838 | } |
2839 | ||
6aa8b732 AK |
2840 | static void paging_free(struct kvm_vcpu *vcpu) |
2841 | { | |
2842 | nonpaging_free(vcpu); | |
2843 | } | |
2844 | ||
3241f22d | 2845 | static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level) |
82725b20 DE |
2846 | { |
2847 | int bit7; | |
2848 | ||
2849 | bit7 = (gpte >> 7) & 1; | |
3241f22d | 2850 | return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0; |
82725b20 DE |
2851 | } |
2852 | ||
6aa8b732 AK |
2853 | #define PTTYPE 64 |
2854 | #include "paging_tmpl.h" | |
2855 | #undef PTTYPE | |
2856 | ||
2857 | #define PTTYPE 32 | |
2858 | #include "paging_tmpl.h" | |
2859 | #undef PTTYPE | |
2860 | ||
52fde8df JR |
2861 | static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, |
2862 | struct kvm_mmu *context, | |
2863 | int level) | |
82725b20 | 2864 | { |
82725b20 DE |
2865 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
2866 | u64 exb_bit_rsvd = 0; | |
2867 | ||
2d48a985 | 2868 | if (!context->nx) |
82725b20 DE |
2869 | exb_bit_rsvd = rsvd_bits(63, 63); |
2870 | switch (level) { | |
2871 | case PT32_ROOT_LEVEL: | |
2872 | /* no rsvd bits for 2 level 4K page table entries */ | |
2873 | context->rsvd_bits_mask[0][1] = 0; | |
2874 | context->rsvd_bits_mask[0][0] = 0; | |
f815bce8 XG |
2875 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
2876 | ||
2877 | if (!is_pse(vcpu)) { | |
2878 | context->rsvd_bits_mask[1][1] = 0; | |
2879 | break; | |
2880 | } | |
2881 | ||
82725b20 DE |
2882 | if (is_cpuid_PSE36()) |
2883 | /* 36bits PSE 4MB page */ | |
2884 | context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); | |
2885 | else | |
2886 | /* 32 bits PSE 4MB page */ | |
2887 | context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); | |
82725b20 DE |
2888 | break; |
2889 | case PT32E_ROOT_LEVEL: | |
20c466b5 DE |
2890 | context->rsvd_bits_mask[0][2] = |
2891 | rsvd_bits(maxphyaddr, 63) | | |
2892 | rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */ | |
82725b20 | 2893 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
4c26b4cd | 2894 | rsvd_bits(maxphyaddr, 62); /* PDE */ |
82725b20 DE |
2895 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
2896 | rsvd_bits(maxphyaddr, 62); /* PTE */ | |
2897 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | | |
2898 | rsvd_bits(maxphyaddr, 62) | | |
2899 | rsvd_bits(13, 20); /* large page */ | |
f815bce8 | 2900 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
82725b20 DE |
2901 | break; |
2902 | case PT64_ROOT_LEVEL: | |
2903 | context->rsvd_bits_mask[0][3] = exb_bit_rsvd | | |
2904 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); | |
2905 | context->rsvd_bits_mask[0][2] = exb_bit_rsvd | | |
2906 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); | |
2907 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | | |
4c26b4cd | 2908 | rsvd_bits(maxphyaddr, 51); |
82725b20 DE |
2909 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
2910 | rsvd_bits(maxphyaddr, 51); | |
2911 | context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3]; | |
e04da980 JR |
2912 | context->rsvd_bits_mask[1][2] = exb_bit_rsvd | |
2913 | rsvd_bits(maxphyaddr, 51) | | |
2914 | rsvd_bits(13, 29); | |
82725b20 | 2915 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
4c26b4cd SY |
2916 | rsvd_bits(maxphyaddr, 51) | |
2917 | rsvd_bits(13, 20); /* large page */ | |
f815bce8 | 2918 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
82725b20 DE |
2919 | break; |
2920 | } | |
2921 | } | |
2922 | ||
52fde8df JR |
2923 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, |
2924 | struct kvm_mmu *context, | |
2925 | int level) | |
6aa8b732 | 2926 | { |
2d48a985 JR |
2927 | context->nx = is_nx(vcpu); |
2928 | ||
52fde8df | 2929 | reset_rsvds_bits_mask(vcpu, context, level); |
6aa8b732 AK |
2930 | |
2931 | ASSERT(is_pae(vcpu)); | |
2932 | context->new_cr3 = paging_new_cr3; | |
2933 | context->page_fault = paging64_page_fault; | |
6aa8b732 | 2934 | context->gva_to_gpa = paging64_gva_to_gpa; |
c7addb90 | 2935 | context->prefetch_page = paging64_prefetch_page; |
e8bc217a | 2936 | context->sync_page = paging64_sync_page; |
a7052897 | 2937 | context->invlpg = paging64_invlpg; |
0f53b5b1 | 2938 | context->update_pte = paging64_update_pte; |
6aa8b732 | 2939 | context->free = paging_free; |
17ac10ad AK |
2940 | context->root_level = level; |
2941 | context->shadow_root_level = level; | |
17c3ba9d | 2942 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 2943 | context->direct_map = false; |
6aa8b732 AK |
2944 | return 0; |
2945 | } | |
2946 | ||
52fde8df JR |
2947 | static int paging64_init_context(struct kvm_vcpu *vcpu, |
2948 | struct kvm_mmu *context) | |
17ac10ad | 2949 | { |
52fde8df | 2950 | return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL); |
17ac10ad AK |
2951 | } |
2952 | ||
52fde8df JR |
2953 | static int paging32_init_context(struct kvm_vcpu *vcpu, |
2954 | struct kvm_mmu *context) | |
6aa8b732 | 2955 | { |
2d48a985 JR |
2956 | context->nx = false; |
2957 | ||
52fde8df | 2958 | reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL); |
6aa8b732 AK |
2959 | |
2960 | context->new_cr3 = paging_new_cr3; | |
2961 | context->page_fault = paging32_page_fault; | |
6aa8b732 AK |
2962 | context->gva_to_gpa = paging32_gva_to_gpa; |
2963 | context->free = paging_free; | |
c7addb90 | 2964 | context->prefetch_page = paging32_prefetch_page; |
e8bc217a | 2965 | context->sync_page = paging32_sync_page; |
a7052897 | 2966 | context->invlpg = paging32_invlpg; |
0f53b5b1 | 2967 | context->update_pte = paging32_update_pte; |
6aa8b732 AK |
2968 | context->root_level = PT32_ROOT_LEVEL; |
2969 | context->shadow_root_level = PT32E_ROOT_LEVEL; | |
17c3ba9d | 2970 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 2971 | context->direct_map = false; |
6aa8b732 AK |
2972 | return 0; |
2973 | } | |
2974 | ||
52fde8df JR |
2975 | static int paging32E_init_context(struct kvm_vcpu *vcpu, |
2976 | struct kvm_mmu *context) | |
6aa8b732 | 2977 | { |
52fde8df | 2978 | return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL); |
6aa8b732 AK |
2979 | } |
2980 | ||
fb72d167 JR |
2981 | static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
2982 | { | |
14dfe855 | 2983 | struct kvm_mmu *context = vcpu->arch.walk_mmu; |
fb72d167 | 2984 | |
c445f8ef | 2985 | context->base_role.word = 0; |
fb72d167 JR |
2986 | context->new_cr3 = nonpaging_new_cr3; |
2987 | context->page_fault = tdp_page_fault; | |
2988 | context->free = nonpaging_free; | |
2989 | context->prefetch_page = nonpaging_prefetch_page; | |
e8bc217a | 2990 | context->sync_page = nonpaging_sync_page; |
a7052897 | 2991 | context->invlpg = nonpaging_invlpg; |
0f53b5b1 | 2992 | context->update_pte = nonpaging_update_pte; |
67253af5 | 2993 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); |
fb72d167 | 2994 | context->root_hpa = INVALID_PAGE; |
c5a78f2b | 2995 | context->direct_map = true; |
1c97f0a0 | 2996 | context->set_cr3 = kvm_x86_ops->set_tdp_cr3; |
5777ed34 | 2997 | context->get_cr3 = get_cr3; |
cb659db8 | 2998 | context->inject_page_fault = kvm_inject_page_fault; |
2d48a985 | 2999 | context->nx = is_nx(vcpu); |
fb72d167 JR |
3000 | |
3001 | if (!is_paging(vcpu)) { | |
2d48a985 | 3002 | context->nx = false; |
fb72d167 JR |
3003 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
3004 | context->root_level = 0; | |
3005 | } else if (is_long_mode(vcpu)) { | |
2d48a985 | 3006 | context->nx = is_nx(vcpu); |
52fde8df | 3007 | reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL); |
fb72d167 JR |
3008 | context->gva_to_gpa = paging64_gva_to_gpa; |
3009 | context->root_level = PT64_ROOT_LEVEL; | |
3010 | } else if (is_pae(vcpu)) { | |
2d48a985 | 3011 | context->nx = is_nx(vcpu); |
52fde8df | 3012 | reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL); |
fb72d167 JR |
3013 | context->gva_to_gpa = paging64_gva_to_gpa; |
3014 | context->root_level = PT32E_ROOT_LEVEL; | |
3015 | } else { | |
2d48a985 | 3016 | context->nx = false; |
52fde8df | 3017 | reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL); |
fb72d167 JR |
3018 | context->gva_to_gpa = paging32_gva_to_gpa; |
3019 | context->root_level = PT32_ROOT_LEVEL; | |
3020 | } | |
3021 | ||
3022 | return 0; | |
3023 | } | |
3024 | ||
52fde8df | 3025 | int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context) |
6aa8b732 | 3026 | { |
a770f6f2 | 3027 | int r; |
6aa8b732 | 3028 | ASSERT(vcpu); |
ad312c7c | 3029 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 AK |
3030 | |
3031 | if (!is_paging(vcpu)) | |
52fde8df | 3032 | r = nonpaging_init_context(vcpu, context); |
a9058ecd | 3033 | else if (is_long_mode(vcpu)) |
52fde8df | 3034 | r = paging64_init_context(vcpu, context); |
6aa8b732 | 3035 | else if (is_pae(vcpu)) |
52fde8df | 3036 | r = paging32E_init_context(vcpu, context); |
6aa8b732 | 3037 | else |
52fde8df | 3038 | r = paging32_init_context(vcpu, context); |
a770f6f2 | 3039 | |
5b7e0102 | 3040 | vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu); |
f43addd4 | 3041 | vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu); |
52fde8df JR |
3042 | |
3043 | return r; | |
3044 | } | |
3045 | EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu); | |
3046 | ||
3047 | static int init_kvm_softmmu(struct kvm_vcpu *vcpu) | |
3048 | { | |
14dfe855 | 3049 | int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu); |
52fde8df | 3050 | |
14dfe855 JR |
3051 | vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3; |
3052 | vcpu->arch.walk_mmu->get_cr3 = get_cr3; | |
3053 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; | |
a770f6f2 AK |
3054 | |
3055 | return r; | |
6aa8b732 AK |
3056 | } |
3057 | ||
02f59dc9 JR |
3058 | static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu) |
3059 | { | |
3060 | struct kvm_mmu *g_context = &vcpu->arch.nested_mmu; | |
3061 | ||
3062 | g_context->get_cr3 = get_cr3; | |
3063 | g_context->inject_page_fault = kvm_inject_page_fault; | |
3064 | ||
3065 | /* | |
3066 | * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The | |
3067 | * translation of l2_gpa to l1_gpa addresses is done using the | |
3068 | * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa | |
3069 | * functions between mmu and nested_mmu are swapped. | |
3070 | */ | |
3071 | if (!is_paging(vcpu)) { | |
2d48a985 | 3072 | g_context->nx = false; |
02f59dc9 JR |
3073 | g_context->root_level = 0; |
3074 | g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested; | |
3075 | } else if (is_long_mode(vcpu)) { | |
2d48a985 | 3076 | g_context->nx = is_nx(vcpu); |
02f59dc9 JR |
3077 | reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL); |
3078 | g_context->root_level = PT64_ROOT_LEVEL; | |
3079 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; | |
3080 | } else if (is_pae(vcpu)) { | |
2d48a985 | 3081 | g_context->nx = is_nx(vcpu); |
02f59dc9 JR |
3082 | reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL); |
3083 | g_context->root_level = PT32E_ROOT_LEVEL; | |
3084 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; | |
3085 | } else { | |
2d48a985 | 3086 | g_context->nx = false; |
02f59dc9 JR |
3087 | reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL); |
3088 | g_context->root_level = PT32_ROOT_LEVEL; | |
3089 | g_context->gva_to_gpa = paging32_gva_to_gpa_nested; | |
3090 | } | |
3091 | ||
3092 | return 0; | |
3093 | } | |
3094 | ||
fb72d167 JR |
3095 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
3096 | { | |
02f59dc9 JR |
3097 | if (mmu_is_nested(vcpu)) |
3098 | return init_kvm_nested_mmu(vcpu); | |
3099 | else if (tdp_enabled) | |
fb72d167 JR |
3100 | return init_kvm_tdp_mmu(vcpu); |
3101 | else | |
3102 | return init_kvm_softmmu(vcpu); | |
3103 | } | |
3104 | ||
6aa8b732 AK |
3105 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
3106 | { | |
3107 | ASSERT(vcpu); | |
62ad0755 SY |
3108 | if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
3109 | /* mmu.free() should set root_hpa = INVALID_PAGE */ | |
ad312c7c | 3110 | vcpu->arch.mmu.free(vcpu); |
6aa8b732 AK |
3111 | } |
3112 | ||
3113 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) | |
17c3ba9d AK |
3114 | { |
3115 | destroy_kvm_mmu(vcpu); | |
3116 | return init_kvm_mmu(vcpu); | |
3117 | } | |
8668a3c4 | 3118 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
17c3ba9d AK |
3119 | |
3120 | int kvm_mmu_load(struct kvm_vcpu *vcpu) | |
6aa8b732 | 3121 | { |
714b93da AK |
3122 | int r; |
3123 | ||
e2dec939 | 3124 | r = mmu_topup_memory_caches(vcpu); |
17c3ba9d AK |
3125 | if (r) |
3126 | goto out; | |
8986ecc0 | 3127 | r = mmu_alloc_roots(vcpu); |
8facbbff | 3128 | spin_lock(&vcpu->kvm->mmu_lock); |
0ba73cda | 3129 | mmu_sync_roots(vcpu); |
aaee2c94 | 3130 | spin_unlock(&vcpu->kvm->mmu_lock); |
8986ecc0 MT |
3131 | if (r) |
3132 | goto out; | |
3662cb1c | 3133 | /* set_cr3() should ensure TLB has been flushed */ |
f43addd4 | 3134 | vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
714b93da AK |
3135 | out: |
3136 | return r; | |
6aa8b732 | 3137 | } |
17c3ba9d AK |
3138 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
3139 | ||
3140 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) | |
3141 | { | |
3142 | mmu_free_roots(vcpu); | |
3143 | } | |
4b16184c | 3144 | EXPORT_SYMBOL_GPL(kvm_mmu_unload); |
6aa8b732 | 3145 | |
09072daf | 3146 | static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, |
4db35314 | 3147 | struct kvm_mmu_page *sp, |
ac1b714e AK |
3148 | u64 *spte) |
3149 | { | |
3150 | u64 pte; | |
3151 | struct kvm_mmu_page *child; | |
3152 | ||
3153 | pte = *spte; | |
c7addb90 | 3154 | if (is_shadow_present_pte(pte)) { |
776e6633 | 3155 | if (is_last_spte(pte, sp->role.level)) |
be38d276 | 3156 | drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte); |
ac1b714e AK |
3157 | else { |
3158 | child = page_header(pte & PT64_BASE_ADDR_MASK); | |
90cb0529 | 3159 | mmu_page_remove_parent_pte(child, spte); |
ac1b714e AK |
3160 | } |
3161 | } | |
d555c333 | 3162 | __set_spte(spte, shadow_trap_nonpresent_pte); |
05da4558 MT |
3163 | if (is_large_pte(pte)) |
3164 | --vcpu->kvm->stat.lpages; | |
ac1b714e AK |
3165 | } |
3166 | ||
0028425f | 3167 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
7c562522 XG |
3168 | struct kvm_mmu_page *sp, u64 *spte, |
3169 | const void *new) | |
0028425f | 3170 | { |
30945387 | 3171 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) { |
7e4e4056 JR |
3172 | ++vcpu->kvm->stat.mmu_pde_zapped; |
3173 | return; | |
30945387 | 3174 | } |
0028425f | 3175 | |
4cee5764 | 3176 | ++vcpu->kvm->stat.mmu_pte_updated; |
7c562522 | 3177 | vcpu->arch.mmu.update_pte(vcpu, sp, spte, new); |
0028425f AK |
3178 | } |
3179 | ||
79539cec AK |
3180 | static bool need_remote_flush(u64 old, u64 new) |
3181 | { | |
3182 | if (!is_shadow_present_pte(old)) | |
3183 | return false; | |
3184 | if (!is_shadow_present_pte(new)) | |
3185 | return true; | |
3186 | if ((old ^ new) & PT64_BASE_ADDR_MASK) | |
3187 | return true; | |
3188 | old ^= PT64_NX_MASK; | |
3189 | new ^= PT64_NX_MASK; | |
3190 | return (old & ~new & PT64_PERM_MASK) != 0; | |
3191 | } | |
3192 | ||
0671a8e7 XG |
3193 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page, |
3194 | bool remote_flush, bool local_flush) | |
79539cec | 3195 | { |
0671a8e7 XG |
3196 | if (zap_page) |
3197 | return; | |
3198 | ||
3199 | if (remote_flush) | |
79539cec | 3200 | kvm_flush_remote_tlbs(vcpu->kvm); |
0671a8e7 | 3201 | else if (local_flush) |
79539cec AK |
3202 | kvm_mmu_flush_tlb(vcpu); |
3203 | } | |
3204 | ||
12b7d28f AK |
3205 | static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu) |
3206 | { | |
ad312c7c | 3207 | u64 *spte = vcpu->arch.last_pte_updated; |
12b7d28f | 3208 | |
7b52345e | 3209 | return !!(spte && (*spte & shadow_accessed_mask)); |
12b7d28f AK |
3210 | } |
3211 | ||
1b7fcd32 AK |
3212 | static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
3213 | { | |
3214 | u64 *spte = vcpu->arch.last_pte_updated; | |
3215 | ||
3216 | if (spte | |
3217 | && vcpu->arch.last_pte_gfn == gfn | |
3218 | && shadow_accessed_mask | |
3219 | && !(*spte & shadow_accessed_mask) | |
3220 | && is_shadow_present_pte(*spte)) | |
3221 | set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); | |
3222 | } | |
3223 | ||
09072daf | 3224 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
ad218f85 MT |
3225 | const u8 *new, int bytes, |
3226 | bool guest_initiated) | |
da4a00f0 | 3227 | { |
9b7a0325 | 3228 | gfn_t gfn = gpa >> PAGE_SHIFT; |
fa1de2bf | 3229 | union kvm_mmu_page_role mask = { .word = 0 }; |
4db35314 | 3230 | struct kvm_mmu_page *sp; |
f41d335a | 3231 | struct hlist_node *node; |
d98ba053 | 3232 | LIST_HEAD(invalid_list); |
0f53b5b1 XG |
3233 | u64 entry, gentry, *spte; |
3234 | unsigned pte_size, page_offset, misaligned, quadrant, offset; | |
3235 | int level, npte, invlpg_counter, r, flooded = 0; | |
0671a8e7 XG |
3236 | bool remote_flush, local_flush, zap_page; |
3237 | ||
332b207d XG |
3238 | /* |
3239 | * If we don't have indirect shadow pages, it means no page is | |
3240 | * write-protected, so we can exit simply. | |
3241 | */ | |
3242 | if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages)) | |
3243 | return; | |
3244 | ||
0671a8e7 | 3245 | zap_page = remote_flush = local_flush = false; |
0f53b5b1 | 3246 | offset = offset_in_page(gpa); |
9b7a0325 | 3247 | |
b8688d51 | 3248 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
72016f3a | 3249 | |
08e850c6 | 3250 | invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter); |
72016f3a AK |
3251 | |
3252 | /* | |
3253 | * Assume that the pte write on a page table of the same type | |
49b26e26 XG |
3254 | * as the current vcpu paging mode since we update the sptes only |
3255 | * when they have the same mode. | |
72016f3a | 3256 | */ |
08e850c6 | 3257 | if ((is_pae(vcpu) && bytes == 4) || !new) { |
72016f3a | 3258 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
08e850c6 AK |
3259 | if (is_pae(vcpu)) { |
3260 | gpa &= ~(gpa_t)7; | |
3261 | bytes = 8; | |
3262 | } | |
3263 | r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8)); | |
72016f3a AK |
3264 | if (r) |
3265 | gentry = 0; | |
08e850c6 AK |
3266 | new = (const u8 *)&gentry; |
3267 | } | |
3268 | ||
3269 | switch (bytes) { | |
3270 | case 4: | |
3271 | gentry = *(const u32 *)new; | |
3272 | break; | |
3273 | case 8: | |
3274 | gentry = *(const u64 *)new; | |
3275 | break; | |
3276 | default: | |
3277 | gentry = 0; | |
3278 | break; | |
72016f3a AK |
3279 | } |
3280 | ||
aaee2c94 | 3281 | spin_lock(&vcpu->kvm->mmu_lock); |
08e850c6 AK |
3282 | if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) |
3283 | gentry = 0; | |
eb787d10 | 3284 | kvm_mmu_free_some_pages(vcpu); |
4cee5764 | 3285 | ++vcpu->kvm->stat.mmu_pte_write; |
8b1fe17c | 3286 | trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE); |
ad218f85 | 3287 | if (guest_initiated) { |
1b7fd45c | 3288 | kvm_mmu_access_page(vcpu, gfn); |
ad218f85 MT |
3289 | if (gfn == vcpu->arch.last_pt_write_gfn |
3290 | && !last_updated_pte_accessed(vcpu)) { | |
3291 | ++vcpu->arch.last_pt_write_count; | |
3292 | if (vcpu->arch.last_pt_write_count >= 3) | |
3293 | flooded = 1; | |
3294 | } else { | |
3295 | vcpu->arch.last_pt_write_gfn = gfn; | |
3296 | vcpu->arch.last_pt_write_count = 1; | |
3297 | vcpu->arch.last_pte_updated = NULL; | |
3298 | } | |
86a5ba02 | 3299 | } |
3246af0e | 3300 | |
fa1de2bf | 3301 | mask.cr0_wp = mask.cr4_pae = mask.nxe = 1; |
f41d335a | 3302 | for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) { |
5b7e0102 | 3303 | pte_size = sp->role.cr4_pae ? 8 : 4; |
0e7bc4b9 | 3304 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
e925c5ba | 3305 | misaligned |= bytes < 4; |
86a5ba02 | 3306 | if (misaligned || flooded) { |
0e7bc4b9 AK |
3307 | /* |
3308 | * Misaligned accesses are too much trouble to fix | |
3309 | * up; also, they usually indicate a page is not used | |
3310 | * as a page table. | |
86a5ba02 AK |
3311 | * |
3312 | * If we're seeing too many writes to a page, | |
3313 | * it may no longer be a page table, or we may be | |
3314 | * forking, in which case it is better to unmap the | |
3315 | * page. | |
0e7bc4b9 AK |
3316 | */ |
3317 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", | |
4db35314 | 3318 | gpa, bytes, sp->role.word); |
0671a8e7 | 3319 | zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
f41d335a | 3320 | &invalid_list); |
4cee5764 | 3321 | ++vcpu->kvm->stat.mmu_flooded; |
0e7bc4b9 AK |
3322 | continue; |
3323 | } | |
9b7a0325 | 3324 | page_offset = offset; |
4db35314 | 3325 | level = sp->role.level; |
ac1b714e | 3326 | npte = 1; |
5b7e0102 | 3327 | if (!sp->role.cr4_pae) { |
ac1b714e AK |
3328 | page_offset <<= 1; /* 32->64 */ |
3329 | /* | |
3330 | * A 32-bit pde maps 4MB while the shadow pdes map | |
3331 | * only 2MB. So we need to double the offset again | |
3332 | * and zap two pdes instead of one. | |
3333 | */ | |
3334 | if (level == PT32_ROOT_LEVEL) { | |
6b8d0f9b | 3335 | page_offset &= ~7; /* kill rounding error */ |
ac1b714e AK |
3336 | page_offset <<= 1; |
3337 | npte = 2; | |
3338 | } | |
fce0657f | 3339 | quadrant = page_offset >> PAGE_SHIFT; |
9b7a0325 | 3340 | page_offset &= ~PAGE_MASK; |
4db35314 | 3341 | if (quadrant != sp->role.quadrant) |
fce0657f | 3342 | continue; |
9b7a0325 | 3343 | } |
0671a8e7 | 3344 | local_flush = true; |
4db35314 | 3345 | spte = &sp->spt[page_offset / sizeof(*spte)]; |
ac1b714e | 3346 | while (npte--) { |
79539cec | 3347 | entry = *spte; |
4db35314 | 3348 | mmu_pte_write_zap_pte(vcpu, sp, spte); |
fa1de2bf XG |
3349 | if (gentry && |
3350 | !((sp->role.word ^ vcpu->arch.mmu.base_role.word) | |
3351 | & mask.word)) | |
7c562522 | 3352 | mmu_pte_write_new_pte(vcpu, sp, spte, &gentry); |
0671a8e7 XG |
3353 | if (!remote_flush && need_remote_flush(entry, *spte)) |
3354 | remote_flush = true; | |
ac1b714e | 3355 | ++spte; |
9b7a0325 | 3356 | } |
9b7a0325 | 3357 | } |
0671a8e7 | 3358 | mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush); |
d98ba053 | 3359 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
8b1fe17c | 3360 | trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE); |
aaee2c94 | 3361 | spin_unlock(&vcpu->kvm->mmu_lock); |
da4a00f0 AK |
3362 | } |
3363 | ||
a436036b AK |
3364 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
3365 | { | |
10589a46 MT |
3366 | gpa_t gpa; |
3367 | int r; | |
a436036b | 3368 | |
c5a78f2b | 3369 | if (vcpu->arch.mmu.direct_map) |
60f24784 AK |
3370 | return 0; |
3371 | ||
1871c602 | 3372 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL); |
10589a46 | 3373 | |
aaee2c94 | 3374 | spin_lock(&vcpu->kvm->mmu_lock); |
10589a46 | 3375 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
aaee2c94 | 3376 | spin_unlock(&vcpu->kvm->mmu_lock); |
10589a46 | 3377 | return r; |
a436036b | 3378 | } |
577bdc49 | 3379 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt); |
a436036b | 3380 | |
22d95b12 | 3381 | void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
ebeace86 | 3382 | { |
d98ba053 | 3383 | LIST_HEAD(invalid_list); |
103ad25a | 3384 | |
e0df7b9f | 3385 | while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES && |
3b80fffe | 3386 | !list_empty(&vcpu->kvm->arch.active_mmu_pages)) { |
4db35314 | 3387 | struct kvm_mmu_page *sp; |
ebeace86 | 3388 | |
f05e70ac | 3389 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev, |
4db35314 | 3390 | struct kvm_mmu_page, link); |
e0df7b9f | 3391 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); |
80b63faf | 3392 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
4cee5764 | 3393 | ++vcpu->kvm->stat.mmu_recycled; |
ebeace86 AK |
3394 | } |
3395 | } | |
ebeace86 | 3396 | |
dc25e89e AP |
3397 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code, |
3398 | void *insn, int insn_len) | |
3067714c AK |
3399 | { |
3400 | int r; | |
3401 | enum emulation_result er; | |
3402 | ||
56028d08 | 3403 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false); |
3067714c AK |
3404 | if (r < 0) |
3405 | goto out; | |
3406 | ||
3407 | if (!r) { | |
3408 | r = 1; | |
3409 | goto out; | |
3410 | } | |
3411 | ||
b733bfb5 AK |
3412 | r = mmu_topup_memory_caches(vcpu); |
3413 | if (r) | |
3414 | goto out; | |
3415 | ||
dc25e89e | 3416 | er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len); |
3067714c AK |
3417 | |
3418 | switch (er) { | |
3419 | case EMULATE_DONE: | |
3420 | return 1; | |
3421 | case EMULATE_DO_MMIO: | |
3422 | ++vcpu->stat.mmio_exits; | |
6d77dbfc | 3423 | /* fall through */ |
3067714c | 3424 | case EMULATE_FAIL: |
3f5d18a9 | 3425 | return 0; |
3067714c AK |
3426 | default: |
3427 | BUG(); | |
3428 | } | |
3429 | out: | |
3067714c AK |
3430 | return r; |
3431 | } | |
3432 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); | |
3433 | ||
a7052897 MT |
3434 | void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
3435 | { | |
a7052897 | 3436 | vcpu->arch.mmu.invlpg(vcpu, gva); |
a7052897 MT |
3437 | kvm_mmu_flush_tlb(vcpu); |
3438 | ++vcpu->stat.invlpg; | |
3439 | } | |
3440 | EXPORT_SYMBOL_GPL(kvm_mmu_invlpg); | |
3441 | ||
18552672 JR |
3442 | void kvm_enable_tdp(void) |
3443 | { | |
3444 | tdp_enabled = true; | |
3445 | } | |
3446 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); | |
3447 | ||
5f4cb662 JR |
3448 | void kvm_disable_tdp(void) |
3449 | { | |
3450 | tdp_enabled = false; | |
3451 | } | |
3452 | EXPORT_SYMBOL_GPL(kvm_disable_tdp); | |
3453 | ||
6aa8b732 AK |
3454 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
3455 | { | |
ad312c7c | 3456 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
81407ca5 JR |
3457 | if (vcpu->arch.mmu.lm_root != NULL) |
3458 | free_page((unsigned long)vcpu->arch.mmu.lm_root); | |
6aa8b732 AK |
3459 | } |
3460 | ||
3461 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) | |
3462 | { | |
17ac10ad | 3463 | struct page *page; |
6aa8b732 AK |
3464 | int i; |
3465 | ||
3466 | ASSERT(vcpu); | |
3467 | ||
17ac10ad AK |
3468 | /* |
3469 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. | |
3470 | * Therefore we need to allocate shadow page tables in the first | |
3471 | * 4GB of memory, which happens to fit the DMA32 zone. | |
3472 | */ | |
3473 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); | |
3474 | if (!page) | |
d7fa6ab2 WY |
3475 | return -ENOMEM; |
3476 | ||
ad312c7c | 3477 | vcpu->arch.mmu.pae_root = page_address(page); |
17ac10ad | 3478 | for (i = 0; i < 4; ++i) |
ad312c7c | 3479 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
17ac10ad | 3480 | |
6aa8b732 | 3481 | return 0; |
6aa8b732 AK |
3482 | } |
3483 | ||
8018c27b | 3484 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
6aa8b732 | 3485 | { |
6aa8b732 | 3486 | ASSERT(vcpu); |
ad312c7c | 3487 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 | 3488 | |
8018c27b IM |
3489 | return alloc_mmu_pages(vcpu); |
3490 | } | |
6aa8b732 | 3491 | |
8018c27b IM |
3492 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
3493 | { | |
3494 | ASSERT(vcpu); | |
ad312c7c | 3495 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
2c264957 | 3496 | |
8018c27b | 3497 | return init_kvm_mmu(vcpu); |
6aa8b732 AK |
3498 | } |
3499 | ||
90cb0529 | 3500 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
6aa8b732 | 3501 | { |
4db35314 | 3502 | struct kvm_mmu_page *sp; |
6aa8b732 | 3503 | |
f05e70ac | 3504 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
6aa8b732 AK |
3505 | int i; |
3506 | u64 *pt; | |
3507 | ||
291f26bc | 3508 | if (!test_bit(slot, sp->slot_bitmap)) |
6aa8b732 AK |
3509 | continue; |
3510 | ||
4db35314 | 3511 | pt = sp->spt; |
8234b22e | 3512 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
da8dc75f XG |
3513 | if (!is_shadow_present_pte(pt[i]) || |
3514 | !is_last_spte(pt[i], sp->role.level)) | |
3515 | continue; | |
3516 | ||
3517 | if (is_large_pte(pt[i])) { | |
8234b22e AK |
3518 | drop_spte(kvm, &pt[i], |
3519 | shadow_trap_nonpresent_pte); | |
3520 | --kvm->stat.lpages; | |
da8dc75f | 3521 | continue; |
8234b22e | 3522 | } |
da8dc75f | 3523 | |
6aa8b732 | 3524 | /* avoid RMW */ |
01c168ac | 3525 | if (is_writable_pte(pt[i])) |
700e1b12 | 3526 | update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK); |
8234b22e | 3527 | } |
6aa8b732 | 3528 | } |
171d595d | 3529 | kvm_flush_remote_tlbs(kvm); |
6aa8b732 | 3530 | } |
37a7d8b0 | 3531 | |
90cb0529 | 3532 | void kvm_mmu_zap_all(struct kvm *kvm) |
e0fa826f | 3533 | { |
4db35314 | 3534 | struct kvm_mmu_page *sp, *node; |
d98ba053 | 3535 | LIST_HEAD(invalid_list); |
e0fa826f | 3536 | |
aaee2c94 | 3537 | spin_lock(&kvm->mmu_lock); |
3246af0e | 3538 | restart: |
f05e70ac | 3539 | list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) |
d98ba053 | 3540 | if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list)) |
3246af0e XG |
3541 | goto restart; |
3542 | ||
d98ba053 | 3543 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
aaee2c94 | 3544 | spin_unlock(&kvm->mmu_lock); |
e0fa826f DL |
3545 | } |
3546 | ||
d98ba053 XG |
3547 | static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm, |
3548 | struct list_head *invalid_list) | |
3ee16c81 IE |
3549 | { |
3550 | struct kvm_mmu_page *page; | |
3551 | ||
3552 | page = container_of(kvm->arch.active_mmu_pages.prev, | |
3553 | struct kvm_mmu_page, link); | |
d98ba053 | 3554 | return kvm_mmu_prepare_zap_page(kvm, page, invalid_list); |
3ee16c81 IE |
3555 | } |
3556 | ||
1495f230 | 3557 | static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc) |
3ee16c81 IE |
3558 | { |
3559 | struct kvm *kvm; | |
3560 | struct kvm *kvm_freed = NULL; | |
1495f230 | 3561 | int nr_to_scan = sc->nr_to_scan; |
45221ab6 DH |
3562 | |
3563 | if (nr_to_scan == 0) | |
3564 | goto out; | |
3ee16c81 | 3565 | |
e935b837 | 3566 | raw_spin_lock(&kvm_lock); |
3ee16c81 IE |
3567 | |
3568 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
45221ab6 | 3569 | int idx, freed_pages; |
d98ba053 | 3570 | LIST_HEAD(invalid_list); |
3ee16c81 | 3571 | |
f656ce01 | 3572 | idx = srcu_read_lock(&kvm->srcu); |
3ee16c81 | 3573 | spin_lock(&kvm->mmu_lock); |
45221ab6 DH |
3574 | if (!kvm_freed && nr_to_scan > 0 && |
3575 | kvm->arch.n_used_mmu_pages > 0) { | |
d98ba053 XG |
3576 | freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm, |
3577 | &invalid_list); | |
3ee16c81 IE |
3578 | kvm_freed = kvm; |
3579 | } | |
3580 | nr_to_scan--; | |
3581 | ||
d98ba053 | 3582 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
3ee16c81 | 3583 | spin_unlock(&kvm->mmu_lock); |
f656ce01 | 3584 | srcu_read_unlock(&kvm->srcu, idx); |
3ee16c81 IE |
3585 | } |
3586 | if (kvm_freed) | |
3587 | list_move_tail(&kvm_freed->vm_list, &vm_list); | |
3588 | ||
e935b837 | 3589 | raw_spin_unlock(&kvm_lock); |
3ee16c81 | 3590 | |
45221ab6 DH |
3591 | out: |
3592 | return percpu_counter_read_positive(&kvm_total_used_mmu_pages); | |
3ee16c81 IE |
3593 | } |
3594 | ||
3595 | static struct shrinker mmu_shrinker = { | |
3596 | .shrink = mmu_shrink, | |
3597 | .seeks = DEFAULT_SEEKS * 10, | |
3598 | }; | |
3599 | ||
2ddfd20e | 3600 | static void mmu_destroy_caches(void) |
b5a33a75 AK |
3601 | { |
3602 | if (pte_chain_cache) | |
3603 | kmem_cache_destroy(pte_chain_cache); | |
3604 | if (rmap_desc_cache) | |
3605 | kmem_cache_destroy(rmap_desc_cache); | |
d3d25b04 AK |
3606 | if (mmu_page_header_cache) |
3607 | kmem_cache_destroy(mmu_page_header_cache); | |
b5a33a75 AK |
3608 | } |
3609 | ||
3610 | int kvm_mmu_module_init(void) | |
3611 | { | |
3612 | pte_chain_cache = kmem_cache_create("kvm_pte_chain", | |
3613 | sizeof(struct kvm_pte_chain), | |
20c2df83 | 3614 | 0, 0, NULL); |
b5a33a75 AK |
3615 | if (!pte_chain_cache) |
3616 | goto nomem; | |
3617 | rmap_desc_cache = kmem_cache_create("kvm_rmap_desc", | |
3618 | sizeof(struct kvm_rmap_desc), | |
20c2df83 | 3619 | 0, 0, NULL); |
b5a33a75 AK |
3620 | if (!rmap_desc_cache) |
3621 | goto nomem; | |
3622 | ||
d3d25b04 AK |
3623 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
3624 | sizeof(struct kvm_mmu_page), | |
20c2df83 | 3625 | 0, 0, NULL); |
d3d25b04 AK |
3626 | if (!mmu_page_header_cache) |
3627 | goto nomem; | |
3628 | ||
45bf21a8 WY |
3629 | if (percpu_counter_init(&kvm_total_used_mmu_pages, 0)) |
3630 | goto nomem; | |
3631 | ||
3ee16c81 IE |
3632 | register_shrinker(&mmu_shrinker); |
3633 | ||
b5a33a75 AK |
3634 | return 0; |
3635 | ||
3636 | nomem: | |
3ee16c81 | 3637 | mmu_destroy_caches(); |
b5a33a75 AK |
3638 | return -ENOMEM; |
3639 | } | |
3640 | ||
3ad82a7e ZX |
3641 | /* |
3642 | * Caculate mmu pages needed for kvm. | |
3643 | */ | |
3644 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) | |
3645 | { | |
3646 | int i; | |
3647 | unsigned int nr_mmu_pages; | |
3648 | unsigned int nr_pages = 0; | |
bc6678a3 | 3649 | struct kvm_memslots *slots; |
3ad82a7e | 3650 | |
90d83dc3 LJ |
3651 | slots = kvm_memslots(kvm); |
3652 | ||
bc6678a3 MT |
3653 | for (i = 0; i < slots->nmemslots; i++) |
3654 | nr_pages += slots->memslots[i].npages; | |
3ad82a7e ZX |
3655 | |
3656 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; | |
3657 | nr_mmu_pages = max(nr_mmu_pages, | |
3658 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); | |
3659 | ||
3660 | return nr_mmu_pages; | |
3661 | } | |
3662 | ||
2f333bcb MT |
3663 | static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
3664 | unsigned len) | |
3665 | { | |
3666 | if (len > buffer->len) | |
3667 | return NULL; | |
3668 | return buffer->ptr; | |
3669 | } | |
3670 | ||
3671 | static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer, | |
3672 | unsigned len) | |
3673 | { | |
3674 | void *ret; | |
3675 | ||
3676 | ret = pv_mmu_peek_buffer(buffer, len); | |
3677 | if (!ret) | |
3678 | return ret; | |
3679 | buffer->ptr += len; | |
3680 | buffer->len -= len; | |
3681 | buffer->processed += len; | |
3682 | return ret; | |
3683 | } | |
3684 | ||
3685 | static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu, | |
3686 | gpa_t addr, gpa_t value) | |
3687 | { | |
3688 | int bytes = 8; | |
3689 | int r; | |
3690 | ||
3691 | if (!is_long_mode(vcpu) && !is_pae(vcpu)) | |
3692 | bytes = 4; | |
3693 | ||
3694 | r = mmu_topup_memory_caches(vcpu); | |
3695 | if (r) | |
3696 | return r; | |
3697 | ||
3200f405 | 3698 | if (!emulator_write_phys(vcpu, addr, &value, bytes)) |
2f333bcb MT |
3699 | return -EFAULT; |
3700 | ||
3701 | return 1; | |
3702 | } | |
3703 | ||
3704 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) | |
3705 | { | |
9f8fe504 | 3706 | (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu)); |
2f333bcb MT |
3707 | return 1; |
3708 | } | |
3709 | ||
3710 | static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr) | |
3711 | { | |
3712 | spin_lock(&vcpu->kvm->mmu_lock); | |
3713 | mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT); | |
3714 | spin_unlock(&vcpu->kvm->mmu_lock); | |
3715 | return 1; | |
3716 | } | |
3717 | ||
3718 | static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu, | |
3719 | struct kvm_pv_mmu_op_buffer *buffer) | |
3720 | { | |
3721 | struct kvm_mmu_op_header *header; | |
3722 | ||
3723 | header = pv_mmu_peek_buffer(buffer, sizeof *header); | |
3724 | if (!header) | |
3725 | return 0; | |
3726 | switch (header->op) { | |
3727 | case KVM_MMU_OP_WRITE_PTE: { | |
3728 | struct kvm_mmu_op_write_pte *wpte; | |
3729 | ||
3730 | wpte = pv_mmu_read_buffer(buffer, sizeof *wpte); | |
3731 | if (!wpte) | |
3732 | return 0; | |
3733 | return kvm_pv_mmu_write(vcpu, wpte->pte_phys, | |
3734 | wpte->pte_val); | |
3735 | } | |
3736 | case KVM_MMU_OP_FLUSH_TLB: { | |
3737 | struct kvm_mmu_op_flush_tlb *ftlb; | |
3738 | ||
3739 | ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb); | |
3740 | if (!ftlb) | |
3741 | return 0; | |
3742 | return kvm_pv_mmu_flush_tlb(vcpu); | |
3743 | } | |
3744 | case KVM_MMU_OP_RELEASE_PT: { | |
3745 | struct kvm_mmu_op_release_pt *rpt; | |
3746 | ||
3747 | rpt = pv_mmu_read_buffer(buffer, sizeof *rpt); | |
3748 | if (!rpt) | |
3749 | return 0; | |
3750 | return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys); | |
3751 | } | |
3752 | default: return 0; | |
3753 | } | |
3754 | } | |
3755 | ||
3756 | int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes, | |
3757 | gpa_t addr, unsigned long *ret) | |
3758 | { | |
3759 | int r; | |
6ad18fba | 3760 | struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer; |
2f333bcb | 3761 | |
6ad18fba DH |
3762 | buffer->ptr = buffer->buf; |
3763 | buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf); | |
3764 | buffer->processed = 0; | |
2f333bcb | 3765 | |
6ad18fba | 3766 | r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len); |
2f333bcb MT |
3767 | if (r) |
3768 | goto out; | |
3769 | ||
6ad18fba DH |
3770 | while (buffer->len) { |
3771 | r = kvm_pv_mmu_op_one(vcpu, buffer); | |
2f333bcb MT |
3772 | if (r < 0) |
3773 | goto out; | |
3774 | if (r == 0) | |
3775 | break; | |
3776 | } | |
3777 | ||
3778 | r = 1; | |
3779 | out: | |
6ad18fba | 3780 | *ret = buffer->processed; |
2f333bcb MT |
3781 | return r; |
3782 | } | |
3783 | ||
94d8b056 MT |
3784 | int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4]) |
3785 | { | |
3786 | struct kvm_shadow_walk_iterator iterator; | |
3787 | int nr_sptes = 0; | |
3788 | ||
3789 | spin_lock(&vcpu->kvm->mmu_lock); | |
3790 | for_each_shadow_entry(vcpu, addr, iterator) { | |
3791 | sptes[iterator.level-1] = *iterator.sptep; | |
3792 | nr_sptes++; | |
3793 | if (!is_shadow_present_pte(*iterator.sptep)) | |
3794 | break; | |
3795 | } | |
3796 | spin_unlock(&vcpu->kvm->mmu_lock); | |
3797 | ||
3798 | return nr_sptes; | |
3799 | } | |
3800 | EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy); | |
3801 | ||
c42fffe3 XG |
3802 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
3803 | { | |
3804 | ASSERT(vcpu); | |
3805 | ||
3806 | destroy_kvm_mmu(vcpu); | |
3807 | free_mmu_pages(vcpu); | |
3808 | mmu_free_memory_caches(vcpu); | |
b034cf01 XG |
3809 | } |
3810 | ||
3811 | #ifdef CONFIG_KVM_MMU_AUDIT | |
3812 | #include "mmu_audit.c" | |
3813 | #else | |
3814 | static void mmu_audit_disable(void) { } | |
3815 | #endif | |
3816 | ||
3817 | void kvm_mmu_module_exit(void) | |
3818 | { | |
3819 | mmu_destroy_caches(); | |
3820 | percpu_counter_destroy(&kvm_total_used_mmu_pages); | |
3821 | unregister_shrinker(&mmu_shrinker); | |
c42fffe3 XG |
3822 | mmu_audit_disable(); |
3823 | } |