blob: d046ba8077631b2cee91badfc4d19eac2a8a9fd2 [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"
22
Avi Kivitye4956062007-06-28 14:15:57 -040023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <linux/module.h>
28
29#include <asm/page.h>
30#include <asm/cmpxchg.h>
31
Avi Kivity37a7d8b2007-01-05 16:36:56 -080032#undef MMU_DEBUG
33
34#undef AUDIT
35
36#ifdef AUDIT
37static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
38#else
39static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
40#endif
41
42#ifdef MMU_DEBUG
43
44#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
45#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
46
47#else
48
49#define pgprintk(x...) do { } while (0)
50#define rmap_printk(x...) do { } while (0)
51
52#endif
53
54#if defined(MMU_DEBUG) || defined(AUDIT)
55static int dbg = 1;
56#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080058#ifndef MMU_DEBUG
59#define ASSERT(x) do { } while (0)
60#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080061#define ASSERT(x) \
62 if (!(x)) { \
63 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
64 __FILE__, __LINE__, #x); \
65 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080066#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
Avi Kivitycea0f0e2007-01-05 16:36:43 -080068#define PT64_PT_BITS 9
69#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
70#define PT32_PT_BITS 10
71#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
73#define PT_WRITABLE_SHIFT 1
74
75#define PT_PRESENT_MASK (1ULL << 0)
76#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
77#define PT_USER_MASK (1ULL << 2)
78#define PT_PWT_MASK (1ULL << 3)
79#define PT_PCD_MASK (1ULL << 4)
80#define PT_ACCESSED_MASK (1ULL << 5)
81#define PT_DIRTY_MASK (1ULL << 6)
82#define PT_PAGE_SIZE_MASK (1ULL << 7)
83#define PT_PAT_MASK (1ULL << 7)
84#define PT_GLOBAL_MASK (1ULL << 8)
85#define PT64_NX_MASK (1ULL << 63)
86
87#define PT_PAT_SHIFT 7
88#define PT_DIR_PAT_SHIFT 12
89#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
90
91#define PT32_DIR_PSE36_SIZE 4
92#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040093#define PT32_DIR_PSE36_MASK \
94 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
96
Avi Kivity6aa8b732006-12-10 02:21:36 -080097#define PT_FIRST_AVAIL_BITS_SHIFT 9
98#define PT64_SECOND_AVAIL_BITS_SHIFT 52
99
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
101
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102#define VALID_PAGE(x) ((x) != INVALID_PAGE)
103
104#define PT64_LEVEL_BITS 9
105
106#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400107 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108
109#define PT64_LEVEL_MASK(level) \
110 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
111
112#define PT64_INDEX(address, level)\
113 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
114
115
116#define PT32_LEVEL_BITS 10
117
118#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400119 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120
121#define PT32_LEVEL_MASK(level) \
122 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
123
124#define PT32_INDEX(address, level)\
125 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
126
127
Avi Kivity27aba762007-03-09 13:04:31 +0200128#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129#define PT64_DIR_BASE_ADDR_MASK \
130 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
131
132#define PT32_BASE_ADDR_MASK PAGE_MASK
133#define PT32_DIR_BASE_ADDR_MASK \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
135
136
137#define PFERR_PRESENT_MASK (1U << 0)
138#define PFERR_WRITE_MASK (1U << 1)
139#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800140#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141
142#define PT64_ROOT_LEVEL 4
143#define PT32_ROOT_LEVEL 2
144#define PT32E_ROOT_LEVEL 3
145
146#define PT_DIRECTORY_LEVEL 2
147#define PT_PAGE_TABLE_LEVEL 1
148
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800149#define RMAP_EXT 4
150
151struct kvm_rmap_desc {
152 u64 *shadow_ptes[RMAP_EXT];
153 struct kvm_rmap_desc *more;
154};
155
Avi Kivityb5a33a72007-04-15 16:31:09 +0300156static struct kmem_cache *pte_chain_cache;
157static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300158static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300159
Avi Kivityc7addb92007-09-16 18:58:32 +0200160static u64 __read_mostly shadow_trap_nonpresent_pte;
161static u64 __read_mostly shadow_notrap_nonpresent_pte;
162
163void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
164{
165 shadow_trap_nonpresent_pte = trap_pte;
166 shadow_notrap_nonpresent_pte = notrap_pte;
167}
168EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
169
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static int is_write_protection(struct kvm_vcpu *vcpu)
171{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000172 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173}
174
175static int is_cpuid_PSE36(void)
176{
177 return 1;
178}
179
Avi Kivity73b10872007-01-26 00:56:41 -0800180static int is_nx(struct kvm_vcpu *vcpu)
181{
182 return vcpu->shadow_efer & EFER_NX;
183}
184
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static int is_present_pte(unsigned long pte)
186{
187 return pte & PT_PRESENT_MASK;
188}
189
Avi Kivityc7addb92007-09-16 18:58:32 +0200190static int is_shadow_present_pte(u64 pte)
191{
192 pte &= ~PT_SHADOW_IO_MARK;
193 return pte != shadow_trap_nonpresent_pte
194 && pte != shadow_notrap_nonpresent_pte;
195}
196
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197static int is_writeble_pte(unsigned long pte)
198{
199 return pte & PT_WRITABLE_MASK;
200}
201
202static int is_io_pte(unsigned long pte)
203{
204 return pte & PT_SHADOW_IO_MARK;
205}
206
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800207static int is_rmap_pte(u64 pte)
208{
209 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
210 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
211}
212
Avi Kivitye663ee62007-05-31 15:46:04 +0300213static void set_shadow_pte(u64 *sptep, u64 spte)
214{
215#ifdef CONFIG_X86_64
216 set_64bit((unsigned long *)sptep, spte);
217#else
218 set_64bit((unsigned long long *)sptep, spte);
219#endif
220}
221
Avi Kivitye2dec932007-01-05 16:36:54 -0800222static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300223 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800224{
225 void *obj;
226
227 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800228 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800229 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300230 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800231 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800232 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800233 cache->objects[cache->nobjs++] = obj;
234 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800235 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800236}
237
238static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
239{
240 while (mc->nobjs)
241 kfree(mc->objects[--mc->nobjs]);
242}
243
Avi Kivityc1158e62007-07-20 08:18:27 +0300244static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300245 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300246{
247 struct page *page;
248
249 if (cache->nobjs >= min)
250 return 0;
251 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300252 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300253 if (!page)
254 return -ENOMEM;
255 set_page_private(page, 0);
256 cache->objects[cache->nobjs++] = page_address(page);
257 }
258 return 0;
259}
260
261static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
262{
263 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300264 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300265}
266
Avi Kivity8c438502007-04-16 11:53:17 +0300267static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
268{
269 int r;
270
Avi Kivity22d95b12007-09-14 20:26:06 +0300271 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300272 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
273 pte_chain_cache, 4);
274 if (r)
275 goto out;
276 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
277 rmap_desc_cache, 1);
278 if (r)
279 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200280 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300281 if (r)
282 goto out;
283 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
284 mmu_page_header_cache, 4);
285out:
Avi Kivity8c438502007-04-16 11:53:17 +0300286 return r;
287}
288
Avi Kivity714b93d2007-01-05 16:36:53 -0800289static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
290{
291 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
292 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300293 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300294 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800295}
296
297static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
298 size_t size)
299{
300 void *p;
301
302 BUG_ON(!mc->nobjs);
303 p = mc->objects[--mc->nobjs];
304 memset(p, 0, size);
305 return p;
306}
307
Avi Kivity714b93d2007-01-05 16:36:53 -0800308static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
309{
310 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
311 sizeof(struct kvm_pte_chain));
312}
313
Avi Kivity90cb0522007-07-17 13:04:56 +0300314static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800315{
Avi Kivity90cb0522007-07-17 13:04:56 +0300316 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800317}
318
319static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
320{
321 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
322 sizeof(struct kvm_rmap_desc));
323}
324
Avi Kivity90cb0522007-07-17 13:04:56 +0300325static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800326{
Avi Kivity90cb0522007-07-17 13:04:56 +0300327 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800328}
329
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800330/*
Izik Eidus290fc382007-09-27 14:11:22 +0200331 * Take gfn and return the reverse mapping to it.
332 * Note: gfn must be unaliased before this function get called
333 */
334
335static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
336{
337 struct kvm_memory_slot *slot;
338
339 slot = gfn_to_memslot(kvm, gfn);
340 return &slot->rmap[gfn - slot->base_gfn];
341}
342
343/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800344 * Reverse mapping data structures:
345 *
Izik Eidus290fc382007-09-27 14:11:22 +0200346 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
347 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800348 *
Izik Eidus290fc382007-09-27 14:11:22 +0200349 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
350 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800351 */
Izik Eidus290fc382007-09-27 14:11:22 +0200352static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800353{
Izik Eidus290fc382007-09-27 14:11:22 +0200354 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800355 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200356 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800357 int i;
358
359 if (!is_rmap_pte(*spte))
360 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200361 gfn = unalias_gfn(vcpu->kvm, gfn);
362 page = page_header(__pa(spte));
363 page->gfns[spte - page->spt] = gfn;
364 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
365 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800366 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200367 *rmapp = (unsigned long)spte;
368 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800369 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800370 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200371 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200373 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800374 } else {
375 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200376 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800377 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
378 desc = desc->more;
379 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800380 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800381 desc = desc->more;
382 }
383 for (i = 0; desc->shadow_ptes[i]; ++i)
384 ;
385 desc->shadow_ptes[i] = spte;
386 }
387}
388
Izik Eidus290fc382007-09-27 14:11:22 +0200389static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800390 struct kvm_rmap_desc *desc,
391 int i,
392 struct kvm_rmap_desc *prev_desc)
393{
394 int j;
395
396 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
397 ;
398 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000399 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800400 if (j != 0)
401 return;
402 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200403 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800404 else
405 if (prev_desc)
406 prev_desc->more = desc->more;
407 else
Izik Eidus290fc382007-09-27 14:11:22 +0200408 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300409 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800410}
411
Izik Eidus290fc382007-09-27 14:11:22 +0200412static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800413{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800414 struct kvm_rmap_desc *desc;
415 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200416 struct kvm_mmu_page *page;
417 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800418 int i;
419
420 if (!is_rmap_pte(*spte))
421 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200422 page = page_header(__pa(spte));
423 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
424 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800425 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
426 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200427 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800428 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200429 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800430 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
431 spte, *spte);
432 BUG();
433 }
Izik Eidus290fc382007-09-27 14:11:22 +0200434 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800435 } else {
436 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200437 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800438 prev_desc = NULL;
439 while (desc) {
440 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
441 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200442 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800443 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800444 prev_desc);
445 return;
446 }
447 prev_desc = desc;
448 desc = desc->more;
449 }
450 BUG();
451 }
452}
453
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500454static void rmap_write_protect(struct kvm *kvm, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800455{
Avi Kivity374cbac2007-01-05 16:36:43 -0800456 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200457 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800458 u64 *spte;
459
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500460 gfn = unalias_gfn(kvm, gfn);
461 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800462
Izik Eidus290fc382007-09-27 14:11:22 +0200463 while (*rmapp) {
464 if (!(*rmapp & 1))
465 spte = (u64 *)*rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800466 else {
Izik Eidus290fc382007-09-27 14:11:22 +0200467 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800468 spte = desc->shadow_ptes[0];
469 }
470 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800471 BUG_ON(!(*spte & PT_PRESENT_MASK));
472 BUG_ON(!(*spte & PT_WRITABLE_MASK));
473 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500474 rmap_remove(kvm, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300475 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500476 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800477 }
478}
479
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800480#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300481static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482{
Avi Kivity139bdb22007-01-05 16:36:50 -0800483 u64 *pos;
484 u64 *end;
485
Avi Kivity47ad8e62007-05-06 15:50:58 +0300486 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200487 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800488 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
489 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800491 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492 return 1;
493}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800494#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495
Avi Kivity90cb0522007-07-17 13:04:56 +0300496static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300497 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800498{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300499 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300500 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300501 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200502 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300503 kfree(page_head);
504 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800505}
506
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800507static unsigned kvm_page_table_hashfn(gfn_t gfn)
508{
509 return gfn;
510}
511
Avi Kivity25c0de22007-01-05 16:36:42 -0800512static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
513 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514{
515 struct kvm_mmu_page *page;
516
Avi Kivityd3d25b02007-05-30 12:34:53 +0300517 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800518 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519
Avi Kivityd3d25b02007-05-30 12:34:53 +0300520 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
521 sizeof *page);
522 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200523 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300524 set_page_private(virt_to_page(page->spt), (unsigned long)page);
525 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300526 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800528 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800530 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800531 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532}
533
Avi Kivity714b93d2007-01-05 16:36:53 -0800534static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
535 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800536{
537 struct kvm_pte_chain *pte_chain;
538 struct hlist_node *node;
539 int i;
540
541 if (!parent_pte)
542 return;
543 if (!page->multimapped) {
544 u64 *old = page->parent_pte;
545
546 if (!old) {
547 page->parent_pte = parent_pte;
548 return;
549 }
550 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800551 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800552 INIT_HLIST_HEAD(&page->parent_ptes);
553 hlist_add_head(&pte_chain->link, &page->parent_ptes);
554 pte_chain->parent_ptes[0] = old;
555 }
556 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
557 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
558 continue;
559 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
560 if (!pte_chain->parent_ptes[i]) {
561 pte_chain->parent_ptes[i] = parent_pte;
562 return;
563 }
564 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800565 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800566 BUG_ON(!pte_chain);
567 hlist_add_head(&pte_chain->link, &page->parent_ptes);
568 pte_chain->parent_ptes[0] = parent_pte;
569}
570
Avi Kivity90cb0522007-07-17 13:04:56 +0300571static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800572 u64 *parent_pte)
573{
574 struct kvm_pte_chain *pte_chain;
575 struct hlist_node *node;
576 int i;
577
578 if (!page->multimapped) {
579 BUG_ON(page->parent_pte != parent_pte);
580 page->parent_pte = NULL;
581 return;
582 }
583 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
584 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
585 if (!pte_chain->parent_ptes[i])
586 break;
587 if (pte_chain->parent_ptes[i] != parent_pte)
588 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800589 while (i + 1 < NR_PTE_CHAIN_ENTRIES
590 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800591 pte_chain->parent_ptes[i]
592 = pte_chain->parent_ptes[i + 1];
593 ++i;
594 }
595 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800596 if (i == 0) {
597 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300598 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800599 if (hlist_empty(&page->parent_ptes)) {
600 page->multimapped = 0;
601 page->parent_pte = NULL;
602 }
603 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800604 return;
605 }
606 BUG();
607}
608
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500609static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800610 gfn_t gfn)
611{
612 unsigned index;
613 struct hlist_head *bucket;
614 struct kvm_mmu_page *page;
615 struct hlist_node *node;
616
617 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
618 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500619 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800620 hlist_for_each_entry(page, node, bucket, hash_link)
621 if (page->gfn == gfn && !page->role.metaphysical) {
622 pgprintk("%s: found role %x\n",
623 __FUNCTION__, page->role.word);
624 return page;
625 }
626 return NULL;
627}
628
629static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
630 gfn_t gfn,
631 gva_t gaddr,
632 unsigned level,
633 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200634 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800635 u64 *parent_pte)
636{
637 union kvm_mmu_page_role role;
638 unsigned index;
639 unsigned quadrant;
640 struct hlist_head *bucket;
641 struct kvm_mmu_page *page;
642 struct hlist_node *node;
643
644 role.word = 0;
645 role.glevels = vcpu->mmu.root_level;
646 role.level = level;
647 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200648 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800649 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
650 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
651 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
652 role.quadrant = quadrant;
653 }
654 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
655 gfn, role.word);
656 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
657 bucket = &vcpu->kvm->mmu_page_hash[index];
658 hlist_for_each_entry(page, node, bucket, hash_link)
659 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800660 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800661 pgprintk("%s: found\n", __FUNCTION__);
662 return page;
663 }
664 page = kvm_mmu_alloc_page(vcpu, parent_pte);
665 if (!page)
666 return page;
667 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
668 page->gfn = gfn;
669 page->role = role;
670 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200671 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800672 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500673 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800674 return page;
675}
676
Avi Kivity90cb0522007-07-17 13:04:56 +0300677static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800678 struct kvm_mmu_page *page)
679{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800680 unsigned i;
681 u64 *pt;
682 u64 ent;
683
Avi Kivity47ad8e62007-05-06 15:50:58 +0300684 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800685
686 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
687 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200688 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200689 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200690 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800691 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300692 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800693 return;
694 }
695
696 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
697 ent = pt[i];
698
Avi Kivityc7addb92007-09-16 18:58:32 +0200699 pt[i] = shadow_trap_nonpresent_pte;
700 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800701 continue;
702 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300703 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800704 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300705 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800706}
707
Avi Kivity90cb0522007-07-17 13:04:56 +0300708static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800709 u64 *parent_pte)
710{
Avi Kivity90cb0522007-07-17 13:04:56 +0300711 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800712}
713
Avi Kivity12b7d282007-09-23 14:10:49 +0200714static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
715{
716 int i;
717
718 for (i = 0; i < KVM_MAX_VCPUS; ++i)
719 if (kvm->vcpus[i])
720 kvm->vcpus[i]->last_pte_updated = NULL;
721}
722
Avi Kivity90cb0522007-07-17 13:04:56 +0300723static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800724 struct kvm_mmu_page *page)
725{
726 u64 *parent_pte;
727
728 while (page->multimapped || page->parent_pte) {
729 if (!page->multimapped)
730 parent_pte = page->parent_pte;
731 else {
732 struct kvm_pte_chain *chain;
733
734 chain = container_of(page->parent_ptes.first,
735 struct kvm_pte_chain, link);
736 parent_pte = chain->parent_ptes[0];
737 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800738 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300739 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200740 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800741 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300742 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800743 if (!page->root_count) {
744 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300745 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200746 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300747 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200748 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800749}
750
Izik Eidus82ce2c92007-10-02 18:52:55 +0200751/*
752 * Changing the number of mmu pages allocated to the vm
753 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
754 */
755void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
756{
757 /*
758 * If we set the number of mmu pages to be smaller be than the
759 * number of actived pages , we must to free some mmu pages before we
760 * change the value
761 */
762
763 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
764 kvm_nr_mmu_pages) {
765 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
766 - kvm->n_free_mmu_pages;
767
768 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
769 struct kvm_mmu_page *page;
770
771 page = container_of(kvm->active_mmu_pages.prev,
772 struct kvm_mmu_page, link);
773 kvm_mmu_zap_page(kvm, page);
774 n_used_mmu_pages--;
775 }
776 kvm->n_free_mmu_pages = 0;
777 }
778 else
779 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
780 - kvm->n_alloc_mmu_pages;
781
782 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
783}
784
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500785static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800786{
787 unsigned index;
788 struct hlist_head *bucket;
789 struct kvm_mmu_page *page;
790 struct hlist_node *node, *n;
791 int r;
792
793 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
794 r = 0;
795 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500796 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800797 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
798 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800799 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
800 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500801 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800802 r = 1;
803 }
804 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800805}
806
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500807static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300808{
809 struct kvm_mmu_page *page;
810
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500811 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300812 pgprintk("%s: zap %lx %x\n",
813 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500814 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300815 }
816}
817
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
819{
820 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
821 struct kvm_mmu_page *page_head = page_header(__pa(pte));
822
823 __set_bit(slot, &page_head->slot_bitmap);
824}
825
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500826hpa_t safe_gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500828 hpa_t hpa = gpa_to_hpa(kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829
830 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
831}
832
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500833hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 struct page *page;
836
837 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500838 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300839 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
842 | (gpa & (PAGE_SIZE-1));
843}
844
845hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
846{
847 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
848
849 if (gpa == UNMAPPED_GVA)
850 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500851 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852}
853
Avi Kivity039576c2007-03-20 12:46:50 +0200854struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
855{
856 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
857
858 if (gpa == UNMAPPED_GVA)
859 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500860 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200861}
862
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
864{
865}
866
867static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
868{
869 int level = PT32E_ROOT_LEVEL;
870 hpa_t table_addr = vcpu->mmu.root_hpa;
871
872 for (; ; level--) {
873 u32 index = PT64_INDEX(v, level);
874 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876
877 ASSERT(VALID_PAGE(table_addr));
878 table = __va(table_addr);
879
880 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800881 pte = table[index];
Avi Kivityc7addb92007-09-16 18:58:32 +0200882 if (is_shadow_present_pte(pte) && is_writeble_pte(pte))
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800883 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
885 page_header_update_slot(vcpu->kvm, table, v);
886 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
887 PT_USER_MASK;
Izik Eidus290fc382007-09-27 14:11:22 +0200888 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 return 0;
890 }
891
Avi Kivityc7addb92007-09-16 18:58:32 +0200892 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800893 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800894 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800896 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
897 >> PAGE_SHIFT;
898 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
899 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200900 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800901 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 pgprintk("nonpaging_map: ENOMEM\n");
903 return -ENOMEM;
904 }
905
Avi Kivity47ad8e62007-05-06 15:50:58 +0300906 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800907 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 }
909 table_addr = table[index] & PT64_BASE_ADDR_MASK;
910 }
911}
912
Avi Kivityc7addb92007-09-16 18:58:32 +0200913static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
914 struct kvm_mmu_page *sp)
915{
916 int i;
917
918 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
919 sp->spt[i] = shadow_trap_nonpresent_pte;
920}
921
Avi Kivity17ac10a2007-01-05 16:36:40 -0800922static void mmu_free_roots(struct kvm_vcpu *vcpu)
923{
924 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800925 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800926
Avi Kivity7b53aa52007-06-05 12:17:03 +0300927 if (!VALID_PAGE(vcpu->mmu.root_hpa))
928 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800929#ifdef CONFIG_X86_64
930 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
931 hpa_t root = vcpu->mmu.root_hpa;
932
Avi Kivity3bb65a22007-01-05 16:36:51 -0800933 page = page_header(root);
934 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800935 vcpu->mmu.root_hpa = INVALID_PAGE;
936 return;
937 }
938#endif
939 for (i = 0; i < 4; ++i) {
940 hpa_t root = vcpu->mmu.pae_root[i];
941
Avi Kivity417726a2007-04-12 17:35:58 +0300942 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300943 root &= PT64_BASE_ADDR_MASK;
944 page = page_header(root);
945 --page->root_count;
946 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800947 vcpu->mmu.pae_root[i] = INVALID_PAGE;
948 }
949 vcpu->mmu.root_hpa = INVALID_PAGE;
950}
951
952static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
953{
954 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800955 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800956 struct kvm_mmu_page *page;
957
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800958 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800959
960#ifdef CONFIG_X86_64
961 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
962 hpa_t root = vcpu->mmu.root_hpa;
963
964 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800965 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200966 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300967 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800968 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800969 vcpu->mmu.root_hpa = root;
970 return;
971 }
972#endif
973 for (i = 0; i < 4; ++i) {
974 hpa_t root = vcpu->mmu.pae_root[i];
975
976 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300977 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
978 if (!is_present_pte(vcpu->pdptrs[i])) {
979 vcpu->mmu.pae_root[i] = 0;
980 continue;
981 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800982 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300983 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800985 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800986 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200987 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300988 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800989 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800990 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
991 }
992 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
993}
994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
996{
997 return vaddr;
998}
999
1000static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1001 u32 error_code)
1002{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001004 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001005 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006
Avi Kivitye2dec932007-01-05 16:36:54 -08001007 r = mmu_topup_memory_caches(vcpu);
1008 if (r)
1009 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001010
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011 ASSERT(vcpu);
1012 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1013
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001015 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016
Avi Kivityebeace82007-01-05 16:36:47 -08001017 if (is_error_hpa(paddr))
1018 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
Avi Kivityebeace82007-01-05 16:36:47 -08001020 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021}
1022
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023static void nonpaging_free(struct kvm_vcpu *vcpu)
1024{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001025 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026}
1027
1028static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1029{
1030 struct kvm_mmu *context = &vcpu->mmu;
1031
1032 context->new_cr3 = nonpaging_new_cr3;
1033 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 context->gva_to_gpa = nonpaging_gva_to_gpa;
1035 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001036 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001037 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001039 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 return 0;
1041}
1042
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1044{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001045 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001046 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047}
1048
1049static void paging_new_cr3(struct kvm_vcpu *vcpu)
1050{
Avi Kivity374cbac2007-01-05 16:36:43 -08001051 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001052 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053}
1054
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055static void inject_page_fault(struct kvm_vcpu *vcpu,
1056 u64 addr,
1057 u32 err_code)
1058{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001059 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060}
1061
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062static void paging_free(struct kvm_vcpu *vcpu)
1063{
1064 nonpaging_free(vcpu);
1065}
1066
1067#define PTTYPE 64
1068#include "paging_tmpl.h"
1069#undef PTTYPE
1070
1071#define PTTYPE 32
1072#include "paging_tmpl.h"
1073#undef PTTYPE
1074
Avi Kivity17ac10a2007-01-05 16:36:40 -08001075static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076{
1077 struct kvm_mmu *context = &vcpu->mmu;
1078
1079 ASSERT(is_pae(vcpu));
1080 context->new_cr3 = paging_new_cr3;
1081 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001083 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001085 context->root_level = level;
1086 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001087 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 return 0;
1089}
1090
Avi Kivity17ac10a2007-01-05 16:36:40 -08001091static int paging64_init_context(struct kvm_vcpu *vcpu)
1092{
1093 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1094}
1095
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096static int paging32_init_context(struct kvm_vcpu *vcpu)
1097{
1098 struct kvm_mmu *context = &vcpu->mmu;
1099
1100 context->new_cr3 = paging_new_cr3;
1101 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 context->gva_to_gpa = paging32_gva_to_gpa;
1103 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001104 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 context->root_level = PT32_ROOT_LEVEL;
1106 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001107 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 return 0;
1109}
1110
1111static int paging32E_init_context(struct kvm_vcpu *vcpu)
1112{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001113 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114}
1115
1116static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1117{
1118 ASSERT(vcpu);
1119 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1120
1121 if (!is_paging(vcpu))
1122 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001123 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 return paging64_init_context(vcpu);
1125 else if (is_pae(vcpu))
1126 return paging32E_init_context(vcpu);
1127 else
1128 return paging32_init_context(vcpu);
1129}
1130
1131static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1132{
1133 ASSERT(vcpu);
1134 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1135 vcpu->mmu.free(vcpu);
1136 vcpu->mmu.root_hpa = INVALID_PAGE;
1137 }
1138}
1139
1140int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1141{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001142 destroy_kvm_mmu(vcpu);
1143 return init_kvm_mmu(vcpu);
1144}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001145EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001146
1147int kvm_mmu_load(struct kvm_vcpu *vcpu)
1148{
Avi Kivity714b93d2007-01-05 16:36:53 -08001149 int r;
1150
Shaohua Li11ec2802007-07-23 14:51:37 +08001151 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001152 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001153 if (r)
1154 goto out;
1155 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001156 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001157 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001158out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001159 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001160 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001162EXPORT_SYMBOL_GPL(kvm_mmu_load);
1163
1164void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1165{
1166 mmu_free_roots(vcpu);
1167}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168
Avi Kivity09072da2007-05-01 14:16:52 +03001169static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001170 struct kvm_mmu_page *page,
1171 u64 *spte)
1172{
1173 u64 pte;
1174 struct kvm_mmu_page *child;
1175
1176 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001177 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001178 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001179 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001180 else {
1181 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001182 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001183 }
1184 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001185 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001186 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001187}
1188
Avi Kivity00284252007-05-01 16:53:31 +03001189static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1190 struct kvm_mmu_page *page,
1191 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001192 const void *new, int bytes,
1193 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001194{
1195 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1196 return;
1197
1198 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001199 paging32_update_pte(vcpu, page, spte, new, bytes,
1200 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001201 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001202 paging64_update_pte(vcpu, page, spte, new, bytes,
1203 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001204}
1205
Avi Kivity12b7d282007-09-23 14:10:49 +02001206static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1207{
1208 u64 *spte = vcpu->last_pte_updated;
1209
1210 return !!(spte && (*spte & PT_ACCESSED_MASK));
1211}
1212
Avi Kivity09072da2007-05-01 14:16:52 +03001213void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001214 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001215{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001216 gfn_t gfn = gpa >> PAGE_SHIFT;
1217 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001218 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001219 struct hlist_head *bucket;
1220 unsigned index;
1221 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001222 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001223 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001224 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001225 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001226 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001227 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001228 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001229 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001230
Avi Kivityda4a00f2007-01-05 16:36:44 -08001231 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityc7addb92007-09-16 18:58:32 +02001232 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001233 if (gfn == vcpu->last_pt_write_gfn
1234 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001235 ++vcpu->last_pt_write_count;
1236 if (vcpu->last_pt_write_count >= 3)
1237 flooded = 1;
1238 } else {
1239 vcpu->last_pt_write_gfn = gfn;
1240 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001241 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001242 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001243 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1244 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001245 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001246 if (page->gfn != gfn || page->role.metaphysical)
1247 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001248 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1249 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001250 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001251 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001252 /*
1253 * Misaligned accesses are too much trouble to fix
1254 * up; also, they usually indicate a page is not used
1255 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001256 *
1257 * If we're seeing too many writes to a page,
1258 * it may no longer be a page table, or we may be
1259 * forking, in which case it is better to unmap the
1260 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001261 */
1262 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1263 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001264 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001265 continue;
1266 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001267 page_offset = offset;
1268 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001269 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001270 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001271 page_offset <<= 1; /* 32->64 */
1272 /*
1273 * A 32-bit pde maps 4MB while the shadow pdes map
1274 * only 2MB. So we need to double the offset again
1275 * and zap two pdes instead of one.
1276 */
1277 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001278 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001279 page_offset <<= 1;
1280 npte = 2;
1281 }
Avi Kivityfce06572007-05-01 16:44:05 +03001282 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001283 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001284 if (quadrant != page->role.quadrant)
1285 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001286 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001287 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001288 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001289 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001290 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1291 page_offset & (pte_size - 1));
Avi Kivityac1b7142007-03-08 17:13:32 +02001292 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001293 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001294 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001295 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001296}
1297
Avi Kivitya4360362007-01-05 16:36:45 -08001298int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1299{
1300 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1301
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001302 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001303}
1304
Avi Kivity22d95b12007-09-14 20:26:06 +03001305void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001306{
1307 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1308 struct kvm_mmu_page *page;
1309
1310 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1311 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001312 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityebeace82007-01-05 16:36:47 -08001313 }
1314}
Avi Kivityebeace82007-01-05 16:36:47 -08001315
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316static void free_mmu_pages(struct kvm_vcpu *vcpu)
1317{
Avi Kivityf51234c2007-01-05 16:36:52 -08001318 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319
Avi Kivityf51234c2007-01-05 16:36:52 -08001320 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1321 page = container_of(vcpu->kvm->active_mmu_pages.next,
1322 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001323 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001324 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001325 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326}
1327
1328static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1329{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001330 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 int i;
1332
1333 ASSERT(vcpu);
1334
Izik Eidus82ce2c92007-10-02 18:52:55 +02001335 if (vcpu->kvm->n_requested_mmu_pages)
1336 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1337 else
1338 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001339 /*
1340 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1341 * Therefore we need to allocate shadow page tables in the first
1342 * 4GB of memory, which happens to fit the DMA32 zone.
1343 */
1344 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1345 if (!page)
1346 goto error_1;
1347 vcpu->mmu.pae_root = page_address(page);
1348 for (i = 0; i < 4; ++i)
1349 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1350
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 return 0;
1352
1353error_1:
1354 free_mmu_pages(vcpu);
1355 return -ENOMEM;
1356}
1357
Ingo Molnar8018c272006-12-29 16:50:01 -08001358int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 ASSERT(vcpu);
1361 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362
Ingo Molnar8018c272006-12-29 16:50:01 -08001363 return alloc_mmu_pages(vcpu);
1364}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365
Ingo Molnar8018c272006-12-29 16:50:01 -08001366int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1367{
1368 ASSERT(vcpu);
1369 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001370
Ingo Molnar8018c272006-12-29 16:50:01 -08001371 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372}
1373
1374void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1375{
1376 ASSERT(vcpu);
1377
1378 destroy_kvm_mmu(vcpu);
1379 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001380 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
1382
Avi Kivity90cb0522007-07-17 13:04:56 +03001383void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384{
1385 struct kvm_mmu_page *page;
1386
1387 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1388 int i;
1389 u64 *pt;
1390
1391 if (!test_bit(slot, &page->slot_bitmap))
1392 continue;
1393
Avi Kivity47ad8e62007-05-06 15:50:58 +03001394 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1396 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001397 if (pt[i] & PT_WRITABLE_MASK) {
Izik Eidus290fc382007-09-27 14:11:22 +02001398 rmap_remove(kvm, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001400 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 }
1402}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001403
Avi Kivity90cb0522007-07-17 13:04:56 +03001404void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001405{
Avi Kivity90cb0522007-07-17 13:04:56 +03001406 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001407
Avi Kivity90cb0522007-07-17 13:04:56 +03001408 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1409 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001410
Avi Kivity90cb0522007-07-17 13:04:56 +03001411 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001412}
1413
Avi Kivityb5a33a72007-04-15 16:31:09 +03001414void kvm_mmu_module_exit(void)
1415{
1416 if (pte_chain_cache)
1417 kmem_cache_destroy(pte_chain_cache);
1418 if (rmap_desc_cache)
1419 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001420 if (mmu_page_header_cache)
1421 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001422}
1423
1424int kvm_mmu_module_init(void)
1425{
1426 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1427 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001428 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001429 if (!pte_chain_cache)
1430 goto nomem;
1431 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1432 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001433 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001434 if (!rmap_desc_cache)
1435 goto nomem;
1436
Avi Kivityd3d25b02007-05-30 12:34:53 +03001437 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1438 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001439 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001440 if (!mmu_page_header_cache)
1441 goto nomem;
1442
Avi Kivityb5a33a72007-04-15 16:31:09 +03001443 return 0;
1444
1445nomem:
1446 kvm_mmu_module_exit();
1447 return -ENOMEM;
1448}
1449
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001450#ifdef AUDIT
1451
1452static const char *audit_msg;
1453
1454static gva_t canonicalize(gva_t gva)
1455{
1456#ifdef CONFIG_X86_64
1457 gva = (long long)(gva << 16) >> 16;
1458#endif
1459 return gva;
1460}
1461
1462static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1463 gva_t va, int level)
1464{
1465 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1466 int i;
1467 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1468
1469 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1470 u64 ent = pt[i];
1471
Avi Kivityc7addb92007-09-16 18:58:32 +02001472 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001473 continue;
1474
1475 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001476 if (level > 1) {
1477 if (ent == shadow_notrap_nonpresent_pte)
1478 printk(KERN_ERR "audit: (%s) nontrapping pte"
1479 " in nonleaf level: levels %d gva %lx"
1480 " level %d pte %llx\n", audit_msg,
1481 vcpu->mmu.root_level, va, level, ent);
1482
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001483 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001484 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001485 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1486 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1487
Avi Kivityc7addb92007-09-16 18:58:32 +02001488 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001489 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001490 printk(KERN_ERR "xx audit error: (%s) levels %d"
1491 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001492 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001493 va, gpa, hpa, ent,
1494 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001495 else if (ent == shadow_notrap_nonpresent_pte
1496 && !is_error_hpa(hpa))
1497 printk(KERN_ERR "audit: (%s) notrap shadow,"
1498 " valid guest gva %lx\n", audit_msg, va);
1499
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001500 }
1501 }
1502}
1503
1504static void audit_mappings(struct kvm_vcpu *vcpu)
1505{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001506 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001507
1508 if (vcpu->mmu.root_level == 4)
1509 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1510 else
1511 for (i = 0; i < 4; ++i)
1512 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1513 audit_mappings_page(vcpu,
1514 vcpu->mmu.pae_root[i],
1515 i << 30,
1516 2);
1517}
1518
1519static int count_rmaps(struct kvm_vcpu *vcpu)
1520{
1521 int nmaps = 0;
1522 int i, j, k;
1523
1524 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1525 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1526 struct kvm_rmap_desc *d;
1527
1528 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001529 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001530
Izik Eidus290fc382007-09-27 14:11:22 +02001531 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001532 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001533 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001534 ++nmaps;
1535 continue;
1536 }
Izik Eidus290fc382007-09-27 14:11:22 +02001537 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001538 while (d) {
1539 for (k = 0; k < RMAP_EXT; ++k)
1540 if (d->shadow_ptes[k])
1541 ++nmaps;
1542 else
1543 break;
1544 d = d->more;
1545 }
1546 }
1547 }
1548 return nmaps;
1549}
1550
1551static int count_writable_mappings(struct kvm_vcpu *vcpu)
1552{
1553 int nmaps = 0;
1554 struct kvm_mmu_page *page;
1555 int i;
1556
1557 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001558 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001559
1560 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1561 continue;
1562
1563 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1564 u64 ent = pt[i];
1565
1566 if (!(ent & PT_PRESENT_MASK))
1567 continue;
1568 if (!(ent & PT_WRITABLE_MASK))
1569 continue;
1570 ++nmaps;
1571 }
1572 }
1573 return nmaps;
1574}
1575
1576static void audit_rmap(struct kvm_vcpu *vcpu)
1577{
1578 int n_rmap = count_rmaps(vcpu);
1579 int n_actual = count_writable_mappings(vcpu);
1580
1581 if (n_rmap != n_actual)
1582 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1583 __FUNCTION__, audit_msg, n_rmap, n_actual);
1584}
1585
1586static void audit_write_protection(struct kvm_vcpu *vcpu)
1587{
1588 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001589 struct kvm_memory_slot *slot;
1590 unsigned long *rmapp;
1591 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001592
1593 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001594 if (page->role.metaphysical)
1595 continue;
1596
Izik Eidus290fc382007-09-27 14:11:22 +02001597 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1598 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1599 rmapp = &slot->rmap[gfn - slot->base_gfn];
1600 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001601 printk(KERN_ERR "%s: (%s) shadow page has writable"
1602 " mappings: gfn %lx role %x\n",
1603 __FUNCTION__, audit_msg, page->gfn,
1604 page->role.word);
1605 }
1606}
1607
1608static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1609{
1610 int olddbg = dbg;
1611
1612 dbg = 0;
1613 audit_msg = msg;
1614 audit_rmap(vcpu);
1615 audit_write_protection(vcpu);
1616 audit_mappings(vcpu);
1617 dbg = olddbg;
1618}
1619
1620#endif