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