blob: 7a17db1cdcd6c7f4cc5e9e1556f0f423217d0732 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020021#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030022#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040025#include <linux/types.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/highmem.h>
29#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020030#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030031#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050032#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020033#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Avi Kivitye4956062007-06-28 14:15:57 -040035
36#include <asm/page.h>
37#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020038#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020039#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040040
Joerg Roedel18552672008-02-07 13:47:41 +010041/*
42 * When setting this variable to true it enables Two-Dimensional-Paging
43 * where the hardware walks 2 page tables:
44 * 1. the guest-virtual to guest-physical
45 * 2. while doing 1. it walks guest-physical to host-physical
46 * If the hardware supports that we don't need to do shadow paging.
47 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050048bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010049
Avi Kivity37a7d8b2007-01-05 16:36:56 -080050#undef MMU_DEBUG
51
52#undef AUDIT
53
54#ifdef AUDIT
55static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
56#else
57static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
58#endif
59
60#ifdef MMU_DEBUG
61
62#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
63#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
64
65#else
66
67#define pgprintk(x...) do { } while (0)
68#define rmap_printk(x...) do { } while (0)
69
70#endif
71
72#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030073static int dbg = 0;
74module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080075#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080076
Marcelo Tosatti582801a2008-09-23 13:18:41 -030077static int oos_shadow = 1;
78module_param(oos_shadow, bool, 0644);
79
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080080#ifndef MMU_DEBUG
81#define ASSERT(x) do { } while (0)
82#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define ASSERT(x) \
84 if (!(x)) { \
85 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
86 __FILE__, __LINE__, #x); \
87 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080088#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080089
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define PT_FIRST_AVAIL_BITS_SHIFT 9
91#define PT64_SECOND_AVAIL_BITS_SHIFT 52
92
Avi Kivity6aa8b732006-12-10 02:21:36 -080093#define VALID_PAGE(x) ((x) != INVALID_PAGE)
94
95#define PT64_LEVEL_BITS 9
96
97#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040098 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080099
100#define PT64_LEVEL_MASK(level) \
101 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
102
103#define PT64_INDEX(address, level)\
104 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
105
106
107#define PT32_LEVEL_BITS 10
108
109#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400110 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111
112#define PT32_LEVEL_MASK(level) \
113 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200114#define PT32_LVL_OFFSET_MASK(level) \
115 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
116 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117
118#define PT32_INDEX(address, level)\
119 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
120
121
Avi Kivity27aba762007-03-09 13:04:31 +0200122#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123#define PT64_DIR_BASE_ADDR_MASK \
124 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200125#define PT64_LVL_ADDR_MASK(level) \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
127 * PT64_LEVEL_BITS))) - 1))
128#define PT64_LVL_OFFSET_MASK(level) \
129 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
132#define PT32_BASE_ADDR_MASK PAGE_MASK
133#define PT32_DIR_BASE_ADDR_MASK \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200135#define PT32_LVL_ADDR_MASK(level) \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
137 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138
Avi Kivity79539ce2007-11-21 02:06:21 +0200139#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
140 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800142#define RMAP_EXT 4
143
Avi Kivityfe135d22007-12-09 16:15:46 +0200144#define ACC_EXEC_MASK 1
145#define ACC_WRITE_MASK PT_WRITABLE_MASK
146#define ACC_USER_MASK PT_USER_MASK
147#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
148
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200149#include <trace/events/kvm.h>
150
Avi Kivity07420172009-07-06 12:21:32 +0300151#define CREATE_TRACE_POINTS
152#include "mmutrace.h"
153
Izik Eidus14032832009-09-23 21:47:17 +0300154#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
155
Avi Kivity135f8c22008-08-21 17:49:56 +0300156#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
157
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800158struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300159 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160 struct kvm_rmap_desc *more;
161};
162
Avi Kivity2d111232008-12-25 14:39:47 +0200163struct kvm_shadow_walk_iterator {
164 u64 addr;
165 hpa_t shadow_addr;
166 int level;
167 u64 *sptep;
168 unsigned index;
169};
170
171#define for_each_shadow_entry(_vcpu, _addr, _walker) \
172 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
173 shadow_walk_okay(&(_walker)); \
174 shadow_walk_next(&(_walker)))
175
Xiao Guangrong6b184932010-04-16 21:29:17 +0800176typedef int (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300177
Avi Kivityb5a33a72007-04-15 16:31:09 +0300178static struct kmem_cache *pte_chain_cache;
179static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300180static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300181
Avi Kivityc7addb92007-09-16 18:58:32 +0200182static u64 __read_mostly shadow_trap_nonpresent_pte;
183static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800184static u64 __read_mostly shadow_base_present_pte;
185static u64 __read_mostly shadow_nx_mask;
186static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
187static u64 __read_mostly shadow_user_mask;
188static u64 __read_mostly shadow_accessed_mask;
189static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200190
Dong, Eddie82725b22009-03-30 16:21:08 +0800191static inline u64 rsvd_bits(int s, int e)
192{
193 return ((1ULL << (e - s + 1)) - 1) << s;
194}
195
Avi Kivityc7addb92007-09-16 18:58:32 +0200196void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
197{
198 shadow_trap_nonpresent_pte = trap_pte;
199 shadow_notrap_nonpresent_pte = notrap_pte;
200}
201EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
202
Sheng Yang7b523452008-04-25 21:13:50 +0800203void kvm_mmu_set_base_ptes(u64 base_pte)
204{
205 shadow_base_present_pte = base_pte;
206}
207EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
208
209void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800210 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800211{
212 shadow_user_mask = user_mask;
213 shadow_accessed_mask = accessed_mask;
214 shadow_dirty_mask = dirty_mask;
215 shadow_nx_mask = nx_mask;
216 shadow_x_mask = x_mask;
217}
218EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220static int is_write_protection(struct kvm_vcpu *vcpu)
221{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200222 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223}
224
225static int is_cpuid_PSE36(void)
226{
227 return 1;
228}
229
Avi Kivity73b10872007-01-26 00:56:41 -0800230static int is_nx(struct kvm_vcpu *vcpu)
231{
Avi Kivityf6801df2010-01-21 15:31:50 +0200232 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800233}
234
Avi Kivityc7addb92007-09-16 18:58:32 +0200235static int is_shadow_present_pte(u64 pte)
236{
Avi Kivityc7addb92007-09-16 18:58:32 +0200237 return pte != shadow_trap_nonpresent_pte
238 && pte != shadow_notrap_nonpresent_pte;
239}
240
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300241static int is_large_pte(u64 pte)
242{
243 return pte & PT_PAGE_SIZE_MASK;
244}
245
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900246static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247{
248 return pte & PT_WRITABLE_MASK;
249}
250
Avi Kivity43a37952009-06-10 14:12:05 +0300251static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200252{
Avi Kivity439e2182009-06-10 12:56:54 +0300253 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200254}
255
Avi Kivity43a37952009-06-10 14:12:05 +0300256static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800257{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200258 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800259}
260
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300261static int is_last_spte(u64 pte, int level)
262{
263 if (level == PT_PAGE_TABLE_LEVEL)
264 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200265 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300266 return 1;
267 return 0;
268}
269
Anthony Liguori35149e22008-04-02 14:46:56 -0500270static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200271{
Anthony Liguori35149e22008-04-02 14:46:56 -0500272 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200273}
274
Avi Kivityda928522007-11-21 13:54:47 +0200275static gfn_t pse36_gfn_delta(u32 gpte)
276{
277 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
278
279 return (gpte & PT32_DIR_PSE36_MASK) << shift;
280}
281
Avi Kivityd555c332009-06-10 14:24:23 +0300282static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300283{
284#ifdef CONFIG_X86_64
285 set_64bit((unsigned long *)sptep, spte);
286#else
287 set_64bit((unsigned long long *)sptep, spte);
288#endif
289}
290
Avi Kivitye2dec932007-01-05 16:36:54 -0800291static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300292 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800293{
294 void *obj;
295
296 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800297 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800298 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300299 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800300 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800301 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800302 cache->objects[cache->nobjs++] = obj;
303 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800304 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800305}
306
307static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
308{
309 while (mc->nobjs)
310 kfree(mc->objects[--mc->nobjs]);
311}
312
Avi Kivityc1158e62007-07-20 08:18:27 +0300313static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300314 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300315{
316 struct page *page;
317
318 if (cache->nobjs >= min)
319 return 0;
320 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300321 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300322 if (!page)
323 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300324 cache->objects[cache->nobjs++] = page_address(page);
325 }
326 return 0;
327}
328
329static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
330{
331 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300332 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300333}
334
Avi Kivity8c438502007-04-16 11:53:17 +0300335static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
336{
337 int r;
338
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800339 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300340 pte_chain_cache, 4);
341 if (r)
342 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200344 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300345 if (r)
346 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800347 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300348 if (r)
349 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300351 mmu_page_header_cache, 4);
352out:
Avi Kivity8c438502007-04-16 11:53:17 +0300353 return r;
354}
355
Avi Kivity714b93d2007-01-05 16:36:53 -0800356static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
357{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800358 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
359 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
360 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
361 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800362}
363
364static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
365 size_t size)
366{
367 void *p;
368
369 BUG_ON(!mc->nobjs);
370 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800371 return p;
372}
373
Avi Kivity714b93d2007-01-05 16:36:53 -0800374static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
375{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800376 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800377 sizeof(struct kvm_pte_chain));
378}
379
Avi Kivity90cb0522007-07-17 13:04:56 +0300380static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800381{
Avi Kivity90cb0522007-07-17 13:04:56 +0300382 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800383}
384
385static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
386{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800387 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800388 sizeof(struct kvm_rmap_desc));
389}
390
Avi Kivity90cb0522007-07-17 13:04:56 +0300391static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800392{
Avi Kivity90cb0522007-07-17 13:04:56 +0300393 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800394}
395
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800396/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300397 * Return the pointer to the largepage write count for a given
398 * gfn, handling slots that are not large page aligned.
399 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200400static int *slot_largepage_idx(gfn_t gfn,
401 struct kvm_memory_slot *slot,
402 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300403{
404 unsigned long idx;
405
Joerg Roedeld25797b2009-07-27 16:30:43 +0200406 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
407 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
408 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409}
410
411static void account_shadowed(struct kvm *kvm, gfn_t gfn)
412{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200413 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300414 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200415 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300416
Izik Eidus28430992008-10-03 17:40:32 +0300417 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200418
419 slot = gfn_to_memslot_unaliased(kvm, gfn);
420 for (i = PT_DIRECTORY_LEVEL;
421 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
422 write_count = slot_largepage_idx(gfn, slot, i);
423 *write_count += 1;
424 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300425}
426
427static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
428{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200429 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300430 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200431 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300432
Izik Eidus28430992008-10-03 17:40:32 +0300433 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200434 for (i = PT_DIRECTORY_LEVEL;
435 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
436 slot = gfn_to_memslot_unaliased(kvm, gfn);
437 write_count = slot_largepage_idx(gfn, slot, i);
438 *write_count -= 1;
439 WARN_ON(*write_count < 0);
440 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441}
442
Joerg Roedeld25797b2009-07-27 16:30:43 +0200443static int has_wrprotected_page(struct kvm *kvm,
444 gfn_t gfn,
445 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300446{
Izik Eidus28430992008-10-03 17:40:32 +0300447 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448 int *largepage_idx;
449
Izik Eidus28430992008-10-03 17:40:32 +0300450 gfn = unalias_gfn(kvm, gfn);
451 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200453 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300454 return *largepage_idx;
455 }
456
457 return 1;
458}
459
Joerg Roedeld25797b2009-07-27 16:30:43 +0200460static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100462 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200463 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300464
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100465 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466
Joerg Roedeld25797b2009-07-27 16:30:43 +0200467 for (i = PT_PAGE_TABLE_LEVEL;
468 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
469 if (page_size >= KVM_HPAGE_SIZE(i))
470 ret = i;
471 else
472 break;
473 }
474
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300475 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476}
477
Joerg Roedeld25797b2009-07-27 16:30:43 +0200478static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479{
480 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800481 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482
483 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
484 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200485 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486
Joerg Roedeld25797b2009-07-27 16:30:43 +0200487 host_level = host_mapping_level(vcpu->kvm, large_gfn);
488
489 if (host_level == PT_PAGE_TABLE_LEVEL)
490 return host_level;
491
Sheng Yang878403b2010-01-05 19:02:29 +0800492 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
493 kvm_x86_ops->get_lpage_level() : host_level;
494
495 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200496 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
497 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498
499 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300500}
501
502/*
Izik Eidus290fc382007-09-27 14:11:22 +0200503 * Take gfn and return the reverse mapping to it.
504 * Note: gfn must be unaliased before this function get called
505 */
506
Joerg Roedel44ad9942009-07-27 16:30:42 +0200507static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200508{
509 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300510 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200511
512 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200513 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300514 return &slot->rmap[gfn - slot->base_gfn];
515
Joerg Roedel44ad9942009-07-27 16:30:42 +0200516 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
517 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300518
Joerg Roedel44ad9942009-07-27 16:30:42 +0200519 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200520}
521
522/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800523 * Reverse mapping data structures:
524 *
Izik Eidus290fc382007-09-27 14:11:22 +0200525 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
526 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527 *
Izik Eidus290fc382007-09-27 14:11:22 +0200528 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
529 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300530 *
531 * Returns the number of rmap entries before the spte was added or zero if
532 * the spte was not added.
533 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800534 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200535static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800536{
Avi Kivity4db35312007-11-21 15:28:32 +0200537 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200539 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300540 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541
Avi Kivity43a37952009-06-10 14:12:05 +0300542 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300543 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200544 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200545 sp = page_header(__pa(spte));
546 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200547 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200548 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800549 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200550 *rmapp = (unsigned long)spte;
551 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800553 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300554 desc->sptes[0] = (u64 *)*rmapp;
555 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200556 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 } else {
558 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200559 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300560 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300562 count += RMAP_EXT;
563 }
Avi Kivityd555c332009-06-10 14:24:23 +0300564 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800565 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566 desc = desc->more;
567 }
Avi Kivityd555c332009-06-10 14:24:23 +0300568 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800569 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300570 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300572 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800573}
574
Izik Eidus290fc382007-09-27 14:11:22 +0200575static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800576 struct kvm_rmap_desc *desc,
577 int i,
578 struct kvm_rmap_desc *prev_desc)
579{
580 int j;
581
Avi Kivityd555c332009-06-10 14:24:23 +0300582 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800583 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300584 desc->sptes[i] = desc->sptes[j];
585 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 if (j != 0)
587 return;
588 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300589 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800590 else
591 if (prev_desc)
592 prev_desc->more = desc->more;
593 else
Izik Eidus290fc382007-09-27 14:11:22 +0200594 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300595 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596}
597
Izik Eidus290fc382007-09-27 14:11:22 +0200598static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800599{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 struct kvm_rmap_desc *desc;
601 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200602 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500603 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200604 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800605 int i;
606
Avi Kivity43a37952009-06-10 14:12:05 +0300607 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800608 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200609 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500610 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800611 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500612 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900613 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300614 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200615 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200616 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800617 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
618 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200619 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200621 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800622 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
623 spte, *spte);
624 BUG();
625 }
Izik Eidus290fc382007-09-27 14:11:22 +0200626 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800627 } else {
628 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200629 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800630 prev_desc = NULL;
631 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300632 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
633 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200634 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800635 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 prev_desc);
637 return;
638 }
639 prev_desc = desc;
640 desc = desc->more;
641 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200642 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 BUG();
644 }
645}
646
Izik Eidus98348e92007-10-16 14:42:30 +0200647static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800648{
Avi Kivity374cbac2007-01-05 16:36:43 -0800649 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200650 struct kvm_rmap_desc *prev_desc;
651 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);
662 prev_desc = NULL;
663 prev_spte = NULL;
664 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300665 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200666 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300667 return desc->sptes[i];
668 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200669 }
670 desc = desc->more;
671 }
672 return NULL;
673}
674
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200675static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200676{
Izik Eidus290fc382007-09-27 14:11:22 +0200677 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800678 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200679 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800680
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500681 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200682 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800683
Izik Eidus98348e92007-10-16 14:42:30 +0200684 spte = rmap_next(kvm, rmapp, NULL);
685 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800686 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800687 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800688 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900689 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300690 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800691 write_protected = 1;
692 }
Izik Eidus9647c142007-10-16 14:43:46 +0200693 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800694 }
Izik Eidus855149a2008-03-20 18:17:24 +0200695 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500696 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200697
698 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500699 pfn = spte_to_pfn(*spte);
700 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200701 }
702
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300703 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200704 for (i = PT_DIRECTORY_LEVEL;
705 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
706 rmapp = gfn_to_rmap(kvm, gfn, i);
707 spte = rmap_next(kvm, rmapp, NULL);
708 while (spte) {
709 BUG_ON(!spte);
710 BUG_ON(!(*spte & PT_PRESENT_MASK));
711 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
712 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900713 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200714 rmap_remove(kvm, spte);
715 --kvm->stat.lpages;
716 __set_spte(spte, shadow_trap_nonpresent_pte);
717 spte = NULL;
718 write_protected = 1;
719 }
720 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300721 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300722 }
723
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200724 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800725}
726
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000727static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
728 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200729{
730 u64 *spte;
731 int need_tlb_flush = 0;
732
733 while ((spte = rmap_next(kvm, rmapp, NULL))) {
734 BUG_ON(!(*spte & PT_PRESENT_MASK));
735 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
736 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300737 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200738 need_tlb_flush = 1;
739 }
740 return need_tlb_flush;
741}
742
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000743static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
744 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300745{
746 int need_flush = 0;
747 u64 *spte, new_spte;
748 pte_t *ptep = (pte_t *)data;
749 pfn_t new_pfn;
750
751 WARN_ON(pte_huge(*ptep));
752 new_pfn = pte_pfn(*ptep);
753 spte = rmap_next(kvm, rmapp, NULL);
754 while (spte) {
755 BUG_ON(!is_shadow_present_pte(*spte));
756 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
757 need_flush = 1;
758 if (pte_write(*ptep)) {
759 rmap_remove(kvm, spte);
760 __set_spte(spte, shadow_trap_nonpresent_pte);
761 spte = rmap_next(kvm, rmapp, NULL);
762 } else {
763 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
764 new_spte |= (u64)new_pfn << PAGE_SHIFT;
765
766 new_spte &= ~PT_WRITABLE_MASK;
767 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900768 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300769 kvm_set_pfn_dirty(spte_to_pfn(*spte));
770 __set_spte(spte, new_spte);
771 spte = rmap_next(kvm, rmapp, spte);
772 }
773 }
774 if (need_flush)
775 kvm_flush_remote_tlbs(kvm);
776
777 return 0;
778}
779
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000780static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
781 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300782 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000783 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200784{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200785 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200786 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200787 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200788 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200789
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200790 slots = rcu_dereference(kvm->memslots);
791
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200792 for (i = 0; i < slots->nmemslots; i++) {
793 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200794 unsigned long start = memslot->userspace_addr;
795 unsigned long end;
796
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200797 end = start + (memslot->npages << PAGE_SHIFT);
798 if (hva >= start && hva < end) {
799 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200800
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200801 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200802
803 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
804 int idx = gfn_offset;
805 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200806 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300807 &memslot->lpage_info[j][idx].rmap_pde,
808 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200809 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200810 trace_kvm_age_page(hva, memslot, ret);
811 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200812 }
813 }
814
815 return retval;
816}
817
818int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
819{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300820 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200821}
822
Izik Eidus3da0dd42009-09-23 21:47:18 +0300823void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
824{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000825 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300826}
827
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000828static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
829 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200830{
831 u64 *spte;
832 int young = 0;
833
Rik van Riel6316e1c2010-02-03 16:11:03 -0500834 /*
835 * Emulate the accessed bit for EPT, by checking if this page has
836 * an EPT mapping, and clearing it if it does. On the next access,
837 * a new EPT mapping will be established.
838 * This has some overhead, but not as much as the cost of swapping
839 * out actively used pages or breaking up actively used hugepages.
840 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800841 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500842 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800843
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200844 spte = rmap_next(kvm, rmapp, NULL);
845 while (spte) {
846 int _young;
847 u64 _spte = *spte;
848 BUG_ON(!(_spte & PT_PRESENT_MASK));
849 _young = _spte & PT_ACCESSED_MASK;
850 if (_young) {
851 young = 1;
852 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
853 }
854 spte = rmap_next(kvm, rmapp, spte);
855 }
856 return young;
857}
858
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300859#define RMAP_RECYCLE_THRESHOLD 1000
860
Joerg Roedel852e3c12009-07-27 16:30:44 +0200861static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300862{
863 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200864 struct kvm_mmu_page *sp;
865
866 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300867
868 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200869 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300870
Izik Eidus3da0dd42009-09-23 21:47:18 +0300871 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300872 kvm_flush_remote_tlbs(vcpu->kvm);
873}
874
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200875int kvm_age_hva(struct kvm *kvm, unsigned long hva)
876{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300877 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200878}
879
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800880#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300881static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800882{
Avi Kivity139bdb22007-01-05 16:36:50 -0800883 u64 *pos;
884 u64 *end;
885
Avi Kivity47ad8e62007-05-06 15:50:58 +0300886 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300887 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800888 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800889 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800891 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 return 1;
893}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800894#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895
Avi Kivity4db35312007-11-21 15:28:32 +0200896static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800897{
Avi Kivity4db35312007-11-21 15:28:32 +0200898 ASSERT(is_empty_shadow_page(sp->spt));
899 list_del(&sp->link);
900 __free_page(virt_to_page(sp->spt));
901 __free_page(virt_to_page(sp->gfns));
902 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800903 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800904}
905
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800906static unsigned kvm_page_table_hashfn(gfn_t gfn)
907{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200908 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800909}
910
Avi Kivity25c0de22007-01-05 16:36:42 -0800911static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
912 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913{
Avi Kivity4db35312007-11-21 15:28:32 +0200914 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800916 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
917 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
918 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200919 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800920 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800921 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200922 sp->multimapped = 0;
923 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800924 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200925 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926}
927
Avi Kivity714b93d2007-01-05 16:36:53 -0800928static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200929 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800930{
931 struct kvm_pte_chain *pte_chain;
932 struct hlist_node *node;
933 int i;
934
935 if (!parent_pte)
936 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200937 if (!sp->multimapped) {
938 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800939
940 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200941 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800942 return;
943 }
Avi Kivity4db35312007-11-21 15:28:32 +0200944 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800945 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200946 INIT_HLIST_HEAD(&sp->parent_ptes);
947 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800948 pte_chain->parent_ptes[0] = old;
949 }
Avi Kivity4db35312007-11-21 15:28:32 +0200950 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800951 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
952 continue;
953 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
954 if (!pte_chain->parent_ptes[i]) {
955 pte_chain->parent_ptes[i] = parent_pte;
956 return;
957 }
958 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800959 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800960 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200961 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800962 pte_chain->parent_ptes[0] = parent_pte;
963}
964
Avi Kivity4db35312007-11-21 15:28:32 +0200965static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800966 u64 *parent_pte)
967{
968 struct kvm_pte_chain *pte_chain;
969 struct hlist_node *node;
970 int i;
971
Avi Kivity4db35312007-11-21 15:28:32 +0200972 if (!sp->multimapped) {
973 BUG_ON(sp->parent_pte != parent_pte);
974 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800975 return;
976 }
Avi Kivity4db35312007-11-21 15:28:32 +0200977 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800978 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
979 if (!pte_chain->parent_ptes[i])
980 break;
981 if (pte_chain->parent_ptes[i] != parent_pte)
982 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800983 while (i + 1 < NR_PTE_CHAIN_ENTRIES
984 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800985 pte_chain->parent_ptes[i]
986 = pte_chain->parent_ptes[i + 1];
987 ++i;
988 }
989 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800990 if (i == 0) {
991 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300992 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200993 if (hlist_empty(&sp->parent_ptes)) {
994 sp->multimapped = 0;
995 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800996 }
997 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800998 return;
999 }
1000 BUG();
1001}
1002
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001003
Xiao Guangrong6b184932010-04-16 21:29:17 +08001004static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001005{
1006 struct kvm_pte_chain *pte_chain;
1007 struct hlist_node *node;
1008 struct kvm_mmu_page *parent_sp;
1009 int i;
1010
1011 if (!sp->multimapped && sp->parent_pte) {
1012 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001013 fn(parent_sp);
1014 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001015 return;
1016 }
1017 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1018 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1019 if (!pte_chain->parent_ptes[i])
1020 break;
1021 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001022 fn(parent_sp);
1023 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001024 }
1025}
1026
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001027static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1028{
1029 unsigned int index;
1030 struct kvm_mmu_page *sp = page_header(__pa(spte));
1031
1032 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001033 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1034 sp->unsync_children++;
1035 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001036}
1037
1038static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1039{
1040 struct kvm_pte_chain *pte_chain;
1041 struct hlist_node *node;
1042 int i;
1043
1044 if (!sp->parent_pte)
1045 return;
1046
1047 if (!sp->multimapped) {
1048 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1049 return;
1050 }
1051
1052 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1053 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1054 if (!pte_chain->parent_ptes[i])
1055 break;
1056 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1057 }
1058}
1059
Xiao Guangrong6b184932010-04-16 21:29:17 +08001060static int unsync_walk_fn(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001061{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001062 kvm_mmu_update_parents_unsync(sp);
1063 return 1;
1064}
1065
Xiao Guangrong6b184932010-04-16 21:29:17 +08001066static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001067{
Xiao Guangrong6b184932010-04-16 21:29:17 +08001068 mmu_parent_walk(sp, unsync_walk_fn);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001069 kvm_mmu_update_parents_unsync(sp);
1070}
1071
Avi Kivityd761a502008-05-29 14:55:03 +03001072static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1073 struct kvm_mmu_page *sp)
1074{
1075 int i;
1076
1077 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1078 sp->spt[i] = shadow_trap_nonpresent_pte;
1079}
1080
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001081static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1082 struct kvm_mmu_page *sp)
1083{
1084 return 1;
1085}
1086
Marcelo Tosattia7052892008-09-23 13:18:35 -03001087static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1088{
1089}
1090
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001091#define KVM_PAGE_ARRAY_NR 16
1092
1093struct kvm_mmu_pages {
1094 struct mmu_page_and_offset {
1095 struct kvm_mmu_page *sp;
1096 unsigned int idx;
1097 } page[KVM_PAGE_ARRAY_NR];
1098 unsigned int nr;
1099};
1100
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001101#define for_each_unsync_children(bitmap, idx) \
1102 for (idx = find_first_bit(bitmap, 512); \
1103 idx < 512; \
1104 idx = find_next_bit(bitmap, 512, idx+1))
1105
Hannes Edercded19f2009-02-21 02:19:13 +01001106static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1107 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001108{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001109 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001110
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111 if (sp->unsync)
1112 for (i=0; i < pvec->nr; i++)
1113 if (pvec->page[i].sp == sp)
1114 return 0;
1115
1116 pvec->page[pvec->nr].sp = sp;
1117 pvec->page[pvec->nr].idx = idx;
1118 pvec->nr++;
1119 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1120}
1121
1122static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1123 struct kvm_mmu_pages *pvec)
1124{
1125 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001126
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001127 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128 u64 ent = sp->spt[i];
1129
Marcelo Tosatti87917232008-12-22 18:49:30 -02001130 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001131 struct kvm_mmu_page *child;
1132 child = page_header(ent & PT64_BASE_ADDR_MASK);
1133
1134 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001135 if (mmu_pages_add(pvec, child, i))
1136 return -ENOSPC;
1137
1138 ret = __mmu_unsync_walk(child, pvec);
1139 if (!ret)
1140 __clear_bit(i, sp->unsync_child_bitmap);
1141 else if (ret > 0)
1142 nr_unsync_leaf += ret;
1143 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001144 return ret;
1145 }
1146
1147 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001148 nr_unsync_leaf++;
1149 if (mmu_pages_add(pvec, child, i))
1150 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001151 }
1152 }
1153 }
1154
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001155 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001156 sp->unsync_children = 0;
1157
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001158 return nr_unsync_leaf;
1159}
1160
1161static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1162 struct kvm_mmu_pages *pvec)
1163{
1164 if (!sp->unsync_children)
1165 return 0;
1166
1167 mmu_pages_add(pvec, sp, 0);
1168 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001169}
1170
Avi Kivity4db35312007-11-21 15:28:32 +02001171static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001172{
1173 unsigned index;
1174 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001175 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001176 struct hlist_node *node;
1177
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001178 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001179 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001180 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001181 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001182 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001183 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001184 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001185 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001186 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001187 }
1188 return NULL;
1189}
1190
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001191static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1192{
1193 WARN_ON(!sp->unsync);
1194 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
Avi Kivityf691fe12009-07-06 15:58:14 +03001207 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001208 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1209 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001210 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001211 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1212 kvm_mmu_zap_page(vcpu->kvm, sp);
1213 return 1;
1214 }
1215
1216 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001217 return 0;
1218}
1219
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001220struct mmu_page_path {
1221 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1222 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001223};
1224
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001225#define for_each_sp(pvec, sp, parents, i) \
1226 for (i = mmu_pages_next(&pvec, &parents, -1), \
1227 sp = pvec.page[i].sp; \
1228 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1229 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230
Hannes Edercded19f2009-02-21 02:19:13 +01001231static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1232 struct mmu_page_path *parents,
1233 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001234{
1235 int n;
1236
1237 for (n = i+1; n < pvec->nr; n++) {
1238 struct kvm_mmu_page *sp = pvec->page[n].sp;
1239
1240 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1241 parents->idx[0] = pvec->page[n].idx;
1242 return n;
1243 }
1244
1245 parents->parent[sp->role.level-2] = sp;
1246 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1247 }
1248
1249 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250}
1251
Hannes Edercded19f2009-02-21 02:19:13 +01001252static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001253{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001254 struct kvm_mmu_page *sp;
1255 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001257 do {
1258 unsigned int idx = parents->idx[level];
1259
1260 sp = parents->parent[level];
1261 if (!sp)
1262 return;
1263
1264 --sp->unsync_children;
1265 WARN_ON((int)sp->unsync_children < 0);
1266 __clear_bit(idx, sp->unsync_child_bitmap);
1267 level++;
1268 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1269}
1270
1271static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1272 struct mmu_page_path *parents,
1273 struct kvm_mmu_pages *pvec)
1274{
1275 parents->parent[parent->role.level-1] = NULL;
1276 pvec->nr = 0;
1277}
1278
1279static void mmu_sync_children(struct kvm_vcpu *vcpu,
1280 struct kvm_mmu_page *parent)
1281{
1282 int i;
1283 struct kvm_mmu_page *sp;
1284 struct mmu_page_path parents;
1285 struct kvm_mmu_pages pages;
1286
1287 kvm_mmu_pages_init(parent, &parents, &pages);
1288 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001289 int protected = 0;
1290
1291 for_each_sp(pages, sp, parents, i)
1292 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1293
1294 if (protected)
1295 kvm_flush_remote_tlbs(vcpu->kvm);
1296
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001297 for_each_sp(pages, sp, parents, i) {
1298 kvm_sync_page(vcpu, sp);
1299 mmu_pages_clear_parents(&parents);
1300 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001301 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001302 kvm_mmu_pages_init(parent, &parents, &pages);
1303 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001304}
1305
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001306static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1307 gfn_t gfn,
1308 gva_t gaddr,
1309 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001310 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001311 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001312 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001313{
1314 union kvm_mmu_page_role role;
1315 unsigned index;
1316 unsigned quadrant;
1317 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001318 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001319 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001320
Avi Kivitya770f6f2008-12-21 19:20:09 +02001321 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001322 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001323 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001324 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001325 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001326 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001327 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001328 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1329 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1330 role.quadrant = quadrant;
1331 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001332 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001333 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001334 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1335 if (sp->gfn == gfn) {
1336 if (sp->unsync)
1337 if (kvm_sync_page(vcpu, sp))
1338 continue;
1339
1340 if (sp->role.word != role.word)
1341 continue;
1342
Avi Kivity4db35312007-11-21 15:28:32 +02001343 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001344 if (sp->unsync_children) {
1345 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001346 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001347 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001348 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001349 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001350 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001351 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001352 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1353 if (!sp)
1354 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001355 sp->gfn = gfn;
1356 sp->role = role;
1357 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001358 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001359 if (rmap_write_protect(vcpu->kvm, gfn))
1360 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001361 account_shadowed(vcpu->kvm, gfn);
1362 }
Avi Kivity131d8272008-05-29 14:56:28 +03001363 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1364 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1365 else
1366 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001367 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001368 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001369}
1370
Avi Kivity2d111232008-12-25 14:39:47 +02001371static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1372 struct kvm_vcpu *vcpu, u64 addr)
1373{
1374 iterator->addr = addr;
1375 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1376 iterator->level = vcpu->arch.mmu.shadow_root_level;
1377 if (iterator->level == PT32E_ROOT_LEVEL) {
1378 iterator->shadow_addr
1379 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1380 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1381 --iterator->level;
1382 if (!iterator->shadow_addr)
1383 iterator->level = 0;
1384 }
1385}
1386
1387static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1388{
1389 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1390 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001391
1392 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1393 if (is_large_pte(*iterator->sptep))
1394 return false;
1395
Avi Kivity2d111232008-12-25 14:39:47 +02001396 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1397 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1398 return true;
1399}
1400
1401static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1402{
1403 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1404 --iterator->level;
1405}
1406
Avi Kivity90cb0522007-07-17 13:04:56 +03001407static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001408 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001409{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001410 unsigned i;
1411 u64 *pt;
1412 u64 ent;
1413
Avi Kivity4db35312007-11-21 15:28:32 +02001414 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001415
Avi Kivity697fe2e2007-01-05 16:36:46 -08001416 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1417 ent = pt[i];
1418
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001419 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001420 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001421 ent &= PT64_BASE_ADDR_MASK;
1422 mmu_page_remove_parent_pte(page_header(ent),
1423 &pt[i]);
1424 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001425 if (is_large_pte(ent))
1426 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001427 rmap_remove(kvm, &pt[i]);
1428 }
1429 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001430 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001431 }
Avi Kivitya4360362007-01-05 16:36:45 -08001432}
1433
Avi Kivity4db35312007-11-21 15:28:32 +02001434static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001435{
Avi Kivity4db35312007-11-21 15:28:32 +02001436 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001437}
1438
Avi Kivity12b7d282007-09-23 14:10:49 +02001439static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1440{
1441 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001442 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001443
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001444 kvm_for_each_vcpu(i, vcpu, kvm)
1445 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001446}
1447
Avi Kivity31aa2b42008-07-11 17:59:46 +03001448static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001449{
1450 u64 *parent_pte;
1451
Avi Kivity4db35312007-11-21 15:28:32 +02001452 while (sp->multimapped || sp->parent_pte) {
1453 if (!sp->multimapped)
1454 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001455 else {
1456 struct kvm_pte_chain *chain;
1457
Avi Kivity4db35312007-11-21 15:28:32 +02001458 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001459 struct kvm_pte_chain, link);
1460 parent_pte = chain->parent_ptes[0];
1461 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001462 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001463 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001464 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001465 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001466}
1467
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001468static int mmu_zap_unsync_children(struct kvm *kvm,
1469 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001470{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001471 int i, zapped = 0;
1472 struct mmu_page_path parents;
1473 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001474
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001475 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001476 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001477
1478 kvm_mmu_pages_init(parent, &parents, &pages);
1479 while (mmu_unsync_walk(parent, &pages)) {
1480 struct kvm_mmu_page *sp;
1481
1482 for_each_sp(pages, sp, parents, i) {
1483 kvm_mmu_zap_page(kvm, sp);
1484 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001485 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001486 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001487 kvm_mmu_pages_init(parent, &parents, &pages);
1488 }
1489
1490 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001491}
1492
Marcelo Tosatti07385412008-09-23 13:18:37 -03001493static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001494{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001495 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001496
1497 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001498 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001499 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001500 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001501 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001502 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001503 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001504 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001505 if (sp->unsync)
1506 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001507 if (!sp->root_count) {
1508 hlist_del(&sp->hash_link);
1509 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001510 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001511 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001512 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001513 kvm_reload_remote_mmus(kvm);
1514 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001515 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001516 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001517}
1518
Izik Eidus82ce2c92007-10-02 18:52:55 +02001519/*
1520 * Changing the number of mmu pages allocated to the vm
1521 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1522 */
1523void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1524{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001525 int used_pages;
1526
1527 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1528 used_pages = max(0, used_pages);
1529
Izik Eidus82ce2c92007-10-02 18:52:55 +02001530 /*
1531 * If we set the number of mmu pages to be smaller be than the
1532 * number of actived pages , we must to free some mmu pages before we
1533 * change the value
1534 */
1535
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001536 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001537 while (used_pages > kvm_nr_mmu_pages &&
1538 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001539 struct kvm_mmu_page *page;
1540
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001541 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001542 struct kvm_mmu_page, link);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001543 used_pages -= kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001544 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001545 }
Xiao Guangrong77662e02010-04-16 16:34:42 +08001546 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001547 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001548 }
1549 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001550 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1551 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001552
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001553 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001554}
1555
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001556static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001557{
1558 unsigned index;
1559 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001560 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001561 struct hlist_node *node, *n;
1562 int r;
1563
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001564 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001565 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001566 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001567 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001568restart:
Avi Kivity4db35312007-11-21 15:28:32 +02001569 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001570 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001571 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001572 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001573 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001574 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001575 goto restart;
Avi Kivitya4360362007-01-05 16:36:45 -08001576 }
1577 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001578}
1579
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001580static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001581{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001582 unsigned index;
1583 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001584 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001585 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001586
Avi Kivity4677a3b2009-01-06 13:00:27 +02001587 index = kvm_page_table_hashfn(gfn);
1588 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001589restart:
Avi Kivity4677a3b2009-01-06 13:00:27 +02001590 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001591 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001592 && !sp->role.invalid) {
1593 pgprintk("%s: zap %lx %x\n",
1594 __func__, gfn, sp->role.word);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001595 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001596 goto restart;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001597 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001598 }
1599}
1600
Avi Kivity38c335f2007-11-21 14:20:22 +02001601static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001603 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001604 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605
Sheng Yang291f26b2008-10-16 17:30:57 +08001606 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607}
1608
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001609static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1610{
1611 int i;
1612 u64 *pt = sp->spt;
1613
1614 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1615 return;
1616
1617 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1618 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001619 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001620 }
1621}
1622
Avi Kivity039576c2007-03-20 12:46:50 +02001623struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1624{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001625 struct page *page;
1626
Gleb Natapov1871c602010-02-10 14:21:32 +02001627 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Avi Kivity039576c2007-03-20 12:46:50 +02001628
1629 if (gpa == UNMAPPED_GVA)
1630 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001631
Izik Eidus72dc67a2008-02-10 18:04:15 +02001632 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001633
1634 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001635}
1636
Sheng Yang74be52e2008-10-09 16:01:56 +08001637/*
1638 * The function is based on mtrr_type_lookup() in
1639 * arch/x86/kernel/cpu/mtrr/generic.c
1640 */
1641static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1642 u64 start, u64 end)
1643{
1644 int i;
1645 u64 base, mask;
1646 u8 prev_match, curr_match;
1647 int num_var_ranges = KVM_NR_VAR_MTRR;
1648
1649 if (!mtrr_state->enabled)
1650 return 0xFF;
1651
1652 /* Make end inclusive end, instead of exclusive */
1653 end--;
1654
1655 /* Look in fixed ranges. Just return the type as per start */
1656 if (mtrr_state->have_fixed && (start < 0x100000)) {
1657 int idx;
1658
1659 if (start < 0x80000) {
1660 idx = 0;
1661 idx += (start >> 16);
1662 return mtrr_state->fixed_ranges[idx];
1663 } else if (start < 0xC0000) {
1664 idx = 1 * 8;
1665 idx += ((start - 0x80000) >> 14);
1666 return mtrr_state->fixed_ranges[idx];
1667 } else if (start < 0x1000000) {
1668 idx = 3 * 8;
1669 idx += ((start - 0xC0000) >> 12);
1670 return mtrr_state->fixed_ranges[idx];
1671 }
1672 }
1673
1674 /*
1675 * Look in variable ranges
1676 * Look of multiple ranges matching this address and pick type
1677 * as per MTRR precedence
1678 */
1679 if (!(mtrr_state->enabled & 2))
1680 return mtrr_state->def_type;
1681
1682 prev_match = 0xFF;
1683 for (i = 0; i < num_var_ranges; ++i) {
1684 unsigned short start_state, end_state;
1685
1686 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1687 continue;
1688
1689 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1690 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1691 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1692 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1693
1694 start_state = ((start & mask) == (base & mask));
1695 end_state = ((end & mask) == (base & mask));
1696 if (start_state != end_state)
1697 return 0xFE;
1698
1699 if ((start & mask) != (base & mask))
1700 continue;
1701
1702 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1703 if (prev_match == 0xFF) {
1704 prev_match = curr_match;
1705 continue;
1706 }
1707
1708 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1709 curr_match == MTRR_TYPE_UNCACHABLE)
1710 return MTRR_TYPE_UNCACHABLE;
1711
1712 if ((prev_match == MTRR_TYPE_WRBACK &&
1713 curr_match == MTRR_TYPE_WRTHROUGH) ||
1714 (prev_match == MTRR_TYPE_WRTHROUGH &&
1715 curr_match == MTRR_TYPE_WRBACK)) {
1716 prev_match = MTRR_TYPE_WRTHROUGH;
1717 curr_match = MTRR_TYPE_WRTHROUGH;
1718 }
1719
1720 if (prev_match != curr_match)
1721 return MTRR_TYPE_UNCACHABLE;
1722 }
1723
1724 if (prev_match != 0xFF)
1725 return prev_match;
1726
1727 return mtrr_state->def_type;
1728}
1729
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001730u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001731{
1732 u8 mtrr;
1733
1734 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1735 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1736 if (mtrr == 0xfe || mtrr == 0xff)
1737 mtrr = MTRR_TYPE_WRBACK;
1738 return mtrr;
1739}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001740EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001741
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001742static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1743{
1744 unsigned index;
1745 struct hlist_head *bucket;
1746 struct kvm_mmu_page *s;
1747 struct hlist_node *node, *n;
1748
Avi Kivityf691fe12009-07-06 15:58:14 +03001749 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001750 index = kvm_page_table_hashfn(sp->gfn);
1751 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1752 /* don't unsync if pagetable is shadowed with multiple roles */
1753 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001754 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001755 continue;
1756 if (s->role.word != sp->role.word)
1757 return 1;
1758 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001759 ++vcpu->kvm->stat.mmu_unsync;
1760 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001761
Xiao Guangrong6b184932010-04-16 21:29:17 +08001762 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001763
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001764 mmu_convert_notrap(sp);
1765 return 0;
1766}
1767
1768static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1769 bool can_unsync)
1770{
1771 struct kvm_mmu_page *shadow;
1772
1773 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1774 if (shadow) {
1775 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1776 return 1;
1777 if (shadow->unsync)
1778 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001779 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001780 return kvm_unsync_page(vcpu, shadow);
1781 return 1;
1782 }
1783 return 0;
1784}
1785
Avi Kivityd555c332009-06-10 14:24:23 +03001786static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001787 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001788 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001789 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001790 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001791{
1792 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001793 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001794
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001795 /*
1796 * We don't set the accessed bit, since we sometimes want to see
1797 * whether the guest actually used the pte (in order to detect
1798 * demand paging).
1799 */
Sheng Yang7b523452008-04-25 21:13:50 +08001800 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001801 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001802 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001803 if (!dirty)
1804 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001805 if (pte_access & ACC_EXEC_MASK)
1806 spte |= shadow_x_mask;
1807 else
1808 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001809 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001810 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001811 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001812 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001813 if (tdp_enabled)
1814 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1815 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001816
Izik Eidus14032832009-09-23 21:47:17 +03001817 if (reset_host_protection)
1818 spte |= SPTE_HOST_WRITEABLE;
1819
Anthony Liguori35149e22008-04-02 14:46:56 -05001820 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001821
1822 if ((pte_access & ACC_WRITE_MASK)
1823 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001824
Joerg Roedel852e3c12009-07-27 16:30:44 +02001825 if (level > PT_PAGE_TABLE_LEVEL &&
1826 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001827 ret = 1;
1828 spte = shadow_trap_nonpresent_pte;
1829 goto set_pte;
1830 }
1831
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001832 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001833
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001834 /*
1835 * Optimization: for pte sync, if spte was writable the hash
1836 * lookup is unnecessary (and expensive). Write protection
1837 * is responsibility of mmu_get_page / kvm_sync_page.
1838 * Same reasoning can be applied to dirty page accounting.
1839 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001840 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001841 goto set_pte;
1842
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001843 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001844 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001845 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001846 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001847 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001848 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001849 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001850 }
1851 }
1852
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001853 if (pte_access & ACC_WRITE_MASK)
1854 mark_page_dirty(vcpu->kvm, gfn);
1855
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001856set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001857 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001858 return ret;
1859}
1860
Avi Kivityd555c332009-06-10 14:24:23 +03001861static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001862 unsigned pt_access, unsigned pte_access,
1863 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001864 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001865 pfn_t pfn, bool speculative,
1866 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001867{
1868 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001869 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001870 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001871
1872 pgprintk("%s: spte %llx access %x write_fault %d"
1873 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001874 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001875 write_fault, user_fault, gfn);
1876
Avi Kivityd555c332009-06-10 14:24:23 +03001877 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001878 /*
1879 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1880 * the parent of the now unreachable PTE.
1881 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001882 if (level > PT_PAGE_TABLE_LEVEL &&
1883 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001884 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001885 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001886
1887 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001888 mmu_page_remove_parent_pte(child, sptep);
1889 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001890 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001891 spte_to_pfn(*sptep), pfn);
1892 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001893 } else
1894 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001895 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001896
Avi Kivityd555c332009-06-10 14:24:23 +03001897 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001898 dirty, level, gfn, pfn, speculative, true,
1899 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001900 if (write_fault)
1901 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001902 kvm_x86_ops->tlb_flush(vcpu);
1903 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001904
Avi Kivityd555c332009-06-10 14:24:23 +03001905 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001906 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001907 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001908 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1909 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001910 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001911 ++vcpu->kvm->stat.lpages;
1912
Avi Kivityd555c332009-06-10 14:24:23 +03001913 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001914 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001915 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001916 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001917 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001918 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001919 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001920 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001921 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001922 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001923 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001924 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001925 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001926 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001927 vcpu->arch.last_pte_gfn = gfn;
1928 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001929}
1930
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1932{
1933}
1934
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001935static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001936 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937{
Avi Kivity9f652d22008-12-25 14:54:25 +02001938 struct kvm_shadow_walk_iterator iterator;
1939 struct kvm_mmu_page *sp;
1940 int pt_write = 0;
1941 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942
Avi Kivity9f652d22008-12-25 14:54:25 +02001943 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001944 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001945 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1946 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001947 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001948 ++vcpu->stat.pf_fixed;
1949 break;
1950 }
1951
1952 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1953 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1954 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1955 iterator.level - 1,
1956 1, ACC_ALL, iterator.sptep);
1957 if (!sp) {
1958 pgprintk("nonpaging_map: ENOMEM\n");
1959 kvm_release_pfn_clean(pfn);
1960 return -ENOMEM;
1961 }
1962
Avi Kivityd555c332009-06-10 14:24:23 +03001963 __set_spte(iterator.sptep,
1964 __pa(sp->spt)
1965 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1966 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001967 }
1968 }
1969 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970}
1971
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001972static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1973{
1974 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001975 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001976 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001977 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001978
Joerg Roedel852e3c12009-07-27 16:30:44 +02001979 level = mapping_level(vcpu, gfn);
1980
1981 /*
1982 * This path builds a PAE pagetable - so we can map 2mb pages at
1983 * maximum. Therefore check if the level is larger than that.
1984 */
1985 if (level > PT_DIRECTORY_LEVEL)
1986 level = PT_DIRECTORY_LEVEL;
1987
1988 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001989
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001990 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001991 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001992 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001993
Avi Kivityd196e342008-01-24 11:44:11 +02001994 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001995 if (is_error_pfn(pfn)) {
1996 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001997 return 1;
1998 }
1999
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002000 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002001 if (mmu_notifier_retry(vcpu, mmu_seq))
2002 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002003 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002004 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002005 spin_unlock(&vcpu->kvm->mmu_lock);
2006
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002007
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002008 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002009
2010out_unlock:
2011 spin_unlock(&vcpu->kvm->mmu_lock);
2012 kvm_release_pfn_clean(pfn);
2013 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002014}
2015
2016
Avi Kivity17ac10a2007-01-05 16:36:40 -08002017static void mmu_free_roots(struct kvm_vcpu *vcpu)
2018{
2019 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002020 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002021
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002022 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002023 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002024 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002025 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2026 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002027
Avi Kivity4db35312007-11-21 15:28:32 +02002028 sp = page_header(root);
2029 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002030 if (!sp->root_count && sp->role.invalid)
2031 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002032 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002033 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002034 return;
2035 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002036 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002037 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038
Avi Kivity417726a2007-04-12 17:35:58 +03002039 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002040 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002041 sp = page_header(root);
2042 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002043 if (!sp->root_count && sp->role.invalid)
2044 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002045 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002046 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002047 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002048 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002049 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002050}
2051
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002052static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2053{
2054 int ret = 0;
2055
2056 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2057 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2058 ret = 1;
2059 }
2060
2061 return ret;
2062}
2063
2064static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002065{
2066 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002067 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002068 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002069 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002070 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002071
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002072 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002073
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002074 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2075 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002076
2077 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002078 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002079 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002080 if (mmu_check_root(vcpu, root_gfn))
2081 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002082 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002083 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002084 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002085 root = __pa(sp->spt);
2086 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002087 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002088 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002089 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002090 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002091 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002092 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002093 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002094 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002095
2096 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002097 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002098 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002099 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002100 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002101 continue;
2102 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002103 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002104 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002105 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002106 if (mmu_check_root(vcpu, root_gfn))
2107 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002108 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002109 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002110 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002111 root = __pa(sp->spt);
2112 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002114 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002115 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002116 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002117}
2118
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002119static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2120{
2121 int i;
2122 struct kvm_mmu_page *sp;
2123
2124 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2125 return;
2126 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2127 hpa_t root = vcpu->arch.mmu.root_hpa;
2128 sp = page_header(root);
2129 mmu_sync_children(vcpu, sp);
2130 return;
2131 }
2132 for (i = 0; i < 4; ++i) {
2133 hpa_t root = vcpu->arch.mmu.pae_root[i];
2134
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002135 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002136 root &= PT64_BASE_ADDR_MASK;
2137 sp = page_header(root);
2138 mmu_sync_children(vcpu, sp);
2139 }
2140 }
2141}
2142
2143void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2144{
2145 spin_lock(&vcpu->kvm->mmu_lock);
2146 mmu_sync_roots(vcpu);
2147 spin_unlock(&vcpu->kvm->mmu_lock);
2148}
2149
Gleb Natapov1871c602010-02-10 14:21:32 +02002150static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2151 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152{
Gleb Natapov1871c602010-02-10 14:21:32 +02002153 if (error)
2154 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 return vaddr;
2156}
2157
2158static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002159 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160{
Avi Kivitye8332402007-12-09 18:43:00 +02002161 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002162 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002164 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002165 r = mmu_topup_memory_caches(vcpu);
2166 if (r)
2167 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002168
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002170 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171
Avi Kivitye8332402007-12-09 18:43:00 +02002172 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173
Avi Kivitye8332402007-12-09 18:43:00 +02002174 return nonpaging_map(vcpu, gva & PAGE_MASK,
2175 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176}
2177
Joerg Roedelfb72d162008-02-07 13:47:44 +01002178static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2179 u32 error_code)
2180{
Anthony Liguori35149e22008-04-02 14:46:56 -05002181 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002182 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002183 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002184 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002185 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002186
2187 ASSERT(vcpu);
2188 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2189
2190 r = mmu_topup_memory_caches(vcpu);
2191 if (r)
2192 return r;
2193
Joerg Roedel852e3c12009-07-27 16:30:44 +02002194 level = mapping_level(vcpu, gfn);
2195
2196 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2197
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002198 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002199 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002200 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002201 if (is_error_pfn(pfn)) {
2202 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002203 return 1;
2204 }
2205 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002206 if (mmu_notifier_retry(vcpu, mmu_seq))
2207 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002208 kvm_mmu_free_some_pages(vcpu);
2209 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002210 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002211 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002212
2213 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002214
2215out_unlock:
2216 spin_unlock(&vcpu->kvm->mmu_lock);
2217 kvm_release_pfn_clean(pfn);
2218 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002219}
2220
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221static void nonpaging_free(struct kvm_vcpu *vcpu)
2222{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002223 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224}
2225
2226static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2227{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002228 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229
2230 context->new_cr3 = nonpaging_new_cr3;
2231 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 context->gva_to_gpa = nonpaging_gva_to_gpa;
2233 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002234 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002235 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002236 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002237 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002239 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 return 0;
2241}
2242
Avi Kivityd835dfe2007-11-21 02:57:59 +02002243void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002245 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002246 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247}
2248
2249static void paging_new_cr3(struct kvm_vcpu *vcpu)
2250{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002251 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002252 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253}
2254
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255static void inject_page_fault(struct kvm_vcpu *vcpu,
2256 u64 addr,
2257 u32 err_code)
2258{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002259 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260}
2261
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262static void paging_free(struct kvm_vcpu *vcpu)
2263{
2264 nonpaging_free(vcpu);
2265}
2266
Dong, Eddie82725b22009-03-30 16:21:08 +08002267static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2268{
2269 int bit7;
2270
2271 bit7 = (gpte >> 7) & 1;
2272 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2273}
2274
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275#define PTTYPE 64
2276#include "paging_tmpl.h"
2277#undef PTTYPE
2278
2279#define PTTYPE 32
2280#include "paging_tmpl.h"
2281#undef PTTYPE
2282
Dong, Eddie82725b22009-03-30 16:21:08 +08002283static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2284{
2285 struct kvm_mmu *context = &vcpu->arch.mmu;
2286 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2287 u64 exb_bit_rsvd = 0;
2288
2289 if (!is_nx(vcpu))
2290 exb_bit_rsvd = rsvd_bits(63, 63);
2291 switch (level) {
2292 case PT32_ROOT_LEVEL:
2293 /* no rsvd bits for 2 level 4K page table entries */
2294 context->rsvd_bits_mask[0][1] = 0;
2295 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002296 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2297
2298 if (!is_pse(vcpu)) {
2299 context->rsvd_bits_mask[1][1] = 0;
2300 break;
2301 }
2302
Dong, Eddie82725b22009-03-30 16:21:08 +08002303 if (is_cpuid_PSE36())
2304 /* 36bits PSE 4MB page */
2305 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2306 else
2307 /* 32 bits PSE 4MB page */
2308 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002309 break;
2310 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002311 context->rsvd_bits_mask[0][2] =
2312 rsvd_bits(maxphyaddr, 63) |
2313 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002314 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002315 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002316 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2317 rsvd_bits(maxphyaddr, 62); /* PTE */
2318 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2319 rsvd_bits(maxphyaddr, 62) |
2320 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002321 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002322 break;
2323 case PT64_ROOT_LEVEL:
2324 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2325 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2326 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2327 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2328 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002329 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002330 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2331 rsvd_bits(maxphyaddr, 51);
2332 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002333 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2334 rsvd_bits(maxphyaddr, 51) |
2335 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002336 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002337 rsvd_bits(maxphyaddr, 51) |
2338 rsvd_bits(13, 20); /* large page */
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 }
2342}
2343
Avi Kivity17ac10a2007-01-05 16:36:40 -08002344static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002346 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
2348 ASSERT(is_pae(vcpu));
2349 context->new_cr3 = paging_new_cr3;
2350 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002352 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002353 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002354 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002356 context->root_level = level;
2357 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002358 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 return 0;
2360}
2361
Avi Kivity17ac10a2007-01-05 16:36:40 -08002362static int paging64_init_context(struct kvm_vcpu *vcpu)
2363{
Dong, Eddie82725b22009-03-30 16:21:08 +08002364 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002365 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2366}
2367
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368static int paging32_init_context(struct kvm_vcpu *vcpu)
2369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002370 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371
Dong, Eddie82725b22009-03-30 16:21:08 +08002372 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 context->new_cr3 = paging_new_cr3;
2374 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375 context->gva_to_gpa = paging32_gva_to_gpa;
2376 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002377 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002378 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002379 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 context->root_level = PT32_ROOT_LEVEL;
2381 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002382 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 return 0;
2384}
2385
2386static int paging32E_init_context(struct kvm_vcpu *vcpu)
2387{
Dong, Eddie82725b22009-03-30 16:21:08 +08002388 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002389 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390}
2391
Joerg Roedelfb72d162008-02-07 13:47:44 +01002392static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2393{
2394 struct kvm_mmu *context = &vcpu->arch.mmu;
2395
2396 context->new_cr3 = nonpaging_new_cr3;
2397 context->page_fault = tdp_page_fault;
2398 context->free = nonpaging_free;
2399 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002400 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002401 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002402 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002403 context->root_hpa = INVALID_PAGE;
2404
2405 if (!is_paging(vcpu)) {
2406 context->gva_to_gpa = nonpaging_gva_to_gpa;
2407 context->root_level = 0;
2408 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002409 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002410 context->gva_to_gpa = paging64_gva_to_gpa;
2411 context->root_level = PT64_ROOT_LEVEL;
2412 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002413 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002414 context->gva_to_gpa = paging64_gva_to_gpa;
2415 context->root_level = PT32E_ROOT_LEVEL;
2416 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002417 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002418 context->gva_to_gpa = paging32_gva_to_gpa;
2419 context->root_level = PT32_ROOT_LEVEL;
2420 }
2421
2422 return 0;
2423}
2424
2425static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002427 int r;
2428
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002430 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431
2432 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002433 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002434 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002435 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002437 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002439 r = paging32_init_context(vcpu);
2440
Avi Kivity5b7e0102010-04-14 19:20:03 +03002441 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002442
2443 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444}
2445
Joerg Roedelfb72d162008-02-07 13:47:44 +01002446static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2447{
Anthony Liguori35149e22008-04-02 14:46:56 -05002448 vcpu->arch.update_pte.pfn = bad_pfn;
2449
Joerg Roedelfb72d162008-02-07 13:47:44 +01002450 if (tdp_enabled)
2451 return init_kvm_tdp_mmu(vcpu);
2452 else
2453 return init_kvm_softmmu(vcpu);
2454}
2455
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2457{
2458 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002459 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2460 vcpu->arch.mmu.free(vcpu);
2461 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 }
2463}
2464
2465int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2466{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002467 destroy_kvm_mmu(vcpu);
2468 return init_kvm_mmu(vcpu);
2469}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002470EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002471
2472int kvm_mmu_load(struct kvm_vcpu *vcpu)
2473{
Avi Kivity714b93d2007-01-05 16:36:53 -08002474 int r;
2475
Avi Kivitye2dec932007-01-05 16:36:54 -08002476 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002477 if (r)
2478 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002479 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002480 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002481 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002482 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002483 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002484 if (r)
2485 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002486 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002487 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002488out:
2489 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002491EXPORT_SYMBOL_GPL(kvm_mmu_load);
2492
2493void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2494{
2495 mmu_free_roots(vcpu);
2496}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497
Avi Kivity09072da2007-05-01 14:16:52 +03002498static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002499 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002500 u64 *spte)
2501{
2502 u64 pte;
2503 struct kvm_mmu_page *child;
2504
2505 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002506 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002507 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002508 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002509 else {
2510 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002511 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002512 }
2513 }
Avi Kivityd555c332009-06-10 14:24:23 +03002514 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002515 if (is_large_pte(pte))
2516 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002517}
2518
Avi Kivity00284252007-05-01 16:53:31 +03002519static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002520 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002521 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002522 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002523{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002524 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002525 ++vcpu->kvm->stat.mmu_pde_zapped;
2526 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002527 }
Avi Kivity00284252007-05-01 16:53:31 +03002528
Avi Kivity4cee5762007-11-18 16:37:07 +02002529 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002530 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002531 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002532 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002533 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002534}
2535
Avi Kivity79539ce2007-11-21 02:06:21 +02002536static bool need_remote_flush(u64 old, u64 new)
2537{
2538 if (!is_shadow_present_pte(old))
2539 return false;
2540 if (!is_shadow_present_pte(new))
2541 return true;
2542 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2543 return true;
2544 old ^= PT64_NX_MASK;
2545 new ^= PT64_NX_MASK;
2546 return (old & ~new & PT64_PERM_MASK) != 0;
2547}
2548
2549static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2550{
2551 if (need_remote_flush(old, new))
2552 kvm_flush_remote_tlbs(vcpu->kvm);
2553 else
2554 kvm_mmu_flush_tlb(vcpu);
2555}
2556
Avi Kivity12b7d282007-09-23 14:10:49 +02002557static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2558{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002559 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002560
Sheng Yang7b523452008-04-25 21:13:50 +08002561 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002562}
2563
Avi Kivityd7824ff2007-12-30 12:29:05 +02002564static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002565 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002566{
2567 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002568 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002569
Avi Kivity43a37952009-06-10 14:12:05 +03002570 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002571 return;
2572 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002573
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002574 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002575 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002576 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002577
Anthony Liguori35149e22008-04-02 14:46:56 -05002578 if (is_error_pfn(pfn)) {
2579 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002580 return;
2581 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002582 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002583 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002584}
2585
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002586static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2587{
2588 u64 *spte = vcpu->arch.last_pte_updated;
2589
2590 if (spte
2591 && vcpu->arch.last_pte_gfn == gfn
2592 && shadow_accessed_mask
2593 && !(*spte & shadow_accessed_mask)
2594 && is_shadow_present_pte(*spte))
2595 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2596}
2597
Avi Kivity09072da2007-05-01 14:16:52 +03002598void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002599 const u8 *new, int bytes,
2600 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002601{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002602 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002603 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002604 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002605 struct hlist_head *bucket;
2606 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002607 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002608 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002609 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002610 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002611 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002612 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002613 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002614 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002615 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002616 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002617 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002618 int invlpg_counter;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002619
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002620 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002621
Avi Kivity08e850c2010-03-15 13:59:57 +02002622 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2623
2624 /*
2625 * Assume that the pte write on a page table of the same type
2626 * as the current vcpu paging mode. This is nearly always true
2627 * (might be false while changing modes). Note it is verified later
2628 * by update_pte().
2629 */
2630 if ((is_pae(vcpu) && bytes == 4) || !new) {
2631 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2632 if (is_pae(vcpu)) {
2633 gpa &= ~(gpa_t)7;
2634 bytes = 8;
2635 }
2636 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2637 if (r)
2638 gentry = 0;
2639 new = (const u8 *)&gentry;
2640 }
2641
Avi Kivity72016f32010-03-15 13:59:53 +02002642 switch (bytes) {
2643 case 4:
2644 gentry = *(const u32 *)new;
2645 break;
2646 case 8:
2647 gentry = *(const u64 *)new;
2648 break;
2649 default:
2650 gentry = 0;
2651 break;
2652 }
2653
Avi Kivity72016f32010-03-15 13:59:53 +02002654 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002655 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002656 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2657 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002658 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002659 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002660 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002661 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002662 if (guest_initiated) {
2663 if (gfn == vcpu->arch.last_pt_write_gfn
2664 && !last_updated_pte_accessed(vcpu)) {
2665 ++vcpu->arch.last_pt_write_count;
2666 if (vcpu->arch.last_pt_write_count >= 3)
2667 flooded = 1;
2668 } else {
2669 vcpu->arch.last_pt_write_gfn = gfn;
2670 vcpu->arch.last_pt_write_count = 1;
2671 vcpu->arch.last_pte_updated = NULL;
2672 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002673 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002674 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002675 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08002676
2677restart:
Avi Kivity4db35312007-11-21 15:28:32 +02002678 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002679 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002680 continue;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002681 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002682 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002683 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002684 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002685 /*
2686 * Misaligned accesses are too much trouble to fix
2687 * up; also, they usually indicate a page is not used
2688 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002689 *
2690 * If we're seeing too many writes to a page,
2691 * it may no longer be a page table, or we may be
2692 * forking, in which case it is better to unmap the
2693 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002694 */
2695 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002696 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002697 if (kvm_mmu_zap_page(vcpu->kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002698 goto restart;
Avi Kivity4cee5762007-11-18 16:37:07 +02002699 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002700 continue;
2701 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002702 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002703 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002704 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002705 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002706 page_offset <<= 1; /* 32->64 */
2707 /*
2708 * A 32-bit pde maps 4MB while the shadow pdes map
2709 * only 2MB. So we need to double the offset again
2710 * and zap two pdes instead of one.
2711 */
2712 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002713 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002714 page_offset <<= 1;
2715 npte = 2;
2716 }
Avi Kivityfce06572007-05-01 16:44:05 +03002717 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002718 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002719 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002720 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002721 }
Avi Kivity4db35312007-11-21 15:28:32 +02002722 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002723 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002724 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002725 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002726 if (gentry)
2727 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Avi Kivity79539ce2007-11-21 02:06:21 +02002728 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002729 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002730 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002731 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002732 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002733 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002734 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2735 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2736 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002737 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002738}
2739
Avi Kivitya4360362007-01-05 16:36:45 -08002740int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2741{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002742 gpa_t gpa;
2743 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002744
Avi Kivity60f24782009-08-27 13:37:06 +03002745 if (tdp_enabled)
2746 return 0;
2747
Gleb Natapov1871c602010-02-10 14:21:32 +02002748 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002749
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002750 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002751 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002752 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002753 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002754}
Avi Kivity577bdc42008-07-19 08:57:05 +03002755EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002756
Avi Kivity22d95b12007-09-14 20:26:06 +03002757void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002758{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002759 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2760 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002761 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002762
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002763 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002764 struct kvm_mmu_page, link);
2765 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002766 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002767 }
2768}
Avi Kivityebeace82007-01-05 16:36:47 -08002769
Avi Kivity30677142007-10-28 18:48:59 +02002770int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2771{
2772 int r;
2773 enum emulation_result er;
2774
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002775 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002776 if (r < 0)
2777 goto out;
2778
2779 if (!r) {
2780 r = 1;
2781 goto out;
2782 }
2783
Avi Kivityb733bfb2007-10-28 18:52:05 +02002784 r = mmu_topup_memory_caches(vcpu);
2785 if (r)
2786 goto out;
2787
Avi Kivity851ba692009-08-24 11:10:17 +03002788 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002789
2790 switch (er) {
2791 case EMULATE_DONE:
2792 return 1;
2793 case EMULATE_DO_MMIO:
2794 ++vcpu->stat.mmio_exits;
2795 return 0;
2796 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002797 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2798 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002799 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002800 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002801 default:
2802 BUG();
2803 }
2804out:
Avi Kivity30677142007-10-28 18:48:59 +02002805 return r;
2806}
2807EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2808
Marcelo Tosattia7052892008-09-23 13:18:35 -03002809void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2810{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002811 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002812 kvm_mmu_flush_tlb(vcpu);
2813 ++vcpu->stat.invlpg;
2814}
2815EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2816
Joerg Roedel18552672008-02-07 13:47:41 +01002817void kvm_enable_tdp(void)
2818{
2819 tdp_enabled = true;
2820}
2821EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2822
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002823void kvm_disable_tdp(void)
2824{
2825 tdp_enabled = false;
2826}
2827EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2828
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829static void free_mmu_pages(struct kvm_vcpu *vcpu)
2830{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002831 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832}
2833
2834static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2835{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002836 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 int i;
2838
2839 ASSERT(vcpu);
2840
Avi Kivity17ac10a2007-01-05 16:36:40 -08002841 /*
2842 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2843 * Therefore we need to allocate shadow page tables in the first
2844 * 4GB of memory, which happens to fit the DMA32 zone.
2845 */
2846 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2847 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002848 return -ENOMEM;
2849
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002850 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002851 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002852 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002853
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855}
2856
Ingo Molnar8018c272006-12-29 16:50:01 -08002857int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002860 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861
Ingo Molnar8018c272006-12-29 16:50:01 -08002862 return alloc_mmu_pages(vcpu);
2863}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864
Ingo Molnar8018c272006-12-29 16:50:01 -08002865int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2866{
2867 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002868 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002869
Ingo Molnar8018c272006-12-29 16:50:01 -08002870 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871}
2872
2873void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2874{
2875 ASSERT(vcpu);
2876
2877 destroy_kvm_mmu(vcpu);
2878 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002879 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880}
2881
Avi Kivity90cb0522007-07-17 13:04:56 +03002882void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883{
Avi Kivity4db35312007-11-21 15:28:32 +02002884 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002886 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 int i;
2888 u64 *pt;
2889
Sheng Yang291f26b2008-10-16 17:30:57 +08002890 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 continue;
2892
Avi Kivity4db35312007-11-21 15:28:32 +02002893 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2895 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002896 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 }
Avi Kivity171d5952008-08-27 16:40:51 +03002899 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002901
Avi Kivity90cb0522007-07-17 13:04:56 +03002902void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002903{
Avi Kivity4db35312007-11-21 15:28:32 +02002904 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002905
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002906 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08002907restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002908 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002909 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002910 goto restart;
2911
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002912 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002913
Avi Kivity90cb0522007-07-17 13:04:56 +03002914 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002915}
2916
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002917static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002918{
2919 struct kvm_mmu_page *page;
2920
2921 page = container_of(kvm->arch.active_mmu_pages.prev,
2922 struct kvm_mmu_page, link);
2923 kvm_mmu_zap_page(kvm, page);
2924}
2925
2926static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2927{
2928 struct kvm *kvm;
2929 struct kvm *kvm_freed = NULL;
2930 int cache_count = 0;
2931
2932 spin_lock(&kvm_lock);
2933
2934 list_for_each_entry(kvm, &vm_list, vm_list) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002935 int npages, idx;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002936
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002937 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002938 spin_lock(&kvm->mmu_lock);
2939 npages = kvm->arch.n_alloc_mmu_pages -
2940 kvm->arch.n_free_mmu_pages;
2941 cache_count += npages;
2942 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2943 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2944 cache_count--;
2945 kvm_freed = kvm;
2946 }
2947 nr_to_scan--;
2948
2949 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002950 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002951 }
2952 if (kvm_freed)
2953 list_move_tail(&kvm_freed->vm_list, &vm_list);
2954
2955 spin_unlock(&kvm_lock);
2956
2957 return cache_count;
2958}
2959
2960static struct shrinker mmu_shrinker = {
2961 .shrink = mmu_shrink,
2962 .seeks = DEFAULT_SEEKS * 10,
2963};
2964
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002965static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002966{
2967 if (pte_chain_cache)
2968 kmem_cache_destroy(pte_chain_cache);
2969 if (rmap_desc_cache)
2970 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002971 if (mmu_page_header_cache)
2972 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002973}
2974
Izik Eidus3ee16c82008-03-30 15:17:21 +03002975void kvm_mmu_module_exit(void)
2976{
2977 mmu_destroy_caches();
2978 unregister_shrinker(&mmu_shrinker);
2979}
2980
Avi Kivityb5a33a72007-04-15 16:31:09 +03002981int kvm_mmu_module_init(void)
2982{
2983 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2984 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002985 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002986 if (!pte_chain_cache)
2987 goto nomem;
2988 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2989 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002990 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002991 if (!rmap_desc_cache)
2992 goto nomem;
2993
Avi Kivityd3d25b02007-05-30 12:34:53 +03002994 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2995 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002996 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002997 if (!mmu_page_header_cache)
2998 goto nomem;
2999
Izik Eidus3ee16c82008-03-30 15:17:21 +03003000 register_shrinker(&mmu_shrinker);
3001
Avi Kivityb5a33a72007-04-15 16:31:09 +03003002 return 0;
3003
3004nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003005 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003006 return -ENOMEM;
3007}
3008
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003009/*
3010 * Caculate mmu pages needed for kvm.
3011 */
3012unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3013{
3014 int i;
3015 unsigned int nr_mmu_pages;
3016 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003017 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003018
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003019 slots = rcu_dereference(kvm->memslots);
3020 for (i = 0; i < slots->nmemslots; i++)
3021 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003022
3023 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3024 nr_mmu_pages = max(nr_mmu_pages,
3025 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3026
3027 return nr_mmu_pages;
3028}
3029
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003030static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3031 unsigned len)
3032{
3033 if (len > buffer->len)
3034 return NULL;
3035 return buffer->ptr;
3036}
3037
3038static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3039 unsigned len)
3040{
3041 void *ret;
3042
3043 ret = pv_mmu_peek_buffer(buffer, len);
3044 if (!ret)
3045 return ret;
3046 buffer->ptr += len;
3047 buffer->len -= len;
3048 buffer->processed += len;
3049 return ret;
3050}
3051
3052static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3053 gpa_t addr, gpa_t value)
3054{
3055 int bytes = 8;
3056 int r;
3057
3058 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3059 bytes = 4;
3060
3061 r = mmu_topup_memory_caches(vcpu);
3062 if (r)
3063 return r;
3064
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003065 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003066 return -EFAULT;
3067
3068 return 1;
3069}
3070
3071static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3072{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003073 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003074 return 1;
3075}
3076
3077static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3078{
3079 spin_lock(&vcpu->kvm->mmu_lock);
3080 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3081 spin_unlock(&vcpu->kvm->mmu_lock);
3082 return 1;
3083}
3084
3085static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3086 struct kvm_pv_mmu_op_buffer *buffer)
3087{
3088 struct kvm_mmu_op_header *header;
3089
3090 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3091 if (!header)
3092 return 0;
3093 switch (header->op) {
3094 case KVM_MMU_OP_WRITE_PTE: {
3095 struct kvm_mmu_op_write_pte *wpte;
3096
3097 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3098 if (!wpte)
3099 return 0;
3100 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3101 wpte->pte_val);
3102 }
3103 case KVM_MMU_OP_FLUSH_TLB: {
3104 struct kvm_mmu_op_flush_tlb *ftlb;
3105
3106 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3107 if (!ftlb)
3108 return 0;
3109 return kvm_pv_mmu_flush_tlb(vcpu);
3110 }
3111 case KVM_MMU_OP_RELEASE_PT: {
3112 struct kvm_mmu_op_release_pt *rpt;
3113
3114 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3115 if (!rpt)
3116 return 0;
3117 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3118 }
3119 default: return 0;
3120 }
3121}
3122
3123int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3124 gpa_t addr, unsigned long *ret)
3125{
3126 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003127 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003128
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003129 buffer->ptr = buffer->buf;
3130 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3131 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003132
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003133 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003134 if (r)
3135 goto out;
3136
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003137 while (buffer->len) {
3138 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003139 if (r < 0)
3140 goto out;
3141 if (r == 0)
3142 break;
3143 }
3144
3145 r = 1;
3146out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003147 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003148 return r;
3149}
3150
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003151int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3152{
3153 struct kvm_shadow_walk_iterator iterator;
3154 int nr_sptes = 0;
3155
3156 spin_lock(&vcpu->kvm->mmu_lock);
3157 for_each_shadow_entry(vcpu, addr, iterator) {
3158 sptes[iterator.level-1] = *iterator.sptep;
3159 nr_sptes++;
3160 if (!is_shadow_present_pte(*iterator.sptep))
3161 break;
3162 }
3163 spin_unlock(&vcpu->kvm->mmu_lock);
3164
3165 return nr_sptes;
3166}
3167EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3168
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003169#ifdef AUDIT
3170
3171static const char *audit_msg;
3172
3173static gva_t canonicalize(gva_t gva)
3174{
3175#ifdef CONFIG_X86_64
3176 gva = (long long)(gva << 16) >> 16;
3177#endif
3178 return gva;
3179}
3180
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003181
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003182typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003183
3184static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3185 inspect_spte_fn fn)
3186{
3187 int i;
3188
3189 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3190 u64 ent = sp->spt[i];
3191
3192 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003193 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003194 struct kvm_mmu_page *child;
3195 child = page_header(ent & PT64_BASE_ADDR_MASK);
3196 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003197 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003198 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003199 }
3200 }
3201}
3202
3203static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3204{
3205 int i;
3206 struct kvm_mmu_page *sp;
3207
3208 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3209 return;
3210 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3211 hpa_t root = vcpu->arch.mmu.root_hpa;
3212 sp = page_header(root);
3213 __mmu_spte_walk(vcpu->kvm, sp, fn);
3214 return;
3215 }
3216 for (i = 0; i < 4; ++i) {
3217 hpa_t root = vcpu->arch.mmu.pae_root[i];
3218
3219 if (root && VALID_PAGE(root)) {
3220 root &= PT64_BASE_ADDR_MASK;
3221 sp = page_header(root);
3222 __mmu_spte_walk(vcpu->kvm, sp, fn);
3223 }
3224 }
3225 return;
3226}
3227
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003228static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3229 gva_t va, int level)
3230{
3231 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3232 int i;
3233 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3234
3235 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3236 u64 ent = pt[i];
3237
Avi Kivityc7addb92007-09-16 18:58:32 +02003238 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003239 continue;
3240
3241 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003242 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3243 audit_mappings_page(vcpu, ent, va, level - 1);
3244 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003245 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003246 gfn_t gfn = gpa >> PAGE_SHIFT;
3247 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3248 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003249
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003250 if (is_error_pfn(pfn)) {
3251 kvm_release_pfn_clean(pfn);
3252 continue;
3253 }
3254
Avi Kivityc7addb92007-09-16 18:58:32 +02003255 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003256 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003257 printk(KERN_ERR "xx audit error: (%s) levels %d"
3258 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003259 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003260 va, gpa, hpa, ent,
3261 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003262 else if (ent == shadow_notrap_nonpresent_pte
3263 && !is_error_hpa(hpa))
3264 printk(KERN_ERR "audit: (%s) notrap shadow,"
3265 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003266 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003267
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003268 }
3269 }
3270}
3271
3272static void audit_mappings(struct kvm_vcpu *vcpu)
3273{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003274 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003275
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003276 if (vcpu->arch.mmu.root_level == 4)
3277 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003278 else
3279 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003280 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003281 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003282 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003283 i << 30,
3284 2);
3285}
3286
3287static int count_rmaps(struct kvm_vcpu *vcpu)
3288{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003289 struct kvm *kvm = vcpu->kvm;
3290 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003291 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003292 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003293
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003294 idx = srcu_read_lock(&kvm->srcu);
3295 slots = rcu_dereference(kvm->memslots);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003296 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003297 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003298 struct kvm_rmap_desc *d;
3299
3300 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003301 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003302
Izik Eidus290fc382007-09-27 14:11:22 +02003303 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003304 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003305 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003306 ++nmaps;
3307 continue;
3308 }
Izik Eidus290fc382007-09-27 14:11:22 +02003309 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003310 while (d) {
3311 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003312 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003313 ++nmaps;
3314 else
3315 break;
3316 d = d->more;
3317 }
3318 }
3319 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003320 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003321 return nmaps;
3322}
3323
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003324void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003325{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003326 unsigned long *rmapp;
3327 struct kvm_mmu_page *rev_sp;
3328 gfn_t gfn;
3329
3330 if (*sptep & PT_WRITABLE_MASK) {
3331 rev_sp = page_header(__pa(sptep));
3332 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3333
3334 if (!gfn_to_memslot(kvm, gfn)) {
3335 if (!printk_ratelimit())
3336 return;
3337 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3338 audit_msg, gfn);
3339 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003340 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003341 rev_sp->gfn);
3342 dump_stack();
3343 return;
3344 }
3345
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003346 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003347 rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003348 if (!*rmapp) {
3349 if (!printk_ratelimit())
3350 return;
3351 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3352 audit_msg, *sptep);
3353 dump_stack();
3354 }
3355 }
3356
3357}
3358
3359void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3360{
3361 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3362}
3363
3364static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3365{
Avi Kivity4db35312007-11-21 15:28:32 +02003366 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367 int i;
3368
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003369 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003370 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003371
Avi Kivity4db35312007-11-21 15:28:32 +02003372 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003373 continue;
3374
3375 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3376 u64 ent = pt[i];
3377
3378 if (!(ent & PT_PRESENT_MASK))
3379 continue;
3380 if (!(ent & PT_WRITABLE_MASK))
3381 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003382 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003383 }
3384 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003385 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003386}
3387
3388static void audit_rmap(struct kvm_vcpu *vcpu)
3389{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003390 check_writable_mappings_rmap(vcpu);
3391 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003392}
3393
3394static void audit_write_protection(struct kvm_vcpu *vcpu)
3395{
Avi Kivity4db35312007-11-21 15:28:32 +02003396 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003397 struct kvm_memory_slot *slot;
3398 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003399 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003400 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003401
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003402 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003403 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003404 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003405 if (sp->unsync)
3406 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003407
Avi Kivity4db35312007-11-21 15:28:32 +02003408 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003409 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003410 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003411
3412 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3413 while (spte) {
3414 if (*spte & PT_WRITABLE_MASK)
3415 printk(KERN_ERR "%s: (%s) shadow page has "
3416 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003417 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003418 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003419 spte = rmap_next(vcpu->kvm, rmapp, spte);
3420 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003421 }
3422}
3423
3424static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3425{
3426 int olddbg = dbg;
3427
3428 dbg = 0;
3429 audit_msg = msg;
3430 audit_rmap(vcpu);
3431 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003432 if (strcmp("pre pte write", audit_msg) != 0)
3433 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003434 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003435 dbg = olddbg;
3436}
3437
3438#endif