blob: c85c6649280ec875268a34d242dc37c711c3e5d2 [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 Kivity47ad8e62007-05-06 15:50:58 +0300442static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443{
Avi Kivity139bdb22007-01-05 16:36:50 -0800444 u64 *pos;
445 u64 *end;
446
Avi Kivity47ad8e62007-05-06 15:50:58 +0300447 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800448 if (*pos != 0) {
449 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
450 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800452 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453 return 1;
454}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800455#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300457static void kvm_mmu_free_page(struct kvm_vcpu *vcpu,
458 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800459{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300460 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivity36868f72007-03-26 19:31:52 +0200461 list_move(&page_head->link, &vcpu->free_pages);
Avi Kivity260746c2007-01-05 16:36:49 -0800462 ++vcpu->kvm->n_free_mmu_pages;
463}
464
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800465static unsigned kvm_page_table_hashfn(gfn_t gfn)
466{
467 return gfn;
468}
469
Avi Kivity25c0de22007-01-05 16:36:42 -0800470static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
471 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472{
473 struct kvm_mmu_page *page;
474
475 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800476 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477
478 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
Avi Kivity36868f72007-03-26 19:31:52 +0200479 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300480 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800482 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800484 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800485 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486}
487
Avi Kivity714b93d2007-01-05 16:36:53 -0800488static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
489 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800490{
491 struct kvm_pte_chain *pte_chain;
492 struct hlist_node *node;
493 int i;
494
495 if (!parent_pte)
496 return;
497 if (!page->multimapped) {
498 u64 *old = page->parent_pte;
499
500 if (!old) {
501 page->parent_pte = parent_pte;
502 return;
503 }
504 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800505 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800506 INIT_HLIST_HEAD(&page->parent_ptes);
507 hlist_add_head(&pte_chain->link, &page->parent_ptes);
508 pte_chain->parent_ptes[0] = old;
509 }
510 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
511 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
512 continue;
513 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
514 if (!pte_chain->parent_ptes[i]) {
515 pte_chain->parent_ptes[i] = parent_pte;
516 return;
517 }
518 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800519 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800520 BUG_ON(!pte_chain);
521 hlist_add_head(&pte_chain->link, &page->parent_ptes);
522 pte_chain->parent_ptes[0] = parent_pte;
523}
524
Avi Kivity714b93d2007-01-05 16:36:53 -0800525static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
526 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800527 u64 *parent_pte)
528{
529 struct kvm_pte_chain *pte_chain;
530 struct hlist_node *node;
531 int i;
532
533 if (!page->multimapped) {
534 BUG_ON(page->parent_pte != parent_pte);
535 page->parent_pte = NULL;
536 return;
537 }
538 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
539 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
540 if (!pte_chain->parent_ptes[i])
541 break;
542 if (pte_chain->parent_ptes[i] != parent_pte)
543 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800544 while (i + 1 < NR_PTE_CHAIN_ENTRIES
545 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800546 pte_chain->parent_ptes[i]
547 = pte_chain->parent_ptes[i + 1];
548 ++i;
549 }
550 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800551 if (i == 0) {
552 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800553 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800554 if (hlist_empty(&page->parent_ptes)) {
555 page->multimapped = 0;
556 page->parent_pte = NULL;
557 }
558 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800559 return;
560 }
561 BUG();
562}
563
564static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
565 gfn_t gfn)
566{
567 unsigned index;
568 struct hlist_head *bucket;
569 struct kvm_mmu_page *page;
570 struct hlist_node *node;
571
572 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
573 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
574 bucket = &vcpu->kvm->mmu_page_hash[index];
575 hlist_for_each_entry(page, node, bucket, hash_link)
576 if (page->gfn == gfn && !page->role.metaphysical) {
577 pgprintk("%s: found role %x\n",
578 __FUNCTION__, page->role.word);
579 return page;
580 }
581 return NULL;
582}
583
584static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
585 gfn_t gfn,
586 gva_t gaddr,
587 unsigned level,
588 int metaphysical,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200589 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800590 u64 *parent_pte)
591{
592 union kvm_mmu_page_role role;
593 unsigned index;
594 unsigned quadrant;
595 struct hlist_head *bucket;
596 struct kvm_mmu_page *page;
597 struct hlist_node *node;
598
599 role.word = 0;
600 role.glevels = vcpu->mmu.root_level;
601 role.level = level;
602 role.metaphysical = metaphysical;
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200603 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800604 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
605 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
606 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
607 role.quadrant = quadrant;
608 }
609 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
610 gfn, role.word);
611 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
612 bucket = &vcpu->kvm->mmu_page_hash[index];
613 hlist_for_each_entry(page, node, bucket, hash_link)
614 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800615 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800616 pgprintk("%s: found\n", __FUNCTION__);
617 return page;
618 }
619 page = kvm_mmu_alloc_page(vcpu, parent_pte);
620 if (!page)
621 return page;
622 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
623 page->gfn = gfn;
624 page->role = role;
625 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800626 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800627 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800628 return page;
629}
630
Avi Kivitya4360362007-01-05 16:36:45 -0800631static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
632 struct kvm_mmu_page *page)
633{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800634 unsigned i;
635 u64 *pt;
636 u64 ent;
637
Avi Kivity47ad8e62007-05-06 15:50:58 +0300638 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800639
640 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
641 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
642 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800643 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800644 pt[i] = 0;
645 }
Avi Kivity40907d52007-01-05 16:36:55 -0800646 kvm_arch_ops->tlb_flush(vcpu);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800647 return;
648 }
649
650 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
651 ent = pt[i];
652
653 pt[i] = 0;
654 if (!(ent & PT_PRESENT_MASK))
655 continue;
656 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800657 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800658 }
Avi Kivitya4360362007-01-05 16:36:45 -0800659}
660
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800661static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
662 struct kvm_mmu_page *page,
663 u64 *parent_pte)
664{
Avi Kivity714b93d2007-01-05 16:36:53 -0800665 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800666}
667
668static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
669 struct kvm_mmu_page *page)
670{
671 u64 *parent_pte;
672
673 while (page->multimapped || page->parent_pte) {
674 if (!page->multimapped)
675 parent_pte = page->parent_pte;
676 else {
677 struct kvm_pte_chain *chain;
678
679 chain = container_of(page->parent_ptes.first,
680 struct kvm_pte_chain, link);
681 parent_pte = chain->parent_ptes[0];
682 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800683 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800684 kvm_mmu_put_page(vcpu, page, parent_pte);
685 *parent_pte = 0;
686 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800687 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800688 if (!page->root_count) {
689 hlist_del(&page->hash_link);
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300690 kvm_mmu_free_page(vcpu, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200691 } else
692 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivitya4360362007-01-05 16:36:45 -0800693}
694
695static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
696{
697 unsigned index;
698 struct hlist_head *bucket;
699 struct kvm_mmu_page *page;
700 struct hlist_node *node, *n;
701 int r;
702
703 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
704 r = 0;
705 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
706 bucket = &vcpu->kvm->mmu_page_hash[index];
707 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
708 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800709 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
710 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800711 kvm_mmu_zap_page(vcpu, page);
712 r = 1;
713 }
714 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800715}
716
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
718{
719 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
720 struct kvm_mmu_page *page_head = page_header(__pa(pte));
721
722 __set_bit(slot, &page_head->slot_bitmap);
723}
724
725hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
726{
727 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
728
729 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
730}
731
732hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
733{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 struct page *page;
735
736 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc22007-03-30 14:02:32 +0300737 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
738 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
741 | (gpa & (PAGE_SIZE-1));
742}
743
744hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
745{
746 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
747
748 if (gpa == UNMAPPED_GVA)
749 return UNMAPPED_GVA;
750 return gpa_to_hpa(vcpu, gpa);
751}
752
Avi Kivity039576c2007-03-20 12:46:50 +0200753struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
754{
755 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
756
757 if (gpa == UNMAPPED_GVA)
758 return NULL;
759 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
760}
761
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
763{
764}
765
766static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
767{
768 int level = PT32E_ROOT_LEVEL;
769 hpa_t table_addr = vcpu->mmu.root_hpa;
770
771 for (; ; level--) {
772 u32 index = PT64_INDEX(v, level);
773 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800774 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775
776 ASSERT(VALID_PAGE(table_addr));
777 table = __va(table_addr);
778
779 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800780 pte = table[index];
781 if (is_present_pte(pte) && is_writeble_pte(pte))
782 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
784 page_header_update_slot(vcpu->kvm, table, v);
785 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
786 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800787 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 return 0;
789 }
790
791 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800792 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800793 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800795 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
796 >> PAGE_SHIFT;
797 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
798 v, level - 1,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200799 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800800 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 pgprintk("nonpaging_map: ENOMEM\n");
802 return -ENOMEM;
803 }
804
Avi Kivity47ad8e62007-05-06 15:50:58 +0300805 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800806 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 }
808 table_addr = table[index] & PT64_BASE_ADDR_MASK;
809 }
810}
811
Avi Kivity17ac10a2007-01-05 16:36:40 -0800812static void mmu_free_roots(struct kvm_vcpu *vcpu)
813{
814 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800815 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800816
817#ifdef CONFIG_X86_64
818 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
819 hpa_t root = vcpu->mmu.root_hpa;
820
821 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800822 page = page_header(root);
823 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800824 vcpu->mmu.root_hpa = INVALID_PAGE;
825 return;
826 }
827#endif
828 for (i = 0; i < 4; ++i) {
829 hpa_t root = vcpu->mmu.pae_root[i];
830
Avi Kivity417726a2007-04-12 17:35:58 +0300831 if (root) {
832 ASSERT(VALID_PAGE(root));
833 root &= PT64_BASE_ADDR_MASK;
834 page = page_header(root);
835 --page->root_count;
836 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800837 vcpu->mmu.pae_root[i] = INVALID_PAGE;
838 }
839 vcpu->mmu.root_hpa = INVALID_PAGE;
840}
841
842static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
843{
844 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800846 struct kvm_mmu_page *page;
847
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800848 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800849
850#ifdef CONFIG_X86_64
851 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
852 hpa_t root = vcpu->mmu.root_hpa;
853
854 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800855 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200856 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300857 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800858 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800859 vcpu->mmu.root_hpa = root;
860 return;
861 }
862#endif
863 for (i = 0; i < 4; ++i) {
864 hpa_t root = vcpu->mmu.pae_root[i];
865
866 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300867 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
868 if (!is_present_pte(vcpu->pdptrs[i])) {
869 vcpu->mmu.pae_root[i] = 0;
870 continue;
871 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800872 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300873 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800874 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800875 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800876 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cf2007-03-23 09:55:25 +0200877 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300878 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800879 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800880 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
881 }
882 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
883}
884
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
886{
887 return vaddr;
888}
889
890static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
891 u32 error_code)
892{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800894 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800895 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896
Avi Kivitye2dec932007-01-05 16:36:54 -0800897 r = mmu_topup_memory_caches(vcpu);
898 if (r)
899 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800900
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 ASSERT(vcpu);
902 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
903
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904
Avi Kivityebeace82007-01-05 16:36:47 -0800905 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906
Avi Kivityebeace82007-01-05 16:36:47 -0800907 if (is_error_hpa(paddr))
908 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909
Avi Kivityebeace82007-01-05 16:36:47 -0800910 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911}
912
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913static void nonpaging_free(struct kvm_vcpu *vcpu)
914{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800915 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916}
917
918static int nonpaging_init_context(struct kvm_vcpu *vcpu)
919{
920 struct kvm_mmu *context = &vcpu->mmu;
921
922 context->new_cr3 = nonpaging_new_cr3;
923 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 context->gva_to_gpa = nonpaging_gva_to_gpa;
925 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800926 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800928 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 ASSERT(VALID_PAGE(context->root_hpa));
930 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
931 return 0;
932}
933
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
935{
Avi Kivity1165f5f2007-04-19 17:27:43 +0300936 ++vcpu->stat.tlb_flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 kvm_arch_ops->tlb_flush(vcpu);
938}
939
940static void paging_new_cr3(struct kvm_vcpu *vcpu)
941{
Avi Kivity374cbac2007-01-05 16:36:43 -0800942 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800943 mmu_free_roots(vcpu);
Ingo Molnar7f7417d2007-01-05 16:36:57 -0800944 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
945 kvm_mmu_free_some_pages(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800948 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949}
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951static inline void set_pte_common(struct kvm_vcpu *vcpu,
952 u64 *shadow_pte,
953 gpa_t gaddr,
954 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800955 u64 access_bits,
956 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957{
958 hpa_t paddr;
959
960 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
961 if (!dirty)
962 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963
964 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
965
Avi Kivity374cbac2007-01-05 16:36:43 -0800966 *shadow_pte |= access_bits;
967
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 if (is_error_hpa(paddr)) {
969 *shadow_pte |= gaddr;
970 *shadow_pte |= PT_SHADOW_IO_MARK;
971 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800972 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800974
975 *shadow_pte |= paddr;
976
977 if (access_bits & PT_WRITABLE_MASK) {
978 struct kvm_mmu_page *shadow;
979
Avi Kivity815af8d2007-01-05 16:36:44 -0800980 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800981 if (shadow) {
982 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800983 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800984 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity40907d52007-01-05 16:36:55 -0800985 if (is_writeble_pte(*shadow_pte)) {
986 *shadow_pte &= ~PT_WRITABLE_MASK;
987 kvm_arch_ops->tlb_flush(vcpu);
988 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800989 }
990 }
991
992 if (access_bits & PT_WRITABLE_MASK)
993 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
994
995 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -0800996 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997}
998
999static void inject_page_fault(struct kvm_vcpu *vcpu,
1000 u64 addr,
1001 u32 err_code)
1002{
1003 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
1004}
1005
1006static inline int fix_read_pf(u64 *shadow_ent)
1007{
1008 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
1009 !(*shadow_ent & PT_USER_MASK)) {
1010 /*
1011 * If supervisor write protect is disabled, we shadow kernel
1012 * pages as user pages so we can trap the write access.
1013 */
1014 *shadow_ent |= PT_USER_MASK;
1015 *shadow_ent &= ~PT_WRITABLE_MASK;
1016
1017 return 1;
1018
1019 }
1020 return 0;
1021}
1022
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023static void paging_free(struct kvm_vcpu *vcpu)
1024{
1025 nonpaging_free(vcpu);
1026}
1027
1028#define PTTYPE 64
1029#include "paging_tmpl.h"
1030#undef PTTYPE
1031
1032#define PTTYPE 32
1033#include "paging_tmpl.h"
1034#undef PTTYPE
1035
Avi Kivity17ac10a2007-01-05 16:36:40 -08001036static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037{
1038 struct kvm_mmu *context = &vcpu->mmu;
1039
1040 ASSERT(is_pae(vcpu));
1041 context->new_cr3 = paging_new_cr3;
1042 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 context->gva_to_gpa = paging64_gva_to_gpa;
1044 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001045 context->root_level = level;
1046 context->shadow_root_level = level;
1047 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 ASSERT(VALID_PAGE(context->root_hpa));
1049 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1050 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1051 return 0;
1052}
1053
Avi Kivity17ac10a2007-01-05 16:36:40 -08001054static int paging64_init_context(struct kvm_vcpu *vcpu)
1055{
1056 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1057}
1058
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059static int paging32_init_context(struct kvm_vcpu *vcpu)
1060{
1061 struct kvm_mmu *context = &vcpu->mmu;
1062
1063 context->new_cr3 = paging_new_cr3;
1064 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 context->gva_to_gpa = paging32_gva_to_gpa;
1066 context->free = paging_free;
1067 context->root_level = PT32_ROOT_LEVEL;
1068 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001069 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 ASSERT(VALID_PAGE(context->root_hpa));
1071 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1072 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1073 return 0;
1074}
1075
1076static int paging32E_init_context(struct kvm_vcpu *vcpu)
1077{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001078 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079}
1080
1081static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1082{
1083 ASSERT(vcpu);
1084 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1085
1086 if (!is_paging(vcpu))
1087 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001088 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 return paging64_init_context(vcpu);
1090 else if (is_pae(vcpu))
1091 return paging32E_init_context(vcpu);
1092 else
1093 return paging32_init_context(vcpu);
1094}
1095
1096static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1097{
1098 ASSERT(vcpu);
1099 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1100 vcpu->mmu.free(vcpu);
1101 vcpu->mmu.root_hpa = INVALID_PAGE;
1102 }
1103}
1104
1105int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1106{
Avi Kivity714b93d2007-01-05 16:36:53 -08001107 int r;
1108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001110 r = init_kvm_mmu(vcpu);
1111 if (r < 0)
1112 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001113 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001114out:
1115 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116}
1117
Avi Kivity09072da2007-05-01 14:16:52 +03001118static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001119 struct kvm_mmu_page *page,
1120 u64 *spte)
1121{
1122 u64 pte;
1123 struct kvm_mmu_page *child;
1124
1125 pte = *spte;
1126 if (is_present_pte(pte)) {
1127 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1128 rmap_remove(vcpu, spte);
1129 else {
1130 child = page_header(pte & PT64_BASE_ADDR_MASK);
1131 mmu_page_remove_parent_pte(vcpu, child, spte);
1132 }
1133 }
1134 *spte = 0;
1135}
1136
Avi Kivity00284252007-05-01 16:53:31 +03001137static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1138 struct kvm_mmu_page *page,
1139 u64 *spte,
1140 const void *new, int bytes)
1141{
1142 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1143 return;
1144
1145 if (page->role.glevels == PT32_ROOT_LEVEL)
1146 paging32_update_pte(vcpu, page, spte, new, bytes);
1147 else
1148 paging64_update_pte(vcpu, page, spte, new, bytes);
1149}
1150
Avi Kivity09072da2007-05-01 14:16:52 +03001151void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1152 const u8 *old, const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001153{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001154 gfn_t gfn = gpa >> PAGE_SHIFT;
1155 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001156 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001157 struct hlist_head *bucket;
1158 unsigned index;
1159 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001160 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001161 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001162 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001163 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001164 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001165 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001166 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001167 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001168
Avi Kivityda4a00f2007-01-05 16:36:44 -08001169 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001170 if (gfn == vcpu->last_pt_write_gfn) {
1171 ++vcpu->last_pt_write_count;
1172 if (vcpu->last_pt_write_count >= 3)
1173 flooded = 1;
1174 } else {
1175 vcpu->last_pt_write_gfn = gfn;
1176 vcpu->last_pt_write_count = 1;
1177 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001178 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1179 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001180 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001181 if (page->gfn != gfn || page->role.metaphysical)
1182 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001183 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1184 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001185 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001186 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001187 /*
1188 * Misaligned accesses are too much trouble to fix
1189 * up; also, they usually indicate a page is not used
1190 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001191 *
1192 * If we're seeing too many writes to a page,
1193 * it may no longer be a page table, or we may be
1194 * forking, in which case it is better to unmap the
1195 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001196 */
1197 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1198 gpa, bytes, page->role.word);
1199 kvm_mmu_zap_page(vcpu, page);
1200 continue;
1201 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001202 page_offset = offset;
1203 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001204 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001205 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001206 page_offset <<= 1; /* 32->64 */
1207 /*
1208 * A 32-bit pde maps 4MB while the shadow pdes map
1209 * only 2MB. So we need to double the offset again
1210 * and zap two pdes instead of one.
1211 */
1212 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001213 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001214 page_offset <<= 1;
1215 npte = 2;
1216 }
Avi Kivityfce06572007-05-01 16:44:05 +03001217 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001218 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001219 if (quadrant != page->role.quadrant)
1220 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001221 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001222 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001223 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001224 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivity00284252007-05-01 16:53:31 +03001225 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
Avi Kivityac1b7142007-03-08 17:13:32 +02001226 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001227 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001228 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001229}
1230
Avi Kivitya4360362007-01-05 16:36:45 -08001231int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1232{
1233 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1234
1235 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1236}
1237
Avi Kivityebeace82007-01-05 16:36:47 -08001238void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1239{
1240 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1241 struct kvm_mmu_page *page;
1242
1243 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1244 struct kvm_mmu_page, link);
1245 kvm_mmu_zap_page(vcpu, page);
1246 }
1247}
1248EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1249
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250static void free_mmu_pages(struct kvm_vcpu *vcpu)
1251{
Avi Kivityf51234c2007-01-05 16:36:52 -08001252 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253
Avi Kivityf51234c2007-01-05 16:36:52 -08001254 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1255 page = container_of(vcpu->kvm->active_mmu_pages.next,
1256 struct kvm_mmu_page, link);
1257 kvm_mmu_zap_page(vcpu, page);
1258 }
1259 while (!list_empty(&vcpu->free_pages)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 page = list_entry(vcpu->free_pages.next,
1261 struct kvm_mmu_page, link);
1262 list_del(&page->link);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001263 free_page((unsigned long)page->spt);
1264 page->spt = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001266 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267}
1268
1269static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1270{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001271 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 int i;
1273
1274 ASSERT(vcpu);
1275
1276 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1278
1279 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001280 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 goto error_1;
Markus Rechberger5972e952007-02-19 14:37:47 +02001282 set_page_private(page, (unsigned long)page_header);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001283 page_header->spt = page_address(page);
1284 memset(page_header->spt, 0, PAGE_SIZE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001286 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001288
1289 /*
1290 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1291 * Therefore we need to allocate shadow page tables in the first
1292 * 4GB of memory, which happens to fit the DMA32 zone.
1293 */
1294 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1295 if (!page)
1296 goto error_1;
1297 vcpu->mmu.pae_root = page_address(page);
1298 for (i = 0; i < 4; ++i)
1299 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1300
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 return 0;
1302
1303error_1:
1304 free_mmu_pages(vcpu);
1305 return -ENOMEM;
1306}
1307
Ingo Molnar8018c272006-12-29 16:50:01 -08001308int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 ASSERT(vcpu);
1311 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1312 ASSERT(list_empty(&vcpu->free_pages));
1313
Ingo Molnar8018c272006-12-29 16:50:01 -08001314 return alloc_mmu_pages(vcpu);
1315}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316
Ingo Molnar8018c272006-12-29 16:50:01 -08001317int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1318{
1319 ASSERT(vcpu);
1320 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1321 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001322
Ingo Molnar8018c272006-12-29 16:50:01 -08001323 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324}
1325
1326void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1327{
1328 ASSERT(vcpu);
1329
1330 destroy_kvm_mmu(vcpu);
1331 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001332 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
Avi Kivity714b93d2007-01-05 16:36:53 -08001335void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336{
Avi Kivity714b93d2007-01-05 16:36:53 -08001337 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 struct kvm_mmu_page *page;
1339
1340 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1341 int i;
1342 u64 *pt;
1343
1344 if (!test_bit(slot, &page->slot_bitmap))
1345 continue;
1346
Avi Kivity47ad8e62007-05-06 15:50:58 +03001347 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1349 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001350 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001351 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001353 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 }
1355}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001356
Dor Laore0fa8262007-03-30 13:06:33 +03001357void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1358{
1359 destroy_kvm_mmu(vcpu);
1360
1361 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1362 struct kvm_mmu_page *page;
1363
1364 page = container_of(vcpu->kvm->active_mmu_pages.next,
1365 struct kvm_mmu_page, link);
1366 kvm_mmu_zap_page(vcpu, page);
1367 }
1368
1369 mmu_free_memory_caches(vcpu);
1370 kvm_arch_ops->tlb_flush(vcpu);
1371 init_kvm_mmu(vcpu);
1372}
1373
Avi Kivityb5a33a72007-04-15 16:31:09 +03001374void kvm_mmu_module_exit(void)
1375{
1376 if (pte_chain_cache)
1377 kmem_cache_destroy(pte_chain_cache);
1378 if (rmap_desc_cache)
1379 kmem_cache_destroy(rmap_desc_cache);
1380}
1381
1382int kvm_mmu_module_init(void)
1383{
1384 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1385 sizeof(struct kvm_pte_chain),
1386 0, 0, NULL, NULL);
1387 if (!pte_chain_cache)
1388 goto nomem;
1389 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1390 sizeof(struct kvm_rmap_desc),
1391 0, 0, NULL, NULL);
1392 if (!rmap_desc_cache)
1393 goto nomem;
1394
1395 return 0;
1396
1397nomem:
1398 kvm_mmu_module_exit();
1399 return -ENOMEM;
1400}
1401
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001402#ifdef AUDIT
1403
1404static const char *audit_msg;
1405
1406static gva_t canonicalize(gva_t gva)
1407{
1408#ifdef CONFIG_X86_64
1409 gva = (long long)(gva << 16) >> 16;
1410#endif
1411 return gva;
1412}
1413
1414static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1415 gva_t va, int level)
1416{
1417 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1418 int i;
1419 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1420
1421 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1422 u64 ent = pt[i];
1423
Adrian Bunk28076962007-04-28 21:20:48 +02001424 if (!(ent & PT_PRESENT_MASK))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001425 continue;
1426
1427 va = canonicalize(va);
1428 if (level > 1)
1429 audit_mappings_page(vcpu, ent, va, level - 1);
1430 else {
1431 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1432 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1433
1434 if ((ent & PT_PRESENT_MASK)
1435 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1436 printk(KERN_ERR "audit error: (%s) levels %d"
1437 " gva %lx gpa %llx hpa %llx ent %llx\n",
1438 audit_msg, vcpu->mmu.root_level,
1439 va, gpa, hpa, ent);
1440 }
1441 }
1442}
1443
1444static void audit_mappings(struct kvm_vcpu *vcpu)
1445{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001446 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001447
1448 if (vcpu->mmu.root_level == 4)
1449 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1450 else
1451 for (i = 0; i < 4; ++i)
1452 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1453 audit_mappings_page(vcpu,
1454 vcpu->mmu.pae_root[i],
1455 i << 30,
1456 2);
1457}
1458
1459static int count_rmaps(struct kvm_vcpu *vcpu)
1460{
1461 int nmaps = 0;
1462 int i, j, k;
1463
1464 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1465 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1466 struct kvm_rmap_desc *d;
1467
1468 for (j = 0; j < m->npages; ++j) {
1469 struct page *page = m->phys_mem[j];
1470
1471 if (!page->private)
1472 continue;
1473 if (!(page->private & 1)) {
1474 ++nmaps;
1475 continue;
1476 }
1477 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1478 while (d) {
1479 for (k = 0; k < RMAP_EXT; ++k)
1480 if (d->shadow_ptes[k])
1481 ++nmaps;
1482 else
1483 break;
1484 d = d->more;
1485 }
1486 }
1487 }
1488 return nmaps;
1489}
1490
1491static int count_writable_mappings(struct kvm_vcpu *vcpu)
1492{
1493 int nmaps = 0;
1494 struct kvm_mmu_page *page;
1495 int i;
1496
1497 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001498 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001499
1500 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1501 continue;
1502
1503 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1504 u64 ent = pt[i];
1505
1506 if (!(ent & PT_PRESENT_MASK))
1507 continue;
1508 if (!(ent & PT_WRITABLE_MASK))
1509 continue;
1510 ++nmaps;
1511 }
1512 }
1513 return nmaps;
1514}
1515
1516static void audit_rmap(struct kvm_vcpu *vcpu)
1517{
1518 int n_rmap = count_rmaps(vcpu);
1519 int n_actual = count_writable_mappings(vcpu);
1520
1521 if (n_rmap != n_actual)
1522 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1523 __FUNCTION__, audit_msg, n_rmap, n_actual);
1524}
1525
1526static void audit_write_protection(struct kvm_vcpu *vcpu)
1527{
1528 struct kvm_mmu_page *page;
1529
1530 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1531 hfn_t hfn;
1532 struct page *pg;
1533
1534 if (page->role.metaphysical)
1535 continue;
1536
1537 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1538 >> PAGE_SHIFT;
1539 pg = pfn_to_page(hfn);
1540 if (pg->private)
1541 printk(KERN_ERR "%s: (%s) shadow page has writable"
1542 " mappings: gfn %lx role %x\n",
1543 __FUNCTION__, audit_msg, page->gfn,
1544 page->role.word);
1545 }
1546}
1547
1548static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1549{
1550 int olddbg = dbg;
1551
1552 dbg = 0;
1553 audit_msg = msg;
1554 audit_rmap(vcpu);
1555 audit_write_protection(vcpu);
1556 audit_mappings(vcpu);
1557 dbg = olddbg;
1558}
1559
1560#endif