blob: 7761089ef3bcea4b3caddeed860fc218ab338b70 [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 Kivitycea0f0e2007-01-05 16:36:43 -080029#define pgprintk(x...) do { printk(x); } while (0)
30#define rmap_printk(x...) do { printk(x); } while (0)
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
32#define ASSERT(x) \
33 if (!(x)) { \
34 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
35 __FILE__, __LINE__, #x); \
36 }
37
Avi Kivitycea0f0e2007-01-05 16:36:43 -080038#define PT64_PT_BITS 9
39#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
40#define PT32_PT_BITS 10
41#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
43#define PT_WRITABLE_SHIFT 1
44
45#define PT_PRESENT_MASK (1ULL << 0)
46#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
47#define PT_USER_MASK (1ULL << 2)
48#define PT_PWT_MASK (1ULL << 3)
49#define PT_PCD_MASK (1ULL << 4)
50#define PT_ACCESSED_MASK (1ULL << 5)
51#define PT_DIRTY_MASK (1ULL << 6)
52#define PT_PAGE_SIZE_MASK (1ULL << 7)
53#define PT_PAT_MASK (1ULL << 7)
54#define PT_GLOBAL_MASK (1ULL << 8)
55#define PT64_NX_MASK (1ULL << 63)
56
57#define PT_PAT_SHIFT 7
58#define PT_DIR_PAT_SHIFT 12
59#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
60
61#define PT32_DIR_PSE36_SIZE 4
62#define PT32_DIR_PSE36_SHIFT 13
63#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
64
65
66#define PT32_PTE_COPY_MASK \
Avi Kivity8c7bb722006-12-13 00:34:02 -080067 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
Avi Kivity8c7bb722006-12-13 00:34:02 -080069#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
71#define PT_FIRST_AVAIL_BITS_SHIFT 9
72#define PT64_SECOND_AVAIL_BITS_SHIFT 52
73
74#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
75#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
76
77#define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
78#define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
79
80#define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
81#define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
82
83#define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
84
85#define VALID_PAGE(x) ((x) != INVALID_PAGE)
86
87#define PT64_LEVEL_BITS 9
88
89#define PT64_LEVEL_SHIFT(level) \
90 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
91
92#define PT64_LEVEL_MASK(level) \
93 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
94
95#define PT64_INDEX(address, level)\
96 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
97
98
99#define PT32_LEVEL_BITS 10
100
101#define PT32_LEVEL_SHIFT(level) \
102 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
103
104#define PT32_LEVEL_MASK(level) \
105 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
106
107#define PT32_INDEX(address, level)\
108 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
109
110
111#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & PAGE_MASK)
112#define PT64_DIR_BASE_ADDR_MASK \
113 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
114
115#define PT32_BASE_ADDR_MASK PAGE_MASK
116#define PT32_DIR_BASE_ADDR_MASK \
117 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
118
119
120#define PFERR_PRESENT_MASK (1U << 0)
121#define PFERR_WRITE_MASK (1U << 1)
122#define PFERR_USER_MASK (1U << 2)
123
124#define PT64_ROOT_LEVEL 4
125#define PT32_ROOT_LEVEL 2
126#define PT32E_ROOT_LEVEL 3
127
128#define PT_DIRECTORY_LEVEL 2
129#define PT_PAGE_TABLE_LEVEL 1
130
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800131#define RMAP_EXT 4
132
133struct kvm_rmap_desc {
134 u64 *shadow_ptes[RMAP_EXT];
135 struct kvm_rmap_desc *more;
136};
137
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138static int is_write_protection(struct kvm_vcpu *vcpu)
139{
140 return vcpu->cr0 & CR0_WP_MASK;
141}
142
143static int is_cpuid_PSE36(void)
144{
145 return 1;
146}
147
148static int is_present_pte(unsigned long pte)
149{
150 return pte & PT_PRESENT_MASK;
151}
152
153static int is_writeble_pte(unsigned long pte)
154{
155 return pte & PT_WRITABLE_MASK;
156}
157
158static int is_io_pte(unsigned long pte)
159{
160 return pte & PT_SHADOW_IO_MARK;
161}
162
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163static int is_rmap_pte(u64 pte)
164{
165 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
166 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
167}
168
Avi Kivitye2dec932007-01-05 16:36:54 -0800169static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
170 size_t objsize, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800171{
172 void *obj;
173
174 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800175 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800176 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
177 obj = kzalloc(objsize, GFP_NOWAIT);
178 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800179 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800180 cache->objects[cache->nobjs++] = obj;
181 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800182 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800183}
184
185static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
186{
187 while (mc->nobjs)
188 kfree(mc->objects[--mc->nobjs]);
189}
190
Avi Kivitye2dec932007-01-05 16:36:54 -0800191static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800192{
Avi Kivitye2dec932007-01-05 16:36:54 -0800193 int r;
194
195 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
196 sizeof(struct kvm_pte_chain), 4);
197 if (r)
198 goto out;
199 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
200 sizeof(struct kvm_rmap_desc), 1);
201out:
202 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800203}
204
205static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
206{
207 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
208 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
209}
210
211static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
212 size_t size)
213{
214 void *p;
215
216 BUG_ON(!mc->nobjs);
217 p = mc->objects[--mc->nobjs];
218 memset(p, 0, size);
219 return p;
220}
221
222static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
223{
224 if (mc->nobjs < KVM_NR_MEM_OBJS)
225 mc->objects[mc->nobjs++] = obj;
226 else
227 kfree(obj);
228}
229
230static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
231{
232 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
233 sizeof(struct kvm_pte_chain));
234}
235
236static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
237 struct kvm_pte_chain *pc)
238{
239 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
240}
241
242static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
243{
244 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
245 sizeof(struct kvm_rmap_desc));
246}
247
248static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
249 struct kvm_rmap_desc *rd)
250{
251 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
252}
253
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800254/*
255 * Reverse mapping data structures:
256 *
257 * If page->private bit zero is zero, then page->private points to the
258 * shadow page table entry that points to page_address(page).
259 *
260 * If page->private bit zero is one, (then page->private & ~1) points
261 * to a struct kvm_rmap_desc containing more mappings.
262 */
Avi Kivity714b93d2007-01-05 16:36:53 -0800263static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800264{
265 struct page *page;
266 struct kvm_rmap_desc *desc;
267 int i;
268
269 if (!is_rmap_pte(*spte))
270 return;
271 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
272 if (!page->private) {
273 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
274 page->private = (unsigned long)spte;
275 } else if (!(page->private & 1)) {
276 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800277 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800278 desc->shadow_ptes[0] = (u64 *)page->private;
279 desc->shadow_ptes[1] = spte;
280 page->private = (unsigned long)desc | 1;
281 } else {
282 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
283 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
284 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
285 desc = desc->more;
286 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800287 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800288 desc = desc->more;
289 }
290 for (i = 0; desc->shadow_ptes[i]; ++i)
291 ;
292 desc->shadow_ptes[i] = spte;
293 }
294}
295
Avi Kivity714b93d2007-01-05 16:36:53 -0800296static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
297 struct page *page,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800298 struct kvm_rmap_desc *desc,
299 int i,
300 struct kvm_rmap_desc *prev_desc)
301{
302 int j;
303
304 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
305 ;
306 desc->shadow_ptes[i] = desc->shadow_ptes[j];
307 desc->shadow_ptes[j] = 0;
308 if (j != 0)
309 return;
310 if (!prev_desc && !desc->more)
311 page->private = (unsigned long)desc->shadow_ptes[0];
312 else
313 if (prev_desc)
314 prev_desc->more = desc->more;
315 else
316 page->private = (unsigned long)desc->more | 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800317 mmu_free_rmap_desc(vcpu, desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800318}
319
Avi Kivity714b93d2007-01-05 16:36:53 -0800320static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800321{
322 struct page *page;
323 struct kvm_rmap_desc *desc;
324 struct kvm_rmap_desc *prev_desc;
325 int i;
326
327 if (!is_rmap_pte(*spte))
328 return;
329 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
330 if (!page->private) {
331 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
332 BUG();
333 } else if (!(page->private & 1)) {
334 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
335 if ((u64 *)page->private != spte) {
336 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
337 spte, *spte);
338 BUG();
339 }
340 page->private = 0;
341 } else {
342 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
343 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
344 prev_desc = NULL;
345 while (desc) {
346 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
347 if (desc->shadow_ptes[i] == spte) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800348 rmap_desc_remove_entry(vcpu, page,
349 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350 prev_desc);
351 return;
352 }
353 prev_desc = desc;
354 desc = desc->more;
355 }
356 BUG();
357 }
358}
359
Avi Kivity714b93d2007-01-05 16:36:53 -0800360static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800361{
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 struct kvm *kvm = vcpu->kvm;
Avi Kivity374cbac2007-01-05 16:36:43 -0800363 struct page *page;
364 struct kvm_memory_slot *slot;
365 struct kvm_rmap_desc *desc;
366 u64 *spte;
367
368 slot = gfn_to_memslot(kvm, gfn);
369 BUG_ON(!slot);
370 page = gfn_to_page(slot, gfn);
371
372 while (page->private) {
373 if (!(page->private & 1))
374 spte = (u64 *)page->private;
375 else {
376 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
377 spte = desc->shadow_ptes[0];
378 }
379 BUG_ON(!spte);
380 BUG_ON((*spte & PT64_BASE_ADDR_MASK) !=
381 page_to_pfn(page) << PAGE_SHIFT);
382 BUG_ON(!(*spte & PT_PRESENT_MASK));
383 BUG_ON(!(*spte & PT_WRITABLE_MASK));
384 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800385 rmap_remove(vcpu, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800386 *spte &= ~(u64)PT_WRITABLE_MASK;
387 }
388}
389
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390static int is_empty_shadow_page(hpa_t page_hpa)
391{
Avi Kivity139bdb22007-01-05 16:36:50 -0800392 u64 *pos;
393 u64 *end;
394
395 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 pos != end; pos++)
Avi Kivity139bdb22007-01-05 16:36:50 -0800397 if (*pos != 0) {
398 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
399 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800401 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 return 1;
403}
404
Avi Kivity260746c2007-01-05 16:36:49 -0800405static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
406{
407 struct kvm_mmu_page *page_head = page_header(page_hpa);
408
Avi Kivity5f1e0b62007-01-05 16:36:49 -0800409 ASSERT(is_empty_shadow_page(page_hpa));
Avi Kivity260746c2007-01-05 16:36:49 -0800410 list_del(&page_head->link);
411 page_head->page_hpa = page_hpa;
412 list_add(&page_head->link, &vcpu->free_pages);
413 ++vcpu->kvm->n_free_mmu_pages;
414}
415
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800416static unsigned kvm_page_table_hashfn(gfn_t gfn)
417{
418 return gfn;
419}
420
Avi Kivity25c0de22007-01-05 16:36:42 -0800421static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
422 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423{
424 struct kvm_mmu_page *page;
425
426 if (list_empty(&vcpu->free_pages))
Avi Kivity25c0de22007-01-05 16:36:42 -0800427 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428
429 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
430 list_del(&page->link);
431 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
432 ASSERT(is_empty_shadow_page(page->page_hpa));
433 page->slot_bitmap = 0;
434 page->global = 1;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800435 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800437 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800438 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439}
440
Avi Kivity714b93d2007-01-05 16:36:53 -0800441static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
442 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800443{
444 struct kvm_pte_chain *pte_chain;
445 struct hlist_node *node;
446 int i;
447
448 if (!parent_pte)
449 return;
450 if (!page->multimapped) {
451 u64 *old = page->parent_pte;
452
453 if (!old) {
454 page->parent_pte = parent_pte;
455 return;
456 }
457 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800458 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800459 INIT_HLIST_HEAD(&page->parent_ptes);
460 hlist_add_head(&pte_chain->link, &page->parent_ptes);
461 pte_chain->parent_ptes[0] = old;
462 }
463 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
464 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
465 continue;
466 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
467 if (!pte_chain->parent_ptes[i]) {
468 pte_chain->parent_ptes[i] = parent_pte;
469 return;
470 }
471 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800472 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800473 BUG_ON(!pte_chain);
474 hlist_add_head(&pte_chain->link, &page->parent_ptes);
475 pte_chain->parent_ptes[0] = parent_pte;
476}
477
Avi Kivity714b93d2007-01-05 16:36:53 -0800478static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
479 struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800480 u64 *parent_pte)
481{
482 struct kvm_pte_chain *pte_chain;
483 struct hlist_node *node;
484 int i;
485
486 if (!page->multimapped) {
487 BUG_ON(page->parent_pte != parent_pte);
488 page->parent_pte = NULL;
489 return;
490 }
491 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
492 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
493 if (!pte_chain->parent_ptes[i])
494 break;
495 if (pte_chain->parent_ptes[i] != parent_pte)
496 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800497 while (i + 1 < NR_PTE_CHAIN_ENTRIES
498 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800499 pte_chain->parent_ptes[i]
500 = pte_chain->parent_ptes[i + 1];
501 ++i;
502 }
503 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800504 if (i == 0) {
505 hlist_del(&pte_chain->link);
Avi Kivity714b93d2007-01-05 16:36:53 -0800506 mmu_free_pte_chain(vcpu, pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800507 if (hlist_empty(&page->parent_ptes)) {
508 page->multimapped = 0;
509 page->parent_pte = NULL;
510 }
511 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800512 return;
513 }
514 BUG();
515}
516
517static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
518 gfn_t gfn)
519{
520 unsigned index;
521 struct hlist_head *bucket;
522 struct kvm_mmu_page *page;
523 struct hlist_node *node;
524
525 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
526 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
527 bucket = &vcpu->kvm->mmu_page_hash[index];
528 hlist_for_each_entry(page, node, bucket, hash_link)
529 if (page->gfn == gfn && !page->role.metaphysical) {
530 pgprintk("%s: found role %x\n",
531 __FUNCTION__, page->role.word);
532 return page;
533 }
534 return NULL;
535}
536
537static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
538 gfn_t gfn,
539 gva_t gaddr,
540 unsigned level,
541 int metaphysical,
542 u64 *parent_pte)
543{
544 union kvm_mmu_page_role role;
545 unsigned index;
546 unsigned quadrant;
547 struct hlist_head *bucket;
548 struct kvm_mmu_page *page;
549 struct hlist_node *node;
550
551 role.word = 0;
552 role.glevels = vcpu->mmu.root_level;
553 role.level = level;
554 role.metaphysical = metaphysical;
555 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
556 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
557 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
558 role.quadrant = quadrant;
559 }
560 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
561 gfn, role.word);
562 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
563 bucket = &vcpu->kvm->mmu_page_hash[index];
564 hlist_for_each_entry(page, node, bucket, hash_link)
565 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800566 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800567 pgprintk("%s: found\n", __FUNCTION__);
568 return page;
569 }
570 page = kvm_mmu_alloc_page(vcpu, parent_pte);
571 if (!page)
572 return page;
573 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
574 page->gfn = gfn;
575 page->role = role;
576 hlist_add_head(&page->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800577 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800578 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800579 return page;
580}
581
Avi Kivitya4360362007-01-05 16:36:45 -0800582static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
583 struct kvm_mmu_page *page)
584{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800585 unsigned i;
586 u64 *pt;
587 u64 ent;
588
589 pt = __va(page->page_hpa);
590
591 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
592 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
593 if (pt[i] & PT_PRESENT_MASK)
Avi Kivity714b93d2007-01-05 16:36:53 -0800594 rmap_remove(vcpu, &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800595 pt[i] = 0;
596 }
597 return;
598 }
599
600 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
601 ent = pt[i];
602
603 pt[i] = 0;
604 if (!(ent & PT_PRESENT_MASK))
605 continue;
606 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800607 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800608 }
Avi Kivitya4360362007-01-05 16:36:45 -0800609}
610
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800611static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
612 struct kvm_mmu_page *page,
613 u64 *parent_pte)
614{
Avi Kivity714b93d2007-01-05 16:36:53 -0800615 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800616}
617
618static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
619 struct kvm_mmu_page *page)
620{
621 u64 *parent_pte;
622
623 while (page->multimapped || page->parent_pte) {
624 if (!page->multimapped)
625 parent_pte = page->parent_pte;
626 else {
627 struct kvm_pte_chain *chain;
628
629 chain = container_of(page->parent_ptes.first,
630 struct kvm_pte_chain, link);
631 parent_pte = chain->parent_ptes[0];
632 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800633 BUG_ON(!parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800634 kvm_mmu_put_page(vcpu, page, parent_pte);
635 *parent_pte = 0;
636 }
Avi Kivitycc4529e2007-01-05 16:36:47 -0800637 kvm_mmu_page_unlink_children(vcpu, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800638 if (!page->root_count) {
639 hlist_del(&page->hash_link);
640 kvm_mmu_free_page(vcpu, page->page_hpa);
641 } else {
642 list_del(&page->link);
643 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
644 }
Avi Kivitya4360362007-01-05 16:36:45 -0800645}
646
647static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
648{
649 unsigned index;
650 struct hlist_head *bucket;
651 struct kvm_mmu_page *page;
652 struct hlist_node *node, *n;
653 int r;
654
655 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
656 r = 0;
657 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
658 bucket = &vcpu->kvm->mmu_page_hash[index];
659 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
660 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800661 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
662 page->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -0800663 kvm_mmu_zap_page(vcpu, page);
664 r = 1;
665 }
666 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800667}
668
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
670{
671 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
672 struct kvm_mmu_page *page_head = page_header(__pa(pte));
673
674 __set_bit(slot, &page_head->slot_bitmap);
675}
676
677hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
678{
679 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
680
681 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
682}
683
684hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
685{
686 struct kvm_memory_slot *slot;
687 struct page *page;
688
689 ASSERT((gpa & HPA_ERR_MASK) == 0);
690 slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
691 if (!slot)
692 return gpa | HPA_ERR_MASK;
693 page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
694 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
695 | (gpa & (PAGE_SIZE-1));
696}
697
698hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
699{
700 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
701
702 if (gpa == UNMAPPED_GVA)
703 return UNMAPPED_GVA;
704 return gpa_to_hpa(vcpu, gpa);
705}
706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
708{
709}
710
711static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
712{
713 int level = PT32E_ROOT_LEVEL;
714 hpa_t table_addr = vcpu->mmu.root_hpa;
715
716 for (; ; level--) {
717 u32 index = PT64_INDEX(v, level);
718 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800719 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720
721 ASSERT(VALID_PAGE(table_addr));
722 table = __va(table_addr);
723
724 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800725 pte = table[index];
726 if (is_present_pte(pte) && is_writeble_pte(pte))
727 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
729 page_header_update_slot(vcpu->kvm, table, v);
730 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
731 PT_USER_MASK;
Avi Kivity714b93d2007-01-05 16:36:53 -0800732 rmap_add(vcpu, &table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 return 0;
734 }
735
736 if (table[index] == 0) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800737 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800738 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800740 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
741 >> PAGE_SHIFT;
742 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
743 v, level - 1,
744 1, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800745 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 pgprintk("nonpaging_map: ENOMEM\n");
747 return -ENOMEM;
748 }
749
Avi Kivity25c0de22007-01-05 16:36:42 -0800750 table[index] = new_table->page_hpa | PT_PRESENT_MASK
751 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 }
753 table_addr = table[index] & PT64_BASE_ADDR_MASK;
754 }
755}
756
Avi Kivity17ac10a2007-01-05 16:36:40 -0800757static void mmu_free_roots(struct kvm_vcpu *vcpu)
758{
759 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800760 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800761
762#ifdef CONFIG_X86_64
763 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
764 hpa_t root = vcpu->mmu.root_hpa;
765
766 ASSERT(VALID_PAGE(root));
Avi Kivity3bb65a22007-01-05 16:36:51 -0800767 page = page_header(root);
768 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800769 vcpu->mmu.root_hpa = INVALID_PAGE;
770 return;
771 }
772#endif
773 for (i = 0; i < 4; ++i) {
774 hpa_t root = vcpu->mmu.pae_root[i];
775
776 ASSERT(VALID_PAGE(root));
777 root &= PT64_BASE_ADDR_MASK;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800778 page = page_header(root);
779 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800780 vcpu->mmu.pae_root[i] = INVALID_PAGE;
781 }
782 vcpu->mmu.root_hpa = INVALID_PAGE;
783}
784
785static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
786{
787 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800788 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800789 struct kvm_mmu_page *page;
790
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800791 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800792
793#ifdef CONFIG_X86_64
794 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
795 hpa_t root = vcpu->mmu.root_hpa;
796
797 ASSERT(!VALID_PAGE(root));
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800798 root = kvm_mmu_get_page(vcpu, root_gfn, 0,
799 PT64_ROOT_LEVEL, 0, NULL)->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800800 page = page_header(root);
801 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800802 vcpu->mmu.root_hpa = root;
803 return;
804 }
805#endif
806 for (i = 0; i < 4; ++i) {
807 hpa_t root = vcpu->mmu.pae_root[i];
808
809 ASSERT(!VALID_PAGE(root));
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800810 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
811 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
812 else if (vcpu->mmu.root_level == 0)
813 root_gfn = 0;
814 root = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
815 PT32_ROOT_LEVEL, !is_paging(vcpu),
816 NULL)->page_hpa;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800817 page = page_header(root);
818 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800819 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
820 }
821 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
822}
823
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
825{
826 return vaddr;
827}
828
829static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
830 u32 error_code)
831{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800833 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800834 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835
Avi Kivitye2dec932007-01-05 16:36:54 -0800836 r = mmu_topup_memory_caches(vcpu);
837 if (r)
838 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800839
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 ASSERT(vcpu);
841 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
842
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843
Avi Kivityebeace82007-01-05 16:36:47 -0800844 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845
Avi Kivityebeace82007-01-05 16:36:47 -0800846 if (is_error_hpa(paddr))
847 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
Avi Kivityebeace82007-01-05 16:36:47 -0800849 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850}
851
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852static void nonpaging_free(struct kvm_vcpu *vcpu)
853{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800854 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855}
856
857static int nonpaging_init_context(struct kvm_vcpu *vcpu)
858{
859 struct kvm_mmu *context = &vcpu->mmu;
860
861 context->new_cr3 = nonpaging_new_cr3;
862 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 context->gva_to_gpa = nonpaging_gva_to_gpa;
864 context->free = nonpaging_free;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800865 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800867 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 ASSERT(VALID_PAGE(context->root_hpa));
869 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
870 return 0;
871}
872
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
874{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875 ++kvm_stat.tlb_flush;
876 kvm_arch_ops->tlb_flush(vcpu);
877}
878
879static void paging_new_cr3(struct kvm_vcpu *vcpu)
880{
Avi Kivity374cbac2007-01-05 16:36:43 -0800881 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800882 mmu_free_roots(vcpu);
883 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 kvm_mmu_flush_tlb(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800885 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886}
887
888static void mark_pagetable_nonglobal(void *shadow_pte)
889{
890 page_header(__pa(shadow_pte))->global = 0;
891}
892
893static inline void set_pte_common(struct kvm_vcpu *vcpu,
894 u64 *shadow_pte,
895 gpa_t gaddr,
896 int dirty,
Avi Kivity815af8d2007-01-05 16:36:44 -0800897 u64 access_bits,
898 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899{
900 hpa_t paddr;
901
902 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
903 if (!dirty)
904 access_bits &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905
906 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
907
Avi Kivity374cbac2007-01-05 16:36:43 -0800908 *shadow_pte |= access_bits;
909
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 if (!(*shadow_pte & PT_GLOBAL_MASK))
911 mark_pagetable_nonglobal(shadow_pte);
912
913 if (is_error_hpa(paddr)) {
914 *shadow_pte |= gaddr;
915 *shadow_pte |= PT_SHADOW_IO_MARK;
916 *shadow_pte &= ~PT_PRESENT_MASK;
Avi Kivity374cbac2007-01-05 16:36:43 -0800917 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 }
Avi Kivity374cbac2007-01-05 16:36:43 -0800919
920 *shadow_pte |= paddr;
921
922 if (access_bits & PT_WRITABLE_MASK) {
923 struct kvm_mmu_page *shadow;
924
Avi Kivity815af8d2007-01-05 16:36:44 -0800925 shadow = kvm_mmu_lookup_page(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800926 if (shadow) {
927 pgprintk("%s: found shadow page for %lx, marking ro\n",
Avi Kivity815af8d2007-01-05 16:36:44 -0800928 __FUNCTION__, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800929 access_bits &= ~PT_WRITABLE_MASK;
930 *shadow_pte &= ~PT_WRITABLE_MASK;
931 }
932 }
933
934 if (access_bits & PT_WRITABLE_MASK)
935 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
936
937 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
Avi Kivity714b93d2007-01-05 16:36:53 -0800938 rmap_add(vcpu, shadow_pte);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939}
940
941static void inject_page_fault(struct kvm_vcpu *vcpu,
942 u64 addr,
943 u32 err_code)
944{
945 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
946}
947
948static inline int fix_read_pf(u64 *shadow_ent)
949{
950 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
951 !(*shadow_ent & PT_USER_MASK)) {
952 /*
953 * If supervisor write protect is disabled, we shadow kernel
954 * pages as user pages so we can trap the write access.
955 */
956 *shadow_ent |= PT_USER_MASK;
957 *shadow_ent &= ~PT_WRITABLE_MASK;
958
959 return 1;
960
961 }
962 return 0;
963}
964
965static int may_access(u64 pte, int write, int user)
966{
967
968 if (user && !(pte & PT_USER_MASK))
969 return 0;
970 if (write && !(pte & PT_WRITABLE_MASK))
971 return 0;
972 return 1;
973}
974
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975static void paging_free(struct kvm_vcpu *vcpu)
976{
977 nonpaging_free(vcpu);
978}
979
980#define PTTYPE 64
981#include "paging_tmpl.h"
982#undef PTTYPE
983
984#define PTTYPE 32
985#include "paging_tmpl.h"
986#undef PTTYPE
987
Avi Kivity17ac10a2007-01-05 16:36:40 -0800988static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989{
990 struct kvm_mmu *context = &vcpu->mmu;
991
992 ASSERT(is_pae(vcpu));
993 context->new_cr3 = paging_new_cr3;
994 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 context->gva_to_gpa = paging64_gva_to_gpa;
996 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800997 context->root_level = level;
998 context->shadow_root_level = level;
999 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 ASSERT(VALID_PAGE(context->root_hpa));
1001 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1002 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1003 return 0;
1004}
1005
Avi Kivity17ac10a2007-01-05 16:36:40 -08001006static int paging64_init_context(struct kvm_vcpu *vcpu)
1007{
1008 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1009}
1010
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011static int paging32_init_context(struct kvm_vcpu *vcpu)
1012{
1013 struct kvm_mmu *context = &vcpu->mmu;
1014
1015 context->new_cr3 = paging_new_cr3;
1016 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 context->gva_to_gpa = paging32_gva_to_gpa;
1018 context->free = paging_free;
1019 context->root_level = PT32_ROOT_LEVEL;
1020 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001021 mmu_alloc_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 ASSERT(VALID_PAGE(context->root_hpa));
1023 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1024 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1025 return 0;
1026}
1027
1028static int paging32E_init_context(struct kvm_vcpu *vcpu)
1029{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001030 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
1033static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1034{
1035 ASSERT(vcpu);
1036 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1037
1038 if (!is_paging(vcpu))
1039 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001040 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 return paging64_init_context(vcpu);
1042 else if (is_pae(vcpu))
1043 return paging32E_init_context(vcpu);
1044 else
1045 return paging32_init_context(vcpu);
1046}
1047
1048static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1049{
1050 ASSERT(vcpu);
1051 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1052 vcpu->mmu.free(vcpu);
1053 vcpu->mmu.root_hpa = INVALID_PAGE;
1054 }
1055}
1056
1057int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1058{
Avi Kivity714b93d2007-01-05 16:36:53 -08001059 int r;
1060
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 destroy_kvm_mmu(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001062 r = init_kvm_mmu(vcpu);
1063 if (r < 0)
1064 goto out;
Avi Kivitye2dec932007-01-05 16:36:54 -08001065 r = mmu_topup_memory_caches(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001066out:
1067 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068}
1069
Avi Kivityda4a00f2007-01-05 16:36:44 -08001070void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1071{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001072 gfn_t gfn = gpa >> PAGE_SHIFT;
1073 struct kvm_mmu_page *page;
1074 struct kvm_mmu_page *child;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001075 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001076 struct hlist_head *bucket;
1077 unsigned index;
1078 u64 *spte;
1079 u64 pte;
1080 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001081 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001082 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001083 unsigned misaligned;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001084 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001085 int flooded = 0;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001086
Avi Kivityda4a00f2007-01-05 16:36:44 -08001087 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001088 if (gfn == vcpu->last_pt_write_gfn) {
1089 ++vcpu->last_pt_write_count;
1090 if (vcpu->last_pt_write_count >= 3)
1091 flooded = 1;
1092 } else {
1093 vcpu->last_pt_write_gfn = gfn;
1094 vcpu->last_pt_write_count = 1;
1095 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001096 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1097 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001098 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001099 if (page->gfn != gfn || page->role.metaphysical)
1100 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001101 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1102 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivity86a5ba02007-01-05 16:36:50 -08001103 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001104 /*
1105 * Misaligned accesses are too much trouble to fix
1106 * up; also, they usually indicate a page is not used
1107 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001108 *
1109 * If we're seeing too many writes to a page,
1110 * it may no longer be a page table, or we may be
1111 * forking, in which case it is better to unmap the
1112 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001113 */
1114 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1115 gpa, bytes, page->role.word);
1116 kvm_mmu_zap_page(vcpu, page);
1117 continue;
1118 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001119 page_offset = offset;
1120 level = page->role.level;
1121 if (page->role.glevels == PT32_ROOT_LEVEL) {
1122 page_offset <<= 1; /* 32->64 */
1123 page_offset &= ~PAGE_MASK;
1124 }
1125 spte = __va(page->page_hpa);
1126 spte += page_offset / sizeof(*spte);
1127 pte = *spte;
1128 if (is_present_pte(pte)) {
1129 if (level == PT_PAGE_TABLE_LEVEL)
Avi Kivity714b93d2007-01-05 16:36:53 -08001130 rmap_remove(vcpu, spte);
Avi Kivity9b7a0322007-01-05 16:36:45 -08001131 else {
1132 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity714b93d2007-01-05 16:36:53 -08001133 mmu_page_remove_parent_pte(vcpu, child, spte);
Avi Kivity9b7a0322007-01-05 16:36:45 -08001134 }
1135 }
1136 *spte = 0;
1137 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001138}
1139
1140void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1141{
1142}
1143
Avi Kivitya4360362007-01-05 16:36:45 -08001144int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1145{
1146 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1147
1148 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1149}
1150
Avi Kivityebeace82007-01-05 16:36:47 -08001151void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1152{
1153 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1154 struct kvm_mmu_page *page;
1155
1156 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1157 struct kvm_mmu_page, link);
1158 kvm_mmu_zap_page(vcpu, page);
1159 }
1160}
1161EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1162
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163static void free_mmu_pages(struct kvm_vcpu *vcpu)
1164{
Avi Kivityf51234c2007-01-05 16:36:52 -08001165 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166
Avi Kivityf51234c2007-01-05 16:36:52 -08001167 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1168 page = container_of(vcpu->kvm->active_mmu_pages.next,
1169 struct kvm_mmu_page, link);
1170 kvm_mmu_zap_page(vcpu, page);
1171 }
1172 while (!list_empty(&vcpu->free_pages)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 page = list_entry(vcpu->free_pages.next,
1174 struct kvm_mmu_page, link);
1175 list_del(&page->link);
1176 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1177 page->page_hpa = INVALID_PAGE;
1178 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001179 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180}
1181
1182static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1183{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001184 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 int i;
1186
1187 ASSERT(vcpu);
1188
1189 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1191
1192 INIT_LIST_HEAD(&page_header->link);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001193 if ((page = alloc_page(GFP_KERNEL)) == NULL)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 goto error_1;
1195 page->private = (unsigned long)page_header;
1196 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1197 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1198 list_add(&page_header->link, &vcpu->free_pages);
Avi Kivityebeace82007-01-05 16:36:47 -08001199 ++vcpu->kvm->n_free_mmu_pages;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001201
1202 /*
1203 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1204 * Therefore we need to allocate shadow page tables in the first
1205 * 4GB of memory, which happens to fit the DMA32 zone.
1206 */
1207 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1208 if (!page)
1209 goto error_1;
1210 vcpu->mmu.pae_root = page_address(page);
1211 for (i = 0; i < 4; ++i)
1212 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1213
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 return 0;
1215
1216error_1:
1217 free_mmu_pages(vcpu);
1218 return -ENOMEM;
1219}
1220
Ingo Molnar8018c272006-12-29 16:50:01 -08001221int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 ASSERT(vcpu);
1224 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1225 ASSERT(list_empty(&vcpu->free_pages));
1226
Ingo Molnar8018c272006-12-29 16:50:01 -08001227 return alloc_mmu_pages(vcpu);
1228}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229
Ingo Molnar8018c272006-12-29 16:50:01 -08001230int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1231{
1232 ASSERT(vcpu);
1233 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1234 ASSERT(!list_empty(&vcpu->free_pages));
Avi Kivity2c264952006-12-22 01:05:28 -08001235
Ingo Molnar8018c272006-12-29 16:50:01 -08001236 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237}
1238
1239void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1240{
1241 ASSERT(vcpu);
1242
1243 destroy_kvm_mmu(vcpu);
1244 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001245 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246}
1247
Avi Kivity714b93d2007-01-05 16:36:53 -08001248void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249{
Avi Kivity714b93d2007-01-05 16:36:53 -08001250 struct kvm *kvm = vcpu->kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251 struct kvm_mmu_page *page;
1252
1253 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1254 int i;
1255 u64 *pt;
1256
1257 if (!test_bit(slot, &page->slot_bitmap))
1258 continue;
1259
1260 pt = __va(page->page_hpa);
1261 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1262 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001263 if (pt[i] & PT_WRITABLE_MASK) {
Avi Kivity714b93d2007-01-05 16:36:53 -08001264 rmap_remove(vcpu, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001266 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 }
1268}