blob: 14e54e331f50a4bc7aef522f7092521f72ba3729 [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
Izik Eidus98348e92007-10-16 14:42:30 +0200459static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800460{
Avi Kivity374cbac2007-01-05 16:36:43 -0800461 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200462 struct kvm_rmap_desc *prev_desc;
463 u64 *prev_spte;
464 int i;
465
466 if (!*rmapp)
467 return NULL;
468 else if (!(*rmapp & 1)) {
469 if (!spte)
470 return (u64 *)*rmapp;
471 return NULL;
472 }
473 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
474 prev_desc = NULL;
475 prev_spte = NULL;
476 while (desc) {
477 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
478 if (prev_spte == spte)
479 return desc->shadow_ptes[i];
480 prev_spte = desc->shadow_ptes[i];
481 }
482 desc = desc->more;
483 }
484 return NULL;
485}
486
487static void rmap_write_protect(struct kvm *kvm, u64 gfn)
488{
Izik Eidus290fc382007-09-27 14:11:22 +0200489 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800490 u64 *spte;
Izik Eidus98348e92007-10-16 14:42:30 +0200491 u64 *prev_spte;
Avi Kivity374cbac2007-01-05 16:36:43 -0800492
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500493 gfn = unalias_gfn(kvm, gfn);
494 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800495
Izik Eidus98348e92007-10-16 14:42:30 +0200496 spte = rmap_next(kvm, rmapp, NULL);
497 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800498 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800499 BUG_ON(!(*spte & PT_PRESENT_MASK));
500 BUG_ON(!(*spte & PT_WRITABLE_MASK));
501 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Izik Eidus98348e92007-10-16 14:42:30 +0200502 prev_spte = spte;
503 spte = rmap_next(kvm, rmapp, spte);
504 rmap_remove(kvm, prev_spte);
505 set_shadow_pte(prev_spte, *prev_spte & ~PT_WRITABLE_MASK);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500506 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800507 }
508}
509
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800510#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300511static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512{
Avi Kivity139bdb22007-01-05 16:36:50 -0800513 u64 *pos;
514 u64 *end;
515
Avi Kivity47ad8e62007-05-06 15:50:58 +0300516 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200517 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800518 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
519 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800521 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 return 1;
523}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800524#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525
Avi Kivity90cb0522007-07-17 13:04:56 +0300526static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300527 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800528{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300529 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300530 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300531 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200532 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300533 kfree(page_head);
534 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800535}
536
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800537static unsigned kvm_page_table_hashfn(gfn_t gfn)
538{
539 return gfn;
540}
541
Avi Kivity25c0de22007-01-05 16:36:42 -0800542static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
543 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544{
545 struct kvm_mmu_page *page;
546
Avi Kivityd3d25b02007-05-30 12:34:53 +0300547 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800548 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549
Avi Kivityd3d25b02007-05-30 12:34:53 +0300550 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
551 sizeof *page);
552 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200553 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300554 set_page_private(virt_to_page(page->spt), (unsigned long)page);
555 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300556 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800558 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800560 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800561 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562}
563
Avi Kivity714b93d2007-01-05 16:36:53 -0800564static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
565 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800566{
567 struct kvm_pte_chain *pte_chain;
568 struct hlist_node *node;
569 int i;
570
571 if (!parent_pte)
572 return;
573 if (!page->multimapped) {
574 u64 *old = page->parent_pte;
575
576 if (!old) {
577 page->parent_pte = parent_pte;
578 return;
579 }
580 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800581 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800582 INIT_HLIST_HEAD(&page->parent_ptes);
583 hlist_add_head(&pte_chain->link, &page->parent_ptes);
584 pte_chain->parent_ptes[0] = old;
585 }
586 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
587 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
588 continue;
589 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
590 if (!pte_chain->parent_ptes[i]) {
591 pte_chain->parent_ptes[i] = parent_pte;
592 return;
593 }
594 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800595 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800596 BUG_ON(!pte_chain);
597 hlist_add_head(&pte_chain->link, &page->parent_ptes);
598 pte_chain->parent_ptes[0] = parent_pte;
599}
600
Avi Kivity90cb0522007-07-17 13:04:56 +0300601static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800602 u64 *parent_pte)
603{
604 struct kvm_pte_chain *pte_chain;
605 struct hlist_node *node;
606 int i;
607
608 if (!page->multimapped) {
609 BUG_ON(page->parent_pte != parent_pte);
610 page->parent_pte = NULL;
611 return;
612 }
613 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
614 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
615 if (!pte_chain->parent_ptes[i])
616 break;
617 if (pte_chain->parent_ptes[i] != parent_pte)
618 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800619 while (i + 1 < NR_PTE_CHAIN_ENTRIES
620 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800621 pte_chain->parent_ptes[i]
622 = pte_chain->parent_ptes[i + 1];
623 ++i;
624 }
625 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800626 if (i == 0) {
627 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300628 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800629 if (hlist_empty(&page->parent_ptes)) {
630 page->multimapped = 0;
631 page->parent_pte = NULL;
632 }
633 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800634 return;
635 }
636 BUG();
637}
638
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500639static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800640 gfn_t gfn)
641{
642 unsigned index;
643 struct hlist_head *bucket;
644 struct kvm_mmu_page *page;
645 struct hlist_node *node;
646
647 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
648 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500649 bucket = &kvm->mmu_page_hash[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800650 hlist_for_each_entry(page, node, bucket, hash_link)
651 if (page->gfn == gfn && !page->role.metaphysical) {
652 pgprintk("%s: found role %x\n",
653 __FUNCTION__, page->role.word);
654 return page;
655 }
656 return NULL;
657}
658
659static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
660 gfn_t gfn,
661 gva_t gaddr,
662 unsigned level,
663 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200664 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800665 u64 *parent_pte)
666{
667 union kvm_mmu_page_role role;
668 unsigned index;
669 unsigned quadrant;
670 struct hlist_head *bucket;
671 struct kvm_mmu_page *page;
672 struct hlist_node *node;
673
674 role.word = 0;
675 role.glevels = vcpu->mmu.root_level;
676 role.level = level;
677 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200678 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800679 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
680 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
681 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
682 role.quadrant = quadrant;
683 }
684 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
685 gfn, role.word);
686 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
687 bucket = &vcpu->kvm->mmu_page_hash[index];
688 hlist_for_each_entry(page, node, bucket, hash_link)
689 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800690 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800691 pgprintk("%s: found\n", __FUNCTION__);
692 return page;
693 }
694 page = kvm_mmu_alloc_page(vcpu, parent_pte);
695 if (!page)
696 return page;
697 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
698 page->gfn = gfn;
699 page->role = role;
700 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200701 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800702 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500703 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704 return page;
705}
706
Avi Kivity90cb0522007-07-17 13:04:56 +0300707static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800708 struct kvm_mmu_page *page)
709{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800710 unsigned i;
711 u64 *pt;
712 u64 ent;
713
Avi Kivity47ad8e62007-05-06 15:50:58 +0300714 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800715
716 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
717 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200718 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200719 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200720 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800721 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300722 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800723 return;
724 }
725
726 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
727 ent = pt[i];
728
Avi Kivityc7addb92007-09-16 18:58:32 +0200729 pt[i] = shadow_trap_nonpresent_pte;
730 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800731 continue;
732 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300733 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800734 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300735 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800736}
737
Avi Kivity90cb0522007-07-17 13:04:56 +0300738static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800739 u64 *parent_pte)
740{
Avi Kivity90cb0522007-07-17 13:04:56 +0300741 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800742}
743
Avi Kivity12b7d282007-09-23 14:10:49 +0200744static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
745{
746 int i;
747
748 for (i = 0; i < KVM_MAX_VCPUS; ++i)
749 if (kvm->vcpus[i])
750 kvm->vcpus[i]->last_pte_updated = NULL;
751}
752
Avi Kivity90cb0522007-07-17 13:04:56 +0300753static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800754 struct kvm_mmu_page *page)
755{
756 u64 *parent_pte;
757
758 while (page->multimapped || page->parent_pte) {
759 if (!page->multimapped)
760 parent_pte = page->parent_pte;
761 else {
762 struct kvm_pte_chain *chain;
763
764 chain = container_of(page->parent_ptes.first,
765 struct kvm_pte_chain, link);
766 parent_pte = chain->parent_ptes[0];
767 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800768 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300769 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200770 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800771 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300772 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800773 if (!page->root_count) {
774 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300775 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200776 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300777 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200778 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800779}
780
Izik Eidus82ce2c92007-10-02 18:52:55 +0200781/*
782 * Changing the number of mmu pages allocated to the vm
783 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
784 */
785void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
786{
787 /*
788 * If we set the number of mmu pages to be smaller be than the
789 * number of actived pages , we must to free some mmu pages before we
790 * change the value
791 */
792
793 if ((kvm->n_alloc_mmu_pages - kvm->n_free_mmu_pages) >
794 kvm_nr_mmu_pages) {
795 int n_used_mmu_pages = kvm->n_alloc_mmu_pages
796 - kvm->n_free_mmu_pages;
797
798 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
799 struct kvm_mmu_page *page;
800
801 page = container_of(kvm->active_mmu_pages.prev,
802 struct kvm_mmu_page, link);
803 kvm_mmu_zap_page(kvm, page);
804 n_used_mmu_pages--;
805 }
806 kvm->n_free_mmu_pages = 0;
807 }
808 else
809 kvm->n_free_mmu_pages += kvm_nr_mmu_pages
810 - kvm->n_alloc_mmu_pages;
811
812 kvm->n_alloc_mmu_pages = kvm_nr_mmu_pages;
813}
814
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500815static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800816{
817 unsigned index;
818 struct hlist_head *bucket;
819 struct kvm_mmu_page *page;
820 struct hlist_node *node, *n;
821 int r;
822
823 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
824 r = 0;
825 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500826 bucket = &kvm->mmu_page_hash[index];
Avi Kivitya4360362007-01-05 16:36:45 -0800827 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
828 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800829 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
830 page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500831 kvm_mmu_zap_page(kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800832 r = 1;
833 }
834 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835}
836
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500837static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300838{
839 struct kvm_mmu_page *page;
840
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500841 while ((page = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Avi Kivity97a0a012007-05-31 15:08:29 +0300842 pgprintk("%s: zap %lx %x\n",
843 __FUNCTION__, gfn, page->role.word);
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500844 kvm_mmu_zap_page(kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300845 }
846}
847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
849{
850 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
851 struct kvm_mmu_page *page_head = page_header(__pa(pte));
852
853 __set_bit(slot, &page_head->slot_bitmap);
854}
855
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500856hpa_t safe_gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857{
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500858 hpa_t hpa = gpa_to_hpa(kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859
860 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
861}
862
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500863hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 struct page *page;
866
867 ASSERT((gpa & HPA_ERR_MASK) == 0);
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500868 page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300869 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
872 | (gpa & (PAGE_SIZE-1));
873}
874
875hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
876{
877 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
878
879 if (gpa == UNMAPPED_GVA)
880 return UNMAPPED_GVA;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500881 return gpa_to_hpa(vcpu->kvm, gpa);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882}
883
Avi Kivity039576c2007-03-20 12:46:50 +0200884struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
885{
886 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
887
888 if (gpa == UNMAPPED_GVA)
889 return NULL;
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500890 return pfn_to_page(gpa_to_hpa(vcpu->kvm, gpa) >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200891}
892
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
894{
895}
896
897static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
898{
899 int level = PT32E_ROOT_LEVEL;
900 hpa_t table_addr = vcpu->mmu.root_hpa;
901
902 for (; ; level--) {
903 u32 index = PT64_INDEX(v, level);
904 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800905 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906
907 ASSERT(VALID_PAGE(table_addr));
908 table = __va(table_addr);
909
910 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911 pte = table[index];
Avi Kivityc7addb92007-09-16 18:58:32 +0200912 if (is_shadow_present_pte(pte) && is_writeble_pte(pte))
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800913 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
915 page_header_update_slot(vcpu->kvm, table, v);
916 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
917 PT_USER_MASK;
Izik Eidus290fc382007-09-27 14:11:22 +0200918 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 return 0;
920 }
921
Avi Kivityc7addb92007-09-16 18:58:32 +0200922 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800923 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800926 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
927 >> PAGE_SHIFT;
928 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
929 v, level - 1,
Avi Kivity6bfccdc2007-10-11 15:13:49 +0200930 1, 3, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800931 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 pgprintk("nonpaging_map: ENOMEM\n");
933 return -ENOMEM;
934 }
935
Avi Kivity47ad8e62007-05-06 15:50:58 +0300936 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800937 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 }
939 table_addr = table[index] & PT64_BASE_ADDR_MASK;
940 }
941}
942
Avi Kivityc7addb92007-09-16 18:58:32 +0200943static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
944 struct kvm_mmu_page *sp)
945{
946 int i;
947
948 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
949 sp->spt[i] = shadow_trap_nonpresent_pte;
950}
951
Avi Kivity17ac10a2007-01-05 16:36:40 -0800952static void mmu_free_roots(struct kvm_vcpu *vcpu)
953{
954 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800955 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800956
Avi Kivity7b53aa52007-06-05 12:17:03 +0300957 if (!VALID_PAGE(vcpu->mmu.root_hpa))
958 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800959#ifdef CONFIG_X86_64
960 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
961 hpa_t root = vcpu->mmu.root_hpa;
962
Avi Kivity3bb65a22007-01-05 16:36:51 -0800963 page = page_header(root);
964 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800965 vcpu->mmu.root_hpa = INVALID_PAGE;
966 return;
967 }
968#endif
969 for (i = 0; i < 4; ++i) {
970 hpa_t root = vcpu->mmu.pae_root[i];
971
Avi Kivity417726a2007-04-12 17:35:58 +0300972 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300973 root &= PT64_BASE_ADDR_MASK;
974 page = page_header(root);
975 --page->root_count;
976 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800977 vcpu->mmu.pae_root[i] = INVALID_PAGE;
978 }
979 vcpu->mmu.root_hpa = INVALID_PAGE;
980}
981
982static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
983{
984 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800985 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800986 struct kvm_mmu_page *page;
987
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800988 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800989
990#ifdef CONFIG_X86_64
991 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
992 hpa_t root = vcpu->mmu.root_hpa;
993
994 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f62007-01-05 16:36:59 -0800995 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200996 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300997 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800998 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800999 vcpu->mmu.root_hpa = root;
1000 return;
1001 }
1002#endif
1003 for (i = 0; i < 4; ++i) {
1004 hpa_t root = vcpu->mmu.pae_root[i];
1005
1006 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +03001007 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
1008 if (!is_present_pte(vcpu->pdptrs[i])) {
1009 vcpu->mmu.pae_root[i] = 0;
1010 continue;
1011 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001012 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +03001013 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001014 root_gfn = 0;
Ingo Molnar68a99f62007-01-05 16:36:59 -08001015 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001016 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +02001017 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +03001018 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -08001019 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001020 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
1021 }
1022 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
1023}
1024
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1026{
1027 return vaddr;
1028}
1029
1030static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1031 u32 error_code)
1032{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -08001034 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -08001035 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036
Avi Kivitye2dec932007-01-05 16:36:54 -08001037 r = mmu_topup_memory_caches(vcpu);
1038 if (r)
1039 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001040
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 ASSERT(vcpu);
1042 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
1043
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001045 paddr = gpa_to_hpa(vcpu->kvm, addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046
Avi Kivityebeace82007-01-05 16:36:47 -08001047 if (is_error_hpa(paddr))
1048 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049
Avi Kivityebeace82007-01-05 16:36:47 -08001050 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051}
1052
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053static void nonpaging_free(struct kvm_vcpu *vcpu)
1054{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001055 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056}
1057
1058static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1059{
1060 struct kvm_mmu *context = &vcpu->mmu;
1061
1062 context->new_cr3 = nonpaging_new_cr3;
1063 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 context->gva_to_gpa = nonpaging_gva_to_gpa;
1065 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001066 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001067 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001069 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 return 0;
1071}
1072
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1074{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001075 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001076 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077}
1078
1079static void paging_new_cr3(struct kvm_vcpu *vcpu)
1080{
Avi Kivity374cbac2007-01-05 16:36:43 -08001081 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001082 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083}
1084
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085static void inject_page_fault(struct kvm_vcpu *vcpu,
1086 u64 addr,
1087 u32 err_code)
1088{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001089 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090}
1091
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092static void paging_free(struct kvm_vcpu *vcpu)
1093{
1094 nonpaging_free(vcpu);
1095}
1096
1097#define PTTYPE 64
1098#include "paging_tmpl.h"
1099#undef PTTYPE
1100
1101#define PTTYPE 32
1102#include "paging_tmpl.h"
1103#undef PTTYPE
1104
Avi Kivity17ac10a2007-01-05 16:36:40 -08001105static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106{
1107 struct kvm_mmu *context = &vcpu->mmu;
1108
1109 ASSERT(is_pae(vcpu));
1110 context->new_cr3 = paging_new_cr3;
1111 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001113 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001115 context->root_level = level;
1116 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001117 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 return 0;
1119}
1120
Avi Kivity17ac10a2007-01-05 16:36:40 -08001121static int paging64_init_context(struct kvm_vcpu *vcpu)
1122{
1123 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1124}
1125
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126static int paging32_init_context(struct kvm_vcpu *vcpu)
1127{
1128 struct kvm_mmu *context = &vcpu->mmu;
1129
1130 context->new_cr3 = paging_new_cr3;
1131 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 context->gva_to_gpa = paging32_gva_to_gpa;
1133 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001134 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 context->root_level = PT32_ROOT_LEVEL;
1136 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001137 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 return 0;
1139}
1140
1141static int paging32E_init_context(struct kvm_vcpu *vcpu)
1142{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001143 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144}
1145
1146static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1147{
1148 ASSERT(vcpu);
1149 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1150
1151 if (!is_paging(vcpu))
1152 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001153 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 return paging64_init_context(vcpu);
1155 else if (is_pae(vcpu))
1156 return paging32E_init_context(vcpu);
1157 else
1158 return paging32_init_context(vcpu);
1159}
1160
1161static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1162{
1163 ASSERT(vcpu);
1164 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1165 vcpu->mmu.free(vcpu);
1166 vcpu->mmu.root_hpa = INVALID_PAGE;
1167 }
1168}
1169
1170int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1171{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001172 destroy_kvm_mmu(vcpu);
1173 return init_kvm_mmu(vcpu);
1174}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001175EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001176
1177int kvm_mmu_load(struct kvm_vcpu *vcpu)
1178{
Avi Kivity714b93d2007-01-05 16:36:53 -08001179 int r;
1180
Shaohua Li11ec2802007-07-23 14:51:37 +08001181 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001182 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001183 if (r)
1184 goto out;
1185 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001186 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001187 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001188out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001189 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001190 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001192EXPORT_SYMBOL_GPL(kvm_mmu_load);
1193
1194void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1195{
1196 mmu_free_roots(vcpu);
1197}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198
Avi Kivity09072da2007-05-01 14:16:52 +03001199static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001200 struct kvm_mmu_page *page,
1201 u64 *spte)
1202{
1203 u64 pte;
1204 struct kvm_mmu_page *child;
1205
1206 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001207 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001208 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001209 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001210 else {
1211 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001212 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001213 }
1214 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001215 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001216 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001217}
1218
Avi Kivity00284252007-05-01 16:53:31 +03001219static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1220 struct kvm_mmu_page *page,
1221 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001222 const void *new, int bytes,
1223 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001224{
1225 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1226 return;
1227
1228 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001229 paging32_update_pte(vcpu, page, spte, new, bytes,
1230 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001231 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001232 paging64_update_pte(vcpu, page, spte, new, bytes,
1233 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001234}
1235
Avi Kivity12b7d282007-09-23 14:10:49 +02001236static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1237{
1238 u64 *spte = vcpu->last_pte_updated;
1239
1240 return !!(spte && (*spte & PT_ACCESSED_MASK));
1241}
1242
Avi Kivity09072da2007-05-01 14:16:52 +03001243void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001244 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001245{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001246 gfn_t gfn = gpa >> PAGE_SHIFT;
1247 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001248 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001249 struct hlist_head *bucket;
1250 unsigned index;
1251 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001252 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001253 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001254 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001255 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001256 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001257 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001258 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001259 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001260
Avi Kivityda4a00f2007-01-05 16:36:44 -08001261 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityc7addb92007-09-16 18:58:32 +02001262 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001263 if (gfn == vcpu->last_pt_write_gfn
1264 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001265 ++vcpu->last_pt_write_count;
1266 if (vcpu->last_pt_write_count >= 3)
1267 flooded = 1;
1268 } else {
1269 vcpu->last_pt_write_gfn = gfn;
1270 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001271 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001272 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001273 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1274 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001275 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001276 if (page->gfn != gfn || page->role.metaphysical)
1277 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001278 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1279 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001280 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001281 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001282 /*
1283 * Misaligned accesses are too much trouble to fix
1284 * up; also, they usually indicate a page is not used
1285 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001286 *
1287 * If we're seeing too many writes to a page,
1288 * it may no longer be a page table, or we may be
1289 * forking, in which case it is better to unmap the
1290 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001291 */
1292 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1293 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001294 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001295 continue;
1296 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001297 page_offset = offset;
1298 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001299 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001300 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001301 page_offset <<= 1; /* 32->64 */
1302 /*
1303 * A 32-bit pde maps 4MB while the shadow pdes map
1304 * only 2MB. So we need to double the offset again
1305 * and zap two pdes instead of one.
1306 */
1307 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001308 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001309 page_offset <<= 1;
1310 npte = 2;
1311 }
Avi Kivityfce06572007-05-01 16:44:05 +03001312 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001313 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001314 if (quadrant != page->role.quadrant)
1315 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001316 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001317 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001318 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001319 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001320 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1321 page_offset & (pte_size - 1));
Avi Kivityac1b7142007-03-08 17:13:32 +02001322 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001323 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001324 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001325 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001326}
1327
Avi Kivitya4360362007-01-05 16:36:45 -08001328int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1329{
1330 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1331
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001332 return kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivitya4360362007-01-05 16:36:45 -08001333}
1334
Avi Kivity22d95b12007-09-14 20:26:06 +03001335void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001336{
1337 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1338 struct kvm_mmu_page *page;
1339
1340 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1341 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001342 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityebeace82007-01-05 16:36:47 -08001343 }
1344}
Avi Kivityebeace82007-01-05 16:36:47 -08001345
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346static void free_mmu_pages(struct kvm_vcpu *vcpu)
1347{
Avi Kivityf51234c2007-01-05 16:36:52 -08001348 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349
Avi Kivityf51234c2007-01-05 16:36:52 -08001350 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1351 page = container_of(vcpu->kvm->active_mmu_pages.next,
1352 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001353 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001354 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001355 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356}
1357
1358static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1359{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001360 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 int i;
1362
1363 ASSERT(vcpu);
1364
Izik Eidus82ce2c92007-10-02 18:52:55 +02001365 if (vcpu->kvm->n_requested_mmu_pages)
1366 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_requested_mmu_pages;
1367 else
1368 vcpu->kvm->n_free_mmu_pages = vcpu->kvm->n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001369 /*
1370 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1371 * Therefore we need to allocate shadow page tables in the first
1372 * 4GB of memory, which happens to fit the DMA32 zone.
1373 */
1374 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1375 if (!page)
1376 goto error_1;
1377 vcpu->mmu.pae_root = page_address(page);
1378 for (i = 0; i < 4; ++i)
1379 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1380
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 return 0;
1382
1383error_1:
1384 free_mmu_pages(vcpu);
1385 return -ENOMEM;
1386}
1387
Ingo Molnar8018c272006-12-29 16:50:01 -08001388int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 ASSERT(vcpu);
1391 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392
Ingo Molnar8018c272006-12-29 16:50:01 -08001393 return alloc_mmu_pages(vcpu);
1394}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395
Ingo Molnar8018c272006-12-29 16:50:01 -08001396int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1397{
1398 ASSERT(vcpu);
1399 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001400
Ingo Molnar8018c272006-12-29 16:50:01 -08001401 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402}
1403
1404void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1405{
1406 ASSERT(vcpu);
1407
1408 destroy_kvm_mmu(vcpu);
1409 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001410 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411}
1412
Avi Kivity90cb0522007-07-17 13:04:56 +03001413void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414{
1415 struct kvm_mmu_page *page;
1416
1417 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1418 int i;
1419 u64 *pt;
1420
1421 if (!test_bit(slot, &page->slot_bitmap))
1422 continue;
1423
Avi Kivity47ad8e62007-05-06 15:50:58 +03001424 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1426 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001427 if (pt[i] & PT_WRITABLE_MASK) {
Izik Eidus290fc382007-09-27 14:11:22 +02001428 rmap_remove(kvm, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001430 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 }
1432}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001433
Avi Kivity90cb0522007-07-17 13:04:56 +03001434void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001435{
Avi Kivity90cb0522007-07-17 13:04:56 +03001436 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001437
Avi Kivity90cb0522007-07-17 13:04:56 +03001438 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1439 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001440
Avi Kivity90cb0522007-07-17 13:04:56 +03001441 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001442}
1443
Avi Kivityb5a33a72007-04-15 16:31:09 +03001444void kvm_mmu_module_exit(void)
1445{
1446 if (pte_chain_cache)
1447 kmem_cache_destroy(pte_chain_cache);
1448 if (rmap_desc_cache)
1449 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001450 if (mmu_page_header_cache)
1451 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001452}
1453
1454int kvm_mmu_module_init(void)
1455{
1456 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1457 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001458 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001459 if (!pte_chain_cache)
1460 goto nomem;
1461 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1462 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001463 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001464 if (!rmap_desc_cache)
1465 goto nomem;
1466
Avi Kivityd3d25b02007-05-30 12:34:53 +03001467 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1468 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001469 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001470 if (!mmu_page_header_cache)
1471 goto nomem;
1472
Avi Kivityb5a33a72007-04-15 16:31:09 +03001473 return 0;
1474
1475nomem:
1476 kvm_mmu_module_exit();
1477 return -ENOMEM;
1478}
1479
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001480#ifdef AUDIT
1481
1482static const char *audit_msg;
1483
1484static gva_t canonicalize(gva_t gva)
1485{
1486#ifdef CONFIG_X86_64
1487 gva = (long long)(gva << 16) >> 16;
1488#endif
1489 return gva;
1490}
1491
1492static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1493 gva_t va, int level)
1494{
1495 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1496 int i;
1497 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1498
1499 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1500 u64 ent = pt[i];
1501
Avi Kivityc7addb92007-09-16 18:58:32 +02001502 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001503 continue;
1504
1505 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001506 if (level > 1) {
1507 if (ent == shadow_notrap_nonpresent_pte)
1508 printk(KERN_ERR "audit: (%s) nontrapping pte"
1509 " in nonleaf level: levels %d gva %lx"
1510 " level %d pte %llx\n", audit_msg,
1511 vcpu->mmu.root_level, va, level, ent);
1512
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001513 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001514 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001515 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1516 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1517
Avi Kivityc7addb92007-09-16 18:58:32 +02001518 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001519 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001520 printk(KERN_ERR "xx audit error: (%s) levels %d"
1521 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001522 audit_msg, vcpu->mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001523 va, gpa, hpa, ent,
1524 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001525 else if (ent == shadow_notrap_nonpresent_pte
1526 && !is_error_hpa(hpa))
1527 printk(KERN_ERR "audit: (%s) notrap shadow,"
1528 " valid guest gva %lx\n", audit_msg, va);
1529
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001530 }
1531 }
1532}
1533
1534static void audit_mappings(struct kvm_vcpu *vcpu)
1535{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001536 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001537
1538 if (vcpu->mmu.root_level == 4)
1539 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1540 else
1541 for (i = 0; i < 4; ++i)
1542 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1543 audit_mappings_page(vcpu,
1544 vcpu->mmu.pae_root[i],
1545 i << 30,
1546 2);
1547}
1548
1549static int count_rmaps(struct kvm_vcpu *vcpu)
1550{
1551 int nmaps = 0;
1552 int i, j, k;
1553
1554 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1555 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1556 struct kvm_rmap_desc *d;
1557
1558 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001559 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001560
Izik Eidus290fc382007-09-27 14:11:22 +02001561 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001562 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001563 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001564 ++nmaps;
1565 continue;
1566 }
Izik Eidus290fc382007-09-27 14:11:22 +02001567 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001568 while (d) {
1569 for (k = 0; k < RMAP_EXT; ++k)
1570 if (d->shadow_ptes[k])
1571 ++nmaps;
1572 else
1573 break;
1574 d = d->more;
1575 }
1576 }
1577 }
1578 return nmaps;
1579}
1580
1581static int count_writable_mappings(struct kvm_vcpu *vcpu)
1582{
1583 int nmaps = 0;
1584 struct kvm_mmu_page *page;
1585 int i;
1586
1587 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001588 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001589
1590 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1591 continue;
1592
1593 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1594 u64 ent = pt[i];
1595
1596 if (!(ent & PT_PRESENT_MASK))
1597 continue;
1598 if (!(ent & PT_WRITABLE_MASK))
1599 continue;
1600 ++nmaps;
1601 }
1602 }
1603 return nmaps;
1604}
1605
1606static void audit_rmap(struct kvm_vcpu *vcpu)
1607{
1608 int n_rmap = count_rmaps(vcpu);
1609 int n_actual = count_writable_mappings(vcpu);
1610
1611 if (n_rmap != n_actual)
1612 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1613 __FUNCTION__, audit_msg, n_rmap, n_actual);
1614}
1615
1616static void audit_write_protection(struct kvm_vcpu *vcpu)
1617{
1618 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001619 struct kvm_memory_slot *slot;
1620 unsigned long *rmapp;
1621 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001622
1623 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001624 if (page->role.metaphysical)
1625 continue;
1626
Izik Eidus290fc382007-09-27 14:11:22 +02001627 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1628 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1629 rmapp = &slot->rmap[gfn - slot->base_gfn];
1630 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001631 printk(KERN_ERR "%s: (%s) shadow page has writable"
1632 " mappings: gfn %lx role %x\n",
1633 __FUNCTION__, audit_msg, page->gfn,
1634 page->role.word);
1635 }
1636}
1637
1638static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1639{
1640 int olddbg = dbg;
1641
1642 dbg = 0;
1643 audit_msg = msg;
1644 audit_rmap(vcpu);
1645 audit_write_protection(vcpu);
1646 audit_mappings(vcpu);
1647 dbg = olddbg;
1648}
1649
1650#endif