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. | |
10 | * | |
11 | * Authors: | |
12 | * Yaniv Kamay <yaniv@qumranet.com> | |
13 | * Avi Kivity <avi@qumranet.com> | |
14 | * | |
15 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
16 | * the COPYING file in the top-level directory. | |
17 | * | |
18 | */ | |
e495606d AK |
19 | |
20 | #include "vmx.h" | |
1d737c8a | 21 | #include "mmu.h" |
e495606d | 22 | |
edf88417 | 23 | #include <linux/kvm_host.h> |
6aa8b732 AK |
24 | #include <linux/types.h> |
25 | #include <linux/string.h> | |
6aa8b732 AK |
26 | #include <linux/mm.h> |
27 | #include <linux/highmem.h> | |
28 | #include <linux/module.h> | |
448353ca | 29 | #include <linux/swap.h> |
05da4558 | 30 | #include <linux/hugetlb.h> |
2f333bcb | 31 | #include <linux/compiler.h> |
6aa8b732 | 32 | |
e495606d AK |
33 | #include <asm/page.h> |
34 | #include <asm/cmpxchg.h> | |
4e542370 | 35 | #include <asm/io.h> |
6aa8b732 | 36 | |
18552672 JR |
37 | /* |
38 | * When setting this variable to true it enables Two-Dimensional-Paging | |
39 | * where the hardware walks 2 page tables: | |
40 | * 1. the guest-virtual to guest-physical | |
41 | * 2. while doing 1. it walks guest-physical to host-physical | |
42 | * If the hardware supports that we don't need to do shadow paging. | |
43 | */ | |
2f333bcb | 44 | bool tdp_enabled = false; |
18552672 | 45 | |
37a7d8b0 AK |
46 | #undef MMU_DEBUG |
47 | ||
48 | #undef AUDIT | |
49 | ||
50 | #ifdef AUDIT | |
51 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg); | |
52 | #else | |
53 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {} | |
54 | #endif | |
55 | ||
56 | #ifdef MMU_DEBUG | |
57 | ||
58 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) | |
59 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) | |
60 | ||
61 | #else | |
62 | ||
63 | #define pgprintk(x...) do { } while (0) | |
64 | #define rmap_printk(x...) do { } while (0) | |
65 | ||
66 | #endif | |
67 | ||
68 | #if defined(MMU_DEBUG) || defined(AUDIT) | |
69 | static int dbg = 1; | |
70 | #endif | |
6aa8b732 | 71 | |
d6c69ee9 YD |
72 | #ifndef MMU_DEBUG |
73 | #define ASSERT(x) do { } while (0) | |
74 | #else | |
6aa8b732 AK |
75 | #define ASSERT(x) \ |
76 | if (!(x)) { \ | |
77 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ | |
78 | __FILE__, __LINE__, #x); \ | |
79 | } | |
d6c69ee9 | 80 | #endif |
6aa8b732 | 81 | |
cea0f0e7 AK |
82 | #define PT64_PT_BITS 9 |
83 | #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS) | |
84 | #define PT32_PT_BITS 10 | |
85 | #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS) | |
6aa8b732 AK |
86 | |
87 | #define PT_WRITABLE_SHIFT 1 | |
88 | ||
89 | #define PT_PRESENT_MASK (1ULL << 0) | |
90 | #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT) | |
91 | #define PT_USER_MASK (1ULL << 2) | |
92 | #define PT_PWT_MASK (1ULL << 3) | |
93 | #define PT_PCD_MASK (1ULL << 4) | |
94 | #define PT_ACCESSED_MASK (1ULL << 5) | |
95 | #define PT_DIRTY_MASK (1ULL << 6) | |
96 | #define PT_PAGE_SIZE_MASK (1ULL << 7) | |
97 | #define PT_PAT_MASK (1ULL << 7) | |
98 | #define PT_GLOBAL_MASK (1ULL << 8) | |
fe135d2c AK |
99 | #define PT64_NX_SHIFT 63 |
100 | #define PT64_NX_MASK (1ULL << PT64_NX_SHIFT) | |
6aa8b732 AK |
101 | |
102 | #define PT_PAT_SHIFT 7 | |
103 | #define PT_DIR_PAT_SHIFT 12 | |
104 | #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT) | |
105 | ||
106 | #define PT32_DIR_PSE36_SIZE 4 | |
107 | #define PT32_DIR_PSE36_SHIFT 13 | |
d77c26fc MD |
108 | #define PT32_DIR_PSE36_MASK \ |
109 | (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT) | |
6aa8b732 AK |
110 | |
111 | ||
6aa8b732 AK |
112 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
113 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 | |
114 | ||
6aa8b732 AK |
115 | #define VALID_PAGE(x) ((x) != INVALID_PAGE) |
116 | ||
117 | #define PT64_LEVEL_BITS 9 | |
118 | ||
119 | #define PT64_LEVEL_SHIFT(level) \ | |
d77c26fc | 120 | (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS) |
6aa8b732 AK |
121 | |
122 | #define PT64_LEVEL_MASK(level) \ | |
123 | (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level)) | |
124 | ||
125 | #define PT64_INDEX(address, level)\ | |
126 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) | |
127 | ||
128 | ||
129 | #define PT32_LEVEL_BITS 10 | |
130 | ||
131 | #define PT32_LEVEL_SHIFT(level) \ | |
d77c26fc | 132 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
6aa8b732 AK |
133 | |
134 | #define PT32_LEVEL_MASK(level) \ | |
135 | (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level)) | |
136 | ||
137 | #define PT32_INDEX(address, level)\ | |
138 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) | |
139 | ||
140 | ||
27aba766 | 141 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
6aa8b732 AK |
142 | #define PT64_DIR_BASE_ADDR_MASK \ |
143 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) | |
144 | ||
145 | #define PT32_BASE_ADDR_MASK PAGE_MASK | |
146 | #define PT32_DIR_BASE_ADDR_MASK \ | |
147 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) | |
148 | ||
79539cec AK |
149 | #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \ |
150 | | PT64_NX_MASK) | |
6aa8b732 AK |
151 | |
152 | #define PFERR_PRESENT_MASK (1U << 0) | |
153 | #define PFERR_WRITE_MASK (1U << 1) | |
154 | #define PFERR_USER_MASK (1U << 2) | |
73b1087e | 155 | #define PFERR_FETCH_MASK (1U << 4) |
6aa8b732 AK |
156 | |
157 | #define PT64_ROOT_LEVEL 4 | |
158 | #define PT32_ROOT_LEVEL 2 | |
159 | #define PT32E_ROOT_LEVEL 3 | |
160 | ||
161 | #define PT_DIRECTORY_LEVEL 2 | |
162 | #define PT_PAGE_TABLE_LEVEL 1 | |
163 | ||
cd4a4e53 AK |
164 | #define RMAP_EXT 4 |
165 | ||
fe135d2c AK |
166 | #define ACC_EXEC_MASK 1 |
167 | #define ACC_WRITE_MASK PT_WRITABLE_MASK | |
168 | #define ACC_USER_MASK PT_USER_MASK | |
169 | #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) | |
170 | ||
2f333bcb MT |
171 | struct kvm_pv_mmu_op_buffer { |
172 | void *ptr; | |
173 | unsigned len; | |
174 | unsigned processed; | |
175 | char buf[512] __aligned(sizeof(long)); | |
176 | }; | |
177 | ||
cd4a4e53 AK |
178 | struct kvm_rmap_desc { |
179 | u64 *shadow_ptes[RMAP_EXT]; | |
180 | struct kvm_rmap_desc *more; | |
181 | }; | |
182 | ||
b5a33a75 AK |
183 | static struct kmem_cache *pte_chain_cache; |
184 | static struct kmem_cache *rmap_desc_cache; | |
d3d25b04 | 185 | static struct kmem_cache *mmu_page_header_cache; |
b5a33a75 | 186 | |
c7addb90 AK |
187 | static u64 __read_mostly shadow_trap_nonpresent_pte; |
188 | static u64 __read_mostly shadow_notrap_nonpresent_pte; | |
189 | ||
190 | void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte) | |
191 | { | |
192 | shadow_trap_nonpresent_pte = trap_pte; | |
193 | shadow_notrap_nonpresent_pte = notrap_pte; | |
194 | } | |
195 | EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes); | |
196 | ||
6aa8b732 AK |
197 | static int is_write_protection(struct kvm_vcpu *vcpu) |
198 | { | |
ad312c7c | 199 | return vcpu->arch.cr0 & X86_CR0_WP; |
6aa8b732 AK |
200 | } |
201 | ||
202 | static int is_cpuid_PSE36(void) | |
203 | { | |
204 | return 1; | |
205 | } | |
206 | ||
73b1087e AK |
207 | static int is_nx(struct kvm_vcpu *vcpu) |
208 | { | |
ad312c7c | 209 | return vcpu->arch.shadow_efer & EFER_NX; |
73b1087e AK |
210 | } |
211 | ||
6aa8b732 AK |
212 | static int is_present_pte(unsigned long pte) |
213 | { | |
214 | return pte & PT_PRESENT_MASK; | |
215 | } | |
216 | ||
c7addb90 AK |
217 | static int is_shadow_present_pte(u64 pte) |
218 | { | |
c7addb90 AK |
219 | return pte != shadow_trap_nonpresent_pte |
220 | && pte != shadow_notrap_nonpresent_pte; | |
221 | } | |
222 | ||
05da4558 MT |
223 | static int is_large_pte(u64 pte) |
224 | { | |
225 | return pte & PT_PAGE_SIZE_MASK; | |
226 | } | |
227 | ||
6aa8b732 AK |
228 | static int is_writeble_pte(unsigned long pte) |
229 | { | |
230 | return pte & PT_WRITABLE_MASK; | |
231 | } | |
232 | ||
e3c5e7ec AK |
233 | static int is_dirty_pte(unsigned long pte) |
234 | { | |
235 | return pte & PT_DIRTY_MASK; | |
236 | } | |
237 | ||
cd4a4e53 AK |
238 | static int is_rmap_pte(u64 pte) |
239 | { | |
4b1a80fa | 240 | return is_shadow_present_pte(pte); |
cd4a4e53 AK |
241 | } |
242 | ||
da928521 AK |
243 | static gfn_t pse36_gfn_delta(u32 gpte) |
244 | { | |
245 | int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT; | |
246 | ||
247 | return (gpte & PT32_DIR_PSE36_MASK) << shift; | |
248 | } | |
249 | ||
e663ee64 AK |
250 | static void set_shadow_pte(u64 *sptep, u64 spte) |
251 | { | |
252 | #ifdef CONFIG_X86_64 | |
253 | set_64bit((unsigned long *)sptep, spte); | |
254 | #else | |
255 | set_64bit((unsigned long long *)sptep, spte); | |
256 | #endif | |
257 | } | |
258 | ||
e2dec939 | 259 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
2e3e5882 | 260 | struct kmem_cache *base_cache, int min) |
714b93da AK |
261 | { |
262 | void *obj; | |
263 | ||
264 | if (cache->nobjs >= min) | |
e2dec939 | 265 | return 0; |
714b93da | 266 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
2e3e5882 | 267 | obj = kmem_cache_zalloc(base_cache, GFP_KERNEL); |
714b93da | 268 | if (!obj) |
e2dec939 | 269 | return -ENOMEM; |
714b93da AK |
270 | cache->objects[cache->nobjs++] = obj; |
271 | } | |
e2dec939 | 272 | return 0; |
714b93da AK |
273 | } |
274 | ||
275 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) | |
276 | { | |
277 | while (mc->nobjs) | |
278 | kfree(mc->objects[--mc->nobjs]); | |
279 | } | |
280 | ||
c1158e63 | 281 | static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache, |
2e3e5882 | 282 | int min) |
c1158e63 AK |
283 | { |
284 | struct page *page; | |
285 | ||
286 | if (cache->nobjs >= min) | |
287 | return 0; | |
288 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { | |
2e3e5882 | 289 | page = alloc_page(GFP_KERNEL); |
c1158e63 AK |
290 | if (!page) |
291 | return -ENOMEM; | |
292 | set_page_private(page, 0); | |
293 | cache->objects[cache->nobjs++] = page_address(page); | |
294 | } | |
295 | return 0; | |
296 | } | |
297 | ||
298 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) | |
299 | { | |
300 | while (mc->nobjs) | |
c4d198d5 | 301 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
c1158e63 AK |
302 | } |
303 | ||
2e3e5882 | 304 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
714b93da | 305 | { |
e2dec939 AK |
306 | int r; |
307 | ||
ad312c7c | 308 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache, |
2e3e5882 | 309 | pte_chain_cache, 4); |
e2dec939 AK |
310 | if (r) |
311 | goto out; | |
ad312c7c | 312 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, |
2e3e5882 | 313 | rmap_desc_cache, 1); |
d3d25b04 AK |
314 | if (r) |
315 | goto out; | |
ad312c7c | 316 | r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8); |
d3d25b04 AK |
317 | if (r) |
318 | goto out; | |
ad312c7c | 319 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
2e3e5882 | 320 | mmu_page_header_cache, 4); |
e2dec939 AK |
321 | out: |
322 | return r; | |
714b93da AK |
323 | } |
324 | ||
325 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) | |
326 | { | |
ad312c7c ZX |
327 | mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache); |
328 | mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache); | |
329 | mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache); | |
330 | mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache); | |
714b93da AK |
331 | } |
332 | ||
333 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc, | |
334 | size_t size) | |
335 | { | |
336 | void *p; | |
337 | ||
338 | BUG_ON(!mc->nobjs); | |
339 | p = mc->objects[--mc->nobjs]; | |
340 | memset(p, 0, size); | |
341 | return p; | |
342 | } | |
343 | ||
714b93da AK |
344 | static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu) |
345 | { | |
ad312c7c | 346 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache, |
714b93da AK |
347 | sizeof(struct kvm_pte_chain)); |
348 | } | |
349 | ||
90cb0529 | 350 | static void mmu_free_pte_chain(struct kvm_pte_chain *pc) |
714b93da | 351 | { |
90cb0529 | 352 | kfree(pc); |
714b93da AK |
353 | } |
354 | ||
355 | static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu) | |
356 | { | |
ad312c7c | 357 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache, |
714b93da AK |
358 | sizeof(struct kvm_rmap_desc)); |
359 | } | |
360 | ||
90cb0529 | 361 | static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd) |
714b93da | 362 | { |
90cb0529 | 363 | kfree(rd); |
714b93da AK |
364 | } |
365 | ||
05da4558 MT |
366 | /* |
367 | * Return the pointer to the largepage write count for a given | |
368 | * gfn, handling slots that are not large page aligned. | |
369 | */ | |
370 | static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot) | |
371 | { | |
372 | unsigned long idx; | |
373 | ||
374 | idx = (gfn / KVM_PAGES_PER_HPAGE) - | |
375 | (slot->base_gfn / KVM_PAGES_PER_HPAGE); | |
376 | return &slot->lpage_info[idx].write_count; | |
377 | } | |
378 | ||
379 | static void account_shadowed(struct kvm *kvm, gfn_t gfn) | |
380 | { | |
381 | int *write_count; | |
382 | ||
383 | write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn)); | |
384 | *write_count += 1; | |
385 | WARN_ON(*write_count > KVM_PAGES_PER_HPAGE); | |
386 | } | |
387 | ||
388 | static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn) | |
389 | { | |
390 | int *write_count; | |
391 | ||
392 | write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn)); | |
393 | *write_count -= 1; | |
394 | WARN_ON(*write_count < 0); | |
395 | } | |
396 | ||
397 | static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) | |
398 | { | |
399 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
400 | int *largepage_idx; | |
401 | ||
402 | if (slot) { | |
403 | largepage_idx = slot_largepage_idx(gfn, slot); | |
404 | return *largepage_idx; | |
405 | } | |
406 | ||
407 | return 1; | |
408 | } | |
409 | ||
410 | static int host_largepage_backed(struct kvm *kvm, gfn_t gfn) | |
411 | { | |
412 | struct vm_area_struct *vma; | |
413 | unsigned long addr; | |
414 | ||
415 | addr = gfn_to_hva(kvm, gfn); | |
416 | if (kvm_is_error_hva(addr)) | |
417 | return 0; | |
418 | ||
419 | vma = find_vma(current->mm, addr); | |
420 | if (vma && is_vm_hugetlb_page(vma)) | |
421 | return 1; | |
422 | ||
423 | return 0; | |
424 | } | |
425 | ||
426 | static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn) | |
427 | { | |
428 | struct kvm_memory_slot *slot; | |
429 | ||
430 | if (has_wrprotected_page(vcpu->kvm, large_gfn)) | |
431 | return 0; | |
432 | ||
433 | if (!host_largepage_backed(vcpu->kvm, large_gfn)) | |
434 | return 0; | |
435 | ||
436 | slot = gfn_to_memslot(vcpu->kvm, large_gfn); | |
437 | if (slot && slot->dirty_bitmap) | |
438 | return 0; | |
439 | ||
440 | return 1; | |
441 | } | |
442 | ||
290fc38d IE |
443 | /* |
444 | * Take gfn and return the reverse mapping to it. | |
445 | * Note: gfn must be unaliased before this function get called | |
446 | */ | |
447 | ||
05da4558 | 448 | static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage) |
290fc38d IE |
449 | { |
450 | struct kvm_memory_slot *slot; | |
05da4558 | 451 | unsigned long idx; |
290fc38d IE |
452 | |
453 | slot = gfn_to_memslot(kvm, gfn); | |
05da4558 MT |
454 | if (!lpage) |
455 | return &slot->rmap[gfn - slot->base_gfn]; | |
456 | ||
457 | idx = (gfn / KVM_PAGES_PER_HPAGE) - | |
458 | (slot->base_gfn / KVM_PAGES_PER_HPAGE); | |
459 | ||
460 | return &slot->lpage_info[idx].rmap_pde; | |
290fc38d IE |
461 | } |
462 | ||
cd4a4e53 AK |
463 | /* |
464 | * Reverse mapping data structures: | |
465 | * | |
290fc38d IE |
466 | * If rmapp bit zero is zero, then rmapp point to the shadw page table entry |
467 | * that points to page_address(page). | |
cd4a4e53 | 468 | * |
290fc38d IE |
469 | * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc |
470 | * containing more mappings. | |
cd4a4e53 | 471 | */ |
05da4558 | 472 | static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage) |
cd4a4e53 | 473 | { |
4db35314 | 474 | struct kvm_mmu_page *sp; |
cd4a4e53 | 475 | struct kvm_rmap_desc *desc; |
290fc38d | 476 | unsigned long *rmapp; |
cd4a4e53 AK |
477 | int i; |
478 | ||
479 | if (!is_rmap_pte(*spte)) | |
480 | return; | |
290fc38d | 481 | gfn = unalias_gfn(vcpu->kvm, gfn); |
4db35314 AK |
482 | sp = page_header(__pa(spte)); |
483 | sp->gfns[spte - sp->spt] = gfn; | |
05da4558 | 484 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage); |
290fc38d | 485 | if (!*rmapp) { |
cd4a4e53 | 486 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); |
290fc38d IE |
487 | *rmapp = (unsigned long)spte; |
488 | } else if (!(*rmapp & 1)) { | |
cd4a4e53 | 489 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); |
714b93da | 490 | desc = mmu_alloc_rmap_desc(vcpu); |
290fc38d | 491 | desc->shadow_ptes[0] = (u64 *)*rmapp; |
cd4a4e53 | 492 | desc->shadow_ptes[1] = spte; |
290fc38d | 493 | *rmapp = (unsigned long)desc | 1; |
cd4a4e53 AK |
494 | } else { |
495 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); | |
290fc38d | 496 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
cd4a4e53 AK |
497 | while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) |
498 | desc = desc->more; | |
499 | if (desc->shadow_ptes[RMAP_EXT-1]) { | |
714b93da | 500 | desc->more = mmu_alloc_rmap_desc(vcpu); |
cd4a4e53 AK |
501 | desc = desc->more; |
502 | } | |
503 | for (i = 0; desc->shadow_ptes[i]; ++i) | |
504 | ; | |
505 | desc->shadow_ptes[i] = spte; | |
506 | } | |
507 | } | |
508 | ||
290fc38d | 509 | static void rmap_desc_remove_entry(unsigned long *rmapp, |
cd4a4e53 AK |
510 | struct kvm_rmap_desc *desc, |
511 | int i, | |
512 | struct kvm_rmap_desc *prev_desc) | |
513 | { | |
514 | int j; | |
515 | ||
516 | for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j) | |
517 | ; | |
518 | desc->shadow_ptes[i] = desc->shadow_ptes[j]; | |
11718b4d | 519 | desc->shadow_ptes[j] = NULL; |
cd4a4e53 AK |
520 | if (j != 0) |
521 | return; | |
522 | if (!prev_desc && !desc->more) | |
290fc38d | 523 | *rmapp = (unsigned long)desc->shadow_ptes[0]; |
cd4a4e53 AK |
524 | else |
525 | if (prev_desc) | |
526 | prev_desc->more = desc->more; | |
527 | else | |
290fc38d | 528 | *rmapp = (unsigned long)desc->more | 1; |
90cb0529 | 529 | mmu_free_rmap_desc(desc); |
cd4a4e53 AK |
530 | } |
531 | ||
290fc38d | 532 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
cd4a4e53 | 533 | { |
cd4a4e53 AK |
534 | struct kvm_rmap_desc *desc; |
535 | struct kvm_rmap_desc *prev_desc; | |
4db35314 | 536 | struct kvm_mmu_page *sp; |
76c35c6e | 537 | struct page *page; |
290fc38d | 538 | unsigned long *rmapp; |
cd4a4e53 AK |
539 | int i; |
540 | ||
541 | if (!is_rmap_pte(*spte)) | |
542 | return; | |
4db35314 | 543 | sp = page_header(__pa(spte)); |
76c35c6e | 544 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); |
448353ca | 545 | mark_page_accessed(page); |
b4231d61 | 546 | if (is_writeble_pte(*spte)) |
76c35c6e | 547 | kvm_release_page_dirty(page); |
b4231d61 | 548 | else |
76c35c6e | 549 | kvm_release_page_clean(page); |
05da4558 | 550 | rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte)); |
290fc38d | 551 | if (!*rmapp) { |
cd4a4e53 AK |
552 | printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte); |
553 | BUG(); | |
290fc38d | 554 | } else if (!(*rmapp & 1)) { |
cd4a4e53 | 555 | rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte); |
290fc38d | 556 | if ((u64 *)*rmapp != spte) { |
cd4a4e53 AK |
557 | printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n", |
558 | spte, *spte); | |
559 | BUG(); | |
560 | } | |
290fc38d | 561 | *rmapp = 0; |
cd4a4e53 AK |
562 | } else { |
563 | rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte); | |
290fc38d | 564 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
cd4a4e53 AK |
565 | prev_desc = NULL; |
566 | while (desc) { | |
567 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) | |
568 | if (desc->shadow_ptes[i] == spte) { | |
290fc38d | 569 | rmap_desc_remove_entry(rmapp, |
714b93da | 570 | desc, i, |
cd4a4e53 AK |
571 | prev_desc); |
572 | return; | |
573 | } | |
574 | prev_desc = desc; | |
575 | desc = desc->more; | |
576 | } | |
577 | BUG(); | |
578 | } | |
579 | } | |
580 | ||
98348e95 | 581 | static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte) |
374cbac0 | 582 | { |
374cbac0 | 583 | struct kvm_rmap_desc *desc; |
98348e95 IE |
584 | struct kvm_rmap_desc *prev_desc; |
585 | u64 *prev_spte; | |
586 | int i; | |
587 | ||
588 | if (!*rmapp) | |
589 | return NULL; | |
590 | else if (!(*rmapp & 1)) { | |
591 | if (!spte) | |
592 | return (u64 *)*rmapp; | |
593 | return NULL; | |
594 | } | |
595 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); | |
596 | prev_desc = NULL; | |
597 | prev_spte = NULL; | |
598 | while (desc) { | |
599 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) { | |
600 | if (prev_spte == spte) | |
601 | return desc->shadow_ptes[i]; | |
602 | prev_spte = desc->shadow_ptes[i]; | |
603 | } | |
604 | desc = desc->more; | |
605 | } | |
606 | return NULL; | |
607 | } | |
608 | ||
609 | static void rmap_write_protect(struct kvm *kvm, u64 gfn) | |
610 | { | |
290fc38d | 611 | unsigned long *rmapp; |
374cbac0 | 612 | u64 *spte; |
caa5b8a5 | 613 | int write_protected = 0; |
374cbac0 | 614 | |
4a4c9924 | 615 | gfn = unalias_gfn(kvm, gfn); |
05da4558 | 616 | rmapp = gfn_to_rmap(kvm, gfn, 0); |
374cbac0 | 617 | |
98348e95 IE |
618 | spte = rmap_next(kvm, rmapp, NULL); |
619 | while (spte) { | |
374cbac0 | 620 | BUG_ON(!spte); |
374cbac0 | 621 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
374cbac0 | 622 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
caa5b8a5 | 623 | if (is_writeble_pte(*spte)) { |
9647c14c | 624 | set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK); |
caa5b8a5 ED |
625 | write_protected = 1; |
626 | } | |
9647c14c | 627 | spte = rmap_next(kvm, rmapp, spte); |
374cbac0 | 628 | } |
05da4558 MT |
629 | /* check for huge page mappings */ |
630 | rmapp = gfn_to_rmap(kvm, gfn, 1); | |
631 | spte = rmap_next(kvm, rmapp, NULL); | |
632 | while (spte) { | |
633 | BUG_ON(!spte); | |
634 | BUG_ON(!(*spte & PT_PRESENT_MASK)); | |
635 | BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)); | |
636 | pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn); | |
637 | if (is_writeble_pte(*spte)) { | |
638 | rmap_remove(kvm, spte); | |
639 | --kvm->stat.lpages; | |
640 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); | |
641 | write_protected = 1; | |
642 | } | |
643 | spte = rmap_next(kvm, rmapp, spte); | |
644 | } | |
645 | ||
caa5b8a5 ED |
646 | if (write_protected) |
647 | kvm_flush_remote_tlbs(kvm); | |
05da4558 MT |
648 | |
649 | account_shadowed(kvm, gfn); | |
374cbac0 AK |
650 | } |
651 | ||
d6c69ee9 | 652 | #ifdef MMU_DEBUG |
47ad8e68 | 653 | static int is_empty_shadow_page(u64 *spt) |
6aa8b732 | 654 | { |
139bdb2d AK |
655 | u64 *pos; |
656 | u64 *end; | |
657 | ||
47ad8e68 | 658 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
d196e343 | 659 | if (*pos != shadow_trap_nonpresent_pte) { |
b8688d51 | 660 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
139bdb2d | 661 | pos, *pos); |
6aa8b732 | 662 | return 0; |
139bdb2d | 663 | } |
6aa8b732 AK |
664 | return 1; |
665 | } | |
d6c69ee9 | 666 | #endif |
6aa8b732 | 667 | |
4db35314 | 668 | static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
260746c0 | 669 | { |
4db35314 AK |
670 | ASSERT(is_empty_shadow_page(sp->spt)); |
671 | list_del(&sp->link); | |
672 | __free_page(virt_to_page(sp->spt)); | |
673 | __free_page(virt_to_page(sp->gfns)); | |
674 | kfree(sp); | |
f05e70ac | 675 | ++kvm->arch.n_free_mmu_pages; |
260746c0 AK |
676 | } |
677 | ||
cea0f0e7 AK |
678 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
679 | { | |
1ae0a13d | 680 | return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1); |
cea0f0e7 AK |
681 | } |
682 | ||
25c0de2c AK |
683 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
684 | u64 *parent_pte) | |
6aa8b732 | 685 | { |
4db35314 | 686 | struct kvm_mmu_page *sp; |
6aa8b732 | 687 | |
ad312c7c ZX |
688 | sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp); |
689 | sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); | |
690 | sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); | |
4db35314 | 691 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
f05e70ac | 692 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
4db35314 AK |
693 | ASSERT(is_empty_shadow_page(sp->spt)); |
694 | sp->slot_bitmap = 0; | |
695 | sp->multimapped = 0; | |
696 | sp->parent_pte = parent_pte; | |
f05e70ac | 697 | --vcpu->kvm->arch.n_free_mmu_pages; |
4db35314 | 698 | return sp; |
6aa8b732 AK |
699 | } |
700 | ||
714b93da | 701 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
4db35314 | 702 | struct kvm_mmu_page *sp, u64 *parent_pte) |
cea0f0e7 AK |
703 | { |
704 | struct kvm_pte_chain *pte_chain; | |
705 | struct hlist_node *node; | |
706 | int i; | |
707 | ||
708 | if (!parent_pte) | |
709 | return; | |
4db35314 AK |
710 | if (!sp->multimapped) { |
711 | u64 *old = sp->parent_pte; | |
cea0f0e7 AK |
712 | |
713 | if (!old) { | |
4db35314 | 714 | sp->parent_pte = parent_pte; |
cea0f0e7 AK |
715 | return; |
716 | } | |
4db35314 | 717 | sp->multimapped = 1; |
714b93da | 718 | pte_chain = mmu_alloc_pte_chain(vcpu); |
4db35314 AK |
719 | INIT_HLIST_HEAD(&sp->parent_ptes); |
720 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); | |
cea0f0e7 AK |
721 | pte_chain->parent_ptes[0] = old; |
722 | } | |
4db35314 | 723 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) { |
cea0f0e7 AK |
724 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) |
725 | continue; | |
726 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) | |
727 | if (!pte_chain->parent_ptes[i]) { | |
728 | pte_chain->parent_ptes[i] = parent_pte; | |
729 | return; | |
730 | } | |
731 | } | |
714b93da | 732 | pte_chain = mmu_alloc_pte_chain(vcpu); |
cea0f0e7 | 733 | BUG_ON(!pte_chain); |
4db35314 | 734 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); |
cea0f0e7 AK |
735 | pte_chain->parent_ptes[0] = parent_pte; |
736 | } | |
737 | ||
4db35314 | 738 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
cea0f0e7 AK |
739 | u64 *parent_pte) |
740 | { | |
741 | struct kvm_pte_chain *pte_chain; | |
742 | struct hlist_node *node; | |
743 | int i; | |
744 | ||
4db35314 AK |
745 | if (!sp->multimapped) { |
746 | BUG_ON(sp->parent_pte != parent_pte); | |
747 | sp->parent_pte = NULL; | |
cea0f0e7 AK |
748 | return; |
749 | } | |
4db35314 | 750 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
cea0f0e7 AK |
751 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
752 | if (!pte_chain->parent_ptes[i]) | |
753 | break; | |
754 | if (pte_chain->parent_ptes[i] != parent_pte) | |
755 | continue; | |
697fe2e2 AK |
756 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
757 | && pte_chain->parent_ptes[i + 1]) { | |
cea0f0e7 AK |
758 | pte_chain->parent_ptes[i] |
759 | = pte_chain->parent_ptes[i + 1]; | |
760 | ++i; | |
761 | } | |
762 | pte_chain->parent_ptes[i] = NULL; | |
697fe2e2 AK |
763 | if (i == 0) { |
764 | hlist_del(&pte_chain->link); | |
90cb0529 | 765 | mmu_free_pte_chain(pte_chain); |
4db35314 AK |
766 | if (hlist_empty(&sp->parent_ptes)) { |
767 | sp->multimapped = 0; | |
768 | sp->parent_pte = NULL; | |
697fe2e2 AK |
769 | } |
770 | } | |
cea0f0e7 AK |
771 | return; |
772 | } | |
773 | BUG(); | |
774 | } | |
775 | ||
4db35314 | 776 | static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn) |
cea0f0e7 AK |
777 | { |
778 | unsigned index; | |
779 | struct hlist_head *bucket; | |
4db35314 | 780 | struct kvm_mmu_page *sp; |
cea0f0e7 AK |
781 | struct hlist_node *node; |
782 | ||
b8688d51 | 783 | pgprintk("%s: looking for gfn %lx\n", __func__, gfn); |
1ae0a13d | 784 | index = kvm_page_table_hashfn(gfn); |
f05e70ac | 785 | bucket = &kvm->arch.mmu_page_hash[index]; |
4db35314 | 786 | hlist_for_each_entry(sp, node, bucket, hash_link) |
2e53d63a MT |
787 | if (sp->gfn == gfn && !sp->role.metaphysical |
788 | && !sp->role.invalid) { | |
cea0f0e7 | 789 | pgprintk("%s: found role %x\n", |
b8688d51 | 790 | __func__, sp->role.word); |
4db35314 | 791 | return sp; |
cea0f0e7 AK |
792 | } |
793 | return NULL; | |
794 | } | |
795 | ||
796 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, | |
797 | gfn_t gfn, | |
798 | gva_t gaddr, | |
799 | unsigned level, | |
800 | int metaphysical, | |
41074d07 | 801 | unsigned access, |
f7d9c7b7 | 802 | u64 *parent_pte) |
cea0f0e7 AK |
803 | { |
804 | union kvm_mmu_page_role role; | |
805 | unsigned index; | |
806 | unsigned quadrant; | |
807 | struct hlist_head *bucket; | |
4db35314 | 808 | struct kvm_mmu_page *sp; |
cea0f0e7 AK |
809 | struct hlist_node *node; |
810 | ||
811 | role.word = 0; | |
ad312c7c | 812 | role.glevels = vcpu->arch.mmu.root_level; |
cea0f0e7 AK |
813 | role.level = level; |
814 | role.metaphysical = metaphysical; | |
41074d07 | 815 | role.access = access; |
ad312c7c | 816 | if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { |
cea0f0e7 AK |
817 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
818 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; | |
819 | role.quadrant = quadrant; | |
820 | } | |
b8688d51 | 821 | pgprintk("%s: looking gfn %lx role %x\n", __func__, |
cea0f0e7 | 822 | gfn, role.word); |
1ae0a13d | 823 | index = kvm_page_table_hashfn(gfn); |
f05e70ac | 824 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
4db35314 AK |
825 | hlist_for_each_entry(sp, node, bucket, hash_link) |
826 | if (sp->gfn == gfn && sp->role.word == role.word) { | |
827 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); | |
b8688d51 | 828 | pgprintk("%s: found\n", __func__); |
4db35314 | 829 | return sp; |
cea0f0e7 | 830 | } |
dfc5aa00 | 831 | ++vcpu->kvm->stat.mmu_cache_miss; |
4db35314 AK |
832 | sp = kvm_mmu_alloc_page(vcpu, parent_pte); |
833 | if (!sp) | |
834 | return sp; | |
b8688d51 | 835 | pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word); |
4db35314 AK |
836 | sp->gfn = gfn; |
837 | sp->role = role; | |
838 | hlist_add_head(&sp->hash_link, bucket); | |
ad312c7c | 839 | vcpu->arch.mmu.prefetch_page(vcpu, sp); |
374cbac0 | 840 | if (!metaphysical) |
4a4c9924 | 841 | rmap_write_protect(vcpu->kvm, gfn); |
4db35314 | 842 | return sp; |
cea0f0e7 AK |
843 | } |
844 | ||
90cb0529 | 845 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
4db35314 | 846 | struct kvm_mmu_page *sp) |
a436036b | 847 | { |
697fe2e2 AK |
848 | unsigned i; |
849 | u64 *pt; | |
850 | u64 ent; | |
851 | ||
4db35314 | 852 | pt = sp->spt; |
697fe2e2 | 853 | |
4db35314 | 854 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { |
697fe2e2 | 855 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
c7addb90 | 856 | if (is_shadow_present_pte(pt[i])) |
290fc38d | 857 | rmap_remove(kvm, &pt[i]); |
c7addb90 | 858 | pt[i] = shadow_trap_nonpresent_pte; |
697fe2e2 | 859 | } |
90cb0529 | 860 | kvm_flush_remote_tlbs(kvm); |
697fe2e2 AK |
861 | return; |
862 | } | |
863 | ||
864 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { | |
865 | ent = pt[i]; | |
866 | ||
05da4558 MT |
867 | if (is_shadow_present_pte(ent)) { |
868 | if (!is_large_pte(ent)) { | |
869 | ent &= PT64_BASE_ADDR_MASK; | |
870 | mmu_page_remove_parent_pte(page_header(ent), | |
871 | &pt[i]); | |
872 | } else { | |
873 | --kvm->stat.lpages; | |
874 | rmap_remove(kvm, &pt[i]); | |
875 | } | |
876 | } | |
c7addb90 | 877 | pt[i] = shadow_trap_nonpresent_pte; |
697fe2e2 | 878 | } |
90cb0529 | 879 | kvm_flush_remote_tlbs(kvm); |
a436036b AK |
880 | } |
881 | ||
4db35314 | 882 | static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte) |
cea0f0e7 | 883 | { |
4db35314 | 884 | mmu_page_remove_parent_pte(sp, parent_pte); |
a436036b AK |
885 | } |
886 | ||
12b7d28f AK |
887 | static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm) |
888 | { | |
889 | int i; | |
890 | ||
891 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
892 | if (kvm->vcpus[i]) | |
ad312c7c | 893 | kvm->vcpus[i]->arch.last_pte_updated = NULL; |
12b7d28f AK |
894 | } |
895 | ||
4db35314 | 896 | static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
a436036b AK |
897 | { |
898 | u64 *parent_pte; | |
899 | ||
4cee5764 | 900 | ++kvm->stat.mmu_shadow_zapped; |
4db35314 AK |
901 | while (sp->multimapped || sp->parent_pte) { |
902 | if (!sp->multimapped) | |
903 | parent_pte = sp->parent_pte; | |
a436036b AK |
904 | else { |
905 | struct kvm_pte_chain *chain; | |
906 | ||
4db35314 | 907 | chain = container_of(sp->parent_ptes.first, |
a436036b AK |
908 | struct kvm_pte_chain, link); |
909 | parent_pte = chain->parent_ptes[0]; | |
910 | } | |
697fe2e2 | 911 | BUG_ON(!parent_pte); |
4db35314 | 912 | kvm_mmu_put_page(sp, parent_pte); |
c7addb90 | 913 | set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte); |
a436036b | 914 | } |
4db35314 AK |
915 | kvm_mmu_page_unlink_children(kvm, sp); |
916 | if (!sp->root_count) { | |
05da4558 MT |
917 | if (!sp->role.metaphysical) |
918 | unaccount_shadowed(kvm, sp->gfn); | |
4db35314 AK |
919 | hlist_del(&sp->hash_link); |
920 | kvm_mmu_free_page(kvm, sp); | |
2e53d63a | 921 | } else { |
f05e70ac | 922 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
2e53d63a MT |
923 | sp->role.invalid = 1; |
924 | kvm_reload_remote_mmus(kvm); | |
925 | } | |
12b7d28f | 926 | kvm_mmu_reset_last_pte_updated(kvm); |
a436036b AK |
927 | } |
928 | ||
82ce2c96 IE |
929 | /* |
930 | * Changing the number of mmu pages allocated to the vm | |
931 | * Note: if kvm_nr_mmu_pages is too small, you will get dead lock | |
932 | */ | |
933 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages) | |
934 | { | |
935 | /* | |
936 | * If we set the number of mmu pages to be smaller be than the | |
937 | * number of actived pages , we must to free some mmu pages before we | |
938 | * change the value | |
939 | */ | |
940 | ||
f05e70ac | 941 | if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) > |
82ce2c96 | 942 | kvm_nr_mmu_pages) { |
f05e70ac ZX |
943 | int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages |
944 | - kvm->arch.n_free_mmu_pages; | |
82ce2c96 IE |
945 | |
946 | while (n_used_mmu_pages > kvm_nr_mmu_pages) { | |
947 | struct kvm_mmu_page *page; | |
948 | ||
f05e70ac | 949 | page = container_of(kvm->arch.active_mmu_pages.prev, |
82ce2c96 IE |
950 | struct kvm_mmu_page, link); |
951 | kvm_mmu_zap_page(kvm, page); | |
952 | n_used_mmu_pages--; | |
953 | } | |
f05e70ac | 954 | kvm->arch.n_free_mmu_pages = 0; |
82ce2c96 IE |
955 | } |
956 | else | |
f05e70ac ZX |
957 | kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages |
958 | - kvm->arch.n_alloc_mmu_pages; | |
82ce2c96 | 959 | |
f05e70ac | 960 | kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages; |
82ce2c96 IE |
961 | } |
962 | ||
f67a46f4 | 963 | static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
a436036b AK |
964 | { |
965 | unsigned index; | |
966 | struct hlist_head *bucket; | |
4db35314 | 967 | struct kvm_mmu_page *sp; |
a436036b AK |
968 | struct hlist_node *node, *n; |
969 | int r; | |
970 | ||
b8688d51 | 971 | pgprintk("%s: looking for gfn %lx\n", __func__, gfn); |
a436036b | 972 | r = 0; |
1ae0a13d | 973 | index = kvm_page_table_hashfn(gfn); |
f05e70ac | 974 | bucket = &kvm->arch.mmu_page_hash[index]; |
4db35314 AK |
975 | hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) |
976 | if (sp->gfn == gfn && !sp->role.metaphysical) { | |
b8688d51 | 977 | pgprintk("%s: gfn %lx role %x\n", __func__, gfn, |
4db35314 AK |
978 | sp->role.word); |
979 | kvm_mmu_zap_page(kvm, sp); | |
a436036b AK |
980 | r = 1; |
981 | } | |
982 | return r; | |
cea0f0e7 AK |
983 | } |
984 | ||
f67a46f4 | 985 | static void mmu_unshadow(struct kvm *kvm, gfn_t gfn) |
97a0a01e | 986 | { |
4db35314 | 987 | struct kvm_mmu_page *sp; |
97a0a01e | 988 | |
4db35314 | 989 | while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) { |
b8688d51 | 990 | pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word); |
4db35314 | 991 | kvm_mmu_zap_page(kvm, sp); |
97a0a01e AK |
992 | } |
993 | } | |
994 | ||
38c335f1 | 995 | static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn) |
6aa8b732 | 996 | { |
38c335f1 | 997 | int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn)); |
4db35314 | 998 | struct kvm_mmu_page *sp = page_header(__pa(pte)); |
6aa8b732 | 999 | |
4db35314 | 1000 | __set_bit(slot, &sp->slot_bitmap); |
6aa8b732 AK |
1001 | } |
1002 | ||
039576c0 AK |
1003 | struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva) |
1004 | { | |
72dc67a6 IE |
1005 | struct page *page; |
1006 | ||
ad312c7c | 1007 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva); |
039576c0 AK |
1008 | |
1009 | if (gpa == UNMAPPED_GVA) | |
1010 | return NULL; | |
72dc67a6 IE |
1011 | |
1012 | down_read(¤t->mm->mmap_sem); | |
1013 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); | |
1014 | up_read(¤t->mm->mmap_sem); | |
1015 | ||
1016 | return page; | |
039576c0 AK |
1017 | } |
1018 | ||
1c4f1fd6 AK |
1019 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, |
1020 | unsigned pt_access, unsigned pte_access, | |
1021 | int user_fault, int write_fault, int dirty, | |
05da4558 MT |
1022 | int *ptwrite, int largepage, gfn_t gfn, |
1023 | struct page *page) | |
1c4f1fd6 AK |
1024 | { |
1025 | u64 spte; | |
15aaa819 | 1026 | int was_rmapped = 0; |
75e68e60 | 1027 | int was_writeble = is_writeble_pte(*shadow_pte); |
15aaa819 | 1028 | hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
1c4f1fd6 | 1029 | |
bc750ba8 | 1030 | pgprintk("%s: spte %llx access %x write_fault %d" |
1c4f1fd6 | 1031 | " user_fault %d gfn %lx\n", |
b8688d51 | 1032 | __func__, *shadow_pte, pt_access, |
1c4f1fd6 AK |
1033 | write_fault, user_fault, gfn); |
1034 | ||
15aaa819 | 1035 | if (is_rmap_pte(*shadow_pte)) { |
05da4558 MT |
1036 | /* |
1037 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink | |
1038 | * the parent of the now unreachable PTE. | |
1039 | */ | |
1040 | if (largepage && !is_large_pte(*shadow_pte)) { | |
1041 | struct kvm_mmu_page *child; | |
1042 | u64 pte = *shadow_pte; | |
1043 | ||
1044 | child = page_header(pte & PT64_BASE_ADDR_MASK); | |
1045 | mmu_page_remove_parent_pte(child, shadow_pte); | |
1046 | } else if (host_pfn != page_to_pfn(page)) { | |
15aaa819 MT |
1047 | pgprintk("hfn old %lx new %lx\n", |
1048 | host_pfn, page_to_pfn(page)); | |
1049 | rmap_remove(vcpu->kvm, shadow_pte); | |
05da4558 MT |
1050 | } else { |
1051 | if (largepage) | |
1052 | was_rmapped = is_large_pte(*shadow_pte); | |
1053 | else | |
1054 | was_rmapped = 1; | |
15aaa819 | 1055 | } |
15aaa819 MT |
1056 | } |
1057 | ||
1c4f1fd6 AK |
1058 | /* |
1059 | * We don't set the accessed bit, since we sometimes want to see | |
1060 | * whether the guest actually used the pte (in order to detect | |
1061 | * demand paging). | |
1062 | */ | |
1063 | spte = PT_PRESENT_MASK | PT_DIRTY_MASK; | |
1064 | if (!dirty) | |
1065 | pte_access &= ~ACC_WRITE_MASK; | |
1066 | if (!(pte_access & ACC_EXEC_MASK)) | |
1067 | spte |= PT64_NX_MASK; | |
1068 | ||
1c4f1fd6 AK |
1069 | spte |= PT_PRESENT_MASK; |
1070 | if (pte_access & ACC_USER_MASK) | |
1071 | spte |= PT_USER_MASK; | |
05da4558 MT |
1072 | if (largepage) |
1073 | spte |= PT_PAGE_SIZE_MASK; | |
1c4f1fd6 | 1074 | |
1c4f1fd6 AK |
1075 | spte |= page_to_phys(page); |
1076 | ||
1077 | if ((pte_access & ACC_WRITE_MASK) | |
1078 | || (write_fault && !is_write_protection(vcpu) && !user_fault)) { | |
1079 | struct kvm_mmu_page *shadow; | |
1080 | ||
1081 | spte |= PT_WRITABLE_MASK; | |
1082 | if (user_fault) { | |
1083 | mmu_unshadow(vcpu->kvm, gfn); | |
1084 | goto unshadowed; | |
1085 | } | |
1086 | ||
1087 | shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn); | |
05da4558 MT |
1088 | if (shadow || |
1089 | (largepage && has_wrprotected_page(vcpu->kvm, gfn))) { | |
1c4f1fd6 | 1090 | pgprintk("%s: found shadow page for %lx, marking ro\n", |
b8688d51 | 1091 | __func__, gfn); |
1c4f1fd6 AK |
1092 | pte_access &= ~ACC_WRITE_MASK; |
1093 | if (is_writeble_pte(spte)) { | |
1094 | spte &= ~PT_WRITABLE_MASK; | |
1095 | kvm_x86_ops->tlb_flush(vcpu); | |
1096 | } | |
1097 | if (write_fault) | |
1098 | *ptwrite = 1; | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | unshadowed: | |
1103 | ||
1104 | if (pte_access & ACC_WRITE_MASK) | |
1105 | mark_page_dirty(vcpu->kvm, gfn); | |
1106 | ||
b8688d51 | 1107 | pgprintk("%s: setting spte %llx\n", __func__, spte); |
05da4558 MT |
1108 | pgprintk("instantiating %s PTE (%s) at %d (%llx) addr %llx\n", |
1109 | (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB", | |
1110 | (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte); | |
1c4f1fd6 | 1111 | set_shadow_pte(shadow_pte, spte); |
05da4558 MT |
1112 | if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK) |
1113 | && (spte & PT_PRESENT_MASK)) | |
1114 | ++vcpu->kvm->stat.lpages; | |
1115 | ||
1c4f1fd6 AK |
1116 | page_header_update_slot(vcpu->kvm, shadow_pte, gfn); |
1117 | if (!was_rmapped) { | |
05da4558 | 1118 | rmap_add(vcpu, shadow_pte, gfn, largepage); |
1c4f1fd6 AK |
1119 | if (!is_rmap_pte(*shadow_pte)) |
1120 | kvm_release_page_clean(page); | |
75e68e60 IE |
1121 | } else { |
1122 | if (was_writeble) | |
1123 | kvm_release_page_dirty(page); | |
1124 | else | |
1125 | kvm_release_page_clean(page); | |
1c4f1fd6 | 1126 | } |
1c4f1fd6 | 1127 | if (!ptwrite || !*ptwrite) |
ad312c7c | 1128 | vcpu->arch.last_pte_updated = shadow_pte; |
1c4f1fd6 AK |
1129 | } |
1130 | ||
6aa8b732 AK |
1131 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
1132 | { | |
1133 | } | |
1134 | ||
4d9976bb | 1135 | static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, |
05da4558 MT |
1136 | int largepage, gfn_t gfn, struct page *page, |
1137 | int level) | |
6aa8b732 | 1138 | { |
ad312c7c | 1139 | hpa_t table_addr = vcpu->arch.mmu.root_hpa; |
e833240f | 1140 | int pt_write = 0; |
6aa8b732 AK |
1141 | |
1142 | for (; ; level--) { | |
1143 | u32 index = PT64_INDEX(v, level); | |
1144 | u64 *table; | |
1145 | ||
1146 | ASSERT(VALID_PAGE(table_addr)); | |
1147 | table = __va(table_addr); | |
1148 | ||
1149 | if (level == 1) { | |
e833240f | 1150 | mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL, |
05da4558 MT |
1151 | 0, write, 1, &pt_write, 0, gfn, page); |
1152 | return pt_write; | |
1153 | } | |
1154 | ||
1155 | if (largepage && level == 2) { | |
1156 | mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL, | |
1157 | 0, write, 1, &pt_write, 1, gfn, page); | |
d196e343 | 1158 | return pt_write; |
6aa8b732 AK |
1159 | } |
1160 | ||
c7addb90 | 1161 | if (table[index] == shadow_trap_nonpresent_pte) { |
25c0de2c | 1162 | struct kvm_mmu_page *new_table; |
cea0f0e7 | 1163 | gfn_t pseudo_gfn; |
6aa8b732 | 1164 | |
cea0f0e7 AK |
1165 | pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK) |
1166 | >> PAGE_SHIFT; | |
1167 | new_table = kvm_mmu_get_page(vcpu, pseudo_gfn, | |
1168 | v, level - 1, | |
f7d9c7b7 | 1169 | 1, ACC_ALL, &table[index]); |
25c0de2c | 1170 | if (!new_table) { |
6aa8b732 | 1171 | pgprintk("nonpaging_map: ENOMEM\n"); |
d7824fff | 1172 | kvm_release_page_clean(page); |
6aa8b732 AK |
1173 | return -ENOMEM; |
1174 | } | |
1175 | ||
47ad8e68 | 1176 | table[index] = __pa(new_table->spt) | PT_PRESENT_MASK |
25c0de2c | 1177 | | PT_WRITABLE_MASK | PT_USER_MASK; |
6aa8b732 AK |
1178 | } |
1179 | table_addr = table[index] & PT64_BASE_ADDR_MASK; | |
1180 | } | |
1181 | } | |
1182 | ||
10589a46 MT |
1183 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn) |
1184 | { | |
1185 | int r; | |
05da4558 | 1186 | int largepage = 0; |
10589a46 | 1187 | |
aaee2c94 MT |
1188 | struct page *page; |
1189 | ||
72dc67a6 IE |
1190 | down_read(&vcpu->kvm->slots_lock); |
1191 | ||
aaee2c94 | 1192 | down_read(¤t->mm->mmap_sem); |
05da4558 MT |
1193 | if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) { |
1194 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); | |
1195 | largepage = 1; | |
1196 | } | |
1197 | ||
aaee2c94 | 1198 | page = gfn_to_page(vcpu->kvm, gfn); |
72dc67a6 | 1199 | up_read(¤t->mm->mmap_sem); |
aaee2c94 | 1200 | |
d196e343 AK |
1201 | /* mmio */ |
1202 | if (is_error_page(page)) { | |
1203 | kvm_release_page_clean(page); | |
1204 | up_read(&vcpu->kvm->slots_lock); | |
1205 | return 1; | |
1206 | } | |
1207 | ||
aaee2c94 | 1208 | spin_lock(&vcpu->kvm->mmu_lock); |
eb787d10 | 1209 | kvm_mmu_free_some_pages(vcpu); |
05da4558 MT |
1210 | r = __direct_map(vcpu, v, write, largepage, gfn, page, |
1211 | PT32E_ROOT_LEVEL); | |
aaee2c94 MT |
1212 | spin_unlock(&vcpu->kvm->mmu_lock); |
1213 | ||
72dc67a6 | 1214 | up_read(&vcpu->kvm->slots_lock); |
aaee2c94 | 1215 | |
10589a46 MT |
1216 | return r; |
1217 | } | |
1218 | ||
1219 | ||
c7addb90 AK |
1220 | static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu, |
1221 | struct kvm_mmu_page *sp) | |
1222 | { | |
1223 | int i; | |
1224 | ||
1225 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) | |
1226 | sp->spt[i] = shadow_trap_nonpresent_pte; | |
1227 | } | |
1228 | ||
17ac10ad AK |
1229 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
1230 | { | |
1231 | int i; | |
4db35314 | 1232 | struct kvm_mmu_page *sp; |
17ac10ad | 1233 | |
ad312c7c | 1234 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
7b53aa56 | 1235 | return; |
aaee2c94 | 1236 | spin_lock(&vcpu->kvm->mmu_lock); |
17ac10ad | 1237 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
1238 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
1239 | hpa_t root = vcpu->arch.mmu.root_hpa; | |
17ac10ad | 1240 | |
4db35314 AK |
1241 | sp = page_header(root); |
1242 | --sp->root_count; | |
2e53d63a MT |
1243 | if (!sp->root_count && sp->role.invalid) |
1244 | kvm_mmu_zap_page(vcpu->kvm, sp); | |
ad312c7c | 1245 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
aaee2c94 | 1246 | spin_unlock(&vcpu->kvm->mmu_lock); |
17ac10ad AK |
1247 | return; |
1248 | } | |
1249 | #endif | |
1250 | for (i = 0; i < 4; ++i) { | |
ad312c7c | 1251 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
17ac10ad | 1252 | |
417726a3 | 1253 | if (root) { |
417726a3 | 1254 | root &= PT64_BASE_ADDR_MASK; |
4db35314 AK |
1255 | sp = page_header(root); |
1256 | --sp->root_count; | |
2e53d63a MT |
1257 | if (!sp->root_count && sp->role.invalid) |
1258 | kvm_mmu_zap_page(vcpu->kvm, sp); | |
417726a3 | 1259 | } |
ad312c7c | 1260 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
17ac10ad | 1261 | } |
aaee2c94 | 1262 | spin_unlock(&vcpu->kvm->mmu_lock); |
ad312c7c | 1263 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
17ac10ad AK |
1264 | } |
1265 | ||
1266 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) | |
1267 | { | |
1268 | int i; | |
cea0f0e7 | 1269 | gfn_t root_gfn; |
4db35314 | 1270 | struct kvm_mmu_page *sp; |
fb72d167 | 1271 | int metaphysical = 0; |
3bb65a22 | 1272 | |
ad312c7c | 1273 | root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT; |
17ac10ad AK |
1274 | |
1275 | #ifdef CONFIG_X86_64 | |
ad312c7c ZX |
1276 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
1277 | hpa_t root = vcpu->arch.mmu.root_hpa; | |
17ac10ad AK |
1278 | |
1279 | ASSERT(!VALID_PAGE(root)); | |
fb72d167 JR |
1280 | if (tdp_enabled) |
1281 | metaphysical = 1; | |
4db35314 | 1282 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, |
fb72d167 JR |
1283 | PT64_ROOT_LEVEL, metaphysical, |
1284 | ACC_ALL, NULL); | |
4db35314 AK |
1285 | root = __pa(sp->spt); |
1286 | ++sp->root_count; | |
ad312c7c | 1287 | vcpu->arch.mmu.root_hpa = root; |
17ac10ad AK |
1288 | return; |
1289 | } | |
1290 | #endif | |
fb72d167 JR |
1291 | metaphysical = !is_paging(vcpu); |
1292 | if (tdp_enabled) | |
1293 | metaphysical = 1; | |
17ac10ad | 1294 | for (i = 0; i < 4; ++i) { |
ad312c7c | 1295 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
17ac10ad AK |
1296 | |
1297 | ASSERT(!VALID_PAGE(root)); | |
ad312c7c ZX |
1298 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
1299 | if (!is_present_pte(vcpu->arch.pdptrs[i])) { | |
1300 | vcpu->arch.mmu.pae_root[i] = 0; | |
417726a3 AK |
1301 | continue; |
1302 | } | |
ad312c7c ZX |
1303 | root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT; |
1304 | } else if (vcpu->arch.mmu.root_level == 0) | |
cea0f0e7 | 1305 | root_gfn = 0; |
4db35314 | 1306 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
fb72d167 | 1307 | PT32_ROOT_LEVEL, metaphysical, |
f7d9c7b7 | 1308 | ACC_ALL, NULL); |
4db35314 AK |
1309 | root = __pa(sp->spt); |
1310 | ++sp->root_count; | |
ad312c7c | 1311 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; |
17ac10ad | 1312 | } |
ad312c7c | 1313 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
17ac10ad AK |
1314 | } |
1315 | ||
6aa8b732 AK |
1316 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
1317 | { | |
1318 | return vaddr; | |
1319 | } | |
1320 | ||
1321 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, | |
3f3e7124 | 1322 | u32 error_code) |
6aa8b732 | 1323 | { |
e833240f | 1324 | gfn_t gfn; |
e2dec939 | 1325 | int r; |
6aa8b732 | 1326 | |
b8688d51 | 1327 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
e2dec939 AK |
1328 | r = mmu_topup_memory_caches(vcpu); |
1329 | if (r) | |
1330 | return r; | |
714b93da | 1331 | |
6aa8b732 | 1332 | ASSERT(vcpu); |
ad312c7c | 1333 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 | 1334 | |
e833240f | 1335 | gfn = gva >> PAGE_SHIFT; |
6aa8b732 | 1336 | |
e833240f AK |
1337 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
1338 | error_code & PFERR_WRITE_MASK, gfn); | |
6aa8b732 AK |
1339 | } |
1340 | ||
fb72d167 JR |
1341 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, |
1342 | u32 error_code) | |
1343 | { | |
1344 | struct page *page; | |
1345 | int r; | |
05da4558 MT |
1346 | int largepage = 0; |
1347 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
fb72d167 JR |
1348 | |
1349 | ASSERT(vcpu); | |
1350 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); | |
1351 | ||
1352 | r = mmu_topup_memory_caches(vcpu); | |
1353 | if (r) | |
1354 | return r; | |
1355 | ||
1356 | down_read(¤t->mm->mmap_sem); | |
05da4558 MT |
1357 | if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) { |
1358 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); | |
1359 | largepage = 1; | |
1360 | } | |
1361 | page = gfn_to_page(vcpu->kvm, gfn); | |
fb72d167 JR |
1362 | if (is_error_page(page)) { |
1363 | kvm_release_page_clean(page); | |
1364 | up_read(¤t->mm->mmap_sem); | |
1365 | return 1; | |
1366 | } | |
1367 | spin_lock(&vcpu->kvm->mmu_lock); | |
1368 | kvm_mmu_free_some_pages(vcpu); | |
1369 | r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK, | |
05da4558 | 1370 | largepage, gfn, page, TDP_ROOT_LEVEL); |
fb72d167 JR |
1371 | spin_unlock(&vcpu->kvm->mmu_lock); |
1372 | up_read(¤t->mm->mmap_sem); | |
1373 | ||
1374 | return r; | |
1375 | } | |
1376 | ||
6aa8b732 AK |
1377 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
1378 | { | |
17ac10ad | 1379 | mmu_free_roots(vcpu); |
6aa8b732 AK |
1380 | } |
1381 | ||
1382 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) | |
1383 | { | |
ad312c7c | 1384 | struct kvm_mmu *context = &vcpu->arch.mmu; |
6aa8b732 AK |
1385 | |
1386 | context->new_cr3 = nonpaging_new_cr3; | |
1387 | context->page_fault = nonpaging_page_fault; | |
6aa8b732 AK |
1388 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
1389 | context->free = nonpaging_free; | |
c7addb90 | 1390 | context->prefetch_page = nonpaging_prefetch_page; |
cea0f0e7 | 1391 | context->root_level = 0; |
6aa8b732 | 1392 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
17c3ba9d | 1393 | context->root_hpa = INVALID_PAGE; |
6aa8b732 AK |
1394 | return 0; |
1395 | } | |
1396 | ||
d835dfec | 1397 | void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
6aa8b732 | 1398 | { |
1165f5fe | 1399 | ++vcpu->stat.tlb_flush; |
cbdd1bea | 1400 | kvm_x86_ops->tlb_flush(vcpu); |
6aa8b732 AK |
1401 | } |
1402 | ||
1403 | static void paging_new_cr3(struct kvm_vcpu *vcpu) | |
1404 | { | |
b8688d51 | 1405 | pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3); |
cea0f0e7 | 1406 | mmu_free_roots(vcpu); |
6aa8b732 AK |
1407 | } |
1408 | ||
6aa8b732 AK |
1409 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
1410 | u64 addr, | |
1411 | u32 err_code) | |
1412 | { | |
c3c91fee | 1413 | kvm_inject_page_fault(vcpu, addr, err_code); |
6aa8b732 AK |
1414 | } |
1415 | ||
6aa8b732 AK |
1416 | static void paging_free(struct kvm_vcpu *vcpu) |
1417 | { | |
1418 | nonpaging_free(vcpu); | |
1419 | } | |
1420 | ||
1421 | #define PTTYPE 64 | |
1422 | #include "paging_tmpl.h" | |
1423 | #undef PTTYPE | |
1424 | ||
1425 | #define PTTYPE 32 | |
1426 | #include "paging_tmpl.h" | |
1427 | #undef PTTYPE | |
1428 | ||
17ac10ad | 1429 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
6aa8b732 | 1430 | { |
ad312c7c | 1431 | struct kvm_mmu *context = &vcpu->arch.mmu; |
6aa8b732 AK |
1432 | |
1433 | ASSERT(is_pae(vcpu)); | |
1434 | context->new_cr3 = paging_new_cr3; | |
1435 | context->page_fault = paging64_page_fault; | |
6aa8b732 | 1436 | context->gva_to_gpa = paging64_gva_to_gpa; |
c7addb90 | 1437 | context->prefetch_page = paging64_prefetch_page; |
6aa8b732 | 1438 | context->free = paging_free; |
17ac10ad AK |
1439 | context->root_level = level; |
1440 | context->shadow_root_level = level; | |
17c3ba9d | 1441 | context->root_hpa = INVALID_PAGE; |
6aa8b732 AK |
1442 | return 0; |
1443 | } | |
1444 | ||
17ac10ad AK |
1445 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
1446 | { | |
1447 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); | |
1448 | } | |
1449 | ||
6aa8b732 AK |
1450 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
1451 | { | |
ad312c7c | 1452 | struct kvm_mmu *context = &vcpu->arch.mmu; |
6aa8b732 AK |
1453 | |
1454 | context->new_cr3 = paging_new_cr3; | |
1455 | context->page_fault = paging32_page_fault; | |
6aa8b732 AK |
1456 | context->gva_to_gpa = paging32_gva_to_gpa; |
1457 | context->free = paging_free; | |
c7addb90 | 1458 | context->prefetch_page = paging32_prefetch_page; |
6aa8b732 AK |
1459 | context->root_level = PT32_ROOT_LEVEL; |
1460 | context->shadow_root_level = PT32E_ROOT_LEVEL; | |
17c3ba9d | 1461 | context->root_hpa = INVALID_PAGE; |
6aa8b732 AK |
1462 | return 0; |
1463 | } | |
1464 | ||
1465 | static int paging32E_init_context(struct kvm_vcpu *vcpu) | |
1466 | { | |
17ac10ad | 1467 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
6aa8b732 AK |
1468 | } |
1469 | ||
fb72d167 JR |
1470 | static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
1471 | { | |
1472 | struct kvm_mmu *context = &vcpu->arch.mmu; | |
1473 | ||
1474 | context->new_cr3 = nonpaging_new_cr3; | |
1475 | context->page_fault = tdp_page_fault; | |
1476 | context->free = nonpaging_free; | |
1477 | context->prefetch_page = nonpaging_prefetch_page; | |
1478 | context->shadow_root_level = TDP_ROOT_LEVEL; | |
1479 | context->root_hpa = INVALID_PAGE; | |
1480 | ||
1481 | if (!is_paging(vcpu)) { | |
1482 | context->gva_to_gpa = nonpaging_gva_to_gpa; | |
1483 | context->root_level = 0; | |
1484 | } else if (is_long_mode(vcpu)) { | |
1485 | context->gva_to_gpa = paging64_gva_to_gpa; | |
1486 | context->root_level = PT64_ROOT_LEVEL; | |
1487 | } else if (is_pae(vcpu)) { | |
1488 | context->gva_to_gpa = paging64_gva_to_gpa; | |
1489 | context->root_level = PT32E_ROOT_LEVEL; | |
1490 | } else { | |
1491 | context->gva_to_gpa = paging32_gva_to_gpa; | |
1492 | context->root_level = PT32_ROOT_LEVEL; | |
1493 | } | |
1494 | ||
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | static int init_kvm_softmmu(struct kvm_vcpu *vcpu) | |
6aa8b732 AK |
1499 | { |
1500 | ASSERT(vcpu); | |
ad312c7c | 1501 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 AK |
1502 | |
1503 | if (!is_paging(vcpu)) | |
1504 | return nonpaging_init_context(vcpu); | |
a9058ecd | 1505 | else if (is_long_mode(vcpu)) |
6aa8b732 AK |
1506 | return paging64_init_context(vcpu); |
1507 | else if (is_pae(vcpu)) | |
1508 | return paging32E_init_context(vcpu); | |
1509 | else | |
1510 | return paging32_init_context(vcpu); | |
1511 | } | |
1512 | ||
fb72d167 JR |
1513 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
1514 | { | |
1515 | if (tdp_enabled) | |
1516 | return init_kvm_tdp_mmu(vcpu); | |
1517 | else | |
1518 | return init_kvm_softmmu(vcpu); | |
1519 | } | |
1520 | ||
6aa8b732 AK |
1521 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
1522 | { | |
1523 | ASSERT(vcpu); | |
ad312c7c ZX |
1524 | if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) { |
1525 | vcpu->arch.mmu.free(vcpu); | |
1526 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; | |
6aa8b732 AK |
1527 | } |
1528 | } | |
1529 | ||
1530 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) | |
17c3ba9d AK |
1531 | { |
1532 | destroy_kvm_mmu(vcpu); | |
1533 | return init_kvm_mmu(vcpu); | |
1534 | } | |
8668a3c4 | 1535 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
17c3ba9d AK |
1536 | |
1537 | int kvm_mmu_load(struct kvm_vcpu *vcpu) | |
6aa8b732 | 1538 | { |
714b93da AK |
1539 | int r; |
1540 | ||
e2dec939 | 1541 | r = mmu_topup_memory_caches(vcpu); |
17c3ba9d AK |
1542 | if (r) |
1543 | goto out; | |
aaee2c94 | 1544 | spin_lock(&vcpu->kvm->mmu_lock); |
eb787d10 | 1545 | kvm_mmu_free_some_pages(vcpu); |
17c3ba9d | 1546 | mmu_alloc_roots(vcpu); |
aaee2c94 | 1547 | spin_unlock(&vcpu->kvm->mmu_lock); |
ad312c7c | 1548 | kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
17c3ba9d | 1549 | kvm_mmu_flush_tlb(vcpu); |
714b93da AK |
1550 | out: |
1551 | return r; | |
6aa8b732 | 1552 | } |
17c3ba9d AK |
1553 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
1554 | ||
1555 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) | |
1556 | { | |
1557 | mmu_free_roots(vcpu); | |
1558 | } | |
6aa8b732 | 1559 | |
09072daf | 1560 | static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, |
4db35314 | 1561 | struct kvm_mmu_page *sp, |
ac1b714e AK |
1562 | u64 *spte) |
1563 | { | |
1564 | u64 pte; | |
1565 | struct kvm_mmu_page *child; | |
1566 | ||
1567 | pte = *spte; | |
c7addb90 | 1568 | if (is_shadow_present_pte(pte)) { |
05da4558 MT |
1569 | if (sp->role.level == PT_PAGE_TABLE_LEVEL || |
1570 | is_large_pte(pte)) | |
290fc38d | 1571 | rmap_remove(vcpu->kvm, spte); |
ac1b714e AK |
1572 | else { |
1573 | child = page_header(pte & PT64_BASE_ADDR_MASK); | |
90cb0529 | 1574 | mmu_page_remove_parent_pte(child, spte); |
ac1b714e AK |
1575 | } |
1576 | } | |
c7addb90 | 1577 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); |
05da4558 MT |
1578 | if (is_large_pte(pte)) |
1579 | --vcpu->kvm->stat.lpages; | |
ac1b714e AK |
1580 | } |
1581 | ||
0028425f | 1582 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
4db35314 | 1583 | struct kvm_mmu_page *sp, |
0028425f | 1584 | u64 *spte, |
489f1d65 | 1585 | const void *new) |
0028425f | 1586 | { |
05da4558 MT |
1587 | if ((sp->role.level != PT_PAGE_TABLE_LEVEL) |
1588 | && !vcpu->arch.update_pte.largepage) { | |
4cee5764 | 1589 | ++vcpu->kvm->stat.mmu_pde_zapped; |
0028425f | 1590 | return; |
4cee5764 | 1591 | } |
0028425f | 1592 | |
4cee5764 | 1593 | ++vcpu->kvm->stat.mmu_pte_updated; |
4db35314 | 1594 | if (sp->role.glevels == PT32_ROOT_LEVEL) |
489f1d65 | 1595 | paging32_update_pte(vcpu, sp, spte, new); |
0028425f | 1596 | else |
489f1d65 | 1597 | paging64_update_pte(vcpu, sp, spte, new); |
0028425f AK |
1598 | } |
1599 | ||
79539cec AK |
1600 | static bool need_remote_flush(u64 old, u64 new) |
1601 | { | |
1602 | if (!is_shadow_present_pte(old)) | |
1603 | return false; | |
1604 | if (!is_shadow_present_pte(new)) | |
1605 | return true; | |
1606 | if ((old ^ new) & PT64_BASE_ADDR_MASK) | |
1607 | return true; | |
1608 | old ^= PT64_NX_MASK; | |
1609 | new ^= PT64_NX_MASK; | |
1610 | return (old & ~new & PT64_PERM_MASK) != 0; | |
1611 | } | |
1612 | ||
1613 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new) | |
1614 | { | |
1615 | if (need_remote_flush(old, new)) | |
1616 | kvm_flush_remote_tlbs(vcpu->kvm); | |
1617 | else | |
1618 | kvm_mmu_flush_tlb(vcpu); | |
1619 | } | |
1620 | ||
12b7d28f AK |
1621 | static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu) |
1622 | { | |
ad312c7c | 1623 | u64 *spte = vcpu->arch.last_pte_updated; |
12b7d28f AK |
1624 | |
1625 | return !!(spte && (*spte & PT_ACCESSED_MASK)); | |
1626 | } | |
1627 | ||
d7824fff AK |
1628 | static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
1629 | const u8 *new, int bytes) | |
1630 | { | |
1631 | gfn_t gfn; | |
1632 | int r; | |
1633 | u64 gpte = 0; | |
72dc67a6 | 1634 | struct page *page; |
d7824fff | 1635 | |
05da4558 MT |
1636 | vcpu->arch.update_pte.largepage = 0; |
1637 | ||
d7824fff AK |
1638 | if (bytes != 4 && bytes != 8) |
1639 | return; | |
1640 | ||
1641 | /* | |
1642 | * Assume that the pte write on a page table of the same type | |
1643 | * as the current vcpu paging mode. This is nearly always true | |
1644 | * (might be false while changing modes). Note it is verified later | |
1645 | * by update_pte(). | |
1646 | */ | |
1647 | if (is_pae(vcpu)) { | |
1648 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ | |
1649 | if ((bytes == 4) && (gpa % 4 == 0)) { | |
1650 | r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8); | |
1651 | if (r) | |
1652 | return; | |
1653 | memcpy((void *)&gpte + (gpa % 8), new, 4); | |
1654 | } else if ((bytes == 8) && (gpa % 8 == 0)) { | |
1655 | memcpy((void *)&gpte, new, 8); | |
1656 | } | |
1657 | } else { | |
1658 | if ((bytes == 4) && (gpa % 4 == 0)) | |
1659 | memcpy((void *)&gpte, new, 4); | |
1660 | } | |
1661 | if (!is_present_pte(gpte)) | |
1662 | return; | |
1663 | gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; | |
72dc67a6 | 1664 | |
05da4558 MT |
1665 | down_read(¤t->mm->mmap_sem); |
1666 | if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) { | |
1667 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); | |
1668 | vcpu->arch.update_pte.largepage = 1; | |
1669 | } | |
72dc67a6 | 1670 | page = gfn_to_page(vcpu->kvm, gfn); |
05da4558 | 1671 | up_read(¤t->mm->mmap_sem); |
72dc67a6 | 1672 | |
d196e343 AK |
1673 | if (is_error_page(page)) { |
1674 | kvm_release_page_clean(page); | |
1675 | return; | |
1676 | } | |
d7824fff | 1677 | vcpu->arch.update_pte.gfn = gfn; |
e48bb497 | 1678 | vcpu->arch.update_pte.page = page; |
d7824fff AK |
1679 | } |
1680 | ||
09072daf | 1681 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
fe551881 | 1682 | const u8 *new, int bytes) |
da4a00f0 | 1683 | { |
9b7a0325 | 1684 | gfn_t gfn = gpa >> PAGE_SHIFT; |
4db35314 | 1685 | struct kvm_mmu_page *sp; |
0e7bc4b9 | 1686 | struct hlist_node *node, *n; |
9b7a0325 AK |
1687 | struct hlist_head *bucket; |
1688 | unsigned index; | |
489f1d65 | 1689 | u64 entry, gentry; |
9b7a0325 | 1690 | u64 *spte; |
9b7a0325 | 1691 | unsigned offset = offset_in_page(gpa); |
0e7bc4b9 | 1692 | unsigned pte_size; |
9b7a0325 | 1693 | unsigned page_offset; |
0e7bc4b9 | 1694 | unsigned misaligned; |
fce0657f | 1695 | unsigned quadrant; |
9b7a0325 | 1696 | int level; |
86a5ba02 | 1697 | int flooded = 0; |
ac1b714e | 1698 | int npte; |
489f1d65 | 1699 | int r; |
9b7a0325 | 1700 | |
b8688d51 | 1701 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
d7824fff | 1702 | mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes); |
aaee2c94 | 1703 | spin_lock(&vcpu->kvm->mmu_lock); |
eb787d10 | 1704 | kvm_mmu_free_some_pages(vcpu); |
4cee5764 | 1705 | ++vcpu->kvm->stat.mmu_pte_write; |
c7addb90 | 1706 | kvm_mmu_audit(vcpu, "pre pte write"); |
ad312c7c | 1707 | if (gfn == vcpu->arch.last_pt_write_gfn |
12b7d28f | 1708 | && !last_updated_pte_accessed(vcpu)) { |
ad312c7c ZX |
1709 | ++vcpu->arch.last_pt_write_count; |
1710 | if (vcpu->arch.last_pt_write_count >= 3) | |
86a5ba02 AK |
1711 | flooded = 1; |
1712 | } else { | |
ad312c7c ZX |
1713 | vcpu->arch.last_pt_write_gfn = gfn; |
1714 | vcpu->arch.last_pt_write_count = 1; | |
1715 | vcpu->arch.last_pte_updated = NULL; | |
86a5ba02 | 1716 | } |
1ae0a13d | 1717 | index = kvm_page_table_hashfn(gfn); |
f05e70ac | 1718 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
4db35314 AK |
1719 | hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) { |
1720 | if (sp->gfn != gfn || sp->role.metaphysical) | |
9b7a0325 | 1721 | continue; |
4db35314 | 1722 | pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8; |
0e7bc4b9 | 1723 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
e925c5ba | 1724 | misaligned |= bytes < 4; |
86a5ba02 | 1725 | if (misaligned || flooded) { |
0e7bc4b9 AK |
1726 | /* |
1727 | * Misaligned accesses are too much trouble to fix | |
1728 | * up; also, they usually indicate a page is not used | |
1729 | * as a page table. | |
86a5ba02 AK |
1730 | * |
1731 | * If we're seeing too many writes to a page, | |
1732 | * it may no longer be a page table, or we may be | |
1733 | * forking, in which case it is better to unmap the | |
1734 | * page. | |
0e7bc4b9 AK |
1735 | */ |
1736 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", | |
4db35314 AK |
1737 | gpa, bytes, sp->role.word); |
1738 | kvm_mmu_zap_page(vcpu->kvm, sp); | |
4cee5764 | 1739 | ++vcpu->kvm->stat.mmu_flooded; |
0e7bc4b9 AK |
1740 | continue; |
1741 | } | |
9b7a0325 | 1742 | page_offset = offset; |
4db35314 | 1743 | level = sp->role.level; |
ac1b714e | 1744 | npte = 1; |
4db35314 | 1745 | if (sp->role.glevels == PT32_ROOT_LEVEL) { |
ac1b714e AK |
1746 | page_offset <<= 1; /* 32->64 */ |
1747 | /* | |
1748 | * A 32-bit pde maps 4MB while the shadow pdes map | |
1749 | * only 2MB. So we need to double the offset again | |
1750 | * and zap two pdes instead of one. | |
1751 | */ | |
1752 | if (level == PT32_ROOT_LEVEL) { | |
6b8d0f9b | 1753 | page_offset &= ~7; /* kill rounding error */ |
ac1b714e AK |
1754 | page_offset <<= 1; |
1755 | npte = 2; | |
1756 | } | |
fce0657f | 1757 | quadrant = page_offset >> PAGE_SHIFT; |
9b7a0325 | 1758 | page_offset &= ~PAGE_MASK; |
4db35314 | 1759 | if (quadrant != sp->role.quadrant) |
fce0657f | 1760 | continue; |
9b7a0325 | 1761 | } |
4db35314 | 1762 | spte = &sp->spt[page_offset / sizeof(*spte)]; |
489f1d65 DE |
1763 | if ((gpa & (pte_size - 1)) || (bytes < pte_size)) { |
1764 | gentry = 0; | |
1765 | r = kvm_read_guest_atomic(vcpu->kvm, | |
1766 | gpa & ~(u64)(pte_size - 1), | |
1767 | &gentry, pte_size); | |
1768 | new = (const void *)&gentry; | |
1769 | if (r < 0) | |
1770 | new = NULL; | |
1771 | } | |
ac1b714e | 1772 | while (npte--) { |
79539cec | 1773 | entry = *spte; |
4db35314 | 1774 | mmu_pte_write_zap_pte(vcpu, sp, spte); |
489f1d65 DE |
1775 | if (new) |
1776 | mmu_pte_write_new_pte(vcpu, sp, spte, new); | |
79539cec | 1777 | mmu_pte_write_flush_tlb(vcpu, entry, *spte); |
ac1b714e | 1778 | ++spte; |
9b7a0325 | 1779 | } |
9b7a0325 | 1780 | } |
c7addb90 | 1781 | kvm_mmu_audit(vcpu, "post pte write"); |
aaee2c94 | 1782 | spin_unlock(&vcpu->kvm->mmu_lock); |
d7824fff AK |
1783 | if (vcpu->arch.update_pte.page) { |
1784 | kvm_release_page_clean(vcpu->arch.update_pte.page); | |
1785 | vcpu->arch.update_pte.page = NULL; | |
1786 | } | |
da4a00f0 AK |
1787 | } |
1788 | ||
a436036b AK |
1789 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
1790 | { | |
10589a46 MT |
1791 | gpa_t gpa; |
1792 | int r; | |
a436036b | 1793 | |
72dc67a6 | 1794 | down_read(&vcpu->kvm->slots_lock); |
10589a46 | 1795 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva); |
72dc67a6 | 1796 | up_read(&vcpu->kvm->slots_lock); |
10589a46 | 1797 | |
aaee2c94 | 1798 | spin_lock(&vcpu->kvm->mmu_lock); |
10589a46 | 1799 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
aaee2c94 | 1800 | spin_unlock(&vcpu->kvm->mmu_lock); |
10589a46 | 1801 | return r; |
a436036b AK |
1802 | } |
1803 | ||
22d95b12 | 1804 | void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
ebeace86 | 1805 | { |
f05e70ac | 1806 | while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) { |
4db35314 | 1807 | struct kvm_mmu_page *sp; |
ebeace86 | 1808 | |
f05e70ac | 1809 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev, |
4db35314 AK |
1810 | struct kvm_mmu_page, link); |
1811 | kvm_mmu_zap_page(vcpu->kvm, sp); | |
4cee5764 | 1812 | ++vcpu->kvm->stat.mmu_recycled; |
ebeace86 AK |
1813 | } |
1814 | } | |
ebeace86 | 1815 | |
3067714c AK |
1816 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code) |
1817 | { | |
1818 | int r; | |
1819 | enum emulation_result er; | |
1820 | ||
ad312c7c | 1821 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code); |
3067714c AK |
1822 | if (r < 0) |
1823 | goto out; | |
1824 | ||
1825 | if (!r) { | |
1826 | r = 1; | |
1827 | goto out; | |
1828 | } | |
1829 | ||
b733bfb5 AK |
1830 | r = mmu_topup_memory_caches(vcpu); |
1831 | if (r) | |
1832 | goto out; | |
1833 | ||
3067714c | 1834 | er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0); |
3067714c AK |
1835 | |
1836 | switch (er) { | |
1837 | case EMULATE_DONE: | |
1838 | return 1; | |
1839 | case EMULATE_DO_MMIO: | |
1840 | ++vcpu->stat.mmio_exits; | |
1841 | return 0; | |
1842 | case EMULATE_FAIL: | |
1843 | kvm_report_emulation_failure(vcpu, "pagetable"); | |
1844 | return 1; | |
1845 | default: | |
1846 | BUG(); | |
1847 | } | |
1848 | out: | |
3067714c AK |
1849 | return r; |
1850 | } | |
1851 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); | |
1852 | ||
18552672 JR |
1853 | void kvm_enable_tdp(void) |
1854 | { | |
1855 | tdp_enabled = true; | |
1856 | } | |
1857 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); | |
1858 | ||
6aa8b732 AK |
1859 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
1860 | { | |
4db35314 | 1861 | struct kvm_mmu_page *sp; |
6aa8b732 | 1862 | |
f05e70ac ZX |
1863 | while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) { |
1864 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.next, | |
4db35314 AK |
1865 | struct kvm_mmu_page, link); |
1866 | kvm_mmu_zap_page(vcpu->kvm, sp); | |
f51234c2 | 1867 | } |
ad312c7c | 1868 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
6aa8b732 AK |
1869 | } |
1870 | ||
1871 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) | |
1872 | { | |
17ac10ad | 1873 | struct page *page; |
6aa8b732 AK |
1874 | int i; |
1875 | ||
1876 | ASSERT(vcpu); | |
1877 | ||
f05e70ac ZX |
1878 | if (vcpu->kvm->arch.n_requested_mmu_pages) |
1879 | vcpu->kvm->arch.n_free_mmu_pages = | |
1880 | vcpu->kvm->arch.n_requested_mmu_pages; | |
82ce2c96 | 1881 | else |
f05e70ac ZX |
1882 | vcpu->kvm->arch.n_free_mmu_pages = |
1883 | vcpu->kvm->arch.n_alloc_mmu_pages; | |
17ac10ad AK |
1884 | /* |
1885 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. | |
1886 | * Therefore we need to allocate shadow page tables in the first | |
1887 | * 4GB of memory, which happens to fit the DMA32 zone. | |
1888 | */ | |
1889 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); | |
1890 | if (!page) | |
1891 | goto error_1; | |
ad312c7c | 1892 | vcpu->arch.mmu.pae_root = page_address(page); |
17ac10ad | 1893 | for (i = 0; i < 4; ++i) |
ad312c7c | 1894 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
17ac10ad | 1895 | |
6aa8b732 AK |
1896 | return 0; |
1897 | ||
1898 | error_1: | |
1899 | free_mmu_pages(vcpu); | |
1900 | return -ENOMEM; | |
1901 | } | |
1902 | ||
8018c27b | 1903 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
6aa8b732 | 1904 | { |
6aa8b732 | 1905 | ASSERT(vcpu); |
ad312c7c | 1906 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
6aa8b732 | 1907 | |
8018c27b IM |
1908 | return alloc_mmu_pages(vcpu); |
1909 | } | |
6aa8b732 | 1910 | |
8018c27b IM |
1911 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
1912 | { | |
1913 | ASSERT(vcpu); | |
ad312c7c | 1914 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
2c264957 | 1915 | |
8018c27b | 1916 | return init_kvm_mmu(vcpu); |
6aa8b732 AK |
1917 | } |
1918 | ||
1919 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) | |
1920 | { | |
1921 | ASSERT(vcpu); | |
1922 | ||
1923 | destroy_kvm_mmu(vcpu); | |
1924 | free_mmu_pages(vcpu); | |
714b93da | 1925 | mmu_free_memory_caches(vcpu); |
6aa8b732 AK |
1926 | } |
1927 | ||
90cb0529 | 1928 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
6aa8b732 | 1929 | { |
4db35314 | 1930 | struct kvm_mmu_page *sp; |
6aa8b732 | 1931 | |
f05e70ac | 1932 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
6aa8b732 AK |
1933 | int i; |
1934 | u64 *pt; | |
1935 | ||
4db35314 | 1936 | if (!test_bit(slot, &sp->slot_bitmap)) |
6aa8b732 AK |
1937 | continue; |
1938 | ||
4db35314 | 1939 | pt = sp->spt; |
6aa8b732 AK |
1940 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
1941 | /* avoid RMW */ | |
9647c14c | 1942 | if (pt[i] & PT_WRITABLE_MASK) |
6aa8b732 | 1943 | pt[i] &= ~PT_WRITABLE_MASK; |
6aa8b732 AK |
1944 | } |
1945 | } | |
37a7d8b0 | 1946 | |
90cb0529 | 1947 | void kvm_mmu_zap_all(struct kvm *kvm) |
e0fa826f | 1948 | { |
4db35314 | 1949 | struct kvm_mmu_page *sp, *node; |
e0fa826f | 1950 | |
aaee2c94 | 1951 | spin_lock(&kvm->mmu_lock); |
f05e70ac | 1952 | list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) |
4db35314 | 1953 | kvm_mmu_zap_page(kvm, sp); |
aaee2c94 | 1954 | spin_unlock(&kvm->mmu_lock); |
e0fa826f | 1955 | |
90cb0529 | 1956 | kvm_flush_remote_tlbs(kvm); |
e0fa826f DL |
1957 | } |
1958 | ||
b5a33a75 AK |
1959 | void kvm_mmu_module_exit(void) |
1960 | { | |
1961 | if (pte_chain_cache) | |
1962 | kmem_cache_destroy(pte_chain_cache); | |
1963 | if (rmap_desc_cache) | |
1964 | kmem_cache_destroy(rmap_desc_cache); | |
d3d25b04 AK |
1965 | if (mmu_page_header_cache) |
1966 | kmem_cache_destroy(mmu_page_header_cache); | |
b5a33a75 AK |
1967 | } |
1968 | ||
1969 | int kvm_mmu_module_init(void) | |
1970 | { | |
1971 | pte_chain_cache = kmem_cache_create("kvm_pte_chain", | |
1972 | sizeof(struct kvm_pte_chain), | |
20c2df83 | 1973 | 0, 0, NULL); |
b5a33a75 AK |
1974 | if (!pte_chain_cache) |
1975 | goto nomem; | |
1976 | rmap_desc_cache = kmem_cache_create("kvm_rmap_desc", | |
1977 | sizeof(struct kvm_rmap_desc), | |
20c2df83 | 1978 | 0, 0, NULL); |
b5a33a75 AK |
1979 | if (!rmap_desc_cache) |
1980 | goto nomem; | |
1981 | ||
d3d25b04 AK |
1982 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
1983 | sizeof(struct kvm_mmu_page), | |
20c2df83 | 1984 | 0, 0, NULL); |
d3d25b04 AK |
1985 | if (!mmu_page_header_cache) |
1986 | goto nomem; | |
1987 | ||
b5a33a75 AK |
1988 | return 0; |
1989 | ||
1990 | nomem: | |
1991 | kvm_mmu_module_exit(); | |
1992 | return -ENOMEM; | |
1993 | } | |
1994 | ||
3ad82a7e ZX |
1995 | /* |
1996 | * Caculate mmu pages needed for kvm. | |
1997 | */ | |
1998 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) | |
1999 | { | |
2000 | int i; | |
2001 | unsigned int nr_mmu_pages; | |
2002 | unsigned int nr_pages = 0; | |
2003 | ||
2004 | for (i = 0; i < kvm->nmemslots; i++) | |
2005 | nr_pages += kvm->memslots[i].npages; | |
2006 | ||
2007 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; | |
2008 | nr_mmu_pages = max(nr_mmu_pages, | |
2009 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); | |
2010 | ||
2011 | return nr_mmu_pages; | |
2012 | } | |
2013 | ||
2f333bcb MT |
2014 | static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
2015 | unsigned len) | |
2016 | { | |
2017 | if (len > buffer->len) | |
2018 | return NULL; | |
2019 | return buffer->ptr; | |
2020 | } | |
2021 | ||
2022 | static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer, | |
2023 | unsigned len) | |
2024 | { | |
2025 | void *ret; | |
2026 | ||
2027 | ret = pv_mmu_peek_buffer(buffer, len); | |
2028 | if (!ret) | |
2029 | return ret; | |
2030 | buffer->ptr += len; | |
2031 | buffer->len -= len; | |
2032 | buffer->processed += len; | |
2033 | return ret; | |
2034 | } | |
2035 | ||
2036 | static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu, | |
2037 | gpa_t addr, gpa_t value) | |
2038 | { | |
2039 | int bytes = 8; | |
2040 | int r; | |
2041 | ||
2042 | if (!is_long_mode(vcpu) && !is_pae(vcpu)) | |
2043 | bytes = 4; | |
2044 | ||
2045 | r = mmu_topup_memory_caches(vcpu); | |
2046 | if (r) | |
2047 | return r; | |
2048 | ||
2049 | if (!__emulator_write_phys(vcpu, addr, &value, bytes)) | |
2050 | return -EFAULT; | |
2051 | ||
2052 | return 1; | |
2053 | } | |
2054 | ||
2055 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) | |
2056 | { | |
2057 | kvm_x86_ops->tlb_flush(vcpu); | |
2058 | return 1; | |
2059 | } | |
2060 | ||
2061 | static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr) | |
2062 | { | |
2063 | spin_lock(&vcpu->kvm->mmu_lock); | |
2064 | mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT); | |
2065 | spin_unlock(&vcpu->kvm->mmu_lock); | |
2066 | return 1; | |
2067 | } | |
2068 | ||
2069 | static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu, | |
2070 | struct kvm_pv_mmu_op_buffer *buffer) | |
2071 | { | |
2072 | struct kvm_mmu_op_header *header; | |
2073 | ||
2074 | header = pv_mmu_peek_buffer(buffer, sizeof *header); | |
2075 | if (!header) | |
2076 | return 0; | |
2077 | switch (header->op) { | |
2078 | case KVM_MMU_OP_WRITE_PTE: { | |
2079 | struct kvm_mmu_op_write_pte *wpte; | |
2080 | ||
2081 | wpte = pv_mmu_read_buffer(buffer, sizeof *wpte); | |
2082 | if (!wpte) | |
2083 | return 0; | |
2084 | return kvm_pv_mmu_write(vcpu, wpte->pte_phys, | |
2085 | wpte->pte_val); | |
2086 | } | |
2087 | case KVM_MMU_OP_FLUSH_TLB: { | |
2088 | struct kvm_mmu_op_flush_tlb *ftlb; | |
2089 | ||
2090 | ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb); | |
2091 | if (!ftlb) | |
2092 | return 0; | |
2093 | return kvm_pv_mmu_flush_tlb(vcpu); | |
2094 | } | |
2095 | case KVM_MMU_OP_RELEASE_PT: { | |
2096 | struct kvm_mmu_op_release_pt *rpt; | |
2097 | ||
2098 | rpt = pv_mmu_read_buffer(buffer, sizeof *rpt); | |
2099 | if (!rpt) | |
2100 | return 0; | |
2101 | return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys); | |
2102 | } | |
2103 | default: return 0; | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes, | |
2108 | gpa_t addr, unsigned long *ret) | |
2109 | { | |
2110 | int r; | |
2111 | struct kvm_pv_mmu_op_buffer buffer; | |
2112 | ||
2113 | down_read(&vcpu->kvm->slots_lock); | |
2114 | down_read(¤t->mm->mmap_sem); | |
2115 | ||
2116 | buffer.ptr = buffer.buf; | |
2117 | buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf); | |
2118 | buffer.processed = 0; | |
2119 | ||
2120 | r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len); | |
2121 | if (r) | |
2122 | goto out; | |
2123 | ||
2124 | while (buffer.len) { | |
2125 | r = kvm_pv_mmu_op_one(vcpu, &buffer); | |
2126 | if (r < 0) | |
2127 | goto out; | |
2128 | if (r == 0) | |
2129 | break; | |
2130 | } | |
2131 | ||
2132 | r = 1; | |
2133 | out: | |
2134 | *ret = buffer.processed; | |
2135 | up_read(¤t->mm->mmap_sem); | |
2136 | up_read(&vcpu->kvm->slots_lock); | |
2137 | return r; | |
2138 | } | |
2139 | ||
37a7d8b0 AK |
2140 | #ifdef AUDIT |
2141 | ||
2142 | static const char *audit_msg; | |
2143 | ||
2144 | static gva_t canonicalize(gva_t gva) | |
2145 | { | |
2146 | #ifdef CONFIG_X86_64 | |
2147 | gva = (long long)(gva << 16) >> 16; | |
2148 | #endif | |
2149 | return gva; | |
2150 | } | |
2151 | ||
2152 | static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte, | |
2153 | gva_t va, int level) | |
2154 | { | |
2155 | u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK); | |
2156 | int i; | |
2157 | gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1)); | |
2158 | ||
2159 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) { | |
2160 | u64 ent = pt[i]; | |
2161 | ||
c7addb90 | 2162 | if (ent == shadow_trap_nonpresent_pte) |
37a7d8b0 AK |
2163 | continue; |
2164 | ||
2165 | va = canonicalize(va); | |
c7addb90 AK |
2166 | if (level > 1) { |
2167 | if (ent == shadow_notrap_nonpresent_pte) | |
2168 | printk(KERN_ERR "audit: (%s) nontrapping pte" | |
2169 | " in nonleaf level: levels %d gva %lx" | |
2170 | " level %d pte %llx\n", audit_msg, | |
ad312c7c | 2171 | vcpu->arch.mmu.root_level, va, level, ent); |
c7addb90 | 2172 | |
37a7d8b0 | 2173 | audit_mappings_page(vcpu, ent, va, level - 1); |
c7addb90 | 2174 | } else { |
ad312c7c | 2175 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va); |
1d28f5f4 AK |
2176 | struct page *page = gpa_to_page(vcpu, gpa); |
2177 | hpa_t hpa = page_to_phys(page); | |
37a7d8b0 | 2178 | |
c7addb90 | 2179 | if (is_shadow_present_pte(ent) |
37a7d8b0 | 2180 | && (ent & PT64_BASE_ADDR_MASK) != hpa) |
c7addb90 AK |
2181 | printk(KERN_ERR "xx audit error: (%s) levels %d" |
2182 | " gva %lx gpa %llx hpa %llx ent %llx %d\n", | |
ad312c7c | 2183 | audit_msg, vcpu->arch.mmu.root_level, |
d77c26fc MD |
2184 | va, gpa, hpa, ent, |
2185 | is_shadow_present_pte(ent)); | |
c7addb90 AK |
2186 | else if (ent == shadow_notrap_nonpresent_pte |
2187 | && !is_error_hpa(hpa)) | |
2188 | printk(KERN_ERR "audit: (%s) notrap shadow," | |
2189 | " valid guest gva %lx\n", audit_msg, va); | |
b4231d61 | 2190 | kvm_release_page_clean(page); |
c7addb90 | 2191 | |
37a7d8b0 AK |
2192 | } |
2193 | } | |
2194 | } | |
2195 | ||
2196 | static void audit_mappings(struct kvm_vcpu *vcpu) | |
2197 | { | |
1ea252af | 2198 | unsigned i; |
37a7d8b0 | 2199 | |
ad312c7c ZX |
2200 | if (vcpu->arch.mmu.root_level == 4) |
2201 | audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4); | |
37a7d8b0 AK |
2202 | else |
2203 | for (i = 0; i < 4; ++i) | |
ad312c7c | 2204 | if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK) |
37a7d8b0 | 2205 | audit_mappings_page(vcpu, |
ad312c7c | 2206 | vcpu->arch.mmu.pae_root[i], |
37a7d8b0 AK |
2207 | i << 30, |
2208 | 2); | |
2209 | } | |
2210 | ||
2211 | static int count_rmaps(struct kvm_vcpu *vcpu) | |
2212 | { | |
2213 | int nmaps = 0; | |
2214 | int i, j, k; | |
2215 | ||
2216 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
2217 | struct kvm_memory_slot *m = &vcpu->kvm->memslots[i]; | |
2218 | struct kvm_rmap_desc *d; | |
2219 | ||
2220 | for (j = 0; j < m->npages; ++j) { | |
290fc38d | 2221 | unsigned long *rmapp = &m->rmap[j]; |
37a7d8b0 | 2222 | |
290fc38d | 2223 | if (!*rmapp) |
37a7d8b0 | 2224 | continue; |
290fc38d | 2225 | if (!(*rmapp & 1)) { |
37a7d8b0 AK |
2226 | ++nmaps; |
2227 | continue; | |
2228 | } | |
290fc38d | 2229 | d = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
37a7d8b0 AK |
2230 | while (d) { |
2231 | for (k = 0; k < RMAP_EXT; ++k) | |
2232 | if (d->shadow_ptes[k]) | |
2233 | ++nmaps; | |
2234 | else | |
2235 | break; | |
2236 | d = d->more; | |
2237 | } | |
2238 | } | |
2239 | } | |
2240 | return nmaps; | |
2241 | } | |
2242 | ||
2243 | static int count_writable_mappings(struct kvm_vcpu *vcpu) | |
2244 | { | |
2245 | int nmaps = 0; | |
4db35314 | 2246 | struct kvm_mmu_page *sp; |
37a7d8b0 AK |
2247 | int i; |
2248 | ||
f05e70ac | 2249 | list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) { |
4db35314 | 2250 | u64 *pt = sp->spt; |
37a7d8b0 | 2251 | |
4db35314 | 2252 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) |
37a7d8b0 AK |
2253 | continue; |
2254 | ||
2255 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { | |
2256 | u64 ent = pt[i]; | |
2257 | ||
2258 | if (!(ent & PT_PRESENT_MASK)) | |
2259 | continue; | |
2260 | if (!(ent & PT_WRITABLE_MASK)) | |
2261 | continue; | |
2262 | ++nmaps; | |
2263 | } | |
2264 | } | |
2265 | return nmaps; | |
2266 | } | |
2267 | ||
2268 | static void audit_rmap(struct kvm_vcpu *vcpu) | |
2269 | { | |
2270 | int n_rmap = count_rmaps(vcpu); | |
2271 | int n_actual = count_writable_mappings(vcpu); | |
2272 | ||
2273 | if (n_rmap != n_actual) | |
2274 | printk(KERN_ERR "%s: (%s) rmap %d actual %d\n", | |
b8688d51 | 2275 | __func__, audit_msg, n_rmap, n_actual); |
37a7d8b0 AK |
2276 | } |
2277 | ||
2278 | static void audit_write_protection(struct kvm_vcpu *vcpu) | |
2279 | { | |
4db35314 | 2280 | struct kvm_mmu_page *sp; |
290fc38d IE |
2281 | struct kvm_memory_slot *slot; |
2282 | unsigned long *rmapp; | |
2283 | gfn_t gfn; | |
37a7d8b0 | 2284 | |
f05e70ac | 2285 | list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) { |
4db35314 | 2286 | if (sp->role.metaphysical) |
37a7d8b0 AK |
2287 | continue; |
2288 | ||
4db35314 AK |
2289 | slot = gfn_to_memslot(vcpu->kvm, sp->gfn); |
2290 | gfn = unalias_gfn(vcpu->kvm, sp->gfn); | |
290fc38d IE |
2291 | rmapp = &slot->rmap[gfn - slot->base_gfn]; |
2292 | if (*rmapp) | |
37a7d8b0 AK |
2293 | printk(KERN_ERR "%s: (%s) shadow page has writable" |
2294 | " mappings: gfn %lx role %x\n", | |
b8688d51 | 2295 | __func__, audit_msg, sp->gfn, |
4db35314 | 2296 | sp->role.word); |
37a7d8b0 AK |
2297 | } |
2298 | } | |
2299 | ||
2300 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) | |
2301 | { | |
2302 | int olddbg = dbg; | |
2303 | ||
2304 | dbg = 0; | |
2305 | audit_msg = msg; | |
2306 | audit_rmap(vcpu); | |
2307 | audit_write_protection(vcpu); | |
2308 | audit_mappings(vcpu); | |
2309 | dbg = olddbg; | |
2310 | } | |
2311 | ||
2312 | #endif |