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