blob: b9ba240144b7cb7dafa81b43cee3c513ef6eb076 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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
Avi Kivitycea0f0e2007-01-05 16:36:43 -080029#define pgprintk(x...) do { printk(x); } while (0)
30#define rmap_printk(x...) do { printk(x); } while (0)
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
32#define ASSERT(x) \
33 if (!(x)) { \
34 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
35 __FILE__, __LINE__, #x); \
36 }
37
Avi Kivitycea0f0e2007-01-05 16:36:43 -080038#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)
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
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 \
Avi Kivity8c7bb722006-12-13 00:34:02 -080067 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivity8c7bb722006-12-13 00:34:02 -080069#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
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
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800131#define RMAP_EXT 4
132
133struct kvm_rmap_desc {
134 u64 *shadow_ptes[RMAP_EXT];
135 struct kvm_rmap_desc *more;
136};
137
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138static int is_write_protection(struct kvm_vcpu *vcpu)
139{
140 return vcpu->cr0 & CR0_WP_MASK;
141}
142
143static int is_cpuid_PSE36(void)
144{
145 return 1;
146}
147
148static int is_present_pte(unsigned long pte)
149{
150 return pte & PT_PRESENT_MASK;
151}
152
153static int is_writeble_pte(unsigned long pte)
154{
155 return pte & PT_WRITABLE_MASK;
156}
157
158static int is_io_pte(unsigned long pte)
159{
160 return pte & PT_SHADOW_IO_MARK;
161}
162
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163static 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 */
178static 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
215static 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
238static 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
Avi Kivity374cbac2007-01-05 16:36:43 -0800277static 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
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306static int is_empty_shadow_page(hpa_t page_hpa)
307{
308 u32 *pos;
309 u32 *end;
310 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u32);
311 pos != end; pos++)
312 if (*pos != 0)
313 return 0;
314 return 1;
315}
316
Avi Kivity260746c2007-01-05 16:36:49 -0800317static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
318{
319 struct kvm_mmu_page *page_head = page_header(page_hpa);
320
Avi Kivity5f1e0b62007-01-05 16:36:49 -0800321 ASSERT(is_empty_shadow_page(page_hpa));
Avi Kivity260746c2007-01-05 16:36:49 -0800322 list_del(&page_head->link);
323 page_head->page_hpa = page_hpa;
324 list_add(&page_head->link, &vcpu->free_pages);
325 ++vcpu->kvm->n_free_mmu_pages;
326}
327
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800328static unsigned kvm_page_table_hashfn(gfn_t gfn)
329{
330 return gfn;
331}
332
Avi Kivity25c0de22007-01-05 16:36:42 -0800333static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
334 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335{
336 struct kvm_mmu_page *page;
337
338 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800339 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340
341 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
342 list_del(&page->link);
343 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
344 ASSERT(is_empty_shadow_page(page->page_hpa));
345 page->slot_bitmap = 0;
346 page->global = 1;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800347 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800349 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800350 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351}
352
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800353static void mmu_page_add_parent_pte(struct kvm_mmu_page *page, u64 *parent_pte)
354{
355 struct kvm_pte_chain *pte_chain;
356 struct hlist_node *node;
357 int i;
358
359 if (!parent_pte)
360 return;
361 if (!page->multimapped) {
362 u64 *old = page->parent_pte;
363
364 if (!old) {
365 page->parent_pte = parent_pte;
366 return;
367 }
368 page->multimapped = 1;
369 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
370 BUG_ON(!pte_chain);
371 INIT_HLIST_HEAD(&page->parent_ptes);
372 hlist_add_head(&pte_chain->link, &page->parent_ptes);
373 pte_chain->parent_ptes[0] = old;
374 }
375 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
376 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
377 continue;
378 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
379 if (!pte_chain->parent_ptes[i]) {
380 pte_chain->parent_ptes[i] = parent_pte;
381 return;
382 }
383 }
384 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
385 BUG_ON(!pte_chain);
386 hlist_add_head(&pte_chain->link, &page->parent_ptes);
387 pte_chain->parent_ptes[0] = parent_pte;
388}
389
390static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
391 u64 *parent_pte)
392{
393 struct kvm_pte_chain *pte_chain;
394 struct hlist_node *node;
395 int i;
396
397 if (!page->multimapped) {
398 BUG_ON(page->parent_pte != parent_pte);
399 page->parent_pte = NULL;
400 return;
401 }
402 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
403 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
404 if (!pte_chain->parent_ptes[i])
405 break;
406 if (pte_chain->parent_ptes[i] != parent_pte)
407 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800408 while (i + 1 < NR_PTE_CHAIN_ENTRIES
409 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800410 pte_chain->parent_ptes[i]
411 = pte_chain->parent_ptes[i + 1];
412 ++i;
413 }
414 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800415 if (i == 0) {
416 hlist_del(&pte_chain->link);
417 kfree(pte_chain);
418 if (hlist_empty(&page->parent_ptes)) {
419 page->multimapped = 0;
420 page->parent_pte = NULL;
421 }
422 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800423 return;
424 }
425 BUG();
426}
427
428static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
429 gfn_t gfn)
430{
431 unsigned index;
432 struct hlist_head *bucket;
433 struct kvm_mmu_page *page;
434 struct hlist_node *node;
435
436 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
437 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
438 bucket = &vcpu->kvm->mmu_page_hash[index];
439 hlist_for_each_entry(page, node, bucket, hash_link)
440 if (page->gfn == gfn && !page->role.metaphysical) {
441 pgprintk("%s: found role %x\n",
442 __FUNCTION__, page->role.word);
443 return page;
444 }
445 return NULL;
446}
447
448static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
449 gfn_t gfn,
450 gva_t gaddr,
451 unsigned level,
452 int metaphysical,
453 u64 *parent_pte)
454{
455 union kvm_mmu_page_role role;
456 unsigned index;
457 unsigned quadrant;
458 struct hlist_head *bucket;
459 struct kvm_mmu_page *page;
460 struct hlist_node *node;
461
462 role.word = 0;
463 role.glevels = vcpu->mmu.root_level;
464 role.level = level;
465 role.metaphysical = metaphysical;
466 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
467 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
468 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
469 role.quadrant = quadrant;
470 }
471 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
472 gfn, role.word);
473 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
474 bucket = &vcpu->kvm->mmu_page_hash[index];
475 hlist_for_each_entry(page, node, bucket, hash_link)
476 if (page->gfn == gfn && page->role.word == role.word) {
477 mmu_page_add_parent_pte(page, parent_pte);
478 pgprintk("%s: found\n", __FUNCTION__);
479 return page;
480 }
481 page = kvm_mmu_alloc_page(vcpu, parent_pte);
482 if (!page)
483 return page;
484 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
485 page->gfn = gfn;
486 page->role = role;
487 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800488 if (!metaphysical)
489 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800490 return page;
491}
492
Avi Kivitya4360362007-01-05 16:36:45 -0800493static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
494 struct kvm_mmu_page *page)
495{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800496 unsigned i;
497 u64 *pt;
498 u64 ent;
499
500 pt = __va(page->page_hpa);
501
502 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
503 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
504 if (pt[i] & PT_PRESENT_MASK)
505 rmap_remove(vcpu->kvm, &pt[i]);
506 pt[i] = 0;
507 }
508 return;
509 }
510
511 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
512 ent = pt[i];
513
514 pt[i] = 0;
515 if (!(ent & PT_PRESENT_MASK))
516 continue;
517 ent &= PT64_BASE_ADDR_MASK;
518 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
519 }
Avi Kivitya4360362007-01-05 16:36:45 -0800520}
521
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800522static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
523 struct kvm_mmu_page *page,
524 u64 *parent_pte)
525{
526 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800527}
528
529static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
530 struct kvm_mmu_page *page)
531{
532 u64 *parent_pte;
533
534 while (page->multimapped || page->parent_pte) {
535 if (!page->multimapped)
536 parent_pte = page->parent_pte;
537 else {
538 struct kvm_pte_chain *chain;
539
540 chain = container_of(page->parent_ptes.first,
541 struct kvm_pte_chain, link);
542 parent_pte = chain->parent_ptes[0];
543 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800544 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800545 kvm_mmu_put_page(vcpu, page, parent_pte);
546 *parent_pte = 0;
547 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800548 kvm_mmu_page_unlink_children(vcpu, page);
549 hlist_del(&page->hash_link);
Avi Kivityebeace82007-01-05 16:36:47 -0800550 kvm_mmu_free_page(vcpu, page->page_hpa);
Avi Kivitya4360362007-01-05 16:36:45 -0800551}
552
553static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
554{
555 unsigned index;
556 struct hlist_head *bucket;
557 struct kvm_mmu_page *page;
558 struct hlist_node *node, *n;
559 int r;
560
561 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
562 r = 0;
563 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
564 bucket = &vcpu->kvm->mmu_page_hash[index];
565 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
566 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800567 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
568 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800569 kvm_mmu_zap_page(vcpu, page);
570 r = 1;
571 }
572 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800573}
574
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
576{
577 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
578 struct kvm_mmu_page *page_head = page_header(__pa(pte));
579
580 __set_bit(slot, &page_head->slot_bitmap);
581}
582
583hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
584{
585 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
586
587 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
588}
589
590hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
591{
592 struct kvm_memory_slot *slot;
593 struct page *page;
594
595 ASSERT((gpa & HPA_ERR_MASK) == 0);
596 slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
597 if (!slot)
598 return gpa | HPA_ERR_MASK;
599 page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
600 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
601 | (gpa & (PAGE_SIZE-1));
602}
603
604hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
605{
606 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
607
608 if (gpa == UNMAPPED_GVA)
609 return UNMAPPED_GVA;
610 return gpa_to_hpa(vcpu, gpa);
611}
612
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
614{
615}
616
617static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
618{
619 int level = PT32E_ROOT_LEVEL;
620 hpa_t table_addr = vcpu->mmu.root_hpa;
621
622 for (; ; level--) {
623 u32 index = PT64_INDEX(v, level);
624 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800625 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626
627 ASSERT(VALID_PAGE(table_addr));
628 table = __va(table_addr);
629
630 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800631 pte = table[index];
632 if (is_present_pte(pte) && is_writeble_pte(pte))
633 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
635 page_header_update_slot(vcpu->kvm, table, v);
636 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
637 PT_USER_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800638 rmap_add(vcpu->kvm, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 return 0;
640 }
641
642 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800643 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800644 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800646 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
647 >> PAGE_SHIFT;
648 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
649 v, level - 1,
650 1, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800651 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 pgprintk("nonpaging_map: ENOMEM\n");
653 return -ENOMEM;
654 }
655
Avi Kivity25c0de22007-01-05 16:36:42 -0800656 table[index] = new_table->page_hpa | PT_PRESENT_MASK
657 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 }
659 table_addr = table[index] & PT64_BASE_ADDR_MASK;
660 }
661}
662
Avi Kivity17ac10a2007-01-05 16:36:40 -0800663static void mmu_free_roots(struct kvm_vcpu *vcpu)
664{
665 int i;
666
667#ifdef CONFIG_X86_64
668 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
669 hpa_t root = vcpu->mmu.root_hpa;
670
671 ASSERT(VALID_PAGE(root));
Avi Kivity17ac10a2007-01-05 16:36:40 -0800672 vcpu->mmu.root_hpa = INVALID_PAGE;
673 return;
674 }
675#endif
676 for (i = 0; i < 4; ++i) {
677 hpa_t root = vcpu->mmu.pae_root[i];
678
679 ASSERT(VALID_PAGE(root));
680 root &= PT64_BASE_ADDR_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800681 vcpu->mmu.pae_root[i] = INVALID_PAGE;
682 }
683 vcpu->mmu.root_hpa = INVALID_PAGE;
684}
685
686static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
687{
688 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800689 gfn_t root_gfn;
690 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800691
692#ifdef CONFIG_X86_64
693 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
694 hpa_t root = vcpu->mmu.root_hpa;
695
696 ASSERT(!VALID_PAGE(root));
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800697 root = kvm_mmu_get_page(vcpu, root_gfn, 0,
698 PT64_ROOT_LEVEL, 0, NULL)->page_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800699 vcpu->mmu.root_hpa = root;
700 return;
701 }
702#endif
703 for (i = 0; i < 4; ++i) {
704 hpa_t root = vcpu->mmu.pae_root[i];
705
706 ASSERT(!VALID_PAGE(root));
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800707 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
708 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
709 else if (vcpu->mmu.root_level == 0)
710 root_gfn = 0;
711 root = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
712 PT32_ROOT_LEVEL, !is_paging(vcpu),
713 NULL)->page_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800714 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
715 }
716 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
717}
718
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
720{
721 return vaddr;
722}
723
724static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
725 u32 error_code)
726{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800728 hpa_t paddr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
730 ASSERT(vcpu);
731 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
732
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733
Avi Kivityebeace82007-01-05 16:36:47 -0800734 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
Avi Kivityebeace82007-01-05 16:36:47 -0800736 if (is_error_hpa(paddr))
737 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738
Avi Kivityebeace82007-01-05 16:36:47 -0800739 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740}
741
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742static void nonpaging_free(struct kvm_vcpu *vcpu)
743{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800744 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745}
746
747static int nonpaging_init_context(struct kvm_vcpu *vcpu)
748{
749 struct kvm_mmu *context = &vcpu->mmu;
750
751 context->new_cr3 = nonpaging_new_cr3;
752 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 context->gva_to_gpa = nonpaging_gva_to_gpa;
754 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800755 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800757 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 ASSERT(VALID_PAGE(context->root_hpa));
759 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
760 return 0;
761}
762
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
764{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 ++kvm_stat.tlb_flush;
766 kvm_arch_ops->tlb_flush(vcpu);
767}
768
769static void paging_new_cr3(struct kvm_vcpu *vcpu)
770{
Avi Kivity374cbac2007-01-05 16:36:43 -0800771 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800772 mmu_free_roots(vcpu);
773 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800775 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776}
777
778static void mark_pagetable_nonglobal(void *shadow_pte)
779{
780 page_header(__pa(shadow_pte))->global = 0;
781}
782
783static inline void set_pte_common(struct kvm_vcpu *vcpu,
784 u64 *shadow_pte,
785 gpa_t gaddr,
786 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800787 u64 access_bits,
788 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
790 hpa_t paddr;
791
792 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
793 if (!dirty)
794 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795
796 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
797
Avi Kivity374cbac2007-01-05 16:36:43 -0800798 *shadow_pte |= access_bits;
799
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 if (!(*shadow_pte & PT_GLOBAL_MASK))
801 mark_pagetable_nonglobal(shadow_pte);
802
803 if (is_error_hpa(paddr)) {
804 *shadow_pte |= gaddr;
805 *shadow_pte |= PT_SHADOW_IO_MARK;
806 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800807 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800809
810 *shadow_pte |= paddr;
811
812 if (access_bits & PT_WRITABLE_MASK) {
813 struct kvm_mmu_page *shadow;
814
Avi Kivity815af8d2007-01-05 16:36:44 -0800815 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800816 if (shadow) {
817 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800818 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800819 access_bits &= ~PT_WRITABLE_MASK;
820 *shadow_pte &= ~PT_WRITABLE_MASK;
821 }
822 }
823
824 if (access_bits & PT_WRITABLE_MASK)
825 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
826
827 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
828 rmap_add(vcpu->kvm, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829}
830
831static void inject_page_fault(struct kvm_vcpu *vcpu,
832 u64 addr,
833 u32 err_code)
834{
835 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
836}
837
838static inline int fix_read_pf(u64 *shadow_ent)
839{
840 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
841 !(*shadow_ent & PT_USER_MASK)) {
842 /*
843 * If supervisor write protect is disabled, we shadow kernel
844 * pages as user pages so we can trap the write access.
845 */
846 *shadow_ent |= PT_USER_MASK;
847 *shadow_ent &= ~PT_WRITABLE_MASK;
848
849 return 1;
850
851 }
852 return 0;
853}
854
855static int may_access(u64 pte, int write, int user)
856{
857
858 if (user && !(pte & PT_USER_MASK))
859 return 0;
860 if (write && !(pte & PT_WRITABLE_MASK))
861 return 0;
862 return 1;
863}
864
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865static void paging_free(struct kvm_vcpu *vcpu)
866{
867 nonpaging_free(vcpu);
868}
869
870#define PTTYPE 64
871#include "paging_tmpl.h"
872#undef PTTYPE
873
874#define PTTYPE 32
875#include "paging_tmpl.h"
876#undef PTTYPE
877
Avi Kivity17ac10a2007-01-05 16:36:40 -0800878static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879{
880 struct kvm_mmu *context = &vcpu->mmu;
881
882 ASSERT(is_pae(vcpu));
883 context->new_cr3 = paging_new_cr3;
884 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 context->gva_to_gpa = paging64_gva_to_gpa;
886 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800887 context->root_level = level;
888 context->shadow_root_level = level;
889 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 ASSERT(VALID_PAGE(context->root_hpa));
891 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
892 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
893 return 0;
894}
895
Avi Kivity17ac10a2007-01-05 16:36:40 -0800896static int paging64_init_context(struct kvm_vcpu *vcpu)
897{
898 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
899}
900
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901static int paging32_init_context(struct kvm_vcpu *vcpu)
902{
903 struct kvm_mmu *context = &vcpu->mmu;
904
905 context->new_cr3 = paging_new_cr3;
906 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 context->gva_to_gpa = paging32_gva_to_gpa;
908 context->free = paging_free;
909 context->root_level = PT32_ROOT_LEVEL;
910 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800911 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 ASSERT(VALID_PAGE(context->root_hpa));
913 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
914 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
915 return 0;
916}
917
918static int paging32E_init_context(struct kvm_vcpu *vcpu)
919{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800920 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921}
922
923static int init_kvm_mmu(struct kvm_vcpu *vcpu)
924{
925 ASSERT(vcpu);
926 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
927
928 if (!is_paging(vcpu))
929 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -0800930 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 return paging64_init_context(vcpu);
932 else if (is_pae(vcpu))
933 return paging32E_init_context(vcpu);
934 else
935 return paging32_init_context(vcpu);
936}
937
938static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
939{
940 ASSERT(vcpu);
941 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
942 vcpu->mmu.free(vcpu);
943 vcpu->mmu.root_hpa = INVALID_PAGE;
944 }
945}
946
947int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
948{
949 destroy_kvm_mmu(vcpu);
950 return init_kvm_mmu(vcpu);
951}
952
Avi Kivityda4a00f2007-01-05 16:36:44 -0800953void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
954{
Avi Kivity9b7a0322007-01-05 16:36:45 -0800955 gfn_t gfn = gpa >> PAGE_SHIFT;
956 struct kvm_mmu_page *page;
957 struct kvm_mmu_page *child;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -0800958 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -0800959 struct hlist_head *bucket;
960 unsigned index;
961 u64 *spte;
962 u64 pte;
963 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -0800964 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -0800965 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -0800966 unsigned misaligned;
Avi Kivity9b7a0322007-01-05 16:36:45 -0800967 int level;
968
Avi Kivityda4a00f2007-01-05 16:36:44 -0800969 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity9b7a0322007-01-05 16:36:45 -0800970 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
971 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -0800972 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -0800973 if (page->gfn != gfn || page->role.metaphysical)
974 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -0800975 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
976 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
977 if (misaligned) {
978 /*
979 * Misaligned accesses are too much trouble to fix
980 * up; also, they usually indicate a page is not used
981 * as a page table.
982 */
983 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
984 gpa, bytes, page->role.word);
985 kvm_mmu_zap_page(vcpu, page);
986 continue;
987 }
Avi Kivity9b7a0322007-01-05 16:36:45 -0800988 page_offset = offset;
989 level = page->role.level;
990 if (page->role.glevels == PT32_ROOT_LEVEL) {
991 page_offset <<= 1; /* 32->64 */
992 page_offset &= ~PAGE_MASK;
993 }
994 spte = __va(page->page_hpa);
995 spte += page_offset / sizeof(*spte);
996 pte = *spte;
997 if (is_present_pte(pte)) {
998 if (level == PT_PAGE_TABLE_LEVEL)
999 rmap_remove(vcpu->kvm, spte);
1000 else {
1001 child = page_header(pte & PT64_BASE_ADDR_MASK);
1002 mmu_page_remove_parent_pte(child, spte);
1003 }
1004 }
1005 *spte = 0;
1006 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001007}
1008
1009void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1010{
1011}
1012
Avi Kivitya4360362007-01-05 16:36:45 -08001013int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1014{
1015 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1016
1017 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1018}
1019
Avi Kivityebeace82007-01-05 16:36:47 -08001020void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1021{
1022 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1023 struct kvm_mmu_page *page;
1024
1025 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1026 struct kvm_mmu_page, link);
1027 kvm_mmu_zap_page(vcpu, page);
1028 }
1029}
1030EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1031
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032static void free_mmu_pages(struct kvm_vcpu *vcpu)
1033{
1034 while (!list_empty(&vcpu->free_pages)) {
1035 struct kvm_mmu_page *page;
1036
1037 page = list_entry(vcpu->free_pages.next,
1038 struct kvm_mmu_page, link);
1039 list_del(&page->link);
1040 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1041 page->page_hpa = INVALID_PAGE;
1042 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001043 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044}
1045
1046static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1047{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001048 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 int i;
1050
1051 ASSERT(vcpu);
1052
1053 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1055
1056 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001057 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 goto error_1;
1059 page->private = (unsigned long)page_header;
1060 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1061 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1062 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001063 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001065
1066 /*
1067 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1068 * Therefore we need to allocate shadow page tables in the first
1069 * 4GB of memory, which happens to fit the DMA32 zone.
1070 */
1071 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1072 if (!page)
1073 goto error_1;
1074 vcpu->mmu.pae_root = page_address(page);
1075 for (i = 0; i < 4; ++i)
1076 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 return 0;
1079
1080error_1:
1081 free_mmu_pages(vcpu);
1082 return -ENOMEM;
1083}
1084
Ingo Molnar8018c272006-12-29 16:50:01 -08001085int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 ASSERT(vcpu);
1088 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1089 ASSERT(list_empty(&vcpu->free_pages));
1090
Ingo Molnar8018c272006-12-29 16:50:01 -08001091 return alloc_mmu_pages(vcpu);
1092}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093
Ingo Molnar8018c272006-12-29 16:50:01 -08001094int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1095{
1096 ASSERT(vcpu);
1097 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1098 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001099
Ingo Molnar8018c272006-12-29 16:50:01 -08001100 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101}
1102
1103void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1104{
1105 ASSERT(vcpu);
1106
1107 destroy_kvm_mmu(vcpu);
1108 free_mmu_pages(vcpu);
1109}
1110
1111void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1112{
1113 struct kvm_mmu_page *page;
1114
1115 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1116 int i;
1117 u64 *pt;
1118
1119 if (!test_bit(slot, &page->slot_bitmap))
1120 continue;
1121
1122 pt = __va(page->page_hpa);
1123 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1124 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001125 if (pt[i] & PT_WRITABLE_MASK) {
1126 rmap_remove(kvm, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001128 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 }
1130}