blob: 9be54a5e858e9e58986a4dd7423aa9533f1a8e58 [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"
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080022#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
29
30#include <asm/page.h>
31#include <asm/cmpxchg.h>
32
Avi Kivity37a7d8b2007-01-05 16:36:56 -080033#undef MMU_DEBUG
34
35#undef AUDIT
36
37#ifdef AUDIT
38static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
39#else
40static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
41#endif
42
43#ifdef MMU_DEBUG
44
45#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
46#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
47
48#else
49
50#define pgprintk(x...) do { } while (0)
51#define rmap_printk(x...) do { } while (0)
52
53#endif
54
55#if defined(MMU_DEBUG) || defined(AUDIT)
56static int dbg = 1;
57#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080058
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080059#ifndef MMU_DEBUG
60#define ASSERT(x) do { } while (0)
61#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080062#define ASSERT(x) \
63 if (!(x)) { \
64 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
65 __FILE__, __LINE__, #x); \
66 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080067#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivitycea0f0e2007-01-05 16:36:43 -080069#define PT64_PT_BITS 9
70#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
71#define PT32_PT_BITS 10
72#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
74#define PT_WRITABLE_SHIFT 1
75
76#define PT_PRESENT_MASK (1ULL << 0)
77#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
78#define PT_USER_MASK (1ULL << 2)
79#define PT_PWT_MASK (1ULL << 3)
80#define PT_PCD_MASK (1ULL << 4)
81#define PT_ACCESSED_MASK (1ULL << 5)
82#define PT_DIRTY_MASK (1ULL << 6)
83#define PT_PAGE_SIZE_MASK (1ULL << 7)
84#define PT_PAT_MASK (1ULL << 7)
85#define PT_GLOBAL_MASK (1ULL << 8)
86#define PT64_NX_MASK (1ULL << 63)
87
88#define PT_PAT_SHIFT 7
89#define PT_DIR_PAT_SHIFT 12
90#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
91
92#define PT32_DIR_PSE36_SIZE 4
93#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040094#define PT32_DIR_PSE36_MASK \
95 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
97
Avi Kivity6aa8b732006-12-10 02:21:36 -080098#define PT_FIRST_AVAIL_BITS_SHIFT 9
99#define PT64_SECOND_AVAIL_BITS_SHIFT 52
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103#define VALID_PAGE(x) ((x) != INVALID_PAGE)
104
105#define PT64_LEVEL_BITS 9
106
107#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400108 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109
110#define PT64_LEVEL_MASK(level) \
111 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
112
113#define PT64_INDEX(address, level)\
114 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
115
116
117#define PT32_LEVEL_BITS 10
118
119#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400120 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121
122#define PT32_LEVEL_MASK(level) \
123 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
124
125#define PT32_INDEX(address, level)\
126 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
127
128
Avi Kivity27aba762007-03-09 13:04:31 +0200129#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130#define PT64_DIR_BASE_ADDR_MASK \
131 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
132
133#define PT32_BASE_ADDR_MASK PAGE_MASK
134#define PT32_DIR_BASE_ADDR_MASK \
135 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
136
137
138#define PFERR_PRESENT_MASK (1U << 0)
139#define PFERR_WRITE_MASK (1U << 1)
140#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800141#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142
143#define PT64_ROOT_LEVEL 4
144#define PT32_ROOT_LEVEL 2
145#define PT32E_ROOT_LEVEL 3
146
147#define PT_DIRECTORY_LEVEL 2
148#define PT_PAGE_TABLE_LEVEL 1
149
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800150#define RMAP_EXT 4
151
152struct kvm_rmap_desc {
153 u64 *shadow_ptes[RMAP_EXT];
154 struct kvm_rmap_desc *more;
155};
156
Avi Kivityb5a33a72007-04-15 16:31:09 +0300157static struct kmem_cache *pte_chain_cache;
158static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300159static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300160
Avi Kivityc7addb92007-09-16 18:58:32 +0200161static u64 __read_mostly shadow_trap_nonpresent_pte;
162static u64 __read_mostly shadow_notrap_nonpresent_pte;
163
164void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
165{
166 shadow_trap_nonpresent_pte = trap_pte;
167 shadow_notrap_nonpresent_pte = notrap_pte;
168}
169EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
170
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171static int is_write_protection(struct kvm_vcpu *vcpu)
172{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000173 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174}
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
Avi Kivityc7addb92007-09-16 18:58:32 +0200191static int is_shadow_present_pte(u64 pte)
192{
193 pte &= ~PT_SHADOW_IO_MARK;
194 return pte != shadow_trap_nonpresent_pte
195 && pte != shadow_notrap_nonpresent_pte;
196}
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static int is_writeble_pte(unsigned long pte)
199{
200 return pte & PT_WRITABLE_MASK;
201}
202
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200203static int is_dirty_pte(unsigned long pte)
204{
205 return pte & PT_DIRTY_MASK;
206}
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static int is_io_pte(unsigned long pte)
209{
210 return pte & PT_SHADOW_IO_MARK;
211}
212
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800213static int is_rmap_pte(u64 pte)
214{
Izik Eidus9647c142007-10-16 14:43:46 +0200215 return pte != shadow_trap_nonpresent_pte
216 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800217}
218
Avi Kivitye663ee62007-05-31 15:46:04 +0300219static void set_shadow_pte(u64 *sptep, u64 spte)
220{
221#ifdef CONFIG_X86_64
222 set_64bit((unsigned long *)sptep, spte);
223#else
224 set_64bit((unsigned long long *)sptep, spte);
225#endif
226}
227
Avi Kivitye2dec932007-01-05 16:36:54 -0800228static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300229 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800230{
231 void *obj;
232
233 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800234 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800235 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300236 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800237 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800238 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800239 cache->objects[cache->nobjs++] = obj;
240 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800241 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800242}
243
244static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
245{
246 while (mc->nobjs)
247 kfree(mc->objects[--mc->nobjs]);
248}
249
Avi Kivityc1158e62007-07-20 08:18:27 +0300250static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300251 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300252{
253 struct page *page;
254
255 if (cache->nobjs >= min)
256 return 0;
257 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300258 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300259 if (!page)
260 return -ENOMEM;
261 set_page_private(page, 0);
262 cache->objects[cache->nobjs++] = page_address(page);
263 }
264 return 0;
265}
266
267static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
268{
269 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300270 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300271}
272
Avi Kivity8c438502007-04-16 11:53:17 +0300273static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
274{
275 int r;
276
Avi Kivity22d95b12007-09-14 20:26:06 +0300277 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300278 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
279 pte_chain_cache, 4);
280 if (r)
281 goto out;
282 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
283 rmap_desc_cache, 1);
284 if (r)
285 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200286 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300287 if (r)
288 goto out;
289 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
290 mmu_page_header_cache, 4);
291out:
Avi Kivity8c438502007-04-16 11:53:17 +0300292 return r;
293}
294
Avi Kivity714b93d2007-01-05 16:36:53 -0800295static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
296{
297 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
298 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300299 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300300 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800301}
302
303static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
304 size_t size)
305{
306 void *p;
307
308 BUG_ON(!mc->nobjs);
309 p = mc->objects[--mc->nobjs];
310 memset(p, 0, size);
311 return p;
312}
313
Avi Kivity714b93d2007-01-05 16:36:53 -0800314static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
315{
316 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
317 sizeof(struct kvm_pte_chain));
318}
319
Avi Kivity90cb0522007-07-17 13:04:56 +0300320static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800321{
Avi Kivity90cb0522007-07-17 13:04:56 +0300322 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800323}
324
325static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
326{
327 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
328 sizeof(struct kvm_rmap_desc));
329}
330
Avi Kivity90cb0522007-07-17 13:04:56 +0300331static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800332{
Avi Kivity90cb0522007-07-17 13:04:56 +0300333 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800334}
335
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800336/*
Izik Eidus290fc382007-09-27 14:11:22 +0200337 * Take gfn and return the reverse mapping to it.
338 * Note: gfn must be unaliased before this function get called
339 */
340
341static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
342{
343 struct kvm_memory_slot *slot;
344
345 slot = gfn_to_memslot(kvm, gfn);
346 return &slot->rmap[gfn - slot->base_gfn];
347}
348
349/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350 * Reverse mapping data structures:
351 *
Izik Eidus290fc382007-09-27 14:11:22 +0200352 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
353 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800354 *
Izik Eidus290fc382007-09-27 14:11:22 +0200355 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
356 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800357 */
Izik Eidus290fc382007-09-27 14:11:22 +0200358static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800359{
Izik Eidus290fc382007-09-27 14:11:22 +0200360 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800361 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200362 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800363 int i;
364
365 if (!is_rmap_pte(*spte))
366 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200367 gfn = unalias_gfn(vcpu->kvm, gfn);
368 page = page_header(__pa(spte));
369 page->gfns[spte - page->spt] = gfn;
370 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
371 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200373 *rmapp = (unsigned long)spte;
374 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800375 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800376 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200377 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800378 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200379 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800380 } else {
381 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200382 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800383 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
384 desc = desc->more;
385 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800386 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800387 desc = desc->more;
388 }
389 for (i = 0; desc->shadow_ptes[i]; ++i)
390 ;
391 desc->shadow_ptes[i] = spte;
392 }
393}
394
Izik Eidus290fc382007-09-27 14:11:22 +0200395static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800396 struct kvm_rmap_desc *desc,
397 int i,
398 struct kvm_rmap_desc *prev_desc)
399{
400 int j;
401
402 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
403 ;
404 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000405 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800406 if (j != 0)
407 return;
408 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200409 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800410 else
411 if (prev_desc)
412 prev_desc->more = desc->more;
413 else
Izik Eidus290fc382007-09-27 14:11:22 +0200414 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300415 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800416}
417
Izik Eidus290fc382007-09-27 14:11:22 +0200418static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800419{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800420 struct kvm_rmap_desc *desc;
421 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200422 struct kvm_mmu_page *page;
423 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800424 int i;
425
426 if (!is_rmap_pte(*spte))
427 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200428 page = page_header(__pa(spte));
Izik Eidus8a7ae052007-10-18 11:09:33 +0200429 kvm_release_page(pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >>
430 PAGE_SHIFT));
Izik Eidus290fc382007-09-27 14:11:22 +0200431 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
432 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800433 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
434 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200435 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800436 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200437 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800438 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
439 spte, *spte);
440 BUG();
441 }
Izik Eidus290fc382007-09-27 14:11:22 +0200442 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800443 } else {
444 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200445 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800446 prev_desc = NULL;
447 while (desc) {
448 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
449 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200450 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800451 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800452 prev_desc);
453 return;
454 }
455 prev_desc = desc;
456 desc = desc->more;
457 }
458 BUG();
459 }
460}
461
Izik Eidus98348e92007-10-16 14:42:30 +0200462static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800463{
Avi Kivity374cbac2007-01-05 16:36:43 -0800464 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200465 struct kvm_rmap_desc *prev_desc;
466 u64 *prev_spte;
467 int i;
468
469 if (!*rmapp)
470 return NULL;
471 else if (!(*rmapp & 1)) {
472 if (!spte)
473 return (u64 *)*rmapp;
474 return NULL;
475 }
476 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
477 prev_desc = NULL;
478 prev_spte = NULL;
479 while (desc) {
480 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
481 if (prev_spte == spte)
482 return desc->shadow_ptes[i];
483 prev_spte = desc->shadow_ptes[i];
484 }
485 desc = desc->more;
486 }
487 return NULL;
488}
489
490static void rmap_write_protect(struct kvm *kvm, u64 gfn)
491{
Izik Eidus290fc382007-09-27 14:11:22 +0200492 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800493 u64 *spte;
494
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500495 gfn = unalias_gfn(kvm, gfn);
496 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800497
Izik Eidus98348e92007-10-16 14:42:30 +0200498 spte = rmap_next(kvm, rmapp, NULL);
499 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800500 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800501 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800502 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Izik Eidus9647c142007-10-16 14:43:46 +0200503 if (is_writeble_pte(*spte))
504 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500505 kvm_flush_remote_tlbs(kvm);
Izik Eidus9647c142007-10-16 14:43:46 +0200506 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800507 }
508}
509
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800510#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300511static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512{
Avi Kivity139bdb22007-01-05 16:36:50 -0800513 u64 *pos;
514 u64 *end;
515
Avi Kivity47ad8e62007-05-06 15:50:58 +0300516 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200517 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800518 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
519 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800521 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 return 1;
523}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800524#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525
Avi Kivity90cb0522007-07-17 13:04:56 +0300526static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300527 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800528{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300529 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300530 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300531 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200532 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300533 kfree(page_head);
534 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800535}
536
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800537static unsigned kvm_page_table_hashfn(gfn_t gfn)
538{
539 return gfn;
540}
541
Avi Kivity25c0de22007-01-05 16:36:42 -0800542static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
543 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544{
545 struct kvm_mmu_page *page;
546
Avi Kivityd3d25b02007-05-30 12:34:53 +0300547 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800548 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549
Avi Kivityd3d25b02007-05-30 12:34:53 +0300550 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
551 sizeof *page);
552 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200553 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300554 set_page_private(virt_to_page(page->spt), (unsigned long)page);
555 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300556 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800558 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800560 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800561 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562}
563
Avi Kivity714b93d2007-01-05 16:36:53 -0800564static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
565 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800566{
567 struct kvm_pte_chain *pte_chain;
568 struct hlist_node *node;
569 int i;
570
571 if (!parent_pte)
572 return;
573 if (!page->multimapped) {
574 u64 *old = page->parent_pte;
575
576 if (!old) {
577 page->parent_pte = parent_pte;
578 return;
579 }
580 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800581 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800582 INIT_HLIST_HEAD(&page->parent_ptes);
583 hlist_add_head(&pte_chain->link, &page->parent_ptes);
584 pte_chain->parent_ptes[0] = old;
585 }
586 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
587 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
588 continue;
589 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
590 if (!pte_chain->parent_ptes[i]) {
591 pte_chain->parent_ptes[i] = parent_pte;
592 return;
593 }
594 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800595 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800596 BUG_ON(!pte_chain);
597 hlist_add_head(&pte_chain->link, &page->parent_ptes);
598 pte_chain->parent_ptes[0] = parent_pte;
599}
600
Avi Kivity90cb0522007-07-17 13:04:56 +0300601static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800602 u64 *parent_pte)
603{
604 struct kvm_pte_chain *pte_chain;
605 struct hlist_node *node;
606 int i;
607
608 if (!page->multimapped) {
609 BUG_ON(page->parent_pte != parent_pte);
610 page->parent_pte = NULL;
611 return;
612 }
613 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
614 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
615 if (!pte_chain->parent_ptes[i])
616 break;
617 if (pte_chain->parent_ptes[i] != parent_pte)
618 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800619 while (i + 1 < NR_PTE_CHAIN_ENTRIES
620 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800621 pte_chain->parent_ptes[i]
622 = pte_chain->parent_ptes[i + 1];
623 ++i;
624 }
625 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800626 if (i == 0) {
627 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300628 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800629 if (hlist_empty(&page->parent_ptes)) {
630 page->multimapped = 0;
631 page->parent_pte = NULL;
632 }
633 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800634 return;
635 }
636 BUG();
637}
638
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500639static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800640 gfn_t gfn)
641{
642 unsigned index;
643 struct hlist_head *bucket;
644 struct kvm_mmu_page *page;
645 struct hlist_node *node;
646
647 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
648 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500649 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800650 hlist_for_each_entry(page, node, bucket, hash_link)
651 if (page->gfn == gfn && !page->role.metaphysical) {
652 pgprintk("%s: found role %x\n",
653 __FUNCTION__, page->role.word);
654 return page;
655 }
656 return NULL;
657}
658
659static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
660 gfn_t gfn,
661 gva_t gaddr,
662 unsigned level,
663 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200664 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800665 u64 *parent_pte)
666{
667 union kvm_mmu_page_role role;
668 unsigned index;
669 unsigned quadrant;
670 struct hlist_head *bucket;
671 struct kvm_mmu_page *page;
672 struct hlist_node *node;
673
674 role.word = 0;
675 role.glevels = vcpu->mmu.root_level;
676 role.level = level;
677 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200678 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800679 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
680 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
681 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
682 role.quadrant = quadrant;
683 }
684 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
685 gfn, role.word);
686 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
687 bucket = &vcpu->kvm->mmu_page_hash[index];
688 hlist_for_each_entry(page, node, bucket, hash_link)
689 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800690 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800691 pgprintk("%s: found\n", __FUNCTION__);
692 return page;
693 }
694 page = kvm_mmu_alloc_page(vcpu, parent_pte);
695 if (!page)
696 return page;
697 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
698 page->gfn = gfn;
699 page->role = role;
700 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200701 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800702 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500703 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704 return page;
705}
706
Avi Kivity90cb0522007-07-17 13:04:56 +0300707static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800708 struct kvm_mmu_page *page)
709{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800710 unsigned i;
711 u64 *pt;
712 u64 ent;
713
Avi Kivity47ad8e62007-05-06 15:50:58 +0300714 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800715
716 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
717 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200718 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200719 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200720 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800721 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300722 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800723 return;
724 }
725
726 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
727 ent = pt[i];
728
Avi Kivityc7addb92007-09-16 18:58:32 +0200729 pt[i] = shadow_trap_nonpresent_pte;
730 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800731 continue;
732 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300733 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300735 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800736}
737
Avi Kivity90cb0522007-07-17 13:04:56 +0300738static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800739 u64 *parent_pte)
740{
Avi Kivity90cb0522007-07-17 13:04:56 +0300741 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800742}
743
Avi Kivity12b7d282007-09-23 14:10:49 +0200744static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
745{
746 int i;
747
748 for (i = 0; i < KVM_MAX_VCPUS; ++i)
749 if (kvm->vcpus[i])
750 kvm->vcpus[i]->last_pte_updated = NULL;
751}
752
Avi Kivity90cb0522007-07-17 13:04:56 +0300753static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800754 struct kvm_mmu_page *page)
755{
756 u64 *parent_pte;
757
758 while (page->multimapped || page->parent_pte) {
759 if (!page->multimapped)
760 parent_pte = page->parent_pte;
761 else {
762 struct kvm_pte_chain *chain;
763
764 chain = container_of(page->parent_ptes.first,
765 struct kvm_pte_chain, link);
766 parent_pte = chain->parent_ptes[0];
767 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800768 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300769 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200770 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800771 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300772 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800773 if (!page->root_count) {
774 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300775 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200776 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300777 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200778 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800779}
780
Izik Eidus82ce2c92007-10-02 18:52:55 +0200781/*
782 * Changing the number of mmu pages allocated to the vm
783 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
784 */
785void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
786{
787 /*
788 * If we set the number of mmu pages to be smaller be than the
789 * number of actived pages , we must to free some mmu pages before we
790 * change the value
791 */
792
793 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
794 kvm_nr_mmu_pages) {
795 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
796 - kvm->n_free_mmu_pages;
797
798 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
799 struct kvm_mmu_page *page;
800
801 page = container_of(kvm->active_mmu_pages.prev,
802 struct kvm_mmu_page, link);
803 kvm_mmu_zap_page(kvm, page);
804 n_used_mmu_pages--;
805 }
806 kvm->n_free_mmu_pages = 0;
807 }
808 else
809 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
810 - kvm->n_alloc_mmu_pages;
811
812 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
813}
814
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500815static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800816{
817 unsigned index;
818 struct hlist_head *bucket;
819 struct kvm_mmu_page *page;
820 struct hlist_node *node, *n;
821 int r;
822
823 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
824 r = 0;
825 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500826 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800827 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
828 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800829 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
830 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500831 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800832 r = 1;
833 }
834 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835}
836
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500837static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300838{
839 struct kvm_mmu_page *page;
840
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500841 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300842 pgprintk("%s: zap %lx %x\n",
843 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500844 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300845 }
846}
847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
849{
850 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
851 struct kvm_mmu_page *page_head = page_header(__pa(pte));
852
853 __set_bit(slot, &page_head->slot_bitmap);
854}
855
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500856hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 struct page *page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200859 hpa_t hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860
861 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500862 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200863 hpa = ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) | (gpa & (PAGE_SIZE-1));
864 if (is_error_page(page))
865 return hpa | HPA_ERR_MASK;
866 return hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867}
868
869hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
870{
871 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
872
873 if (gpa == UNMAPPED_GVA)
874 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500875 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876}
877
Avi Kivity039576c2007-03-20 12:46:50 +0200878struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
879{
880 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
881
882 if (gpa == UNMAPPED_GVA)
883 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500884 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200885}
886
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
888{
889}
890
891static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
892{
893 int level = PT32E_ROOT_LEVEL;
894 hpa_t table_addr = vcpu->mmu.root_hpa;
895
896 for (; ; level--) {
897 u32 index = PT64_INDEX(v, level);
898 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800899 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900
901 ASSERT(VALID_PAGE(table_addr));
902 table = __va(table_addr);
903
904 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200905 int was_rmapped;
906
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800907 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200908 was_rmapped = is_rmap_pte(pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200909 if (is_shadow_present_pte(pte) && is_writeble_pte(pte))
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800910 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
912 page_header_update_slot(vcpu->kvm, table, v);
913 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
914 PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200915 if (!was_rmapped)
916 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200917 else
918 kvm_release_page(pfn_to_page(p >> PAGE_SHIFT));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 return 0;
920 }
921
Avi Kivityc7addb92007-09-16 18:58:32 +0200922 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800923 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800926 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
927 >> PAGE_SHIFT;
928 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
929 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200930 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800931 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidus8a7ae052007-10-18 11:09:33 +0200933 kvm_release_page(pfn_to_page(p >> PAGE_SHIFT));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 return -ENOMEM;
935 }
936
Avi Kivity47ad8e62007-05-06 15:50:58 +0300937 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800938 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 }
940 table_addr = table[index] & PT64_BASE_ADDR_MASK;
941 }
942}
943
Avi Kivityc7addb92007-09-16 18:58:32 +0200944static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
945 struct kvm_mmu_page *sp)
946{
947 int i;
948
949 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
950 sp->spt[i] = shadow_trap_nonpresent_pte;
951}
952
Avi Kivity17ac10a2007-01-05 16:36:40 -0800953static void mmu_free_roots(struct kvm_vcpu *vcpu)
954{
955 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800956 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800957
Avi Kivity7b53aa52007-06-05 12:17:03 +0300958 if (!VALID_PAGE(vcpu->mmu.root_hpa))
959 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800960#ifdef CONFIG_X86_64
961 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
962 hpa_t root = vcpu->mmu.root_hpa;
963
Avi Kivity3bb65a22007-01-05 16:36:51 -0800964 page = page_header(root);
965 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800966 vcpu->mmu.root_hpa = INVALID_PAGE;
967 return;
968 }
969#endif
970 for (i = 0; i < 4; ++i) {
971 hpa_t root = vcpu->mmu.pae_root[i];
972
Avi Kivity417726a2007-04-12 17:35:58 +0300973 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300974 root &= PT64_BASE_ADDR_MASK;
975 page = page_header(root);
976 --page->root_count;
977 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800978 vcpu->mmu.pae_root[i] = INVALID_PAGE;
979 }
980 vcpu->mmu.root_hpa = INVALID_PAGE;
981}
982
983static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
984{
985 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800986 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800987 struct kvm_mmu_page *page;
988
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800989 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800990
991#ifdef CONFIG_X86_64
992 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
993 hpa_t root = vcpu->mmu.root_hpa;
994
995 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800996 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200997 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300998 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800999 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001000 vcpu->mmu.root_hpa = root;
1001 return;
1002 }
1003#endif
1004 for (i = 0; i < 4; ++i) {
1005 hpa_t root = vcpu->mmu.pae_root[i];
1006
1007 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001008 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1009 if (!is_present_pte(vcpu->pdptrs[i])) {
1010 vcpu->mmu.pae_root[i] = 0;
1011 continue;
1012 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001013 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001014 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001015 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -08001016 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001017 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001018 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001019 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001020 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001021 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1022 }
1023 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1024}
1025
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1027{
1028 return vaddr;
1029}
1030
1031static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1032 u32 error_code)
1033{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001035 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001036 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037
Avi Kivitye2dec932007-01-05 16:36:54 -08001038 r = mmu_topup_memory_caches(vcpu);
1039 if (r)
1040 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001041
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 ASSERT(vcpu);
1043 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1044
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001046 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047
Izik Eidus8a7ae052007-10-18 11:09:33 +02001048 if (is_error_hpa(paddr)) {
1049 kvm_release_page(pfn_to_page((paddr & PT64_BASE_ADDR_MASK)
1050 >> PAGE_SHIFT));
Avi Kivityebeace82007-01-05 16:36:47 -08001051 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001052 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053
Avi Kivityebeace82007-01-05 16:36:47 -08001054 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055}
1056
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057static void nonpaging_free(struct kvm_vcpu *vcpu)
1058{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001059 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060}
1061
1062static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1063{
1064 struct kvm_mmu *context = &vcpu->mmu;
1065
1066 context->new_cr3 = nonpaging_new_cr3;
1067 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 context->gva_to_gpa = nonpaging_gva_to_gpa;
1069 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001070 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001071 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001073 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 return 0;
1075}
1076
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1078{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001079 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001080 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081}
1082
1083static void paging_new_cr3(struct kvm_vcpu *vcpu)
1084{
Avi Kivity374cbac2007-01-05 16:36:43 -08001085 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001086 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087}
1088
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089static void inject_page_fault(struct kvm_vcpu *vcpu,
1090 u64 addr,
1091 u32 err_code)
1092{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001093 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094}
1095
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096static void paging_free(struct kvm_vcpu *vcpu)
1097{
1098 nonpaging_free(vcpu);
1099}
1100
1101#define PTTYPE 64
1102#include "paging_tmpl.h"
1103#undef PTTYPE
1104
1105#define PTTYPE 32
1106#include "paging_tmpl.h"
1107#undef PTTYPE
1108
Avi Kivity17ac10a2007-01-05 16:36:40 -08001109static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110{
1111 struct kvm_mmu *context = &vcpu->mmu;
1112
1113 ASSERT(is_pae(vcpu));
1114 context->new_cr3 = paging_new_cr3;
1115 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001117 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001119 context->root_level = level;
1120 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001121 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 return 0;
1123}
1124
Avi Kivity17ac10a2007-01-05 16:36:40 -08001125static int paging64_init_context(struct kvm_vcpu *vcpu)
1126{
1127 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1128}
1129
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130static int paging32_init_context(struct kvm_vcpu *vcpu)
1131{
1132 struct kvm_mmu *context = &vcpu->mmu;
1133
1134 context->new_cr3 = paging_new_cr3;
1135 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 context->gva_to_gpa = paging32_gva_to_gpa;
1137 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001138 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 context->root_level = PT32_ROOT_LEVEL;
1140 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001141 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 return 0;
1143}
1144
1145static int paging32E_init_context(struct kvm_vcpu *vcpu)
1146{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001147 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148}
1149
1150static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1151{
1152 ASSERT(vcpu);
1153 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1154
1155 if (!is_paging(vcpu))
1156 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001157 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 return paging64_init_context(vcpu);
1159 else if (is_pae(vcpu))
1160 return paging32E_init_context(vcpu);
1161 else
1162 return paging32_init_context(vcpu);
1163}
1164
1165static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1166{
1167 ASSERT(vcpu);
1168 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1169 vcpu->mmu.free(vcpu);
1170 vcpu->mmu.root_hpa = INVALID_PAGE;
1171 }
1172}
1173
1174int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1175{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001176 destroy_kvm_mmu(vcpu);
1177 return init_kvm_mmu(vcpu);
1178}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001179EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001180
1181int kvm_mmu_load(struct kvm_vcpu *vcpu)
1182{
Avi Kivity714b93d2007-01-05 16:36:53 -08001183 int r;
1184
Shaohua Li11ec2802007-07-23 14:51:37 +08001185 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001186 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001187 if (r)
1188 goto out;
1189 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001190 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001191 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001192out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001193 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001194 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001196EXPORT_SYMBOL_GPL(kvm_mmu_load);
1197
1198void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1199{
1200 mmu_free_roots(vcpu);
1201}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202
Avi Kivity09072da2007-05-01 14:16:52 +03001203static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001204 struct kvm_mmu_page *page,
1205 u64 *spte)
1206{
1207 u64 pte;
1208 struct kvm_mmu_page *child;
1209
1210 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001211 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001212 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001213 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001214 else {
1215 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001216 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001217 }
1218 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001219 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001220 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001221}
1222
Avi Kivity00284252007-05-01 16:53:31 +03001223static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1224 struct kvm_mmu_page *page,
1225 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001226 const void *new, int bytes,
1227 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001228{
1229 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1230 return;
1231
1232 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001233 paging32_update_pte(vcpu, page, spte, new, bytes,
1234 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001235 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001236 paging64_update_pte(vcpu, page, spte, new, bytes,
1237 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001238}
1239
Avi Kivity12b7d282007-09-23 14:10:49 +02001240static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1241{
1242 u64 *spte = vcpu->last_pte_updated;
1243
1244 return !!(spte && (*spte & PT_ACCESSED_MASK));
1245}
1246
Avi Kivity09072da2007-05-01 14:16:52 +03001247void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001248 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001249{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001250 gfn_t gfn = gpa >> PAGE_SHIFT;
1251 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001252 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001253 struct hlist_head *bucket;
1254 unsigned index;
1255 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001256 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001257 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001258 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001259 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001260 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001261 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001262 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001263 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001264
Avi Kivityda4a00f2007-01-05 16:36:44 -08001265 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityc7addb92007-09-16 18:58:32 +02001266 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001267 if (gfn == vcpu->last_pt_write_gfn
1268 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001269 ++vcpu->last_pt_write_count;
1270 if (vcpu->last_pt_write_count >= 3)
1271 flooded = 1;
1272 } else {
1273 vcpu->last_pt_write_gfn = gfn;
1274 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001275 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001276 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001277 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1278 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001279 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001280 if (page->gfn != gfn || page->role.metaphysical)
1281 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001282 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1283 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001284 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001285 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001286 /*
1287 * Misaligned accesses are too much trouble to fix
1288 * up; also, they usually indicate a page is not used
1289 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001290 *
1291 * If we're seeing too many writes to a page,
1292 * it may no longer be a page table, or we may be
1293 * forking, in which case it is better to unmap the
1294 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001295 */
1296 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1297 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001298 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001299 continue;
1300 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001301 page_offset = offset;
1302 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001303 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001304 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001305 page_offset <<= 1; /* 32->64 */
1306 /*
1307 * A 32-bit pde maps 4MB while the shadow pdes map
1308 * only 2MB. So we need to double the offset again
1309 * and zap two pdes instead of one.
1310 */
1311 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001312 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001313 page_offset <<= 1;
1314 npte = 2;
1315 }
Avi Kivityfce06572007-05-01 16:44:05 +03001316 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001317 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001318 if (quadrant != page->role.quadrant)
1319 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001320 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001321 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001322 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001323 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001324 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1325 page_offset & (pte_size - 1));
Avi Kivityac1b7142007-03-08 17:13:32 +02001326 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001327 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001328 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001329 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001330}
1331
Avi Kivitya4360362007-01-05 16:36:45 -08001332int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1333{
1334 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1335
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001336 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001337}
1338
Avi Kivity22d95b12007-09-14 20:26:06 +03001339void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001340{
1341 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1342 struct kvm_mmu_page *page;
1343
1344 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1345 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001346 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityebeace82007-01-05 16:36:47 -08001347 }
1348}
Avi Kivityebeace82007-01-05 16:36:47 -08001349
Avi Kivity30677142007-10-28 18:48:59 +02001350int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1351{
1352 int r;
1353 enum emulation_result er;
1354
1355 mutex_lock(&vcpu->kvm->lock);
1356 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1357 if (r < 0)
1358 goto out;
1359
1360 if (!r) {
1361 r = 1;
1362 goto out;
1363 }
1364
Avi Kivityb733bfb2007-10-28 18:52:05 +02001365 r = mmu_topup_memory_caches(vcpu);
1366 if (r)
1367 goto out;
1368
Avi Kivity30677142007-10-28 18:48:59 +02001369 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1370 mutex_unlock(&vcpu->kvm->lock);
1371
1372 switch (er) {
1373 case EMULATE_DONE:
1374 return 1;
1375 case EMULATE_DO_MMIO:
1376 ++vcpu->stat.mmio_exits;
1377 return 0;
1378 case EMULATE_FAIL:
1379 kvm_report_emulation_failure(vcpu, "pagetable");
1380 return 1;
1381 default:
1382 BUG();
1383 }
1384out:
1385 mutex_unlock(&vcpu->kvm->lock);
1386 return r;
1387}
1388EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1389
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390static void free_mmu_pages(struct kvm_vcpu *vcpu)
1391{
Avi Kivityf51234c2007-01-05 16:36:52 -08001392 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
Avi Kivityf51234c2007-01-05 16:36:52 -08001394 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1395 page = container_of(vcpu->kvm->active_mmu_pages.next,
1396 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001397 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001398 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001399 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400}
1401
1402static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1403{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001404 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 int i;
1406
1407 ASSERT(vcpu);
1408
Izik Eidus82ce2c92007-10-02 18:52:55 +02001409 if (vcpu->kvm->n_requested_mmu_pages)
1410 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1411 else
1412 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001413 /*
1414 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1415 * Therefore we need to allocate shadow page tables in the first
1416 * 4GB of memory, which happens to fit the DMA32 zone.
1417 */
1418 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1419 if (!page)
1420 goto error_1;
1421 vcpu->mmu.pae_root = page_address(page);
1422 for (i = 0; i < 4; ++i)
1423 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1424
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 return 0;
1426
1427error_1:
1428 free_mmu_pages(vcpu);
1429 return -ENOMEM;
1430}
1431
Ingo Molnar8018c272006-12-29 16:50:01 -08001432int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434 ASSERT(vcpu);
1435 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
Ingo Molnar8018c272006-12-29 16:50:01 -08001437 return alloc_mmu_pages(vcpu);
1438}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439
Ingo Molnar8018c272006-12-29 16:50:01 -08001440int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1441{
1442 ASSERT(vcpu);
1443 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001444
Ingo Molnar8018c272006-12-29 16:50:01 -08001445 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446}
1447
1448void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1449{
1450 ASSERT(vcpu);
1451
1452 destroy_kvm_mmu(vcpu);
1453 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001454 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455}
1456
Avi Kivity90cb0522007-07-17 13:04:56 +03001457void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458{
1459 struct kvm_mmu_page *page;
1460
1461 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1462 int i;
1463 u64 *pt;
1464
1465 if (!test_bit(slot, &page->slot_bitmap))
1466 continue;
1467
Avi Kivity47ad8e62007-05-06 15:50:58 +03001468 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1470 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001471 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 }
1474}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001475
Avi Kivity90cb0522007-07-17 13:04:56 +03001476void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001477{
Avi Kivity90cb0522007-07-17 13:04:56 +03001478 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001479
Avi Kivity90cb0522007-07-17 13:04:56 +03001480 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1481 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001482
Avi Kivity90cb0522007-07-17 13:04:56 +03001483 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001484}
1485
Avi Kivityb5a33a72007-04-15 16:31:09 +03001486void kvm_mmu_module_exit(void)
1487{
1488 if (pte_chain_cache)
1489 kmem_cache_destroy(pte_chain_cache);
1490 if (rmap_desc_cache)
1491 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001492 if (mmu_page_header_cache)
1493 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001494}
1495
1496int kvm_mmu_module_init(void)
1497{
1498 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1499 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001500 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001501 if (!pte_chain_cache)
1502 goto nomem;
1503 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1504 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001505 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001506 if (!rmap_desc_cache)
1507 goto nomem;
1508
Avi Kivityd3d25b02007-05-30 12:34:53 +03001509 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1510 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001511 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001512 if (!mmu_page_header_cache)
1513 goto nomem;
1514
Avi Kivityb5a33a72007-04-15 16:31:09 +03001515 return 0;
1516
1517nomem:
1518 kvm_mmu_module_exit();
1519 return -ENOMEM;
1520}
1521
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001522#ifdef AUDIT
1523
1524static const char *audit_msg;
1525
1526static gva_t canonicalize(gva_t gva)
1527{
1528#ifdef CONFIG_X86_64
1529 gva = (long long)(gva << 16) >> 16;
1530#endif
1531 return gva;
1532}
1533
1534static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1535 gva_t va, int level)
1536{
1537 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1538 int i;
1539 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1540
1541 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1542 u64 ent = pt[i];
1543
Avi Kivityc7addb92007-09-16 18:58:32 +02001544 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001545 continue;
1546
1547 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001548 if (level > 1) {
1549 if (ent == shadow_notrap_nonpresent_pte)
1550 printk(KERN_ERR "audit: (%s) nontrapping pte"
1551 " in nonleaf level: levels %d gva %lx"
1552 " level %d pte %llx\n", audit_msg,
1553 vcpu->mmu.root_level, va, level, ent);
1554
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001555 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001556 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001557 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1558 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001559 struct page *page;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001560
Avi Kivityc7addb92007-09-16 18:58:32 +02001561 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001562 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001563 printk(KERN_ERR "xx audit error: (%s) levels %d"
1564 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001565 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001566 va, gpa, hpa, ent,
1567 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001568 else if (ent == shadow_notrap_nonpresent_pte
1569 && !is_error_hpa(hpa))
1570 printk(KERN_ERR "audit: (%s) notrap shadow,"
1571 " valid guest gva %lx\n", audit_msg, va);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001572 page = pfn_to_page((gpa & PT64_BASE_ADDR_MASK)
1573 >> PAGE_SHIFT);
1574 kvm_release_page(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001575
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001576 }
1577 }
1578}
1579
1580static void audit_mappings(struct kvm_vcpu *vcpu)
1581{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001582 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001583
1584 if (vcpu->mmu.root_level == 4)
1585 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1586 else
1587 for (i = 0; i < 4; ++i)
1588 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1589 audit_mappings_page(vcpu,
1590 vcpu->mmu.pae_root[i],
1591 i << 30,
1592 2);
1593}
1594
1595static int count_rmaps(struct kvm_vcpu *vcpu)
1596{
1597 int nmaps = 0;
1598 int i, j, k;
1599
1600 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1601 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1602 struct kvm_rmap_desc *d;
1603
1604 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001605 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001606
Izik Eidus290fc382007-09-27 14:11:22 +02001607 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001608 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001609 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001610 ++nmaps;
1611 continue;
1612 }
Izik Eidus290fc382007-09-27 14:11:22 +02001613 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001614 while (d) {
1615 for (k = 0; k < RMAP_EXT; ++k)
1616 if (d->shadow_ptes[k])
1617 ++nmaps;
1618 else
1619 break;
1620 d = d->more;
1621 }
1622 }
1623 }
1624 return nmaps;
1625}
1626
1627static int count_writable_mappings(struct kvm_vcpu *vcpu)
1628{
1629 int nmaps = 0;
1630 struct kvm_mmu_page *page;
1631 int i;
1632
1633 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001634 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001635
1636 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1637 continue;
1638
1639 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1640 u64 ent = pt[i];
1641
1642 if (!(ent & PT_PRESENT_MASK))
1643 continue;
1644 if (!(ent & PT_WRITABLE_MASK))
1645 continue;
1646 ++nmaps;
1647 }
1648 }
1649 return nmaps;
1650}
1651
1652static void audit_rmap(struct kvm_vcpu *vcpu)
1653{
1654 int n_rmap = count_rmaps(vcpu);
1655 int n_actual = count_writable_mappings(vcpu);
1656
1657 if (n_rmap != n_actual)
1658 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1659 __FUNCTION__, audit_msg, n_rmap, n_actual);
1660}
1661
1662static void audit_write_protection(struct kvm_vcpu *vcpu)
1663{
1664 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001665 struct kvm_memory_slot *slot;
1666 unsigned long *rmapp;
1667 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001668
1669 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001670 if (page->role.metaphysical)
1671 continue;
1672
Izik Eidus290fc382007-09-27 14:11:22 +02001673 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1674 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1675 rmapp = &slot->rmap[gfn - slot->base_gfn];
1676 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001677 printk(KERN_ERR "%s: (%s) shadow page has writable"
1678 " mappings: gfn %lx role %x\n",
1679 __FUNCTION__, audit_msg, page->gfn,
1680 page->role.word);
1681 }
1682}
1683
1684static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1685{
1686 int olddbg = dbg;
1687
1688 dbg = 0;
1689 audit_msg = msg;
1690 audit_rmap(vcpu);
1691 audit_write_protection(vcpu);
1692 audit_mappings(vcpu);
1693 dbg = olddbg;
1694}
1695
1696#endif