blob: c2ac7004441ae323f77a8992121cdcfba4d41607 [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.
Avi Kivity221d0592010-05-23 18:37:00 +030010 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020022#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030023#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/highmem.h>
30#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020031#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030032#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050033#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020034#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080036#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
38#include <asm/page.h>
39#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020040#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020041#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040042
Joerg Roedel18552672008-02-07 13:47:41 +010043/*
44 * When setting this variable to true it enables Two-Dimensional-Paging
45 * where the hardware walks 2 page tables:
46 * 1. the guest-virtual to guest-physical
47 * 2. while doing 1. it walks guest-physical to host-physical
48 * If the hardware supports that we don't need to do shadow paging.
49 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050050bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010051
Avi Kivity37a7d8b2007-01-05 16:36:56 -080052#undef MMU_DEBUG
53
54#undef AUDIT
55
56#ifdef AUDIT
57static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
58#else
59static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
60#endif
61
62#ifdef MMU_DEBUG
63
64#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
65#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
66
67#else
68
69#define pgprintk(x...) do { } while (0)
70#define rmap_printk(x...) do { } while (0)
71
72#endif
73
74#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030075static int dbg = 0;
76module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080077#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Marcelo Tosatti582801a2008-09-23 13:18:41 -030079static int oos_shadow = 1;
80module_param(oos_shadow, bool, 0644);
81
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080082#ifndef MMU_DEBUG
83#define ASSERT(x) do { } while (0)
84#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080085#define ASSERT(x) \
86 if (!(x)) { \
87 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
88 __FILE__, __LINE__, #x); \
89 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080090#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
Avi Kivity6aa8b732006-12-10 02:21:36 -080092#define PT_FIRST_AVAIL_BITS_SHIFT 9
93#define PT64_SECOND_AVAIL_BITS_SHIFT 52
94
Avi Kivity6aa8b732006-12-10 02:21:36 -080095#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
Avi Kivity07420172009-07-06 12:21:32 +0300151#define CREATE_TRACE_POINTS
152#include "mmutrace.h"
153
Izik Eidus14032832009-09-23 21:47:17 +0300154#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
155
Avi Kivity135f8c22008-08-21 17:49:56 +0300156#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
157
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800158struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300159 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160 struct kvm_rmap_desc *more;
161};
162
Avi Kivity2d111232008-12-25 14:39:47 +0200163struct kvm_shadow_walk_iterator {
164 u64 addr;
165 hpa_t shadow_addr;
166 int level;
167 u64 *sptep;
168 unsigned index;
169};
170
171#define for_each_shadow_entry(_vcpu, _addr, _walker) \
172 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
173 shadow_walk_okay(&(_walker)); \
174 shadow_walk_next(&(_walker)))
175
Xiao Guangrong1047df12010-06-11 21:35:15 +0800176typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300177
Avi Kivityb5a33a72007-04-15 16:31:09 +0300178static struct kmem_cache *pte_chain_cache;
179static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300180static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700181static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300182
Avi Kivityc7addb92007-09-16 18:58:32 +0200183static u64 __read_mostly shadow_trap_nonpresent_pte;
184static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800185static u64 __read_mostly shadow_base_present_pte;
186static u64 __read_mostly shadow_nx_mask;
187static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
188static u64 __read_mostly shadow_user_mask;
189static u64 __read_mostly shadow_accessed_mask;
190static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200191
Dong, Eddie82725b22009-03-30 16:21:08 +0800192static inline u64 rsvd_bits(int s, int e)
193{
194 return ((1ULL << (e - s + 1)) - 1) << s;
195}
196
Avi Kivityc7addb92007-09-16 18:58:32 +0200197void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
198{
199 shadow_trap_nonpresent_pte = trap_pte;
200 shadow_notrap_nonpresent_pte = notrap_pte;
201}
202EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
203
Sheng Yang7b523452008-04-25 21:13:50 +0800204void kvm_mmu_set_base_ptes(u64 base_pte)
205{
206 shadow_base_present_pte = base_pte;
207}
208EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
209
210void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800211 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800212{
213 shadow_user_mask = user_mask;
214 shadow_accessed_mask = accessed_mask;
215 shadow_dirty_mask = dirty_mask;
216 shadow_nx_mask = nx_mask;
217 shadow_x_mask = x_mask;
218}
219EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
220
Avi Kivity3dbe1412010-05-12 11:48:18 +0300221static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200223 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224}
225
226static int is_cpuid_PSE36(void)
227{
228 return 1;
229}
230
Avi Kivity73b10872007-01-26 00:56:41 -0800231static int is_nx(struct kvm_vcpu *vcpu)
232{
Avi Kivityf6801df2010-01-21 15:31:50 +0200233 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800234}
235
Avi Kivityc7addb92007-09-16 18:58:32 +0200236static int is_shadow_present_pte(u64 pte)
237{
Avi Kivityc7addb92007-09-16 18:58:32 +0200238 return pte != shadow_trap_nonpresent_pte
239 && pte != shadow_notrap_nonpresent_pte;
240}
241
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300242static int is_large_pte(u64 pte)
243{
244 return pte & PT_PAGE_SIZE_MASK;
245}
246
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900247static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248{
249 return pte & PT_WRITABLE_MASK;
250}
251
Avi Kivity43a37952009-06-10 14:12:05 +0300252static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200253{
Avi Kivity439e2182009-06-10 12:56:54 +0300254 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200255}
256
Avi Kivity43a37952009-06-10 14:12:05 +0300257static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800258{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200259 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800260}
261
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300262static int is_last_spte(u64 pte, int level)
263{
264 if (level == PT_PAGE_TABLE_LEVEL)
265 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200266 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300267 return 1;
268 return 0;
269}
270
Anthony Liguori35149e22008-04-02 14:46:56 -0500271static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200272{
Anthony Liguori35149e22008-04-02 14:46:56 -0500273 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200274}
275
Avi Kivityda928522007-11-21 13:54:47 +0200276static gfn_t pse36_gfn_delta(u32 gpte)
277{
278 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
279
280 return (gpte & PT32_DIR_PSE36_MASK) << shift;
281}
282
Avi Kivityd555c332009-06-10 14:24:23 +0300283static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300284{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700285 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300286}
287
Avi Kivitya9221dd2010-06-06 14:48:06 +0300288static u64 __xchg_spte(u64 *sptep, u64 new_spte)
289{
290#ifdef CONFIG_X86_64
291 return xchg(sptep, new_spte);
292#else
293 u64 old_spte;
294
295 do {
296 old_spte = *sptep;
297 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
298
299 return old_spte;
300#endif
301}
302
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800303static bool spte_has_volatile_bits(u64 spte)
304{
305 if (!shadow_accessed_mask)
306 return false;
307
308 if (!is_shadow_present_pte(spte))
309 return false;
310
Xiao Guangrong41327792010-08-02 16:15:08 +0800311 if ((spte & shadow_accessed_mask) &&
312 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800313 return false;
314
315 return true;
316}
317
Xiao Guangrong41327792010-08-02 16:15:08 +0800318static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
319{
320 return (old_spte & bit_mask) && !(new_spte & bit_mask);
321}
322
Avi Kivityb79b93f2010-06-06 15:46:44 +0300323static void update_spte(u64 *sptep, u64 new_spte)
324{
Xiao Guangrong41327792010-08-02 16:15:08 +0800325 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300326
Xiao Guangrong41327792010-08-02 16:15:08 +0800327 WARN_ON(!is_rmap_spte(new_spte));
328
329 new_spte |= old_spte & shadow_dirty_mask;
330
331 mask = shadow_accessed_mask;
332 if (is_writable_pte(old_spte))
333 mask |= shadow_dirty_mask;
334
335 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300336 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800337 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300338 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800339
340 if (!shadow_accessed_mask)
341 return;
342
343 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
344 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
345 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
346 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300347}
348
Avi Kivitye2dec932007-01-05 16:36:54 -0800349static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300350 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800351{
352 void *obj;
353
354 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800355 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800356 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300357 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800358 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800359 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800360 cache->objects[cache->nobjs++] = obj;
361 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800362 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800363}
364
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800365static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
366 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800367{
368 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800369 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800370}
371
Avi Kivityc1158e62007-07-20 08:18:27 +0300372static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300373 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300374{
375 struct page *page;
376
377 if (cache->nobjs >= min)
378 return 0;
379 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300380 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300381 if (!page)
382 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300383 cache->objects[cache->nobjs++] = page_address(page);
384 }
385 return 0;
386}
387
388static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
389{
390 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300391 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300392}
393
Avi Kivity8c438502007-04-16 11:53:17 +0300394static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
395{
396 int r;
397
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800398 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300399 pte_chain_cache, 4);
400 if (r)
401 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800402 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200403 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300404 if (r)
405 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800406 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300407 if (r)
408 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800409 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300410 mmu_page_header_cache, 4);
411out:
Avi Kivity8c438502007-04-16 11:53:17 +0300412 return r;
413}
414
Avi Kivity714b93d2007-01-05 16:36:53 -0800415static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
416{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800417 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
418 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800419 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800420 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
421 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800422}
423
424static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
425 size_t size)
426{
427 void *p;
428
429 BUG_ON(!mc->nobjs);
430 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800431 return p;
432}
433
Avi Kivity714b93d2007-01-05 16:36:53 -0800434static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
435{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800436 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800437 sizeof(struct kvm_pte_chain));
438}
439
Avi Kivity90cb0522007-07-17 13:04:56 +0300440static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800441{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800442 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800443}
444
445static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
446{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800447 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800448 sizeof(struct kvm_rmap_desc));
449}
450
Avi Kivity90cb0522007-07-17 13:04:56 +0300451static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800452{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800453 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800454}
455
Lai Jiangshan2032a932010-05-26 16:49:59 +0800456static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
457{
458 if (!sp->role.direct)
459 return sp->gfns[index];
460
461 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
462}
463
464static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
465{
466 if (sp->role.direct)
467 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
468 else
469 sp->gfns[index] = gfn;
470}
471
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800472/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300473 * Return the pointer to the largepage write count for a given
474 * gfn, handling slots that are not large page aligned.
475 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200476static int *slot_largepage_idx(gfn_t gfn,
477 struct kvm_memory_slot *slot,
478 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479{
480 unsigned long idx;
481
Joerg Roedel82855412010-07-01 16:00:11 +0200482 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
483 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Joerg Roedeld25797b2009-07-27 16:30:43 +0200484 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300485}
486
487static void account_shadowed(struct kvm *kvm, gfn_t gfn)
488{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200489 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300490 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200491 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300493 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200494 for (i = PT_DIRECTORY_LEVEL;
495 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
496 write_count = slot_largepage_idx(gfn, slot, i);
497 *write_count += 1;
498 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499}
500
501static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
502{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200503 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300504 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300507 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200508 for (i = PT_DIRECTORY_LEVEL;
509 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200510 write_count = slot_largepage_idx(gfn, slot, i);
511 *write_count -= 1;
512 WARN_ON(*write_count < 0);
513 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300514}
515
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516static int has_wrprotected_page(struct kvm *kvm,
517 gfn_t gfn,
518 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300519{
Izik Eidus28430992008-10-03 17:40:32 +0300520 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300521 int *largepage_idx;
522
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300523 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300524 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200525 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300526 return *largepage_idx;
527 }
528
529 return 1;
530}
531
Joerg Roedeld25797b2009-07-27 16:30:43 +0200532static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300533{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100534 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200535 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300536
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100537 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300538
Joerg Roedeld25797b2009-07-27 16:30:43 +0200539 for (i = PT_PAGE_TABLE_LEVEL;
540 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
541 if (page_size >= KVM_HPAGE_SIZE(i))
542 ret = i;
543 else
544 break;
545 }
546
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300547 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300548}
549
Joerg Roedeld25797b2009-07-27 16:30:43 +0200550static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300551{
552 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800553 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300554
555 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
556 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200557 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300558
Joerg Roedeld25797b2009-07-27 16:30:43 +0200559 host_level = host_mapping_level(vcpu->kvm, large_gfn);
560
561 if (host_level == PT_PAGE_TABLE_LEVEL)
562 return host_level;
563
Sheng Yang878403b2010-01-05 19:02:29 +0800564 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
565 kvm_x86_ops->get_lpage_level() : host_level;
566
567 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200568 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
569 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200570
571 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300572}
573
574/*
Izik Eidus290fc382007-09-27 14:11:22 +0200575 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200576 */
577
Joerg Roedel44ad9942009-07-27 16:30:42 +0200578static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200579{
580 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300581 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200582
583 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200584 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300585 return &slot->rmap[gfn - slot->base_gfn];
586
Joerg Roedel82855412010-07-01 16:00:11 +0200587 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
588 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300589
Joerg Roedel44ad9942009-07-27 16:30:42 +0200590 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200591}
592
593/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 * Reverse mapping data structures:
595 *
Izik Eidus290fc382007-09-27 14:11:22 +0200596 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
597 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800598 *
Izik Eidus290fc382007-09-27 14:11:22 +0200599 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
600 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300601 *
602 * Returns the number of rmap entries before the spte was added or zero if
603 * the spte was not added.
604 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800605 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200606static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800607{
Avi Kivity4db35312007-11-21 15:28:32 +0200608 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200610 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300611 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800612
Avi Kivity43a37952009-06-10 14:12:05 +0300613 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300614 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200615 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800616 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200617 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200618 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200620 *rmapp = (unsigned long)spte;
621 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800622 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800623 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300624 desc->sptes[0] = (u64 *)*rmapp;
625 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200626 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800627 } else {
628 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200629 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300630 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800631 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300632 count += RMAP_EXT;
633 }
Avi Kivityd555c332009-06-10 14:24:23 +0300634 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800635 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 desc = desc->more;
637 }
Avi Kivityd555c332009-06-10 14:24:23 +0300638 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300640 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800641 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300642 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643}
644
Izik Eidus290fc382007-09-27 14:11:22 +0200645static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800646 struct kvm_rmap_desc *desc,
647 int i,
648 struct kvm_rmap_desc *prev_desc)
649{
650 int j;
651
Avi Kivityd555c332009-06-10 14:24:23 +0300652 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800653 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300654 desc->sptes[i] = desc->sptes[j];
655 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800656 if (j != 0)
657 return;
658 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300659 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800660 else
661 if (prev_desc)
662 prev_desc->more = desc->more;
663 else
Izik Eidus290fc382007-09-27 14:11:22 +0200664 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300665 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800666}
667
Izik Eidus290fc382007-09-27 14:11:22 +0200668static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800669{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800670 struct kvm_rmap_desc *desc;
671 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200672 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800673 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200674 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800675 int i;
676
Avi Kivity4db35312007-11-21 15:28:32 +0200677 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800678 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
679 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200680 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800681 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800682 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200683 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800684 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200685 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800686 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800687 BUG();
688 }
Izik Eidus290fc382007-09-27 14:11:22 +0200689 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800690 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800691 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200692 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800693 prev_desc = NULL;
694 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300695 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
696 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200697 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800698 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800699 prev_desc);
700 return;
701 }
702 prev_desc = desc;
703 desc = desc->more;
704 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800705 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800706 BUG();
707 }
708}
709
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800710static void set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300711{
Avi Kivityce061862010-06-06 14:38:12 +0300712 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800713 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300714
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800715 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800716 __set_spte(sptep, new_spte);
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800717 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800718 old_spte = __xchg_spte(sptep, new_spte);
719
Avi Kivitya9221dd2010-06-06 14:48:06 +0300720 if (!is_rmap_spte(old_spte))
Avi Kivityce061862010-06-06 14:38:12 +0300721 return;
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800722
Avi Kivitya9221dd2010-06-06 14:48:06 +0300723 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800724 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300725 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800726 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300727 kvm_set_pfn_dirty(pfn);
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800728}
729
730static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
731{
732 set_spte_track_bits(sptep, new_spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300733 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300734}
735
Izik Eidus98348e92007-10-16 14:42:30 +0200736static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800737{
Avi Kivity374cbac2007-01-05 16:36:43 -0800738 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200739 u64 *prev_spte;
740 int i;
741
742 if (!*rmapp)
743 return NULL;
744 else if (!(*rmapp & 1)) {
745 if (!spte)
746 return (u64 *)*rmapp;
747 return NULL;
748 }
749 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200750 prev_spte = NULL;
751 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300752 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200753 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300754 return desc->sptes[i];
755 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200756 }
757 desc = desc->more;
758 }
759 return NULL;
760}
761
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200762static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200763{
Izik Eidus290fc382007-09-27 14:11:22 +0200764 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800765 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200766 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800767
Joerg Roedel44ad9942009-07-27 16:30:42 +0200768 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800769
Izik Eidus98348e92007-10-16 14:42:30 +0200770 spte = rmap_next(kvm, rmapp, NULL);
771 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800772 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800773 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800774 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900775 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300776 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800777 write_protected = 1;
778 }
Izik Eidus9647c142007-10-16 14:43:46 +0200779 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800780 }
Izik Eidus855149a2008-03-20 18:17:24 +0200781
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300782 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200783 for (i = PT_DIRECTORY_LEVEL;
784 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
785 rmapp = gfn_to_rmap(kvm, gfn, i);
786 spte = rmap_next(kvm, rmapp, NULL);
787 while (spte) {
788 BUG_ON(!spte);
789 BUG_ON(!(*spte & PT_PRESENT_MASK));
790 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
791 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900792 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300793 drop_spte(kvm, spte,
794 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200795 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200796 spte = NULL;
797 write_protected = 1;
798 }
799 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300800 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300801 }
802
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200803 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800804}
805
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000806static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
807 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200808{
809 u64 *spte;
810 int need_tlb_flush = 0;
811
812 while ((spte = rmap_next(kvm, rmapp, NULL))) {
813 BUG_ON(!(*spte & PT_PRESENT_MASK));
814 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300815 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816 need_tlb_flush = 1;
817 }
818 return need_tlb_flush;
819}
820
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000821static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
822 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300823{
824 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800825 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300826 pte_t *ptep = (pte_t *)data;
827 pfn_t new_pfn;
828
829 WARN_ON(pte_huge(*ptep));
830 new_pfn = pte_pfn(*ptep);
831 spte = rmap_next(kvm, rmapp, NULL);
832 while (spte) {
833 BUG_ON(!is_shadow_present_pte(*spte));
834 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
835 need_flush = 1;
836 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300837 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300838 spte = rmap_next(kvm, rmapp, NULL);
839 } else {
840 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
841 new_spte |= (u64)new_pfn << PAGE_SHIFT;
842
843 new_spte &= ~PT_WRITABLE_MASK;
844 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300845 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800846 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300847 spte = rmap_next(kvm, rmapp, spte);
848 }
849 }
850 if (need_flush)
851 kvm_flush_remote_tlbs(kvm);
852
853 return 0;
854}
855
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000856static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
857 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300858 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000859 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200860{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200861 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200862 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200863 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200864 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200865
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800866 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200867
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200868 for (i = 0; i < slots->nmemslots; i++) {
869 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200870 unsigned long start = memslot->userspace_addr;
871 unsigned long end;
872
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200873 end = start + (memslot->npages << PAGE_SHIFT);
874 if (hva >= start && hva < end) {
875 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200876
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200877 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200878
879 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200880 unsigned long idx;
881 int sh;
882
883 sh = KVM_HPAGE_GFN_SHIFT(PT_DIRECTORY_LEVEL+j);
884 idx = ((memslot->base_gfn+gfn_offset) >> sh) -
885 (memslot->base_gfn >> sh);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200886 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300887 &memslot->lpage_info[j][idx].rmap_pde,
888 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200889 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200890 trace_kvm_age_page(hva, memslot, ret);
891 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200892 }
893 }
894
895 return retval;
896}
897
898int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
899{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300900 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200901}
902
Izik Eidus3da0dd42009-09-23 21:47:18 +0300903void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
904{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000905 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300906}
907
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000908static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
909 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200910{
911 u64 *spte;
912 int young = 0;
913
Rik van Riel6316e1c2010-02-03 16:11:03 -0500914 /*
915 * Emulate the accessed bit for EPT, by checking if this page has
916 * an EPT mapping, and clearing it if it does. On the next access,
917 * a new EPT mapping will be established.
918 * This has some overhead, but not as much as the cost of swapping
919 * out actively used pages or breaking up actively used hugepages.
920 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800921 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500922 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800923
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200924 spte = rmap_next(kvm, rmapp, NULL);
925 while (spte) {
926 int _young;
927 u64 _spte = *spte;
928 BUG_ON(!(_spte & PT_PRESENT_MASK));
929 _young = _spte & PT_ACCESSED_MASK;
930 if (_young) {
931 young = 1;
932 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
933 }
934 spte = rmap_next(kvm, rmapp, spte);
935 }
936 return young;
937}
938
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300939#define RMAP_RECYCLE_THRESHOLD 1000
940
Joerg Roedel852e3c12009-07-27 16:30:44 +0200941static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300942{
943 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200944 struct kvm_mmu_page *sp;
945
946 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300947
Joerg Roedel852e3c12009-07-27 16:30:44 +0200948 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300949
Izik Eidus3da0dd42009-09-23 21:47:18 +0300950 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300951 kvm_flush_remote_tlbs(vcpu->kvm);
952}
953
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200954int kvm_age_hva(struct kvm *kvm, unsigned long hva)
955{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300956 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200957}
958
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800959#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300960static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961{
Avi Kivity139bdb22007-01-05 16:36:50 -0800962 u64 *pos;
963 u64 *end;
964
Avi Kivity47ad8e62007-05-06 15:50:58 +0300965 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300966 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800967 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800968 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800970 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 return 1;
972}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800973#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974
Dave Hansen45221ab2010-08-19 18:11:37 -0700975/*
976 * This value is the sum of all of the kvm instances's
977 * kvm->arch.n_used_mmu_pages values. We need a global,
978 * aggregate version in order to make the slab shrinker
979 * faster
980 */
981static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
982{
983 kvm->arch.n_used_mmu_pages += nr;
984 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
985}
986
Avi Kivity4db35312007-11-21 15:28:32 +0200987static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800988{
Avi Kivity4db35312007-11-21 15:28:32 +0200989 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800990 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200991 list_del(&sp->link);
992 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800993 if (!sp->role.direct)
994 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800995 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -0700996 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -0800997}
998
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800999static unsigned kvm_page_table_hashfn(gfn_t gfn)
1000{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001001 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001002}
1003
Avi Kivity25c0de22007-01-05 16:36:42 -08001004static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001005 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006{
Avi Kivity4db35312007-11-21 15:28:32 +02001007 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001009 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1010 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001011 if (!direct)
1012 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1013 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001014 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001015 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001016 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001017 sp->multimapped = 0;
1018 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001019 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001020 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021}
1022
Avi Kivity714b93d2007-01-05 16:36:53 -08001023static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001024 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001025{
1026 struct kvm_pte_chain *pte_chain;
1027 struct hlist_node *node;
1028 int i;
1029
1030 if (!parent_pte)
1031 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001032 if (!sp->multimapped) {
1033 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001034
1035 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001036 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001037 return;
1038 }
Avi Kivity4db35312007-11-21 15:28:32 +02001039 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001040 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001041 INIT_HLIST_HEAD(&sp->parent_ptes);
1042 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001043 pte_chain->parent_ptes[0] = old;
1044 }
Avi Kivity4db35312007-11-21 15:28:32 +02001045 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001046 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1047 continue;
1048 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1049 if (!pte_chain->parent_ptes[i]) {
1050 pte_chain->parent_ptes[i] = parent_pte;
1051 return;
1052 }
1053 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001054 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001055 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001056 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001057 pte_chain->parent_ptes[0] = parent_pte;
1058}
1059
Avi Kivity4db35312007-11-21 15:28:32 +02001060static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001061 u64 *parent_pte)
1062{
1063 struct kvm_pte_chain *pte_chain;
1064 struct hlist_node *node;
1065 int i;
1066
Avi Kivity4db35312007-11-21 15:28:32 +02001067 if (!sp->multimapped) {
1068 BUG_ON(sp->parent_pte != parent_pte);
1069 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001070 return;
1071 }
Avi Kivity4db35312007-11-21 15:28:32 +02001072 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001073 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1074 if (!pte_chain->parent_ptes[i])
1075 break;
1076 if (pte_chain->parent_ptes[i] != parent_pte)
1077 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001078 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1079 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001080 pte_chain->parent_ptes[i]
1081 = pte_chain->parent_ptes[i + 1];
1082 ++i;
1083 }
1084 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001085 if (i == 0) {
1086 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001087 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001088 if (hlist_empty(&sp->parent_ptes)) {
1089 sp->multimapped = 0;
1090 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001091 }
1092 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001093 return;
1094 }
1095 BUG();
1096}
1097
Xiao Guangrong6b184932010-04-16 21:29:17 +08001098static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001099{
1100 struct kvm_pte_chain *pte_chain;
1101 struct hlist_node *node;
1102 struct kvm_mmu_page *parent_sp;
1103 int i;
1104
1105 if (!sp->multimapped && sp->parent_pte) {
1106 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001107 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001108 return;
1109 }
1110
1111 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1112 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001113 u64 *spte = pte_chain->parent_ptes[i];
1114
1115 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001116 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001117 parent_sp = page_header(__pa(spte));
1118 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001119 }
1120}
1121
Xiao Guangrong1047df12010-06-11 21:35:15 +08001122static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001123static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001125 mmu_parent_walk(sp, mark_unsync);
1126}
1127
1128static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1129{
1130 unsigned int index;
1131
1132 index = spte - sp->spt;
1133 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1134 return;
1135 if (sp->unsync_children++)
1136 return;
1137 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001138}
1139
Avi Kivityd761a502008-05-29 14:55:03 +03001140static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1141 struct kvm_mmu_page *sp)
1142{
1143 int i;
1144
1145 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1146 sp->spt[i] = shadow_trap_nonpresent_pte;
1147}
1148
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001149static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001150 struct kvm_mmu_page *sp, bool clear_unsync)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001151{
1152 return 1;
1153}
1154
Marcelo Tosattia7052892008-09-23 13:18:35 -03001155static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1156{
1157}
1158
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001159#define KVM_PAGE_ARRAY_NR 16
1160
1161struct kvm_mmu_pages {
1162 struct mmu_page_and_offset {
1163 struct kvm_mmu_page *sp;
1164 unsigned int idx;
1165 } page[KVM_PAGE_ARRAY_NR];
1166 unsigned int nr;
1167};
1168
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001169#define for_each_unsync_children(bitmap, idx) \
1170 for (idx = find_first_bit(bitmap, 512); \
1171 idx < 512; \
1172 idx = find_next_bit(bitmap, 512, idx+1))
1173
Hannes Edercded19f2009-02-21 02:19:13 +01001174static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1175 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001176{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001177 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001178
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001179 if (sp->unsync)
1180 for (i=0; i < pvec->nr; i++)
1181 if (pvec->page[i].sp == sp)
1182 return 0;
1183
1184 pvec->page[pvec->nr].sp = sp;
1185 pvec->page[pvec->nr].idx = idx;
1186 pvec->nr++;
1187 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1188}
1189
1190static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1191 struct kvm_mmu_pages *pvec)
1192{
1193 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001194
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001195 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001196 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001197 u64 ent = sp->spt[i];
1198
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001199 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1200 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001201
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001202 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001203
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001204 if (child->unsync_children) {
1205 if (mmu_pages_add(pvec, child, i))
1206 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001207
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001208 ret = __mmu_unsync_walk(child, pvec);
1209 if (!ret)
1210 goto clear_child_bitmap;
1211 else if (ret > 0)
1212 nr_unsync_leaf += ret;
1213 else
1214 return ret;
1215 } else if (child->unsync) {
1216 nr_unsync_leaf++;
1217 if (mmu_pages_add(pvec, child, i))
1218 return -ENOSPC;
1219 } else
1220 goto clear_child_bitmap;
1221
1222 continue;
1223
1224clear_child_bitmap:
1225 __clear_bit(i, sp->unsync_child_bitmap);
1226 sp->unsync_children--;
1227 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001228 }
1229
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001231 return nr_unsync_leaf;
1232}
1233
1234static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1235 struct kvm_mmu_pages *pvec)
1236{
1237 if (!sp->unsync_children)
1238 return 0;
1239
1240 mmu_pages_add(pvec, sp, 0);
1241 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001242}
1243
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001244static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1245{
1246 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001247 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248 sp->unsync = 0;
1249 --kvm->stat.mmu_unsync;
1250}
1251
Xiao Guangrong77758342010-06-04 21:53:54 +08001252static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1253 struct list_head *invalid_list);
1254static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1255 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001257#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1258 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001259 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1260 if ((sp)->gfn != (gfn)) {} else
1261
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001262#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1263 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001264 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1265 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1266 (sp)->role.invalid) {} else
1267
Xiao Guangrongf918b442010-06-11 21:30:36 +08001268/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001269static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001270 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001271{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001272 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001273 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001274 return 1;
1275 }
1276
Xiao Guangrongf918b442010-06-11 21:30:36 +08001277 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001278 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001279
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001280 if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001281 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001282 return 1;
1283 }
1284
1285 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001286 return 0;
1287}
1288
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001289static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1290 struct kvm_mmu_page *sp)
1291{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001292 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001293 int ret;
1294
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001295 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001296 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001297 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1298
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001299 return ret;
1300}
1301
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001302static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1303 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001304{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001305 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001306}
1307
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001308/* @gfn should be write-protected at the call site */
1309static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1310{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001311 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001312 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001313 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001314 bool flush = false;
1315
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001316 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001317 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001318 continue;
1319
1320 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1321 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001322 (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001323 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001324 continue;
1325 }
1326 kvm_unlink_unsync_page(vcpu->kvm, s);
1327 flush = true;
1328 }
1329
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001330 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001331 if (flush)
1332 kvm_mmu_flush_tlb(vcpu);
1333}
1334
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001335struct mmu_page_path {
1336 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1337 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001338};
1339
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001340#define for_each_sp(pvec, sp, parents, i) \
1341 for (i = mmu_pages_next(&pvec, &parents, -1), \
1342 sp = pvec.page[i].sp; \
1343 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1344 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001345
Hannes Edercded19f2009-02-21 02:19:13 +01001346static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1347 struct mmu_page_path *parents,
1348 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001349{
1350 int n;
1351
1352 for (n = i+1; n < pvec->nr; n++) {
1353 struct kvm_mmu_page *sp = pvec->page[n].sp;
1354
1355 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1356 parents->idx[0] = pvec->page[n].idx;
1357 return n;
1358 }
1359
1360 parents->parent[sp->role.level-2] = sp;
1361 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1362 }
1363
1364 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001365}
1366
Hannes Edercded19f2009-02-21 02:19:13 +01001367static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001368{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001369 struct kvm_mmu_page *sp;
1370 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001371
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001372 do {
1373 unsigned int idx = parents->idx[level];
1374
1375 sp = parents->parent[level];
1376 if (!sp)
1377 return;
1378
1379 --sp->unsync_children;
1380 WARN_ON((int)sp->unsync_children < 0);
1381 __clear_bit(idx, sp->unsync_child_bitmap);
1382 level++;
1383 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1384}
1385
1386static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1387 struct mmu_page_path *parents,
1388 struct kvm_mmu_pages *pvec)
1389{
1390 parents->parent[parent->role.level-1] = NULL;
1391 pvec->nr = 0;
1392}
1393
1394static void mmu_sync_children(struct kvm_vcpu *vcpu,
1395 struct kvm_mmu_page *parent)
1396{
1397 int i;
1398 struct kvm_mmu_page *sp;
1399 struct mmu_page_path parents;
1400 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001401 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001402
1403 kvm_mmu_pages_init(parent, &parents, &pages);
1404 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001405 int protected = 0;
1406
1407 for_each_sp(pages, sp, parents, i)
1408 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1409
1410 if (protected)
1411 kvm_flush_remote_tlbs(vcpu->kvm);
1412
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001413 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001414 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001415 mmu_pages_clear_parents(&parents);
1416 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001417 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001418 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001419 kvm_mmu_pages_init(parent, &parents, &pages);
1420 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001421}
1422
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001423static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1424 gfn_t gfn,
1425 gva_t gaddr,
1426 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001427 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001428 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001429 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001430{
1431 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001432 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001433 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001434 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001435 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001436
Avi Kivitya770f6f2008-12-21 19:20:09 +02001437 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001438 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001439 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001440 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001441 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001442 role.access = access;
Gui Jianfengb66d8002010-05-31 17:11:39 +08001443 if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001444 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1445 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1446 role.quadrant = quadrant;
1447 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001448 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001449 if (!need_sync && sp->unsync)
1450 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001451
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001452 if (sp->role.word != role.word)
1453 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001454
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001455 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1456 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001457
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001458 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1459 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001460 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001461 kvm_mmu_mark_parents_unsync(sp);
1462 } else if (sp->unsync)
1463 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001464
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001465 trace_kvm_mmu_get_page(sp, false);
1466 return sp;
1467 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001468 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001469 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001470 if (!sp)
1471 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001472 sp->gfn = gfn;
1473 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001474 hlist_add_head(&sp->hash_link,
1475 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001476 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001477 if (rmap_write_protect(vcpu->kvm, gfn))
1478 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001479 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1480 kvm_sync_pages(vcpu, gfn);
1481
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001482 account_shadowed(vcpu->kvm, gfn);
1483 }
Avi Kivity131d8272008-05-29 14:56:28 +03001484 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1485 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1486 else
1487 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001488 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001489 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001490}
1491
Avi Kivity2d111232008-12-25 14:39:47 +02001492static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1493 struct kvm_vcpu *vcpu, u64 addr)
1494{
1495 iterator->addr = addr;
1496 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1497 iterator->level = vcpu->arch.mmu.shadow_root_level;
1498 if (iterator->level == PT32E_ROOT_LEVEL) {
1499 iterator->shadow_addr
1500 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1501 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1502 --iterator->level;
1503 if (!iterator->shadow_addr)
1504 iterator->level = 0;
1505 }
1506}
1507
1508static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1509{
1510 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1511 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001512
1513 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1514 if (is_large_pte(*iterator->sptep))
1515 return false;
1516
Avi Kivity2d111232008-12-25 14:39:47 +02001517 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1518 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1519 return true;
1520}
1521
1522static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1523{
1524 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1525 --iterator->level;
1526}
1527
Avi Kivity32ef26a2010-07-13 14:27:04 +03001528static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1529{
1530 u64 spte;
1531
1532 spte = __pa(sp->spt)
1533 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1534 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001535 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001536}
1537
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001538static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1539{
1540 if (is_large_pte(*sptep)) {
1541 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1542 kvm_flush_remote_tlbs(vcpu->kvm);
1543 }
1544}
1545
Avi Kivitya357bd22010-07-13 14:27:07 +03001546static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1547 unsigned direct_access)
1548{
1549 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1550 struct kvm_mmu_page *child;
1551
1552 /*
1553 * For the direct sp, if the guest pte's dirty bit
1554 * changed form clean to dirty, it will corrupt the
1555 * sp's access: allow writable in the read-only sp,
1556 * so we should update the spte at this point to get
1557 * a new sp with the correct access.
1558 */
1559 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1560 if (child->role.access == direct_access)
1561 return;
1562
1563 mmu_page_remove_parent_pte(child, sptep);
1564 __set_spte(sptep, shadow_trap_nonpresent_pte);
1565 kvm_flush_remote_tlbs(vcpu->kvm);
1566 }
1567}
1568
Avi Kivity90cb0522007-07-17 13:04:56 +03001569static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001570 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001571{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001572 unsigned i;
1573 u64 *pt;
1574 u64 ent;
1575
Avi Kivity4db35312007-11-21 15:28:32 +02001576 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001577
Avi Kivity697fe2e2007-01-05 16:36:46 -08001578 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1579 ent = pt[i];
1580
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001581 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001582 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001583 ent &= PT64_BASE_ADDR_MASK;
1584 mmu_page_remove_parent_pte(page_header(ent),
1585 &pt[i]);
1586 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001587 if (is_large_pte(ent))
1588 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001589 drop_spte(kvm, &pt[i],
1590 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001591 }
1592 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001593 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001594 }
Avi Kivitya4360362007-01-05 16:36:45 -08001595}
1596
Avi Kivity4db35312007-11-21 15:28:32 +02001597static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001598{
Avi Kivity4db35312007-11-21 15:28:32 +02001599 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001600}
1601
Avi Kivity12b7d282007-09-23 14:10:49 +02001602static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1603{
1604 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001605 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001606
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001607 kvm_for_each_vcpu(i, vcpu, kvm)
1608 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001609}
1610
Avi Kivity31aa2b42008-07-11 17:59:46 +03001611static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001612{
1613 u64 *parent_pte;
1614
Avi Kivity4db35312007-11-21 15:28:32 +02001615 while (sp->multimapped || sp->parent_pte) {
1616 if (!sp->multimapped)
1617 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001618 else {
1619 struct kvm_pte_chain *chain;
1620
Avi Kivity4db35312007-11-21 15:28:32 +02001621 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001622 struct kvm_pte_chain, link);
1623 parent_pte = chain->parent_ptes[0];
1624 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001625 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001626 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001627 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001628 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001629}
1630
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001631static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001632 struct kvm_mmu_page *parent,
1633 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001634{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001635 int i, zapped = 0;
1636 struct mmu_page_path parents;
1637 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001638
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001639 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001640 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001641
1642 kvm_mmu_pages_init(parent, &parents, &pages);
1643 while (mmu_unsync_walk(parent, &pages)) {
1644 struct kvm_mmu_page *sp;
1645
1646 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001647 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001648 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001649 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001650 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001651 kvm_mmu_pages_init(parent, &parents, &pages);
1652 }
1653
1654 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655}
1656
Xiao Guangrong77758342010-06-04 21:53:54 +08001657static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1658 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001659{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001660 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001661
Xiao Guangrong77758342010-06-04 21:53:54 +08001662 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001663 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001664 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001665 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001666 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001667 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001668 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001669 if (sp->unsync)
1670 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001671 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001672 /* Count self */
1673 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001674 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001675 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001676 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001677 kvm_reload_remote_mmus(kvm);
1678 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001679
1680 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001681 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001682 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001683}
1684
Xiao Guangrong77758342010-06-04 21:53:54 +08001685static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1686 struct list_head *invalid_list)
1687{
1688 struct kvm_mmu_page *sp;
1689
1690 if (list_empty(invalid_list))
1691 return;
1692
1693 kvm_flush_remote_tlbs(kvm);
1694
1695 do {
1696 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1697 WARN_ON(!sp->role.invalid || sp->root_count);
1698 kvm_mmu_free_page(kvm, sp);
1699 } while (!list_empty(invalid_list));
1700
1701}
1702
Izik Eidus82ce2c92007-10-02 18:52:55 +02001703/*
1704 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001705 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001706 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001707void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001708{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001709 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001710 /*
1711 * If we set the number of mmu pages to be smaller be than the
1712 * number of actived pages , we must to free some mmu pages before we
1713 * change the value
1714 */
1715
Dave Hansen49d5ca22010-08-19 18:11:28 -07001716 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1717 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001718 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001719 struct kvm_mmu_page *page;
1720
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001721 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001722 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001723 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1724 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001725 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001726 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001727 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001728
Dave Hansen49d5ca22010-08-19 18:11:28 -07001729 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001730}
1731
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001732static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001733{
Avi Kivity4db35312007-11-21 15:28:32 +02001734 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001735 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001736 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001737 int r;
1738
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001739 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001740 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001741
1742 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001743 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1744 sp->role.word);
1745 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001746 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001747 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001748 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001749 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001750}
1751
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001752static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001753{
Avi Kivity4db35312007-11-21 15:28:32 +02001754 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001755 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001756 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001757
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001758 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001759 pgprintk("%s: zap %lx %x\n",
1760 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001761 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001762 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001763 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001764}
1765
Avi Kivity38c335f2007-11-21 14:20:22 +02001766static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001768 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001769 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770
Sheng Yang291f26b2008-10-16 17:30:57 +08001771 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772}
1773
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001774static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1775{
1776 int i;
1777 u64 *pt = sp->spt;
1778
1779 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1780 return;
1781
1782 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1783 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001784 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001785 }
1786}
1787
Sheng Yang74be52e2008-10-09 16:01:56 +08001788/*
1789 * The function is based on mtrr_type_lookup() in
1790 * arch/x86/kernel/cpu/mtrr/generic.c
1791 */
1792static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1793 u64 start, u64 end)
1794{
1795 int i;
1796 u64 base, mask;
1797 u8 prev_match, curr_match;
1798 int num_var_ranges = KVM_NR_VAR_MTRR;
1799
1800 if (!mtrr_state->enabled)
1801 return 0xFF;
1802
1803 /* Make end inclusive end, instead of exclusive */
1804 end--;
1805
1806 /* Look in fixed ranges. Just return the type as per start */
1807 if (mtrr_state->have_fixed && (start < 0x100000)) {
1808 int idx;
1809
1810 if (start < 0x80000) {
1811 idx = 0;
1812 idx += (start >> 16);
1813 return mtrr_state->fixed_ranges[idx];
1814 } else if (start < 0xC0000) {
1815 idx = 1 * 8;
1816 idx += ((start - 0x80000) >> 14);
1817 return mtrr_state->fixed_ranges[idx];
1818 } else if (start < 0x1000000) {
1819 idx = 3 * 8;
1820 idx += ((start - 0xC0000) >> 12);
1821 return mtrr_state->fixed_ranges[idx];
1822 }
1823 }
1824
1825 /*
1826 * Look in variable ranges
1827 * Look of multiple ranges matching this address and pick type
1828 * as per MTRR precedence
1829 */
1830 if (!(mtrr_state->enabled & 2))
1831 return mtrr_state->def_type;
1832
1833 prev_match = 0xFF;
1834 for (i = 0; i < num_var_ranges; ++i) {
1835 unsigned short start_state, end_state;
1836
1837 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1838 continue;
1839
1840 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1841 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1842 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1843 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1844
1845 start_state = ((start & mask) == (base & mask));
1846 end_state = ((end & mask) == (base & mask));
1847 if (start_state != end_state)
1848 return 0xFE;
1849
1850 if ((start & mask) != (base & mask))
1851 continue;
1852
1853 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1854 if (prev_match == 0xFF) {
1855 prev_match = curr_match;
1856 continue;
1857 }
1858
1859 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1860 curr_match == MTRR_TYPE_UNCACHABLE)
1861 return MTRR_TYPE_UNCACHABLE;
1862
1863 if ((prev_match == MTRR_TYPE_WRBACK &&
1864 curr_match == MTRR_TYPE_WRTHROUGH) ||
1865 (prev_match == MTRR_TYPE_WRTHROUGH &&
1866 curr_match == MTRR_TYPE_WRBACK)) {
1867 prev_match = MTRR_TYPE_WRTHROUGH;
1868 curr_match = MTRR_TYPE_WRTHROUGH;
1869 }
1870
1871 if (prev_match != curr_match)
1872 return MTRR_TYPE_UNCACHABLE;
1873 }
1874
1875 if (prev_match != 0xFF)
1876 return prev_match;
1877
1878 return mtrr_state->def_type;
1879}
1880
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001881u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001882{
1883 u8 mtrr;
1884
1885 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1886 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1887 if (mtrr == 0xfe || mtrr == 0xff)
1888 mtrr = MTRR_TYPE_WRBACK;
1889 return mtrr;
1890}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001891EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001892
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001893static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001894{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001895 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001896 ++vcpu->kvm->stat.mmu_unsync;
1897 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001898
Xiao Guangrong6b184932010-04-16 21:29:17 +08001899 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001900 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001901}
1902
1903static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1904{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001905 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001906 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001907
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001908 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001909 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001910 continue;
1911 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1912 __kvm_unsync_page(vcpu, s);
1913 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001914}
1915
1916static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1917 bool can_unsync)
1918{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001919 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001920 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001921 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001922
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001923 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001924 if (!can_unsync)
1925 return 1;
1926
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001927 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001928 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001929
1930 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001931 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001932 return 1;
1933 need_unsync = true;
1934 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001935 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001936 if (need_unsync)
1937 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001938 return 0;
1939}
1940
Avi Kivityd555c332009-06-10 14:24:23 +03001941static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001942 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001943 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001944 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001945 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001946{
1947 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001948 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001949
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001950 /*
1951 * We don't set the accessed bit, since we sometimes want to see
1952 * whether the guest actually used the pte (in order to detect
1953 * demand paging).
1954 */
Xiao Guangrong41327792010-08-02 16:15:08 +08001955 spte = shadow_base_present_pte;
Avi Kivity947da532008-03-18 11:05:52 +02001956 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001957 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001958 if (!dirty)
1959 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001960 if (pte_access & ACC_EXEC_MASK)
1961 spte |= shadow_x_mask;
1962 else
1963 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001964 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001965 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001966 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001967 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001968 if (tdp_enabled)
1969 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1970 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001971
Izik Eidus14032832009-09-23 21:47:17 +03001972 if (reset_host_protection)
1973 spte |= SPTE_HOST_WRITEABLE;
1974
Anthony Liguori35149e22008-04-02 14:46:56 -05001975 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001976
1977 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001978 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1979 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001980
Joerg Roedel852e3c12009-07-27 16:30:44 +02001981 if (level > PT_PAGE_TABLE_LEVEL &&
1982 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001983 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001984 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1985 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001986 }
1987
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001988 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001989
Avi Kivity69325a12010-05-27 14:35:58 +03001990 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1991 spte &= ~PT_USER_MASK;
1992
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001993 /*
1994 * Optimization: for pte sync, if spte was writable the hash
1995 * lookup is unnecessary (and expensive). Write protection
1996 * is responsibility of mmu_get_page / kvm_sync_page.
1997 * Same reasoning can be applied to dirty page accounting.
1998 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001999 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002000 goto set_pte;
2001
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002002 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002003 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002004 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002005 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002006 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002007 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002008 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002009 }
2010 }
2011
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002012 if (pte_access & ACC_WRITE_MASK)
2013 mark_page_dirty(vcpu->kvm, gfn);
2014
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002015set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002016 update_spte(sptep, spte);
Avi Kivitybe38d272010-06-06 14:31:27 +03002017done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002018 return ret;
2019}
2020
Avi Kivityd555c332009-06-10 14:24:23 +03002021static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002022 unsigned pt_access, unsigned pte_access,
2023 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002024 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002025 pfn_t pfn, bool speculative,
2026 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002027{
2028 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002029 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002030
2031 pgprintk("%s: spte %llx access %x write_fault %d"
2032 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002033 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002034 write_fault, user_fault, gfn);
2035
Avi Kivityd555c332009-06-10 14:24:23 +03002036 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002037 /*
2038 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2039 * the parent of the now unreachable PTE.
2040 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002041 if (level > PT_PAGE_TABLE_LEVEL &&
2042 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002043 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002044 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002045
2046 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002047 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002048 __set_spte(sptep, shadow_trap_nonpresent_pte);
2049 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002050 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002051 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002052 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002053 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002054 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002055 } else
2056 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002057 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002058
Avi Kivityd555c332009-06-10 14:24:23 +03002059 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002060 dirty, level, gfn, pfn, speculative, true,
2061 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002062 if (write_fault)
2063 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002064 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002065 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002066
Avi Kivityd555c332009-06-10 14:24:23 +03002067 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002068 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002069 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002070 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2071 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002072 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002073 ++vcpu->kvm->stat.lpages;
2074
Avi Kivityd555c332009-06-10 14:24:23 +03002075 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002076 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002077 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002078 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002079 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002080 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002081 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002082 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002083 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002084 vcpu->arch.last_pte_gfn = gfn;
2085 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002086}
2087
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2089{
2090}
2091
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002092static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002093 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094{
Avi Kivity9f652d22008-12-25 14:54:25 +02002095 struct kvm_shadow_walk_iterator iterator;
2096 struct kvm_mmu_page *sp;
2097 int pt_write = 0;
2098 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099
Avi Kivity9f652d22008-12-25 14:54:25 +02002100 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002101 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002102 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2103 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002104 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02002105 ++vcpu->stat.pf_fixed;
2106 break;
2107 }
2108
2109 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002110 u64 base_addr = iterator.addr;
2111
2112 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2113 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002114 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2115 iterator.level - 1,
2116 1, ACC_ALL, iterator.sptep);
2117 if (!sp) {
2118 pgprintk("nonpaging_map: ENOMEM\n");
2119 kvm_release_pfn_clean(pfn);
2120 return -ENOMEM;
2121 }
2122
Avi Kivityd555c332009-06-10 14:24:23 +03002123 __set_spte(iterator.sptep,
2124 __pa(sp->spt)
2125 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2126 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002127 }
2128 }
2129 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130}
2131
Huang Yingbf998152010-05-31 14:28:19 +08002132static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2133{
2134 char buf[1];
2135 void __user *hva;
2136 int r;
2137
2138 /* Touch the page, so send SIGBUS */
2139 hva = (void __user *)gfn_to_hva(kvm, gfn);
2140 r = copy_from_user(buf, hva, 1);
2141}
2142
2143static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2144{
2145 kvm_release_pfn_clean(pfn);
2146 if (is_hwpoison_pfn(pfn)) {
2147 kvm_send_hwpoison_signal(kvm, gfn);
2148 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002149 } else if (is_fault_pfn(pfn))
2150 return -EFAULT;
2151
Huang Yingbf998152010-05-31 14:28:19 +08002152 return 1;
2153}
2154
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002155static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2156{
2157 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002158 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002159 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002160 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002161
Joerg Roedel852e3c12009-07-27 16:30:44 +02002162 level = mapping_level(vcpu, gfn);
2163
2164 /*
2165 * This path builds a PAE pagetable - so we can map 2mb pages at
2166 * maximum. Therefore check if the level is larger than that.
2167 */
2168 if (level > PT_DIRECTORY_LEVEL)
2169 level = PT_DIRECTORY_LEVEL;
2170
2171 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002172
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002173 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002174 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002175 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002176
Avi Kivityd196e342008-01-24 11:44:11 +02002177 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002178 if (is_error_pfn(pfn))
2179 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002180
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002181 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002182 if (mmu_notifier_retry(vcpu, mmu_seq))
2183 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002184 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002185 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002186 spin_unlock(&vcpu->kvm->mmu_lock);
2187
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002188
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002189 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002190
2191out_unlock:
2192 spin_unlock(&vcpu->kvm->mmu_lock);
2193 kvm_release_pfn_clean(pfn);
2194 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002195}
2196
2197
Avi Kivity17ac10a2007-01-05 16:36:40 -08002198static void mmu_free_roots(struct kvm_vcpu *vcpu)
2199{
2200 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002201 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002202 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002203
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002204 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002205 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002206 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002207 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2208 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002209
Avi Kivity4db35312007-11-21 15:28:32 +02002210 sp = page_header(root);
2211 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002212 if (!sp->root_count && sp->role.invalid) {
2213 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2214 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2215 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002216 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002217 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002218 return;
2219 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002220 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002221 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002222
Avi Kivity417726a2007-04-12 17:35:58 +03002223 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002224 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002225 sp = page_header(root);
2226 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002227 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002228 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2229 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002230 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002231 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002232 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002233 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002234 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002235 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002236}
2237
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002238static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2239{
2240 int ret = 0;
2241
2242 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002243 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002244 ret = 1;
2245 }
2246
2247 return ret;
2248}
2249
2250static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002251{
2252 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002253 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002254 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002255 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002256 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002257
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002258 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002259
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002260 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2261 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002262
2263 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002264 if (mmu_check_root(vcpu, root_gfn))
2265 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002266 if (tdp_enabled) {
2267 direct = 1;
2268 root_gfn = 0;
2269 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002270 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002271 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002272 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002273 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002274 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002275 root = __pa(sp->spt);
2276 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002277 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002278 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002279 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002280 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002281 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002282 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002283 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002284
2285 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002286 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002287 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002288 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002289 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002290 continue;
2291 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002292 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002293 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002294 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002295 if (mmu_check_root(vcpu, root_gfn))
2296 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002297 if (tdp_enabled) {
2298 direct = 1;
2299 root_gfn = i << 30;
2300 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002301 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002302 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002303 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002304 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002305 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002306 root = __pa(sp->spt);
2307 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002308 spin_unlock(&vcpu->kvm->mmu_lock);
2309
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002310 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002311 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002312 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002313 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002314}
2315
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002316static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2317{
2318 int i;
2319 struct kvm_mmu_page *sp;
2320
2321 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2322 return;
2323 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2324 hpa_t root = vcpu->arch.mmu.root_hpa;
2325 sp = page_header(root);
2326 mmu_sync_children(vcpu, sp);
2327 return;
2328 }
2329 for (i = 0; i < 4; ++i) {
2330 hpa_t root = vcpu->arch.mmu.pae_root[i];
2331
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002332 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002333 root &= PT64_BASE_ADDR_MASK;
2334 sp = page_header(root);
2335 mmu_sync_children(vcpu, sp);
2336 }
2337 }
2338}
2339
2340void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2341{
2342 spin_lock(&vcpu->kvm->mmu_lock);
2343 mmu_sync_roots(vcpu);
2344 spin_unlock(&vcpu->kvm->mmu_lock);
2345}
2346
Gleb Natapov1871c602010-02-10 14:21:32 +02002347static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2348 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349{
Gleb Natapov1871c602010-02-10 14:21:32 +02002350 if (error)
2351 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 return vaddr;
2353}
2354
2355static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002356 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357{
Avi Kivitye8332402007-12-09 18:43:00 +02002358 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002359 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002361 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002362 r = mmu_topup_memory_caches(vcpu);
2363 if (r)
2364 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002365
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002367 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
Avi Kivitye8332402007-12-09 18:43:00 +02002369 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
Avi Kivitye8332402007-12-09 18:43:00 +02002371 return nonpaging_map(vcpu, gva & PAGE_MASK,
2372 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373}
2374
Joerg Roedelfb72d162008-02-07 13:47:44 +01002375static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2376 u32 error_code)
2377{
Anthony Liguori35149e22008-04-02 14:46:56 -05002378 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002379 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002380 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002381 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002382 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002383
2384 ASSERT(vcpu);
2385 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2386
2387 r = mmu_topup_memory_caches(vcpu);
2388 if (r)
2389 return r;
2390
Joerg Roedel852e3c12009-07-27 16:30:44 +02002391 level = mapping_level(vcpu, gfn);
2392
2393 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2394
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002395 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002396 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002397 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002398 if (is_error_pfn(pfn))
2399 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002400 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002401 if (mmu_notifier_retry(vcpu, mmu_seq))
2402 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002403 kvm_mmu_free_some_pages(vcpu);
2404 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002405 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002406 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002407
2408 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002409
2410out_unlock:
2411 spin_unlock(&vcpu->kvm->mmu_lock);
2412 kvm_release_pfn_clean(pfn);
2413 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002414}
2415
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416static void nonpaging_free(struct kvm_vcpu *vcpu)
2417{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002418 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419}
2420
2421static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2422{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002423 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424
2425 context->new_cr3 = nonpaging_new_cr3;
2426 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 context->gva_to_gpa = nonpaging_gva_to_gpa;
2428 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002429 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002430 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002431 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002432 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002434 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 return 0;
2436}
2437
Avi Kivityd835dfe2007-11-21 02:57:59 +02002438void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002440 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002441 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442}
2443
2444static void paging_new_cr3(struct kvm_vcpu *vcpu)
2445{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002446 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002447 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448}
2449
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450static void inject_page_fault(struct kvm_vcpu *vcpu,
2451 u64 addr,
2452 u32 err_code)
2453{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002454 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455}
2456
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457static void paging_free(struct kvm_vcpu *vcpu)
2458{
2459 nonpaging_free(vcpu);
2460}
2461
Dong, Eddie82725b22009-03-30 16:21:08 +08002462static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2463{
2464 int bit7;
2465
2466 bit7 = (gpte >> 7) & 1;
2467 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2468}
2469
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470#define PTTYPE 64
2471#include "paging_tmpl.h"
2472#undef PTTYPE
2473
2474#define PTTYPE 32
2475#include "paging_tmpl.h"
2476#undef PTTYPE
2477
Dong, Eddie82725b22009-03-30 16:21:08 +08002478static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2479{
2480 struct kvm_mmu *context = &vcpu->arch.mmu;
2481 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2482 u64 exb_bit_rsvd = 0;
2483
2484 if (!is_nx(vcpu))
2485 exb_bit_rsvd = rsvd_bits(63, 63);
2486 switch (level) {
2487 case PT32_ROOT_LEVEL:
2488 /* no rsvd bits for 2 level 4K page table entries */
2489 context->rsvd_bits_mask[0][1] = 0;
2490 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002491 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2492
2493 if (!is_pse(vcpu)) {
2494 context->rsvd_bits_mask[1][1] = 0;
2495 break;
2496 }
2497
Dong, Eddie82725b22009-03-30 16:21:08 +08002498 if (is_cpuid_PSE36())
2499 /* 36bits PSE 4MB page */
2500 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2501 else
2502 /* 32 bits PSE 4MB page */
2503 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002504 break;
2505 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002506 context->rsvd_bits_mask[0][2] =
2507 rsvd_bits(maxphyaddr, 63) |
2508 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002509 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002510 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002511 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2512 rsvd_bits(maxphyaddr, 62); /* PTE */
2513 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2514 rsvd_bits(maxphyaddr, 62) |
2515 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002516 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002517 break;
2518 case PT64_ROOT_LEVEL:
2519 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2520 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2521 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2522 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2523 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002524 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002525 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2526 rsvd_bits(maxphyaddr, 51);
2527 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002528 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2529 rsvd_bits(maxphyaddr, 51) |
2530 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002531 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002532 rsvd_bits(maxphyaddr, 51) |
2533 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002534 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002535 break;
2536 }
2537}
2538
Avi Kivity17ac10a2007-01-05 16:36:40 -08002539static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002541 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542
2543 ASSERT(is_pae(vcpu));
2544 context->new_cr3 = paging_new_cr3;
2545 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002547 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002548 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002549 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002551 context->root_level = level;
2552 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002553 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 return 0;
2555}
2556
Avi Kivity17ac10a2007-01-05 16:36:40 -08002557static int paging64_init_context(struct kvm_vcpu *vcpu)
2558{
Dong, Eddie82725b22009-03-30 16:21:08 +08002559 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002560 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2561}
2562
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563static int paging32_init_context(struct kvm_vcpu *vcpu)
2564{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002565 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566
Dong, Eddie82725b22009-03-30 16:21:08 +08002567 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 context->new_cr3 = paging_new_cr3;
2569 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 context->gva_to_gpa = paging32_gva_to_gpa;
2571 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002572 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002573 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002574 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 context->root_level = PT32_ROOT_LEVEL;
2576 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002577 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 return 0;
2579}
2580
2581static int paging32E_init_context(struct kvm_vcpu *vcpu)
2582{
Dong, Eddie82725b22009-03-30 16:21:08 +08002583 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002584 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585}
2586
Joerg Roedelfb72d162008-02-07 13:47:44 +01002587static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2588{
2589 struct kvm_mmu *context = &vcpu->arch.mmu;
2590
2591 context->new_cr3 = nonpaging_new_cr3;
2592 context->page_fault = tdp_page_fault;
2593 context->free = nonpaging_free;
2594 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002595 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002596 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002597 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002598 context->root_hpa = INVALID_PAGE;
2599
2600 if (!is_paging(vcpu)) {
2601 context->gva_to_gpa = nonpaging_gva_to_gpa;
2602 context->root_level = 0;
2603 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002604 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002605 context->gva_to_gpa = paging64_gva_to_gpa;
2606 context->root_level = PT64_ROOT_LEVEL;
2607 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002608 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002609 context->gva_to_gpa = paging64_gva_to_gpa;
2610 context->root_level = PT32E_ROOT_LEVEL;
2611 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002612 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002613 context->gva_to_gpa = paging32_gva_to_gpa;
2614 context->root_level = PT32_ROOT_LEVEL;
2615 }
2616
2617 return 0;
2618}
2619
2620static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002622 int r;
2623
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002625 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626
2627 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002628 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002629 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002630 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002632 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002634 r = paging32_init_context(vcpu);
2635
Avi Kivity5b7e0102010-04-14 19:20:03 +03002636 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002637 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002638
2639 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640}
2641
Joerg Roedelfb72d162008-02-07 13:47:44 +01002642static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2643{
Anthony Liguori35149e22008-04-02 14:46:56 -05002644 vcpu->arch.update_pte.pfn = bad_pfn;
2645
Joerg Roedelfb72d162008-02-07 13:47:44 +01002646 if (tdp_enabled)
2647 return init_kvm_tdp_mmu(vcpu);
2648 else
2649 return init_kvm_softmmu(vcpu);
2650}
2651
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2653{
2654 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002655 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2656 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002657 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658}
2659
2660int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2661{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002662 destroy_kvm_mmu(vcpu);
2663 return init_kvm_mmu(vcpu);
2664}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002665EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002666
2667int kvm_mmu_load(struct kvm_vcpu *vcpu)
2668{
Avi Kivity714b93d2007-01-05 16:36:53 -08002669 int r;
2670
Avi Kivitye2dec932007-01-05 16:36:54 -08002671 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002672 if (r)
2673 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002674 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002675 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002676 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002677 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002678 if (r)
2679 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002680 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002681 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002682out:
2683 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002685EXPORT_SYMBOL_GPL(kvm_mmu_load);
2686
2687void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2688{
2689 mmu_free_roots(vcpu);
2690}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691
Avi Kivity09072da2007-05-01 14:16:52 +03002692static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002693 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002694 u64 *spte)
2695{
2696 u64 pte;
2697 struct kvm_mmu_page *child;
2698
2699 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002700 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002701 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002702 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002703 else {
2704 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002705 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002706 }
2707 }
Avi Kivityd555c332009-06-10 14:24:23 +03002708 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002709 if (is_large_pte(pte))
2710 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002711}
2712
Avi Kivity00284252007-05-01 16:53:31 +03002713static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002714 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002715 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002716 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002717{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002718 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002719 ++vcpu->kvm->stat.mmu_pde_zapped;
2720 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002721 }
Avi Kivity00284252007-05-01 16:53:31 +03002722
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002723 if (is_rsvd_bits_set(vcpu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
2724 return;
2725
Avi Kivity4cee5762007-11-18 16:37:07 +02002726 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002727 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002728 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002729 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002730 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002731}
2732
Avi Kivity79539ce2007-11-21 02:06:21 +02002733static bool need_remote_flush(u64 old, u64 new)
2734{
2735 if (!is_shadow_present_pte(old))
2736 return false;
2737 if (!is_shadow_present_pte(new))
2738 return true;
2739 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2740 return true;
2741 old ^= PT64_NX_MASK;
2742 new ^= PT64_NX_MASK;
2743 return (old & ~new & PT64_PERM_MASK) != 0;
2744}
2745
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002746static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2747 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002748{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002749 if (zap_page)
2750 return;
2751
2752 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002753 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002754 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002755 kvm_mmu_flush_tlb(vcpu);
2756}
2757
Avi Kivity12b7d282007-09-23 14:10:49 +02002758static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2759{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002760 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002761
Sheng Yang7b523452008-04-25 21:13:50 +08002762 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002763}
2764
Avi Kivityd7824ff2007-12-30 12:29:05 +02002765static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002766 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002767{
2768 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002769 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002770
Avi Kivity43a37952009-06-10 14:12:05 +03002771 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002772 return;
2773 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002774
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002775 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002776 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002777 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002778
Anthony Liguori35149e22008-04-02 14:46:56 -05002779 if (is_error_pfn(pfn)) {
2780 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002781 return;
2782 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002783 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002784 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002785}
2786
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002787static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2788{
2789 u64 *spte = vcpu->arch.last_pte_updated;
2790
2791 if (spte
2792 && vcpu->arch.last_pte_gfn == gfn
2793 && shadow_accessed_mask
2794 && !(*spte & shadow_accessed_mask)
2795 && is_shadow_present_pte(*spte))
2796 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2797}
2798
Avi Kivity09072da2007-05-01 14:16:52 +03002799void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002800 const u8 *new, int bytes,
2801 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002802{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002803 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002804 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02002805 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002806 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002807 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002808 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002809 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002810 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002811 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002812 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002813 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002814 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002815 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002816 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002817 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002818 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002819 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002820 bool remote_flush, local_flush, zap_page;
2821
2822 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002823
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002824 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002825
Avi Kivity08e850c2010-03-15 13:59:57 +02002826 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2827
2828 /*
2829 * Assume that the pte write on a page table of the same type
2830 * as the current vcpu paging mode. This is nearly always true
2831 * (might be false while changing modes). Note it is verified later
2832 * by update_pte().
2833 */
2834 if ((is_pae(vcpu) && bytes == 4) || !new) {
2835 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2836 if (is_pae(vcpu)) {
2837 gpa &= ~(gpa_t)7;
2838 bytes = 8;
2839 }
2840 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2841 if (r)
2842 gentry = 0;
2843 new = (const u8 *)&gentry;
2844 }
2845
Avi Kivity72016f32010-03-15 13:59:53 +02002846 switch (bytes) {
2847 case 4:
2848 gentry = *(const u32 *)new;
2849 break;
2850 case 8:
2851 gentry = *(const u64 *)new;
2852 break;
2853 default:
2854 gentry = 0;
2855 break;
2856 }
2857
Avi Kivity72016f32010-03-15 13:59:53 +02002858 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002859 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002860 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2861 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002862 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002863 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002864 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002865 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002866 if (guest_initiated) {
2867 if (gfn == vcpu->arch.last_pt_write_gfn
2868 && !last_updated_pte_accessed(vcpu)) {
2869 ++vcpu->arch.last_pt_write_count;
2870 if (vcpu->arch.last_pt_write_count >= 3)
2871 flooded = 1;
2872 } else {
2873 vcpu->arch.last_pt_write_gfn = gfn;
2874 vcpu->arch.last_pt_write_count = 1;
2875 vcpu->arch.last_pte_updated = NULL;
2876 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002877 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002878
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002879 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002880 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002881 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002882 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002883 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002884 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002885 /*
2886 * Misaligned accesses are too much trouble to fix
2887 * up; also, they usually indicate a page is not used
2888 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002889 *
2890 * If we're seeing too many writes to a page,
2891 * it may no longer be a page table, or we may be
2892 * forking, in which case it is better to unmap the
2893 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002894 */
2895 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002896 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002897 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002898 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002899 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002900 continue;
2901 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002902 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002903 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002904 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002905 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002906 page_offset <<= 1; /* 32->64 */
2907 /*
2908 * A 32-bit pde maps 4MB while the shadow pdes map
2909 * only 2MB. So we need to double the offset again
2910 * and zap two pdes instead of one.
2911 */
2912 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002913 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002914 page_offset <<= 1;
2915 npte = 2;
2916 }
Avi Kivityfce06572007-05-01 16:44:05 +03002917 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002918 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002919 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002920 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002921 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002922 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02002923 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002924 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002925 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002926 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002927 if (gentry &&
2928 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
2929 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02002930 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002931 if (!remote_flush && need_remote_flush(entry, *spte))
2932 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02002933 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002934 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002935 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002936 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002937 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityc7addb92007-09-16 18:58:32 +02002938 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002939 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002940 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2941 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2942 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002943 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002944}
2945
Avi Kivitya4360362007-01-05 16:36:45 -08002946int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2947{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002948 gpa_t gpa;
2949 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002950
Avi Kivity60f24782009-08-27 13:37:06 +03002951 if (tdp_enabled)
2952 return 0;
2953
Gleb Natapov1871c602010-02-10 14:21:32 +02002954 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002955
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002956 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002957 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002958 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002959 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002960}
Avi Kivity577bdc42008-07-19 08:57:05 +03002961EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002962
Avi Kivity22d95b12007-09-14 20:26:06 +03002963void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002964{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002965 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08002966
Dave Hansene0df7b92010-08-19 18:11:05 -07002967 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03002968 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002969 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002970
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002971 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002972 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07002973 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08002974 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002975 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002976 }
2977}
Avi Kivityebeace82007-01-05 16:36:47 -08002978
Avi Kivity30677142007-10-28 18:48:59 +02002979int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2980{
2981 int r;
2982 enum emulation_result er;
2983
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002984 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002985 if (r < 0)
2986 goto out;
2987
2988 if (!r) {
2989 r = 1;
2990 goto out;
2991 }
2992
Avi Kivityb733bfb2007-10-28 18:52:05 +02002993 r = mmu_topup_memory_caches(vcpu);
2994 if (r)
2995 goto out;
2996
Avi Kivity851ba692009-08-24 11:10:17 +03002997 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002998
2999 switch (er) {
3000 case EMULATE_DONE:
3001 return 1;
3002 case EMULATE_DO_MMIO:
3003 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003004 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003005 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003006 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003007 default:
3008 BUG();
3009 }
3010out:
Avi Kivity30677142007-10-28 18:48:59 +02003011 return r;
3012}
3013EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3014
Marcelo Tosattia7052892008-09-23 13:18:35 -03003015void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3016{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003017 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003018 kvm_mmu_flush_tlb(vcpu);
3019 ++vcpu->stat.invlpg;
3020}
3021EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3022
Joerg Roedel18552672008-02-07 13:47:41 +01003023void kvm_enable_tdp(void)
3024{
3025 tdp_enabled = true;
3026}
3027EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3028
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003029void kvm_disable_tdp(void)
3030{
3031 tdp_enabled = false;
3032}
3033EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3034
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035static void free_mmu_pages(struct kvm_vcpu *vcpu)
3036{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003037 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038}
3039
3040static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3041{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003042 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 int i;
3044
3045 ASSERT(vcpu);
3046
Avi Kivity17ac10a2007-01-05 16:36:40 -08003047 /*
3048 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3049 * Therefore we need to allocate shadow page tables in the first
3050 * 4GB of memory, which happens to fit the DMA32 zone.
3051 */
3052 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3053 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003054 return -ENOMEM;
3055
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003056 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003057 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003058 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003059
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061}
3062
Ingo Molnar8018c272006-12-29 16:50:01 -08003063int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003066 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067
Ingo Molnar8018c272006-12-29 16:50:01 -08003068 return alloc_mmu_pages(vcpu);
3069}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070
Ingo Molnar8018c272006-12-29 16:50:01 -08003071int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3072{
3073 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003074 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003075
Ingo Molnar8018c272006-12-29 16:50:01 -08003076 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077}
3078
3079void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3080{
3081 ASSERT(vcpu);
3082
3083 destroy_kvm_mmu(vcpu);
3084 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08003085 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086}
3087
Avi Kivity90cb0522007-07-17 13:04:56 +03003088void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089{
Avi Kivity4db35312007-11-21 15:28:32 +02003090 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003092 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 int i;
3094 u64 *pt;
3095
Sheng Yang291f26b2008-10-16 17:30:57 +08003096 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 continue;
3098
Avi Kivity4db35312007-11-21 15:28:32 +02003099 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3101 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003102 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 }
Avi Kivity171d5952008-08-27 16:40:51 +03003105 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003107
Avi Kivity90cb0522007-07-17 13:04:56 +03003108void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003109{
Avi Kivity4db35312007-11-21 15:28:32 +02003110 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003111 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003112
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003113 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003114restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003115 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003116 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003117 goto restart;
3118
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003119 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003120 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003121}
3122
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003123static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3124 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003125{
3126 struct kvm_mmu_page *page;
3127
3128 page = container_of(kvm->arch.active_mmu_pages.prev,
3129 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003130 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003131}
3132
Dave Chinner7f8275d2010-07-19 14:56:17 +10003133static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003134{
3135 struct kvm *kvm;
3136 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003137
3138 if (nr_to_scan == 0)
3139 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003140
3141 spin_lock(&kvm_lock);
3142
3143 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003144 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003145 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003146
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003147 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003148 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003149 if (!kvm_freed && nr_to_scan > 0 &&
3150 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003151 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3152 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003153 kvm_freed = kvm;
3154 }
3155 nr_to_scan--;
3156
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003157 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003158 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003159 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003160 }
3161 if (kvm_freed)
3162 list_move_tail(&kvm_freed->vm_list, &vm_list);
3163
3164 spin_unlock(&kvm_lock);
3165
Dave Hansen45221ab2010-08-19 18:11:37 -07003166out:
3167 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003168}
3169
3170static struct shrinker mmu_shrinker = {
3171 .shrink = mmu_shrink,
3172 .seeks = DEFAULT_SEEKS * 10,
3173};
3174
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003175static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003176{
3177 if (pte_chain_cache)
3178 kmem_cache_destroy(pte_chain_cache);
3179 if (rmap_desc_cache)
3180 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003181 if (mmu_page_header_cache)
3182 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003183}
3184
Izik Eidus3ee16c82008-03-30 15:17:21 +03003185void kvm_mmu_module_exit(void)
3186{
3187 mmu_destroy_caches();
3188 unregister_shrinker(&mmu_shrinker);
3189}
3190
Avi Kivityb5a33a72007-04-15 16:31:09 +03003191int kvm_mmu_module_init(void)
3192{
3193 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3194 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003195 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003196 if (!pte_chain_cache)
3197 goto nomem;
3198 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3199 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003200 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003201 if (!rmap_desc_cache)
3202 goto nomem;
3203
Avi Kivityd3d25b02007-05-30 12:34:53 +03003204 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3205 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003206 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003207 if (!mmu_page_header_cache)
3208 goto nomem;
3209
Dave Hansen45221ab2010-08-19 18:11:37 -07003210 percpu_counter_init(&kvm_total_used_mmu_pages, 0);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003211 register_shrinker(&mmu_shrinker);
3212
Avi Kivityb5a33a72007-04-15 16:31:09 +03003213 return 0;
3214
3215nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003216 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003217 return -ENOMEM;
3218}
3219
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003220/*
3221 * Caculate mmu pages needed for kvm.
3222 */
3223unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3224{
3225 int i;
3226 unsigned int nr_mmu_pages;
3227 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003228 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003229
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003230 slots = kvm_memslots(kvm);
3231
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003232 for (i = 0; i < slots->nmemslots; i++)
3233 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003234
3235 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3236 nr_mmu_pages = max(nr_mmu_pages,
3237 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3238
3239 return nr_mmu_pages;
3240}
3241
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003242static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3243 unsigned len)
3244{
3245 if (len > buffer->len)
3246 return NULL;
3247 return buffer->ptr;
3248}
3249
3250static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3251 unsigned len)
3252{
3253 void *ret;
3254
3255 ret = pv_mmu_peek_buffer(buffer, len);
3256 if (!ret)
3257 return ret;
3258 buffer->ptr += len;
3259 buffer->len -= len;
3260 buffer->processed += len;
3261 return ret;
3262}
3263
3264static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3265 gpa_t addr, gpa_t value)
3266{
3267 int bytes = 8;
3268 int r;
3269
3270 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3271 bytes = 4;
3272
3273 r = mmu_topup_memory_caches(vcpu);
3274 if (r)
3275 return r;
3276
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003277 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003278 return -EFAULT;
3279
3280 return 1;
3281}
3282
3283static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3284{
Avi Kivity23902182010-06-10 17:02:16 +03003285 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003286 return 1;
3287}
3288
3289static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3290{
3291 spin_lock(&vcpu->kvm->mmu_lock);
3292 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3293 spin_unlock(&vcpu->kvm->mmu_lock);
3294 return 1;
3295}
3296
3297static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3298 struct kvm_pv_mmu_op_buffer *buffer)
3299{
3300 struct kvm_mmu_op_header *header;
3301
3302 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3303 if (!header)
3304 return 0;
3305 switch (header->op) {
3306 case KVM_MMU_OP_WRITE_PTE: {
3307 struct kvm_mmu_op_write_pte *wpte;
3308
3309 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3310 if (!wpte)
3311 return 0;
3312 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3313 wpte->pte_val);
3314 }
3315 case KVM_MMU_OP_FLUSH_TLB: {
3316 struct kvm_mmu_op_flush_tlb *ftlb;
3317
3318 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3319 if (!ftlb)
3320 return 0;
3321 return kvm_pv_mmu_flush_tlb(vcpu);
3322 }
3323 case KVM_MMU_OP_RELEASE_PT: {
3324 struct kvm_mmu_op_release_pt *rpt;
3325
3326 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3327 if (!rpt)
3328 return 0;
3329 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3330 }
3331 default: return 0;
3332 }
3333}
3334
3335int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3336 gpa_t addr, unsigned long *ret)
3337{
3338 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003339 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003340
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003341 buffer->ptr = buffer->buf;
3342 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3343 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003344
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003345 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003346 if (r)
3347 goto out;
3348
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003349 while (buffer->len) {
3350 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003351 if (r < 0)
3352 goto out;
3353 if (r == 0)
3354 break;
3355 }
3356
3357 r = 1;
3358out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003359 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003360 return r;
3361}
3362
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003363int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3364{
3365 struct kvm_shadow_walk_iterator iterator;
3366 int nr_sptes = 0;
3367
3368 spin_lock(&vcpu->kvm->mmu_lock);
3369 for_each_shadow_entry(vcpu, addr, iterator) {
3370 sptes[iterator.level-1] = *iterator.sptep;
3371 nr_sptes++;
3372 if (!is_shadow_present_pte(*iterator.sptep))
3373 break;
3374 }
3375 spin_unlock(&vcpu->kvm->mmu_lock);
3376
3377 return nr_sptes;
3378}
3379EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3380
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003381#ifdef AUDIT
3382
3383static const char *audit_msg;
3384
3385static gva_t canonicalize(gva_t gva)
3386{
3387#ifdef CONFIG_X86_64
3388 gva = (long long)(gva << 16) >> 16;
3389#endif
3390 return gva;
3391}
3392
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003393
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003394typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003395
3396static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3397 inspect_spte_fn fn)
3398{
3399 int i;
3400
3401 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3402 u64 ent = sp->spt[i];
3403
3404 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003405 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003406 struct kvm_mmu_page *child;
3407 child = page_header(ent & PT64_BASE_ADDR_MASK);
3408 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003409 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003410 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003411 }
3412 }
3413}
3414
3415static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3416{
3417 int i;
3418 struct kvm_mmu_page *sp;
3419
3420 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3421 return;
3422 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3423 hpa_t root = vcpu->arch.mmu.root_hpa;
3424 sp = page_header(root);
3425 __mmu_spte_walk(vcpu->kvm, sp, fn);
3426 return;
3427 }
3428 for (i = 0; i < 4; ++i) {
3429 hpa_t root = vcpu->arch.mmu.pae_root[i];
3430
3431 if (root && VALID_PAGE(root)) {
3432 root &= PT64_BASE_ADDR_MASK;
3433 sp = page_header(root);
3434 __mmu_spte_walk(vcpu->kvm, sp, fn);
3435 }
3436 }
3437 return;
3438}
3439
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003440static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3441 gva_t va, int level)
3442{
3443 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3444 int i;
3445 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3446
3447 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3448 u64 ent = pt[i];
3449
Avi Kivityc7addb92007-09-16 18:58:32 +02003450 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003451 continue;
3452
3453 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003454 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3455 audit_mappings_page(vcpu, ent, va, level - 1);
3456 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003457 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003458 gfn_t gfn = gpa >> PAGE_SHIFT;
3459 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3460 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003461
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003462 if (is_error_pfn(pfn)) {
3463 kvm_release_pfn_clean(pfn);
3464 continue;
3465 }
3466
Avi Kivityc7addb92007-09-16 18:58:32 +02003467 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003468 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003469 printk(KERN_ERR "xx audit error: (%s) levels %d"
3470 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003471 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003472 va, gpa, hpa, ent,
3473 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003474 else if (ent == shadow_notrap_nonpresent_pte
3475 && !is_error_hpa(hpa))
3476 printk(KERN_ERR "audit: (%s) notrap shadow,"
3477 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003478 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003479
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003480 }
3481 }
3482}
3483
3484static void audit_mappings(struct kvm_vcpu *vcpu)
3485{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003486 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003487
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003488 if (vcpu->arch.mmu.root_level == 4)
3489 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003490 else
3491 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003492 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003493 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003494 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003495 i << 30,
3496 2);
3497}
3498
3499static int count_rmaps(struct kvm_vcpu *vcpu)
3500{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003501 struct kvm *kvm = vcpu->kvm;
3502 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003503 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003504 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003505
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003506 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003507 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003508 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003509 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003510 struct kvm_rmap_desc *d;
3511
3512 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003513 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003514
Izik Eidus290fc382007-09-27 14:11:22 +02003515 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003516 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003517 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003518 ++nmaps;
3519 continue;
3520 }
Izik Eidus290fc382007-09-27 14:11:22 +02003521 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003522 while (d) {
3523 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003524 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003525 ++nmaps;
3526 else
3527 break;
3528 d = d->more;
3529 }
3530 }
3531 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003532 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003533 return nmaps;
3534}
3535
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003536void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003537{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003538 unsigned long *rmapp;
3539 struct kvm_mmu_page *rev_sp;
3540 gfn_t gfn;
3541
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003542 if (is_writable_pte(*sptep)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003543 rev_sp = page_header(__pa(sptep));
Lai Jiangshan2032a932010-05-26 16:49:59 +08003544 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003545
3546 if (!gfn_to_memslot(kvm, gfn)) {
3547 if (!printk_ratelimit())
3548 return;
3549 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3550 audit_msg, gfn);
3551 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003552 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003553 rev_sp->gfn);
3554 dump_stack();
3555 return;
3556 }
3557
Lai Jiangshan2032a932010-05-26 16:49:59 +08003558 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003559 if (!*rmapp) {
3560 if (!printk_ratelimit())
3561 return;
3562 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3563 audit_msg, *sptep);
3564 dump_stack();
3565 }
3566 }
3567
3568}
3569
3570void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3571{
3572 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3573}
3574
3575static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3576{
Avi Kivity4db35312007-11-21 15:28:32 +02003577 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003578 int i;
3579
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003580 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003581 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003582
Avi Kivity4db35312007-11-21 15:28:32 +02003583 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003584 continue;
3585
3586 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3587 u64 ent = pt[i];
3588
3589 if (!(ent & PT_PRESENT_MASK))
3590 continue;
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003591 if (!is_writable_pte(ent))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003592 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003593 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003594 }
3595 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003596 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003597}
3598
3599static void audit_rmap(struct kvm_vcpu *vcpu)
3600{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003601 check_writable_mappings_rmap(vcpu);
3602 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003603}
3604
3605static void audit_write_protection(struct kvm_vcpu *vcpu)
3606{
Avi Kivity4db35312007-11-21 15:28:32 +02003607 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003608 struct kvm_memory_slot *slot;
3609 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003610 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003611 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003612
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003613 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003614 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003615 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003616 if (sp->unsync)
3617 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003618
Avi Kivitya1f4d3952010-06-21 11:44:20 +03003619 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003620 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003621
3622 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3623 while (spte) {
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003624 if (is_writable_pte(*spte))
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003625 printk(KERN_ERR "%s: (%s) shadow page has "
3626 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003627 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003628 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003629 spte = rmap_next(vcpu->kvm, rmapp, spte);
3630 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003631 }
3632}
3633
3634static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3635{
3636 int olddbg = dbg;
3637
3638 dbg = 0;
3639 audit_msg = msg;
3640 audit_rmap(vcpu);
3641 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003642 if (strcmp("pre pte write", audit_msg) != 0)
3643 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003644 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003645 dbg = olddbg;
3646}
3647
3648#endif