blob: e55af12e11b7aeb523ff0296d7e525d2f7a33a6d [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{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200225 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800226}
227
Avi Kivityda928522007-11-21 13:54:47 +0200228static gfn_t pse36_gfn_delta(u32 gpte)
229{
230 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
231
232 return (gpte & PT32_DIR_PSE36_MASK) << shift;
233}
234
Avi Kivitye663ee62007-05-31 15:46:04 +0300235static void set_shadow_pte(u64 *sptep, u64 spte)
236{
237#ifdef CONFIG_X86_64
238 set_64bit((unsigned long *)sptep, spte);
239#else
240 set_64bit((unsigned long long *)sptep, spte);
241#endif
242}
243
Avi Kivitye2dec932007-01-05 16:36:54 -0800244static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300245 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800246{
247 void *obj;
248
249 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800250 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800251 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300252 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800253 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800254 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800255 cache->objects[cache->nobjs++] = obj;
256 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800257 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800258}
259
260static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
261{
262 while (mc->nobjs)
263 kfree(mc->objects[--mc->nobjs]);
264}
265
Avi Kivityc1158e62007-07-20 08:18:27 +0300266static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300267 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300268{
269 struct page *page;
270
271 if (cache->nobjs >= min)
272 return 0;
273 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300274 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300275 if (!page)
276 return -ENOMEM;
277 set_page_private(page, 0);
278 cache->objects[cache->nobjs++] = page_address(page);
279 }
280 return 0;
281}
282
283static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
284{
285 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300286 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300287}
288
Avi Kivity8c438502007-04-16 11:53:17 +0300289static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
290{
291 int r;
292
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800293 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300294 pte_chain_cache, 4);
295 if (r)
296 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800297 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300298 rmap_desc_cache, 1);
299 if (r)
300 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800301 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300302 if (r)
303 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800304 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300305 mmu_page_header_cache, 4);
306out:
Avi Kivity8c438502007-04-16 11:53:17 +0300307 return r;
308}
309
Avi Kivity714b93d2007-01-05 16:36:53 -0800310static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
311{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800312 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
313 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
314 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
315 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800316}
317
318static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
319 size_t size)
320{
321 void *p;
322
323 BUG_ON(!mc->nobjs);
324 p = mc->objects[--mc->nobjs];
325 memset(p, 0, size);
326 return p;
327}
328
Avi Kivity714b93d2007-01-05 16:36:53 -0800329static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
330{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800331 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800332 sizeof(struct kvm_pte_chain));
333}
334
Avi Kivity90cb0522007-07-17 13:04:56 +0300335static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800336{
Avi Kivity90cb0522007-07-17 13:04:56 +0300337 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800338}
339
340static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
341{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800343 sizeof(struct kvm_rmap_desc));
344}
345
Avi Kivity90cb0522007-07-17 13:04:56 +0300346static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800347{
Avi Kivity90cb0522007-07-17 13:04:56 +0300348 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800349}
350
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800351/*
Izik Eidus290fc382007-09-27 14:11:22 +0200352 * Take gfn and return the reverse mapping to it.
353 * Note: gfn must be unaliased before this function get called
354 */
355
356static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
357{
358 struct kvm_memory_slot *slot;
359
360 slot = gfn_to_memslot(kvm, gfn);
361 return &slot->rmap[gfn - slot->base_gfn];
362}
363
364/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800365 * Reverse mapping data structures:
366 *
Izik Eidus290fc382007-09-27 14:11:22 +0200367 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
368 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800369 *
Izik Eidus290fc382007-09-27 14:11:22 +0200370 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
371 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 */
Izik Eidus290fc382007-09-27 14:11:22 +0200373static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800374{
Avi Kivity4db35312007-11-21 15:28:32 +0200375 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800376 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200377 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800378 int i;
379
380 if (!is_rmap_pte(*spte))
381 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200382 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200383 sp = page_header(__pa(spte));
384 sp->gfns[spte - sp->spt] = gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200385 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
386 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800387 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200388 *rmapp = (unsigned long)spte;
389 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800390 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800391 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200392 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800393 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200394 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800395 } else {
396 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200397 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800398 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
399 desc = desc->more;
400 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800401 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800402 desc = desc->more;
403 }
404 for (i = 0; desc->shadow_ptes[i]; ++i)
405 ;
406 desc->shadow_ptes[i] = spte;
407 }
408}
409
Izik Eidus290fc382007-09-27 14:11:22 +0200410static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800411 struct kvm_rmap_desc *desc,
412 int i,
413 struct kvm_rmap_desc *prev_desc)
414{
415 int j;
416
417 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
418 ;
419 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000420 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800421 if (j != 0)
422 return;
423 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200424 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800425 else
426 if (prev_desc)
427 prev_desc->more = desc->more;
428 else
Izik Eidus290fc382007-09-27 14:11:22 +0200429 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300430 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800431}
432
Izik Eidus290fc382007-09-27 14:11:22 +0200433static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800434{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800435 struct kvm_rmap_desc *desc;
436 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200437 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200438 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200439 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800440 int i;
441
442 if (!is_rmap_pte(*spte))
443 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200444 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200445 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200446 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200447 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200448 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200449 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200450 kvm_release_page_clean(page);
Avi Kivity4db35312007-11-21 15:28:32 +0200451 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
Izik Eidus290fc382007-09-27 14:11:22 +0200452 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800453 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
454 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200455 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800456 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200457 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800458 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
459 spte, *spte);
460 BUG();
461 }
Izik Eidus290fc382007-09-27 14:11:22 +0200462 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800463 } else {
464 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200465 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800466 prev_desc = NULL;
467 while (desc) {
468 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
469 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200470 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800471 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800472 prev_desc);
473 return;
474 }
475 prev_desc = desc;
476 desc = desc->more;
477 }
478 BUG();
479 }
480}
481
Izik Eidus98348e92007-10-16 14:42:30 +0200482static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800483{
Avi Kivity374cbac2007-01-05 16:36:43 -0800484 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200485 struct kvm_rmap_desc *prev_desc;
486 u64 *prev_spte;
487 int i;
488
489 if (!*rmapp)
490 return NULL;
491 else if (!(*rmapp & 1)) {
492 if (!spte)
493 return (u64 *)*rmapp;
494 return NULL;
495 }
496 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
497 prev_desc = NULL;
498 prev_spte = NULL;
499 while (desc) {
500 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
501 if (prev_spte == spte)
502 return desc->shadow_ptes[i];
503 prev_spte = desc->shadow_ptes[i];
504 }
505 desc = desc->more;
506 }
507 return NULL;
508}
509
510static void rmap_write_protect(struct kvm *kvm, u64 gfn)
511{
Izik Eidus290fc382007-09-27 14:11:22 +0200512 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800513 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800514 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800515
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500516 gfn = unalias_gfn(kvm, gfn);
517 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800518
Izik Eidus98348e92007-10-16 14:42:30 +0200519 spte = rmap_next(kvm, rmapp, NULL);
520 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800521 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800522 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800523 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800524 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200525 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800526 write_protected = 1;
527 }
Izik Eidus9647c142007-10-16 14:43:46 +0200528 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800529 }
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800530 if (write_protected)
531 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800532}
533
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800534#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300535static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536{
Avi Kivity139bdb22007-01-05 16:36:50 -0800537 u64 *pos;
538 u64 *end;
539
Avi Kivity47ad8e62007-05-06 15:50:58 +0300540 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200541 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800542 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
543 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800545 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 return 1;
547}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800548#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549
Avi Kivity4db35312007-11-21 15:28:32 +0200550static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800551{
Avi Kivity4db35312007-11-21 15:28:32 +0200552 ASSERT(is_empty_shadow_page(sp->spt));
553 list_del(&sp->link);
554 __free_page(virt_to_page(sp->spt));
555 __free_page(virt_to_page(sp->gfns));
556 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800557 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800558}
559
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800560static unsigned kvm_page_table_hashfn(gfn_t gfn)
561{
562 return gfn;
563}
564
Avi Kivity25c0de22007-01-05 16:36:42 -0800565static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
566 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567{
Avi Kivity4db35312007-11-21 15:28:32 +0200568 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800570 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
571 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
572 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200573 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800574 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200575 ASSERT(is_empty_shadow_page(sp->spt));
576 sp->slot_bitmap = 0;
577 sp->multimapped = 0;
578 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800579 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200580 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581}
582
Avi Kivity714b93d2007-01-05 16:36:53 -0800583static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200584 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800585{
586 struct kvm_pte_chain *pte_chain;
587 struct hlist_node *node;
588 int i;
589
590 if (!parent_pte)
591 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200592 if (!sp->multimapped) {
593 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800594
595 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200596 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800597 return;
598 }
Avi Kivity4db35312007-11-21 15:28:32 +0200599 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800600 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200601 INIT_HLIST_HEAD(&sp->parent_ptes);
602 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800603 pte_chain->parent_ptes[0] = old;
604 }
Avi Kivity4db35312007-11-21 15:28:32 +0200605 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800606 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
607 continue;
608 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
609 if (!pte_chain->parent_ptes[i]) {
610 pte_chain->parent_ptes[i] = parent_pte;
611 return;
612 }
613 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800614 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800615 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200616 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800617 pte_chain->parent_ptes[0] = parent_pte;
618}
619
Avi Kivity4db35312007-11-21 15:28:32 +0200620static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800621 u64 *parent_pte)
622{
623 struct kvm_pte_chain *pte_chain;
624 struct hlist_node *node;
625 int i;
626
Avi Kivity4db35312007-11-21 15:28:32 +0200627 if (!sp->multimapped) {
628 BUG_ON(sp->parent_pte != parent_pte);
629 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800630 return;
631 }
Avi Kivity4db35312007-11-21 15:28:32 +0200632 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800633 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
634 if (!pte_chain->parent_ptes[i])
635 break;
636 if (pte_chain->parent_ptes[i] != parent_pte)
637 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800638 while (i + 1 < NR_PTE_CHAIN_ENTRIES
639 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800640 pte_chain->parent_ptes[i]
641 = pte_chain->parent_ptes[i + 1];
642 ++i;
643 }
644 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800645 if (i == 0) {
646 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300647 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200648 if (hlist_empty(&sp->parent_ptes)) {
649 sp->multimapped = 0;
650 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800651 }
652 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800653 return;
654 }
655 BUG();
656}
657
Avi Kivity4db35312007-11-21 15:28:32 +0200658static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800659{
660 unsigned index;
661 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200662 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800663 struct hlist_node *node;
664
665 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
666 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800667 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200668 hlist_for_each_entry(sp, node, bucket, hash_link)
669 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800670 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200671 __FUNCTION__, sp->role.word);
672 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800673 }
674 return NULL;
675}
676
677static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
678 gfn_t gfn,
679 gva_t gaddr,
680 unsigned level,
681 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200682 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200683 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800684{
685 union kvm_mmu_page_role role;
686 unsigned index;
687 unsigned quadrant;
688 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200689 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800690 struct hlist_node *node;
691
692 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800693 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800694 role.level = level;
695 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200696 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800697 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800698 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
699 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
700 role.quadrant = quadrant;
701 }
702 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
703 gfn, role.word);
704 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800705 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200706 hlist_for_each_entry(sp, node, bucket, hash_link)
707 if (sp->gfn == gfn && sp->role.word == role.word) {
708 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800709 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200710 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800711 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200712 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200713 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
714 if (!sp)
715 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200717 sp->gfn = gfn;
718 sp->role = role;
719 hlist_add_head(&sp->hash_link, bucket);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800720 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800721 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500722 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200723 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800724}
725
Avi Kivity90cb0522007-07-17 13:04:56 +0300726static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200727 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800728{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800729 unsigned i;
730 u64 *pt;
731 u64 ent;
732
Avi Kivity4db35312007-11-21 15:28:32 +0200733 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734
Avi Kivity4db35312007-11-21 15:28:32 +0200735 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800736 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200737 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200738 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200739 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800740 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300741 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800742 return;
743 }
744
745 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
746 ent = pt[i];
747
Avi Kivityc7addb92007-09-16 18:58:32 +0200748 pt[i] = shadow_trap_nonpresent_pte;
749 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800750 continue;
751 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300752 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800753 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300754 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800755}
756
Avi Kivity4db35312007-11-21 15:28:32 +0200757static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800758{
Avi Kivity4db35312007-11-21 15:28:32 +0200759 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800760}
761
Avi Kivity12b7d282007-09-23 14:10:49 +0200762static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
763{
764 int i;
765
766 for (i = 0; i < KVM_MAX_VCPUS; ++i)
767 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800768 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200769}
770
Avi Kivity4db35312007-11-21 15:28:32 +0200771static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800772{
773 u64 *parent_pte;
774
Avi Kivity4cee5762007-11-18 16:37:07 +0200775 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200776 while (sp->multimapped || sp->parent_pte) {
777 if (!sp->multimapped)
778 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800779 else {
780 struct kvm_pte_chain *chain;
781
Avi Kivity4db35312007-11-21 15:28:32 +0200782 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800783 struct kvm_pte_chain, link);
784 parent_pte = chain->parent_ptes[0];
785 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800786 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200787 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200788 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800789 }
Avi Kivity4db35312007-11-21 15:28:32 +0200790 kvm_mmu_page_unlink_children(kvm, sp);
791 if (!sp->root_count) {
792 hlist_del(&sp->hash_link);
793 kvm_mmu_free_page(kvm, sp);
Avi Kivity36868f72007-03-26 19:31:52 +0200794 } else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800795 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200796 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800797}
798
Izik Eidus82ce2c92007-10-02 18:52:55 +0200799/*
800 * Changing the number of mmu pages allocated to the vm
801 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
802 */
803void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
804{
805 /*
806 * If we set the number of mmu pages to be smaller be than the
807 * number of actived pages , we must to free some mmu pages before we
808 * change the value
809 */
810
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800811 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200812 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800813 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
814 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200815
816 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
817 struct kvm_mmu_page *page;
818
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800819 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200820 struct kvm_mmu_page, link);
821 kvm_mmu_zap_page(kvm, page);
822 n_used_mmu_pages--;
823 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800824 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200825 }
826 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800827 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
828 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200829
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800830 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200831}
832
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500833static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800834{
835 unsigned index;
836 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200837 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800838 struct hlist_node *node, *n;
839 int r;
840
841 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
842 r = 0;
843 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800844 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200845 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
846 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800847 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200848 sp->role.word);
849 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800850 r = 1;
851 }
852 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800853}
854
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500855static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300856{
Avi Kivity4db35312007-11-21 15:28:32 +0200857 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300858
Avi Kivity4db35312007-11-21 15:28:32 +0200859 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
860 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
861 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300862 }
863}
864
Avi Kivity38c335f2007-11-21 14:20:22 +0200865static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866{
Avi Kivity38c335f2007-11-21 14:20:22 +0200867 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200868 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869
Avi Kivity4db35312007-11-21 15:28:32 +0200870 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871}
872
Avi Kivity039576c2007-03-20 12:46:50 +0200873struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
874{
Izik Eidus72dc67a2008-02-10 18:04:15 +0200875 struct page *page;
876
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800877 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +0200878
879 if (gpa == UNMAPPED_GVA)
880 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +0200881
882 down_read(&current->mm->mmap_sem);
883 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
884 up_read(&current->mm->mmap_sem);
885
886 return page;
Avi Kivity039576c2007-03-20 12:46:50 +0200887}
888
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200889static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
890 unsigned pt_access, unsigned pte_access,
891 int user_fault, int write_fault, int dirty,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200892 int *ptwrite, gfn_t gfn, struct page *page)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200893{
894 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -0300895 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +0200896 int was_writeble = is_writeble_pte(*shadow_pte);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -0300897 hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200898
Avi Kivitybc750ba2007-12-09 18:39:41 +0200899 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200900 " user_fault %d gfn %lx\n",
Avi Kivitybc750ba2007-12-09 18:39:41 +0200901 __FUNCTION__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200902 write_fault, user_fault, gfn);
903
Marcelo Tosatti15aaa812008-03-17 10:08:18 -0300904 if (is_rmap_pte(*shadow_pte)) {
905 if (host_pfn != page_to_pfn(page)) {
906 pgprintk("hfn old %lx new %lx\n",
907 host_pfn, page_to_pfn(page));
908 rmap_remove(vcpu->kvm, shadow_pte);
909 }
910 else
911 was_rmapped = 1;
912 }
913
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200914 /*
915 * We don't set the accessed bit, since we sometimes want to see
916 * whether the guest actually used the pte (in order to detect
917 * demand paging).
918 */
919 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
920 if (!dirty)
921 pte_access &= ~ACC_WRITE_MASK;
922 if (!(pte_access & ACC_EXEC_MASK))
923 spte |= PT64_NX_MASK;
924
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200925 spte |= PT_PRESENT_MASK;
926 if (pte_access & ACC_USER_MASK)
927 spte |= PT_USER_MASK;
928
929 if (is_error_page(page)) {
930 set_shadow_pte(shadow_pte,
931 shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
932 kvm_release_page_clean(page);
933 return;
934 }
935
936 spte |= page_to_phys(page);
937
938 if ((pte_access & ACC_WRITE_MASK)
939 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
940 struct kvm_mmu_page *shadow;
941
942 spte |= PT_WRITABLE_MASK;
943 if (user_fault) {
944 mmu_unshadow(vcpu->kvm, gfn);
945 goto unshadowed;
946 }
947
948 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
949 if (shadow) {
950 pgprintk("%s: found shadow page for %lx, marking ro\n",
951 __FUNCTION__, gfn);
952 pte_access &= ~ACC_WRITE_MASK;
953 if (is_writeble_pte(spte)) {
954 spte &= ~PT_WRITABLE_MASK;
955 kvm_x86_ops->tlb_flush(vcpu);
956 }
957 if (write_fault)
958 *ptwrite = 1;
959 }
960 }
961
962unshadowed:
963
964 if (pte_access & ACC_WRITE_MASK)
965 mark_page_dirty(vcpu->kvm, gfn);
966
967 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
968 set_shadow_pte(shadow_pte, spte);
969 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
970 if (!was_rmapped) {
971 rmap_add(vcpu, shadow_pte, gfn);
972 if (!is_rmap_pte(*shadow_pte))
973 kvm_release_page_clean(page);
Izik Eidus75e68e62008-01-12 23:49:09 +0200974 } else {
975 if (was_writeble)
976 kvm_release_page_dirty(page);
977 else
978 kvm_release_page_clean(page);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200979 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200980 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800981 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200982}
983
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
985{
986}
987
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500988static int __nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write,
989 gfn_t gfn, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990{
991 int level = PT32E_ROOT_LEVEL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800992 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +0200993 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994
995 for (; ; level--) {
996 u32 index = PT64_INDEX(v, level);
997 u64 *table;
998
999 ASSERT(VALID_PAGE(table_addr));
1000 table = __va(table_addr);
1001
1002 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001003 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Avi Kivityd7824ff2007-12-30 12:29:05 +02001004 0, write, 1, &pt_write, gfn, page);
Avi Kivitye8332402007-12-09 18:43:00 +02001005 return pt_write || is_io_pte(table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 }
1007
Avi Kivityc7addb92007-09-16 18:58:32 +02001008 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001009 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001010 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001012 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1013 >> PAGE_SHIFT;
1014 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1015 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001016 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001017 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001019 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020 return -ENOMEM;
1021 }
1022
Avi Kivity47ad8e62007-05-06 15:50:58 +03001023 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001024 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 }
1026 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1027 }
1028}
1029
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001030static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1031{
1032 int r;
1033
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001034 struct page *page;
1035
Izik Eidus72dc67a2008-02-10 18:04:15 +02001036 down_read(&vcpu->kvm->slots_lock);
1037
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001038 down_read(&current->mm->mmap_sem);
1039 page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001040 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001041
1042 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001043 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001044 r = __nonpaging_map(vcpu, v, write, gfn, page);
1045 spin_unlock(&vcpu->kvm->mmu_lock);
1046
Izik Eidus72dc67a2008-02-10 18:04:15 +02001047 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001048
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001049 return r;
1050}
1051
1052
Avi Kivityc7addb92007-09-16 18:58:32 +02001053static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1054 struct kvm_mmu_page *sp)
1055{
1056 int i;
1057
1058 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1059 sp->spt[i] = shadow_trap_nonpresent_pte;
1060}
1061
Avi Kivity17ac10a2007-01-05 16:36:40 -08001062static void mmu_free_roots(struct kvm_vcpu *vcpu)
1063{
1064 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001065 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001066
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001067 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001068 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001069 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001070#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001071 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1072 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001073
Avi Kivity4db35312007-11-21 15:28:32 +02001074 sp = page_header(root);
1075 --sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001076 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001077 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001078 return;
1079 }
1080#endif
1081 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001082 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001083
Avi Kivity417726a2007-04-12 17:35:58 +03001084 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001085 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001086 sp = page_header(root);
1087 --sp->root_count;
Avi Kivity417726a2007-04-12 17:35:58 +03001088 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001089 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001090 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001091 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001092 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001093}
1094
1095static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1096{
1097 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001098 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001099 struct kvm_mmu_page *sp;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001100
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001101 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001102
1103#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001104 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1105 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001106
1107 ASSERT(!VALID_PAGE(root));
Avi Kivity4db35312007-11-21 15:28:32 +02001108 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001109 PT64_ROOT_LEVEL, 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001110 root = __pa(sp->spt);
1111 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001112 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001113 return;
1114 }
1115#endif
1116 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001117 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001118
1119 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001120 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1121 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1122 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001123 continue;
1124 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001125 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1126 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001127 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001128 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1129 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001130 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001131 root = __pa(sp->spt);
1132 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001133 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001134 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001135 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001136}
1137
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1139{
1140 return vaddr;
1141}
1142
1143static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001144 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145{
Avi Kivitye8332402007-12-09 18:43:00 +02001146 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001147 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148
Avi Kivitye8332402007-12-09 18:43:00 +02001149 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001150 r = mmu_topup_memory_caches(vcpu);
1151 if (r)
1152 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001153
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001155 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
Avi Kivitye8332402007-12-09 18:43:00 +02001157 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158
Avi Kivitye8332402007-12-09 18:43:00 +02001159 return nonpaging_map(vcpu, gva & PAGE_MASK,
1160 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161}
1162
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163static void nonpaging_free(struct kvm_vcpu *vcpu)
1164{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001165 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166}
1167
1168static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1169{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001170 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171
1172 context->new_cr3 = nonpaging_new_cr3;
1173 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174 context->gva_to_gpa = nonpaging_gva_to_gpa;
1175 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001176 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001177 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001179 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 return 0;
1181}
1182
Avi Kivityd835dfe2007-11-21 02:57:59 +02001183void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001185 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001186 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187}
1188
1189static void paging_new_cr3(struct kvm_vcpu *vcpu)
1190{
Marcelo Tosatti24993d52008-02-14 21:25:39 -02001191 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001192 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193}
1194
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195static void inject_page_fault(struct kvm_vcpu *vcpu,
1196 u64 addr,
1197 u32 err_code)
1198{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001199 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200}
1201
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202static void paging_free(struct kvm_vcpu *vcpu)
1203{
1204 nonpaging_free(vcpu);
1205}
1206
1207#define PTTYPE 64
1208#include "paging_tmpl.h"
1209#undef PTTYPE
1210
1211#define PTTYPE 32
1212#include "paging_tmpl.h"
1213#undef PTTYPE
1214
Avi Kivity17ac10a2007-01-05 16:36:40 -08001215static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001217 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218
1219 ASSERT(is_pae(vcpu));
1220 context->new_cr3 = paging_new_cr3;
1221 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001223 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001225 context->root_level = level;
1226 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001227 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 return 0;
1229}
1230
Avi Kivity17ac10a2007-01-05 16:36:40 -08001231static int paging64_init_context(struct kvm_vcpu *vcpu)
1232{
1233 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1234}
1235
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236static int paging32_init_context(struct kvm_vcpu *vcpu)
1237{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001238 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239
1240 context->new_cr3 = paging_new_cr3;
1241 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242 context->gva_to_gpa = paging32_gva_to_gpa;
1243 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001244 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 context->root_level = PT32_ROOT_LEVEL;
1246 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001247 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 return 0;
1249}
1250
1251static int paging32E_init_context(struct kvm_vcpu *vcpu)
1252{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001253 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254}
1255
1256static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1257{
1258 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
1261 if (!is_paging(vcpu))
1262 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001263 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 return paging64_init_context(vcpu);
1265 else if (is_pae(vcpu))
1266 return paging32E_init_context(vcpu);
1267 else
1268 return paging32_init_context(vcpu);
1269}
1270
1271static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1272{
1273 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001274 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1275 vcpu->arch.mmu.free(vcpu);
1276 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 }
1278}
1279
1280int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1281{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001282 destroy_kvm_mmu(vcpu);
1283 return init_kvm_mmu(vcpu);
1284}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001285EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001286
1287int kvm_mmu_load(struct kvm_vcpu *vcpu)
1288{
Avi Kivity714b93d2007-01-05 16:36:53 -08001289 int r;
1290
Avi Kivitye2dec932007-01-05 16:36:54 -08001291 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001292 if (r)
1293 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001294 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001295 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001296 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001297 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001298 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001299 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001300out:
1301 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001303EXPORT_SYMBOL_GPL(kvm_mmu_load);
1304
1305void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1306{
1307 mmu_free_roots(vcpu);
1308}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309
Avi Kivity09072da2007-05-01 14:16:52 +03001310static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001311 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001312 u64 *spte)
1313{
1314 u64 pte;
1315 struct kvm_mmu_page *child;
1316
1317 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001318 if (is_shadow_present_pte(pte)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001319 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001320 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001321 else {
1322 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001323 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001324 }
1325 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001326 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001327}
1328
Avi Kivity00284252007-05-01 16:53:31 +03001329static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001330 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001331 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001332 const void *new, int bytes,
1333 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001334{
Avi Kivity4db35312007-11-21 15:28:32 +02001335 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001336 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001337 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001338 }
Avi Kivity00284252007-05-01 16:53:31 +03001339
Avi Kivity4cee5762007-11-18 16:37:07 +02001340 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001341 if (sp->role.glevels == PT32_ROOT_LEVEL)
1342 paging32_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001343 else
Avi Kivity4db35312007-11-21 15:28:32 +02001344 paging64_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001345}
1346
Avi Kivity79539ce2007-11-21 02:06:21 +02001347static bool need_remote_flush(u64 old, u64 new)
1348{
1349 if (!is_shadow_present_pte(old))
1350 return false;
1351 if (!is_shadow_present_pte(new))
1352 return true;
1353 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1354 return true;
1355 old ^= PT64_NX_MASK;
1356 new ^= PT64_NX_MASK;
1357 return (old & ~new & PT64_PERM_MASK) != 0;
1358}
1359
1360static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1361{
1362 if (need_remote_flush(old, new))
1363 kvm_flush_remote_tlbs(vcpu->kvm);
1364 else
1365 kvm_mmu_flush_tlb(vcpu);
1366}
1367
Avi Kivity12b7d282007-09-23 14:10:49 +02001368static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001370 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001371
1372 return !!(spte && (*spte & PT_ACCESSED_MASK));
1373}
1374
Avi Kivityd7824ff2007-12-30 12:29:05 +02001375static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1376 const u8 *new, int bytes)
1377{
1378 gfn_t gfn;
1379 int r;
1380 u64 gpte = 0;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001381 struct page *page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001382
1383 if (bytes != 4 && bytes != 8)
1384 return;
1385
1386 /*
1387 * Assume that the pte write on a page table of the same type
1388 * as the current vcpu paging mode. This is nearly always true
1389 * (might be false while changing modes). Note it is verified later
1390 * by update_pte().
1391 */
1392 if (is_pae(vcpu)) {
1393 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1394 if ((bytes == 4) && (gpa % 4 == 0)) {
1395 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1396 if (r)
1397 return;
1398 memcpy((void *)&gpte + (gpa % 8), new, 4);
1399 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1400 memcpy((void *)&gpte, new, 8);
1401 }
1402 } else {
1403 if ((bytes == 4) && (gpa % 4 == 0))
1404 memcpy((void *)&gpte, new, 4);
1405 }
1406 if (!is_present_pte(gpte))
1407 return;
1408 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001409
1410 down_read(&current->mm->mmap_sem);
1411 page = gfn_to_page(vcpu->kvm, gfn);
1412 up_read(&current->mm->mmap_sem);
1413
Avi Kivityd7824ff2007-12-30 12:29:05 +02001414 vcpu->arch.update_pte.gfn = gfn;
Avi Kivitye48bb492008-03-23 14:21:08 +02001415 vcpu->arch.update_pte.page = page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001416}
1417
Avi Kivity09072da2007-05-01 14:16:52 +03001418void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001419 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001420{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001421 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001422 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001423 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001424 struct hlist_head *bucket;
1425 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001426 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001427 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001428 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001429 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001430 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001431 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001432 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001433 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001434 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001435 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001436
Avi Kivityda4a00f2007-01-05 16:36:44 -08001437 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001438 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001439 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001440 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001441 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001442 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001443 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001444 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001445 ++vcpu->arch.last_pt_write_count;
1446 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001447 flooded = 1;
1448 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001449 vcpu->arch.last_pt_write_gfn = gfn;
1450 vcpu->arch.last_pt_write_count = 1;
1451 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001452 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001453 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001454 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001455 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1456 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001457 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001458 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001459 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001460 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001461 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001462 /*
1463 * Misaligned accesses are too much trouble to fix
1464 * up; also, they usually indicate a page is not used
1465 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001466 *
1467 * If we're seeing too many writes to a page,
1468 * it may no longer be a page table, or we may be
1469 * forking, in which case it is better to unmap the
1470 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001471 */
1472 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001473 gpa, bytes, sp->role.word);
1474 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001475 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001476 continue;
1477 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001478 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001479 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001480 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001481 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001482 page_offset <<= 1; /* 32->64 */
1483 /*
1484 * A 32-bit pde maps 4MB while the shadow pdes map
1485 * only 2MB. So we need to double the offset again
1486 * and zap two pdes instead of one.
1487 */
1488 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001489 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001490 page_offset <<= 1;
1491 npte = 2;
1492 }
Avi Kivityfce06572007-05-01 16:44:05 +03001493 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001494 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001495 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001496 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001497 }
Avi Kivity4db35312007-11-21 15:28:32 +02001498 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001499 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001500 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001501 mmu_pte_write_zap_pte(vcpu, sp, spte);
1502 mmu_pte_write_new_pte(vcpu, sp, spte, new, bytes,
Avi Kivityc7addb92007-09-16 18:58:32 +02001503 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001504 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001505 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001506 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001507 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001508 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001509 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001510 if (vcpu->arch.update_pte.page) {
1511 kvm_release_page_clean(vcpu->arch.update_pte.page);
1512 vcpu->arch.update_pte.page = NULL;
1513 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001514}
1515
Avi Kivitya4360362007-01-05 16:36:45 -08001516int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1517{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001518 gpa_t gpa;
1519 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001520
Izik Eidus72dc67a2008-02-10 18:04:15 +02001521 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001522 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001523 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001524
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001525 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001526 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001527 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001528 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001529}
1530
Avi Kivity22d95b12007-09-14 20:26:06 +03001531void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001532{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001533 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001534 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001535
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001536 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001537 struct kvm_mmu_page, link);
1538 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001539 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001540 }
1541}
Avi Kivityebeace82007-01-05 16:36:47 -08001542
Avi Kivity30677142007-10-28 18:48:59 +02001543int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1544{
1545 int r;
1546 enum emulation_result er;
1547
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001548 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001549 if (r < 0)
1550 goto out;
1551
1552 if (!r) {
1553 r = 1;
1554 goto out;
1555 }
1556
Avi Kivityb733bfb2007-10-28 18:52:05 +02001557 r = mmu_topup_memory_caches(vcpu);
1558 if (r)
1559 goto out;
1560
Avi Kivity30677142007-10-28 18:48:59 +02001561 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001562
1563 switch (er) {
1564 case EMULATE_DONE:
1565 return 1;
1566 case EMULATE_DO_MMIO:
1567 ++vcpu->stat.mmio_exits;
1568 return 0;
1569 case EMULATE_FAIL:
1570 kvm_report_emulation_failure(vcpu, "pagetable");
1571 return 1;
1572 default:
1573 BUG();
1574 }
1575out:
Avi Kivity30677142007-10-28 18:48:59 +02001576 return r;
1577}
1578EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1579
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580static void free_mmu_pages(struct kvm_vcpu *vcpu)
1581{
Avi Kivity4db35312007-11-21 15:28:32 +02001582 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001584 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1585 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001586 struct kvm_mmu_page, link);
1587 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001588 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001589 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590}
1591
1592static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1593{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001594 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 int i;
1596
1597 ASSERT(vcpu);
1598
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001599 if (vcpu->kvm->arch.n_requested_mmu_pages)
1600 vcpu->kvm->arch.n_free_mmu_pages =
1601 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001602 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001603 vcpu->kvm->arch.n_free_mmu_pages =
1604 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001605 /*
1606 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1607 * Therefore we need to allocate shadow page tables in the first
1608 * 4GB of memory, which happens to fit the DMA32 zone.
1609 */
1610 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1611 if (!page)
1612 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001613 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001614 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001615 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001616
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617 return 0;
1618
1619error_1:
1620 free_mmu_pages(vcpu);
1621 return -ENOMEM;
1622}
1623
Ingo Molnar8018c272006-12-29 16:50:01 -08001624int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001627 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628
Ingo Molnar8018c272006-12-29 16:50:01 -08001629 return alloc_mmu_pages(vcpu);
1630}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631
Ingo Molnar8018c272006-12-29 16:50:01 -08001632int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1633{
1634 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001635 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001636
Ingo Molnar8018c272006-12-29 16:50:01 -08001637 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638}
1639
1640void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1641{
1642 ASSERT(vcpu);
1643
1644 destroy_kvm_mmu(vcpu);
1645 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001646 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647}
1648
Avi Kivity90cb0522007-07-17 13:04:56 +03001649void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650{
Avi Kivity4db35312007-11-21 15:28:32 +02001651 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001653 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654 int i;
1655 u64 *pt;
1656
Avi Kivity4db35312007-11-21 15:28:32 +02001657 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 continue;
1659
Avi Kivity4db35312007-11-21 15:28:32 +02001660 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1662 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001663 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 }
1666}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001667
Avi Kivity90cb0522007-07-17 13:04:56 +03001668void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001669{
Avi Kivity4db35312007-11-21 15:28:32 +02001670 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001671
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001672 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001673 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001674 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001675 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001676
Avi Kivity90cb0522007-07-17 13:04:56 +03001677 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001678}
1679
Avi Kivityb5a33a72007-04-15 16:31:09 +03001680void kvm_mmu_module_exit(void)
1681{
1682 if (pte_chain_cache)
1683 kmem_cache_destroy(pte_chain_cache);
1684 if (rmap_desc_cache)
1685 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001686 if (mmu_page_header_cache)
1687 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001688}
1689
1690int kvm_mmu_module_init(void)
1691{
1692 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1693 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001694 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001695 if (!pte_chain_cache)
1696 goto nomem;
1697 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1698 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001699 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001700 if (!rmap_desc_cache)
1701 goto nomem;
1702
Avi Kivityd3d25b02007-05-30 12:34:53 +03001703 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1704 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001705 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001706 if (!mmu_page_header_cache)
1707 goto nomem;
1708
Avi Kivityb5a33a72007-04-15 16:31:09 +03001709 return 0;
1710
1711nomem:
1712 kvm_mmu_module_exit();
1713 return -ENOMEM;
1714}
1715
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001716/*
1717 * Caculate mmu pages needed for kvm.
1718 */
1719unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1720{
1721 int i;
1722 unsigned int nr_mmu_pages;
1723 unsigned int nr_pages = 0;
1724
1725 for (i = 0; i < kvm->nmemslots; i++)
1726 nr_pages += kvm->memslots[i].npages;
1727
1728 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1729 nr_mmu_pages = max(nr_mmu_pages,
1730 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1731
1732 return nr_mmu_pages;
1733}
1734
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001735#ifdef AUDIT
1736
1737static const char *audit_msg;
1738
1739static gva_t canonicalize(gva_t gva)
1740{
1741#ifdef CONFIG_X86_64
1742 gva = (long long)(gva << 16) >> 16;
1743#endif
1744 return gva;
1745}
1746
1747static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1748 gva_t va, int level)
1749{
1750 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1751 int i;
1752 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1753
1754 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1755 u64 ent = pt[i];
1756
Avi Kivityc7addb92007-09-16 18:58:32 +02001757 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001758 continue;
1759
1760 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001761 if (level > 1) {
1762 if (ent == shadow_notrap_nonpresent_pte)
1763 printk(KERN_ERR "audit: (%s) nontrapping pte"
1764 " in nonleaf level: levels %d gva %lx"
1765 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001766 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02001767
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001768 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001769 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001770 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001771 struct page *page = gpa_to_page(vcpu, gpa);
1772 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001773
Avi Kivityc7addb92007-09-16 18:58:32 +02001774 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001775 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001776 printk(KERN_ERR "xx audit error: (%s) levels %d"
1777 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001778 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001779 va, gpa, hpa, ent,
1780 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001781 else if (ent == shadow_notrap_nonpresent_pte
1782 && !is_error_hpa(hpa))
1783 printk(KERN_ERR "audit: (%s) notrap shadow,"
1784 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001785 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001786
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001787 }
1788 }
1789}
1790
1791static void audit_mappings(struct kvm_vcpu *vcpu)
1792{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001793 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001794
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001795 if (vcpu->arch.mmu.root_level == 4)
1796 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001797 else
1798 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001799 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001800 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001801 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001802 i << 30,
1803 2);
1804}
1805
1806static int count_rmaps(struct kvm_vcpu *vcpu)
1807{
1808 int nmaps = 0;
1809 int i, j, k;
1810
1811 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1812 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1813 struct kvm_rmap_desc *d;
1814
1815 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001816 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001817
Izik Eidus290fc382007-09-27 14:11:22 +02001818 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001819 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001820 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001821 ++nmaps;
1822 continue;
1823 }
Izik Eidus290fc382007-09-27 14:11:22 +02001824 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001825 while (d) {
1826 for (k = 0; k < RMAP_EXT; ++k)
1827 if (d->shadow_ptes[k])
1828 ++nmaps;
1829 else
1830 break;
1831 d = d->more;
1832 }
1833 }
1834 }
1835 return nmaps;
1836}
1837
1838static int count_writable_mappings(struct kvm_vcpu *vcpu)
1839{
1840 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001841 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001842 int i;
1843
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001844 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001845 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001846
Avi Kivity4db35312007-11-21 15:28:32 +02001847 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001848 continue;
1849
1850 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1851 u64 ent = pt[i];
1852
1853 if (!(ent & PT_PRESENT_MASK))
1854 continue;
1855 if (!(ent & PT_WRITABLE_MASK))
1856 continue;
1857 ++nmaps;
1858 }
1859 }
1860 return nmaps;
1861}
1862
1863static void audit_rmap(struct kvm_vcpu *vcpu)
1864{
1865 int n_rmap = count_rmaps(vcpu);
1866 int n_actual = count_writable_mappings(vcpu);
1867
1868 if (n_rmap != n_actual)
1869 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1870 __FUNCTION__, audit_msg, n_rmap, n_actual);
1871}
1872
1873static void audit_write_protection(struct kvm_vcpu *vcpu)
1874{
Avi Kivity4db35312007-11-21 15:28:32 +02001875 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001876 struct kvm_memory_slot *slot;
1877 unsigned long *rmapp;
1878 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001879
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001880 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001881 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001882 continue;
1883
Avi Kivity4db35312007-11-21 15:28:32 +02001884 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1885 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02001886 rmapp = &slot->rmap[gfn - slot->base_gfn];
1887 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001888 printk(KERN_ERR "%s: (%s) shadow page has writable"
1889 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001890 __FUNCTION__, audit_msg, sp->gfn,
1891 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001892 }
1893}
1894
1895static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1896{
1897 int olddbg = dbg;
1898
1899 dbg = 0;
1900 audit_msg = msg;
1901 audit_rmap(vcpu);
1902 audit_write_protection(vcpu);
1903 audit_mappings(vcpu);
1904 dbg = olddbg;
1905}
1906
1907#endif