blob: b297a6b111ac077074f3519e4dcbb8e6332ed8f8 [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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
21#include "kvm.h"
22
Avi Kivitye4956062007-06-28 14:15:57 -040023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <linux/module.h>
28
29#include <asm/page.h>
30#include <asm/cmpxchg.h>
31
Avi Kivity37a7d8b2007-01-05 16:36:56 -080032#undef MMU_DEBUG
33
34#undef AUDIT
35
36#ifdef AUDIT
37static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
38#else
39static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
40#endif
41
42#ifdef MMU_DEBUG
43
44#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
45#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
46
47#else
48
49#define pgprintk(x...) do { } while (0)
50#define rmap_printk(x...) do { } while (0)
51
52#endif
53
54#if defined(MMU_DEBUG) || defined(AUDIT)
55static int dbg = 1;
56#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080058#ifndef MMU_DEBUG
59#define ASSERT(x) do { } while (0)
60#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080061#define ASSERT(x) \
62 if (!(x)) { \
63 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
64 __FILE__, __LINE__, #x); \
65 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080066#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
Avi Kivitycea0f0e2007-01-05 16:36:43 -080068#define PT64_PT_BITS 9
69#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
70#define PT32_PT_BITS 10
71#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
73#define PT_WRITABLE_SHIFT 1
74
75#define PT_PRESENT_MASK (1ULL << 0)
76#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
77#define PT_USER_MASK (1ULL << 2)
78#define PT_PWT_MASK (1ULL << 3)
79#define PT_PCD_MASK (1ULL << 4)
80#define PT_ACCESSED_MASK (1ULL << 5)
81#define PT_DIRTY_MASK (1ULL << 6)
82#define PT_PAGE_SIZE_MASK (1ULL << 7)
83#define PT_PAT_MASK (1ULL << 7)
84#define PT_GLOBAL_MASK (1ULL << 8)
85#define PT64_NX_MASK (1ULL << 63)
86
87#define PT_PAT_SHIFT 7
88#define PT_DIR_PAT_SHIFT 12
89#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
90
91#define PT32_DIR_PSE36_SIZE 4
92#define PT32_DIR_PSE36_SHIFT 13
93#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
94
95
Avi Kivity6aa8b732006-12-10 02:21:36 -080096#define PT_FIRST_AVAIL_BITS_SHIFT 9
97#define PT64_SECOND_AVAIL_BITS_SHIFT 52
98
Avi Kivity6aa8b732006-12-10 02:21:36 -080099#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define VALID_PAGE(x) ((x) != INVALID_PAGE)
102
103#define PT64_LEVEL_BITS 9
104
105#define PT64_LEVEL_SHIFT(level) \
106 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
107
108#define PT64_LEVEL_MASK(level) \
109 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
110
111#define PT64_INDEX(address, level)\
112 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
113
114
115#define PT32_LEVEL_BITS 10
116
117#define PT32_LEVEL_SHIFT(level) \
118 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
119
120#define PT32_LEVEL_MASK(level) \
121 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
122
123#define PT32_INDEX(address, level)\
124 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
125
126
Avi Kivity27aba762007-03-09 13:04:31 +0200127#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128#define PT64_DIR_BASE_ADDR_MASK \
129 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
130
131#define PT32_BASE_ADDR_MASK PAGE_MASK
132#define PT32_DIR_BASE_ADDR_MASK \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
134
135
136#define PFERR_PRESENT_MASK (1U << 0)
137#define PFERR_WRITE_MASK (1U << 1)
138#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800139#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PT64_ROOT_LEVEL 4
142#define PT32_ROOT_LEVEL 2
143#define PT32E_ROOT_LEVEL 3
144
145#define PT_DIRECTORY_LEVEL 2
146#define PT_PAGE_TABLE_LEVEL 1
147
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800148#define RMAP_EXT 4
149
150struct kvm_rmap_desc {
151 u64 *shadow_ptes[RMAP_EXT];
152 struct kvm_rmap_desc *more;
153};
154
Avi Kivityb5a33a72007-04-15 16:31:09 +0300155static struct kmem_cache *pte_chain_cache;
156static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300157static struct kmem_cache *mmu_page_cache;
158static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300159
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160static int is_write_protection(struct kvm_vcpu *vcpu)
161{
162 return vcpu->cr0 & CR0_WP_MASK;
163}
164
165static int is_cpuid_PSE36(void)
166{
167 return 1;
168}
169
Avi Kivity73b10872007-01-26 00:56:41 -0800170static int is_nx(struct kvm_vcpu *vcpu)
171{
172 return vcpu->shadow_efer & EFER_NX;
173}
174
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175static int is_present_pte(unsigned long pte)
176{
177 return pte & PT_PRESENT_MASK;
178}
179
180static int is_writeble_pte(unsigned long pte)
181{
182 return pte & PT_WRITABLE_MASK;
183}
184
185static int is_io_pte(unsigned long pte)
186{
187 return pte & PT_SHADOW_IO_MARK;
188}
189
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800190static int is_rmap_pte(u64 pte)
191{
192 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
193 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
194}
195
Avi Kivitye663ee62007-05-31 15:46:04 +0300196static void set_shadow_pte(u64 *sptep, u64 spte)
197{
198#ifdef CONFIG_X86_64
199 set_64bit((unsigned long *)sptep, spte);
200#else
201 set_64bit((unsigned long long *)sptep, spte);
202#endif
203}
204
Avi Kivitye2dec932007-01-05 16:36:54 -0800205static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300206 struct kmem_cache *base_cache, int min,
207 gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800208{
209 void *obj;
210
211 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800212 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800213 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity8c438502007-04-16 11:53:17 +0300214 obj = kmem_cache_zalloc(base_cache, gfp_flags);
Avi Kivity714b93d2007-01-05 16:36:53 -0800215 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800216 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800217 cache->objects[cache->nobjs++] = obj;
218 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800219 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800220}
221
222static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
223{
224 while (mc->nobjs)
225 kfree(mc->objects[--mc->nobjs]);
226}
227
Avi Kivity8c438502007-04-16 11:53:17 +0300228static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800229{
Avi Kivitye2dec932007-01-05 16:36:54 -0800230 int r;
231
232 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300233 pte_chain_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800234 if (r)
235 goto out;
236 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300237 rmap_desc_cache, 1, gfp_flags);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300238 if (r)
239 goto out;
240 r = mmu_topup_memory_cache(&vcpu->mmu_page_cache,
241 mmu_page_cache, 4, gfp_flags);
242 if (r)
243 goto out;
244 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
245 mmu_page_header_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800246out:
247 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800248}
249
Avi Kivity8c438502007-04-16 11:53:17 +0300250static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
251{
252 int r;
253
254 r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
255 if (r < 0) {
256 spin_unlock(&vcpu->kvm->lock);
257 kvm_arch_ops->vcpu_put(vcpu);
258 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
259 kvm_arch_ops->vcpu_load(vcpu);
260 spin_lock(&vcpu->kvm->lock);
261 }
262 return r;
263}
264
Avi Kivity714b93d2007-01-05 16:36:53 -0800265static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
266{
267 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
268 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300269 mmu_free_memory_cache(&vcpu->mmu_page_cache);
270 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800271}
272
273static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
274 size_t size)
275{
276 void *p;
277
278 BUG_ON(!mc->nobjs);
279 p = mc->objects[--mc->nobjs];
280 memset(p, 0, size);
281 return p;
282}
283
284static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
285{
286 if (mc->nobjs < KVM_NR_MEM_OBJS)
287 mc->objects[mc->nobjs++] = obj;
288 else
289 kfree(obj);
290}
291
292static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
293{
294 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
295 sizeof(struct kvm_pte_chain));
296}
297
298static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
299 struct kvm_pte_chain *pc)
300{
301 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
302}
303
304static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
305{
306 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
307 sizeof(struct kvm_rmap_desc));
308}
309
310static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
311 struct kvm_rmap_desc *rd)
312{
313 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
314}
315
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800316/*
317 * Reverse mapping data structures:
318 *
319 * If page->private bit zero is zero, then page->private points to the
320 * shadow page table entry that points to page_address(page).
321 *
322 * If page->private bit zero is one, (then page->private & ~1) points
323 * to a struct kvm_rmap_desc containing more mappings.
324 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800325static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800326{
327 struct page *page;
328 struct kvm_rmap_desc *desc;
329 int i;
330
331 if (!is_rmap_pte(*spte))
332 return;
333 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200334 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800335 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200336 set_page_private(page,(unsigned long)spte);
337 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800338 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800339 desc = mmu_alloc_rmap_desc(vcpu);
Markus Rechberger5972e952007-02-19 14:37:47 +0200340 desc->shadow_ptes[0] = (u64 *)page_private(page);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800341 desc->shadow_ptes[1] = spte;
Markus Rechberger5972e952007-02-19 14:37:47 +0200342 set_page_private(page,(unsigned long)desc | 1);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800343 } else {
344 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200345 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800346 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
347 desc = desc->more;
348 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800349 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350 desc = desc->more;
351 }
352 for (i = 0; desc->shadow_ptes[i]; ++i)
353 ;
354 desc->shadow_ptes[i] = spte;
355 }
356}
357
Avi Kivity714b93d2007-01-05 16:36:53 -0800358static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
359 struct page *page,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800360 struct kvm_rmap_desc *desc,
361 int i,
362 struct kvm_rmap_desc *prev_desc)
363{
364 int j;
365
366 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
367 ;
368 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000369 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370 if (j != 0)
371 return;
372 if (!prev_desc && !desc->more)
Markus Rechberger5972e952007-02-19 14:37:47 +0200373 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800374 else
375 if (prev_desc)
376 prev_desc->more = desc->more;
377 else
Markus Rechberger5972e952007-02-19 14:37:47 +0200378 set_page_private(page,(unsigned long)desc->more | 1);
Avi Kivity714b93d2007-01-05 16:36:53 -0800379 mmu_free_rmap_desc(vcpu, desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800380}
381
Avi Kivity714b93d2007-01-05 16:36:53 -0800382static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800383{
384 struct page *page;
385 struct kvm_rmap_desc *desc;
386 struct kvm_rmap_desc *prev_desc;
387 int i;
388
389 if (!is_rmap_pte(*spte))
390 return;
391 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200392 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800393 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
394 BUG();
Markus Rechberger5972e952007-02-19 14:37:47 +0200395 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800396 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200397 if ((u64 *)page_private(page) != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800398 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
399 spte, *spte);
400 BUG();
401 }
Markus Rechberger5972e952007-02-19 14:37:47 +0200402 set_page_private(page,0);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800403 } else {
404 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200405 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800406 prev_desc = NULL;
407 while (desc) {
408 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
409 if (desc->shadow_ptes[i] == spte) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800410 rmap_desc_remove_entry(vcpu, page,
411 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800412 prev_desc);
413 return;
414 }
415 prev_desc = desc;
416 desc = desc->more;
417 }
418 BUG();
419 }
420}
421
Avi Kivity714b93d2007-01-05 16:36:53 -0800422static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800423{
Avi Kivity714b93d2007-01-05 16:36:53 -0800424 struct kvm *kvm = vcpu->kvm;
Avi Kivity374cbac2007-01-05 16:36:43 -0800425 struct page *page;
Avi Kivity374cbac2007-01-05 16:36:43 -0800426 struct kvm_rmap_desc *desc;
427 u64 *spte;
428
Avi Kivity954bbbc2007-03-30 14:02:32 +0300429 page = gfn_to_page(kvm, gfn);
430 BUG_ON(!page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800431
Markus Rechberger5972e952007-02-19 14:37:47 +0200432 while (page_private(page)) {
433 if (!(page_private(page) & 1))
434 spte = (u64 *)page_private(page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800435 else {
Markus Rechberger5972e952007-02-19 14:37:47 +0200436 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800437 spte = desc->shadow_ptes[0];
438 }
439 BUG_ON(!spte);
Avi Kivity27aba762007-03-09 13:04:31 +0200440 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
441 != page_to_pfn(page));
Avi Kivity374cbac2007-01-05 16:36:43 -0800442 BUG_ON(!(*spte & PT_PRESENT_MASK));
443 BUG_ON(!(*spte & PT_WRITABLE_MASK));
444 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800445 rmap_remove(vcpu, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300446 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Shaohua Li88a97f02007-06-20 17:13:26 +0800447 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800448 }
449}
450
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800451#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300452static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453{
Avi Kivity139bdb22007-01-05 16:36:50 -0800454 u64 *pos;
455 u64 *end;
456
Avi Kivity47ad8e62007-05-06 15:50:58 +0300457 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800458 if (*pos != 0) {
459 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
460 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800462 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463 return 1;
464}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800465#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300467static void kvm_mmu_free_page(struct kvm_vcpu *vcpu,
468 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800469{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300470 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300471 list_del(&page_head->link);
472 mmu_memory_cache_free(&vcpu->mmu_page_cache, page_head->spt);
473 mmu_memory_cache_free(&vcpu->mmu_page_header_cache, page_head);
Avi Kivity260746c2007-01-05 16:36:49 -0800474 ++vcpu->kvm->n_free_mmu_pages;
475}
476
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800477static unsigned kvm_page_table_hashfn(gfn_t gfn)
478{
479 return gfn;
480}
481
Avi Kivity25c0de22007-01-05 16:36:42 -0800482static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
483 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484{
485 struct kvm_mmu_page *page;
486
Avi Kivityd3d25b02007-05-30 12:34:53 +0300487 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800488 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489
Avi Kivityd3d25b02007-05-30 12:34:53 +0300490 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
491 sizeof *page);
492 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
493 set_page_private(virt_to_page(page->spt), (unsigned long)page);
494 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300495 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800497 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800499 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800500 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501}
502
Avi Kivity714b93d2007-01-05 16:36:53 -0800503static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
504 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800505{
506 struct kvm_pte_chain *pte_chain;
507 struct hlist_node *node;
508 int i;
509
510 if (!parent_pte)
511 return;
512 if (!page->multimapped) {
513 u64 *old = page->parent_pte;
514
515 if (!old) {
516 page->parent_pte = parent_pte;
517 return;
518 }
519 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800520 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800521 INIT_HLIST_HEAD(&page->parent_ptes);
522 hlist_add_head(&pte_chain->link, &page->parent_ptes);
523 pte_chain->parent_ptes[0] = old;
524 }
525 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
526 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
527 continue;
528 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
529 if (!pte_chain->parent_ptes[i]) {
530 pte_chain->parent_ptes[i] = parent_pte;
531 return;
532 }
533 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800534 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800535 BUG_ON(!pte_chain);
536 hlist_add_head(&pte_chain->link, &page->parent_ptes);
537 pte_chain->parent_ptes[0] = parent_pte;
538}
539
Avi Kivity714b93d2007-01-05 16:36:53 -0800540static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
541 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800542 u64 *parent_pte)
543{
544 struct kvm_pte_chain *pte_chain;
545 struct hlist_node *node;
546 int i;
547
548 if (!page->multimapped) {
549 BUG_ON(page->parent_pte != parent_pte);
550 page->parent_pte = NULL;
551 return;
552 }
553 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
554 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
555 if (!pte_chain->parent_ptes[i])
556 break;
557 if (pte_chain->parent_ptes[i] != parent_pte)
558 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800559 while (i + 1 < NR_PTE_CHAIN_ENTRIES
560 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800561 pte_chain->parent_ptes[i]
562 = pte_chain->parent_ptes[i + 1];
563 ++i;
564 }
565 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800566 if (i == 0) {
567 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800568 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800569 if (hlist_empty(&page->parent_ptes)) {
570 page->multimapped = 0;
571 page->parent_pte = NULL;
572 }
573 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800574 return;
575 }
576 BUG();
577}
578
579static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
580 gfn_t gfn)
581{
582 unsigned index;
583 struct hlist_head *bucket;
584 struct kvm_mmu_page *page;
585 struct hlist_node *node;
586
587 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
588 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
589 bucket = &vcpu->kvm->mmu_page_hash[index];
590 hlist_for_each_entry(page, node, bucket, hash_link)
591 if (page->gfn == gfn && !page->role.metaphysical) {
592 pgprintk("%s: found role %x\n",
593 __FUNCTION__, page->role.word);
594 return page;
595 }
596 return NULL;
597}
598
599static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
600 gfn_t gfn,
601 gva_t gaddr,
602 unsigned level,
603 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200604 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800605 u64 *parent_pte)
606{
607 union kvm_mmu_page_role role;
608 unsigned index;
609 unsigned quadrant;
610 struct hlist_head *bucket;
611 struct kvm_mmu_page *page;
612 struct hlist_node *node;
613
614 role.word = 0;
615 role.glevels = vcpu->mmu.root_level;
616 role.level = level;
617 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200618 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800619 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
620 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
621 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
622 role.quadrant = quadrant;
623 }
624 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
625 gfn, role.word);
626 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
627 bucket = &vcpu->kvm->mmu_page_hash[index];
628 hlist_for_each_entry(page, node, bucket, hash_link)
629 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800630 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800631 pgprintk("%s: found\n", __FUNCTION__);
632 return page;
633 }
634 page = kvm_mmu_alloc_page(vcpu, parent_pte);
635 if (!page)
636 return page;
637 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
638 page->gfn = gfn;
639 page->role = role;
640 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800641 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800642 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800643 return page;
644}
645
Avi Kivitya4360362007-01-05 16:36:45 -0800646static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
647 struct kvm_mmu_page *page)
648{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800649 unsigned i;
650 u64 *pt;
651 u64 ent;
652
Avi Kivity47ad8e62007-05-06 15:50:58 +0300653 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800654
655 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
656 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
657 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800658 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800659 pt[i] = 0;
660 }
Avi Kivityd9e368d2007-06-07 19:18:30 +0300661 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800662 return;
663 }
664
665 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
666 ent = pt[i];
667
668 pt[i] = 0;
669 if (!(ent & PT_PRESENT_MASK))
670 continue;
671 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800672 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800673 }
Avi Kivityd9e368d2007-06-07 19:18:30 +0300674 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800675}
676
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800677static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
678 struct kvm_mmu_page *page,
679 u64 *parent_pte)
680{
Avi Kivity714b93d2007-01-05 16:36:53 -0800681 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800682}
683
684static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
685 struct kvm_mmu_page *page)
686{
687 u64 *parent_pte;
688
689 while (page->multimapped || page->parent_pte) {
690 if (!page->multimapped)
691 parent_pte = page->parent_pte;
692 else {
693 struct kvm_pte_chain *chain;
694
695 chain = container_of(page->parent_ptes.first,
696 struct kvm_pte_chain, link);
697 parent_pte = chain->parent_ptes[0];
698 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800699 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800700 kvm_mmu_put_page(vcpu, page, parent_pte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300701 set_shadow_pte(parent_pte, 0);
Avi Kivitya4360362007-01-05 16:36:45 -0800702 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800703 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800704 if (!page->root_count) {
705 hlist_del(&page->hash_link);
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300706 kvm_mmu_free_page(vcpu, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200707 } else
708 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800709}
710
711static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
712{
713 unsigned index;
714 struct hlist_head *bucket;
715 struct kvm_mmu_page *page;
716 struct hlist_node *node, *n;
717 int r;
718
719 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
720 r = 0;
721 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
722 bucket = &vcpu->kvm->mmu_page_hash[index];
723 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
724 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800725 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
726 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800727 kvm_mmu_zap_page(vcpu, page);
728 r = 1;
729 }
730 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800731}
732
Avi Kivity97a0a012007-05-31 15:08:29 +0300733static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn)
734{
735 struct kvm_mmu_page *page;
736
737 while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) {
738 pgprintk("%s: zap %lx %x\n",
739 __FUNCTION__, gfn, page->role.word);
740 kvm_mmu_zap_page(vcpu, page);
741 }
742}
743
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
745{
746 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
747 struct kvm_mmu_page *page_head = page_header(__pa(pte));
748
749 __set_bit(slot, &page_head->slot_bitmap);
750}
751
752hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
753{
754 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
755
756 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
757}
758
759hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
760{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761 struct page *page;
762
763 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300764 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
765 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
768 | (gpa & (PAGE_SIZE-1));
769}
770
771hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
772{
773 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
774
775 if (gpa == UNMAPPED_GVA)
776 return UNMAPPED_GVA;
777 return gpa_to_hpa(vcpu, gpa);
778}
779
Avi Kivity039576c2007-03-20 12:46:50 +0200780struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
781{
782 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
783
784 if (gpa == UNMAPPED_GVA)
785 return NULL;
786 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
787}
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
790{
791}
792
793static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
794{
795 int level = PT32E_ROOT_LEVEL;
796 hpa_t table_addr = vcpu->mmu.root_hpa;
797
798 for (; ; level--) {
799 u32 index = PT64_INDEX(v, level);
800 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800801 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802
803 ASSERT(VALID_PAGE(table_addr));
804 table = __va(table_addr);
805
806 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800807 pte = table[index];
808 if (is_present_pte(pte) && is_writeble_pte(pte))
809 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
811 page_header_update_slot(vcpu->kvm, table, v);
812 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
813 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800814 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 return 0;
816 }
817
818 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800819 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800820 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
823 >> PAGE_SHIFT;
824 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
825 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200826 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800827 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 pgprintk("nonpaging_map: ENOMEM\n");
829 return -ENOMEM;
830 }
831
Avi Kivity47ad8e62007-05-06 15:50:58 +0300832 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800833 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 }
835 table_addr = table[index] & PT64_BASE_ADDR_MASK;
836 }
837}
838
Avi Kivity17ac10a2007-01-05 16:36:40 -0800839static void mmu_free_roots(struct kvm_vcpu *vcpu)
840{
841 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800842 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800843
Avi Kivity7b53aa52007-06-05 12:17:03 +0300844 if (!VALID_PAGE(vcpu->mmu.root_hpa))
845 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800846#ifdef CONFIG_X86_64
847 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
848 hpa_t root = vcpu->mmu.root_hpa;
849
Avi Kivity3bb65a22007-01-05 16:36:51 -0800850 page = page_header(root);
851 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800852 vcpu->mmu.root_hpa = INVALID_PAGE;
853 return;
854 }
855#endif
856 for (i = 0; i < 4; ++i) {
857 hpa_t root = vcpu->mmu.pae_root[i];
858
Avi Kivity417726a2007-04-12 17:35:58 +0300859 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300860 root &= PT64_BASE_ADDR_MASK;
861 page = page_header(root);
862 --page->root_count;
863 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800864 vcpu->mmu.pae_root[i] = INVALID_PAGE;
865 }
866 vcpu->mmu.root_hpa = INVALID_PAGE;
867}
868
869static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
870{
871 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800872 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800873 struct kvm_mmu_page *page;
874
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800876
877#ifdef CONFIG_X86_64
878 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
879 hpa_t root = vcpu->mmu.root_hpa;
880
881 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800882 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200883 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300884 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800885 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800886 vcpu->mmu.root_hpa = root;
887 return;
888 }
889#endif
890 for (i = 0; i < 4; ++i) {
891 hpa_t root = vcpu->mmu.pae_root[i];
892
893 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300894 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
895 if (!is_present_pte(vcpu->pdptrs[i])) {
896 vcpu->mmu.pae_root[i] = 0;
897 continue;
898 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800899 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300900 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800901 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800902 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800903 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200904 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300905 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800906 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800907 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
908 }
909 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
910}
911
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
913{
914 return vaddr;
915}
916
917static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
918 u32 error_code)
919{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800921 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800922 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Avi Kivitye2dec932007-01-05 16:36:54 -0800924 r = mmu_topup_memory_caches(vcpu);
925 if (r)
926 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800927
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 ASSERT(vcpu);
929 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
930
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931
Avi Kivityebeace82007-01-05 16:36:47 -0800932 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933
Avi Kivityebeace82007-01-05 16:36:47 -0800934 if (is_error_hpa(paddr))
935 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Avi Kivityebeace82007-01-05 16:36:47 -0800937 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938}
939
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940static void nonpaging_free(struct kvm_vcpu *vcpu)
941{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800942 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943}
944
945static int nonpaging_init_context(struct kvm_vcpu *vcpu)
946{
947 struct kvm_mmu *context = &vcpu->mmu;
948
949 context->new_cr3 = nonpaging_new_cr3;
950 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 context->gva_to_gpa = nonpaging_gva_to_gpa;
952 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800953 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +0300955 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 return 0;
957}
958
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
960{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300961 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 kvm_arch_ops->tlb_flush(vcpu);
963}
964
965static void paging_new_cr3(struct kvm_vcpu *vcpu)
966{
Avi Kivity374cbac2007-01-05 16:36:43 -0800967 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800968 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969}
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971static void inject_page_fault(struct kvm_vcpu *vcpu,
972 u64 addr,
973 u32 err_code)
974{
975 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
976}
977
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978static void paging_free(struct kvm_vcpu *vcpu)
979{
980 nonpaging_free(vcpu);
981}
982
983#define PTTYPE 64
984#include "paging_tmpl.h"
985#undef PTTYPE
986
987#define PTTYPE 32
988#include "paging_tmpl.h"
989#undef PTTYPE
990
Avi Kivity17ac10a2007-01-05 16:36:40 -0800991static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992{
993 struct kvm_mmu *context = &vcpu->mmu;
994
995 ASSERT(is_pae(vcpu));
996 context->new_cr3 = paging_new_cr3;
997 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 context->gva_to_gpa = paging64_gva_to_gpa;
999 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001000 context->root_level = level;
1001 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001002 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 return 0;
1004}
1005
Avi Kivity17ac10a2007-01-05 16:36:40 -08001006static int paging64_init_context(struct kvm_vcpu *vcpu)
1007{
1008 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1009}
1010
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011static int paging32_init_context(struct kvm_vcpu *vcpu)
1012{
1013 struct kvm_mmu *context = &vcpu->mmu;
1014
1015 context->new_cr3 = paging_new_cr3;
1016 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 context->gva_to_gpa = paging32_gva_to_gpa;
1018 context->free = paging_free;
1019 context->root_level = PT32_ROOT_LEVEL;
1020 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001021 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 return 0;
1023}
1024
1025static int paging32E_init_context(struct kvm_vcpu *vcpu)
1026{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001027 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028}
1029
1030static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1031{
1032 ASSERT(vcpu);
1033 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1034
1035 if (!is_paging(vcpu))
1036 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001037 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 return paging64_init_context(vcpu);
1039 else if (is_pae(vcpu))
1040 return paging32E_init_context(vcpu);
1041 else
1042 return paging32_init_context(vcpu);
1043}
1044
1045static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1046{
1047 ASSERT(vcpu);
1048 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1049 vcpu->mmu.free(vcpu);
1050 vcpu->mmu.root_hpa = INVALID_PAGE;
1051 }
1052}
1053
1054int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1055{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001056 destroy_kvm_mmu(vcpu);
1057 return init_kvm_mmu(vcpu);
1058}
1059
1060int kvm_mmu_load(struct kvm_vcpu *vcpu)
1061{
Avi Kivity714b93d2007-01-05 16:36:53 -08001062 int r;
1063
Avi Kivity17c3ba92007-06-04 15:58:30 +03001064 spin_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001065 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001066 if (r)
1067 goto out;
1068 mmu_alloc_roots(vcpu);
1069 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
1070 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001071out:
Avi Kivity17c3ba92007-06-04 15:58:30 +03001072 spin_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001073 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001075EXPORT_SYMBOL_GPL(kvm_mmu_load);
1076
1077void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1078{
1079 mmu_free_roots(vcpu);
1080}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081
Avi Kivity09072da2007-05-01 14:16:52 +03001082static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001083 struct kvm_mmu_page *page,
1084 u64 *spte)
1085{
1086 u64 pte;
1087 struct kvm_mmu_page *child;
1088
1089 pte = *spte;
1090 if (is_present_pte(pte)) {
1091 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1092 rmap_remove(vcpu, spte);
1093 else {
1094 child = page_header(pte & PT64_BASE_ADDR_MASK);
1095 mmu_page_remove_parent_pte(vcpu, child, spte);
1096 }
1097 }
1098 *spte = 0;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001099 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001100}
1101
Avi Kivity00284252007-05-01 16:53:31 +03001102static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1103 struct kvm_mmu_page *page,
1104 u64 *spte,
1105 const void *new, int bytes)
1106{
1107 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1108 return;
1109
1110 if (page->role.glevels == PT32_ROOT_LEVEL)
1111 paging32_update_pte(vcpu, page, spte, new, bytes);
1112 else
1113 paging64_update_pte(vcpu, page, spte, new, bytes);
1114}
1115
Avi Kivity09072da2007-05-01 14:16:52 +03001116void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1117 const u8 *old, const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001118{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001119 gfn_t gfn = gpa >> PAGE_SHIFT;
1120 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001121 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001122 struct hlist_head *bucket;
1123 unsigned index;
1124 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001125 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001126 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001127 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001128 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001129 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001130 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001131 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001132 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001133
Avi Kivityda4a00f2007-01-05 16:36:44 -08001134 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001135 if (gfn == vcpu->last_pt_write_gfn) {
1136 ++vcpu->last_pt_write_count;
1137 if (vcpu->last_pt_write_count >= 3)
1138 flooded = 1;
1139 } else {
1140 vcpu->last_pt_write_gfn = gfn;
1141 vcpu->last_pt_write_count = 1;
1142 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001143 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1144 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001145 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001146 if (page->gfn != gfn || page->role.metaphysical)
1147 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001148 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1149 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001150 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001151 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001152 /*
1153 * Misaligned accesses are too much trouble to fix
1154 * up; also, they usually indicate a page is not used
1155 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001156 *
1157 * If we're seeing too many writes to a page,
1158 * it may no longer be a page table, or we may be
1159 * forking, in which case it is better to unmap the
1160 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001161 */
1162 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1163 gpa, bytes, page->role.word);
1164 kvm_mmu_zap_page(vcpu, page);
1165 continue;
1166 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001167 page_offset = offset;
1168 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001169 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001170 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001171 page_offset <<= 1; /* 32->64 */
1172 /*
1173 * A 32-bit pde maps 4MB while the shadow pdes map
1174 * only 2MB. So we need to double the offset again
1175 * and zap two pdes instead of one.
1176 */
1177 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001178 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001179 page_offset <<= 1;
1180 npte = 2;
1181 }
Avi Kivityfce06572007-05-01 16:44:05 +03001182 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001183 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001184 if (quadrant != page->role.quadrant)
1185 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001186 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001187 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001188 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001189 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivity00284252007-05-01 16:53:31 +03001190 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
Avi Kivityac1b7142007-03-08 17:13:32 +02001191 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001192 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001193 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001194}
1195
Avi Kivitya4360362007-01-05 16:36:45 -08001196int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1197{
1198 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1199
1200 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1201}
1202
Avi Kivityebeace82007-01-05 16:36:47 -08001203void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1204{
1205 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1206 struct kvm_mmu_page *page;
1207
1208 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1209 struct kvm_mmu_page, link);
1210 kvm_mmu_zap_page(vcpu, page);
1211 }
1212}
1213EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1214
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215static void free_mmu_pages(struct kvm_vcpu *vcpu)
1216{
Avi Kivityf51234c2007-01-05 16:36:52 -08001217 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218
Avi Kivityf51234c2007-01-05 16:36:52 -08001219 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1220 page = container_of(vcpu->kvm->active_mmu_pages.next,
1221 struct kvm_mmu_page, link);
1222 kvm_mmu_zap_page(vcpu, page);
1223 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001224 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225}
1226
1227static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1228{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001229 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 int i;
1231
1232 ASSERT(vcpu);
1233
Avi Kivityd3d25b02007-05-30 12:34:53 +03001234 vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001235
1236 /*
1237 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1238 * Therefore we need to allocate shadow page tables in the first
1239 * 4GB of memory, which happens to fit the DMA32 zone.
1240 */
1241 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1242 if (!page)
1243 goto error_1;
1244 vcpu->mmu.pae_root = page_address(page);
1245 for (i = 0; i < 4; ++i)
1246 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1247
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 return 0;
1249
1250error_1:
1251 free_mmu_pages(vcpu);
1252 return -ENOMEM;
1253}
1254
Ingo Molnar8018c272006-12-29 16:50:01 -08001255int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 ASSERT(vcpu);
1258 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259
Ingo Molnar8018c272006-12-29 16:50:01 -08001260 return alloc_mmu_pages(vcpu);
1261}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
Ingo Molnar8018c272006-12-29 16:50:01 -08001263int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1264{
1265 ASSERT(vcpu);
1266 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001267
Ingo Molnar8018c272006-12-29 16:50:01 -08001268 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269}
1270
1271void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1272{
1273 ASSERT(vcpu);
1274
1275 destroy_kvm_mmu(vcpu);
1276 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001277 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
Avi Kivity714b93d2007-01-05 16:36:53 -08001280void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281{
Avi Kivity714b93d2007-01-05 16:36:53 -08001282 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 struct kvm_mmu_page *page;
1284
1285 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1286 int i;
1287 u64 *pt;
1288
1289 if (!test_bit(slot, &page->slot_bitmap))
1290 continue;
1291
Avi Kivity47ad8e62007-05-06 15:50:58 +03001292 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1294 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001295 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001296 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001298 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 }
1300}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001301
Dor Laore0fa8262007-03-30 13:06:33 +03001302void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1303{
1304 destroy_kvm_mmu(vcpu);
1305
1306 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1307 struct kvm_mmu_page *page;
1308
1309 page = container_of(vcpu->kvm->active_mmu_pages.next,
1310 struct kvm_mmu_page, link);
1311 kvm_mmu_zap_page(vcpu, page);
1312 }
1313
1314 mmu_free_memory_caches(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001315 kvm_flush_remote_tlbs(vcpu->kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001316 init_kvm_mmu(vcpu);
1317}
1318
Avi Kivityb5a33a72007-04-15 16:31:09 +03001319void kvm_mmu_module_exit(void)
1320{
1321 if (pte_chain_cache)
1322 kmem_cache_destroy(pte_chain_cache);
1323 if (rmap_desc_cache)
1324 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001325 if (mmu_page_cache)
1326 kmem_cache_destroy(mmu_page_cache);
1327 if (mmu_page_header_cache)
1328 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001329}
1330
1331int kvm_mmu_module_init(void)
1332{
1333 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1334 sizeof(struct kvm_pte_chain),
1335 0, 0, NULL, NULL);
1336 if (!pte_chain_cache)
1337 goto nomem;
1338 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1339 sizeof(struct kvm_rmap_desc),
1340 0, 0, NULL, NULL);
1341 if (!rmap_desc_cache)
1342 goto nomem;
1343
Avi Kivityd3d25b02007-05-30 12:34:53 +03001344 mmu_page_cache = kmem_cache_create("kvm_mmu_page",
1345 PAGE_SIZE,
1346 PAGE_SIZE, 0, NULL, NULL);
1347 if (!mmu_page_cache)
1348 goto nomem;
1349
1350 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1351 sizeof(struct kvm_mmu_page),
1352 0, 0, NULL, NULL);
1353 if (!mmu_page_header_cache)
1354 goto nomem;
1355
Avi Kivityb5a33a72007-04-15 16:31:09 +03001356 return 0;
1357
1358nomem:
1359 kvm_mmu_module_exit();
1360 return -ENOMEM;
1361}
1362
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001363#ifdef AUDIT
1364
1365static const char *audit_msg;
1366
1367static gva_t canonicalize(gva_t gva)
1368{
1369#ifdef CONFIG_X86_64
1370 gva = (long long)(gva << 16) >> 16;
1371#endif
1372 return gva;
1373}
1374
1375static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1376 gva_t va, int level)
1377{
1378 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1379 int i;
1380 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1381
1382 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1383 u64 ent = pt[i];
1384
Adrian Bunk28076962007-04-28 21:20:48 +02001385 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001386 continue;
1387
1388 va = canonicalize(va);
1389 if (level > 1)
1390 audit_mappings_page(vcpu, ent, va, level - 1);
1391 else {
1392 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1393 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1394
1395 if ((ent & PT_PRESENT_MASK)
1396 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1397 printk(KERN_ERR "audit error: (%s) levels %d"
1398 " gva %lx gpa %llx hpa %llx ent %llx\n",
1399 audit_msg, vcpu->mmu.root_level,
1400 va, gpa, hpa, ent);
1401 }
1402 }
1403}
1404
1405static void audit_mappings(struct kvm_vcpu *vcpu)
1406{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001407 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001408
1409 if (vcpu->mmu.root_level == 4)
1410 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1411 else
1412 for (i = 0; i < 4; ++i)
1413 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1414 audit_mappings_page(vcpu,
1415 vcpu->mmu.pae_root[i],
1416 i << 30,
1417 2);
1418}
1419
1420static int count_rmaps(struct kvm_vcpu *vcpu)
1421{
1422 int nmaps = 0;
1423 int i, j, k;
1424
1425 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1426 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1427 struct kvm_rmap_desc *d;
1428
1429 for (j = 0; j < m->npages; ++j) {
1430 struct page *page = m->phys_mem[j];
1431
1432 if (!page->private)
1433 continue;
1434 if (!(page->private & 1)) {
1435 ++nmaps;
1436 continue;
1437 }
1438 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1439 while (d) {
1440 for (k = 0; k < RMAP_EXT; ++k)
1441 if (d->shadow_ptes[k])
1442 ++nmaps;
1443 else
1444 break;
1445 d = d->more;
1446 }
1447 }
1448 }
1449 return nmaps;
1450}
1451
1452static int count_writable_mappings(struct kvm_vcpu *vcpu)
1453{
1454 int nmaps = 0;
1455 struct kvm_mmu_page *page;
1456 int i;
1457
1458 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001459 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001460
1461 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1462 continue;
1463
1464 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1465 u64 ent = pt[i];
1466
1467 if (!(ent & PT_PRESENT_MASK))
1468 continue;
1469 if (!(ent & PT_WRITABLE_MASK))
1470 continue;
1471 ++nmaps;
1472 }
1473 }
1474 return nmaps;
1475}
1476
1477static void audit_rmap(struct kvm_vcpu *vcpu)
1478{
1479 int n_rmap = count_rmaps(vcpu);
1480 int n_actual = count_writable_mappings(vcpu);
1481
1482 if (n_rmap != n_actual)
1483 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1484 __FUNCTION__, audit_msg, n_rmap, n_actual);
1485}
1486
1487static void audit_write_protection(struct kvm_vcpu *vcpu)
1488{
1489 struct kvm_mmu_page *page;
1490
1491 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1492 hfn_t hfn;
1493 struct page *pg;
1494
1495 if (page->role.metaphysical)
1496 continue;
1497
1498 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1499 >> PAGE_SHIFT;
1500 pg = pfn_to_page(hfn);
1501 if (pg->private)
1502 printk(KERN_ERR "%s: (%s) shadow page has writable"
1503 " mappings: gfn %lx role %x\n",
1504 __FUNCTION__, audit_msg, page->gfn,
1505 page->role.word);
1506 }
1507}
1508
1509static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1510{
1511 int olddbg = dbg;
1512
1513 dbg = 0;
1514 audit_msg = msg;
1515 audit_rmap(vcpu);
1516 audit_write_protection(vcpu);
1517 audit_mappings(vcpu);
1518 dbg = olddbg;
1519}
1520
1521#endif