blob: 46491b4cd859158e9ffbc81d2fa8f28889a71254 [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
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080055#ifndef MMU_DEBUG
56#define ASSERT(x) do { } while (0)
57#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080058#define ASSERT(x) \
59 if (!(x)) { \
60 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
61 __FILE__, __LINE__, #x); \
62 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080063#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080064
Avi Kivitycea0f0e2007-01-05 16:36:43 -080065#define PT64_PT_BITS 9
66#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
67#define PT32_PT_BITS 10
68#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069
70#define PT_WRITABLE_SHIFT 1
71
72#define PT_PRESENT_MASK (1ULL << 0)
73#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
74#define PT_USER_MASK (1ULL << 2)
75#define PT_PWT_MASK (1ULL << 3)
76#define PT_PCD_MASK (1ULL << 4)
77#define PT_ACCESSED_MASK (1ULL << 5)
78#define PT_DIRTY_MASK (1ULL << 6)
79#define PT_PAGE_SIZE_MASK (1ULL << 7)
80#define PT_PAT_MASK (1ULL << 7)
81#define PT_GLOBAL_MASK (1ULL << 8)
82#define PT64_NX_MASK (1ULL << 63)
83
84#define PT_PAT_SHIFT 7
85#define PT_DIR_PAT_SHIFT 12
86#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
87
88#define PT32_DIR_PSE36_SIZE 4
89#define PT32_DIR_PSE36_SHIFT 13
90#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
91
92
93#define PT32_PTE_COPY_MASK \
Avi Kivity8c7bb722006-12-13 00:34:02 -080094 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
Avi Kivity8c7bb722006-12-13 00:34:02 -080096#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
98#define PT_FIRST_AVAIL_BITS_SHIFT 9
99#define PT64_SECOND_AVAIL_BITS_SHIFT 52
100
101#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
102#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
103
104#define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
105#define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
106
107#define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
108#define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
109
110#define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
111
112#define VALID_PAGE(x) ((x) != INVALID_PAGE)
113
114#define PT64_LEVEL_BITS 9
115
116#define PT64_LEVEL_SHIFT(level) \
117 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
118
119#define PT64_LEVEL_MASK(level) \
120 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
121
122#define PT64_INDEX(address, level)\
123 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
124
125
126#define PT32_LEVEL_BITS 10
127
128#define PT32_LEVEL_SHIFT(level) \
129 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
130
131#define PT32_LEVEL_MASK(level) \
132 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
133
134#define PT32_INDEX(address, level)\
135 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
136
137
Avi Kivity27aba762007-03-09 13:04:31 +0200138#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139#define PT64_DIR_BASE_ADDR_MASK \
140 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
141
142#define PT32_BASE_ADDR_MASK PAGE_MASK
143#define PT32_DIR_BASE_ADDR_MASK \
144 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
145
146
147#define PFERR_PRESENT_MASK (1U << 0)
148#define PFERR_WRITE_MASK (1U << 1)
149#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800150#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151
152#define PT64_ROOT_LEVEL 4
153#define PT32_ROOT_LEVEL 2
154#define PT32E_ROOT_LEVEL 3
155
156#define PT_DIRECTORY_LEVEL 2
157#define PT_PAGE_TABLE_LEVEL 1
158
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800159#define RMAP_EXT 4
160
161struct kvm_rmap_desc {
162 u64 *shadow_ptes[RMAP_EXT];
163 struct kvm_rmap_desc *more;
164};
165
Avi Kivityb5a33a72007-04-15 16:31:09 +0300166static struct kmem_cache *pte_chain_cache;
167static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300168static struct kmem_cache *mmu_page_cache;
169static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300170
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171static int is_write_protection(struct kvm_vcpu *vcpu)
172{
173 return vcpu->cr0 & CR0_WP_MASK;
174}
175
176static int is_cpuid_PSE36(void)
177{
178 return 1;
179}
180
Avi Kivity73b10872007-01-26 00:56:41 -0800181static int is_nx(struct kvm_vcpu *vcpu)
182{
183 return vcpu->shadow_efer & EFER_NX;
184}
185
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186static int is_present_pte(unsigned long pte)
187{
188 return pte & PT_PRESENT_MASK;
189}
190
191static int is_writeble_pte(unsigned long pte)
192{
193 return pte & PT_WRITABLE_MASK;
194}
195
196static int is_io_pte(unsigned long pte)
197{
198 return pte & PT_SHADOW_IO_MARK;
199}
200
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800201static int is_rmap_pte(u64 pte)
202{
203 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
204 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
205}
206
Avi Kivitye2dec932007-01-05 16:36:54 -0800207static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300208 struct kmem_cache *base_cache, int min,
209 gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800210{
211 void *obj;
212
213 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800214 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800215 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity8c438502007-04-16 11:53:17 +0300216 obj = kmem_cache_zalloc(base_cache, gfp_flags);
Avi Kivity714b93d2007-01-05 16:36:53 -0800217 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800218 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800219 cache->objects[cache->nobjs++] = obj;
220 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800221 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800222}
223
224static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
225{
226 while (mc->nobjs)
227 kfree(mc->objects[--mc->nobjs]);
228}
229
Avi Kivity8c438502007-04-16 11:53:17 +0300230static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800231{
Avi Kivitye2dec932007-01-05 16:36:54 -0800232 int r;
233
234 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300235 pte_chain_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800236 if (r)
237 goto out;
238 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300239 rmap_desc_cache, 1, gfp_flags);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300240 if (r)
241 goto out;
242 r = mmu_topup_memory_cache(&vcpu->mmu_page_cache,
243 mmu_page_cache, 4, gfp_flags);
244 if (r)
245 goto out;
246 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
247 mmu_page_header_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800248out:
249 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800250}
251
Avi Kivity8c438502007-04-16 11:53:17 +0300252static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
253{
254 int r;
255
256 r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
257 if (r < 0) {
258 spin_unlock(&vcpu->kvm->lock);
259 kvm_arch_ops->vcpu_put(vcpu);
260 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
261 kvm_arch_ops->vcpu_load(vcpu);
262 spin_lock(&vcpu->kvm->lock);
263 }
264 return r;
265}
266
Avi Kivity714b93d2007-01-05 16:36:53 -0800267static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
268{
269 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
270 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300271 mmu_free_memory_cache(&vcpu->mmu_page_cache);
272 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800273}
274
275static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
276 size_t size)
277{
278 void *p;
279
280 BUG_ON(!mc->nobjs);
281 p = mc->objects[--mc->nobjs];
282 memset(p, 0, size);
283 return p;
284}
285
286static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
287{
288 if (mc->nobjs < KVM_NR_MEM_OBJS)
289 mc->objects[mc->nobjs++] = obj;
290 else
291 kfree(obj);
292}
293
294static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
295{
296 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
297 sizeof(struct kvm_pte_chain));
298}
299
300static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
301 struct kvm_pte_chain *pc)
302{
303 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
304}
305
306static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
307{
308 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
309 sizeof(struct kvm_rmap_desc));
310}
311
312static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
313 struct kvm_rmap_desc *rd)
314{
315 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
316}
317
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800318/*
319 * Reverse mapping data structures:
320 *
321 * If page->private bit zero is zero, then page->private points to the
322 * shadow page table entry that points to page_address(page).
323 *
324 * If page->private bit zero is one, (then page->private & ~1) points
325 * to a struct kvm_rmap_desc containing more mappings.
326 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800327static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800328{
329 struct page *page;
330 struct kvm_rmap_desc *desc;
331 int i;
332
333 if (!is_rmap_pte(*spte))
334 return;
335 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200336 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800337 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200338 set_page_private(page,(unsigned long)spte);
339 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800340 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800341 desc = mmu_alloc_rmap_desc(vcpu);
Markus Rechberger5972e952007-02-19 14:37:47 +0200342 desc->shadow_ptes[0] = (u64 *)page_private(page);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800343 desc->shadow_ptes[1] = spte;
Markus Rechberger5972e952007-02-19 14:37:47 +0200344 set_page_private(page,(unsigned long)desc | 1);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800345 } else {
346 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200347 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800348 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
349 desc = desc->more;
350 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800351 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800352 desc = desc->more;
353 }
354 for (i = 0; desc->shadow_ptes[i]; ++i)
355 ;
356 desc->shadow_ptes[i] = spte;
357 }
358}
359
Avi Kivity714b93d2007-01-05 16:36:53 -0800360static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
361 struct page *page,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800362 struct kvm_rmap_desc *desc,
363 int i,
364 struct kvm_rmap_desc *prev_desc)
365{
366 int j;
367
368 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
369 ;
370 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000371 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 if (j != 0)
373 return;
374 if (!prev_desc && !desc->more)
Markus Rechberger5972e952007-02-19 14:37:47 +0200375 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800376 else
377 if (prev_desc)
378 prev_desc->more = desc->more;
379 else
Markus Rechberger5972e952007-02-19 14:37:47 +0200380 set_page_private(page,(unsigned long)desc->more | 1);
Avi Kivity714b93d2007-01-05 16:36:53 -0800381 mmu_free_rmap_desc(vcpu, desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800382}
383
Avi Kivity714b93d2007-01-05 16:36:53 -0800384static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800385{
386 struct page *page;
387 struct kvm_rmap_desc *desc;
388 struct kvm_rmap_desc *prev_desc;
389 int i;
390
391 if (!is_rmap_pte(*spte))
392 return;
393 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200394 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800395 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
396 BUG();
Markus Rechberger5972e952007-02-19 14:37:47 +0200397 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800398 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200399 if ((u64 *)page_private(page) != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800400 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
401 spte, *spte);
402 BUG();
403 }
Markus Rechberger5972e952007-02-19 14:37:47 +0200404 set_page_private(page,0);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405 } else {
406 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200407 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800408 prev_desc = NULL;
409 while (desc) {
410 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
411 if (desc->shadow_ptes[i] == spte) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800412 rmap_desc_remove_entry(vcpu, page,
413 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800414 prev_desc);
415 return;
416 }
417 prev_desc = desc;
418 desc = desc->more;
419 }
420 BUG();
421 }
422}
423
Avi Kivity714b93d2007-01-05 16:36:53 -0800424static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800425{
Avi Kivity714b93d2007-01-05 16:36:53 -0800426 struct kvm *kvm = vcpu->kvm;
Avi Kivity374cbac2007-01-05 16:36:43 -0800427 struct page *page;
Avi Kivity374cbac2007-01-05 16:36:43 -0800428 struct kvm_rmap_desc *desc;
429 u64 *spte;
430
Avi Kivity954bbbc2007-03-30 14:02:32 +0300431 page = gfn_to_page(kvm, gfn);
432 BUG_ON(!page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800433
Markus Rechberger5972e952007-02-19 14:37:47 +0200434 while (page_private(page)) {
435 if (!(page_private(page) & 1))
436 spte = (u64 *)page_private(page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800437 else {
Markus Rechberger5972e952007-02-19 14:37:47 +0200438 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800439 spte = desc->shadow_ptes[0];
440 }
441 BUG_ON(!spte);
Avi Kivity27aba762007-03-09 13:04:31 +0200442 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
443 != page_to_pfn(page));
Avi Kivity374cbac2007-01-05 16:36:43 -0800444 BUG_ON(!(*spte & PT_PRESENT_MASK));
445 BUG_ON(!(*spte & PT_WRITABLE_MASK));
446 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800447 rmap_remove(vcpu, spte);
Avi Kivity40907d52007-01-05 16:36:55 -0800448 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity374cbac2007-01-05 16:36:43 -0800449 *spte &= ~(u64)PT_WRITABLE_MASK;
450 }
451}
452
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800453#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300454static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455{
Avi Kivity139bdb22007-01-05 16:36:50 -0800456 u64 *pos;
457 u64 *end;
458
Avi Kivity47ad8e62007-05-06 15:50:58 +0300459 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800460 if (*pos != 0) {
461 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
462 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800464 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465 return 1;
466}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800467#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300469static void kvm_mmu_free_page(struct kvm_vcpu *vcpu,
470 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800471{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300472 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300473 list_del(&page_head->link);
474 mmu_memory_cache_free(&vcpu->mmu_page_cache, page_head->spt);
475 mmu_memory_cache_free(&vcpu->mmu_page_header_cache, page_head);
Avi Kivity260746c2007-01-05 16:36:49 -0800476 ++vcpu->kvm->n_free_mmu_pages;
477}
478
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800479static unsigned kvm_page_table_hashfn(gfn_t gfn)
480{
481 return gfn;
482}
483
Avi Kivity25c0de22007-01-05 16:36:42 -0800484static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
485 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486{
487 struct kvm_mmu_page *page;
488
Avi Kivityd3d25b02007-05-30 12:34:53 +0300489 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800490 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491
Avi Kivityd3d25b02007-05-30 12:34:53 +0300492 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
493 sizeof *page);
494 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
495 set_page_private(virt_to_page(page->spt), (unsigned long)page);
496 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300497 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800499 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800501 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800502 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503}
504
Avi Kivity714b93d2007-01-05 16:36:53 -0800505static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
506 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800507{
508 struct kvm_pte_chain *pte_chain;
509 struct hlist_node *node;
510 int i;
511
512 if (!parent_pte)
513 return;
514 if (!page->multimapped) {
515 u64 *old = page->parent_pte;
516
517 if (!old) {
518 page->parent_pte = parent_pte;
519 return;
520 }
521 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800522 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800523 INIT_HLIST_HEAD(&page->parent_ptes);
524 hlist_add_head(&pte_chain->link, &page->parent_ptes);
525 pte_chain->parent_ptes[0] = old;
526 }
527 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
528 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
529 continue;
530 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
531 if (!pte_chain->parent_ptes[i]) {
532 pte_chain->parent_ptes[i] = parent_pte;
533 return;
534 }
535 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800536 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800537 BUG_ON(!pte_chain);
538 hlist_add_head(&pte_chain->link, &page->parent_ptes);
539 pte_chain->parent_ptes[0] = parent_pte;
540}
541
Avi Kivity714b93d2007-01-05 16:36:53 -0800542static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
543 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800544 u64 *parent_pte)
545{
546 struct kvm_pte_chain *pte_chain;
547 struct hlist_node *node;
548 int i;
549
550 if (!page->multimapped) {
551 BUG_ON(page->parent_pte != parent_pte);
552 page->parent_pte = NULL;
553 return;
554 }
555 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
556 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
557 if (!pte_chain->parent_ptes[i])
558 break;
559 if (pte_chain->parent_ptes[i] != parent_pte)
560 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800561 while (i + 1 < NR_PTE_CHAIN_ENTRIES
562 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800563 pte_chain->parent_ptes[i]
564 = pte_chain->parent_ptes[i + 1];
565 ++i;
566 }
567 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800568 if (i == 0) {
569 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800570 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800571 if (hlist_empty(&page->parent_ptes)) {
572 page->multimapped = 0;
573 page->parent_pte = NULL;
574 }
575 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800576 return;
577 }
578 BUG();
579}
580
581static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
582 gfn_t gfn)
583{
584 unsigned index;
585 struct hlist_head *bucket;
586 struct kvm_mmu_page *page;
587 struct hlist_node *node;
588
589 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
590 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
591 bucket = &vcpu->kvm->mmu_page_hash[index];
592 hlist_for_each_entry(page, node, bucket, hash_link)
593 if (page->gfn == gfn && !page->role.metaphysical) {
594 pgprintk("%s: found role %x\n",
595 __FUNCTION__, page->role.word);
596 return page;
597 }
598 return NULL;
599}
600
601static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
602 gfn_t gfn,
603 gva_t gaddr,
604 unsigned level,
605 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200606 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800607 u64 *parent_pte)
608{
609 union kvm_mmu_page_role role;
610 unsigned index;
611 unsigned quadrant;
612 struct hlist_head *bucket;
613 struct kvm_mmu_page *page;
614 struct hlist_node *node;
615
616 role.word = 0;
617 role.glevels = vcpu->mmu.root_level;
618 role.level = level;
619 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200620 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800621 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
622 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
623 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
624 role.quadrant = quadrant;
625 }
626 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
627 gfn, role.word);
628 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
629 bucket = &vcpu->kvm->mmu_page_hash[index];
630 hlist_for_each_entry(page, node, bucket, hash_link)
631 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800632 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800633 pgprintk("%s: found\n", __FUNCTION__);
634 return page;
635 }
636 page = kvm_mmu_alloc_page(vcpu, parent_pte);
637 if (!page)
638 return page;
639 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
640 page->gfn = gfn;
641 page->role = role;
642 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800643 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800644 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800645 return page;
646}
647
Avi Kivitya4360362007-01-05 16:36:45 -0800648static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
649 struct kvm_mmu_page *page)
650{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800651 unsigned i;
652 u64 *pt;
653 u64 ent;
654
Avi Kivity47ad8e62007-05-06 15:50:58 +0300655 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800656
657 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
658 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
659 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800660 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800661 pt[i] = 0;
662 }
Avi Kivity40907d52007-01-05 16:36:55 -0800663 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800664 return;
665 }
666
667 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
668 ent = pt[i];
669
670 pt[i] = 0;
671 if (!(ent & PT_PRESENT_MASK))
672 continue;
673 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800674 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800675 }
Avi Kivitya4360362007-01-05 16:36:45 -0800676}
677
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800678static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
679 struct kvm_mmu_page *page,
680 u64 *parent_pte)
681{
Avi Kivity714b93d2007-01-05 16:36:53 -0800682 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800683}
684
685static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
686 struct kvm_mmu_page *page)
687{
688 u64 *parent_pte;
689
690 while (page->multimapped || page->parent_pte) {
691 if (!page->multimapped)
692 parent_pte = page->parent_pte;
693 else {
694 struct kvm_pte_chain *chain;
695
696 chain = container_of(page->parent_ptes.first,
697 struct kvm_pte_chain, link);
698 parent_pte = chain->parent_ptes[0];
699 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800700 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800701 kvm_mmu_put_page(vcpu, page, parent_pte);
702 *parent_pte = 0;
703 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800704 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800705 if (!page->root_count) {
706 hlist_del(&page->hash_link);
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300707 kvm_mmu_free_page(vcpu, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200708 } else
709 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800710}
711
712static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
713{
714 unsigned index;
715 struct hlist_head *bucket;
716 struct kvm_mmu_page *page;
717 struct hlist_node *node, *n;
718 int r;
719
720 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
721 r = 0;
722 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
723 bucket = &vcpu->kvm->mmu_page_hash[index];
724 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
725 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800726 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
727 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800728 kvm_mmu_zap_page(vcpu, page);
729 r = 1;
730 }
731 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800732}
733
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
735{
736 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
737 struct kvm_mmu_page *page_head = page_header(__pa(pte));
738
739 __set_bit(slot, &page_head->slot_bitmap);
740}
741
742hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
743{
744 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
745
746 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
747}
748
749hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
750{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 struct page *page;
752
753 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300754 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
755 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
758 | (gpa & (PAGE_SIZE-1));
759}
760
761hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
762{
763 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
764
765 if (gpa == UNMAPPED_GVA)
766 return UNMAPPED_GVA;
767 return gpa_to_hpa(vcpu, gpa);
768}
769
Avi Kivity039576c2007-03-20 12:46:50 +0200770struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
771{
772 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
773
774 if (gpa == UNMAPPED_GVA)
775 return NULL;
776 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
777}
778
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
780{
781}
782
783static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
784{
785 int level = PT32E_ROOT_LEVEL;
786 hpa_t table_addr = vcpu->mmu.root_hpa;
787
788 for (; ; level--) {
789 u32 index = PT64_INDEX(v, level);
790 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800791 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792
793 ASSERT(VALID_PAGE(table_addr));
794 table = __va(table_addr);
795
796 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800797 pte = table[index];
798 if (is_present_pte(pte) && is_writeble_pte(pte))
799 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
801 page_header_update_slot(vcpu->kvm, table, v);
802 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
803 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800804 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 return 0;
806 }
807
808 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800809 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800810 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800812 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
813 >> PAGE_SHIFT;
814 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
815 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200816 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800817 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 pgprintk("nonpaging_map: ENOMEM\n");
819 return -ENOMEM;
820 }
821
Avi Kivity47ad8e62007-05-06 15:50:58 +0300822 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800823 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 }
825 table_addr = table[index] & PT64_BASE_ADDR_MASK;
826 }
827}
828
Avi Kivity17ac10a2007-01-05 16:36:40 -0800829static void mmu_free_roots(struct kvm_vcpu *vcpu)
830{
831 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800832 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800833
834#ifdef CONFIG_X86_64
835 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
836 hpa_t root = vcpu->mmu.root_hpa;
837
838 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800839 page = page_header(root);
840 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800841 vcpu->mmu.root_hpa = INVALID_PAGE;
842 return;
843 }
844#endif
845 for (i = 0; i < 4; ++i) {
846 hpa_t root = vcpu->mmu.pae_root[i];
847
Avi Kivity417726a2007-04-12 17:35:58 +0300848 if (root) {
849 ASSERT(VALID_PAGE(root));
850 root &= PT64_BASE_ADDR_MASK;
851 page = page_header(root);
852 --page->root_count;
853 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800854 vcpu->mmu.pae_root[i] = INVALID_PAGE;
855 }
856 vcpu->mmu.root_hpa = INVALID_PAGE;
857}
858
859static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
860{
861 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800862 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800863 struct kvm_mmu_page *page;
864
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800865 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800866
867#ifdef CONFIG_X86_64
868 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
869 hpa_t root = vcpu->mmu.root_hpa;
870
871 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800872 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200873 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300874 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800875 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800876 vcpu->mmu.root_hpa = root;
877 return;
878 }
879#endif
880 for (i = 0; i < 4; ++i) {
881 hpa_t root = vcpu->mmu.pae_root[i];
882
883 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300884 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
885 if (!is_present_pte(vcpu->pdptrs[i])) {
886 vcpu->mmu.pae_root[i] = 0;
887 continue;
888 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800889 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300890 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800891 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800892 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800893 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200894 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300895 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800896 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800897 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
898 }
899 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
900}
901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
903{
904 return vaddr;
905}
906
907static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
908 u32 error_code)
909{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800911 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800912 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Avi Kivitye2dec932007-01-05 16:36:54 -0800914 r = mmu_topup_memory_caches(vcpu);
915 if (r)
916 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800917
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 ASSERT(vcpu);
919 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
920
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921
Avi Kivityebeace82007-01-05 16:36:47 -0800922 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Avi Kivityebeace82007-01-05 16:36:47 -0800924 if (is_error_hpa(paddr))
925 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926
Avi Kivityebeace82007-01-05 16:36:47 -0800927 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928}
929
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930static void nonpaging_free(struct kvm_vcpu *vcpu)
931{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800932 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933}
934
935static int nonpaging_init_context(struct kvm_vcpu *vcpu)
936{
937 struct kvm_mmu *context = &vcpu->mmu;
938
939 context->new_cr3 = nonpaging_new_cr3;
940 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 context->gva_to_gpa = nonpaging_gva_to_gpa;
942 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800943 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800945 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 ASSERT(VALID_PAGE(context->root_hpa));
947 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
948 return 0;
949}
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
952{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300953 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 kvm_arch_ops->tlb_flush(vcpu);
955}
956
957static void paging_new_cr3(struct kvm_vcpu *vcpu)
958{
Avi Kivity374cbac2007-01-05 16:36:43 -0800959 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800960 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800961 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
962 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800963 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800965 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966}
967
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968static inline void set_pte_common(struct kvm_vcpu *vcpu,
969 u64 *shadow_pte,
970 gpa_t gaddr,
971 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800972 u64 access_bits,
973 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974{
975 hpa_t paddr;
976
977 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
978 if (!dirty)
979 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980
981 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
982
Avi Kivity374cbac2007-01-05 16:36:43 -0800983 *shadow_pte |= access_bits;
984
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 if (is_error_hpa(paddr)) {
986 *shadow_pte |= gaddr;
987 *shadow_pte |= PT_SHADOW_IO_MARK;
988 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800989 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800991
992 *shadow_pte |= paddr;
993
994 if (access_bits & PT_WRITABLE_MASK) {
995 struct kvm_mmu_page *shadow;
996
Avi Kivity815af8d2007-01-05 16:36:44 -0800997 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800998 if (shadow) {
999 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -08001000 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -08001001 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity40907d52007-01-05 16:36:55 -08001002 if (is_writeble_pte(*shadow_pte)) {
1003 *shadow_pte &= ~PT_WRITABLE_MASK;
1004 kvm_arch_ops->tlb_flush(vcpu);
1005 }
Avi Kivity374cbac2007-01-05 16:36:43 -08001006 }
1007 }
1008
1009 if (access_bits & PT_WRITABLE_MASK)
1010 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
1011
1012 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -08001013 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014}
1015
1016static void inject_page_fault(struct kvm_vcpu *vcpu,
1017 u64 addr,
1018 u32 err_code)
1019{
1020 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
1021}
1022
1023static inline int fix_read_pf(u64 *shadow_ent)
1024{
1025 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
1026 !(*shadow_ent & PT_USER_MASK)) {
1027 /*
1028 * If supervisor write protect is disabled, we shadow kernel
1029 * pages as user pages so we can trap the write access.
1030 */
1031 *shadow_ent |= PT_USER_MASK;
1032 *shadow_ent &= ~PT_WRITABLE_MASK;
1033
1034 return 1;
1035
1036 }
1037 return 0;
1038}
1039
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040static void paging_free(struct kvm_vcpu *vcpu)
1041{
1042 nonpaging_free(vcpu);
1043}
1044
1045#define PTTYPE 64
1046#include "paging_tmpl.h"
1047#undef PTTYPE
1048
1049#define PTTYPE 32
1050#include "paging_tmpl.h"
1051#undef PTTYPE
1052
Avi Kivity17ac10a2007-01-05 16:36:40 -08001053static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054{
1055 struct kvm_mmu *context = &vcpu->mmu;
1056
1057 ASSERT(is_pae(vcpu));
1058 context->new_cr3 = paging_new_cr3;
1059 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 context->gva_to_gpa = paging64_gva_to_gpa;
1061 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001062 context->root_level = level;
1063 context->shadow_root_level = level;
1064 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 ASSERT(VALID_PAGE(context->root_hpa));
1066 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1067 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1068 return 0;
1069}
1070
Avi Kivity17ac10a2007-01-05 16:36:40 -08001071static int paging64_init_context(struct kvm_vcpu *vcpu)
1072{
1073 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1074}
1075
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076static int paging32_init_context(struct kvm_vcpu *vcpu)
1077{
1078 struct kvm_mmu *context = &vcpu->mmu;
1079
1080 context->new_cr3 = paging_new_cr3;
1081 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 context->gva_to_gpa = paging32_gva_to_gpa;
1083 context->free = paging_free;
1084 context->root_level = PT32_ROOT_LEVEL;
1085 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001086 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 ASSERT(VALID_PAGE(context->root_hpa));
1088 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1089 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1090 return 0;
1091}
1092
1093static int paging32E_init_context(struct kvm_vcpu *vcpu)
1094{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001095 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096}
1097
1098static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1099{
1100 ASSERT(vcpu);
1101 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1102
Avi Kivityd3d25b02007-05-30 12:34:53 +03001103 mmu_topup_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 if (!is_paging(vcpu))
1105 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001106 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 return paging64_init_context(vcpu);
1108 else if (is_pae(vcpu))
1109 return paging32E_init_context(vcpu);
1110 else
1111 return paging32_init_context(vcpu);
1112}
1113
1114static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1115{
1116 ASSERT(vcpu);
1117 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1118 vcpu->mmu.free(vcpu);
1119 vcpu->mmu.root_hpa = INVALID_PAGE;
1120 }
1121}
1122
1123int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1124{
Avi Kivity714b93d2007-01-05 16:36:53 -08001125 int r;
1126
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001128 r = init_kvm_mmu(vcpu);
1129 if (r < 0)
1130 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001131 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001132out:
1133 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134}
1135
Avi Kivity09072da2007-05-01 14:16:52 +03001136static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001137 struct kvm_mmu_page *page,
1138 u64 *spte)
1139{
1140 u64 pte;
1141 struct kvm_mmu_page *child;
1142
1143 pte = *spte;
1144 if (is_present_pte(pte)) {
1145 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1146 rmap_remove(vcpu, spte);
1147 else {
1148 child = page_header(pte & PT64_BASE_ADDR_MASK);
1149 mmu_page_remove_parent_pte(vcpu, child, spte);
1150 }
1151 }
1152 *spte = 0;
1153}
1154
Avi Kivity00284252007-05-01 16:53:31 +03001155static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1156 struct kvm_mmu_page *page,
1157 u64 *spte,
1158 const void *new, int bytes)
1159{
1160 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1161 return;
1162
1163 if (page->role.glevels == PT32_ROOT_LEVEL)
1164 paging32_update_pte(vcpu, page, spte, new, bytes);
1165 else
1166 paging64_update_pte(vcpu, page, spte, new, bytes);
1167}
1168
Avi Kivity09072da2007-05-01 14:16:52 +03001169void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1170 const u8 *old, const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001171{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001172 gfn_t gfn = gpa >> PAGE_SHIFT;
1173 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001174 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001175 struct hlist_head *bucket;
1176 unsigned index;
1177 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001178 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001179 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001180 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001181 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001182 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001183 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001184 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001185 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001186
Avi Kivityda4a00f2007-01-05 16:36:44 -08001187 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001188 if (gfn == vcpu->last_pt_write_gfn) {
1189 ++vcpu->last_pt_write_count;
1190 if (vcpu->last_pt_write_count >= 3)
1191 flooded = 1;
1192 } else {
1193 vcpu->last_pt_write_gfn = gfn;
1194 vcpu->last_pt_write_count = 1;
1195 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001196 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1197 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001198 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001199 if (page->gfn != gfn || page->role.metaphysical)
1200 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001201 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1202 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001203 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001204 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001205 /*
1206 * Misaligned accesses are too much trouble to fix
1207 * up; also, they usually indicate a page is not used
1208 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001209 *
1210 * If we're seeing too many writes to a page,
1211 * it may no longer be a page table, or we may be
1212 * forking, in which case it is better to unmap the
1213 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001214 */
1215 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1216 gpa, bytes, page->role.word);
1217 kvm_mmu_zap_page(vcpu, page);
1218 continue;
1219 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001220 page_offset = offset;
1221 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001222 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001223 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001224 page_offset <<= 1; /* 32->64 */
1225 /*
1226 * A 32-bit pde maps 4MB while the shadow pdes map
1227 * only 2MB. So we need to double the offset again
1228 * and zap two pdes instead of one.
1229 */
1230 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001231 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001232 page_offset <<= 1;
1233 npte = 2;
1234 }
Avi Kivityfce06572007-05-01 16:44:05 +03001235 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001236 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001237 if (quadrant != page->role.quadrant)
1238 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001239 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001240 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001241 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001242 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivity00284252007-05-01 16:53:31 +03001243 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
Avi Kivityac1b7142007-03-08 17:13:32 +02001244 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001245 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001246 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001247}
1248
Avi Kivitya4360362007-01-05 16:36:45 -08001249int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1250{
1251 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1252
1253 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1254}
1255
Avi Kivityebeace82007-01-05 16:36:47 -08001256void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1257{
1258 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1259 struct kvm_mmu_page *page;
1260
1261 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1262 struct kvm_mmu_page, link);
1263 kvm_mmu_zap_page(vcpu, page);
1264 }
1265}
1266EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1267
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268static void free_mmu_pages(struct kvm_vcpu *vcpu)
1269{
Avi Kivityf51234c2007-01-05 16:36:52 -08001270 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271
Avi Kivityf51234c2007-01-05 16:36:52 -08001272 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1273 page = container_of(vcpu->kvm->active_mmu_pages.next,
1274 struct kvm_mmu_page, link);
1275 kvm_mmu_zap_page(vcpu, page);
1276 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001277 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
1280static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1281{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001282 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 int i;
1284
1285 ASSERT(vcpu);
1286
Avi Kivityd3d25b02007-05-30 12:34:53 +03001287 vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001288
1289 /*
1290 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1291 * Therefore we need to allocate shadow page tables in the first
1292 * 4GB of memory, which happens to fit the DMA32 zone.
1293 */
1294 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1295 if (!page)
1296 goto error_1;
1297 vcpu->mmu.pae_root = page_address(page);
1298 for (i = 0; i < 4; ++i)
1299 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1300
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 return 0;
1302
1303error_1:
1304 free_mmu_pages(vcpu);
1305 return -ENOMEM;
1306}
1307
Ingo Molnar8018c272006-12-29 16:50:01 -08001308int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 ASSERT(vcpu);
1311 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312
Ingo Molnar8018c272006-12-29 16:50:01 -08001313 return alloc_mmu_pages(vcpu);
1314}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315
Ingo Molnar8018c272006-12-29 16:50:01 -08001316int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1317{
1318 ASSERT(vcpu);
1319 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001320
Ingo Molnar8018c272006-12-29 16:50:01 -08001321 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322}
1323
1324void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1325{
1326 ASSERT(vcpu);
1327
1328 destroy_kvm_mmu(vcpu);
1329 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001330 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
Avi Kivity714b93d2007-01-05 16:36:53 -08001333void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334{
Avi Kivity714b93d2007-01-05 16:36:53 -08001335 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 struct kvm_mmu_page *page;
1337
1338 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1339 int i;
1340 u64 *pt;
1341
1342 if (!test_bit(slot, &page->slot_bitmap))
1343 continue;
1344
Avi Kivity47ad8e62007-05-06 15:50:58 +03001345 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1347 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001348 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001349 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001351 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 }
1353}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001354
Dor Laore0fa8262007-03-30 13:06:33 +03001355void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1356{
1357 destroy_kvm_mmu(vcpu);
1358
1359 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1360 struct kvm_mmu_page *page;
1361
1362 page = container_of(vcpu->kvm->active_mmu_pages.next,
1363 struct kvm_mmu_page, link);
1364 kvm_mmu_zap_page(vcpu, page);
1365 }
1366
1367 mmu_free_memory_caches(vcpu);
1368 kvm_arch_ops->tlb_flush(vcpu);
1369 init_kvm_mmu(vcpu);
1370}
1371
Avi Kivityb5a33a72007-04-15 16:31:09 +03001372void kvm_mmu_module_exit(void)
1373{
1374 if (pte_chain_cache)
1375 kmem_cache_destroy(pte_chain_cache);
1376 if (rmap_desc_cache)
1377 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001378 if (mmu_page_cache)
1379 kmem_cache_destroy(mmu_page_cache);
1380 if (mmu_page_header_cache)
1381 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001382}
1383
1384int kvm_mmu_module_init(void)
1385{
1386 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1387 sizeof(struct kvm_pte_chain),
1388 0, 0, NULL, NULL);
1389 if (!pte_chain_cache)
1390 goto nomem;
1391 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1392 sizeof(struct kvm_rmap_desc),
1393 0, 0, NULL, NULL);
1394 if (!rmap_desc_cache)
1395 goto nomem;
1396
Avi Kivityd3d25b02007-05-30 12:34:53 +03001397 mmu_page_cache = kmem_cache_create("kvm_mmu_page",
1398 PAGE_SIZE,
1399 PAGE_SIZE, 0, NULL, NULL);
1400 if (!mmu_page_cache)
1401 goto nomem;
1402
1403 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1404 sizeof(struct kvm_mmu_page),
1405 0, 0, NULL, NULL);
1406 if (!mmu_page_header_cache)
1407 goto nomem;
1408
Avi Kivityb5a33a72007-04-15 16:31:09 +03001409 return 0;
1410
1411nomem:
1412 kvm_mmu_module_exit();
1413 return -ENOMEM;
1414}
1415
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001416#ifdef AUDIT
1417
1418static const char *audit_msg;
1419
1420static gva_t canonicalize(gva_t gva)
1421{
1422#ifdef CONFIG_X86_64
1423 gva = (long long)(gva << 16) >> 16;
1424#endif
1425 return gva;
1426}
1427
1428static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1429 gva_t va, int level)
1430{
1431 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1432 int i;
1433 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1434
1435 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1436 u64 ent = pt[i];
1437
Adrian Bunk28076962007-04-28 21:20:48 +02001438 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001439 continue;
1440
1441 va = canonicalize(va);
1442 if (level > 1)
1443 audit_mappings_page(vcpu, ent, va, level - 1);
1444 else {
1445 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1446 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1447
1448 if ((ent & PT_PRESENT_MASK)
1449 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1450 printk(KERN_ERR "audit error: (%s) levels %d"
1451 " gva %lx gpa %llx hpa %llx ent %llx\n",
1452 audit_msg, vcpu->mmu.root_level,
1453 va, gpa, hpa, ent);
1454 }
1455 }
1456}
1457
1458static void audit_mappings(struct kvm_vcpu *vcpu)
1459{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001460 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001461
1462 if (vcpu->mmu.root_level == 4)
1463 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1464 else
1465 for (i = 0; i < 4; ++i)
1466 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1467 audit_mappings_page(vcpu,
1468 vcpu->mmu.pae_root[i],
1469 i << 30,
1470 2);
1471}
1472
1473static int count_rmaps(struct kvm_vcpu *vcpu)
1474{
1475 int nmaps = 0;
1476 int i, j, k;
1477
1478 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1479 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1480 struct kvm_rmap_desc *d;
1481
1482 for (j = 0; j < m->npages; ++j) {
1483 struct page *page = m->phys_mem[j];
1484
1485 if (!page->private)
1486 continue;
1487 if (!(page->private & 1)) {
1488 ++nmaps;
1489 continue;
1490 }
1491 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1492 while (d) {
1493 for (k = 0; k < RMAP_EXT; ++k)
1494 if (d->shadow_ptes[k])
1495 ++nmaps;
1496 else
1497 break;
1498 d = d->more;
1499 }
1500 }
1501 }
1502 return nmaps;
1503}
1504
1505static int count_writable_mappings(struct kvm_vcpu *vcpu)
1506{
1507 int nmaps = 0;
1508 struct kvm_mmu_page *page;
1509 int i;
1510
1511 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001512 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001513
1514 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1515 continue;
1516
1517 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1518 u64 ent = pt[i];
1519
1520 if (!(ent & PT_PRESENT_MASK))
1521 continue;
1522 if (!(ent & PT_WRITABLE_MASK))
1523 continue;
1524 ++nmaps;
1525 }
1526 }
1527 return nmaps;
1528}
1529
1530static void audit_rmap(struct kvm_vcpu *vcpu)
1531{
1532 int n_rmap = count_rmaps(vcpu);
1533 int n_actual = count_writable_mappings(vcpu);
1534
1535 if (n_rmap != n_actual)
1536 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1537 __FUNCTION__, audit_msg, n_rmap, n_actual);
1538}
1539
1540static void audit_write_protection(struct kvm_vcpu *vcpu)
1541{
1542 struct kvm_mmu_page *page;
1543
1544 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1545 hfn_t hfn;
1546 struct page *pg;
1547
1548 if (page->role.metaphysical)
1549 continue;
1550
1551 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1552 >> PAGE_SHIFT;
1553 pg = pfn_to_page(hfn);
1554 if (pg->private)
1555 printk(KERN_ERR "%s: (%s) shadow page has writable"
1556 " mappings: gfn %lx role %x\n",
1557 __FUNCTION__, audit_msg, page->gfn,
1558 page->role.word);
1559 }
1560}
1561
1562static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1563{
1564 int olddbg = dbg;
1565
1566 dbg = 0;
1567 audit_msg = msg;
1568 audit_rmap(vcpu);
1569 audit_write_protection(vcpu);
1570 audit_mappings(vcpu);
1571 dbg = olddbg;
1572}
1573
1574#endif