blob: 62a74151f84798f1c103a775c397ba32130a5eac [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>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Avi Kivitye4956062007-06-28 14:15:57 -040030
31#include <asm/page.h>
32#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020033#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040034
Avi Kivity37a7d8b2007-01-05 16:36:56 -080035#undef MMU_DEBUG
36
37#undef AUDIT
38
39#ifdef AUDIT
40static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
41#else
42static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
43#endif
44
45#ifdef MMU_DEBUG
46
47#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
48#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
49
50#else
51
52#define pgprintk(x...) do { } while (0)
53#define rmap_printk(x...) do { } while (0)
54
55#endif
56
57#if defined(MMU_DEBUG) || defined(AUDIT)
58static int dbg = 1;
59#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080060
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080061#ifndef MMU_DEBUG
62#define ASSERT(x) do { } while (0)
63#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080064#define ASSERT(x) \
65 if (!(x)) { \
66 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
67 __FILE__, __LINE__, #x); \
68 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080069#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
Avi Kivitycea0f0e2007-01-05 16:36:43 -080071#define PT64_PT_BITS 9
72#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
73#define PT32_PT_BITS 10
74#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075
76#define PT_WRITABLE_SHIFT 1
77
78#define PT_PRESENT_MASK (1ULL << 0)
79#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
80#define PT_USER_MASK (1ULL << 2)
81#define PT_PWT_MASK (1ULL << 3)
82#define PT_PCD_MASK (1ULL << 4)
83#define PT_ACCESSED_MASK (1ULL << 5)
84#define PT_DIRTY_MASK (1ULL << 6)
85#define PT_PAGE_SIZE_MASK (1ULL << 7)
86#define PT_PAT_MASK (1ULL << 7)
87#define PT_GLOBAL_MASK (1ULL << 8)
88#define PT64_NX_MASK (1ULL << 63)
89
90#define PT_PAT_SHIFT 7
91#define PT_DIR_PAT_SHIFT 12
92#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
93
94#define PT32_DIR_PSE36_SIZE 4
95#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040096#define PT32_DIR_PSE36_MASK \
97 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
99
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100#define PT_FIRST_AVAIL_BITS_SHIFT 9
101#define PT64_SECOND_AVAIL_BITS_SHIFT 52
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
104
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105#define VALID_PAGE(x) ((x) != INVALID_PAGE)
106
107#define PT64_LEVEL_BITS 9
108
109#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400110 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111
112#define PT64_LEVEL_MASK(level) \
113 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
114
115#define PT64_INDEX(address, level)\
116 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
117
118
119#define PT32_LEVEL_BITS 10
120
121#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400122 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123
124#define PT32_LEVEL_MASK(level) \
125 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
126
127#define PT32_INDEX(address, level)\
128 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
129
130
Avi Kivity27aba762007-03-09 13:04:31 +0200131#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132#define PT64_DIR_BASE_ADDR_MASK \
133 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
134
135#define PT32_BASE_ADDR_MASK PAGE_MASK
136#define PT32_DIR_BASE_ADDR_MASK \
137 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
138
Avi Kivity79539ce2007-11-21 02:06:21 +0200139#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
140 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141
142#define PFERR_PRESENT_MASK (1U << 0)
143#define PFERR_WRITE_MASK (1U << 1)
144#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800145#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146
147#define PT64_ROOT_LEVEL 4
148#define PT32_ROOT_LEVEL 2
149#define PT32E_ROOT_LEVEL 3
150
151#define PT_DIRECTORY_LEVEL 2
152#define PT_PAGE_TABLE_LEVEL 1
153
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800154#define RMAP_EXT 4
155
156struct kvm_rmap_desc {
157 u64 *shadow_ptes[RMAP_EXT];
158 struct kvm_rmap_desc *more;
159};
160
Avi Kivityb5a33a72007-04-15 16:31:09 +0300161static struct kmem_cache *pte_chain_cache;
162static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300163static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300164
Avi Kivityc7addb92007-09-16 18:58:32 +0200165static u64 __read_mostly shadow_trap_nonpresent_pte;
166static u64 __read_mostly shadow_notrap_nonpresent_pte;
167
168void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
169{
170 shadow_trap_nonpresent_pte = trap_pte;
171 shadow_notrap_nonpresent_pte = notrap_pte;
172}
173EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
174
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175static int is_write_protection(struct kvm_vcpu *vcpu)
176{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000177 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178}
179
180static int is_cpuid_PSE36(void)
181{
182 return 1;
183}
184
Avi Kivity73b10872007-01-26 00:56:41 -0800185static int is_nx(struct kvm_vcpu *vcpu)
186{
187 return vcpu->shadow_efer & EFER_NX;
188}
189
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190static int is_present_pte(unsigned long pte)
191{
192 return pte & PT_PRESENT_MASK;
193}
194
Avi Kivityc7addb92007-09-16 18:58:32 +0200195static int is_shadow_present_pte(u64 pte)
196{
197 pte &= ~PT_SHADOW_IO_MARK;
198 return pte != shadow_trap_nonpresent_pte
199 && pte != shadow_notrap_nonpresent_pte;
200}
201
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202static int is_writeble_pte(unsigned long pte)
203{
204 return pte & PT_WRITABLE_MASK;
205}
206
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200207static int is_dirty_pte(unsigned long pte)
208{
209 return pte & PT_DIRTY_MASK;
210}
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static int is_io_pte(unsigned long pte)
213{
214 return pte & PT_SHADOW_IO_MARK;
215}
216
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800217static int is_rmap_pte(u64 pte)
218{
Izik Eidus9647c142007-10-16 14:43:46 +0200219 return pte != shadow_trap_nonpresent_pte
220 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800221}
222
Avi Kivityda9285212007-11-21 13:54:47 +0200223static gfn_t pse36_gfn_delta(u32 gpte)
224{
225 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
226
227 return (gpte & PT32_DIR_PSE36_MASK) << shift;
228}
229
Avi Kivitye663ee62007-05-31 15:46:04 +0300230static void set_shadow_pte(u64 *sptep, u64 spte)
231{
232#ifdef CONFIG_X86_64
233 set_64bit((unsigned long *)sptep, spte);
234#else
235 set_64bit((unsigned long long *)sptep, spte);
236#endif
237}
238
Avi Kivitye2dec932007-01-05 16:36:54 -0800239static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300240 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800241{
242 void *obj;
243
244 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800245 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800246 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300247 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800248 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800249 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800250 cache->objects[cache->nobjs++] = obj;
251 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800252 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800253}
254
255static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
256{
257 while (mc->nobjs)
258 kfree(mc->objects[--mc->nobjs]);
259}
260
Avi Kivityc1158e62007-07-20 08:18:27 +0300261static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300262 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300263{
264 struct page *page;
265
266 if (cache->nobjs >= min)
267 return 0;
268 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300269 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300270 if (!page)
271 return -ENOMEM;
272 set_page_private(page, 0);
273 cache->objects[cache->nobjs++] = page_address(page);
274 }
275 return 0;
276}
277
278static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
279{
280 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300281 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300282}
283
Avi Kivity8c438502007-04-16 11:53:17 +0300284static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
285{
286 int r;
287
Avi Kivity22d95b12007-09-14 20:26:06 +0300288 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300289 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
290 pte_chain_cache, 4);
291 if (r)
292 goto out;
293 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
294 rmap_desc_cache, 1);
295 if (r)
296 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200297 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300298 if (r)
299 goto out;
300 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
301 mmu_page_header_cache, 4);
302out:
Avi Kivity8c438502007-04-16 11:53:17 +0300303 return r;
304}
305
Avi Kivity714b93d2007-01-05 16:36:53 -0800306static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
307{
308 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
309 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300310 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300311 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800312}
313
314static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
315 size_t size)
316{
317 void *p;
318
319 BUG_ON(!mc->nobjs);
320 p = mc->objects[--mc->nobjs];
321 memset(p, 0, size);
322 return p;
323}
324
Avi Kivity714b93d2007-01-05 16:36:53 -0800325static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
326{
327 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
328 sizeof(struct kvm_pte_chain));
329}
330
Avi Kivity90cb0522007-07-17 13:04:56 +0300331static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800332{
Avi Kivity90cb0522007-07-17 13:04:56 +0300333 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800334}
335
336static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
337{
338 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
339 sizeof(struct kvm_rmap_desc));
340}
341
Avi Kivity90cb0522007-07-17 13:04:56 +0300342static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800343{
Avi Kivity90cb0522007-07-17 13:04:56 +0300344 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800345}
346
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800347/*
Izik Eidus290fc382007-09-27 14:11:22 +0200348 * Take gfn and return the reverse mapping to it.
349 * Note: gfn must be unaliased before this function get called
350 */
351
352static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
353{
354 struct kvm_memory_slot *slot;
355
356 slot = gfn_to_memslot(kvm, gfn);
357 return &slot->rmap[gfn - slot->base_gfn];
358}
359
360/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800361 * Reverse mapping data structures:
362 *
Izik Eidus290fc382007-09-27 14:11:22 +0200363 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
364 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800365 *
Izik Eidus290fc382007-09-27 14:11:22 +0200366 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
367 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800368 */
Izik Eidus290fc382007-09-27 14:11:22 +0200369static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370{
Avi Kivity4db35312007-11-21 15:28:32 +0200371 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200373 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800374 int i;
375
376 if (!is_rmap_pte(*spte))
377 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200378 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200379 sp = page_header(__pa(spte));
380 sp->gfns[spte - sp->spt] = gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200381 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
382 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800383 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200384 *rmapp = (unsigned long)spte;
385 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800386 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800387 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200388 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800389 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200390 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800391 } else {
392 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200393 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800394 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
395 desc = desc->more;
396 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800397 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800398 desc = desc->more;
399 }
400 for (i = 0; desc->shadow_ptes[i]; ++i)
401 ;
402 desc->shadow_ptes[i] = spte;
403 }
404}
405
Izik Eidus290fc382007-09-27 14:11:22 +0200406static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800407 struct kvm_rmap_desc *desc,
408 int i,
409 struct kvm_rmap_desc *prev_desc)
410{
411 int j;
412
413 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
414 ;
415 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000416 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800417 if (j != 0)
418 return;
419 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200420 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800421 else
422 if (prev_desc)
423 prev_desc->more = desc->more;
424 else
Izik Eidus290fc382007-09-27 14:11:22 +0200425 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300426 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800427}
428
Izik Eidus290fc382007-09-27 14:11:22 +0200429static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800430{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800431 struct kvm_rmap_desc *desc;
432 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200433 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200434 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200435 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800436 int i;
437
438 if (!is_rmap_pte(*spte))
439 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200440 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200441 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200442 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200443 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200444 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200445 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200446 kvm_release_page_clean(page);
Avi Kivity4db35312007-11-21 15:28:32 +0200447 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
Izik Eidus290fc382007-09-27 14:11:22 +0200448 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800449 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
450 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200451 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800452 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200453 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800454 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
455 spte, *spte);
456 BUG();
457 }
Izik Eidus290fc382007-09-27 14:11:22 +0200458 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800459 } else {
460 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200461 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800462 prev_desc = NULL;
463 while (desc) {
464 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
465 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200466 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800467 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800468 prev_desc);
469 return;
470 }
471 prev_desc = desc;
472 desc = desc->more;
473 }
474 BUG();
475 }
476}
477
Izik Eidus98348e92007-10-16 14:42:30 +0200478static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800479{
Avi Kivity374cbac2007-01-05 16:36:43 -0800480 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200481 struct kvm_rmap_desc *prev_desc;
482 u64 *prev_spte;
483 int i;
484
485 if (!*rmapp)
486 return NULL;
487 else if (!(*rmapp & 1)) {
488 if (!spte)
489 return (u64 *)*rmapp;
490 return NULL;
491 }
492 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
493 prev_desc = NULL;
494 prev_spte = NULL;
495 while (desc) {
496 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
497 if (prev_spte == spte)
498 return desc->shadow_ptes[i];
499 prev_spte = desc->shadow_ptes[i];
500 }
501 desc = desc->more;
502 }
503 return NULL;
504}
505
506static void rmap_write_protect(struct kvm *kvm, u64 gfn)
507{
Izik Eidus290fc382007-09-27 14:11:22 +0200508 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800509 u64 *spte;
510
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500511 gfn = unalias_gfn(kvm, gfn);
512 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800513
Izik Eidus98348e92007-10-16 14:42:30 +0200514 spte = rmap_next(kvm, rmapp, NULL);
515 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800516 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800517 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800518 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Izik Eidus9647c142007-10-16 14:43:46 +0200519 if (is_writeble_pte(*spte))
520 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500521 kvm_flush_remote_tlbs(kvm);
Izik Eidus9647c142007-10-16 14:43:46 +0200522 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800523 }
524}
525
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800526#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300527static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528{
Avi Kivity139bdb22007-01-05 16:36:50 -0800529 u64 *pos;
530 u64 *end;
531
Avi Kivity47ad8e62007-05-06 15:50:58 +0300532 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200533 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800534 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
535 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800537 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 return 1;
539}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800540#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541
Avi Kivity4db35312007-11-21 15:28:32 +0200542static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800543{
Avi Kivity4db35312007-11-21 15:28:32 +0200544 ASSERT(is_empty_shadow_page(sp->spt));
545 list_del(&sp->link);
546 __free_page(virt_to_page(sp->spt));
547 __free_page(virt_to_page(sp->gfns));
548 kfree(sp);
Avi Kivity90cb0522007-07-17 13:04:56 +0300549 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800550}
551
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800552static unsigned kvm_page_table_hashfn(gfn_t gfn)
553{
554 return gfn;
555}
556
Avi Kivity25c0de22007-01-05 16:36:42 -0800557static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
558 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559{
Avi Kivity4db35312007-11-21 15:28:32 +0200560 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
Avi Kivityd3d25b02007-05-30 12:34:53 +0300562 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800563 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564
Avi Kivity4db35312007-11-21 15:28:32 +0200565 sp = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache, sizeof *sp);
566 sp->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
567 sp->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
568 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
569 list_add(&sp->link, &vcpu->kvm->active_mmu_pages);
570 ASSERT(is_empty_shadow_page(sp->spt));
571 sp->slot_bitmap = 0;
572 sp->multimapped = 0;
573 sp->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800574 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200575 return sp;
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,
Avi Kivity4db35312007-11-21 15:28:32 +0200579 struct kvm_mmu_page *sp, 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;
Avi Kivity4db35312007-11-21 15:28:32 +0200587 if (!sp->multimapped) {
588 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800589
590 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200591 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800592 return;
593 }
Avi Kivity4db35312007-11-21 15:28:32 +0200594 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800595 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200596 INIT_HLIST_HEAD(&sp->parent_ptes);
597 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800598 pte_chain->parent_ptes[0] = old;
599 }
Avi Kivity4db35312007-11-21 15:28:32 +0200600 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800601 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);
Avi Kivity4db35312007-11-21 15:28:32 +0200611 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800612 pte_chain->parent_ptes[0] = parent_pte;
613}
614
Avi Kivity4db35312007-11-21 15:28:32 +0200615static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
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
Avi Kivity4db35312007-11-21 15:28:32 +0200622 if (!sp->multimapped) {
623 BUG_ON(sp->parent_pte != parent_pte);
624 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800625 return;
626 }
Avi Kivity4db35312007-11-21 15:28:32 +0200627 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800628 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 Kivity4db35312007-11-21 15:28:32 +0200643 if (hlist_empty(&sp->parent_ptes)) {
644 sp->multimapped = 0;
645 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800646 }
647 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800648 return;
649 }
650 BUG();
651}
652
Avi Kivity4db35312007-11-21 15:28:32 +0200653static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800654{
655 unsigned index;
656 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200657 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800658 struct hlist_node *node;
659
660 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
661 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500662 bucket = &kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200663 hlist_for_each_entry(sp, node, bucket, hash_link)
664 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800665 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200666 __FUNCTION__, sp->role.word);
667 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800668 }
669 return NULL;
670}
671
672static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
673 gfn_t gfn,
674 gva_t gaddr,
675 unsigned level,
676 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200677 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800678 u64 *parent_pte)
679{
680 union kvm_mmu_page_role role;
681 unsigned index;
682 unsigned quadrant;
683 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200684 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800685 struct hlist_node *node;
686
687 role.word = 0;
688 role.glevels = vcpu->mmu.root_level;
689 role.level = level;
690 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200691 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800692 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
693 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
694 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
695 role.quadrant = quadrant;
696 }
697 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
698 gfn, role.word);
699 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
700 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200701 hlist_for_each_entry(sp, node, bucket, hash_link)
702 if (sp->gfn == gfn && sp->role.word == role.word) {
703 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200705 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800706 }
Avi Kivity4db35312007-11-21 15:28:32 +0200707 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
708 if (!sp)
709 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800710 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200711 sp->gfn = gfn;
712 sp->role = role;
713 hlist_add_head(&sp->hash_link, bucket);
714 vcpu->mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800715 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500716 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200717 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800718}
719
Avi Kivity90cb0522007-07-17 13:04:56 +0300720static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200721 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800722{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800723 unsigned i;
724 u64 *pt;
725 u64 ent;
726
Avi Kivity4db35312007-11-21 15:28:32 +0200727 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800728
Avi Kivity4db35312007-11-21 15:28:32 +0200729 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800730 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200731 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200732 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200733 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300735 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800736 return;
737 }
738
739 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
740 ent = pt[i];
741
Avi Kivityc7addb92007-09-16 18:58:32 +0200742 pt[i] = shadow_trap_nonpresent_pte;
743 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800744 continue;
745 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300746 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800747 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300748 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800749}
750
Avi Kivity4db35312007-11-21 15:28:32 +0200751static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800752{
Avi Kivity4db35312007-11-21 15:28:32 +0200753 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800754}
755
Avi Kivity12b7d282007-09-23 14:10:49 +0200756static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
757{
758 int i;
759
760 for (i = 0; i < KVM_MAX_VCPUS; ++i)
761 if (kvm->vcpus[i])
762 kvm->vcpus[i]->last_pte_updated = NULL;
763}
764
Avi Kivity4db35312007-11-21 15:28:32 +0200765static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800766{
767 u64 *parent_pte;
768
Avi Kivity4cee5762007-11-18 16:37:07 +0200769 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200770 while (sp->multimapped || sp->parent_pte) {
771 if (!sp->multimapped)
772 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800773 else {
774 struct kvm_pte_chain *chain;
775
Avi Kivity4db35312007-11-21 15:28:32 +0200776 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800777 struct kvm_pte_chain, link);
778 parent_pte = chain->parent_ptes[0];
779 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800780 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200781 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200782 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800783 }
Avi Kivity4db35312007-11-21 15:28:32 +0200784 kvm_mmu_page_unlink_children(kvm, sp);
785 if (!sp->root_count) {
786 hlist_del(&sp->hash_link);
787 kvm_mmu_free_page(kvm, sp);
Avi Kivity36868f72007-03-26 19:31:52 +0200788 } else
Avi Kivity4db35312007-11-21 15:28:32 +0200789 list_move(&sp->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200790 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800791}
792
Izik Eidus82ce2c92007-10-02 18:52:55 +0200793/*
794 * Changing the number of mmu pages allocated to the vm
795 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
796 */
797void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
798{
799 /*
800 * If we set the number of mmu pages to be smaller be than the
801 * number of actived pages , we must to free some mmu pages before we
802 * change the value
803 */
804
805 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
806 kvm_nr_mmu_pages) {
807 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
808 - kvm->n_free_mmu_pages;
809
810 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
811 struct kvm_mmu_page *page;
812
813 page = container_of(kvm->active_mmu_pages.prev,
814 struct kvm_mmu_page, link);
815 kvm_mmu_zap_page(kvm, page);
816 n_used_mmu_pages--;
817 }
818 kvm->n_free_mmu_pages = 0;
819 }
820 else
821 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
822 - kvm->n_alloc_mmu_pages;
823
824 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
825}
826
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500827static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800828{
829 unsigned index;
830 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200831 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800832 struct hlist_node *node, *n;
833 int r;
834
835 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
836 r = 0;
837 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500838 bucket = &kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200839 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
840 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800841 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200842 sp->role.word);
843 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800844 r = 1;
845 }
846 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800847}
848
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500849static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300850{
Avi Kivity4db35312007-11-21 15:28:32 +0200851 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300852
Avi Kivity4db35312007-11-21 15:28:32 +0200853 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
854 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
855 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300856 }
857}
858
Avi Kivity38c335f2007-11-21 14:20:22 +0200859static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860{
Avi Kivity38c335f2007-11-21 14:20:22 +0200861 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200862 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
Avi Kivity4db35312007-11-21 15:28:32 +0200864 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865}
866
Avi Kivity039576c2007-03-20 12:46:50 +0200867struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
868{
869 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
870
871 if (gpa == UNMAPPED_GVA)
872 return NULL;
Avi Kivity1d28f5f2007-11-21 15:01:44 +0200873 return gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200874}
875
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
877{
878}
879
Avi Kivity3f3e7122007-11-21 14:54:16 +0200880static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881{
882 int level = PT32E_ROOT_LEVEL;
883 hpa_t table_addr = vcpu->mmu.root_hpa;
884
885 for (; ; level--) {
886 u32 index = PT64_INDEX(v, level);
887 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800888 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889
890 ASSERT(VALID_PAGE(table_addr));
891 table = __va(table_addr);
892
893 if (level == 1) {
Izik Eidus9647c142007-10-16 14:43:46 +0200894 int was_rmapped;
895
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800896 pte = table[index];
Izik Eidus9647c142007-10-16 14:43:46 +0200897 was_rmapped = is_rmap_pte(pte);
Izik Eidus2065b372007-11-20 11:30:04 +0200898 if (is_shadow_present_pte(pte) && is_writeble_pte(pte)) {
Izik Eidusb4231d62007-11-20 11:49:33 +0200899 kvm_release_page_clean(page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800900 return 0;
Izik Eidus2065b372007-11-20 11:30:04 +0200901 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
Avi Kivity38c335f2007-11-21 14:20:22 +0200903 page_header_update_slot(vcpu->kvm, table,
904 v >> PAGE_SHIFT);
Avi Kivity3f3e7122007-11-21 14:54:16 +0200905 table[index] = page_to_phys(page)
906 | PT_PRESENT_MASK | PT_WRITABLE_MASK
907 | PT_USER_MASK;
Izik Eidus9647c142007-10-16 14:43:46 +0200908 if (!was_rmapped)
909 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200910 else
Izik Eidusb4231d62007-11-20 11:49:33 +0200911 kvm_release_page_clean(page);
912
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 return 0;
914 }
915
Avi Kivityc7addb92007-09-16 18:58:32 +0200916 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800917 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800918 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800920 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
921 >> PAGE_SHIFT;
922 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
923 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200924 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800925 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 pgprintk("nonpaging_map: ENOMEM\n");
Izik Eidusb4231d62007-11-20 11:49:33 +0200927 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 return -ENOMEM;
929 }
930
Avi Kivity47ad8e62007-05-06 15:50:58 +0300931 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800932 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 }
934 table_addr = table[index] & PT64_BASE_ADDR_MASK;
935 }
936}
937
Avi Kivityc7addb92007-09-16 18:58:32 +0200938static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
939 struct kvm_mmu_page *sp)
940{
941 int i;
942
943 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
944 sp->spt[i] = shadow_trap_nonpresent_pte;
945}
946
Avi Kivity17ac10a2007-01-05 16:36:40 -0800947static void mmu_free_roots(struct kvm_vcpu *vcpu)
948{
949 int i;
Avi Kivity4db35312007-11-21 15:28:32 +0200950 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800951
Avi Kivity7b53aa52007-06-05 12:17:03 +0300952 if (!VALID_PAGE(vcpu->mmu.root_hpa))
953 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800954#ifdef CONFIG_X86_64
955 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
956 hpa_t root = vcpu->mmu.root_hpa;
957
Avi Kivity4db35312007-11-21 15:28:32 +0200958 sp = page_header(root);
959 --sp->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800960 vcpu->mmu.root_hpa = INVALID_PAGE;
961 return;
962 }
963#endif
964 for (i = 0; i < 4; ++i) {
965 hpa_t root = vcpu->mmu.pae_root[i];
966
Avi Kivity417726a2007-04-12 17:35:58 +0300967 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300968 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +0200969 sp = page_header(root);
970 --sp->root_count;
Avi Kivity417726a2007-04-12 17:35:58 +0300971 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800972 vcpu->mmu.pae_root[i] = INVALID_PAGE;
973 }
974 vcpu->mmu.root_hpa = INVALID_PAGE;
975}
976
977static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
978{
979 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800980 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +0200981 struct kvm_mmu_page *sp;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800982
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800983 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800984
985#ifdef CONFIG_X86_64
986 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
987 hpa_t root = vcpu->mmu.root_hpa;
988
989 ASSERT(!VALID_PAGE(root));
Avi Kivity4db35312007-11-21 15:28:32 +0200990 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
991 PT64_ROOT_LEVEL, 0, 0, NULL);
992 root = __pa(sp->spt);
993 ++sp->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800994 vcpu->mmu.root_hpa = root;
995 return;
996 }
997#endif
998 for (i = 0; i < 4; ++i) {
999 hpa_t root = vcpu->mmu.pae_root[i];
1000
1001 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001002 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1003 if (!is_present_pte(vcpu->pdptrs[i])) {
1004 vcpu->mmu.pae_root[i] = 0;
1005 continue;
1006 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001008 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001009 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001010 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1011 PT32_ROOT_LEVEL, !is_paging(vcpu),
1012 0, NULL);
1013 root = __pa(sp->spt);
1014 ++sp->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001015 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1016 }
1017 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1018}
1019
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1021{
1022 return vaddr;
1023}
1024
1025static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001026 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027{
Avi Kivity3f3e7122007-11-21 14:54:16 +02001028 struct page *page;
Avi Kivitye2dec932007-01-05 16:36:54 -08001029 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
Avi Kivitye2dec932007-01-05 16:36:54 -08001031 r = mmu_topup_memory_caches(vcpu);
1032 if (r)
1033 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001034
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 ASSERT(vcpu);
1036 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1037
Avi Kivity3f3e7122007-11-21 14:54:16 +02001038 page = gfn_to_page(vcpu->kvm, gva >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039
Avi Kivity3f3e7122007-11-21 14:54:16 +02001040 if (is_error_page(page)) {
1041 kvm_release_page_clean(page);
Avi Kivityebeace82007-01-05 16:36:47 -08001042 return 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001043 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044
Avi Kivity3f3e7122007-11-21 14:54:16 +02001045 return nonpaging_map(vcpu, gva & PAGE_MASK, page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046}
1047
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048static void nonpaging_free(struct kvm_vcpu *vcpu)
1049{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001050 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051}
1052
1053static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1054{
1055 struct kvm_mmu *context = &vcpu->mmu;
1056
1057 context->new_cr3 = nonpaging_new_cr3;
1058 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 context->gva_to_gpa = nonpaging_gva_to_gpa;
1060 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001061 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001062 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001064 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 return 0;
1066}
1067
Avi Kivityd835dfe2007-11-21 02:57:59 +02001068void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001070 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001071 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072}
1073
1074static void paging_new_cr3(struct kvm_vcpu *vcpu)
1075{
Avi Kivity374cbac2007-01-05 16:36:43 -08001076 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001077 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078}
1079
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080static void inject_page_fault(struct kvm_vcpu *vcpu,
1081 u64 addr,
1082 u32 err_code)
1083{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001084 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085}
1086
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087static void paging_free(struct kvm_vcpu *vcpu)
1088{
1089 nonpaging_free(vcpu);
1090}
1091
1092#define PTTYPE 64
1093#include "paging_tmpl.h"
1094#undef PTTYPE
1095
1096#define PTTYPE 32
1097#include "paging_tmpl.h"
1098#undef PTTYPE
1099
Avi Kivity17ac10a2007-01-05 16:36:40 -08001100static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101{
1102 struct kvm_mmu *context = &vcpu->mmu;
1103
1104 ASSERT(is_pae(vcpu));
1105 context->new_cr3 = paging_new_cr3;
1106 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001108 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001110 context->root_level = level;
1111 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001112 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 return 0;
1114}
1115
Avi Kivity17ac10a2007-01-05 16:36:40 -08001116static int paging64_init_context(struct kvm_vcpu *vcpu)
1117{
1118 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1119}
1120
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121static int paging32_init_context(struct kvm_vcpu *vcpu)
1122{
1123 struct kvm_mmu *context = &vcpu->mmu;
1124
1125 context->new_cr3 = paging_new_cr3;
1126 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 context->gva_to_gpa = paging32_gva_to_gpa;
1128 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001129 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 context->root_level = PT32_ROOT_LEVEL;
1131 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001132 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 return 0;
1134}
1135
1136static int paging32E_init_context(struct kvm_vcpu *vcpu)
1137{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001138 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139}
1140
1141static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1142{
1143 ASSERT(vcpu);
1144 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1145
1146 if (!is_paging(vcpu))
1147 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001148 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 return paging64_init_context(vcpu);
1150 else if (is_pae(vcpu))
1151 return paging32E_init_context(vcpu);
1152 else
1153 return paging32_init_context(vcpu);
1154}
1155
1156static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1157{
1158 ASSERT(vcpu);
1159 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1160 vcpu->mmu.free(vcpu);
1161 vcpu->mmu.root_hpa = INVALID_PAGE;
1162 }
1163}
1164
1165int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1166{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001167 destroy_kvm_mmu(vcpu);
1168 return init_kvm_mmu(vcpu);
1169}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001170EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001171
1172int kvm_mmu_load(struct kvm_vcpu *vcpu)
1173{
Avi Kivity714b93d2007-01-05 16:36:53 -08001174 int r;
1175
Shaohua Li11ec2802007-07-23 14:51:37 +08001176 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001177 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001178 if (r)
1179 goto out;
1180 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001181 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001182 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001183out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001184 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001185 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001187EXPORT_SYMBOL_GPL(kvm_mmu_load);
1188
1189void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1190{
1191 mmu_free_roots(vcpu);
1192}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193
Avi Kivity09072da2007-05-01 14:16:52 +03001194static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001195 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001196 u64 *spte)
1197{
1198 u64 pte;
1199 struct kvm_mmu_page *child;
1200
1201 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001202 if (is_shadow_present_pte(pte)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001203 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001204 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001205 else {
1206 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001207 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001208 }
1209 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001210 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001211}
1212
Avi Kivity00284252007-05-01 16:53:31 +03001213static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001214 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001215 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001216 const void *new, int bytes,
1217 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001218{
Avi Kivity4db35312007-11-21 15:28:32 +02001219 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001220 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001221 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001222 }
Avi Kivity00284252007-05-01 16:53:31 +03001223
Avi Kivity4cee5762007-11-18 16:37:07 +02001224 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001225 if (sp->role.glevels == PT32_ROOT_LEVEL)
1226 paging32_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001227 else
Avi Kivity4db35312007-11-21 15:28:32 +02001228 paging64_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001229}
1230
Avi Kivity79539ce2007-11-21 02:06:21 +02001231static bool need_remote_flush(u64 old, u64 new)
1232{
1233 if (!is_shadow_present_pte(old))
1234 return false;
1235 if (!is_shadow_present_pte(new))
1236 return true;
1237 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1238 return true;
1239 old ^= PT64_NX_MASK;
1240 new ^= PT64_NX_MASK;
1241 return (old & ~new & PT64_PERM_MASK) != 0;
1242}
1243
1244static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1245{
1246 if (need_remote_flush(old, new))
1247 kvm_flush_remote_tlbs(vcpu->kvm);
1248 else
1249 kvm_mmu_flush_tlb(vcpu);
1250}
1251
Avi Kivity12b7d282007-09-23 14:10:49 +02001252static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1253{
1254 u64 *spte = vcpu->last_pte_updated;
1255
1256 return !!(spte && (*spte & PT_ACCESSED_MASK));
1257}
1258
Avi Kivity09072da2007-05-01 14:16:52 +03001259void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life5518812007-07-23 14:51:39 +08001260 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001261{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001262 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001263 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001264 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001265 struct hlist_head *bucket;
1266 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001267 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001268 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001269 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001270 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001271 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001272 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001273 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001274 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001275 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001276 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001277
Avi Kivityda4a00f2007-01-05 16:36:44 -08001278 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity4cee5762007-11-18 16:37:07 +02001279 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001280 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001281 if (gfn == vcpu->last_pt_write_gfn
1282 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001283 ++vcpu->last_pt_write_count;
1284 if (vcpu->last_pt_write_count >= 3)
1285 flooded = 1;
1286 } else {
1287 vcpu->last_pt_write_gfn = gfn;
1288 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001289 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001290 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001291 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1292 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001293 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1294 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001295 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001296 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001297 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001298 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001299 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001300 /*
1301 * Misaligned accesses are too much trouble to fix
1302 * up; also, they usually indicate a page is not used
1303 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001304 *
1305 * If we're seeing too many writes to a page,
1306 * it may no longer be a page table, or we may be
1307 * forking, in which case it is better to unmap the
1308 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001309 */
1310 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001311 gpa, bytes, sp->role.word);
1312 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001313 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001314 continue;
1315 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001316 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001317 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001318 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001319 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001320 page_offset <<= 1; /* 32->64 */
1321 /*
1322 * A 32-bit pde maps 4MB while the shadow pdes map
1323 * only 2MB. So we need to double the offset again
1324 * and zap two pdes instead of one.
1325 */
1326 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001327 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001328 page_offset <<= 1;
1329 npte = 2;
1330 }
Avi Kivityfce06572007-05-01 16:44:05 +03001331 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001332 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001333 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001334 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001335 }
Avi Kivity4db35312007-11-21 15:28:32 +02001336 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001337 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001338 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001339 mmu_pte_write_zap_pte(vcpu, sp, spte);
1340 mmu_pte_write_new_pte(vcpu, sp, spte, new, bytes,
Avi Kivityc7addb92007-09-16 18:58:32 +02001341 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001342 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001343 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001344 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001345 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001346 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001347}
1348
Avi Kivitya4360362007-01-05 16:36:45 -08001349int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1350{
1351 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1352
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001353 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001354}
1355
Avi Kivity22d95b12007-09-14 20:26:06 +03001356void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001357{
1358 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001359 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001360
Avi Kivity4db35312007-11-21 15:28:32 +02001361 sp = container_of(vcpu->kvm->active_mmu_pages.prev,
1362 struct kvm_mmu_page, link);
1363 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001364 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001365 }
1366}
Avi Kivityebeace82007-01-05 16:36:47 -08001367
Avi Kivity30677142007-10-28 18:48:59 +02001368int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1369{
1370 int r;
1371 enum emulation_result er;
1372
1373 mutex_lock(&vcpu->kvm->lock);
1374 r = vcpu->mmu.page_fault(vcpu, cr2, error_code);
1375 if (r < 0)
1376 goto out;
1377
1378 if (!r) {
1379 r = 1;
1380 goto out;
1381 }
1382
Avi Kivityb733bfb2007-10-28 18:52:05 +02001383 r = mmu_topup_memory_caches(vcpu);
1384 if (r)
1385 goto out;
1386
Avi Kivity30677142007-10-28 18:48:59 +02001387 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1388 mutex_unlock(&vcpu->kvm->lock);
1389
1390 switch (er) {
1391 case EMULATE_DONE:
1392 return 1;
1393 case EMULATE_DO_MMIO:
1394 ++vcpu->stat.mmio_exits;
1395 return 0;
1396 case EMULATE_FAIL:
1397 kvm_report_emulation_failure(vcpu, "pagetable");
1398 return 1;
1399 default:
1400 BUG();
1401 }
1402out:
1403 mutex_unlock(&vcpu->kvm->lock);
1404 return r;
1405}
1406EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1407
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408static void free_mmu_pages(struct kvm_vcpu *vcpu)
1409{
Avi Kivity4db35312007-11-21 15:28:32 +02001410 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
Avi Kivityf51234c2007-01-05 16:36:52 -08001412 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001413 sp = container_of(vcpu->kvm->active_mmu_pages.next,
1414 struct kvm_mmu_page, link);
1415 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001416 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001417 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418}
1419
1420static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1421{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001422 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423 int i;
1424
1425 ASSERT(vcpu);
1426
Izik Eidus82ce2c92007-10-02 18:52:55 +02001427 if (vcpu->kvm->n_requested_mmu_pages)
1428 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1429 else
1430 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001431 /*
1432 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1433 * Therefore we need to allocate shadow page tables in the first
1434 * 4GB of memory, which happens to fit the DMA32 zone.
1435 */
1436 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1437 if (!page)
1438 goto error_1;
1439 vcpu->mmu.pae_root = page_address(page);
1440 for (i = 0; i < 4; ++i)
1441 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1442
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443 return 0;
1444
1445error_1:
1446 free_mmu_pages(vcpu);
1447 return -ENOMEM;
1448}
1449
Ingo Molnar8018c272006-12-29 16:50:01 -08001450int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452 ASSERT(vcpu);
1453 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Ingo Molnar8018c272006-12-29 16:50:01 -08001455 return alloc_mmu_pages(vcpu);
1456}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457
Ingo Molnar8018c272006-12-29 16:50:01 -08001458int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1459{
1460 ASSERT(vcpu);
1461 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001462
Ingo Molnar8018c272006-12-29 16:50:01 -08001463 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464}
1465
1466void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1467{
1468 ASSERT(vcpu);
1469
1470 destroy_kvm_mmu(vcpu);
1471 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001472 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473}
1474
Avi Kivity90cb0522007-07-17 13:04:56 +03001475void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476{
Avi Kivity4db35312007-11-21 15:28:32 +02001477 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478
Avi Kivity4db35312007-11-21 15:28:32 +02001479 list_for_each_entry(sp, &kvm->active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 int i;
1481 u64 *pt;
1482
Avi Kivity4db35312007-11-21 15:28:32 +02001483 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 continue;
1485
Avi Kivity4db35312007-11-21 15:28:32 +02001486 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1488 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001489 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 }
1492}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001493
Avi Kivity90cb0522007-07-17 13:04:56 +03001494void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001495{
Avi Kivity4db35312007-11-21 15:28:32 +02001496 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001497
Avi Kivity4db35312007-11-21 15:28:32 +02001498 list_for_each_entry_safe(sp, node, &kvm->active_mmu_pages, link)
1499 kvm_mmu_zap_page(kvm, sp);
Dor Laore0fa8262007-03-30 13:06:33 +03001500
Avi Kivity90cb0522007-07-17 13:04:56 +03001501 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001502}
1503
Avi Kivityb5a33a72007-04-15 16:31:09 +03001504void kvm_mmu_module_exit(void)
1505{
1506 if (pte_chain_cache)
1507 kmem_cache_destroy(pte_chain_cache);
1508 if (rmap_desc_cache)
1509 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001510 if (mmu_page_header_cache)
1511 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001512}
1513
1514int kvm_mmu_module_init(void)
1515{
1516 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1517 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001518 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001519 if (!pte_chain_cache)
1520 goto nomem;
1521 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1522 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001523 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001524 if (!rmap_desc_cache)
1525 goto nomem;
1526
Avi Kivityd3d25b02007-05-30 12:34:53 +03001527 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1528 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001529 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001530 if (!mmu_page_header_cache)
1531 goto nomem;
1532
Avi Kivityb5a33a72007-04-15 16:31:09 +03001533 return 0;
1534
1535nomem:
1536 kvm_mmu_module_exit();
1537 return -ENOMEM;
1538}
1539
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001540/*
1541 * Caculate mmu pages needed for kvm.
1542 */
1543unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1544{
1545 int i;
1546 unsigned int nr_mmu_pages;
1547 unsigned int nr_pages = 0;
1548
1549 for (i = 0; i < kvm->nmemslots; i++)
1550 nr_pages += kvm->memslots[i].npages;
1551
1552 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1553 nr_mmu_pages = max(nr_mmu_pages,
1554 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1555
1556 return nr_mmu_pages;
1557}
1558
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001559#ifdef AUDIT
1560
1561static const char *audit_msg;
1562
1563static gva_t canonicalize(gva_t gva)
1564{
1565#ifdef CONFIG_X86_64
1566 gva = (long long)(gva << 16) >> 16;
1567#endif
1568 return gva;
1569}
1570
1571static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1572 gva_t va, int level)
1573{
1574 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1575 int i;
1576 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1577
1578 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1579 u64 ent = pt[i];
1580
Avi Kivityc7addb92007-09-16 18:58:32 +02001581 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001582 continue;
1583
1584 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001585 if (level > 1) {
1586 if (ent == shadow_notrap_nonpresent_pte)
1587 printk(KERN_ERR "audit: (%s) nontrapping pte"
1588 " in nonleaf level: levels %d gva %lx"
1589 " level %d pte %llx\n", audit_msg,
1590 vcpu->mmu.root_level, va, level, ent);
1591
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001592 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001593 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001594 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001595 struct page *page = gpa_to_page(vcpu, gpa);
1596 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001597
Avi Kivityc7addb92007-09-16 18:58:32 +02001598 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001599 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001600 printk(KERN_ERR "xx audit error: (%s) levels %d"
1601 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001602 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001603 va, gpa, hpa, ent,
1604 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001605 else if (ent == shadow_notrap_nonpresent_pte
1606 && !is_error_hpa(hpa))
1607 printk(KERN_ERR "audit: (%s) notrap shadow,"
1608 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001609 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001610
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001611 }
1612 }
1613}
1614
1615static void audit_mappings(struct kvm_vcpu *vcpu)
1616{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001617 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001618
1619 if (vcpu->mmu.root_level == 4)
1620 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1621 else
1622 for (i = 0; i < 4; ++i)
1623 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1624 audit_mappings_page(vcpu,
1625 vcpu->mmu.pae_root[i],
1626 i << 30,
1627 2);
1628}
1629
1630static int count_rmaps(struct kvm_vcpu *vcpu)
1631{
1632 int nmaps = 0;
1633 int i, j, k;
1634
1635 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1636 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1637 struct kvm_rmap_desc *d;
1638
1639 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001640 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001641
Izik Eidus290fc382007-09-27 14:11:22 +02001642 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001643 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001644 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001645 ++nmaps;
1646 continue;
1647 }
Izik Eidus290fc382007-09-27 14:11:22 +02001648 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001649 while (d) {
1650 for (k = 0; k < RMAP_EXT; ++k)
1651 if (d->shadow_ptes[k])
1652 ++nmaps;
1653 else
1654 break;
1655 d = d->more;
1656 }
1657 }
1658 }
1659 return nmaps;
1660}
1661
1662static int count_writable_mappings(struct kvm_vcpu *vcpu)
1663{
1664 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001665 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001666 int i;
1667
Avi Kivity4db35312007-11-21 15:28:32 +02001668 list_for_each_entry(sp, &vcpu->kvm->active_mmu_pages, link) {
1669 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001670
Avi Kivity4db35312007-11-21 15:28:32 +02001671 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001672 continue;
1673
1674 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1675 u64 ent = pt[i];
1676
1677 if (!(ent & PT_PRESENT_MASK))
1678 continue;
1679 if (!(ent & PT_WRITABLE_MASK))
1680 continue;
1681 ++nmaps;
1682 }
1683 }
1684 return nmaps;
1685}
1686
1687static void audit_rmap(struct kvm_vcpu *vcpu)
1688{
1689 int n_rmap = count_rmaps(vcpu);
1690 int n_actual = count_writable_mappings(vcpu);
1691
1692 if (n_rmap != n_actual)
1693 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1694 __FUNCTION__, audit_msg, n_rmap, n_actual);
1695}
1696
1697static void audit_write_protection(struct kvm_vcpu *vcpu)
1698{
Avi Kivity4db35312007-11-21 15:28:32 +02001699 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001700 struct kvm_memory_slot *slot;
1701 unsigned long *rmapp;
1702 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001703
Avi Kivity4db35312007-11-21 15:28:32 +02001704 list_for_each_entry(sp, &vcpu->kvm->active_mmu_pages, link) {
1705 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001706 continue;
1707
Avi Kivity4db35312007-11-21 15:28:32 +02001708 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1709 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02001710 rmapp = &slot->rmap[gfn - slot->base_gfn];
1711 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001712 printk(KERN_ERR "%s: (%s) shadow page has writable"
1713 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001714 __FUNCTION__, audit_msg, sp->gfn,
1715 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001716 }
1717}
1718
1719static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1720{
1721 int olddbg = dbg;
1722
1723 dbg = 0;
1724 audit_msg = msg;
1725 audit_rmap(vcpu);
1726 audit_write_protection(vcpu);
1727 audit_mappings(vcpu);
1728 dbg = olddbg;
1729}
1730
1731#endif