blob: 834698d24595667d154a20110608e2e5f4be5b97 [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"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
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>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Avi Kivitye4956062007-06-28 14:15:57 -040030
31#include <asm/page.h>
32#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020033#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040034
Avi Kivity37a7d8b2007-01-05 16:36:56 -080035#undef MMU_DEBUG
36
37#undef AUDIT
38
39#ifdef AUDIT
40static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
41#else
42static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
43#endif
44
45#ifdef MMU_DEBUG
46
47#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
48#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
49
50#else
51
52#define pgprintk(x...) do { } while (0)
53#define rmap_printk(x...) do { } while (0)
54
55#endif
56
57#if defined(MMU_DEBUG) || defined(AUDIT)
58static int dbg = 1;
59#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080060
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080061#ifndef MMU_DEBUG
62#define ASSERT(x) do { } while (0)
63#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080064#define ASSERT(x) \
65 if (!(x)) { \
66 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
67 __FILE__, __LINE__, #x); \
68 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080069#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
Avi Kivitycea0f0e2007-01-05 16:36:43 -080071#define PT64_PT_BITS 9
72#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
73#define PT32_PT_BITS 10
74#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075
76#define PT_WRITABLE_SHIFT 1
77
78#define PT_PRESENT_MASK (1ULL << 0)
79#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
80#define PT_USER_MASK (1ULL << 2)
81#define PT_PWT_MASK (1ULL << 3)
82#define PT_PCD_MASK (1ULL << 4)
83#define PT_ACCESSED_MASK (1ULL << 5)
84#define PT_DIRTY_MASK (1ULL << 6)
85#define PT_PAGE_SIZE_MASK (1ULL << 7)
86#define PT_PAT_MASK (1ULL << 7)
87#define PT_GLOBAL_MASK (1ULL << 8)
Avi Kivityfe135d22007-12-09 16:15:46 +020088#define PT64_NX_SHIFT 63
89#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080090
91#define PT_PAT_SHIFT 7
92#define PT_DIR_PAT_SHIFT 12
93#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
94
95#define PT32_DIR_PSE36_SIZE 4
96#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040097#define PT32_DIR_PSE36_MASK \
98 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080099
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define PT_FIRST_AVAIL_BITS_SHIFT 9
102#define PT64_SECOND_AVAIL_BITS_SHIFT 52
103
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
105
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106#define VALID_PAGE(x) ((x) != INVALID_PAGE)
107
108#define PT64_LEVEL_BITS 9
109
110#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400111 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
113#define PT64_LEVEL_MASK(level) \
114 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
115
116#define PT64_INDEX(address, level)\
117 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
118
119
120#define PT32_LEVEL_BITS 10
121
122#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400123 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
125#define PT32_LEVEL_MASK(level) \
126 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
127
128#define PT32_INDEX(address, level)\
129 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
130
131
Avi Kivity27aba762007-03-09 13:04:31 +0200132#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define PT64_DIR_BASE_ADDR_MASK \
134 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
135
136#define PT32_BASE_ADDR_MASK PAGE_MASK
137#define PT32_DIR_BASE_ADDR_MASK \
138 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
139
Avi Kivity79539ce2007-11-21 02:06:21 +0200140#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
141 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142
143#define PFERR_PRESENT_MASK (1U << 0)
144#define PFERR_WRITE_MASK (1U << 1)
145#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800146#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147
148#define PT64_ROOT_LEVEL 4
149#define PT32_ROOT_LEVEL 2
150#define PT32E_ROOT_LEVEL 3
151
152#define PT_DIRECTORY_LEVEL 2
153#define PT_PAGE_TABLE_LEVEL 1
154
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800155#define RMAP_EXT 4
156
Avi Kivityfe135d22007-12-09 16:15:46 +0200157#define ACC_EXEC_MASK 1
158#define ACC_WRITE_MASK PT_WRITABLE_MASK
159#define ACC_USER_MASK PT_USER_MASK
160#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
161
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800162struct kvm_rmap_desc {
163 u64 *shadow_ptes[RMAP_EXT];
164 struct kvm_rmap_desc *more;
165};
166
Avi Kivityb5a33a72007-04-15 16:31:09 +0300167static struct kmem_cache *pte_chain_cache;
168static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300169static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300170
Avi Kivityc7addb92007-09-16 18:58:32 +0200171static u64 __read_mostly shadow_trap_nonpresent_pte;
172static u64 __read_mostly shadow_notrap_nonpresent_pte;
173
174void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
175{
176 shadow_trap_nonpresent_pte = trap_pte;
177 shadow_notrap_nonpresent_pte = notrap_pte;
178}
179EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
180
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181static int is_write_protection(struct kvm_vcpu *vcpu)
182{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800183 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184}
185
186static int is_cpuid_PSE36(void)
187{
188 return 1;
189}
190
Avi Kivity73b10872007-01-26 00:56:41 -0800191static int is_nx(struct kvm_vcpu *vcpu)
192{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800193 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800194}
195
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196static int is_present_pte(unsigned long pte)
197{
198 return pte & PT_PRESENT_MASK;
199}
200
Avi Kivityc7addb92007-09-16 18:58:32 +0200201static int is_shadow_present_pte(u64 pte)
202{
203 pte &= ~PT_SHADOW_IO_MARK;
204 return pte != shadow_trap_nonpresent_pte
205 && pte != shadow_notrap_nonpresent_pte;
206}
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static int is_writeble_pte(unsigned long pte)
209{
210 return pte & PT_WRITABLE_MASK;
211}
212
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200213static int is_dirty_pte(unsigned long pte)
214{
215 return pte & PT_DIRTY_MASK;
216}
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218static int is_io_pte(unsigned long pte)
219{
220 return pte & PT_SHADOW_IO_MARK;
221}
222
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800223static int is_rmap_pte(u64 pte)
224{
Izik Eidus9647c142007-10-16 14:43:46 +0200225 return pte != shadow_trap_nonpresent_pte
226 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800227}
228
Avi Kivityda9285212007-11-21 13:54:47 +0200229static gfn_t pse36_gfn_delta(u32 gpte)
230{
231 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
232
233 return (gpte & PT32_DIR_PSE36_MASK) << shift;
234}
235
Avi Kivitye663ee62007-05-31 15:46:04 +0300236static void set_shadow_pte(u64 *sptep, u64 spte)
237{
238#ifdef CONFIG_X86_64
239 set_64bit((unsigned long *)sptep, spte);
240#else
241 set_64bit((unsigned long long *)sptep, spte);
242#endif
243}
244
Avi Kivitye2dec932007-01-05 16:36:54 -0800245static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300246 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800247{
248 void *obj;
249
250 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800251 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800252 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300253 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800254 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800255 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800256 cache->objects[cache->nobjs++] = obj;
257 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800258 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800259}
260
261static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
262{
263 while (mc->nobjs)
264 kfree(mc->objects[--mc->nobjs]);
265}
266
Avi Kivityc1158e62007-07-20 08:18:27 +0300267static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300268 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300269{
270 struct page *page;
271
272 if (cache->nobjs >= min)
273 return 0;
274 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300275 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300276 if (!page)
277 return -ENOMEM;
278 set_page_private(page, 0);
279 cache->objects[cache->nobjs++] = page_address(page);
280 }
281 return 0;
282}
283
284static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
285{
286 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300287 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300288}
289
Avi Kivity8c438502007-04-16 11:53:17 +0300290static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
291{
292 int r;
293
Avi Kivity22d95b12007-09-14 20:26:06 +0300294 kvm_mmu_free_some_pages(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800295 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300296 pte_chain_cache, 4);
297 if (r)
298 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800299 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300300 rmap_desc_cache, 1);
301 if (r)
302 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800303 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300304 if (r)
305 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800306 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300307 mmu_page_header_cache, 4);
308out:
Avi Kivity8c438502007-04-16 11:53:17 +0300309 return r;
310}
311
Avi Kivity714b93d2007-01-05 16:36:53 -0800312static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
313{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800314 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
315 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
316 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
317 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800318}
319
320static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
321 size_t size)
322{
323 void *p;
324
325 BUG_ON(!mc->nobjs);
326 p = mc->objects[--mc->nobjs];
327 memset(p, 0, size);
328 return p;
329}
330
Avi Kivity714b93d2007-01-05 16:36:53 -0800331static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
332{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800334 sizeof(struct kvm_pte_chain));
335}
336
Avi Kivity90cb0522007-07-17 13:04:56 +0300337static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800338{
Avi Kivity90cb0522007-07-17 13:04:56 +0300339 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800340}
341
342static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
343{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800344 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800345 sizeof(struct kvm_rmap_desc));
346}
347
Avi Kivity90cb0522007-07-17 13:04:56 +0300348static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800349{
Avi Kivity90cb0522007-07-17 13:04:56 +0300350 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800351}
352
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800353/*
Izik Eidus290fc382007-09-27 14:11:22 +0200354 * Take gfn and return the reverse mapping to it.
355 * Note: gfn must be unaliased before this function get called
356 */
357
358static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
359{
360 struct kvm_memory_slot *slot;
361
362 slot = gfn_to_memslot(kvm, gfn);
363 return &slot->rmap[gfn - slot->base_gfn];
364}
365
366/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800367 * Reverse mapping data structures:
368 *
Izik Eidus290fc382007-09-27 14:11:22 +0200369 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
370 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800371 *
Izik Eidus290fc382007-09-27 14:11:22 +0200372 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
373 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800374 */
Izik Eidus290fc382007-09-27 14:11:22 +0200375static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800376{
Avi Kivity4db35312007-11-21 15:28:32 +0200377 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800378 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200379 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800380 int i;
381
382 if (!is_rmap_pte(*spte))
383 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200384 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200385 sp = page_header(__pa(spte));
386 sp->gfns[spte - sp->spt] = gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200387 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
388 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800389 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200390 *rmapp = (unsigned long)spte;
391 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800392 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800393 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200394 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800395 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200396 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800397 } else {
398 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200399 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800400 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
401 desc = desc->more;
402 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800403 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800404 desc = desc->more;
405 }
406 for (i = 0; desc->shadow_ptes[i]; ++i)
407 ;
408 desc->shadow_ptes[i] = spte;
409 }
410}
411
Izik Eidus290fc382007-09-27 14:11:22 +0200412static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800413 struct kvm_rmap_desc *desc,
414 int i,
415 struct kvm_rmap_desc *prev_desc)
416{
417 int j;
418
419 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
420 ;
421 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000422 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800423 if (j != 0)
424 return;
425 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200426 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800427 else
428 if (prev_desc)
429 prev_desc->more = desc->more;
430 else
Izik Eidus290fc382007-09-27 14:11:22 +0200431 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300432 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800433}
434
Izik Eidus290fc382007-09-27 14:11:22 +0200435static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800436{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800437 struct kvm_rmap_desc *desc;
438 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200439 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200440 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200441 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800442 int i;
443
444 if (!is_rmap_pte(*spte))
445 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200446 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200447 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200448 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200449 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200450 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200451 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200452 kvm_release_page_clean(page);
Avi Kivity4db35312007-11-21 15:28:32 +0200453 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
Izik Eidus290fc382007-09-27 14:11:22 +0200454 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800455 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
456 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200457 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800458 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200459 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800460 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
461 spte, *spte);
462 BUG();
463 }
Izik Eidus290fc382007-09-27 14:11:22 +0200464 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800465 } else {
466 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200467 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800468 prev_desc = NULL;
469 while (desc) {
470 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
471 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200472 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800473 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800474 prev_desc);
475 return;
476 }
477 prev_desc = desc;
478 desc = desc->more;
479 }
480 BUG();
481 }
482}
483
Izik Eidus98348e92007-10-16 14:42:30 +0200484static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800485{
Avi Kivity374cbac2007-01-05 16:36:43 -0800486 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200487 struct kvm_rmap_desc *prev_desc;
488 u64 *prev_spte;
489 int i;
490
491 if (!*rmapp)
492 return NULL;
493 else if (!(*rmapp & 1)) {
494 if (!spte)
495 return (u64 *)*rmapp;
496 return NULL;
497 }
498 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
499 prev_desc = NULL;
500 prev_spte = NULL;
501 while (desc) {
502 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
503 if (prev_spte == spte)
504 return desc->shadow_ptes[i];
505 prev_spte = desc->shadow_ptes[i];
506 }
507 desc = desc->more;
508 }
509 return NULL;
510}
511
512static void rmap_write_protect(struct kvm *kvm, u64 gfn)
513{
Izik Eidus290fc382007-09-27 14:11:22 +0200514 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800515 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800516 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800517
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500518 gfn = unalias_gfn(kvm, gfn);
519 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800520
Izik Eidus98348e92007-10-16 14:42:30 +0200521 spte = rmap_next(kvm, rmapp, NULL);
522 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800523 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800524 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800525 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800526 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200527 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800528 write_protected = 1;
529 }
Izik Eidus9647c142007-10-16 14:43:46 +0200530 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800531 }
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800532 if (write_protected)
533 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800534}
535
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800536#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300537static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538{
Avi Kivity139bdb22007-01-05 16:36:50 -0800539 u64 *pos;
540 u64 *end;
541
Avi Kivity47ad8e62007-05-06 15:50:58 +0300542 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200543 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800544 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
545 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800547 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 return 1;
549}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800550#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551
Avi Kivity4db35312007-11-21 15:28:32 +0200552static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800553{
Avi Kivity4db35312007-11-21 15:28:32 +0200554 ASSERT(is_empty_shadow_page(sp->spt));
555 list_del(&sp->link);
556 __free_page(virt_to_page(sp->spt));
557 __free_page(virt_to_page(sp->gfns));
558 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800559 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800560}
561
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800562static unsigned kvm_page_table_hashfn(gfn_t gfn)
563{
564 return gfn;
565}
566
Avi Kivity25c0de22007-01-05 16:36:42 -0800567static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
568 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569{
Avi Kivity4db35312007-11-21 15:28:32 +0200570 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800572 if (!vcpu->kvm->arch.n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800573 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800575 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
576 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
577 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200578 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800579 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200580 ASSERT(is_empty_shadow_page(sp->spt));
581 sp->slot_bitmap = 0;
582 sp->multimapped = 0;
583 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800584 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200585 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586}
587
Avi Kivity714b93d2007-01-05 16:36:53 -0800588static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200589 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800590{
591 struct kvm_pte_chain *pte_chain;
592 struct hlist_node *node;
593 int i;
594
595 if (!parent_pte)
596 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200597 if (!sp->multimapped) {
598 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800599
600 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200601 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800602 return;
603 }
Avi Kivity4db35312007-11-21 15:28:32 +0200604 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800605 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200606 INIT_HLIST_HEAD(&sp->parent_ptes);
607 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800608 pte_chain->parent_ptes[0] = old;
609 }
Avi Kivity4db35312007-11-21 15:28:32 +0200610 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800611 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
612 continue;
613 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
614 if (!pte_chain->parent_ptes[i]) {
615 pte_chain->parent_ptes[i] = parent_pte;
616 return;
617 }
618 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800619 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800620 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200621 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800622 pte_chain->parent_ptes[0] = parent_pte;
623}
624
Avi Kivity4db35312007-11-21 15:28:32 +0200625static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800626 u64 *parent_pte)
627{
628 struct kvm_pte_chain *pte_chain;
629 struct hlist_node *node;
630 int i;
631
Avi Kivity4db35312007-11-21 15:28:32 +0200632 if (!sp->multimapped) {
633 BUG_ON(sp->parent_pte != parent_pte);
634 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800635 return;
636 }
Avi Kivity4db35312007-11-21 15:28:32 +0200637 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800638 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
639 if (!pte_chain->parent_ptes[i])
640 break;
641 if (pte_chain->parent_ptes[i] != parent_pte)
642 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800643 while (i + 1 < NR_PTE_CHAIN_ENTRIES
644 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800645 pte_chain->parent_ptes[i]
646 = pte_chain->parent_ptes[i + 1];
647 ++i;
648 }
649 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800650 if (i == 0) {
651 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300652 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200653 if (hlist_empty(&sp->parent_ptes)) {
654 sp->multimapped = 0;
655 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800656 }
657 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800658 return;
659 }
660 BUG();
661}
662
Avi Kivity4db35312007-11-21 15:28:32 +0200663static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800664{
665 unsigned index;
666 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200667 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800668 struct hlist_node *node;
669
670 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
671 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800672 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200673 hlist_for_each_entry(sp, node, bucket, hash_link)
674 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800675 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200676 __FUNCTION__, sp->role.word);
677 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800678 }
679 return NULL;
680}
681
682static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
683 gfn_t gfn,
684 gva_t gaddr,
685 unsigned level,
686 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200687 unsigned access,
Marcelo Tosatti78190262007-12-11 19:12:27 -0500688 u64 *parent_pte,
689 bool *new_page)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800690{
691 union kvm_mmu_page_role role;
692 unsigned index;
693 unsigned quadrant;
694 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200695 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800696 struct hlist_node *node;
697
698 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800699 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800700 role.level = level;
701 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200702 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800703 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
705 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
706 role.quadrant = quadrant;
707 }
708 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
709 gfn, role.word);
710 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800711 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200712 hlist_for_each_entry(sp, node, bucket, hash_link)
713 if (sp->gfn == gfn && sp->role.word == role.word) {
714 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800715 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200716 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800717 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200718 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200719 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
720 if (!sp)
721 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800722 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200723 sp->gfn = gfn;
724 sp->role = role;
725 hlist_add_head(&sp->hash_link, bucket);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800726 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800727 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500728 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosatti78190262007-12-11 19:12:27 -0500729 if (new_page)
730 *new_page = 1;
Avi Kivity4db35312007-11-21 15:28:32 +0200731 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800732}
733
Avi Kivity90cb0522007-07-17 13:04:56 +0300734static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200735 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800736{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800737 unsigned i;
738 u64 *pt;
739 u64 ent;
740
Avi Kivity4db35312007-11-21 15:28:32 +0200741 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800742
Avi Kivity4db35312007-11-21 15:28:32 +0200743 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800744 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200745 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200746 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200747 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800748 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300749 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800750 return;
751 }
752
753 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
754 ent = pt[i];
755
Avi Kivityc7addb92007-09-16 18:58:32 +0200756 pt[i] = shadow_trap_nonpresent_pte;
757 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800758 continue;
759 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300760 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800761 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300762 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800763}
764
Avi Kivity4db35312007-11-21 15:28:32 +0200765static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800766{
Avi Kivity4db35312007-11-21 15:28:32 +0200767 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800768}
769
Avi Kivity12b7d282007-09-23 14:10:49 +0200770static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
771{
772 int i;
773
774 for (i = 0; i < KVM_MAX_VCPUS; ++i)
775 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800776 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200777}
778
Avi Kivity4db35312007-11-21 15:28:32 +0200779static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800780{
781 u64 *parent_pte;
782
Avi Kivity4cee5762007-11-18 16:37:07 +0200783 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200784 while (sp->multimapped || sp->parent_pte) {
785 if (!sp->multimapped)
786 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800787 else {
788 struct kvm_pte_chain *chain;
789
Avi Kivity4db35312007-11-21 15:28:32 +0200790 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800791 struct kvm_pte_chain, link);
792 parent_pte = chain->parent_ptes[0];
793 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800794 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200795 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200796 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800797 }
Avi Kivity4db35312007-11-21 15:28:32 +0200798 kvm_mmu_page_unlink_children(kvm, sp);
799 if (!sp->root_count) {
800 hlist_del(&sp->hash_link);
801 kvm_mmu_free_page(kvm, sp);
Avi Kivity36868f72007-03-26 19:31:52 +0200802 } else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800803 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200804 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800805}
806
Izik Eidus82ce2c92007-10-02 18:52:55 +0200807/*
808 * Changing the number of mmu pages allocated to the vm
809 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
810 */
811void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
812{
813 /*
814 * If we set the number of mmu pages to be smaller be than the
815 * number of actived pages , we must to free some mmu pages before we
816 * change the value
817 */
818
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800819 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200820 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800821 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
822 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200823
824 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
825 struct kvm_mmu_page *page;
826
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800827 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200828 struct kvm_mmu_page, link);
829 kvm_mmu_zap_page(kvm, page);
830 n_used_mmu_pages--;
831 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800832 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200833 }
834 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800835 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
836 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200837
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800838 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200839}
840
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500841static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800842{
843 unsigned index;
844 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200845 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800846 struct hlist_node *node, *n;
847 int r;
848
849 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
850 r = 0;
851 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800852 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200853 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
854 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800855 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200856 sp->role.word);
857 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800858 r = 1;
859 }
860 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800861}
862
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500863static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300864{
Avi Kivity4db35312007-11-21 15:28:32 +0200865 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300866
Avi Kivity4db35312007-11-21 15:28:32 +0200867 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
868 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
869 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300870 }
871}
872
Avi Kivity38c335f2007-11-21 14:20:22 +0200873static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874{
Avi Kivity38c335f2007-11-21 14:20:22 +0200875 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200876 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877
Avi Kivity4db35312007-11-21 15:28:32 +0200878 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879}
880
Avi Kivity039576c2007-03-20 12:46:50 +0200881struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
882{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800883 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +0200884
885 if (gpa == UNMAPPED_GVA)
886 return NULL;
Avi Kivity1d28f5f2007-11-21 15:01:44 +0200887 return gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200888}
889
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200890static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
891 unsigned pt_access, unsigned pte_access,
892 int user_fault, int write_fault, int dirty,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200893 int *ptwrite, gfn_t gfn, struct page *page)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200894{
895 u64 spte;
896 int was_rmapped = is_rmap_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200897
Avi Kivitybc750ba2007-12-09 18:39:41 +0200898 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200899 " user_fault %d gfn %lx\n",
Avi Kivitybc750ba2007-12-09 18:39:41 +0200900 __FUNCTION__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200901 write_fault, user_fault, gfn);
902
903 /*
904 * We don't set the accessed bit, since we sometimes want to see
905 * whether the guest actually used the pte (in order to detect
906 * demand paging).
907 */
908 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
909 if (!dirty)
910 pte_access &= ~ACC_WRITE_MASK;
911 if (!(pte_access & ACC_EXEC_MASK))
912 spte |= PT64_NX_MASK;
913
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200914 spte |= PT_PRESENT_MASK;
915 if (pte_access & ACC_USER_MASK)
916 spte |= PT_USER_MASK;
917
918 if (is_error_page(page)) {
919 set_shadow_pte(shadow_pte,
920 shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
921 kvm_release_page_clean(page);
922 return;
923 }
924
925 spte |= page_to_phys(page);
926
927 if ((pte_access & ACC_WRITE_MASK)
928 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
929 struct kvm_mmu_page *shadow;
930
931 spte |= PT_WRITABLE_MASK;
932 if (user_fault) {
933 mmu_unshadow(vcpu->kvm, gfn);
934 goto unshadowed;
935 }
936
937 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
938 if (shadow) {
939 pgprintk("%s: found shadow page for %lx, marking ro\n",
940 __FUNCTION__, gfn);
941 pte_access &= ~ACC_WRITE_MASK;
942 if (is_writeble_pte(spte)) {
943 spte &= ~PT_WRITABLE_MASK;
944 kvm_x86_ops->tlb_flush(vcpu);
945 }
946 if (write_fault)
947 *ptwrite = 1;
948 }
949 }
950
951unshadowed:
952
953 if (pte_access & ACC_WRITE_MASK)
954 mark_page_dirty(vcpu->kvm, gfn);
955
956 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
957 set_shadow_pte(shadow_pte, spte);
958 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
959 if (!was_rmapped) {
960 rmap_add(vcpu, shadow_pte, gfn);
961 if (!is_rmap_pte(*shadow_pte))
962 kvm_release_page_clean(page);
963 }
964 else
965 kvm_release_page_clean(page);
966 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800967 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200968}
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
971{
972}
973
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500974static int __nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write,
975 gfn_t gfn, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976{
977 int level = PT32E_ROOT_LEVEL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800978 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +0200979 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980
981 for (; ; level--) {
982 u32 index = PT64_INDEX(v, level);
983 u64 *table;
984
985 ASSERT(VALID_PAGE(table_addr));
986 table = __va(table_addr);
987
988 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +0200989 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200990 0, write, 1, &pt_write, gfn, page);
Avi Kivitye8332402007-12-09 18:43:00 +0200991 return pt_write || is_io_pte(table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 }
993
Avi Kivityc7addb92007-09-16 18:58:32 +0200994 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800995 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800996 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800998 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
999 >> PAGE_SHIFT;
1000 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1001 v, level - 1,
Marcelo Tosatti78190262007-12-11 19:12:27 -05001002 1, ACC_ALL, &table[index],
1003 NULL);
Avi Kivity25c0de22007-01-05 16:36:42 -08001004 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001006 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 return -ENOMEM;
1008 }
1009
Avi Kivity47ad8e62007-05-06 15:50:58 +03001010 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001011 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 }
1013 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1014 }
1015}
1016
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001017static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1018{
1019 int r;
1020
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001021 struct page *page;
1022
1023 down_read(&current->mm->mmap_sem);
1024 page = gfn_to_page(vcpu->kvm, gfn);
1025
1026 spin_lock(&vcpu->kvm->mmu_lock);
1027 r = __nonpaging_map(vcpu, v, write, gfn, page);
1028 spin_unlock(&vcpu->kvm->mmu_lock);
1029
1030 up_read(&current->mm->mmap_sem);
1031
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001032 return r;
1033}
1034
1035
Avi Kivityc7addb92007-09-16 18:58:32 +02001036static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1037 struct kvm_mmu_page *sp)
1038{
1039 int i;
1040
1041 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1042 sp->spt[i] = shadow_trap_nonpresent_pte;
1043}
1044
Avi Kivity17ac10a2007-01-05 16:36:40 -08001045static void mmu_free_roots(struct kvm_vcpu *vcpu)
1046{
1047 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001048 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001049
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001050 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001051 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001052 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001053#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001054 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1055 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001056
Avi Kivity4db35312007-11-21 15:28:32 +02001057 sp = page_header(root);
1058 --sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001059 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001060 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001061 return;
1062 }
1063#endif
1064 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001065 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001066
Avi Kivity417726a2007-04-12 17:35:58 +03001067 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001068 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001069 sp = page_header(root);
1070 --sp->root_count;
Avi Kivity417726a2007-04-12 17:35:58 +03001071 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001072 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001073 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001074 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001075 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001076}
1077
1078static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1079{
1080 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001081 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001082 struct kvm_mmu_page *sp;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001083
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001084 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001085
1086#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001087 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1088 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001089
1090 ASSERT(!VALID_PAGE(root));
Avi Kivity4db35312007-11-21 15:28:32 +02001091 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Marcelo Tosatti78190262007-12-11 19:12:27 -05001092 PT64_ROOT_LEVEL, 0, ACC_ALL, NULL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001093 root = __pa(sp->spt);
1094 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001095 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001096 return;
1097 }
1098#endif
1099 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001100 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001101
1102 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001103 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1104 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1105 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001106 continue;
1107 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001108 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1109 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001110 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001111 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1112 PT32_ROOT_LEVEL, !is_paging(vcpu),
Marcelo Tosatti78190262007-12-11 19:12:27 -05001113 ACC_ALL, NULL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001114 root = __pa(sp->spt);
1115 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001116 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001117 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001118 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001119}
1120
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1122{
1123 return vaddr;
1124}
1125
1126static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001127 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128{
Avi Kivitye8332402007-12-09 18:43:00 +02001129 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001130 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131
Avi Kivitye8332402007-12-09 18:43:00 +02001132 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001133 r = mmu_topup_memory_caches(vcpu);
1134 if (r)
1135 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001136
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001138 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139
Avi Kivitye8332402007-12-09 18:43:00 +02001140 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Avi Kivitye8332402007-12-09 18:43:00 +02001142 return nonpaging_map(vcpu, gva & PAGE_MASK,
1143 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144}
1145
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146static void nonpaging_free(struct kvm_vcpu *vcpu)
1147{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001148 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149}
1150
1151static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1152{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001153 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154
1155 context->new_cr3 = nonpaging_new_cr3;
1156 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 context->gva_to_gpa = nonpaging_gva_to_gpa;
1158 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001159 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001160 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001162 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 return 0;
1164}
1165
Avi Kivityd835dfe2007-11-21 02:57:59 +02001166void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001168 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001169 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170}
1171
1172static void paging_new_cr3(struct kvm_vcpu *vcpu)
1173{
Avi Kivity374cbac2007-01-05 16:36:43 -08001174 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001175 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176}
1177
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178static void inject_page_fault(struct kvm_vcpu *vcpu,
1179 u64 addr,
1180 u32 err_code)
1181{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001182 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183}
1184
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185static void paging_free(struct kvm_vcpu *vcpu)
1186{
1187 nonpaging_free(vcpu);
1188}
1189
1190#define PTTYPE 64
1191#include "paging_tmpl.h"
1192#undef PTTYPE
1193
1194#define PTTYPE 32
1195#include "paging_tmpl.h"
1196#undef PTTYPE
1197
Avi Kivity17ac10a2007-01-05 16:36:40 -08001198static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001200 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201
1202 ASSERT(is_pae(vcpu));
1203 context->new_cr3 = paging_new_cr3;
1204 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001206 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001208 context->root_level = level;
1209 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001210 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 return 0;
1212}
1213
Avi Kivity17ac10a2007-01-05 16:36:40 -08001214static int paging64_init_context(struct kvm_vcpu *vcpu)
1215{
1216 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1217}
1218
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219static int paging32_init_context(struct kvm_vcpu *vcpu)
1220{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001221 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222
1223 context->new_cr3 = paging_new_cr3;
1224 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 context->gva_to_gpa = paging32_gva_to_gpa;
1226 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001227 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 context->root_level = PT32_ROOT_LEVEL;
1229 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001230 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 return 0;
1232}
1233
1234static int paging32E_init_context(struct kvm_vcpu *vcpu)
1235{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001236 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237}
1238
1239static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1240{
1241 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001242 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243
1244 if (!is_paging(vcpu))
1245 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001246 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247 return paging64_init_context(vcpu);
1248 else if (is_pae(vcpu))
1249 return paging32E_init_context(vcpu);
1250 else
1251 return paging32_init_context(vcpu);
1252}
1253
1254static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1255{
1256 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001257 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1258 vcpu->arch.mmu.free(vcpu);
1259 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 }
1261}
1262
1263int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1264{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001265 destroy_kvm_mmu(vcpu);
1266 return init_kvm_mmu(vcpu);
1267}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001268EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001269
1270int kvm_mmu_load(struct kvm_vcpu *vcpu)
1271{
Avi Kivity714b93d2007-01-05 16:36:53 -08001272 int r;
1273
Avi Kivitye2dec932007-01-05 16:36:54 -08001274 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001275 if (r)
1276 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001277 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001278 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001279 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001280 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001281 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001282out:
1283 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001285EXPORT_SYMBOL_GPL(kvm_mmu_load);
1286
1287void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1288{
1289 mmu_free_roots(vcpu);
1290}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291
Avi Kivity09072da2007-05-01 14:16:52 +03001292static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001293 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001294 u64 *spte)
1295{
1296 u64 pte;
1297 struct kvm_mmu_page *child;
1298
1299 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001300 if (is_shadow_present_pte(pte)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001301 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001302 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001303 else {
1304 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001305 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001306 }
1307 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001308 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001309}
1310
Avi Kivity00284252007-05-01 16:53:31 +03001311static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001312 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001313 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001314 const void *new, int bytes,
1315 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001316{
Avi Kivity4db35312007-11-21 15:28:32 +02001317 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001318 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001319 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001320 }
Avi Kivity00284252007-05-01 16:53:31 +03001321
Avi Kivity4cee5762007-11-18 16:37:07 +02001322 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001323 if (sp->role.glevels == PT32_ROOT_LEVEL)
1324 paging32_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001325 else
Avi Kivity4db35312007-11-21 15:28:32 +02001326 paging64_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001327}
1328
Avi Kivity79539ce2007-11-21 02:06:21 +02001329static bool need_remote_flush(u64 old, u64 new)
1330{
1331 if (!is_shadow_present_pte(old))
1332 return false;
1333 if (!is_shadow_present_pte(new))
1334 return true;
1335 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1336 return true;
1337 old ^= PT64_NX_MASK;
1338 new ^= PT64_NX_MASK;
1339 return (old & ~new & PT64_PERM_MASK) != 0;
1340}
1341
1342static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1343{
1344 if (need_remote_flush(old, new))
1345 kvm_flush_remote_tlbs(vcpu->kvm);
1346 else
1347 kvm_mmu_flush_tlb(vcpu);
1348}
1349
Avi Kivity12b7d282007-09-23 14:10:49 +02001350static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1351{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001352 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001353
1354 return !!(spte && (*spte & PT_ACCESSED_MASK));
1355}
1356
Avi Kivityd7824ff2007-12-30 12:29:05 +02001357static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1358 const u8 *new, int bytes)
1359{
1360 gfn_t gfn;
1361 int r;
1362 u64 gpte = 0;
1363
1364 if (bytes != 4 && bytes != 8)
1365 return;
1366
1367 /*
1368 * Assume that the pte write on a page table of the same type
1369 * as the current vcpu paging mode. This is nearly always true
1370 * (might be false while changing modes). Note it is verified later
1371 * by update_pte().
1372 */
1373 if (is_pae(vcpu)) {
1374 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1375 if ((bytes == 4) && (gpa % 4 == 0)) {
1376 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1377 if (r)
1378 return;
1379 memcpy((void *)&gpte + (gpa % 8), new, 4);
1380 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1381 memcpy((void *)&gpte, new, 8);
1382 }
1383 } else {
1384 if ((bytes == 4) && (gpa % 4 == 0))
1385 memcpy((void *)&gpte, new, 4);
1386 }
1387 if (!is_present_pte(gpte))
1388 return;
1389 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1390 vcpu->arch.update_pte.gfn = gfn;
1391 vcpu->arch.update_pte.page = gfn_to_page(vcpu->kvm, gfn);
1392}
1393
Avi Kivity09072da2007-05-01 14:16:52 +03001394void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life5518812007-07-23 14:51:39 +08001395 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001396{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001397 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001398 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001399 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001400 struct hlist_head *bucket;
1401 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001402 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001403 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001404 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001405 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001406 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001407 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001408 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001409 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001410 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001411 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001412
Avi Kivityda4a00f2007-01-05 16:36:44 -08001413 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001414 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001415 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity4cee5762007-11-18 16:37:07 +02001416 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001417 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001418 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001419 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001420 ++vcpu->arch.last_pt_write_count;
1421 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001422 flooded = 1;
1423 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001424 vcpu->arch.last_pt_write_gfn = gfn;
1425 vcpu->arch.last_pt_write_count = 1;
1426 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001427 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001428 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001429 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001430 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1431 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001432 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001433 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001434 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001435 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001436 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001437 /*
1438 * Misaligned accesses are too much trouble to fix
1439 * up; also, they usually indicate a page is not used
1440 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001441 *
1442 * If we're seeing too many writes to a page,
1443 * it may no longer be a page table, or we may be
1444 * forking, in which case it is better to unmap the
1445 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001446 */
1447 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001448 gpa, bytes, sp->role.word);
1449 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001450 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001451 continue;
1452 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001453 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001454 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001455 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001456 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001457 page_offset <<= 1; /* 32->64 */
1458 /*
1459 * A 32-bit pde maps 4MB while the shadow pdes map
1460 * only 2MB. So we need to double the offset again
1461 * and zap two pdes instead of one.
1462 */
1463 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001464 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001465 page_offset <<= 1;
1466 npte = 2;
1467 }
Avi Kivityfce06572007-05-01 16:44:05 +03001468 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001469 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001470 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001471 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001472 }
Avi Kivity4db35312007-11-21 15:28:32 +02001473 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001474 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001475 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001476 mmu_pte_write_zap_pte(vcpu, sp, spte);
1477 mmu_pte_write_new_pte(vcpu, sp, spte, new, bytes,
Avi Kivityc7addb92007-09-16 18:58:32 +02001478 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001479 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001480 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001481 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001482 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001483 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001484 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001485 if (vcpu->arch.update_pte.page) {
1486 kvm_release_page_clean(vcpu->arch.update_pte.page);
1487 vcpu->arch.update_pte.page = NULL;
1488 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001489}
1490
Avi Kivitya4360362007-01-05 16:36:45 -08001491int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1492{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001493 gpa_t gpa;
1494 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001495
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001496 down_read(&current->mm->mmap_sem);
1497 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1498 up_read(&current->mm->mmap_sem);
1499
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001500 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001501 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001502 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001503 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001504}
1505
Avi Kivity22d95b12007-09-14 20:26:06 +03001506void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001507{
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001508 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001509 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001510 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001511
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001512 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001513 struct kvm_mmu_page, link);
1514 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001515 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001516 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001517 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityebeace82007-01-05 16:36:47 -08001518}
Avi Kivityebeace82007-01-05 16:36:47 -08001519
Avi Kivity30677142007-10-28 18:48:59 +02001520int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1521{
1522 int r;
1523 enum emulation_result er;
1524
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001525 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001526 if (r < 0)
1527 goto out;
1528
1529 if (!r) {
1530 r = 1;
1531 goto out;
1532 }
1533
Avi Kivityb733bfb2007-10-28 18:52:05 +02001534 r = mmu_topup_memory_caches(vcpu);
1535 if (r)
1536 goto out;
1537
Avi Kivity30677142007-10-28 18:48:59 +02001538 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001539
1540 switch (er) {
1541 case EMULATE_DONE:
1542 return 1;
1543 case EMULATE_DO_MMIO:
1544 ++vcpu->stat.mmio_exits;
1545 return 0;
1546 case EMULATE_FAIL:
1547 kvm_report_emulation_failure(vcpu, "pagetable");
1548 return 1;
1549 default:
1550 BUG();
1551 }
1552out:
Avi Kivity30677142007-10-28 18:48:59 +02001553 return r;
1554}
1555EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1556
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557static void free_mmu_pages(struct kvm_vcpu *vcpu)
1558{
Avi Kivity4db35312007-11-21 15:28:32 +02001559 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001561 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1562 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001563 struct kvm_mmu_page, link);
1564 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001565 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001566 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567}
1568
1569static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1570{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001571 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572 int i;
1573
1574 ASSERT(vcpu);
1575
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001576 if (vcpu->kvm->arch.n_requested_mmu_pages)
1577 vcpu->kvm->arch.n_free_mmu_pages =
1578 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001579 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001580 vcpu->kvm->arch.n_free_mmu_pages =
1581 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001582 /*
1583 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1584 * Therefore we need to allocate shadow page tables in the first
1585 * 4GB of memory, which happens to fit the DMA32 zone.
1586 */
1587 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1588 if (!page)
1589 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001590 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001591 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001592 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001593
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 return 0;
1595
1596error_1:
1597 free_mmu_pages(vcpu);
1598 return -ENOMEM;
1599}
1600
Ingo Molnar8018c272006-12-29 16:50:01 -08001601int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001604 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605
Ingo Molnar8018c272006-12-29 16:50:01 -08001606 return alloc_mmu_pages(vcpu);
1607}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608
Ingo Molnar8018c272006-12-29 16:50:01 -08001609int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1610{
1611 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001612 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001613
Ingo Molnar8018c272006-12-29 16:50:01 -08001614 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615}
1616
1617void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1618{
1619 ASSERT(vcpu);
1620
1621 destroy_kvm_mmu(vcpu);
1622 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001623 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624}
1625
Avi Kivity90cb0522007-07-17 13:04:56 +03001626void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627{
Avi Kivity4db35312007-11-21 15:28:32 +02001628 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001630 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 int i;
1632 u64 *pt;
1633
Avi Kivity4db35312007-11-21 15:28:32 +02001634 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 continue;
1636
Avi Kivity4db35312007-11-21 15:28:32 +02001637 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1639 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001640 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642 }
1643}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001644
Avi Kivity90cb0522007-07-17 13:04:56 +03001645void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001646{
Avi Kivity4db35312007-11-21 15:28:32 +02001647 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001648
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001649 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001650 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001651 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001652 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001653
Avi Kivity90cb0522007-07-17 13:04:56 +03001654 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001655}
1656
Avi Kivityb5a33a72007-04-15 16:31:09 +03001657void kvm_mmu_module_exit(void)
1658{
1659 if (pte_chain_cache)
1660 kmem_cache_destroy(pte_chain_cache);
1661 if (rmap_desc_cache)
1662 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001663 if (mmu_page_header_cache)
1664 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001665}
1666
1667int kvm_mmu_module_init(void)
1668{
1669 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1670 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001671 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001672 if (!pte_chain_cache)
1673 goto nomem;
1674 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1675 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001676 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001677 if (!rmap_desc_cache)
1678 goto nomem;
1679
Avi Kivityd3d25b02007-05-30 12:34:53 +03001680 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1681 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001682 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001683 if (!mmu_page_header_cache)
1684 goto nomem;
1685
Avi Kivityb5a33a72007-04-15 16:31:09 +03001686 return 0;
1687
1688nomem:
1689 kvm_mmu_module_exit();
1690 return -ENOMEM;
1691}
1692
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001693/*
1694 * Caculate mmu pages needed for kvm.
1695 */
1696unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1697{
1698 int i;
1699 unsigned int nr_mmu_pages;
1700 unsigned int nr_pages = 0;
1701
1702 for (i = 0; i < kvm->nmemslots; i++)
1703 nr_pages += kvm->memslots[i].npages;
1704
1705 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1706 nr_mmu_pages = max(nr_mmu_pages,
1707 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1708
1709 return nr_mmu_pages;
1710}
1711
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001712#ifdef AUDIT
1713
1714static const char *audit_msg;
1715
1716static gva_t canonicalize(gva_t gva)
1717{
1718#ifdef CONFIG_X86_64
1719 gva = (long long)(gva << 16) >> 16;
1720#endif
1721 return gva;
1722}
1723
1724static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1725 gva_t va, int level)
1726{
1727 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1728 int i;
1729 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1730
1731 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1732 u64 ent = pt[i];
1733
Avi Kivityc7addb92007-09-16 18:58:32 +02001734 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001735 continue;
1736
1737 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001738 if (level > 1) {
1739 if (ent == shadow_notrap_nonpresent_pte)
1740 printk(KERN_ERR "audit: (%s) nontrapping pte"
1741 " in nonleaf level: levels %d gva %lx"
1742 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001743 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02001744
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001745 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001746 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001747 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001748 struct page *page = gpa_to_page(vcpu, gpa);
1749 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001750
Avi Kivityc7addb92007-09-16 18:58:32 +02001751 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001752 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001753 printk(KERN_ERR "xx audit error: (%s) levels %d"
1754 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001755 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001756 va, gpa, hpa, ent,
1757 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001758 else if (ent == shadow_notrap_nonpresent_pte
1759 && !is_error_hpa(hpa))
1760 printk(KERN_ERR "audit: (%s) notrap shadow,"
1761 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001762 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001763
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001764 }
1765 }
1766}
1767
1768static void audit_mappings(struct kvm_vcpu *vcpu)
1769{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001770 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001771
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001772 if (vcpu->arch.mmu.root_level == 4)
1773 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001774 else
1775 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001776 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001777 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001778 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001779 i << 30,
1780 2);
1781}
1782
1783static int count_rmaps(struct kvm_vcpu *vcpu)
1784{
1785 int nmaps = 0;
1786 int i, j, k;
1787
1788 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1789 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1790 struct kvm_rmap_desc *d;
1791
1792 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001793 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001794
Izik Eidus290fc382007-09-27 14:11:22 +02001795 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001796 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001797 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001798 ++nmaps;
1799 continue;
1800 }
Izik Eidus290fc382007-09-27 14:11:22 +02001801 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001802 while (d) {
1803 for (k = 0; k < RMAP_EXT; ++k)
1804 if (d->shadow_ptes[k])
1805 ++nmaps;
1806 else
1807 break;
1808 d = d->more;
1809 }
1810 }
1811 }
1812 return nmaps;
1813}
1814
1815static int count_writable_mappings(struct kvm_vcpu *vcpu)
1816{
1817 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001818 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001819 int i;
1820
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001821 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001822 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001823
Avi Kivity4db35312007-11-21 15:28:32 +02001824 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001825 continue;
1826
1827 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1828 u64 ent = pt[i];
1829
1830 if (!(ent & PT_PRESENT_MASK))
1831 continue;
1832 if (!(ent & PT_WRITABLE_MASK))
1833 continue;
1834 ++nmaps;
1835 }
1836 }
1837 return nmaps;
1838}
1839
1840static void audit_rmap(struct kvm_vcpu *vcpu)
1841{
1842 int n_rmap = count_rmaps(vcpu);
1843 int n_actual = count_writable_mappings(vcpu);
1844
1845 if (n_rmap != n_actual)
1846 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1847 __FUNCTION__, audit_msg, n_rmap, n_actual);
1848}
1849
1850static void audit_write_protection(struct kvm_vcpu *vcpu)
1851{
Avi Kivity4db35312007-11-21 15:28:32 +02001852 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001853 struct kvm_memory_slot *slot;
1854 unsigned long *rmapp;
1855 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001856
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001857 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001858 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001859 continue;
1860
Avi Kivity4db35312007-11-21 15:28:32 +02001861 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1862 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02001863 rmapp = &slot->rmap[gfn - slot->base_gfn];
1864 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001865 printk(KERN_ERR "%s: (%s) shadow page has writable"
1866 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001867 __FUNCTION__, audit_msg, sp->gfn,
1868 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001869 }
1870}
1871
1872static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1873{
1874 int olddbg = dbg;
1875
1876 dbg = 0;
1877 audit_msg = msg;
1878 audit_rmap(vcpu);
1879 audit_write_protection(vcpu);
1880 audit_mappings(vcpu);
1881 dbg = olddbg;
1882}
1883
1884#endif