blob: 19a8906bcaa2c5301dddf55821078248a85e23a7 [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
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020021#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030022#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040025#include <linux/types.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/highmem.h>
29#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020030#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030031#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050032#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020033#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Avi Kivitye4956062007-06-28 14:15:57 -040035
36#include <asm/page.h>
37#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020038#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020039#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040040
Joerg Roedel18552672008-02-07 13:47:41 +010041/*
42 * When setting this variable to true it enables Two-Dimensional-Paging
43 * where the hardware walks 2 page tables:
44 * 1. the guest-virtual to guest-physical
45 * 2. while doing 1. it walks guest-physical to host-physical
46 * If the hardware supports that we don't need to do shadow paging.
47 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050048bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010049
Avi Kivity37a7d8b2007-01-05 16:36:56 -080050#undef MMU_DEBUG
51
52#undef AUDIT
53
54#ifdef AUDIT
55static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
56#else
57static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
58#endif
59
60#ifdef MMU_DEBUG
61
62#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
63#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
64
65#else
66
67#define pgprintk(x...) do { } while (0)
68#define rmap_printk(x...) do { } while (0)
69
70#endif
71
72#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030073static int dbg = 0;
74module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080075#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080076
Marcelo Tosatti582801a2008-09-23 13:18:41 -030077static int oos_shadow = 1;
78module_param(oos_shadow, bool, 0644);
79
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080080#ifndef MMU_DEBUG
81#define ASSERT(x) do { } while (0)
82#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define ASSERT(x) \
84 if (!(x)) { \
85 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
86 __FILE__, __LINE__, #x); \
87 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080088#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080089
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define PT_FIRST_AVAIL_BITS_SHIFT 9
91#define PT64_SECOND_AVAIL_BITS_SHIFT 52
92
Avi Kivity6aa8b732006-12-10 02:21:36 -080093#define VALID_PAGE(x) ((x) != INVALID_PAGE)
94
95#define PT64_LEVEL_BITS 9
96
97#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040098 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080099
100#define PT64_LEVEL_MASK(level) \
101 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
102
103#define PT64_INDEX(address, level)\
104 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
105
106
107#define PT32_LEVEL_BITS 10
108
109#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400110 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111
112#define PT32_LEVEL_MASK(level) \
113 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200114#define PT32_LVL_OFFSET_MASK(level) \
115 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
116 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117
118#define PT32_INDEX(address, level)\
119 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
120
121
Avi Kivity27aba762007-03-09 13:04:31 +0200122#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123#define PT64_DIR_BASE_ADDR_MASK \
124 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200125#define PT64_LVL_ADDR_MASK(level) \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
127 * PT64_LEVEL_BITS))) - 1))
128#define PT64_LVL_OFFSET_MASK(level) \
129 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
132#define PT32_BASE_ADDR_MASK PAGE_MASK
133#define PT32_DIR_BASE_ADDR_MASK \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200135#define PT32_LVL_ADDR_MASK(level) \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
137 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138
Avi Kivity79539ce2007-11-21 02:06:21 +0200139#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
140 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800142#define RMAP_EXT 4
143
Avi Kivityfe135d22007-12-09 16:15:46 +0200144#define ACC_EXEC_MASK 1
145#define ACC_WRITE_MASK PT_WRITABLE_MASK
146#define ACC_USER_MASK PT_USER_MASK
147#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
148
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200149#include <trace/events/kvm.h>
150
151#undef TRACE_INCLUDE_FILE
Avi Kivity07420172009-07-06 12:21:32 +0300152#define CREATE_TRACE_POINTS
153#include "mmutrace.h"
154
Izik Eidus14032832009-09-23 21:47:17 +0300155#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
156
Avi Kivity135f8c22008-08-21 17:49:56 +0300157#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
158
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800159struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300160 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800161 struct kvm_rmap_desc *more;
162};
163
Avi Kivity2d111232008-12-25 14:39:47 +0200164struct kvm_shadow_walk_iterator {
165 u64 addr;
166 hpa_t shadow_addr;
167 int level;
168 u64 *sptep;
169 unsigned index;
170};
171
172#define for_each_shadow_entry(_vcpu, _addr, _walker) \
173 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
174 shadow_walk_okay(&(_walker)); \
175 shadow_walk_next(&(_walker)))
176
177
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300178struct kvm_unsync_walk {
179 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
180};
181
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300182typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
183
Avi Kivityb5a33a72007-04-15 16:31:09 +0300184static struct kmem_cache *pte_chain_cache;
185static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300186static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300187
Avi Kivityc7addb92007-09-16 18:58:32 +0200188static u64 __read_mostly shadow_trap_nonpresent_pte;
189static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800190static u64 __read_mostly shadow_base_present_pte;
191static u64 __read_mostly shadow_nx_mask;
192static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
193static u64 __read_mostly shadow_user_mask;
194static u64 __read_mostly shadow_accessed_mask;
195static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200196
Dong, Eddie82725b22009-03-30 16:21:08 +0800197static inline u64 rsvd_bits(int s, int e)
198{
199 return ((1ULL << (e - s + 1)) - 1) << s;
200}
201
Avi Kivityc7addb92007-09-16 18:58:32 +0200202void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
203{
204 shadow_trap_nonpresent_pte = trap_pte;
205 shadow_notrap_nonpresent_pte = notrap_pte;
206}
207EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
208
Sheng Yang7b523452008-04-25 21:13:50 +0800209void kvm_mmu_set_base_ptes(u64 base_pte)
210{
211 shadow_base_present_pte = base_pte;
212}
213EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
214
215void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800216 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800217{
218 shadow_user_mask = user_mask;
219 shadow_accessed_mask = accessed_mask;
220 shadow_dirty_mask = dirty_mask;
221 shadow_nx_mask = nx_mask;
222 shadow_x_mask = x_mask;
223}
224EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
225
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226static int is_write_protection(struct kvm_vcpu *vcpu)
227{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200228 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229}
230
231static int is_cpuid_PSE36(void)
232{
233 return 1;
234}
235
Avi Kivity73b10872007-01-26 00:56:41 -0800236static int is_nx(struct kvm_vcpu *vcpu)
237{
Avi Kivityf6801df2010-01-21 15:31:50 +0200238 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800239}
240
Avi Kivityc7addb92007-09-16 18:58:32 +0200241static int is_shadow_present_pte(u64 pte)
242{
Avi Kivityc7addb92007-09-16 18:58:32 +0200243 return pte != shadow_trap_nonpresent_pte
244 && pte != shadow_notrap_nonpresent_pte;
245}
246
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300247static int is_large_pte(u64 pte)
248{
249 return pte & PT_PAGE_SIZE_MASK;
250}
251
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900252static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253{
254 return pte & PT_WRITABLE_MASK;
255}
256
Avi Kivity43a37952009-06-10 14:12:05 +0300257static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200258{
Avi Kivity439e2182009-06-10 12:56:54 +0300259 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200260}
261
Avi Kivity43a37952009-06-10 14:12:05 +0300262static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800263{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200264 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800265}
266
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300267static int is_last_spte(u64 pte, int level)
268{
269 if (level == PT_PAGE_TABLE_LEVEL)
270 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200271 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300272 return 1;
273 return 0;
274}
275
Anthony Liguori35149e22008-04-02 14:46:56 -0500276static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200277{
Anthony Liguori35149e22008-04-02 14:46:56 -0500278 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200279}
280
Avi Kivityda928522007-11-21 13:54:47 +0200281static gfn_t pse36_gfn_delta(u32 gpte)
282{
283 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
284
285 return (gpte & PT32_DIR_PSE36_MASK) << shift;
286}
287
Avi Kivityd555c332009-06-10 14:24:23 +0300288static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300289{
290#ifdef CONFIG_X86_64
291 set_64bit((unsigned long *)sptep, spte);
292#else
293 set_64bit((unsigned long long *)sptep, spte);
294#endif
295}
296
Avi Kivitye2dec932007-01-05 16:36:54 -0800297static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300298 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800299{
300 void *obj;
301
302 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800303 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800304 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300305 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800306 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800307 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800308 cache->objects[cache->nobjs++] = obj;
309 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800310 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800311}
312
313static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
314{
315 while (mc->nobjs)
316 kfree(mc->objects[--mc->nobjs]);
317}
318
Avi Kivityc1158e62007-07-20 08:18:27 +0300319static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300320 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300321{
322 struct page *page;
323
324 if (cache->nobjs >= min)
325 return 0;
326 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300327 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300328 if (!page)
329 return -ENOMEM;
330 set_page_private(page, 0);
331 cache->objects[cache->nobjs++] = page_address(page);
332 }
333 return 0;
334}
335
336static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
337{
338 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300339 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300340}
341
Avi Kivity8c438502007-04-16 11:53:17 +0300342static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
343{
344 int r;
345
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800346 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300347 pte_chain_cache, 4);
348 if (r)
349 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200351 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300352 if (r)
353 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800354 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300355 if (r)
356 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800357 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300358 mmu_page_header_cache, 4);
359out:
Avi Kivity8c438502007-04-16 11:53:17 +0300360 return r;
361}
362
Avi Kivity714b93d2007-01-05 16:36:53 -0800363static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
364{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800365 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
366 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
367 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
368 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800369}
370
371static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
372 size_t size)
373{
374 void *p;
375
376 BUG_ON(!mc->nobjs);
377 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800378 return p;
379}
380
Avi Kivity714b93d2007-01-05 16:36:53 -0800381static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
382{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800383 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800384 sizeof(struct kvm_pte_chain));
385}
386
Avi Kivity90cb0522007-07-17 13:04:56 +0300387static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800388{
Avi Kivity90cb0522007-07-17 13:04:56 +0300389 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800390}
391
392static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
393{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800394 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800395 sizeof(struct kvm_rmap_desc));
396}
397
Avi Kivity90cb0522007-07-17 13:04:56 +0300398static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800399{
Avi Kivity90cb0522007-07-17 13:04:56 +0300400 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800401}
402
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800403/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300404 * Return the pointer to the largepage write count for a given
405 * gfn, handling slots that are not large page aligned.
406 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200407static int *slot_largepage_idx(gfn_t gfn,
408 struct kvm_memory_slot *slot,
409 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300410{
411 unsigned long idx;
412
Joerg Roedeld25797b2009-07-27 16:30:43 +0200413 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
414 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
415 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300416}
417
418static void account_shadowed(struct kvm *kvm, gfn_t gfn)
419{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200420 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200422 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423
Izik Eidus28430992008-10-03 17:40:32 +0300424 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200425
426 slot = gfn_to_memslot_unaliased(kvm, gfn);
427 for (i = PT_DIRECTORY_LEVEL;
428 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
429 write_count = slot_largepage_idx(gfn, slot, i);
430 *write_count += 1;
431 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300432}
433
434static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
435{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200436 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300437 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439
Izik Eidus28430992008-10-03 17:40:32 +0300440 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200441 for (i = PT_DIRECTORY_LEVEL;
442 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
443 slot = gfn_to_memslot_unaliased(kvm, gfn);
444 write_count = slot_largepage_idx(gfn, slot, i);
445 *write_count -= 1;
446 WARN_ON(*write_count < 0);
447 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448}
449
Joerg Roedeld25797b2009-07-27 16:30:43 +0200450static int has_wrprotected_page(struct kvm *kvm,
451 gfn_t gfn,
452 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453{
Izik Eidus28430992008-10-03 17:40:32 +0300454 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455 int *largepage_idx;
456
Izik Eidus28430992008-10-03 17:40:32 +0300457 gfn = unalias_gfn(kvm, gfn);
458 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200460 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461 return *largepage_idx;
462 }
463
464 return 1;
465}
466
Joerg Roedeld25797b2009-07-27 16:30:43 +0200467static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100469 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200470 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300471
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100472 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300473
Joerg Roedeld25797b2009-07-27 16:30:43 +0200474 for (i = PT_PAGE_TABLE_LEVEL;
475 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
476 if (page_size >= KVM_HPAGE_SIZE(i))
477 ret = i;
478 else
479 break;
480 }
481
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300482 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300483}
484
Joerg Roedeld25797b2009-07-27 16:30:43 +0200485static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486{
487 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800488 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300489
490 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
491 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200492 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493
Joerg Roedeld25797b2009-07-27 16:30:43 +0200494 host_level = host_mapping_level(vcpu->kvm, large_gfn);
495
496 if (host_level == PT_PAGE_TABLE_LEVEL)
497 return host_level;
498
Sheng Yang878403b2010-01-05 19:02:29 +0800499 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
500 kvm_x86_ops->get_lpage_level() : host_level;
501
502 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200503 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
504 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505
506 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300507}
508
509/*
Izik Eidus290fc382007-09-27 14:11:22 +0200510 * Take gfn and return the reverse mapping to it.
511 * Note: gfn must be unaliased before this function get called
512 */
513
Joerg Roedel44ad9942009-07-27 16:30:42 +0200514static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200515{
516 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300517 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200518
519 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200520 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300521 return &slot->rmap[gfn - slot->base_gfn];
522
Joerg Roedel44ad9942009-07-27 16:30:42 +0200523 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
524 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300525
Joerg Roedel44ad9942009-07-27 16:30:42 +0200526 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200527}
528
529/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800530 * Reverse mapping data structures:
531 *
Izik Eidus290fc382007-09-27 14:11:22 +0200532 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
533 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800534 *
Izik Eidus290fc382007-09-27 14:11:22 +0200535 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
536 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300537 *
538 * Returns the number of rmap entries before the spte was added or zero if
539 * the spte was not added.
540 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200542static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543{
Avi Kivity4db35312007-11-21 15:28:32 +0200544 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800545 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200546 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300547 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548
Avi Kivity43a37952009-06-10 14:12:05 +0300549 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300550 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200551 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200552 sp = page_header(__pa(spte));
553 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200554 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200555 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200557 *rmapp = (unsigned long)spte;
558 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800560 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300561 desc->sptes[0] = (u64 *)*rmapp;
562 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200563 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800564 } else {
565 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200566 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300567 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800568 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300569 count += RMAP_EXT;
570 }
Avi Kivityd555c332009-06-10 14:24:23 +0300571 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800572 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800573 desc = desc->more;
574 }
Avi Kivityd555c332009-06-10 14:24:23 +0300575 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800576 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300577 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800578 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300579 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580}
581
Izik Eidus290fc382007-09-27 14:11:22 +0200582static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800583 struct kvm_rmap_desc *desc,
584 int i,
585 struct kvm_rmap_desc *prev_desc)
586{
587 int j;
588
Avi Kivityd555c332009-06-10 14:24:23 +0300589 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800590 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300591 desc->sptes[i] = desc->sptes[j];
592 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800593 if (j != 0)
594 return;
595 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300596 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800597 else
598 if (prev_desc)
599 prev_desc->more = desc->more;
600 else
Izik Eidus290fc382007-09-27 14:11:22 +0200601 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300602 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800603}
604
Izik Eidus290fc382007-09-27 14:11:22 +0200605static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800607 struct kvm_rmap_desc *desc;
608 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200609 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500610 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200611 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800612 int i;
613
Avi Kivity43a37952009-06-10 14:12:05 +0300614 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200616 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500617 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800618 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500619 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900620 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300621 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200622 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200623 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800624 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
625 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200626 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800627 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200628 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
630 spte, *spte);
631 BUG();
632 }
Izik Eidus290fc382007-09-27 14:11:22 +0200633 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800634 } else {
635 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200636 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800637 prev_desc = NULL;
638 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300639 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
640 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200641 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800642 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 prev_desc);
644 return;
645 }
646 prev_desc = desc;
647 desc = desc->more;
648 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200649 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800650 BUG();
651 }
652}
653
Izik Eidus98348e92007-10-16 14:42:30 +0200654static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800655{
Avi Kivity374cbac2007-01-05 16:36:43 -0800656 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200657 struct kvm_rmap_desc *prev_desc;
658 u64 *prev_spte;
659 int i;
660
661 if (!*rmapp)
662 return NULL;
663 else if (!(*rmapp & 1)) {
664 if (!spte)
665 return (u64 *)*rmapp;
666 return NULL;
667 }
668 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
669 prev_desc = NULL;
670 prev_spte = NULL;
671 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300672 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200673 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300674 return desc->sptes[i];
675 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200676 }
677 desc = desc->more;
678 }
679 return NULL;
680}
681
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200682static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200683{
Izik Eidus290fc382007-09-27 14:11:22 +0200684 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800685 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200686 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800687
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500688 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200689 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800690
Izik Eidus98348e92007-10-16 14:42:30 +0200691 spte = rmap_next(kvm, rmapp, NULL);
692 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800693 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800694 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800695 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900696 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300697 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800698 write_protected = 1;
699 }
Izik Eidus9647c142007-10-16 14:43:46 +0200700 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800701 }
Izik Eidus855149a2008-03-20 18:17:24 +0200702 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500703 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200704
705 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500706 pfn = spte_to_pfn(*spte);
707 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200708 }
709
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300710 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200711 for (i = PT_DIRECTORY_LEVEL;
712 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
713 rmapp = gfn_to_rmap(kvm, gfn, i);
714 spte = rmap_next(kvm, rmapp, NULL);
715 while (spte) {
716 BUG_ON(!spte);
717 BUG_ON(!(*spte & PT_PRESENT_MASK));
718 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
719 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900720 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200721 rmap_remove(kvm, spte);
722 --kvm->stat.lpages;
723 __set_spte(spte, shadow_trap_nonpresent_pte);
724 spte = NULL;
725 write_protected = 1;
726 }
727 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300728 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300729 }
730
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200731 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800732}
733
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000734static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
735 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200736{
737 u64 *spte;
738 int need_tlb_flush = 0;
739
740 while ((spte = rmap_next(kvm, rmapp, NULL))) {
741 BUG_ON(!(*spte & PT_PRESENT_MASK));
742 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
743 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300744 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200745 need_tlb_flush = 1;
746 }
747 return need_tlb_flush;
748}
749
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000750static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
751 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300752{
753 int need_flush = 0;
754 u64 *spte, new_spte;
755 pte_t *ptep = (pte_t *)data;
756 pfn_t new_pfn;
757
758 WARN_ON(pte_huge(*ptep));
759 new_pfn = pte_pfn(*ptep);
760 spte = rmap_next(kvm, rmapp, NULL);
761 while (spte) {
762 BUG_ON(!is_shadow_present_pte(*spte));
763 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
764 need_flush = 1;
765 if (pte_write(*ptep)) {
766 rmap_remove(kvm, spte);
767 __set_spte(spte, shadow_trap_nonpresent_pte);
768 spte = rmap_next(kvm, rmapp, NULL);
769 } else {
770 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
771 new_spte |= (u64)new_pfn << PAGE_SHIFT;
772
773 new_spte &= ~PT_WRITABLE_MASK;
774 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900775 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300776 kvm_set_pfn_dirty(spte_to_pfn(*spte));
777 __set_spte(spte, new_spte);
778 spte = rmap_next(kvm, rmapp, spte);
779 }
780 }
781 if (need_flush)
782 kvm_flush_remote_tlbs(kvm);
783
784 return 0;
785}
786
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000787static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
788 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300789 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000790 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200791{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200792 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200793 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200794 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200795 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200796
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200797 slots = rcu_dereference(kvm->memslots);
798
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200799 for (i = 0; i < slots->nmemslots; i++) {
800 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200801 unsigned long start = memslot->userspace_addr;
802 unsigned long end;
803
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200804 end = start + (memslot->npages << PAGE_SHIFT);
805 if (hva >= start && hva < end) {
806 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200807
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200808 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200809
810 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
811 int idx = gfn_offset;
812 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200813 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300814 &memslot->lpage_info[j][idx].rmap_pde,
815 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200816 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200817 trace_kvm_age_page(hva, memslot, ret);
818 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200819 }
820 }
821
822 return retval;
823}
824
825int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
826{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300827 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200828}
829
Izik Eidus3da0dd42009-09-23 21:47:18 +0300830void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
831{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000832 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300833}
834
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000835static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
836 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200837{
838 u64 *spte;
839 int young = 0;
840
Rik van Riel6316e1c2010-02-03 16:11:03 -0500841 /*
842 * Emulate the accessed bit for EPT, by checking if this page has
843 * an EPT mapping, and clearing it if it does. On the next access,
844 * a new EPT mapping will be established.
845 * This has some overhead, but not as much as the cost of swapping
846 * out actively used pages or breaking up actively used hugepages.
847 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800848 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500849 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800850
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200851 spte = rmap_next(kvm, rmapp, NULL);
852 while (spte) {
853 int _young;
854 u64 _spte = *spte;
855 BUG_ON(!(_spte & PT_PRESENT_MASK));
856 _young = _spte & PT_ACCESSED_MASK;
857 if (_young) {
858 young = 1;
859 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
860 }
861 spte = rmap_next(kvm, rmapp, spte);
862 }
863 return young;
864}
865
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300866#define RMAP_RECYCLE_THRESHOLD 1000
867
Joerg Roedel852e3c12009-07-27 16:30:44 +0200868static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300869{
870 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200871 struct kvm_mmu_page *sp;
872
873 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300874
875 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200876 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300877
Izik Eidus3da0dd42009-09-23 21:47:18 +0300878 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300879 kvm_flush_remote_tlbs(vcpu->kvm);
880}
881
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200882int kvm_age_hva(struct kvm *kvm, unsigned long hva)
883{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300884 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200885}
886
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800887#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300888static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889{
Avi Kivity139bdb22007-01-05 16:36:50 -0800890 u64 *pos;
891 u64 *end;
892
Avi Kivity47ad8e62007-05-06 15:50:58 +0300893 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300894 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800895 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800896 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800898 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 return 1;
900}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800901#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902
Avi Kivity4db35312007-11-21 15:28:32 +0200903static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800904{
Avi Kivity4db35312007-11-21 15:28:32 +0200905 ASSERT(is_empty_shadow_page(sp->spt));
906 list_del(&sp->link);
907 __free_page(virt_to_page(sp->spt));
908 __free_page(virt_to_page(sp->gfns));
909 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800910 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800911}
912
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800913static unsigned kvm_page_table_hashfn(gfn_t gfn)
914{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200915 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800916}
917
Avi Kivity25c0de22007-01-05 16:36:42 -0800918static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
919 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920{
Avi Kivity4db35312007-11-21 15:28:32 +0200921 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800923 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
924 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
925 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200926 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800927 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200928 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800929 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200930 sp->multimapped = 0;
931 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800932 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200933 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934}
935
Avi Kivity714b93d2007-01-05 16:36:53 -0800936static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200937 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800938{
939 struct kvm_pte_chain *pte_chain;
940 struct hlist_node *node;
941 int i;
942
943 if (!parent_pte)
944 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200945 if (!sp->multimapped) {
946 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947
948 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200949 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800950 return;
951 }
Avi Kivity4db35312007-11-21 15:28:32 +0200952 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800953 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200954 INIT_HLIST_HEAD(&sp->parent_ptes);
955 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800956 pte_chain->parent_ptes[0] = old;
957 }
Avi Kivity4db35312007-11-21 15:28:32 +0200958 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800959 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
960 continue;
961 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
962 if (!pte_chain->parent_ptes[i]) {
963 pte_chain->parent_ptes[i] = parent_pte;
964 return;
965 }
966 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800967 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800968 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200969 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800970 pte_chain->parent_ptes[0] = parent_pte;
971}
972
Avi Kivity4db35312007-11-21 15:28:32 +0200973static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800974 u64 *parent_pte)
975{
976 struct kvm_pte_chain *pte_chain;
977 struct hlist_node *node;
978 int i;
979
Avi Kivity4db35312007-11-21 15:28:32 +0200980 if (!sp->multimapped) {
981 BUG_ON(sp->parent_pte != parent_pte);
982 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800983 return;
984 }
Avi Kivity4db35312007-11-21 15:28:32 +0200985 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800986 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
987 if (!pte_chain->parent_ptes[i])
988 break;
989 if (pte_chain->parent_ptes[i] != parent_pte)
990 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800991 while (i + 1 < NR_PTE_CHAIN_ENTRIES
992 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800993 pte_chain->parent_ptes[i]
994 = pte_chain->parent_ptes[i + 1];
995 ++i;
996 }
997 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800998 if (i == 0) {
999 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001000 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001001 if (hlist_empty(&sp->parent_ptes)) {
1002 sp->multimapped = 0;
1003 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001004 }
1005 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001006 return;
1007 }
1008 BUG();
1009}
1010
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001011
1012static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1013 mmu_parent_walk_fn fn)
1014{
1015 struct kvm_pte_chain *pte_chain;
1016 struct hlist_node *node;
1017 struct kvm_mmu_page *parent_sp;
1018 int i;
1019
1020 if (!sp->multimapped && sp->parent_pte) {
1021 parent_sp = page_header(__pa(sp->parent_pte));
1022 fn(vcpu, parent_sp);
1023 mmu_parent_walk(vcpu, parent_sp, fn);
1024 return;
1025 }
1026 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1027 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1028 if (!pte_chain->parent_ptes[i])
1029 break;
1030 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1031 fn(vcpu, parent_sp);
1032 mmu_parent_walk(vcpu, parent_sp, fn);
1033 }
1034}
1035
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001036static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1037{
1038 unsigned int index;
1039 struct kvm_mmu_page *sp = page_header(__pa(spte));
1040
1041 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001042 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1043 sp->unsync_children++;
1044 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001045}
1046
1047static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1048{
1049 struct kvm_pte_chain *pte_chain;
1050 struct hlist_node *node;
1051 int i;
1052
1053 if (!sp->parent_pte)
1054 return;
1055
1056 if (!sp->multimapped) {
1057 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1058 return;
1059 }
1060
1061 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1062 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1063 if (!pte_chain->parent_ptes[i])
1064 break;
1065 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1066 }
1067}
1068
1069static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1070{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001071 kvm_mmu_update_parents_unsync(sp);
1072 return 1;
1073}
1074
1075static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1076 struct kvm_mmu_page *sp)
1077{
1078 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1079 kvm_mmu_update_parents_unsync(sp);
1080}
1081
Avi Kivityd761a502008-05-29 14:55:03 +03001082static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1083 struct kvm_mmu_page *sp)
1084{
1085 int i;
1086
1087 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1088 sp->spt[i] = shadow_trap_nonpresent_pte;
1089}
1090
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001091static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1092 struct kvm_mmu_page *sp)
1093{
1094 return 1;
1095}
1096
Marcelo Tosattia7052892008-09-23 13:18:35 -03001097static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1098{
1099}
1100
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001101#define KVM_PAGE_ARRAY_NR 16
1102
1103struct kvm_mmu_pages {
1104 struct mmu_page_and_offset {
1105 struct kvm_mmu_page *sp;
1106 unsigned int idx;
1107 } page[KVM_PAGE_ARRAY_NR];
1108 unsigned int nr;
1109};
1110
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001111#define for_each_unsync_children(bitmap, idx) \
1112 for (idx = find_first_bit(bitmap, 512); \
1113 idx < 512; \
1114 idx = find_next_bit(bitmap, 512, idx+1))
1115
Hannes Edercded19f2009-02-21 02:19:13 +01001116static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1117 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001118{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001119 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001120
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001121 if (sp->unsync)
1122 for (i=0; i < pvec->nr; i++)
1123 if (pvec->page[i].sp == sp)
1124 return 0;
1125
1126 pvec->page[pvec->nr].sp = sp;
1127 pvec->page[pvec->nr].idx = idx;
1128 pvec->nr++;
1129 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1130}
1131
1132static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1133 struct kvm_mmu_pages *pvec)
1134{
1135 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001136
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001137 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001138 u64 ent = sp->spt[i];
1139
Marcelo Tosatti87917232008-12-22 18:49:30 -02001140 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001141 struct kvm_mmu_page *child;
1142 child = page_header(ent & PT64_BASE_ADDR_MASK);
1143
1144 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001145 if (mmu_pages_add(pvec, child, i))
1146 return -ENOSPC;
1147
1148 ret = __mmu_unsync_walk(child, pvec);
1149 if (!ret)
1150 __clear_bit(i, sp->unsync_child_bitmap);
1151 else if (ret > 0)
1152 nr_unsync_leaf += ret;
1153 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001154 return ret;
1155 }
1156
1157 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001158 nr_unsync_leaf++;
1159 if (mmu_pages_add(pvec, child, i))
1160 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001161 }
1162 }
1163 }
1164
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001165 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001166 sp->unsync_children = 0;
1167
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001168 return nr_unsync_leaf;
1169}
1170
1171static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1172 struct kvm_mmu_pages *pvec)
1173{
1174 if (!sp->unsync_children)
1175 return 0;
1176
1177 mmu_pages_add(pvec, sp, 0);
1178 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001179}
1180
Avi Kivity4db35312007-11-21 15:28:32 +02001181static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001182{
1183 unsigned index;
1184 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001185 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001186 struct hlist_node *node;
1187
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001188 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001189 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001190 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001191 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001192 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001193 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001194 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001195 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001196 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001197 }
1198 return NULL;
1199}
1200
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001201static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1202{
1203 WARN_ON(!sp->unsync);
1204 sp->unsync = 0;
1205 --kvm->stat.mmu_unsync;
1206}
1207
1208static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1209
1210static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1211{
1212 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1213 kvm_mmu_zap_page(vcpu->kvm, sp);
1214 return 1;
1215 }
1216
Avi Kivityf691fe12009-07-06 15:58:14 +03001217 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001218 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1219 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001220 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1222 kvm_mmu_zap_page(vcpu->kvm, sp);
1223 return 1;
1224 }
1225
1226 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001227 return 0;
1228}
1229
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001230struct mmu_page_path {
1231 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1232 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233};
1234
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001235#define for_each_sp(pvec, sp, parents, i) \
1236 for (i = mmu_pages_next(&pvec, &parents, -1), \
1237 sp = pvec.page[i].sp; \
1238 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1239 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001240
Hannes Edercded19f2009-02-21 02:19:13 +01001241static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1242 struct mmu_page_path *parents,
1243 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001244{
1245 int n;
1246
1247 for (n = i+1; n < pvec->nr; n++) {
1248 struct kvm_mmu_page *sp = pvec->page[n].sp;
1249
1250 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1251 parents->idx[0] = pvec->page[n].idx;
1252 return n;
1253 }
1254
1255 parents->parent[sp->role.level-2] = sp;
1256 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1257 }
1258
1259 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001260}
1261
Hannes Edercded19f2009-02-21 02:19:13 +01001262static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001263{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001264 struct kvm_mmu_page *sp;
1265 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001266
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001267 do {
1268 unsigned int idx = parents->idx[level];
1269
1270 sp = parents->parent[level];
1271 if (!sp)
1272 return;
1273
1274 --sp->unsync_children;
1275 WARN_ON((int)sp->unsync_children < 0);
1276 __clear_bit(idx, sp->unsync_child_bitmap);
1277 level++;
1278 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1279}
1280
1281static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1282 struct mmu_page_path *parents,
1283 struct kvm_mmu_pages *pvec)
1284{
1285 parents->parent[parent->role.level-1] = NULL;
1286 pvec->nr = 0;
1287}
1288
1289static void mmu_sync_children(struct kvm_vcpu *vcpu,
1290 struct kvm_mmu_page *parent)
1291{
1292 int i;
1293 struct kvm_mmu_page *sp;
1294 struct mmu_page_path parents;
1295 struct kvm_mmu_pages pages;
1296
1297 kvm_mmu_pages_init(parent, &parents, &pages);
1298 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001299 int protected = 0;
1300
1301 for_each_sp(pages, sp, parents, i)
1302 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1303
1304 if (protected)
1305 kvm_flush_remote_tlbs(vcpu->kvm);
1306
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001307 for_each_sp(pages, sp, parents, i) {
1308 kvm_sync_page(vcpu, sp);
1309 mmu_pages_clear_parents(&parents);
1310 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001311 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001312 kvm_mmu_pages_init(parent, &parents, &pages);
1313 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001314}
1315
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001316static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1317 gfn_t gfn,
1318 gva_t gaddr,
1319 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001320 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001321 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001322 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001323{
1324 union kvm_mmu_page_role role;
1325 unsigned index;
1326 unsigned quadrant;
1327 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001328 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001329 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001330
Avi Kivitya770f6f2008-12-21 19:20:09 +02001331 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001332 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001333 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001334 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001335 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001336 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1337 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1338 role.quadrant = quadrant;
1339 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001340 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001341 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001342 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1343 if (sp->gfn == gfn) {
1344 if (sp->unsync)
1345 if (kvm_sync_page(vcpu, sp))
1346 continue;
1347
1348 if (sp->role.word != role.word)
1349 continue;
1350
Avi Kivity4db35312007-11-21 15:28:32 +02001351 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001352 if (sp->unsync_children) {
1353 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1354 kvm_mmu_mark_parents_unsync(vcpu, sp);
1355 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001356 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001357 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001358 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001359 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001360 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1361 if (!sp)
1362 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001363 sp->gfn = gfn;
1364 sp->role = role;
1365 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001366 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001367 if (rmap_write_protect(vcpu->kvm, gfn))
1368 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001369 account_shadowed(vcpu->kvm, gfn);
1370 }
Avi Kivity131d8272008-05-29 14:56:28 +03001371 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1372 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1373 else
1374 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001375 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001376 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001377}
1378
Avi Kivity2d111232008-12-25 14:39:47 +02001379static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1380 struct kvm_vcpu *vcpu, u64 addr)
1381{
1382 iterator->addr = addr;
1383 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1384 iterator->level = vcpu->arch.mmu.shadow_root_level;
1385 if (iterator->level == PT32E_ROOT_LEVEL) {
1386 iterator->shadow_addr
1387 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1388 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1389 --iterator->level;
1390 if (!iterator->shadow_addr)
1391 iterator->level = 0;
1392 }
1393}
1394
1395static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1396{
1397 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1398 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001399
1400 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1401 if (is_large_pte(*iterator->sptep))
1402 return false;
1403
Avi Kivity2d111232008-12-25 14:39:47 +02001404 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1405 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1406 return true;
1407}
1408
1409static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1410{
1411 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1412 --iterator->level;
1413}
1414
Avi Kivity90cb0522007-07-17 13:04:56 +03001415static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001416 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001417{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001418 unsigned i;
1419 u64 *pt;
1420 u64 ent;
1421
Avi Kivity4db35312007-11-21 15:28:32 +02001422 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001423
Avi Kivity697fe2e2007-01-05 16:36:46 -08001424 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1425 ent = pt[i];
1426
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001427 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001428 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001429 ent &= PT64_BASE_ADDR_MASK;
1430 mmu_page_remove_parent_pte(page_header(ent),
1431 &pt[i]);
1432 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001433 if (is_large_pte(ent))
1434 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001435 rmap_remove(kvm, &pt[i]);
1436 }
1437 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001438 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001439 }
Avi Kivitya4360362007-01-05 16:36:45 -08001440}
1441
Avi Kivity4db35312007-11-21 15:28:32 +02001442static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001443{
Avi Kivity4db35312007-11-21 15:28:32 +02001444 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001445}
1446
Avi Kivity12b7d282007-09-23 14:10:49 +02001447static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1448{
1449 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001450 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001451
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001452 kvm_for_each_vcpu(i, vcpu, kvm)
1453 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001454}
1455
Avi Kivity31aa2b42008-07-11 17:59:46 +03001456static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001457{
1458 u64 *parent_pte;
1459
Avi Kivity4db35312007-11-21 15:28:32 +02001460 while (sp->multimapped || sp->parent_pte) {
1461 if (!sp->multimapped)
1462 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001463 else {
1464 struct kvm_pte_chain *chain;
1465
Avi Kivity4db35312007-11-21 15:28:32 +02001466 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001467 struct kvm_pte_chain, link);
1468 parent_pte = chain->parent_ptes[0];
1469 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001470 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001471 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001472 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001473 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001474}
1475
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001476static int mmu_zap_unsync_children(struct kvm *kvm,
1477 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001478{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001479 int i, zapped = 0;
1480 struct mmu_page_path parents;
1481 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001482
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001483 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001484 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001485
1486 kvm_mmu_pages_init(parent, &parents, &pages);
1487 while (mmu_unsync_walk(parent, &pages)) {
1488 struct kvm_mmu_page *sp;
1489
1490 for_each_sp(pages, sp, parents, i) {
1491 kvm_mmu_zap_page(kvm, sp);
1492 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001493 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001494 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001495 kvm_mmu_pages_init(parent, &parents, &pages);
1496 }
1497
1498 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001499}
1500
Marcelo Tosatti07385412008-09-23 13:18:37 -03001501static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001502{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001503 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001504
1505 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001506 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001507 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001508 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001509 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001510 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001511 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001512 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001513 if (sp->unsync)
1514 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001515 if (!sp->root_count) {
1516 hlist_del(&sp->hash_link);
1517 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001518 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001519 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001520 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001521 kvm_reload_remote_mmus(kvm);
1522 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001523 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001524 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001525}
1526
Izik Eidus82ce2c92007-10-02 18:52:55 +02001527/*
1528 * Changing the number of mmu pages allocated to the vm
1529 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1530 */
1531void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1532{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001533 int used_pages;
1534
1535 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1536 used_pages = max(0, used_pages);
1537
Izik Eidus82ce2c92007-10-02 18:52:55 +02001538 /*
1539 * If we set the number of mmu pages to be smaller be than the
1540 * number of actived pages , we must to free some mmu pages before we
1541 * change the value
1542 */
1543
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001544 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001545 while (used_pages > kvm_nr_mmu_pages &&
1546 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001547 struct kvm_mmu_page *page;
1548
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001549 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001550 struct kvm_mmu_page, link);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001551 used_pages -= kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001552 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001553 }
Xiao Guangrong77662e02010-04-16 16:34:42 +08001554 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001555 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001556 }
1557 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001558 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1559 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001560
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001561 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001562}
1563
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001564static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001565{
1566 unsigned index;
1567 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001568 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001569 struct hlist_node *node, *n;
1570 int r;
1571
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001572 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001573 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001574 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001575 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001576 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001577 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001578 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001579 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001580 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001581 if (kvm_mmu_zap_page(kvm, sp))
1582 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001583 }
1584 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001585}
1586
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001587static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001588{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001589 unsigned index;
1590 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001591 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001592 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001593
Avi Kivity4677a3b2009-01-06 13:00:27 +02001594 index = kvm_page_table_hashfn(gfn);
1595 bucket = &kvm->arch.mmu_page_hash[index];
1596 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001597 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001598 && !sp->role.invalid) {
1599 pgprintk("%s: zap %lx %x\n",
1600 __func__, gfn, sp->role.word);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001601 if (kvm_mmu_zap_page(kvm, sp))
1602 nn = bucket->first;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001603 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001604 }
1605}
1606
Avi Kivity38c335f2007-11-21 14:20:22 +02001607static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001609 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001610 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611
Sheng Yang291f26b2008-10-16 17:30:57 +08001612 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613}
1614
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001615static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1616{
1617 int i;
1618 u64 *pt = sp->spt;
1619
1620 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1621 return;
1622
1623 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1624 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001625 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001626 }
1627}
1628
Avi Kivity039576c2007-03-20 12:46:50 +02001629struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1630{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001631 struct page *page;
1632
Gleb Natapov1871c602010-02-10 14:21:32 +02001633 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Avi Kivity039576c2007-03-20 12:46:50 +02001634
1635 if (gpa == UNMAPPED_GVA)
1636 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001637
Izik Eidus72dc67a2008-02-10 18:04:15 +02001638 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001639
1640 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001641}
1642
Sheng Yang74be52e2008-10-09 16:01:56 +08001643/*
1644 * The function is based on mtrr_type_lookup() in
1645 * arch/x86/kernel/cpu/mtrr/generic.c
1646 */
1647static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1648 u64 start, u64 end)
1649{
1650 int i;
1651 u64 base, mask;
1652 u8 prev_match, curr_match;
1653 int num_var_ranges = KVM_NR_VAR_MTRR;
1654
1655 if (!mtrr_state->enabled)
1656 return 0xFF;
1657
1658 /* Make end inclusive end, instead of exclusive */
1659 end--;
1660
1661 /* Look in fixed ranges. Just return the type as per start */
1662 if (mtrr_state->have_fixed && (start < 0x100000)) {
1663 int idx;
1664
1665 if (start < 0x80000) {
1666 idx = 0;
1667 idx += (start >> 16);
1668 return mtrr_state->fixed_ranges[idx];
1669 } else if (start < 0xC0000) {
1670 idx = 1 * 8;
1671 idx += ((start - 0x80000) >> 14);
1672 return mtrr_state->fixed_ranges[idx];
1673 } else if (start < 0x1000000) {
1674 idx = 3 * 8;
1675 idx += ((start - 0xC0000) >> 12);
1676 return mtrr_state->fixed_ranges[idx];
1677 }
1678 }
1679
1680 /*
1681 * Look in variable ranges
1682 * Look of multiple ranges matching this address and pick type
1683 * as per MTRR precedence
1684 */
1685 if (!(mtrr_state->enabled & 2))
1686 return mtrr_state->def_type;
1687
1688 prev_match = 0xFF;
1689 for (i = 0; i < num_var_ranges; ++i) {
1690 unsigned short start_state, end_state;
1691
1692 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1693 continue;
1694
1695 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1696 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1697 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1698 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1699
1700 start_state = ((start & mask) == (base & mask));
1701 end_state = ((end & mask) == (base & mask));
1702 if (start_state != end_state)
1703 return 0xFE;
1704
1705 if ((start & mask) != (base & mask))
1706 continue;
1707
1708 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1709 if (prev_match == 0xFF) {
1710 prev_match = curr_match;
1711 continue;
1712 }
1713
1714 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1715 curr_match == MTRR_TYPE_UNCACHABLE)
1716 return MTRR_TYPE_UNCACHABLE;
1717
1718 if ((prev_match == MTRR_TYPE_WRBACK &&
1719 curr_match == MTRR_TYPE_WRTHROUGH) ||
1720 (prev_match == MTRR_TYPE_WRTHROUGH &&
1721 curr_match == MTRR_TYPE_WRBACK)) {
1722 prev_match = MTRR_TYPE_WRTHROUGH;
1723 curr_match = MTRR_TYPE_WRTHROUGH;
1724 }
1725
1726 if (prev_match != curr_match)
1727 return MTRR_TYPE_UNCACHABLE;
1728 }
1729
1730 if (prev_match != 0xFF)
1731 return prev_match;
1732
1733 return mtrr_state->def_type;
1734}
1735
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001736u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001737{
1738 u8 mtrr;
1739
1740 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1741 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1742 if (mtrr == 0xfe || mtrr == 0xff)
1743 mtrr = MTRR_TYPE_WRBACK;
1744 return mtrr;
1745}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001746EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001747
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001748static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1749{
1750 unsigned index;
1751 struct hlist_head *bucket;
1752 struct kvm_mmu_page *s;
1753 struct hlist_node *node, *n;
1754
Avi Kivityf691fe12009-07-06 15:58:14 +03001755 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001756 index = kvm_page_table_hashfn(sp->gfn);
1757 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1758 /* don't unsync if pagetable is shadowed with multiple roles */
1759 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001760 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001761 continue;
1762 if (s->role.word != sp->role.word)
1763 return 1;
1764 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001765 ++vcpu->kvm->stat.mmu_unsync;
1766 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001767
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001768 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001769
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001770 mmu_convert_notrap(sp);
1771 return 0;
1772}
1773
1774static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1775 bool can_unsync)
1776{
1777 struct kvm_mmu_page *shadow;
1778
1779 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1780 if (shadow) {
1781 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1782 return 1;
1783 if (shadow->unsync)
1784 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001785 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001786 return kvm_unsync_page(vcpu, shadow);
1787 return 1;
1788 }
1789 return 0;
1790}
1791
Avi Kivityd555c332009-06-10 14:24:23 +03001792static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001793 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001794 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001795 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001796 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001797{
1798 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001799 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001800
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001801 /*
1802 * We don't set the accessed bit, since we sometimes want to see
1803 * whether the guest actually used the pte (in order to detect
1804 * demand paging).
1805 */
Sheng Yang7b523452008-04-25 21:13:50 +08001806 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001807 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001808 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001809 if (!dirty)
1810 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001811 if (pte_access & ACC_EXEC_MASK)
1812 spte |= shadow_x_mask;
1813 else
1814 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001815 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001816 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001817 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001818 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001819 if (tdp_enabled)
1820 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1821 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001822
Izik Eidus14032832009-09-23 21:47:17 +03001823 if (reset_host_protection)
1824 spte |= SPTE_HOST_WRITEABLE;
1825
Anthony Liguori35149e22008-04-02 14:46:56 -05001826 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001827
1828 if ((pte_access & ACC_WRITE_MASK)
1829 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001830
Joerg Roedel852e3c12009-07-27 16:30:44 +02001831 if (level > PT_PAGE_TABLE_LEVEL &&
1832 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001833 ret = 1;
1834 spte = shadow_trap_nonpresent_pte;
1835 goto set_pte;
1836 }
1837
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001838 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001839
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001840 /*
1841 * Optimization: for pte sync, if spte was writable the hash
1842 * lookup is unnecessary (and expensive). Write protection
1843 * is responsibility of mmu_get_page / kvm_sync_page.
1844 * Same reasoning can be applied to dirty page accounting.
1845 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001846 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001847 goto set_pte;
1848
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001849 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001850 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001851 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001852 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001853 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001854 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001855 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001856 }
1857 }
1858
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001859 if (pte_access & ACC_WRITE_MASK)
1860 mark_page_dirty(vcpu->kvm, gfn);
1861
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001862set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001863 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001864 return ret;
1865}
1866
Avi Kivityd555c332009-06-10 14:24:23 +03001867static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001868 unsigned pt_access, unsigned pte_access,
1869 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001870 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001871 pfn_t pfn, bool speculative,
1872 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001873{
1874 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001875 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001876 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001877
1878 pgprintk("%s: spte %llx access %x write_fault %d"
1879 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001880 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001881 write_fault, user_fault, gfn);
1882
Avi Kivityd555c332009-06-10 14:24:23 +03001883 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001884 /*
1885 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1886 * the parent of the now unreachable PTE.
1887 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001888 if (level > PT_PAGE_TABLE_LEVEL &&
1889 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001890 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001891 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001892
1893 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001894 mmu_page_remove_parent_pte(child, sptep);
1895 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001896 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001897 spte_to_pfn(*sptep), pfn);
1898 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001899 } else
1900 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001901 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001902
Avi Kivityd555c332009-06-10 14:24:23 +03001903 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001904 dirty, level, gfn, pfn, speculative, true,
1905 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001906 if (write_fault)
1907 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001908 kvm_x86_ops->tlb_flush(vcpu);
1909 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001910
Avi Kivityd555c332009-06-10 14:24:23 +03001911 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001912 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001913 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001914 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1915 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001916 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001917 ++vcpu->kvm->stat.lpages;
1918
Avi Kivityd555c332009-06-10 14:24:23 +03001919 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001920 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001921 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001922 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001923 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001924 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001925 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001926 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001927 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001928 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001929 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001930 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001931 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001932 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001933 vcpu->arch.last_pte_gfn = gfn;
1934 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001935}
1936
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1938{
1939}
1940
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001941static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001942 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943{
Avi Kivity9f652d22008-12-25 14:54:25 +02001944 struct kvm_shadow_walk_iterator iterator;
1945 struct kvm_mmu_page *sp;
1946 int pt_write = 0;
1947 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948
Avi Kivity9f652d22008-12-25 14:54:25 +02001949 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001950 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001951 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1952 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001953 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001954 ++vcpu->stat.pf_fixed;
1955 break;
1956 }
1957
1958 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1959 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1960 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1961 iterator.level - 1,
1962 1, ACC_ALL, iterator.sptep);
1963 if (!sp) {
1964 pgprintk("nonpaging_map: ENOMEM\n");
1965 kvm_release_pfn_clean(pfn);
1966 return -ENOMEM;
1967 }
1968
Avi Kivityd555c332009-06-10 14:24:23 +03001969 __set_spte(iterator.sptep,
1970 __pa(sp->spt)
1971 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1972 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001973 }
1974 }
1975 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976}
1977
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001978static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1979{
1980 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001981 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001982 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001983 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001984
Joerg Roedel852e3c12009-07-27 16:30:44 +02001985 level = mapping_level(vcpu, gfn);
1986
1987 /*
1988 * This path builds a PAE pagetable - so we can map 2mb pages at
1989 * maximum. Therefore check if the level is larger than that.
1990 */
1991 if (level > PT_DIRECTORY_LEVEL)
1992 level = PT_DIRECTORY_LEVEL;
1993
1994 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001995
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001996 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001997 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001998 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001999
Avi Kivityd196e342008-01-24 11:44:11 +02002000 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05002001 if (is_error_pfn(pfn)) {
2002 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002003 return 1;
2004 }
2005
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002006 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002007 if (mmu_notifier_retry(vcpu, mmu_seq))
2008 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002009 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002010 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002011 spin_unlock(&vcpu->kvm->mmu_lock);
2012
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002013
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002014 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002015
2016out_unlock:
2017 spin_unlock(&vcpu->kvm->mmu_lock);
2018 kvm_release_pfn_clean(pfn);
2019 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002020}
2021
2022
Avi Kivity17ac10a2007-01-05 16:36:40 -08002023static void mmu_free_roots(struct kvm_vcpu *vcpu)
2024{
2025 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002026 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002027
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002028 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002029 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002030 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002031 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2032 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002033
Avi Kivity4db35312007-11-21 15:28:32 +02002034 sp = page_header(root);
2035 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002036 if (!sp->root_count && sp->role.invalid)
2037 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002038 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002039 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002040 return;
2041 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002042 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002043 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002044
Avi Kivity417726a2007-04-12 17:35:58 +03002045 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002046 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002047 sp = page_header(root);
2048 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002049 if (!sp->root_count && sp->role.invalid)
2050 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002051 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002053 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002054 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002055 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002056}
2057
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002058static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2059{
2060 int ret = 0;
2061
2062 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2063 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2064 ret = 1;
2065 }
2066
2067 return ret;
2068}
2069
2070static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002071{
2072 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002073 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002074 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002075 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002076 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002077
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002078 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002079
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002080 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2081 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002082
2083 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002084 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002085 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002086 if (mmu_check_root(vcpu, root_gfn))
2087 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002088 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002089 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002090 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002091 root = __pa(sp->spt);
2092 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002093 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002094 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002095 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002096 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002097 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002098 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002099 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002100 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002101
2102 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002103 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002104 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002105 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002106 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002107 continue;
2108 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002109 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002111 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002112 if (mmu_check_root(vcpu, root_gfn))
2113 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002114 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002115 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002116 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002117 root = __pa(sp->spt);
2118 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002119 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002120 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002121 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002122 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002123}
2124
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002125static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2126{
2127 int i;
2128 struct kvm_mmu_page *sp;
2129
2130 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2131 return;
2132 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2133 hpa_t root = vcpu->arch.mmu.root_hpa;
2134 sp = page_header(root);
2135 mmu_sync_children(vcpu, sp);
2136 return;
2137 }
2138 for (i = 0; i < 4; ++i) {
2139 hpa_t root = vcpu->arch.mmu.pae_root[i];
2140
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002141 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002142 root &= PT64_BASE_ADDR_MASK;
2143 sp = page_header(root);
2144 mmu_sync_children(vcpu, sp);
2145 }
2146 }
2147}
2148
2149void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2150{
2151 spin_lock(&vcpu->kvm->mmu_lock);
2152 mmu_sync_roots(vcpu);
2153 spin_unlock(&vcpu->kvm->mmu_lock);
2154}
2155
Gleb Natapov1871c602010-02-10 14:21:32 +02002156static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2157 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158{
Gleb Natapov1871c602010-02-10 14:21:32 +02002159 if (error)
2160 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161 return vaddr;
2162}
2163
2164static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002165 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166{
Avi Kivitye8332402007-12-09 18:43:00 +02002167 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002168 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002170 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002171 r = mmu_topup_memory_caches(vcpu);
2172 if (r)
2173 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002174
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002176 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177
Avi Kivitye8332402007-12-09 18:43:00 +02002178 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179
Avi Kivitye8332402007-12-09 18:43:00 +02002180 return nonpaging_map(vcpu, gva & PAGE_MASK,
2181 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182}
2183
Joerg Roedelfb72d162008-02-07 13:47:44 +01002184static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2185 u32 error_code)
2186{
Anthony Liguori35149e22008-04-02 14:46:56 -05002187 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002188 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002189 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002190 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002191 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002192
2193 ASSERT(vcpu);
2194 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2195
2196 r = mmu_topup_memory_caches(vcpu);
2197 if (r)
2198 return r;
2199
Joerg Roedel852e3c12009-07-27 16:30:44 +02002200 level = mapping_level(vcpu, gfn);
2201
2202 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2203
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002204 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002205 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002206 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002207 if (is_error_pfn(pfn)) {
2208 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002209 return 1;
2210 }
2211 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002212 if (mmu_notifier_retry(vcpu, mmu_seq))
2213 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002214 kvm_mmu_free_some_pages(vcpu);
2215 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002216 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002217 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002218
2219 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002220
2221out_unlock:
2222 spin_unlock(&vcpu->kvm->mmu_lock);
2223 kvm_release_pfn_clean(pfn);
2224 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002225}
2226
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227static void nonpaging_free(struct kvm_vcpu *vcpu)
2228{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002229 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230}
2231
2232static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2233{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002234 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235
2236 context->new_cr3 = nonpaging_new_cr3;
2237 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 context->gva_to_gpa = nonpaging_gva_to_gpa;
2239 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002240 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002241 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002242 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002243 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002245 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 return 0;
2247}
2248
Avi Kivityd835dfe2007-11-21 02:57:59 +02002249void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002251 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002252 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253}
2254
2255static void paging_new_cr3(struct kvm_vcpu *vcpu)
2256{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002257 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002258 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259}
2260
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261static void inject_page_fault(struct kvm_vcpu *vcpu,
2262 u64 addr,
2263 u32 err_code)
2264{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002265 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266}
2267
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268static void paging_free(struct kvm_vcpu *vcpu)
2269{
2270 nonpaging_free(vcpu);
2271}
2272
Dong, Eddie82725b22009-03-30 16:21:08 +08002273static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2274{
2275 int bit7;
2276
2277 bit7 = (gpte >> 7) & 1;
2278 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2279}
2280
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281#define PTTYPE 64
2282#include "paging_tmpl.h"
2283#undef PTTYPE
2284
2285#define PTTYPE 32
2286#include "paging_tmpl.h"
2287#undef PTTYPE
2288
Dong, Eddie82725b22009-03-30 16:21:08 +08002289static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2290{
2291 struct kvm_mmu *context = &vcpu->arch.mmu;
2292 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2293 u64 exb_bit_rsvd = 0;
2294
2295 if (!is_nx(vcpu))
2296 exb_bit_rsvd = rsvd_bits(63, 63);
2297 switch (level) {
2298 case PT32_ROOT_LEVEL:
2299 /* no rsvd bits for 2 level 4K page table entries */
2300 context->rsvd_bits_mask[0][1] = 0;
2301 context->rsvd_bits_mask[0][0] = 0;
2302 if (is_cpuid_PSE36())
2303 /* 36bits PSE 4MB page */
2304 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2305 else
2306 /* 32 bits PSE 4MB page */
2307 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002308 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002309 break;
2310 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002311 context->rsvd_bits_mask[0][2] =
2312 rsvd_bits(maxphyaddr, 63) |
2313 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002314 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002315 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002316 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2317 rsvd_bits(maxphyaddr, 62); /* PTE */
2318 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2319 rsvd_bits(maxphyaddr, 62) |
2320 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002321 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002322 break;
2323 case PT64_ROOT_LEVEL:
2324 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2325 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2326 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2327 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2328 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002329 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002330 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2331 rsvd_bits(maxphyaddr, 51);
2332 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002333 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2334 rsvd_bits(maxphyaddr, 51) |
2335 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002336 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002337 rsvd_bits(maxphyaddr, 51) |
2338 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002339 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002340 break;
2341 }
2342}
2343
Avi Kivity17ac10a2007-01-05 16:36:40 -08002344static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002346 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
2348 ASSERT(is_pae(vcpu));
2349 context->new_cr3 = paging_new_cr3;
2350 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002352 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002353 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002354 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002356 context->root_level = level;
2357 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002358 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 return 0;
2360}
2361
Avi Kivity17ac10a2007-01-05 16:36:40 -08002362static int paging64_init_context(struct kvm_vcpu *vcpu)
2363{
Dong, Eddie82725b22009-03-30 16:21:08 +08002364 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002365 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2366}
2367
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368static int paging32_init_context(struct kvm_vcpu *vcpu)
2369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002370 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371
Dong, Eddie82725b22009-03-30 16:21:08 +08002372 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 context->new_cr3 = paging_new_cr3;
2374 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375 context->gva_to_gpa = paging32_gva_to_gpa;
2376 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002377 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002378 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002379 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 context->root_level = PT32_ROOT_LEVEL;
2381 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002382 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 return 0;
2384}
2385
2386static int paging32E_init_context(struct kvm_vcpu *vcpu)
2387{
Dong, Eddie82725b22009-03-30 16:21:08 +08002388 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002389 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390}
2391
Joerg Roedelfb72d162008-02-07 13:47:44 +01002392static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2393{
2394 struct kvm_mmu *context = &vcpu->arch.mmu;
2395
2396 context->new_cr3 = nonpaging_new_cr3;
2397 context->page_fault = tdp_page_fault;
2398 context->free = nonpaging_free;
2399 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002400 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002401 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002402 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002403 context->root_hpa = INVALID_PAGE;
2404
2405 if (!is_paging(vcpu)) {
2406 context->gva_to_gpa = nonpaging_gva_to_gpa;
2407 context->root_level = 0;
2408 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002409 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002410 context->gva_to_gpa = paging64_gva_to_gpa;
2411 context->root_level = PT64_ROOT_LEVEL;
2412 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002413 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002414 context->gva_to_gpa = paging64_gva_to_gpa;
2415 context->root_level = PT32E_ROOT_LEVEL;
2416 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002417 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002418 context->gva_to_gpa = paging32_gva_to_gpa;
2419 context->root_level = PT32_ROOT_LEVEL;
2420 }
2421
2422 return 0;
2423}
2424
2425static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002427 int r;
2428
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002430 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431
2432 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002433 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002434 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002435 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002437 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002439 r = paging32_init_context(vcpu);
2440
2441 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2442
2443 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444}
2445
Joerg Roedelfb72d162008-02-07 13:47:44 +01002446static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2447{
Anthony Liguori35149e22008-04-02 14:46:56 -05002448 vcpu->arch.update_pte.pfn = bad_pfn;
2449
Joerg Roedelfb72d162008-02-07 13:47:44 +01002450 if (tdp_enabled)
2451 return init_kvm_tdp_mmu(vcpu);
2452 else
2453 return init_kvm_softmmu(vcpu);
2454}
2455
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2457{
2458 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002459 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2460 vcpu->arch.mmu.free(vcpu);
2461 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 }
2463}
2464
2465int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2466{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002467 destroy_kvm_mmu(vcpu);
2468 return init_kvm_mmu(vcpu);
2469}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002470EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002471
2472int kvm_mmu_load(struct kvm_vcpu *vcpu)
2473{
Avi Kivity714b93d2007-01-05 16:36:53 -08002474 int r;
2475
Avi Kivitye2dec932007-01-05 16:36:54 -08002476 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002477 if (r)
2478 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002479 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002480 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002481 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002482 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002483 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002484 if (r)
2485 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002486 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002487 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002488out:
2489 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002491EXPORT_SYMBOL_GPL(kvm_mmu_load);
2492
2493void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2494{
2495 mmu_free_roots(vcpu);
2496}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497
Avi Kivity09072da2007-05-01 14:16:52 +03002498static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002499 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002500 u64 *spte)
2501{
2502 u64 pte;
2503 struct kvm_mmu_page *child;
2504
2505 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002506 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002507 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002508 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002509 else {
2510 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002511 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002512 }
2513 }
Avi Kivityd555c332009-06-10 14:24:23 +03002514 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002515 if (is_large_pte(pte))
2516 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002517}
2518
Avi Kivity00284252007-05-01 16:53:31 +03002519static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002520 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002521 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002522 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002523{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002524 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002525 ++vcpu->kvm->stat.mmu_pde_zapped;
2526 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002527 }
Avi Kivity00284252007-05-01 16:53:31 +03002528
Avi Kivity4cee5762007-11-18 16:37:07 +02002529 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002530 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002531 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002532 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002533 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002534}
2535
Avi Kivity79539ce2007-11-21 02:06:21 +02002536static bool need_remote_flush(u64 old, u64 new)
2537{
2538 if (!is_shadow_present_pte(old))
2539 return false;
2540 if (!is_shadow_present_pte(new))
2541 return true;
2542 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2543 return true;
2544 old ^= PT64_NX_MASK;
2545 new ^= PT64_NX_MASK;
2546 return (old & ~new & PT64_PERM_MASK) != 0;
2547}
2548
2549static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2550{
2551 if (need_remote_flush(old, new))
2552 kvm_flush_remote_tlbs(vcpu->kvm);
2553 else
2554 kvm_mmu_flush_tlb(vcpu);
2555}
2556
Avi Kivity12b7d282007-09-23 14:10:49 +02002557static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2558{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002559 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002560
Sheng Yang7b523452008-04-25 21:13:50 +08002561 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002562}
2563
Avi Kivityd7824ff2007-12-30 12:29:05 +02002564static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2565 const u8 *new, int bytes)
2566{
2567 gfn_t gfn;
2568 int r;
2569 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002570 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002571
2572 if (bytes != 4 && bytes != 8)
2573 return;
2574
2575 /*
2576 * Assume that the pte write on a page table of the same type
2577 * as the current vcpu paging mode. This is nearly always true
2578 * (might be false while changing modes). Note it is verified later
2579 * by update_pte().
2580 */
2581 if (is_pae(vcpu)) {
2582 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2583 if ((bytes == 4) && (gpa % 4 == 0)) {
2584 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2585 if (r)
2586 return;
2587 memcpy((void *)&gpte + (gpa % 8), new, 4);
2588 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2589 memcpy((void *)&gpte, new, 8);
2590 }
2591 } else {
2592 if ((bytes == 4) && (gpa % 4 == 0))
2593 memcpy((void *)&gpte, new, 4);
2594 }
Avi Kivity43a37952009-06-10 14:12:05 +03002595 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002596 return;
2597 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002598
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002599 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002600 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002601 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002602
Anthony Liguori35149e22008-04-02 14:46:56 -05002603 if (is_error_pfn(pfn)) {
2604 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002605 return;
2606 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002607 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002608 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002609}
2610
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002611static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2612{
2613 u64 *spte = vcpu->arch.last_pte_updated;
2614
2615 if (spte
2616 && vcpu->arch.last_pte_gfn == gfn
2617 && shadow_accessed_mask
2618 && !(*spte & shadow_accessed_mask)
2619 && is_shadow_present_pte(*spte))
2620 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2621}
2622
Avi Kivity09072da2007-05-01 14:16:52 +03002623void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002624 const u8 *new, int bytes,
2625 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002626{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002627 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002628 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002629 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002630 struct hlist_head *bucket;
2631 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002632 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002633 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002634 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002635 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002636 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002637 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002638 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002639 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002640 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002641 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002642 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002643
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002644 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002645 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002646 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002647 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002648 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002649 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002650 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002651 if (guest_initiated) {
2652 if (gfn == vcpu->arch.last_pt_write_gfn
2653 && !last_updated_pte_accessed(vcpu)) {
2654 ++vcpu->arch.last_pt_write_count;
2655 if (vcpu->arch.last_pt_write_count >= 3)
2656 flooded = 1;
2657 } else {
2658 vcpu->arch.last_pt_write_gfn = gfn;
2659 vcpu->arch.last_pt_write_count = 1;
2660 vcpu->arch.last_pte_updated = NULL;
2661 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002662 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002663 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002664 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002665 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002666 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002667 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002668 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002669 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002670 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002671 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002672 /*
2673 * Misaligned accesses are too much trouble to fix
2674 * up; also, they usually indicate a page is not used
2675 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002676 *
2677 * If we're seeing too many writes to a page,
2678 * it may no longer be a page table, or we may be
2679 * forking, in which case it is better to unmap the
2680 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002681 */
2682 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002683 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002684 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2685 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002686 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002687 continue;
2688 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002689 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002690 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002691 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002692 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002693 page_offset <<= 1; /* 32->64 */
2694 /*
2695 * A 32-bit pde maps 4MB while the shadow pdes map
2696 * only 2MB. So we need to double the offset again
2697 * and zap two pdes instead of one.
2698 */
2699 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002700 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002701 page_offset <<= 1;
2702 npte = 2;
2703 }
Avi Kivityfce06572007-05-01 16:44:05 +03002704 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002705 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002706 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002707 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002708 }
Avi Kivity4db35312007-11-21 15:28:32 +02002709 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002710 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2711 gentry = 0;
2712 r = kvm_read_guest_atomic(vcpu->kvm,
2713 gpa & ~(u64)(pte_size - 1),
2714 &gentry, pte_size);
2715 new = (const void *)&gentry;
2716 if (r < 0)
2717 new = NULL;
2718 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002719 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002720 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002721 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002722 if (new)
2723 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002724 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002725 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002726 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002727 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002728 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002729 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002730 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2731 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2732 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002733 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002734}
2735
Avi Kivitya4360362007-01-05 16:36:45 -08002736int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2737{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002738 gpa_t gpa;
2739 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002740
Avi Kivity60f24782009-08-27 13:37:06 +03002741 if (tdp_enabled)
2742 return 0;
2743
Gleb Natapov1871c602010-02-10 14:21:32 +02002744 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002745
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002746 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002747 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002748 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002749 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002750}
Avi Kivity577bdc42008-07-19 08:57:05 +03002751EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002752
Avi Kivity22d95b12007-09-14 20:26:06 +03002753void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002754{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002755 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2756 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002757 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002758
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002759 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002760 struct kvm_mmu_page, link);
2761 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002762 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002763 }
2764}
Avi Kivityebeace82007-01-05 16:36:47 -08002765
Avi Kivity30677142007-10-28 18:48:59 +02002766int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2767{
2768 int r;
2769 enum emulation_result er;
2770
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002771 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002772 if (r < 0)
2773 goto out;
2774
2775 if (!r) {
2776 r = 1;
2777 goto out;
2778 }
2779
Avi Kivityb733bfb2007-10-28 18:52:05 +02002780 r = mmu_topup_memory_caches(vcpu);
2781 if (r)
2782 goto out;
2783
Avi Kivity851ba692009-08-24 11:10:17 +03002784 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002785
2786 switch (er) {
2787 case EMULATE_DONE:
2788 return 1;
2789 case EMULATE_DO_MMIO:
2790 ++vcpu->stat.mmio_exits;
2791 return 0;
2792 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002793 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2794 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002795 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002796 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002797 default:
2798 BUG();
2799 }
2800out:
Avi Kivity30677142007-10-28 18:48:59 +02002801 return r;
2802}
2803EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2804
Marcelo Tosattia7052892008-09-23 13:18:35 -03002805void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2806{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002807 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002808 kvm_mmu_flush_tlb(vcpu);
2809 ++vcpu->stat.invlpg;
2810}
2811EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2812
Joerg Roedel18552672008-02-07 13:47:41 +01002813void kvm_enable_tdp(void)
2814{
2815 tdp_enabled = true;
2816}
2817EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2818
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002819void kvm_disable_tdp(void)
2820{
2821 tdp_enabled = false;
2822}
2823EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2824
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825static void free_mmu_pages(struct kvm_vcpu *vcpu)
2826{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002827 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828}
2829
2830static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2831{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002832 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 int i;
2834
2835 ASSERT(vcpu);
2836
Avi Kivity17ac10a2007-01-05 16:36:40 -08002837 /*
2838 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2839 * Therefore we need to allocate shadow page tables in the first
2840 * 4GB of memory, which happens to fit the DMA32 zone.
2841 */
2842 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2843 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002844 return -ENOMEM;
2845
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002846 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002847 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002849
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851}
2852
Ingo Molnar8018c272006-12-29 16:50:01 -08002853int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002856 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857
Ingo Molnar8018c272006-12-29 16:50:01 -08002858 return alloc_mmu_pages(vcpu);
2859}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860
Ingo Molnar8018c272006-12-29 16:50:01 -08002861int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2862{
2863 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002864 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002865
Ingo Molnar8018c272006-12-29 16:50:01 -08002866 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867}
2868
2869void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2870{
2871 ASSERT(vcpu);
2872
2873 destroy_kvm_mmu(vcpu);
2874 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002875 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876}
2877
Avi Kivity90cb0522007-07-17 13:04:56 +03002878void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879{
Avi Kivity4db35312007-11-21 15:28:32 +02002880 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002882 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 int i;
2884 u64 *pt;
2885
Sheng Yang291f26b2008-10-16 17:30:57 +08002886 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 continue;
2888
Avi Kivity4db35312007-11-21 15:28:32 +02002889 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2891 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002892 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 }
Avi Kivity171d5952008-08-27 16:40:51 +03002895 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002897
Avi Kivity90cb0522007-07-17 13:04:56 +03002898void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002899{
Avi Kivity4db35312007-11-21 15:28:32 +02002900 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002901
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002902 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002903 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002904 if (kvm_mmu_zap_page(kvm, sp))
2905 node = container_of(kvm->arch.active_mmu_pages.next,
2906 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002907 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002908
Avi Kivity90cb0522007-07-17 13:04:56 +03002909 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002910}
2911
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002912static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002913{
2914 struct kvm_mmu_page *page;
2915
2916 page = container_of(kvm->arch.active_mmu_pages.prev,
2917 struct kvm_mmu_page, link);
2918 kvm_mmu_zap_page(kvm, page);
2919}
2920
2921static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2922{
2923 struct kvm *kvm;
2924 struct kvm *kvm_freed = NULL;
2925 int cache_count = 0;
2926
2927 spin_lock(&kvm_lock);
2928
2929 list_for_each_entry(kvm, &vm_list, vm_list) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002930 int npages, idx;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002931
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002932 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002933 spin_lock(&kvm->mmu_lock);
2934 npages = kvm->arch.n_alloc_mmu_pages -
2935 kvm->arch.n_free_mmu_pages;
2936 cache_count += npages;
2937 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2938 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2939 cache_count--;
2940 kvm_freed = kvm;
2941 }
2942 nr_to_scan--;
2943
2944 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002945 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002946 }
2947 if (kvm_freed)
2948 list_move_tail(&kvm_freed->vm_list, &vm_list);
2949
2950 spin_unlock(&kvm_lock);
2951
2952 return cache_count;
2953}
2954
2955static struct shrinker mmu_shrinker = {
2956 .shrink = mmu_shrink,
2957 .seeks = DEFAULT_SEEKS * 10,
2958};
2959
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002960static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002961{
2962 if (pte_chain_cache)
2963 kmem_cache_destroy(pte_chain_cache);
2964 if (rmap_desc_cache)
2965 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002966 if (mmu_page_header_cache)
2967 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002968}
2969
Izik Eidus3ee16c82008-03-30 15:17:21 +03002970void kvm_mmu_module_exit(void)
2971{
2972 mmu_destroy_caches();
2973 unregister_shrinker(&mmu_shrinker);
2974}
2975
Avi Kivityb5a33a72007-04-15 16:31:09 +03002976int kvm_mmu_module_init(void)
2977{
2978 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2979 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002980 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002981 if (!pte_chain_cache)
2982 goto nomem;
2983 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2984 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002985 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002986 if (!rmap_desc_cache)
2987 goto nomem;
2988
Avi Kivityd3d25b02007-05-30 12:34:53 +03002989 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2990 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002991 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002992 if (!mmu_page_header_cache)
2993 goto nomem;
2994
Izik Eidus3ee16c82008-03-30 15:17:21 +03002995 register_shrinker(&mmu_shrinker);
2996
Avi Kivityb5a33a72007-04-15 16:31:09 +03002997 return 0;
2998
2999nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003000 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003001 return -ENOMEM;
3002}
3003
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003004/*
3005 * Caculate mmu pages needed for kvm.
3006 */
3007unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3008{
3009 int i;
3010 unsigned int nr_mmu_pages;
3011 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003012 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003013
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003014 slots = rcu_dereference(kvm->memslots);
3015 for (i = 0; i < slots->nmemslots; i++)
3016 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003017
3018 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3019 nr_mmu_pages = max(nr_mmu_pages,
3020 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3021
3022 return nr_mmu_pages;
3023}
3024
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003025static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3026 unsigned len)
3027{
3028 if (len > buffer->len)
3029 return NULL;
3030 return buffer->ptr;
3031}
3032
3033static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3034 unsigned len)
3035{
3036 void *ret;
3037
3038 ret = pv_mmu_peek_buffer(buffer, len);
3039 if (!ret)
3040 return ret;
3041 buffer->ptr += len;
3042 buffer->len -= len;
3043 buffer->processed += len;
3044 return ret;
3045}
3046
3047static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3048 gpa_t addr, gpa_t value)
3049{
3050 int bytes = 8;
3051 int r;
3052
3053 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3054 bytes = 4;
3055
3056 r = mmu_topup_memory_caches(vcpu);
3057 if (r)
3058 return r;
3059
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003060 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003061 return -EFAULT;
3062
3063 return 1;
3064}
3065
3066static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3067{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003068 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003069 return 1;
3070}
3071
3072static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3073{
3074 spin_lock(&vcpu->kvm->mmu_lock);
3075 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3076 spin_unlock(&vcpu->kvm->mmu_lock);
3077 return 1;
3078}
3079
3080static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3081 struct kvm_pv_mmu_op_buffer *buffer)
3082{
3083 struct kvm_mmu_op_header *header;
3084
3085 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3086 if (!header)
3087 return 0;
3088 switch (header->op) {
3089 case KVM_MMU_OP_WRITE_PTE: {
3090 struct kvm_mmu_op_write_pte *wpte;
3091
3092 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3093 if (!wpte)
3094 return 0;
3095 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3096 wpte->pte_val);
3097 }
3098 case KVM_MMU_OP_FLUSH_TLB: {
3099 struct kvm_mmu_op_flush_tlb *ftlb;
3100
3101 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3102 if (!ftlb)
3103 return 0;
3104 return kvm_pv_mmu_flush_tlb(vcpu);
3105 }
3106 case KVM_MMU_OP_RELEASE_PT: {
3107 struct kvm_mmu_op_release_pt *rpt;
3108
3109 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3110 if (!rpt)
3111 return 0;
3112 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3113 }
3114 default: return 0;
3115 }
3116}
3117
3118int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3119 gpa_t addr, unsigned long *ret)
3120{
3121 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003122 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003123
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003124 buffer->ptr = buffer->buf;
3125 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3126 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003127
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003128 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003129 if (r)
3130 goto out;
3131
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003132 while (buffer->len) {
3133 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003134 if (r < 0)
3135 goto out;
3136 if (r == 0)
3137 break;
3138 }
3139
3140 r = 1;
3141out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003142 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003143 return r;
3144}
3145
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003146int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3147{
3148 struct kvm_shadow_walk_iterator iterator;
3149 int nr_sptes = 0;
3150
3151 spin_lock(&vcpu->kvm->mmu_lock);
3152 for_each_shadow_entry(vcpu, addr, iterator) {
3153 sptes[iterator.level-1] = *iterator.sptep;
3154 nr_sptes++;
3155 if (!is_shadow_present_pte(*iterator.sptep))
3156 break;
3157 }
3158 spin_unlock(&vcpu->kvm->mmu_lock);
3159
3160 return nr_sptes;
3161}
3162EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3163
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003164#ifdef AUDIT
3165
3166static const char *audit_msg;
3167
3168static gva_t canonicalize(gva_t gva)
3169{
3170#ifdef CONFIG_X86_64
3171 gva = (long long)(gva << 16) >> 16;
3172#endif
3173 return gva;
3174}
3175
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003176
3177typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3178 u64 *sptep);
3179
3180static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3181 inspect_spte_fn fn)
3182{
3183 int i;
3184
3185 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3186 u64 ent = sp->spt[i];
3187
3188 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003189 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003190 struct kvm_mmu_page *child;
3191 child = page_header(ent & PT64_BASE_ADDR_MASK);
3192 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003193 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003194 fn(kvm, sp, &sp->spt[i]);
3195 }
3196 }
3197}
3198
3199static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3200{
3201 int i;
3202 struct kvm_mmu_page *sp;
3203
3204 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3205 return;
3206 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3207 hpa_t root = vcpu->arch.mmu.root_hpa;
3208 sp = page_header(root);
3209 __mmu_spte_walk(vcpu->kvm, sp, fn);
3210 return;
3211 }
3212 for (i = 0; i < 4; ++i) {
3213 hpa_t root = vcpu->arch.mmu.pae_root[i];
3214
3215 if (root && VALID_PAGE(root)) {
3216 root &= PT64_BASE_ADDR_MASK;
3217 sp = page_header(root);
3218 __mmu_spte_walk(vcpu->kvm, sp, fn);
3219 }
3220 }
3221 return;
3222}
3223
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003224static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3225 gva_t va, int level)
3226{
3227 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3228 int i;
3229 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3230
3231 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3232 u64 ent = pt[i];
3233
Avi Kivityc7addb92007-09-16 18:58:32 +02003234 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003235 continue;
3236
3237 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003238 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3239 audit_mappings_page(vcpu, ent, va, level - 1);
3240 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003241 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003242 gfn_t gfn = gpa >> PAGE_SHIFT;
3243 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3244 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003245
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003246 if (is_error_pfn(pfn)) {
3247 kvm_release_pfn_clean(pfn);
3248 continue;
3249 }
3250
Avi Kivityc7addb92007-09-16 18:58:32 +02003251 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003252 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003253 printk(KERN_ERR "xx audit error: (%s) levels %d"
3254 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003255 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003256 va, gpa, hpa, ent,
3257 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003258 else if (ent == shadow_notrap_nonpresent_pte
3259 && !is_error_hpa(hpa))
3260 printk(KERN_ERR "audit: (%s) notrap shadow,"
3261 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003262 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003263
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003264 }
3265 }
3266}
3267
3268static void audit_mappings(struct kvm_vcpu *vcpu)
3269{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003270 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003271
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003272 if (vcpu->arch.mmu.root_level == 4)
3273 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003274 else
3275 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003276 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003277 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003278 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003279 i << 30,
3280 2);
3281}
3282
3283static int count_rmaps(struct kvm_vcpu *vcpu)
3284{
3285 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003286 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003287
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003288 idx = srcu_read_lock(&kvm->srcu);
3289 slots = rcu_dereference(kvm->memslots);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003290 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003291 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003292 struct kvm_rmap_desc *d;
3293
3294 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003295 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003296
Izik Eidus290fc382007-09-27 14:11:22 +02003297 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003298 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003299 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003300 ++nmaps;
3301 continue;
3302 }
Izik Eidus290fc382007-09-27 14:11:22 +02003303 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003304 while (d) {
3305 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003306 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003307 ++nmaps;
3308 else
3309 break;
3310 d = d->more;
3311 }
3312 }
3313 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003314 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315 return nmaps;
3316}
3317
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003318void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003319{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003320 unsigned long *rmapp;
3321 struct kvm_mmu_page *rev_sp;
3322 gfn_t gfn;
3323
3324 if (*sptep & PT_WRITABLE_MASK) {
3325 rev_sp = page_header(__pa(sptep));
3326 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3327
3328 if (!gfn_to_memslot(kvm, gfn)) {
3329 if (!printk_ratelimit())
3330 return;
3331 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3332 audit_msg, gfn);
3333 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3334 audit_msg, sptep - rev_sp->spt,
3335 rev_sp->gfn);
3336 dump_stack();
3337 return;
3338 }
3339
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003340 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3341 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003342 if (!*rmapp) {
3343 if (!printk_ratelimit())
3344 return;
3345 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3346 audit_msg, *sptep);
3347 dump_stack();
3348 }
3349 }
3350
3351}
3352
3353void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3354{
3355 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3356}
3357
3358static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3359{
Avi Kivity4db35312007-11-21 15:28:32 +02003360 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003361 int i;
3362
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003363 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003364 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003365
Avi Kivity4db35312007-11-21 15:28:32 +02003366 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367 continue;
3368
3369 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3370 u64 ent = pt[i];
3371
3372 if (!(ent & PT_PRESENT_MASK))
3373 continue;
3374 if (!(ent & PT_WRITABLE_MASK))
3375 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003376 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003377 }
3378 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003379 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003380}
3381
3382static void audit_rmap(struct kvm_vcpu *vcpu)
3383{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003384 check_writable_mappings_rmap(vcpu);
3385 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003386}
3387
3388static void audit_write_protection(struct kvm_vcpu *vcpu)
3389{
Avi Kivity4db35312007-11-21 15:28:32 +02003390 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003391 struct kvm_memory_slot *slot;
3392 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003393 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003394 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003395
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003396 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003397 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003398 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003399 if (sp->unsync)
3400 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003401
Avi Kivity4db35312007-11-21 15:28:32 +02003402 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003403 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003404 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003405
3406 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3407 while (spte) {
3408 if (*spte & PT_WRITABLE_MASK)
3409 printk(KERN_ERR "%s: (%s) shadow page has "
3410 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003411 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003412 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003413 spte = rmap_next(vcpu->kvm, rmapp, spte);
3414 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003415 }
3416}
3417
3418static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3419{
3420 int olddbg = dbg;
3421
3422 dbg = 0;
3423 audit_msg = msg;
3424 audit_rmap(vcpu);
3425 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003426 if (strcmp("pre pte write", audit_msg) != 0)
3427 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003428 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003429 dbg = olddbg;
3430}
3431
3432#endif