blob: e6616a6c9cefbdabaf1ebeddc6fd44a607d60e33 [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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
21#include "kvm.h"
22
Avi Kivitye4956062007-06-28 14:15:57 -040023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <linux/module.h>
28
29#include <asm/page.h>
30#include <asm/cmpxchg.h>
31
Avi Kivity37a7d8b2007-01-05 16:36:56 -080032#undef MMU_DEBUG
33
34#undef AUDIT
35
36#ifdef AUDIT
37static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
38#else
39static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
40#endif
41
42#ifdef MMU_DEBUG
43
44#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
45#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
46
47#else
48
49#define pgprintk(x...) do { } while (0)
50#define rmap_printk(x...) do { } while (0)
51
52#endif
53
54#if defined(MMU_DEBUG) || defined(AUDIT)
55static int dbg = 1;
56#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080058#ifndef MMU_DEBUG
59#define ASSERT(x) do { } while (0)
60#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080061#define ASSERT(x) \
62 if (!(x)) { \
63 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
64 __FILE__, __LINE__, #x); \
65 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080066#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
Avi Kivitycea0f0e2007-01-05 16:36:43 -080068#define PT64_PT_BITS 9
69#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
70#define PT32_PT_BITS 10
71#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
73#define PT_WRITABLE_SHIFT 1
74
75#define PT_PRESENT_MASK (1ULL << 0)
76#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
77#define PT_USER_MASK (1ULL << 2)
78#define PT_PWT_MASK (1ULL << 3)
79#define PT_PCD_MASK (1ULL << 4)
80#define PT_ACCESSED_MASK (1ULL << 5)
81#define PT_DIRTY_MASK (1ULL << 6)
82#define PT_PAGE_SIZE_MASK (1ULL << 7)
83#define PT_PAT_MASK (1ULL << 7)
84#define PT_GLOBAL_MASK (1ULL << 8)
85#define PT64_NX_MASK (1ULL << 63)
86
87#define PT_PAT_SHIFT 7
88#define PT_DIR_PAT_SHIFT 12
89#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
90
91#define PT32_DIR_PSE36_SIZE 4
92#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040093#define PT32_DIR_PSE36_MASK \
94 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
96
Avi Kivity6aa8b732006-12-10 02:21:36 -080097#define PT_FIRST_AVAIL_BITS_SHIFT 9
98#define PT64_SECOND_AVAIL_BITS_SHIFT 52
99
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
101
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102#define VALID_PAGE(x) ((x) != INVALID_PAGE)
103
104#define PT64_LEVEL_BITS 9
105
106#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400107 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108
109#define PT64_LEVEL_MASK(level) \
110 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
111
112#define PT64_INDEX(address, level)\
113 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
114
115
116#define PT32_LEVEL_BITS 10
117
118#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400119 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120
121#define PT32_LEVEL_MASK(level) \
122 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
123
124#define PT32_INDEX(address, level)\
125 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
126
127
Avi Kivity27aba762007-03-09 13:04:31 +0200128#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129#define PT64_DIR_BASE_ADDR_MASK \
130 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
131
132#define PT32_BASE_ADDR_MASK PAGE_MASK
133#define PT32_DIR_BASE_ADDR_MASK \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
135
136
137#define PFERR_PRESENT_MASK (1U << 0)
138#define PFERR_WRITE_MASK (1U << 1)
139#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800140#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141
142#define PT64_ROOT_LEVEL 4
143#define PT32_ROOT_LEVEL 2
144#define PT32E_ROOT_LEVEL 3
145
146#define PT_DIRECTORY_LEVEL 2
147#define PT_PAGE_TABLE_LEVEL 1
148
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800149#define RMAP_EXT 4
150
151struct kvm_rmap_desc {
152 u64 *shadow_ptes[RMAP_EXT];
153 struct kvm_rmap_desc *more;
154};
155
Avi Kivityb5a33a72007-04-15 16:31:09 +0300156static struct kmem_cache *pte_chain_cache;
157static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300158static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300159
Avi Kivityc7addb92007-09-16 18:58:32 +0200160static u64 __read_mostly shadow_trap_nonpresent_pte;
161static u64 __read_mostly shadow_notrap_nonpresent_pte;
162
163void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
164{
165 shadow_trap_nonpresent_pte = trap_pte;
166 shadow_notrap_nonpresent_pte = notrap_pte;
167}
168EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
169
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static int is_write_protection(struct kvm_vcpu *vcpu)
171{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000172 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173}
174
175static int is_cpuid_PSE36(void)
176{
177 return 1;
178}
179
Avi Kivity73b10872007-01-26 00:56:41 -0800180static int is_nx(struct kvm_vcpu *vcpu)
181{
182 return vcpu->shadow_efer & EFER_NX;
183}
184
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static int is_present_pte(unsigned long pte)
186{
187 return pte & PT_PRESENT_MASK;
188}
189
Avi Kivityc7addb92007-09-16 18:58:32 +0200190static int is_shadow_present_pte(u64 pte)
191{
192 pte &= ~PT_SHADOW_IO_MARK;
193 return pte != shadow_trap_nonpresent_pte
194 && pte != shadow_notrap_nonpresent_pte;
195}
196
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197static int is_writeble_pte(unsigned long pte)
198{
199 return pte & PT_WRITABLE_MASK;
200}
201
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200202static int is_dirty_pte(unsigned long pte)
203{
204 return pte & PT_DIRTY_MASK;
205}
206
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207static int is_io_pte(unsigned long pte)
208{
209 return pte & PT_SHADOW_IO_MARK;
210}
211
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800212static int is_rmap_pte(u64 pte)
213{
214 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
215 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
216}
217
Avi Kivitye663ee62007-05-31 15:46:04 +0300218static void set_shadow_pte(u64 *sptep, u64 spte)
219{
220#ifdef CONFIG_X86_64
221 set_64bit((unsigned long *)sptep, spte);
222#else
223 set_64bit((unsigned long long *)sptep, spte);
224#endif
225}
226
Avi Kivitye2dec932007-01-05 16:36:54 -0800227static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300228 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800229{
230 void *obj;
231
232 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800233 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800234 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300235 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800236 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800237 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800238 cache->objects[cache->nobjs++] = obj;
239 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800240 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800241}
242
243static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
244{
245 while (mc->nobjs)
246 kfree(mc->objects[--mc->nobjs]);
247}
248
Avi Kivityc1158e62007-07-20 08:18:27 +0300249static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300250 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300251{
252 struct page *page;
253
254 if (cache->nobjs >= min)
255 return 0;
256 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300257 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300258 if (!page)
259 return -ENOMEM;
260 set_page_private(page, 0);
261 cache->objects[cache->nobjs++] = page_address(page);
262 }
263 return 0;
264}
265
266static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
267{
268 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300269 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300270}
271
Avi Kivity8c438502007-04-16 11:53:17 +0300272static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
273{
274 int r;
275
Avi Kivity22d95b12007-09-14 20:26:06 +0300276 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300277 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
278 pte_chain_cache, 4);
279 if (r)
280 goto out;
281 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
282 rmap_desc_cache, 1);
283 if (r)
284 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200285 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300286 if (r)
287 goto out;
288 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
289 mmu_page_header_cache, 4);
290out:
Avi Kivity8c438502007-04-16 11:53:17 +0300291 return r;
292}
293
Avi Kivity714b93d2007-01-05 16:36:53 -0800294static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
295{
296 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
297 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300298 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300299 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800300}
301
302static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
303 size_t size)
304{
305 void *p;
306
307 BUG_ON(!mc->nobjs);
308 p = mc->objects[--mc->nobjs];
309 memset(p, 0, size);
310 return p;
311}
312
Avi Kivity714b93d2007-01-05 16:36:53 -0800313static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
314{
315 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
316 sizeof(struct kvm_pte_chain));
317}
318
Avi Kivity90cb0522007-07-17 13:04:56 +0300319static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800320{
Avi Kivity90cb0522007-07-17 13:04:56 +0300321 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800322}
323
324static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
325{
326 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
327 sizeof(struct kvm_rmap_desc));
328}
329
Avi Kivity90cb0522007-07-17 13:04:56 +0300330static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800331{
Avi Kivity90cb0522007-07-17 13:04:56 +0300332 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800333}
334
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800335/*
Izik Eidus290fc382007-09-27 14:11:22 +0200336 * Take gfn and return the reverse mapping to it.
337 * Note: gfn must be unaliased before this function get called
338 */
339
340static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
341{
342 struct kvm_memory_slot *slot;
343
344 slot = gfn_to_memslot(kvm, gfn);
345 return &slot->rmap[gfn - slot->base_gfn];
346}
347
348/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800349 * Reverse mapping data structures:
350 *
Izik Eidus290fc382007-09-27 14:11:22 +0200351 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
352 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800353 *
Izik Eidus290fc382007-09-27 14:11:22 +0200354 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
355 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800356 */
Izik Eidus290fc382007-09-27 14:11:22 +0200357static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800358{
Izik Eidus290fc382007-09-27 14:11:22 +0200359 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800360 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200361 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800362 int i;
363
364 if (!is_rmap_pte(*spte))
365 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200366 gfn = unalias_gfn(vcpu->kvm, gfn);
367 page = page_header(__pa(spte));
368 page->gfns[spte - page->spt] = gfn;
369 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
370 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800371 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200372 *rmapp = (unsigned long)spte;
373 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800374 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800375 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200376 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800377 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200378 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800379 } else {
380 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200381 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800382 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
383 desc = desc->more;
384 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800385 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800386 desc = desc->more;
387 }
388 for (i = 0; desc->shadow_ptes[i]; ++i)
389 ;
390 desc->shadow_ptes[i] = spte;
391 }
392}
393
Izik Eidus290fc382007-09-27 14:11:22 +0200394static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800395 struct kvm_rmap_desc *desc,
396 int i,
397 struct kvm_rmap_desc *prev_desc)
398{
399 int j;
400
401 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
402 ;
403 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000404 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405 if (j != 0)
406 return;
407 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200408 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800409 else
410 if (prev_desc)
411 prev_desc->more = desc->more;
412 else
Izik Eidus290fc382007-09-27 14:11:22 +0200413 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300414 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800415}
416
Izik Eidus290fc382007-09-27 14:11:22 +0200417static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800418{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800419 struct kvm_rmap_desc *desc;
420 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200421 struct kvm_mmu_page *page;
422 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800423 int i;
424
425 if (!is_rmap_pte(*spte))
426 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200427 page = page_header(__pa(spte));
428 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
429 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800430 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
431 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200432 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800433 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200434 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800435 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
436 spte, *spte);
437 BUG();
438 }
Izik Eidus290fc382007-09-27 14:11:22 +0200439 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800440 } else {
441 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200442 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800443 prev_desc = NULL;
444 while (desc) {
445 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
446 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200447 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800448 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800449 prev_desc);
450 return;
451 }
452 prev_desc = desc;
453 desc = desc->more;
454 }
455 BUG();
456 }
457}
458
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500459static void rmap_write_protect(struct kvm *kvm, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800460{
Avi Kivity374cbac2007-01-05 16:36:43 -0800461 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200462 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800463 u64 *spte;
464
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500465 gfn = unalias_gfn(kvm, gfn);
466 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800467
Izik Eidus290fc382007-09-27 14:11:22 +0200468 while (*rmapp) {
469 if (!(*rmapp & 1))
470 spte = (u64 *)*rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800471 else {
Izik Eidus290fc382007-09-27 14:11:22 +0200472 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800473 spte = desc->shadow_ptes[0];
474 }
475 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800476 BUG_ON(!(*spte & PT_PRESENT_MASK));
477 BUG_ON(!(*spte & PT_WRITABLE_MASK));
478 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500479 rmap_remove(kvm, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300480 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500481 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800482 }
483}
484
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800485#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300486static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487{
Avi Kivity139bdb22007-01-05 16:36:50 -0800488 u64 *pos;
489 u64 *end;
490
Avi Kivity47ad8e62007-05-06 15:50:58 +0300491 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200492 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800493 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
494 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800496 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 return 1;
498}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800499#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500
Avi Kivity90cb0522007-07-17 13:04:56 +0300501static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300502 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800503{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300504 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300505 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300506 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200507 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300508 kfree(page_head);
509 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800510}
511
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800512static unsigned kvm_page_table_hashfn(gfn_t gfn)
513{
514 return gfn;
515}
516
Avi Kivity25c0de22007-01-05 16:36:42 -0800517static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
518 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519{
520 struct kvm_mmu_page *page;
521
Avi Kivityd3d25b02007-05-30 12:34:53 +0300522 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800523 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524
Avi Kivityd3d25b02007-05-30 12:34:53 +0300525 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
526 sizeof *page);
527 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200528 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300529 set_page_private(virt_to_page(page->spt), (unsigned long)page);
530 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300531 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800533 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800535 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800536 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537}
538
Avi Kivity714b93d2007-01-05 16:36:53 -0800539static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
540 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800541{
542 struct kvm_pte_chain *pte_chain;
543 struct hlist_node *node;
544 int i;
545
546 if (!parent_pte)
547 return;
548 if (!page->multimapped) {
549 u64 *old = page->parent_pte;
550
551 if (!old) {
552 page->parent_pte = parent_pte;
553 return;
554 }
555 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800556 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800557 INIT_HLIST_HEAD(&page->parent_ptes);
558 hlist_add_head(&pte_chain->link, &page->parent_ptes);
559 pte_chain->parent_ptes[0] = old;
560 }
561 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
562 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
563 continue;
564 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
565 if (!pte_chain->parent_ptes[i]) {
566 pte_chain->parent_ptes[i] = parent_pte;
567 return;
568 }
569 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800570 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800571 BUG_ON(!pte_chain);
572 hlist_add_head(&pte_chain->link, &page->parent_ptes);
573 pte_chain->parent_ptes[0] = parent_pte;
574}
575
Avi Kivity90cb0522007-07-17 13:04:56 +0300576static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800577 u64 *parent_pte)
578{
579 struct kvm_pte_chain *pte_chain;
580 struct hlist_node *node;
581 int i;
582
583 if (!page->multimapped) {
584 BUG_ON(page->parent_pte != parent_pte);
585 page->parent_pte = NULL;
586 return;
587 }
588 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
589 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
590 if (!pte_chain->parent_ptes[i])
591 break;
592 if (pte_chain->parent_ptes[i] != parent_pte)
593 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800594 while (i + 1 < NR_PTE_CHAIN_ENTRIES
595 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800596 pte_chain->parent_ptes[i]
597 = pte_chain->parent_ptes[i + 1];
598 ++i;
599 }
600 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800601 if (i == 0) {
602 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300603 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800604 if (hlist_empty(&page->parent_ptes)) {
605 page->multimapped = 0;
606 page->parent_pte = NULL;
607 }
608 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800609 return;
610 }
611 BUG();
612}
613
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500614static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800615 gfn_t gfn)
616{
617 unsigned index;
618 struct hlist_head *bucket;
619 struct kvm_mmu_page *page;
620 struct hlist_node *node;
621
622 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
623 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500624 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800625 hlist_for_each_entry(page, node, bucket, hash_link)
626 if (page->gfn == gfn && !page->role.metaphysical) {
627 pgprintk("%s: found role %x\n",
628 __FUNCTION__, page->role.word);
629 return page;
630 }
631 return NULL;
632}
633
634static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
635 gfn_t gfn,
636 gva_t gaddr,
637 unsigned level,
638 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200639 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800640 u64 *parent_pte)
641{
642 union kvm_mmu_page_role role;
643 unsigned index;
644 unsigned quadrant;
645 struct hlist_head *bucket;
646 struct kvm_mmu_page *page;
647 struct hlist_node *node;
648
649 role.word = 0;
650 role.glevels = vcpu->mmu.root_level;
651 role.level = level;
652 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200653 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800654 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
655 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
656 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
657 role.quadrant = quadrant;
658 }
659 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
660 gfn, role.word);
661 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
662 bucket = &vcpu->kvm->mmu_page_hash[index];
663 hlist_for_each_entry(page, node, bucket, hash_link)
664 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800665 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800666 pgprintk("%s: found\n", __FUNCTION__);
667 return page;
668 }
669 page = kvm_mmu_alloc_page(vcpu, parent_pte);
670 if (!page)
671 return page;
672 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
673 page->gfn = gfn;
674 page->role = role;
675 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200676 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800677 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500678 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800679 return page;
680}
681
Avi Kivity90cb0522007-07-17 13:04:56 +0300682static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800683 struct kvm_mmu_page *page)
684{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800685 unsigned i;
686 u64 *pt;
687 u64 ent;
688
Avi Kivity47ad8e62007-05-06 15:50:58 +0300689 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800690
691 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
692 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200693 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200694 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200695 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800696 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300697 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800698 return;
699 }
700
701 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
702 ent = pt[i];
703
Avi Kivityc7addb92007-09-16 18:58:32 +0200704 pt[i] = shadow_trap_nonpresent_pte;
705 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800706 continue;
707 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300708 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800709 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300710 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800711}
712
Avi Kivity90cb0522007-07-17 13:04:56 +0300713static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800714 u64 *parent_pte)
715{
Avi Kivity90cb0522007-07-17 13:04:56 +0300716 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800717}
718
Avi Kivity12b7d282007-09-23 14:10:49 +0200719static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
720{
721 int i;
722
723 for (i = 0; i < KVM_MAX_VCPUS; ++i)
724 if (kvm->vcpus[i])
725 kvm->vcpus[i]->last_pte_updated = NULL;
726}
727
Avi Kivity90cb0522007-07-17 13:04:56 +0300728static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800729 struct kvm_mmu_page *page)
730{
731 u64 *parent_pte;
732
733 while (page->multimapped || page->parent_pte) {
734 if (!page->multimapped)
735 parent_pte = page->parent_pte;
736 else {
737 struct kvm_pte_chain *chain;
738
739 chain = container_of(page->parent_ptes.first,
740 struct kvm_pte_chain, link);
741 parent_pte = chain->parent_ptes[0];
742 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800743 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300744 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200745 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800746 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300747 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800748 if (!page->root_count) {
749 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300750 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200751 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300752 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200753 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800754}
755
Izik Eidus82ce2c92007-10-02 18:52:55 +0200756/*
757 * Changing the number of mmu pages allocated to the vm
758 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
759 */
760void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
761{
762 /*
763 * If we set the number of mmu pages to be smaller be than the
764 * number of actived pages , we must to free some mmu pages before we
765 * change the value
766 */
767
768 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
769 kvm_nr_mmu_pages) {
770 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
771 - kvm->n_free_mmu_pages;
772
773 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
774 struct kvm_mmu_page *page;
775
776 page = container_of(kvm->active_mmu_pages.prev,
777 struct kvm_mmu_page, link);
778 kvm_mmu_zap_page(kvm, page);
779 n_used_mmu_pages--;
780 }
781 kvm->n_free_mmu_pages = 0;
782 }
783 else
784 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
785 - kvm->n_alloc_mmu_pages;
786
787 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
788}
789
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500790static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800791{
792 unsigned index;
793 struct hlist_head *bucket;
794 struct kvm_mmu_page *page;
795 struct hlist_node *node, *n;
796 int r;
797
798 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
799 r = 0;
800 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500801 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800802 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
803 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800804 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
805 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500806 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800807 r = 1;
808 }
809 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800810}
811
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500812static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300813{
814 struct kvm_mmu_page *page;
815
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500816 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300817 pgprintk("%s: zap %lx %x\n",
818 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500819 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300820 }
821}
822
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
824{
825 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
826 struct kvm_mmu_page *page_head = page_header(__pa(pte));
827
828 __set_bit(slot, &page_head->slot_bitmap);
829}
830
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500831hpa_t safe_gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832{
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500833 hpa_t hpa = gpa_to_hpa(kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834
835 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
836}
837
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500838hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 struct page *page;
841
842 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500843 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300844 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
847 | (gpa & (PAGE_SIZE-1));
848}
849
850hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
851{
852 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
853
854 if (gpa == UNMAPPED_GVA)
855 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500856 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857}
858
Avi Kivity039576c2007-03-20 12:46:50 +0200859struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
860{
861 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
862
863 if (gpa == UNMAPPED_GVA)
864 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500865 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200866}
867
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
869{
870}
871
872static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
873{
874 int level = PT32E_ROOT_LEVEL;
875 hpa_t table_addr = vcpu->mmu.root_hpa;
876
877 for (; ; level--) {
878 u32 index = PT64_INDEX(v, level);
879 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800880 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881
882 ASSERT(VALID_PAGE(table_addr));
883 table = __va(table_addr);
884
885 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800886 pte = table[index];
Avi Kivityc7addb92007-09-16 18:58:32 +0200887 if (is_shadow_present_pte(pte) && is_writeble_pte(pte))
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800888 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
890 page_header_update_slot(vcpu->kvm, table, v);
891 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
892 PT_USER_MASK;
Izik Eidus290fc382007-09-27 14:11:22 +0200893 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 return 0;
895 }
896
Avi Kivityc7addb92007-09-16 18:58:32 +0200897 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800898 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800899 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800901 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
902 >> PAGE_SHIFT;
903 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
904 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200905 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800906 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 pgprintk("nonpaging_map: ENOMEM\n");
908 return -ENOMEM;
909 }
910
Avi Kivity47ad8e62007-05-06 15:50:58 +0300911 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800912 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 }
914 table_addr = table[index] & PT64_BASE_ADDR_MASK;
915 }
916}
917
Avi Kivityc7addb92007-09-16 18:58:32 +0200918static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
919 struct kvm_mmu_page *sp)
920{
921 int i;
922
923 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
924 sp->spt[i] = shadow_trap_nonpresent_pte;
925}
926
Avi Kivity17ac10a2007-01-05 16:36:40 -0800927static void mmu_free_roots(struct kvm_vcpu *vcpu)
928{
929 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800930 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800931
Avi Kivity7b53aa52007-06-05 12:17:03 +0300932 if (!VALID_PAGE(vcpu->mmu.root_hpa))
933 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800934#ifdef CONFIG_X86_64
935 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
936 hpa_t root = vcpu->mmu.root_hpa;
937
Avi Kivity3bb65a22007-01-05 16:36:51 -0800938 page = page_header(root);
939 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800940 vcpu->mmu.root_hpa = INVALID_PAGE;
941 return;
942 }
943#endif
944 for (i = 0; i < 4; ++i) {
945 hpa_t root = vcpu->mmu.pae_root[i];
946
Avi Kivity417726a2007-04-12 17:35:58 +0300947 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300948 root &= PT64_BASE_ADDR_MASK;
949 page = page_header(root);
950 --page->root_count;
951 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800952 vcpu->mmu.pae_root[i] = INVALID_PAGE;
953 }
954 vcpu->mmu.root_hpa = INVALID_PAGE;
955}
956
957static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
958{
959 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800960 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800961 struct kvm_mmu_page *page;
962
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800963 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800964
965#ifdef CONFIG_X86_64
966 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
967 hpa_t root = vcpu->mmu.root_hpa;
968
969 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800970 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200971 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300972 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800973 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800974 vcpu->mmu.root_hpa = root;
975 return;
976 }
977#endif
978 for (i = 0; i < 4; ++i) {
979 hpa_t root = vcpu->mmu.pae_root[i];
980
981 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300982 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
983 if (!is_present_pte(vcpu->pdptrs[i])) {
984 vcpu->mmu.pae_root[i] = 0;
985 continue;
986 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800987 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300988 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800989 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -0800990 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800991 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200992 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300993 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800994 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800995 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
996 }
997 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
998}
999
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1001{
1002 return vaddr;
1003}
1004
1005static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1006 u32 error_code)
1007{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001009 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001010 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
Avi Kivitye2dec932007-01-05 16:36:54 -08001012 r = mmu_topup_memory_caches(vcpu);
1013 if (r)
1014 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001015
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 ASSERT(vcpu);
1017 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1018
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001020 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Avi Kivityebeace82007-01-05 16:36:47 -08001022 if (is_error_hpa(paddr))
1023 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
Avi Kivityebeace82007-01-05 16:36:47 -08001025 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026}
1027
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028static void nonpaging_free(struct kvm_vcpu *vcpu)
1029{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001030 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
1033static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1034{
1035 struct kvm_mmu *context = &vcpu->mmu;
1036
1037 context->new_cr3 = nonpaging_new_cr3;
1038 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 context->gva_to_gpa = nonpaging_gva_to_gpa;
1040 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001041 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001042 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001044 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 return 0;
1046}
1047
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1049{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001050 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001051 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052}
1053
1054static void paging_new_cr3(struct kvm_vcpu *vcpu)
1055{
Avi Kivity374cbac2007-01-05 16:36:43 -08001056 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001057 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058}
1059
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060static void inject_page_fault(struct kvm_vcpu *vcpu,
1061 u64 addr,
1062 u32 err_code)
1063{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001064 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065}
1066
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067static void paging_free(struct kvm_vcpu *vcpu)
1068{
1069 nonpaging_free(vcpu);
1070}
1071
1072#define PTTYPE 64
1073#include "paging_tmpl.h"
1074#undef PTTYPE
1075
1076#define PTTYPE 32
1077#include "paging_tmpl.h"
1078#undef PTTYPE
1079
Avi Kivity17ac10a2007-01-05 16:36:40 -08001080static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081{
1082 struct kvm_mmu *context = &vcpu->mmu;
1083
1084 ASSERT(is_pae(vcpu));
1085 context->new_cr3 = paging_new_cr3;
1086 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001088 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001090 context->root_level = level;
1091 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001092 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 return 0;
1094}
1095
Avi Kivity17ac10a2007-01-05 16:36:40 -08001096static int paging64_init_context(struct kvm_vcpu *vcpu)
1097{
1098 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1099}
1100
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101static int paging32_init_context(struct kvm_vcpu *vcpu)
1102{
1103 struct kvm_mmu *context = &vcpu->mmu;
1104
1105 context->new_cr3 = paging_new_cr3;
1106 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 context->gva_to_gpa = paging32_gva_to_gpa;
1108 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001109 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 context->root_level = PT32_ROOT_LEVEL;
1111 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001112 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 return 0;
1114}
1115
1116static int paging32E_init_context(struct kvm_vcpu *vcpu)
1117{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001118 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119}
1120
1121static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1122{
1123 ASSERT(vcpu);
1124 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1125
1126 if (!is_paging(vcpu))
1127 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001128 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 return paging64_init_context(vcpu);
1130 else if (is_pae(vcpu))
1131 return paging32E_init_context(vcpu);
1132 else
1133 return paging32_init_context(vcpu);
1134}
1135
1136static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1137{
1138 ASSERT(vcpu);
1139 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1140 vcpu->mmu.free(vcpu);
1141 vcpu->mmu.root_hpa = INVALID_PAGE;
1142 }
1143}
1144
1145int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1146{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001147 destroy_kvm_mmu(vcpu);
1148 return init_kvm_mmu(vcpu);
1149}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001150EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001151
1152int kvm_mmu_load(struct kvm_vcpu *vcpu)
1153{
Avi Kivity714b93d2007-01-05 16:36:53 -08001154 int r;
1155
Shaohua Li11ec2802007-07-23 14:51:37 +08001156 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001157 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001158 if (r)
1159 goto out;
1160 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001161 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001162 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001163out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001164 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001165 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001167EXPORT_SYMBOL_GPL(kvm_mmu_load);
1168
1169void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1170{
1171 mmu_free_roots(vcpu);
1172}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173
Avi Kivity09072da2007-05-01 14:16:52 +03001174static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001175 struct kvm_mmu_page *page,
1176 u64 *spte)
1177{
1178 u64 pte;
1179 struct kvm_mmu_page *child;
1180
1181 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001182 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001183 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001184 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001185 else {
1186 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001187 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001188 }
1189 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001190 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001191 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001192}
1193
Avi Kivity00284252007-05-01 16:53:31 +03001194static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1195 struct kvm_mmu_page *page,
1196 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001197 const void *new, int bytes,
1198 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001199{
1200 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1201 return;
1202
1203 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001204 paging32_update_pte(vcpu, page, spte, new, bytes,
1205 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001206 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001207 paging64_update_pte(vcpu, page, spte, new, bytes,
1208 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001209}
1210
Avi Kivity12b7d282007-09-23 14:10:49 +02001211static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1212{
1213 u64 *spte = vcpu->last_pte_updated;
1214
1215 return !!(spte && (*spte & PT_ACCESSED_MASK));
1216}
1217
Avi Kivity09072da2007-05-01 14:16:52 +03001218void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001219 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001220{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001221 gfn_t gfn = gpa >> PAGE_SHIFT;
1222 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001223 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001224 struct hlist_head *bucket;
1225 unsigned index;
1226 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001227 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001228 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001229 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001230 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001231 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001232 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001233 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001234 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001235
Avi Kivityda4a00f2007-01-05 16:36:44 -08001236 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityc7addb92007-09-16 18:58:32 +02001237 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001238 if (gfn == vcpu->last_pt_write_gfn
1239 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001240 ++vcpu->last_pt_write_count;
1241 if (vcpu->last_pt_write_count >= 3)
1242 flooded = 1;
1243 } else {
1244 vcpu->last_pt_write_gfn = gfn;
1245 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001246 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001247 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001248 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1249 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001250 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001251 if (page->gfn != gfn || page->role.metaphysical)
1252 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001253 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1254 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001255 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001256 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001257 /*
1258 * Misaligned accesses are too much trouble to fix
1259 * up; also, they usually indicate a page is not used
1260 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001261 *
1262 * If we're seeing too many writes to a page,
1263 * it may no longer be a page table, or we may be
1264 * forking, in which case it is better to unmap the
1265 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001266 */
1267 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1268 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001269 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001270 continue;
1271 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001272 page_offset = offset;
1273 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001274 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001275 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001276 page_offset <<= 1; /* 32->64 */
1277 /*
1278 * A 32-bit pde maps 4MB while the shadow pdes map
1279 * only 2MB. So we need to double the offset again
1280 * and zap two pdes instead of one.
1281 */
1282 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001283 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001284 page_offset <<= 1;
1285 npte = 2;
1286 }
Avi Kivityfce06572007-05-01 16:44:05 +03001287 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001288 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001289 if (quadrant != page->role.quadrant)
1290 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001291 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001292 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001293 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001294 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001295 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1296 page_offset & (pte_size - 1));
Avi Kivityac1b7142007-03-08 17:13:32 +02001297 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001298 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001299 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001300 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001301}
1302
Avi Kivitya4360362007-01-05 16:36:45 -08001303int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1304{
1305 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1306
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001307 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001308}
1309
Avi Kivity22d95b12007-09-14 20:26:06 +03001310void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001311{
1312 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1313 struct kvm_mmu_page *page;
1314
1315 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1316 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001317 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityebeace82007-01-05 16:36:47 -08001318 }
1319}
Avi Kivityebeace82007-01-05 16:36:47 -08001320
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321static void free_mmu_pages(struct kvm_vcpu *vcpu)
1322{
Avi Kivityf51234c2007-01-05 16:36:52 -08001323 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324
Avi Kivityf51234c2007-01-05 16:36:52 -08001325 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1326 page = container_of(vcpu->kvm->active_mmu_pages.next,
1327 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001328 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001329 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001330 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
1333static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1334{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001335 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 int i;
1337
1338 ASSERT(vcpu);
1339
Izik Eidus82ce2c92007-10-02 18:52:55 +02001340 if (vcpu->kvm->n_requested_mmu_pages)
1341 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1342 else
1343 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001344 /*
1345 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1346 * Therefore we need to allocate shadow page tables in the first
1347 * 4GB of memory, which happens to fit the DMA32 zone.
1348 */
1349 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1350 if (!page)
1351 goto error_1;
1352 vcpu->mmu.pae_root = page_address(page);
1353 for (i = 0; i < 4; ++i)
1354 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1355
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 return 0;
1357
1358error_1:
1359 free_mmu_pages(vcpu);
1360 return -ENOMEM;
1361}
1362
Ingo Molnar8018c272006-12-29 16:50:01 -08001363int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365 ASSERT(vcpu);
1366 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
Ingo Molnar8018c272006-12-29 16:50:01 -08001368 return alloc_mmu_pages(vcpu);
1369}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370
Ingo Molnar8018c272006-12-29 16:50:01 -08001371int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1372{
1373 ASSERT(vcpu);
1374 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001375
Ingo Molnar8018c272006-12-29 16:50:01 -08001376 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377}
1378
1379void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1380{
1381 ASSERT(vcpu);
1382
1383 destroy_kvm_mmu(vcpu);
1384 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001385 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
Avi Kivity90cb0522007-07-17 13:04:56 +03001388void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389{
1390 struct kvm_mmu_page *page;
1391
1392 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1393 int i;
1394 u64 *pt;
1395
1396 if (!test_bit(slot, &page->slot_bitmap))
1397 continue;
1398
Avi Kivity47ad8e62007-05-06 15:50:58 +03001399 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1401 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001402 if (pt[i] & PT_WRITABLE_MASK) {
Izik Eidus290fc382007-09-27 14:11:22 +02001403 rmap_remove(kvm, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001405 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 }
1407}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001408
Avi Kivity90cb0522007-07-17 13:04:56 +03001409void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001410{
Avi Kivity90cb0522007-07-17 13:04:56 +03001411 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001412
Avi Kivity90cb0522007-07-17 13:04:56 +03001413 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1414 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001415
Avi Kivity90cb0522007-07-17 13:04:56 +03001416 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001417}
1418
Avi Kivityb5a33a72007-04-15 16:31:09 +03001419void kvm_mmu_module_exit(void)
1420{
1421 if (pte_chain_cache)
1422 kmem_cache_destroy(pte_chain_cache);
1423 if (rmap_desc_cache)
1424 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001425 if (mmu_page_header_cache)
1426 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001427}
1428
1429int kvm_mmu_module_init(void)
1430{
1431 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1432 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001433 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001434 if (!pte_chain_cache)
1435 goto nomem;
1436 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1437 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001438 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001439 if (!rmap_desc_cache)
1440 goto nomem;
1441
Avi Kivityd3d25b02007-05-30 12:34:53 +03001442 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1443 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001444 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001445 if (!mmu_page_header_cache)
1446 goto nomem;
1447
Avi Kivityb5a33a72007-04-15 16:31:09 +03001448 return 0;
1449
1450nomem:
1451 kvm_mmu_module_exit();
1452 return -ENOMEM;
1453}
1454
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001455#ifdef AUDIT
1456
1457static const char *audit_msg;
1458
1459static gva_t canonicalize(gva_t gva)
1460{
1461#ifdef CONFIG_X86_64
1462 gva = (long long)(gva << 16) >> 16;
1463#endif
1464 return gva;
1465}
1466
1467static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1468 gva_t va, int level)
1469{
1470 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1471 int i;
1472 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1473
1474 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1475 u64 ent = pt[i];
1476
Avi Kivityc7addb92007-09-16 18:58:32 +02001477 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001478 continue;
1479
1480 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001481 if (level > 1) {
1482 if (ent == shadow_notrap_nonpresent_pte)
1483 printk(KERN_ERR "audit: (%s) nontrapping pte"
1484 " in nonleaf level: levels %d gva %lx"
1485 " level %d pte %llx\n", audit_msg,
1486 vcpu->mmu.root_level, va, level, ent);
1487
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001488 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001489 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001490 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1491 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1492
Avi Kivityc7addb92007-09-16 18:58:32 +02001493 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001494 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001495 printk(KERN_ERR "xx audit error: (%s) levels %d"
1496 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001497 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001498 va, gpa, hpa, ent,
1499 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001500 else if (ent == shadow_notrap_nonpresent_pte
1501 && !is_error_hpa(hpa))
1502 printk(KERN_ERR "audit: (%s) notrap shadow,"
1503 " valid guest gva %lx\n", audit_msg, va);
1504
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001505 }
1506 }
1507}
1508
1509static void audit_mappings(struct kvm_vcpu *vcpu)
1510{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001511 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001512
1513 if (vcpu->mmu.root_level == 4)
1514 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1515 else
1516 for (i = 0; i < 4; ++i)
1517 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1518 audit_mappings_page(vcpu,
1519 vcpu->mmu.pae_root[i],
1520 i << 30,
1521 2);
1522}
1523
1524static int count_rmaps(struct kvm_vcpu *vcpu)
1525{
1526 int nmaps = 0;
1527 int i, j, k;
1528
1529 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1530 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1531 struct kvm_rmap_desc *d;
1532
1533 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001534 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001535
Izik Eidus290fc382007-09-27 14:11:22 +02001536 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001537 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001538 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001539 ++nmaps;
1540 continue;
1541 }
Izik Eidus290fc382007-09-27 14:11:22 +02001542 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001543 while (d) {
1544 for (k = 0; k < RMAP_EXT; ++k)
1545 if (d->shadow_ptes[k])
1546 ++nmaps;
1547 else
1548 break;
1549 d = d->more;
1550 }
1551 }
1552 }
1553 return nmaps;
1554}
1555
1556static int count_writable_mappings(struct kvm_vcpu *vcpu)
1557{
1558 int nmaps = 0;
1559 struct kvm_mmu_page *page;
1560 int i;
1561
1562 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001563 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001564
1565 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1566 continue;
1567
1568 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1569 u64 ent = pt[i];
1570
1571 if (!(ent & PT_PRESENT_MASK))
1572 continue;
1573 if (!(ent & PT_WRITABLE_MASK))
1574 continue;
1575 ++nmaps;
1576 }
1577 }
1578 return nmaps;
1579}
1580
1581static void audit_rmap(struct kvm_vcpu *vcpu)
1582{
1583 int n_rmap = count_rmaps(vcpu);
1584 int n_actual = count_writable_mappings(vcpu);
1585
1586 if (n_rmap != n_actual)
1587 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1588 __FUNCTION__, audit_msg, n_rmap, n_actual);
1589}
1590
1591static void audit_write_protection(struct kvm_vcpu *vcpu)
1592{
1593 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001594 struct kvm_memory_slot *slot;
1595 unsigned long *rmapp;
1596 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001597
1598 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001599 if (page->role.metaphysical)
1600 continue;
1601
Izik Eidus290fc382007-09-27 14:11:22 +02001602 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1603 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1604 rmapp = &slot->rmap[gfn - slot->base_gfn];
1605 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001606 printk(KERN_ERR "%s: (%s) shadow page has writable"
1607 " mappings: gfn %lx role %x\n",
1608 __FUNCTION__, audit_msg, page->gfn,
1609 page->role.word);
1610 }
1611}
1612
1613static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1614{
1615 int olddbg = dbg;
1616
1617 dbg = 0;
1618 audit_msg = msg;
1619 audit_rmap(vcpu);
1620 audit_write_protection(vcpu);
1621 audit_mappings(vcpu);
1622 dbg = olddbg;
1623}
1624
1625#endif