blob: 1dcffc49eff8677de1156b367f3fe08819118a5f [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
21#include "kvm.h"
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080022#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
29
30#include <asm/page.h>
31#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020032#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033
Avi Kivity37a7d8b2007-01-05 16:36:56 -080034#undef MMU_DEBUG
35
36#undef AUDIT
37
38#ifdef AUDIT
39static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
40#else
41static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
42#endif
43
44#ifdef MMU_DEBUG
45
46#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
47#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
48
49#else
50
51#define pgprintk(x...) do { } while (0)
52#define rmap_printk(x...) do { } while (0)
53
54#endif
55
56#if defined(MMU_DEBUG) || defined(AUDIT)
57static int dbg = 1;
58#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080059
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080060#ifndef MMU_DEBUG
61#define ASSERT(x) do { } while (0)
62#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080063#define ASSERT(x) \
64 if (!(x)) { \
65 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
66 __FILE__, __LINE__, #x); \
67 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080068#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080069
Avi Kivitycea0f0e2007-01-05 16:36:43 -080070#define PT64_PT_BITS 9
71#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
72#define PT32_PT_BITS 10
73#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080074
75#define PT_WRITABLE_SHIFT 1
76
77#define PT_PRESENT_MASK (1ULL << 0)
78#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
79#define PT_USER_MASK (1ULL << 2)
80#define PT_PWT_MASK (1ULL << 3)
81#define PT_PCD_MASK (1ULL << 4)
82#define PT_ACCESSED_MASK (1ULL << 5)
83#define PT_DIRTY_MASK (1ULL << 6)
84#define PT_PAGE_SIZE_MASK (1ULL << 7)
85#define PT_PAT_MASK (1ULL << 7)
86#define PT_GLOBAL_MASK (1ULL << 8)
87#define PT64_NX_MASK (1ULL << 63)
88
89#define PT_PAT_SHIFT 7
90#define PT_DIR_PAT_SHIFT 12
91#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
92
93#define PT32_DIR_PSE36_SIZE 4
94#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040095#define PT32_DIR_PSE36_MASK \
96 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
98
Avi Kivity6aa8b732006-12-10 02:21:36 -080099#define PT_FIRST_AVAIL_BITS_SHIFT 9
100#define PT64_SECOND_AVAIL_BITS_SHIFT 52
101
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
103
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define VALID_PAGE(x) ((x) != INVALID_PAGE)
105
106#define PT64_LEVEL_BITS 9
107
108#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400109 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110
111#define PT64_LEVEL_MASK(level) \
112 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
113
114#define PT64_INDEX(address, level)\
115 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
116
117
118#define PT32_LEVEL_BITS 10
119
120#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400121 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
123#define PT32_LEVEL_MASK(level) \
124 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
125
126#define PT32_INDEX(address, level)\
127 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
128
129
Avi Kivity27aba762007-03-09 13:04:31 +0200130#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define PT64_DIR_BASE_ADDR_MASK \
132 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
133
134#define PT32_BASE_ADDR_MASK PAGE_MASK
135#define PT32_DIR_BASE_ADDR_MASK \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
137
Avi Kivity79539ce2007-11-21 02:06:21 +0200138#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
139 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PFERR_PRESENT_MASK (1U << 0)
142#define PFERR_WRITE_MASK (1U << 1)
143#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800144#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
146#define PT64_ROOT_LEVEL 4
147#define PT32_ROOT_LEVEL 2
148#define PT32E_ROOT_LEVEL 3
149
150#define PT_DIRECTORY_LEVEL 2
151#define PT_PAGE_TABLE_LEVEL 1
152
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800153#define RMAP_EXT 4
154
155struct kvm_rmap_desc {
156 u64 *shadow_ptes[RMAP_EXT];
157 struct kvm_rmap_desc *more;
158};
159
Avi Kivityb5a33a72007-04-15 16:31:09 +0300160static struct kmem_cache *pte_chain_cache;
161static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300162static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300163
Avi Kivityc7addb92007-09-16 18:58:32 +0200164static u64 __read_mostly shadow_trap_nonpresent_pte;
165static u64 __read_mostly shadow_notrap_nonpresent_pte;
166
167void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
168{
169 shadow_trap_nonpresent_pte = trap_pte;
170 shadow_notrap_nonpresent_pte = notrap_pte;
171}
172EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
173
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174static int is_write_protection(struct kvm_vcpu *vcpu)
175{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000176 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177}
178
179static int is_cpuid_PSE36(void)
180{
181 return 1;
182}
183
Avi Kivity73b10872007-01-26 00:56:41 -0800184static int is_nx(struct kvm_vcpu *vcpu)
185{
186 return vcpu->shadow_efer & EFER_NX;
187}
188
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189static int is_present_pte(unsigned long pte)
190{
191 return pte & PT_PRESENT_MASK;
192}
193
Avi Kivityc7addb92007-09-16 18:58:32 +0200194static int is_shadow_present_pte(u64 pte)
195{
196 pte &= ~PT_SHADOW_IO_MARK;
197 return pte != shadow_trap_nonpresent_pte
198 && pte != shadow_notrap_nonpresent_pte;
199}
200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201static int is_writeble_pte(unsigned long pte)
202{
203 return pte & PT_WRITABLE_MASK;
204}
205
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200206static int is_dirty_pte(unsigned long pte)
207{
208 return pte & PT_DIRTY_MASK;
209}
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static int is_io_pte(unsigned long pte)
212{
213 return pte & PT_SHADOW_IO_MARK;
214}
215
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800216static int is_rmap_pte(u64 pte)
217{
Izik Eidus9647c142007-10-16 14:43:46 +0200218 return pte != shadow_trap_nonpresent_pte
219 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800220}
221
Avi Kivityda928522007-11-21 13:54:47 +0200222static gfn_t pse36_gfn_delta(u32 gpte)
223{
224 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
225
226 return (gpte & PT32_DIR_PSE36_MASK) << shift;
227}
228
Avi Kivitye663ee62007-05-31 15:46:04 +0300229static void set_shadow_pte(u64 *sptep, u64 spte)
230{
231#ifdef CONFIG_X86_64
232 set_64bit((unsigned long *)sptep, spte);
233#else
234 set_64bit((unsigned long long *)sptep, spte);
235#endif
236}
237
Avi Kivitye2dec932007-01-05 16:36:54 -0800238static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300239 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800240{
241 void *obj;
242
243 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800244 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800245 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300246 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800247 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800248 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800249 cache->objects[cache->nobjs++] = obj;
250 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800251 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800252}
253
254static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
255{
256 while (mc->nobjs)
257 kfree(mc->objects[--mc->nobjs]);
258}
259
Avi Kivityc1158e62007-07-20 08:18:27 +0300260static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300261 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300262{
263 struct page *page;
264
265 if (cache->nobjs >= min)
266 return 0;
267 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300268 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300269 if (!page)
270 return -ENOMEM;
271 set_page_private(page, 0);
272 cache->objects[cache->nobjs++] = page_address(page);
273 }
274 return 0;
275}
276
277static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
278{
279 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300280 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300281}
282
Avi Kivity8c438502007-04-16 11:53:17 +0300283static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
284{
285 int r;
286
Avi Kivity22d95b12007-09-14 20:26:06 +0300287 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300288 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
289 pte_chain_cache, 4);
290 if (r)
291 goto out;
292 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
293 rmap_desc_cache, 1);
294 if (r)
295 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200296 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300297 if (r)
298 goto out;
299 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
300 mmu_page_header_cache, 4);
301out:
Avi Kivity8c438502007-04-16 11:53:17 +0300302 return r;
303}
304
Avi Kivity714b93d2007-01-05 16:36:53 -0800305static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
306{
307 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
308 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300309 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300310 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800311}
312
313static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
314 size_t size)
315{
316 void *p;
317
318 BUG_ON(!mc->nobjs);
319 p = mc->objects[--mc->nobjs];
320 memset(p, 0, size);
321 return p;
322}
323
Avi Kivity714b93d2007-01-05 16:36:53 -0800324static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
325{
326 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
327 sizeof(struct kvm_pte_chain));
328}
329
Avi Kivity90cb0522007-07-17 13:04:56 +0300330static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800331{
Avi Kivity90cb0522007-07-17 13:04:56 +0300332 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800333}
334
335static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
336{
337 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
338 sizeof(struct kvm_rmap_desc));
339}
340
Avi Kivity90cb0522007-07-17 13:04:56 +0300341static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800342{
Avi Kivity90cb0522007-07-17 13:04:56 +0300343 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800344}
345
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800346/*
Izik Eidus290fc382007-09-27 14:11:22 +0200347 * Take gfn and return the reverse mapping to it.
348 * Note: gfn must be unaliased before this function get called
349 */
350
351static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
352{
353 struct kvm_memory_slot *slot;
354
355 slot = gfn_to_memslot(kvm, gfn);
356 return &slot->rmap[gfn - slot->base_gfn];
357}
358
359/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800360 * Reverse mapping data structures:
361 *
Izik Eidus290fc382007-09-27 14:11:22 +0200362 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
363 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800364 *
Izik Eidus290fc382007-09-27 14:11:22 +0200365 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
366 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800367 */
Izik Eidus290fc382007-09-27 14:11:22 +0200368static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800369{
Izik Eidus290fc382007-09-27 14:11:22 +0200370 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800371 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200372 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373 int i;
374
375 if (!is_rmap_pte(*spte))
376 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200377 gfn = unalias_gfn(vcpu->kvm, gfn);
378 page = page_header(__pa(spte));
379 page->gfns[spte - page->spt] = gfn;
380 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
381 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800382 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200383 *rmapp = (unsigned long)spte;
384 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800385 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800386 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200387 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800388 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200389 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800390 } else {
391 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200392 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800393 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
394 desc = desc->more;
395 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800396 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800397 desc = desc->more;
398 }
399 for (i = 0; desc->shadow_ptes[i]; ++i)
400 ;
401 desc->shadow_ptes[i] = spte;
402 }
403}
404
Izik Eidus290fc382007-09-27 14:11:22 +0200405static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800406 struct kvm_rmap_desc *desc,
407 int i,
408 struct kvm_rmap_desc *prev_desc)
409{
410 int j;
411
412 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
413 ;
414 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000415 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800416 if (j != 0)
417 return;
418 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200419 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800420 else
421 if (prev_desc)
422 prev_desc->more = desc->more;
423 else
Izik Eidus290fc382007-09-27 14:11:22 +0200424 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300425 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800426}
427
Izik Eidus290fc382007-09-27 14:11:22 +0200428static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800429{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800430 struct kvm_rmap_desc *desc;
431 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200432 struct kvm_mmu_page *page;
Izik Eidusb4231d62007-11-20 11:49:33 +0200433 struct page *release_page;
Izik Eidus290fc382007-09-27 14:11:22 +0200434 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800435 int i;
436
437 if (!is_rmap_pte(*spte))
438 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200439 page = page_header(__pa(spte));
Izik Eidusb4231d62007-11-20 11:49:33 +0200440 release_page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
441 if (is_writeble_pte(*spte))
442 kvm_release_page_dirty(release_page);
443 else
444 kvm_release_page_clean(release_page);
Izik Eidus290fc382007-09-27 14:11:22 +0200445 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
446 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800447 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
448 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200449 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800450 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200451 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800452 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
453 spte, *spte);
454 BUG();
455 }
Izik Eidus290fc382007-09-27 14:11:22 +0200456 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800457 } else {
458 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200459 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800460 prev_desc = NULL;
461 while (desc) {
462 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
463 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200464 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800465 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800466 prev_desc);
467 return;
468 }
469 prev_desc = desc;
470 desc = desc->more;
471 }
472 BUG();
473 }
474}
475
Izik Eidus98348e92007-10-16 14:42:30 +0200476static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800477{
Avi Kivity374cbac2007-01-05 16:36:43 -0800478 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200479 struct kvm_rmap_desc *prev_desc;
480 u64 *prev_spte;
481 int i;
482
483 if (!*rmapp)
484 return NULL;
485 else if (!(*rmapp & 1)) {
486 if (!spte)
487 return (u64 *)*rmapp;
488 return NULL;
489 }
490 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
491 prev_desc = NULL;
492 prev_spte = NULL;
493 while (desc) {
494 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
495 if (prev_spte == spte)
496 return desc->shadow_ptes[i];
497 prev_spte = desc->shadow_ptes[i];
498 }
499 desc = desc->more;
500 }
501 return NULL;
502}
503
504static void rmap_write_protect(struct kvm *kvm, u64 gfn)
505{
Izik Eidus290fc382007-09-27 14:11:22 +0200506 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800507 u64 *spte;
508
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500509 gfn = unalias_gfn(kvm, gfn);
510 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800511
Izik Eidus98348e92007-10-16 14:42:30 +0200512 spte = rmap_next(kvm, rmapp, NULL);
513 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800514 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800515 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800516 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Izik Eidus9647c142007-10-16 14:43:46 +0200517 if (is_writeble_pte(*spte))
518 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500519 kvm_flush_remote_tlbs(kvm);
Izik Eidus9647c142007-10-16 14:43:46 +0200520 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800521 }
522}
523
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800524#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300525static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526{
Avi Kivity139bdb22007-01-05 16:36:50 -0800527 u64 *pos;
528 u64 *end;
529
Avi Kivity47ad8e62007-05-06 15:50:58 +0300530 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200531 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800532 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
533 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800535 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 return 1;
537}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800538#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Avi Kivity90cb0522007-07-17 13:04:56 +0300540static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300541 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800542{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300543 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300544 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300545 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200546 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300547 kfree(page_head);
548 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800549}
550
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800551static unsigned kvm_page_table_hashfn(gfn_t gfn)
552{
553 return gfn;
554}
555
Avi Kivity25c0de22007-01-05 16:36:42 -0800556static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
557 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558{
559 struct kvm_mmu_page *page;
560
Avi Kivityd3d25b02007-05-30 12:34:53 +0300561 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800562 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563
Avi Kivityd3d25b02007-05-30 12:34:53 +0300564 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
565 sizeof *page);
566 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200567 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300568 set_page_private(virt_to_page(page->spt), (unsigned long)page);
569 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300570 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800572 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800574 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800575 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576}
577
Avi Kivity714b93d2007-01-05 16:36:53 -0800578static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
579 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800580{
581 struct kvm_pte_chain *pte_chain;
582 struct hlist_node *node;
583 int i;
584
585 if (!parent_pte)
586 return;
587 if (!page->multimapped) {
588 u64 *old = page->parent_pte;
589
590 if (!old) {
591 page->parent_pte = parent_pte;
592 return;
593 }
594 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800595 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800596 INIT_HLIST_HEAD(&page->parent_ptes);
597 hlist_add_head(&pte_chain->link, &page->parent_ptes);
598 pte_chain->parent_ptes[0] = old;
599 }
600 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
601 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
602 continue;
603 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
604 if (!pte_chain->parent_ptes[i]) {
605 pte_chain->parent_ptes[i] = parent_pte;
606 return;
607 }
608 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800609 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800610 BUG_ON(!pte_chain);
611 hlist_add_head(&pte_chain->link, &page->parent_ptes);
612 pte_chain->parent_ptes[0] = parent_pte;
613}
614
Avi Kivity90cb0522007-07-17 13:04:56 +0300615static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800616 u64 *parent_pte)
617{
618 struct kvm_pte_chain *pte_chain;
619 struct hlist_node *node;
620 int i;
621
622 if (!page->multimapped) {
623 BUG_ON(page->parent_pte != parent_pte);
624 page->parent_pte = NULL;
625 return;
626 }
627 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
628 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
629 if (!pte_chain->parent_ptes[i])
630 break;
631 if (pte_chain->parent_ptes[i] != parent_pte)
632 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800633 while (i + 1 < NR_PTE_CHAIN_ENTRIES
634 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800635 pte_chain->parent_ptes[i]
636 = pte_chain->parent_ptes[i + 1];
637 ++i;
638 }
639 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800640 if (i == 0) {
641 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300642 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800643 if (hlist_empty(&page->parent_ptes)) {
644 page->multimapped = 0;
645 page->parent_pte = NULL;
646 }
647 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800648 return;
649 }
650 BUG();
651}
652
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500653static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800654 gfn_t gfn)
655{
656 unsigned index;
657 struct hlist_head *bucket;
658 struct kvm_mmu_page *page;
659 struct hlist_node *node;
660
661 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
662 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500663 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800664 hlist_for_each_entry(page, node, bucket, hash_link)
665 if (page->gfn == gfn && !page->role.metaphysical) {
666 pgprintk("%s: found role %x\n",
667 __FUNCTION__, page->role.word);
668 return page;
669 }
670 return NULL;
671}
672
673static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
674 gfn_t gfn,
675 gva_t gaddr,
676 unsigned level,
677 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200678 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800679 u64 *parent_pte)
680{
681 union kvm_mmu_page_role role;
682 unsigned index;
683 unsigned quadrant;
684 struct hlist_head *bucket;
685 struct kvm_mmu_page *page;
686 struct hlist_node *node;
687
688 role.word = 0;
689 role.glevels = vcpu->mmu.root_level;
690 role.level = level;
691 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200692 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800693 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
694 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
695 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
696 role.quadrant = quadrant;
697 }
698 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
699 gfn, role.word);
700 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
701 bucket = &vcpu->kvm->mmu_page_hash[index];
702 hlist_for_each_entry(page, node, bucket, hash_link)
703 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800704 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800705 pgprintk("%s: found\n", __FUNCTION__);
706 return page;
707 }
708 page = kvm_mmu_alloc_page(vcpu, parent_pte);
709 if (!page)
710 return page;
711 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
712 page->gfn = gfn;
713 page->role = role;
714 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200715 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800716 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500717 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800718 return page;
719}
720
Avi Kivity90cb0522007-07-17 13:04:56 +0300721static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800722 struct kvm_mmu_page *page)
723{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800724 unsigned i;
725 u64 *pt;
726 u64 ent;
727
Avi Kivity47ad8e62007-05-06 15:50:58 +0300728 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800729
730 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
731 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200732 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200733 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200734 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800735 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300736 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800737 return;
738 }
739
740 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
741 ent = pt[i];
742
Avi Kivityc7addb92007-09-16 18:58:32 +0200743 pt[i] = shadow_trap_nonpresent_pte;
744 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800745 continue;
746 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300747 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800748 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300749 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800750}
751
Avi Kivity90cb0522007-07-17 13:04:56 +0300752static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800753 u64 *parent_pte)
754{
Avi Kivity90cb0522007-07-17 13:04:56 +0300755 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800756}
757
Avi Kivity12b7d282007-09-23 14:10:49 +0200758static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
759{
760 int i;
761
762 for (i = 0; i < KVM_MAX_VCPUS; ++i)
763 if (kvm->vcpus[i])
764 kvm->vcpus[i]->last_pte_updated = NULL;
765}
766
Avi Kivity90cb0522007-07-17 13:04:56 +0300767static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800768 struct kvm_mmu_page *page)
769{
770 u64 *parent_pte;
771
Avi Kivity4cee5762007-11-18 16:37:07 +0200772 ++kvm->stat.mmu_shadow_zapped;
Avi Kivitya4360362007-01-05 16:36:45 -0800773 while (page->multimapped || page->parent_pte) {
774 if (!page->multimapped)
775 parent_pte = page->parent_pte;
776 else {
777 struct kvm_pte_chain *chain;
778
779 chain = container_of(page->parent_ptes.first,
780 struct kvm_pte_chain, link);
781 parent_pte = chain->parent_ptes[0];
782 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800783 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300784 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200785 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800786 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300787 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800788 if (!page->root_count) {
789 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300790 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200791 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300792 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200793 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800794}
795
Izik Eidus82ce2c92007-10-02 18:52:55 +0200796/*
797 * Changing the number of mmu pages allocated to the vm
798 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
799 */
800void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
801{
802 /*
803 * If we set the number of mmu pages to be smaller be than the
804 * number of actived pages , we must to free some mmu pages before we
805 * change the value
806 */
807
808 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
809 kvm_nr_mmu_pages) {
810 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
811 - kvm->n_free_mmu_pages;
812
813 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
814 struct kvm_mmu_page *page;
815
816 page = container_of(kvm->active_mmu_pages.prev,
817 struct kvm_mmu_page, link);
818 kvm_mmu_zap_page(kvm, page);
819 n_used_mmu_pages--;
820 }
821 kvm->n_free_mmu_pages = 0;
822 }
823 else
824 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
825 - kvm->n_alloc_mmu_pages;
826
827 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
828}
829
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500830static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800831{
832 unsigned index;
833 struct hlist_head *bucket;
834 struct kvm_mmu_page *page;
835 struct hlist_node *node, *n;
836 int r;
837
838 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
839 r = 0;
840 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500841 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800842 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
843 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800844 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
845 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500846 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800847 r = 1;
848 }
849 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800850}
851
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500852static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300853{
854 struct kvm_mmu_page *page;
855
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500856 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300857 pgprintk("%s: zap %lx %x\n",
858 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500859 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300860 }
861}
862
Avi Kivity38c335f2007-11-21 14:20:22 +0200863static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864{
Avi Kivity38c335f2007-11-21 14:20:22 +0200865 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 struct kvm_mmu_page *page_head = page_header(__pa(pte));
867
868 __set_bit(slot, &page_head->slot_bitmap);
869}
870
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500871hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 struct page *page;
Izik Eiduscea7bb22007-10-17 19:17:48 +0200874 hpa_t hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
876 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500877 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200878 hpa = ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) | (gpa & (PAGE_SIZE-1));
879 if (is_error_page(page))
880 return hpa | HPA_ERR_MASK;
881 return hpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882}
883
884hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
885{
886 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
887
888 if (gpa == UNMAPPED_GVA)
889 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500890 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891}
892
Avi Kivity039576c2007-03-20 12:46:50 +0200893struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
894{
895 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
896
897 if (gpa == UNMAPPED_GVA)
898 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500899 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200900}
901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
903{
904}
905
906static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
907{
908 int level = PT32E_ROOT_LEVEL;
909 hpa_t table_addr = vcpu->mmu.root_hpa;
Izik Eidusb4231d62007-11-20 11:49:33 +0200910 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911
Izik Eidusb4231d62007-11-20 11:49:33 +0200912 page = pfn_to_page(p >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 for (; ; level--) {
914 u32 index = PT64_INDEX(v, level);
915 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800916 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917
918 ASSERT(VALID_PAGE(table_addr));
919 table = __va(table_addr);
920
921 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200922 int was_rmapped;
923
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200925 was_rmapped = is_rmap_pte(pte);
Izik Eidus2065b372007-11-20 11:30:04 +0200926 if (is_shadow_present_pte(pte) && is_writeble_pte(pte)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200927 kvm_release_page_clean(page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800928 return 0;
Izik Eidus2065b372007-11-20 11:30:04 +0200929 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
Avi Kivity38c335f2007-11-21 14:20:22 +0200931 page_header_update_slot(vcpu->kvm, table,
932 v >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
934 PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200935 if (!was_rmapped)
936 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200937 else
Izik Eidusb4231d62007-11-20 11:49:33 +0200938 kvm_release_page_clean(page);
939
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 return 0;
941 }
942
Avi Kivityc7addb92007-09-16 18:58:32 +0200943 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800944 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800945 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
948 >> PAGE_SHIFT;
949 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
950 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200951 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800952 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidusb4231d62007-11-20 11:49:33 +0200954 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 return -ENOMEM;
956 }
957
Avi Kivity47ad8e62007-05-06 15:50:58 +0300958 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800959 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 }
961 table_addr = table[index] & PT64_BASE_ADDR_MASK;
962 }
963}
964
Avi Kivityc7addb92007-09-16 18:58:32 +0200965static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
966 struct kvm_mmu_page *sp)
967{
968 int i;
969
970 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
971 sp->spt[i] = shadow_trap_nonpresent_pte;
972}
973
Avi Kivity17ac10a2007-01-05 16:36:40 -0800974static void mmu_free_roots(struct kvm_vcpu *vcpu)
975{
976 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800977 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800978
Avi Kivity7b53aa52007-06-05 12:17:03 +0300979 if (!VALID_PAGE(vcpu->mmu.root_hpa))
980 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800981#ifdef CONFIG_X86_64
982 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
983 hpa_t root = vcpu->mmu.root_hpa;
984
Avi Kivity3bb65a22007-01-05 16:36:51 -0800985 page = page_header(root);
986 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800987 vcpu->mmu.root_hpa = INVALID_PAGE;
988 return;
989 }
990#endif
991 for (i = 0; i < 4; ++i) {
992 hpa_t root = vcpu->mmu.pae_root[i];
993
Avi Kivity417726a2007-04-12 17:35:58 +0300994 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300995 root &= PT64_BASE_ADDR_MASK;
996 page = page_header(root);
997 --page->root_count;
998 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800999 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1000 }
1001 vcpu->mmu.root_hpa = INVALID_PAGE;
1002}
1003
1004static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1005{
1006 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001008 struct kvm_mmu_page *page;
1009
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001010 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001011
1012#ifdef CONFIG_X86_64
1013 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1014 hpa_t root = vcpu->mmu.root_hpa;
1015
1016 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -08001017 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001018 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001019 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001020 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001021 vcpu->mmu.root_hpa = root;
1022 return;
1023 }
1024#endif
1025 for (i = 0; i < 4; ++i) {
1026 hpa_t root = vcpu->mmu.pae_root[i];
1027
1028 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001029 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1030 if (!is_present_pte(vcpu->pdptrs[i])) {
1031 vcpu->mmu.pae_root[i] = 0;
1032 continue;
1033 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001034 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001035 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001036 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -08001037 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001038 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001039 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001040 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001041 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001042 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1043 }
1044 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1045}
1046
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1048{
1049 return vaddr;
1050}
1051
1052static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1053 u32 error_code)
1054{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001056 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001057 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058
Avi Kivitye2dec932007-01-05 16:36:54 -08001059 r = mmu_topup_memory_caches(vcpu);
1060 if (r)
1061 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001062
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 ASSERT(vcpu);
1064 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1065
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001067 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068
Izik Eidus8a7ae052007-10-18 11:09:33 +02001069 if (is_error_hpa(paddr)) {
Izik Eidusb4231d62007-11-20 11:49:33 +02001070 kvm_release_page_clean(pfn_to_page((paddr & PT64_BASE_ADDR_MASK)
1071 >> PAGE_SHIFT));
Avi Kivityebeace82007-01-05 16:36:47 -08001072 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001073 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074
Avi Kivityebeace82007-01-05 16:36:47 -08001075 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076}
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078static void nonpaging_free(struct kvm_vcpu *vcpu)
1079{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001080 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081}
1082
1083static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1084{
1085 struct kvm_mmu *context = &vcpu->mmu;
1086
1087 context->new_cr3 = nonpaging_new_cr3;
1088 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 context->gva_to_gpa = nonpaging_gva_to_gpa;
1090 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001091 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001092 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001094 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 return 0;
1096}
1097
Avi Kivityd835dfe2007-11-21 02:57:59 +02001098void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001100 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001101 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102}
1103
1104static void paging_new_cr3(struct kvm_vcpu *vcpu)
1105{
Avi Kivity374cbac2007-01-05 16:36:43 -08001106 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001107 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108}
1109
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110static void inject_page_fault(struct kvm_vcpu *vcpu,
1111 u64 addr,
1112 u32 err_code)
1113{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001114 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115}
1116
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117static void paging_free(struct kvm_vcpu *vcpu)
1118{
1119 nonpaging_free(vcpu);
1120}
1121
1122#define PTTYPE 64
1123#include "paging_tmpl.h"
1124#undef PTTYPE
1125
1126#define PTTYPE 32
1127#include "paging_tmpl.h"
1128#undef PTTYPE
1129
Avi Kivity17ac10a2007-01-05 16:36:40 -08001130static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131{
1132 struct kvm_mmu *context = &vcpu->mmu;
1133
1134 ASSERT(is_pae(vcpu));
1135 context->new_cr3 = paging_new_cr3;
1136 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001138 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001140 context->root_level = level;
1141 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001142 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 return 0;
1144}
1145
Avi Kivity17ac10a2007-01-05 16:36:40 -08001146static int paging64_init_context(struct kvm_vcpu *vcpu)
1147{
1148 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1149}
1150
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151static int paging32_init_context(struct kvm_vcpu *vcpu)
1152{
1153 struct kvm_mmu *context = &vcpu->mmu;
1154
1155 context->new_cr3 = paging_new_cr3;
1156 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 context->gva_to_gpa = paging32_gva_to_gpa;
1158 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001159 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 context->root_level = PT32_ROOT_LEVEL;
1161 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001162 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 return 0;
1164}
1165
1166static int paging32E_init_context(struct kvm_vcpu *vcpu)
1167{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001168 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169}
1170
1171static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1172{
1173 ASSERT(vcpu);
1174 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1175
1176 if (!is_paging(vcpu))
1177 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001178 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179 return paging64_init_context(vcpu);
1180 else if (is_pae(vcpu))
1181 return paging32E_init_context(vcpu);
1182 else
1183 return paging32_init_context(vcpu);
1184}
1185
1186static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1187{
1188 ASSERT(vcpu);
1189 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1190 vcpu->mmu.free(vcpu);
1191 vcpu->mmu.root_hpa = INVALID_PAGE;
1192 }
1193}
1194
1195int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1196{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001197 destroy_kvm_mmu(vcpu);
1198 return init_kvm_mmu(vcpu);
1199}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001200EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001201
1202int kvm_mmu_load(struct kvm_vcpu *vcpu)
1203{
Avi Kivity714b93d2007-01-05 16:36:53 -08001204 int r;
1205
Shaohua Li11ec2802007-07-23 14:51:37 +08001206 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001207 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001208 if (r)
1209 goto out;
1210 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001211 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001212 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001213out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001214 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001215 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001217EXPORT_SYMBOL_GPL(kvm_mmu_load);
1218
1219void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1220{
1221 mmu_free_roots(vcpu);
1222}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223
Avi Kivity09072da2007-05-01 14:16:52 +03001224static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001225 struct kvm_mmu_page *page,
1226 u64 *spte)
1227{
1228 u64 pte;
1229 struct kvm_mmu_page *child;
1230
1231 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001232 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001233 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001234 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001235 else {
1236 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001237 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001238 }
1239 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001240 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001241}
1242
Avi Kivity00284252007-05-01 16:53:31 +03001243static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1244 struct kvm_mmu_page *page,
1245 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001246 const void *new, int bytes,
1247 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001248{
Avi Kivity4cee5762007-11-18 16:37:07 +02001249 if (page->role.level != PT_PAGE_TABLE_LEVEL) {
1250 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001251 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001252 }
Avi Kivity00284252007-05-01 16:53:31 +03001253
Avi Kivity4cee5762007-11-18 16:37:07 +02001254 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity00284252007-05-01 16:53:31 +03001255 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001256 paging32_update_pte(vcpu, page, spte, new, bytes,
1257 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001258 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001259 paging64_update_pte(vcpu, page, spte, new, bytes,
1260 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001261}
1262
Avi Kivity79539ce2007-11-21 02:06:21 +02001263static bool need_remote_flush(u64 old, u64 new)
1264{
1265 if (!is_shadow_present_pte(old))
1266 return false;
1267 if (!is_shadow_present_pte(new))
1268 return true;
1269 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1270 return true;
1271 old ^= PT64_NX_MASK;
1272 new ^= PT64_NX_MASK;
1273 return (old & ~new & PT64_PERM_MASK) != 0;
1274}
1275
1276static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1277{
1278 if (need_remote_flush(old, new))
1279 kvm_flush_remote_tlbs(vcpu->kvm);
1280 else
1281 kvm_mmu_flush_tlb(vcpu);
1282}
1283
Avi Kivity12b7d282007-09-23 14:10:49 +02001284static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1285{
1286 u64 *spte = vcpu->last_pte_updated;
1287
1288 return !!(spte && (*spte & PT_ACCESSED_MASK));
1289}
1290
Avi Kivity09072da2007-05-01 14:16:52 +03001291void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001292 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001293{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001294 gfn_t gfn = gpa >> PAGE_SHIFT;
1295 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001296 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001297 struct hlist_head *bucket;
1298 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001299 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001300 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001301 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001302 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001303 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001304 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001305 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001306 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001307 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001308 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001309
Avi Kivityda4a00f2007-01-05 16:36:44 -08001310 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity4cee5762007-11-18 16:37:07 +02001311 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001312 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001313 if (gfn == vcpu->last_pt_write_gfn
1314 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001315 ++vcpu->last_pt_write_count;
1316 if (vcpu->last_pt_write_count >= 3)
1317 flooded = 1;
1318 } else {
1319 vcpu->last_pt_write_gfn = gfn;
1320 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001321 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001322 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001323 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1324 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001325 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001326 if (page->gfn != gfn || page->role.metaphysical)
1327 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001328 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1329 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001330 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001331 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001332 /*
1333 * Misaligned accesses are too much trouble to fix
1334 * up; also, they usually indicate a page is not used
1335 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001336 *
1337 * If we're seeing too many writes to a page,
1338 * it may no longer be a page table, or we may be
1339 * forking, in which case it is better to unmap the
1340 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001341 */
1342 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1343 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001344 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001345 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001346 continue;
1347 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001348 page_offset = offset;
1349 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001350 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001351 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001352 page_offset <<= 1; /* 32->64 */
1353 /*
1354 * A 32-bit pde maps 4MB while the shadow pdes map
1355 * only 2MB. So we need to double the offset again
1356 * and zap two pdes instead of one.
1357 */
1358 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001359 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001360 page_offset <<= 1;
1361 npte = 2;
1362 }
Avi Kivityfce06572007-05-01 16:44:05 +03001363 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001364 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001365 if (quadrant != page->role.quadrant)
1366 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001367 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001368 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001369 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001370 entry = *spte;
Avi Kivity09072da2007-05-01 14:16:52 +03001371 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001372 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1373 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001374 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001375 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001376 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001377 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001378 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001379}
1380
Avi Kivitya4360362007-01-05 16:36:45 -08001381int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1382{
1383 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1384
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001385 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001386}
1387
Avi Kivity22d95b12007-09-14 20:26:06 +03001388void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001389{
1390 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1391 struct kvm_mmu_page *page;
1392
1393 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1394 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001395 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity4cee5762007-11-18 16:37:07 +02001396 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001397 }
1398}
Avi Kivityebeace82007-01-05 16:36:47 -08001399
Avi Kivity30677142007-10-28 18:48:59 +02001400int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1401{
1402 int r;
1403 enum emulation_result er;
1404
1405 mutex_lock(&vcpu->kvm->lock);
1406 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1407 if (r < 0)
1408 goto out;
1409
1410 if (!r) {
1411 r = 1;
1412 goto out;
1413 }
1414
Avi Kivityb733bfb2007-10-28 18:52:05 +02001415 r = mmu_topup_memory_caches(vcpu);
1416 if (r)
1417 goto out;
1418
Avi Kivity30677142007-10-28 18:48:59 +02001419 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1420 mutex_unlock(&vcpu->kvm->lock);
1421
1422 switch (er) {
1423 case EMULATE_DONE:
1424 return 1;
1425 case EMULATE_DO_MMIO:
1426 ++vcpu->stat.mmio_exits;
1427 return 0;
1428 case EMULATE_FAIL:
1429 kvm_report_emulation_failure(vcpu, "pagetable");
1430 return 1;
1431 default:
1432 BUG();
1433 }
1434out:
1435 mutex_unlock(&vcpu->kvm->lock);
1436 return r;
1437}
1438EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1439
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440static void free_mmu_pages(struct kvm_vcpu *vcpu)
1441{
Avi Kivityf51234c2007-01-05 16:36:52 -08001442 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443
Avi Kivityf51234c2007-01-05 16:36:52 -08001444 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1445 page = container_of(vcpu->kvm->active_mmu_pages.next,
1446 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001447 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001448 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001449 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450}
1451
1452static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1453{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001454 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 int i;
1456
1457 ASSERT(vcpu);
1458
Izik Eidus82ce2c92007-10-02 18:52:55 +02001459 if (vcpu->kvm->n_requested_mmu_pages)
1460 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1461 else
1462 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001463 /*
1464 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1465 * Therefore we need to allocate shadow page tables in the first
1466 * 4GB of memory, which happens to fit the DMA32 zone.
1467 */
1468 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1469 if (!page)
1470 goto error_1;
1471 vcpu->mmu.pae_root = page_address(page);
1472 for (i = 0; i < 4; ++i)
1473 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1474
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 return 0;
1476
1477error_1:
1478 free_mmu_pages(vcpu);
1479 return -ENOMEM;
1480}
1481
Ingo Molnar8018c272006-12-29 16:50:01 -08001482int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 ASSERT(vcpu);
1485 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
Ingo Molnar8018c272006-12-29 16:50:01 -08001487 return alloc_mmu_pages(vcpu);
1488}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489
Ingo Molnar8018c272006-12-29 16:50:01 -08001490int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1491{
1492 ASSERT(vcpu);
1493 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001494
Ingo Molnar8018c272006-12-29 16:50:01 -08001495 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
1498void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1499{
1500 ASSERT(vcpu);
1501
1502 destroy_kvm_mmu(vcpu);
1503 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001504 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505}
1506
Avi Kivity90cb0522007-07-17 13:04:56 +03001507void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508{
1509 struct kvm_mmu_page *page;
1510
1511 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1512 int i;
1513 u64 *pt;
1514
1515 if (!test_bit(slot, &page->slot_bitmap))
1516 continue;
1517
Avi Kivity47ad8e62007-05-06 15:50:58 +03001518 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1520 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001521 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 }
1524}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001525
Avi Kivity90cb0522007-07-17 13:04:56 +03001526void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001527{
Avi Kivity90cb0522007-07-17 13:04:56 +03001528 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001529
Avi Kivity90cb0522007-07-17 13:04:56 +03001530 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1531 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001532
Avi Kivity90cb0522007-07-17 13:04:56 +03001533 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001534}
1535
Avi Kivityb5a33a72007-04-15 16:31:09 +03001536void kvm_mmu_module_exit(void)
1537{
1538 if (pte_chain_cache)
1539 kmem_cache_destroy(pte_chain_cache);
1540 if (rmap_desc_cache)
1541 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001542 if (mmu_page_header_cache)
1543 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001544}
1545
1546int kvm_mmu_module_init(void)
1547{
1548 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1549 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001550 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001551 if (!pte_chain_cache)
1552 goto nomem;
1553 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1554 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001555 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001556 if (!rmap_desc_cache)
1557 goto nomem;
1558
Avi Kivityd3d25b02007-05-30 12:34:53 +03001559 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1560 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001561 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001562 if (!mmu_page_header_cache)
1563 goto nomem;
1564
Avi Kivityb5a33a72007-04-15 16:31:09 +03001565 return 0;
1566
1567nomem:
1568 kvm_mmu_module_exit();
1569 return -ENOMEM;
1570}
1571
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001572/*
1573 * Caculate mmu pages needed for kvm.
1574 */
1575unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1576{
1577 int i;
1578 unsigned int nr_mmu_pages;
1579 unsigned int nr_pages = 0;
1580
1581 for (i = 0; i < kvm->nmemslots; i++)
1582 nr_pages += kvm->memslots[i].npages;
1583
1584 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1585 nr_mmu_pages = max(nr_mmu_pages,
1586 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1587
1588 return nr_mmu_pages;
1589}
1590
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001591#ifdef AUDIT
1592
1593static const char *audit_msg;
1594
1595static gva_t canonicalize(gva_t gva)
1596{
1597#ifdef CONFIG_X86_64
1598 gva = (long long)(gva << 16) >> 16;
1599#endif
1600 return gva;
1601}
1602
1603static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1604 gva_t va, int level)
1605{
1606 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1607 int i;
1608 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1609
1610 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1611 u64 ent = pt[i];
1612
Avi Kivityc7addb92007-09-16 18:58:32 +02001613 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001614 continue;
1615
1616 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001617 if (level > 1) {
1618 if (ent == shadow_notrap_nonpresent_pte)
1619 printk(KERN_ERR "audit: (%s) nontrapping pte"
1620 " in nonleaf level: levels %d gva %lx"
1621 " level %d pte %llx\n", audit_msg,
1622 vcpu->mmu.root_level, va, level, ent);
1623
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001624 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001625 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001626 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1627 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001628 struct page *page;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001629
Avi Kivityc7addb92007-09-16 18:58:32 +02001630 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001631 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001632 printk(KERN_ERR "xx audit error: (%s) levels %d"
1633 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001634 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001635 va, gpa, hpa, ent,
1636 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001637 else if (ent == shadow_notrap_nonpresent_pte
1638 && !is_error_hpa(hpa))
1639 printk(KERN_ERR "audit: (%s) notrap shadow,"
1640 " valid guest gva %lx\n", audit_msg, va);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001641 page = pfn_to_page((gpa & PT64_BASE_ADDR_MASK)
1642 >> PAGE_SHIFT);
Izik Eidusb4231d62007-11-20 11:49:33 +02001643 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001644
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001645 }
1646 }
1647}
1648
1649static void audit_mappings(struct kvm_vcpu *vcpu)
1650{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001651 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001652
1653 if (vcpu->mmu.root_level == 4)
1654 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1655 else
1656 for (i = 0; i < 4; ++i)
1657 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1658 audit_mappings_page(vcpu,
1659 vcpu->mmu.pae_root[i],
1660 i << 30,
1661 2);
1662}
1663
1664static int count_rmaps(struct kvm_vcpu *vcpu)
1665{
1666 int nmaps = 0;
1667 int i, j, k;
1668
1669 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1670 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1671 struct kvm_rmap_desc *d;
1672
1673 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001674 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001675
Izik Eidus290fc382007-09-27 14:11:22 +02001676 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001677 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001678 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001679 ++nmaps;
1680 continue;
1681 }
Izik Eidus290fc382007-09-27 14:11:22 +02001682 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001683 while (d) {
1684 for (k = 0; k < RMAP_EXT; ++k)
1685 if (d->shadow_ptes[k])
1686 ++nmaps;
1687 else
1688 break;
1689 d = d->more;
1690 }
1691 }
1692 }
1693 return nmaps;
1694}
1695
1696static int count_writable_mappings(struct kvm_vcpu *vcpu)
1697{
1698 int nmaps = 0;
1699 struct kvm_mmu_page *page;
1700 int i;
1701
1702 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001703 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001704
1705 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1706 continue;
1707
1708 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1709 u64 ent = pt[i];
1710
1711 if (!(ent & PT_PRESENT_MASK))
1712 continue;
1713 if (!(ent & PT_WRITABLE_MASK))
1714 continue;
1715 ++nmaps;
1716 }
1717 }
1718 return nmaps;
1719}
1720
1721static void audit_rmap(struct kvm_vcpu *vcpu)
1722{
1723 int n_rmap = count_rmaps(vcpu);
1724 int n_actual = count_writable_mappings(vcpu);
1725
1726 if (n_rmap != n_actual)
1727 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1728 __FUNCTION__, audit_msg, n_rmap, n_actual);
1729}
1730
1731static void audit_write_protection(struct kvm_vcpu *vcpu)
1732{
1733 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001734 struct kvm_memory_slot *slot;
1735 unsigned long *rmapp;
1736 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001737
1738 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001739 if (page->role.metaphysical)
1740 continue;
1741
Izik Eidus290fc382007-09-27 14:11:22 +02001742 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1743 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1744 rmapp = &slot->rmap[gfn - slot->base_gfn];
1745 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001746 printk(KERN_ERR "%s: (%s) shadow page has writable"
1747 " mappings: gfn %lx role %x\n",
1748 __FUNCTION__, audit_msg, page->gfn,
1749 page->role.word);
1750 }
1751}
1752
1753static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1754{
1755 int olddbg = dbg;
1756
1757 dbg = 0;
1758 audit_msg = msg;
1759 audit_rmap(vcpu);
1760 audit_write_protection(vcpu);
1761 audit_mappings(vcpu);
1762 dbg = olddbg;
1763}
1764
1765#endif