blob: 0cb6580956ba9ea3a099fd4d43c4981cdf871a54 [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 Russell707d92f2007-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{
Avi Kivity4db35312007-11-21 15:28:32 +0200370 struct kvm_mmu_page *sp;
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);
Avi Kivity4db35312007-11-21 15:28:32 +0200378 sp = page_header(__pa(spte));
379 sp->gfns[spte - sp->spt] = gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200380 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;
Avi Kivity4db35312007-11-21 15:28:32 +0200432 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200433 struct page *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;
Avi Kivity4db35312007-11-21 15:28:32 +0200439 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200440 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidusb4231d62007-11-20 11:49:33 +0200441 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200442 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200443 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200444 kvm_release_page_clean(page);
Avi Kivity4db35312007-11-21 15:28:32 +0200445 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
Izik Eidus290fc382007-09-27 14:11:22 +0200446 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 Kivity4db35312007-11-21 15:28:32 +0200540static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800541{
Avi Kivity4db35312007-11-21 15:28:32 +0200542 ASSERT(is_empty_shadow_page(sp->spt));
543 list_del(&sp->link);
544 __free_page(virt_to_page(sp->spt));
545 __free_page(virt_to_page(sp->gfns));
546 kfree(sp);
Avi Kivity90cb0522007-07-17 13:04:56 +0300547 ++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{
Avi Kivity4db35312007-11-21 15:28:32 +0200558 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559
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 Kivity4db35312007-11-21 15:28:32 +0200563 sp = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache, sizeof *sp);
564 sp->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
565 sp->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
566 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
567 list_add(&sp->link, &vcpu->kvm->active_mmu_pages);
568 ASSERT(is_empty_shadow_page(sp->spt));
569 sp->slot_bitmap = 0;
570 sp->multimapped = 0;
571 sp->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800572 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200573 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574}
575
Avi Kivity714b93d2007-01-05 16:36:53 -0800576static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200577 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800578{
579 struct kvm_pte_chain *pte_chain;
580 struct hlist_node *node;
581 int i;
582
583 if (!parent_pte)
584 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200585 if (!sp->multimapped) {
586 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800587
588 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200589 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800590 return;
591 }
Avi Kivity4db35312007-11-21 15:28:32 +0200592 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800593 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200594 INIT_HLIST_HEAD(&sp->parent_ptes);
595 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800596 pte_chain->parent_ptes[0] = old;
597 }
Avi Kivity4db35312007-11-21 15:28:32 +0200598 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800599 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
600 continue;
601 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
602 if (!pte_chain->parent_ptes[i]) {
603 pte_chain->parent_ptes[i] = parent_pte;
604 return;
605 }
606 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800607 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800608 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200609 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800610 pte_chain->parent_ptes[0] = parent_pte;
611}
612
Avi Kivity4db35312007-11-21 15:28:32 +0200613static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800614 u64 *parent_pte)
615{
616 struct kvm_pte_chain *pte_chain;
617 struct hlist_node *node;
618 int i;
619
Avi Kivity4db35312007-11-21 15:28:32 +0200620 if (!sp->multimapped) {
621 BUG_ON(sp->parent_pte != parent_pte);
622 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800623 return;
624 }
Avi Kivity4db35312007-11-21 15:28:32 +0200625 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800626 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
627 if (!pte_chain->parent_ptes[i])
628 break;
629 if (pte_chain->parent_ptes[i] != parent_pte)
630 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800631 while (i + 1 < NR_PTE_CHAIN_ENTRIES
632 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800633 pte_chain->parent_ptes[i]
634 = pte_chain->parent_ptes[i + 1];
635 ++i;
636 }
637 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800638 if (i == 0) {
639 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300640 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200641 if (hlist_empty(&sp->parent_ptes)) {
642 sp->multimapped = 0;
643 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800644 }
645 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800646 return;
647 }
648 BUG();
649}
650
Avi Kivity4db35312007-11-21 15:28:32 +0200651static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800652{
653 unsigned index;
654 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200655 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800656 struct hlist_node *node;
657
658 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
659 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500660 bucket = &kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200661 hlist_for_each_entry(sp, node, bucket, hash_link)
662 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800663 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200664 __FUNCTION__, sp->role.word);
665 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800666 }
667 return NULL;
668}
669
670static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
671 gfn_t gfn,
672 gva_t gaddr,
673 unsigned level,
674 int metaphysical,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200675 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800676 u64 *parent_pte)
677{
678 union kvm_mmu_page_role role;
679 unsigned index;
680 unsigned quadrant;
681 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200682 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800683 struct hlist_node *node;
684
685 role.word = 0;
686 role.glevels = vcpu->mmu.root_level;
687 role.level = level;
688 role.metaphysical = metaphysical;
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200689 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800690 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
691 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
692 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
693 role.quadrant = quadrant;
694 }
695 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
696 gfn, role.word);
697 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
698 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200699 hlist_for_each_entry(sp, node, bucket, hash_link)
700 if (sp->gfn == gfn && sp->role.word == role.word) {
701 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800702 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200703 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704 }
Avi Kivity4db35312007-11-21 15:28:32 +0200705 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
706 if (!sp)
707 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800708 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200709 sp->gfn = gfn;
710 sp->role = role;
711 hlist_add_head(&sp->hash_link, bucket);
712 vcpu->mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800713 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500714 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200715 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716}
717
Avi Kivity90cb0522007-07-17 13:04:56 +0300718static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200719 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800720{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800721 unsigned i;
722 u64 *pt;
723 u64 ent;
724
Avi Kivity4db35312007-11-21 15:28:32 +0200725 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800726
Avi Kivity4db35312007-11-21 15:28:32 +0200727 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800728 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200729 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200730 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200731 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800732 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300733 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734 return;
735 }
736
737 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
738 ent = pt[i];
739
Avi Kivityc7addb92007-09-16 18:58:32 +0200740 pt[i] = shadow_trap_nonpresent_pte;
741 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800742 continue;
743 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300744 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800745 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300746 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800747}
748
Avi Kivity4db35312007-11-21 15:28:32 +0200749static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800750{
Avi Kivity4db35312007-11-21 15:28:32 +0200751 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800752}
753
Avi Kivity12b7d282007-09-23 14:10:49 +0200754static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
755{
756 int i;
757
758 for (i = 0; i < KVM_MAX_VCPUS; ++i)
759 if (kvm->vcpus[i])
760 kvm->vcpus[i]->last_pte_updated = NULL;
761}
762
Avi Kivity4db35312007-11-21 15:28:32 +0200763static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800764{
765 u64 *parent_pte;
766
Avi Kivity4cee5762007-11-18 16:37:07 +0200767 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200768 while (sp->multimapped || sp->parent_pte) {
769 if (!sp->multimapped)
770 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800771 else {
772 struct kvm_pte_chain *chain;
773
Avi Kivity4db35312007-11-21 15:28:32 +0200774 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800775 struct kvm_pte_chain, link);
776 parent_pte = chain->parent_ptes[0];
777 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800778 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200779 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200780 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800781 }
Avi Kivity4db35312007-11-21 15:28:32 +0200782 kvm_mmu_page_unlink_children(kvm, sp);
783 if (!sp->root_count) {
784 hlist_del(&sp->hash_link);
785 kvm_mmu_free_page(kvm, sp);
Avi Kivity36868f72007-03-26 19:31:52 +0200786 } else
Avi Kivity4db35312007-11-21 15:28:32 +0200787 list_move(&sp->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200788 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800789}
790
Izik Eidus82ce2c92007-10-02 18:52:55 +0200791/*
792 * Changing the number of mmu pages allocated to the vm
793 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
794 */
795void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
796{
797 /*
798 * If we set the number of mmu pages to be smaller be than the
799 * number of actived pages , we must to free some mmu pages before we
800 * change the value
801 */
802
803 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
804 kvm_nr_mmu_pages) {
805 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
806 - kvm->n_free_mmu_pages;
807
808 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
809 struct kvm_mmu_page *page;
810
811 page = container_of(kvm->active_mmu_pages.prev,
812 struct kvm_mmu_page, link);
813 kvm_mmu_zap_page(kvm, page);
814 n_used_mmu_pages--;
815 }
816 kvm->n_free_mmu_pages = 0;
817 }
818 else
819 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
820 - kvm->n_alloc_mmu_pages;
821
822 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
823}
824
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500825static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800826{
827 unsigned index;
828 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200829 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800830 struct hlist_node *node, *n;
831 int r;
832
833 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
834 r = 0;
835 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500836 bucket = &kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200837 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
838 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800839 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200840 sp->role.word);
841 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800842 r = 1;
843 }
844 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845}
846
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500847static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300848{
Avi Kivity4db35312007-11-21 15:28:32 +0200849 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300850
Avi Kivity4db35312007-11-21 15:28:32 +0200851 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
852 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
853 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300854 }
855}
856
Avi Kivity38c335f2007-11-21 14:20:22 +0200857static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858{
Avi Kivity38c335f2007-11-21 14:20:22 +0200859 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200860 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
Avi Kivity4db35312007-11-21 15:28:32 +0200862 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863}
864
Avi Kivity039576c2007-03-20 12:46:50 +0200865struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
866{
867 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
868
869 if (gpa == UNMAPPED_GVA)
870 return NULL;
Avi Kivity1d28f5f2007-11-21 15:01:44 +0200871 return gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200872}
873
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
875{
876}
877
Avi Kivity3f3e7122007-11-21 14:54:16 +0200878static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879{
880 int level = PT32E_ROOT_LEVEL;
881 hpa_t table_addr = vcpu->mmu.root_hpa;
882
883 for (; ; level--) {
884 u32 index = PT64_INDEX(v, level);
885 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800886 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887
888 ASSERT(VALID_PAGE(table_addr));
889 table = __va(table_addr);
890
891 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200892 int was_rmapped;
893
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800894 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200895 was_rmapped = is_rmap_pte(pte);
Izik Eidus2065b372007-11-20 11:30:04 +0200896 if (is_shadow_present_pte(pte) && is_writeble_pte(pte)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200897 kvm_release_page_clean(page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800898 return 0;
Izik Eidus2065b372007-11-20 11:30:04 +0200899 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
Avi Kivity38c335f2007-11-21 14:20:22 +0200901 page_header_update_slot(vcpu->kvm, table,
902 v >> PAGE_SHIFT);
Avi Kivity3f3e7122007-11-21 14:54:16 +0200903 table[index] = page_to_phys(page)
904 | PT_PRESENT_MASK | PT_WRITABLE_MASK
905 | PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200906 if (!was_rmapped)
907 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200908 else
Izik Eidusb4231d62007-11-20 11:49:33 +0200909 kvm_release_page_clean(page);
910
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 return 0;
912 }
913
Avi Kivityc7addb92007-09-16 18:58:32 +0200914 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800915 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800916 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800918 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
919 >> PAGE_SHIFT;
920 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
921 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200922 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800923 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidusb4231d62007-11-20 11:49:33 +0200925 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 return -ENOMEM;
927 }
928
Avi Kivity47ad8e62007-05-06 15:50:58 +0300929 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800930 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 }
932 table_addr = table[index] & PT64_BASE_ADDR_MASK;
933 }
934}
935
Avi Kivityc7addb92007-09-16 18:58:32 +0200936static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
937 struct kvm_mmu_page *sp)
938{
939 int i;
940
941 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
942 sp->spt[i] = shadow_trap_nonpresent_pte;
943}
944
Avi Kivity17ac10a2007-01-05 16:36:40 -0800945static void mmu_free_roots(struct kvm_vcpu *vcpu)
946{
947 int i;
Avi Kivity4db35312007-11-21 15:28:32 +0200948 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800949
Avi Kivity7b53aa52007-06-05 12:17:03 +0300950 if (!VALID_PAGE(vcpu->mmu.root_hpa))
951 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800952#ifdef CONFIG_X86_64
953 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
954 hpa_t root = vcpu->mmu.root_hpa;
955
Avi Kivity4db35312007-11-21 15:28:32 +0200956 sp = page_header(root);
957 --sp->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800958 vcpu->mmu.root_hpa = INVALID_PAGE;
959 return;
960 }
961#endif
962 for (i = 0; i < 4; ++i) {
963 hpa_t root = vcpu->mmu.pae_root[i];
964
Avi Kivity417726a2007-04-12 17:35:58 +0300965 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300966 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +0200967 sp = page_header(root);
968 --sp->root_count;
Avi Kivity417726a2007-04-12 17:35:58 +0300969 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800970 vcpu->mmu.pae_root[i] = INVALID_PAGE;
971 }
972 vcpu->mmu.root_hpa = INVALID_PAGE;
973}
974
975static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
976{
977 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800978 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +0200979 struct kvm_mmu_page *sp;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800980
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800981 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800982
983#ifdef CONFIG_X86_64
984 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
985 hpa_t root = vcpu->mmu.root_hpa;
986
987 ASSERT(!VALID_PAGE(root));
Avi Kivity4db35312007-11-21 15:28:32 +0200988 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
989 PT64_ROOT_LEVEL, 0, 0, NULL);
990 root = __pa(sp->spt);
991 ++sp->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800992 vcpu->mmu.root_hpa = root;
993 return;
994 }
995#endif
996 for (i = 0; i < 4; ++i) {
997 hpa_t root = vcpu->mmu.pae_root[i];
998
999 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001000 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1001 if (!is_present_pte(vcpu->pdptrs[i])) {
1002 vcpu->mmu.pae_root[i] = 0;
1003 continue;
1004 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001005 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001006 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001008 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1009 PT32_ROOT_LEVEL, !is_paging(vcpu),
1010 0, NULL);
1011 root = __pa(sp->spt);
1012 ++sp->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001013 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1014 }
1015 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1016}
1017
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1019{
1020 return vaddr;
1021}
1022
1023static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001024 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025{
Avi Kivity3f3e7122007-11-21 14:54:16 +02001026 struct page *page;
Avi Kivitye2dec932007-01-05 16:36:54 -08001027 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028
Avi Kivitye2dec932007-01-05 16:36:54 -08001029 r = mmu_topup_memory_caches(vcpu);
1030 if (r)
1031 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 ASSERT(vcpu);
1034 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1035
Avi Kivity3f3e7122007-11-21 14:54:16 +02001036 page = gfn_to_page(vcpu->kvm, gva >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037
Avi Kivity3f3e7122007-11-21 14:54:16 +02001038 if (is_error_page(page)) {
1039 kvm_release_page_clean(page);
Avi Kivityebeace82007-01-05 16:36:47 -08001040 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001041 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042
Avi Kivity3f3e7122007-11-21 14:54:16 +02001043 return nonpaging_map(vcpu, gva & PAGE_MASK, page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044}
1045
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046static void nonpaging_free(struct kvm_vcpu *vcpu)
1047{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001048 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049}
1050
1051static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1052{
1053 struct kvm_mmu *context = &vcpu->mmu;
1054
1055 context->new_cr3 = nonpaging_new_cr3;
1056 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 context->gva_to_gpa = nonpaging_gva_to_gpa;
1058 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001059 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001060 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001062 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 return 0;
1064}
1065
Avi Kivityd835dfe2007-11-21 02:57:59 +02001066void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001068 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001069 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070}
1071
1072static void paging_new_cr3(struct kvm_vcpu *vcpu)
1073{
Avi Kivity374cbac2007-01-05 16:36:43 -08001074 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001075 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078static void inject_page_fault(struct kvm_vcpu *vcpu,
1079 u64 addr,
1080 u32 err_code)
1081{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001082 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083}
1084
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085static void paging_free(struct kvm_vcpu *vcpu)
1086{
1087 nonpaging_free(vcpu);
1088}
1089
1090#define PTTYPE 64
1091#include "paging_tmpl.h"
1092#undef PTTYPE
1093
1094#define PTTYPE 32
1095#include "paging_tmpl.h"
1096#undef PTTYPE
1097
Avi Kivity17ac10a2007-01-05 16:36:40 -08001098static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099{
1100 struct kvm_mmu *context = &vcpu->mmu;
1101
1102 ASSERT(is_pae(vcpu));
1103 context->new_cr3 = paging_new_cr3;
1104 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001106 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001108 context->root_level = level;
1109 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001110 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 return 0;
1112}
1113
Avi Kivity17ac10a2007-01-05 16:36:40 -08001114static int paging64_init_context(struct kvm_vcpu *vcpu)
1115{
1116 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1117}
1118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119static int paging32_init_context(struct kvm_vcpu *vcpu)
1120{
1121 struct kvm_mmu *context = &vcpu->mmu;
1122
1123 context->new_cr3 = paging_new_cr3;
1124 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 context->gva_to_gpa = paging32_gva_to_gpa;
1126 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001127 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 context->root_level = PT32_ROOT_LEVEL;
1129 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001130 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 return 0;
1132}
1133
1134static int paging32E_init_context(struct kvm_vcpu *vcpu)
1135{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001136 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137}
1138
1139static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1140{
1141 ASSERT(vcpu);
1142 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1143
1144 if (!is_paging(vcpu))
1145 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001146 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 return paging64_init_context(vcpu);
1148 else if (is_pae(vcpu))
1149 return paging32E_init_context(vcpu);
1150 else
1151 return paging32_init_context(vcpu);
1152}
1153
1154static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1155{
1156 ASSERT(vcpu);
1157 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1158 vcpu->mmu.free(vcpu);
1159 vcpu->mmu.root_hpa = INVALID_PAGE;
1160 }
1161}
1162
1163int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1164{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001165 destroy_kvm_mmu(vcpu);
1166 return init_kvm_mmu(vcpu);
1167}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001168EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001169
1170int kvm_mmu_load(struct kvm_vcpu *vcpu)
1171{
Avi Kivity714b93d2007-01-05 16:36:53 -08001172 int r;
1173
Shaohua Li11ec2802007-07-23 14:51:37 +08001174 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001175 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001176 if (r)
1177 goto out;
1178 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001179 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001180 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001181out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001182 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001183 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001185EXPORT_SYMBOL_GPL(kvm_mmu_load);
1186
1187void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1188{
1189 mmu_free_roots(vcpu);
1190}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191
Avi Kivity09072da2007-05-01 14:16:52 +03001192static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001193 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001194 u64 *spte)
1195{
1196 u64 pte;
1197 struct kvm_mmu_page *child;
1198
1199 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001200 if (is_shadow_present_pte(pte)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001201 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001202 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001203 else {
1204 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001205 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001206 }
1207 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001208 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001209}
1210
Avi Kivity00284252007-05-01 16:53:31 +03001211static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001212 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001213 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001214 const void *new, int bytes,
1215 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001216{
Avi Kivity4db35312007-11-21 15:28:32 +02001217 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001218 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001219 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001220 }
Avi Kivity00284252007-05-01 16:53:31 +03001221
Avi Kivity4cee5762007-11-18 16:37:07 +02001222 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001223 if (sp->role.glevels == PT32_ROOT_LEVEL)
1224 paging32_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001225 else
Avi Kivity4db35312007-11-21 15:28:32 +02001226 paging64_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001227}
1228
Avi Kivity79539ce2007-11-21 02:06:21 +02001229static bool need_remote_flush(u64 old, u64 new)
1230{
1231 if (!is_shadow_present_pte(old))
1232 return false;
1233 if (!is_shadow_present_pte(new))
1234 return true;
1235 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1236 return true;
1237 old ^= PT64_NX_MASK;
1238 new ^= PT64_NX_MASK;
1239 return (old & ~new & PT64_PERM_MASK) != 0;
1240}
1241
1242static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1243{
1244 if (need_remote_flush(old, new))
1245 kvm_flush_remote_tlbs(vcpu->kvm);
1246 else
1247 kvm_mmu_flush_tlb(vcpu);
1248}
1249
Avi Kivity12b7d282007-09-23 14:10:49 +02001250static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1251{
1252 u64 *spte = vcpu->last_pte_updated;
1253
1254 return !!(spte && (*spte & PT_ACCESSED_MASK));
1255}
1256
Avi Kivity09072da2007-05-01 14:16:52 +03001257void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001258 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001259{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001260 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001261 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001262 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001263 struct hlist_head *bucket;
1264 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001265 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001266 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001267 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001268 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001269 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001270 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001271 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001272 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001273 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001274 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001275
Avi Kivityda4a00f2007-01-05 16:36:44 -08001276 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity4cee5762007-11-18 16:37:07 +02001277 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001278 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001279 if (gfn == vcpu->last_pt_write_gfn
1280 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001281 ++vcpu->last_pt_write_count;
1282 if (vcpu->last_pt_write_count >= 3)
1283 flooded = 1;
1284 } else {
1285 vcpu->last_pt_write_gfn = gfn;
1286 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001287 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001288 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001289 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1290 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001291 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1292 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001293 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001294 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001295 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001296 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001297 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001298 /*
1299 * Misaligned accesses are too much trouble to fix
1300 * up; also, they usually indicate a page is not used
1301 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001302 *
1303 * If we're seeing too many writes to a page,
1304 * it may no longer be a page table, or we may be
1305 * forking, in which case it is better to unmap the
1306 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001307 */
1308 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001309 gpa, bytes, sp->role.word);
1310 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001311 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001312 continue;
1313 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001314 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001315 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001316 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001317 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001318 page_offset <<= 1; /* 32->64 */
1319 /*
1320 * A 32-bit pde maps 4MB while the shadow pdes map
1321 * only 2MB. So we need to double the offset again
1322 * and zap two pdes instead of one.
1323 */
1324 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001325 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001326 page_offset <<= 1;
1327 npte = 2;
1328 }
Avi Kivityfce06572007-05-01 16:44:05 +03001329 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001330 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001331 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001332 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001333 }
Avi Kivity4db35312007-11-21 15:28:32 +02001334 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001335 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001336 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001337 mmu_pte_write_zap_pte(vcpu, sp, spte);
1338 mmu_pte_write_new_pte(vcpu, sp, spte, new, bytes,
Avi Kivityc7addb92007-09-16 18:58:32 +02001339 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001340 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001341 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001342 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001343 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001344 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001345}
1346
Avi Kivitya4360362007-01-05 16:36:45 -08001347int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1348{
1349 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1350
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001351 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001352}
1353
Avi Kivity22d95b12007-09-14 20:26:06 +03001354void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001355{
1356 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001357 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001358
Avi Kivity4db35312007-11-21 15:28:32 +02001359 sp = container_of(vcpu->kvm->active_mmu_pages.prev,
1360 struct kvm_mmu_page, link);
1361 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001362 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001363 }
1364}
Avi Kivityebeace82007-01-05 16:36:47 -08001365
Avi Kivity30677142007-10-28 18:48:59 +02001366int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1367{
1368 int r;
1369 enum emulation_result er;
1370
1371 mutex_lock(&vcpu->kvm->lock);
1372 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1373 if (r < 0)
1374 goto out;
1375
1376 if (!r) {
1377 r = 1;
1378 goto out;
1379 }
1380
Avi Kivityb733bfb2007-10-28 18:52:05 +02001381 r = mmu_topup_memory_caches(vcpu);
1382 if (r)
1383 goto out;
1384
Avi Kivity30677142007-10-28 18:48:59 +02001385 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1386 mutex_unlock(&vcpu->kvm->lock);
1387
1388 switch (er) {
1389 case EMULATE_DONE:
1390 return 1;
1391 case EMULATE_DO_MMIO:
1392 ++vcpu->stat.mmio_exits;
1393 return 0;
1394 case EMULATE_FAIL:
1395 kvm_report_emulation_failure(vcpu, "pagetable");
1396 return 1;
1397 default:
1398 BUG();
1399 }
1400out:
1401 mutex_unlock(&vcpu->kvm->lock);
1402 return r;
1403}
1404EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1405
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406static void free_mmu_pages(struct kvm_vcpu *vcpu)
1407{
Avi Kivity4db35312007-11-21 15:28:32 +02001408 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409
Avi Kivityf51234c2007-01-05 16:36:52 -08001410 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001411 sp = container_of(vcpu->kvm->active_mmu_pages.next,
1412 struct kvm_mmu_page, link);
1413 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001414 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001415 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
1418static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1419{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001420 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 int i;
1422
1423 ASSERT(vcpu);
1424
Izik Eidus82ce2c92007-10-02 18:52:55 +02001425 if (vcpu->kvm->n_requested_mmu_pages)
1426 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1427 else
1428 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001429 /*
1430 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1431 * Therefore we need to allocate shadow page tables in the first
1432 * 4GB of memory, which happens to fit the DMA32 zone.
1433 */
1434 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1435 if (!page)
1436 goto error_1;
1437 vcpu->mmu.pae_root = page_address(page);
1438 for (i = 0; i < 4; ++i)
1439 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1440
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 return 0;
1442
1443error_1:
1444 free_mmu_pages(vcpu);
1445 return -ENOMEM;
1446}
1447
Ingo Molnar8018c272006-12-29 16:50:01 -08001448int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 ASSERT(vcpu);
1451 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
Ingo Molnar8018c272006-12-29 16:50:01 -08001453 return alloc_mmu_pages(vcpu);
1454}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455
Ingo Molnar8018c272006-12-29 16:50:01 -08001456int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1457{
1458 ASSERT(vcpu);
1459 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001460
Ingo Molnar8018c272006-12-29 16:50:01 -08001461 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462}
1463
1464void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1465{
1466 ASSERT(vcpu);
1467
1468 destroy_kvm_mmu(vcpu);
1469 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001470 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471}
1472
Avi Kivity90cb0522007-07-17 13:04:56 +03001473void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474{
Avi Kivity4db35312007-11-21 15:28:32 +02001475 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476
Avi Kivity4db35312007-11-21 15:28:32 +02001477 list_for_each_entry(sp, &kvm->active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 int i;
1479 u64 *pt;
1480
Avi Kivity4db35312007-11-21 15:28:32 +02001481 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482 continue;
1483
Avi Kivity4db35312007-11-21 15:28:32 +02001484 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1486 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001487 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 }
1490}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001491
Avi Kivity90cb0522007-07-17 13:04:56 +03001492void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001493{
Avi Kivity4db35312007-11-21 15:28:32 +02001494 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001495
Avi Kivity4db35312007-11-21 15:28:32 +02001496 list_for_each_entry_safe(sp, node, &kvm->active_mmu_pages, link)
1497 kvm_mmu_zap_page(kvm, sp);
Dor Laore0fa8262007-03-30 13:06:33 +03001498
Avi Kivity90cb0522007-07-17 13:04:56 +03001499 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001500}
1501
Avi Kivityb5a33a72007-04-15 16:31:09 +03001502void kvm_mmu_module_exit(void)
1503{
1504 if (pte_chain_cache)
1505 kmem_cache_destroy(pte_chain_cache);
1506 if (rmap_desc_cache)
1507 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001508 if (mmu_page_header_cache)
1509 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001510}
1511
1512int kvm_mmu_module_init(void)
1513{
1514 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1515 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001516 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001517 if (!pte_chain_cache)
1518 goto nomem;
1519 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1520 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001521 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001522 if (!rmap_desc_cache)
1523 goto nomem;
1524
Avi Kivityd3d25b02007-05-30 12:34:53 +03001525 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1526 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001527 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001528 if (!mmu_page_header_cache)
1529 goto nomem;
1530
Avi Kivityb5a33a72007-04-15 16:31:09 +03001531 return 0;
1532
1533nomem:
1534 kvm_mmu_module_exit();
1535 return -ENOMEM;
1536}
1537
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001538/*
1539 * Caculate mmu pages needed for kvm.
1540 */
1541unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1542{
1543 int i;
1544 unsigned int nr_mmu_pages;
1545 unsigned int nr_pages = 0;
1546
1547 for (i = 0; i < kvm->nmemslots; i++)
1548 nr_pages += kvm->memslots[i].npages;
1549
1550 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1551 nr_mmu_pages = max(nr_mmu_pages,
1552 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1553
1554 return nr_mmu_pages;
1555}
1556
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001557#ifdef AUDIT
1558
1559static const char *audit_msg;
1560
1561static gva_t canonicalize(gva_t gva)
1562{
1563#ifdef CONFIG_X86_64
1564 gva = (long long)(gva << 16) >> 16;
1565#endif
1566 return gva;
1567}
1568
1569static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1570 gva_t va, int level)
1571{
1572 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1573 int i;
1574 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1575
1576 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1577 u64 ent = pt[i];
1578
Avi Kivityc7addb92007-09-16 18:58:32 +02001579 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001580 continue;
1581
1582 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001583 if (level > 1) {
1584 if (ent == shadow_notrap_nonpresent_pte)
1585 printk(KERN_ERR "audit: (%s) nontrapping pte"
1586 " in nonleaf level: levels %d gva %lx"
1587 " level %d pte %llx\n", audit_msg,
1588 vcpu->mmu.root_level, va, level, ent);
1589
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001590 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001591 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001592 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001593 struct page *page = gpa_to_page(vcpu, gpa);
1594 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001595
Avi Kivityc7addb92007-09-16 18:58:32 +02001596 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001597 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001598 printk(KERN_ERR "xx audit error: (%s) levels %d"
1599 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001600 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001601 va, gpa, hpa, ent,
1602 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001603 else if (ent == shadow_notrap_nonpresent_pte
1604 && !is_error_hpa(hpa))
1605 printk(KERN_ERR "audit: (%s) notrap shadow,"
1606 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001607 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001608
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001609 }
1610 }
1611}
1612
1613static void audit_mappings(struct kvm_vcpu *vcpu)
1614{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001615 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001616
1617 if (vcpu->mmu.root_level == 4)
1618 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1619 else
1620 for (i = 0; i < 4; ++i)
1621 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1622 audit_mappings_page(vcpu,
1623 vcpu->mmu.pae_root[i],
1624 i << 30,
1625 2);
1626}
1627
1628static int count_rmaps(struct kvm_vcpu *vcpu)
1629{
1630 int nmaps = 0;
1631 int i, j, k;
1632
1633 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1634 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1635 struct kvm_rmap_desc *d;
1636
1637 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001638 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001639
Izik Eidus290fc382007-09-27 14:11:22 +02001640 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001641 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001642 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001643 ++nmaps;
1644 continue;
1645 }
Izik Eidus290fc382007-09-27 14:11:22 +02001646 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001647 while (d) {
1648 for (k = 0; k < RMAP_EXT; ++k)
1649 if (d->shadow_ptes[k])
1650 ++nmaps;
1651 else
1652 break;
1653 d = d->more;
1654 }
1655 }
1656 }
1657 return nmaps;
1658}
1659
1660static int count_writable_mappings(struct kvm_vcpu *vcpu)
1661{
1662 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001663 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001664 int i;
1665
Avi Kivity4db35312007-11-21 15:28:32 +02001666 list_for_each_entry(sp, &vcpu->kvm->active_mmu_pages, link) {
1667 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001668
Avi Kivity4db35312007-11-21 15:28:32 +02001669 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001670 continue;
1671
1672 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1673 u64 ent = pt[i];
1674
1675 if (!(ent & PT_PRESENT_MASK))
1676 continue;
1677 if (!(ent & PT_WRITABLE_MASK))
1678 continue;
1679 ++nmaps;
1680 }
1681 }
1682 return nmaps;
1683}
1684
1685static void audit_rmap(struct kvm_vcpu *vcpu)
1686{
1687 int n_rmap = count_rmaps(vcpu);
1688 int n_actual = count_writable_mappings(vcpu);
1689
1690 if (n_rmap != n_actual)
1691 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1692 __FUNCTION__, audit_msg, n_rmap, n_actual);
1693}
1694
1695static void audit_write_protection(struct kvm_vcpu *vcpu)
1696{
Avi Kivity4db35312007-11-21 15:28:32 +02001697 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001698 struct kvm_memory_slot *slot;
1699 unsigned long *rmapp;
1700 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001701
Avi Kivity4db35312007-11-21 15:28:32 +02001702 list_for_each_entry(sp, &vcpu->kvm->active_mmu_pages, link) {
1703 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001704 continue;
1705
Avi Kivity4db35312007-11-21 15:28:32 +02001706 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1707 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02001708 rmapp = &slot->rmap[gfn - slot->base_gfn];
1709 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001710 printk(KERN_ERR "%s: (%s) shadow page has writable"
1711 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001712 __FUNCTION__, audit_msg, sp->gfn,
1713 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001714 }
1715}
1716
1717static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1718{
1719 int olddbg = dbg;
1720
1721 dbg = 0;
1722 audit_msg = msg;
1723 audit_rmap(vcpu);
1724 audit_write_protection(vcpu);
1725 audit_mappings(vcpu);
1726 dbg = olddbg;
1727}
1728
1729#endif