blob: 4843e95e54e14ddcb2d4cd0b5c92fb9aa357866d [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 Kivity37a7d8b2007-01-05 16:36:56 -080029#undef MMU_DEBUG
30
31#undef AUDIT
32
33#ifdef AUDIT
34static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
35#else
36static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
37#endif
38
39#ifdef MMU_DEBUG
40
41#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
42#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
43
44#else
45
46#define pgprintk(x...) do { } while (0)
47#define rmap_printk(x...) do { } while (0)
48
49#endif
50
51#if defined(MMU_DEBUG) || defined(AUDIT)
52static int dbg = 1;
53#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
55#define ASSERT(x) \
56 if (!(x)) { \
57 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
58 __FILE__, __LINE__, #x); \
59 }
60
Avi Kivitycea0f0e2007-01-05 16:36:43 -080061#define PT64_PT_BITS 9
62#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
63#define PT32_PT_BITS 10
64#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065
66#define PT_WRITABLE_SHIFT 1
67
68#define PT_PRESENT_MASK (1ULL << 0)
69#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
70#define PT_USER_MASK (1ULL << 2)
71#define PT_PWT_MASK (1ULL << 3)
72#define PT_PCD_MASK (1ULL << 4)
73#define PT_ACCESSED_MASK (1ULL << 5)
74#define PT_DIRTY_MASK (1ULL << 6)
75#define PT_PAGE_SIZE_MASK (1ULL << 7)
76#define PT_PAT_MASK (1ULL << 7)
77#define PT_GLOBAL_MASK (1ULL << 8)
78#define PT64_NX_MASK (1ULL << 63)
79
80#define PT_PAT_SHIFT 7
81#define PT_DIR_PAT_SHIFT 12
82#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
83
84#define PT32_DIR_PSE36_SIZE 4
85#define PT32_DIR_PSE36_SHIFT 13
86#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
87
88
89#define PT32_PTE_COPY_MASK \
Avi Kivity8c7bb722006-12-13 00:34:02 -080090 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
Avi Kivity8c7bb722006-12-13 00:34:02 -080092#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093
94#define PT_FIRST_AVAIL_BITS_SHIFT 9
95#define PT64_SECOND_AVAIL_BITS_SHIFT 52
96
97#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
98#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
99
100#define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
101#define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
102
103#define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
104#define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
105
106#define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
107
108#define VALID_PAGE(x) ((x) != INVALID_PAGE)
109
110#define PT64_LEVEL_BITS 9
111
112#define PT64_LEVEL_SHIFT(level) \
113 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
114
115#define PT64_LEVEL_MASK(level) \
116 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
117
118#define PT64_INDEX(address, level)\
119 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
120
121
122#define PT32_LEVEL_BITS 10
123
124#define PT32_LEVEL_SHIFT(level) \
125 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
126
127#define PT32_LEVEL_MASK(level) \
128 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
129
130#define PT32_INDEX(address, level)\
131 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
132
133
Avi Kivity27aba762007-03-09 13:04:31 +0200134#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135#define PT64_DIR_BASE_ADDR_MASK \
136 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
137
138#define PT32_BASE_ADDR_MASK PAGE_MASK
139#define PT32_DIR_BASE_ADDR_MASK \
140 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
141
142
143#define PFERR_PRESENT_MASK (1U << 0)
144#define PFERR_WRITE_MASK (1U << 1)
145#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800146#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147
148#define PT64_ROOT_LEVEL 4
149#define PT32_ROOT_LEVEL 2
150#define PT32E_ROOT_LEVEL 3
151
152#define PT_DIRECTORY_LEVEL 2
153#define PT_PAGE_TABLE_LEVEL 1
154
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800155#define RMAP_EXT 4
156
157struct kvm_rmap_desc {
158 u64 *shadow_ptes[RMAP_EXT];
159 struct kvm_rmap_desc *more;
160};
161
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162static int is_write_protection(struct kvm_vcpu *vcpu)
163{
164 return vcpu->cr0 & CR0_WP_MASK;
165}
166
167static int is_cpuid_PSE36(void)
168{
169 return 1;
170}
171
Avi Kivity73b10872007-01-26 00:56:41 -0800172static int is_nx(struct kvm_vcpu *vcpu)
173{
174 return vcpu->shadow_efer & EFER_NX;
175}
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177static int is_present_pte(unsigned long pte)
178{
179 return pte & PT_PRESENT_MASK;
180}
181
182static int is_writeble_pte(unsigned long pte)
183{
184 return pte & PT_WRITABLE_MASK;
185}
186
187static int is_io_pte(unsigned long pte)
188{
189 return pte & PT_SHADOW_IO_MARK;
190}
191
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800192static int is_rmap_pte(u64 pte)
193{
194 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
195 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
196}
197
Avi Kivitye2dec932007-01-05 16:36:54 -0800198static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
199 size_t objsize, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800200{
201 void *obj;
202
203 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800204 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800205 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
206 obj = kzalloc(objsize, GFP_NOWAIT);
207 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800208 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800209 cache->objects[cache->nobjs++] = obj;
210 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800211 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800212}
213
214static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
215{
216 while (mc->nobjs)
217 kfree(mc->objects[--mc->nobjs]);
218}
219
Avi Kivitye2dec932007-01-05 16:36:54 -0800220static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800221{
Avi Kivitye2dec932007-01-05 16:36:54 -0800222 int r;
223
224 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
225 sizeof(struct kvm_pte_chain), 4);
226 if (r)
227 goto out;
228 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
229 sizeof(struct kvm_rmap_desc), 1);
230out:
231 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800232}
233
234static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
235{
236 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
237 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
238}
239
240static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
241 size_t size)
242{
243 void *p;
244
245 BUG_ON(!mc->nobjs);
246 p = mc->objects[--mc->nobjs];
247 memset(p, 0, size);
248 return p;
249}
250
251static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
252{
253 if (mc->nobjs < KVM_NR_MEM_OBJS)
254 mc->objects[mc->nobjs++] = obj;
255 else
256 kfree(obj);
257}
258
259static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
260{
261 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
262 sizeof(struct kvm_pte_chain));
263}
264
265static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
266 struct kvm_pte_chain *pc)
267{
268 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
269}
270
271static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
272{
273 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
274 sizeof(struct kvm_rmap_desc));
275}
276
277static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
278 struct kvm_rmap_desc *rd)
279{
280 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
281}
282
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800283/*
284 * Reverse mapping data structures:
285 *
286 * If page->private bit zero is zero, then page->private points to the
287 * shadow page table entry that points to page_address(page).
288 *
289 * If page->private bit zero is one, (then page->private & ~1) points
290 * to a struct kvm_rmap_desc containing more mappings.
291 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800292static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800293{
294 struct page *page;
295 struct kvm_rmap_desc *desc;
296 int i;
297
298 if (!is_rmap_pte(*spte))
299 return;
300 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200301 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800302 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200303 set_page_private(page,(unsigned long)spte);
304 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800305 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800306 desc = mmu_alloc_rmap_desc(vcpu);
Markus Rechberger5972e952007-02-19 14:37:47 +0200307 desc->shadow_ptes[0] = (u64 *)page_private(page);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800308 desc->shadow_ptes[1] = spte;
Markus Rechberger5972e952007-02-19 14:37:47 +0200309 set_page_private(page,(unsigned long)desc | 1);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800310 } else {
311 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200312 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800313 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
314 desc = desc->more;
315 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800316 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800317 desc = desc->more;
318 }
319 for (i = 0; desc->shadow_ptes[i]; ++i)
320 ;
321 desc->shadow_ptes[i] = spte;
322 }
323}
324
Avi Kivity714b93d2007-01-05 16:36:53 -0800325static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
326 struct page *page,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800327 struct kvm_rmap_desc *desc,
328 int i,
329 struct kvm_rmap_desc *prev_desc)
330{
331 int j;
332
333 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
334 ;
335 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000336 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800337 if (j != 0)
338 return;
339 if (!prev_desc && !desc->more)
Markus Rechberger5972e952007-02-19 14:37:47 +0200340 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800341 else
342 if (prev_desc)
343 prev_desc->more = desc->more;
344 else
Markus Rechberger5972e952007-02-19 14:37:47 +0200345 set_page_private(page,(unsigned long)desc->more | 1);
Avi Kivity714b93d2007-01-05 16:36:53 -0800346 mmu_free_rmap_desc(vcpu, desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800347}
348
Avi Kivity714b93d2007-01-05 16:36:53 -0800349static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350{
351 struct page *page;
352 struct kvm_rmap_desc *desc;
353 struct kvm_rmap_desc *prev_desc;
354 int i;
355
356 if (!is_rmap_pte(*spte))
357 return;
358 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200359 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800360 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
361 BUG();
Markus Rechberger5972e952007-02-19 14:37:47 +0200362 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800363 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200364 if ((u64 *)page_private(page) != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800365 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
366 spte, *spte);
367 BUG();
368 }
Markus Rechberger5972e952007-02-19 14:37:47 +0200369 set_page_private(page,0);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370 } else {
371 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200372 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373 prev_desc = NULL;
374 while (desc) {
375 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
376 if (desc->shadow_ptes[i] == spte) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800377 rmap_desc_remove_entry(vcpu, page,
378 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800379 prev_desc);
380 return;
381 }
382 prev_desc = desc;
383 desc = desc->more;
384 }
385 BUG();
386 }
387}
388
Avi Kivity714b93d2007-01-05 16:36:53 -0800389static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800390{
Avi Kivity714b93d2007-01-05 16:36:53 -0800391 struct kvm *kvm = vcpu->kvm;
Avi Kivity374cbac2007-01-05 16:36:43 -0800392 struct page *page;
393 struct kvm_memory_slot *slot;
394 struct kvm_rmap_desc *desc;
395 u64 *spte;
396
397 slot = gfn_to_memslot(kvm, gfn);
398 BUG_ON(!slot);
399 page = gfn_to_page(slot, gfn);
400
Markus Rechberger5972e952007-02-19 14:37:47 +0200401 while (page_private(page)) {
402 if (!(page_private(page) & 1))
403 spte = (u64 *)page_private(page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800404 else {
Markus Rechberger5972e952007-02-19 14:37:47 +0200405 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800406 spte = desc->shadow_ptes[0];
407 }
408 BUG_ON(!spte);
Avi Kivity27aba762007-03-09 13:04:31 +0200409 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
410 != page_to_pfn(page));
Avi Kivity374cbac2007-01-05 16:36:43 -0800411 BUG_ON(!(*spte & PT_PRESENT_MASK));
412 BUG_ON(!(*spte & PT_WRITABLE_MASK));
413 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800414 rmap_remove(vcpu, spte);
Avi Kivity40907d52007-01-05 16:36:55 -0800415 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity374cbac2007-01-05 16:36:43 -0800416 *spte &= ~(u64)PT_WRITABLE_MASK;
417 }
418}
419
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420static int is_empty_shadow_page(hpa_t page_hpa)
421{
Avi Kivity139bdb22007-01-05 16:36:50 -0800422 u64 *pos;
423 u64 *end;
424
425 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800427 if (*pos != 0) {
428 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
429 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800431 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 return 1;
433}
434
Avi Kivity260746c2007-01-05 16:36:49 -0800435static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
436{
437 struct kvm_mmu_page *page_head = page_header(page_hpa);
438
Avi Kivity5f1e0b62007-01-05 16:36:49 -0800439 ASSERT(is_empty_shadow_page(page_hpa));
Avi Kivity260746c2007-01-05 16:36:49 -0800440 list_del(&page_head->link);
441 page_head->page_hpa = page_hpa;
442 list_add(&page_head->link, &vcpu->free_pages);
443 ++vcpu->kvm->n_free_mmu_pages;
444}
445
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800446static unsigned kvm_page_table_hashfn(gfn_t gfn)
447{
448 return gfn;
449}
450
Avi Kivity25c0de22007-01-05 16:36:42 -0800451static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
452 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453{
454 struct kvm_mmu_page *page;
455
456 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800457 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
459 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
460 list_del(&page->link);
461 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
462 ASSERT(is_empty_shadow_page(page->page_hpa));
463 page->slot_bitmap = 0;
464 page->global = 1;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800465 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800467 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800468 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469}
470
Avi Kivity714b93d2007-01-05 16:36:53 -0800471static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
472 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800473{
474 struct kvm_pte_chain *pte_chain;
475 struct hlist_node *node;
476 int i;
477
478 if (!parent_pte)
479 return;
480 if (!page->multimapped) {
481 u64 *old = page->parent_pte;
482
483 if (!old) {
484 page->parent_pte = parent_pte;
485 return;
486 }
487 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800488 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800489 INIT_HLIST_HEAD(&page->parent_ptes);
490 hlist_add_head(&pte_chain->link, &page->parent_ptes);
491 pte_chain->parent_ptes[0] = old;
492 }
493 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
494 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
495 continue;
496 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
497 if (!pte_chain->parent_ptes[i]) {
498 pte_chain->parent_ptes[i] = parent_pte;
499 return;
500 }
501 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800502 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800503 BUG_ON(!pte_chain);
504 hlist_add_head(&pte_chain->link, &page->parent_ptes);
505 pte_chain->parent_ptes[0] = parent_pte;
506}
507
Avi Kivity714b93d2007-01-05 16:36:53 -0800508static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
509 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800510 u64 *parent_pte)
511{
512 struct kvm_pte_chain *pte_chain;
513 struct hlist_node *node;
514 int i;
515
516 if (!page->multimapped) {
517 BUG_ON(page->parent_pte != parent_pte);
518 page->parent_pte = NULL;
519 return;
520 }
521 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
522 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
523 if (!pte_chain->parent_ptes[i])
524 break;
525 if (pte_chain->parent_ptes[i] != parent_pte)
526 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800527 while (i + 1 < NR_PTE_CHAIN_ENTRIES
528 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800529 pte_chain->parent_ptes[i]
530 = pte_chain->parent_ptes[i + 1];
531 ++i;
532 }
533 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800534 if (i == 0) {
535 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800536 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800537 if (hlist_empty(&page->parent_ptes)) {
538 page->multimapped = 0;
539 page->parent_pte = NULL;
540 }
541 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800542 return;
543 }
544 BUG();
545}
546
547static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
548 gfn_t gfn)
549{
550 unsigned index;
551 struct hlist_head *bucket;
552 struct kvm_mmu_page *page;
553 struct hlist_node *node;
554
555 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
556 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
557 bucket = &vcpu->kvm->mmu_page_hash[index];
558 hlist_for_each_entry(page, node, bucket, hash_link)
559 if (page->gfn == gfn && !page->role.metaphysical) {
560 pgprintk("%s: found role %x\n",
561 __FUNCTION__, page->role.word);
562 return page;
563 }
564 return NULL;
565}
566
567static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
568 gfn_t gfn,
569 gva_t gaddr,
570 unsigned level,
571 int metaphysical,
572 u64 *parent_pte)
573{
574 union kvm_mmu_page_role role;
575 unsigned index;
576 unsigned quadrant;
577 struct hlist_head *bucket;
578 struct kvm_mmu_page *page;
579 struct hlist_node *node;
580
581 role.word = 0;
582 role.glevels = vcpu->mmu.root_level;
583 role.level = level;
584 role.metaphysical = metaphysical;
585 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
586 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
587 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
588 role.quadrant = quadrant;
589 }
590 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
591 gfn, role.word);
592 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
593 bucket = &vcpu->kvm->mmu_page_hash[index];
594 hlist_for_each_entry(page, node, bucket, hash_link)
595 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800596 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800597 pgprintk("%s: found\n", __FUNCTION__);
598 return page;
599 }
600 page = kvm_mmu_alloc_page(vcpu, parent_pte);
601 if (!page)
602 return page;
603 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
604 page->gfn = gfn;
605 page->role = role;
606 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800607 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800608 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800609 return page;
610}
611
Avi Kivitya4360362007-01-05 16:36:45 -0800612static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
613 struct kvm_mmu_page *page)
614{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800615 unsigned i;
616 u64 *pt;
617 u64 ent;
618
619 pt = __va(page->page_hpa);
620
621 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
622 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
623 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800624 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800625 pt[i] = 0;
626 }
Avi Kivity40907d52007-01-05 16:36:55 -0800627 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800628 return;
629 }
630
631 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
632 ent = pt[i];
633
634 pt[i] = 0;
635 if (!(ent & PT_PRESENT_MASK))
636 continue;
637 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800638 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800639 }
Avi Kivitya4360362007-01-05 16:36:45 -0800640}
641
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800642static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
643 struct kvm_mmu_page *page,
644 u64 *parent_pte)
645{
Avi Kivity714b93d2007-01-05 16:36:53 -0800646 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800647}
648
649static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
650 struct kvm_mmu_page *page)
651{
652 u64 *parent_pte;
653
654 while (page->multimapped || page->parent_pte) {
655 if (!page->multimapped)
656 parent_pte = page->parent_pte;
657 else {
658 struct kvm_pte_chain *chain;
659
660 chain = container_of(page->parent_ptes.first,
661 struct kvm_pte_chain, link);
662 parent_pte = chain->parent_ptes[0];
663 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800664 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800665 kvm_mmu_put_page(vcpu, page, parent_pte);
666 *parent_pte = 0;
667 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800668 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800669 if (!page->root_count) {
670 hlist_del(&page->hash_link);
671 kvm_mmu_free_page(vcpu, page->page_hpa);
672 } else {
673 list_del(&page->link);
674 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
675 }
Avi Kivitya4360362007-01-05 16:36:45 -0800676}
677
678static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
679{
680 unsigned index;
681 struct hlist_head *bucket;
682 struct kvm_mmu_page *page;
683 struct hlist_node *node, *n;
684 int r;
685
686 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
687 r = 0;
688 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
689 bucket = &vcpu->kvm->mmu_page_hash[index];
690 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
691 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800692 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
693 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800694 kvm_mmu_zap_page(vcpu, page);
695 r = 1;
696 }
697 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800698}
699
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
701{
702 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
703 struct kvm_mmu_page *page_head = page_header(__pa(pte));
704
705 __set_bit(slot, &page_head->slot_bitmap);
706}
707
708hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
709{
710 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
711
712 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
713}
714
715hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
716{
717 struct kvm_memory_slot *slot;
718 struct page *page;
719
720 ASSERT((gpa & HPA_ERR_MASK) == 0);
721 slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
722 if (!slot)
723 return gpa | HPA_ERR_MASK;
724 page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
725 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
726 | (gpa & (PAGE_SIZE-1));
727}
728
729hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
730{
731 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
732
733 if (gpa == UNMAPPED_GVA)
734 return UNMAPPED_GVA;
735 return gpa_to_hpa(vcpu, gpa);
736}
737
Avi Kivity039576c2007-03-20 12:46:50 +0200738struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
739{
740 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
741
742 if (gpa == UNMAPPED_GVA)
743 return NULL;
744 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
745}
746
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
748{
749}
750
751static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
752{
753 int level = PT32E_ROOT_LEVEL;
754 hpa_t table_addr = vcpu->mmu.root_hpa;
755
756 for (; ; level--) {
757 u32 index = PT64_INDEX(v, level);
758 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800759 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760
761 ASSERT(VALID_PAGE(table_addr));
762 table = __va(table_addr);
763
764 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800765 pte = table[index];
766 if (is_present_pte(pte) && is_writeble_pte(pte))
767 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
769 page_header_update_slot(vcpu->kvm, table, v);
770 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
771 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800772 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 return 0;
774 }
775
776 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800777 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800778 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800780 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
781 >> PAGE_SHIFT;
782 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
783 v, level - 1,
784 1, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800785 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 pgprintk("nonpaging_map: ENOMEM\n");
787 return -ENOMEM;
788 }
789
Avi Kivity25c0de22007-01-05 16:36:42 -0800790 table[index] = new_table->page_hpa | PT_PRESENT_MASK
791 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 }
793 table_addr = table[index] & PT64_BASE_ADDR_MASK;
794 }
795}
796
Avi Kivity17ac10a2007-01-05 16:36:40 -0800797static void mmu_free_roots(struct kvm_vcpu *vcpu)
798{
799 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800800 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800801
802#ifdef CONFIG_X86_64
803 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
804 hpa_t root = vcpu->mmu.root_hpa;
805
806 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800807 page = page_header(root);
808 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800809 vcpu->mmu.root_hpa = INVALID_PAGE;
810 return;
811 }
812#endif
813 for (i = 0; i < 4; ++i) {
814 hpa_t root = vcpu->mmu.pae_root[i];
815
816 ASSERT(VALID_PAGE(root));
817 root &= PT64_BASE_ADDR_MASK;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800818 page = page_header(root);
819 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800820 vcpu->mmu.pae_root[i] = INVALID_PAGE;
821 }
822 vcpu->mmu.root_hpa = INVALID_PAGE;
823}
824
825static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
826{
827 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800828 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800829 struct kvm_mmu_page *page;
830
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800831 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800832
833#ifdef CONFIG_X86_64
834 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
835 hpa_t root = vcpu->mmu.root_hpa;
836
837 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800838 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
839 PT64_ROOT_LEVEL, 0, NULL);
840 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800841 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800842 vcpu->mmu.root_hpa = root;
843 return;
844 }
845#endif
846 for (i = 0; i < 4; ++i) {
847 hpa_t root = vcpu->mmu.pae_root[i];
848
849 ASSERT(!VALID_PAGE(root));
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800850 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
851 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
852 else if (vcpu->mmu.root_level == 0)
853 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800854 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800855 PT32_ROOT_LEVEL, !is_paging(vcpu),
Ingo Molnar68a99f62007-01-05 16:36:59 -0800856 NULL);
857 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800858 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800859 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
860 }
861 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
862}
863
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
865{
866 return vaddr;
867}
868
869static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
870 u32 error_code)
871{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800873 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800874 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
Avi Kivitye2dec932007-01-05 16:36:54 -0800876 r = mmu_topup_memory_caches(vcpu);
877 if (r)
878 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800879
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 ASSERT(vcpu);
881 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
882
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883
Avi Kivityebeace82007-01-05 16:36:47 -0800884 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885
Avi Kivityebeace82007-01-05 16:36:47 -0800886 if (is_error_hpa(paddr))
887 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888
Avi Kivityebeace82007-01-05 16:36:47 -0800889 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890}
891
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892static void nonpaging_free(struct kvm_vcpu *vcpu)
893{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800894 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895}
896
897static int nonpaging_init_context(struct kvm_vcpu *vcpu)
898{
899 struct kvm_mmu *context = &vcpu->mmu;
900
901 context->new_cr3 = nonpaging_new_cr3;
902 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 context->gva_to_gpa = nonpaging_gva_to_gpa;
904 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800905 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800907 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 ASSERT(VALID_PAGE(context->root_hpa));
909 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
910 return 0;
911}
912
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
914{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 ++kvm_stat.tlb_flush;
916 kvm_arch_ops->tlb_flush(vcpu);
917}
918
919static void paging_new_cr3(struct kvm_vcpu *vcpu)
920{
Avi Kivity374cbac2007-01-05 16:36:43 -0800921 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800922 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800923 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
924 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800925 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928}
929
930static void mark_pagetable_nonglobal(void *shadow_pte)
931{
932 page_header(__pa(shadow_pte))->global = 0;
933}
934
935static inline void set_pte_common(struct kvm_vcpu *vcpu,
936 u64 *shadow_pte,
937 gpa_t gaddr,
938 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800939 u64 access_bits,
940 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941{
942 hpa_t paddr;
943
944 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
945 if (!dirty)
946 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947
948 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
949
Avi Kivity374cbac2007-01-05 16:36:43 -0800950 *shadow_pte |= access_bits;
951
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 if (!(*shadow_pte & PT_GLOBAL_MASK))
953 mark_pagetable_nonglobal(shadow_pte);
954
955 if (is_error_hpa(paddr)) {
956 *shadow_pte |= gaddr;
957 *shadow_pte |= PT_SHADOW_IO_MARK;
958 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800959 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800961
962 *shadow_pte |= paddr;
963
964 if (access_bits & PT_WRITABLE_MASK) {
965 struct kvm_mmu_page *shadow;
966
Avi Kivity815af8d2007-01-05 16:36:44 -0800967 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800968 if (shadow) {
969 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800970 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800971 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity40907d52007-01-05 16:36:55 -0800972 if (is_writeble_pte(*shadow_pte)) {
973 *shadow_pte &= ~PT_WRITABLE_MASK;
974 kvm_arch_ops->tlb_flush(vcpu);
975 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800976 }
977 }
978
979 if (access_bits & PT_WRITABLE_MASK)
980 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
981
982 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -0800983 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984}
985
986static void inject_page_fault(struct kvm_vcpu *vcpu,
987 u64 addr,
988 u32 err_code)
989{
990 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
991}
992
993static inline int fix_read_pf(u64 *shadow_ent)
994{
995 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
996 !(*shadow_ent & PT_USER_MASK)) {
997 /*
998 * If supervisor write protect is disabled, we shadow kernel
999 * pages as user pages so we can trap the write access.
1000 */
1001 *shadow_ent |= PT_USER_MASK;
1002 *shadow_ent &= ~PT_WRITABLE_MASK;
1003
1004 return 1;
1005
1006 }
1007 return 0;
1008}
1009
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010static void paging_free(struct kvm_vcpu *vcpu)
1011{
1012 nonpaging_free(vcpu);
1013}
1014
1015#define PTTYPE 64
1016#include "paging_tmpl.h"
1017#undef PTTYPE
1018
1019#define PTTYPE 32
1020#include "paging_tmpl.h"
1021#undef PTTYPE
1022
Avi Kivity17ac10a2007-01-05 16:36:40 -08001023static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024{
1025 struct kvm_mmu *context = &vcpu->mmu;
1026
1027 ASSERT(is_pae(vcpu));
1028 context->new_cr3 = paging_new_cr3;
1029 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 context->gva_to_gpa = paging64_gva_to_gpa;
1031 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001032 context->root_level = level;
1033 context->shadow_root_level = level;
1034 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 ASSERT(VALID_PAGE(context->root_hpa));
1036 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1037 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1038 return 0;
1039}
1040
Avi Kivity17ac10a2007-01-05 16:36:40 -08001041static int paging64_init_context(struct kvm_vcpu *vcpu)
1042{
1043 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1044}
1045
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046static int paging32_init_context(struct kvm_vcpu *vcpu)
1047{
1048 struct kvm_mmu *context = &vcpu->mmu;
1049
1050 context->new_cr3 = paging_new_cr3;
1051 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 context->gva_to_gpa = paging32_gva_to_gpa;
1053 context->free = paging_free;
1054 context->root_level = PT32_ROOT_LEVEL;
1055 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001056 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 ASSERT(VALID_PAGE(context->root_hpa));
1058 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1059 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1060 return 0;
1061}
1062
1063static int paging32E_init_context(struct kvm_vcpu *vcpu)
1064{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001065 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066}
1067
1068static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1069{
1070 ASSERT(vcpu);
1071 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1072
1073 if (!is_paging(vcpu))
1074 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001075 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 return paging64_init_context(vcpu);
1077 else if (is_pae(vcpu))
1078 return paging32E_init_context(vcpu);
1079 else
1080 return paging32_init_context(vcpu);
1081}
1082
1083static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1084{
1085 ASSERT(vcpu);
1086 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1087 vcpu->mmu.free(vcpu);
1088 vcpu->mmu.root_hpa = INVALID_PAGE;
1089 }
1090}
1091
1092int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1093{
Avi Kivity714b93d2007-01-05 16:36:53 -08001094 int r;
1095
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001097 r = init_kvm_mmu(vcpu);
1098 if (r < 0)
1099 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001100 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001101out:
1102 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103}
1104
Avi Kivityac1b7142007-03-08 17:13:32 +02001105static void mmu_pre_write_zap_pte(struct kvm_vcpu *vcpu,
1106 struct kvm_mmu_page *page,
1107 u64 *spte)
1108{
1109 u64 pte;
1110 struct kvm_mmu_page *child;
1111
1112 pte = *spte;
1113 if (is_present_pte(pte)) {
1114 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1115 rmap_remove(vcpu, spte);
1116 else {
1117 child = page_header(pte & PT64_BASE_ADDR_MASK);
1118 mmu_page_remove_parent_pte(vcpu, child, spte);
1119 }
1120 }
1121 *spte = 0;
1122}
1123
Avi Kivityda4a00f2007-01-05 16:36:44 -08001124void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1125{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001126 gfn_t gfn = gpa >> PAGE_SHIFT;
1127 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001128 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001129 struct hlist_head *bucket;
1130 unsigned index;
1131 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001132 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001133 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001134 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001135 unsigned misaligned;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001136 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001137 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001138 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001139
Avi Kivityda4a00f2007-01-05 16:36:44 -08001140 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001141 if (gfn == vcpu->last_pt_write_gfn) {
1142 ++vcpu->last_pt_write_count;
1143 if (vcpu->last_pt_write_count >= 3)
1144 flooded = 1;
1145 } else {
1146 vcpu->last_pt_write_gfn = gfn;
1147 vcpu->last_pt_write_count = 1;
1148 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001149 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1150 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001151 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001152 if (page->gfn != gfn || page->role.metaphysical)
1153 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001154 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1155 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001156 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001157 /*
1158 * Misaligned accesses are too much trouble to fix
1159 * up; also, they usually indicate a page is not used
1160 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001161 *
1162 * If we're seeing too many writes to a page,
1163 * it may no longer be a page table, or we may be
1164 * forking, in which case it is better to unmap the
1165 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001166 */
1167 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1168 gpa, bytes, page->role.word);
1169 kvm_mmu_zap_page(vcpu, page);
1170 continue;
1171 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001172 page_offset = offset;
1173 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001174 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001175 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001176 page_offset <<= 1; /* 32->64 */
1177 /*
1178 * A 32-bit pde maps 4MB while the shadow pdes map
1179 * only 2MB. So we need to double the offset again
1180 * and zap two pdes instead of one.
1181 */
1182 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001183 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001184 page_offset <<= 1;
1185 npte = 2;
1186 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001187 page_offset &= ~PAGE_MASK;
1188 }
1189 spte = __va(page->page_hpa);
1190 spte += page_offset / sizeof(*spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001191 while (npte--) {
1192 mmu_pre_write_zap_pte(vcpu, page, spte);
1193 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001194 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001195 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001196}
1197
1198void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1199{
1200}
1201
Avi Kivitya4360362007-01-05 16:36:45 -08001202int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1203{
1204 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1205
1206 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1207}
1208
Avi Kivityebeace82007-01-05 16:36:47 -08001209void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1210{
1211 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1212 struct kvm_mmu_page *page;
1213
1214 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1215 struct kvm_mmu_page, link);
1216 kvm_mmu_zap_page(vcpu, page);
1217 }
1218}
1219EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1220
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221static void free_mmu_pages(struct kvm_vcpu *vcpu)
1222{
Avi Kivityf51234c2007-01-05 16:36:52 -08001223 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224
Avi Kivityf51234c2007-01-05 16:36:52 -08001225 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1226 page = container_of(vcpu->kvm->active_mmu_pages.next,
1227 struct kvm_mmu_page, link);
1228 kvm_mmu_zap_page(vcpu, page);
1229 }
1230 while (!list_empty(&vcpu->free_pages)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 page = list_entry(vcpu->free_pages.next,
1232 struct kvm_mmu_page, link);
1233 list_del(&page->link);
1234 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1235 page->page_hpa = INVALID_PAGE;
1236 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001237 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238}
1239
1240static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1241{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001242 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 int i;
1244
1245 ASSERT(vcpu);
1246
1247 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1249
1250 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001251 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 goto error_1;
Markus Rechberger5972e952007-02-19 14:37:47 +02001253 set_page_private(page, (unsigned long)page_header);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1255 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1256 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001257 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001259
1260 /*
1261 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1262 * Therefore we need to allocate shadow page tables in the first
1263 * 4GB of memory, which happens to fit the DMA32 zone.
1264 */
1265 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1266 if (!page)
1267 goto error_1;
1268 vcpu->mmu.pae_root = page_address(page);
1269 for (i = 0; i < 4; ++i)
1270 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1271
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 return 0;
1273
1274error_1:
1275 free_mmu_pages(vcpu);
1276 return -ENOMEM;
1277}
1278
Ingo Molnar8018c272006-12-29 16:50:01 -08001279int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 ASSERT(vcpu);
1282 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1283 ASSERT(list_empty(&vcpu->free_pages));
1284
Ingo Molnar8018c272006-12-29 16:50:01 -08001285 return alloc_mmu_pages(vcpu);
1286}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287
Ingo Molnar8018c272006-12-29 16:50:01 -08001288int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1289{
1290 ASSERT(vcpu);
1291 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1292 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001293
Ingo Molnar8018c272006-12-29 16:50:01 -08001294 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295}
1296
1297void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1298{
1299 ASSERT(vcpu);
1300
1301 destroy_kvm_mmu(vcpu);
1302 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001303 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304}
1305
Avi Kivity714b93d2007-01-05 16:36:53 -08001306void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
Avi Kivity714b93d2007-01-05 16:36:53 -08001308 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 struct kvm_mmu_page *page;
1310
1311 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1312 int i;
1313 u64 *pt;
1314
1315 if (!test_bit(slot, &page->slot_bitmap))
1316 continue;
1317
1318 pt = __va(page->page_hpa);
1319 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1320 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001321 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001322 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001324 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 }
1326}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001327
1328#ifdef AUDIT
1329
1330static const char *audit_msg;
1331
1332static gva_t canonicalize(gva_t gva)
1333{
1334#ifdef CONFIG_X86_64
1335 gva = (long long)(gva << 16) >> 16;
1336#endif
1337 return gva;
1338}
1339
1340static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1341 gva_t va, int level)
1342{
1343 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1344 int i;
1345 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1346
1347 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1348 u64 ent = pt[i];
1349
1350 if (!ent & PT_PRESENT_MASK)
1351 continue;
1352
1353 va = canonicalize(va);
1354 if (level > 1)
1355 audit_mappings_page(vcpu, ent, va, level - 1);
1356 else {
1357 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1358 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1359
1360 if ((ent & PT_PRESENT_MASK)
1361 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1362 printk(KERN_ERR "audit error: (%s) levels %d"
1363 " gva %lx gpa %llx hpa %llx ent %llx\n",
1364 audit_msg, vcpu->mmu.root_level,
1365 va, gpa, hpa, ent);
1366 }
1367 }
1368}
1369
1370static void audit_mappings(struct kvm_vcpu *vcpu)
1371{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001372 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001373
1374 if (vcpu->mmu.root_level == 4)
1375 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1376 else
1377 for (i = 0; i < 4; ++i)
1378 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1379 audit_mappings_page(vcpu,
1380 vcpu->mmu.pae_root[i],
1381 i << 30,
1382 2);
1383}
1384
1385static int count_rmaps(struct kvm_vcpu *vcpu)
1386{
1387 int nmaps = 0;
1388 int i, j, k;
1389
1390 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1391 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1392 struct kvm_rmap_desc *d;
1393
1394 for (j = 0; j < m->npages; ++j) {
1395 struct page *page = m->phys_mem[j];
1396
1397 if (!page->private)
1398 continue;
1399 if (!(page->private & 1)) {
1400 ++nmaps;
1401 continue;
1402 }
1403 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1404 while (d) {
1405 for (k = 0; k < RMAP_EXT; ++k)
1406 if (d->shadow_ptes[k])
1407 ++nmaps;
1408 else
1409 break;
1410 d = d->more;
1411 }
1412 }
1413 }
1414 return nmaps;
1415}
1416
1417static int count_writable_mappings(struct kvm_vcpu *vcpu)
1418{
1419 int nmaps = 0;
1420 struct kvm_mmu_page *page;
1421 int i;
1422
1423 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1424 u64 *pt = __va(page->page_hpa);
1425
1426 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1427 continue;
1428
1429 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1430 u64 ent = pt[i];
1431
1432 if (!(ent & PT_PRESENT_MASK))
1433 continue;
1434 if (!(ent & PT_WRITABLE_MASK))
1435 continue;
1436 ++nmaps;
1437 }
1438 }
1439 return nmaps;
1440}
1441
1442static void audit_rmap(struct kvm_vcpu *vcpu)
1443{
1444 int n_rmap = count_rmaps(vcpu);
1445 int n_actual = count_writable_mappings(vcpu);
1446
1447 if (n_rmap != n_actual)
1448 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1449 __FUNCTION__, audit_msg, n_rmap, n_actual);
1450}
1451
1452static void audit_write_protection(struct kvm_vcpu *vcpu)
1453{
1454 struct kvm_mmu_page *page;
1455
1456 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1457 hfn_t hfn;
1458 struct page *pg;
1459
1460 if (page->role.metaphysical)
1461 continue;
1462
1463 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1464 >> PAGE_SHIFT;
1465 pg = pfn_to_page(hfn);
1466 if (pg->private)
1467 printk(KERN_ERR "%s: (%s) shadow page has writable"
1468 " mappings: gfn %lx role %x\n",
1469 __FUNCTION__, audit_msg, page->gfn,
1470 page->role.word);
1471 }
1472}
1473
1474static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1475{
1476 int olddbg = dbg;
1477
1478 dbg = 0;
1479 audit_msg = msg;
1480 audit_rmap(vcpu);
1481 audit_write_protection(vcpu);
1482 audit_mappings(vcpu);
1483 dbg = olddbg;
1484}
1485
1486#endif