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