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