blob: 9ff74805c7d1563351a7cd68b3a301a87029f8de [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;
Avi Kivity374cbac2007-01-05 16:36:43 -0800393 struct kvm_rmap_desc *desc;
394 u64 *spte;
395
Avi Kivity954bbbc22007-03-30 14:02:32 +0300396 page = gfn_to_page(kvm, gfn);
397 BUG_ON(!page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800398
Markus Rechberger5972e952007-02-19 14:37:47 +0200399 while (page_private(page)) {
400 if (!(page_private(page) & 1))
401 spte = (u64 *)page_private(page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800402 else {
Markus Rechberger5972e952007-02-19 14:37:47 +0200403 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800404 spte = desc->shadow_ptes[0];
405 }
406 BUG_ON(!spte);
Avi Kivity27aba762007-03-09 13:04:31 +0200407 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
408 != page_to_pfn(page));
Avi Kivity374cbac2007-01-05 16:36:43 -0800409 BUG_ON(!(*spte & PT_PRESENT_MASK));
410 BUG_ON(!(*spte & PT_WRITABLE_MASK));
411 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800412 rmap_remove(vcpu, spte);
Avi Kivity40907d52007-01-05 16:36:55 -0800413 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity374cbac2007-01-05 16:36:43 -0800414 *spte &= ~(u64)PT_WRITABLE_MASK;
415 }
416}
417
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418static int is_empty_shadow_page(hpa_t page_hpa)
419{
Avi Kivity139bdb22007-01-05 16:36:50 -0800420 u64 *pos;
421 u64 *end;
422
423 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800425 if (*pos != 0) {
426 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
427 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800429 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 return 1;
431}
432
Avi Kivity260746c2007-01-05 16:36:49 -0800433static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
434{
435 struct kvm_mmu_page *page_head = page_header(page_hpa);
436
Avi Kivity5f1e0b62007-01-05 16:36:49 -0800437 ASSERT(is_empty_shadow_page(page_hpa));
Avi Kivity260746c2007-01-05 16:36:49 -0800438 page_head->page_hpa = page_hpa;
Avi Kivity36868f72007-03-26 19:31:52 +0200439 list_move(&page_head->link, &vcpu->free_pages);
Avi Kivity260746c2007-01-05 16:36:49 -0800440 ++vcpu->kvm->n_free_mmu_pages;
441}
442
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800443static unsigned kvm_page_table_hashfn(gfn_t gfn)
444{
445 return gfn;
446}
447
Avi Kivity25c0de22007-01-05 16:36:42 -0800448static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
449 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450{
451 struct kvm_mmu_page *page;
452
453 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800454 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455
456 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
Avi Kivity36868f72007-03-26 19:31:52 +0200457 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 ASSERT(is_empty_shadow_page(page->page_hpa));
459 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800460 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800462 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800463 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464}
465
Avi Kivity714b93d2007-01-05 16:36:53 -0800466static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
467 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800468{
469 struct kvm_pte_chain *pte_chain;
470 struct hlist_node *node;
471 int i;
472
473 if (!parent_pte)
474 return;
475 if (!page->multimapped) {
476 u64 *old = page->parent_pte;
477
478 if (!old) {
479 page->parent_pte = parent_pte;
480 return;
481 }
482 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800483 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800484 INIT_HLIST_HEAD(&page->parent_ptes);
485 hlist_add_head(&pte_chain->link, &page->parent_ptes);
486 pte_chain->parent_ptes[0] = old;
487 }
488 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
489 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
490 continue;
491 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
492 if (!pte_chain->parent_ptes[i]) {
493 pte_chain->parent_ptes[i] = parent_pte;
494 return;
495 }
496 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800497 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800498 BUG_ON(!pte_chain);
499 hlist_add_head(&pte_chain->link, &page->parent_ptes);
500 pte_chain->parent_ptes[0] = parent_pte;
501}
502
Avi Kivity714b93d2007-01-05 16:36:53 -0800503static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
504 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800505 u64 *parent_pte)
506{
507 struct kvm_pte_chain *pte_chain;
508 struct hlist_node *node;
509 int i;
510
511 if (!page->multimapped) {
512 BUG_ON(page->parent_pte != parent_pte);
513 page->parent_pte = NULL;
514 return;
515 }
516 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
517 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
518 if (!pte_chain->parent_ptes[i])
519 break;
520 if (pte_chain->parent_ptes[i] != parent_pte)
521 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800522 while (i + 1 < NR_PTE_CHAIN_ENTRIES
523 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800524 pte_chain->parent_ptes[i]
525 = pte_chain->parent_ptes[i + 1];
526 ++i;
527 }
528 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800529 if (i == 0) {
530 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800531 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800532 if (hlist_empty(&page->parent_ptes)) {
533 page->multimapped = 0;
534 page->parent_pte = NULL;
535 }
536 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800537 return;
538 }
539 BUG();
540}
541
542static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
543 gfn_t gfn)
544{
545 unsigned index;
546 struct hlist_head *bucket;
547 struct kvm_mmu_page *page;
548 struct hlist_node *node;
549
550 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
551 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
552 bucket = &vcpu->kvm->mmu_page_hash[index];
553 hlist_for_each_entry(page, node, bucket, hash_link)
554 if (page->gfn == gfn && !page->role.metaphysical) {
555 pgprintk("%s: found role %x\n",
556 __FUNCTION__, page->role.word);
557 return page;
558 }
559 return NULL;
560}
561
562static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
563 gfn_t gfn,
564 gva_t gaddr,
565 unsigned level,
566 int metaphysical,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200567 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800568 u64 *parent_pte)
569{
570 union kvm_mmu_page_role role;
571 unsigned index;
572 unsigned quadrant;
573 struct hlist_head *bucket;
574 struct kvm_mmu_page *page;
575 struct hlist_node *node;
576
577 role.word = 0;
578 role.glevels = vcpu->mmu.root_level;
579 role.level = level;
580 role.metaphysical = metaphysical;
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200581 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800582 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
583 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
584 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
585 role.quadrant = quadrant;
586 }
587 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
588 gfn, role.word);
589 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
590 bucket = &vcpu->kvm->mmu_page_hash[index];
591 hlist_for_each_entry(page, node, bucket, hash_link)
592 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800593 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800594 pgprintk("%s: found\n", __FUNCTION__);
595 return page;
596 }
597 page = kvm_mmu_alloc_page(vcpu, parent_pte);
598 if (!page)
599 return page;
600 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
601 page->gfn = gfn;
602 page->role = role;
603 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800604 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800605 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800606 return page;
607}
608
Avi Kivitya4360362007-01-05 16:36:45 -0800609static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
610 struct kvm_mmu_page *page)
611{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800612 unsigned i;
613 u64 *pt;
614 u64 ent;
615
616 pt = __va(page->page_hpa);
617
618 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
619 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
620 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800621 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800622 pt[i] = 0;
623 }
Avi Kivity40907d52007-01-05 16:36:55 -0800624 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800625 return;
626 }
627
628 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
629 ent = pt[i];
630
631 pt[i] = 0;
632 if (!(ent & PT_PRESENT_MASK))
633 continue;
634 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800635 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800636 }
Avi Kivitya4360362007-01-05 16:36:45 -0800637}
638
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800639static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
640 struct kvm_mmu_page *page,
641 u64 *parent_pte)
642{
Avi Kivity714b93d2007-01-05 16:36:53 -0800643 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800644}
645
646static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
647 struct kvm_mmu_page *page)
648{
649 u64 *parent_pte;
650
651 while (page->multimapped || page->parent_pte) {
652 if (!page->multimapped)
653 parent_pte = page->parent_pte;
654 else {
655 struct kvm_pte_chain *chain;
656
657 chain = container_of(page->parent_ptes.first,
658 struct kvm_pte_chain, link);
659 parent_pte = chain->parent_ptes[0];
660 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800661 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800662 kvm_mmu_put_page(vcpu, page, parent_pte);
663 *parent_pte = 0;
664 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800665 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800666 if (!page->root_count) {
667 hlist_del(&page->hash_link);
668 kvm_mmu_free_page(vcpu, page->page_hpa);
Avi Kivity36868f72007-03-26 19:31:52 +0200669 } else
670 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800671}
672
673static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
674{
675 unsigned index;
676 struct hlist_head *bucket;
677 struct kvm_mmu_page *page;
678 struct hlist_node *node, *n;
679 int r;
680
681 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
682 r = 0;
683 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
684 bucket = &vcpu->kvm->mmu_page_hash[index];
685 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
686 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800687 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
688 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800689 kvm_mmu_zap_page(vcpu, page);
690 r = 1;
691 }
692 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800693}
694
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
696{
697 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
698 struct kvm_mmu_page *page_head = page_header(__pa(pte));
699
700 __set_bit(slot, &page_head->slot_bitmap);
701}
702
703hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
704{
705 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
706
707 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
708}
709
710hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
711{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 struct page *page;
713
714 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc22007-03-30 14:02:32 +0300715 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
716 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
719 | (gpa & (PAGE_SIZE-1));
720}
721
722hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
723{
724 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
725
726 if (gpa == UNMAPPED_GVA)
727 return UNMAPPED_GVA;
728 return gpa_to_hpa(vcpu, gpa);
729}
730
Avi Kivity039576c2007-03-20 12:46:50 +0200731struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
732{
733 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
734
735 if (gpa == UNMAPPED_GVA)
736 return NULL;
737 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
738}
739
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
741{
742}
743
744static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
745{
746 int level = PT32E_ROOT_LEVEL;
747 hpa_t table_addr = vcpu->mmu.root_hpa;
748
749 for (; ; level--) {
750 u32 index = PT64_INDEX(v, level);
751 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800752 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753
754 ASSERT(VALID_PAGE(table_addr));
755 table = __va(table_addr);
756
757 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800758 pte = table[index];
759 if (is_present_pte(pte) && is_writeble_pte(pte))
760 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
762 page_header_update_slot(vcpu->kvm, table, v);
763 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
764 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800765 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 return 0;
767 }
768
769 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800770 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800771 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800773 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
774 >> PAGE_SHIFT;
775 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
776 v, level - 1,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200777 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800778 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 pgprintk("nonpaging_map: ENOMEM\n");
780 return -ENOMEM;
781 }
782
Avi Kivity25c0de22007-01-05 16:36:42 -0800783 table[index] = new_table->page_hpa | PT_PRESENT_MASK
784 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 }
786 table_addr = table[index] & PT64_BASE_ADDR_MASK;
787 }
788}
789
Avi Kivity17ac10a2007-01-05 16:36:40 -0800790static void mmu_free_roots(struct kvm_vcpu *vcpu)
791{
792 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800793 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800794
795#ifdef CONFIG_X86_64
796 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
797 hpa_t root = vcpu->mmu.root_hpa;
798
799 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800800 page = page_header(root);
801 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800802 vcpu->mmu.root_hpa = INVALID_PAGE;
803 return;
804 }
805#endif
806 for (i = 0; i < 4; ++i) {
807 hpa_t root = vcpu->mmu.pae_root[i];
808
Avi Kivity417726a2007-04-12 17:35:58 +0300809 if (root) {
810 ASSERT(VALID_PAGE(root));
811 root &= PT64_BASE_ADDR_MASK;
812 page = page_header(root);
813 --page->root_count;
814 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800815 vcpu->mmu.pae_root[i] = INVALID_PAGE;
816 }
817 vcpu->mmu.root_hpa = INVALID_PAGE;
818}
819
820static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
821{
822 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800823 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800824 struct kvm_mmu_page *page;
825
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800826 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800827
828#ifdef CONFIG_X86_64
829 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
830 hpa_t root = vcpu->mmu.root_hpa;
831
832 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800833 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200834 PT64_ROOT_LEVEL, 0, 0, NULL);
Ingo Molnar68a99f62007-01-05 16:36:59 -0800835 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800836 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800837 vcpu->mmu.root_hpa = root;
838 return;
839 }
840#endif
841 for (i = 0; i < 4; ++i) {
842 hpa_t root = vcpu->mmu.pae_root[i];
843
844 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300845 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
846 if (!is_present_pte(vcpu->pdptrs[i])) {
847 vcpu->mmu.pae_root[i] = 0;
848 continue;
849 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800850 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300851 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800853 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800854 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200855 0, NULL);
Ingo Molnar68a99f62007-01-05 16:36:59 -0800856 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800857 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800858 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
859 }
860 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
861}
862
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
864{
865 return vaddr;
866}
867
868static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
869 u32 error_code)
870{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800872 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800873 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874
Avi Kivitye2dec932007-01-05 16:36:54 -0800875 r = mmu_topup_memory_caches(vcpu);
876 if (r)
877 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800878
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 ASSERT(vcpu);
880 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
881
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882
Avi Kivityebeace82007-01-05 16:36:47 -0800883 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884
Avi Kivityebeace82007-01-05 16:36:47 -0800885 if (is_error_hpa(paddr))
886 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887
Avi Kivityebeace82007-01-05 16:36:47 -0800888 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889}
890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891static void nonpaging_free(struct kvm_vcpu *vcpu)
892{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800893 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894}
895
896static int nonpaging_init_context(struct kvm_vcpu *vcpu)
897{
898 struct kvm_mmu *context = &vcpu->mmu;
899
900 context->new_cr3 = nonpaging_new_cr3;
901 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 context->gva_to_gpa = nonpaging_gva_to_gpa;
903 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800904 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800906 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 ASSERT(VALID_PAGE(context->root_hpa));
908 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
909 return 0;
910}
911
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
913{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 ++kvm_stat.tlb_flush;
915 kvm_arch_ops->tlb_flush(vcpu);
916}
917
918static void paging_new_cr3(struct kvm_vcpu *vcpu)
919{
Avi Kivity374cbac2007-01-05 16:36:43 -0800920 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800921 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800922 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
923 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800926 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927}
928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929static inline void set_pte_common(struct kvm_vcpu *vcpu,
930 u64 *shadow_pte,
931 gpa_t gaddr,
932 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800933 u64 access_bits,
934 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935{
936 hpa_t paddr;
937
938 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
939 if (!dirty)
940 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941
942 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
943
Avi Kivity374cbac2007-01-05 16:36:43 -0800944 *shadow_pte |= access_bits;
945
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 if (is_error_hpa(paddr)) {
947 *shadow_pte |= gaddr;
948 *shadow_pte |= PT_SHADOW_IO_MARK;
949 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800950 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800952
953 *shadow_pte |= paddr;
954
955 if (access_bits & PT_WRITABLE_MASK) {
956 struct kvm_mmu_page *shadow;
957
Avi Kivity815af8d2007-01-05 16:36:44 -0800958 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800959 if (shadow) {
960 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800961 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800962 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity40907d52007-01-05 16:36:55 -0800963 if (is_writeble_pte(*shadow_pte)) {
964 *shadow_pte &= ~PT_WRITABLE_MASK;
965 kvm_arch_ops->tlb_flush(vcpu);
966 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800967 }
968 }
969
970 if (access_bits & PT_WRITABLE_MASK)
971 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
972
973 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -0800974 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975}
976
977static void inject_page_fault(struct kvm_vcpu *vcpu,
978 u64 addr,
979 u32 err_code)
980{
981 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
982}
983
984static inline int fix_read_pf(u64 *shadow_ent)
985{
986 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
987 !(*shadow_ent & PT_USER_MASK)) {
988 /*
989 * If supervisor write protect is disabled, we shadow kernel
990 * pages as user pages so we can trap the write access.
991 */
992 *shadow_ent |= PT_USER_MASK;
993 *shadow_ent &= ~PT_WRITABLE_MASK;
994
995 return 1;
996
997 }
998 return 0;
999}
1000
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001static void paging_free(struct kvm_vcpu *vcpu)
1002{
1003 nonpaging_free(vcpu);
1004}
1005
1006#define PTTYPE 64
1007#include "paging_tmpl.h"
1008#undef PTTYPE
1009
1010#define PTTYPE 32
1011#include "paging_tmpl.h"
1012#undef PTTYPE
1013
Avi Kivity17ac10a2007-01-05 16:36:40 -08001014static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015{
1016 struct kvm_mmu *context = &vcpu->mmu;
1017
1018 ASSERT(is_pae(vcpu));
1019 context->new_cr3 = paging_new_cr3;
1020 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 context->gva_to_gpa = paging64_gva_to_gpa;
1022 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001023 context->root_level = level;
1024 context->shadow_root_level = level;
1025 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 ASSERT(VALID_PAGE(context->root_hpa));
1027 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1028 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1029 return 0;
1030}
1031
Avi Kivity17ac10a2007-01-05 16:36:40 -08001032static int paging64_init_context(struct kvm_vcpu *vcpu)
1033{
1034 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1035}
1036
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037static int paging32_init_context(struct kvm_vcpu *vcpu)
1038{
1039 struct kvm_mmu *context = &vcpu->mmu;
1040
1041 context->new_cr3 = paging_new_cr3;
1042 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 context->gva_to_gpa = paging32_gva_to_gpa;
1044 context->free = paging_free;
1045 context->root_level = PT32_ROOT_LEVEL;
1046 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001047 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 ASSERT(VALID_PAGE(context->root_hpa));
1049 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1050 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1051 return 0;
1052}
1053
1054static int paging32E_init_context(struct kvm_vcpu *vcpu)
1055{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001056 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057}
1058
1059static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1060{
1061 ASSERT(vcpu);
1062 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1063
1064 if (!is_paging(vcpu))
1065 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001066 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 return paging64_init_context(vcpu);
1068 else if (is_pae(vcpu))
1069 return paging32E_init_context(vcpu);
1070 else
1071 return paging32_init_context(vcpu);
1072}
1073
1074static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1075{
1076 ASSERT(vcpu);
1077 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1078 vcpu->mmu.free(vcpu);
1079 vcpu->mmu.root_hpa = INVALID_PAGE;
1080 }
1081}
1082
1083int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1084{
Avi Kivity714b93d2007-01-05 16:36:53 -08001085 int r;
1086
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001088 r = init_kvm_mmu(vcpu);
1089 if (r < 0)
1090 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001091 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001092out:
1093 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094}
1095
Avi Kivityac1b7142007-03-08 17:13:32 +02001096static void mmu_pre_write_zap_pte(struct kvm_vcpu *vcpu,
1097 struct kvm_mmu_page *page,
1098 u64 *spte)
1099{
1100 u64 pte;
1101 struct kvm_mmu_page *child;
1102
1103 pte = *spte;
1104 if (is_present_pte(pte)) {
1105 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1106 rmap_remove(vcpu, spte);
1107 else {
1108 child = page_header(pte & PT64_BASE_ADDR_MASK);
1109 mmu_page_remove_parent_pte(vcpu, child, spte);
1110 }
1111 }
1112 *spte = 0;
1113}
1114
Avi Kivityda4a00f2007-01-05 16:36:44 -08001115void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1116{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001117 gfn_t gfn = gpa >> PAGE_SHIFT;
1118 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001119 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001120 struct hlist_head *bucket;
1121 unsigned index;
1122 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001123 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001124 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001125 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001126 unsigned misaligned;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001127 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001128 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001129 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001130
Avi Kivityda4a00f2007-01-05 16:36:44 -08001131 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001132 if (gfn == vcpu->last_pt_write_gfn) {
1133 ++vcpu->last_pt_write_count;
1134 if (vcpu->last_pt_write_count >= 3)
1135 flooded = 1;
1136 } else {
1137 vcpu->last_pt_write_gfn = gfn;
1138 vcpu->last_pt_write_count = 1;
1139 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001140 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1141 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001142 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001143 if (page->gfn != gfn || page->role.metaphysical)
1144 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001145 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1146 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001147 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001148 /*
1149 * Misaligned accesses are too much trouble to fix
1150 * up; also, they usually indicate a page is not used
1151 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001152 *
1153 * If we're seeing too many writes to a page,
1154 * it may no longer be a page table, or we may be
1155 * forking, in which case it is better to unmap the
1156 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001157 */
1158 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1159 gpa, bytes, page->role.word);
1160 kvm_mmu_zap_page(vcpu, page);
1161 continue;
1162 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001163 page_offset = offset;
1164 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001165 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001166 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001167 page_offset <<= 1; /* 32->64 */
1168 /*
1169 * A 32-bit pde maps 4MB while the shadow pdes map
1170 * only 2MB. So we need to double the offset again
1171 * and zap two pdes instead of one.
1172 */
1173 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001174 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001175 page_offset <<= 1;
1176 npte = 2;
1177 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001178 page_offset &= ~PAGE_MASK;
1179 }
1180 spte = __va(page->page_hpa);
1181 spte += page_offset / sizeof(*spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001182 while (npte--) {
1183 mmu_pre_write_zap_pte(vcpu, page, spte);
1184 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001185 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001186 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001187}
1188
1189void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1190{
1191}
1192
Avi Kivitya4360362007-01-05 16:36:45 -08001193int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1194{
1195 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1196
1197 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1198}
1199
Avi Kivityebeace82007-01-05 16:36:47 -08001200void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1201{
1202 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1203 struct kvm_mmu_page *page;
1204
1205 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1206 struct kvm_mmu_page, link);
1207 kvm_mmu_zap_page(vcpu, page);
1208 }
1209}
1210EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1211
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212static void free_mmu_pages(struct kvm_vcpu *vcpu)
1213{
Avi Kivityf51234c2007-01-05 16:36:52 -08001214 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215
Avi Kivityf51234c2007-01-05 16:36:52 -08001216 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1217 page = container_of(vcpu->kvm->active_mmu_pages.next,
1218 struct kvm_mmu_page, link);
1219 kvm_mmu_zap_page(vcpu, page);
1220 }
1221 while (!list_empty(&vcpu->free_pages)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 page = list_entry(vcpu->free_pages.next,
1223 struct kvm_mmu_page, link);
1224 list_del(&page->link);
1225 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1226 page->page_hpa = INVALID_PAGE;
1227 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001228 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230
1231static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1232{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 int i;
1235
1236 ASSERT(vcpu);
1237
1238 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1240
1241 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001242 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 goto error_1;
Markus Rechberger5972e952007-02-19 14:37:47 +02001244 set_page_private(page, (unsigned long)page_header);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1246 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1247 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001248 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001250
1251 /*
1252 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1253 * Therefore we need to allocate shadow page tables in the first
1254 * 4GB of memory, which happens to fit the DMA32 zone.
1255 */
1256 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1257 if (!page)
1258 goto error_1;
1259 vcpu->mmu.pae_root = page_address(page);
1260 for (i = 0; i < 4; ++i)
1261 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1262
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263 return 0;
1264
1265error_1:
1266 free_mmu_pages(vcpu);
1267 return -ENOMEM;
1268}
1269
Ingo Molnar8018c272006-12-29 16:50:01 -08001270int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 ASSERT(vcpu);
1273 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1274 ASSERT(list_empty(&vcpu->free_pages));
1275
Ingo Molnar8018c272006-12-29 16:50:01 -08001276 return alloc_mmu_pages(vcpu);
1277}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278
Ingo Molnar8018c272006-12-29 16:50:01 -08001279int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1280{
1281 ASSERT(vcpu);
1282 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1283 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001284
Ingo Molnar8018c272006-12-29 16:50:01 -08001285 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286}
1287
1288void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1289{
1290 ASSERT(vcpu);
1291
1292 destroy_kvm_mmu(vcpu);
1293 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001294 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295}
1296
Avi Kivity714b93d2007-01-05 16:36:53 -08001297void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298{
Avi Kivity714b93d2007-01-05 16:36:53 -08001299 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 struct kvm_mmu_page *page;
1301
1302 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1303 int i;
1304 u64 *pt;
1305
1306 if (!test_bit(slot, &page->slot_bitmap))
1307 continue;
1308
1309 pt = __va(page->page_hpa);
1310 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1311 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001312 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001313 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001315 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 }
1317}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001318
Dor Laore0fa8262007-03-30 13:06:33 +03001319void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1320{
1321 destroy_kvm_mmu(vcpu);
1322
1323 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1324 struct kvm_mmu_page *page;
1325
1326 page = container_of(vcpu->kvm->active_mmu_pages.next,
1327 struct kvm_mmu_page, link);
1328 kvm_mmu_zap_page(vcpu, page);
1329 }
1330
1331 mmu_free_memory_caches(vcpu);
1332 kvm_arch_ops->tlb_flush(vcpu);
1333 init_kvm_mmu(vcpu);
1334}
1335
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001336#ifdef AUDIT
1337
1338static const char *audit_msg;
1339
1340static gva_t canonicalize(gva_t gva)
1341{
1342#ifdef CONFIG_X86_64
1343 gva = (long long)(gva << 16) >> 16;
1344#endif
1345 return gva;
1346}
1347
1348static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1349 gva_t va, int level)
1350{
1351 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1352 int i;
1353 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1354
1355 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1356 u64 ent = pt[i];
1357
1358 if (!ent & PT_PRESENT_MASK)
1359 continue;
1360
1361 va = canonicalize(va);
1362 if (level > 1)
1363 audit_mappings_page(vcpu, ent, va, level - 1);
1364 else {
1365 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1366 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1367
1368 if ((ent & PT_PRESENT_MASK)
1369 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1370 printk(KERN_ERR "audit error: (%s) levels %d"
1371 " gva %lx gpa %llx hpa %llx ent %llx\n",
1372 audit_msg, vcpu->mmu.root_level,
1373 va, gpa, hpa, ent);
1374 }
1375 }
1376}
1377
1378static void audit_mappings(struct kvm_vcpu *vcpu)
1379{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001380 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001381
1382 if (vcpu->mmu.root_level == 4)
1383 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1384 else
1385 for (i = 0; i < 4; ++i)
1386 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1387 audit_mappings_page(vcpu,
1388 vcpu->mmu.pae_root[i],
1389 i << 30,
1390 2);
1391}
1392
1393static int count_rmaps(struct kvm_vcpu *vcpu)
1394{
1395 int nmaps = 0;
1396 int i, j, k;
1397
1398 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1399 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1400 struct kvm_rmap_desc *d;
1401
1402 for (j = 0; j < m->npages; ++j) {
1403 struct page *page = m->phys_mem[j];
1404
1405 if (!page->private)
1406 continue;
1407 if (!(page->private & 1)) {
1408 ++nmaps;
1409 continue;
1410 }
1411 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1412 while (d) {
1413 for (k = 0; k < RMAP_EXT; ++k)
1414 if (d->shadow_ptes[k])
1415 ++nmaps;
1416 else
1417 break;
1418 d = d->more;
1419 }
1420 }
1421 }
1422 return nmaps;
1423}
1424
1425static int count_writable_mappings(struct kvm_vcpu *vcpu)
1426{
1427 int nmaps = 0;
1428 struct kvm_mmu_page *page;
1429 int i;
1430
1431 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1432 u64 *pt = __va(page->page_hpa);
1433
1434 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1435 continue;
1436
1437 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1438 u64 ent = pt[i];
1439
1440 if (!(ent & PT_PRESENT_MASK))
1441 continue;
1442 if (!(ent & PT_WRITABLE_MASK))
1443 continue;
1444 ++nmaps;
1445 }
1446 }
1447 return nmaps;
1448}
1449
1450static void audit_rmap(struct kvm_vcpu *vcpu)
1451{
1452 int n_rmap = count_rmaps(vcpu);
1453 int n_actual = count_writable_mappings(vcpu);
1454
1455 if (n_rmap != n_actual)
1456 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1457 __FUNCTION__, audit_msg, n_rmap, n_actual);
1458}
1459
1460static void audit_write_protection(struct kvm_vcpu *vcpu)
1461{
1462 struct kvm_mmu_page *page;
1463
1464 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1465 hfn_t hfn;
1466 struct page *pg;
1467
1468 if (page->role.metaphysical)
1469 continue;
1470
1471 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1472 >> PAGE_SHIFT;
1473 pg = pfn_to_page(hfn);
1474 if (pg->private)
1475 printk(KERN_ERR "%s: (%s) shadow page has writable"
1476 " mappings: gfn %lx role %x\n",
1477 __FUNCTION__, audit_msg, page->gfn,
1478 page->role.word);
1479 }
1480}
1481
1482static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1483{
1484 int olddbg = dbg;
1485
1486 dbg = 0;
1487 audit_msg = msg;
1488 audit_rmap(vcpu);
1489 audit_write_protection(vcpu);
1490 audit_mappings(vcpu);
1491 dbg = olddbg;
1492}
1493
1494#endif