blob: 72757db150655d1d24e2ebaf5dd8d7edcdda7184 [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
93#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
94
95
Avi Kivity6aa8b732006-12-10 02:21:36 -080096#define PT_FIRST_AVAIL_BITS_SHIFT 9
97#define PT64_SECOND_AVAIL_BITS_SHIFT 52
98
Avi Kivity6aa8b732006-12-10 02:21:36 -080099#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define VALID_PAGE(x) ((x) != INVALID_PAGE)
102
103#define PT64_LEVEL_BITS 9
104
105#define PT64_LEVEL_SHIFT(level) \
106 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
107
108#define PT64_LEVEL_MASK(level) \
109 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
110
111#define PT64_INDEX(address, level)\
112 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
113
114
115#define PT32_LEVEL_BITS 10
116
117#define PT32_LEVEL_SHIFT(level) \
118 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
119
120#define PT32_LEVEL_MASK(level) \
121 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
122
123#define PT32_INDEX(address, level)\
124 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
125
126
Avi Kivity27aba762007-03-09 13:04:31 +0200127#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128#define PT64_DIR_BASE_ADDR_MASK \
129 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
130
131#define PT32_BASE_ADDR_MASK PAGE_MASK
132#define PT32_DIR_BASE_ADDR_MASK \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
134
135
136#define PFERR_PRESENT_MASK (1U << 0)
137#define PFERR_WRITE_MASK (1U << 1)
138#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800139#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PT64_ROOT_LEVEL 4
142#define PT32_ROOT_LEVEL 2
143#define PT32E_ROOT_LEVEL 3
144
145#define PT_DIRECTORY_LEVEL 2
146#define PT_PAGE_TABLE_LEVEL 1
147
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800148#define RMAP_EXT 4
149
150struct kvm_rmap_desc {
151 u64 *shadow_ptes[RMAP_EXT];
152 struct kvm_rmap_desc *more;
153};
154
Avi Kivityb5a33a72007-04-15 16:31:09 +0300155static struct kmem_cache *pte_chain_cache;
156static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300157static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300158
Avi Kivityc7addb92007-09-16 18:58:32 +0200159static u64 __read_mostly shadow_trap_nonpresent_pte;
160static u64 __read_mostly shadow_notrap_nonpresent_pte;
161
162void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
163{
164 shadow_trap_nonpresent_pte = trap_pte;
165 shadow_notrap_nonpresent_pte = notrap_pte;
166}
167EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
168
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static int is_write_protection(struct kvm_vcpu *vcpu)
170{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000171 return vcpu->cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172}
173
174static int is_cpuid_PSE36(void)
175{
176 return 1;
177}
178
Avi Kivity73b10872007-01-26 00:56:41 -0800179static int is_nx(struct kvm_vcpu *vcpu)
180{
181 return vcpu->shadow_efer & EFER_NX;
182}
183
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184static int is_present_pte(unsigned long pte)
185{
186 return pte & PT_PRESENT_MASK;
187}
188
Avi Kivityc7addb92007-09-16 18:58:32 +0200189static int is_shadow_present_pte(u64 pte)
190{
191 pte &= ~PT_SHADOW_IO_MARK;
192 return pte != shadow_trap_nonpresent_pte
193 && pte != shadow_notrap_nonpresent_pte;
194}
195
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196static int is_writeble_pte(unsigned long pte)
197{
198 return pte & PT_WRITABLE_MASK;
199}
200
201static int is_io_pte(unsigned long pte)
202{
203 return pte & PT_SHADOW_IO_MARK;
204}
205
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800206static int is_rmap_pte(u64 pte)
207{
208 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
209 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
210}
211
Avi Kivitye663ee62007-05-31 15:46:04 +0300212static void set_shadow_pte(u64 *sptep, u64 spte)
213{
214#ifdef CONFIG_X86_64
215 set_64bit((unsigned long *)sptep, spte);
216#else
217 set_64bit((unsigned long long *)sptep, spte);
218#endif
219}
220
Avi Kivitye2dec932007-01-05 16:36:54 -0800221static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300222 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800223{
224 void *obj;
225
226 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800227 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800228 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300229 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800230 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800231 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800232 cache->objects[cache->nobjs++] = obj;
233 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800234 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800235}
236
237static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
238{
239 while (mc->nobjs)
240 kfree(mc->objects[--mc->nobjs]);
241}
242
Avi Kivityc1158e62007-07-20 08:18:27 +0300243static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300244 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300245{
246 struct page *page;
247
248 if (cache->nobjs >= min)
249 return 0;
250 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300251 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300252 if (!page)
253 return -ENOMEM;
254 set_page_private(page, 0);
255 cache->objects[cache->nobjs++] = page_address(page);
256 }
257 return 0;
258}
259
260static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
261{
262 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300263 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300264}
265
Avi Kivity8c438502007-04-16 11:53:17 +0300266static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
267{
268 int r;
269
Avi Kivity22d95b12007-09-14 20:26:06 +0300270 kvm_mmu_free_some_pages(vcpu);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300271 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
272 pte_chain_cache, 4);
273 if (r)
274 goto out;
275 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
276 rmap_desc_cache, 1);
277 if (r)
278 goto out;
Izik Eidus290fc382007-09-27 14:11:22 +0200279 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300280 if (r)
281 goto out;
282 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
283 mmu_page_header_cache, 4);
284out:
Avi Kivity8c438502007-04-16 11:53:17 +0300285 return r;
286}
287
Avi Kivity714b93d2007-01-05 16:36:53 -0800288static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
289{
290 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
291 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
Avi Kivityc1158e62007-07-20 08:18:27 +0300292 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300293 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800294}
295
296static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
297 size_t size)
298{
299 void *p;
300
301 BUG_ON(!mc->nobjs);
302 p = mc->objects[--mc->nobjs];
303 memset(p, 0, size);
304 return p;
305}
306
Avi Kivity714b93d2007-01-05 16:36:53 -0800307static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
308{
309 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
310 sizeof(struct kvm_pte_chain));
311}
312
Avi Kivity90cb0522007-07-17 13:04:56 +0300313static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800314{
Avi Kivity90cb0522007-07-17 13:04:56 +0300315 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800316}
317
318static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
319{
320 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
321 sizeof(struct kvm_rmap_desc));
322}
323
Avi Kivity90cb0522007-07-17 13:04:56 +0300324static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800325{
Avi Kivity90cb0522007-07-17 13:04:56 +0300326 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800327}
328
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800329/*
Izik Eidus290fc382007-09-27 14:11:22 +0200330 * Take gfn and return the reverse mapping to it.
331 * Note: gfn must be unaliased before this function get called
332 */
333
334static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
335{
336 struct kvm_memory_slot *slot;
337
338 slot = gfn_to_memslot(kvm, gfn);
339 return &slot->rmap[gfn - slot->base_gfn];
340}
341
342/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800343 * Reverse mapping data structures:
344 *
Izik Eidus290fc382007-09-27 14:11:22 +0200345 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
346 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800347 *
Izik Eidus290fc382007-09-27 14:11:22 +0200348 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
349 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800350 */
Izik Eidus290fc382007-09-27 14:11:22 +0200351static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800352{
Izik Eidus290fc382007-09-27 14:11:22 +0200353 struct kvm_mmu_page *page;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800354 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200355 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800356 int i;
357
358 if (!is_rmap_pte(*spte))
359 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200360 gfn = unalias_gfn(vcpu->kvm, gfn);
361 page = page_header(__pa(spte));
362 page->gfns[spte - page->spt] = gfn;
363 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
364 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800365 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200366 *rmapp = (unsigned long)spte;
367 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800368 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800369 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200370 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800371 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200372 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373 } else {
374 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200375 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800376 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
377 desc = desc->more;
378 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800379 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800380 desc = desc->more;
381 }
382 for (i = 0; desc->shadow_ptes[i]; ++i)
383 ;
384 desc->shadow_ptes[i] = spte;
385 }
386}
387
Izik Eidus290fc382007-09-27 14:11:22 +0200388static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800389 struct kvm_rmap_desc *desc,
390 int i,
391 struct kvm_rmap_desc *prev_desc)
392{
393 int j;
394
395 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
396 ;
397 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000398 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800399 if (j != 0)
400 return;
401 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200402 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800403 else
404 if (prev_desc)
405 prev_desc->more = desc->more;
406 else
Izik Eidus290fc382007-09-27 14:11:22 +0200407 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300408 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800409}
410
Izik Eidus290fc382007-09-27 14:11:22 +0200411static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800412{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800413 struct kvm_rmap_desc *desc;
414 struct kvm_rmap_desc *prev_desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200415 struct kvm_mmu_page *page;
416 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800417 int i;
418
419 if (!is_rmap_pte(*spte))
420 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200421 page = page_header(__pa(spte));
422 rmapp = gfn_to_rmap(kvm, page->gfns[spte - page->spt]);
423 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800424 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
425 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200426 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800427 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200428 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800429 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
430 spte, *spte);
431 BUG();
432 }
Izik Eidus290fc382007-09-27 14:11:22 +0200433 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800434 } else {
435 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200436 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800437 prev_desc = NULL;
438 while (desc) {
439 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
440 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200441 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800442 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800443 prev_desc);
444 return;
445 }
446 prev_desc = desc;
447 desc = desc->more;
448 }
449 BUG();
450 }
451}
452
Avi Kivity714b93d2007-01-05 16:36:53 -0800453static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Avi Kivity374cbac2007-01-05 16:36:43 -0800454{
Avi Kivity374cbac2007-01-05 16:36:43 -0800455 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200456 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800457 u64 *spte;
458
Izik Eidus290fc382007-09-27 14:11:22 +0200459 gfn = unalias_gfn(vcpu->kvm, gfn);
460 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800461
Izik Eidus290fc382007-09-27 14:11:22 +0200462 while (*rmapp) {
463 if (!(*rmapp & 1))
464 spte = (u64 *)*rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800465 else {
Izik Eidus290fc382007-09-27 14:11:22 +0200466 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity374cbac2007-01-05 16:36:43 -0800467 spte = desc->shadow_ptes[0];
468 }
469 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800470 BUG_ON(!(*spte & PT_PRESENT_MASK));
471 BUG_ON(!(*spte & PT_WRITABLE_MASK));
472 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200473 rmap_remove(vcpu->kvm, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300474 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Shaohua Li88a97f02007-06-20 17:13:26 +0800475 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800476 }
477}
478
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800479#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300480static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481{
Avi Kivity139bdb22007-01-05 16:36:50 -0800482 u64 *pos;
483 u64 *end;
484
Avi Kivity47ad8e62007-05-06 15:50:58 +0300485 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200486 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800487 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
488 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800490 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 return 1;
492}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800493#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494
Avi Kivity90cb0522007-07-17 13:04:56 +0300495static void kvm_mmu_free_page(struct kvm *kvm,
Avi Kivity4b02d6d2007-05-06 15:36:30 +0300496 struct kvm_mmu_page *page_head)
Avi Kivity260746c2007-01-05 16:36:49 -0800497{
Avi Kivity47ad8e62007-05-06 15:50:58 +0300498 ASSERT(is_empty_shadow_page(page_head->spt));
Avi Kivityd3d25b02007-05-30 12:34:53 +0300499 list_del(&page_head->link);
Avi Kivityc1158e62007-07-20 08:18:27 +0300500 __free_page(virt_to_page(page_head->spt));
Izik Eidus290fc382007-09-27 14:11:22 +0200501 __free_page(virt_to_page(page_head->gfns));
Avi Kivity90cb0522007-07-17 13:04:56 +0300502 kfree(page_head);
503 ++kvm->n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800504}
505
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800506static unsigned kvm_page_table_hashfn(gfn_t gfn)
507{
508 return gfn;
509}
510
Avi Kivity25c0de22007-01-05 16:36:42 -0800511static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
512 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513{
514 struct kvm_mmu_page *page;
515
Avi Kivityd3d25b02007-05-30 12:34:53 +0300516 if (!vcpu->kvm->n_free_mmu_pages)
Avi Kivity25c0de22007-01-05 16:36:42 -0800517 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518
Avi Kivityd3d25b02007-05-30 12:34:53 +0300519 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
520 sizeof *page);
521 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Izik Eidus290fc382007-09-27 14:11:22 +0200522 page->gfns = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
Avi Kivityd3d25b02007-05-30 12:34:53 +0300523 set_page_private(virt_to_page(page->spt), (unsigned long)page);
524 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300525 ASSERT(is_empty_shadow_page(page->spt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 page->slot_bitmap = 0;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800527 page->multimapped = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 page->parent_pte = parent_pte;
Avi Kivityebeace82007-01-05 16:36:47 -0800529 --vcpu->kvm->n_free_mmu_pages;
Avi Kivity25c0de22007-01-05 16:36:42 -0800530 return page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531}
532
Avi Kivity714b93d2007-01-05 16:36:53 -0800533static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
534 struct kvm_mmu_page *page, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800535{
536 struct kvm_pte_chain *pte_chain;
537 struct hlist_node *node;
538 int i;
539
540 if (!parent_pte)
541 return;
542 if (!page->multimapped) {
543 u64 *old = page->parent_pte;
544
545 if (!old) {
546 page->parent_pte = parent_pte;
547 return;
548 }
549 page->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800550 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800551 INIT_HLIST_HEAD(&page->parent_ptes);
552 hlist_add_head(&pte_chain->link, &page->parent_ptes);
553 pte_chain->parent_ptes[0] = old;
554 }
555 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
556 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
557 continue;
558 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
559 if (!pte_chain->parent_ptes[i]) {
560 pte_chain->parent_ptes[i] = parent_pte;
561 return;
562 }
563 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800564 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800565 BUG_ON(!pte_chain);
566 hlist_add_head(&pte_chain->link, &page->parent_ptes);
567 pte_chain->parent_ptes[0] = parent_pte;
568}
569
Avi Kivity90cb0522007-07-17 13:04:56 +0300570static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800571 u64 *parent_pte)
572{
573 struct kvm_pte_chain *pte_chain;
574 struct hlist_node *node;
575 int i;
576
577 if (!page->multimapped) {
578 BUG_ON(page->parent_pte != parent_pte);
579 page->parent_pte = NULL;
580 return;
581 }
582 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
583 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
584 if (!pte_chain->parent_ptes[i])
585 break;
586 if (pte_chain->parent_ptes[i] != parent_pte)
587 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800588 while (i + 1 < NR_PTE_CHAIN_ENTRIES
589 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800590 pte_chain->parent_ptes[i]
591 = pte_chain->parent_ptes[i + 1];
592 ++i;
593 }
594 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800595 if (i == 0) {
596 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300597 mmu_free_pte_chain(pte_chain);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800598 if (hlist_empty(&page->parent_ptes)) {
599 page->multimapped = 0;
600 page->parent_pte = NULL;
601 }
602 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800603 return;
604 }
605 BUG();
606}
607
608static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
609 gfn_t gfn)
610{
611 unsigned index;
612 struct hlist_head *bucket;
613 struct kvm_mmu_page *page;
614 struct hlist_node *node;
615
616 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
617 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
618 bucket = &vcpu->kvm->mmu_page_hash[index];
619 hlist_for_each_entry(page, node, bucket, hash_link)
620 if (page->gfn == gfn && !page->role.metaphysical) {
621 pgprintk("%s: found role %x\n",
622 __FUNCTION__, page->role.word);
623 return page;
624 }
625 return NULL;
626}
627
628static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
629 gfn_t gfn,
630 gva_t gaddr,
631 unsigned level,
632 int metaphysical,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200633 unsigned hugepage_access,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800634 u64 *parent_pte)
635{
636 union kvm_mmu_page_role role;
637 unsigned index;
638 unsigned quadrant;
639 struct hlist_head *bucket;
640 struct kvm_mmu_page *page;
641 struct hlist_node *node;
642
643 role.word = 0;
644 role.glevels = vcpu->mmu.root_level;
645 role.level = level;
646 role.metaphysical = metaphysical;
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200647 role.hugepage_access = hugepage_access;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800648 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
649 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
650 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
651 role.quadrant = quadrant;
652 }
653 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
654 gfn, role.word);
655 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
656 bucket = &vcpu->kvm->mmu_page_hash[index];
657 hlist_for_each_entry(page, node, bucket, hash_link)
658 if (page->gfn == gfn && page->role.word == role.word) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800659 mmu_page_add_parent_pte(vcpu, page, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800660 pgprintk("%s: found\n", __FUNCTION__);
661 return page;
662 }
663 page = kvm_mmu_alloc_page(vcpu, parent_pte);
664 if (!page)
665 return page;
666 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
667 page->gfn = gfn;
668 page->role = role;
669 hlist_add_head(&page->hash_link, bucket);
Avi Kivityc7addb92007-09-16 18:58:32 +0200670 vcpu->mmu.prefetch_page(vcpu, page);
Avi Kivity374cbac2007-01-05 16:36:43 -0800671 if (!metaphysical)
Avi Kivity714b93d2007-01-05 16:36:53 -0800672 rmap_write_protect(vcpu, gfn);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800673 return page;
674}
675
Avi Kivity90cb0522007-07-17 13:04:56 +0300676static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800677 struct kvm_mmu_page *page)
678{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800679 unsigned i;
680 u64 *pt;
681 u64 ent;
682
Avi Kivity47ad8e62007-05-06 15:50:58 +0300683 pt = page->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800684
685 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
686 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200687 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200688 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200689 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800690 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300691 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800692 return;
693 }
694
695 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
696 ent = pt[i];
697
Avi Kivityc7addb92007-09-16 18:58:32 +0200698 pt[i] = shadow_trap_nonpresent_pte;
699 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800700 continue;
701 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300702 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800703 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300704 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800705}
706
Avi Kivity90cb0522007-07-17 13:04:56 +0300707static void kvm_mmu_put_page(struct kvm_mmu_page *page,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800708 u64 *parent_pte)
709{
Avi Kivity90cb0522007-07-17 13:04:56 +0300710 mmu_page_remove_parent_pte(page, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800711}
712
Avi Kivity12b7d282007-09-23 14:10:49 +0200713static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
714{
715 int i;
716
717 for (i = 0; i < KVM_MAX_VCPUS; ++i)
718 if (kvm->vcpus[i])
719 kvm->vcpus[i]->last_pte_updated = NULL;
720}
721
Avi Kivity90cb0522007-07-17 13:04:56 +0300722static void kvm_mmu_zap_page(struct kvm *kvm,
Avi Kivitya4360362007-01-05 16:36:45 -0800723 struct kvm_mmu_page *page)
724{
725 u64 *parent_pte;
726
727 while (page->multimapped || page->parent_pte) {
728 if (!page->multimapped)
729 parent_pte = page->parent_pte;
730 else {
731 struct kvm_pte_chain *chain;
732
733 chain = container_of(page->parent_ptes.first,
734 struct kvm_pte_chain, link);
735 parent_pte = chain->parent_ptes[0];
736 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800737 BUG_ON(!parent_pte);
Avi Kivity90cb0522007-07-17 13:04:56 +0300738 kvm_mmu_put_page(page, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200739 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800740 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300741 kvm_mmu_page_unlink_children(kvm, page);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800742 if (!page->root_count) {
743 hlist_del(&page->hash_link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300744 kvm_mmu_free_page(kvm, page);
Avi Kivity36868f72007-03-26 19:31:52 +0200745 } else
Avi Kivity90cb0522007-07-17 13:04:56 +0300746 list_move(&page->link, &kvm->active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200747 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800748}
749
750static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
751{
752 unsigned index;
753 struct hlist_head *bucket;
754 struct kvm_mmu_page *page;
755 struct hlist_node *node, *n;
756 int r;
757
758 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
759 r = 0;
760 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
761 bucket = &vcpu->kvm->mmu_page_hash[index];
762 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
763 if (page->gfn == gfn && !page->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800764 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
765 page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +0300766 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivitya4360362007-01-05 16:36:45 -0800767 r = 1;
768 }
769 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800770}
771
Avi Kivity97a0a012007-05-31 15:08:29 +0300772static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn)
773{
774 struct kvm_mmu_page *page;
775
776 while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) {
777 pgprintk("%s: zap %lx %x\n",
778 __FUNCTION__, gfn, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +0300779 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity97a0a012007-05-31 15:08:29 +0300780 }
781}
782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
784{
785 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
786 struct kvm_mmu_page *page_head = page_header(__pa(pte));
787
788 __set_bit(slot, &page_head->slot_bitmap);
789}
790
791hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
792{
793 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
794
795 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
796}
797
798hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
799{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 struct page *page;
801
802 ASSERT((gpa & HPA_ERR_MASK) == 0);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300803 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
804 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 return gpa | HPA_ERR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
807 | (gpa & (PAGE_SIZE-1));
808}
809
810hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
811{
812 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
813
814 if (gpa == UNMAPPED_GVA)
815 return UNMAPPED_GVA;
816 return gpa_to_hpa(vcpu, gpa);
817}
818
Avi Kivity039576c2007-03-20 12:46:50 +0200819struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
820{
821 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
822
823 if (gpa == UNMAPPED_GVA)
824 return NULL;
825 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
826}
827
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
829{
830}
831
832static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
833{
834 int level = PT32E_ROOT_LEVEL;
835 hpa_t table_addr = vcpu->mmu.root_hpa;
836
837 for (; ; level--) {
838 u32 index = PT64_INDEX(v, level);
839 u64 *table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800840 u64 pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
842 ASSERT(VALID_PAGE(table_addr));
843 table = __va(table_addr);
844
845 if (level == 1) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 pte = table[index];
Avi Kivityc7addb92007-09-16 18:58:32 +0200847 if (is_shadow_present_pte(pte) && is_writeble_pte(pte))
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800848 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
850 page_header_update_slot(vcpu->kvm, table, v);
851 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
852 PT_USER_MASK;
Izik Eidus290fc382007-09-27 14:11:22 +0200853 rmap_add(vcpu, &table[index], v >> PAGE_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 return 0;
855 }
856
Avi Kivityc7addb92007-09-16 18:58:32 +0200857 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800858 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800859 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800861 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
862 >> PAGE_SHIFT;
863 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
864 v, level - 1,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200865 1, 0, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -0800866 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 pgprintk("nonpaging_map: ENOMEM\n");
868 return -ENOMEM;
869 }
870
Avi Kivity47ad8e62007-05-06 15:50:58 +0300871 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -0800872 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 }
874 table_addr = table[index] & PT64_BASE_ADDR_MASK;
875 }
876}
877
Avi Kivityc7addb92007-09-16 18:58:32 +0200878static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
879 struct kvm_mmu_page *sp)
880{
881 int i;
882
883 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
884 sp->spt[i] = shadow_trap_nonpresent_pte;
885}
886
Avi Kivity17ac10a2007-01-05 16:36:40 -0800887static void mmu_free_roots(struct kvm_vcpu *vcpu)
888{
889 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800890 struct kvm_mmu_page *page;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800891
Avi Kivity7b53aa52007-06-05 12:17:03 +0300892 if (!VALID_PAGE(vcpu->mmu.root_hpa))
893 return;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800894#ifdef CONFIG_X86_64
895 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
896 hpa_t root = vcpu->mmu.root_hpa;
897
Avi Kivity3bb65a22007-01-05 16:36:51 -0800898 page = page_header(root);
899 --page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800900 vcpu->mmu.root_hpa = INVALID_PAGE;
901 return;
902 }
903#endif
904 for (i = 0; i < 4; ++i) {
905 hpa_t root = vcpu->mmu.pae_root[i];
906
Avi Kivity417726a2007-04-12 17:35:58 +0300907 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +0300908 root &= PT64_BASE_ADDR_MASK;
909 page = page_header(root);
910 --page->root_count;
911 }
Avi Kivity17ac10a2007-01-05 16:36:40 -0800912 vcpu->mmu.pae_root[i] = INVALID_PAGE;
913 }
914 vcpu->mmu.root_hpa = INVALID_PAGE;
915}
916
917static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
918{
919 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800920 gfn_t root_gfn;
Avi Kivity3bb65a22007-01-05 16:36:51 -0800921 struct kvm_mmu_page *page;
922
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800923 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800924
925#ifdef CONFIG_X86_64
926 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
927 hpa_t root = vcpu->mmu.root_hpa;
928
929 ASSERT(!VALID_PAGE(root));
Ingo Molnar68a99f6d2007-01-05 16:36:59 -0800930 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200931 PT64_ROOT_LEVEL, 0, 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300932 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800933 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800934 vcpu->mmu.root_hpa = root;
935 return;
936 }
937#endif
938 for (i = 0; i < 4; ++i) {
939 hpa_t root = vcpu->mmu.pae_root[i];
940
941 ASSERT(!VALID_PAGE(root));
Avi Kivity417726a2007-04-12 17:35:58 +0300942 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
943 if (!is_present_pte(vcpu->pdptrs[i])) {
944 vcpu->mmu.pae_root[i] = 0;
945 continue;
946 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
Avi Kivity417726a2007-04-12 17:35:58 +0300948 } else if (vcpu->mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800949 root_gfn = 0;
Ingo Molnar68a99f6d2007-01-05 16:36:59 -0800950 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800951 PT32_ROOT_LEVEL, !is_paging(vcpu),
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200952 0, NULL);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300953 root = __pa(page->spt);
Avi Kivity3bb65a22007-01-05 16:36:51 -0800954 ++page->root_count;
Avi Kivity17ac10a2007-01-05 16:36:40 -0800955 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
956 }
957 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
958}
959
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
961{
962 return vaddr;
963}
964
965static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
966 u32 error_code)
967{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 gpa_t addr = gva;
Avi Kivityebeace82007-01-05 16:36:47 -0800969 hpa_t paddr;
Avi Kivitye2dec932007-01-05 16:36:54 -0800970 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Avi Kivitye2dec932007-01-05 16:36:54 -0800972 r = mmu_topup_memory_caches(vcpu);
973 if (r)
974 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800975
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 ASSERT(vcpu);
977 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
978
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979
Avi Kivityebeace82007-01-05 16:36:47 -0800980 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981
Avi Kivityebeace82007-01-05 16:36:47 -0800982 if (is_error_hpa(paddr))
983 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Avi Kivityebeace82007-01-05 16:36:47 -0800985 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986}
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988static void nonpaging_free(struct kvm_vcpu *vcpu)
989{
Avi Kivity17ac10a2007-01-05 16:36:40 -0800990 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991}
992
993static int nonpaging_init_context(struct kvm_vcpu *vcpu)
994{
995 struct kvm_mmu *context = &vcpu->mmu;
996
997 context->new_cr3 = nonpaging_new_cr3;
998 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 context->gva_to_gpa = nonpaging_gva_to_gpa;
1000 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001001 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001002 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001004 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 return 0;
1006}
1007
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1009{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001010 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001011 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
1014static void paging_new_cr3(struct kvm_vcpu *vcpu)
1015{
Avi Kivity374cbac2007-01-05 16:36:43 -08001016 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001017 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018}
1019
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020static void inject_page_fault(struct kvm_vcpu *vcpu,
1021 u64 addr,
1022 u32 err_code)
1023{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001024 kvm_x86_ops->inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025}
1026
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027static void paging_free(struct kvm_vcpu *vcpu)
1028{
1029 nonpaging_free(vcpu);
1030}
1031
1032#define PTTYPE 64
1033#include "paging_tmpl.h"
1034#undef PTTYPE
1035
1036#define PTTYPE 32
1037#include "paging_tmpl.h"
1038#undef PTTYPE
1039
Avi Kivity17ac10a2007-01-05 16:36:40 -08001040static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041{
1042 struct kvm_mmu *context = &vcpu->mmu;
1043
1044 ASSERT(is_pae(vcpu));
1045 context->new_cr3 = paging_new_cr3;
1046 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001048 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001050 context->root_level = level;
1051 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001052 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 return 0;
1054}
1055
Avi Kivity17ac10a2007-01-05 16:36:40 -08001056static int paging64_init_context(struct kvm_vcpu *vcpu)
1057{
1058 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1059}
1060
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061static int paging32_init_context(struct kvm_vcpu *vcpu)
1062{
1063 struct kvm_mmu *context = &vcpu->mmu;
1064
1065 context->new_cr3 = paging_new_cr3;
1066 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 context->gva_to_gpa = paging32_gva_to_gpa;
1068 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001069 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 context->root_level = PT32_ROOT_LEVEL;
1071 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001072 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 return 0;
1074}
1075
1076static int paging32E_init_context(struct kvm_vcpu *vcpu)
1077{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001078 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079}
1080
1081static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1082{
1083 ASSERT(vcpu);
1084 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1085
1086 if (!is_paging(vcpu))
1087 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001088 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 return paging64_init_context(vcpu);
1090 else if (is_pae(vcpu))
1091 return paging32E_init_context(vcpu);
1092 else
1093 return paging32_init_context(vcpu);
1094}
1095
1096static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1097{
1098 ASSERT(vcpu);
1099 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1100 vcpu->mmu.free(vcpu);
1101 vcpu->mmu.root_hpa = INVALID_PAGE;
1102 }
1103}
1104
1105int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1106{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001107 destroy_kvm_mmu(vcpu);
1108 return init_kvm_mmu(vcpu);
1109}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001110EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001111
1112int kvm_mmu_load(struct kvm_vcpu *vcpu)
1113{
Avi Kivity714b93d2007-01-05 16:36:53 -08001114 int r;
1115
Shaohua Li11ec2802007-07-23 14:51:37 +08001116 mutex_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001117 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001118 if (r)
1119 goto out;
1120 mmu_alloc_roots(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001121 kvm_x86_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001122 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001123out:
Shaohua Li11ec2802007-07-23 14:51:37 +08001124 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity714b93d2007-01-05 16:36:53 -08001125 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001127EXPORT_SYMBOL_GPL(kvm_mmu_load);
1128
1129void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1130{
1131 mmu_free_roots(vcpu);
1132}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133
Avi Kivity09072da2007-05-01 14:16:52 +03001134static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivityac1b7142007-03-08 17:13:32 +02001135 struct kvm_mmu_page *page,
1136 u64 *spte)
1137{
1138 u64 pte;
1139 struct kvm_mmu_page *child;
1140
1141 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001142 if (is_shadow_present_pte(pte)) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001143 if (page->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001144 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001145 else {
1146 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001147 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001148 }
1149 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001150 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityd9e368d2007-06-07 19:18:30 +03001151 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityac1b7142007-03-08 17:13:32 +02001152}
1153
Avi Kivity00284252007-05-01 16:53:31 +03001154static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1155 struct kvm_mmu_page *page,
1156 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001157 const void *new, int bytes,
1158 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001159{
1160 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1161 return;
1162
1163 if (page->role.glevels == PT32_ROOT_LEVEL)
Avi Kivityc7addb92007-09-16 18:58:32 +02001164 paging32_update_pte(vcpu, page, spte, new, bytes,
1165 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001166 else
Avi Kivityc7addb92007-09-16 18:58:32 +02001167 paging64_update_pte(vcpu, page, spte, new, bytes,
1168 offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001169}
1170
Avi Kivity12b7d282007-09-23 14:10:49 +02001171static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1172{
1173 u64 *spte = vcpu->last_pte_updated;
1174
1175 return !!(spte && (*spte & PT_ACCESSED_MASK));
1176}
1177
Avi Kivity09072da2007-05-01 14:16:52 +03001178void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life5518812007-07-23 14:51:39 +08001179 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001180{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001181 gfn_t gfn = gpa >> PAGE_SHIFT;
1182 struct kvm_mmu_page *page;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001183 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001184 struct hlist_head *bucket;
1185 unsigned index;
1186 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001187 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001188 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001189 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001190 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001191 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001192 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001193 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001194 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001195
Avi Kivityda4a00f2007-01-05 16:36:44 -08001196 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityc7addb92007-09-16 18:58:32 +02001197 kvm_mmu_audit(vcpu, "pre pte write");
Avi Kivity12b7d282007-09-23 14:10:49 +02001198 if (gfn == vcpu->last_pt_write_gfn
1199 && !last_updated_pte_accessed(vcpu)) {
Avi Kivity86a5ba02007-01-05 16:36:50 -08001200 ++vcpu->last_pt_write_count;
1201 if (vcpu->last_pt_write_count >= 3)
1202 flooded = 1;
1203 } else {
1204 vcpu->last_pt_write_gfn = gfn;
1205 vcpu->last_pt_write_count = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001206 vcpu->last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001207 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001208 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1209 bucket = &vcpu->kvm->mmu_page_hash[index];
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001210 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
Avi Kivity9b7a0322007-01-05 16:36:45 -08001211 if (page->gfn != gfn || page->role.metaphysical)
1212 continue;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001213 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1214 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001215 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001216 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001217 /*
1218 * Misaligned accesses are too much trouble to fix
1219 * up; also, they usually indicate a page is not used
1220 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001221 *
1222 * If we're seeing too many writes to a page,
1223 * it may no longer be a page table, or we may be
1224 * forking, in which case it is better to unmap the
1225 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001226 */
1227 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1228 gpa, bytes, page->role.word);
Avi Kivity90cb0522007-07-17 13:04:56 +03001229 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001230 continue;
1231 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001232 page_offset = offset;
1233 level = page->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001234 npte = 1;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001235 if (page->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001236 page_offset <<= 1; /* 32->64 */
1237 /*
1238 * A 32-bit pde maps 4MB while the shadow pdes map
1239 * only 2MB. So we need to double the offset again
1240 * and zap two pdes instead of one.
1241 */
1242 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001243 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001244 page_offset <<= 1;
1245 npte = 2;
1246 }
Avi Kivityfce06572007-05-01 16:44:05 +03001247 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001248 page_offset &= ~PAGE_MASK;
Avi Kivityfce06572007-05-01 16:44:05 +03001249 if (quadrant != page->role.quadrant)
1250 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001251 }
Avi Kivity47ad8e62007-05-06 15:50:58 +03001252 spte = &page->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001253 while (npte--) {
Avi Kivity09072da2007-05-01 14:16:52 +03001254 mmu_pte_write_zap_pte(vcpu, page, spte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001255 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes,
1256 page_offset & (pte_size - 1));
Avi Kivityac1b7142007-03-08 17:13:32 +02001257 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001258 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001259 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001260 kvm_mmu_audit(vcpu, "post pte write");
Avi Kivityda4a00f2007-01-05 16:36:44 -08001261}
1262
Avi Kivitya4360362007-01-05 16:36:45 -08001263int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1264{
1265 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1266
1267 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1268}
1269
Avi Kivity22d95b12007-09-14 20:26:06 +03001270void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001271{
1272 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1273 struct kvm_mmu_page *page;
1274
1275 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1276 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001277 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityebeace82007-01-05 16:36:47 -08001278 }
1279}
Avi Kivityebeace82007-01-05 16:36:47 -08001280
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281static void free_mmu_pages(struct kvm_vcpu *vcpu)
1282{
Avi Kivityf51234c2007-01-05 16:36:52 -08001283 struct kvm_mmu_page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284
Avi Kivityf51234c2007-01-05 16:36:52 -08001285 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1286 page = container_of(vcpu->kvm->active_mmu_pages.next,
1287 struct kvm_mmu_page, link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001288 kvm_mmu_zap_page(vcpu->kvm, page);
Avi Kivityf51234c2007-01-05 16:36:52 -08001289 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001290 free_page((unsigned long)vcpu->mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
1293static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1294{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001295 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 int i;
1297
1298 ASSERT(vcpu);
1299
Avi Kivityd3d25b02007-05-30 12:34:53 +03001300 vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001301
1302 /*
1303 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1304 * Therefore we need to allocate shadow page tables in the first
1305 * 4GB of memory, which happens to fit the DMA32 zone.
1306 */
1307 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1308 if (!page)
1309 goto error_1;
1310 vcpu->mmu.pae_root = page_address(page);
1311 for (i = 0; i < 4; ++i)
1312 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1313
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 return 0;
1315
1316error_1:
1317 free_mmu_pages(vcpu);
1318 return -ENOMEM;
1319}
1320
Ingo Molnar8018c272006-12-29 16:50:01 -08001321int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 ASSERT(vcpu);
1324 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325
Ingo Molnar8018c272006-12-29 16:50:01 -08001326 return alloc_mmu_pages(vcpu);
1327}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328
Ingo Molnar8018c272006-12-29 16:50:01 -08001329int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1330{
1331 ASSERT(vcpu);
1332 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001333
Ingo Molnar8018c272006-12-29 16:50:01 -08001334 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
1336
1337void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1338{
1339 ASSERT(vcpu);
1340
1341 destroy_kvm_mmu(vcpu);
1342 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001343 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
Avi Kivity90cb0522007-07-17 13:04:56 +03001346void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347{
1348 struct kvm_mmu_page *page;
1349
1350 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1351 int i;
1352 u64 *pt;
1353
1354 if (!test_bit(slot, &page->slot_bitmap))
1355 continue;
1356
Avi Kivity47ad8e62007-05-06 15:50:58 +03001357 pt = page->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1359 /* avoid RMW */
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001360 if (pt[i] & PT_WRITABLE_MASK) {
Izik Eidus290fc382007-09-27 14:11:22 +02001361 rmap_remove(kvm, &pt[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001363 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 }
1365}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001366
Avi Kivity90cb0522007-07-17 13:04:56 +03001367void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001368{
Avi Kivity90cb0522007-07-17 13:04:56 +03001369 struct kvm_mmu_page *page, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001370
Avi Kivity90cb0522007-07-17 13:04:56 +03001371 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1372 kvm_mmu_zap_page(kvm, page);
Dor Laore0fa8262007-03-30 13:06:33 +03001373
Avi Kivity90cb0522007-07-17 13:04:56 +03001374 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001375}
1376
Avi Kivityb5a33a72007-04-15 16:31:09 +03001377void kvm_mmu_module_exit(void)
1378{
1379 if (pte_chain_cache)
1380 kmem_cache_destroy(pte_chain_cache);
1381 if (rmap_desc_cache)
1382 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001383 if (mmu_page_header_cache)
1384 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001385}
1386
1387int kvm_mmu_module_init(void)
1388{
1389 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1390 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001391 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001392 if (!pte_chain_cache)
1393 goto nomem;
1394 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1395 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001396 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001397 if (!rmap_desc_cache)
1398 goto nomem;
1399
Avi Kivityd3d25b02007-05-30 12:34:53 +03001400 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1401 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001402 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001403 if (!mmu_page_header_cache)
1404 goto nomem;
1405
Avi Kivityb5a33a72007-04-15 16:31:09 +03001406 return 0;
1407
1408nomem:
1409 kvm_mmu_module_exit();
1410 return -ENOMEM;
1411}
1412
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001413#ifdef AUDIT
1414
1415static const char *audit_msg;
1416
1417static gva_t canonicalize(gva_t gva)
1418{
1419#ifdef CONFIG_X86_64
1420 gva = (long long)(gva << 16) >> 16;
1421#endif
1422 return gva;
1423}
1424
1425static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1426 gva_t va, int level)
1427{
1428 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1429 int i;
1430 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1431
1432 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1433 u64 ent = pt[i];
1434
Avi Kivityc7addb92007-09-16 18:58:32 +02001435 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001436 continue;
1437
1438 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001439 if (level > 1) {
1440 if (ent == shadow_notrap_nonpresent_pte)
1441 printk(KERN_ERR "audit: (%s) nontrapping pte"
1442 " in nonleaf level: levels %d gva %lx"
1443 " level %d pte %llx\n", audit_msg,
1444 vcpu->mmu.root_level, va, level, ent);
1445
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001446 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001447 } else {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001448 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1449 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1450
Avi Kivityc7addb92007-09-16 18:58:32 +02001451 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001452 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001453 printk(KERN_ERR "xx audit error: (%s) levels %d"
1454 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001455 audit_msg, vcpu->mmu.root_level,
Avi Kivityc7addb92007-09-16 18:58:32 +02001456 va, gpa, hpa, ent, is_shadow_present_pte(ent));
1457 else if (ent == shadow_notrap_nonpresent_pte
1458 && !is_error_hpa(hpa))
1459 printk(KERN_ERR "audit: (%s) notrap shadow,"
1460 " valid guest gva %lx\n", audit_msg, va);
1461
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001462 }
1463 }
1464}
1465
1466static void audit_mappings(struct kvm_vcpu *vcpu)
1467{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001468 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001469
1470 if (vcpu->mmu.root_level == 4)
1471 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1472 else
1473 for (i = 0; i < 4; ++i)
1474 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1475 audit_mappings_page(vcpu,
1476 vcpu->mmu.pae_root[i],
1477 i << 30,
1478 2);
1479}
1480
1481static int count_rmaps(struct kvm_vcpu *vcpu)
1482{
1483 int nmaps = 0;
1484 int i, j, k;
1485
1486 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1487 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1488 struct kvm_rmap_desc *d;
1489
1490 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001491 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001492
Izik Eidus290fc382007-09-27 14:11:22 +02001493 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001494 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001495 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001496 ++nmaps;
1497 continue;
1498 }
Izik Eidus290fc382007-09-27 14:11:22 +02001499 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001500 while (d) {
1501 for (k = 0; k < RMAP_EXT; ++k)
1502 if (d->shadow_ptes[k])
1503 ++nmaps;
1504 else
1505 break;
1506 d = d->more;
1507 }
1508 }
1509 }
1510 return nmaps;
1511}
1512
1513static int count_writable_mappings(struct kvm_vcpu *vcpu)
1514{
1515 int nmaps = 0;
1516 struct kvm_mmu_page *page;
1517 int i;
1518
1519 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity47ad8e62007-05-06 15:50:58 +03001520 u64 *pt = page->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001521
1522 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1523 continue;
1524
1525 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1526 u64 ent = pt[i];
1527
1528 if (!(ent & PT_PRESENT_MASK))
1529 continue;
1530 if (!(ent & PT_WRITABLE_MASK))
1531 continue;
1532 ++nmaps;
1533 }
1534 }
1535 return nmaps;
1536}
1537
1538static void audit_rmap(struct kvm_vcpu *vcpu)
1539{
1540 int n_rmap = count_rmaps(vcpu);
1541 int n_actual = count_writable_mappings(vcpu);
1542
1543 if (n_rmap != n_actual)
1544 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1545 __FUNCTION__, audit_msg, n_rmap, n_actual);
1546}
1547
1548static void audit_write_protection(struct kvm_vcpu *vcpu)
1549{
1550 struct kvm_mmu_page *page;
Izik Eidus290fc382007-09-27 14:11:22 +02001551 struct kvm_memory_slot *slot;
1552 unsigned long *rmapp;
1553 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001554
1555 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001556 if (page->role.metaphysical)
1557 continue;
1558
Izik Eidus290fc382007-09-27 14:11:22 +02001559 slot = gfn_to_memslot(vcpu->kvm, page->gfn);
1560 gfn = unalias_gfn(vcpu->kvm, page->gfn);
1561 rmapp = &slot->rmap[gfn - slot->base_gfn];
1562 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001563 printk(KERN_ERR "%s: (%s) shadow page has writable"
1564 " mappings: gfn %lx role %x\n",
1565 __FUNCTION__, audit_msg, page->gfn,
1566 page->role.word);
1567 }
1568}
1569
1570static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1571{
1572 int olddbg = dbg;
1573
1574 dbg = 0;
1575 audit_msg = msg;
1576 audit_rmap(vcpu);
1577 audit_write_protection(vcpu);
1578 audit_mappings(vcpu);
1579 dbg = olddbg;
1580}
1581
1582#endif