blob: e8e228118de9be496660ac8fa9ece297a39c1b57 [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>
25
26#include "vmx.h"
27#include "kvm.h"
28
Avi Kivity37a7d8b2007-01-05 16:36:56 -080029#undef MMU_DEBUG
30
31#undef AUDIT
32
33#ifdef AUDIT
34static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
35#else
36static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
37#endif
38
39#ifdef MMU_DEBUG
40
41#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
42#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
43
44#else
45
46#define pgprintk(x...) do { } while (0)
47#define rmap_printk(x...) do { } while (0)
48
49#endif
50
51#if defined(MMU_DEBUG) || defined(AUDIT)
52static int dbg = 1;
53#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080055#ifndef MMU_DEBUG
56#define ASSERT(x) do { } while (0)
57#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080058#define ASSERT(x) \
59 if (!(x)) { \
60 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
61 __FILE__, __LINE__, #x); \
62 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080063#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080064
Avi Kivitycea0f0e2007-01-05 16:36:43 -080065#define PT64_PT_BITS 9
66#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
67#define PT32_PT_BITS 10
68#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080069
70#define PT_WRITABLE_SHIFT 1
71
72#define PT_PRESENT_MASK (1ULL << 0)
73#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
74#define PT_USER_MASK (1ULL << 2)
75#define PT_PWT_MASK (1ULL << 3)
76#define PT_PCD_MASK (1ULL << 4)
77#define PT_ACCESSED_MASK (1ULL << 5)
78#define PT_DIRTY_MASK (1ULL << 6)
79#define PT_PAGE_SIZE_MASK (1ULL << 7)
80#define PT_PAT_MASK (1ULL << 7)
81#define PT_GLOBAL_MASK (1ULL << 8)
82#define PT64_NX_MASK (1ULL << 63)
83
84#define PT_PAT_SHIFT 7
85#define PT_DIR_PAT_SHIFT 12
86#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
87
88#define PT32_DIR_PSE36_SIZE 4
89#define PT32_DIR_PSE36_SHIFT 13
90#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
91
92
93#define PT32_PTE_COPY_MASK \
Avi Kivity8c7bb722006-12-13 00:34:02 -080094 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
Avi Kivity8c7bb722006-12-13 00:34:02 -080096#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
98#define PT_FIRST_AVAIL_BITS_SHIFT 9
99#define PT64_SECOND_AVAIL_BITS_SHIFT 52
100
101#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
102#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
103
104#define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
105#define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
106
107#define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
108#define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
109
110#define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
111
112#define VALID_PAGE(x) ((x) != INVALID_PAGE)
113
114#define PT64_LEVEL_BITS 9
115
116#define PT64_LEVEL_SHIFT(level) \
117 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
118
119#define PT64_LEVEL_MASK(level) \
120 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
121
122#define PT64_INDEX(address, level)\
123 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
124
125
126#define PT32_LEVEL_BITS 10
127
128#define PT32_LEVEL_SHIFT(level) \
129 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
130
131#define PT32_LEVEL_MASK(level) \
132 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
133
134#define PT32_INDEX(address, level)\
135 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
136
137
Avi Kivity27aba762007-03-09 13:04:31 +0200138#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139#define PT64_DIR_BASE_ADDR_MASK \
140 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
141
142#define PT32_BASE_ADDR_MASK PAGE_MASK
143#define PT32_DIR_BASE_ADDR_MASK \
144 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
145
146
147#define PFERR_PRESENT_MASK (1U << 0)
148#define PFERR_WRITE_MASK (1U << 1)
149#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800150#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151
152#define PT64_ROOT_LEVEL 4
153#define PT32_ROOT_LEVEL 2
154#define PT32E_ROOT_LEVEL 3
155
156#define PT_DIRECTORY_LEVEL 2
157#define PT_PAGE_TABLE_LEVEL 1
158
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800159#define RMAP_EXT 4
160
161struct kvm_rmap_desc {
162 u64 *shadow_ptes[RMAP_EXT];
163 struct kvm_rmap_desc *more;
164};
165
Avi Kivityb5a33a72007-04-15 16:31:09 +0300166static struct kmem_cache *pte_chain_cache;
167static struct kmem_cache *rmap_desc_cache;
168
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static int is_write_protection(struct kvm_vcpu *vcpu)
170{
171 return vcpu->cr0 & CR0_WP_MASK;
172}
173
174static int is_cpuid_PSE36(void)
175{
176 return 1;
177}
178
Avi Kivity73b10872007-01-26 00:56:41 -0800179static int is_nx(struct kvm_vcpu *vcpu)
180{
181 return vcpu->shadow_efer & EFER_NX;
182}
183
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184static int is_present_pte(unsigned long pte)
185{
186 return pte & PT_PRESENT_MASK;
187}
188
189static int is_writeble_pte(unsigned long pte)
190{
191 return pte & PT_WRITABLE_MASK;
192}
193
194static int is_io_pte(unsigned long pte)
195{
196 return pte & PT_SHADOW_IO_MARK;
197}
198
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800199static int is_rmap_pte(u64 pte)
200{
201 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
202 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
203}
204
Avi Kivitye2dec932007-01-05 16:36:54 -0800205static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300206 struct kmem_cache *base_cache, int min,
207 gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800208{
209 void *obj;
210
211 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800212 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800213 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity8c438502007-04-16 11:53:17 +0300214 obj = kmem_cache_zalloc(base_cache, gfp_flags);
Avi Kivity714b93d2007-01-05 16:36:53 -0800215 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800216 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800217 cache->objects[cache->nobjs++] = obj;
218 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800219 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800220}
221
222static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
223{
224 while (mc->nobjs)
225 kfree(mc->objects[--mc->nobjs]);
226}
227
Avi Kivity8c438502007-04-16 11:53:17 +0300228static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
Avi Kivity714b93d2007-01-05 16:36:53 -0800229{
Avi Kivitye2dec932007-01-05 16:36:54 -0800230 int r;
231
232 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300233 pte_chain_cache, 4, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800234 if (r)
235 goto out;
236 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
Avi Kivity8c438502007-04-16 11:53:17 +0300237 rmap_desc_cache, 1, gfp_flags);
Avi Kivitye2dec932007-01-05 16:36:54 -0800238out:
239 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800240}
241
Avi Kivity8c438502007-04-16 11:53:17 +0300242static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
243{
244 int r;
245
246 r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
247 if (r < 0) {
248 spin_unlock(&vcpu->kvm->lock);
249 kvm_arch_ops->vcpu_put(vcpu);
250 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
251 kvm_arch_ops->vcpu_load(vcpu);
252 spin_lock(&vcpu->kvm->lock);
253 }
254 return r;
255}
256
Avi Kivity714b93d2007-01-05 16:36:53 -0800257static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
258{
259 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
260 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
261}
262
263static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
264 size_t size)
265{
266 void *p;
267
268 BUG_ON(!mc->nobjs);
269 p = mc->objects[--mc->nobjs];
270 memset(p, 0, size);
271 return p;
272}
273
274static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
275{
276 if (mc->nobjs < KVM_NR_MEM_OBJS)
277 mc->objects[mc->nobjs++] = obj;
278 else
279 kfree(obj);
280}
281
282static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
283{
284 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
285 sizeof(struct kvm_pte_chain));
286}
287
288static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
289 struct kvm_pte_chain *pc)
290{
291 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
292}
293
294static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
295{
296 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
297 sizeof(struct kvm_rmap_desc));
298}
299
300static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
301 struct kvm_rmap_desc *rd)
302{
303 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
304}
305
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800306/*
307 * Reverse mapping data structures:
308 *
309 * If page->private bit zero is zero, then page->private points to the
310 * shadow page table entry that points to page_address(page).
311 *
312 * If page->private bit zero is one, (then page->private & ~1) points
313 * to a struct kvm_rmap_desc containing more mappings.
314 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800315static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800316{
317 struct page *page;
318 struct kvm_rmap_desc *desc;
319 int i;
320
321 if (!is_rmap_pte(*spte))
322 return;
323 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200324 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800325 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200326 set_page_private(page,(unsigned long)spte);
327 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800328 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800329 desc = mmu_alloc_rmap_desc(vcpu);
Markus Rechberger5972e952007-02-19 14:37:47 +0200330 desc->shadow_ptes[0] = (u64 *)page_private(page);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800331 desc->shadow_ptes[1] = spte;
Markus Rechberger5972e952007-02-19 14:37:47 +0200332 set_page_private(page,(unsigned long)desc | 1);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800333 } else {
334 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200335 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800336 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
337 desc = desc->more;
338 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800339 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800340 desc = desc->more;
341 }
342 for (i = 0; desc->shadow_ptes[i]; ++i)
343 ;
344 desc->shadow_ptes[i] = spte;
345 }
346}
347
Avi Kivity714b93d2007-01-05 16:36:53 -0800348static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
349 struct page *page,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350 struct kvm_rmap_desc *desc,
351 int i,
352 struct kvm_rmap_desc *prev_desc)
353{
354 int j;
355
356 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
357 ;
358 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000359 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800360 if (j != 0)
361 return;
362 if (!prev_desc && !desc->more)
Markus Rechberger5972e952007-02-19 14:37:47 +0200363 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800364 else
365 if (prev_desc)
366 prev_desc->more = desc->more;
367 else
Markus Rechberger5972e952007-02-19 14:37:47 +0200368 set_page_private(page,(unsigned long)desc->more | 1);
Avi Kivity714b93d2007-01-05 16:36:53 -0800369 mmu_free_rmap_desc(vcpu, desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370}
371
Avi Kivity714b93d2007-01-05 16:36:53 -0800372static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373{
374 struct page *page;
375 struct kvm_rmap_desc *desc;
376 struct kvm_rmap_desc *prev_desc;
377 int i;
378
379 if (!is_rmap_pte(*spte))
380 return;
381 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Markus Rechberger5972e952007-02-19 14:37:47 +0200382 if (!page_private(page)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800383 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
384 BUG();
Markus Rechberger5972e952007-02-19 14:37:47 +0200385 } else if (!(page_private(page) & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800386 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200387 if ((u64 *)page_private(page) != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800388 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
389 spte, *spte);
390 BUG();
391 }
Markus Rechberger5972e952007-02-19 14:37:47 +0200392 set_page_private(page,0);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800393 } else {
394 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Markus Rechberger5972e952007-02-19 14:37:47 +0200395 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800396 prev_desc = NULL;
397 while (desc) {
398 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
399 if (desc->shadow_ptes[i] == spte) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800400 rmap_desc_remove_entry(vcpu, page,
401 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800402 prev_desc);
403 return;
404 }
405 prev_desc = desc;
406 desc = desc->more;
407 }
408 BUG();
409 }
410}
411
Avi Kivity714b93d2007-01-05 16:36:53 -0800412static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800413{
Avi Kivity714b93d2007-01-05 16:36:53 -0800414 struct kvm *kvm = vcpu->kvm;
Avi Kivity374cbac2007-01-05 16:36:43 -0800415 struct page *page;
Avi Kivity374cbac2007-01-05 16:36:43 -0800416 struct kvm_rmap_desc *desc;
417 u64 *spte;
418
Avi Kivity954bbbc2007-03-30 14:02:32 +0300419 page = gfn_to_page(kvm, gfn);
420 BUG_ON(!page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800421
Markus Rechberger5972e952007-02-19 14:37:47 +0200422 while (page_private(page)) {
423 if (!(page_private(page) & 1))
424 spte = (u64 *)page_private(page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800425 else {
Markus Rechberger5972e952007-02-19 14:37:47 +0200426 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800427 spte = desc->shadow_ptes[0];
428 }
429 BUG_ON(!spte);
Avi Kivity27aba762007-03-09 13:04:31 +0200430 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
431 != page_to_pfn(page));
Avi Kivity374cbac2007-01-05 16:36:43 -0800432 BUG_ON(!(*spte & PT_PRESENT_MASK));
433 BUG_ON(!(*spte & PT_WRITABLE_MASK));
434 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800435 rmap_remove(vcpu, spte);
Avi Kivity40907d52007-01-05 16:36:55 -0800436 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity374cbac2007-01-05 16:36:43 -0800437 *spte &= ~(u64)PT_WRITABLE_MASK;
438 }
439}
440
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800441#ifdef MMU_DEBUG
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442static int is_empty_shadow_page(hpa_t page_hpa)
443{
Avi Kivity139bdb22007-01-05 16:36:50 -0800444 u64 *pos;
445 u64 *end;
446
447 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800449 if (*pos != 0) {
450 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
451 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800453 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 return 1;
455}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800456#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Avi Kivity260746c2007-01-05 16:36:49 -0800458static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
459{
460 struct kvm_mmu_page *page_head = page_header(page_hpa);
461
Avi Kivity5f1e0b62007-01-05 16:36:49 -0800462 ASSERT(is_empty_shadow_page(page_hpa));
Avi Kivity260746c2007-01-05 16:36:49 -0800463 page_head->page_hpa = page_hpa;
Avi Kivity36868f72007-03-26 19:31:52 +0200464 list_move(&page_head->link, &vcpu->free_pages);
Avi Kivity260746c2007-01-05 16:36:49 -0800465 ++vcpu->kvm->n_free_mmu_pages;
466}
467
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800468static unsigned kvm_page_table_hashfn(gfn_t gfn)
469{
470 return gfn;
471}
472
Avi Kivity25c0de22007-01-05 16:36:42 -0800473static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
474 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475{
476 struct kvm_mmu_page *page;
477
478 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800479 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480
481 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
Avi Kivity36868f72007-03-26 19:31:52 +0200482 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 ASSERT(is_empty_shadow_page(page->page_hpa));
484 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800485 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800487 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800488 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489}
490
Avi Kivity714b93d2007-01-05 16:36:53 -0800491static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
492 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800493{
494 struct kvm_pte_chain *pte_chain;
495 struct hlist_node *node;
496 int i;
497
498 if (!parent_pte)
499 return;
500 if (!page->multimapped) {
501 u64 *old = page->parent_pte;
502
503 if (!old) {
504 page->parent_pte = parent_pte;
505 return;
506 }
507 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800508 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800509 INIT_HLIST_HEAD(&page->parent_ptes);
510 hlist_add_head(&pte_chain->link, &page->parent_ptes);
511 pte_chain->parent_ptes[0] = old;
512 }
513 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
514 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
515 continue;
516 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
517 if (!pte_chain->parent_ptes[i]) {
518 pte_chain->parent_ptes[i] = parent_pte;
519 return;
520 }
521 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800522 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800523 BUG_ON(!pte_chain);
524 hlist_add_head(&pte_chain->link, &page->parent_ptes);
525 pte_chain->parent_ptes[0] = parent_pte;
526}
527
Avi Kivity714b93d2007-01-05 16:36:53 -0800528static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
529 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800530 u64 *parent_pte)
531{
532 struct kvm_pte_chain *pte_chain;
533 struct hlist_node *node;
534 int i;
535
536 if (!page->multimapped) {
537 BUG_ON(page->parent_pte != parent_pte);
538 page->parent_pte = NULL;
539 return;
540 }
541 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
542 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
543 if (!pte_chain->parent_ptes[i])
544 break;
545 if (pte_chain->parent_ptes[i] != parent_pte)
546 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800547 while (i + 1 < NR_PTE_CHAIN_ENTRIES
548 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800549 pte_chain->parent_ptes[i]
550 = pte_chain->parent_ptes[i + 1];
551 ++i;
552 }
553 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800554 if (i == 0) {
555 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800556 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800557 if (hlist_empty(&page->parent_ptes)) {
558 page->multimapped = 0;
559 page->parent_pte = NULL;
560 }
561 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800562 return;
563 }
564 BUG();
565}
566
567static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
568 gfn_t gfn)
569{
570 unsigned index;
571 struct hlist_head *bucket;
572 struct kvm_mmu_page *page;
573 struct hlist_node *node;
574
575 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
576 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
577 bucket = &vcpu->kvm->mmu_page_hash[index];
578 hlist_for_each_entry(page, node, bucket, hash_link)
579 if (page->gfn == gfn && !page->role.metaphysical) {
580 pgprintk("%s: found role %x\n",
581 __FUNCTION__, page->role.word);
582 return page;
583 }
584 return NULL;
585}
586
587static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
588 gfn_t gfn,
589 gva_t gaddr,
590 unsigned level,
591 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200592 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800593 u64 *parent_pte)
594{
595 union kvm_mmu_page_role role;
596 unsigned index;
597 unsigned quadrant;
598 struct hlist_head *bucket;
599 struct kvm_mmu_page *page;
600 struct hlist_node *node;
601
602 role.word = 0;
603 role.glevels = vcpu->mmu.root_level;
604 role.level = level;
605 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200606 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800607 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
608 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
609 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
610 role.quadrant = quadrant;
611 }
612 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
613 gfn, role.word);
614 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
615 bucket = &vcpu->kvm->mmu_page_hash[index];
616 hlist_for_each_entry(page, node, bucket, hash_link)
617 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800618 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800619 pgprintk("%s: found\n", __FUNCTION__);
620 return page;
621 }
622 page = kvm_mmu_alloc_page(vcpu, parent_pte);
623 if (!page)
624 return page;
625 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
626 page->gfn = gfn;
627 page->role = role;
628 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800629 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800630 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800631 return page;
632}
633
Avi Kivitya4360362007-01-05 16:36:45 -0800634static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
635 struct kvm_mmu_page *page)
636{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800637 unsigned i;
638 u64 *pt;
639 u64 ent;
640
641 pt = __va(page->page_hpa);
642
643 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
644 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
645 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800646 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800647 pt[i] = 0;
648 }
Avi Kivity40907d52007-01-05 16:36:55 -0800649 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800650 return;
651 }
652
653 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
654 ent = pt[i];
655
656 pt[i] = 0;
657 if (!(ent & PT_PRESENT_MASK))
658 continue;
659 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800660 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800661 }
Avi Kivitya4360362007-01-05 16:36:45 -0800662}
663
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800664static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
665 struct kvm_mmu_page *page,
666 u64 *parent_pte)
667{
Avi Kivity714b93d2007-01-05 16:36:53 -0800668 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800669}
670
671static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
672 struct kvm_mmu_page *page)
673{
674 u64 *parent_pte;
675
676 while (page->multimapped || page->parent_pte) {
677 if (!page->multimapped)
678 parent_pte = page->parent_pte;
679 else {
680 struct kvm_pte_chain *chain;
681
682 chain = container_of(page->parent_ptes.first,
683 struct kvm_pte_chain, link);
684 parent_pte = chain->parent_ptes[0];
685 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800686 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800687 kvm_mmu_put_page(vcpu, page, parent_pte);
688 *parent_pte = 0;
689 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800690 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800691 if (!page->root_count) {
692 hlist_del(&page->hash_link);
693 kvm_mmu_free_page(vcpu, page->page_hpa);
Avi Kivity36868f72007-03-26 19:31:52 +0200694 } else
695 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800696}
697
698static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
699{
700 unsigned index;
701 struct hlist_head *bucket;
702 struct kvm_mmu_page *page;
703 struct hlist_node *node, *n;
704 int r;
705
706 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
707 r = 0;
708 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
709 bucket = &vcpu->kvm->mmu_page_hash[index];
710 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
711 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800712 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
713 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800714 kvm_mmu_zap_page(vcpu, page);
715 r = 1;
716 }
717 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800718}
719
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
721{
722 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
723 struct kvm_mmu_page *page_head = page_header(__pa(pte));
724
725 __set_bit(slot, &page_head->slot_bitmap);
726}
727
728hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
729{
730 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
731
732 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
733}
734
735hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
736{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 struct page *page;
738
739 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300740 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
741 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
744 | (gpa & (PAGE_SIZE-1));
745}
746
747hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
748{
749 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
750
751 if (gpa == UNMAPPED_GVA)
752 return UNMAPPED_GVA;
753 return gpa_to_hpa(vcpu, gpa);
754}
755
Avi Kivity039576c2007-03-20 12:46:50 +0200756struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
757{
758 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
759
760 if (gpa == UNMAPPED_GVA)
761 return NULL;
762 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
763}
764
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
766{
767}
768
769static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
770{
771 int level = PT32E_ROOT_LEVEL;
772 hpa_t table_addr = vcpu->mmu.root_hpa;
773
774 for (; ; level--) {
775 u32 index = PT64_INDEX(v, level);
776 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800777 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778
779 ASSERT(VALID_PAGE(table_addr));
780 table = __va(table_addr);
781
782 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800783 pte = table[index];
784 if (is_present_pte(pte) && is_writeble_pte(pte))
785 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
787 page_header_update_slot(vcpu->kvm, table, v);
788 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
789 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800790 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 return 0;
792 }
793
794 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800795 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800796 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800798 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
799 >> PAGE_SHIFT;
800 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
801 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200802 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800803 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 pgprintk("nonpaging_map: ENOMEM\n");
805 return -ENOMEM;
806 }
807
Avi Kivity25c0de22007-01-05 16:36:42 -0800808 table[index] = new_table->page_hpa | PT_PRESENT_MASK
809 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 }
811 table_addr = table[index] & PT64_BASE_ADDR_MASK;
812 }
813}
814
Avi Kivity17ac10a2007-01-05 16:36:40 -0800815static void mmu_free_roots(struct kvm_vcpu *vcpu)
816{
817 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800818 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800819
820#ifdef CONFIG_X86_64
821 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
822 hpa_t root = vcpu->mmu.root_hpa;
823
824 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800825 page = page_header(root);
826 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800827 vcpu->mmu.root_hpa = INVALID_PAGE;
828 return;
829 }
830#endif
831 for (i = 0; i < 4; ++i) {
832 hpa_t root = vcpu->mmu.pae_root[i];
833
Avi Kivity417726a2007-04-12 17:35:58 +0300834 if (root) {
835 ASSERT(VALID_PAGE(root));
836 root &= PT64_BASE_ADDR_MASK;
837 page = page_header(root);
838 --page->root_count;
839 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800840 vcpu->mmu.pae_root[i] = INVALID_PAGE;
841 }
842 vcpu->mmu.root_hpa = INVALID_PAGE;
843}
844
845static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
846{
847 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800848 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800849 struct kvm_mmu_page *page;
850
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800851 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800852
853#ifdef CONFIG_X86_64
854 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
855 hpa_t root = vcpu->mmu.root_hpa;
856
857 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800858 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200859 PT64_ROOT_LEVEL, 0, 0, NULL);
Ingo Molnar68a99f62007-01-05 16:36:59 -0800860 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800861 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800862 vcpu->mmu.root_hpa = root;
863 return;
864 }
865#endif
866 for (i = 0; i < 4; ++i) {
867 hpa_t root = vcpu->mmu.pae_root[i];
868
869 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300870 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
871 if (!is_present_pte(vcpu->pdptrs[i])) {
872 vcpu->mmu.pae_root[i] = 0;
873 continue;
874 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300876 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800877 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800878 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800879 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200880 0, NULL);
Ingo Molnar68a99f62007-01-05 16:36:59 -0800881 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800882 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800883 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
884 }
885 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
886}
887
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
889{
890 return vaddr;
891}
892
893static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
894 u32 error_code)
895{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800897 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800898 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899
Avi Kivitye2dec932007-01-05 16:36:54 -0800900 r = mmu_topup_memory_caches(vcpu);
901 if (r)
902 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800903
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 ASSERT(vcpu);
905 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
906
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907
Avi Kivityebeace82007-01-05 16:36:47 -0800908 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909
Avi Kivityebeace82007-01-05 16:36:47 -0800910 if (is_error_hpa(paddr))
911 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Avi Kivityebeace82007-01-05 16:36:47 -0800913 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914}
915
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916static void nonpaging_free(struct kvm_vcpu *vcpu)
917{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800918 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919}
920
921static int nonpaging_init_context(struct kvm_vcpu *vcpu)
922{
923 struct kvm_mmu *context = &vcpu->mmu;
924
925 context->new_cr3 = nonpaging_new_cr3;
926 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 context->gva_to_gpa = nonpaging_gva_to_gpa;
928 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800929 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800931 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 ASSERT(VALID_PAGE(context->root_hpa));
933 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
934 return 0;
935}
936
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
938{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300939 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 kvm_arch_ops->tlb_flush(vcpu);
941}
942
943static void paging_new_cr3(struct kvm_vcpu *vcpu)
944{
Avi Kivity374cbac2007-01-05 16:36:43 -0800945 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800947 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
948 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800949 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800951 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952}
953
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954static inline void set_pte_common(struct kvm_vcpu *vcpu,
955 u64 *shadow_pte,
956 gpa_t gaddr,
957 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800958 u64 access_bits,
959 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960{
961 hpa_t paddr;
962
963 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
964 if (!dirty)
965 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966
967 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
968
Avi Kivity374cbac2007-01-05 16:36:43 -0800969 *shadow_pte |= access_bits;
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 if (is_error_hpa(paddr)) {
972 *shadow_pte |= gaddr;
973 *shadow_pte |= PT_SHADOW_IO_MARK;
974 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800975 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800977
978 *shadow_pte |= paddr;
979
980 if (access_bits & PT_WRITABLE_MASK) {
981 struct kvm_mmu_page *shadow;
982
Avi Kivity815af8d2007-01-05 16:36:44 -0800983 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800984 if (shadow) {
985 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800986 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800987 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity40907d52007-01-05 16:36:55 -0800988 if (is_writeble_pte(*shadow_pte)) {
989 *shadow_pte &= ~PT_WRITABLE_MASK;
990 kvm_arch_ops->tlb_flush(vcpu);
991 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800992 }
993 }
994
995 if (access_bits & PT_WRITABLE_MASK)
996 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
997
998 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -0800999 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000}
1001
1002static void inject_page_fault(struct kvm_vcpu *vcpu,
1003 u64 addr,
1004 u32 err_code)
1005{
1006 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
1007}
1008
1009static inline int fix_read_pf(u64 *shadow_ent)
1010{
1011 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
1012 !(*shadow_ent & PT_USER_MASK)) {
1013 /*
1014 * If supervisor write protect is disabled, we shadow kernel
1015 * pages as user pages so we can trap the write access.
1016 */
1017 *shadow_ent |= PT_USER_MASK;
1018 *shadow_ent &= ~PT_WRITABLE_MASK;
1019
1020 return 1;
1021
1022 }
1023 return 0;
1024}
1025
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026static void paging_free(struct kvm_vcpu *vcpu)
1027{
1028 nonpaging_free(vcpu);
1029}
1030
1031#define PTTYPE 64
1032#include "paging_tmpl.h"
1033#undef PTTYPE
1034
1035#define PTTYPE 32
1036#include "paging_tmpl.h"
1037#undef PTTYPE
1038
Avi Kivity17ac10a2007-01-05 16:36:40 -08001039static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040{
1041 struct kvm_mmu *context = &vcpu->mmu;
1042
1043 ASSERT(is_pae(vcpu));
1044 context->new_cr3 = paging_new_cr3;
1045 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 context->gva_to_gpa = paging64_gva_to_gpa;
1047 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001048 context->root_level = level;
1049 context->shadow_root_level = level;
1050 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051 ASSERT(VALID_PAGE(context->root_hpa));
1052 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1053 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1054 return 0;
1055}
1056
Avi Kivity17ac10a2007-01-05 16:36:40 -08001057static int paging64_init_context(struct kvm_vcpu *vcpu)
1058{
1059 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1060}
1061
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062static int paging32_init_context(struct kvm_vcpu *vcpu)
1063{
1064 struct kvm_mmu *context = &vcpu->mmu;
1065
1066 context->new_cr3 = paging_new_cr3;
1067 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 context->gva_to_gpa = paging32_gva_to_gpa;
1069 context->free = paging_free;
1070 context->root_level = PT32_ROOT_LEVEL;
1071 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001072 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 ASSERT(VALID_PAGE(context->root_hpa));
1074 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1075 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1076 return 0;
1077}
1078
1079static int paging32E_init_context(struct kvm_vcpu *vcpu)
1080{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001081 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082}
1083
1084static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1085{
1086 ASSERT(vcpu);
1087 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1088
1089 if (!is_paging(vcpu))
1090 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001091 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 return paging64_init_context(vcpu);
1093 else if (is_pae(vcpu))
1094 return paging32E_init_context(vcpu);
1095 else
1096 return paging32_init_context(vcpu);
1097}
1098
1099static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1100{
1101 ASSERT(vcpu);
1102 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1103 vcpu->mmu.free(vcpu);
1104 vcpu->mmu.root_hpa = INVALID_PAGE;
1105 }
1106}
1107
1108int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1109{
Avi Kivity714b93d2007-01-05 16:36:53 -08001110 int r;
1111
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001113 r = init_kvm_mmu(vcpu);
1114 if (r < 0)
1115 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001116 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001117out:
1118 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119}
1120
Avi Kivityac1b7142007-03-08 17:13:32 +02001121static void mmu_pre_write_zap_pte(struct kvm_vcpu *vcpu,
1122 struct kvm_mmu_page *page,
1123 u64 *spte)
1124{
1125 u64 pte;
1126 struct kvm_mmu_page *child;
1127
1128 pte = *spte;
1129 if (is_present_pte(pte)) {
1130 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1131 rmap_remove(vcpu, spte);
1132 else {
1133 child = page_header(pte & PT64_BASE_ADDR_MASK);
1134 mmu_page_remove_parent_pte(vcpu, child, spte);
1135 }
1136 }
1137 *spte = 0;
1138}
1139
Avi Kivityda4a00f2007-01-05 16:36:44 -08001140void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1141{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001142 gfn_t gfn = gpa >> PAGE_SHIFT;
1143 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001144 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001145 struct hlist_head *bucket;
1146 unsigned index;
1147 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001148 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001149 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001150 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001151 unsigned misaligned;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001152 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001153 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001154 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001155
Avi Kivityda4a00f2007-01-05 16:36:44 -08001156 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001157 if (gfn == vcpu->last_pt_write_gfn) {
1158 ++vcpu->last_pt_write_count;
1159 if (vcpu->last_pt_write_count >= 3)
1160 flooded = 1;
1161 } else {
1162 vcpu->last_pt_write_gfn = gfn;
1163 vcpu->last_pt_write_count = 1;
1164 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001165 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1166 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001167 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001168 if (page->gfn != gfn || page->role.metaphysical)
1169 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001170 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1171 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001172 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001173 /*
1174 * Misaligned accesses are too much trouble to fix
1175 * up; also, they usually indicate a page is not used
1176 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001177 *
1178 * If we're seeing too many writes to a page,
1179 * it may no longer be a page table, or we may be
1180 * forking, in which case it is better to unmap the
1181 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001182 */
1183 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1184 gpa, bytes, page->role.word);
1185 kvm_mmu_zap_page(vcpu, page);
1186 continue;
1187 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001188 page_offset = offset;
1189 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001190 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001191 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001192 page_offset <<= 1; /* 32->64 */
1193 /*
1194 * A 32-bit pde maps 4MB while the shadow pdes map
1195 * only 2MB. So we need to double the offset again
1196 * and zap two pdes instead of one.
1197 */
1198 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001199 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001200 page_offset <<= 1;
1201 npte = 2;
1202 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001203 page_offset &= ~PAGE_MASK;
1204 }
1205 spte = __va(page->page_hpa);
1206 spte += page_offset / sizeof(*spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001207 while (npte--) {
1208 mmu_pre_write_zap_pte(vcpu, page, spte);
1209 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001210 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001211 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001212}
1213
1214void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1215{
1216}
1217
Avi Kivitya4360362007-01-05 16:36:45 -08001218int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1219{
1220 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1221
1222 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1223}
1224
Avi Kivityebeace82007-01-05 16:36:47 -08001225void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1226{
1227 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1228 struct kvm_mmu_page *page;
1229
1230 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1231 struct kvm_mmu_page, link);
1232 kvm_mmu_zap_page(vcpu, page);
1233 }
1234}
1235EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1236
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237static void free_mmu_pages(struct kvm_vcpu *vcpu)
1238{
Avi Kivityf51234c2007-01-05 16:36:52 -08001239 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240
Avi Kivityf51234c2007-01-05 16:36:52 -08001241 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1242 page = container_of(vcpu->kvm->active_mmu_pages.next,
1243 struct kvm_mmu_page, link);
1244 kvm_mmu_zap_page(vcpu, page);
1245 }
1246 while (!list_empty(&vcpu->free_pages)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247 page = list_entry(vcpu->free_pages.next,
1248 struct kvm_mmu_page, link);
1249 list_del(&page->link);
1250 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1251 page->page_hpa = INVALID_PAGE;
1252 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001253 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254}
1255
1256static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1257{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001258 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 int i;
1260
1261 ASSERT(vcpu);
1262
1263 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1265
1266 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001267 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 goto error_1;
Markus Rechberger5972e952007-02-19 14:37:47 +02001269 set_page_private(page, (unsigned long)page_header);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1271 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1272 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001273 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001275
1276 /*
1277 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1278 * Therefore we need to allocate shadow page tables in the first
1279 * 4GB of memory, which happens to fit the DMA32 zone.
1280 */
1281 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1282 if (!page)
1283 goto error_1;
1284 vcpu->mmu.pae_root = page_address(page);
1285 for (i = 0; i < 4; ++i)
1286 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1287
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288 return 0;
1289
1290error_1:
1291 free_mmu_pages(vcpu);
1292 return -ENOMEM;
1293}
1294
Ingo Molnar8018c272006-12-29 16:50:01 -08001295int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 ASSERT(vcpu);
1298 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1299 ASSERT(list_empty(&vcpu->free_pages));
1300
Ingo Molnar8018c272006-12-29 16:50:01 -08001301 return alloc_mmu_pages(vcpu);
1302}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303
Ingo Molnar8018c272006-12-29 16:50:01 -08001304int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1305{
1306 ASSERT(vcpu);
1307 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1308 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001309
Ingo Molnar8018c272006-12-29 16:50:01 -08001310 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311}
1312
1313void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1314{
1315 ASSERT(vcpu);
1316
1317 destroy_kvm_mmu(vcpu);
1318 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001319 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320}
1321
Avi Kivity714b93d2007-01-05 16:36:53 -08001322void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323{
Avi Kivity714b93d2007-01-05 16:36:53 -08001324 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 struct kvm_mmu_page *page;
1326
1327 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1328 int i;
1329 u64 *pt;
1330
1331 if (!test_bit(slot, &page->slot_bitmap))
1332 continue;
1333
1334 pt = __va(page->page_hpa);
1335 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1336 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001337 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001338 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001340 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 }
1342}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001343
Dor Laore0fa8262007-03-30 13:06:33 +03001344void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1345{
1346 destroy_kvm_mmu(vcpu);
1347
1348 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1349 struct kvm_mmu_page *page;
1350
1351 page = container_of(vcpu->kvm->active_mmu_pages.next,
1352 struct kvm_mmu_page, link);
1353 kvm_mmu_zap_page(vcpu, page);
1354 }
1355
1356 mmu_free_memory_caches(vcpu);
1357 kvm_arch_ops->tlb_flush(vcpu);
1358 init_kvm_mmu(vcpu);
1359}
1360
Avi Kivityb5a33a72007-04-15 16:31:09 +03001361void kvm_mmu_module_exit(void)
1362{
1363 if (pte_chain_cache)
1364 kmem_cache_destroy(pte_chain_cache);
1365 if (rmap_desc_cache)
1366 kmem_cache_destroy(rmap_desc_cache);
1367}
1368
1369int kvm_mmu_module_init(void)
1370{
1371 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1372 sizeof(struct kvm_pte_chain),
1373 0, 0, NULL, NULL);
1374 if (!pte_chain_cache)
1375 goto nomem;
1376 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1377 sizeof(struct kvm_rmap_desc),
1378 0, 0, NULL, NULL);
1379 if (!rmap_desc_cache)
1380 goto nomem;
1381
1382 return 0;
1383
1384nomem:
1385 kvm_mmu_module_exit();
1386 return -ENOMEM;
1387}
1388
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001389#ifdef AUDIT
1390
1391static const char *audit_msg;
1392
1393static gva_t canonicalize(gva_t gva)
1394{
1395#ifdef CONFIG_X86_64
1396 gva = (long long)(gva << 16) >> 16;
1397#endif
1398 return gva;
1399}
1400
1401static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1402 gva_t va, int level)
1403{
1404 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1405 int i;
1406 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1407
1408 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1409 u64 ent = pt[i];
1410
Adrian Bunk28076962007-04-28 21:20:48 +02001411 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001412 continue;
1413
1414 va = canonicalize(va);
1415 if (level > 1)
1416 audit_mappings_page(vcpu, ent, va, level - 1);
1417 else {
1418 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1419 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1420
1421 if ((ent & PT_PRESENT_MASK)
1422 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1423 printk(KERN_ERR "audit error: (%s) levels %d"
1424 " gva %lx gpa %llx hpa %llx ent %llx\n",
1425 audit_msg, vcpu->mmu.root_level,
1426 va, gpa, hpa, ent);
1427 }
1428 }
1429}
1430
1431static void audit_mappings(struct kvm_vcpu *vcpu)
1432{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001433 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001434
1435 if (vcpu->mmu.root_level == 4)
1436 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1437 else
1438 for (i = 0; i < 4; ++i)
1439 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1440 audit_mappings_page(vcpu,
1441 vcpu->mmu.pae_root[i],
1442 i << 30,
1443 2);
1444}
1445
1446static int count_rmaps(struct kvm_vcpu *vcpu)
1447{
1448 int nmaps = 0;
1449 int i, j, k;
1450
1451 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1452 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1453 struct kvm_rmap_desc *d;
1454
1455 for (j = 0; j < m->npages; ++j) {
1456 struct page *page = m->phys_mem[j];
1457
1458 if (!page->private)
1459 continue;
1460 if (!(page->private & 1)) {
1461 ++nmaps;
1462 continue;
1463 }
1464 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1465 while (d) {
1466 for (k = 0; k < RMAP_EXT; ++k)
1467 if (d->shadow_ptes[k])
1468 ++nmaps;
1469 else
1470 break;
1471 d = d->more;
1472 }
1473 }
1474 }
1475 return nmaps;
1476}
1477
1478static int count_writable_mappings(struct kvm_vcpu *vcpu)
1479{
1480 int nmaps = 0;
1481 struct kvm_mmu_page *page;
1482 int i;
1483
1484 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1485 u64 *pt = __va(page->page_hpa);
1486
1487 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1488 continue;
1489
1490 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1491 u64 ent = pt[i];
1492
1493 if (!(ent & PT_PRESENT_MASK))
1494 continue;
1495 if (!(ent & PT_WRITABLE_MASK))
1496 continue;
1497 ++nmaps;
1498 }
1499 }
1500 return nmaps;
1501}
1502
1503static void audit_rmap(struct kvm_vcpu *vcpu)
1504{
1505 int n_rmap = count_rmaps(vcpu);
1506 int n_actual = count_writable_mappings(vcpu);
1507
1508 if (n_rmap != n_actual)
1509 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1510 __FUNCTION__, audit_msg, n_rmap, n_actual);
1511}
1512
1513static void audit_write_protection(struct kvm_vcpu *vcpu)
1514{
1515 struct kvm_mmu_page *page;
1516
1517 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1518 hfn_t hfn;
1519 struct page *pg;
1520
1521 if (page->role.metaphysical)
1522 continue;
1523
1524 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1525 >> PAGE_SHIFT;
1526 pg = pfn_to_page(hfn);
1527 if (pg->private)
1528 printk(KERN_ERR "%s: (%s) shadow page has writable"
1529 " mappings: gfn %lx role %x\n",
1530 __FUNCTION__, audit_msg, page->gfn,
1531 page->role.word);
1532 }
1533}
1534
1535static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1536{
1537 int olddbg = dbg;
1538
1539 dbg = 0;
1540 audit_msg = msg;
1541 audit_rmap(vcpu);
1542 audit_write_protection(vcpu);
1543 audit_mappings(vcpu);
1544 dbg = olddbg;
1545}
1546
1547#endif