blob: 6651dfadae500b106156fcb040a356c83a3a17a8 [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 VALID_PAGE(x) ((x) != INVALID_PAGE)
105
106#define PT64_LEVEL_BITS 9
107
108#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400109 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110
111#define PT64_LEVEL_MASK(level) \
112 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
113
114#define PT64_INDEX(address, level)\
115 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
116
117
118#define PT32_LEVEL_BITS 10
119
120#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400121 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
123#define PT32_LEVEL_MASK(level) \
124 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
125
126#define PT32_INDEX(address, level)\
127 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
128
129
Avi Kivity27aba762007-03-09 13:04:31 +0200130#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define PT64_DIR_BASE_ADDR_MASK \
132 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
133
134#define PT32_BASE_ADDR_MASK PAGE_MASK
135#define PT32_DIR_BASE_ADDR_MASK \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
137
Avi Kivity79539ce2007-11-21 02:06:21 +0200138#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
139 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PFERR_PRESENT_MASK (1U << 0)
142#define PFERR_WRITE_MASK (1U << 1)
143#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800144#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
146#define PT64_ROOT_LEVEL 4
147#define PT32_ROOT_LEVEL 2
148#define PT32E_ROOT_LEVEL 3
149
150#define PT_DIRECTORY_LEVEL 2
151#define PT_PAGE_TABLE_LEVEL 1
152
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800153#define RMAP_EXT 4
154
Avi Kivityfe135d22007-12-09 16:15:46 +0200155#define ACC_EXEC_MASK 1
156#define ACC_WRITE_MASK PT_WRITABLE_MASK
157#define ACC_USER_MASK PT_USER_MASK
158#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
159
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160struct kvm_rmap_desc {
161 u64 *shadow_ptes[RMAP_EXT];
162 struct kvm_rmap_desc *more;
163};
164
Avi Kivityb5a33a72007-04-15 16:31:09 +0300165static struct kmem_cache *pte_chain_cache;
166static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300167static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300168
Avi Kivityc7addb92007-09-16 18:58:32 +0200169static u64 __read_mostly shadow_trap_nonpresent_pte;
170static u64 __read_mostly shadow_notrap_nonpresent_pte;
171
172void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
173{
174 shadow_trap_nonpresent_pte = trap_pte;
175 shadow_notrap_nonpresent_pte = notrap_pte;
176}
177EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
178
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179static int is_write_protection(struct kvm_vcpu *vcpu)
180{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800181 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182}
183
184static int is_cpuid_PSE36(void)
185{
186 return 1;
187}
188
Avi Kivity73b10872007-01-26 00:56:41 -0800189static int is_nx(struct kvm_vcpu *vcpu)
190{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800191 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800192}
193
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194static int is_present_pte(unsigned long pte)
195{
196 return pte & PT_PRESENT_MASK;
197}
198
Avi Kivityc7addb92007-09-16 18:58:32 +0200199static int is_shadow_present_pte(u64 pte)
200{
Avi Kivityc7addb92007-09-16 18:58:32 +0200201 return pte != shadow_trap_nonpresent_pte
202 && pte != shadow_notrap_nonpresent_pte;
203}
204
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205static int is_writeble_pte(unsigned long pte)
206{
207 return pte & PT_WRITABLE_MASK;
208}
209
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200210static int is_dirty_pte(unsigned long pte)
211{
212 return pte & PT_DIRTY_MASK;
213}
214
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800215static int is_rmap_pte(u64 pte)
216{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200217 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800218}
219
Avi Kivityda928522007-11-21 13:54:47 +0200220static gfn_t pse36_gfn_delta(u32 gpte)
221{
222 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
223
224 return (gpte & PT32_DIR_PSE36_MASK) << shift;
225}
226
Avi Kivitye663ee62007-05-31 15:46:04 +0300227static void set_shadow_pte(u64 *sptep, u64 spte)
228{
229#ifdef CONFIG_X86_64
230 set_64bit((unsigned long *)sptep, spte);
231#else
232 set_64bit((unsigned long long *)sptep, spte);
233#endif
234}
235
Avi Kivitye2dec932007-01-05 16:36:54 -0800236static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300237 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800238{
239 void *obj;
240
241 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800242 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800243 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300244 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800245 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800246 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800247 cache->objects[cache->nobjs++] = obj;
248 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800249 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800250}
251
252static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
253{
254 while (mc->nobjs)
255 kfree(mc->objects[--mc->nobjs]);
256}
257
Avi Kivityc1158e62007-07-20 08:18:27 +0300258static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300259 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300260{
261 struct page *page;
262
263 if (cache->nobjs >= min)
264 return 0;
265 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300266 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300267 if (!page)
268 return -ENOMEM;
269 set_page_private(page, 0);
270 cache->objects[cache->nobjs++] = page_address(page);
271 }
272 return 0;
273}
274
275static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
276{
277 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300278 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300279}
280
Avi Kivity8c438502007-04-16 11:53:17 +0300281static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
282{
283 int r;
284
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800285 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300286 pte_chain_cache, 4);
287 if (r)
288 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800289 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300290 rmap_desc_cache, 1);
291 if (r)
292 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800293 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300294 if (r)
295 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800296 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300297 mmu_page_header_cache, 4);
298out:
Avi Kivity8c438502007-04-16 11:53:17 +0300299 return r;
300}
301
Avi Kivity714b93d2007-01-05 16:36:53 -0800302static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
303{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800304 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
305 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
306 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
307 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800308}
309
310static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
311 size_t size)
312{
313 void *p;
314
315 BUG_ON(!mc->nobjs);
316 p = mc->objects[--mc->nobjs];
317 memset(p, 0, size);
318 return p;
319}
320
Avi Kivity714b93d2007-01-05 16:36:53 -0800321static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
322{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800323 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800324 sizeof(struct kvm_pte_chain));
325}
326
Avi Kivity90cb0522007-07-17 13:04:56 +0300327static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800328{
Avi Kivity90cb0522007-07-17 13:04:56 +0300329 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800330}
331
332static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
333{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800334 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800335 sizeof(struct kvm_rmap_desc));
336}
337
Avi Kivity90cb0522007-07-17 13:04:56 +0300338static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800339{
Avi Kivity90cb0522007-07-17 13:04:56 +0300340 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800341}
342
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800343/*
Izik Eidus290fc382007-09-27 14:11:22 +0200344 * Take gfn and return the reverse mapping to it.
345 * Note: gfn must be unaliased before this function get called
346 */
347
348static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
349{
350 struct kvm_memory_slot *slot;
351
352 slot = gfn_to_memslot(kvm, gfn);
353 return &slot->rmap[gfn - slot->base_gfn];
354}
355
356/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800357 * Reverse mapping data structures:
358 *
Izik Eidus290fc382007-09-27 14:11:22 +0200359 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
360 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800361 *
Izik Eidus290fc382007-09-27 14:11:22 +0200362 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
363 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800364 */
Izik Eidus290fc382007-09-27 14:11:22 +0200365static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800366{
Avi Kivity4db35312007-11-21 15:28:32 +0200367 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800368 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200369 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370 int i;
371
372 if (!is_rmap_pte(*spte))
373 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200374 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200375 sp = page_header(__pa(spte));
376 sp->gfns[spte - sp->spt] = gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200377 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
378 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800379 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200380 *rmapp = (unsigned long)spte;
381 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800382 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800383 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200384 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800385 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200386 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800387 } else {
388 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200389 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800390 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
391 desc = desc->more;
392 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800393 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800394 desc = desc->more;
395 }
396 for (i = 0; desc->shadow_ptes[i]; ++i)
397 ;
398 desc->shadow_ptes[i] = spte;
399 }
400}
401
Izik Eidus290fc382007-09-27 14:11:22 +0200402static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800403 struct kvm_rmap_desc *desc,
404 int i,
405 struct kvm_rmap_desc *prev_desc)
406{
407 int j;
408
409 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
410 ;
411 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000412 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800413 if (j != 0)
414 return;
415 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200416 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800417 else
418 if (prev_desc)
419 prev_desc->more = desc->more;
420 else
Izik Eidus290fc382007-09-27 14:11:22 +0200421 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300422 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800423}
424
Izik Eidus290fc382007-09-27 14:11:22 +0200425static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800426{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800427 struct kvm_rmap_desc *desc;
428 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200429 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200430 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200431 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800432 int i;
433
434 if (!is_rmap_pte(*spte))
435 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200436 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200437 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200438 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200439 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200440 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200441 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200442 kvm_release_page_clean(page);
Avi Kivity4db35312007-11-21 15:28:32 +0200443 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
Izik Eidus290fc382007-09-27 14:11:22 +0200444 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800445 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
446 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200447 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800448 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200449 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800450 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
451 spte, *spte);
452 BUG();
453 }
Izik Eidus290fc382007-09-27 14:11:22 +0200454 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800455 } else {
456 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200457 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800458 prev_desc = NULL;
459 while (desc) {
460 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
461 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200462 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800463 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800464 prev_desc);
465 return;
466 }
467 prev_desc = desc;
468 desc = desc->more;
469 }
470 BUG();
471 }
472}
473
Izik Eidus98348e92007-10-16 14:42:30 +0200474static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800475{
Avi Kivity374cbac2007-01-05 16:36:43 -0800476 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200477 struct kvm_rmap_desc *prev_desc;
478 u64 *prev_spte;
479 int i;
480
481 if (!*rmapp)
482 return NULL;
483 else if (!(*rmapp & 1)) {
484 if (!spte)
485 return (u64 *)*rmapp;
486 return NULL;
487 }
488 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
489 prev_desc = NULL;
490 prev_spte = NULL;
491 while (desc) {
492 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
493 if (prev_spte == spte)
494 return desc->shadow_ptes[i];
495 prev_spte = desc->shadow_ptes[i];
496 }
497 desc = desc->more;
498 }
499 return NULL;
500}
501
502static void rmap_write_protect(struct kvm *kvm, u64 gfn)
503{
Izik Eidus290fc382007-09-27 14:11:22 +0200504 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800505 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800506 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800507
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500508 gfn = unalias_gfn(kvm, gfn);
509 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800510
Izik Eidus98348e92007-10-16 14:42:30 +0200511 spte = rmap_next(kvm, rmapp, NULL);
512 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800513 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800514 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800515 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800516 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200517 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800518 write_protected = 1;
519 }
Izik Eidus9647c142007-10-16 14:43:46 +0200520 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800521 }
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800522 if (write_protected)
523 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800524}
525
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800526#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300527static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528{
Avi Kivity139bdb22007-01-05 16:36:50 -0800529 u64 *pos;
530 u64 *end;
531
Avi Kivity47ad8e62007-05-06 15:50:58 +0300532 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityd196e342008-01-24 11:44:11 +0200533 if (*pos != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800534 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
535 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800537 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 return 1;
539}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800540#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541
Avi Kivity4db35312007-11-21 15:28:32 +0200542static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800543{
Avi Kivity4db35312007-11-21 15:28:32 +0200544 ASSERT(is_empty_shadow_page(sp->spt));
545 list_del(&sp->link);
546 __free_page(virt_to_page(sp->spt));
547 __free_page(virt_to_page(sp->gfns));
548 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800549 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800550}
551
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800552static unsigned kvm_page_table_hashfn(gfn_t gfn)
553{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200554 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800555}
556
Avi Kivity25c0de22007-01-05 16:36:42 -0800557static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
558 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559{
Avi Kivity4db35312007-11-21 15:28:32 +0200560 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800562 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
563 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
564 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200565 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800566 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200567 ASSERT(is_empty_shadow_page(sp->spt));
568 sp->slot_bitmap = 0;
569 sp->multimapped = 0;
570 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800571 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200572 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573}
574
Avi Kivity714b93d2007-01-05 16:36:53 -0800575static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200576 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800577{
578 struct kvm_pte_chain *pte_chain;
579 struct hlist_node *node;
580 int i;
581
582 if (!parent_pte)
583 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200584 if (!sp->multimapped) {
585 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800586
587 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200588 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800589 return;
590 }
Avi Kivity4db35312007-11-21 15:28:32 +0200591 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800592 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200593 INIT_HLIST_HEAD(&sp->parent_ptes);
594 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800595 pte_chain->parent_ptes[0] = old;
596 }
Avi Kivity4db35312007-11-21 15:28:32 +0200597 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800598 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
599 continue;
600 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
601 if (!pte_chain->parent_ptes[i]) {
602 pte_chain->parent_ptes[i] = parent_pte;
603 return;
604 }
605 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800606 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800607 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200608 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800609 pte_chain->parent_ptes[0] = parent_pte;
610}
611
Avi Kivity4db35312007-11-21 15:28:32 +0200612static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800613 u64 *parent_pte)
614{
615 struct kvm_pte_chain *pte_chain;
616 struct hlist_node *node;
617 int i;
618
Avi Kivity4db35312007-11-21 15:28:32 +0200619 if (!sp->multimapped) {
620 BUG_ON(sp->parent_pte != parent_pte);
621 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800622 return;
623 }
Avi Kivity4db35312007-11-21 15:28:32 +0200624 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800625 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
626 if (!pte_chain->parent_ptes[i])
627 break;
628 if (pte_chain->parent_ptes[i] != parent_pte)
629 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800630 while (i + 1 < NR_PTE_CHAIN_ENTRIES
631 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800632 pte_chain->parent_ptes[i]
633 = pte_chain->parent_ptes[i + 1];
634 ++i;
635 }
636 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800637 if (i == 0) {
638 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300639 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200640 if (hlist_empty(&sp->parent_ptes)) {
641 sp->multimapped = 0;
642 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800643 }
644 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800645 return;
646 }
647 BUG();
648}
649
Avi Kivity4db35312007-11-21 15:28:32 +0200650static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800651{
652 unsigned index;
653 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200654 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800655 struct hlist_node *node;
656
657 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200658 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800659 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200660 hlist_for_each_entry(sp, node, bucket, hash_link)
661 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800662 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200663 __FUNCTION__, sp->role.word);
664 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800665 }
666 return NULL;
667}
668
669static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
670 gfn_t gfn,
671 gva_t gaddr,
672 unsigned level,
673 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200674 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200675 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800676{
677 union kvm_mmu_page_role role;
678 unsigned index;
679 unsigned quadrant;
680 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200681 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800682 struct hlist_node *node;
683
684 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800685 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800686 role.level = level;
687 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200688 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800689 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800690 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
691 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
692 role.quadrant = quadrant;
693 }
694 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
695 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200696 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800697 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200698 hlist_for_each_entry(sp, node, bucket, hash_link)
699 if (sp->gfn == gfn && sp->role.word == role.word) {
700 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800701 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200702 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800703 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200704 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200705 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
706 if (!sp)
707 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800708 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200709 sp->gfn = gfn;
710 sp->role = role;
711 hlist_add_head(&sp->hash_link, bucket);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800712 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800713 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500714 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200715 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716}
717
Avi Kivity90cb0522007-07-17 13:04:56 +0300718static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200719 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800720{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800721 unsigned i;
722 u64 *pt;
723 u64 ent;
724
Avi Kivity4db35312007-11-21 15:28:32 +0200725 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800726
Avi Kivity4db35312007-11-21 15:28:32 +0200727 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800728 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200729 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200730 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200731 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800732 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300733 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734 return;
735 }
736
737 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
738 ent = pt[i];
739
Avi Kivityc7addb92007-09-16 18:58:32 +0200740 pt[i] = shadow_trap_nonpresent_pte;
741 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800742 continue;
743 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300744 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800745 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300746 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800747}
748
Avi Kivity4db35312007-11-21 15:28:32 +0200749static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800750{
Avi Kivity4db35312007-11-21 15:28:32 +0200751 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800752}
753
Avi Kivity12b7d282007-09-23 14:10:49 +0200754static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
755{
756 int i;
757
758 for (i = 0; i < KVM_MAX_VCPUS; ++i)
759 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800760 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200761}
762
Avi Kivity4db35312007-11-21 15:28:32 +0200763static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800764{
765 u64 *parent_pte;
766
Avi Kivity4cee5762007-11-18 16:37:07 +0200767 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200768 while (sp->multimapped || sp->parent_pte) {
769 if (!sp->multimapped)
770 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800771 else {
772 struct kvm_pte_chain *chain;
773
Avi Kivity4db35312007-11-21 15:28:32 +0200774 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800775 struct kvm_pte_chain, link);
776 parent_pte = chain->parent_ptes[0];
777 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800778 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200779 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200780 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800781 }
Avi Kivity4db35312007-11-21 15:28:32 +0200782 kvm_mmu_page_unlink_children(kvm, sp);
783 if (!sp->root_count) {
784 hlist_del(&sp->hash_link);
785 kvm_mmu_free_page(kvm, sp);
Avi Kivity36868f72007-03-26 19:31:52 +0200786 } else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800787 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200788 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800789}
790
Izik Eidus82ce2c92007-10-02 18:52:55 +0200791/*
792 * Changing the number of mmu pages allocated to the vm
793 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
794 */
795void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
796{
797 /*
798 * If we set the number of mmu pages to be smaller be than the
799 * number of actived pages , we must to free some mmu pages before we
800 * change the value
801 */
802
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800803 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200804 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800805 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
806 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200807
808 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
809 struct kvm_mmu_page *page;
810
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800811 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200812 struct kvm_mmu_page, link);
813 kvm_mmu_zap_page(kvm, page);
814 n_used_mmu_pages--;
815 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800816 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200817 }
818 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800819 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
820 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200821
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800822 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200823}
824
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500825static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800826{
827 unsigned index;
828 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200829 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800830 struct hlist_node *node, *n;
831 int r;
832
833 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
834 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200835 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800836 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200837 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
838 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800839 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200840 sp->role.word);
841 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800842 r = 1;
843 }
844 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845}
846
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500847static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300848{
Avi Kivity4db35312007-11-21 15:28:32 +0200849 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300850
Avi Kivity4db35312007-11-21 15:28:32 +0200851 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
852 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
853 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300854 }
855}
856
Avi Kivity38c335f2007-11-21 14:20:22 +0200857static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858{
Avi Kivity38c335f2007-11-21 14:20:22 +0200859 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200860 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
Avi Kivity4db35312007-11-21 15:28:32 +0200862 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863}
864
Avi Kivity039576c2007-03-20 12:46:50 +0200865struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
866{
Izik Eidus72dc67a2008-02-10 18:04:15 +0200867 struct page *page;
868
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800869 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +0200870
871 if (gpa == UNMAPPED_GVA)
872 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +0200873
874 down_read(&current->mm->mmap_sem);
875 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
876 up_read(&current->mm->mmap_sem);
877
878 return page;
Avi Kivity039576c2007-03-20 12:46:50 +0200879}
880
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200881static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
882 unsigned pt_access, unsigned pte_access,
883 int user_fault, int write_fault, int dirty,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200884 int *ptwrite, gfn_t gfn, struct page *page)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200885{
886 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -0300887 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +0200888 int was_writeble = is_writeble_pte(*shadow_pte);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -0300889 hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200890
Avi Kivitybc750ba2007-12-09 18:39:41 +0200891 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200892 " user_fault %d gfn %lx\n",
Avi Kivitybc750ba2007-12-09 18:39:41 +0200893 __FUNCTION__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200894 write_fault, user_fault, gfn);
895
Marcelo Tosatti15aaa812008-03-17 10:08:18 -0300896 if (is_rmap_pte(*shadow_pte)) {
897 if (host_pfn != page_to_pfn(page)) {
898 pgprintk("hfn old %lx new %lx\n",
899 host_pfn, page_to_pfn(page));
900 rmap_remove(vcpu->kvm, shadow_pte);
901 }
902 else
903 was_rmapped = 1;
904 }
905
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200906 /*
907 * We don't set the accessed bit, since we sometimes want to see
908 * whether the guest actually used the pte (in order to detect
909 * demand paging).
910 */
911 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
912 if (!dirty)
913 pte_access &= ~ACC_WRITE_MASK;
914 if (!(pte_access & ACC_EXEC_MASK))
915 spte |= PT64_NX_MASK;
916
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200917 spte |= PT_PRESENT_MASK;
918 if (pte_access & ACC_USER_MASK)
919 spte |= PT_USER_MASK;
920
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200921 spte |= page_to_phys(page);
922
923 if ((pte_access & ACC_WRITE_MASK)
924 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
925 struct kvm_mmu_page *shadow;
926
927 spte |= PT_WRITABLE_MASK;
928 if (user_fault) {
929 mmu_unshadow(vcpu->kvm, gfn);
930 goto unshadowed;
931 }
932
933 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
934 if (shadow) {
935 pgprintk("%s: found shadow page for %lx, marking ro\n",
936 __FUNCTION__, gfn);
937 pte_access &= ~ACC_WRITE_MASK;
938 if (is_writeble_pte(spte)) {
939 spte &= ~PT_WRITABLE_MASK;
940 kvm_x86_ops->tlb_flush(vcpu);
941 }
942 if (write_fault)
943 *ptwrite = 1;
944 }
945 }
946
947unshadowed:
948
949 if (pte_access & ACC_WRITE_MASK)
950 mark_page_dirty(vcpu->kvm, gfn);
951
952 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
953 set_shadow_pte(shadow_pte, spte);
954 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
955 if (!was_rmapped) {
956 rmap_add(vcpu, shadow_pte, gfn);
957 if (!is_rmap_pte(*shadow_pte))
958 kvm_release_page_clean(page);
Izik Eidus75e68e62008-01-12 23:49:09 +0200959 } else {
960 if (was_writeble)
961 kvm_release_page_dirty(page);
962 else
963 kvm_release_page_clean(page);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200964 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200965 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800966 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200967}
968
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
970{
971}
972
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500973static int __nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write,
974 gfn_t gfn, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975{
976 int level = PT32E_ROOT_LEVEL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800977 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +0200978 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979
980 for (; ; level--) {
981 u32 index = PT64_INDEX(v, level);
982 u64 *table;
983
984 ASSERT(VALID_PAGE(table_addr));
985 table = __va(table_addr);
986
987 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +0200988 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200989 0, write, 1, &pt_write, gfn, page);
Avi Kivityd196e342008-01-24 11:44:11 +0200990 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 }
992
Avi Kivityc7addb92007-09-16 18:58:32 +0200993 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800994 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800995 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800997 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
998 >> PAGE_SHIFT;
999 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1000 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001001 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001002 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001004 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 return -ENOMEM;
1006 }
1007
Avi Kivity47ad8e62007-05-06 15:50:58 +03001008 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001009 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 }
1011 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1012 }
1013}
1014
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001015static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1016{
1017 int r;
1018
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001019 struct page *page;
1020
Izik Eidus72dc67a2008-02-10 18:04:15 +02001021 down_read(&vcpu->kvm->slots_lock);
1022
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001023 down_read(&current->mm->mmap_sem);
1024 page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001025 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001026
Avi Kivityd196e342008-01-24 11:44:11 +02001027 /* mmio */
1028 if (is_error_page(page)) {
1029 kvm_release_page_clean(page);
1030 up_read(&vcpu->kvm->slots_lock);
1031 return 1;
1032 }
1033
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001034 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001035 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001036 r = __nonpaging_map(vcpu, v, write, gfn, page);
1037 spin_unlock(&vcpu->kvm->mmu_lock);
1038
Izik Eidus72dc67a2008-02-10 18:04:15 +02001039 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001040
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001041 return r;
1042}
1043
1044
Avi Kivityc7addb92007-09-16 18:58:32 +02001045static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1046 struct kvm_mmu_page *sp)
1047{
1048 int i;
1049
1050 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1051 sp->spt[i] = shadow_trap_nonpresent_pte;
1052}
1053
Avi Kivity17ac10a2007-01-05 16:36:40 -08001054static void mmu_free_roots(struct kvm_vcpu *vcpu)
1055{
1056 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001057 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001058
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001059 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001060 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001061 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001062#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001063 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1064 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001065
Avi Kivity4db35312007-11-21 15:28:32 +02001066 sp = page_header(root);
1067 --sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001068 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001069 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001070 return;
1071 }
1072#endif
1073 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001074 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001075
Avi Kivity417726a2007-04-12 17:35:58 +03001076 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001077 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001078 sp = page_header(root);
1079 --sp->root_count;
Avi Kivity417726a2007-04-12 17:35:58 +03001080 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001081 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001082 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001083 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001084 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001085}
1086
1087static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1088{
1089 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001090 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001091 struct kvm_mmu_page *sp;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001092
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001093 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001094
1095#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001096 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1097 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001098
1099 ASSERT(!VALID_PAGE(root));
Avi Kivity4db35312007-11-21 15:28:32 +02001100 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001101 PT64_ROOT_LEVEL, 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001102 root = __pa(sp->spt);
1103 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001104 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001105 return;
1106 }
1107#endif
1108 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001109 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001110
1111 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001112 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1113 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1114 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001115 continue;
1116 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001117 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1118 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001119 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001120 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1121 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001122 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001123 root = __pa(sp->spt);
1124 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001125 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001126 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001127 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001128}
1129
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1131{
1132 return vaddr;
1133}
1134
1135static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001136 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137{
Avi Kivitye8332402007-12-09 18:43:00 +02001138 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001139 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140
Avi Kivitye8332402007-12-09 18:43:00 +02001141 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001142 r = mmu_topup_memory_caches(vcpu);
1143 if (r)
1144 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001145
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001147 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148
Avi Kivitye8332402007-12-09 18:43:00 +02001149 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150
Avi Kivitye8332402007-12-09 18:43:00 +02001151 return nonpaging_map(vcpu, gva & PAGE_MASK,
1152 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153}
1154
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155static void nonpaging_free(struct kvm_vcpu *vcpu)
1156{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001157 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158}
1159
1160static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1161{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001162 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163
1164 context->new_cr3 = nonpaging_new_cr3;
1165 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 context->gva_to_gpa = nonpaging_gva_to_gpa;
1167 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001168 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001169 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001171 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 return 0;
1173}
1174
Avi Kivityd835dfe2007-11-21 02:57:59 +02001175void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001177 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001178 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179}
1180
1181static void paging_new_cr3(struct kvm_vcpu *vcpu)
1182{
Marcelo Tosatti24993d52008-02-14 21:25:39 -02001183 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001184 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185}
1186
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187static void inject_page_fault(struct kvm_vcpu *vcpu,
1188 u64 addr,
1189 u32 err_code)
1190{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001191 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192}
1193
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194static void paging_free(struct kvm_vcpu *vcpu)
1195{
1196 nonpaging_free(vcpu);
1197}
1198
1199#define PTTYPE 64
1200#include "paging_tmpl.h"
1201#undef PTTYPE
1202
1203#define PTTYPE 32
1204#include "paging_tmpl.h"
1205#undef PTTYPE
1206
Avi Kivity17ac10a2007-01-05 16:36:40 -08001207static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001209 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210
1211 ASSERT(is_pae(vcpu));
1212 context->new_cr3 = paging_new_cr3;
1213 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001215 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001217 context->root_level = level;
1218 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001219 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 return 0;
1221}
1222
Avi Kivity17ac10a2007-01-05 16:36:40 -08001223static int paging64_init_context(struct kvm_vcpu *vcpu)
1224{
1225 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1226}
1227
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228static int paging32_init_context(struct kvm_vcpu *vcpu)
1229{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001230 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231
1232 context->new_cr3 = paging_new_cr3;
1233 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 context->gva_to_gpa = paging32_gva_to_gpa;
1235 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001236 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237 context->root_level = PT32_ROOT_LEVEL;
1238 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001239 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 return 0;
1241}
1242
1243static int paging32E_init_context(struct kvm_vcpu *vcpu)
1244{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001245 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246}
1247
1248static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1249{
1250 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001251 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252
1253 if (!is_paging(vcpu))
1254 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001255 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 return paging64_init_context(vcpu);
1257 else if (is_pae(vcpu))
1258 return paging32E_init_context(vcpu);
1259 else
1260 return paging32_init_context(vcpu);
1261}
1262
1263static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1264{
1265 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001266 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1267 vcpu->arch.mmu.free(vcpu);
1268 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269 }
1270}
1271
1272int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1273{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001274 destroy_kvm_mmu(vcpu);
1275 return init_kvm_mmu(vcpu);
1276}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001277EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001278
1279int kvm_mmu_load(struct kvm_vcpu *vcpu)
1280{
Avi Kivity714b93d2007-01-05 16:36:53 -08001281 int r;
1282
Avi Kivitye2dec932007-01-05 16:36:54 -08001283 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001284 if (r)
1285 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001286 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001287 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001288 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001289 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001290 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001291 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001292out:
1293 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001295EXPORT_SYMBOL_GPL(kvm_mmu_load);
1296
1297void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1298{
1299 mmu_free_roots(vcpu);
1300}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301
Avi Kivity09072da2007-05-01 14:16:52 +03001302static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001303 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001304 u64 *spte)
1305{
1306 u64 pte;
1307 struct kvm_mmu_page *child;
1308
1309 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001310 if (is_shadow_present_pte(pte)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001311 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001312 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001313 else {
1314 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001315 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001316 }
1317 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001318 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001319}
1320
Avi Kivity00284252007-05-01 16:53:31 +03001321static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001322 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001323 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001324 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001325{
Avi Kivity4db35312007-11-21 15:28:32 +02001326 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001327 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001328 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001329 }
Avi Kivity00284252007-05-01 16:53:31 +03001330
Avi Kivity4cee5762007-11-18 16:37:07 +02001331 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001332 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001333 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001334 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001335 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001336}
1337
Avi Kivity79539ce2007-11-21 02:06:21 +02001338static bool need_remote_flush(u64 old, u64 new)
1339{
1340 if (!is_shadow_present_pte(old))
1341 return false;
1342 if (!is_shadow_present_pte(new))
1343 return true;
1344 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1345 return true;
1346 old ^= PT64_NX_MASK;
1347 new ^= PT64_NX_MASK;
1348 return (old & ~new & PT64_PERM_MASK) != 0;
1349}
1350
1351static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1352{
1353 if (need_remote_flush(old, new))
1354 kvm_flush_remote_tlbs(vcpu->kvm);
1355 else
1356 kvm_mmu_flush_tlb(vcpu);
1357}
1358
Avi Kivity12b7d282007-09-23 14:10:49 +02001359static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1360{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001361 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001362
1363 return !!(spte && (*spte & PT_ACCESSED_MASK));
1364}
1365
Avi Kivityd7824ff2007-12-30 12:29:05 +02001366static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1367 const u8 *new, int bytes)
1368{
1369 gfn_t gfn;
1370 int r;
1371 u64 gpte = 0;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001372 struct page *page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001373
1374 if (bytes != 4 && bytes != 8)
1375 return;
1376
1377 /*
1378 * Assume that the pte write on a page table of the same type
1379 * as the current vcpu paging mode. This is nearly always true
1380 * (might be false while changing modes). Note it is verified later
1381 * by update_pte().
1382 */
1383 if (is_pae(vcpu)) {
1384 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1385 if ((bytes == 4) && (gpa % 4 == 0)) {
1386 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1387 if (r)
1388 return;
1389 memcpy((void *)&gpte + (gpa % 8), new, 4);
1390 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1391 memcpy((void *)&gpte, new, 8);
1392 }
1393 } else {
1394 if ((bytes == 4) && (gpa % 4 == 0))
1395 memcpy((void *)&gpte, new, 4);
1396 }
1397 if (!is_present_pte(gpte))
1398 return;
1399 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001400
Avi Kivityd196e342008-01-24 11:44:11 +02001401 down_read(&vcpu->kvm->slots_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001402 page = gfn_to_page(vcpu->kvm, gfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001403 up_read(&vcpu->kvm->slots_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001404
Avi Kivityd196e342008-01-24 11:44:11 +02001405 if (is_error_page(page)) {
1406 kvm_release_page_clean(page);
1407 return;
1408 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001409 vcpu->arch.update_pte.gfn = gfn;
Avi Kivitye48bb492008-03-23 14:21:08 +02001410 vcpu->arch.update_pte.page = page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001411}
1412
Avi Kivity09072da2007-05-01 14:16:52 +03001413void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001414 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001415{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001416 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001417 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001418 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001419 struct hlist_head *bucket;
1420 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001421 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001422 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001423 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001424 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001425 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001426 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001427 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001428 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001429 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001430 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001431 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001432
Avi Kivityda4a00f2007-01-05 16:36:44 -08001433 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001434 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001435 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001436 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001437 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001438 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001439 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001440 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001441 ++vcpu->arch.last_pt_write_count;
1442 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001443 flooded = 1;
1444 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001445 vcpu->arch.last_pt_write_gfn = gfn;
1446 vcpu->arch.last_pt_write_count = 1;
1447 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001448 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001449 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001450 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001451 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1452 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001453 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001454 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001455 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001456 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001457 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001458 /*
1459 * Misaligned accesses are too much trouble to fix
1460 * up; also, they usually indicate a page is not used
1461 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001462 *
1463 * If we're seeing too many writes to a page,
1464 * it may no longer be a page table, or we may be
1465 * forking, in which case it is better to unmap the
1466 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001467 */
1468 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001469 gpa, bytes, sp->role.word);
1470 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001471 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001472 continue;
1473 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001474 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001475 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001476 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001477 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001478 page_offset <<= 1; /* 32->64 */
1479 /*
1480 * A 32-bit pde maps 4MB while the shadow pdes map
1481 * only 2MB. So we need to double the offset again
1482 * and zap two pdes instead of one.
1483 */
1484 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001485 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001486 page_offset <<= 1;
1487 npte = 2;
1488 }
Avi Kivityfce06572007-05-01 16:44:05 +03001489 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001490 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001491 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001492 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001493 }
Avi Kivity4db35312007-11-21 15:28:32 +02001494 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001495 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1496 gentry = 0;
1497 r = kvm_read_guest_atomic(vcpu->kvm,
1498 gpa & ~(u64)(pte_size - 1),
1499 &gentry, pte_size);
1500 new = (const void *)&gentry;
1501 if (r < 0)
1502 new = NULL;
1503 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001504 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001505 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001506 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001507 if (new)
1508 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001509 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001510 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001511 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001512 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001513 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001514 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001515 if (vcpu->arch.update_pte.page) {
1516 kvm_release_page_clean(vcpu->arch.update_pte.page);
1517 vcpu->arch.update_pte.page = NULL;
1518 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001519}
1520
Avi Kivitya4360362007-01-05 16:36:45 -08001521int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1522{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001523 gpa_t gpa;
1524 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001525
Izik Eidus72dc67a2008-02-10 18:04:15 +02001526 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001527 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001528 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001529
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001530 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001531 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001532 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001533 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001534}
1535
Avi Kivity22d95b12007-09-14 20:26:06 +03001536void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001537{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001538 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001539 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001540
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001541 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001542 struct kvm_mmu_page, link);
1543 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001544 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001545 }
1546}
Avi Kivityebeace82007-01-05 16:36:47 -08001547
Avi Kivity30677142007-10-28 18:48:59 +02001548int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1549{
1550 int r;
1551 enum emulation_result er;
1552
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001553 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001554 if (r < 0)
1555 goto out;
1556
1557 if (!r) {
1558 r = 1;
1559 goto out;
1560 }
1561
Avi Kivityb733bfb2007-10-28 18:52:05 +02001562 r = mmu_topup_memory_caches(vcpu);
1563 if (r)
1564 goto out;
1565
Avi Kivity30677142007-10-28 18:48:59 +02001566 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001567
1568 switch (er) {
1569 case EMULATE_DONE:
1570 return 1;
1571 case EMULATE_DO_MMIO:
1572 ++vcpu->stat.mmio_exits;
1573 return 0;
1574 case EMULATE_FAIL:
1575 kvm_report_emulation_failure(vcpu, "pagetable");
1576 return 1;
1577 default:
1578 BUG();
1579 }
1580out:
Avi Kivity30677142007-10-28 18:48:59 +02001581 return r;
1582}
1583EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1584
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585static void free_mmu_pages(struct kvm_vcpu *vcpu)
1586{
Avi Kivity4db35312007-11-21 15:28:32 +02001587 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001589 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1590 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001591 struct kvm_mmu_page, link);
1592 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001593 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001594 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595}
1596
1597static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1598{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001599 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 int i;
1601
1602 ASSERT(vcpu);
1603
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001604 if (vcpu->kvm->arch.n_requested_mmu_pages)
1605 vcpu->kvm->arch.n_free_mmu_pages =
1606 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001607 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001608 vcpu->kvm->arch.n_free_mmu_pages =
1609 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001610 /*
1611 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1612 * Therefore we need to allocate shadow page tables in the first
1613 * 4GB of memory, which happens to fit the DMA32 zone.
1614 */
1615 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1616 if (!page)
1617 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001618 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001619 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001620 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001621
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 return 0;
1623
1624error_1:
1625 free_mmu_pages(vcpu);
1626 return -ENOMEM;
1627}
1628
Ingo Molnar8018c272006-12-29 16:50:01 -08001629int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001632 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633
Ingo Molnar8018c272006-12-29 16:50:01 -08001634 return alloc_mmu_pages(vcpu);
1635}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636
Ingo Molnar8018c272006-12-29 16:50:01 -08001637int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1638{
1639 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001640 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001641
Ingo Molnar8018c272006-12-29 16:50:01 -08001642 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643}
1644
1645void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1646{
1647 ASSERT(vcpu);
1648
1649 destroy_kvm_mmu(vcpu);
1650 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001651 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
Avi Kivity90cb0522007-07-17 13:04:56 +03001654void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655{
Avi Kivity4db35312007-11-21 15:28:32 +02001656 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001658 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 int i;
1660 u64 *pt;
1661
Avi Kivity4db35312007-11-21 15:28:32 +02001662 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 continue;
1664
Avi Kivity4db35312007-11-21 15:28:32 +02001665 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1667 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001668 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 }
1671}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001672
Avi Kivity90cb0522007-07-17 13:04:56 +03001673void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001674{
Avi Kivity4db35312007-11-21 15:28:32 +02001675 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001676
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001677 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001678 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001679 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001680 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001681
Avi Kivity90cb0522007-07-17 13:04:56 +03001682 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001683}
1684
Avi Kivityb5a33a72007-04-15 16:31:09 +03001685void kvm_mmu_module_exit(void)
1686{
1687 if (pte_chain_cache)
1688 kmem_cache_destroy(pte_chain_cache);
1689 if (rmap_desc_cache)
1690 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001691 if (mmu_page_header_cache)
1692 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001693}
1694
1695int kvm_mmu_module_init(void)
1696{
1697 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1698 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001699 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001700 if (!pte_chain_cache)
1701 goto nomem;
1702 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1703 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001704 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001705 if (!rmap_desc_cache)
1706 goto nomem;
1707
Avi Kivityd3d25b02007-05-30 12:34:53 +03001708 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1709 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001710 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001711 if (!mmu_page_header_cache)
1712 goto nomem;
1713
Avi Kivityb5a33a72007-04-15 16:31:09 +03001714 return 0;
1715
1716nomem:
1717 kvm_mmu_module_exit();
1718 return -ENOMEM;
1719}
1720
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001721/*
1722 * Caculate mmu pages needed for kvm.
1723 */
1724unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1725{
1726 int i;
1727 unsigned int nr_mmu_pages;
1728 unsigned int nr_pages = 0;
1729
1730 for (i = 0; i < kvm->nmemslots; i++)
1731 nr_pages += kvm->memslots[i].npages;
1732
1733 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1734 nr_mmu_pages = max(nr_mmu_pages,
1735 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1736
1737 return nr_mmu_pages;
1738}
1739
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001740#ifdef AUDIT
1741
1742static const char *audit_msg;
1743
1744static gva_t canonicalize(gva_t gva)
1745{
1746#ifdef CONFIG_X86_64
1747 gva = (long long)(gva << 16) >> 16;
1748#endif
1749 return gva;
1750}
1751
1752static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1753 gva_t va, int level)
1754{
1755 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1756 int i;
1757 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1758
1759 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1760 u64 ent = pt[i];
1761
Avi Kivityc7addb92007-09-16 18:58:32 +02001762 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001763 continue;
1764
1765 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001766 if (level > 1) {
1767 if (ent == shadow_notrap_nonpresent_pte)
1768 printk(KERN_ERR "audit: (%s) nontrapping pte"
1769 " in nonleaf level: levels %d gva %lx"
1770 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001771 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02001772
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001773 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001774 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001775 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001776 struct page *page = gpa_to_page(vcpu, gpa);
1777 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001778
Avi Kivityc7addb92007-09-16 18:58:32 +02001779 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001780 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001781 printk(KERN_ERR "xx audit error: (%s) levels %d"
1782 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001783 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001784 va, gpa, hpa, ent,
1785 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001786 else if (ent == shadow_notrap_nonpresent_pte
1787 && !is_error_hpa(hpa))
1788 printk(KERN_ERR "audit: (%s) notrap shadow,"
1789 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001790 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001791
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001792 }
1793 }
1794}
1795
1796static void audit_mappings(struct kvm_vcpu *vcpu)
1797{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001798 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001799
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001800 if (vcpu->arch.mmu.root_level == 4)
1801 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001802 else
1803 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001804 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001805 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001806 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001807 i << 30,
1808 2);
1809}
1810
1811static int count_rmaps(struct kvm_vcpu *vcpu)
1812{
1813 int nmaps = 0;
1814 int i, j, k;
1815
1816 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1817 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1818 struct kvm_rmap_desc *d;
1819
1820 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001821 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001822
Izik Eidus290fc382007-09-27 14:11:22 +02001823 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001824 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001825 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001826 ++nmaps;
1827 continue;
1828 }
Izik Eidus290fc382007-09-27 14:11:22 +02001829 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001830 while (d) {
1831 for (k = 0; k < RMAP_EXT; ++k)
1832 if (d->shadow_ptes[k])
1833 ++nmaps;
1834 else
1835 break;
1836 d = d->more;
1837 }
1838 }
1839 }
1840 return nmaps;
1841}
1842
1843static int count_writable_mappings(struct kvm_vcpu *vcpu)
1844{
1845 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001846 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001847 int i;
1848
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001849 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001850 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001851
Avi Kivity4db35312007-11-21 15:28:32 +02001852 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001853 continue;
1854
1855 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1856 u64 ent = pt[i];
1857
1858 if (!(ent & PT_PRESENT_MASK))
1859 continue;
1860 if (!(ent & PT_WRITABLE_MASK))
1861 continue;
1862 ++nmaps;
1863 }
1864 }
1865 return nmaps;
1866}
1867
1868static void audit_rmap(struct kvm_vcpu *vcpu)
1869{
1870 int n_rmap = count_rmaps(vcpu);
1871 int n_actual = count_writable_mappings(vcpu);
1872
1873 if (n_rmap != n_actual)
1874 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1875 __FUNCTION__, audit_msg, n_rmap, n_actual);
1876}
1877
1878static void audit_write_protection(struct kvm_vcpu *vcpu)
1879{
Avi Kivity4db35312007-11-21 15:28:32 +02001880 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001881 struct kvm_memory_slot *slot;
1882 unsigned long *rmapp;
1883 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001884
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001885 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001886 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001887 continue;
1888
Avi Kivity4db35312007-11-21 15:28:32 +02001889 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1890 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02001891 rmapp = &slot->rmap[gfn - slot->base_gfn];
1892 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001893 printk(KERN_ERR "%s: (%s) shadow page has writable"
1894 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001895 __FUNCTION__, audit_msg, sp->gfn,
1896 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001897 }
1898}
1899
1900static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1901{
1902 int olddbg = dbg;
1903
1904 dbg = 0;
1905 audit_msg = msg;
1906 audit_rmap(vcpu);
1907 audit_write_protection(vcpu);
1908 audit_mappings(vcpu);
1909 dbg = olddbg;
1910}
1911
1912#endif