blob: 9b75b102b8d1f97feb8f31e70cb46fcca7531533 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
21#include "kvm.h"
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080022#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
29
30#include <asm/page.h>
31#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020032#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033
Avi Kivity37a7d8b2007-01-05 16:36:56 -080034#undef MMU_DEBUG
35
36#undef AUDIT
37
38#ifdef AUDIT
39static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
40#else
41static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
42#endif
43
44#ifdef MMU_DEBUG
45
46#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
47#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
48
49#else
50
51#define pgprintk(x...) do { } while (0)
52#define rmap_printk(x...) do { } while (0)
53
54#endif
55
56#if defined(MMU_DEBUG) || defined(AUDIT)
57static int dbg = 1;
58#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080059
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080060#ifndef MMU_DEBUG
61#define ASSERT(x) do { } while (0)
62#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080063#define ASSERT(x) \
64 if (!(x)) { \
65 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
66 __FILE__, __LINE__, #x); \
67 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080068#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080069
Avi Kivitycea0f0e2007-01-05 16:36:43 -080070#define PT64_PT_BITS 9
71#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
72#define PT32_PT_BITS 10
73#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080074
75#define PT_WRITABLE_SHIFT 1
76
77#define PT_PRESENT_MASK (1ULL << 0)
78#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
79#define PT_USER_MASK (1ULL << 2)
80#define PT_PWT_MASK (1ULL << 3)
81#define PT_PCD_MASK (1ULL << 4)
82#define PT_ACCESSED_MASK (1ULL << 5)
83#define PT_DIRTY_MASK (1ULL << 6)
84#define PT_PAGE_SIZE_MASK (1ULL << 7)
85#define PT_PAT_MASK (1ULL << 7)
86#define PT_GLOBAL_MASK (1ULL << 8)
87#define PT64_NX_MASK (1ULL << 63)
88
89#define PT_PAT_SHIFT 7
90#define PT_DIR_PAT_SHIFT 12
91#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
92
93#define PT32_DIR_PSE36_SIZE 4
94#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040095#define PT32_DIR_PSE36_MASK \
96 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
98
Avi Kivity6aa8b732006-12-10 02:21:36 -080099#define PT_FIRST_AVAIL_BITS_SHIFT 9
100#define PT64_SECOND_AVAIL_BITS_SHIFT 52
101
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
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
155struct kvm_rmap_desc {
156 u64 *shadow_ptes[RMAP_EXT];
157 struct kvm_rmap_desc *more;
158};
159
Avi Kivityb5a33a72007-04-15 16:31:09 +0300160static struct kmem_cache *pte_chain_cache;
161static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300162static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300163
Avi Kivityc7addb92007-09-16 18:58:32 +0200164static u64 __read_mostly shadow_trap_nonpresent_pte;
165static u64 __read_mostly shadow_notrap_nonpresent_pte;
166
167void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
168{
169 shadow_trap_nonpresent_pte = trap_pte;
170 shadow_notrap_nonpresent_pte = notrap_pte;
171}
172EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
173
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174static int is_write_protection(struct kvm_vcpu *vcpu)
175{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000176 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177}
178
179static int is_cpuid_PSE36(void)
180{
181 return 1;
182}
183
Avi Kivity73b10872007-01-26 00:56:41 -0800184static int is_nx(struct kvm_vcpu *vcpu)
185{
186 return vcpu->shadow_efer & EFER_NX;
187}
188
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189static int is_present_pte(unsigned long pte)
190{
191 return pte & PT_PRESENT_MASK;
192}
193
Avi Kivityc7addb92007-09-16 18:58:32 +0200194static int is_shadow_present_pte(u64 pte)
195{
196 pte &= ~PT_SHADOW_IO_MARK;
197 return pte != shadow_trap_nonpresent_pte
198 && pte != shadow_notrap_nonpresent_pte;
199}
200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201static int is_writeble_pte(unsigned long pte)
202{
203 return pte & PT_WRITABLE_MASK;
204}
205
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200206static int is_dirty_pte(unsigned long pte)
207{
208 return pte & PT_DIRTY_MASK;
209}
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static int is_io_pte(unsigned long pte)
212{
213 return pte & PT_SHADOW_IO_MARK;
214}
215
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800216static int is_rmap_pte(u64 pte)
217{
Izik Eidus9647c142007-10-16 14:43:46 +0200218 return pte != shadow_trap_nonpresent_pte
219 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800220}
221
Avi Kivityda928522007-11-21 13:54:47 +0200222static gfn_t pse36_gfn_delta(u32 gpte)
223{
224 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
225
226 return (gpte & PT32_DIR_PSE36_MASK) << shift;
227}
228
Avi Kivitye663ee62007-05-31 15:46:04 +0300229static void set_shadow_pte(u64 *sptep, u64 spte)
230{
231#ifdef CONFIG_X86_64
232 set_64bit((unsigned long *)sptep, spte);
233#else
234 set_64bit((unsigned long long *)sptep, spte);
235#endif
236}
237
Avi Kivitye2dec932007-01-05 16:36:54 -0800238static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300239 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800240{
241 void *obj;
242
243 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800244 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800245 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300246 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800247 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800248 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800249 cache->objects[cache->nobjs++] = obj;
250 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800251 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800252}
253
254static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
255{
256 while (mc->nobjs)
257 kfree(mc->objects[--mc->nobjs]);
258}
259
Avi Kivityc1158e62007-07-20 08:18:27 +0300260static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300261 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300262{
263 struct page *page;
264
265 if (cache->nobjs >= min)
266 return 0;
267 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300268 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300269 if (!page)
270 return -ENOMEM;
271 set_page_private(page, 0);
272 cache->objects[cache->nobjs++] = page_address(page);
273 }
274 return 0;
275}
276
277static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
278{
279 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300280 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300281}
282
Avi Kivity8c438502007-04-16 11:53:17 +0300283static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
284{
285 int r;
286
Avi Kivity22d95b12007-09-14 20:26:06 +0300287 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300288 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
289 pte_chain_cache, 4);
290 if (r)
291 goto out;
292 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
293 rmap_desc_cache, 1);
294 if (r)
295 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200296 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300297 if (r)
298 goto out;
299 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
300 mmu_page_header_cache, 4);
301out:
Avi Kivity8c438502007-04-16 11:53:17 +0300302 return r;
303}
304
Avi Kivity714b93d2007-01-05 16:36:53 -0800305static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
306{
307 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
308 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300309 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300310 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800311}
312
313static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
314 size_t size)
315{
316 void *p;
317
318 BUG_ON(!mc->nobjs);
319 p = mc->objects[--mc->nobjs];
320 memset(p, 0, size);
321 return p;
322}
323
Avi Kivity714b93d2007-01-05 16:36:53 -0800324static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
325{
326 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
327 sizeof(struct kvm_pte_chain));
328}
329
Avi Kivity90cb0522007-07-17 13:04:56 +0300330static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800331{
Avi Kivity90cb0522007-07-17 13:04:56 +0300332 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800333}
334
335static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
336{
337 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
338 sizeof(struct kvm_rmap_desc));
339}
340
Avi Kivity90cb0522007-07-17 13:04:56 +0300341static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800342{
Avi Kivity90cb0522007-07-17 13:04:56 +0300343 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800344}
345
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800346/*
Izik Eidus290fc382007-09-27 14:11:22 +0200347 * Take gfn and return the reverse mapping to it.
348 * Note: gfn must be unaliased before this function get called
349 */
350
351static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
352{
353 struct kvm_memory_slot *slot;
354
355 slot = gfn_to_memslot(kvm, gfn);
356 return &slot->rmap[gfn - slot->base_gfn];
357}
358
359/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800360 * Reverse mapping data structures:
361 *
Izik Eidus290fc382007-09-27 14:11:22 +0200362 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
363 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800364 *
Izik Eidus290fc382007-09-27 14:11:22 +0200365 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
366 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800367 */
Izik Eidus290fc382007-09-27 14:11:22 +0200368static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800369{
Izik Eidus290fc382007-09-27 14:11:22 +0200370 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800371 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200372 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373 int i;
374
375 if (!is_rmap_pte(*spte))
376 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200377 gfn = unalias_gfn(vcpu->kvm, gfn);
378 page = page_header(__pa(spte));
379 page->gfns[spte - page->spt] = gfn;
380 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
381 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800382 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200383 *rmapp = (unsigned long)spte;
384 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800385 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800386 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200387 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800388 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200389 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800390 } else {
391 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200392 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800393 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
394 desc = desc->more;
395 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800396 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800397 desc = desc->more;
398 }
399 for (i = 0; desc->shadow_ptes[i]; ++i)
400 ;
401 desc->shadow_ptes[i] = spte;
402 }
403}
404
Izik Eidus290fc382007-09-27 14:11:22 +0200405static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800406 struct kvm_rmap_desc *desc,
407 int i,
408 struct kvm_rmap_desc *prev_desc)
409{
410 int j;
411
412 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
413 ;
414 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000415 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800416 if (j != 0)
417 return;
418 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200419 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800420 else
421 if (prev_desc)
422 prev_desc->more = desc->more;
423 else
Izik Eidus290fc382007-09-27 14:11:22 +0200424 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300425 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800426}
427
Izik Eidus290fc382007-09-27 14:11:22 +0200428static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800429{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800430 struct kvm_rmap_desc *desc;
431 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200432 struct kvm_mmu_page *page;
Izik Eidusb4231d62007-11-20 11:49:33 +0200433 struct page *release_page;
Izik Eidus290fc382007-09-27 14:11:22 +0200434 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800435 int i;
436
437 if (!is_rmap_pte(*spte))
438 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200439 page = page_header(__pa(spte));
Izik Eidusb4231d62007-11-20 11:49:33 +0200440 release_page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
441 if (is_writeble_pte(*spte))
442 kvm_release_page_dirty(release_page);
443 else
444 kvm_release_page_clean(release_page);
Izik Eidus290fc382007-09-27 14:11:22 +0200445 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
446 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800447 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
448 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200449 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800450 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200451 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800452 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
453 spte, *spte);
454 BUG();
455 }
Izik Eidus290fc382007-09-27 14:11:22 +0200456 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800457 } else {
458 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200459 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800460 prev_desc = NULL;
461 while (desc) {
462 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
463 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200464 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800465 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800466 prev_desc);
467 return;
468 }
469 prev_desc = desc;
470 desc = desc->more;
471 }
472 BUG();
473 }
474}
475
Izik Eidus98348e92007-10-16 14:42:30 +0200476static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800477{
Avi Kivity374cbac2007-01-05 16:36:43 -0800478 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200479 struct kvm_rmap_desc *prev_desc;
480 u64 *prev_spte;
481 int i;
482
483 if (!*rmapp)
484 return NULL;
485 else if (!(*rmapp & 1)) {
486 if (!spte)
487 return (u64 *)*rmapp;
488 return NULL;
489 }
490 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
491 prev_desc = NULL;
492 prev_spte = NULL;
493 while (desc) {
494 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
495 if (prev_spte == spte)
496 return desc->shadow_ptes[i];
497 prev_spte = desc->shadow_ptes[i];
498 }
499 desc = desc->more;
500 }
501 return NULL;
502}
503
504static void rmap_write_protect(struct kvm *kvm, u64 gfn)
505{
Izik Eidus290fc382007-09-27 14:11:22 +0200506 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800507 u64 *spte;
508
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500509 gfn = unalias_gfn(kvm, gfn);
510 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800511
Izik Eidus98348e92007-10-16 14:42:30 +0200512 spte = rmap_next(kvm, rmapp, NULL);
513 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800514 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800515 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800516 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Izik Eidus9647c142007-10-16 14:43:46 +0200517 if (is_writeble_pte(*spte))
518 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500519 kvm_flush_remote_tlbs(kvm);
Izik Eidus9647c142007-10-16 14:43:46 +0200520 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800521 }
522}
523
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800524#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300525static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526{
Avi Kivity139bdb22007-01-05 16:36:50 -0800527 u64 *pos;
528 u64 *end;
529
Avi Kivity47ad8e62007-05-06 15:50:58 +0300530 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200531 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800532 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
533 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800535 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 return 1;
537}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800538#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Avi Kivity90cb0522007-07-17 13:04:56 +0300540static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300541 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800542{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300543 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300544 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300545 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200546 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300547 kfree(page_head);
548 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800549}
550
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800551static unsigned kvm_page_table_hashfn(gfn_t gfn)
552{
553 return gfn;
554}
555
Avi Kivity25c0de22007-01-05 16:36:42 -0800556static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
557 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558{
559 struct kvm_mmu_page *page;
560
Avi Kivityd3d25b02007-05-30 12:34:53 +0300561 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800562 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563
Avi Kivityd3d25b02007-05-30 12:34:53 +0300564 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
565 sizeof *page);
566 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200567 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300568 set_page_private(virt_to_page(page->spt), (unsigned long)page);
569 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300570 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800572 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800574 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800575 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576}
577
Avi Kivity714b93d2007-01-05 16:36:53 -0800578static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
579 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800580{
581 struct kvm_pte_chain *pte_chain;
582 struct hlist_node *node;
583 int i;
584
585 if (!parent_pte)
586 return;
587 if (!page->multimapped) {
588 u64 *old = page->parent_pte;
589
590 if (!old) {
591 page->parent_pte = parent_pte;
592 return;
593 }
594 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800595 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800596 INIT_HLIST_HEAD(&page->parent_ptes);
597 hlist_add_head(&pte_chain->link, &page->parent_ptes);
598 pte_chain->parent_ptes[0] = old;
599 }
600 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
601 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
602 continue;
603 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
604 if (!pte_chain->parent_ptes[i]) {
605 pte_chain->parent_ptes[i] = parent_pte;
606 return;
607 }
608 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800609 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800610 BUG_ON(!pte_chain);
611 hlist_add_head(&pte_chain->link, &page->parent_ptes);
612 pte_chain->parent_ptes[0] = parent_pte;
613}
614
Avi Kivity90cb0522007-07-17 13:04:56 +0300615static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800616 u64 *parent_pte)
617{
618 struct kvm_pte_chain *pte_chain;
619 struct hlist_node *node;
620 int i;
621
622 if (!page->multimapped) {
623 BUG_ON(page->parent_pte != parent_pte);
624 page->parent_pte = NULL;
625 return;
626 }
627 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
628 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
629 if (!pte_chain->parent_ptes[i])
630 break;
631 if (pte_chain->parent_ptes[i] != parent_pte)
632 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800633 while (i + 1 < NR_PTE_CHAIN_ENTRIES
634 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800635 pte_chain->parent_ptes[i]
636 = pte_chain->parent_ptes[i + 1];
637 ++i;
638 }
639 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800640 if (i == 0) {
641 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300642 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800643 if (hlist_empty(&page->parent_ptes)) {
644 page->multimapped = 0;
645 page->parent_pte = NULL;
646 }
647 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800648 return;
649 }
650 BUG();
651}
652
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500653static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800654 gfn_t gfn)
655{
656 unsigned index;
657 struct hlist_head *bucket;
658 struct kvm_mmu_page *page;
659 struct hlist_node *node;
660
661 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
662 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500663 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800664 hlist_for_each_entry(page, node, bucket, hash_link)
665 if (page->gfn == gfn && !page->role.metaphysical) {
666 pgprintk("%s: found role %x\n",
667 __FUNCTION__, page->role.word);
668 return page;
669 }
670 return NULL;
671}
672
673static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
674 gfn_t gfn,
675 gva_t gaddr,
676 unsigned level,
677 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200678 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800679 u64 *parent_pte)
680{
681 union kvm_mmu_page_role role;
682 unsigned index;
683 unsigned quadrant;
684 struct hlist_head *bucket;
685 struct kvm_mmu_page *page;
686 struct hlist_node *node;
687
688 role.word = 0;
689 role.glevels = vcpu->mmu.root_level;
690 role.level = level;
691 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200692 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800693 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
694 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
695 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
696 role.quadrant = quadrant;
697 }
698 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
699 gfn, role.word);
700 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
701 bucket = &vcpu->kvm->mmu_page_hash[index];
702 hlist_for_each_entry(page, node, bucket, hash_link)
703 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800704 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800705 pgprintk("%s: found\n", __FUNCTION__);
706 return page;
707 }
708 page = kvm_mmu_alloc_page(vcpu, parent_pte);
709 if (!page)
710 return page;
711 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
712 page->gfn = gfn;
713 page->role = role;
714 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200715 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800716 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500717 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800718 return page;
719}
720
Avi Kivity90cb0522007-07-17 13:04:56 +0300721static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800722 struct kvm_mmu_page *page)
723{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800724 unsigned i;
725 u64 *pt;
726 u64 ent;
727
Avi Kivity47ad8e62007-05-06 15:50:58 +0300728 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800729
730 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
731 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200732 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200733 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200734 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800735 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300736 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800737 return;
738 }
739
740 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
741 ent = pt[i];
742
Avi Kivityc7addb92007-09-16 18:58:32 +0200743 pt[i] = shadow_trap_nonpresent_pte;
744 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800745 continue;
746 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300747 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800748 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300749 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800750}
751
Avi Kivity90cb0522007-07-17 13:04:56 +0300752static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800753 u64 *parent_pte)
754{
Avi Kivity90cb0522007-07-17 13:04:56 +0300755 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800756}
757
Avi Kivity12b7d282007-09-23 14:10:49 +0200758static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
759{
760 int i;
761
762 for (i = 0; i < KVM_MAX_VCPUS; ++i)
763 if (kvm->vcpus[i])
764 kvm->vcpus[i]->last_pte_updated = NULL;
765}
766
Avi Kivity90cb0522007-07-17 13:04:56 +0300767static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800768 struct kvm_mmu_page *page)
769{
770 u64 *parent_pte;
771
Avi Kivity4cee5762007-11-18 16:37:07 +0200772 ++kvm->stat.mmu_shadow_zapped;
Avi Kivitya4360362007-01-05 16:36:45 -0800773 while (page->multimapped || page->parent_pte) {
774 if (!page->multimapped)
775 parent_pte = page->parent_pte;
776 else {
777 struct kvm_pte_chain *chain;
778
779 chain = container_of(page->parent_ptes.first,
780 struct kvm_pte_chain, link);
781 parent_pte = chain->parent_ptes[0];
782 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800783 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300784 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200785 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800786 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300787 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800788 if (!page->root_count) {
789 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300790 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200791 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300792 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200793 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800794}
795
Izik Eidus82ce2c92007-10-02 18:52:55 +0200796/*
797 * Changing the number of mmu pages allocated to the vm
798 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
799 */
800void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
801{
802 /*
803 * If we set the number of mmu pages to be smaller be than the
804 * number of actived pages , we must to free some mmu pages before we
805 * change the value
806 */
807
808 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
809 kvm_nr_mmu_pages) {
810 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
811 - kvm->n_free_mmu_pages;
812
813 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
814 struct kvm_mmu_page *page;
815
816 page = container_of(kvm->active_mmu_pages.prev,
817 struct kvm_mmu_page, link);
818 kvm_mmu_zap_page(kvm, page);
819 n_used_mmu_pages--;
820 }
821 kvm->n_free_mmu_pages = 0;
822 }
823 else
824 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
825 - kvm->n_alloc_mmu_pages;
826
827 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
828}
829
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500830static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800831{
832 unsigned index;
833 struct hlist_head *bucket;
834 struct kvm_mmu_page *page;
835 struct hlist_node *node, *n;
836 int r;
837
838 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
839 r = 0;
840 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500841 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800842 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
843 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800844 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
845 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500846 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800847 r = 1;
848 }
849 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800850}
851
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500852static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300853{
854 struct kvm_mmu_page *page;
855
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500856 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300857 pgprintk("%s: zap %lx %x\n",
858 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500859 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300860 }
861}
862
Avi Kivity38c335f2007-11-21 14:20:22 +0200863static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864{
Avi Kivity38c335f2007-11-21 14:20:22 +0200865 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 struct kvm_mmu_page *page_head = page_header(__pa(pte));
867
868 __set_bit(slot, &page_head->slot_bitmap);
869}
870
Avi Kivity039576c2007-03-20 12:46:50 +0200871struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
872{
873 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
874
875 if (gpa == UNMAPPED_GVA)
876 return NULL;
Avi Kivity1d28f5f2007-11-21 15:01:44 +0200877 return gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200878}
879
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
881{
882}
883
Avi Kivity3f3e7122007-11-21 14:54:16 +0200884static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885{
886 int level = PT32E_ROOT_LEVEL;
887 hpa_t table_addr = vcpu->mmu.root_hpa;
888
889 for (; ; level--) {
890 u32 index = PT64_INDEX(v, level);
891 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800892 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893
894 ASSERT(VALID_PAGE(table_addr));
895 table = __va(table_addr);
896
897 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200898 int was_rmapped;
899
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800900 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200901 was_rmapped = is_rmap_pte(pte);
Izik Eidus2065b372007-11-20 11:30:04 +0200902 if (is_shadow_present_pte(pte) && is_writeble_pte(pte)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200903 kvm_release_page_clean(page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800904 return 0;
Izik Eidus2065b372007-11-20 11:30:04 +0200905 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
Avi Kivity38c335f2007-11-21 14:20:22 +0200907 page_header_update_slot(vcpu->kvm, table,
908 v >> PAGE_SHIFT);
Avi Kivity3f3e7122007-11-21 14:54:16 +0200909 table[index] = page_to_phys(page)
910 | PT_PRESENT_MASK | PT_WRITABLE_MASK
911 | PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200912 if (!was_rmapped)
913 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200914 else
Izik Eidusb4231d62007-11-20 11:49:33 +0200915 kvm_release_page_clean(page);
916
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 return 0;
918 }
919
Avi Kivityc7addb92007-09-16 18:58:32 +0200920 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800921 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800922 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
925 >> PAGE_SHIFT;
926 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
927 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200928 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800929 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidusb4231d62007-11-20 11:49:33 +0200931 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 return -ENOMEM;
933 }
934
Avi Kivity47ad8e62007-05-06 15:50:58 +0300935 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800936 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 }
938 table_addr = table[index] & PT64_BASE_ADDR_MASK;
939 }
940}
941
Avi Kivityc7addb92007-09-16 18:58:32 +0200942static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
943 struct kvm_mmu_page *sp)
944{
945 int i;
946
947 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
948 sp->spt[i] = shadow_trap_nonpresent_pte;
949}
950
Avi Kivity17ac10a2007-01-05 16:36:40 -0800951static void mmu_free_roots(struct kvm_vcpu *vcpu)
952{
953 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800954 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800955
Avi Kivity7b53aa52007-06-05 12:17:03 +0300956 if (!VALID_PAGE(vcpu->mmu.root_hpa))
957 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800958#ifdef CONFIG_X86_64
959 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
960 hpa_t root = vcpu->mmu.root_hpa;
961
Avi Kivity3bb65a22007-01-05 16:36:51 -0800962 page = page_header(root);
963 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800964 vcpu->mmu.root_hpa = INVALID_PAGE;
965 return;
966 }
967#endif
968 for (i = 0; i < 4; ++i) {
969 hpa_t root = vcpu->mmu.pae_root[i];
970
Avi Kivity417726a2007-04-12 17:35:58 +0300971 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300972 root &= PT64_BASE_ADDR_MASK;
973 page = page_header(root);
974 --page->root_count;
975 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800976 vcpu->mmu.pae_root[i] = INVALID_PAGE;
977 }
978 vcpu->mmu.root_hpa = INVALID_PAGE;
979}
980
981static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
982{
983 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800985 struct kvm_mmu_page *page;
986
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800987 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800988
989#ifdef CONFIG_X86_64
990 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
991 hpa_t root = vcpu->mmu.root_hpa;
992
993 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800994 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200995 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300996 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800997 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800998 vcpu->mmu.root_hpa = root;
999 return;
1000 }
1001#endif
1002 for (i = 0; i < 4; ++i) {
1003 hpa_t root = vcpu->mmu.pae_root[i];
1004
1005 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001006 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1007 if (!is_present_pte(vcpu->pdptrs[i])) {
1008 vcpu->mmu.pae_root[i] = 0;
1009 continue;
1010 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001011 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001012 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001013 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -08001014 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001015 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001016 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001017 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001018 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001019 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1020 }
1021 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1022}
1023
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1025{
1026 return vaddr;
1027}
1028
1029static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001030 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031{
Avi Kivity3f3e7122007-11-21 14:54:16 +02001032 struct page *page;
Avi Kivitye2dec932007-01-05 16:36:54 -08001033 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034
Avi Kivitye2dec932007-01-05 16:36:54 -08001035 r = mmu_topup_memory_caches(vcpu);
1036 if (r)
1037 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001038
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 ASSERT(vcpu);
1040 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1041
Avi Kivity3f3e7122007-11-21 14:54:16 +02001042 page = gfn_to_page(vcpu->kvm, gva >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043
Avi Kivity3f3e7122007-11-21 14:54:16 +02001044 if (is_error_page(page)) {
1045 kvm_release_page_clean(page);
Avi Kivityebeace82007-01-05 16:36:47 -08001046 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001047 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048
Avi Kivity3f3e7122007-11-21 14:54:16 +02001049 return nonpaging_map(vcpu, gva & PAGE_MASK, page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050}
1051
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052static void nonpaging_free(struct kvm_vcpu *vcpu)
1053{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001054 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055}
1056
1057static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1058{
1059 struct kvm_mmu *context = &vcpu->mmu;
1060
1061 context->new_cr3 = nonpaging_new_cr3;
1062 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 context->gva_to_gpa = nonpaging_gva_to_gpa;
1064 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001065 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001066 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001068 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 return 0;
1070}
1071
Avi Kivityd835dfe2007-11-21 02:57:59 +02001072void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001074 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001075 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076}
1077
1078static void paging_new_cr3(struct kvm_vcpu *vcpu)
1079{
Avi Kivity374cbac2007-01-05 16:36:43 -08001080 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001081 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082}
1083
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084static void inject_page_fault(struct kvm_vcpu *vcpu,
1085 u64 addr,
1086 u32 err_code)
1087{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001088 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089}
1090
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091static void paging_free(struct kvm_vcpu *vcpu)
1092{
1093 nonpaging_free(vcpu);
1094}
1095
1096#define PTTYPE 64
1097#include "paging_tmpl.h"
1098#undef PTTYPE
1099
1100#define PTTYPE 32
1101#include "paging_tmpl.h"
1102#undef PTTYPE
1103
Avi Kivity17ac10a2007-01-05 16:36:40 -08001104static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105{
1106 struct kvm_mmu *context = &vcpu->mmu;
1107
1108 ASSERT(is_pae(vcpu));
1109 context->new_cr3 = paging_new_cr3;
1110 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001112 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001114 context->root_level = level;
1115 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001116 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 return 0;
1118}
1119
Avi Kivity17ac10a2007-01-05 16:36:40 -08001120static int paging64_init_context(struct kvm_vcpu *vcpu)
1121{
1122 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1123}
1124
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125static int paging32_init_context(struct kvm_vcpu *vcpu)
1126{
1127 struct kvm_mmu *context = &vcpu->mmu;
1128
1129 context->new_cr3 = paging_new_cr3;
1130 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 context->gva_to_gpa = paging32_gva_to_gpa;
1132 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001133 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 context->root_level = PT32_ROOT_LEVEL;
1135 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001136 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 return 0;
1138}
1139
1140static int paging32E_init_context(struct kvm_vcpu *vcpu)
1141{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001142 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143}
1144
1145static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1146{
1147 ASSERT(vcpu);
1148 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1149
1150 if (!is_paging(vcpu))
1151 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001152 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 return paging64_init_context(vcpu);
1154 else if (is_pae(vcpu))
1155 return paging32E_init_context(vcpu);
1156 else
1157 return paging32_init_context(vcpu);
1158}
1159
1160static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1161{
1162 ASSERT(vcpu);
1163 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1164 vcpu->mmu.free(vcpu);
1165 vcpu->mmu.root_hpa = INVALID_PAGE;
1166 }
1167}
1168
1169int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1170{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001171 destroy_kvm_mmu(vcpu);
1172 return init_kvm_mmu(vcpu);
1173}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001174EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001175
1176int kvm_mmu_load(struct kvm_vcpu *vcpu)
1177{
Avi Kivity714b93d2007-01-05 16:36:53 -08001178 int r;
1179
Shaohua Li11ec2802007-07-23 14:51:37 +08001180 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001181 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001182 if (r)
1183 goto out;
1184 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001185 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001186 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001187out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001188 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001189 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001191EXPORT_SYMBOL_GPL(kvm_mmu_load);
1192
1193void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1194{
1195 mmu_free_roots(vcpu);
1196}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197
Avi Kivity09072da2007-05-01 14:16:52 +03001198static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001199 struct kvm_mmu_page *page,
1200 u64 *spte)
1201{
1202 u64 pte;
1203 struct kvm_mmu_page *child;
1204
1205 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001206 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001207 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001208 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001209 else {
1210 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001211 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001212 }
1213 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001214 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001215}
1216
Avi Kivity00284252007-05-01 16:53:31 +03001217static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1218 struct kvm_mmu_page *page,
1219 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001220 const void *new, int bytes,
1221 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001222{
Avi Kivity4cee5762007-11-18 16:37:07 +02001223 if (page->role.level != PT_PAGE_TABLE_LEVEL) {
1224 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001225 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001226 }
Avi Kivity00284252007-05-01 16:53:31 +03001227
Avi Kivity4cee5762007-11-18 16:37:07 +02001228 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity00284252007-05-01 16:53:31 +03001229 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001230 paging32_update_pte(vcpu, page, spte, new, bytes,
1231 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001232 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001233 paging64_update_pte(vcpu, page, spte, new, bytes,
1234 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001235}
1236
Avi Kivity79539ce2007-11-21 02:06:21 +02001237static bool need_remote_flush(u64 old, u64 new)
1238{
1239 if (!is_shadow_present_pte(old))
1240 return false;
1241 if (!is_shadow_present_pte(new))
1242 return true;
1243 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1244 return true;
1245 old ^= PT64_NX_MASK;
1246 new ^= PT64_NX_MASK;
1247 return (old & ~new & PT64_PERM_MASK) != 0;
1248}
1249
1250static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1251{
1252 if (need_remote_flush(old, new))
1253 kvm_flush_remote_tlbs(vcpu->kvm);
1254 else
1255 kvm_mmu_flush_tlb(vcpu);
1256}
1257
Avi Kivity12b7d282007-09-23 14:10:49 +02001258static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1259{
1260 u64 *spte = vcpu->last_pte_updated;
1261
1262 return !!(spte && (*spte & PT_ACCESSED_MASK));
1263}
1264
Avi Kivity09072da2007-05-01 14:16:52 +03001265void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001266 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001267{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001268 gfn_t gfn = gpa >> PAGE_SHIFT;
1269 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001270 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001271 struct hlist_head *bucket;
1272 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001273 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001274 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001275 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001276 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001277 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001278 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001279 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001280 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001281 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001282 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001283
Avi Kivityda4a00f2007-01-05 16:36:44 -08001284 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity4cee5762007-11-18 16:37:07 +02001285 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001286 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001287 if (gfn == vcpu->last_pt_write_gfn
1288 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001289 ++vcpu->last_pt_write_count;
1290 if (vcpu->last_pt_write_count >= 3)
1291 flooded = 1;
1292 } else {
1293 vcpu->last_pt_write_gfn = gfn;
1294 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001295 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001296 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001297 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1298 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001299 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001300 if (page->gfn != gfn || page->role.metaphysical)
1301 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001302 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1303 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001304 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001305 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001306 /*
1307 * Misaligned accesses are too much trouble to fix
1308 * up; also, they usually indicate a page is not used
1309 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001310 *
1311 * If we're seeing too many writes to a page,
1312 * it may no longer be a page table, or we may be
1313 * forking, in which case it is better to unmap the
1314 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001315 */
1316 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1317 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001318 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001319 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001320 continue;
1321 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001322 page_offset = offset;
1323 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001324 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001325 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001326 page_offset <<= 1; /* 32->64 */
1327 /*
1328 * A 32-bit pde maps 4MB while the shadow pdes map
1329 * only 2MB. So we need to double the offset again
1330 * and zap two pdes instead of one.
1331 */
1332 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001333 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001334 page_offset <<= 1;
1335 npte = 2;
1336 }
Avi Kivityfce06572007-05-01 16:44:05 +03001337 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001338 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001339 if (quadrant != page->role.quadrant)
1340 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001341 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001342 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001343 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001344 entry = *spte;
Avi Kivity09072da2007-05-01 14:16:52 +03001345 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001346 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1347 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001348 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001349 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001350 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001351 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001352 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001353}
1354
Avi Kivitya4360362007-01-05 16:36:45 -08001355int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1356{
1357 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1358
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001359 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001360}
1361
Avi Kivity22d95b12007-09-14 20:26:06 +03001362void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001363{
1364 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1365 struct kvm_mmu_page *page;
1366
1367 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1368 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001369 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001370 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001371 }
1372}
Avi Kivityebeace82007-01-05 16:36:47 -08001373
Avi Kivity30677142007-10-28 18:48:59 +02001374int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1375{
1376 int r;
1377 enum emulation_result er;
1378
1379 mutex_lock(&vcpu->kvm->lock);
1380 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1381 if (r < 0)
1382 goto out;
1383
1384 if (!r) {
1385 r = 1;
1386 goto out;
1387 }
1388
Avi Kivityb733bfb2007-10-28 18:52:05 +02001389 r = mmu_topup_memory_caches(vcpu);
1390 if (r)
1391 goto out;
1392
Avi Kivity30677142007-10-28 18:48:59 +02001393 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1394 mutex_unlock(&vcpu->kvm->lock);
1395
1396 switch (er) {
1397 case EMULATE_DONE:
1398 return 1;
1399 case EMULATE_DO_MMIO:
1400 ++vcpu->stat.mmio_exits;
1401 return 0;
1402 case EMULATE_FAIL:
1403 kvm_report_emulation_failure(vcpu, "pagetable");
1404 return 1;
1405 default:
1406 BUG();
1407 }
1408out:
1409 mutex_unlock(&vcpu->kvm->lock);
1410 return r;
1411}
1412EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1413
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414static void free_mmu_pages(struct kvm_vcpu *vcpu)
1415{
Avi Kivityf51234c2007-01-05 16:36:52 -08001416 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417
Avi Kivityf51234c2007-01-05 16:36:52 -08001418 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1419 page = container_of(vcpu->kvm->active_mmu_pages.next,
1420 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001421 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001422 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001423 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424}
1425
1426static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1427{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001428 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 int i;
1430
1431 ASSERT(vcpu);
1432
Izik Eidus82ce2c92007-10-02 18:52:55 +02001433 if (vcpu->kvm->n_requested_mmu_pages)
1434 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1435 else
1436 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001437 /*
1438 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1439 * Therefore we need to allocate shadow page tables in the first
1440 * 4GB of memory, which happens to fit the DMA32 zone.
1441 */
1442 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1443 if (!page)
1444 goto error_1;
1445 vcpu->mmu.pae_root = page_address(page);
1446 for (i = 0; i < 4; ++i)
1447 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1448
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 return 0;
1450
1451error_1:
1452 free_mmu_pages(vcpu);
1453 return -ENOMEM;
1454}
1455
Ingo Molnar8018c272006-12-29 16:50:01 -08001456int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 ASSERT(vcpu);
1459 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460
Ingo Molnar8018c272006-12-29 16:50:01 -08001461 return alloc_mmu_pages(vcpu);
1462}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463
Ingo Molnar8018c272006-12-29 16:50:01 -08001464int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1465{
1466 ASSERT(vcpu);
1467 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001468
Ingo Molnar8018c272006-12-29 16:50:01 -08001469 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470}
1471
1472void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1473{
1474 ASSERT(vcpu);
1475
1476 destroy_kvm_mmu(vcpu);
1477 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001478 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479}
1480
Avi Kivity90cb0522007-07-17 13:04:56 +03001481void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482{
1483 struct kvm_mmu_page *page;
1484
1485 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1486 int i;
1487 u64 *pt;
1488
1489 if (!test_bit(slot, &page->slot_bitmap))
1490 continue;
1491
Avi Kivity47ad8e62007-05-06 15:50:58 +03001492 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1494 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001495 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497 }
1498}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001499
Avi Kivity90cb0522007-07-17 13:04:56 +03001500void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001501{
Avi Kivity90cb0522007-07-17 13:04:56 +03001502 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001503
Avi Kivity90cb0522007-07-17 13:04:56 +03001504 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1505 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001506
Avi Kivity90cb0522007-07-17 13:04:56 +03001507 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001508}
1509
Avi Kivityb5a33a72007-04-15 16:31:09 +03001510void kvm_mmu_module_exit(void)
1511{
1512 if (pte_chain_cache)
1513 kmem_cache_destroy(pte_chain_cache);
1514 if (rmap_desc_cache)
1515 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001516 if (mmu_page_header_cache)
1517 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001518}
1519
1520int kvm_mmu_module_init(void)
1521{
1522 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1523 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001524 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001525 if (!pte_chain_cache)
1526 goto nomem;
1527 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1528 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001529 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001530 if (!rmap_desc_cache)
1531 goto nomem;
1532
Avi Kivityd3d25b02007-05-30 12:34:53 +03001533 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1534 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001535 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001536 if (!mmu_page_header_cache)
1537 goto nomem;
1538
Avi Kivityb5a33a72007-04-15 16:31:09 +03001539 return 0;
1540
1541nomem:
1542 kvm_mmu_module_exit();
1543 return -ENOMEM;
1544}
1545
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001546/*
1547 * Caculate mmu pages needed for kvm.
1548 */
1549unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1550{
1551 int i;
1552 unsigned int nr_mmu_pages;
1553 unsigned int nr_pages = 0;
1554
1555 for (i = 0; i < kvm->nmemslots; i++)
1556 nr_pages += kvm->memslots[i].npages;
1557
1558 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1559 nr_mmu_pages = max(nr_mmu_pages,
1560 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1561
1562 return nr_mmu_pages;
1563}
1564
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001565#ifdef AUDIT
1566
1567static const char *audit_msg;
1568
1569static gva_t canonicalize(gva_t gva)
1570{
1571#ifdef CONFIG_X86_64
1572 gva = (long long)(gva << 16) >> 16;
1573#endif
1574 return gva;
1575}
1576
1577static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1578 gva_t va, int level)
1579{
1580 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1581 int i;
1582 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1583
1584 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1585 u64 ent = pt[i];
1586
Avi Kivityc7addb92007-09-16 18:58:32 +02001587 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001588 continue;
1589
1590 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001591 if (level > 1) {
1592 if (ent == shadow_notrap_nonpresent_pte)
1593 printk(KERN_ERR "audit: (%s) nontrapping pte"
1594 " in nonleaf level: levels %d gva %lx"
1595 " level %d pte %llx\n", audit_msg,
1596 vcpu->mmu.root_level, va, level, ent);
1597
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001598 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001599 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001600 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001601 struct page *page = gpa_to_page(vcpu, gpa);
1602 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001603
Avi Kivityc7addb92007-09-16 18:58:32 +02001604 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001605 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001606 printk(KERN_ERR "xx audit error: (%s) levels %d"
1607 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001608 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001609 va, gpa, hpa, ent,
1610 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001611 else if (ent == shadow_notrap_nonpresent_pte
1612 && !is_error_hpa(hpa))
1613 printk(KERN_ERR "audit: (%s) notrap shadow,"
1614 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001615 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001616
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001617 }
1618 }
1619}
1620
1621static void audit_mappings(struct kvm_vcpu *vcpu)
1622{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001623 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001624
1625 if (vcpu->mmu.root_level == 4)
1626 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1627 else
1628 for (i = 0; i < 4; ++i)
1629 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1630 audit_mappings_page(vcpu,
1631 vcpu->mmu.pae_root[i],
1632 i << 30,
1633 2);
1634}
1635
1636static int count_rmaps(struct kvm_vcpu *vcpu)
1637{
1638 int nmaps = 0;
1639 int i, j, k;
1640
1641 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1642 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1643 struct kvm_rmap_desc *d;
1644
1645 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001646 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001647
Izik Eidus290fc382007-09-27 14:11:22 +02001648 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001649 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001650 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001651 ++nmaps;
1652 continue;
1653 }
Izik Eidus290fc382007-09-27 14:11:22 +02001654 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001655 while (d) {
1656 for (k = 0; k < RMAP_EXT; ++k)
1657 if (d->shadow_ptes[k])
1658 ++nmaps;
1659 else
1660 break;
1661 d = d->more;
1662 }
1663 }
1664 }
1665 return nmaps;
1666}
1667
1668static int count_writable_mappings(struct kvm_vcpu *vcpu)
1669{
1670 int nmaps = 0;
1671 struct kvm_mmu_page *page;
1672 int i;
1673
1674 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001675 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001676
1677 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1678 continue;
1679
1680 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1681 u64 ent = pt[i];
1682
1683 if (!(ent & PT_PRESENT_MASK))
1684 continue;
1685 if (!(ent & PT_WRITABLE_MASK))
1686 continue;
1687 ++nmaps;
1688 }
1689 }
1690 return nmaps;
1691}
1692
1693static void audit_rmap(struct kvm_vcpu *vcpu)
1694{
1695 int n_rmap = count_rmaps(vcpu);
1696 int n_actual = count_writable_mappings(vcpu);
1697
1698 if (n_rmap != n_actual)
1699 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1700 __FUNCTION__, audit_msg, n_rmap, n_actual);
1701}
1702
1703static void audit_write_protection(struct kvm_vcpu *vcpu)
1704{
1705 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001706 struct kvm_memory_slot *slot;
1707 unsigned long *rmapp;
1708 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001709
1710 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001711 if (page->role.metaphysical)
1712 continue;
1713
Izik Eidus290fc382007-09-27 14:11:22 +02001714 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1715 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1716 rmapp = &slot->rmap[gfn - slot->base_gfn];
1717 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001718 printk(KERN_ERR "%s: (%s) shadow page has writable"
1719 " mappings: gfn %lx role %x\n",
1720 __FUNCTION__, audit_msg, page->gfn,
1721 page->role.word);
1722 }
1723}
1724
1725static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1726{
1727 int olddbg = dbg;
1728
1729 dbg = 0;
1730 audit_msg = msg;
1731 audit_rmap(vcpu);
1732 audit_write_protection(vcpu);
1733 audit_mappings(vcpu);
1734 dbg = olddbg;
1735}
1736
1737#endif