blob: a96c9ae54f3c4bfefd34dd0bd279b42ac673373d [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 Kivity954bbbc22007-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 Kivity4b02d6d2007-05-06 15:36:30 +0300458static void kvm_mmu_free_page(struct kvm_vcpu *vcpu,
459 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800460{
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300461 ASSERT(is_empty_shadow_page(page_head->page_hpa));
Avi Kivity36868f72007-03-26 19:31:52 +0200462 list_move(&page_head->link, &vcpu->free_pages);
Avi Kivity260746c2007-01-05 16:36:49 -0800463 ++vcpu->kvm->n_free_mmu_pages;
464}
465
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800466static unsigned kvm_page_table_hashfn(gfn_t gfn)
467{
468 return gfn;
469}
470
Avi Kivity25c0de22007-01-05 16:36:42 -0800471static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
472 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473{
474 struct kvm_mmu_page *page;
475
476 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800477 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
479 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
Avi Kivity36868f72007-03-26 19:31:52 +0200480 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 ASSERT(is_empty_shadow_page(page->page_hpa));
482 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800483 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800485 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800486 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487}
488
Avi Kivity714b93d2007-01-05 16:36:53 -0800489static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
490 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800491{
492 struct kvm_pte_chain *pte_chain;
493 struct hlist_node *node;
494 int i;
495
496 if (!parent_pte)
497 return;
498 if (!page->multimapped) {
499 u64 *old = page->parent_pte;
500
501 if (!old) {
502 page->parent_pte = parent_pte;
503 return;
504 }
505 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800506 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800507 INIT_HLIST_HEAD(&page->parent_ptes);
508 hlist_add_head(&pte_chain->link, &page->parent_ptes);
509 pte_chain->parent_ptes[0] = old;
510 }
511 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
512 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
513 continue;
514 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
515 if (!pte_chain->parent_ptes[i]) {
516 pte_chain->parent_ptes[i] = parent_pte;
517 return;
518 }
519 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800520 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800521 BUG_ON(!pte_chain);
522 hlist_add_head(&pte_chain->link, &page->parent_ptes);
523 pte_chain->parent_ptes[0] = parent_pte;
524}
525
Avi Kivity714b93d2007-01-05 16:36:53 -0800526static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
527 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800528 u64 *parent_pte)
529{
530 struct kvm_pte_chain *pte_chain;
531 struct hlist_node *node;
532 int i;
533
534 if (!page->multimapped) {
535 BUG_ON(page->parent_pte != parent_pte);
536 page->parent_pte = NULL;
537 return;
538 }
539 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
540 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
541 if (!pte_chain->parent_ptes[i])
542 break;
543 if (pte_chain->parent_ptes[i] != parent_pte)
544 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800545 while (i + 1 < NR_PTE_CHAIN_ENTRIES
546 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800547 pte_chain->parent_ptes[i]
548 = pte_chain->parent_ptes[i + 1];
549 ++i;
550 }
551 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800552 if (i == 0) {
553 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800554 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800555 if (hlist_empty(&page->parent_ptes)) {
556 page->multimapped = 0;
557 page->parent_pte = NULL;
558 }
559 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800560 return;
561 }
562 BUG();
563}
564
565static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
566 gfn_t gfn)
567{
568 unsigned index;
569 struct hlist_head *bucket;
570 struct kvm_mmu_page *page;
571 struct hlist_node *node;
572
573 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
574 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
575 bucket = &vcpu->kvm->mmu_page_hash[index];
576 hlist_for_each_entry(page, node, bucket, hash_link)
577 if (page->gfn == gfn && !page->role.metaphysical) {
578 pgprintk("%s: found role %x\n",
579 __FUNCTION__, page->role.word);
580 return page;
581 }
582 return NULL;
583}
584
585static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
586 gfn_t gfn,
587 gva_t gaddr,
588 unsigned level,
589 int metaphysical,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200590 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800591 u64 *parent_pte)
592{
593 union kvm_mmu_page_role role;
594 unsigned index;
595 unsigned quadrant;
596 struct hlist_head *bucket;
597 struct kvm_mmu_page *page;
598 struct hlist_node *node;
599
600 role.word = 0;
601 role.glevels = vcpu->mmu.root_level;
602 role.level = level;
603 role.metaphysical = metaphysical;
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200604 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800605 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
606 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
607 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
608 role.quadrant = quadrant;
609 }
610 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
611 gfn, role.word);
612 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
613 bucket = &vcpu->kvm->mmu_page_hash[index];
614 hlist_for_each_entry(page, node, bucket, hash_link)
615 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800616 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800617 pgprintk("%s: found\n", __FUNCTION__);
618 return page;
619 }
620 page = kvm_mmu_alloc_page(vcpu, parent_pte);
621 if (!page)
622 return page;
623 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
624 page->gfn = gfn;
625 page->role = role;
626 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800627 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800628 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800629 return page;
630}
631
Avi Kivitya4360362007-01-05 16:36:45 -0800632static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
633 struct kvm_mmu_page *page)
634{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800635 unsigned i;
636 u64 *pt;
637 u64 ent;
638
639 pt = __va(page->page_hpa);
640
641 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
642 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
643 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800644 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800645 pt[i] = 0;
646 }
Avi Kivity40907d52007-01-05 16:36:55 -0800647 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800648 return;
649 }
650
651 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
652 ent = pt[i];
653
654 pt[i] = 0;
655 if (!(ent & PT_PRESENT_MASK))
656 continue;
657 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800658 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800659 }
Avi Kivitya4360362007-01-05 16:36:45 -0800660}
661
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800662static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
663 struct kvm_mmu_page *page,
664 u64 *parent_pte)
665{
Avi Kivity714b93d2007-01-05 16:36:53 -0800666 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800667}
668
669static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
670 struct kvm_mmu_page *page)
671{
672 u64 *parent_pte;
673
674 while (page->multimapped || page->parent_pte) {
675 if (!page->multimapped)
676 parent_pte = page->parent_pte;
677 else {
678 struct kvm_pte_chain *chain;
679
680 chain = container_of(page->parent_ptes.first,
681 struct kvm_pte_chain, link);
682 parent_pte = chain->parent_ptes[0];
683 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800684 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800685 kvm_mmu_put_page(vcpu, page, parent_pte);
686 *parent_pte = 0;
687 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800688 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800689 if (!page->root_count) {
690 hlist_del(&page->hash_link);
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300691 kvm_mmu_free_page(vcpu, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200692 } else
693 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800694}
695
696static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
697{
698 unsigned index;
699 struct hlist_head *bucket;
700 struct kvm_mmu_page *page;
701 struct hlist_node *node, *n;
702 int r;
703
704 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
705 r = 0;
706 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
707 bucket = &vcpu->kvm->mmu_page_hash[index];
708 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
709 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800710 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
711 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800712 kvm_mmu_zap_page(vcpu, page);
713 r = 1;
714 }
715 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716}
717
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
719{
720 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
721 struct kvm_mmu_page *page_head = page_header(__pa(pte));
722
723 __set_bit(slot, &page_head->slot_bitmap);
724}
725
726hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
727{
728 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
729
730 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
731}
732
733hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
734{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 struct page *page;
736
737 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc22007-03-30 14:02:32 +0300738 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
739 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
742 | (gpa & (PAGE_SIZE-1));
743}
744
745hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
746{
747 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
748
749 if (gpa == UNMAPPED_GVA)
750 return UNMAPPED_GVA;
751 return gpa_to_hpa(vcpu, gpa);
752}
753
Avi Kivity039576c2007-03-20 12:46:50 +0200754struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
755{
756 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
757
758 if (gpa == UNMAPPED_GVA)
759 return NULL;
760 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
761}
762
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
764{
765}
766
767static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
768{
769 int level = PT32E_ROOT_LEVEL;
770 hpa_t table_addr = vcpu->mmu.root_hpa;
771
772 for (; ; level--) {
773 u32 index = PT64_INDEX(v, level);
774 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800775 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776
777 ASSERT(VALID_PAGE(table_addr));
778 table = __va(table_addr);
779
780 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800781 pte = table[index];
782 if (is_present_pte(pte) && is_writeble_pte(pte))
783 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
785 page_header_update_slot(vcpu->kvm, table, v);
786 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
787 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800788 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 return 0;
790 }
791
792 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800793 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800794 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800796 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
797 >> PAGE_SHIFT;
798 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
799 v, level - 1,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200800 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800801 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 pgprintk("nonpaging_map: ENOMEM\n");
803 return -ENOMEM;
804 }
805
Avi Kivity25c0de22007-01-05 16:36:42 -0800806 table[index] = new_table->page_hpa | PT_PRESENT_MASK
807 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 }
809 table_addr = table[index] & PT64_BASE_ADDR_MASK;
810 }
811}
812
Avi Kivity17ac10a2007-01-05 16:36:40 -0800813static void mmu_free_roots(struct kvm_vcpu *vcpu)
814{
815 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800816 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800817
818#ifdef CONFIG_X86_64
819 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
820 hpa_t root = vcpu->mmu.root_hpa;
821
822 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800823 page = page_header(root);
824 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800825 vcpu->mmu.root_hpa = INVALID_PAGE;
826 return;
827 }
828#endif
829 for (i = 0; i < 4; ++i) {
830 hpa_t root = vcpu->mmu.pae_root[i];
831
Avi Kivity417726a2007-04-12 17:35:58 +0300832 if (root) {
833 ASSERT(VALID_PAGE(root));
834 root &= PT64_BASE_ADDR_MASK;
835 page = page_header(root);
836 --page->root_count;
837 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800838 vcpu->mmu.pae_root[i] = INVALID_PAGE;
839 }
840 vcpu->mmu.root_hpa = INVALID_PAGE;
841}
842
843static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
844{
845 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800847 struct kvm_mmu_page *page;
848
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800849 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800850
851#ifdef CONFIG_X86_64
852 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
853 hpa_t root = vcpu->mmu.root_hpa;
854
855 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800856 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200857 PT64_ROOT_LEVEL, 0, 0, NULL);
Ingo Molnar68a99f62007-01-05 16:36:59 -0800858 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800859 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800860 vcpu->mmu.root_hpa = root;
861 return;
862 }
863#endif
864 for (i = 0; i < 4; ++i) {
865 hpa_t root = vcpu->mmu.pae_root[i];
866
867 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300868 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
869 if (!is_present_pte(vcpu->pdptrs[i])) {
870 vcpu->mmu.pae_root[i] = 0;
871 continue;
872 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800873 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300874 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800876 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800877 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200878 0, NULL);
Ingo Molnar68a99f62007-01-05 16:36:59 -0800879 root = page->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800880 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800881 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
882 }
883 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
884}
885
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
887{
888 return vaddr;
889}
890
891static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
892 u32 error_code)
893{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800895 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800896 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897
Avi Kivitye2dec932007-01-05 16:36:54 -0800898 r = mmu_topup_memory_caches(vcpu);
899 if (r)
900 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 ASSERT(vcpu);
903 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
904
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905
Avi Kivityebeace82007-01-05 16:36:47 -0800906 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907
Avi Kivityebeace82007-01-05 16:36:47 -0800908 if (is_error_hpa(paddr))
909 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910
Avi Kivityebeace82007-01-05 16:36:47 -0800911 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912}
913
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914static void nonpaging_free(struct kvm_vcpu *vcpu)
915{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800916 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917}
918
919static int nonpaging_init_context(struct kvm_vcpu *vcpu)
920{
921 struct kvm_mmu *context = &vcpu->mmu;
922
923 context->new_cr3 = nonpaging_new_cr3;
924 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 context->gva_to_gpa = nonpaging_gva_to_gpa;
926 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800929 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 ASSERT(VALID_PAGE(context->root_hpa));
931 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
932 return 0;
933}
934
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
936{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300937 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 kvm_arch_ops->tlb_flush(vcpu);
939}
940
941static void paging_new_cr3(struct kvm_vcpu *vcpu)
942{
Avi Kivity374cbac2007-01-05 16:36:43 -0800943 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800944 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800945 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
946 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800949 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950}
951
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952static inline void set_pte_common(struct kvm_vcpu *vcpu,
953 u64 *shadow_pte,
954 gpa_t gaddr,
955 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800956 u64 access_bits,
957 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958{
959 hpa_t paddr;
960
961 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
962 if (!dirty)
963 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
965 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
966
Avi Kivity374cbac2007-01-05 16:36:43 -0800967 *shadow_pte |= access_bits;
968
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 if (is_error_hpa(paddr)) {
970 *shadow_pte |= gaddr;
971 *shadow_pte |= PT_SHADOW_IO_MARK;
972 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800973 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800975
976 *shadow_pte |= paddr;
977
978 if (access_bits & PT_WRITABLE_MASK) {
979 struct kvm_mmu_page *shadow;
980
Avi Kivity815af8d2007-01-05 16:36:44 -0800981 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800982 if (shadow) {
983 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800984 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800985 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity40907d52007-01-05 16:36:55 -0800986 if (is_writeble_pte(*shadow_pte)) {
987 *shadow_pte &= ~PT_WRITABLE_MASK;
988 kvm_arch_ops->tlb_flush(vcpu);
989 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800990 }
991 }
992
993 if (access_bits & PT_WRITABLE_MASK)
994 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
995
996 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -0800997 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998}
999
1000static void inject_page_fault(struct kvm_vcpu *vcpu,
1001 u64 addr,
1002 u32 err_code)
1003{
1004 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
1005}
1006
1007static inline int fix_read_pf(u64 *shadow_ent)
1008{
1009 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
1010 !(*shadow_ent & PT_USER_MASK)) {
1011 /*
1012 * If supervisor write protect is disabled, we shadow kernel
1013 * pages as user pages so we can trap the write access.
1014 */
1015 *shadow_ent |= PT_USER_MASK;
1016 *shadow_ent &= ~PT_WRITABLE_MASK;
1017
1018 return 1;
1019
1020 }
1021 return 0;
1022}
1023
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024static void paging_free(struct kvm_vcpu *vcpu)
1025{
1026 nonpaging_free(vcpu);
1027}
1028
1029#define PTTYPE 64
1030#include "paging_tmpl.h"
1031#undef PTTYPE
1032
1033#define PTTYPE 32
1034#include "paging_tmpl.h"
1035#undef PTTYPE
1036
Avi Kivity17ac10a2007-01-05 16:36:40 -08001037static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038{
1039 struct kvm_mmu *context = &vcpu->mmu;
1040
1041 ASSERT(is_pae(vcpu));
1042 context->new_cr3 = paging_new_cr3;
1043 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 context->gva_to_gpa = paging64_gva_to_gpa;
1045 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001046 context->root_level = level;
1047 context->shadow_root_level = level;
1048 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 ASSERT(VALID_PAGE(context->root_hpa));
1050 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1051 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1052 return 0;
1053}
1054
Avi Kivity17ac10a2007-01-05 16:36:40 -08001055static int paging64_init_context(struct kvm_vcpu *vcpu)
1056{
1057 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1058}
1059
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060static int paging32_init_context(struct kvm_vcpu *vcpu)
1061{
1062 struct kvm_mmu *context = &vcpu->mmu;
1063
1064 context->new_cr3 = paging_new_cr3;
1065 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 context->gva_to_gpa = paging32_gva_to_gpa;
1067 context->free = paging_free;
1068 context->root_level = PT32_ROOT_LEVEL;
1069 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001070 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 ASSERT(VALID_PAGE(context->root_hpa));
1072 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1073 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1074 return 0;
1075}
1076
1077static int paging32E_init_context(struct kvm_vcpu *vcpu)
1078{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001079 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080}
1081
1082static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1083{
1084 ASSERT(vcpu);
1085 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1086
1087 if (!is_paging(vcpu))
1088 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001089 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090 return paging64_init_context(vcpu);
1091 else if (is_pae(vcpu))
1092 return paging32E_init_context(vcpu);
1093 else
1094 return paging32_init_context(vcpu);
1095}
1096
1097static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1098{
1099 ASSERT(vcpu);
1100 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1101 vcpu->mmu.free(vcpu);
1102 vcpu->mmu.root_hpa = INVALID_PAGE;
1103 }
1104}
1105
1106int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1107{
Avi Kivity714b93d2007-01-05 16:36:53 -08001108 int r;
1109
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001111 r = init_kvm_mmu(vcpu);
1112 if (r < 0)
1113 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001114 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001115out:
1116 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117}
1118
Avi Kivity09072da2007-05-01 14:16:52 +03001119static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001120 struct kvm_mmu_page *page,
1121 u64 *spte)
1122{
1123 u64 pte;
1124 struct kvm_mmu_page *child;
1125
1126 pte = *spte;
1127 if (is_present_pte(pte)) {
1128 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1129 rmap_remove(vcpu, spte);
1130 else {
1131 child = page_header(pte & PT64_BASE_ADDR_MASK);
1132 mmu_page_remove_parent_pte(vcpu, child, spte);
1133 }
1134 }
1135 *spte = 0;
1136}
1137
Avi Kivity00284252007-05-01 16:53:31 +03001138static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1139 struct kvm_mmu_page *page,
1140 u64 *spte,
1141 const void *new, int bytes)
1142{
1143 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1144 return;
1145
1146 if (page->role.glevels == PT32_ROOT_LEVEL)
1147 paging32_update_pte(vcpu, page, spte, new, bytes);
1148 else
1149 paging64_update_pte(vcpu, page, spte, new, bytes);
1150}
1151
Avi Kivity09072da2007-05-01 14:16:52 +03001152void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1153 const u8 *old, const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001154{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001155 gfn_t gfn = gpa >> PAGE_SHIFT;
1156 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001157 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001158 struct hlist_head *bucket;
1159 unsigned index;
1160 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001161 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001162 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001163 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001164 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001165 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001166 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001167 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001168 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001169
Avi Kivityda4a00f2007-01-05 16:36:44 -08001170 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001171 if (gfn == vcpu->last_pt_write_gfn) {
1172 ++vcpu->last_pt_write_count;
1173 if (vcpu->last_pt_write_count >= 3)
1174 flooded = 1;
1175 } else {
1176 vcpu->last_pt_write_gfn = gfn;
1177 vcpu->last_pt_write_count = 1;
1178 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001179 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1180 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001181 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001182 if (page->gfn != gfn || page->role.metaphysical)
1183 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001184 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1185 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001186 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001187 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001188 /*
1189 * Misaligned accesses are too much trouble to fix
1190 * up; also, they usually indicate a page is not used
1191 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001192 *
1193 * If we're seeing too many writes to a page,
1194 * it may no longer be a page table, or we may be
1195 * forking, in which case it is better to unmap the
1196 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001197 */
1198 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1199 gpa, bytes, page->role.word);
1200 kvm_mmu_zap_page(vcpu, page);
1201 continue;
1202 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001203 page_offset = offset;
1204 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001205 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001206 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001207 page_offset <<= 1; /* 32->64 */
1208 /*
1209 * A 32-bit pde maps 4MB while the shadow pdes map
1210 * only 2MB. So we need to double the offset again
1211 * and zap two pdes instead of one.
1212 */
1213 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001214 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001215 page_offset <<= 1;
1216 npte = 2;
1217 }
Avi Kivityfce06572007-05-01 16:44:05 +03001218 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001219 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001220 if (quadrant != page->role.quadrant)
1221 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001222 }
1223 spte = __va(page->page_hpa);
1224 spte += page_offset / sizeof(*spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001225 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001226 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivity00284252007-05-01 16:53:31 +03001227 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
Avi Kivityac1b7142007-03-08 17:13:32 +02001228 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001229 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001230 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001231}
1232
Avi Kivitya4360362007-01-05 16:36:45 -08001233int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1234{
1235 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1236
1237 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1238}
1239
Avi Kivityebeace82007-01-05 16:36:47 -08001240void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1241{
1242 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1243 struct kvm_mmu_page *page;
1244
1245 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1246 struct kvm_mmu_page, link);
1247 kvm_mmu_zap_page(vcpu, page);
1248 }
1249}
1250EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1251
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252static void free_mmu_pages(struct kvm_vcpu *vcpu)
1253{
Avi Kivityf51234c2007-01-05 16:36:52 -08001254 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255
Avi Kivityf51234c2007-01-05 16:36:52 -08001256 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1257 page = container_of(vcpu->kvm->active_mmu_pages.next,
1258 struct kvm_mmu_page, link);
1259 kvm_mmu_zap_page(vcpu, page);
1260 }
1261 while (!list_empty(&vcpu->free_pages)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 page = list_entry(vcpu->free_pages.next,
1263 struct kvm_mmu_page, link);
1264 list_del(&page->link);
1265 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1266 page->page_hpa = INVALID_PAGE;
1267 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001268 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269}
1270
1271static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1272{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001273 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 int i;
1275
1276 ASSERT(vcpu);
1277
1278 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1280
1281 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001282 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 goto error_1;
Markus Rechberger5972e952007-02-19 14:37:47 +02001284 set_page_private(page, (unsigned long)page_header);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1286 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1287 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001288 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001290
1291 /*
1292 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1293 * Therefore we need to allocate shadow page tables in the first
1294 * 4GB of memory, which happens to fit the DMA32 zone.
1295 */
1296 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1297 if (!page)
1298 goto error_1;
1299 vcpu->mmu.pae_root = page_address(page);
1300 for (i = 0; i < 4; ++i)
1301 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1302
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 return 0;
1304
1305error_1:
1306 free_mmu_pages(vcpu);
1307 return -ENOMEM;
1308}
1309
Ingo Molnar8018c272006-12-29 16:50:01 -08001310int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 ASSERT(vcpu);
1313 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1314 ASSERT(list_empty(&vcpu->free_pages));
1315
Ingo Molnar8018c272006-12-29 16:50:01 -08001316 return alloc_mmu_pages(vcpu);
1317}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
Ingo Molnar8018c272006-12-29 16:50:01 -08001319int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1320{
1321 ASSERT(vcpu);
1322 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1323 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001324
Ingo Molnar8018c272006-12-29 16:50:01 -08001325 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326}
1327
1328void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1329{
1330 ASSERT(vcpu);
1331
1332 destroy_kvm_mmu(vcpu);
1333 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001334 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
1336
Avi Kivity714b93d2007-01-05 16:36:53 -08001337void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338{
Avi Kivity714b93d2007-01-05 16:36:53 -08001339 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 struct kvm_mmu_page *page;
1341
1342 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1343 int i;
1344 u64 *pt;
1345
1346 if (!test_bit(slot, &page->slot_bitmap))
1347 continue;
1348
1349 pt = __va(page->page_hpa);
1350 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1351 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001352 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001353 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001355 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 }
1357}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001358
Dor Laore0fa8262007-03-30 13:06:33 +03001359void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1360{
1361 destroy_kvm_mmu(vcpu);
1362
1363 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1364 struct kvm_mmu_page *page;
1365
1366 page = container_of(vcpu->kvm->active_mmu_pages.next,
1367 struct kvm_mmu_page, link);
1368 kvm_mmu_zap_page(vcpu, page);
1369 }
1370
1371 mmu_free_memory_caches(vcpu);
1372 kvm_arch_ops->tlb_flush(vcpu);
1373 init_kvm_mmu(vcpu);
1374}
1375
Avi Kivityb5a33a72007-04-15 16:31:09 +03001376void kvm_mmu_module_exit(void)
1377{
1378 if (pte_chain_cache)
1379 kmem_cache_destroy(pte_chain_cache);
1380 if (rmap_desc_cache)
1381 kmem_cache_destroy(rmap_desc_cache);
1382}
1383
1384int kvm_mmu_module_init(void)
1385{
1386 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1387 sizeof(struct kvm_pte_chain),
1388 0, 0, NULL, NULL);
1389 if (!pte_chain_cache)
1390 goto nomem;
1391 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1392 sizeof(struct kvm_rmap_desc),
1393 0, 0, NULL, NULL);
1394 if (!rmap_desc_cache)
1395 goto nomem;
1396
1397 return 0;
1398
1399nomem:
1400 kvm_mmu_module_exit();
1401 return -ENOMEM;
1402}
1403
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001404#ifdef AUDIT
1405
1406static const char *audit_msg;
1407
1408static gva_t canonicalize(gva_t gva)
1409{
1410#ifdef CONFIG_X86_64
1411 gva = (long long)(gva << 16) >> 16;
1412#endif
1413 return gva;
1414}
1415
1416static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1417 gva_t va, int level)
1418{
1419 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1420 int i;
1421 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1422
1423 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1424 u64 ent = pt[i];
1425
Adrian Bunk28076962007-04-28 21:20:48 +02001426 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001427 continue;
1428
1429 va = canonicalize(va);
1430 if (level > 1)
1431 audit_mappings_page(vcpu, ent, va, level - 1);
1432 else {
1433 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1434 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1435
1436 if ((ent & PT_PRESENT_MASK)
1437 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1438 printk(KERN_ERR "audit error: (%s) levels %d"
1439 " gva %lx gpa %llx hpa %llx ent %llx\n",
1440 audit_msg, vcpu->mmu.root_level,
1441 va, gpa, hpa, ent);
1442 }
1443 }
1444}
1445
1446static void audit_mappings(struct kvm_vcpu *vcpu)
1447{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001448 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001449
1450 if (vcpu->mmu.root_level == 4)
1451 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1452 else
1453 for (i = 0; i < 4; ++i)
1454 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1455 audit_mappings_page(vcpu,
1456 vcpu->mmu.pae_root[i],
1457 i << 30,
1458 2);
1459}
1460
1461static int count_rmaps(struct kvm_vcpu *vcpu)
1462{
1463 int nmaps = 0;
1464 int i, j, k;
1465
1466 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1467 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1468 struct kvm_rmap_desc *d;
1469
1470 for (j = 0; j < m->npages; ++j) {
1471 struct page *page = m->phys_mem[j];
1472
1473 if (!page->private)
1474 continue;
1475 if (!(page->private & 1)) {
1476 ++nmaps;
1477 continue;
1478 }
1479 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1480 while (d) {
1481 for (k = 0; k < RMAP_EXT; ++k)
1482 if (d->shadow_ptes[k])
1483 ++nmaps;
1484 else
1485 break;
1486 d = d->more;
1487 }
1488 }
1489 }
1490 return nmaps;
1491}
1492
1493static int count_writable_mappings(struct kvm_vcpu *vcpu)
1494{
1495 int nmaps = 0;
1496 struct kvm_mmu_page *page;
1497 int i;
1498
1499 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1500 u64 *pt = __va(page->page_hpa);
1501
1502 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1503 continue;
1504
1505 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1506 u64 ent = pt[i];
1507
1508 if (!(ent & PT_PRESENT_MASK))
1509 continue;
1510 if (!(ent & PT_WRITABLE_MASK))
1511 continue;
1512 ++nmaps;
1513 }
1514 }
1515 return nmaps;
1516}
1517
1518static void audit_rmap(struct kvm_vcpu *vcpu)
1519{
1520 int n_rmap = count_rmaps(vcpu);
1521 int n_actual = count_writable_mappings(vcpu);
1522
1523 if (n_rmap != n_actual)
1524 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1525 __FUNCTION__, audit_msg, n_rmap, n_actual);
1526}
1527
1528static void audit_write_protection(struct kvm_vcpu *vcpu)
1529{
1530 struct kvm_mmu_page *page;
1531
1532 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1533 hfn_t hfn;
1534 struct page *pg;
1535
1536 if (page->role.metaphysical)
1537 continue;
1538
1539 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1540 >> PAGE_SHIFT;
1541 pg = pfn_to_page(hfn);
1542 if (pg->private)
1543 printk(KERN_ERR "%s: (%s) shadow page has writable"
1544 " mappings: gfn %lx role %x\n",
1545 __FUNCTION__, audit_msg, page->gfn,
1546 page->role.word);
1547 }
1548}
1549
1550static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1551{
1552 int olddbg = dbg;
1553
1554 dbg = 0;
1555 audit_msg = msg;
1556 audit_rmap(vcpu);
1557 audit_write_protection(vcpu);
1558 audit_mappings(vcpu);
1559 dbg = olddbg;
1560}
1561
1562#endif