blob: f24b540148aa3def67b832e4dacbd3fefe6715e0 [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 */
19#include <linux/types.h>
20#include <linux/string.h>
21#include <asm/page.h>
22#include <linux/mm.h>
23#include <linux/highmem.h>
24#include <linux/module.h>
Avi Kivitye663ee62007-05-31 15:46:04 +030025#include <asm/cmpxchg.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026
27#include "vmx.h"
28#include "kvm.h"
29
Avi Kivity37a7d8b2007-01-05 16:36:56 -080030#undef MMU_DEBUG
31
32#undef AUDIT
33
34#ifdef AUDIT
35static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
36#else
37static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
38#endif
39
40#ifdef MMU_DEBUG
41
42#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
43#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
44
45#else
46
47#define pgprintk(x...) do { } while (0)
48#define rmap_printk(x...) do { } while (0)
49
50#endif
51
52#if defined(MMU_DEBUG) || defined(AUDIT)
53static int dbg = 1;
54#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080056#ifndef MMU_DEBUG
57#define ASSERT(x) do { } while (0)
58#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080059#define ASSERT(x) \
60 if (!(x)) { \
61 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
62 __FILE__, __LINE__, #x); \
63 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080064#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080065
Avi Kivitycea0f0e2007-01-05 16:36:43 -080066#define PT64_PT_BITS 9
67#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
68#define PT32_PT_BITS 10
69#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
71#define PT_WRITABLE_SHIFT 1
72
73#define PT_PRESENT_MASK (1ULL << 0)
74#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
75#define PT_USER_MASK (1ULL << 2)
76#define PT_PWT_MASK (1ULL << 3)
77#define PT_PCD_MASK (1ULL << 4)
78#define PT_ACCESSED_MASK (1ULL << 5)
79#define PT_DIRTY_MASK (1ULL << 6)
80#define PT_PAGE_SIZE_MASK (1ULL << 7)
81#define PT_PAT_MASK (1ULL << 7)
82#define PT_GLOBAL_MASK (1ULL << 8)
83#define PT64_NX_MASK (1ULL << 63)
84
85#define PT_PAT_SHIFT 7
86#define PT_DIR_PAT_SHIFT 12
87#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
88
89#define PT32_DIR_PSE36_SIZE 4
90#define PT32_DIR_PSE36_SHIFT 13
91#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
92
93
94#define PT32_PTE_COPY_MASK \
Avi Kivity8c7bb722006-12-13 00:34:02 -080095 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Avi Kivity8c7bb722006-12-13 00:34:02 -080097#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
99#define PT_FIRST_AVAIL_BITS_SHIFT 9
100#define PT64_SECOND_AVAIL_BITS_SHIFT 52
101
102#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
103#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
104
105#define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
106#define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
107
108#define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
109#define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
110
111#define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
112
113#define VALID_PAGE(x) ((x) != INVALID_PAGE)
114
115#define PT64_LEVEL_BITS 9
116
117#define PT64_LEVEL_SHIFT(level) \
118 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
119
120#define PT64_LEVEL_MASK(level) \
121 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
122
123#define PT64_INDEX(address, level)\
124 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
125
126
127#define PT32_LEVEL_BITS 10
128
129#define PT32_LEVEL_SHIFT(level) \
130 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
131
132#define PT32_LEVEL_MASK(level) \
133 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
134
135#define PT32_INDEX(address, level)\
136 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
137
138
Avi Kivity27aba762007-03-09 13:04:31 +0200139#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140#define PT64_DIR_BASE_ADDR_MASK \
141 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
142
143#define PT32_BASE_ADDR_MASK PAGE_MASK
144#define PT32_DIR_BASE_ADDR_MASK \
145 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
146
147
148#define PFERR_PRESENT_MASK (1U << 0)
149#define PFERR_WRITE_MASK (1U << 1)
150#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800151#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152
153#define PT64_ROOT_LEVEL 4
154#define PT32_ROOT_LEVEL 2
155#define PT32E_ROOT_LEVEL 3
156
157#define PT_DIRECTORY_LEVEL 2
158#define PT_PAGE_TABLE_LEVEL 1
159
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160#define RMAP_EXT 4
161
162struct kvm_rmap_desc {
163 u64 *shadow_ptes[RMAP_EXT];
164 struct kvm_rmap_desc *more;
165};
166
Avi Kivityb5a33a72007-04-15 16:31:09 +0300167static struct kmem_cache *pte_chain_cache;
168static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300169static struct kmem_cache *mmu_page_cache;
170static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300171
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172static int is_write_protection(struct kvm_vcpu *vcpu)
173{
174 return vcpu->cr0 & CR0_WP_MASK;
175}
176
177static int is_cpuid_PSE36(void)
178{
179 return 1;
180}
181
Avi Kivity73b10872007-01-26 00:56:41 -0800182static int is_nx(struct kvm_vcpu *vcpu)
183{
184 return vcpu->shadow_efer & EFER_NX;
185}
186
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187static int is_present_pte(unsigned long pte)
188{
189 return pte & PT_PRESENT_MASK;
190}
191
192static int is_writeble_pte(unsigned long pte)
193{
194 return pte & PT_WRITABLE_MASK;
195}
196
197static int is_io_pte(unsigned long pte)
198{
199 return pte & PT_SHADOW_IO_MARK;
200}
201
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800202static int is_rmap_pte(u64 pte)
203{
204 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
205 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
206}
207
Avi Kivitye663ee62007-05-31 15:46:04 +0300208static void set_shadow_pte(u64 *sptep, u64 spte)
209{
210#ifdef CONFIG_X86_64
211 set_64bit((unsigned long *)sptep, spte);
212#else
213 set_64bit((unsigned long long *)sptep, spte);
214#endif
215}
216
Avi Kivitye2dec932007-01-05 16:36:54 -0800217static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300218 struct kmem_cache *base_cache, int min,
219 gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800220{
221 void *obj;
222
223 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800224 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800225 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity8c438502007-04-16 11:53:17 +0300226 obj = kmem_cache_zalloc(base_cache, gfp_flags);
Avi Kivity714b93d2007-01-05 16:36:53 -0800227 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800228 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800229 cache->objects[cache->nobjs++] = obj;
230 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800231 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800232}
233
234static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
235{
236 while (mc->nobjs)
237 kfree(mc->objects[--mc->nobjs]);
238}
239
Avi Kivity8c438502007-04-16 11:53:17 +0300240static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800241{
Avi Kivitye2dec932007-01-05 16:36:54 -0800242 int r;
243
244 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300245 pte_chain_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800246 if (r)
247 goto out;
248 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300249 rmap_desc_cache, 1, gfp_flags);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300250 if (r)
251 goto out;
252 r = mmu_topup_memory_cache(&vcpu->mmu_page_cache,
253 mmu_page_cache, 4, gfp_flags);
254 if (r)
255 goto out;
256 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
257 mmu_page_header_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800258out:
259 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800260}
261
Avi Kivity8c438502007-04-16 11:53:17 +0300262static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
263{
264 int r;
265
266 r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
267 if (r < 0) {
268 spin_unlock(&vcpu->kvm->lock);
269 kvm_arch_ops->vcpu_put(vcpu);
270 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
271 kvm_arch_ops->vcpu_load(vcpu);
272 spin_lock(&vcpu->kvm->lock);
273 }
274 return r;
275}
276
Avi Kivity714b93d2007-01-05 16:36:53 -0800277static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
278{
279 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
280 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300281 mmu_free_memory_cache(&vcpu->mmu_page_cache);
282 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800283}
284
285static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
286 size_t size)
287{
288 void *p;
289
290 BUG_ON(!mc->nobjs);
291 p = mc->objects[--mc->nobjs];
292 memset(p, 0, size);
293 return p;
294}
295
296static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
297{
298 if (mc->nobjs < KVM_NR_MEM_OBJS)
299 mc->objects[mc->nobjs++] = obj;
300 else
301 kfree(obj);
302}
303
304static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
305{
306 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
307 sizeof(struct kvm_pte_chain));
308}
309
310static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
311 struct kvm_pte_chain *pc)
312{
313 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
314}
315
316static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
317{
318 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
319 sizeof(struct kvm_rmap_desc));
320}
321
322static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
323 struct kvm_rmap_desc *rd)
324{
325 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
326}
327
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800328/*
329 * Reverse mapping data structures:
330 *
331 * If page->private bit zero is zero, then page->private points to the
332 * shadow page table entry that points to page_address(page).
333 *
334 * If page->private bit zero is one, (then page->private & ~1) points
335 * to a struct kvm_rmap_desc containing more mappings.
336 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800337static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800338{
339 struct page *page;
340 struct kvm_rmap_desc *desc;
341 int i;
342
343 if (!is_rmap_pte(*spte))
344 return;
345 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200346 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800347 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200348 set_page_private(page,(unsigned long)spte);
349 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800351 desc = mmu_alloc_rmap_desc(vcpu);
Markus Rechberger5972e952007-02-19 14:37:47 +0200352 desc->shadow_ptes[0] = (u64 *)page_private(page);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800353 desc->shadow_ptes[1] = spte;
Markus Rechberger5972e952007-02-19 14:37:47 +0200354 set_page_private(page,(unsigned long)desc | 1);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800355 } else {
356 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200357 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800358 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
359 desc = desc->more;
360 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800361 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800362 desc = desc->more;
363 }
364 for (i = 0; desc->shadow_ptes[i]; ++i)
365 ;
366 desc->shadow_ptes[i] = spte;
367 }
368}
369
Avi Kivity714b93d2007-01-05 16:36:53 -0800370static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
371 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 Kivity714b93d2007-01-05 16:36:53 -0800391 mmu_free_rmap_desc(vcpu, desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800392}
393
Avi Kivity714b93d2007-01-05 16:36:53 -0800394static void rmap_remove(struct kvm_vcpu *vcpu, 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 Kivity714b93d2007-01-05 16:36:53 -0800422 rmap_desc_remove_entry(vcpu, page,
423 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 Kivity954bbbc2007-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 Kivity714b93d2007-01-05 16:36:53 -0800457 rmap_remove(vcpu, spte);
Avi Kivity40907d52007-01-05 16:36:55 -0800458 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivitye663ee62007-05-31 15:46:04 +0300459 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
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 Kivity4b02d6d2007-05-06 15:36:30 +0300479static void kvm_mmu_free_page(struct kvm_vcpu *vcpu,
480 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);
484 mmu_memory_cache_free(&vcpu->mmu_page_cache, page_head->spt);
485 mmu_memory_cache_free(&vcpu->mmu_page_header_cache, page_head);
Avi Kivity260746c2007-01-05 16:36:49 -0800486 ++vcpu->kvm->n_free_mmu_pages;
487}
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 Kivity714b93d2007-01-05 16:36:53 -0800552static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
553 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800554 u64 *parent_pte)
555{
556 struct kvm_pte_chain *pte_chain;
557 struct hlist_node *node;
558 int i;
559
560 if (!page->multimapped) {
561 BUG_ON(page->parent_pte != parent_pte);
562 page->parent_pte = NULL;
563 return;
564 }
565 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
566 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
567 if (!pte_chain->parent_ptes[i])
568 break;
569 if (pte_chain->parent_ptes[i] != parent_pte)
570 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800571 while (i + 1 < NR_PTE_CHAIN_ENTRIES
572 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800573 pte_chain->parent_ptes[i]
574 = pte_chain->parent_ptes[i + 1];
575 ++i;
576 }
577 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800578 if (i == 0) {
579 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800580 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800581 if (hlist_empty(&page->parent_ptes)) {
582 page->multimapped = 0;
583 page->parent_pte = NULL;
584 }
585 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800586 return;
587 }
588 BUG();
589}
590
591static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
592 gfn_t gfn)
593{
594 unsigned index;
595 struct hlist_head *bucket;
596 struct kvm_mmu_page *page;
597 struct hlist_node *node;
598
599 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
600 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
601 bucket = &vcpu->kvm->mmu_page_hash[index];
602 hlist_for_each_entry(page, node, bucket, hash_link)
603 if (page->gfn == gfn && !page->role.metaphysical) {
604 pgprintk("%s: found role %x\n",
605 __FUNCTION__, page->role.word);
606 return page;
607 }
608 return NULL;
609}
610
611static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
612 gfn_t gfn,
613 gva_t gaddr,
614 unsigned level,
615 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200616 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800617 u64 *parent_pte)
618{
619 union kvm_mmu_page_role role;
620 unsigned index;
621 unsigned quadrant;
622 struct hlist_head *bucket;
623 struct kvm_mmu_page *page;
624 struct hlist_node *node;
625
626 role.word = 0;
627 role.glevels = vcpu->mmu.root_level;
628 role.level = level;
629 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200630 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800631 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
632 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
633 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
634 role.quadrant = quadrant;
635 }
636 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
637 gfn, role.word);
638 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
639 bucket = &vcpu->kvm->mmu_page_hash[index];
640 hlist_for_each_entry(page, node, bucket, hash_link)
641 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800642 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800643 pgprintk("%s: found\n", __FUNCTION__);
644 return page;
645 }
646 page = kvm_mmu_alloc_page(vcpu, parent_pte);
647 if (!page)
648 return page;
649 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
650 page->gfn = gfn;
651 page->role = role;
652 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800653 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800654 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800655 return page;
656}
657
Avi Kivitya4360362007-01-05 16:36:45 -0800658static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
659 struct kvm_mmu_page *page)
660{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800661 unsigned i;
662 u64 *pt;
663 u64 ent;
664
Avi Kivity47ad8e62007-05-06 15:50:58 +0300665 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800666
667 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
668 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
669 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800670 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800671 pt[i] = 0;
672 }
Avi Kivity40907d52007-01-05 16:36:55 -0800673 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800674 return;
675 }
676
677 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
678 ent = pt[i];
679
680 pt[i] = 0;
681 if (!(ent & PT_PRESENT_MASK))
682 continue;
683 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800684 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800685 }
Avi Kivitya4360362007-01-05 16:36:45 -0800686}
687
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800688static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
689 struct kvm_mmu_page *page,
690 u64 *parent_pte)
691{
Avi Kivity714b93d2007-01-05 16:36:53 -0800692 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800693}
694
695static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
696 struct kvm_mmu_page *page)
697{
698 u64 *parent_pte;
699
700 while (page->multimapped || page->parent_pte) {
701 if (!page->multimapped)
702 parent_pte = page->parent_pte;
703 else {
704 struct kvm_pte_chain *chain;
705
706 chain = container_of(page->parent_ptes.first,
707 struct kvm_pte_chain, link);
708 parent_pte = chain->parent_ptes[0];
709 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800710 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800711 kvm_mmu_put_page(vcpu, page, parent_pte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300712 set_shadow_pte(parent_pte, 0);
Avi Kivitya4360362007-01-05 16:36:45 -0800713 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800714 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800715 if (!page->root_count) {
716 hlist_del(&page->hash_link);
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300717 kvm_mmu_free_page(vcpu, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200718 } else
719 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800720}
721
722static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
723{
724 unsigned index;
725 struct hlist_head *bucket;
726 struct kvm_mmu_page *page;
727 struct hlist_node *node, *n;
728 int r;
729
730 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
731 r = 0;
732 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
733 bucket = &vcpu->kvm->mmu_page_hash[index];
734 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
735 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800736 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
737 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800738 kvm_mmu_zap_page(vcpu, page);
739 r = 1;
740 }
741 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800742}
743
Avi Kivity97a0a012007-05-31 15:08:29 +0300744static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn)
745{
746 struct kvm_mmu_page *page;
747
748 while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) {
749 pgprintk("%s: zap %lx %x\n",
750 __FUNCTION__, gfn, page->role.word);
751 kvm_mmu_zap_page(vcpu, page);
752 }
753}
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
756{
757 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
758 struct kvm_mmu_page *page_head = page_header(__pa(pte));
759
760 __set_bit(slot, &page_head->slot_bitmap);
761}
762
763hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
764{
765 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
766
767 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
768}
769
770hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
771{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 struct page *page;
773
774 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300775 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
776 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
779 | (gpa & (PAGE_SIZE-1));
780}
781
782hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
783{
784 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
785
786 if (gpa == UNMAPPED_GVA)
787 return UNMAPPED_GVA;
788 return gpa_to_hpa(vcpu, gpa);
789}
790
Avi Kivity039576c2007-03-20 12:46:50 +0200791struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
792{
793 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
794
795 if (gpa == UNMAPPED_GVA)
796 return NULL;
797 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
798}
799
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
801{
802}
803
804static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
805{
806 int level = PT32E_ROOT_LEVEL;
807 hpa_t table_addr = vcpu->mmu.root_hpa;
808
809 for (; ; level--) {
810 u32 index = PT64_INDEX(v, level);
811 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800812 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813
814 ASSERT(VALID_PAGE(table_addr));
815 table = __va(table_addr);
816
817 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800818 pte = table[index];
819 if (is_present_pte(pte) && is_writeble_pte(pte))
820 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
822 page_header_update_slot(vcpu->kvm, table, v);
823 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
824 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800825 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826 return 0;
827 }
828
829 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800830 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800831 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800833 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
834 >> PAGE_SHIFT;
835 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
836 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200837 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800838 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 pgprintk("nonpaging_map: ENOMEM\n");
840 return -ENOMEM;
841 }
842
Avi Kivity47ad8e62007-05-06 15:50:58 +0300843 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800844 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 }
846 table_addr = table[index] & PT64_BASE_ADDR_MASK;
847 }
848}
849
Avi Kivity17ac10a2007-01-05 16:36:40 -0800850static void mmu_free_roots(struct kvm_vcpu *vcpu)
851{
852 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800853 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800854
855#ifdef CONFIG_X86_64
856 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
857 hpa_t root = vcpu->mmu.root_hpa;
858
859 ASSERT(VALID_PAGE(root));
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) {
870 ASSERT(VALID_PAGE(root));
871 root &= PT64_BASE_ADDR_MASK;
872 page = page_header(root);
873 --page->root_count;
874 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800875 vcpu->mmu.pae_root[i] = INVALID_PAGE;
876 }
877 vcpu->mmu.root_hpa = INVALID_PAGE;
878}
879
880static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
881{
882 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800883 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800884 struct kvm_mmu_page *page;
885
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800886 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800887
888#ifdef CONFIG_X86_64
889 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
890 hpa_t root = vcpu->mmu.root_hpa;
891
892 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800893 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200894 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300895 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800896 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800897 vcpu->mmu.root_hpa = root;
898 return;
899 }
900#endif
901 for (i = 0; i < 4; ++i) {
902 hpa_t root = vcpu->mmu.pae_root[i];
903
904 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300905 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
906 if (!is_present_pte(vcpu->pdptrs[i])) {
907 vcpu->mmu.pae_root[i] = 0;
908 continue;
909 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800910 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300911 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800912 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800913 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800914 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200915 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300916 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800917 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800918 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
919 }
920 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
921}
922
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
924{
925 return vaddr;
926}
927
928static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
929 u32 error_code)
930{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800932 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800933 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Avi Kivitye2dec932007-01-05 16:36:54 -0800935 r = mmu_topup_memory_caches(vcpu);
936 if (r)
937 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800938
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 ASSERT(vcpu);
940 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
941
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942
Avi Kivityebeace82007-01-05 16:36:47 -0800943 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Avi Kivityebeace82007-01-05 16:36:47 -0800945 if (is_error_hpa(paddr))
946 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947
Avi Kivityebeace82007-01-05 16:36:47 -0800948 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949}
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951static void nonpaging_free(struct kvm_vcpu *vcpu)
952{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800953 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954}
955
956static int nonpaging_init_context(struct kvm_vcpu *vcpu)
957{
958 struct kvm_mmu *context = &vcpu->mmu;
959
960 context->new_cr3 = nonpaging_new_cr3;
961 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 context->gva_to_gpa = nonpaging_gva_to_gpa;
963 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800966 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 ASSERT(VALID_PAGE(context->root_hpa));
968 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
969 return 0;
970}
971
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
973{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300974 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 kvm_arch_ops->tlb_flush(vcpu);
976}
977
978static void paging_new_cr3(struct kvm_vcpu *vcpu)
979{
Avi Kivity374cbac2007-01-05 16:36:43 -0800980 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800981 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800982 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
983 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800986 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987}
988
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989static void inject_page_fault(struct kvm_vcpu *vcpu,
990 u64 addr,
991 u32 err_code)
992{
993 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
994}
995
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996static void paging_free(struct kvm_vcpu *vcpu)
997{
998 nonpaging_free(vcpu);
999}
1000
1001#define PTTYPE 64
1002#include "paging_tmpl.h"
1003#undef PTTYPE
1004
1005#define PTTYPE 32
1006#include "paging_tmpl.h"
1007#undef PTTYPE
1008
Avi Kivity17ac10a2007-01-05 16:36:40 -08001009static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010{
1011 struct kvm_mmu *context = &vcpu->mmu;
1012
1013 ASSERT(is_pae(vcpu));
1014 context->new_cr3 = paging_new_cr3;
1015 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 context->gva_to_gpa = paging64_gva_to_gpa;
1017 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001018 context->root_level = level;
1019 context->shadow_root_level = level;
1020 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 ASSERT(VALID_PAGE(context->root_hpa));
1022 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1023 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1024 return 0;
1025}
1026
Avi Kivity17ac10a2007-01-05 16:36:40 -08001027static int paging64_init_context(struct kvm_vcpu *vcpu)
1028{
1029 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1030}
1031
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032static int paging32_init_context(struct kvm_vcpu *vcpu)
1033{
1034 struct kvm_mmu *context = &vcpu->mmu;
1035
1036 context->new_cr3 = paging_new_cr3;
1037 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 context->gva_to_gpa = paging32_gva_to_gpa;
1039 context->free = paging_free;
1040 context->root_level = PT32_ROOT_LEVEL;
1041 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001042 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 ASSERT(VALID_PAGE(context->root_hpa));
1044 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1045 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1046 return 0;
1047}
1048
1049static int paging32E_init_context(struct kvm_vcpu *vcpu)
1050{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001051 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052}
1053
1054static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1055{
1056 ASSERT(vcpu);
1057 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1058
Avi Kivityd3d25b02007-05-30 12:34:53 +03001059 mmu_topup_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 if (!is_paging(vcpu))
1061 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001062 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 return paging64_init_context(vcpu);
1064 else if (is_pae(vcpu))
1065 return paging32E_init_context(vcpu);
1066 else
1067 return paging32_init_context(vcpu);
1068}
1069
1070static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1071{
1072 ASSERT(vcpu);
1073 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1074 vcpu->mmu.free(vcpu);
1075 vcpu->mmu.root_hpa = INVALID_PAGE;
1076 }
1077}
1078
1079int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1080{
Avi Kivity714b93d2007-01-05 16:36:53 -08001081 int r;
1082
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001084 r = init_kvm_mmu(vcpu);
1085 if (r < 0)
1086 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001087 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001088out:
1089 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090}
1091
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)
1102 rmap_remove(vcpu, spte);
1103 else {
1104 child = page_header(pte & PT64_BASE_ADDR_MASK);
1105 mmu_page_remove_parent_pte(vcpu, child, spte);
1106 }
1107 }
1108 *spte = 0;
1109}
1110
Avi Kivity00284252007-05-01 16:53:31 +03001111static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1112 struct kvm_mmu_page *page,
1113 u64 *spte,
1114 const void *new, int bytes)
1115{
1116 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1117 return;
1118
1119 if (page->role.glevels == PT32_ROOT_LEVEL)
1120 paging32_update_pte(vcpu, page, spte, new, bytes);
1121 else
1122 paging64_update_pte(vcpu, page, spte, new, bytes);
1123}
1124
Avi Kivity09072da2007-05-01 14:16:52 +03001125void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1126 const u8 *old, const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001127{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001128 gfn_t gfn = gpa >> PAGE_SHIFT;
1129 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001130 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001131 struct hlist_head *bucket;
1132 unsigned index;
1133 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001134 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001135 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001136 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001137 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001138 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001139 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001140 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001141 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001142
Avi Kivityda4a00f2007-01-05 16:36:44 -08001143 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001144 if (gfn == vcpu->last_pt_write_gfn) {
1145 ++vcpu->last_pt_write_count;
1146 if (vcpu->last_pt_write_count >= 3)
1147 flooded = 1;
1148 } else {
1149 vcpu->last_pt_write_gfn = gfn;
1150 vcpu->last_pt_write_count = 1;
1151 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001152 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1153 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001154 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001155 if (page->gfn != gfn || page->role.metaphysical)
1156 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001157 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1158 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001159 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001160 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001161 /*
1162 * Misaligned accesses are too much trouble to fix
1163 * up; also, they usually indicate a page is not used
1164 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001165 *
1166 * If we're seeing too many writes to a page,
1167 * it may no longer be a page table, or we may be
1168 * forking, in which case it is better to unmap the
1169 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001170 */
1171 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1172 gpa, bytes, page->role.word);
1173 kvm_mmu_zap_page(vcpu, page);
1174 continue;
1175 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001176 page_offset = offset;
1177 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001178 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001179 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001180 page_offset <<= 1; /* 32->64 */
1181 /*
1182 * A 32-bit pde maps 4MB while the shadow pdes map
1183 * only 2MB. So we need to double the offset again
1184 * and zap two pdes instead of one.
1185 */
1186 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001187 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001188 page_offset <<= 1;
1189 npte = 2;
1190 }
Avi Kivityfce06572007-05-01 16:44:05 +03001191 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001192 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001193 if (quadrant != page->role.quadrant)
1194 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001195 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001196 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001197 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001198 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivity00284252007-05-01 16:53:31 +03001199 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
Avi Kivityac1b7142007-03-08 17:13:32 +02001200 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001201 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001202 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001203}
1204
Avi Kivitya4360362007-01-05 16:36:45 -08001205int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1206{
1207 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1208
1209 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1210}
1211
Avi Kivityebeace82007-01-05 16:36:47 -08001212void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1213{
1214 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1215 struct kvm_mmu_page *page;
1216
1217 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1218 struct kvm_mmu_page, link);
1219 kvm_mmu_zap_page(vcpu, page);
1220 }
1221}
1222EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1223
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);
1231 kvm_mmu_zap_page(vcpu, page);
1232 }
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 Kivity714b93d2007-01-05 16:36:53 -08001289void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290{
Avi Kivity714b93d2007-01-05 16:36:53 -08001291 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292 struct kvm_mmu_page *page;
1293
1294 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1295 int i;
1296 u64 *pt;
1297
1298 if (!test_bit(slot, &page->slot_bitmap))
1299 continue;
1300
Avi Kivity47ad8e62007-05-06 15:50:58 +03001301 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1303 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001304 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001305 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001307 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 }
1309}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001310
Dor Laore0fa8262007-03-30 13:06:33 +03001311void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1312{
1313 destroy_kvm_mmu(vcpu);
1314
1315 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1316 struct kvm_mmu_page *page;
1317
1318 page = container_of(vcpu->kvm->active_mmu_pages.next,
1319 struct kvm_mmu_page, link);
1320 kvm_mmu_zap_page(vcpu, page);
1321 }
1322
1323 mmu_free_memory_caches(vcpu);
1324 kvm_arch_ops->tlb_flush(vcpu);
1325 init_kvm_mmu(vcpu);
1326}
1327
Avi Kivityb5a33a72007-04-15 16:31:09 +03001328void kvm_mmu_module_exit(void)
1329{
1330 if (pte_chain_cache)
1331 kmem_cache_destroy(pte_chain_cache);
1332 if (rmap_desc_cache)
1333 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001334 if (mmu_page_cache)
1335 kmem_cache_destroy(mmu_page_cache);
1336 if (mmu_page_header_cache)
1337 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001338}
1339
1340int kvm_mmu_module_init(void)
1341{
1342 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1343 sizeof(struct kvm_pte_chain),
1344 0, 0, NULL, NULL);
1345 if (!pte_chain_cache)
1346 goto nomem;
1347 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1348 sizeof(struct kvm_rmap_desc),
1349 0, 0, NULL, NULL);
1350 if (!rmap_desc_cache)
1351 goto nomem;
1352
Avi Kivityd3d25b02007-05-30 12:34:53 +03001353 mmu_page_cache = kmem_cache_create("kvm_mmu_page",
1354 PAGE_SIZE,
1355 PAGE_SIZE, 0, NULL, NULL);
1356 if (!mmu_page_cache)
1357 goto nomem;
1358
1359 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1360 sizeof(struct kvm_mmu_page),
1361 0, 0, NULL, NULL);
1362 if (!mmu_page_header_cache)
1363 goto nomem;
1364
Avi Kivityb5a33a72007-04-15 16:31:09 +03001365 return 0;
1366
1367nomem:
1368 kvm_mmu_module_exit();
1369 return -ENOMEM;
1370}
1371
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001372#ifdef AUDIT
1373
1374static const char *audit_msg;
1375
1376static gva_t canonicalize(gva_t gva)
1377{
1378#ifdef CONFIG_X86_64
1379 gva = (long long)(gva << 16) >> 16;
1380#endif
1381 return gva;
1382}
1383
1384static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1385 gva_t va, int level)
1386{
1387 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1388 int i;
1389 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1390
1391 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1392 u64 ent = pt[i];
1393
Adrian Bunk28076962007-04-28 21:20:48 +02001394 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001395 continue;
1396
1397 va = canonicalize(va);
1398 if (level > 1)
1399 audit_mappings_page(vcpu, ent, va, level - 1);
1400 else {
1401 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1402 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1403
1404 if ((ent & PT_PRESENT_MASK)
1405 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1406 printk(KERN_ERR "audit error: (%s) levels %d"
1407 " gva %lx gpa %llx hpa %llx ent %llx\n",
1408 audit_msg, vcpu->mmu.root_level,
1409 va, gpa, hpa, ent);
1410 }
1411 }
1412}
1413
1414static void audit_mappings(struct kvm_vcpu *vcpu)
1415{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001416 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001417
1418 if (vcpu->mmu.root_level == 4)
1419 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1420 else
1421 for (i = 0; i < 4; ++i)
1422 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1423 audit_mappings_page(vcpu,
1424 vcpu->mmu.pae_root[i],
1425 i << 30,
1426 2);
1427}
1428
1429static int count_rmaps(struct kvm_vcpu *vcpu)
1430{
1431 int nmaps = 0;
1432 int i, j, k;
1433
1434 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1435 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1436 struct kvm_rmap_desc *d;
1437
1438 for (j = 0; j < m->npages; ++j) {
1439 struct page *page = m->phys_mem[j];
1440
1441 if (!page->private)
1442 continue;
1443 if (!(page->private & 1)) {
1444 ++nmaps;
1445 continue;
1446 }
1447 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1448 while (d) {
1449 for (k = 0; k < RMAP_EXT; ++k)
1450 if (d->shadow_ptes[k])
1451 ++nmaps;
1452 else
1453 break;
1454 d = d->more;
1455 }
1456 }
1457 }
1458 return nmaps;
1459}
1460
1461static int count_writable_mappings(struct kvm_vcpu *vcpu)
1462{
1463 int nmaps = 0;
1464 struct kvm_mmu_page *page;
1465 int i;
1466
1467 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001468 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001469
1470 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1471 continue;
1472
1473 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1474 u64 ent = pt[i];
1475
1476 if (!(ent & PT_PRESENT_MASK))
1477 continue;
1478 if (!(ent & PT_WRITABLE_MASK))
1479 continue;
1480 ++nmaps;
1481 }
1482 }
1483 return nmaps;
1484}
1485
1486static void audit_rmap(struct kvm_vcpu *vcpu)
1487{
1488 int n_rmap = count_rmaps(vcpu);
1489 int n_actual = count_writable_mappings(vcpu);
1490
1491 if (n_rmap != n_actual)
1492 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1493 __FUNCTION__, audit_msg, n_rmap, n_actual);
1494}
1495
1496static void audit_write_protection(struct kvm_vcpu *vcpu)
1497{
1498 struct kvm_mmu_page *page;
1499
1500 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1501 hfn_t hfn;
1502 struct page *pg;
1503
1504 if (page->role.metaphysical)
1505 continue;
1506
1507 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1508 >> PAGE_SHIFT;
1509 pg = pfn_to_page(hfn);
1510 if (pg->private)
1511 printk(KERN_ERR "%s: (%s) shadow page has writable"
1512 " mappings: gfn %lx role %x\n",
1513 __FUNCTION__, audit_msg, page->gfn,
1514 page->role.word);
1515 }
1516}
1517
1518static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1519{
1520 int olddbg = dbg;
1521
1522 dbg = 0;
1523 audit_msg = msg;
1524 audit_rmap(vcpu);
1525 audit_write_protection(vcpu);
1526 audit_mappings(vcpu);
1527 dbg = olddbg;
1528}
1529
1530#endif