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 | */ | |
19 | #include <linux/types.h> | |
20 | #include <linux/string.h> | |
21 | #include <asm/page.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/highmem.h> | |
24 | #include <linux/module.h> | |
25 | ||
26 | #include "vmx.h" | |
27 | #include "kvm.h" | |
28 | ||
cea0f0e7 AK |
29 | #define pgprintk(x...) do { printk(x); } while (0) |
30 | #define rmap_printk(x...) do { printk(x); } while (0) | |
6aa8b732 AK |
31 | |
32 | #define ASSERT(x) \ | |
33 | if (!(x)) { \ | |
34 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ | |
35 | __FILE__, __LINE__, #x); \ | |
36 | } | |
37 | ||
cea0f0e7 AK |
38 | #define PT64_PT_BITS 9 |
39 | #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS) | |
40 | #define PT32_PT_BITS 10 | |
41 | #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS) | |
6aa8b732 AK |
42 | |
43 | #define PT_WRITABLE_SHIFT 1 | |
44 | ||
45 | #define PT_PRESENT_MASK (1ULL << 0) | |
46 | #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT) | |
47 | #define PT_USER_MASK (1ULL << 2) | |
48 | #define PT_PWT_MASK (1ULL << 3) | |
49 | #define PT_PCD_MASK (1ULL << 4) | |
50 | #define PT_ACCESSED_MASK (1ULL << 5) | |
51 | #define PT_DIRTY_MASK (1ULL << 6) | |
52 | #define PT_PAGE_SIZE_MASK (1ULL << 7) | |
53 | #define PT_PAT_MASK (1ULL << 7) | |
54 | #define PT_GLOBAL_MASK (1ULL << 8) | |
55 | #define PT64_NX_MASK (1ULL << 63) | |
56 | ||
57 | #define PT_PAT_SHIFT 7 | |
58 | #define PT_DIR_PAT_SHIFT 12 | |
59 | #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT) | |
60 | ||
61 | #define PT32_DIR_PSE36_SIZE 4 | |
62 | #define PT32_DIR_PSE36_SHIFT 13 | |
63 | #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT) | |
64 | ||
65 | ||
66 | #define PT32_PTE_COPY_MASK \ | |
8c7bb723 | 67 | (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK) |
6aa8b732 | 68 | |
8c7bb723 | 69 | #define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK) |
6aa8b732 AK |
70 | |
71 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 | |
72 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 | |
73 | ||
74 | #define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) | |
75 | #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) | |
76 | ||
77 | #define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1) | |
78 | #define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT) | |
79 | ||
80 | #define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1) | |
81 | #define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT)) | |
82 | ||
83 | #define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT) | |
84 | ||
85 | #define VALID_PAGE(x) ((x) != INVALID_PAGE) | |
86 | ||
87 | #define PT64_LEVEL_BITS 9 | |
88 | ||
89 | #define PT64_LEVEL_SHIFT(level) \ | |
90 | ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS ) | |
91 | ||
92 | #define PT64_LEVEL_MASK(level) \ | |
93 | (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level)) | |
94 | ||
95 | #define PT64_INDEX(address, level)\ | |
96 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) | |
97 | ||
98 | ||
99 | #define PT32_LEVEL_BITS 10 | |
100 | ||
101 | #define PT32_LEVEL_SHIFT(level) \ | |
102 | ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS ) | |
103 | ||
104 | #define PT32_LEVEL_MASK(level) \ | |
105 | (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level)) | |
106 | ||
107 | #define PT32_INDEX(address, level)\ | |
108 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) | |
109 | ||
110 | ||
111 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & PAGE_MASK) | |
112 | #define PT64_DIR_BASE_ADDR_MASK \ | |
113 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) | |
114 | ||
115 | #define PT32_BASE_ADDR_MASK PAGE_MASK | |
116 | #define PT32_DIR_BASE_ADDR_MASK \ | |
117 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) | |
118 | ||
119 | ||
120 | #define PFERR_PRESENT_MASK (1U << 0) | |
121 | #define PFERR_WRITE_MASK (1U << 1) | |
122 | #define PFERR_USER_MASK (1U << 2) | |
123 | ||
124 | #define PT64_ROOT_LEVEL 4 | |
125 | #define PT32_ROOT_LEVEL 2 | |
126 | #define PT32E_ROOT_LEVEL 3 | |
127 | ||
128 | #define PT_DIRECTORY_LEVEL 2 | |
129 | #define PT_PAGE_TABLE_LEVEL 1 | |
130 | ||
cd4a4e53 AK |
131 | #define RMAP_EXT 4 |
132 | ||
133 | struct kvm_rmap_desc { | |
134 | u64 *shadow_ptes[RMAP_EXT]; | |
135 | struct kvm_rmap_desc *more; | |
136 | }; | |
137 | ||
6aa8b732 AK |
138 | static int is_write_protection(struct kvm_vcpu *vcpu) |
139 | { | |
140 | return vcpu->cr0 & CR0_WP_MASK; | |
141 | } | |
142 | ||
143 | static int is_cpuid_PSE36(void) | |
144 | { | |
145 | return 1; | |
146 | } | |
147 | ||
148 | static int is_present_pte(unsigned long pte) | |
149 | { | |
150 | return pte & PT_PRESENT_MASK; | |
151 | } | |
152 | ||
153 | static int is_writeble_pte(unsigned long pte) | |
154 | { | |
155 | return pte & PT_WRITABLE_MASK; | |
156 | } | |
157 | ||
158 | static int is_io_pte(unsigned long pte) | |
159 | { | |
160 | return pte & PT_SHADOW_IO_MARK; | |
161 | } | |
162 | ||
cd4a4e53 AK |
163 | static int is_rmap_pte(u64 pte) |
164 | { | |
165 | return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK)) | |
166 | == (PT_WRITABLE_MASK | PT_PRESENT_MASK); | |
167 | } | |
168 | ||
169 | /* | |
170 | * Reverse mapping data structures: | |
171 | * | |
172 | * If page->private bit zero is zero, then page->private points to the | |
173 | * shadow page table entry that points to page_address(page). | |
174 | * | |
175 | * If page->private bit zero is one, (then page->private & ~1) points | |
176 | * to a struct kvm_rmap_desc containing more mappings. | |
177 | */ | |
178 | static void rmap_add(struct kvm *kvm, u64 *spte) | |
179 | { | |
180 | struct page *page; | |
181 | struct kvm_rmap_desc *desc; | |
182 | int i; | |
183 | ||
184 | if (!is_rmap_pte(*spte)) | |
185 | return; | |
186 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); | |
187 | if (!page->private) { | |
188 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); | |
189 | page->private = (unsigned long)spte; | |
190 | } else if (!(page->private & 1)) { | |
191 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); | |
192 | desc = kzalloc(sizeof *desc, GFP_NOWAIT); | |
193 | if (!desc) | |
194 | BUG(); /* FIXME: return error */ | |
195 | desc->shadow_ptes[0] = (u64 *)page->private; | |
196 | desc->shadow_ptes[1] = spte; | |
197 | page->private = (unsigned long)desc | 1; | |
198 | } else { | |
199 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); | |
200 | desc = (struct kvm_rmap_desc *)(page->private & ~1ul); | |
201 | while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) | |
202 | desc = desc->more; | |
203 | if (desc->shadow_ptes[RMAP_EXT-1]) { | |
204 | desc->more = kzalloc(sizeof *desc->more, GFP_NOWAIT); | |
205 | if (!desc->more) | |
206 | BUG(); /* FIXME: return error */ | |
207 | desc = desc->more; | |
208 | } | |
209 | for (i = 0; desc->shadow_ptes[i]; ++i) | |
210 | ; | |
211 | desc->shadow_ptes[i] = spte; | |
212 | } | |
213 | } | |
214 | ||
215 | static void rmap_desc_remove_entry(struct page *page, | |
216 | struct kvm_rmap_desc *desc, | |
217 | int i, | |
218 | struct kvm_rmap_desc *prev_desc) | |
219 | { | |
220 | int j; | |
221 | ||
222 | for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j) | |
223 | ; | |
224 | desc->shadow_ptes[i] = desc->shadow_ptes[j]; | |
225 | desc->shadow_ptes[j] = 0; | |
226 | if (j != 0) | |
227 | return; | |
228 | if (!prev_desc && !desc->more) | |
229 | page->private = (unsigned long)desc->shadow_ptes[0]; | |
230 | else | |
231 | if (prev_desc) | |
232 | prev_desc->more = desc->more; | |
233 | else | |
234 | page->private = (unsigned long)desc->more | 1; | |
235 | kfree(desc); | |
236 | } | |
237 | ||
238 | static void rmap_remove(struct kvm *kvm, u64 *spte) | |
239 | { | |
240 | struct page *page; | |
241 | struct kvm_rmap_desc *desc; | |
242 | struct kvm_rmap_desc *prev_desc; | |
243 | int i; | |
244 | ||
245 | if (!is_rmap_pte(*spte)) | |
246 | return; | |
247 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); | |
248 | if (!page->private) { | |
249 | printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte); | |
250 | BUG(); | |
251 | } else if (!(page->private & 1)) { | |
252 | rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte); | |
253 | if ((u64 *)page->private != spte) { | |
254 | printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n", | |
255 | spte, *spte); | |
256 | BUG(); | |
257 | } | |
258 | page->private = 0; | |
259 | } else { | |
260 | rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte); | |
261 | desc = (struct kvm_rmap_desc *)(page->private & ~1ul); | |
262 | prev_desc = NULL; | |
263 | while (desc) { | |
264 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) | |
265 | if (desc->shadow_ptes[i] == spte) { | |
266 | rmap_desc_remove_entry(page, desc, i, | |
267 | prev_desc); | |
268 | return; | |
269 | } | |
270 | prev_desc = desc; | |
271 | desc = desc->more; | |
272 | } | |
273 | BUG(); | |
274 | } | |
275 | } | |
276 | ||
374cbac0 AK |
277 | static void rmap_write_protect(struct kvm *kvm, u64 gfn) |
278 | { | |
279 | struct page *page; | |
280 | struct kvm_memory_slot *slot; | |
281 | struct kvm_rmap_desc *desc; | |
282 | u64 *spte; | |
283 | ||
284 | slot = gfn_to_memslot(kvm, gfn); | |
285 | BUG_ON(!slot); | |
286 | page = gfn_to_page(slot, gfn); | |
287 | ||
288 | while (page->private) { | |
289 | if (!(page->private & 1)) | |
290 | spte = (u64 *)page->private; | |
291 | else { | |
292 | desc = (struct kvm_rmap_desc *)(page->private & ~1ul); | |
293 | spte = desc->shadow_ptes[0]; | |
294 | } | |
295 | BUG_ON(!spte); | |
296 | BUG_ON((*spte & PT64_BASE_ADDR_MASK) != | |
297 | page_to_pfn(page) << PAGE_SHIFT); | |
298 | BUG_ON(!(*spte & PT_PRESENT_MASK)); | |
299 | BUG_ON(!(*spte & PT_WRITABLE_MASK)); | |
300 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); | |
301 | rmap_remove(kvm, spte); | |
302 | *spte &= ~(u64)PT_WRITABLE_MASK; | |
303 | } | |
304 | } | |
305 | ||
6aa8b732 AK |
306 | static int is_empty_shadow_page(hpa_t page_hpa) |
307 | { | |
139bdb2d AK |
308 | u64 *pos; |
309 | u64 *end; | |
310 | ||
311 | for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64); | |
6aa8b732 | 312 | pos != end; pos++) |
139bdb2d AK |
313 | if (*pos != 0) { |
314 | printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__, | |
315 | pos, *pos); | |
6aa8b732 | 316 | return 0; |
139bdb2d | 317 | } |
6aa8b732 AK |
318 | return 1; |
319 | } | |
320 | ||
260746c0 AK |
321 | static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa) |
322 | { | |
323 | struct kvm_mmu_page *page_head = page_header(page_hpa); | |
324 | ||
5f1e0b6a | 325 | ASSERT(is_empty_shadow_page(page_hpa)); |
260746c0 AK |
326 | list_del(&page_head->link); |
327 | page_head->page_hpa = page_hpa; | |
328 | list_add(&page_head->link, &vcpu->free_pages); | |
329 | ++vcpu->kvm->n_free_mmu_pages; | |
330 | } | |
331 | ||
cea0f0e7 AK |
332 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
333 | { | |
334 | return gfn; | |
335 | } | |
336 | ||
25c0de2c AK |
337 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
338 | u64 *parent_pte) | |
6aa8b732 AK |
339 | { |
340 | struct kvm_mmu_page *page; | |
341 | ||
342 | if (list_empty(&vcpu->free_pages)) | |
25c0de2c | 343 | return NULL; |
6aa8b732 AK |
344 | |
345 | page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link); | |
346 | list_del(&page->link); | |
347 | list_add(&page->link, &vcpu->kvm->active_mmu_pages); | |
348 | ASSERT(is_empty_shadow_page(page->page_hpa)); | |
349 | page->slot_bitmap = 0; | |
350 | page->global = 1; | |
cea0f0e7 | 351 | page->multimapped = 0; |
6aa8b732 | 352 | page->parent_pte = parent_pte; |
ebeace86 | 353 | --vcpu->kvm->n_free_mmu_pages; |
25c0de2c | 354 | return page; |
6aa8b732 AK |
355 | } |
356 | ||
cea0f0e7 AK |
357 | static void mmu_page_add_parent_pte(struct kvm_mmu_page *page, u64 *parent_pte) |
358 | { | |
359 | struct kvm_pte_chain *pte_chain; | |
360 | struct hlist_node *node; | |
361 | int i; | |
362 | ||
363 | if (!parent_pte) | |
364 | return; | |
365 | if (!page->multimapped) { | |
366 | u64 *old = page->parent_pte; | |
367 | ||
368 | if (!old) { | |
369 | page->parent_pte = parent_pte; | |
370 | return; | |
371 | } | |
372 | page->multimapped = 1; | |
373 | pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT); | |
374 | BUG_ON(!pte_chain); | |
375 | INIT_HLIST_HEAD(&page->parent_ptes); | |
376 | hlist_add_head(&pte_chain->link, &page->parent_ptes); | |
377 | pte_chain->parent_ptes[0] = old; | |
378 | } | |
379 | hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) { | |
380 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) | |
381 | continue; | |
382 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) | |
383 | if (!pte_chain->parent_ptes[i]) { | |
384 | pte_chain->parent_ptes[i] = parent_pte; | |
385 | return; | |
386 | } | |
387 | } | |
388 | pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT); | |
389 | BUG_ON(!pte_chain); | |
390 | hlist_add_head(&pte_chain->link, &page->parent_ptes); | |
391 | pte_chain->parent_ptes[0] = parent_pte; | |
392 | } | |
393 | ||
394 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page, | |
395 | u64 *parent_pte) | |
396 | { | |
397 | struct kvm_pte_chain *pte_chain; | |
398 | struct hlist_node *node; | |
399 | int i; | |
400 | ||
401 | if (!page->multimapped) { | |
402 | BUG_ON(page->parent_pte != parent_pte); | |
403 | page->parent_pte = NULL; | |
404 | return; | |
405 | } | |
406 | hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) | |
407 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { | |
408 | if (!pte_chain->parent_ptes[i]) | |
409 | break; | |
410 | if (pte_chain->parent_ptes[i] != parent_pte) | |
411 | continue; | |
697fe2e2 AK |
412 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
413 | && pte_chain->parent_ptes[i + 1]) { | |
cea0f0e7 AK |
414 | pte_chain->parent_ptes[i] |
415 | = pte_chain->parent_ptes[i + 1]; | |
416 | ++i; | |
417 | } | |
418 | pte_chain->parent_ptes[i] = NULL; | |
697fe2e2 AK |
419 | if (i == 0) { |
420 | hlist_del(&pte_chain->link); | |
421 | kfree(pte_chain); | |
422 | if (hlist_empty(&page->parent_ptes)) { | |
423 | page->multimapped = 0; | |
424 | page->parent_pte = NULL; | |
425 | } | |
426 | } | |
cea0f0e7 AK |
427 | return; |
428 | } | |
429 | BUG(); | |
430 | } | |
431 | ||
432 | static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu, | |
433 | gfn_t gfn) | |
434 | { | |
435 | unsigned index; | |
436 | struct hlist_head *bucket; | |
437 | struct kvm_mmu_page *page; | |
438 | struct hlist_node *node; | |
439 | ||
440 | pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn); | |
441 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; | |
442 | bucket = &vcpu->kvm->mmu_page_hash[index]; | |
443 | hlist_for_each_entry(page, node, bucket, hash_link) | |
444 | if (page->gfn == gfn && !page->role.metaphysical) { | |
445 | pgprintk("%s: found role %x\n", | |
446 | __FUNCTION__, page->role.word); | |
447 | return page; | |
448 | } | |
449 | return NULL; | |
450 | } | |
451 | ||
452 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, | |
453 | gfn_t gfn, | |
454 | gva_t gaddr, | |
455 | unsigned level, | |
456 | int metaphysical, | |
457 | u64 *parent_pte) | |
458 | { | |
459 | union kvm_mmu_page_role role; | |
460 | unsigned index; | |
461 | unsigned quadrant; | |
462 | struct hlist_head *bucket; | |
463 | struct kvm_mmu_page *page; | |
464 | struct hlist_node *node; | |
465 | ||
466 | role.word = 0; | |
467 | role.glevels = vcpu->mmu.root_level; | |
468 | role.level = level; | |
469 | role.metaphysical = metaphysical; | |
470 | if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) { | |
471 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); | |
472 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; | |
473 | role.quadrant = quadrant; | |
474 | } | |
475 | pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__, | |
476 | gfn, role.word); | |
477 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; | |
478 | bucket = &vcpu->kvm->mmu_page_hash[index]; | |
479 | hlist_for_each_entry(page, node, bucket, hash_link) | |
480 | if (page->gfn == gfn && page->role.word == role.word) { | |
481 | mmu_page_add_parent_pte(page, parent_pte); | |
482 | pgprintk("%s: found\n", __FUNCTION__); | |
483 | return page; | |
484 | } | |
485 | page = kvm_mmu_alloc_page(vcpu, parent_pte); | |
486 | if (!page) | |
487 | return page; | |
488 | pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word); | |
489 | page->gfn = gfn; | |
490 | page->role = role; | |
491 | hlist_add_head(&page->hash_link, bucket); | |
374cbac0 AK |
492 | if (!metaphysical) |
493 | rmap_write_protect(vcpu->kvm, gfn); | |
cea0f0e7 AK |
494 | return page; |
495 | } | |
496 | ||
a436036b AK |
497 | static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu, |
498 | struct kvm_mmu_page *page) | |
499 | { | |
697fe2e2 AK |
500 | unsigned i; |
501 | u64 *pt; | |
502 | u64 ent; | |
503 | ||
504 | pt = __va(page->page_hpa); | |
505 | ||
506 | if (page->role.level == PT_PAGE_TABLE_LEVEL) { | |
507 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { | |
508 | if (pt[i] & PT_PRESENT_MASK) | |
509 | rmap_remove(vcpu->kvm, &pt[i]); | |
510 | pt[i] = 0; | |
511 | } | |
512 | return; | |
513 | } | |
514 | ||
515 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { | |
516 | ent = pt[i]; | |
517 | ||
518 | pt[i] = 0; | |
519 | if (!(ent & PT_PRESENT_MASK)) | |
520 | continue; | |
521 | ent &= PT64_BASE_ADDR_MASK; | |
522 | mmu_page_remove_parent_pte(page_header(ent), &pt[i]); | |
523 | } | |
a436036b AK |
524 | } |
525 | ||
cea0f0e7 AK |
526 | static void kvm_mmu_put_page(struct kvm_vcpu *vcpu, |
527 | struct kvm_mmu_page *page, | |
528 | u64 *parent_pte) | |
529 | { | |
530 | mmu_page_remove_parent_pte(page, parent_pte); | |
a436036b AK |
531 | } |
532 | ||
533 | static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu, | |
534 | struct kvm_mmu_page *page) | |
535 | { | |
536 | u64 *parent_pte; | |
537 | ||
538 | while (page->multimapped || page->parent_pte) { | |
539 | if (!page->multimapped) | |
540 | parent_pte = page->parent_pte; | |
541 | else { | |
542 | struct kvm_pte_chain *chain; | |
543 | ||
544 | chain = container_of(page->parent_ptes.first, | |
545 | struct kvm_pte_chain, link); | |
546 | parent_pte = chain->parent_ptes[0]; | |
547 | } | |
697fe2e2 | 548 | BUG_ON(!parent_pte); |
a436036b AK |
549 | kvm_mmu_put_page(vcpu, page, parent_pte); |
550 | *parent_pte = 0; | |
551 | } | |
cc4529ef AK |
552 | kvm_mmu_page_unlink_children(vcpu, page); |
553 | hlist_del(&page->hash_link); | |
ebeace86 | 554 | kvm_mmu_free_page(vcpu, page->page_hpa); |
a436036b AK |
555 | } |
556 | ||
557 | static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn) | |
558 | { | |
559 | unsigned index; | |
560 | struct hlist_head *bucket; | |
561 | struct kvm_mmu_page *page; | |
562 | struct hlist_node *node, *n; | |
563 | int r; | |
564 | ||
565 | pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn); | |
566 | r = 0; | |
567 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; | |
568 | bucket = &vcpu->kvm->mmu_page_hash[index]; | |
569 | hlist_for_each_entry_safe(page, node, n, bucket, hash_link) | |
570 | if (page->gfn == gfn && !page->role.metaphysical) { | |
697fe2e2 AK |
571 | pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn, |
572 | page->role.word); | |
a436036b AK |
573 | kvm_mmu_zap_page(vcpu, page); |
574 | r = 1; | |
575 | } | |
576 | return r; | |
cea0f0e7 AK |
577 | } |
578 | ||
6aa8b732 AK |
579 | static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa) |
580 | { | |
581 | int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT)); | |
582 | struct kvm_mmu_page *page_head = page_header(__pa(pte)); | |
583 | ||
584 | __set_bit(slot, &page_head->slot_bitmap); | |
585 | } | |
586 | ||
587 | hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa) | |
588 | { | |
589 | hpa_t hpa = gpa_to_hpa(vcpu, gpa); | |
590 | ||
591 | return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa; | |
592 | } | |
593 | ||
594 | hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa) | |
595 | { | |
596 | struct kvm_memory_slot *slot; | |
597 | struct page *page; | |
598 | ||
599 | ASSERT((gpa & HPA_ERR_MASK) == 0); | |
600 | slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT); | |
601 | if (!slot) | |
602 | return gpa | HPA_ERR_MASK; | |
603 | page = gfn_to_page(slot, gpa >> PAGE_SHIFT); | |
604 | return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) | |
605 | | (gpa & (PAGE_SIZE-1)); | |
606 | } | |
607 | ||
608 | hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva) | |
609 | { | |
610 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); | |
611 | ||
612 | if (gpa == UNMAPPED_GVA) | |
613 | return UNMAPPED_GVA; | |
614 | return gpa_to_hpa(vcpu, gpa); | |
615 | } | |
616 | ||
6aa8b732 AK |
617 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
618 | { | |
619 | } | |
620 | ||
621 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p) | |
622 | { | |
623 | int level = PT32E_ROOT_LEVEL; | |
624 | hpa_t table_addr = vcpu->mmu.root_hpa; | |
625 | ||
626 | for (; ; level--) { | |
627 | u32 index = PT64_INDEX(v, level); | |
628 | u64 *table; | |
cea0f0e7 | 629 | u64 pte; |
6aa8b732 AK |
630 | |
631 | ASSERT(VALID_PAGE(table_addr)); | |
632 | table = __va(table_addr); | |
633 | ||
634 | if (level == 1) { | |
cea0f0e7 AK |
635 | pte = table[index]; |
636 | if (is_present_pte(pte) && is_writeble_pte(pte)) | |
637 | return 0; | |
6aa8b732 AK |
638 | mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT); |
639 | page_header_update_slot(vcpu->kvm, table, v); | |
640 | table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK | | |
641 | PT_USER_MASK; | |
cd4a4e53 | 642 | rmap_add(vcpu->kvm, &table[index]); |
6aa8b732 AK |
643 | return 0; |
644 | } | |
645 | ||
646 | if (table[index] == 0) { | |
25c0de2c | 647 | struct kvm_mmu_page *new_table; |
cea0f0e7 | 648 | gfn_t pseudo_gfn; |
6aa8b732 | 649 | |
cea0f0e7 AK |
650 | pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK) |
651 | >> PAGE_SHIFT; | |
652 | new_table = kvm_mmu_get_page(vcpu, pseudo_gfn, | |
653 | v, level - 1, | |
654 | 1, &table[index]); | |
25c0de2c | 655 | if (!new_table) { |
6aa8b732 AK |
656 | pgprintk("nonpaging_map: ENOMEM\n"); |
657 | return -ENOMEM; | |
658 | } | |
659 | ||
25c0de2c AK |
660 | table[index] = new_table->page_hpa | PT_PRESENT_MASK |
661 | | PT_WRITABLE_MASK | PT_USER_MASK; | |
6aa8b732 AK |
662 | } |
663 | table_addr = table[index] & PT64_BASE_ADDR_MASK; | |
664 | } | |
665 | } | |
666 | ||
17ac10ad AK |
667 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
668 | { | |
669 | int i; | |
670 | ||
671 | #ifdef CONFIG_X86_64 | |
672 | if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) { | |
673 | hpa_t root = vcpu->mmu.root_hpa; | |
674 | ||
675 | ASSERT(VALID_PAGE(root)); | |
17ac10ad AK |
676 | vcpu->mmu.root_hpa = INVALID_PAGE; |
677 | return; | |
678 | } | |
679 | #endif | |
680 | for (i = 0; i < 4; ++i) { | |
681 | hpa_t root = vcpu->mmu.pae_root[i]; | |
682 | ||
683 | ASSERT(VALID_PAGE(root)); | |
684 | root &= PT64_BASE_ADDR_MASK; | |
17ac10ad AK |
685 | vcpu->mmu.pae_root[i] = INVALID_PAGE; |
686 | } | |
687 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
688 | } | |
689 | ||
690 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) | |
691 | { | |
692 | int i; | |
cea0f0e7 AK |
693 | gfn_t root_gfn; |
694 | root_gfn = vcpu->cr3 >> PAGE_SHIFT; | |
17ac10ad AK |
695 | |
696 | #ifdef CONFIG_X86_64 | |
697 | if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) { | |
698 | hpa_t root = vcpu->mmu.root_hpa; | |
699 | ||
700 | ASSERT(!VALID_PAGE(root)); | |
cea0f0e7 AK |
701 | root = kvm_mmu_get_page(vcpu, root_gfn, 0, |
702 | PT64_ROOT_LEVEL, 0, NULL)->page_hpa; | |
17ac10ad AK |
703 | vcpu->mmu.root_hpa = root; |
704 | return; | |
705 | } | |
706 | #endif | |
707 | for (i = 0; i < 4; ++i) { | |
708 | hpa_t root = vcpu->mmu.pae_root[i]; | |
709 | ||
710 | ASSERT(!VALID_PAGE(root)); | |
cea0f0e7 AK |
711 | if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) |
712 | root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT; | |
713 | else if (vcpu->mmu.root_level == 0) | |
714 | root_gfn = 0; | |
715 | root = kvm_mmu_get_page(vcpu, root_gfn, i << 30, | |
716 | PT32_ROOT_LEVEL, !is_paging(vcpu), | |
717 | NULL)->page_hpa; | |
17ac10ad AK |
718 | vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK; |
719 | } | |
720 | vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root); | |
721 | } | |
722 | ||
6aa8b732 AK |
723 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
724 | { | |
725 | return vaddr; | |
726 | } | |
727 | ||
728 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, | |
729 | u32 error_code) | |
730 | { | |
6aa8b732 | 731 | gpa_t addr = gva; |
ebeace86 | 732 | hpa_t paddr; |
6aa8b732 AK |
733 | |
734 | ASSERT(vcpu); | |
735 | ASSERT(VALID_PAGE(vcpu->mmu.root_hpa)); | |
736 | ||
6aa8b732 | 737 | |
ebeace86 | 738 | paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK); |
6aa8b732 | 739 | |
ebeace86 AK |
740 | if (is_error_hpa(paddr)) |
741 | return 1; | |
6aa8b732 | 742 | |
ebeace86 | 743 | return nonpaging_map(vcpu, addr & PAGE_MASK, paddr); |
6aa8b732 AK |
744 | } |
745 | ||
6aa8b732 AK |
746 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
747 | { | |
17ac10ad | 748 | mmu_free_roots(vcpu); |
6aa8b732 AK |
749 | } |
750 | ||
751 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) | |
752 | { | |
753 | struct kvm_mmu *context = &vcpu->mmu; | |
754 | ||
755 | context->new_cr3 = nonpaging_new_cr3; | |
756 | context->page_fault = nonpaging_page_fault; | |
6aa8b732 AK |
757 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
758 | context->free = nonpaging_free; | |
cea0f0e7 | 759 | context->root_level = 0; |
6aa8b732 | 760 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
17ac10ad | 761 | mmu_alloc_roots(vcpu); |
6aa8b732 AK |
762 | ASSERT(VALID_PAGE(context->root_hpa)); |
763 | kvm_arch_ops->set_cr3(vcpu, context->root_hpa); | |
764 | return 0; | |
765 | } | |
766 | ||
6aa8b732 AK |
767 | static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
768 | { | |
6aa8b732 AK |
769 | ++kvm_stat.tlb_flush; |
770 | kvm_arch_ops->tlb_flush(vcpu); | |
771 | } | |
772 | ||
773 | static void paging_new_cr3(struct kvm_vcpu *vcpu) | |
774 | { | |
374cbac0 | 775 | pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3); |
cea0f0e7 AK |
776 | mmu_free_roots(vcpu); |
777 | mmu_alloc_roots(vcpu); | |
6aa8b732 | 778 | kvm_mmu_flush_tlb(vcpu); |
cea0f0e7 | 779 | kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa); |
6aa8b732 AK |
780 | } |
781 | ||
782 | static void mark_pagetable_nonglobal(void *shadow_pte) | |
783 | { | |
784 | page_header(__pa(shadow_pte))->global = 0; | |
785 | } | |
786 | ||
787 | static inline void set_pte_common(struct kvm_vcpu *vcpu, | |
788 | u64 *shadow_pte, | |
789 | gpa_t gaddr, | |
790 | int dirty, | |
815af8d4 AK |
791 | u64 access_bits, |
792 | gfn_t gfn) | |
6aa8b732 AK |
793 | { |
794 | hpa_t paddr; | |
795 | ||
796 | *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET; | |
797 | if (!dirty) | |
798 | access_bits &= ~PT_WRITABLE_MASK; | |
cea0f0e7 | 799 | |
374cbac0 | 800 | paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK); |
6aa8b732 AK |
801 | |
802 | *shadow_pte |= access_bits; | |
803 | ||
6aa8b732 AK |
804 | if (!(*shadow_pte & PT_GLOBAL_MASK)) |
805 | mark_pagetable_nonglobal(shadow_pte); | |
806 | ||
807 | if (is_error_hpa(paddr)) { | |
808 | *shadow_pte |= gaddr; | |
809 | *shadow_pte |= PT_SHADOW_IO_MARK; | |
810 | *shadow_pte &= ~PT_PRESENT_MASK; | |
374cbac0 | 811 | return; |
6aa8b732 | 812 | } |
374cbac0 AK |
813 | |
814 | *shadow_pte |= paddr; | |
815 | ||
816 | if (access_bits & PT_WRITABLE_MASK) { | |
817 | struct kvm_mmu_page *shadow; | |
818 | ||
815af8d4 | 819 | shadow = kvm_mmu_lookup_page(vcpu, gfn); |
374cbac0 AK |
820 | if (shadow) { |
821 | pgprintk("%s: found shadow page for %lx, marking ro\n", | |
815af8d4 | 822 | __FUNCTION__, gfn); |
374cbac0 AK |
823 | access_bits &= ~PT_WRITABLE_MASK; |
824 | *shadow_pte &= ~PT_WRITABLE_MASK; | |
825 | } | |
826 | } | |
827 | ||
828 | if (access_bits & PT_WRITABLE_MASK) | |
829 | mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT); | |
830 | ||
831 | page_header_update_slot(vcpu->kvm, shadow_pte, gaddr); | |
832 | rmap_add(vcpu->kvm, shadow_pte); | |
6aa8b732 AK |
833 | } |
834 | ||
835 | static void inject_page_fault(struct kvm_vcpu *vcpu, | |
836 | u64 addr, | |
837 | u32 err_code) | |
838 | { | |
839 | kvm_arch_ops->inject_page_fault(vcpu, addr, err_code); | |
840 | } | |
841 | ||
842 | static inline int fix_read_pf(u64 *shadow_ent) | |
843 | { | |
844 | if ((*shadow_ent & PT_SHADOW_USER_MASK) && | |
845 | !(*shadow_ent & PT_USER_MASK)) { | |
846 | /* | |
847 | * If supervisor write protect is disabled, we shadow kernel | |
848 | * pages as user pages so we can trap the write access. | |
849 | */ | |
850 | *shadow_ent |= PT_USER_MASK; | |
851 | *shadow_ent &= ~PT_WRITABLE_MASK; | |
852 | ||
853 | return 1; | |
854 | ||
855 | } | |
856 | return 0; | |
857 | } | |
858 | ||
859 | static int may_access(u64 pte, int write, int user) | |
860 | { | |
861 | ||
862 | if (user && !(pte & PT_USER_MASK)) | |
863 | return 0; | |
864 | if (write && !(pte & PT_WRITABLE_MASK)) | |
865 | return 0; | |
866 | return 1; | |
867 | } | |
868 | ||
6aa8b732 AK |
869 | static void paging_free(struct kvm_vcpu *vcpu) |
870 | { | |
871 | nonpaging_free(vcpu); | |
872 | } | |
873 | ||
874 | #define PTTYPE 64 | |
875 | #include "paging_tmpl.h" | |
876 | #undef PTTYPE | |
877 | ||
878 | #define PTTYPE 32 | |
879 | #include "paging_tmpl.h" | |
880 | #undef PTTYPE | |
881 | ||
17ac10ad | 882 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
6aa8b732 AK |
883 | { |
884 | struct kvm_mmu *context = &vcpu->mmu; | |
885 | ||
886 | ASSERT(is_pae(vcpu)); | |
887 | context->new_cr3 = paging_new_cr3; | |
888 | context->page_fault = paging64_page_fault; | |
6aa8b732 AK |
889 | context->gva_to_gpa = paging64_gva_to_gpa; |
890 | context->free = paging_free; | |
17ac10ad AK |
891 | context->root_level = level; |
892 | context->shadow_root_level = level; | |
893 | mmu_alloc_roots(vcpu); | |
6aa8b732 AK |
894 | ASSERT(VALID_PAGE(context->root_hpa)); |
895 | kvm_arch_ops->set_cr3(vcpu, context->root_hpa | | |
896 | (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK))); | |
897 | return 0; | |
898 | } | |
899 | ||
17ac10ad AK |
900 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
901 | { | |
902 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); | |
903 | } | |
904 | ||
6aa8b732 AK |
905 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
906 | { | |
907 | struct kvm_mmu *context = &vcpu->mmu; | |
908 | ||
909 | context->new_cr3 = paging_new_cr3; | |
910 | context->page_fault = paging32_page_fault; | |
6aa8b732 AK |
911 | context->gva_to_gpa = paging32_gva_to_gpa; |
912 | context->free = paging_free; | |
913 | context->root_level = PT32_ROOT_LEVEL; | |
914 | context->shadow_root_level = PT32E_ROOT_LEVEL; | |
17ac10ad | 915 | mmu_alloc_roots(vcpu); |
6aa8b732 AK |
916 | ASSERT(VALID_PAGE(context->root_hpa)); |
917 | kvm_arch_ops->set_cr3(vcpu, context->root_hpa | | |
918 | (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK))); | |
919 | return 0; | |
920 | } | |
921 | ||
922 | static int paging32E_init_context(struct kvm_vcpu *vcpu) | |
923 | { | |
17ac10ad | 924 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
6aa8b732 AK |
925 | } |
926 | ||
927 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) | |
928 | { | |
929 | ASSERT(vcpu); | |
930 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); | |
931 | ||
932 | if (!is_paging(vcpu)) | |
933 | return nonpaging_init_context(vcpu); | |
a9058ecd | 934 | else if (is_long_mode(vcpu)) |
6aa8b732 AK |
935 | return paging64_init_context(vcpu); |
936 | else if (is_pae(vcpu)) | |
937 | return paging32E_init_context(vcpu); | |
938 | else | |
939 | return paging32_init_context(vcpu); | |
940 | } | |
941 | ||
942 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) | |
943 | { | |
944 | ASSERT(vcpu); | |
945 | if (VALID_PAGE(vcpu->mmu.root_hpa)) { | |
946 | vcpu->mmu.free(vcpu); | |
947 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
948 | } | |
949 | } | |
950 | ||
951 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) | |
952 | { | |
953 | destroy_kvm_mmu(vcpu); | |
954 | return init_kvm_mmu(vcpu); | |
955 | } | |
956 | ||
da4a00f0 AK |
957 | void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes) |
958 | { | |
9b7a0325 AK |
959 | gfn_t gfn = gpa >> PAGE_SHIFT; |
960 | struct kvm_mmu_page *page; | |
961 | struct kvm_mmu_page *child; | |
0e7bc4b9 | 962 | struct hlist_node *node, *n; |
9b7a0325 AK |
963 | struct hlist_head *bucket; |
964 | unsigned index; | |
965 | u64 *spte; | |
966 | u64 pte; | |
967 | unsigned offset = offset_in_page(gpa); | |
0e7bc4b9 | 968 | unsigned pte_size; |
9b7a0325 | 969 | unsigned page_offset; |
0e7bc4b9 | 970 | unsigned misaligned; |
9b7a0325 AK |
971 | int level; |
972 | ||
da4a00f0 | 973 | pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes); |
9b7a0325 AK |
974 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
975 | bucket = &vcpu->kvm->mmu_page_hash[index]; | |
0e7bc4b9 | 976 | hlist_for_each_entry_safe(page, node, n, bucket, hash_link) { |
9b7a0325 AK |
977 | if (page->gfn != gfn || page->role.metaphysical) |
978 | continue; | |
0e7bc4b9 AK |
979 | pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8; |
980 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); | |
981 | if (misaligned) { | |
982 | /* | |
983 | * Misaligned accesses are too much trouble to fix | |
984 | * up; also, they usually indicate a page is not used | |
985 | * as a page table. | |
986 | */ | |
987 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", | |
988 | gpa, bytes, page->role.word); | |
989 | kvm_mmu_zap_page(vcpu, page); | |
990 | continue; | |
991 | } | |
9b7a0325 AK |
992 | page_offset = offset; |
993 | level = page->role.level; | |
994 | if (page->role.glevels == PT32_ROOT_LEVEL) { | |
995 | page_offset <<= 1; /* 32->64 */ | |
996 | page_offset &= ~PAGE_MASK; | |
997 | } | |
998 | spte = __va(page->page_hpa); | |
999 | spte += page_offset / sizeof(*spte); | |
1000 | pte = *spte; | |
1001 | if (is_present_pte(pte)) { | |
1002 | if (level == PT_PAGE_TABLE_LEVEL) | |
1003 | rmap_remove(vcpu->kvm, spte); | |
1004 | else { | |
1005 | child = page_header(pte & PT64_BASE_ADDR_MASK); | |
1006 | mmu_page_remove_parent_pte(child, spte); | |
1007 | } | |
1008 | } | |
1009 | *spte = 0; | |
1010 | } | |
da4a00f0 AK |
1011 | } |
1012 | ||
1013 | void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes) | |
1014 | { | |
1015 | } | |
1016 | ||
a436036b AK |
1017 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
1018 | { | |
1019 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); | |
1020 | ||
1021 | return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT); | |
1022 | } | |
1023 | ||
ebeace86 AK |
1024 | void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
1025 | { | |
1026 | while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) { | |
1027 | struct kvm_mmu_page *page; | |
1028 | ||
1029 | page = container_of(vcpu->kvm->active_mmu_pages.prev, | |
1030 | struct kvm_mmu_page, link); | |
1031 | kvm_mmu_zap_page(vcpu, page); | |
1032 | } | |
1033 | } | |
1034 | EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages); | |
1035 | ||
6aa8b732 AK |
1036 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
1037 | { | |
1038 | while (!list_empty(&vcpu->free_pages)) { | |
1039 | struct kvm_mmu_page *page; | |
1040 | ||
1041 | page = list_entry(vcpu->free_pages.next, | |
1042 | struct kvm_mmu_page, link); | |
1043 | list_del(&page->link); | |
1044 | __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT)); | |
1045 | page->page_hpa = INVALID_PAGE; | |
1046 | } | |
17ac10ad | 1047 | free_page((unsigned long)vcpu->mmu.pae_root); |
6aa8b732 AK |
1048 | } |
1049 | ||
1050 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) | |
1051 | { | |
17ac10ad | 1052 | struct page *page; |
6aa8b732 AK |
1053 | int i; |
1054 | ||
1055 | ASSERT(vcpu); | |
1056 | ||
1057 | for (i = 0; i < KVM_NUM_MMU_PAGES; i++) { | |
6aa8b732 AK |
1058 | struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i]; |
1059 | ||
1060 | INIT_LIST_HEAD(&page_header->link); | |
17ac10ad | 1061 | if ((page = alloc_page(GFP_KERNEL)) == NULL) |
6aa8b732 AK |
1062 | goto error_1; |
1063 | page->private = (unsigned long)page_header; | |
1064 | page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT; | |
1065 | memset(__va(page_header->page_hpa), 0, PAGE_SIZE); | |
1066 | list_add(&page_header->link, &vcpu->free_pages); | |
ebeace86 | 1067 | ++vcpu->kvm->n_free_mmu_pages; |
6aa8b732 | 1068 | } |
17ac10ad AK |
1069 | |
1070 | /* | |
1071 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. | |
1072 | * Therefore we need to allocate shadow page tables in the first | |
1073 | * 4GB of memory, which happens to fit the DMA32 zone. | |
1074 | */ | |
1075 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); | |
1076 | if (!page) | |
1077 | goto error_1; | |
1078 | vcpu->mmu.pae_root = page_address(page); | |
1079 | for (i = 0; i < 4; ++i) | |
1080 | vcpu->mmu.pae_root[i] = INVALID_PAGE; | |
1081 | ||
6aa8b732 AK |
1082 | return 0; |
1083 | ||
1084 | error_1: | |
1085 | free_mmu_pages(vcpu); | |
1086 | return -ENOMEM; | |
1087 | } | |
1088 | ||
8018c27b | 1089 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
6aa8b732 | 1090 | { |
6aa8b732 AK |
1091 | ASSERT(vcpu); |
1092 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); | |
1093 | ASSERT(list_empty(&vcpu->free_pages)); | |
1094 | ||
8018c27b IM |
1095 | return alloc_mmu_pages(vcpu); |
1096 | } | |
6aa8b732 | 1097 | |
8018c27b IM |
1098 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
1099 | { | |
1100 | ASSERT(vcpu); | |
1101 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); | |
1102 | ASSERT(!list_empty(&vcpu->free_pages)); | |
2c264957 | 1103 | |
8018c27b | 1104 | return init_kvm_mmu(vcpu); |
6aa8b732 AK |
1105 | } |
1106 | ||
1107 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) | |
1108 | { | |
1109 | ASSERT(vcpu); | |
1110 | ||
1111 | destroy_kvm_mmu(vcpu); | |
1112 | free_mmu_pages(vcpu); | |
1113 | } | |
1114 | ||
1115 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) | |
1116 | { | |
1117 | struct kvm_mmu_page *page; | |
1118 | ||
1119 | list_for_each_entry(page, &kvm->active_mmu_pages, link) { | |
1120 | int i; | |
1121 | u64 *pt; | |
1122 | ||
1123 | if (!test_bit(slot, &page->slot_bitmap)) | |
1124 | continue; | |
1125 | ||
1126 | pt = __va(page->page_hpa); | |
1127 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) | |
1128 | /* avoid RMW */ | |
cd4a4e53 AK |
1129 | if (pt[i] & PT_WRITABLE_MASK) { |
1130 | rmap_remove(kvm, &pt[i]); | |
6aa8b732 | 1131 | pt[i] &= ~PT_WRITABLE_MASK; |
cd4a4e53 | 1132 | } |
6aa8b732 AK |
1133 | } |
1134 | } |