blob: 23965aa5ee7807315aebecf5b03d0970fbd1efbe [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
93#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
94
95
Avi Kivity6aa8b732006-12-10 02:21:36 -080096#define PT_FIRST_AVAIL_BITS_SHIFT 9
97#define PT64_SECOND_AVAIL_BITS_SHIFT 52
98
Avi Kivity6aa8b732006-12-10 02:21:36 -080099#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define VALID_PAGE(x) ((x) != INVALID_PAGE)
102
103#define PT64_LEVEL_BITS 9
104
105#define PT64_LEVEL_SHIFT(level) \
106 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
107
108#define PT64_LEVEL_MASK(level) \
109 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
110
111#define PT64_INDEX(address, level)\
112 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
113
114
115#define PT32_LEVEL_BITS 10
116
117#define PT32_LEVEL_SHIFT(level) \
118 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
119
120#define PT32_LEVEL_MASK(level) \
121 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
122
123#define PT32_INDEX(address, level)\
124 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
125
126
Avi Kivity27aba762007-03-09 13:04:31 +0200127#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128#define PT64_DIR_BASE_ADDR_MASK \
129 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
130
131#define PT32_BASE_ADDR_MASK PAGE_MASK
132#define PT32_DIR_BASE_ADDR_MASK \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
134
135
136#define PFERR_PRESENT_MASK (1U << 0)
137#define PFERR_WRITE_MASK (1U << 1)
138#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800139#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PT64_ROOT_LEVEL 4
142#define PT32_ROOT_LEVEL 2
143#define PT32E_ROOT_LEVEL 3
144
145#define PT_DIRECTORY_LEVEL 2
146#define PT_PAGE_TABLE_LEVEL 1
147
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800148#define RMAP_EXT 4
149
150struct kvm_rmap_desc {
151 u64 *shadow_ptes[RMAP_EXT];
152 struct kvm_rmap_desc *more;
153};
154
Avi Kivityb5a33a72007-04-15 16:31:09 +0300155static struct kmem_cache *pte_chain_cache;
156static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300157static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300158
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159static int is_write_protection(struct kvm_vcpu *vcpu)
160{
161 return vcpu->cr0 & CR0_WP_MASK;
162}
163
164static int is_cpuid_PSE36(void)
165{
166 return 1;
167}
168
Avi Kivity73b10872007-01-26 00:56:41 -0800169static int is_nx(struct kvm_vcpu *vcpu)
170{
171 return vcpu->shadow_efer & EFER_NX;
172}
173
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174static int is_present_pte(unsigned long pte)
175{
176 return pte & PT_PRESENT_MASK;
177}
178
179static int is_writeble_pte(unsigned long pte)
180{
181 return pte & PT_WRITABLE_MASK;
182}
183
184static int is_io_pte(unsigned long pte)
185{
186 return pte & PT_SHADOW_IO_MARK;
187}
188
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800189static int is_rmap_pte(u64 pte)
190{
191 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
192 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
193}
194
Avi Kivitye663ee62007-05-31 15:46:04 +0300195static void set_shadow_pte(u64 *sptep, u64 spte)
196{
197#ifdef CONFIG_X86_64
198 set_64bit((unsigned long *)sptep, spte);
199#else
200 set_64bit((unsigned long long *)sptep, spte);
201#endif
202}
203
Avi Kivitye2dec932007-01-05 16:36:54 -0800204static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300205 struct kmem_cache *base_cache, int min,
206 gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800207{
208 void *obj;
209
210 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800211 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800212 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity8c438502007-04-16 11:53:17 +0300213 obj = kmem_cache_zalloc(base_cache, gfp_flags);
Avi Kivity714b93d2007-01-05 16:36:53 -0800214 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800215 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800216 cache->objects[cache->nobjs++] = obj;
217 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800218 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800219}
220
221static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
222{
223 while (mc->nobjs)
224 kfree(mc->objects[--mc->nobjs]);
225}
226
Avi Kivityc1158e62007-07-20 08:18:27 +0300227static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
228 int min, gfp_t gfp_flags)
229{
230 struct page *page;
231
232 if (cache->nobjs >= min)
233 return 0;
234 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
235 page = alloc_page(gfp_flags);
236 if (!page)
237 return -ENOMEM;
238 set_page_private(page, 0);
239 cache->objects[cache->nobjs++] = page_address(page);
240 }
241 return 0;
242}
243
244static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
245{
246 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300247 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300248}
249
Avi Kivity8c438502007-04-16 11:53:17 +0300250static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800251{
Avi Kivitye2dec932007-01-05 16:36:54 -0800252 int r;
253
254 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300255 pte_chain_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800256 if (r)
257 goto out;
258 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300259 rmap_desc_cache, 1, gfp_flags);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300260 if (r)
261 goto out;
Avi Kivityc1158e62007-07-20 08:18:27 +0300262 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 4, gfp_flags);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300263 if (r)
264 goto out;
265 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
266 mmu_page_header_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800267out:
268 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800269}
270
Avi Kivity8c438502007-04-16 11:53:17 +0300271static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
272{
273 int r;
274
275 r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
Avi Kivity22d95b12007-09-14 20:26:06 +0300276 kvm_mmu_free_some_pages(vcpu);
Avi Kivity8c438502007-04-16 11:53:17 +0300277 if (r < 0) {
278 spin_unlock(&vcpu->kvm->lock);
279 kvm_arch_ops->vcpu_put(vcpu);
280 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
281 kvm_arch_ops->vcpu_load(vcpu);
282 spin_lock(&vcpu->kvm->lock);
Avi Kivity22d95b12007-09-14 20:26:06 +0300283 kvm_mmu_free_some_pages(vcpu);
Avi Kivity8c438502007-04-16 11:53:17 +0300284 }
285 return r;
286}
287
Avi Kivity714b93d2007-01-05 16:36:53 -0800288static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
289{
290 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
291 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300292 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300293 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800294}
295
296static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
297 size_t size)
298{
299 void *p;
300
301 BUG_ON(!mc->nobjs);
302 p = mc->objects[--mc->nobjs];
303 memset(p, 0, size);
304 return p;
305}
306
Avi Kivity714b93d2007-01-05 16:36:53 -0800307static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
308{
309 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
310 sizeof(struct kvm_pte_chain));
311}
312
Avi Kivity90cb0522007-07-17 13:04:56 +0300313static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800314{
Avi Kivity90cb0522007-07-17 13:04:56 +0300315 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800316}
317
318static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
319{
320 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
321 sizeof(struct kvm_rmap_desc));
322}
323
Avi Kivity90cb0522007-07-17 13:04:56 +0300324static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800325{
Avi Kivity90cb0522007-07-17 13:04:56 +0300326 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800327}
328
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800329/*
330 * Reverse mapping data structures:
331 *
332 * If page->private bit zero is zero, then page->private points to the
333 * shadow page table entry that points to page_address(page).
334 *
335 * If page->private bit zero is one, (then page->private & ~1) points
336 * to a struct kvm_rmap_desc containing more mappings.
337 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800338static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800339{
340 struct page *page;
341 struct kvm_rmap_desc *desc;
342 int i;
343
344 if (!is_rmap_pte(*spte))
345 return;
346 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200347 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800348 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200349 set_page_private(page,(unsigned long)spte);
350 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800351 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800352 desc = mmu_alloc_rmap_desc(vcpu);
Markus Rechberger5972e952007-02-19 14:37:47 +0200353 desc->shadow_ptes[0] = (u64 *)page_private(page);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800354 desc->shadow_ptes[1] = spte;
Markus Rechberger5972e952007-02-19 14:37:47 +0200355 set_page_private(page,(unsigned long)desc | 1);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800356 } else {
357 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200358 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800359 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
360 desc = desc->more;
361 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800363 desc = desc->more;
364 }
365 for (i = 0; desc->shadow_ptes[i]; ++i)
366 ;
367 desc->shadow_ptes[i] = spte;
368 }
369}
370
Avi Kivity90cb0522007-07-17 13:04:56 +0300371static void rmap_desc_remove_entry(struct page *page,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372 struct kvm_rmap_desc *desc,
373 int i,
374 struct kvm_rmap_desc *prev_desc)
375{
376 int j;
377
378 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
379 ;
380 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000381 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800382 if (j != 0)
383 return;
384 if (!prev_desc && !desc->more)
Markus Rechberger5972e952007-02-19 14:37:47 +0200385 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800386 else
387 if (prev_desc)
388 prev_desc->more = desc->more;
389 else
Markus Rechberger5972e952007-02-19 14:37:47 +0200390 set_page_private(page,(unsigned long)desc->more | 1);
Avi Kivity90cb0522007-07-17 13:04:56 +0300391 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800392}
393
Avi Kivity90cb0522007-07-17 13:04:56 +0300394static void rmap_remove(u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800395{
396 struct page *page;
397 struct kvm_rmap_desc *desc;
398 struct kvm_rmap_desc *prev_desc;
399 int i;
400
401 if (!is_rmap_pte(*spte))
402 return;
403 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200404 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
406 BUG();
Markus Rechberger5972e952007-02-19 14:37:47 +0200407 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800408 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200409 if ((u64 *)page_private(page) != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800410 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
411 spte, *spte);
412 BUG();
413 }
Markus Rechberger5972e952007-02-19 14:37:47 +0200414 set_page_private(page,0);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800415 } else {
416 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200417 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800418 prev_desc = NULL;
419 while (desc) {
420 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
421 if (desc->shadow_ptes[i] == spte) {
Avi Kivity90cb0522007-07-17 13:04:56 +0300422 rmap_desc_remove_entry(page,
Avi Kivity714b93d2007-01-05 16:36:53 -0800423 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800424 prev_desc);
425 return;
426 }
427 prev_desc = desc;
428 desc = desc->more;
429 }
430 BUG();
431 }
432}
433
Avi Kivity714b93d2007-01-05 16:36:53 -0800434static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800435{
Avi Kivity714b93d2007-01-05 16:36:53 -0800436 struct kvm *kvm = vcpu->kvm;
Avi Kivity374cbac2007-01-05 16:36:43 -0800437 struct page *page;
Avi Kivity374cbac2007-01-05 16:36:43 -0800438 struct kvm_rmap_desc *desc;
439 u64 *spte;
440
Avi Kivity954bbbc22007-03-30 14:02:32 +0300441 page = gfn_to_page(kvm, gfn);
442 BUG_ON(!page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800443
Markus Rechberger5972e952007-02-19 14:37:47 +0200444 while (page_private(page)) {
445 if (!(page_private(page) & 1))
446 spte = (u64 *)page_private(page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800447 else {
Markus Rechberger5972e952007-02-19 14:37:47 +0200448 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800449 spte = desc->shadow_ptes[0];
450 }
451 BUG_ON(!spte);
Avi Kivity27aba762007-03-09 13:04:31 +0200452 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
453 != page_to_pfn(page));
Avi Kivity374cbac2007-01-05 16:36:43 -0800454 BUG_ON(!(*spte & PT_PRESENT_MASK));
455 BUG_ON(!(*spte & PT_WRITABLE_MASK));
456 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300457 rmap_remove(spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300458 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Shaohua Li88a97f02007-06-20 17:13:26 +0800459 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800460 }
461}
462
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800463#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300464static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465{
Avi Kivity139bdb22007-01-05 16:36:50 -0800466 u64 *pos;
467 u64 *end;
468
Avi Kivity47ad8e62007-05-06 15:50:58 +0300469 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800470 if (*pos != 0) {
471 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
472 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800474 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 return 1;
476}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800477#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
Avi Kivity90cb0522007-07-17 13:04:56 +0300479static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300480 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800481{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300482 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300483 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300484 __free_page(virt_to_page(page_head->spt));
Avi Kivity90cb0522007-07-17 13:04:56 +0300485 kfree(page_head);
486 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800487}
488
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800489static unsigned kvm_page_table_hashfn(gfn_t gfn)
490{
491 return gfn;
492}
493
Avi Kivity25c0de22007-01-05 16:36:42 -0800494static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
495 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496{
497 struct kvm_mmu_page *page;
498
Avi Kivityd3d25b02007-05-30 12:34:53 +0300499 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800500 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501
Avi Kivityd3d25b02007-05-30 12:34:53 +0300502 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
503 sizeof *page);
504 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
505 set_page_private(virt_to_page(page->spt), (unsigned long)page);
506 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300507 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800509 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800511 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800512 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513}
514
Avi Kivity714b93d2007-01-05 16:36:53 -0800515static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
516 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800517{
518 struct kvm_pte_chain *pte_chain;
519 struct hlist_node *node;
520 int i;
521
522 if (!parent_pte)
523 return;
524 if (!page->multimapped) {
525 u64 *old = page->parent_pte;
526
527 if (!old) {
528 page->parent_pte = parent_pte;
529 return;
530 }
531 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800532 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800533 INIT_HLIST_HEAD(&page->parent_ptes);
534 hlist_add_head(&pte_chain->link, &page->parent_ptes);
535 pte_chain->parent_ptes[0] = old;
536 }
537 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
538 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
539 continue;
540 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
541 if (!pte_chain->parent_ptes[i]) {
542 pte_chain->parent_ptes[i] = parent_pte;
543 return;
544 }
545 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800546 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800547 BUG_ON(!pte_chain);
548 hlist_add_head(&pte_chain->link, &page->parent_ptes);
549 pte_chain->parent_ptes[0] = parent_pte;
550}
551
Avi Kivity90cb0522007-07-17 13:04:56 +0300552static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800553 u64 *parent_pte)
554{
555 struct kvm_pte_chain *pte_chain;
556 struct hlist_node *node;
557 int i;
558
559 if (!page->multimapped) {
560 BUG_ON(page->parent_pte != parent_pte);
561 page->parent_pte = NULL;
562 return;
563 }
564 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
565 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
566 if (!pte_chain->parent_ptes[i])
567 break;
568 if (pte_chain->parent_ptes[i] != parent_pte)
569 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800570 while (i + 1 < NR_PTE_CHAIN_ENTRIES
571 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800572 pte_chain->parent_ptes[i]
573 = pte_chain->parent_ptes[i + 1];
574 ++i;
575 }
576 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800577 if (i == 0) {
578 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300579 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800580 if (hlist_empty(&page->parent_ptes)) {
581 page->multimapped = 0;
582 page->parent_pte = NULL;
583 }
584 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800585 return;
586 }
587 BUG();
588}
589
590static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
591 gfn_t gfn)
592{
593 unsigned index;
594 struct hlist_head *bucket;
595 struct kvm_mmu_page *page;
596 struct hlist_node *node;
597
598 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
599 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
600 bucket = &vcpu->kvm->mmu_page_hash[index];
601 hlist_for_each_entry(page, node, bucket, hash_link)
602 if (page->gfn == gfn && !page->role.metaphysical) {
603 pgprintk("%s: found role %x\n",
604 __FUNCTION__, page->role.word);
605 return page;
606 }
607 return NULL;
608}
609
610static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
611 gfn_t gfn,
612 gva_t gaddr,
613 unsigned level,
614 int metaphysical,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200615 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800616 u64 *parent_pte)
617{
618 union kvm_mmu_page_role role;
619 unsigned index;
620 unsigned quadrant;
621 struct hlist_head *bucket;
622 struct kvm_mmu_page *page;
623 struct hlist_node *node;
624
625 role.word = 0;
626 role.glevels = vcpu->mmu.root_level;
627 role.level = level;
628 role.metaphysical = metaphysical;
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200629 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800630 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
631 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
632 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
633 role.quadrant = quadrant;
634 }
635 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
636 gfn, role.word);
637 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
638 bucket = &vcpu->kvm->mmu_page_hash[index];
639 hlist_for_each_entry(page, node, bucket, hash_link)
640 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800641 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800642 pgprintk("%s: found\n", __FUNCTION__);
643 return page;
644 }
645 page = kvm_mmu_alloc_page(vcpu, parent_pte);
646 if (!page)
647 return page;
648 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
649 page->gfn = gfn;
650 page->role = role;
651 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800652 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800653 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800654 return page;
655}
656
Avi Kivity90cb0522007-07-17 13:04:56 +0300657static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800658 struct kvm_mmu_page *page)
659{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800660 unsigned i;
661 u64 *pt;
662 u64 ent;
663
Avi Kivity47ad8e62007-05-06 15:50:58 +0300664 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800665
666 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
667 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
668 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity90cb0522007-07-17 13:04:56 +0300669 rmap_remove(&pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800670 pt[i] = 0;
671 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300672 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800673 return;
674 }
675
676 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
677 ent = pt[i];
678
679 pt[i] = 0;
680 if (!(ent & PT_PRESENT_MASK))
681 continue;
682 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300683 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800684 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300685 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800686}
687
Avi Kivity90cb0522007-07-17 13:04:56 +0300688static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800689 u64 *parent_pte)
690{
Avi Kivity90cb0522007-07-17 13:04:56 +0300691 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800692}
693
Avi Kivity90cb0522007-07-17 13:04:56 +0300694static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800695 struct kvm_mmu_page *page)
696{
697 u64 *parent_pte;
698
699 while (page->multimapped || page->parent_pte) {
700 if (!page->multimapped)
701 parent_pte = page->parent_pte;
702 else {
703 struct kvm_pte_chain *chain;
704
705 chain = container_of(page->parent_ptes.first,
706 struct kvm_pte_chain, link);
707 parent_pte = chain->parent_ptes[0];
708 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800709 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300710 kvm_mmu_put_page(page, parent_pte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300711 set_shadow_pte(parent_pte, 0);
Avi Kivitya4360362007-01-05 16:36:45 -0800712 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300713 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800714 if (!page->root_count) {
715 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300716 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200717 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300718 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800719}
720
721static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
722{
723 unsigned index;
724 struct hlist_head *bucket;
725 struct kvm_mmu_page *page;
726 struct hlist_node *node, *n;
727 int r;
728
729 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
730 r = 0;
731 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
732 bucket = &vcpu->kvm->mmu_page_hash[index];
733 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
734 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800735 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
736 page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +0300737 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800738 r = 1;
739 }
740 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800741}
742
Avi Kivity97a0a012007-05-31 15:08:29 +0300743static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn)
744{
745 struct kvm_mmu_page *page;
746
747 while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) {
748 pgprintk("%s: zap %lx %x\n",
749 __FUNCTION__, gfn, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +0300750 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300751 }
752}
753
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
755{
756 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
757 struct kvm_mmu_page *page_head = page_header(__pa(pte));
758
759 __set_bit(slot, &page_head->slot_bitmap);
760}
761
762hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
763{
764 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
765
766 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
767}
768
769hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
770{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 struct page *page;
772
773 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc22007-03-30 14:02:32 +0300774 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
775 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
778 | (gpa & (PAGE_SIZE-1));
779}
780
781hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
782{
783 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
784
785 if (gpa == UNMAPPED_GVA)
786 return UNMAPPED_GVA;
787 return gpa_to_hpa(vcpu, gpa);
788}
789
Avi Kivity039576c2007-03-20 12:46:50 +0200790struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
791{
792 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
793
794 if (gpa == UNMAPPED_GVA)
795 return NULL;
796 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
797}
798
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
800{
801}
802
803static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
804{
805 int level = PT32E_ROOT_LEVEL;
806 hpa_t table_addr = vcpu->mmu.root_hpa;
807
808 for (; ; level--) {
809 u32 index = PT64_INDEX(v, level);
810 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800811 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
813 ASSERT(VALID_PAGE(table_addr));
814 table = __va(table_addr);
815
816 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800817 pte = table[index];
818 if (is_present_pte(pte) && is_writeble_pte(pte))
819 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
821 page_header_update_slot(vcpu->kvm, table, v);
822 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
823 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800824 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 return 0;
826 }
827
828 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800829 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800830 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800832 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
833 >> PAGE_SHIFT;
834 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
835 v, level - 1,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200836 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800837 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 pgprintk("nonpaging_map: ENOMEM\n");
839 return -ENOMEM;
840 }
841
Avi Kivity47ad8e62007-05-06 15:50:58 +0300842 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800843 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 }
845 table_addr = table[index] & PT64_BASE_ADDR_MASK;
846 }
847}
848
Avi Kivity17ac10a2007-01-05 16:36:40 -0800849static void mmu_free_roots(struct kvm_vcpu *vcpu)
850{
851 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800852 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800853
Avi Kivity7b53aa52007-06-05 12:17:03 +0300854 if (!VALID_PAGE(vcpu->mmu.root_hpa))
855 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800856#ifdef CONFIG_X86_64
857 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
858 hpa_t root = vcpu->mmu.root_hpa;
859
Avi Kivity3bb65a22007-01-05 16:36:51 -0800860 page = page_header(root);
861 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800862 vcpu->mmu.root_hpa = INVALID_PAGE;
863 return;
864 }
865#endif
866 for (i = 0; i < 4; ++i) {
867 hpa_t root = vcpu->mmu.pae_root[i];
868
Avi Kivity417726a2007-04-12 17:35:58 +0300869 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300870 root &= PT64_BASE_ADDR_MASK;
871 page = page_header(root);
872 --page->root_count;
873 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800874 vcpu->mmu.pae_root[i] = INVALID_PAGE;
875 }
876 vcpu->mmu.root_hpa = INVALID_PAGE;
877}
878
879static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
880{
881 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800882 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800883 struct kvm_mmu_page *page;
884
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800885 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800886
887#ifdef CONFIG_X86_64
888 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
889 hpa_t root = vcpu->mmu.root_hpa;
890
891 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800892 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200893 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300894 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800895 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800896 vcpu->mmu.root_hpa = root;
897 return;
898 }
899#endif
900 for (i = 0; i < 4; ++i) {
901 hpa_t root = vcpu->mmu.pae_root[i];
902
903 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300904 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
905 if (!is_present_pte(vcpu->pdptrs[i])) {
906 vcpu->mmu.pae_root[i] = 0;
907 continue;
908 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800909 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300910 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800912 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800913 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200914 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300915 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800916 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800917 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
918 }
919 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
920}
921
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
923{
924 return vaddr;
925}
926
927static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
928 u32 error_code)
929{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800931 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800932 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933
Avi Kivitye2dec932007-01-05 16:36:54 -0800934 r = mmu_topup_memory_caches(vcpu);
935 if (r)
936 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 ASSERT(vcpu);
939 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
940
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941
Avi Kivityebeace82007-01-05 16:36:47 -0800942 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
Avi Kivityebeace82007-01-05 16:36:47 -0800944 if (is_error_hpa(paddr))
945 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
Avi Kivityebeace82007-01-05 16:36:47 -0800947 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948}
949
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950static void nonpaging_free(struct kvm_vcpu *vcpu)
951{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800952 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953}
954
955static int nonpaging_init_context(struct kvm_vcpu *vcpu)
956{
957 struct kvm_mmu *context = &vcpu->mmu;
958
959 context->new_cr3 = nonpaging_new_cr3;
960 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 context->gva_to_gpa = nonpaging_gva_to_gpa;
962 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800963 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +0300965 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 return 0;
967}
968
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
970{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300971 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 kvm_arch_ops->tlb_flush(vcpu);
973}
974
975static void paging_new_cr3(struct kvm_vcpu *vcpu)
976{
Avi Kivity374cbac2007-01-05 16:36:43 -0800977 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800978 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979}
980
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981static void inject_page_fault(struct kvm_vcpu *vcpu,
982 u64 addr,
983 u32 err_code)
984{
985 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
986}
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988static void paging_free(struct kvm_vcpu *vcpu)
989{
990 nonpaging_free(vcpu);
991}
992
993#define PTTYPE 64
994#include "paging_tmpl.h"
995#undef PTTYPE
996
997#define PTTYPE 32
998#include "paging_tmpl.h"
999#undef PTTYPE
1000
Avi Kivity17ac10a2007-01-05 16:36:40 -08001001static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002{
1003 struct kvm_mmu *context = &vcpu->mmu;
1004
1005 ASSERT(is_pae(vcpu));
1006 context->new_cr3 = paging_new_cr3;
1007 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 context->gva_to_gpa = paging64_gva_to_gpa;
1009 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001010 context->root_level = level;
1011 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001012 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 return 0;
1014}
1015
Avi Kivity17ac10a2007-01-05 16:36:40 -08001016static int paging64_init_context(struct kvm_vcpu *vcpu)
1017{
1018 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1019}
1020
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021static int paging32_init_context(struct kvm_vcpu *vcpu)
1022{
1023 struct kvm_mmu *context = &vcpu->mmu;
1024
1025 context->new_cr3 = paging_new_cr3;
1026 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027 context->gva_to_gpa = paging32_gva_to_gpa;
1028 context->free = paging_free;
1029 context->root_level = PT32_ROOT_LEVEL;
1030 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001031 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 return 0;
1033}
1034
1035static int paging32E_init_context(struct kvm_vcpu *vcpu)
1036{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001037 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038}
1039
1040static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1041{
1042 ASSERT(vcpu);
1043 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1044
1045 if (!is_paging(vcpu))
1046 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001047 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 return paging64_init_context(vcpu);
1049 else if (is_pae(vcpu))
1050 return paging32E_init_context(vcpu);
1051 else
1052 return paging32_init_context(vcpu);
1053}
1054
1055static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1056{
1057 ASSERT(vcpu);
1058 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1059 vcpu->mmu.free(vcpu);
1060 vcpu->mmu.root_hpa = INVALID_PAGE;
1061 }
1062}
1063
1064int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1065{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001066 destroy_kvm_mmu(vcpu);
1067 return init_kvm_mmu(vcpu);
1068}
1069
1070int kvm_mmu_load(struct kvm_vcpu *vcpu)
1071{
Avi Kivity714b93d2007-01-05 16:36:53 -08001072 int r;
1073
Avi Kivity17c3ba92007-06-04 15:58:30 +03001074 spin_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001075 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001076 if (r)
1077 goto out;
1078 mmu_alloc_roots(vcpu);
1079 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
1080 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001081out:
Avi Kivity17c3ba92007-06-04 15:58:30 +03001082 spin_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001083 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001085EXPORT_SYMBOL_GPL(kvm_mmu_load);
1086
1087void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1088{
1089 mmu_free_roots(vcpu);
1090}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
Avi Kivity09072da2007-05-01 14:16:52 +03001092static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001093 struct kvm_mmu_page *page,
1094 u64 *spte)
1095{
1096 u64 pte;
1097 struct kvm_mmu_page *child;
1098
1099 pte = *spte;
1100 if (is_present_pte(pte)) {
1101 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Avi Kivity90cb0522007-07-17 13:04:56 +03001102 rmap_remove(spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001103 else {
1104 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001105 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001106 }
1107 }
1108 *spte = 0;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001109 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001110}
1111
Avi Kivity00284252007-05-01 16:53:31 +03001112static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1113 struct kvm_mmu_page *page,
1114 u64 *spte,
1115 const void *new, int bytes)
1116{
1117 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1118 return;
1119
1120 if (page->role.glevels == PT32_ROOT_LEVEL)
1121 paging32_update_pte(vcpu, page, spte, new, bytes);
1122 else
1123 paging64_update_pte(vcpu, page, spte, new, bytes);
1124}
1125
Avi Kivity09072da2007-05-01 14:16:52 +03001126void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1127 const u8 *old, const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001128{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001129 gfn_t gfn = gpa >> PAGE_SHIFT;
1130 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001131 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001132 struct hlist_head *bucket;
1133 unsigned index;
1134 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001135 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001136 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001137 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001138 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001139 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001140 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001141 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001142 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001143
Avi Kivityda4a00f2007-01-05 16:36:44 -08001144 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001145 if (gfn == vcpu->last_pt_write_gfn) {
1146 ++vcpu->last_pt_write_count;
1147 if (vcpu->last_pt_write_count >= 3)
1148 flooded = 1;
1149 } else {
1150 vcpu->last_pt_write_gfn = gfn;
1151 vcpu->last_pt_write_count = 1;
1152 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001153 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1154 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001155 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001156 if (page->gfn != gfn || page->role.metaphysical)
1157 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001158 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1159 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001160 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001161 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001162 /*
1163 * Misaligned accesses are too much trouble to fix
1164 * up; also, they usually indicate a page is not used
1165 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001166 *
1167 * If we're seeing too many writes to a page,
1168 * it may no longer be a page table, or we may be
1169 * forking, in which case it is better to unmap the
1170 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001171 */
1172 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1173 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001174 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001175 continue;
1176 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001177 page_offset = offset;
1178 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001179 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001180 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001181 page_offset <<= 1; /* 32->64 */
1182 /*
1183 * A 32-bit pde maps 4MB while the shadow pdes map
1184 * only 2MB. So we need to double the offset again
1185 * and zap two pdes instead of one.
1186 */
1187 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001188 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001189 page_offset <<= 1;
1190 npte = 2;
1191 }
Avi Kivityfce06572007-05-01 16:44:05 +03001192 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001193 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001194 if (quadrant != page->role.quadrant)
1195 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001196 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001197 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001198 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001199 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivity00284252007-05-01 16:53:31 +03001200 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
Avi Kivityac1b7142007-03-08 17:13:32 +02001201 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001202 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001203 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001204}
1205
Avi Kivitya4360362007-01-05 16:36:45 -08001206int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1207{
1208 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1209
1210 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1211}
1212
Avi Kivity22d95b12007-09-14 20:26:06 +03001213void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001214{
1215 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1216 struct kvm_mmu_page *page;
1217
1218 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1219 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001220 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityebeace82007-01-05 16:36:47 -08001221 }
1222}
Avi Kivityebeace82007-01-05 16:36:47 -08001223
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224static void free_mmu_pages(struct kvm_vcpu *vcpu)
1225{
Avi Kivityf51234c2007-01-05 16:36:52 -08001226 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227
Avi Kivityf51234c2007-01-05 16:36:52 -08001228 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1229 page = container_of(vcpu->kvm->active_mmu_pages.next,
1230 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001231 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001232 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234}
1235
1236static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1237{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001238 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239 int i;
1240
1241 ASSERT(vcpu);
1242
Avi Kivityd3d25b02007-05-30 12:34:53 +03001243 vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001244
1245 /*
1246 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1247 * Therefore we need to allocate shadow page tables in the first
1248 * 4GB of memory, which happens to fit the DMA32 zone.
1249 */
1250 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1251 if (!page)
1252 goto error_1;
1253 vcpu->mmu.pae_root = page_address(page);
1254 for (i = 0; i < 4; ++i)
1255 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1256
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 return 0;
1258
1259error_1:
1260 free_mmu_pages(vcpu);
1261 return -ENOMEM;
1262}
1263
Ingo Molnar8018c272006-12-29 16:50:01 -08001264int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 ASSERT(vcpu);
1267 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268
Ingo Molnar8018c272006-12-29 16:50:01 -08001269 return alloc_mmu_pages(vcpu);
1270}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271
Ingo Molnar8018c272006-12-29 16:50:01 -08001272int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1273{
1274 ASSERT(vcpu);
1275 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001276
Ingo Molnar8018c272006-12-29 16:50:01 -08001277 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
1280void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1281{
1282 ASSERT(vcpu);
1283
1284 destroy_kvm_mmu(vcpu);
1285 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001286 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287}
1288
Avi Kivity90cb0522007-07-17 13:04:56 +03001289void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290{
1291 struct kvm_mmu_page *page;
1292
1293 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1294 int i;
1295 u64 *pt;
1296
1297 if (!test_bit(slot, &page->slot_bitmap))
1298 continue;
1299
Avi Kivity47ad8e62007-05-06 15:50:58 +03001300 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1302 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001303 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity90cb0522007-07-17 13:04:56 +03001304 rmap_remove(&pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001306 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 }
1308}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001309
Avi Kivity90cb0522007-07-17 13:04:56 +03001310void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001311{
Avi Kivity90cb0522007-07-17 13:04:56 +03001312 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001313
Avi Kivity90cb0522007-07-17 13:04:56 +03001314 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1315 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001316
Avi Kivity90cb0522007-07-17 13:04:56 +03001317 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001318}
1319
Avi Kivityb5a33a72007-04-15 16:31:09 +03001320void kvm_mmu_module_exit(void)
1321{
1322 if (pte_chain_cache)
1323 kmem_cache_destroy(pte_chain_cache);
1324 if (rmap_desc_cache)
1325 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001326 if (mmu_page_header_cache)
1327 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001328}
1329
1330int kvm_mmu_module_init(void)
1331{
1332 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1333 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001334 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001335 if (!pte_chain_cache)
1336 goto nomem;
1337 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1338 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001339 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001340 if (!rmap_desc_cache)
1341 goto nomem;
1342
Avi Kivityd3d25b02007-05-30 12:34:53 +03001343 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1344 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001345 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001346 if (!mmu_page_header_cache)
1347 goto nomem;
1348
Avi Kivityb5a33a72007-04-15 16:31:09 +03001349 return 0;
1350
1351nomem:
1352 kvm_mmu_module_exit();
1353 return -ENOMEM;
1354}
1355
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001356#ifdef AUDIT
1357
1358static const char *audit_msg;
1359
1360static gva_t canonicalize(gva_t gva)
1361{
1362#ifdef CONFIG_X86_64
1363 gva = (long long)(gva << 16) >> 16;
1364#endif
1365 return gva;
1366}
1367
1368static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1369 gva_t va, int level)
1370{
1371 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1372 int i;
1373 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1374
1375 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1376 u64 ent = pt[i];
1377
Adrian Bunk28076962007-04-28 21:20:48 +02001378 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001379 continue;
1380
1381 va = canonicalize(va);
1382 if (level > 1)
1383 audit_mappings_page(vcpu, ent, va, level - 1);
1384 else {
1385 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1386 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1387
1388 if ((ent & PT_PRESENT_MASK)
1389 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1390 printk(KERN_ERR "audit error: (%s) levels %d"
1391 " gva %lx gpa %llx hpa %llx ent %llx\n",
1392 audit_msg, vcpu->mmu.root_level,
1393 va, gpa, hpa, ent);
1394 }
1395 }
1396}
1397
1398static void audit_mappings(struct kvm_vcpu *vcpu)
1399{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001400 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001401
1402 if (vcpu->mmu.root_level == 4)
1403 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1404 else
1405 for (i = 0; i < 4; ++i)
1406 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1407 audit_mappings_page(vcpu,
1408 vcpu->mmu.pae_root[i],
1409 i << 30,
1410 2);
1411}
1412
1413static int count_rmaps(struct kvm_vcpu *vcpu)
1414{
1415 int nmaps = 0;
1416 int i, j, k;
1417
1418 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1419 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1420 struct kvm_rmap_desc *d;
1421
1422 for (j = 0; j < m->npages; ++j) {
1423 struct page *page = m->phys_mem[j];
1424
1425 if (!page->private)
1426 continue;
1427 if (!(page->private & 1)) {
1428 ++nmaps;
1429 continue;
1430 }
1431 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1432 while (d) {
1433 for (k = 0; k < RMAP_EXT; ++k)
1434 if (d->shadow_ptes[k])
1435 ++nmaps;
1436 else
1437 break;
1438 d = d->more;
1439 }
1440 }
1441 }
1442 return nmaps;
1443}
1444
1445static int count_writable_mappings(struct kvm_vcpu *vcpu)
1446{
1447 int nmaps = 0;
1448 struct kvm_mmu_page *page;
1449 int i;
1450
1451 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001452 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001453
1454 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1455 continue;
1456
1457 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1458 u64 ent = pt[i];
1459
1460 if (!(ent & PT_PRESENT_MASK))
1461 continue;
1462 if (!(ent & PT_WRITABLE_MASK))
1463 continue;
1464 ++nmaps;
1465 }
1466 }
1467 return nmaps;
1468}
1469
1470static void audit_rmap(struct kvm_vcpu *vcpu)
1471{
1472 int n_rmap = count_rmaps(vcpu);
1473 int n_actual = count_writable_mappings(vcpu);
1474
1475 if (n_rmap != n_actual)
1476 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1477 __FUNCTION__, audit_msg, n_rmap, n_actual);
1478}
1479
1480static void audit_write_protection(struct kvm_vcpu *vcpu)
1481{
1482 struct kvm_mmu_page *page;
1483
1484 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1485 hfn_t hfn;
1486 struct page *pg;
1487
1488 if (page->role.metaphysical)
1489 continue;
1490
1491 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1492 >> PAGE_SHIFT;
1493 pg = pfn_to_page(hfn);
1494 if (pg->private)
1495 printk(KERN_ERR "%s: (%s) shadow page has writable"
1496 " mappings: gfn %lx role %x\n",
1497 __FUNCTION__, audit_msg, page->gfn,
1498 page->role.word);
1499 }
1500}
1501
1502static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1503{
1504 int olddbg = dbg;
1505
1506 dbg = 0;
1507 audit_msg = msg;
1508 audit_rmap(vcpu);
1509 audit_write_protection(vcpu);
1510 audit_mappings(vcpu);
1511 dbg = olddbg;
1512}
1513
1514#endif