blob: a9fed594ac580e319c40da8fd9457b898b5b5e0e [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
21#include "kvm.h"
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080022#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
29
30#include <asm/page.h>
31#include <asm/cmpxchg.h>
32
Avi Kivity37a7d8b2007-01-05 16:36:56 -080033#undef MMU_DEBUG
34
35#undef AUDIT
36
37#ifdef AUDIT
38static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
39#else
40static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
41#endif
42
43#ifdef MMU_DEBUG
44
45#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
46#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
47
48#else
49
50#define pgprintk(x...) do { } while (0)
51#define rmap_printk(x...) do { } while (0)
52
53#endif
54
55#if defined(MMU_DEBUG) || defined(AUDIT)
56static int dbg = 1;
57#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080058
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080059#ifndef MMU_DEBUG
60#define ASSERT(x) do { } while (0)
61#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080062#define ASSERT(x) \
63 if (!(x)) { \
64 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
65 __FILE__, __LINE__, #x); \
66 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080067#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivitycea0f0e2007-01-05 16:36:43 -080069#define PT64_PT_BITS 9
70#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
71#define PT32_PT_BITS 10
72#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
74#define PT_WRITABLE_SHIFT 1
75
76#define PT_PRESENT_MASK (1ULL << 0)
77#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
78#define PT_USER_MASK (1ULL << 2)
79#define PT_PWT_MASK (1ULL << 3)
80#define PT_PCD_MASK (1ULL << 4)
81#define PT_ACCESSED_MASK (1ULL << 5)
82#define PT_DIRTY_MASK (1ULL << 6)
83#define PT_PAGE_SIZE_MASK (1ULL << 7)
84#define PT_PAT_MASK (1ULL << 7)
85#define PT_GLOBAL_MASK (1ULL << 8)
86#define PT64_NX_MASK (1ULL << 63)
87
88#define PT_PAT_SHIFT 7
89#define PT_DIR_PAT_SHIFT 12
90#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
91
92#define PT32_DIR_PSE36_SIZE 4
93#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040094#define PT32_DIR_PSE36_MASK \
95 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
97
Avi Kivity6aa8b732006-12-10 02:21:36 -080098#define PT_FIRST_AVAIL_BITS_SHIFT 9
99#define PT64_SECOND_AVAIL_BITS_SHIFT 52
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103#define VALID_PAGE(x) ((x) != INVALID_PAGE)
104
105#define PT64_LEVEL_BITS 9
106
107#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400108 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109
110#define PT64_LEVEL_MASK(level) \
111 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
112
113#define PT64_INDEX(address, level)\
114 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
115
116
117#define PT32_LEVEL_BITS 10
118
119#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400120 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121
122#define PT32_LEVEL_MASK(level) \
123 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
124
125#define PT32_INDEX(address, level)\
126 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
127
128
Avi Kivity27aba762007-03-09 13:04:31 +0200129#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130#define PT64_DIR_BASE_ADDR_MASK \
131 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
132
133#define PT32_BASE_ADDR_MASK PAGE_MASK
134#define PT32_DIR_BASE_ADDR_MASK \
135 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
136
Avi Kivity79539ce2007-11-21 02:06:21 +0200137#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
138 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139
140#define PFERR_PRESENT_MASK (1U << 0)
141#define PFERR_WRITE_MASK (1U << 1)
142#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800143#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144
145#define PT64_ROOT_LEVEL 4
146#define PT32_ROOT_LEVEL 2
147#define PT32E_ROOT_LEVEL 3
148
149#define PT_DIRECTORY_LEVEL 2
150#define PT_PAGE_TABLE_LEVEL 1
151
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800152#define RMAP_EXT 4
153
154struct kvm_rmap_desc {
155 u64 *shadow_ptes[RMAP_EXT];
156 struct kvm_rmap_desc *more;
157};
158
Avi Kivityb5a33a72007-04-15 16:31:09 +0300159static struct kmem_cache *pte_chain_cache;
160static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300161static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300162
Avi Kivityc7addb92007-09-16 18:58:32 +0200163static u64 __read_mostly shadow_trap_nonpresent_pte;
164static u64 __read_mostly shadow_notrap_nonpresent_pte;
165
166void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
167{
168 shadow_trap_nonpresent_pte = trap_pte;
169 shadow_notrap_nonpresent_pte = notrap_pte;
170}
171EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
172
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173static int is_write_protection(struct kvm_vcpu *vcpu)
174{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000175 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176}
177
178static int is_cpuid_PSE36(void)
179{
180 return 1;
181}
182
Avi Kivity73b10872007-01-26 00:56:41 -0800183static int is_nx(struct kvm_vcpu *vcpu)
184{
185 return vcpu->shadow_efer & EFER_NX;
186}
187
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188static int is_present_pte(unsigned long pte)
189{
190 return pte & PT_PRESENT_MASK;
191}
192
Avi Kivityc7addb92007-09-16 18:58:32 +0200193static int is_shadow_present_pte(u64 pte)
194{
195 pte &= ~PT_SHADOW_IO_MARK;
196 return pte != shadow_trap_nonpresent_pte
197 && pte != shadow_notrap_nonpresent_pte;
198}
199
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200static int is_writeble_pte(unsigned long pte)
201{
202 return pte & PT_WRITABLE_MASK;
203}
204
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200205static int is_dirty_pte(unsigned long pte)
206{
207 return pte & PT_DIRTY_MASK;
208}
209
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210static int is_io_pte(unsigned long pte)
211{
212 return pte & PT_SHADOW_IO_MARK;
213}
214
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800215static int is_rmap_pte(u64 pte)
216{
Izik Eidus9647c142007-10-16 14:43:46 +0200217 return pte != shadow_trap_nonpresent_pte
218 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800219}
220
Avi Kivityda9285212007-11-21 13:54:47 +0200221static gfn_t pse36_gfn_delta(u32 gpte)
222{
223 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
224
225 return (gpte & PT32_DIR_PSE36_MASK) << shift;
226}
227
Avi Kivitye663ee62007-05-31 15:46:04 +0300228static void set_shadow_pte(u64 *sptep, u64 spte)
229{
230#ifdef CONFIG_X86_64
231 set_64bit((unsigned long *)sptep, spte);
232#else
233 set_64bit((unsigned long long *)sptep, spte);
234#endif
235}
236
Avi Kivitye2dec932007-01-05 16:36:54 -0800237static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300238 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800239{
240 void *obj;
241
242 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800243 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800244 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300245 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800246 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800247 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800248 cache->objects[cache->nobjs++] = obj;
249 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800250 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800251}
252
253static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
254{
255 while (mc->nobjs)
256 kfree(mc->objects[--mc->nobjs]);
257}
258
Avi Kivityc1158e62007-07-20 08:18:27 +0300259static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300260 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300261{
262 struct page *page;
263
264 if (cache->nobjs >= min)
265 return 0;
266 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300267 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300268 if (!page)
269 return -ENOMEM;
270 set_page_private(page, 0);
271 cache->objects[cache->nobjs++] = page_address(page);
272 }
273 return 0;
274}
275
276static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
277{
278 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300279 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300280}
281
Avi Kivity8c438502007-04-16 11:53:17 +0300282static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
283{
284 int r;
285
Avi Kivity22d95b12007-09-14 20:26:06 +0300286 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300287 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
288 pte_chain_cache, 4);
289 if (r)
290 goto out;
291 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
292 rmap_desc_cache, 1);
293 if (r)
294 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200295 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300296 if (r)
297 goto out;
298 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
299 mmu_page_header_cache, 4);
300out:
Avi Kivity8c438502007-04-16 11:53:17 +0300301 return r;
302}
303
Avi Kivity714b93d2007-01-05 16:36:53 -0800304static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
305{
306 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
307 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300308 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300309 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800310}
311
312static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
313 size_t size)
314{
315 void *p;
316
317 BUG_ON(!mc->nobjs);
318 p = mc->objects[--mc->nobjs];
319 memset(p, 0, size);
320 return p;
321}
322
Avi Kivity714b93d2007-01-05 16:36:53 -0800323static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
324{
325 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
326 sizeof(struct kvm_pte_chain));
327}
328
Avi Kivity90cb0522007-07-17 13:04:56 +0300329static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800330{
Avi Kivity90cb0522007-07-17 13:04:56 +0300331 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800332}
333
334static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
335{
336 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
337 sizeof(struct kvm_rmap_desc));
338}
339
Avi Kivity90cb0522007-07-17 13:04:56 +0300340static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800341{
Avi Kivity90cb0522007-07-17 13:04:56 +0300342 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800343}
344
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800345/*
Izik Eidus290fc382007-09-27 14:11:22 +0200346 * Take gfn and return the reverse mapping to it.
347 * Note: gfn must be unaliased before this function get called
348 */
349
350static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
351{
352 struct kvm_memory_slot *slot;
353
354 slot = gfn_to_memslot(kvm, gfn);
355 return &slot->rmap[gfn - slot->base_gfn];
356}
357
358/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800359 * Reverse mapping data structures:
360 *
Izik Eidus290fc382007-09-27 14:11:22 +0200361 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
362 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800363 *
Izik Eidus290fc382007-09-27 14:11:22 +0200364 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
365 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800366 */
Izik Eidus290fc382007-09-27 14:11:22 +0200367static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800368{
Izik Eidus290fc382007-09-27 14:11:22 +0200369 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200371 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 int i;
373
374 if (!is_rmap_pte(*spte))
375 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200376 gfn = unalias_gfn(vcpu->kvm, gfn);
377 page = page_header(__pa(spte));
378 page->gfns[spte - page->spt] = gfn;
379 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
380 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800381 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200382 *rmapp = (unsigned long)spte;
383 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800384 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800385 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200386 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800387 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200388 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800389 } else {
390 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200391 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800392 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
393 desc = desc->more;
394 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800395 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800396 desc = desc->more;
397 }
398 for (i = 0; desc->shadow_ptes[i]; ++i)
399 ;
400 desc->shadow_ptes[i] = spte;
401 }
402}
403
Izik Eidus290fc382007-09-27 14:11:22 +0200404static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405 struct kvm_rmap_desc *desc,
406 int i,
407 struct kvm_rmap_desc *prev_desc)
408{
409 int j;
410
411 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
412 ;
413 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000414 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800415 if (j != 0)
416 return;
417 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200418 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800419 else
420 if (prev_desc)
421 prev_desc->more = desc->more;
422 else
Izik Eidus290fc382007-09-27 14:11:22 +0200423 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300424 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800425}
426
Izik Eidus290fc382007-09-27 14:11:22 +0200427static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800428{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800429 struct kvm_rmap_desc *desc;
430 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200431 struct kvm_mmu_page *page;
Izik Eidusb4231d62007-11-20 11:49:33 +0200432 struct page *release_page;
Izik Eidus290fc382007-09-27 14:11:22 +0200433 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800434 int i;
435
436 if (!is_rmap_pte(*spte))
437 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200438 page = page_header(__pa(spte));
Izik Eidusb4231d62007-11-20 11:49:33 +0200439 release_page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
440 if (is_writeble_pte(*spte))
441 kvm_release_page_dirty(release_page);
442 else
443 kvm_release_page_clean(release_page);
Izik Eidus290fc382007-09-27 14:11:22 +0200444 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
445 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800446 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
447 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200448 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800449 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200450 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800451 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
452 spte, *spte);
453 BUG();
454 }
Izik Eidus290fc382007-09-27 14:11:22 +0200455 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800456 } else {
457 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200458 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800459 prev_desc = NULL;
460 while (desc) {
461 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
462 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200463 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800464 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800465 prev_desc);
466 return;
467 }
468 prev_desc = desc;
469 desc = desc->more;
470 }
471 BUG();
472 }
473}
474
Izik Eidus98348e92007-10-16 14:42:30 +0200475static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800476{
Avi Kivity374cbac2007-01-05 16:36:43 -0800477 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200478 struct kvm_rmap_desc *prev_desc;
479 u64 *prev_spte;
480 int i;
481
482 if (!*rmapp)
483 return NULL;
484 else if (!(*rmapp & 1)) {
485 if (!spte)
486 return (u64 *)*rmapp;
487 return NULL;
488 }
489 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
490 prev_desc = NULL;
491 prev_spte = NULL;
492 while (desc) {
493 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
494 if (prev_spte == spte)
495 return desc->shadow_ptes[i];
496 prev_spte = desc->shadow_ptes[i];
497 }
498 desc = desc->more;
499 }
500 return NULL;
501}
502
503static void rmap_write_protect(struct kvm *kvm, u64 gfn)
504{
Izik Eidus290fc382007-09-27 14:11:22 +0200505 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800506 u64 *spte;
507
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);
Izik Eidus9647c142007-10-16 14:43:46 +0200516 if (is_writeble_pte(*spte))
517 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500518 kvm_flush_remote_tlbs(kvm);
Izik Eidus9647c142007-10-16 14:43:46 +0200519 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800520 }
521}
522
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800523#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300524static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525{
Avi Kivity139bdb22007-01-05 16:36:50 -0800526 u64 *pos;
527 u64 *end;
528
Avi Kivity47ad8e62007-05-06 15:50:58 +0300529 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200530 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800531 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
532 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800534 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535 return 1;
536}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800537#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538
Avi Kivity90cb0522007-07-17 13:04:56 +0300539static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300540 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800541{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300542 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300543 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300544 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200545 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300546 kfree(page_head);
547 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800548}
549
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800550static unsigned kvm_page_table_hashfn(gfn_t gfn)
551{
552 return gfn;
553}
554
Avi Kivity25c0de22007-01-05 16:36:42 -0800555static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
556 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557{
558 struct kvm_mmu_page *page;
559
Avi Kivityd3d25b02007-05-30 12:34:53 +0300560 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800561 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562
Avi Kivityd3d25b02007-05-30 12:34:53 +0300563 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
564 sizeof *page);
565 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200566 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300567 set_page_private(virt_to_page(page->spt), (unsigned long)page);
568 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300569 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800571 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800573 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800574 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575}
576
Avi Kivity714b93d2007-01-05 16:36:53 -0800577static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
578 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800579{
580 struct kvm_pte_chain *pte_chain;
581 struct hlist_node *node;
582 int i;
583
584 if (!parent_pte)
585 return;
586 if (!page->multimapped) {
587 u64 *old = page->parent_pte;
588
589 if (!old) {
590 page->parent_pte = parent_pte;
591 return;
592 }
593 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800594 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800595 INIT_HLIST_HEAD(&page->parent_ptes);
596 hlist_add_head(&pte_chain->link, &page->parent_ptes);
597 pte_chain->parent_ptes[0] = old;
598 }
599 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
600 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
601 continue;
602 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
603 if (!pte_chain->parent_ptes[i]) {
604 pte_chain->parent_ptes[i] = parent_pte;
605 return;
606 }
607 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800608 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800609 BUG_ON(!pte_chain);
610 hlist_add_head(&pte_chain->link, &page->parent_ptes);
611 pte_chain->parent_ptes[0] = parent_pte;
612}
613
Avi Kivity90cb0522007-07-17 13:04:56 +0300614static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800615 u64 *parent_pte)
616{
617 struct kvm_pte_chain *pte_chain;
618 struct hlist_node *node;
619 int i;
620
621 if (!page->multimapped) {
622 BUG_ON(page->parent_pte != parent_pte);
623 page->parent_pte = NULL;
624 return;
625 }
626 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
627 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
628 if (!pte_chain->parent_ptes[i])
629 break;
630 if (pte_chain->parent_ptes[i] != parent_pte)
631 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800632 while (i + 1 < NR_PTE_CHAIN_ENTRIES
633 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800634 pte_chain->parent_ptes[i]
635 = pte_chain->parent_ptes[i + 1];
636 ++i;
637 }
638 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800639 if (i == 0) {
640 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300641 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800642 if (hlist_empty(&page->parent_ptes)) {
643 page->multimapped = 0;
644 page->parent_pte = NULL;
645 }
646 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800647 return;
648 }
649 BUG();
650}
651
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500652static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800653 gfn_t gfn)
654{
655 unsigned index;
656 struct hlist_head *bucket;
657 struct kvm_mmu_page *page;
658 struct hlist_node *node;
659
660 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
661 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500662 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800663 hlist_for_each_entry(page, node, bucket, hash_link)
664 if (page->gfn == gfn && !page->role.metaphysical) {
665 pgprintk("%s: found role %x\n",
666 __FUNCTION__, page->role.word);
667 return page;
668 }
669 return NULL;
670}
671
672static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
673 gfn_t gfn,
674 gva_t gaddr,
675 unsigned level,
676 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200677 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800678 u64 *parent_pte)
679{
680 union kvm_mmu_page_role role;
681 unsigned index;
682 unsigned quadrant;
683 struct hlist_head *bucket;
684 struct kvm_mmu_page *page;
685 struct hlist_node *node;
686
687 role.word = 0;
688 role.glevels = vcpu->mmu.root_level;
689 role.level = level;
690 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200691 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800692 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
693 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
694 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
695 role.quadrant = quadrant;
696 }
697 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
698 gfn, role.word);
699 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
700 bucket = &vcpu->kvm->mmu_page_hash[index];
701 hlist_for_each_entry(page, node, bucket, hash_link)
702 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800703 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704 pgprintk("%s: found\n", __FUNCTION__);
705 return page;
706 }
707 page = kvm_mmu_alloc_page(vcpu, parent_pte);
708 if (!page)
709 return page;
710 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
711 page->gfn = gfn;
712 page->role = role;
713 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200714 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800715 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500716 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800717 return page;
718}
719
Avi Kivity90cb0522007-07-17 13:04:56 +0300720static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800721 struct kvm_mmu_page *page)
722{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800723 unsigned i;
724 u64 *pt;
725 u64 ent;
726
Avi Kivity47ad8e62007-05-06 15:50:58 +0300727 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800728
729 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
730 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200731 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200732 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200733 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300735 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800736 return;
737 }
738
739 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
740 ent = pt[i];
741
Avi Kivityc7addb92007-09-16 18:58:32 +0200742 pt[i] = shadow_trap_nonpresent_pte;
743 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800744 continue;
745 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300746 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800747 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300748 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800749}
750
Avi Kivity90cb0522007-07-17 13:04:56 +0300751static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800752 u64 *parent_pte)
753{
Avi Kivity90cb0522007-07-17 13:04:56 +0300754 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800755}
756
Avi Kivity12b7d282007-09-23 14:10:49 +0200757static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
758{
759 int i;
760
761 for (i = 0; i < KVM_MAX_VCPUS; ++i)
762 if (kvm->vcpus[i])
763 kvm->vcpus[i]->last_pte_updated = NULL;
764}
765
Avi Kivity90cb0522007-07-17 13:04:56 +0300766static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800767 struct kvm_mmu_page *page)
768{
769 u64 *parent_pte;
770
Avi Kivity4cee5762007-11-18 16:37:07 +0200771 ++kvm->stat.mmu_shadow_zapped;
Avi Kivitya4360362007-01-05 16:36:45 -0800772 while (page->multimapped || page->parent_pte) {
773 if (!page->multimapped)
774 parent_pte = page->parent_pte;
775 else {
776 struct kvm_pte_chain *chain;
777
778 chain = container_of(page->parent_ptes.first,
779 struct kvm_pte_chain, link);
780 parent_pte = chain->parent_ptes[0];
781 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800782 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300783 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200784 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800785 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300786 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800787 if (!page->root_count) {
788 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300789 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200790 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300791 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200792 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800793}
794
Izik Eidus82ce2c92007-10-02 18:52:55 +0200795/*
796 * Changing the number of mmu pages allocated to the vm
797 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
798 */
799void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
800{
801 /*
802 * If we set the number of mmu pages to be smaller be than the
803 * number of actived pages , we must to free some mmu pages before we
804 * change the value
805 */
806
807 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
808 kvm_nr_mmu_pages) {
809 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
810 - kvm->n_free_mmu_pages;
811
812 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
813 struct kvm_mmu_page *page;
814
815 page = container_of(kvm->active_mmu_pages.prev,
816 struct kvm_mmu_page, link);
817 kvm_mmu_zap_page(kvm, page);
818 n_used_mmu_pages--;
819 }
820 kvm->n_free_mmu_pages = 0;
821 }
822 else
823 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
824 - kvm->n_alloc_mmu_pages;
825
826 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
827}
828
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500829static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800830{
831 unsigned index;
832 struct hlist_head *bucket;
833 struct kvm_mmu_page *page;
834 struct hlist_node *node, *n;
835 int r;
836
837 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
838 r = 0;
839 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500840 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800841 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
842 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800843 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
844 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500845 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800846 r = 1;
847 }
848 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800849}
850
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500851static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300852{
853 struct kvm_mmu_page *page;
854
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500855 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300856 pgprintk("%s: zap %lx %x\n",
857 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500858 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300859 }
860}
861
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
863{
864 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
865 struct kvm_mmu_page *page_head = page_header(__pa(pte));
866
867 __set_bit(slot, &page_head->slot_bitmap);
868}
869
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500870hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 struct page *page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200873 hpa_t hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874
875 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500876 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200877 hpa = ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) | (gpa & (PAGE_SIZE-1));
878 if (is_error_page(page))
879 return hpa | HPA_ERR_MASK;
880 return hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881}
882
883hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
884{
885 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
886
887 if (gpa == UNMAPPED_GVA)
888 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500889 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890}
891
Avi Kivity039576c2007-03-20 12:46:50 +0200892struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
893{
894 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
895
896 if (gpa == UNMAPPED_GVA)
897 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500898 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200899}
900
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
902{
903}
904
905static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
906{
907 int level = PT32E_ROOT_LEVEL;
908 hpa_t table_addr = vcpu->mmu.root_hpa;
Izik Eidusb4231d62007-11-20 11:49:33 +0200909 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910
Izik Eidusb4231d62007-11-20 11:49:33 +0200911 page = pfn_to_page(p >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 for (; ; level--) {
913 u32 index = PT64_INDEX(v, level);
914 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800915 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916
917 ASSERT(VALID_PAGE(table_addr));
918 table = __va(table_addr);
919
920 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200921 int was_rmapped;
922
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800923 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200924 was_rmapped = is_rmap_pte(pte);
Izik Eidus2065b372007-11-20 11:30:04 +0200925 if (is_shadow_present_pte(pte) && is_writeble_pte(pte)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200926 kvm_release_page_clean(page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927 return 0;
Izik Eidus2065b372007-11-20 11:30:04 +0200928 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
930 page_header_update_slot(vcpu->kvm, table, v);
931 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
932 PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200933 if (!was_rmapped)
934 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200935 else
Izik Eidusb4231d62007-11-20 11:49:33 +0200936 kvm_release_page_clean(page);
937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 return 0;
939 }
940
Avi Kivityc7addb92007-09-16 18:58:32 +0200941 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800942 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800943 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800945 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
946 >> PAGE_SHIFT;
947 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
948 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200949 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800950 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidusb4231d62007-11-20 11:49:33 +0200952 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 return -ENOMEM;
954 }
955
Avi Kivity47ad8e62007-05-06 15:50:58 +0300956 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800957 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 }
959 table_addr = table[index] & PT64_BASE_ADDR_MASK;
960 }
961}
962
Avi Kivityc7addb92007-09-16 18:58:32 +0200963static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
964 struct kvm_mmu_page *sp)
965{
966 int i;
967
968 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
969 sp->spt[i] = shadow_trap_nonpresent_pte;
970}
971
Avi Kivity17ac10a2007-01-05 16:36:40 -0800972static void mmu_free_roots(struct kvm_vcpu *vcpu)
973{
974 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800975 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800976
Avi Kivity7b53aa52007-06-05 12:17:03 +0300977 if (!VALID_PAGE(vcpu->mmu.root_hpa))
978 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800979#ifdef CONFIG_X86_64
980 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
981 hpa_t root = vcpu->mmu.root_hpa;
982
Avi Kivity3bb65a22007-01-05 16:36:51 -0800983 page = page_header(root);
984 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800985 vcpu->mmu.root_hpa = INVALID_PAGE;
986 return;
987 }
988#endif
989 for (i = 0; i < 4; ++i) {
990 hpa_t root = vcpu->mmu.pae_root[i];
991
Avi Kivity417726a2007-04-12 17:35:58 +0300992 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300993 root &= PT64_BASE_ADDR_MASK;
994 page = page_header(root);
995 --page->root_count;
996 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800997 vcpu->mmu.pae_root[i] = INVALID_PAGE;
998 }
999 vcpu->mmu.root_hpa = INVALID_PAGE;
1000}
1001
1002static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1003{
1004 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001005 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001006 struct kvm_mmu_page *page;
1007
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001008 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001009
1010#ifdef CONFIG_X86_64
1011 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1012 hpa_t root = vcpu->mmu.root_hpa;
1013
1014 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -08001015 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001016 PT64_ROOT_LEVEL, 0, 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.root_hpa = root;
1020 return;
1021 }
1022#endif
1023 for (i = 0; i < 4; ++i) {
1024 hpa_t root = vcpu->mmu.pae_root[i];
1025
1026 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001027 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1028 if (!is_present_pte(vcpu->pdptrs[i])) {
1029 vcpu->mmu.pae_root[i] = 0;
1030 continue;
1031 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001032 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001033 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001034 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -08001035 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001036 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001037 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001038 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001039 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001040 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1041 }
1042 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1043}
1044
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1046{
1047 return vaddr;
1048}
1049
1050static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1051 u32 error_code)
1052{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001054 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001055 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056
Avi Kivitye2dec932007-01-05 16:36:54 -08001057 r = mmu_topup_memory_caches(vcpu);
1058 if (r)
1059 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001060
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 ASSERT(vcpu);
1062 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1063
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001065 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Izik Eidus8a7ae052007-10-18 11:09:33 +02001067 if (is_error_hpa(paddr)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001068 kvm_release_page_clean(pfn_to_page((paddr & PT64_BASE_ADDR_MASK)
1069 >> PAGE_SHIFT));
Avi Kivityebeace82007-01-05 16:36:47 -08001070 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001071 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072
Avi Kivityebeace82007-01-05 16:36:47 -08001073 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074}
1075
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076static void nonpaging_free(struct kvm_vcpu *vcpu)
1077{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001078 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079}
1080
1081static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1082{
1083 struct kvm_mmu *context = &vcpu->mmu;
1084
1085 context->new_cr3 = nonpaging_new_cr3;
1086 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 context->gva_to_gpa = nonpaging_gva_to_gpa;
1088 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001089 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001090 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001092 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 return 0;
1094}
1095
Avi Kivityd835dfe2007-11-21 02:57:59 +02001096void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001098 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001099 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100}
1101
1102static void paging_new_cr3(struct kvm_vcpu *vcpu)
1103{
Avi Kivity374cbac2007-01-05 16:36:43 -08001104 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001105 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106}
1107
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108static void inject_page_fault(struct kvm_vcpu *vcpu,
1109 u64 addr,
1110 u32 err_code)
1111{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001112 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113}
1114
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115static void paging_free(struct kvm_vcpu *vcpu)
1116{
1117 nonpaging_free(vcpu);
1118}
1119
1120#define PTTYPE 64
1121#include "paging_tmpl.h"
1122#undef PTTYPE
1123
1124#define PTTYPE 32
1125#include "paging_tmpl.h"
1126#undef PTTYPE
1127
Avi Kivity17ac10a2007-01-05 16:36:40 -08001128static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129{
1130 struct kvm_mmu *context = &vcpu->mmu;
1131
1132 ASSERT(is_pae(vcpu));
1133 context->new_cr3 = paging_new_cr3;
1134 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001136 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001138 context->root_level = level;
1139 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001140 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 return 0;
1142}
1143
Avi Kivity17ac10a2007-01-05 16:36:40 -08001144static int paging64_init_context(struct kvm_vcpu *vcpu)
1145{
1146 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1147}
1148
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149static int paging32_init_context(struct kvm_vcpu *vcpu)
1150{
1151 struct kvm_mmu *context = &vcpu->mmu;
1152
1153 context->new_cr3 = paging_new_cr3;
1154 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 context->gva_to_gpa = paging32_gva_to_gpa;
1156 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001157 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 context->root_level = PT32_ROOT_LEVEL;
1159 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001160 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 return 0;
1162}
1163
1164static int paging32E_init_context(struct kvm_vcpu *vcpu)
1165{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001166 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
1169static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1170{
1171 ASSERT(vcpu);
1172 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1173
1174 if (!is_paging(vcpu))
1175 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001176 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177 return paging64_init_context(vcpu);
1178 else if (is_pae(vcpu))
1179 return paging32E_init_context(vcpu);
1180 else
1181 return paging32_init_context(vcpu);
1182}
1183
1184static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1185{
1186 ASSERT(vcpu);
1187 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1188 vcpu->mmu.free(vcpu);
1189 vcpu->mmu.root_hpa = INVALID_PAGE;
1190 }
1191}
1192
1193int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1194{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001195 destroy_kvm_mmu(vcpu);
1196 return init_kvm_mmu(vcpu);
1197}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001198EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001199
1200int kvm_mmu_load(struct kvm_vcpu *vcpu)
1201{
Avi Kivity714b93d2007-01-05 16:36:53 -08001202 int r;
1203
Shaohua Li11ec2802007-07-23 14:51:37 +08001204 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001205 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001206 if (r)
1207 goto out;
1208 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001209 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001210 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001211out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001212 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001213 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001215EXPORT_SYMBOL_GPL(kvm_mmu_load);
1216
1217void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1218{
1219 mmu_free_roots(vcpu);
1220}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221
Avi Kivity09072da2007-05-01 14:16:52 +03001222static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001223 struct kvm_mmu_page *page,
1224 u64 *spte)
1225{
1226 u64 pte;
1227 struct kvm_mmu_page *child;
1228
1229 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001230 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001231 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001232 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001233 else {
1234 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001235 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001236 }
1237 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001238 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001239}
1240
Avi Kivity00284252007-05-01 16:53:31 +03001241static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1242 struct kvm_mmu_page *page,
1243 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001244 const void *new, int bytes,
1245 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001246{
Avi Kivity4cee5762007-11-18 16:37:07 +02001247 if (page->role.level != PT_PAGE_TABLE_LEVEL) {
1248 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001249 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001250 }
Avi Kivity00284252007-05-01 16:53:31 +03001251
Avi Kivity4cee5762007-11-18 16:37:07 +02001252 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity00284252007-05-01 16:53:31 +03001253 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001254 paging32_update_pte(vcpu, page, spte, new, bytes,
1255 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001256 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001257 paging64_update_pte(vcpu, page, spte, new, bytes,
1258 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001259}
1260
Avi Kivity79539ce2007-11-21 02:06:21 +02001261static bool need_remote_flush(u64 old, u64 new)
1262{
1263 if (!is_shadow_present_pte(old))
1264 return false;
1265 if (!is_shadow_present_pte(new))
1266 return true;
1267 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1268 return true;
1269 old ^= PT64_NX_MASK;
1270 new ^= PT64_NX_MASK;
1271 return (old & ~new & PT64_PERM_MASK) != 0;
1272}
1273
1274static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1275{
1276 if (need_remote_flush(old, new))
1277 kvm_flush_remote_tlbs(vcpu->kvm);
1278 else
1279 kvm_mmu_flush_tlb(vcpu);
1280}
1281
Avi Kivity12b7d282007-09-23 14:10:49 +02001282static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1283{
1284 u64 *spte = vcpu->last_pte_updated;
1285
1286 return !!(spte && (*spte & PT_ACCESSED_MASK));
1287}
1288
Avi Kivity09072da2007-05-01 14:16:52 +03001289void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life5518812007-07-23 14:51:39 +08001290 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001291{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001292 gfn_t gfn = gpa >> PAGE_SHIFT;
1293 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001294 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001295 struct hlist_head *bucket;
1296 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001297 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001298 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001299 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001300 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001301 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001302 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001303 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001304 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001305 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001306 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001307
Avi Kivityda4a00f2007-01-05 16:36:44 -08001308 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity4cee5762007-11-18 16:37:07 +02001309 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001310 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001311 if (gfn == vcpu->last_pt_write_gfn
1312 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001313 ++vcpu->last_pt_write_count;
1314 if (vcpu->last_pt_write_count >= 3)
1315 flooded = 1;
1316 } else {
1317 vcpu->last_pt_write_gfn = gfn;
1318 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001319 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001320 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001321 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1322 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001323 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001324 if (page->gfn != gfn || page->role.metaphysical)
1325 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001326 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1327 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001328 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001329 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001330 /*
1331 * Misaligned accesses are too much trouble to fix
1332 * up; also, they usually indicate a page is not used
1333 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001334 *
1335 * If we're seeing too many writes to a page,
1336 * it may no longer be a page table, or we may be
1337 * forking, in which case it is better to unmap the
1338 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001339 */
1340 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1341 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001342 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001343 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001344 continue;
1345 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001346 page_offset = offset;
1347 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001348 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001349 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001350 page_offset <<= 1; /* 32->64 */
1351 /*
1352 * A 32-bit pde maps 4MB while the shadow pdes map
1353 * only 2MB. So we need to double the offset again
1354 * and zap two pdes instead of one.
1355 */
1356 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001357 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001358 page_offset <<= 1;
1359 npte = 2;
1360 }
Avi Kivityfce06572007-05-01 16:44:05 +03001361 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001362 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001363 if (quadrant != page->role.quadrant)
1364 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001365 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001366 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001367 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001368 entry = *spte;
Avi Kivity09072da2007-05-01 14:16:52 +03001369 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001370 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1371 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001372 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001373 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001374 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001375 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001376 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001377}
1378
Avi Kivitya4360362007-01-05 16:36:45 -08001379int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1380{
1381 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1382
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001383 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001384}
1385
Avi Kivity22d95b12007-09-14 20:26:06 +03001386void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001387{
1388 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1389 struct kvm_mmu_page *page;
1390
1391 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1392 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001393 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001394 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001395 }
1396}
Avi Kivityebeace82007-01-05 16:36:47 -08001397
Avi Kivity30677142007-10-28 18:48:59 +02001398int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1399{
1400 int r;
1401 enum emulation_result er;
1402
1403 mutex_lock(&vcpu->kvm->lock);
1404 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1405 if (r < 0)
1406 goto out;
1407
1408 if (!r) {
1409 r = 1;
1410 goto out;
1411 }
1412
Avi Kivityb733bfb2007-10-28 18:52:05 +02001413 r = mmu_topup_memory_caches(vcpu);
1414 if (r)
1415 goto out;
1416
Avi Kivity30677142007-10-28 18:48:59 +02001417 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1418 mutex_unlock(&vcpu->kvm->lock);
1419
1420 switch (er) {
1421 case EMULATE_DONE:
1422 return 1;
1423 case EMULATE_DO_MMIO:
1424 ++vcpu->stat.mmio_exits;
1425 return 0;
1426 case EMULATE_FAIL:
1427 kvm_report_emulation_failure(vcpu, "pagetable");
1428 return 1;
1429 default:
1430 BUG();
1431 }
1432out:
1433 mutex_unlock(&vcpu->kvm->lock);
1434 return r;
1435}
1436EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1437
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438static void free_mmu_pages(struct kvm_vcpu *vcpu)
1439{
Avi Kivityf51234c2007-01-05 16:36:52 -08001440 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
Avi Kivityf51234c2007-01-05 16:36:52 -08001442 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1443 page = container_of(vcpu->kvm->active_mmu_pages.next,
1444 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001445 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001446 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001447 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448}
1449
1450static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1451{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001452 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 int i;
1454
1455 ASSERT(vcpu);
1456
Izik Eidus82ce2c92007-10-02 18:52:55 +02001457 if (vcpu->kvm->n_requested_mmu_pages)
1458 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1459 else
1460 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001461 /*
1462 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1463 * Therefore we need to allocate shadow page tables in the first
1464 * 4GB of memory, which happens to fit the DMA32 zone.
1465 */
1466 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1467 if (!page)
1468 goto error_1;
1469 vcpu->mmu.pae_root = page_address(page);
1470 for (i = 0; i < 4; ++i)
1471 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1472
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 return 0;
1474
1475error_1:
1476 free_mmu_pages(vcpu);
1477 return -ENOMEM;
1478}
1479
Ingo Molnar8018c272006-12-29 16:50:01 -08001480int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 ASSERT(vcpu);
1483 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484
Ingo Molnar8018c272006-12-29 16:50:01 -08001485 return alloc_mmu_pages(vcpu);
1486}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487
Ingo Molnar8018c272006-12-29 16:50:01 -08001488int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1489{
1490 ASSERT(vcpu);
1491 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001492
Ingo Molnar8018c272006-12-29 16:50:01 -08001493 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494}
1495
1496void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1497{
1498 ASSERT(vcpu);
1499
1500 destroy_kvm_mmu(vcpu);
1501 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001502 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503}
1504
Avi Kivity90cb0522007-07-17 13:04:56 +03001505void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506{
1507 struct kvm_mmu_page *page;
1508
1509 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1510 int i;
1511 u64 *pt;
1512
1513 if (!test_bit(slot, &page->slot_bitmap))
1514 continue;
1515
Avi Kivity47ad8e62007-05-06 15:50:58 +03001516 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1518 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001519 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 }
1522}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001523
Avi Kivity90cb0522007-07-17 13:04:56 +03001524void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001525{
Avi Kivity90cb0522007-07-17 13:04:56 +03001526 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001527
Avi Kivity90cb0522007-07-17 13:04:56 +03001528 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1529 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001530
Avi Kivity90cb0522007-07-17 13:04:56 +03001531 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001532}
1533
Avi Kivityb5a33a72007-04-15 16:31:09 +03001534void kvm_mmu_module_exit(void)
1535{
1536 if (pte_chain_cache)
1537 kmem_cache_destroy(pte_chain_cache);
1538 if (rmap_desc_cache)
1539 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001540 if (mmu_page_header_cache)
1541 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001542}
1543
1544int kvm_mmu_module_init(void)
1545{
1546 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1547 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001548 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001549 if (!pte_chain_cache)
1550 goto nomem;
1551 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1552 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001553 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001554 if (!rmap_desc_cache)
1555 goto nomem;
1556
Avi Kivityd3d25b02007-05-30 12:34:53 +03001557 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1558 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001559 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001560 if (!mmu_page_header_cache)
1561 goto nomem;
1562
Avi Kivityb5a33a72007-04-15 16:31:09 +03001563 return 0;
1564
1565nomem:
1566 kvm_mmu_module_exit();
1567 return -ENOMEM;
1568}
1569
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001570/*
1571 * Caculate mmu pages needed for kvm.
1572 */
1573unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1574{
1575 int i;
1576 unsigned int nr_mmu_pages;
1577 unsigned int nr_pages = 0;
1578
1579 for (i = 0; i < kvm->nmemslots; i++)
1580 nr_pages += kvm->memslots[i].npages;
1581
1582 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1583 nr_mmu_pages = max(nr_mmu_pages,
1584 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1585
1586 return nr_mmu_pages;
1587}
1588
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001589#ifdef AUDIT
1590
1591static const char *audit_msg;
1592
1593static gva_t canonicalize(gva_t gva)
1594{
1595#ifdef CONFIG_X86_64
1596 gva = (long long)(gva << 16) >> 16;
1597#endif
1598 return gva;
1599}
1600
1601static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1602 gva_t va, int level)
1603{
1604 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1605 int i;
1606 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1607
1608 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1609 u64 ent = pt[i];
1610
Avi Kivityc7addb92007-09-16 18:58:32 +02001611 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001612 continue;
1613
1614 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001615 if (level > 1) {
1616 if (ent == shadow_notrap_nonpresent_pte)
1617 printk(KERN_ERR "audit: (%s) nontrapping pte"
1618 " in nonleaf level: levels %d gva %lx"
1619 " level %d pte %llx\n", audit_msg,
1620 vcpu->mmu.root_level, va, level, ent);
1621
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001622 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001623 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001624 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1625 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001626 struct page *page;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001627
Avi Kivityc7addb92007-09-16 18:58:32 +02001628 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001629 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001630 printk(KERN_ERR "xx audit error: (%s) levels %d"
1631 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001632 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001633 va, gpa, hpa, ent,
1634 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001635 else if (ent == shadow_notrap_nonpresent_pte
1636 && !is_error_hpa(hpa))
1637 printk(KERN_ERR "audit: (%s) notrap shadow,"
1638 " valid guest gva %lx\n", audit_msg, va);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001639 page = pfn_to_page((gpa & PT64_BASE_ADDR_MASK)
1640 >> PAGE_SHIFT);
Izik Eidusb4231d62007-11-20 11:49:33 +02001641 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001642
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001643 }
1644 }
1645}
1646
1647static void audit_mappings(struct kvm_vcpu *vcpu)
1648{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001649 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001650
1651 if (vcpu->mmu.root_level == 4)
1652 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1653 else
1654 for (i = 0; i < 4; ++i)
1655 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1656 audit_mappings_page(vcpu,
1657 vcpu->mmu.pae_root[i],
1658 i << 30,
1659 2);
1660}
1661
1662static int count_rmaps(struct kvm_vcpu *vcpu)
1663{
1664 int nmaps = 0;
1665 int i, j, k;
1666
1667 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1668 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1669 struct kvm_rmap_desc *d;
1670
1671 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001672 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001673
Izik Eidus290fc382007-09-27 14:11:22 +02001674 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001675 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001676 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001677 ++nmaps;
1678 continue;
1679 }
Izik Eidus290fc382007-09-27 14:11:22 +02001680 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001681 while (d) {
1682 for (k = 0; k < RMAP_EXT; ++k)
1683 if (d->shadow_ptes[k])
1684 ++nmaps;
1685 else
1686 break;
1687 d = d->more;
1688 }
1689 }
1690 }
1691 return nmaps;
1692}
1693
1694static int count_writable_mappings(struct kvm_vcpu *vcpu)
1695{
1696 int nmaps = 0;
1697 struct kvm_mmu_page *page;
1698 int i;
1699
1700 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001701 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001702
1703 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1704 continue;
1705
1706 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1707 u64 ent = pt[i];
1708
1709 if (!(ent & PT_PRESENT_MASK))
1710 continue;
1711 if (!(ent & PT_WRITABLE_MASK))
1712 continue;
1713 ++nmaps;
1714 }
1715 }
1716 return nmaps;
1717}
1718
1719static void audit_rmap(struct kvm_vcpu *vcpu)
1720{
1721 int n_rmap = count_rmaps(vcpu);
1722 int n_actual = count_writable_mappings(vcpu);
1723
1724 if (n_rmap != n_actual)
1725 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1726 __FUNCTION__, audit_msg, n_rmap, n_actual);
1727}
1728
1729static void audit_write_protection(struct kvm_vcpu *vcpu)
1730{
1731 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001732 struct kvm_memory_slot *slot;
1733 unsigned long *rmapp;
1734 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001735
1736 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001737 if (page->role.metaphysical)
1738 continue;
1739
Izik Eidus290fc382007-09-27 14:11:22 +02001740 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1741 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1742 rmapp = &slot->rmap[gfn - slot->base_gfn];
1743 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001744 printk(KERN_ERR "%s: (%s) shadow page has writable"
1745 " mappings: gfn %lx role %x\n",
1746 __FUNCTION__, audit_msg, page->gfn,
1747 page->role.word);
1748 }
1749}
1750
1751static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1752{
1753 int olddbg = dbg;
1754
1755 dbg = 0;
1756 audit_msg = msg;
1757 audit_rmap(vcpu);
1758 audit_write_protection(vcpu);
1759 audit_mappings(vcpu);
1760 dbg = olddbg;
1761}
1762
1763#endif