blob: d087d9c4f2d9f5fcc14c9a039bc3588ed7f385d6 [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"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
33#include <asm/page.h>
34#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020035#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030069static int dbg = 0;
70module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080071#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080073#ifndef MMU_DEBUG
74#define ASSERT(x) do { } while (0)
75#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080076#define ASSERT(x) \
77 if (!(x)) { \
78 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
79 __FILE__, __LINE__, #x); \
80 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080081#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define PT_FIRST_AVAIL_BITS_SHIFT 9
84#define PT64_SECOND_AVAIL_BITS_SHIFT 52
85
Avi Kivity6aa8b732006-12-10 02:21:36 -080086#define VALID_PAGE(x) ((x) != INVALID_PAGE)
87
88#define PT64_LEVEL_BITS 9
89
90#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040091 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
93#define PT64_LEVEL_MASK(level) \
94 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
95
96#define PT64_INDEX(address, level)\
97 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
98
99
100#define PT32_LEVEL_BITS 10
101
102#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400103 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104
105#define PT32_LEVEL_MASK(level) \
106 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
107
108#define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
110
111
Avi Kivity27aba762007-03-09 13:04:31 +0200112#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113#define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
115
116#define PT32_BASE_ADDR_MASK PAGE_MASK
117#define PT32_DIR_BASE_ADDR_MASK \
118 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
119
Avi Kivity79539ce2007-11-21 02:06:21 +0200120#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
121 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
123#define PFERR_PRESENT_MASK (1U << 0)
124#define PFERR_WRITE_MASK (1U << 1)
125#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800126#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128#define PT_DIRECTORY_LEVEL 2
129#define PT_PAGE_TABLE_LEVEL 1
130
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800131#define RMAP_EXT 4
132
Avi Kivityfe135d22007-12-09 16:15:46 +0200133#define ACC_EXEC_MASK 1
134#define ACC_WRITE_MASK PT_WRITABLE_MASK
135#define ACC_USER_MASK PT_USER_MASK
136#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
137
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -0500138struct kvm_pv_mmu_op_buffer {
139 void *ptr;
140 unsigned len;
141 unsigned processed;
142 char buf[512] __aligned(sizeof(long));
143};
144
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800145struct kvm_rmap_desc {
146 u64 *shadow_ptes[RMAP_EXT];
147 struct kvm_rmap_desc *more;
148};
149
Avi Kivityb5a33a72007-04-15 16:31:09 +0300150static struct kmem_cache *pte_chain_cache;
151static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300152static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300153
Avi Kivityc7addb92007-09-16 18:58:32 +0200154static u64 __read_mostly shadow_trap_nonpresent_pte;
155static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800156static u64 __read_mostly shadow_base_present_pte;
157static u64 __read_mostly shadow_nx_mask;
158static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
159static u64 __read_mostly shadow_user_mask;
160static u64 __read_mostly shadow_accessed_mask;
161static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200162
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
Sheng Yang7b523452008-04-25 21:13:50 +0800170void kvm_mmu_set_base_ptes(u64 base_pte)
171{
172 shadow_base_present_pte = base_pte;
173}
174EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
175
176void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
177 u64 dirty_mask, u64 nx_mask, u64 x_mask)
178{
179 shadow_user_mask = user_mask;
180 shadow_accessed_mask = accessed_mask;
181 shadow_dirty_mask = dirty_mask;
182 shadow_nx_mask = nx_mask;
183 shadow_x_mask = x_mask;
184}
185EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
186
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187static int is_write_protection(struct kvm_vcpu *vcpu)
188{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800189 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190}
191
192static int is_cpuid_PSE36(void)
193{
194 return 1;
195}
196
Avi Kivity73b10872007-01-26 00:56:41 -0800197static int is_nx(struct kvm_vcpu *vcpu)
198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800199 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800200}
201
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202static int is_present_pte(unsigned long pte)
203{
204 return pte & PT_PRESENT_MASK;
205}
206
Avi Kivityc7addb92007-09-16 18:58:32 +0200207static int is_shadow_present_pte(u64 pte)
208{
Avi Kivityc7addb92007-09-16 18:58:32 +0200209 return pte != shadow_trap_nonpresent_pte
210 && pte != shadow_notrap_nonpresent_pte;
211}
212
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300213static int is_large_pte(u64 pte)
214{
215 return pte & PT_PAGE_SIZE_MASK;
216}
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218static int is_writeble_pte(unsigned long pte)
219{
220 return pte & PT_WRITABLE_MASK;
221}
222
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200223static int is_dirty_pte(unsigned long pte)
224{
Sheng Yang7b523452008-04-25 21:13:50 +0800225 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200226}
227
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800228static int is_rmap_pte(u64 pte)
229{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200230 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800231}
232
Anthony Liguori35149e22008-04-02 14:46:56 -0500233static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200234{
Anthony Liguori35149e22008-04-02 14:46:56 -0500235 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200236}
237
Avi Kivityda928522007-11-21 13:54:47 +0200238static gfn_t pse36_gfn_delta(u32 gpte)
239{
240 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
241
242 return (gpte & PT32_DIR_PSE36_MASK) << shift;
243}
244
Avi Kivitye663ee62007-05-31 15:46:04 +0300245static void set_shadow_pte(u64 *sptep, u64 spte)
246{
247#ifdef CONFIG_X86_64
248 set_64bit((unsigned long *)sptep, spte);
249#else
250 set_64bit((unsigned long long *)sptep, spte);
251#endif
252}
253
Avi Kivitye2dec932007-01-05 16:36:54 -0800254static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300255 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800256{
257 void *obj;
258
259 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800260 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800261 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300262 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800263 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800264 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800265 cache->objects[cache->nobjs++] = obj;
266 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800267 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800268}
269
270static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
271{
272 while (mc->nobjs)
273 kfree(mc->objects[--mc->nobjs]);
274}
275
Avi Kivityc1158e62007-07-20 08:18:27 +0300276static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300277 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300278{
279 struct page *page;
280
281 if (cache->nobjs >= min)
282 return 0;
283 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300284 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300285 if (!page)
286 return -ENOMEM;
287 set_page_private(page, 0);
288 cache->objects[cache->nobjs++] = page_address(page);
289 }
290 return 0;
291}
292
293static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
294{
295 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300296 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300297}
298
Avi Kivity8c438502007-04-16 11:53:17 +0300299static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
300{
301 int r;
302
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800303 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300304 pte_chain_cache, 4);
305 if (r)
306 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800307 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300308 rmap_desc_cache, 1);
309 if (r)
310 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800311 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300312 if (r)
313 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800314 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300315 mmu_page_header_cache, 4);
316out:
Avi Kivity8c438502007-04-16 11:53:17 +0300317 return r;
318}
319
Avi Kivity714b93d2007-01-05 16:36:53 -0800320static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
321{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800322 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
323 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
324 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
325 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800326}
327
328static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
329 size_t size)
330{
331 void *p;
332
333 BUG_ON(!mc->nobjs);
334 p = mc->objects[--mc->nobjs];
335 memset(p, 0, size);
336 return p;
337}
338
Avi Kivity714b93d2007-01-05 16:36:53 -0800339static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
340{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800341 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800342 sizeof(struct kvm_pte_chain));
343}
344
Avi Kivity90cb0522007-07-17 13:04:56 +0300345static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800346{
Avi Kivity90cb0522007-07-17 13:04:56 +0300347 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800348}
349
350static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
351{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800353 sizeof(struct kvm_rmap_desc));
354}
355
Avi Kivity90cb0522007-07-17 13:04:56 +0300356static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800357{
Avi Kivity90cb0522007-07-17 13:04:56 +0300358 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800359}
360
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800361/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300362 * Return the pointer to the largepage write count for a given
363 * gfn, handling slots that are not large page aligned.
364 */
365static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
366{
367 unsigned long idx;
368
369 idx = (gfn / KVM_PAGES_PER_HPAGE) -
370 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
371 return &slot->lpage_info[idx].write_count;
372}
373
374static void account_shadowed(struct kvm *kvm, gfn_t gfn)
375{
376 int *write_count;
377
378 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
379 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300380}
381
382static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
383{
384 int *write_count;
385
386 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
387 *write_count -= 1;
388 WARN_ON(*write_count < 0);
389}
390
391static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
392{
393 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
394 int *largepage_idx;
395
396 if (slot) {
397 largepage_idx = slot_largepage_idx(gfn, slot);
398 return *largepage_idx;
399 }
400
401 return 1;
402}
403
404static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
405{
406 struct vm_area_struct *vma;
407 unsigned long addr;
408
409 addr = gfn_to_hva(kvm, gfn);
410 if (kvm_is_error_hva(addr))
411 return 0;
412
413 vma = find_vma(current->mm, addr);
414 if (vma && is_vm_hugetlb_page(vma))
415 return 1;
416
417 return 0;
418}
419
420static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
421{
422 struct kvm_memory_slot *slot;
423
424 if (has_wrprotected_page(vcpu->kvm, large_gfn))
425 return 0;
426
427 if (!host_largepage_backed(vcpu->kvm, large_gfn))
428 return 0;
429
430 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
431 if (slot && slot->dirty_bitmap)
432 return 0;
433
434 return 1;
435}
436
437/*
Izik Eidus290fc382007-09-27 14:11:22 +0200438 * Take gfn and return the reverse mapping to it.
439 * Note: gfn must be unaliased before this function get called
440 */
441
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300442static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200443{
444 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200446
447 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448 if (!lpage)
449 return &slot->rmap[gfn - slot->base_gfn];
450
451 idx = (gfn / KVM_PAGES_PER_HPAGE) -
452 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
453
454 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200455}
456
457/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800458 * Reverse mapping data structures:
459 *
Izik Eidus290fc382007-09-27 14:11:22 +0200460 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
461 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800462 *
Izik Eidus290fc382007-09-27 14:11:22 +0200463 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
464 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800465 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800467{
Avi Kivity4db35312007-11-21 15:28:32 +0200468 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800469 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200470 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800471 int i;
472
473 if (!is_rmap_pte(*spte))
474 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200475 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200476 sp = page_header(__pa(spte));
477 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300478 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200479 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800480 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200481 *rmapp = (unsigned long)spte;
482 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800483 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800484 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200485 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200487 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800488 } else {
489 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200490 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
492 desc = desc->more;
493 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800494 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800495 desc = desc->more;
496 }
497 for (i = 0; desc->shadow_ptes[i]; ++i)
498 ;
499 desc->shadow_ptes[i] = spte;
500 }
501}
502
Izik Eidus290fc382007-09-27 14:11:22 +0200503static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800504 struct kvm_rmap_desc *desc,
505 int i,
506 struct kvm_rmap_desc *prev_desc)
507{
508 int j;
509
510 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
511 ;
512 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000513 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800514 if (j != 0)
515 return;
516 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200517 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800518 else
519 if (prev_desc)
520 prev_desc->more = desc->more;
521 else
Izik Eidus290fc382007-09-27 14:11:22 +0200522 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300523 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524}
525
Izik Eidus290fc382007-09-27 14:11:22 +0200526static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800528 struct kvm_rmap_desc *desc;
529 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200530 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500531 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200532 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 int i;
534
535 if (!is_rmap_pte(*spte))
536 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200537 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500538 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800539 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500540 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200541 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500542 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200543 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500544 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300545 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200546 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
548 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200549 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200551 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
553 spte, *spte);
554 BUG();
555 }
Izik Eidus290fc382007-09-27 14:11:22 +0200556 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 } else {
558 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200559 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800560 prev_desc = NULL;
561 while (desc) {
562 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
563 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200564 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800565 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566 prev_desc);
567 return;
568 }
569 prev_desc = desc;
570 desc = desc->more;
571 }
572 BUG();
573 }
574}
575
Izik Eidus98348e92007-10-16 14:42:30 +0200576static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800577{
Avi Kivity374cbac2007-01-05 16:36:43 -0800578 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200579 struct kvm_rmap_desc *prev_desc;
580 u64 *prev_spte;
581 int i;
582
583 if (!*rmapp)
584 return NULL;
585 else if (!(*rmapp & 1)) {
586 if (!spte)
587 return (u64 *)*rmapp;
588 return NULL;
589 }
590 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
591 prev_desc = NULL;
592 prev_spte = NULL;
593 while (desc) {
594 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
595 if (prev_spte == spte)
596 return desc->shadow_ptes[i];
597 prev_spte = desc->shadow_ptes[i];
598 }
599 desc = desc->more;
600 }
601 return NULL;
602}
603
604static void rmap_write_protect(struct kvm *kvm, u64 gfn)
605{
Izik Eidus290fc382007-09-27 14:11:22 +0200606 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800607 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800608 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800609
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500610 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300611 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800612
Izik Eidus98348e92007-10-16 14:42:30 +0200613 spte = rmap_next(kvm, rmapp, NULL);
614 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800615 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800616 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800617 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800618 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200619 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800620 write_protected = 1;
621 }
Izik Eidus9647c142007-10-16 14:43:46 +0200622 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800623 }
Izik Eidus855149a2008-03-20 18:17:24 +0200624 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500625 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200626
627 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500628 pfn = spte_to_pfn(*spte);
629 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200630 }
631
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300632 /* check for huge page mappings */
633 rmapp = gfn_to_rmap(kvm, gfn, 1);
634 spte = rmap_next(kvm, rmapp, NULL);
635 while (spte) {
636 BUG_ON(!spte);
637 BUG_ON(!(*spte & PT_PRESENT_MASK));
638 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
639 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
640 if (is_writeble_pte(*spte)) {
641 rmap_remove(kvm, spte);
642 --kvm->stat.lpages;
643 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300644 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300645 write_protected = 1;
646 }
647 spte = rmap_next(kvm, rmapp, spte);
648 }
649
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800650 if (write_protected)
651 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300652
653 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800654}
655
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800656#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300657static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658{
Avi Kivity139bdb22007-01-05 16:36:50 -0800659 u64 *pos;
660 u64 *end;
661
Avi Kivity47ad8e62007-05-06 15:50:58 +0300662 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300663 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800664 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800665 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800667 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 return 1;
669}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800670#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671
Avi Kivity4db35312007-11-21 15:28:32 +0200672static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800673{
Avi Kivity4db35312007-11-21 15:28:32 +0200674 ASSERT(is_empty_shadow_page(sp->spt));
675 list_del(&sp->link);
676 __free_page(virt_to_page(sp->spt));
677 __free_page(virt_to_page(sp->gfns));
678 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800679 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800680}
681
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800682static unsigned kvm_page_table_hashfn(gfn_t gfn)
683{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200684 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800685}
686
Avi Kivity25c0de22007-01-05 16:36:42 -0800687static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
688 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689{
Avi Kivity4db35312007-11-21 15:28:32 +0200690 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800692 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
693 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
694 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200695 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800696 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200697 ASSERT(is_empty_shadow_page(sp->spt));
698 sp->slot_bitmap = 0;
699 sp->multimapped = 0;
700 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800701 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200702 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703}
704
Avi Kivity714b93d2007-01-05 16:36:53 -0800705static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200706 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800707{
708 struct kvm_pte_chain *pte_chain;
709 struct hlist_node *node;
710 int i;
711
712 if (!parent_pte)
713 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200714 if (!sp->multimapped) {
715 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716
717 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200718 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800719 return;
720 }
Avi Kivity4db35312007-11-21 15:28:32 +0200721 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800722 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200723 INIT_HLIST_HEAD(&sp->parent_ptes);
724 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800725 pte_chain->parent_ptes[0] = old;
726 }
Avi Kivity4db35312007-11-21 15:28:32 +0200727 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800728 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
729 continue;
730 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
731 if (!pte_chain->parent_ptes[i]) {
732 pte_chain->parent_ptes[i] = parent_pte;
733 return;
734 }
735 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800736 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800737 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200738 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800739 pte_chain->parent_ptes[0] = parent_pte;
740}
741
Avi Kivity4db35312007-11-21 15:28:32 +0200742static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800743 u64 *parent_pte)
744{
745 struct kvm_pte_chain *pte_chain;
746 struct hlist_node *node;
747 int i;
748
Avi Kivity4db35312007-11-21 15:28:32 +0200749 if (!sp->multimapped) {
750 BUG_ON(sp->parent_pte != parent_pte);
751 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800752 return;
753 }
Avi Kivity4db35312007-11-21 15:28:32 +0200754 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800755 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
756 if (!pte_chain->parent_ptes[i])
757 break;
758 if (pte_chain->parent_ptes[i] != parent_pte)
759 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800760 while (i + 1 < NR_PTE_CHAIN_ENTRIES
761 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800762 pte_chain->parent_ptes[i]
763 = pte_chain->parent_ptes[i + 1];
764 ++i;
765 }
766 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800767 if (i == 0) {
768 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300769 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200770 if (hlist_empty(&sp->parent_ptes)) {
771 sp->multimapped = 0;
772 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800773 }
774 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800775 return;
776 }
777 BUG();
778}
779
Avi Kivityd761a502008-05-29 14:55:03 +0300780static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
781 struct kvm_mmu_page *sp)
782{
783 int i;
784
785 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
786 sp->spt[i] = shadow_trap_nonpresent_pte;
787}
788
Avi Kivity4db35312007-11-21 15:28:32 +0200789static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800790{
791 unsigned index;
792 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200793 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800794 struct hlist_node *node;
795
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800796 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200797 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800798 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200799 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500800 if (sp->gfn == gfn && !sp->role.metaphysical
801 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800802 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800803 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200804 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800805 }
806 return NULL;
807}
808
809static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
810 gfn_t gfn,
811 gva_t gaddr,
812 unsigned level,
813 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200814 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200815 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800816{
817 union kvm_mmu_page_role role;
818 unsigned index;
819 unsigned quadrant;
820 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200821 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 struct hlist_node *node;
823
824 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800825 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800826 role.level = level;
827 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200828 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800829 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800830 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
831 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
832 role.quadrant = quadrant;
833 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800834 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200836 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800837 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200838 hlist_for_each_entry(sp, node, bucket, hash_link)
839 if (sp->gfn == gfn && sp->role.word == role.word) {
840 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800841 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200842 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200844 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200845 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
846 if (!sp)
847 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800848 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200849 sp->gfn = gfn;
850 sp->role = role;
851 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800852 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500853 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity131d8272008-05-29 14:56:28 +0300854 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
855 vcpu->arch.mmu.prefetch_page(vcpu, sp);
856 else
857 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200858 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800859}
860
Avi Kivity90cb0522007-07-17 13:04:56 +0300861static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200862 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800863{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800864 unsigned i;
865 u64 *pt;
866 u64 ent;
867
Avi Kivity4db35312007-11-21 15:28:32 +0200868 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800869
Avi Kivity4db35312007-11-21 15:28:32 +0200870 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800871 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200872 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200873 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200874 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800875 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300876 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800877 return;
878 }
879
880 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
881 ent = pt[i];
882
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300883 if (is_shadow_present_pte(ent)) {
884 if (!is_large_pte(ent)) {
885 ent &= PT64_BASE_ADDR_MASK;
886 mmu_page_remove_parent_pte(page_header(ent),
887 &pt[i]);
888 } else {
889 --kvm->stat.lpages;
890 rmap_remove(kvm, &pt[i]);
891 }
892 }
Avi Kivityc7addb92007-09-16 18:58:32 +0200893 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800894 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300895 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800896}
897
Avi Kivity4db35312007-11-21 15:28:32 +0200898static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800899{
Avi Kivity4db35312007-11-21 15:28:32 +0200900 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800901}
902
Avi Kivity12b7d282007-09-23 14:10:49 +0200903static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
904{
905 int i;
906
907 for (i = 0; i < KVM_MAX_VCPUS; ++i)
908 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800909 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200910}
911
Avi Kivity4db35312007-11-21 15:28:32 +0200912static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800913{
914 u64 *parent_pte;
915
Avi Kivity4cee5762007-11-18 16:37:07 +0200916 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200917 while (sp->multimapped || sp->parent_pte) {
918 if (!sp->multimapped)
919 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800920 else {
921 struct kvm_pte_chain *chain;
922
Avi Kivity4db35312007-11-21 15:28:32 +0200923 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800924 struct kvm_pte_chain, link);
925 parent_pte = chain->parent_ptes[0];
926 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800927 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200928 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200929 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800930 }
Avi Kivity4db35312007-11-21 15:28:32 +0200931 kvm_mmu_page_unlink_children(kvm, sp);
932 if (!sp->root_count) {
Marcelo Tosatti376c53c2008-07-10 20:54:29 -0300933 if (!sp->role.metaphysical && !sp->role.invalid)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300934 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200935 hlist_del(&sp->hash_link);
936 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500937 } else {
Marcelo Tosatti376c53c2008-07-10 20:54:29 -0300938 int invalid = sp->role.invalid;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800939 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500940 sp->role.invalid = 1;
941 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti376c53c2008-07-10 20:54:29 -0300942 if (!sp->role.metaphysical && !invalid)
943 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500944 }
Avi Kivity12b7d282007-09-23 14:10:49 +0200945 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800946}
947
Izik Eidus82ce2c92007-10-02 18:52:55 +0200948/*
949 * Changing the number of mmu pages allocated to the vm
950 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
951 */
952void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
953{
954 /*
955 * If we set the number of mmu pages to be smaller be than the
956 * number of actived pages , we must to free some mmu pages before we
957 * change the value
958 */
959
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800960 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200961 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800962 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
963 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200964
965 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
966 struct kvm_mmu_page *page;
967
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800968 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200969 struct kvm_mmu_page, link);
970 kvm_mmu_zap_page(kvm, page);
971 n_used_mmu_pages--;
972 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800973 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200974 }
975 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800976 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
977 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200978
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800979 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200980}
981
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500982static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800983{
984 unsigned index;
985 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200986 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800987 struct hlist_node *node, *n;
988 int r;
989
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800990 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -0800991 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200992 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800993 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200994 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
995 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800996 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200997 sp->role.word);
998 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800999 r = 1;
1000 }
1001 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001002}
1003
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001004static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001005{
Avi Kivity4db35312007-11-21 15:28:32 +02001006 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001007
Avi Kivity4db35312007-11-21 15:28:32 +02001008 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001009 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001010 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001011 }
1012}
1013
Avi Kivity38c335f2007-11-21 14:20:22 +02001014static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015{
Avi Kivity38c335f2007-11-21 14:20:22 +02001016 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001017 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018
Avi Kivity4db35312007-11-21 15:28:32 +02001019 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020}
1021
Avi Kivity039576c2007-03-20 12:46:50 +02001022struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1023{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001024 struct page *page;
1025
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001026 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001027
1028 if (gpa == UNMAPPED_GVA)
1029 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001030
1031 down_read(&current->mm->mmap_sem);
1032 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1033 up_read(&current->mm->mmap_sem);
1034
1035 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001036}
1037
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001038static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1039 unsigned pt_access, unsigned pte_access,
1040 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001041 int *ptwrite, int largepage, gfn_t gfn,
Anthony Liguori35149e22008-04-02 14:46:56 -05001042 pfn_t pfn, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001043{
1044 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001045 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001046 int was_writeble = is_writeble_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001047
Avi Kivitybc750ba2007-12-09 18:39:41 +02001048 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001049 " user_fault %d gfn %lx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001050 __func__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001051 write_fault, user_fault, gfn);
1052
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001053 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001054 /*
1055 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1056 * the parent of the now unreachable PTE.
1057 */
1058 if (largepage && !is_large_pte(*shadow_pte)) {
1059 struct kvm_mmu_page *child;
1060 u64 pte = *shadow_pte;
1061
1062 child = page_header(pte & PT64_BASE_ADDR_MASK);
1063 mmu_page_remove_parent_pte(child, shadow_pte);
Anthony Liguori35149e22008-04-02 14:46:56 -05001064 } else if (pfn != spte_to_pfn(*shadow_pte)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001065 pgprintk("hfn old %lx new %lx\n",
Anthony Liguori35149e22008-04-02 14:46:56 -05001066 spte_to_pfn(*shadow_pte), pfn);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001067 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001068 } else {
1069 if (largepage)
1070 was_rmapped = is_large_pte(*shadow_pte);
1071 else
1072 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001073 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001074 }
1075
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001076 /*
1077 * We don't set the accessed bit, since we sometimes want to see
1078 * whether the guest actually used the pte (in order to detect
1079 * demand paging).
1080 */
Sheng Yang7b523452008-04-25 21:13:50 +08001081 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001082 if (!speculative)
1083 pte_access |= PT_ACCESSED_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001084 if (!dirty)
1085 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001086 if (pte_access & ACC_EXEC_MASK)
1087 spte |= shadow_x_mask;
1088 else
1089 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001090 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001091 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001092 if (largepage)
1093 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001094
Anthony Liguori35149e22008-04-02 14:46:56 -05001095 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001096
1097 if ((pte_access & ACC_WRITE_MASK)
1098 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1099 struct kvm_mmu_page *shadow;
1100
1101 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001102
1103 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001104 if (shadow ||
1105 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001106 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001107 __func__, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001108 pte_access &= ~ACC_WRITE_MASK;
1109 if (is_writeble_pte(spte)) {
1110 spte &= ~PT_WRITABLE_MASK;
1111 kvm_x86_ops->tlb_flush(vcpu);
1112 }
1113 if (write_fault)
1114 *ptwrite = 1;
1115 }
1116 }
1117
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001118 if (pte_access & ACC_WRITE_MASK)
1119 mark_page_dirty(vcpu->kvm, gfn);
1120
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001121 pgprintk("%s: setting spte %llx\n", __func__, spte);
Avi Kivitydb475c32008-06-22 16:46:22 +03001122 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001123 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1124 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001125 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001126 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1127 && (spte & PT_PRESENT_MASK))
1128 ++vcpu->kvm->stat.lpages;
1129
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001130 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1131 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001132 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001133 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001134 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001135 } else {
1136 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001137 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001138 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001139 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001140 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001141 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001142 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001143 vcpu->arch.last_pte_gfn = gfn;
1144 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001145}
1146
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1148{
1149}
1150
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001151static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Anthony Liguori35149e22008-04-02 14:46:56 -05001152 int largepage, gfn_t gfn, pfn_t pfn,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001153 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001155 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +02001156 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157
1158 for (; ; level--) {
1159 u32 index = PT64_INDEX(v, level);
1160 u64 *table;
1161
1162 ASSERT(VALID_PAGE(table_addr));
1163 table = __va(table_addr);
1164
1165 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001166 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Anthony Liguori35149e22008-04-02 14:46:56 -05001167 0, write, 1, &pt_write, 0, gfn, pfn, false);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001168 return pt_write;
1169 }
1170
1171 if (largepage && level == 2) {
1172 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Anthony Liguori35149e22008-04-02 14:46:56 -05001173 0, write, 1, &pt_write, 1, gfn, pfn, false);
Avi Kivityd196e342008-01-24 11:44:11 +02001174 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 }
1176
Avi Kivityc7addb92007-09-16 18:58:32 +02001177 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001178 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001179 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001181 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1182 >> PAGE_SHIFT;
1183 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1184 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001185 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001186 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 pgprintk("nonpaging_map: ENOMEM\n");
Anthony Liguori35149e22008-04-02 14:46:56 -05001188 kvm_release_pfn_clean(pfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 return -ENOMEM;
1190 }
1191
Avi Kivity722c05f2008-07-13 11:33:54 +03001192 set_shadow_pte(&table[index],
1193 __pa(new_table->spt)
1194 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1195 | shadow_user_mask | shadow_x_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 }
1197 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1198 }
1199}
1200
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001201static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1202{
1203 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001204 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001205 pfn_t pfn;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001206
1207 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001208 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1209 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1210 largepage = 1;
1211 }
1212
Anthony Liguori35149e22008-04-02 14:46:56 -05001213 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001214 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001215
Avi Kivityd196e342008-01-24 11:44:11 +02001216 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001217 if (is_error_pfn(pfn)) {
1218 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001219 return 1;
1220 }
1221
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001222 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001223 kvm_mmu_free_some_pages(vcpu);
Anthony Liguori35149e22008-04-02 14:46:56 -05001224 r = __direct_map(vcpu, v, write, largepage, gfn, pfn,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001225 PT32E_ROOT_LEVEL);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001226 spin_unlock(&vcpu->kvm->mmu_lock);
1227
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001228
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001229 return r;
1230}
1231
1232
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233static void mmu_free_roots(struct kvm_vcpu *vcpu)
1234{
1235 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001236 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001237
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001238 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001239 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001240 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001241 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1242 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001243
Avi Kivity4db35312007-11-21 15:28:32 +02001244 sp = page_header(root);
1245 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001246 if (!sp->root_count && sp->role.invalid)
1247 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001248 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001249 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001250 return;
1251 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001252 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001253 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001254
Avi Kivity417726a2007-04-12 17:35:58 +03001255 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001256 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001257 sp = page_header(root);
1258 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001259 if (!sp->root_count && sp->role.invalid)
1260 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001261 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001262 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001263 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001264 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001265 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001266}
1267
1268static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1269{
1270 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001271 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001272 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001273 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001275 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001276
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001277 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1278 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001279
1280 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001281 if (tdp_enabled)
1282 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001283 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001284 PT64_ROOT_LEVEL, metaphysical,
1285 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001286 root = __pa(sp->spt);
1287 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001288 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001289 return;
1290 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001291 metaphysical = !is_paging(vcpu);
1292 if (tdp_enabled)
1293 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001294 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001295 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001296
1297 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001298 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1299 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1300 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001301 continue;
1302 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001303 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1304 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001305 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001306 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001307 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001308 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001309 root = __pa(sp->spt);
1310 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001311 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001312 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001313 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001314}
1315
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1317{
1318 return vaddr;
1319}
1320
1321static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001322 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323{
Avi Kivitye8332402007-12-09 18:43:00 +02001324 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001325 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001327 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001328 r = mmu_topup_memory_caches(vcpu);
1329 if (r)
1330 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001331
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001333 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334
Avi Kivitye8332402007-12-09 18:43:00 +02001335 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336
Avi Kivitye8332402007-12-09 18:43:00 +02001337 return nonpaging_map(vcpu, gva & PAGE_MASK,
1338 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339}
1340
Joerg Roedelfb72d162008-02-07 13:47:44 +01001341static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1342 u32 error_code)
1343{
Anthony Liguori35149e22008-04-02 14:46:56 -05001344 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001345 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001346 int largepage = 0;
1347 gfn_t gfn = gpa >> PAGE_SHIFT;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001348
1349 ASSERT(vcpu);
1350 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1351
1352 r = mmu_topup_memory_caches(vcpu);
1353 if (r)
1354 return r;
1355
1356 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001357 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1358 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1359 largepage = 1;
1360 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001361 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001362 up_read(&current->mm->mmap_sem);
Anthony Liguori35149e22008-04-02 14:46:56 -05001363 if (is_error_pfn(pfn)) {
1364 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001365 return 1;
1366 }
1367 spin_lock(&vcpu->kvm->mmu_lock);
1368 kvm_mmu_free_some_pages(vcpu);
1369 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Sheng Yang67253af2008-04-25 10:20:22 +08001370 largepage, gfn, pfn, kvm_x86_ops->get_tdp_level());
Joerg Roedelfb72d162008-02-07 13:47:44 +01001371 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001372
1373 return r;
1374}
1375
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376static void nonpaging_free(struct kvm_vcpu *vcpu)
1377{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001378 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379}
1380
1381static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1382{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001383 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384
1385 context->new_cr3 = nonpaging_new_cr3;
1386 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 context->gva_to_gpa = nonpaging_gva_to_gpa;
1388 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001389 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001390 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001392 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 return 0;
1394}
1395
Avi Kivityd835dfe2007-11-21 02:57:59 +02001396void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001398 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001399 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400}
1401
1402static void paging_new_cr3(struct kvm_vcpu *vcpu)
1403{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001404 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001405 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406}
1407
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408static void inject_page_fault(struct kvm_vcpu *vcpu,
1409 u64 addr,
1410 u32 err_code)
1411{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001412 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413}
1414
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415static void paging_free(struct kvm_vcpu *vcpu)
1416{
1417 nonpaging_free(vcpu);
1418}
1419
1420#define PTTYPE 64
1421#include "paging_tmpl.h"
1422#undef PTTYPE
1423
1424#define PTTYPE 32
1425#include "paging_tmpl.h"
1426#undef PTTYPE
1427
Avi Kivity17ac10a2007-01-05 16:36:40 -08001428static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001430 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431
1432 ASSERT(is_pae(vcpu));
1433 context->new_cr3 = paging_new_cr3;
1434 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001436 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001438 context->root_level = level;
1439 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001440 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 return 0;
1442}
1443
Avi Kivity17ac10a2007-01-05 16:36:40 -08001444static int paging64_init_context(struct kvm_vcpu *vcpu)
1445{
1446 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1447}
1448
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449static int paging32_init_context(struct kvm_vcpu *vcpu)
1450{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001451 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
1453 context->new_cr3 = paging_new_cr3;
1454 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 context->gva_to_gpa = paging32_gva_to_gpa;
1456 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001457 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 context->root_level = PT32_ROOT_LEVEL;
1459 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001460 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461 return 0;
1462}
1463
1464static int paging32E_init_context(struct kvm_vcpu *vcpu)
1465{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001466 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467}
1468
Joerg Roedelfb72d162008-02-07 13:47:44 +01001469static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1470{
1471 struct kvm_mmu *context = &vcpu->arch.mmu;
1472
1473 context->new_cr3 = nonpaging_new_cr3;
1474 context->page_fault = tdp_page_fault;
1475 context->free = nonpaging_free;
1476 context->prefetch_page = nonpaging_prefetch_page;
Sheng Yang67253af2008-04-25 10:20:22 +08001477 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01001478 context->root_hpa = INVALID_PAGE;
1479
1480 if (!is_paging(vcpu)) {
1481 context->gva_to_gpa = nonpaging_gva_to_gpa;
1482 context->root_level = 0;
1483 } else if (is_long_mode(vcpu)) {
1484 context->gva_to_gpa = paging64_gva_to_gpa;
1485 context->root_level = PT64_ROOT_LEVEL;
1486 } else if (is_pae(vcpu)) {
1487 context->gva_to_gpa = paging64_gva_to_gpa;
1488 context->root_level = PT32E_ROOT_LEVEL;
1489 } else {
1490 context->gva_to_gpa = paging32_gva_to_gpa;
1491 context->root_level = PT32_ROOT_LEVEL;
1492 }
1493
1494 return 0;
1495}
1496
1497static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498{
1499 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001500 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501
1502 if (!is_paging(vcpu))
1503 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001504 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 return paging64_init_context(vcpu);
1506 else if (is_pae(vcpu))
1507 return paging32E_init_context(vcpu);
1508 else
1509 return paging32_init_context(vcpu);
1510}
1511
Joerg Roedelfb72d162008-02-07 13:47:44 +01001512static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1513{
Anthony Liguori35149e22008-04-02 14:46:56 -05001514 vcpu->arch.update_pte.pfn = bad_pfn;
1515
Joerg Roedelfb72d162008-02-07 13:47:44 +01001516 if (tdp_enabled)
1517 return init_kvm_tdp_mmu(vcpu);
1518 else
1519 return init_kvm_softmmu(vcpu);
1520}
1521
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1523{
1524 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001525 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1526 vcpu->arch.mmu.free(vcpu);
1527 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 }
1529}
1530
1531int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1532{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001533 destroy_kvm_mmu(vcpu);
1534 return init_kvm_mmu(vcpu);
1535}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001536EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001537
1538int kvm_mmu_load(struct kvm_vcpu *vcpu)
1539{
Avi Kivity714b93d2007-01-05 16:36:53 -08001540 int r;
1541
Avi Kivitye2dec932007-01-05 16:36:54 -08001542 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001543 if (r)
1544 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001545 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001546 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001547 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001548 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001549 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001550 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001551out:
1552 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001554EXPORT_SYMBOL_GPL(kvm_mmu_load);
1555
1556void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1557{
1558 mmu_free_roots(vcpu);
1559}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560
Avi Kivity09072da2007-05-01 14:16:52 +03001561static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001562 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001563 u64 *spte)
1564{
1565 u64 pte;
1566 struct kvm_mmu_page *child;
1567
1568 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001569 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001570 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1571 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001572 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001573 else {
1574 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001575 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001576 }
1577 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001578 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001579 if (is_large_pte(pte))
1580 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001581}
1582
Avi Kivity00284252007-05-01 16:53:31 +03001583static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001584 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001585 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001586 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001587{
Marcelo Tosatti30945382008-06-11 20:32:40 -03001588 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1589 if (!vcpu->arch.update_pte.largepage ||
1590 sp->role.glevels == PT32_ROOT_LEVEL) {
1591 ++vcpu->kvm->stat.mmu_pde_zapped;
1592 return;
1593 }
1594 }
Avi Kivity00284252007-05-01 16:53:31 +03001595
Avi Kivity4cee5762007-11-18 16:37:07 +02001596 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001597 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001598 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001599 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001600 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001601}
1602
Avi Kivity79539ce2007-11-21 02:06:21 +02001603static bool need_remote_flush(u64 old, u64 new)
1604{
1605 if (!is_shadow_present_pte(old))
1606 return false;
1607 if (!is_shadow_present_pte(new))
1608 return true;
1609 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1610 return true;
1611 old ^= PT64_NX_MASK;
1612 new ^= PT64_NX_MASK;
1613 return (old & ~new & PT64_PERM_MASK) != 0;
1614}
1615
1616static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1617{
1618 if (need_remote_flush(old, new))
1619 kvm_flush_remote_tlbs(vcpu->kvm);
1620 else
1621 kvm_mmu_flush_tlb(vcpu);
1622}
1623
Avi Kivity12b7d282007-09-23 14:10:49 +02001624static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1625{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001626 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001627
Sheng Yang7b523452008-04-25 21:13:50 +08001628 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02001629}
1630
Avi Kivityd7824ff2007-12-30 12:29:05 +02001631static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1632 const u8 *new, int bytes)
1633{
1634 gfn_t gfn;
1635 int r;
1636 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001637 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001638
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001639 vcpu->arch.update_pte.largepage = 0;
1640
Avi Kivityd7824ff2007-12-30 12:29:05 +02001641 if (bytes != 4 && bytes != 8)
1642 return;
1643
1644 /*
1645 * Assume that the pte write on a page table of the same type
1646 * as the current vcpu paging mode. This is nearly always true
1647 * (might be false while changing modes). Note it is verified later
1648 * by update_pte().
1649 */
1650 if (is_pae(vcpu)) {
1651 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1652 if ((bytes == 4) && (gpa % 4 == 0)) {
1653 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1654 if (r)
1655 return;
1656 memcpy((void *)&gpte + (gpa % 8), new, 4);
1657 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1658 memcpy((void *)&gpte, new, 8);
1659 }
1660 } else {
1661 if ((bytes == 4) && (gpa % 4 == 0))
1662 memcpy((void *)&gpte, new, 4);
1663 }
1664 if (!is_present_pte(gpte))
1665 return;
1666 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001667
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001668 down_read(&current->mm->mmap_sem);
1669 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1670 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1671 vcpu->arch.update_pte.largepage = 1;
1672 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001673 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001674 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001675
Anthony Liguori35149e22008-04-02 14:46:56 -05001676 if (is_error_pfn(pfn)) {
1677 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001678 return;
1679 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001680 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001681 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001682}
1683
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001684static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1685{
1686 u64 *spte = vcpu->arch.last_pte_updated;
1687
1688 if (spte
1689 && vcpu->arch.last_pte_gfn == gfn
1690 && shadow_accessed_mask
1691 && !(*spte & shadow_accessed_mask)
1692 && is_shadow_present_pte(*spte))
1693 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1694}
1695
Avi Kivity09072da2007-05-01 14:16:52 +03001696void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001697 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001698{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001699 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001700 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001701 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001702 struct hlist_head *bucket;
1703 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001704 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001705 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001706 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001707 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001708 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001709 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001710 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001711 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001712 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001713 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001714 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001715
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001716 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001717 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001718 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001719 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02001720 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001721 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001722 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001723 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001724 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001725 ++vcpu->arch.last_pt_write_count;
1726 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001727 flooded = 1;
1728 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001729 vcpu->arch.last_pt_write_gfn = gfn;
1730 vcpu->arch.last_pt_write_count = 1;
1731 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001732 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001733 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001734 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001735 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1736 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001737 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001738 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001739 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001740 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001741 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001742 /*
1743 * Misaligned accesses are too much trouble to fix
1744 * up; also, they usually indicate a page is not used
1745 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001746 *
1747 * If we're seeing too many writes to a page,
1748 * it may no longer be a page table, or we may be
1749 * forking, in which case it is better to unmap the
1750 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001751 */
1752 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001753 gpa, bytes, sp->role.word);
1754 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001755 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001756 continue;
1757 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001758 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001759 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001760 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001761 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001762 page_offset <<= 1; /* 32->64 */
1763 /*
1764 * A 32-bit pde maps 4MB while the shadow pdes map
1765 * only 2MB. So we need to double the offset again
1766 * and zap two pdes instead of one.
1767 */
1768 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001769 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001770 page_offset <<= 1;
1771 npte = 2;
1772 }
Avi Kivityfce06572007-05-01 16:44:05 +03001773 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001774 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001775 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001776 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001777 }
Avi Kivity4db35312007-11-21 15:28:32 +02001778 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001779 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1780 gentry = 0;
1781 r = kvm_read_guest_atomic(vcpu->kvm,
1782 gpa & ~(u64)(pte_size - 1),
1783 &gentry, pte_size);
1784 new = (const void *)&gentry;
1785 if (r < 0)
1786 new = NULL;
1787 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001788 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001789 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001790 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001791 if (new)
1792 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001793 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001794 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001795 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001796 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001797 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001798 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05001799 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1800 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1801 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001802 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001803}
1804
Avi Kivitya4360362007-01-05 16:36:45 -08001805int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1806{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001807 gpa_t gpa;
1808 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001809
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001810 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001811
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001812 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001813 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001814 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001815 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001816}
1817
Avi Kivity22d95b12007-09-14 20:26:06 +03001818void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001819{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001820 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001821 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001822
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001823 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001824 struct kvm_mmu_page, link);
1825 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001826 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001827 }
1828}
Avi Kivityebeace82007-01-05 16:36:47 -08001829
Avi Kivity30677142007-10-28 18:48:59 +02001830int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1831{
1832 int r;
1833 enum emulation_result er;
1834
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001835 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001836 if (r < 0)
1837 goto out;
1838
1839 if (!r) {
1840 r = 1;
1841 goto out;
1842 }
1843
Avi Kivityb733bfb2007-10-28 18:52:05 +02001844 r = mmu_topup_memory_caches(vcpu);
1845 if (r)
1846 goto out;
1847
Avi Kivity30677142007-10-28 18:48:59 +02001848 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001849
1850 switch (er) {
1851 case EMULATE_DONE:
1852 return 1;
1853 case EMULATE_DO_MMIO:
1854 ++vcpu->stat.mmio_exits;
1855 return 0;
1856 case EMULATE_FAIL:
1857 kvm_report_emulation_failure(vcpu, "pagetable");
1858 return 1;
1859 default:
1860 BUG();
1861 }
1862out:
Avi Kivity30677142007-10-28 18:48:59 +02001863 return r;
1864}
1865EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1866
Joerg Roedel18552672008-02-07 13:47:41 +01001867void kvm_enable_tdp(void)
1868{
1869 tdp_enabled = true;
1870}
1871EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1872
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001873void kvm_disable_tdp(void)
1874{
1875 tdp_enabled = false;
1876}
1877EXPORT_SYMBOL_GPL(kvm_disable_tdp);
1878
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879static void free_mmu_pages(struct kvm_vcpu *vcpu)
1880{
Avi Kivity4db35312007-11-21 15:28:32 +02001881 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001883 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1884 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001885 struct kvm_mmu_page, link);
1886 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03001887 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08001888 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001889 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890}
1891
1892static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1893{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001894 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895 int i;
1896
1897 ASSERT(vcpu);
1898
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001899 if (vcpu->kvm->arch.n_requested_mmu_pages)
1900 vcpu->kvm->arch.n_free_mmu_pages =
1901 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001902 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001903 vcpu->kvm->arch.n_free_mmu_pages =
1904 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001905 /*
1906 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1907 * Therefore we need to allocate shadow page tables in the first
1908 * 4GB of memory, which happens to fit the DMA32 zone.
1909 */
1910 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1911 if (!page)
1912 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001913 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001914 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001915 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001916
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917 return 0;
1918
1919error_1:
1920 free_mmu_pages(vcpu);
1921 return -ENOMEM;
1922}
1923
Ingo Molnar8018c272006-12-29 16:50:01 -08001924int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001927 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928
Ingo Molnar8018c272006-12-29 16:50:01 -08001929 return alloc_mmu_pages(vcpu);
1930}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931
Ingo Molnar8018c272006-12-29 16:50:01 -08001932int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1933{
1934 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001936
Ingo Molnar8018c272006-12-29 16:50:01 -08001937 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938}
1939
1940void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1941{
1942 ASSERT(vcpu);
1943
1944 destroy_kvm_mmu(vcpu);
1945 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001946 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947}
1948
Avi Kivity90cb0522007-07-17 13:04:56 +03001949void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950{
Avi Kivity4db35312007-11-21 15:28:32 +02001951 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001953 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 int i;
1955 u64 *pt;
1956
Avi Kivity4db35312007-11-21 15:28:32 +02001957 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 continue;
1959
Avi Kivity4db35312007-11-21 15:28:32 +02001960 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1962 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001963 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 }
1966}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001967
Avi Kivity90cb0522007-07-17 13:04:56 +03001968void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001969{
Avi Kivity4db35312007-11-21 15:28:32 +02001970 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001971
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001972 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001973 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001974 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001975 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001976
Avi Kivity90cb0522007-07-17 13:04:56 +03001977 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001978}
1979
Harvey Harrison8b2cf732008-04-27 12:14:13 -07001980static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03001981{
1982 struct kvm_mmu_page *page;
1983
1984 page = container_of(kvm->arch.active_mmu_pages.prev,
1985 struct kvm_mmu_page, link);
1986 kvm_mmu_zap_page(kvm, page);
1987}
1988
1989static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
1990{
1991 struct kvm *kvm;
1992 struct kvm *kvm_freed = NULL;
1993 int cache_count = 0;
1994
1995 spin_lock(&kvm_lock);
1996
1997 list_for_each_entry(kvm, &vm_list, vm_list) {
1998 int npages;
1999
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002000 if (!down_read_trylock(&kvm->slots_lock))
2001 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002002 spin_lock(&kvm->mmu_lock);
2003 npages = kvm->arch.n_alloc_mmu_pages -
2004 kvm->arch.n_free_mmu_pages;
2005 cache_count += npages;
2006 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2007 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2008 cache_count--;
2009 kvm_freed = kvm;
2010 }
2011 nr_to_scan--;
2012
2013 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002014 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002015 }
2016 if (kvm_freed)
2017 list_move_tail(&kvm_freed->vm_list, &vm_list);
2018
2019 spin_unlock(&kvm_lock);
2020
2021 return cache_count;
2022}
2023
2024static struct shrinker mmu_shrinker = {
2025 .shrink = mmu_shrink,
2026 .seeks = DEFAULT_SEEKS * 10,
2027};
2028
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002029static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002030{
2031 if (pte_chain_cache)
2032 kmem_cache_destroy(pte_chain_cache);
2033 if (rmap_desc_cache)
2034 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002035 if (mmu_page_header_cache)
2036 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002037}
2038
Izik Eidus3ee16c82008-03-30 15:17:21 +03002039void kvm_mmu_module_exit(void)
2040{
2041 mmu_destroy_caches();
2042 unregister_shrinker(&mmu_shrinker);
2043}
2044
Avi Kivityb5a33a72007-04-15 16:31:09 +03002045int kvm_mmu_module_init(void)
2046{
2047 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2048 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002049 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002050 if (!pte_chain_cache)
2051 goto nomem;
2052 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2053 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002054 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002055 if (!rmap_desc_cache)
2056 goto nomem;
2057
Avi Kivityd3d25b02007-05-30 12:34:53 +03002058 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2059 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002060 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002061 if (!mmu_page_header_cache)
2062 goto nomem;
2063
Izik Eidus3ee16c82008-03-30 15:17:21 +03002064 register_shrinker(&mmu_shrinker);
2065
Avi Kivityb5a33a72007-04-15 16:31:09 +03002066 return 0;
2067
2068nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002069 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002070 return -ENOMEM;
2071}
2072
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002073/*
2074 * Caculate mmu pages needed for kvm.
2075 */
2076unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2077{
2078 int i;
2079 unsigned int nr_mmu_pages;
2080 unsigned int nr_pages = 0;
2081
2082 for (i = 0; i < kvm->nmemslots; i++)
2083 nr_pages += kvm->memslots[i].npages;
2084
2085 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2086 nr_mmu_pages = max(nr_mmu_pages,
2087 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2088
2089 return nr_mmu_pages;
2090}
2091
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002092static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2093 unsigned len)
2094{
2095 if (len > buffer->len)
2096 return NULL;
2097 return buffer->ptr;
2098}
2099
2100static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2101 unsigned len)
2102{
2103 void *ret;
2104
2105 ret = pv_mmu_peek_buffer(buffer, len);
2106 if (!ret)
2107 return ret;
2108 buffer->ptr += len;
2109 buffer->len -= len;
2110 buffer->processed += len;
2111 return ret;
2112}
2113
2114static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2115 gpa_t addr, gpa_t value)
2116{
2117 int bytes = 8;
2118 int r;
2119
2120 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2121 bytes = 4;
2122
2123 r = mmu_topup_memory_caches(vcpu);
2124 if (r)
2125 return r;
2126
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002127 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002128 return -EFAULT;
2129
2130 return 1;
2131}
2132
2133static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2134{
2135 kvm_x86_ops->tlb_flush(vcpu);
2136 return 1;
2137}
2138
2139static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2140{
2141 spin_lock(&vcpu->kvm->mmu_lock);
2142 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2143 spin_unlock(&vcpu->kvm->mmu_lock);
2144 return 1;
2145}
2146
2147static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2148 struct kvm_pv_mmu_op_buffer *buffer)
2149{
2150 struct kvm_mmu_op_header *header;
2151
2152 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2153 if (!header)
2154 return 0;
2155 switch (header->op) {
2156 case KVM_MMU_OP_WRITE_PTE: {
2157 struct kvm_mmu_op_write_pte *wpte;
2158
2159 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2160 if (!wpte)
2161 return 0;
2162 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2163 wpte->pte_val);
2164 }
2165 case KVM_MMU_OP_FLUSH_TLB: {
2166 struct kvm_mmu_op_flush_tlb *ftlb;
2167
2168 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2169 if (!ftlb)
2170 return 0;
2171 return kvm_pv_mmu_flush_tlb(vcpu);
2172 }
2173 case KVM_MMU_OP_RELEASE_PT: {
2174 struct kvm_mmu_op_release_pt *rpt;
2175
2176 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2177 if (!rpt)
2178 return 0;
2179 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2180 }
2181 default: return 0;
2182 }
2183}
2184
2185int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2186 gpa_t addr, unsigned long *ret)
2187{
2188 int r;
2189 struct kvm_pv_mmu_op_buffer buffer;
2190
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002191 buffer.ptr = buffer.buf;
2192 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2193 buffer.processed = 0;
2194
2195 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2196 if (r)
2197 goto out;
2198
2199 while (buffer.len) {
2200 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2201 if (r < 0)
2202 goto out;
2203 if (r == 0)
2204 break;
2205 }
2206
2207 r = 1;
2208out:
2209 *ret = buffer.processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002210 return r;
2211}
2212
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002213#ifdef AUDIT
2214
2215static const char *audit_msg;
2216
2217static gva_t canonicalize(gva_t gva)
2218{
2219#ifdef CONFIG_X86_64
2220 gva = (long long)(gva << 16) >> 16;
2221#endif
2222 return gva;
2223}
2224
2225static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2226 gva_t va, int level)
2227{
2228 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2229 int i;
2230 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2231
2232 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2233 u64 ent = pt[i];
2234
Avi Kivityc7addb92007-09-16 18:58:32 +02002235 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002236 continue;
2237
2238 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002239 if (level > 1) {
2240 if (ent == shadow_notrap_nonpresent_pte)
2241 printk(KERN_ERR "audit: (%s) nontrapping pte"
2242 " in nonleaf level: levels %d gva %lx"
2243 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002244 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002245
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002246 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002247 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002248 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002249 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002250
Avi Kivityc7addb92007-09-16 18:58:32 +02002251 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002252 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002253 printk(KERN_ERR "xx audit error: (%s) levels %d"
2254 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002255 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002256 va, gpa, hpa, ent,
2257 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002258 else if (ent == shadow_notrap_nonpresent_pte
2259 && !is_error_hpa(hpa))
2260 printk(KERN_ERR "audit: (%s) notrap shadow,"
2261 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002262 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002263
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002264 }
2265 }
2266}
2267
2268static void audit_mappings(struct kvm_vcpu *vcpu)
2269{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002270 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002271
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002272 if (vcpu->arch.mmu.root_level == 4)
2273 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002274 else
2275 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002276 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002277 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002278 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002279 i << 30,
2280 2);
2281}
2282
2283static int count_rmaps(struct kvm_vcpu *vcpu)
2284{
2285 int nmaps = 0;
2286 int i, j, k;
2287
2288 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2289 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2290 struct kvm_rmap_desc *d;
2291
2292 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002293 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002294
Izik Eidus290fc382007-09-27 14:11:22 +02002295 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002296 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002297 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002298 ++nmaps;
2299 continue;
2300 }
Izik Eidus290fc382007-09-27 14:11:22 +02002301 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002302 while (d) {
2303 for (k = 0; k < RMAP_EXT; ++k)
2304 if (d->shadow_ptes[k])
2305 ++nmaps;
2306 else
2307 break;
2308 d = d->more;
2309 }
2310 }
2311 }
2312 return nmaps;
2313}
2314
2315static int count_writable_mappings(struct kvm_vcpu *vcpu)
2316{
2317 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002318 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002319 int i;
2320
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002321 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002322 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002323
Avi Kivity4db35312007-11-21 15:28:32 +02002324 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002325 continue;
2326
2327 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2328 u64 ent = pt[i];
2329
2330 if (!(ent & PT_PRESENT_MASK))
2331 continue;
2332 if (!(ent & PT_WRITABLE_MASK))
2333 continue;
2334 ++nmaps;
2335 }
2336 }
2337 return nmaps;
2338}
2339
2340static void audit_rmap(struct kvm_vcpu *vcpu)
2341{
2342 int n_rmap = count_rmaps(vcpu);
2343 int n_actual = count_writable_mappings(vcpu);
2344
2345 if (n_rmap != n_actual)
2346 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002347 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002348}
2349
2350static void audit_write_protection(struct kvm_vcpu *vcpu)
2351{
Avi Kivity4db35312007-11-21 15:28:32 +02002352 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002353 struct kvm_memory_slot *slot;
2354 unsigned long *rmapp;
2355 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002356
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002357 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002358 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002359 continue;
2360
Avi Kivity4db35312007-11-21 15:28:32 +02002361 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2362 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002363 rmapp = &slot->rmap[gfn - slot->base_gfn];
2364 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002365 printk(KERN_ERR "%s: (%s) shadow page has writable"
2366 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002367 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002368 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002369 }
2370}
2371
2372static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2373{
2374 int olddbg = dbg;
2375
2376 dbg = 0;
2377 audit_msg = msg;
2378 audit_rmap(vcpu);
2379 audit_write_protection(vcpu);
2380 audit_mappings(vcpu);
2381 dbg = olddbg;
2382}
2383
2384#endif