blob: b666d8d106a919a45c6db571179d44b0ab71e88c [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020021#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030022#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040025#include <linux/types.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/highmem.h>
29#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020030#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030031#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050032#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020033#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080035#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
37#include <asm/page.h>
38#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020039#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020040#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040041
Joerg Roedel18552672008-02-07 13:47:41 +010042/*
43 * When setting this variable to true it enables Two-Dimensional-Paging
44 * where the hardware walks 2 page tables:
45 * 1. the guest-virtual to guest-physical
46 * 2. while doing 1. it walks guest-physical to host-physical
47 * If the hardware supports that we don't need to do shadow paging.
48 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050049bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010050
Avi Kivity37a7d8b2007-01-05 16:36:56 -080051#undef MMU_DEBUG
52
53#undef AUDIT
54
55#ifdef AUDIT
56static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
57#else
58static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
59#endif
60
61#ifdef MMU_DEBUG
62
63#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
64#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
65
66#else
67
68#define pgprintk(x...) do { } while (0)
69#define rmap_printk(x...) do { } while (0)
70
71#endif
72
73#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030074static int dbg = 0;
75module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080076#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080077
Marcelo Tosatti582801a2008-09-23 13:18:41 -030078static int oos_shadow = 1;
79module_param(oos_shadow, bool, 0644);
80
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080081#ifndef MMU_DEBUG
82#define ASSERT(x) do { } while (0)
83#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080084#define ASSERT(x) \
85 if (!(x)) { \
86 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
87 __FILE__, __LINE__, #x); \
88 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080089#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080090
Avi Kivity6aa8b732006-12-10 02:21:36 -080091#define PT_FIRST_AVAIL_BITS_SHIFT 9
92#define PT64_SECOND_AVAIL_BITS_SHIFT 52
93
Avi Kivity6aa8b732006-12-10 02:21:36 -080094#define VALID_PAGE(x) ((x) != INVALID_PAGE)
95
96#define PT64_LEVEL_BITS 9
97
98#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040099 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
101#define PT64_LEVEL_MASK(level) \
102 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
103
104#define PT64_INDEX(address, level)\
105 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
106
107
108#define PT32_LEVEL_BITS 10
109
110#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400111 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
113#define PT32_LEVEL_MASK(level) \
114 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200115#define PT32_LVL_OFFSET_MASK(level) \
116 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
117 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118
119#define PT32_INDEX(address, level)\
120 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
121
122
Avi Kivity27aba762007-03-09 13:04:31 +0200123#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124#define PT64_DIR_BASE_ADDR_MASK \
125 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200126#define PT64_LVL_ADDR_MASK(level) \
127 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
128 * PT64_LEVEL_BITS))) - 1))
129#define PT64_LVL_OFFSET_MASK(level) \
130 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
131 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
133#define PT32_BASE_ADDR_MASK PAGE_MASK
134#define PT32_DIR_BASE_ADDR_MASK \
135 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200136#define PT32_LVL_ADDR_MASK(level) \
137 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
138 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139
Avi Kivity79539ce2007-11-21 02:06:21 +0200140#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
141 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800143#define RMAP_EXT 4
144
Avi Kivityfe135d22007-12-09 16:15:46 +0200145#define ACC_EXEC_MASK 1
146#define ACC_WRITE_MASK PT_WRITABLE_MASK
147#define ACC_USER_MASK PT_USER_MASK
148#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
149
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200150#include <trace/events/kvm.h>
151
Avi Kivity07420172009-07-06 12:21:32 +0300152#define CREATE_TRACE_POINTS
153#include "mmutrace.h"
154
Izik Eidus14032832009-09-23 21:47:17 +0300155#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
156
Avi Kivity135f8c22008-08-21 17:49:56 +0300157#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
158
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800159struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300160 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800161 struct kvm_rmap_desc *more;
162};
163
Avi Kivity2d111232008-12-25 14:39:47 +0200164struct kvm_shadow_walk_iterator {
165 u64 addr;
166 hpa_t shadow_addr;
167 int level;
168 u64 *sptep;
169 unsigned index;
170};
171
172#define for_each_shadow_entry(_vcpu, _addr, _walker) \
173 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
174 shadow_walk_okay(&(_walker)); \
175 shadow_walk_next(&(_walker)))
176
Xiao Guangrong6b184932010-04-16 21:29:17 +0800177typedef int (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300178
Avi Kivityb5a33a72007-04-15 16:31:09 +0300179static struct kmem_cache *pte_chain_cache;
180static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300181static struct kmem_cache *mmu_page_header_cache;
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 Kivityda9285212007-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{
285#ifdef CONFIG_X86_64
286 set_64bit((unsigned long *)sptep, spte);
287#else
288 set_64bit((unsigned long long *)sptep, spte);
289#endif
290}
291
Avi Kivitye2dec932007-01-05 16:36:54 -0800292static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300293 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800294{
295 void *obj;
296
297 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800298 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800299 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300300 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800301 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800302 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800303 cache->objects[cache->nobjs++] = obj;
304 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800305 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800306}
307
308static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
309{
310 while (mc->nobjs)
311 kfree(mc->objects[--mc->nobjs]);
312}
313
Avi Kivityc1158e62007-07-20 08:18:27 +0300314static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300315 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300316{
317 struct page *page;
318
319 if (cache->nobjs >= min)
320 return 0;
321 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300322 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300323 if (!page)
324 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300325 cache->objects[cache->nobjs++] = page_address(page);
326 }
327 return 0;
328}
329
330static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
331{
332 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300333 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300334}
335
Avi Kivity8c438502007-04-16 11:53:17 +0300336static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
337{
338 int r;
339
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800340 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300341 pte_chain_cache, 4);
342 if (r)
343 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800344 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200345 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300346 if (r)
347 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800348 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300349 if (r)
350 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800351 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300352 mmu_page_header_cache, 4);
353out:
Avi Kivity8c438502007-04-16 11:53:17 +0300354 return r;
355}
356
Avi Kivity714b93d2007-01-05 16:36:53 -0800357static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
358{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
360 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
361 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
362 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800363}
364
365static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
366 size_t size)
367{
368 void *p;
369
370 BUG_ON(!mc->nobjs);
371 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800372 return p;
373}
374
Avi Kivity714b93d2007-01-05 16:36:53 -0800375static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800377 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800378 sizeof(struct kvm_pte_chain));
379}
380
Avi Kivity90cb0522007-07-17 13:04:56 +0300381static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800382{
Avi Kivity90cb0522007-07-17 13:04:56 +0300383 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800384}
385
386static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
387{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800388 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800389 sizeof(struct kvm_rmap_desc));
390}
391
Avi Kivity90cb0522007-07-17 13:04:56 +0300392static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800393{
Avi Kivity90cb0522007-07-17 13:04:56 +0300394 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800395}
396
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800397/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300398 * Return the pointer to the largepage write count for a given
399 * gfn, handling slots that are not large page aligned.
400 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200401static int *slot_largepage_idx(gfn_t gfn,
402 struct kvm_memory_slot *slot,
403 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300404{
405 unsigned long idx;
406
Joerg Roedeld25797b2009-07-27 16:30:43 +0200407 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
408 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
409 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300410}
411
412static void account_shadowed(struct kvm *kvm, gfn_t gfn)
413{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200414 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300415 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200416 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300417
Izik Eidus28430992008-10-03 17:40:32 +0300418 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200419
420 slot = gfn_to_memslot_unaliased(kvm, gfn);
421 for (i = PT_DIRECTORY_LEVEL;
422 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
423 write_count = slot_largepage_idx(gfn, slot, i);
424 *write_count += 1;
425 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300426}
427
428static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
429{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200430 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300431 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200432 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300433
Izik Eidus28430992008-10-03 17:40:32 +0300434 gfn = unalias_gfn(kvm, gfn);
Wei Yongjun77a1a712010-04-16 16:21:42 +0800435 slot = gfn_to_memslot_unaliased(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200436 for (i = PT_DIRECTORY_LEVEL;
437 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438 write_count = slot_largepage_idx(gfn, slot, i);
439 *write_count -= 1;
440 WARN_ON(*write_count < 0);
441 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300442}
443
Joerg Roedeld25797b2009-07-27 16:30:43 +0200444static int has_wrprotected_page(struct kvm *kvm,
445 gfn_t gfn,
446 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300447{
Izik Eidus28430992008-10-03 17:40:32 +0300448 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300449 int *largepage_idx;
450
Izik Eidus28430992008-10-03 17:40:32 +0300451 gfn = unalias_gfn(kvm, gfn);
452 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200454 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455 return *largepage_idx;
456 }
457
458 return 1;
459}
460
Joerg Roedeld25797b2009-07-27 16:30:43 +0200461static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300462{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100463 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200464 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300465
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100466 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300467
Joerg Roedeld25797b2009-07-27 16:30:43 +0200468 for (i = PT_PAGE_TABLE_LEVEL;
469 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
470 if (page_size >= KVM_HPAGE_SIZE(i))
471 ret = i;
472 else
473 break;
474 }
475
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300476 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477}
478
Joerg Roedeld25797b2009-07-27 16:30:43 +0200479static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300480{
481 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800482 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300483
484 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
485 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200486 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300487
Joerg Roedeld25797b2009-07-27 16:30:43 +0200488 host_level = host_mapping_level(vcpu->kvm, large_gfn);
489
490 if (host_level == PT_PAGE_TABLE_LEVEL)
491 return host_level;
492
Sheng Yang878403b2010-01-05 19:02:29 +0800493 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
494 kvm_x86_ops->get_lpage_level() : host_level;
495
496 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200497 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
498 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200499
500 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300501}
502
503/*
Izik Eidus290fc382007-09-27 14:11:22 +0200504 * Take gfn and return the reverse mapping to it.
505 * Note: gfn must be unaliased before this function get called
506 */
507
Joerg Roedel44ad9942009-07-27 16:30:42 +0200508static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200509{
510 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300511 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200512
513 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200514 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300515 return &slot->rmap[gfn - slot->base_gfn];
516
Joerg Roedel44ad9942009-07-27 16:30:42 +0200517 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
518 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300519
Joerg Roedel44ad9942009-07-27 16:30:42 +0200520 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200521}
522
523/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 * Reverse mapping data structures:
525 *
Izik Eidus290fc382007-09-27 14:11:22 +0200526 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
527 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800528 *
Izik Eidus290fc382007-09-27 14:11:22 +0200529 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
530 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300531 *
532 * Returns the number of rmap entries before the spte was added or zero if
533 * the spte was not added.
534 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800535 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200536static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800537{
Avi Kivity4db35312007-11-21 15:28:32 +0200538 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800539 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200540 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300541 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800542
Avi Kivity43a37952009-06-10 14:12:05 +0300543 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300544 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200545 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200546 sp = page_header(__pa(spte));
547 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200548 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200549 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200551 *rmapp = (unsigned long)spte;
552 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800554 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300555 desc->sptes[0] = (u64 *)*rmapp;
556 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200557 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 } else {
559 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200560 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300561 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300563 count += RMAP_EXT;
564 }
Avi Kivityd555c332009-06-10 14:24:23 +0300565 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800566 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800567 desc = desc->more;
568 }
Avi Kivityd555c332009-06-10 14:24:23 +0300569 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800570 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300571 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300573 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800574}
575
Izik Eidus290fc382007-09-27 14:11:22 +0200576static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 struct kvm_rmap_desc *desc,
578 int i,
579 struct kvm_rmap_desc *prev_desc)
580{
581 int j;
582
Avi Kivityd555c332009-06-10 14:24:23 +0300583 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800584 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300585 desc->sptes[i] = desc->sptes[j];
586 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800587 if (j != 0)
588 return;
589 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300590 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800591 else
592 if (prev_desc)
593 prev_desc->more = desc->more;
594 else
Izik Eidus290fc382007-09-27 14:11:22 +0200595 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300596 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800597}
598
Izik Eidus290fc382007-09-27 14:11:22 +0200599static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800601 struct kvm_rmap_desc *desc;
602 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200603 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500604 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200605 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 int i;
607
Avi Kivity43a37952009-06-10 14:12:05 +0300608 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200610 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500611 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800612 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500613 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900614 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300615 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200616 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200617 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800618 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
619 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200620 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800621 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200622 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
624 spte, *spte);
625 BUG();
626 }
Izik Eidus290fc382007-09-27 14:11:22 +0200627 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800628 } else {
629 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200630 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800631 prev_desc = NULL;
632 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300633 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
634 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200635 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800636 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800637 prev_desc);
638 return;
639 }
640 prev_desc = desc;
641 desc = desc->more;
642 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200643 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800644 BUG();
645 }
646}
647
Izik Eidus98348e92007-10-16 14:42:30 +0200648static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800649{
Avi Kivity374cbac2007-01-05 16:36:43 -0800650 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200651 u64 *prev_spte;
652 int i;
653
654 if (!*rmapp)
655 return NULL;
656 else if (!(*rmapp & 1)) {
657 if (!spte)
658 return (u64 *)*rmapp;
659 return NULL;
660 }
661 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200662 prev_spte = NULL;
663 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300664 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200665 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300666 return desc->sptes[i];
667 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200668 }
669 desc = desc->more;
670 }
671 return NULL;
672}
673
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200674static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200675{
Izik Eidus290fc382007-09-27 14:11:22 +0200676 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800677 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200678 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800679
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500680 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200681 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800682
Izik Eidus98348e92007-10-16 14:42:30 +0200683 spte = rmap_next(kvm, rmapp, NULL);
684 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800685 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800686 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800687 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900688 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300689 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800690 write_protected = 1;
691 }
Izik Eidus9647c142007-10-16 14:43:46 +0200692 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800693 }
Izik Eidus855149a2008-03-20 18:17:24 +0200694 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500695 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200696
697 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500698 pfn = spte_to_pfn(*spte);
699 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200700 }
701
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300702 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200703 for (i = PT_DIRECTORY_LEVEL;
704 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
705 rmapp = gfn_to_rmap(kvm, gfn, i);
706 spte = rmap_next(kvm, rmapp, NULL);
707 while (spte) {
708 BUG_ON(!spte);
709 BUG_ON(!(*spte & PT_PRESENT_MASK));
710 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
711 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900712 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200713 rmap_remove(kvm, spte);
714 --kvm->stat.lpages;
715 __set_spte(spte, shadow_trap_nonpresent_pte);
716 spte = NULL;
717 write_protected = 1;
718 }
719 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300720 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300721 }
722
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200723 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800724}
725
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000726static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
727 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200728{
729 u64 *spte;
730 int need_tlb_flush = 0;
731
732 while ((spte = rmap_next(kvm, rmapp, NULL))) {
733 BUG_ON(!(*spte & PT_PRESENT_MASK));
734 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
735 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300736 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200737 need_tlb_flush = 1;
738 }
739 return need_tlb_flush;
740}
741
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000742static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
743 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300744{
745 int need_flush = 0;
746 u64 *spte, new_spte;
747 pte_t *ptep = (pte_t *)data;
748 pfn_t new_pfn;
749
750 WARN_ON(pte_huge(*ptep));
751 new_pfn = pte_pfn(*ptep);
752 spte = rmap_next(kvm, rmapp, NULL);
753 while (spte) {
754 BUG_ON(!is_shadow_present_pte(*spte));
755 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
756 need_flush = 1;
757 if (pte_write(*ptep)) {
758 rmap_remove(kvm, spte);
759 __set_spte(spte, shadow_trap_nonpresent_pte);
760 spte = rmap_next(kvm, rmapp, NULL);
761 } else {
762 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
763 new_spte |= (u64)new_pfn << PAGE_SHIFT;
764
765 new_spte &= ~PT_WRITABLE_MASK;
766 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900767 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300768 kvm_set_pfn_dirty(spte_to_pfn(*spte));
769 __set_spte(spte, new_spte);
770 spte = rmap_next(kvm, rmapp, spte);
771 }
772 }
773 if (need_flush)
774 kvm_flush_remote_tlbs(kvm);
775
776 return 0;
777}
778
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000779static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
780 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300781 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000782 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200783{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200784 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200785 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200786 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200787 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200788
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800789 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200790
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200791 for (i = 0; i < slots->nmemslots; i++) {
792 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200793 unsigned long start = memslot->userspace_addr;
794 unsigned long end;
795
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200796 end = start + (memslot->npages << PAGE_SHIFT);
797 if (hva >= start && hva < end) {
798 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200799
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200800 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200801
802 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
803 int idx = gfn_offset;
804 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200805 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300806 &memslot->lpage_info[j][idx].rmap_pde,
807 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200808 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200809 trace_kvm_age_page(hva, memslot, ret);
810 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200811 }
812 }
813
814 return retval;
815}
816
817int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
818{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300819 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200820}
821
Izik Eidus3da0dd42009-09-23 21:47:18 +0300822void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
823{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000824 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300825}
826
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000827static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
828 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200829{
830 u64 *spte;
831 int young = 0;
832
Rik van Riel6316e1c2010-02-03 16:11:03 -0500833 /*
834 * Emulate the accessed bit for EPT, by checking if this page has
835 * an EPT mapping, and clearing it if it does. On the next access,
836 * a new EPT mapping will be established.
837 * This has some overhead, but not as much as the cost of swapping
838 * out actively used pages or breaking up actively used hugepages.
839 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800840 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500841 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800842
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200843 spte = rmap_next(kvm, rmapp, NULL);
844 while (spte) {
845 int _young;
846 u64 _spte = *spte;
847 BUG_ON(!(_spte & PT_PRESENT_MASK));
848 _young = _spte & PT_ACCESSED_MASK;
849 if (_young) {
850 young = 1;
851 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
852 }
853 spte = rmap_next(kvm, rmapp, spte);
854 }
855 return young;
856}
857
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300858#define RMAP_RECYCLE_THRESHOLD 1000
859
Joerg Roedel852e3c12009-07-27 16:30:44 +0200860static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300861{
862 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200863 struct kvm_mmu_page *sp;
864
865 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300866
867 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200868 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300869
Izik Eidus3da0dd42009-09-23 21:47:18 +0300870 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300871 kvm_flush_remote_tlbs(vcpu->kvm);
872}
873
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200874int kvm_age_hva(struct kvm *kvm, unsigned long hva)
875{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300876 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200877}
878
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800879#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300880static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881{
Avi Kivity139bdb22007-01-05 16:36:50 -0800882 u64 *pos;
883 u64 *end;
884
Avi Kivity47ad8e62007-05-06 15:50:58 +0300885 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300886 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800887 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800888 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800890 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return 1;
892}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800893#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
Avi Kivity4db35312007-11-21 15:28:32 +0200895static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800896{
Avi Kivity4db35312007-11-21 15:28:32 +0200897 ASSERT(is_empty_shadow_page(sp->spt));
898 list_del(&sp->link);
899 __free_page(virt_to_page(sp->spt));
900 __free_page(virt_to_page(sp->gfns));
901 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800902 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800903}
904
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800905static unsigned kvm_page_table_hashfn(gfn_t gfn)
906{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200907 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800908}
909
Avi Kivity25c0de22007-01-05 16:36:42 -0800910static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
911 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912{
Avi Kivity4db35312007-11-21 15:28:32 +0200913 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800915 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
916 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
917 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200918 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800919 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800920 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200921 sp->multimapped = 0;
922 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800923 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200924 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925}
926
Avi Kivity714b93d2007-01-05 16:36:53 -0800927static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200928 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800929{
930 struct kvm_pte_chain *pte_chain;
931 struct hlist_node *node;
932 int i;
933
934 if (!parent_pte)
935 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200936 if (!sp->multimapped) {
937 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800938
939 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200940 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800941 return;
942 }
Avi Kivity4db35312007-11-21 15:28:32 +0200943 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800944 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200945 INIT_HLIST_HEAD(&sp->parent_ptes);
946 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947 pte_chain->parent_ptes[0] = old;
948 }
Avi Kivity4db35312007-11-21 15:28:32 +0200949 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800950 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
951 continue;
952 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
953 if (!pte_chain->parent_ptes[i]) {
954 pte_chain->parent_ptes[i] = parent_pte;
955 return;
956 }
957 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800958 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800959 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200960 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800961 pte_chain->parent_ptes[0] = parent_pte;
962}
963
Avi Kivity4db35312007-11-21 15:28:32 +0200964static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800965 u64 *parent_pte)
966{
967 struct kvm_pte_chain *pte_chain;
968 struct hlist_node *node;
969 int i;
970
Avi Kivity4db35312007-11-21 15:28:32 +0200971 if (!sp->multimapped) {
972 BUG_ON(sp->parent_pte != parent_pte);
973 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800974 return;
975 }
Avi Kivity4db35312007-11-21 15:28:32 +0200976 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800977 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
978 if (!pte_chain->parent_ptes[i])
979 break;
980 if (pte_chain->parent_ptes[i] != parent_pte)
981 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800982 while (i + 1 < NR_PTE_CHAIN_ENTRIES
983 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 pte_chain->parent_ptes[i]
985 = pte_chain->parent_ptes[i + 1];
986 ++i;
987 }
988 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800989 if (i == 0) {
990 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300991 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200992 if (hlist_empty(&sp->parent_ptes)) {
993 sp->multimapped = 0;
994 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800995 }
996 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800997 return;
998 }
999 BUG();
1000}
1001
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001002
Xiao Guangrong6b184932010-04-16 21:29:17 +08001003static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001004{
1005 struct kvm_pte_chain *pte_chain;
1006 struct hlist_node *node;
1007 struct kvm_mmu_page *parent_sp;
1008 int i;
1009
1010 if (!sp->multimapped && sp->parent_pte) {
1011 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001012 fn(parent_sp);
1013 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001014 return;
1015 }
1016 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1017 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1018 if (!pte_chain->parent_ptes[i])
1019 break;
1020 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001021 fn(parent_sp);
1022 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001023 }
1024}
1025
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001026static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1027{
1028 unsigned int index;
1029 struct kvm_mmu_page *sp = page_header(__pa(spte));
1030
1031 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001032 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1033 sp->unsync_children++;
1034 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001035}
1036
1037static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1038{
1039 struct kvm_pte_chain *pte_chain;
1040 struct hlist_node *node;
1041 int i;
1042
1043 if (!sp->parent_pte)
1044 return;
1045
1046 if (!sp->multimapped) {
1047 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1048 return;
1049 }
1050
1051 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1052 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1053 if (!pte_chain->parent_ptes[i])
1054 break;
1055 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1056 }
1057}
1058
Xiao Guangrong6b184932010-04-16 21:29:17 +08001059static int unsync_walk_fn(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001060{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001061 kvm_mmu_update_parents_unsync(sp);
1062 return 1;
1063}
1064
Xiao Guangrong6b184932010-04-16 21:29:17 +08001065static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001066{
Xiao Guangrong6b184932010-04-16 21:29:17 +08001067 mmu_parent_walk(sp, unsync_walk_fn);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001068 kvm_mmu_update_parents_unsync(sp);
1069}
1070
Avi Kivityd761a502008-05-29 14:55:03 +03001071static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1072 struct kvm_mmu_page *sp)
1073{
1074 int i;
1075
1076 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1077 sp->spt[i] = shadow_trap_nonpresent_pte;
1078}
1079
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001080static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1081 struct kvm_mmu_page *sp)
1082{
1083 return 1;
1084}
1085
Marcelo Tosattia7052892008-09-23 13:18:35 -03001086static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1087{
1088}
1089
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001090#define KVM_PAGE_ARRAY_NR 16
1091
1092struct kvm_mmu_pages {
1093 struct mmu_page_and_offset {
1094 struct kvm_mmu_page *sp;
1095 unsigned int idx;
1096 } page[KVM_PAGE_ARRAY_NR];
1097 unsigned int nr;
1098};
1099
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001100#define for_each_unsync_children(bitmap, idx) \
1101 for (idx = find_first_bit(bitmap, 512); \
1102 idx < 512; \
1103 idx = find_next_bit(bitmap, 512, idx+1))
1104
Hannes Edercded19f2009-02-21 02:19:13 +01001105static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1106 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001107{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001108 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001109
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001110 if (sp->unsync)
1111 for (i=0; i < pvec->nr; i++)
1112 if (pvec->page[i].sp == sp)
1113 return 0;
1114
1115 pvec->page[pvec->nr].sp = sp;
1116 pvec->page[pvec->nr].idx = idx;
1117 pvec->nr++;
1118 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1119}
1120
1121static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1122 struct kvm_mmu_pages *pvec)
1123{
1124 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001125
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001126 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001127 u64 ent = sp->spt[i];
1128
Marcelo Tosatti87917232008-12-22 18:49:30 -02001129 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001130 struct kvm_mmu_page *child;
1131 child = page_header(ent & PT64_BASE_ADDR_MASK);
1132
1133 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001134 if (mmu_pages_add(pvec, child, i))
1135 return -ENOSPC;
1136
1137 ret = __mmu_unsync_walk(child, pvec);
1138 if (!ret)
1139 __clear_bit(i, sp->unsync_child_bitmap);
1140 else if (ret > 0)
1141 nr_unsync_leaf += ret;
1142 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001143 return ret;
1144 }
1145
1146 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001147 nr_unsync_leaf++;
1148 if (mmu_pages_add(pvec, child, i))
1149 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001150 }
1151 }
1152 }
1153
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001154 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001155 sp->unsync_children = 0;
1156
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001157 return nr_unsync_leaf;
1158}
1159
1160static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1161 struct kvm_mmu_pages *pvec)
1162{
1163 if (!sp->unsync_children)
1164 return 0;
1165
1166 mmu_pages_add(pvec, sp, 0);
1167 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001168}
1169
Avi Kivity4db35312007-11-21 15:28:32 +02001170static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001171{
1172 unsigned index;
1173 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001174 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001175 struct hlist_node *node;
1176
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001177 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001178 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001179 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001180 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001181 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001182 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001183 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001184 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001185 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001186 }
1187 return NULL;
1188}
1189
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001190static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1191{
1192 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001193 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001194 sp->unsync = 0;
1195 --kvm->stat.mmu_unsync;
1196}
1197
1198static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1199
1200static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1201{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001202 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001203 kvm_mmu_zap_page(vcpu->kvm, sp);
1204 return 1;
1205 }
1206
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001207 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1208 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001209 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001210 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1211 kvm_mmu_zap_page(vcpu->kvm, sp);
1212 return 1;
1213 }
1214
1215 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001216 return 0;
1217}
1218
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001219struct mmu_page_path {
1220 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1221 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001222};
1223
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001224#define for_each_sp(pvec, sp, parents, i) \
1225 for (i = mmu_pages_next(&pvec, &parents, -1), \
1226 sp = pvec.page[i].sp; \
1227 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1228 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001229
Hannes Edercded19f2009-02-21 02:19:13 +01001230static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1231 struct mmu_page_path *parents,
1232 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001233{
1234 int n;
1235
1236 for (n = i+1; n < pvec->nr; n++) {
1237 struct kvm_mmu_page *sp = pvec->page[n].sp;
1238
1239 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1240 parents->idx[0] = pvec->page[n].idx;
1241 return n;
1242 }
1243
1244 parents->parent[sp->role.level-2] = sp;
1245 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1246 }
1247
1248 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001249}
1250
Hannes Edercded19f2009-02-21 02:19:13 +01001251static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001252{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001253 struct kvm_mmu_page *sp;
1254 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001255
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001256 do {
1257 unsigned int idx = parents->idx[level];
1258
1259 sp = parents->parent[level];
1260 if (!sp)
1261 return;
1262
1263 --sp->unsync_children;
1264 WARN_ON((int)sp->unsync_children < 0);
1265 __clear_bit(idx, sp->unsync_child_bitmap);
1266 level++;
1267 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1268}
1269
1270static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1271 struct mmu_page_path *parents,
1272 struct kvm_mmu_pages *pvec)
1273{
1274 parents->parent[parent->role.level-1] = NULL;
1275 pvec->nr = 0;
1276}
1277
1278static void mmu_sync_children(struct kvm_vcpu *vcpu,
1279 struct kvm_mmu_page *parent)
1280{
1281 int i;
1282 struct kvm_mmu_page *sp;
1283 struct mmu_page_path parents;
1284 struct kvm_mmu_pages pages;
1285
1286 kvm_mmu_pages_init(parent, &parents, &pages);
1287 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001288 int protected = 0;
1289
1290 for_each_sp(pages, sp, parents, i)
1291 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1292
1293 if (protected)
1294 kvm_flush_remote_tlbs(vcpu->kvm);
1295
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001296 for_each_sp(pages, sp, parents, i) {
1297 kvm_sync_page(vcpu, sp);
1298 mmu_pages_clear_parents(&parents);
1299 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001300 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001301 kvm_mmu_pages_init(parent, &parents, &pages);
1302 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001303}
1304
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001305static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1306 gfn_t gfn,
1307 gva_t gaddr,
1308 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001309 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001310 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001311 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001312{
1313 union kvm_mmu_page_role role;
1314 unsigned index;
1315 unsigned quadrant;
1316 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001317 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001318 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001319
Avi Kivitya770f6f2008-12-21 19:20:09 +02001320 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001321 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001322 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001323 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001324 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001325 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001326 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001327 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1328 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1329 role.quadrant = quadrant;
1330 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001331 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001332 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001333 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1334 if (sp->gfn == gfn) {
1335 if (sp->unsync)
1336 if (kvm_sync_page(vcpu, sp))
1337 continue;
1338
1339 if (sp->role.word != role.word)
1340 continue;
1341
Avi Kivity4db35312007-11-21 15:28:32 +02001342 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001343 if (sp->unsync_children) {
1344 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001345 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001346 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001347 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001348 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001349 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001350 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001351 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1352 if (!sp)
1353 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001354 sp->gfn = gfn;
1355 sp->role = role;
1356 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001357 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001358 if (rmap_write_protect(vcpu->kvm, gfn))
1359 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001360 account_shadowed(vcpu->kvm, gfn);
1361 }
Avi Kivity131d8272008-05-29 14:56:28 +03001362 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1363 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1364 else
1365 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001366 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001367 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001368}
1369
Avi Kivity2d111232008-12-25 14:39:47 +02001370static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1371 struct kvm_vcpu *vcpu, u64 addr)
1372{
1373 iterator->addr = addr;
1374 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1375 iterator->level = vcpu->arch.mmu.shadow_root_level;
1376 if (iterator->level == PT32E_ROOT_LEVEL) {
1377 iterator->shadow_addr
1378 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1379 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1380 --iterator->level;
1381 if (!iterator->shadow_addr)
1382 iterator->level = 0;
1383 }
1384}
1385
1386static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1387{
1388 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1389 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001390
1391 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1392 if (is_large_pte(*iterator->sptep))
1393 return false;
1394
Avi Kivity2d111232008-12-25 14:39:47 +02001395 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1396 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1397 return true;
1398}
1399
1400static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1401{
1402 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1403 --iterator->level;
1404}
1405
Avi Kivity90cb0522007-07-17 13:04:56 +03001406static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001407 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001408{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001409 unsigned i;
1410 u64 *pt;
1411 u64 ent;
1412
Avi Kivity4db35312007-11-21 15:28:32 +02001413 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001414
Avi Kivity697fe2e2007-01-05 16:36:46 -08001415 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1416 ent = pt[i];
1417
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001418 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001419 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001420 ent &= PT64_BASE_ADDR_MASK;
1421 mmu_page_remove_parent_pte(page_header(ent),
1422 &pt[i]);
1423 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001424 if (is_large_pte(ent))
1425 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001426 rmap_remove(kvm, &pt[i]);
1427 }
1428 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001429 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001430 }
Avi Kivitya4360362007-01-05 16:36:45 -08001431}
1432
Avi Kivity4db35312007-11-21 15:28:32 +02001433static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001434{
Avi Kivity4db35312007-11-21 15:28:32 +02001435 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001436}
1437
Avi Kivity12b7d282007-09-23 14:10:49 +02001438static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1439{
1440 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001441 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001442
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001443 kvm_for_each_vcpu(i, vcpu, kvm)
1444 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001445}
1446
Avi Kivity31aa2b42008-07-11 17:59:46 +03001447static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001448{
1449 u64 *parent_pte;
1450
Avi Kivity4db35312007-11-21 15:28:32 +02001451 while (sp->multimapped || sp->parent_pte) {
1452 if (!sp->multimapped)
1453 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001454 else {
1455 struct kvm_pte_chain *chain;
1456
Avi Kivity4db35312007-11-21 15:28:32 +02001457 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001458 struct kvm_pte_chain, link);
1459 parent_pte = chain->parent_ptes[0];
1460 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001461 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001462 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001463 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001464 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001465}
1466
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001467static int mmu_zap_unsync_children(struct kvm *kvm,
1468 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001469{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001470 int i, zapped = 0;
1471 struct mmu_page_path parents;
1472 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001473
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001474 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001475 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001476
1477 kvm_mmu_pages_init(parent, &parents, &pages);
1478 while (mmu_unsync_walk(parent, &pages)) {
1479 struct kvm_mmu_page *sp;
1480
1481 for_each_sp(pages, sp, parents, i) {
1482 kvm_mmu_zap_page(kvm, sp);
1483 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001484 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001485 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001486 kvm_mmu_pages_init(parent, &parents, &pages);
1487 }
1488
1489 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001490}
1491
Marcelo Tosatti07385412008-09-23 13:18:37 -03001492static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001493{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001494 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001495
1496 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001497 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001498 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001499 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001500 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001501 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001502 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001503 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001504 if (sp->unsync)
1505 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001506 if (!sp->root_count) {
1507 hlist_del(&sp->hash_link);
1508 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001509 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001510 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001511 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001512 kvm_reload_remote_mmus(kvm);
1513 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001514 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001515 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001516}
1517
Izik Eidus82ce2c92007-10-02 18:52:55 +02001518/*
1519 * Changing the number of mmu pages allocated to the vm
1520 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1521 */
1522void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1523{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001524 int used_pages;
1525
1526 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1527 used_pages = max(0, used_pages);
1528
Izik Eidus82ce2c92007-10-02 18:52:55 +02001529 /*
1530 * If we set the number of mmu pages to be smaller be than the
1531 * number of actived pages , we must to free some mmu pages before we
1532 * change the value
1533 */
1534
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001535 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001536 while (used_pages > kvm_nr_mmu_pages &&
1537 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001538 struct kvm_mmu_page *page;
1539
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001540 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001541 struct kvm_mmu_page, link);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001542 used_pages -= kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001543 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001544 }
Xiao Guangrong77662e02010-04-16 16:34:42 +08001545 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001546 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001547 }
1548 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001549 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1550 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001551
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001552 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001553}
1554
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001555static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001556{
1557 unsigned index;
1558 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001559 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001560 struct hlist_node *node, *n;
1561 int r;
1562
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001563 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001564 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001565 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001566 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001567restart:
Avi Kivity4db35312007-11-21 15:28:32 +02001568 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001569 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001570 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001571 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001572 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001573 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001574 goto restart;
Avi Kivitya4360362007-01-05 16:36:45 -08001575 }
1576 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001577}
1578
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001579static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001580{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001581 unsigned index;
1582 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001583 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001584 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001585
Avi Kivity4677a3b2009-01-06 13:00:27 +02001586 index = kvm_page_table_hashfn(gfn);
1587 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001588restart:
Avi Kivity4677a3b2009-01-06 13:00:27 +02001589 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001590 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001591 && !sp->role.invalid) {
1592 pgprintk("%s: zap %lx %x\n",
1593 __func__, gfn, sp->role.word);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001594 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001595 goto restart;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001596 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001597 }
1598}
1599
Avi Kivity38c335f2007-11-21 14:20:22 +02001600static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001602 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001603 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604
Sheng Yang291f26b2008-10-16 17:30:57 +08001605 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606}
1607
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001608static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1609{
1610 int i;
1611 u64 *pt = sp->spt;
1612
1613 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1614 return;
1615
1616 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1617 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001618 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001619 }
1620}
1621
Sheng Yang74be52e2008-10-09 16:01:56 +08001622/*
1623 * The function is based on mtrr_type_lookup() in
1624 * arch/x86/kernel/cpu/mtrr/generic.c
1625 */
1626static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1627 u64 start, u64 end)
1628{
1629 int i;
1630 u64 base, mask;
1631 u8 prev_match, curr_match;
1632 int num_var_ranges = KVM_NR_VAR_MTRR;
1633
1634 if (!mtrr_state->enabled)
1635 return 0xFF;
1636
1637 /* Make end inclusive end, instead of exclusive */
1638 end--;
1639
1640 /* Look in fixed ranges. Just return the type as per start */
1641 if (mtrr_state->have_fixed && (start < 0x100000)) {
1642 int idx;
1643
1644 if (start < 0x80000) {
1645 idx = 0;
1646 idx += (start >> 16);
1647 return mtrr_state->fixed_ranges[idx];
1648 } else if (start < 0xC0000) {
1649 idx = 1 * 8;
1650 idx += ((start - 0x80000) >> 14);
1651 return mtrr_state->fixed_ranges[idx];
1652 } else if (start < 0x1000000) {
1653 idx = 3 * 8;
1654 idx += ((start - 0xC0000) >> 12);
1655 return mtrr_state->fixed_ranges[idx];
1656 }
1657 }
1658
1659 /*
1660 * Look in variable ranges
1661 * Look of multiple ranges matching this address and pick type
1662 * as per MTRR precedence
1663 */
1664 if (!(mtrr_state->enabled & 2))
1665 return mtrr_state->def_type;
1666
1667 prev_match = 0xFF;
1668 for (i = 0; i < num_var_ranges; ++i) {
1669 unsigned short start_state, end_state;
1670
1671 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1672 continue;
1673
1674 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1675 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1676 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1677 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1678
1679 start_state = ((start & mask) == (base & mask));
1680 end_state = ((end & mask) == (base & mask));
1681 if (start_state != end_state)
1682 return 0xFE;
1683
1684 if ((start & mask) != (base & mask))
1685 continue;
1686
1687 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1688 if (prev_match == 0xFF) {
1689 prev_match = curr_match;
1690 continue;
1691 }
1692
1693 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1694 curr_match == MTRR_TYPE_UNCACHABLE)
1695 return MTRR_TYPE_UNCACHABLE;
1696
1697 if ((prev_match == MTRR_TYPE_WRBACK &&
1698 curr_match == MTRR_TYPE_WRTHROUGH) ||
1699 (prev_match == MTRR_TYPE_WRTHROUGH &&
1700 curr_match == MTRR_TYPE_WRBACK)) {
1701 prev_match = MTRR_TYPE_WRTHROUGH;
1702 curr_match = MTRR_TYPE_WRTHROUGH;
1703 }
1704
1705 if (prev_match != curr_match)
1706 return MTRR_TYPE_UNCACHABLE;
1707 }
1708
1709 if (prev_match != 0xFF)
1710 return prev_match;
1711
1712 return mtrr_state->def_type;
1713}
1714
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001715u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001716{
1717 u8 mtrr;
1718
1719 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1720 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1721 if (mtrr == 0xfe || mtrr == 0xff)
1722 mtrr = MTRR_TYPE_WRBACK;
1723 return mtrr;
1724}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001725EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001726
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001727static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1728{
1729 unsigned index;
1730 struct hlist_head *bucket;
1731 struct kvm_mmu_page *s;
1732 struct hlist_node *node, *n;
1733
1734 index = kvm_page_table_hashfn(sp->gfn);
1735 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1736 /* don't unsync if pagetable is shadowed with multiple roles */
1737 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001738 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001739 continue;
1740 if (s->role.word != sp->role.word)
1741 return 1;
1742 }
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001743 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001744 ++vcpu->kvm->stat.mmu_unsync;
1745 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001746
Xiao Guangrong6b184932010-04-16 21:29:17 +08001747 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001748
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001749 mmu_convert_notrap(sp);
1750 return 0;
1751}
1752
1753static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1754 bool can_unsync)
1755{
1756 struct kvm_mmu_page *shadow;
1757
1758 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1759 if (shadow) {
1760 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1761 return 1;
1762 if (shadow->unsync)
1763 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001764 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001765 return kvm_unsync_page(vcpu, shadow);
1766 return 1;
1767 }
1768 return 0;
1769}
1770
Avi Kivityd555c332009-06-10 14:24:23 +03001771static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001772 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001773 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001774 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001775 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001776{
1777 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001778 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001779
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001780 /*
1781 * We don't set the accessed bit, since we sometimes want to see
1782 * whether the guest actually used the pte (in order to detect
1783 * demand paging).
1784 */
Sheng Yang7b523452008-04-25 21:13:50 +08001785 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001786 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001787 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001788 if (!dirty)
1789 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001790 if (pte_access & ACC_EXEC_MASK)
1791 spte |= shadow_x_mask;
1792 else
1793 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001794 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001795 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001796 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001797 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001798 if (tdp_enabled)
1799 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1800 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001801
Izik Eidus14032832009-09-23 21:47:17 +03001802 if (reset_host_protection)
1803 spte |= SPTE_HOST_WRITEABLE;
1804
Anthony Liguori35149e22008-04-02 14:46:56 -05001805 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001806
1807 if ((pte_access & ACC_WRITE_MASK)
1808 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001809
Joerg Roedel852e3c12009-07-27 16:30:44 +02001810 if (level > PT_PAGE_TABLE_LEVEL &&
1811 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001812 ret = 1;
1813 spte = shadow_trap_nonpresent_pte;
1814 goto set_pte;
1815 }
1816
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001818
Avi Kivity69325a12010-05-27 14:35:58 +03001819 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1820 spte &= ~PT_USER_MASK;
1821
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001822 /*
1823 * Optimization: for pte sync, if spte was writable the hash
1824 * lookup is unnecessary (and expensive). Write protection
1825 * is responsibility of mmu_get_page / kvm_sync_page.
1826 * Same reasoning can be applied to dirty page accounting.
1827 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001828 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001829 goto set_pte;
1830
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001831 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001832 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001833 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001834 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001835 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001836 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001837 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001838 }
1839 }
1840
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001841 if (pte_access & ACC_WRITE_MASK)
1842 mark_page_dirty(vcpu->kvm, gfn);
1843
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001844set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001845 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001846 return ret;
1847}
1848
Avi Kivityd555c332009-06-10 14:24:23 +03001849static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001850 unsigned pt_access, unsigned pte_access,
1851 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001852 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001853 pfn_t pfn, bool speculative,
1854 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001855{
1856 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001857 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001858 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001859
1860 pgprintk("%s: spte %llx access %x write_fault %d"
1861 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001862 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001863 write_fault, user_fault, gfn);
1864
Avi Kivityd555c332009-06-10 14:24:23 +03001865 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001866 /*
1867 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1868 * the parent of the now unreachable PTE.
1869 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001870 if (level > PT_PAGE_TABLE_LEVEL &&
1871 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001872 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001873 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001874
1875 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001876 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001877 __set_spte(sptep, shadow_trap_nonpresent_pte);
1878 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001879 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001880 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001881 spte_to_pfn(*sptep), pfn);
1882 rmap_remove(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08001883 __set_spte(sptep, shadow_trap_nonpresent_pte);
1884 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001885 } else
1886 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001887 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001888
Avi Kivityd555c332009-06-10 14:24:23 +03001889 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001890 dirty, level, gfn, pfn, speculative, true,
1891 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001892 if (write_fault)
1893 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001894 kvm_x86_ops->tlb_flush(vcpu);
1895 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001896
Avi Kivityd555c332009-06-10 14:24:23 +03001897 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001898 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001899 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001900 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1901 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001902 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001903 ++vcpu->kvm->stat.lpages;
1904
Avi Kivityd555c332009-06-10 14:24:23 +03001905 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001906 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001907 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001908 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001909 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001910 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001911 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001912 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001913 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001914 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001915 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001916 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001917 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001918 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001919 vcpu->arch.last_pte_gfn = gfn;
1920 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001921}
1922
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1924{
1925}
1926
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001927static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001928 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929{
Avi Kivity9f652d212008-12-25 14:54:25 +02001930 struct kvm_shadow_walk_iterator iterator;
1931 struct kvm_mmu_page *sp;
1932 int pt_write = 0;
1933 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934
Avi Kivity9f652d212008-12-25 14:54:25 +02001935 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001936 if (iterator.level == level) {
Avi Kivity9f652d212008-12-25 14:54:25 +02001937 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1938 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001939 level, gfn, pfn, false, true);
Avi Kivity9f652d212008-12-25 14:54:25 +02001940 ++vcpu->stat.pf_fixed;
1941 break;
1942 }
1943
1944 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1945 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1946 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1947 iterator.level - 1,
1948 1, ACC_ALL, iterator.sptep);
1949 if (!sp) {
1950 pgprintk("nonpaging_map: ENOMEM\n");
1951 kvm_release_pfn_clean(pfn);
1952 return -ENOMEM;
1953 }
1954
Avi Kivityd555c332009-06-10 14:24:23 +03001955 __set_spte(iterator.sptep,
1956 __pa(sp->spt)
1957 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1958 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02001959 }
1960 }
1961 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962}
1963
Huang Yingbf998152010-05-31 14:28:19 +08001964static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
1965{
1966 char buf[1];
1967 void __user *hva;
1968 int r;
1969
1970 /* Touch the page, so send SIGBUS */
1971 hva = (void __user *)gfn_to_hva(kvm, gfn);
1972 r = copy_from_user(buf, hva, 1);
1973}
1974
1975static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
1976{
1977 kvm_release_pfn_clean(pfn);
1978 if (is_hwpoison_pfn(pfn)) {
1979 kvm_send_hwpoison_signal(kvm, gfn);
1980 return 0;
1981 }
1982 return 1;
1983}
1984
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001985static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1986{
1987 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001988 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001989 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001990 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001991
Joerg Roedel852e3c12009-07-27 16:30:44 +02001992 level = mapping_level(vcpu, gfn);
1993
1994 /*
1995 * This path builds a PAE pagetable - so we can map 2mb pages at
1996 * maximum. Therefore check if the level is larger than that.
1997 */
1998 if (level > PT_DIRECTORY_LEVEL)
1999 level = PT_DIRECTORY_LEVEL;
2000
2001 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002002
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002003 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002004 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002005 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002006
Avi Kivityd196e342008-01-24 11:44:11 +02002007 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002008 if (is_error_pfn(pfn))
2009 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002010
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002011 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002012 if (mmu_notifier_retry(vcpu, mmu_seq))
2013 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002014 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002015 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002016 spin_unlock(&vcpu->kvm->mmu_lock);
2017
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002018
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002019 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002020
2021out_unlock:
2022 spin_unlock(&vcpu->kvm->mmu_lock);
2023 kvm_release_pfn_clean(pfn);
2024 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002025}
2026
2027
Avi Kivity17ac10a2007-01-05 16:36:40 -08002028static void mmu_free_roots(struct kvm_vcpu *vcpu)
2029{
2030 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002031 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002032
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002033 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002034 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002035 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002036 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2037 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038
Avi Kivity4db35312007-11-21 15:28:32 +02002039 sp = page_header(root);
2040 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002041 if (!sp->root_count && sp->role.invalid)
2042 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002043 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002044 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002045 return;
2046 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002047 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002048 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002049
Avi Kivity417726a2007-04-12 17:35:58 +03002050 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002051 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002052 sp = page_header(root);
2053 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002054 if (!sp->root_count && sp->role.invalid)
2055 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002056 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002057 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002058 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002059 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002060 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002061}
2062
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002063static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2064{
2065 int ret = 0;
2066
2067 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2068 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2069 ret = 1;
2070 }
2071
2072 return ret;
2073}
2074
2075static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002076{
2077 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002078 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002079 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002080 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002081 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002082
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002083 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002084
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002085 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2086 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002087
2088 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002089 if (mmu_check_root(vcpu, root_gfn))
2090 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002091 if (tdp_enabled) {
2092 direct = 1;
2093 root_gfn = 0;
2094 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002095 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity4db35312007-11-21 15:28:32 +02002096 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002097 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002098 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002099 root = __pa(sp->spt);
2100 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002101 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002102 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002103 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002104 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002105 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002106 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002107 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002108
2109 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002111 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002112 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002114 continue;
2115 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002116 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002117 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002118 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002119 if (mmu_check_root(vcpu, root_gfn))
2120 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002121 if (tdp_enabled) {
2122 direct = 1;
2123 root_gfn = i << 30;
2124 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002125 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity4db35312007-11-21 15:28:32 +02002126 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002127 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002128 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002129 root = __pa(sp->spt);
2130 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002131 spin_unlock(&vcpu->kvm->mmu_lock);
2132
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002133 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002134 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002135 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002136 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002137}
2138
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002139static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2140{
2141 int i;
2142 struct kvm_mmu_page *sp;
2143
2144 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2145 return;
2146 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2147 hpa_t root = vcpu->arch.mmu.root_hpa;
2148 sp = page_header(root);
2149 mmu_sync_children(vcpu, sp);
2150 return;
2151 }
2152 for (i = 0; i < 4; ++i) {
2153 hpa_t root = vcpu->arch.mmu.pae_root[i];
2154
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002155 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002156 root &= PT64_BASE_ADDR_MASK;
2157 sp = page_header(root);
2158 mmu_sync_children(vcpu, sp);
2159 }
2160 }
2161}
2162
2163void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2164{
2165 spin_lock(&vcpu->kvm->mmu_lock);
2166 mmu_sync_roots(vcpu);
2167 spin_unlock(&vcpu->kvm->mmu_lock);
2168}
2169
Gleb Natapov1871c602010-02-10 14:21:32 +02002170static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2171 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172{
Gleb Natapov1871c602010-02-10 14:21:32 +02002173 if (error)
2174 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175 return vaddr;
2176}
2177
2178static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002179 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180{
Avi Kivitye8332402007-12-09 18:43:00 +02002181 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002182 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002184 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002185 r = mmu_topup_memory_caches(vcpu);
2186 if (r)
2187 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002188
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002190 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191
Avi Kivitye8332402007-12-09 18:43:00 +02002192 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193
Avi Kivitye8332402007-12-09 18:43:00 +02002194 return nonpaging_map(vcpu, gva & PAGE_MASK,
2195 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196}
2197
Joerg Roedelfb72d162008-02-07 13:47:44 +01002198static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2199 u32 error_code)
2200{
Anthony Liguori35149e22008-04-02 14:46:56 -05002201 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002202 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002203 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002204 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002205 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002206
2207 ASSERT(vcpu);
2208 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2209
2210 r = mmu_topup_memory_caches(vcpu);
2211 if (r)
2212 return r;
2213
Joerg Roedel852e3c12009-07-27 16:30:44 +02002214 level = mapping_level(vcpu, gfn);
2215
2216 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2217
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002218 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002219 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002220 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002221 if (is_error_pfn(pfn))
2222 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002223 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002224 if (mmu_notifier_retry(vcpu, mmu_seq))
2225 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002226 kvm_mmu_free_some_pages(vcpu);
2227 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002228 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002229 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002230
2231 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002232
2233out_unlock:
2234 spin_unlock(&vcpu->kvm->mmu_lock);
2235 kvm_release_pfn_clean(pfn);
2236 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002237}
2238
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239static void nonpaging_free(struct kvm_vcpu *vcpu)
2240{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002241 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242}
2243
2244static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2245{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002246 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247
2248 context->new_cr3 = nonpaging_new_cr3;
2249 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 context->gva_to_gpa = nonpaging_gva_to_gpa;
2251 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002252 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002253 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002254 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002255 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002257 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258 return 0;
2259}
2260
Avi Kivityd835dfe2007-11-21 02:57:59 +02002261void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002263 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002264 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265}
2266
2267static void paging_new_cr3(struct kvm_vcpu *vcpu)
2268{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002269 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002270 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271}
2272
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273static void inject_page_fault(struct kvm_vcpu *vcpu,
2274 u64 addr,
2275 u32 err_code)
2276{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002277 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278}
2279
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280static void paging_free(struct kvm_vcpu *vcpu)
2281{
2282 nonpaging_free(vcpu);
2283}
2284
Dong, Eddie82725b22009-03-30 16:21:08 +08002285static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2286{
2287 int bit7;
2288
2289 bit7 = (gpte >> 7) & 1;
2290 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2291}
2292
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293#define PTTYPE 64
2294#include "paging_tmpl.h"
2295#undef PTTYPE
2296
2297#define PTTYPE 32
2298#include "paging_tmpl.h"
2299#undef PTTYPE
2300
Dong, Eddie82725b22009-03-30 16:21:08 +08002301static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2302{
2303 struct kvm_mmu *context = &vcpu->arch.mmu;
2304 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2305 u64 exb_bit_rsvd = 0;
2306
2307 if (!is_nx(vcpu))
2308 exb_bit_rsvd = rsvd_bits(63, 63);
2309 switch (level) {
2310 case PT32_ROOT_LEVEL:
2311 /* no rsvd bits for 2 level 4K page table entries */
2312 context->rsvd_bits_mask[0][1] = 0;
2313 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002314 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2315
2316 if (!is_pse(vcpu)) {
2317 context->rsvd_bits_mask[1][1] = 0;
2318 break;
2319 }
2320
Dong, Eddie82725b22009-03-30 16:21:08 +08002321 if (is_cpuid_PSE36())
2322 /* 36bits PSE 4MB page */
2323 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2324 else
2325 /* 32 bits PSE 4MB page */
2326 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002327 break;
2328 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002329 context->rsvd_bits_mask[0][2] =
2330 rsvd_bits(maxphyaddr, 63) |
2331 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002332 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002333 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002334 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2335 rsvd_bits(maxphyaddr, 62); /* PTE */
2336 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2337 rsvd_bits(maxphyaddr, 62) |
2338 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002339 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002340 break;
2341 case PT64_ROOT_LEVEL:
2342 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2343 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2344 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2345 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2346 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002347 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002348 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2349 rsvd_bits(maxphyaddr, 51);
2350 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002351 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2352 rsvd_bits(maxphyaddr, 51) |
2353 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002354 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002355 rsvd_bits(maxphyaddr, 51) |
2356 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002357 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002358 break;
2359 }
2360}
2361
Avi Kivity17ac10a2007-01-05 16:36:40 -08002362static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002364 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
2366 ASSERT(is_pae(vcpu));
2367 context->new_cr3 = paging_new_cr3;
2368 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002370 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002371 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002372 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002374 context->root_level = level;
2375 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002376 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 return 0;
2378}
2379
Avi Kivity17ac10a2007-01-05 16:36:40 -08002380static int paging64_init_context(struct kvm_vcpu *vcpu)
2381{
Dong, Eddie82725b22009-03-30 16:21:08 +08002382 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002383 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2384}
2385
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386static int paging32_init_context(struct kvm_vcpu *vcpu)
2387{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002388 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389
Dong, Eddie82725b22009-03-30 16:21:08 +08002390 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 context->new_cr3 = paging_new_cr3;
2392 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 context->gva_to_gpa = paging32_gva_to_gpa;
2394 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002395 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002396 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002397 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 context->root_level = PT32_ROOT_LEVEL;
2399 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002400 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 return 0;
2402}
2403
2404static int paging32E_init_context(struct kvm_vcpu *vcpu)
2405{
Dong, Eddie82725b22009-03-30 16:21:08 +08002406 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002407 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408}
2409
Joerg Roedelfb72d162008-02-07 13:47:44 +01002410static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2411{
2412 struct kvm_mmu *context = &vcpu->arch.mmu;
2413
2414 context->new_cr3 = nonpaging_new_cr3;
2415 context->page_fault = tdp_page_fault;
2416 context->free = nonpaging_free;
2417 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002418 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002419 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002420 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002421 context->root_hpa = INVALID_PAGE;
2422
2423 if (!is_paging(vcpu)) {
2424 context->gva_to_gpa = nonpaging_gva_to_gpa;
2425 context->root_level = 0;
2426 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002427 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002428 context->gva_to_gpa = paging64_gva_to_gpa;
2429 context->root_level = PT64_ROOT_LEVEL;
2430 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002431 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002432 context->gva_to_gpa = paging64_gva_to_gpa;
2433 context->root_level = PT32E_ROOT_LEVEL;
2434 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002435 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002436 context->gva_to_gpa = paging32_gva_to_gpa;
2437 context->root_level = PT32_ROOT_LEVEL;
2438 }
2439
2440 return 0;
2441}
2442
2443static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002445 int r;
2446
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002448 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449
2450 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002451 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002452 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002453 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002455 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002457 r = paging32_init_context(vcpu);
2458
Avi Kivity5b7e0102010-04-14 19:20:03 +03002459 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002460 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002461
2462 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463}
2464
Joerg Roedelfb72d162008-02-07 13:47:44 +01002465static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2466{
Anthony Liguori35149e22008-04-02 14:46:56 -05002467 vcpu->arch.update_pte.pfn = bad_pfn;
2468
Joerg Roedelfb72d162008-02-07 13:47:44 +01002469 if (tdp_enabled)
2470 return init_kvm_tdp_mmu(vcpu);
2471 else
2472 return init_kvm_softmmu(vcpu);
2473}
2474
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2476{
2477 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002478 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2479 vcpu->arch.mmu.free(vcpu);
2480 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 }
2482}
2483
2484int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2485{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002486 destroy_kvm_mmu(vcpu);
2487 return init_kvm_mmu(vcpu);
2488}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002489EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002490
2491int kvm_mmu_load(struct kvm_vcpu *vcpu)
2492{
Avi Kivity714b93d2007-01-05 16:36:53 -08002493 int r;
2494
Avi Kivitye2dec932007-01-05 16:36:54 -08002495 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002496 if (r)
2497 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002498 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002499 kvm_mmu_free_some_pages(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002500 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002501 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002502 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002503 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002504 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002505 if (r)
2506 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002507 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002508 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002509out:
2510 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002512EXPORT_SYMBOL_GPL(kvm_mmu_load);
2513
2514void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2515{
2516 mmu_free_roots(vcpu);
2517}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518
Avi Kivity09072da2007-05-01 14:16:52 +03002519static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002520 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002521 u64 *spte)
2522{
2523 u64 pte;
2524 struct kvm_mmu_page *child;
2525
2526 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002527 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002528 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002529 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002530 else {
2531 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002532 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002533 }
2534 }
Avi Kivityd555c332009-06-10 14:24:23 +03002535 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002536 if (is_large_pte(pte))
2537 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002538}
2539
Avi Kivity00284252007-05-01 16:53:31 +03002540static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002541 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002542 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002543 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002544{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002545 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002546 ++vcpu->kvm->stat.mmu_pde_zapped;
2547 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002548 }
Avi Kivity00284252007-05-01 16:53:31 +03002549
Avi Kivity4cee5762007-11-18 16:37:07 +02002550 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002551 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002552 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002553 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002554 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002555}
2556
Avi Kivity79539ce2007-11-21 02:06:21 +02002557static bool need_remote_flush(u64 old, u64 new)
2558{
2559 if (!is_shadow_present_pte(old))
2560 return false;
2561 if (!is_shadow_present_pte(new))
2562 return true;
2563 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2564 return true;
2565 old ^= PT64_NX_MASK;
2566 new ^= PT64_NX_MASK;
2567 return (old & ~new & PT64_PERM_MASK) != 0;
2568}
2569
2570static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2571{
2572 if (need_remote_flush(old, new))
2573 kvm_flush_remote_tlbs(vcpu->kvm);
2574 else
2575 kvm_mmu_flush_tlb(vcpu);
2576}
2577
Avi Kivity12b7d282007-09-23 14:10:49 +02002578static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2579{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002580 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002581
Sheng Yang7b523452008-04-25 21:13:50 +08002582 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002583}
2584
Avi Kivityd7824ff2007-12-30 12:29:05 +02002585static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002586 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002587{
2588 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002589 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002590
Avi Kivity43a37952009-06-10 14:12:05 +03002591 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002592 return;
2593 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002594
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002595 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002596 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002597 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002598
Anthony Liguori35149e22008-04-02 14:46:56 -05002599 if (is_error_pfn(pfn)) {
2600 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002601 return;
2602 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002603 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002604 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002605}
2606
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002607static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2608{
2609 u64 *spte = vcpu->arch.last_pte_updated;
2610
2611 if (spte
2612 && vcpu->arch.last_pte_gfn == gfn
2613 && shadow_accessed_mask
2614 && !(*spte & shadow_accessed_mask)
2615 && is_shadow_present_pte(*spte))
2616 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2617}
2618
Avi Kivity09072da2007-05-01 14:16:52 +03002619void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002620 const u8 *new, int bytes,
2621 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002622{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002623 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002624 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002625 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002626 struct hlist_head *bucket;
2627 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002628 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002629 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002630 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002631 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002632 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002633 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002634 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002635 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002636 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002637 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002638 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002639 int invlpg_counter;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002640
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002641 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002642
Avi Kivity08e850c2010-03-15 13:59:57 +02002643 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2644
2645 /*
2646 * Assume that the pte write on a page table of the same type
2647 * as the current vcpu paging mode. This is nearly always true
2648 * (might be false while changing modes). Note it is verified later
2649 * by update_pte().
2650 */
2651 if ((is_pae(vcpu) && bytes == 4) || !new) {
2652 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2653 if (is_pae(vcpu)) {
2654 gpa &= ~(gpa_t)7;
2655 bytes = 8;
2656 }
2657 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2658 if (r)
2659 gentry = 0;
2660 new = (const u8 *)&gentry;
2661 }
2662
Avi Kivity72016f32010-03-15 13:59:53 +02002663 switch (bytes) {
2664 case 4:
2665 gentry = *(const u32 *)new;
2666 break;
2667 case 8:
2668 gentry = *(const u64 *)new;
2669 break;
2670 default:
2671 gentry = 0;
2672 break;
2673 }
2674
Avi Kivity72016f32010-03-15 13:59:53 +02002675 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002676 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002677 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2678 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002679 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002680 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002681 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002682 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002683 if (guest_initiated) {
2684 if (gfn == vcpu->arch.last_pt_write_gfn
2685 && !last_updated_pte_accessed(vcpu)) {
2686 ++vcpu->arch.last_pt_write_count;
2687 if (vcpu->arch.last_pt_write_count >= 3)
2688 flooded = 1;
2689 } else {
2690 vcpu->arch.last_pt_write_gfn = gfn;
2691 vcpu->arch.last_pt_write_count = 1;
2692 vcpu->arch.last_pte_updated = NULL;
2693 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002694 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002695 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002696 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08002697
2698restart:
Avi Kivity4db35312007-11-21 15:28:32 +02002699 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002700 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002701 continue;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002702 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002703 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002704 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002705 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002706 /*
2707 * Misaligned accesses are too much trouble to fix
2708 * up; also, they usually indicate a page is not used
2709 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002710 *
2711 * If we're seeing too many writes to a page,
2712 * it may no longer be a page table, or we may be
2713 * forking, in which case it is better to unmap the
2714 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002715 */
2716 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002717 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002718 if (kvm_mmu_zap_page(vcpu->kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002719 goto restart;
Avi Kivity4cee5762007-11-18 16:37:07 +02002720 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002721 continue;
2722 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002723 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002724 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002725 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002726 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002727 page_offset <<= 1; /* 32->64 */
2728 /*
2729 * A 32-bit pde maps 4MB while the shadow pdes map
2730 * only 2MB. So we need to double the offset again
2731 * and zap two pdes instead of one.
2732 */
2733 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002734 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002735 page_offset <<= 1;
2736 npte = 2;
2737 }
Avi Kivityfce06572007-05-01 16:44:05 +03002738 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002739 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002740 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002741 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002742 }
Avi Kivity4db35312007-11-21 15:28:32 +02002743 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002744 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002745 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002746 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002747 if (gentry)
2748 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Avi Kivity79539ce2007-11-21 02:06:21 +02002749 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002750 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002751 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002752 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002753 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002754 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002755 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2756 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2757 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002758 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002759}
2760
Avi Kivitya4360362007-01-05 16:36:45 -08002761int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2762{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002763 gpa_t gpa;
2764 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002765
Avi Kivity60f24782009-08-27 13:37:06 +03002766 if (tdp_enabled)
2767 return 0;
2768
Gleb Natapov1871c602010-02-10 14:21:32 +02002769 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002770
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002771 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002772 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002773 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002774 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002775}
Avi Kivity577bdc42008-07-19 08:57:05 +03002776EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002777
Avi Kivity22d95b12007-09-14 20:26:06 +03002778void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002779{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002780 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2781 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002782 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002783
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002784 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002785 struct kvm_mmu_page, link);
2786 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002787 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002788 }
2789}
Avi Kivityebeace82007-01-05 16:36:47 -08002790
Avi Kivity30677142007-10-28 18:48:59 +02002791int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2792{
2793 int r;
2794 enum emulation_result er;
2795
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002796 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002797 if (r < 0)
2798 goto out;
2799
2800 if (!r) {
2801 r = 1;
2802 goto out;
2803 }
2804
Avi Kivityb733bfb2007-10-28 18:52:05 +02002805 r = mmu_topup_memory_caches(vcpu);
2806 if (r)
2807 goto out;
2808
Avi Kivity851ba692009-08-24 11:10:17 +03002809 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002810
2811 switch (er) {
2812 case EMULATE_DONE:
2813 return 1;
2814 case EMULATE_DO_MMIO:
2815 ++vcpu->stat.mmio_exits;
2816 return 0;
2817 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002818 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2819 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002820 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002821 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002822 default:
2823 BUG();
2824 }
2825out:
Avi Kivity30677142007-10-28 18:48:59 +02002826 return r;
2827}
2828EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2829
Marcelo Tosattia7052892008-09-23 13:18:35 -03002830void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2831{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002832 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002833 kvm_mmu_flush_tlb(vcpu);
2834 ++vcpu->stat.invlpg;
2835}
2836EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2837
Joerg Roedel18552672008-02-07 13:47:41 +01002838void kvm_enable_tdp(void)
2839{
2840 tdp_enabled = true;
2841}
2842EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2843
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002844void kvm_disable_tdp(void)
2845{
2846 tdp_enabled = false;
2847}
2848EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2849
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850static void free_mmu_pages(struct kvm_vcpu *vcpu)
2851{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002852 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853}
2854
2855static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2856{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002857 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 int i;
2859
2860 ASSERT(vcpu);
2861
Avi Kivity17ac10a2007-01-05 16:36:40 -08002862 /*
2863 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2864 * Therefore we need to allocate shadow page tables in the first
2865 * 4GB of memory, which happens to fit the DMA32 zone.
2866 */
2867 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2868 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002869 return -ENOMEM;
2870
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002871 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002872 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002873 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002874
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876}
2877
Ingo Molnar8018c272006-12-29 16:50:01 -08002878int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002881 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882
Ingo Molnar8018c272006-12-29 16:50:01 -08002883 return alloc_mmu_pages(vcpu);
2884}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885
Ingo Molnar8018c272006-12-29 16:50:01 -08002886int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2887{
2888 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002889 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002890
Ingo Molnar8018c272006-12-29 16:50:01 -08002891 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892}
2893
2894void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2895{
2896 ASSERT(vcpu);
2897
2898 destroy_kvm_mmu(vcpu);
2899 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002900 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
2902
Avi Kivity90cb0522007-07-17 13:04:56 +03002903void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904{
Avi Kivity4db35312007-11-21 15:28:32 +02002905 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002907 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 int i;
2909 u64 *pt;
2910
Sheng Yang291f26b2008-10-16 17:30:57 +08002911 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 continue;
2913
Avi Kivity4db35312007-11-21 15:28:32 +02002914 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2916 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002917 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 }
Avi Kivity171d5952008-08-27 16:40:51 +03002920 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002922
Avi Kivity90cb0522007-07-17 13:04:56 +03002923void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002924{
Avi Kivity4db35312007-11-21 15:28:32 +02002925 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002926
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002927 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08002928restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002929 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002930 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002931 goto restart;
2932
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002933 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002934
Avi Kivity90cb0522007-07-17 13:04:56 +03002935 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002936}
2937
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002938static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002939{
2940 struct kvm_mmu_page *page;
2941
2942 page = container_of(kvm->arch.active_mmu_pages.prev,
2943 struct kvm_mmu_page, link);
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002944 return kvm_mmu_zap_page(kvm, page) + 1;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002945}
2946
Dave Chinner7f8275d2010-07-19 14:56:17 +10002947static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002948{
2949 struct kvm *kvm;
2950 struct kvm *kvm_freed = NULL;
2951 int cache_count = 0;
2952
2953 spin_lock(&kvm_lock);
2954
2955 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002956 int npages, idx, freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002957
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002958 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002959 spin_lock(&kvm->mmu_lock);
2960 npages = kvm->arch.n_alloc_mmu_pages -
2961 kvm->arch.n_free_mmu_pages;
2962 cache_count += npages;
2963 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002964 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm);
2965 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002966 kvm_freed = kvm;
2967 }
2968 nr_to_scan--;
2969
2970 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002971 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002972 }
2973 if (kvm_freed)
2974 list_move_tail(&kvm_freed->vm_list, &vm_list);
2975
2976 spin_unlock(&kvm_lock);
2977
2978 return cache_count;
2979}
2980
2981static struct shrinker mmu_shrinker = {
2982 .shrink = mmu_shrink,
2983 .seeks = DEFAULT_SEEKS * 10,
2984};
2985
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002986static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002987{
2988 if (pte_chain_cache)
2989 kmem_cache_destroy(pte_chain_cache);
2990 if (rmap_desc_cache)
2991 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002992 if (mmu_page_header_cache)
2993 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002994}
2995
Izik Eidus3ee16c82008-03-30 15:17:21 +03002996void kvm_mmu_module_exit(void)
2997{
2998 mmu_destroy_caches();
2999 unregister_shrinker(&mmu_shrinker);
3000}
3001
Avi Kivityb5a33a72007-04-15 16:31:09 +03003002int kvm_mmu_module_init(void)
3003{
3004 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3005 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003006 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003007 if (!pte_chain_cache)
3008 goto nomem;
3009 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3010 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003011 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003012 if (!rmap_desc_cache)
3013 goto nomem;
3014
Avi Kivityd3d25b02007-05-30 12:34:53 +03003015 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3016 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003017 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003018 if (!mmu_page_header_cache)
3019 goto nomem;
3020
Izik Eidus3ee16c82008-03-30 15:17:21 +03003021 register_shrinker(&mmu_shrinker);
3022
Avi Kivityb5a33a72007-04-15 16:31:09 +03003023 return 0;
3024
3025nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003026 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003027 return -ENOMEM;
3028}
3029
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003030/*
3031 * Caculate mmu pages needed for kvm.
3032 */
3033unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3034{
3035 int i;
3036 unsigned int nr_mmu_pages;
3037 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003038 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003039
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003040 slots = kvm_memslots(kvm);
3041
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003042 for (i = 0; i < slots->nmemslots; i++)
3043 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003044
3045 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3046 nr_mmu_pages = max(nr_mmu_pages,
3047 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3048
3049 return nr_mmu_pages;
3050}
3051
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003052static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3053 unsigned len)
3054{
3055 if (len > buffer->len)
3056 return NULL;
3057 return buffer->ptr;
3058}
3059
3060static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3061 unsigned len)
3062{
3063 void *ret;
3064
3065 ret = pv_mmu_peek_buffer(buffer, len);
3066 if (!ret)
3067 return ret;
3068 buffer->ptr += len;
3069 buffer->len -= len;
3070 buffer->processed += len;
3071 return ret;
3072}
3073
3074static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3075 gpa_t addr, gpa_t value)
3076{
3077 int bytes = 8;
3078 int r;
3079
3080 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3081 bytes = 4;
3082
3083 r = mmu_topup_memory_caches(vcpu);
3084 if (r)
3085 return r;
3086
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003087 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003088 return -EFAULT;
3089
3090 return 1;
3091}
3092
3093static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3094{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003095 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003096 return 1;
3097}
3098
3099static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3100{
3101 spin_lock(&vcpu->kvm->mmu_lock);
3102 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3103 spin_unlock(&vcpu->kvm->mmu_lock);
3104 return 1;
3105}
3106
3107static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3108 struct kvm_pv_mmu_op_buffer *buffer)
3109{
3110 struct kvm_mmu_op_header *header;
3111
3112 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3113 if (!header)
3114 return 0;
3115 switch (header->op) {
3116 case KVM_MMU_OP_WRITE_PTE: {
3117 struct kvm_mmu_op_write_pte *wpte;
3118
3119 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3120 if (!wpte)
3121 return 0;
3122 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3123 wpte->pte_val);
3124 }
3125 case KVM_MMU_OP_FLUSH_TLB: {
3126 struct kvm_mmu_op_flush_tlb *ftlb;
3127
3128 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3129 if (!ftlb)
3130 return 0;
3131 return kvm_pv_mmu_flush_tlb(vcpu);
3132 }
3133 case KVM_MMU_OP_RELEASE_PT: {
3134 struct kvm_mmu_op_release_pt *rpt;
3135
3136 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3137 if (!rpt)
3138 return 0;
3139 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3140 }
3141 default: return 0;
3142 }
3143}
3144
3145int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3146 gpa_t addr, unsigned long *ret)
3147{
3148 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003149 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003150
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003151 buffer->ptr = buffer->buf;
3152 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3153 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003154
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003155 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003156 if (r)
3157 goto out;
3158
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003159 while (buffer->len) {
3160 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003161 if (r < 0)
3162 goto out;
3163 if (r == 0)
3164 break;
3165 }
3166
3167 r = 1;
3168out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003169 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003170 return r;
3171}
3172
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003173int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3174{
3175 struct kvm_shadow_walk_iterator iterator;
3176 int nr_sptes = 0;
3177
3178 spin_lock(&vcpu->kvm->mmu_lock);
3179 for_each_shadow_entry(vcpu, addr, iterator) {
3180 sptes[iterator.level-1] = *iterator.sptep;
3181 nr_sptes++;
3182 if (!is_shadow_present_pte(*iterator.sptep))
3183 break;
3184 }
3185 spin_unlock(&vcpu->kvm->mmu_lock);
3186
3187 return nr_sptes;
3188}
3189EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3190
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003191#ifdef AUDIT
3192
3193static const char *audit_msg;
3194
3195static gva_t canonicalize(gva_t gva)
3196{
3197#ifdef CONFIG_X86_64
3198 gva = (long long)(gva << 16) >> 16;
3199#endif
3200 return gva;
3201}
3202
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003203
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003204typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003205
3206static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3207 inspect_spte_fn fn)
3208{
3209 int i;
3210
3211 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3212 u64 ent = sp->spt[i];
3213
3214 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003215 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003216 struct kvm_mmu_page *child;
3217 child = page_header(ent & PT64_BASE_ADDR_MASK);
3218 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003219 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003220 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003221 }
3222 }
3223}
3224
3225static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3226{
3227 int i;
3228 struct kvm_mmu_page *sp;
3229
3230 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3231 return;
3232 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3233 hpa_t root = vcpu->arch.mmu.root_hpa;
3234 sp = page_header(root);
3235 __mmu_spte_walk(vcpu->kvm, sp, fn);
3236 return;
3237 }
3238 for (i = 0; i < 4; ++i) {
3239 hpa_t root = vcpu->arch.mmu.pae_root[i];
3240
3241 if (root && VALID_PAGE(root)) {
3242 root &= PT64_BASE_ADDR_MASK;
3243 sp = page_header(root);
3244 __mmu_spte_walk(vcpu->kvm, sp, fn);
3245 }
3246 }
3247 return;
3248}
3249
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003250static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3251 gva_t va, int level)
3252{
3253 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3254 int i;
3255 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3256
3257 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3258 u64 ent = pt[i];
3259
Avi Kivityc7addb92007-09-16 18:58:32 +02003260 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003261 continue;
3262
3263 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003264 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3265 audit_mappings_page(vcpu, ent, va, level - 1);
3266 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003267 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003268 gfn_t gfn = gpa >> PAGE_SHIFT;
3269 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3270 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003271
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003272 if (is_error_pfn(pfn)) {
3273 kvm_release_pfn_clean(pfn);
3274 continue;
3275 }
3276
Avi Kivityc7addb92007-09-16 18:58:32 +02003277 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003278 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003279 printk(KERN_ERR "xx audit error: (%s) levels %d"
3280 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003281 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003282 va, gpa, hpa, ent,
3283 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003284 else if (ent == shadow_notrap_nonpresent_pte
3285 && !is_error_hpa(hpa))
3286 printk(KERN_ERR "audit: (%s) notrap shadow,"
3287 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003288 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003289
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003290 }
3291 }
3292}
3293
3294static void audit_mappings(struct kvm_vcpu *vcpu)
3295{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003296 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003297
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003298 if (vcpu->arch.mmu.root_level == 4)
3299 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003300 else
3301 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003302 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003304 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003305 i << 30,
3306 2);
3307}
3308
3309static int count_rmaps(struct kvm_vcpu *vcpu)
3310{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003311 struct kvm *kvm = vcpu->kvm;
3312 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003313 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003314 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003316 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003317 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003318 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003319 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003320 struct kvm_rmap_desc *d;
3321
3322 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003323 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003324
Izik Eidus290fc382007-09-27 14:11:22 +02003325 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003326 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003327 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003328 ++nmaps;
3329 continue;
3330 }
Izik Eidus290fc382007-09-27 14:11:22 +02003331 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003332 while (d) {
3333 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003334 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003335 ++nmaps;
3336 else
3337 break;
3338 d = d->more;
3339 }
3340 }
3341 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003342 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003343 return nmaps;
3344}
3345
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003346void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003347{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003348 unsigned long *rmapp;
3349 struct kvm_mmu_page *rev_sp;
3350 gfn_t gfn;
3351
3352 if (*sptep & PT_WRITABLE_MASK) {
3353 rev_sp = page_header(__pa(sptep));
3354 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3355
3356 if (!gfn_to_memslot(kvm, gfn)) {
3357 if (!printk_ratelimit())
3358 return;
3359 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3360 audit_msg, gfn);
3361 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003362 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003363 rev_sp->gfn);
3364 dump_stack();
3365 return;
3366 }
3367
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003368 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003369 rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003370 if (!*rmapp) {
3371 if (!printk_ratelimit())
3372 return;
3373 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3374 audit_msg, *sptep);
3375 dump_stack();
3376 }
3377 }
3378
3379}
3380
3381void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3382{
3383 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3384}
3385
3386static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3387{
Avi Kivity4db35312007-11-21 15:28:32 +02003388 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003389 int i;
3390
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003391 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003392 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003393
Avi Kivity4db35312007-11-21 15:28:32 +02003394 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003395 continue;
3396
3397 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3398 u64 ent = pt[i];
3399
3400 if (!(ent & PT_PRESENT_MASK))
3401 continue;
3402 if (!(ent & PT_WRITABLE_MASK))
3403 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003404 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003405 }
3406 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003407 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003408}
3409
3410static void audit_rmap(struct kvm_vcpu *vcpu)
3411{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003412 check_writable_mappings_rmap(vcpu);
3413 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003414}
3415
3416static void audit_write_protection(struct kvm_vcpu *vcpu)
3417{
Avi Kivity4db35312007-11-21 15:28:32 +02003418 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003419 struct kvm_memory_slot *slot;
3420 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003421 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003422 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003423
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003424 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003425 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003426 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003427 if (sp->unsync)
3428 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003429
Avi Kivity4db35312007-11-21 15:28:32 +02003430 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003431 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003432 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003433
3434 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3435 while (spte) {
3436 if (*spte & PT_WRITABLE_MASK)
3437 printk(KERN_ERR "%s: (%s) shadow page has "
3438 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003439 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003440 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003441 spte = rmap_next(vcpu->kvm, rmapp, spte);
3442 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003443 }
3444}
3445
3446static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3447{
3448 int olddbg = dbg;
3449
3450 dbg = 0;
3451 audit_msg = msg;
3452 audit_rmap(vcpu);
3453 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003454 if (strcmp("pre pte write", audit_msg) != 0)
3455 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003456 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003457 dbg = olddbg;
3458}
3459
3460#endif