blob: 87d8e70fe5023ba0dc915b5316c6b51b52687d09 [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
Avi Kivity4cee5762007-11-18 16:37:07 +0200758 ++kvm->stat.mmu_shadow_zapped;
Avi Kivitya4360362007-01-05 16:36:45 -0800759 while (page->multimapped || page->parent_pte) {
760 if (!page->multimapped)
761 parent_pte = page->parent_pte;
762 else {
763 struct kvm_pte_chain *chain;
764
765 chain = container_of(page->parent_ptes.first,
766 struct kvm_pte_chain, link);
767 parent_pte = chain->parent_ptes[0];
768 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800769 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300770 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200771 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800772 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300773 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800774 if (!page->root_count) {
775 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300776 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200777 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300778 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200779 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800780}
781
Izik Eidus82ce2c92007-10-02 18:52:55 +0200782/*
783 * Changing the number of mmu pages allocated to the vm
784 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
785 */
786void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
787{
788 /*
789 * If we set the number of mmu pages to be smaller be than the
790 * number of actived pages , we must to free some mmu pages before we
791 * change the value
792 */
793
794 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
795 kvm_nr_mmu_pages) {
796 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
797 - kvm->n_free_mmu_pages;
798
799 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
800 struct kvm_mmu_page *page;
801
802 page = container_of(kvm->active_mmu_pages.prev,
803 struct kvm_mmu_page, link);
804 kvm_mmu_zap_page(kvm, page);
805 n_used_mmu_pages--;
806 }
807 kvm->n_free_mmu_pages = 0;
808 }
809 else
810 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
811 - kvm->n_alloc_mmu_pages;
812
813 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
814}
815
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500816static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800817{
818 unsigned index;
819 struct hlist_head *bucket;
820 struct kvm_mmu_page *page;
821 struct hlist_node *node, *n;
822 int r;
823
824 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
825 r = 0;
826 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500827 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800828 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
829 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800830 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
831 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500832 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800833 r = 1;
834 }
835 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800836}
837
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500838static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300839{
840 struct kvm_mmu_page *page;
841
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500842 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300843 pgprintk("%s: zap %lx %x\n",
844 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500845 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300846 }
847}
848
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
850{
851 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
852 struct kvm_mmu_page *page_head = page_header(__pa(pte));
853
854 __set_bit(slot, &page_head->slot_bitmap);
855}
856
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500857hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 struct page *page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200860 hpa_t hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
862 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500863 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200864 hpa = ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) | (gpa & (PAGE_SIZE-1));
865 if (is_error_page(page))
866 return hpa | HPA_ERR_MASK;
867 return hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868}
869
870hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
871{
872 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
873
874 if (gpa == UNMAPPED_GVA)
875 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500876 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877}
878
Avi Kivity039576c2007-03-20 12:46:50 +0200879struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
880{
881 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
882
883 if (gpa == UNMAPPED_GVA)
884 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500885 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200886}
887
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
889{
890}
891
892static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
893{
894 int level = PT32E_ROOT_LEVEL;
895 hpa_t table_addr = vcpu->mmu.root_hpa;
896
897 for (; ; level--) {
898 u32 index = PT64_INDEX(v, level);
899 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800900 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901
902 ASSERT(VALID_PAGE(table_addr));
903 table = __va(table_addr);
904
905 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200906 int was_rmapped;
907
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800908 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200909 was_rmapped = is_rmap_pte(pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200910 if (is_shadow_present_pte(pte) && is_writeble_pte(pte))
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
913 page_header_update_slot(vcpu->kvm, table, v);
914 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
915 PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200916 if (!was_rmapped)
917 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200918 else
919 kvm_release_page(pfn_to_page(p >> PAGE_SHIFT));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 return 0;
921 }
922
Avi Kivityc7addb92007-09-16 18:58:32 +0200923 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800924 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800925 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
928 >> PAGE_SHIFT;
929 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
930 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200931 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800932 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidus8a7ae052007-10-18 11:09:33 +0200934 kvm_release_page(pfn_to_page(p >> PAGE_SHIFT));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 return -ENOMEM;
936 }
937
Avi Kivity47ad8e62007-05-06 15:50:58 +0300938 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800939 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 }
941 table_addr = table[index] & PT64_BASE_ADDR_MASK;
942 }
943}
944
Avi Kivityc7addb92007-09-16 18:58:32 +0200945static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
946 struct kvm_mmu_page *sp)
947{
948 int i;
949
950 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
951 sp->spt[i] = shadow_trap_nonpresent_pte;
952}
953
Avi Kivity17ac10a2007-01-05 16:36:40 -0800954static void mmu_free_roots(struct kvm_vcpu *vcpu)
955{
956 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800957 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800958
Avi Kivity7b53aa52007-06-05 12:17:03 +0300959 if (!VALID_PAGE(vcpu->mmu.root_hpa))
960 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800961#ifdef CONFIG_X86_64
962 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
963 hpa_t root = vcpu->mmu.root_hpa;
964
Avi Kivity3bb65a22007-01-05 16:36:51 -0800965 page = page_header(root);
966 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800967 vcpu->mmu.root_hpa = INVALID_PAGE;
968 return;
969 }
970#endif
971 for (i = 0; i < 4; ++i) {
972 hpa_t root = vcpu->mmu.pae_root[i];
973
Avi Kivity417726a2007-04-12 17:35:58 +0300974 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300975 root &= PT64_BASE_ADDR_MASK;
976 page = page_header(root);
977 --page->root_count;
978 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800979 vcpu->mmu.pae_root[i] = INVALID_PAGE;
980 }
981 vcpu->mmu.root_hpa = INVALID_PAGE;
982}
983
984static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
985{
986 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800987 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800988 struct kvm_mmu_page *page;
989
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800990 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800991
992#ifdef CONFIG_X86_64
993 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
994 hpa_t root = vcpu->mmu.root_hpa;
995
996 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800997 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200998 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300999 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001000 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001001 vcpu->mmu.root_hpa = root;
1002 return;
1003 }
1004#endif
1005 for (i = 0; i < 4; ++i) {
1006 hpa_t root = vcpu->mmu.pae_root[i];
1007
1008 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001009 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1010 if (!is_present_pte(vcpu->pdptrs[i])) {
1011 vcpu->mmu.pae_root[i] = 0;
1012 continue;
1013 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001014 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001015 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001016 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -08001017 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001018 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001019 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001020 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001021 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001022 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1023 }
1024 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1025}
1026
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1028{
1029 return vaddr;
1030}
1031
1032static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1033 u32 error_code)
1034{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001036 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001037 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038
Avi Kivitye2dec932007-01-05 16:36:54 -08001039 r = mmu_topup_memory_caches(vcpu);
1040 if (r)
1041 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001042
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 ASSERT(vcpu);
1044 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1045
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001047 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048
Izik Eidus8a7ae052007-10-18 11:09:33 +02001049 if (is_error_hpa(paddr)) {
1050 kvm_release_page(pfn_to_page((paddr & PT64_BASE_ADDR_MASK)
1051 >> PAGE_SHIFT));
Avi Kivityebeace82007-01-05 16:36:47 -08001052 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001053 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054
Avi Kivityebeace82007-01-05 16:36:47 -08001055 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056}
1057
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058static void nonpaging_free(struct kvm_vcpu *vcpu)
1059{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001060 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061}
1062
1063static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1064{
1065 struct kvm_mmu *context = &vcpu->mmu;
1066
1067 context->new_cr3 = nonpaging_new_cr3;
1068 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 context->gva_to_gpa = nonpaging_gva_to_gpa;
1070 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001071 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001072 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001074 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 return 0;
1076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1079{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001080 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001081 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082}
1083
1084static void paging_new_cr3(struct kvm_vcpu *vcpu)
1085{
Avi Kivity374cbac2007-01-05 16:36:43 -08001086 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001087 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088}
1089
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090static void inject_page_fault(struct kvm_vcpu *vcpu,
1091 u64 addr,
1092 u32 err_code)
1093{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001094 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095}
1096
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097static void paging_free(struct kvm_vcpu *vcpu)
1098{
1099 nonpaging_free(vcpu);
1100}
1101
1102#define PTTYPE 64
1103#include "paging_tmpl.h"
1104#undef PTTYPE
1105
1106#define PTTYPE 32
1107#include "paging_tmpl.h"
1108#undef PTTYPE
1109
Avi Kivity17ac10a2007-01-05 16:36:40 -08001110static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111{
1112 struct kvm_mmu *context = &vcpu->mmu;
1113
1114 ASSERT(is_pae(vcpu));
1115 context->new_cr3 = paging_new_cr3;
1116 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001118 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001120 context->root_level = level;
1121 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001122 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 return 0;
1124}
1125
Avi Kivity17ac10a2007-01-05 16:36:40 -08001126static int paging64_init_context(struct kvm_vcpu *vcpu)
1127{
1128 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1129}
1130
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131static int paging32_init_context(struct kvm_vcpu *vcpu)
1132{
1133 struct kvm_mmu *context = &vcpu->mmu;
1134
1135 context->new_cr3 = paging_new_cr3;
1136 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 context->gva_to_gpa = paging32_gva_to_gpa;
1138 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001139 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140 context->root_level = PT32_ROOT_LEVEL;
1141 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001142 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 return 0;
1144}
1145
1146static int paging32E_init_context(struct kvm_vcpu *vcpu)
1147{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001148 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149}
1150
1151static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1152{
1153 ASSERT(vcpu);
1154 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1155
1156 if (!is_paging(vcpu))
1157 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001158 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 return paging64_init_context(vcpu);
1160 else if (is_pae(vcpu))
1161 return paging32E_init_context(vcpu);
1162 else
1163 return paging32_init_context(vcpu);
1164}
1165
1166static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1167{
1168 ASSERT(vcpu);
1169 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1170 vcpu->mmu.free(vcpu);
1171 vcpu->mmu.root_hpa = INVALID_PAGE;
1172 }
1173}
1174
1175int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1176{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001177 destroy_kvm_mmu(vcpu);
1178 return init_kvm_mmu(vcpu);
1179}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001180EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001181
1182int kvm_mmu_load(struct kvm_vcpu *vcpu)
1183{
Avi Kivity714b93d2007-01-05 16:36:53 -08001184 int r;
1185
Shaohua Li11ec2802007-07-23 14:51:37 +08001186 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001187 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001188 if (r)
1189 goto out;
1190 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001191 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001192 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001193out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001194 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001195 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001197EXPORT_SYMBOL_GPL(kvm_mmu_load);
1198
1199void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1200{
1201 mmu_free_roots(vcpu);
1202}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203
Avi Kivity09072da2007-05-01 14:16:52 +03001204static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001205 struct kvm_mmu_page *page,
1206 u64 *spte)
1207{
1208 u64 pte;
1209 struct kvm_mmu_page *child;
1210
1211 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001212 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001213 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001214 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001215 else {
1216 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001217 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001218 }
1219 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001220 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001221 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001222}
1223
Avi Kivity00284252007-05-01 16:53:31 +03001224static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1225 struct kvm_mmu_page *page,
1226 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001227 const void *new, int bytes,
1228 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001229{
Avi Kivity4cee5762007-11-18 16:37:07 +02001230 if (page->role.level != PT_PAGE_TABLE_LEVEL) {
1231 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001232 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001233 }
Avi Kivity00284252007-05-01 16:53:31 +03001234
Avi Kivity4cee5762007-11-18 16:37:07 +02001235 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity00284252007-05-01 16:53:31 +03001236 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001237 paging32_update_pte(vcpu, page, spte, new, bytes,
1238 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001239 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001240 paging64_update_pte(vcpu, page, spte, new, bytes,
1241 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001242}
1243
Avi Kivity12b7d282007-09-23 14:10:49 +02001244static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1245{
1246 u64 *spte = vcpu->last_pte_updated;
1247
1248 return !!(spte && (*spte & PT_ACCESSED_MASK));
1249}
1250
Avi Kivity09072da2007-05-01 14:16:52 +03001251void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001252 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001253{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001254 gfn_t gfn = gpa >> PAGE_SHIFT;
1255 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001256 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001257 struct hlist_head *bucket;
1258 unsigned index;
1259 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001260 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001261 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001262 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001263 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001264 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001265 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001266 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001267 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001268
Avi Kivityda4a00f2007-01-05 16:36:44 -08001269 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity4cee5762007-11-18 16:37:07 +02001270 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001271 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001272 if (gfn == vcpu->last_pt_write_gfn
1273 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001274 ++vcpu->last_pt_write_count;
1275 if (vcpu->last_pt_write_count >= 3)
1276 flooded = 1;
1277 } else {
1278 vcpu->last_pt_write_gfn = gfn;
1279 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001280 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001281 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001282 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1283 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001284 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001285 if (page->gfn != gfn || page->role.metaphysical)
1286 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001287 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1288 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001289 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001290 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001291 /*
1292 * Misaligned accesses are too much trouble to fix
1293 * up; also, they usually indicate a page is not used
1294 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001295 *
1296 * If we're seeing too many writes to a page,
1297 * it may no longer be a page table, or we may be
1298 * forking, in which case it is better to unmap the
1299 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001300 */
1301 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1302 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001303 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001304 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001305 continue;
1306 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001307 page_offset = offset;
1308 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001309 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001310 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001311 page_offset <<= 1; /* 32->64 */
1312 /*
1313 * A 32-bit pde maps 4MB while the shadow pdes map
1314 * only 2MB. So we need to double the offset again
1315 * and zap two pdes instead of one.
1316 */
1317 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001318 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001319 page_offset <<= 1;
1320 npte = 2;
1321 }
Avi Kivityfce06572007-05-01 16:44:05 +03001322 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001323 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001324 if (quadrant != page->role.quadrant)
1325 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001326 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001327 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001328 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001329 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001330 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1331 page_offset & (pte_size - 1));
Avi Kivityac1b7142007-03-08 17:13:32 +02001332 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001333 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001334 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001335 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001336}
1337
Avi Kivitya4360362007-01-05 16:36:45 -08001338int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1339{
1340 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1341
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001342 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001343}
1344
Avi Kivity22d95b12007-09-14 20:26:06 +03001345void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001346{
1347 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1348 struct kvm_mmu_page *page;
1349
1350 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1351 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001352 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001353 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001354 }
1355}
Avi Kivityebeace82007-01-05 16:36:47 -08001356
Avi Kivity30677142007-10-28 18:48:59 +02001357int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1358{
1359 int r;
1360 enum emulation_result er;
1361
1362 mutex_lock(&vcpu->kvm->lock);
1363 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1364 if (r < 0)
1365 goto out;
1366
1367 if (!r) {
1368 r = 1;
1369 goto out;
1370 }
1371
Avi Kivityb733bfb2007-10-28 18:52:05 +02001372 r = mmu_topup_memory_caches(vcpu);
1373 if (r)
1374 goto out;
1375
Avi Kivity30677142007-10-28 18:48:59 +02001376 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1377 mutex_unlock(&vcpu->kvm->lock);
1378
1379 switch (er) {
1380 case EMULATE_DONE:
1381 return 1;
1382 case EMULATE_DO_MMIO:
1383 ++vcpu->stat.mmio_exits;
1384 return 0;
1385 case EMULATE_FAIL:
1386 kvm_report_emulation_failure(vcpu, "pagetable");
1387 return 1;
1388 default:
1389 BUG();
1390 }
1391out:
1392 mutex_unlock(&vcpu->kvm->lock);
1393 return r;
1394}
1395EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1396
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397static void free_mmu_pages(struct kvm_vcpu *vcpu)
1398{
Avi Kivityf51234c2007-01-05 16:36:52 -08001399 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400
Avi Kivityf51234c2007-01-05 16:36:52 -08001401 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1402 page = container_of(vcpu->kvm->active_mmu_pages.next,
1403 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001404 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001405 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001406 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
1408
1409static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1410{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001411 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 int i;
1413
1414 ASSERT(vcpu);
1415
Izik Eidus82ce2c92007-10-02 18:52:55 +02001416 if (vcpu->kvm->n_requested_mmu_pages)
1417 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1418 else
1419 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001420 /*
1421 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1422 * Therefore we need to allocate shadow page tables in the first
1423 * 4GB of memory, which happens to fit the DMA32 zone.
1424 */
1425 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1426 if (!page)
1427 goto error_1;
1428 vcpu->mmu.pae_root = page_address(page);
1429 for (i = 0; i < 4; ++i)
1430 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1431
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432 return 0;
1433
1434error_1:
1435 free_mmu_pages(vcpu);
1436 return -ENOMEM;
1437}
1438
Ingo Molnar8018c272006-12-29 16:50:01 -08001439int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 ASSERT(vcpu);
1442 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443
Ingo Molnar8018c272006-12-29 16:50:01 -08001444 return alloc_mmu_pages(vcpu);
1445}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446
Ingo Molnar8018c272006-12-29 16:50:01 -08001447int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1448{
1449 ASSERT(vcpu);
1450 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001451
Ingo Molnar8018c272006-12-29 16:50:01 -08001452 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453}
1454
1455void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1456{
1457 ASSERT(vcpu);
1458
1459 destroy_kvm_mmu(vcpu);
1460 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001461 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462}
1463
Avi Kivity90cb0522007-07-17 13:04:56 +03001464void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465{
1466 struct kvm_mmu_page *page;
1467
1468 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1469 int i;
1470 u64 *pt;
1471
1472 if (!test_bit(slot, &page->slot_bitmap))
1473 continue;
1474
Avi Kivity47ad8e62007-05-06 15:50:58 +03001475 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1477 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001478 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 }
1481}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001482
Avi Kivity90cb0522007-07-17 13:04:56 +03001483void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001484{
Avi Kivity90cb0522007-07-17 13:04:56 +03001485 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001486
Avi Kivity90cb0522007-07-17 13:04:56 +03001487 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1488 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001489
Avi Kivity90cb0522007-07-17 13:04:56 +03001490 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001491}
1492
Avi Kivityb5a33a72007-04-15 16:31:09 +03001493void kvm_mmu_module_exit(void)
1494{
1495 if (pte_chain_cache)
1496 kmem_cache_destroy(pte_chain_cache);
1497 if (rmap_desc_cache)
1498 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001499 if (mmu_page_header_cache)
1500 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001501}
1502
1503int kvm_mmu_module_init(void)
1504{
1505 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1506 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001507 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001508 if (!pte_chain_cache)
1509 goto nomem;
1510 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1511 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001512 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001513 if (!rmap_desc_cache)
1514 goto nomem;
1515
Avi Kivityd3d25b02007-05-30 12:34:53 +03001516 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1517 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001518 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001519 if (!mmu_page_header_cache)
1520 goto nomem;
1521
Avi Kivityb5a33a72007-04-15 16:31:09 +03001522 return 0;
1523
1524nomem:
1525 kvm_mmu_module_exit();
1526 return -ENOMEM;
1527}
1528
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001529#ifdef AUDIT
1530
1531static const char *audit_msg;
1532
1533static gva_t canonicalize(gva_t gva)
1534{
1535#ifdef CONFIG_X86_64
1536 gva = (long long)(gva << 16) >> 16;
1537#endif
1538 return gva;
1539}
1540
1541static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1542 gva_t va, int level)
1543{
1544 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1545 int i;
1546 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1547
1548 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1549 u64 ent = pt[i];
1550
Avi Kivityc7addb92007-09-16 18:58:32 +02001551 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001552 continue;
1553
1554 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001555 if (level > 1) {
1556 if (ent == shadow_notrap_nonpresent_pte)
1557 printk(KERN_ERR "audit: (%s) nontrapping pte"
1558 " in nonleaf level: levels %d gva %lx"
1559 " level %d pte %llx\n", audit_msg,
1560 vcpu->mmu.root_level, va, level, ent);
1561
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001562 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001563 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001564 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1565 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001566 struct page *page;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001567
Avi Kivityc7addb92007-09-16 18:58:32 +02001568 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001569 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001570 printk(KERN_ERR "xx audit error: (%s) levels %d"
1571 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001572 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001573 va, gpa, hpa, ent,
1574 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001575 else if (ent == shadow_notrap_nonpresent_pte
1576 && !is_error_hpa(hpa))
1577 printk(KERN_ERR "audit: (%s) notrap shadow,"
1578 " valid guest gva %lx\n", audit_msg, va);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001579 page = pfn_to_page((gpa & PT64_BASE_ADDR_MASK)
1580 >> PAGE_SHIFT);
1581 kvm_release_page(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001582
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001583 }
1584 }
1585}
1586
1587static void audit_mappings(struct kvm_vcpu *vcpu)
1588{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001589 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001590
1591 if (vcpu->mmu.root_level == 4)
1592 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1593 else
1594 for (i = 0; i < 4; ++i)
1595 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1596 audit_mappings_page(vcpu,
1597 vcpu->mmu.pae_root[i],
1598 i << 30,
1599 2);
1600}
1601
1602static int count_rmaps(struct kvm_vcpu *vcpu)
1603{
1604 int nmaps = 0;
1605 int i, j, k;
1606
1607 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1608 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1609 struct kvm_rmap_desc *d;
1610
1611 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001612 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001613
Izik Eidus290fc382007-09-27 14:11:22 +02001614 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001615 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001616 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001617 ++nmaps;
1618 continue;
1619 }
Izik Eidus290fc382007-09-27 14:11:22 +02001620 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001621 while (d) {
1622 for (k = 0; k < RMAP_EXT; ++k)
1623 if (d->shadow_ptes[k])
1624 ++nmaps;
1625 else
1626 break;
1627 d = d->more;
1628 }
1629 }
1630 }
1631 return nmaps;
1632}
1633
1634static int count_writable_mappings(struct kvm_vcpu *vcpu)
1635{
1636 int nmaps = 0;
1637 struct kvm_mmu_page *page;
1638 int i;
1639
1640 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001641 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001642
1643 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1644 continue;
1645
1646 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1647 u64 ent = pt[i];
1648
1649 if (!(ent & PT_PRESENT_MASK))
1650 continue;
1651 if (!(ent & PT_WRITABLE_MASK))
1652 continue;
1653 ++nmaps;
1654 }
1655 }
1656 return nmaps;
1657}
1658
1659static void audit_rmap(struct kvm_vcpu *vcpu)
1660{
1661 int n_rmap = count_rmaps(vcpu);
1662 int n_actual = count_writable_mappings(vcpu);
1663
1664 if (n_rmap != n_actual)
1665 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1666 __FUNCTION__, audit_msg, n_rmap, n_actual);
1667}
1668
1669static void audit_write_protection(struct kvm_vcpu *vcpu)
1670{
1671 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001672 struct kvm_memory_slot *slot;
1673 unsigned long *rmapp;
1674 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001675
1676 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001677 if (page->role.metaphysical)
1678 continue;
1679
Izik Eidus290fc382007-09-27 14:11:22 +02001680 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1681 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1682 rmapp = &slot->rmap[gfn - slot->base_gfn];
1683 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001684 printk(KERN_ERR "%s: (%s) shadow page has writable"
1685 " mappings: gfn %lx role %x\n",
1686 __FUNCTION__, audit_msg, page->gfn,
1687 page->role.word);
1688 }
1689}
1690
1691static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1692{
1693 int olddbg = dbg;
1694
1695 dbg = 0;
1696 audit_msg = msg;
1697 audit_rmap(vcpu);
1698 audit_write_protection(vcpu);
1699 audit_mappings(vcpu);
1700 dbg = olddbg;
1701}
1702
1703#endif