blob: a6f695d76928675008a99f2030b00299856840c4 [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 Kivity3dbe1412010-05-12 11:48:18 +0300220static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221{
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);
Wei Yongjun77a1a712010-04-16 16:21:42 +0800434 slot = gfn_to_memslot_unaliased(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200435 for (i = PT_DIRECTORY_LEVEL;
436 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200437 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 u64 *prev_spte;
651 int i;
652
653 if (!*rmapp)
654 return NULL;
655 else if (!(*rmapp & 1)) {
656 if (!spte)
657 return (u64 *)*rmapp;
658 return NULL;
659 }
660 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200661 prev_spte = NULL;
662 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300663 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200664 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300665 return desc->sptes[i];
666 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200667 }
668 desc = desc->more;
669 }
670 return NULL;
671}
672
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200673static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200674{
Izik Eidus290fc382007-09-27 14:11:22 +0200675 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800676 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200677 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800678
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500679 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200680 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800681
Izik Eidus98348e92007-10-16 14:42:30 +0200682 spte = rmap_next(kvm, rmapp, NULL);
683 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800684 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800685 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800686 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900687 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300688 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800689 write_protected = 1;
690 }
Izik Eidus9647c142007-10-16 14:43:46 +0200691 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800692 }
Izik Eidus855149a2008-03-20 18:17:24 +0200693 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500694 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200695
696 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500697 pfn = spte_to_pfn(*spte);
698 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200699 }
700
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300701 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200702 for (i = PT_DIRECTORY_LEVEL;
703 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
704 rmapp = gfn_to_rmap(kvm, gfn, i);
705 spte = rmap_next(kvm, rmapp, NULL);
706 while (spte) {
707 BUG_ON(!spte);
708 BUG_ON(!(*spte & PT_PRESENT_MASK));
709 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
710 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900711 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200712 rmap_remove(kvm, spte);
713 --kvm->stat.lpages;
714 __set_spte(spte, shadow_trap_nonpresent_pte);
715 spte = NULL;
716 write_protected = 1;
717 }
718 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300719 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300720 }
721
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200722 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800723}
724
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000725static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
726 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200727{
728 u64 *spte;
729 int need_tlb_flush = 0;
730
731 while ((spte = rmap_next(kvm, rmapp, NULL))) {
732 BUG_ON(!(*spte & PT_PRESENT_MASK));
733 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
734 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300735 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200736 need_tlb_flush = 1;
737 }
738 return need_tlb_flush;
739}
740
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000741static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
742 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300743{
744 int need_flush = 0;
745 u64 *spte, new_spte;
746 pte_t *ptep = (pte_t *)data;
747 pfn_t new_pfn;
748
749 WARN_ON(pte_huge(*ptep));
750 new_pfn = pte_pfn(*ptep);
751 spte = rmap_next(kvm, rmapp, NULL);
752 while (spte) {
753 BUG_ON(!is_shadow_present_pte(*spte));
754 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
755 need_flush = 1;
756 if (pte_write(*ptep)) {
757 rmap_remove(kvm, spte);
758 __set_spte(spte, shadow_trap_nonpresent_pte);
759 spte = rmap_next(kvm, rmapp, NULL);
760 } else {
761 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
762 new_spte |= (u64)new_pfn << PAGE_SHIFT;
763
764 new_spte &= ~PT_WRITABLE_MASK;
765 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900766 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300767 kvm_set_pfn_dirty(spte_to_pfn(*spte));
768 __set_spte(spte, new_spte);
769 spte = rmap_next(kvm, rmapp, spte);
770 }
771 }
772 if (need_flush)
773 kvm_flush_remote_tlbs(kvm);
774
775 return 0;
776}
777
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000778static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
779 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300780 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000781 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200782{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200783 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200784 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200785 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200786 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200787
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800788 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200789
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200790 for (i = 0; i < slots->nmemslots; i++) {
791 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200792 unsigned long start = memslot->userspace_addr;
793 unsigned long end;
794
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200795 end = start + (memslot->npages << PAGE_SHIFT);
796 if (hva >= start && hva < end) {
797 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200798
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200799 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200800
801 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
802 int idx = gfn_offset;
803 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200804 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300805 &memslot->lpage_info[j][idx].rmap_pde,
806 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200807 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200808 trace_kvm_age_page(hva, memslot, ret);
809 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200810 }
811 }
812
813 return retval;
814}
815
816int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
817{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300818 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200819}
820
Izik Eidus3da0dd42009-09-23 21:47:18 +0300821void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
822{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000823 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300824}
825
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000826static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
827 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200828{
829 u64 *spte;
830 int young = 0;
831
Rik van Riel6316e1c2010-02-03 16:11:03 -0500832 /*
833 * Emulate the accessed bit for EPT, by checking if this page has
834 * an EPT mapping, and clearing it if it does. On the next access,
835 * a new EPT mapping will be established.
836 * This has some overhead, but not as much as the cost of swapping
837 * out actively used pages or breaking up actively used hugepages.
838 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800839 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500840 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800841
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200842 spte = rmap_next(kvm, rmapp, NULL);
843 while (spte) {
844 int _young;
845 u64 _spte = *spte;
846 BUG_ON(!(_spte & PT_PRESENT_MASK));
847 _young = _spte & PT_ACCESSED_MASK;
848 if (_young) {
849 young = 1;
850 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
851 }
852 spte = rmap_next(kvm, rmapp, spte);
853 }
854 return young;
855}
856
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300857#define RMAP_RECYCLE_THRESHOLD 1000
858
Joerg Roedel852e3c12009-07-27 16:30:44 +0200859static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300860{
861 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200862 struct kvm_mmu_page *sp;
863
864 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300865
866 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200867 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300868
Izik Eidus3da0dd42009-09-23 21:47:18 +0300869 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300870 kvm_flush_remote_tlbs(vcpu->kvm);
871}
872
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200873int kvm_age_hva(struct kvm *kvm, unsigned long hva)
874{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300875 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200876}
877
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800878#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300879static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880{
Avi Kivity139bdb22007-01-05 16:36:50 -0800881 u64 *pos;
882 u64 *end;
883
Avi Kivity47ad8e62007-05-06 15:50:58 +0300884 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300885 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800886 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800887 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800889 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 return 1;
891}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800892#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893
Avi Kivity4db35312007-11-21 15:28:32 +0200894static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800895{
Avi Kivity4db35312007-11-21 15:28:32 +0200896 ASSERT(is_empty_shadow_page(sp->spt));
897 list_del(&sp->link);
898 __free_page(virt_to_page(sp->spt));
899 __free_page(virt_to_page(sp->gfns));
900 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800901 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800902}
903
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800904static unsigned kvm_page_table_hashfn(gfn_t gfn)
905{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200906 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800907}
908
Avi Kivity25c0de22007-01-05 16:36:42 -0800909static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
910 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911{
Avi Kivity4db35312007-11-21 15:28:32 +0200912 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800914 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
915 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
916 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200917 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800918 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800919 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200920 sp->multimapped = 0;
921 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800922 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200923 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924}
925
Avi Kivity714b93d2007-01-05 16:36:53 -0800926static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200927 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800928{
929 struct kvm_pte_chain *pte_chain;
930 struct hlist_node *node;
931 int i;
932
933 if (!parent_pte)
934 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200935 if (!sp->multimapped) {
936 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800937
938 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200939 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800940 return;
941 }
Avi Kivity4db35312007-11-21 15:28:32 +0200942 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800943 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200944 INIT_HLIST_HEAD(&sp->parent_ptes);
945 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946 pte_chain->parent_ptes[0] = old;
947 }
Avi Kivity4db35312007-11-21 15:28:32 +0200948 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800949 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
950 continue;
951 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
952 if (!pte_chain->parent_ptes[i]) {
953 pte_chain->parent_ptes[i] = parent_pte;
954 return;
955 }
956 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800957 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800958 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200959 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800960 pte_chain->parent_ptes[0] = parent_pte;
961}
962
Avi Kivity4db35312007-11-21 15:28:32 +0200963static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 u64 *parent_pte)
965{
966 struct kvm_pte_chain *pte_chain;
967 struct hlist_node *node;
968 int i;
969
Avi Kivity4db35312007-11-21 15:28:32 +0200970 if (!sp->multimapped) {
971 BUG_ON(sp->parent_pte != parent_pte);
972 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800973 return;
974 }
Avi Kivity4db35312007-11-21 15:28:32 +0200975 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800976 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
977 if (!pte_chain->parent_ptes[i])
978 break;
979 if (pte_chain->parent_ptes[i] != parent_pte)
980 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800981 while (i + 1 < NR_PTE_CHAIN_ENTRIES
982 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800983 pte_chain->parent_ptes[i]
984 = pte_chain->parent_ptes[i + 1];
985 ++i;
986 }
987 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800988 if (i == 0) {
989 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300990 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200991 if (hlist_empty(&sp->parent_ptes)) {
992 sp->multimapped = 0;
993 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800994 }
995 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800996 return;
997 }
998 BUG();
999}
1000
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001001
Xiao Guangrong6b184932010-04-16 21:29:17 +08001002static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001003{
1004 struct kvm_pte_chain *pte_chain;
1005 struct hlist_node *node;
1006 struct kvm_mmu_page *parent_sp;
1007 int i;
1008
1009 if (!sp->multimapped && sp->parent_pte) {
1010 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001011 fn(parent_sp);
1012 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001013 return;
1014 }
1015 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1016 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1017 if (!pte_chain->parent_ptes[i])
1018 break;
1019 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001020 fn(parent_sp);
1021 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001022 }
1023}
1024
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001025static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1026{
1027 unsigned int index;
1028 struct kvm_mmu_page *sp = page_header(__pa(spte));
1029
1030 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001031 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1032 sp->unsync_children++;
1033 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001034}
1035
1036static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1037{
1038 struct kvm_pte_chain *pte_chain;
1039 struct hlist_node *node;
1040 int i;
1041
1042 if (!sp->parent_pte)
1043 return;
1044
1045 if (!sp->multimapped) {
1046 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1047 return;
1048 }
1049
1050 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1051 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1052 if (!pte_chain->parent_ptes[i])
1053 break;
1054 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1055 }
1056}
1057
Xiao Guangrong6b184932010-04-16 21:29:17 +08001058static int unsync_walk_fn(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001059{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001060 kvm_mmu_update_parents_unsync(sp);
1061 return 1;
1062}
1063
Xiao Guangrong6b184932010-04-16 21:29:17 +08001064static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001065{
Xiao Guangrong6b184932010-04-16 21:29:17 +08001066 mmu_parent_walk(sp, unsync_walk_fn);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001067 kvm_mmu_update_parents_unsync(sp);
1068}
1069
Avi Kivityd761a502008-05-29 14:55:03 +03001070static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1071 struct kvm_mmu_page *sp)
1072{
1073 int i;
1074
1075 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1076 sp->spt[i] = shadow_trap_nonpresent_pte;
1077}
1078
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001079static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1080 struct kvm_mmu_page *sp)
1081{
1082 return 1;
1083}
1084
Marcelo Tosattia7052892008-09-23 13:18:35 -03001085static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1086{
1087}
1088
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001089#define KVM_PAGE_ARRAY_NR 16
1090
1091struct kvm_mmu_pages {
1092 struct mmu_page_and_offset {
1093 struct kvm_mmu_page *sp;
1094 unsigned int idx;
1095 } page[KVM_PAGE_ARRAY_NR];
1096 unsigned int nr;
1097};
1098
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001099#define for_each_unsync_children(bitmap, idx) \
1100 for (idx = find_first_bit(bitmap, 512); \
1101 idx < 512; \
1102 idx = find_next_bit(bitmap, 512, idx+1))
1103
Hannes Edercded19f2009-02-21 02:19:13 +01001104static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1105 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001106{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001107 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001108
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001109 if (sp->unsync)
1110 for (i=0; i < pvec->nr; i++)
1111 if (pvec->page[i].sp == sp)
1112 return 0;
1113
1114 pvec->page[pvec->nr].sp = sp;
1115 pvec->page[pvec->nr].idx = idx;
1116 pvec->nr++;
1117 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1118}
1119
1120static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1121 struct kvm_mmu_pages *pvec)
1122{
1123 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001124
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001125 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001126 u64 ent = sp->spt[i];
1127
Marcelo Tosatti87917232008-12-22 18:49:30 -02001128 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001129 struct kvm_mmu_page *child;
1130 child = page_header(ent & PT64_BASE_ADDR_MASK);
1131
1132 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001133 if (mmu_pages_add(pvec, child, i))
1134 return -ENOSPC;
1135
1136 ret = __mmu_unsync_walk(child, pvec);
1137 if (!ret)
1138 __clear_bit(i, sp->unsync_child_bitmap);
1139 else if (ret > 0)
1140 nr_unsync_leaf += ret;
1141 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001142 return ret;
1143 }
1144
1145 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001146 nr_unsync_leaf++;
1147 if (mmu_pages_add(pvec, child, i))
1148 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001149 }
1150 }
1151 }
1152
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001153 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001154 sp->unsync_children = 0;
1155
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001156 return nr_unsync_leaf;
1157}
1158
1159static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1160 struct kvm_mmu_pages *pvec)
1161{
1162 if (!sp->unsync_children)
1163 return 0;
1164
1165 mmu_pages_add(pvec, sp, 0);
1166 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001167}
1168
Avi Kivity4db35312007-11-21 15:28:32 +02001169static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001170{
1171 unsigned index;
1172 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001173 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001174 struct hlist_node *node;
1175
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001176 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001177 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001178 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001179 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001180 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001181 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001182 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001183 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001184 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001185 }
1186 return NULL;
1187}
1188
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001189static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1190{
1191 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001192 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193 sp->unsync = 0;
1194 --kvm->stat.mmu_unsync;
1195}
1196
1197static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1198
1199static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1200{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001201 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001202 kvm_mmu_zap_page(vcpu->kvm, sp);
1203 return 1;
1204 }
1205
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001206 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1207 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001208 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1210 kvm_mmu_zap_page(vcpu->kvm, sp);
1211 return 1;
1212 }
1213
1214 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215 return 0;
1216}
1217
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001218struct mmu_page_path {
1219 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1220 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221};
1222
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001223#define for_each_sp(pvec, sp, parents, i) \
1224 for (i = mmu_pages_next(&pvec, &parents, -1), \
1225 sp = pvec.page[i].sp; \
1226 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1227 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001228
Hannes Edercded19f2009-02-21 02:19:13 +01001229static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1230 struct mmu_page_path *parents,
1231 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001232{
1233 int n;
1234
1235 for (n = i+1; n < pvec->nr; n++) {
1236 struct kvm_mmu_page *sp = pvec->page[n].sp;
1237
1238 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1239 parents->idx[0] = pvec->page[n].idx;
1240 return n;
1241 }
1242
1243 parents->parent[sp->role.level-2] = sp;
1244 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1245 }
1246
1247 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248}
1249
Hannes Edercded19f2009-02-21 02:19:13 +01001250static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001251{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001252 struct kvm_mmu_page *sp;
1253 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001254
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001255 do {
1256 unsigned int idx = parents->idx[level];
1257
1258 sp = parents->parent[level];
1259 if (!sp)
1260 return;
1261
1262 --sp->unsync_children;
1263 WARN_ON((int)sp->unsync_children < 0);
1264 __clear_bit(idx, sp->unsync_child_bitmap);
1265 level++;
1266 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1267}
1268
1269static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1270 struct mmu_page_path *parents,
1271 struct kvm_mmu_pages *pvec)
1272{
1273 parents->parent[parent->role.level-1] = NULL;
1274 pvec->nr = 0;
1275}
1276
1277static void mmu_sync_children(struct kvm_vcpu *vcpu,
1278 struct kvm_mmu_page *parent)
1279{
1280 int i;
1281 struct kvm_mmu_page *sp;
1282 struct mmu_page_path parents;
1283 struct kvm_mmu_pages pages;
1284
1285 kvm_mmu_pages_init(parent, &parents, &pages);
1286 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001287 int protected = 0;
1288
1289 for_each_sp(pages, sp, parents, i)
1290 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1291
1292 if (protected)
1293 kvm_flush_remote_tlbs(vcpu->kvm);
1294
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001295 for_each_sp(pages, sp, parents, i) {
1296 kvm_sync_page(vcpu, sp);
1297 mmu_pages_clear_parents(&parents);
1298 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001299 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001300 kvm_mmu_pages_init(parent, &parents, &pages);
1301 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001302}
1303
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001304static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1305 gfn_t gfn,
1306 gva_t gaddr,
1307 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001308 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001309 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001310 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001311{
1312 union kvm_mmu_page_role role;
1313 unsigned index;
1314 unsigned quadrant;
1315 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001316 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001317 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001318
Avi Kivitya770f6f2008-12-21 19:20:09 +02001319 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001320 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001321 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001322 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001323 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001324 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001325 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001326 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1327 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1328 role.quadrant = quadrant;
1329 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001330 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001331 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001332 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1333 if (sp->gfn == gfn) {
1334 if (sp->unsync)
1335 if (kvm_sync_page(vcpu, sp))
1336 continue;
1337
1338 if (sp->role.word != role.word)
1339 continue;
1340
Avi Kivity4db35312007-11-21 15:28:32 +02001341 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001342 if (sp->unsync_children) {
1343 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001344 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001345 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001346 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001347 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001348 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001349 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001350 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1351 if (!sp)
1352 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001353 sp->gfn = gfn;
1354 sp->role = role;
1355 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001356 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001357 if (rmap_write_protect(vcpu->kvm, gfn))
1358 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001359 account_shadowed(vcpu->kvm, gfn);
1360 }
Avi Kivity131d8272008-05-29 14:56:28 +03001361 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1362 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1363 else
1364 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001365 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001366 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001367}
1368
Avi Kivity2d111232008-12-25 14:39:47 +02001369static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1370 struct kvm_vcpu *vcpu, u64 addr)
1371{
1372 iterator->addr = addr;
1373 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1374 iterator->level = vcpu->arch.mmu.shadow_root_level;
1375 if (iterator->level == PT32E_ROOT_LEVEL) {
1376 iterator->shadow_addr
1377 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1378 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1379 --iterator->level;
1380 if (!iterator->shadow_addr)
1381 iterator->level = 0;
1382 }
1383}
1384
1385static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1386{
1387 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1388 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001389
1390 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1391 if (is_large_pte(*iterator->sptep))
1392 return false;
1393
Avi Kivity2d111232008-12-25 14:39:47 +02001394 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1395 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1396 return true;
1397}
1398
1399static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1400{
1401 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1402 --iterator->level;
1403}
1404
Avi Kivity90cb0522007-07-17 13:04:56 +03001405static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001406 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001407{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001408 unsigned i;
1409 u64 *pt;
1410 u64 ent;
1411
Avi Kivity4db35312007-11-21 15:28:32 +02001412 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001413
Avi Kivity697fe2e2007-01-05 16:36:46 -08001414 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1415 ent = pt[i];
1416
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001417 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001418 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001419 ent &= PT64_BASE_ADDR_MASK;
1420 mmu_page_remove_parent_pte(page_header(ent),
1421 &pt[i]);
1422 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001423 if (is_large_pte(ent))
1424 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001425 rmap_remove(kvm, &pt[i]);
1426 }
1427 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001428 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001429 }
Avi Kivitya4360362007-01-05 16:36:45 -08001430}
1431
Avi Kivity4db35312007-11-21 15:28:32 +02001432static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001433{
Avi Kivity4db35312007-11-21 15:28:32 +02001434 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001435}
1436
Avi Kivity12b7d282007-09-23 14:10:49 +02001437static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1438{
1439 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001440 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001441
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001442 kvm_for_each_vcpu(i, vcpu, kvm)
1443 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001444}
1445
Avi Kivity31aa2b42008-07-11 17:59:46 +03001446static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001447{
1448 u64 *parent_pte;
1449
Avi Kivity4db35312007-11-21 15:28:32 +02001450 while (sp->multimapped || sp->parent_pte) {
1451 if (!sp->multimapped)
1452 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001453 else {
1454 struct kvm_pte_chain *chain;
1455
Avi Kivity4db35312007-11-21 15:28:32 +02001456 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001457 struct kvm_pte_chain, link);
1458 parent_pte = chain->parent_ptes[0];
1459 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001460 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001461 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001462 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001463 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001464}
1465
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001466static int mmu_zap_unsync_children(struct kvm *kvm,
1467 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001468{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001469 int i, zapped = 0;
1470 struct mmu_page_path parents;
1471 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001472
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001473 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001474 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001475
1476 kvm_mmu_pages_init(parent, &parents, &pages);
1477 while (mmu_unsync_walk(parent, &pages)) {
1478 struct kvm_mmu_page *sp;
1479
1480 for_each_sp(pages, sp, parents, i) {
1481 kvm_mmu_zap_page(kvm, sp);
1482 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001483 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001484 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001485 kvm_mmu_pages_init(parent, &parents, &pages);
1486 }
1487
1488 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001489}
1490
Marcelo Tosatti07385412008-09-23 13:18:37 -03001491static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001492{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001493 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001494
1495 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001496 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001497 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001498 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001499 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001500 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001501 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001502 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001503 if (sp->unsync)
1504 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001505 if (!sp->root_count) {
1506 hlist_del(&sp->hash_link);
1507 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001508 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001509 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001510 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001511 kvm_reload_remote_mmus(kvm);
1512 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001513 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001514 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001515}
1516
Izik Eidus82ce2c92007-10-02 18:52:55 +02001517/*
1518 * Changing the number of mmu pages allocated to the vm
1519 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1520 */
1521void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1522{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001523 int used_pages;
1524
1525 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1526 used_pages = max(0, used_pages);
1527
Izik Eidus82ce2c92007-10-02 18:52:55 +02001528 /*
1529 * If we set the number of mmu pages to be smaller be than the
1530 * number of actived pages , we must to free some mmu pages before we
1531 * change the value
1532 */
1533
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001534 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001535 while (used_pages > kvm_nr_mmu_pages &&
1536 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001537 struct kvm_mmu_page *page;
1538
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001539 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001540 struct kvm_mmu_page, link);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001541 used_pages -= kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001542 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001543 }
Xiao Guangrong77662e02010-04-16 16:34:42 +08001544 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001545 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001546 }
1547 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001548 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1549 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001550
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001551 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001552}
1553
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001554static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001555{
1556 unsigned index;
1557 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001558 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001559 struct hlist_node *node, *n;
1560 int r;
1561
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001562 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001563 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001564 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001565 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001566restart:
Avi Kivity4db35312007-11-21 15:28:32 +02001567 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001568 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001569 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001570 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001571 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001572 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001573 goto restart;
Avi Kivitya4360362007-01-05 16:36:45 -08001574 }
1575 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001576}
1577
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001578static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001579{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001580 unsigned index;
1581 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001582 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001583 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001584
Avi Kivity4677a3b2009-01-06 13:00:27 +02001585 index = kvm_page_table_hashfn(gfn);
1586 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001587restart:
Avi Kivity4677a3b2009-01-06 13:00:27 +02001588 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001589 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001590 && !sp->role.invalid) {
1591 pgprintk("%s: zap %lx %x\n",
1592 __func__, gfn, sp->role.word);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001593 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001594 goto restart;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001595 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001596 }
1597}
1598
Avi Kivity38c335f2007-11-21 14:20:22 +02001599static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001601 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001602 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603
Sheng Yang291f26b2008-10-16 17:30:57 +08001604 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605}
1606
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001607static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1608{
1609 int i;
1610 u64 *pt = sp->spt;
1611
1612 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1613 return;
1614
1615 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1616 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001617 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001618 }
1619}
1620
Sheng Yang74be52e2008-10-09 16:01:56 +08001621/*
1622 * The function is based on mtrr_type_lookup() in
1623 * arch/x86/kernel/cpu/mtrr/generic.c
1624 */
1625static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1626 u64 start, u64 end)
1627{
1628 int i;
1629 u64 base, mask;
1630 u8 prev_match, curr_match;
1631 int num_var_ranges = KVM_NR_VAR_MTRR;
1632
1633 if (!mtrr_state->enabled)
1634 return 0xFF;
1635
1636 /* Make end inclusive end, instead of exclusive */
1637 end--;
1638
1639 /* Look in fixed ranges. Just return the type as per start */
1640 if (mtrr_state->have_fixed && (start < 0x100000)) {
1641 int idx;
1642
1643 if (start < 0x80000) {
1644 idx = 0;
1645 idx += (start >> 16);
1646 return mtrr_state->fixed_ranges[idx];
1647 } else if (start < 0xC0000) {
1648 idx = 1 * 8;
1649 idx += ((start - 0x80000) >> 14);
1650 return mtrr_state->fixed_ranges[idx];
1651 } else if (start < 0x1000000) {
1652 idx = 3 * 8;
1653 idx += ((start - 0xC0000) >> 12);
1654 return mtrr_state->fixed_ranges[idx];
1655 }
1656 }
1657
1658 /*
1659 * Look in variable ranges
1660 * Look of multiple ranges matching this address and pick type
1661 * as per MTRR precedence
1662 */
1663 if (!(mtrr_state->enabled & 2))
1664 return mtrr_state->def_type;
1665
1666 prev_match = 0xFF;
1667 for (i = 0; i < num_var_ranges; ++i) {
1668 unsigned short start_state, end_state;
1669
1670 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1671 continue;
1672
1673 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1674 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1675 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1676 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1677
1678 start_state = ((start & mask) == (base & mask));
1679 end_state = ((end & mask) == (base & mask));
1680 if (start_state != end_state)
1681 return 0xFE;
1682
1683 if ((start & mask) != (base & mask))
1684 continue;
1685
1686 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1687 if (prev_match == 0xFF) {
1688 prev_match = curr_match;
1689 continue;
1690 }
1691
1692 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1693 curr_match == MTRR_TYPE_UNCACHABLE)
1694 return MTRR_TYPE_UNCACHABLE;
1695
1696 if ((prev_match == MTRR_TYPE_WRBACK &&
1697 curr_match == MTRR_TYPE_WRTHROUGH) ||
1698 (prev_match == MTRR_TYPE_WRTHROUGH &&
1699 curr_match == MTRR_TYPE_WRBACK)) {
1700 prev_match = MTRR_TYPE_WRTHROUGH;
1701 curr_match = MTRR_TYPE_WRTHROUGH;
1702 }
1703
1704 if (prev_match != curr_match)
1705 return MTRR_TYPE_UNCACHABLE;
1706 }
1707
1708 if (prev_match != 0xFF)
1709 return prev_match;
1710
1711 return mtrr_state->def_type;
1712}
1713
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001714u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001715{
1716 u8 mtrr;
1717
1718 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1719 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1720 if (mtrr == 0xfe || mtrr == 0xff)
1721 mtrr = MTRR_TYPE_WRBACK;
1722 return mtrr;
1723}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001724EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001725
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001726static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1727{
1728 unsigned index;
1729 struct hlist_head *bucket;
1730 struct kvm_mmu_page *s;
1731 struct hlist_node *node, *n;
1732
1733 index = kvm_page_table_hashfn(sp->gfn);
1734 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1735 /* don't unsync if pagetable is shadowed with multiple roles */
1736 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001737 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001738 continue;
1739 if (s->role.word != sp->role.word)
1740 return 1;
1741 }
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001742 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001743 ++vcpu->kvm->stat.mmu_unsync;
1744 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001745
Xiao Guangrong6b184932010-04-16 21:29:17 +08001746 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001747
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001748 mmu_convert_notrap(sp);
1749 return 0;
1750}
1751
1752static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1753 bool can_unsync)
1754{
1755 struct kvm_mmu_page *shadow;
1756
1757 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1758 if (shadow) {
1759 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1760 return 1;
1761 if (shadow->unsync)
1762 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001763 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001764 return kvm_unsync_page(vcpu, shadow);
1765 return 1;
1766 }
1767 return 0;
1768}
1769
Avi Kivityd555c332009-06-10 14:24:23 +03001770static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001771 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001772 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001773 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001774 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001775{
1776 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001777 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001778
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001779 /*
1780 * We don't set the accessed bit, since we sometimes want to see
1781 * whether the guest actually used the pte (in order to detect
1782 * demand paging).
1783 */
Sheng Yang7b523452008-04-25 21:13:50 +08001784 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001785 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001786 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001787 if (!dirty)
1788 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001789 if (pte_access & ACC_EXEC_MASK)
1790 spte |= shadow_x_mask;
1791 else
1792 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001793 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001794 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001795 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001796 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001797 if (tdp_enabled)
1798 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1799 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001800
Izik Eidus14032832009-09-23 21:47:17 +03001801 if (reset_host_protection)
1802 spte |= SPTE_HOST_WRITEABLE;
1803
Anthony Liguori35149e22008-04-02 14:46:56 -05001804 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001805
1806 if ((pte_access & ACC_WRITE_MASK)
1807 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001808
Joerg Roedel852e3c12009-07-27 16:30:44 +02001809 if (level > PT_PAGE_TABLE_LEVEL &&
1810 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001811 ret = 1;
1812 spte = shadow_trap_nonpresent_pte;
1813 goto set_pte;
1814 }
1815
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001816 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817
Avi Kivity69325a12010-05-27 14:35:58 +03001818 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1819 spte &= ~PT_USER_MASK;
1820
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001821 /*
1822 * Optimization: for pte sync, if spte was writable the hash
1823 * lookup is unnecessary (and expensive). Write protection
1824 * is responsibility of mmu_get_page / kvm_sync_page.
1825 * Same reasoning can be applied to dirty page accounting.
1826 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001827 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001828 goto set_pte;
1829
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001830 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001831 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001832 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001833 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001834 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001835 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001836 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001837 }
1838 }
1839
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001840 if (pte_access & ACC_WRITE_MASK)
1841 mark_page_dirty(vcpu->kvm, gfn);
1842
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001843set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001844 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001845 return ret;
1846}
1847
Avi Kivityd555c332009-06-10 14:24:23 +03001848static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001849 unsigned pt_access, unsigned pte_access,
1850 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001851 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001852 pfn_t pfn, bool speculative,
1853 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001854{
1855 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001856 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001857 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001858
1859 pgprintk("%s: spte %llx access %x write_fault %d"
1860 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001861 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001862 write_fault, user_fault, gfn);
1863
Avi Kivityd555c332009-06-10 14:24:23 +03001864 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001865 /*
1866 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1867 * the parent of the now unreachable PTE.
1868 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001869 if (level > PT_PAGE_TABLE_LEVEL &&
1870 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001871 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001872 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001873
1874 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001875 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001876 __set_spte(sptep, shadow_trap_nonpresent_pte);
1877 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001878 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001879 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001880 spte_to_pfn(*sptep), pfn);
1881 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001882 } else
1883 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001884 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001885
Avi Kivityd555c332009-06-10 14:24:23 +03001886 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001887 dirty, level, gfn, pfn, speculative, true,
1888 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001889 if (write_fault)
1890 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001891 kvm_x86_ops->tlb_flush(vcpu);
1892 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001893
Avi Kivityd555c332009-06-10 14:24:23 +03001894 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001895 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001896 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001897 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1898 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001899 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001900 ++vcpu->kvm->stat.lpages;
1901
Avi Kivityd555c332009-06-10 14:24:23 +03001902 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001903 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001904 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001905 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001906 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001907 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001908 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001909 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001910 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001911 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001912 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001913 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001914 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001915 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001916 vcpu->arch.last_pte_gfn = gfn;
1917 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001918}
1919
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1921{
1922}
1923
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001924static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001925 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926{
Avi Kivity9f652d22008-12-25 14:54:25 +02001927 struct kvm_shadow_walk_iterator iterator;
1928 struct kvm_mmu_page *sp;
1929 int pt_write = 0;
1930 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931
Avi Kivity9f652d22008-12-25 14:54:25 +02001932 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001933 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001934 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1935 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001936 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001937 ++vcpu->stat.pf_fixed;
1938 break;
1939 }
1940
1941 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1942 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1943 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1944 iterator.level - 1,
1945 1, ACC_ALL, iterator.sptep);
1946 if (!sp) {
1947 pgprintk("nonpaging_map: ENOMEM\n");
1948 kvm_release_pfn_clean(pfn);
1949 return -ENOMEM;
1950 }
1951
Avi Kivityd555c332009-06-10 14:24:23 +03001952 __set_spte(iterator.sptep,
1953 __pa(sp->spt)
1954 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1955 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001956 }
1957 }
1958 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959}
1960
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001961static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1962{
1963 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001964 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001965 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001966 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001967
Joerg Roedel852e3c12009-07-27 16:30:44 +02001968 level = mapping_level(vcpu, gfn);
1969
1970 /*
1971 * This path builds a PAE pagetable - so we can map 2mb pages at
1972 * maximum. Therefore check if the level is larger than that.
1973 */
1974 if (level > PT_DIRECTORY_LEVEL)
1975 level = PT_DIRECTORY_LEVEL;
1976
1977 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001978
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001979 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001980 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001981 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001982
Avi Kivityd196e342008-01-24 11:44:11 +02001983 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001984 if (is_error_pfn(pfn)) {
1985 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001986 return 1;
1987 }
1988
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001989 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001990 if (mmu_notifier_retry(vcpu, mmu_seq))
1991 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001992 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02001993 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001994 spin_unlock(&vcpu->kvm->mmu_lock);
1995
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001996
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001997 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001998
1999out_unlock:
2000 spin_unlock(&vcpu->kvm->mmu_lock);
2001 kvm_release_pfn_clean(pfn);
2002 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002003}
2004
2005
Avi Kivity17ac10a2007-01-05 16:36:40 -08002006static void mmu_free_roots(struct kvm_vcpu *vcpu)
2007{
2008 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002009 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002010
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002012 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002013 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002014 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2015 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002016
Avi Kivity4db35312007-11-21 15:28:32 +02002017 sp = page_header(root);
2018 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002019 if (!sp->root_count && sp->role.invalid)
2020 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002021 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002022 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002023 return;
2024 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002025 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002026 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002027
Avi Kivity417726a2007-04-12 17:35:58 +03002028 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002029 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002030 sp = page_header(root);
2031 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002032 if (!sp->root_count && sp->role.invalid)
2033 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002034 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002035 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002036 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002037 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002038 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002039}
2040
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002041static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2042{
2043 int ret = 0;
2044
2045 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2046 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2047 ret = 1;
2048 }
2049
2050 return ret;
2051}
2052
2053static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002054{
2055 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002056 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002057 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002058 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002059 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002060
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002061 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002062
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002063 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2064 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002065
2066 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002067 if (mmu_check_root(vcpu, root_gfn))
2068 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002069 if (tdp_enabled) {
2070 direct = 1;
2071 root_gfn = 0;
2072 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002073 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity4db35312007-11-21 15:28:32 +02002074 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002075 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002076 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002077 root = __pa(sp->spt);
2078 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002079 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002080 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002081 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002082 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002083 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002084 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002085 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002086
2087 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002088 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002089 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002090 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002091 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002092 continue;
2093 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002094 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002095 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002096 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002097 if (mmu_check_root(vcpu, root_gfn))
2098 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002099 if (tdp_enabled) {
2100 direct = 1;
2101 root_gfn = i << 30;
2102 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002103 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity4db35312007-11-21 15:28:32 +02002104 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002105 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002106 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002107 root = __pa(sp->spt);
2108 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002109 spin_unlock(&vcpu->kvm->mmu_lock);
2110
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002111 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002112 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002114 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002115}
2116
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002117static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2118{
2119 int i;
2120 struct kvm_mmu_page *sp;
2121
2122 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2123 return;
2124 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2125 hpa_t root = vcpu->arch.mmu.root_hpa;
2126 sp = page_header(root);
2127 mmu_sync_children(vcpu, sp);
2128 return;
2129 }
2130 for (i = 0; i < 4; ++i) {
2131 hpa_t root = vcpu->arch.mmu.pae_root[i];
2132
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002133 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002134 root &= PT64_BASE_ADDR_MASK;
2135 sp = page_header(root);
2136 mmu_sync_children(vcpu, sp);
2137 }
2138 }
2139}
2140
2141void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2142{
2143 spin_lock(&vcpu->kvm->mmu_lock);
2144 mmu_sync_roots(vcpu);
2145 spin_unlock(&vcpu->kvm->mmu_lock);
2146}
2147
Gleb Natapov1871c602010-02-10 14:21:32 +02002148static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2149 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150{
Gleb Natapov1871c602010-02-10 14:21:32 +02002151 if (error)
2152 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 return vaddr;
2154}
2155
2156static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002157 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158{
Avi Kivitye8332402007-12-09 18:43:00 +02002159 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002160 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002162 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002163 r = mmu_topup_memory_caches(vcpu);
2164 if (r)
2165 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002166
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002168 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169
Avi Kivitye8332402007-12-09 18:43:00 +02002170 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171
Avi Kivitye8332402007-12-09 18:43:00 +02002172 return nonpaging_map(vcpu, gva & PAGE_MASK,
2173 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174}
2175
Joerg Roedelfb72d162008-02-07 13:47:44 +01002176static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2177 u32 error_code)
2178{
Anthony Liguori35149e22008-04-02 14:46:56 -05002179 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002180 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002181 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002182 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002183 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002184
2185 ASSERT(vcpu);
2186 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2187
2188 r = mmu_topup_memory_caches(vcpu);
2189 if (r)
2190 return r;
2191
Joerg Roedel852e3c12009-07-27 16:30:44 +02002192 level = mapping_level(vcpu, gfn);
2193
2194 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2195
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002196 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002197 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002198 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002199 if (is_error_pfn(pfn)) {
2200 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002201 return 1;
2202 }
2203 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002204 if (mmu_notifier_retry(vcpu, mmu_seq))
2205 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002206 kvm_mmu_free_some_pages(vcpu);
2207 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002208 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002209 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002210
2211 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002212
2213out_unlock:
2214 spin_unlock(&vcpu->kvm->mmu_lock);
2215 kvm_release_pfn_clean(pfn);
2216 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002217}
2218
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219static void nonpaging_free(struct kvm_vcpu *vcpu)
2220{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002221 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222}
2223
2224static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2225{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002226 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227
2228 context->new_cr3 = nonpaging_new_cr3;
2229 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230 context->gva_to_gpa = nonpaging_gva_to_gpa;
2231 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002232 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002233 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002234 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002235 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002237 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 return 0;
2239}
2240
Avi Kivityd835dfe2007-11-21 02:57:59 +02002241void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002243 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002244 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245}
2246
2247static void paging_new_cr3(struct kvm_vcpu *vcpu)
2248{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002249 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002250 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251}
2252
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253static void inject_page_fault(struct kvm_vcpu *vcpu,
2254 u64 addr,
2255 u32 err_code)
2256{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002257 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258}
2259
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260static void paging_free(struct kvm_vcpu *vcpu)
2261{
2262 nonpaging_free(vcpu);
2263}
2264
Dong, Eddie82725b22009-03-30 16:21:08 +08002265static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2266{
2267 int bit7;
2268
2269 bit7 = (gpte >> 7) & 1;
2270 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2271}
2272
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273#define PTTYPE 64
2274#include "paging_tmpl.h"
2275#undef PTTYPE
2276
2277#define PTTYPE 32
2278#include "paging_tmpl.h"
2279#undef PTTYPE
2280
Dong, Eddie82725b22009-03-30 16:21:08 +08002281static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2282{
2283 struct kvm_mmu *context = &vcpu->arch.mmu;
2284 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2285 u64 exb_bit_rsvd = 0;
2286
2287 if (!is_nx(vcpu))
2288 exb_bit_rsvd = rsvd_bits(63, 63);
2289 switch (level) {
2290 case PT32_ROOT_LEVEL:
2291 /* no rsvd bits for 2 level 4K page table entries */
2292 context->rsvd_bits_mask[0][1] = 0;
2293 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002294 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2295
2296 if (!is_pse(vcpu)) {
2297 context->rsvd_bits_mask[1][1] = 0;
2298 break;
2299 }
2300
Dong, Eddie82725b22009-03-30 16:21:08 +08002301 if (is_cpuid_PSE36())
2302 /* 36bits PSE 4MB page */
2303 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2304 else
2305 /* 32 bits PSE 4MB page */
2306 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002307 break;
2308 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002309 context->rsvd_bits_mask[0][2] =
2310 rsvd_bits(maxphyaddr, 63) |
2311 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002312 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002313 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002314 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2315 rsvd_bits(maxphyaddr, 62); /* PTE */
2316 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2317 rsvd_bits(maxphyaddr, 62) |
2318 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002319 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002320 break;
2321 case PT64_ROOT_LEVEL:
2322 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2323 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2324 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2325 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2326 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002327 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002328 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2329 rsvd_bits(maxphyaddr, 51);
2330 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002331 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2332 rsvd_bits(maxphyaddr, 51) |
2333 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002334 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002335 rsvd_bits(maxphyaddr, 51) |
2336 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002337 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002338 break;
2339 }
2340}
2341
Avi Kivity17ac10a2007-01-05 16:36:40 -08002342static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002344 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345
2346 ASSERT(is_pae(vcpu));
2347 context->new_cr3 = paging_new_cr3;
2348 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002350 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002351 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002352 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002354 context->root_level = level;
2355 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002356 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 return 0;
2358}
2359
Avi Kivity17ac10a2007-01-05 16:36:40 -08002360static int paging64_init_context(struct kvm_vcpu *vcpu)
2361{
Dong, Eddie82725b22009-03-30 16:21:08 +08002362 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002363 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2364}
2365
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366static int paging32_init_context(struct kvm_vcpu *vcpu)
2367{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002368 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369
Dong, Eddie82725b22009-03-30 16:21:08 +08002370 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 context->new_cr3 = paging_new_cr3;
2372 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 context->gva_to_gpa = paging32_gva_to_gpa;
2374 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002375 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002376 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002377 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 context->root_level = PT32_ROOT_LEVEL;
2379 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002380 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381 return 0;
2382}
2383
2384static int paging32E_init_context(struct kvm_vcpu *vcpu)
2385{
Dong, Eddie82725b22009-03-30 16:21:08 +08002386 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002387 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388}
2389
Joerg Roedelfb72d162008-02-07 13:47:44 +01002390static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2391{
2392 struct kvm_mmu *context = &vcpu->arch.mmu;
2393
2394 context->new_cr3 = nonpaging_new_cr3;
2395 context->page_fault = tdp_page_fault;
2396 context->free = nonpaging_free;
2397 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002398 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002399 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002400 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002401 context->root_hpa = INVALID_PAGE;
2402
2403 if (!is_paging(vcpu)) {
2404 context->gva_to_gpa = nonpaging_gva_to_gpa;
2405 context->root_level = 0;
2406 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002407 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002408 context->gva_to_gpa = paging64_gva_to_gpa;
2409 context->root_level = PT64_ROOT_LEVEL;
2410 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002411 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002412 context->gva_to_gpa = paging64_gva_to_gpa;
2413 context->root_level = PT32E_ROOT_LEVEL;
2414 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002415 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002416 context->gva_to_gpa = paging32_gva_to_gpa;
2417 context->root_level = PT32_ROOT_LEVEL;
2418 }
2419
2420 return 0;
2421}
2422
2423static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002425 int r;
2426
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429
2430 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002431 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002432 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002433 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002435 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002437 r = paging32_init_context(vcpu);
2438
Avi Kivity5b7e0102010-04-14 19:20:03 +03002439 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002440 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002441
2442 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
Joerg Roedelfb72d162008-02-07 13:47:44 +01002445static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2446{
Anthony Liguori35149e22008-04-02 14:46:56 -05002447 vcpu->arch.update_pte.pfn = bad_pfn;
2448
Joerg Roedelfb72d162008-02-07 13:47:44 +01002449 if (tdp_enabled)
2450 return init_kvm_tdp_mmu(vcpu);
2451 else
2452 return init_kvm_softmmu(vcpu);
2453}
2454
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2456{
2457 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002458 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2459 vcpu->arch.mmu.free(vcpu);
2460 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 }
2462}
2463
2464int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2465{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002466 destroy_kvm_mmu(vcpu);
2467 return init_kvm_mmu(vcpu);
2468}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002469EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002470
2471int kvm_mmu_load(struct kvm_vcpu *vcpu)
2472{
Avi Kivity714b93d2007-01-05 16:36:53 -08002473 int r;
2474
Avi Kivitye2dec932007-01-05 16:36:54 -08002475 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002476 if (r)
2477 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002478 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002479 kvm_mmu_free_some_pages(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002480 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002481 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002482 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002483 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002484 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002485 if (r)
2486 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002487 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002488 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002489out:
2490 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002492EXPORT_SYMBOL_GPL(kvm_mmu_load);
2493
2494void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2495{
2496 mmu_free_roots(vcpu);
2497}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498
Avi Kivity09072da2007-05-01 14:16:52 +03002499static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002500 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002501 u64 *spte)
2502{
2503 u64 pte;
2504 struct kvm_mmu_page *child;
2505
2506 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002507 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002508 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002509 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002510 else {
2511 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002512 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002513 }
2514 }
Avi Kivityd555c332009-06-10 14:24:23 +03002515 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002516 if (is_large_pte(pte))
2517 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002518}
2519
Avi Kivity00284252007-05-01 16:53:31 +03002520static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002521 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002522 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002523 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002524{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002525 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002526 ++vcpu->kvm->stat.mmu_pde_zapped;
2527 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002528 }
Avi Kivity00284252007-05-01 16:53:31 +03002529
Avi Kivity4cee5762007-11-18 16:37:07 +02002530 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002531 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002532 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002533 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002534 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002535}
2536
Avi Kivity79539ce2007-11-21 02:06:21 +02002537static bool need_remote_flush(u64 old, u64 new)
2538{
2539 if (!is_shadow_present_pte(old))
2540 return false;
2541 if (!is_shadow_present_pte(new))
2542 return true;
2543 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2544 return true;
2545 old ^= PT64_NX_MASK;
2546 new ^= PT64_NX_MASK;
2547 return (old & ~new & PT64_PERM_MASK) != 0;
2548}
2549
2550static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2551{
2552 if (need_remote_flush(old, new))
2553 kvm_flush_remote_tlbs(vcpu->kvm);
2554 else
2555 kvm_mmu_flush_tlb(vcpu);
2556}
2557
Avi Kivity12b7d282007-09-23 14:10:49 +02002558static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2559{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002560 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002561
Sheng Yang7b523452008-04-25 21:13:50 +08002562 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002563}
2564
Avi Kivityd7824ff2007-12-30 12:29:05 +02002565static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002566 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002567{
2568 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002569 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002570
Avi Kivity43a37952009-06-10 14:12:05 +03002571 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002572 return;
2573 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002574
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002575 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002576 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002577 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002578
Anthony Liguori35149e22008-04-02 14:46:56 -05002579 if (is_error_pfn(pfn)) {
2580 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002581 return;
2582 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002583 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002584 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002585}
2586
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002587static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2588{
2589 u64 *spte = vcpu->arch.last_pte_updated;
2590
2591 if (spte
2592 && vcpu->arch.last_pte_gfn == gfn
2593 && shadow_accessed_mask
2594 && !(*spte & shadow_accessed_mask)
2595 && is_shadow_present_pte(*spte))
2596 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2597}
2598
Avi Kivity09072da2007-05-01 14:16:52 +03002599void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002600 const u8 *new, int bytes,
2601 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002602{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002603 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002604 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002605 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002606 struct hlist_head *bucket;
2607 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002608 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002609 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002610 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002611 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002612 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002613 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002614 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002615 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002616 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002617 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002618 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002619 int invlpg_counter;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002620
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002621 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002622
Avi Kivity08e850c2010-03-15 13:59:57 +02002623 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2624
2625 /*
2626 * Assume that the pte write on a page table of the same type
2627 * as the current vcpu paging mode. This is nearly always true
2628 * (might be false while changing modes). Note it is verified later
2629 * by update_pte().
2630 */
2631 if ((is_pae(vcpu) && bytes == 4) || !new) {
2632 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2633 if (is_pae(vcpu)) {
2634 gpa &= ~(gpa_t)7;
2635 bytes = 8;
2636 }
2637 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2638 if (r)
2639 gentry = 0;
2640 new = (const u8 *)&gentry;
2641 }
2642
Avi Kivity72016f32010-03-15 13:59:53 +02002643 switch (bytes) {
2644 case 4:
2645 gentry = *(const u32 *)new;
2646 break;
2647 case 8:
2648 gentry = *(const u64 *)new;
2649 break;
2650 default:
2651 gentry = 0;
2652 break;
2653 }
2654
Avi Kivity72016f32010-03-15 13:59:53 +02002655 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002656 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002657 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2658 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002659 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002660 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002661 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002662 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002663 if (guest_initiated) {
2664 if (gfn == vcpu->arch.last_pt_write_gfn
2665 && !last_updated_pte_accessed(vcpu)) {
2666 ++vcpu->arch.last_pt_write_count;
2667 if (vcpu->arch.last_pt_write_count >= 3)
2668 flooded = 1;
2669 } else {
2670 vcpu->arch.last_pt_write_gfn = gfn;
2671 vcpu->arch.last_pt_write_count = 1;
2672 vcpu->arch.last_pte_updated = NULL;
2673 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002674 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002675 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002676 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08002677
2678restart:
Avi Kivity4db35312007-11-21 15:28:32 +02002679 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002680 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002681 continue;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002682 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002683 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002684 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002685 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002686 /*
2687 * Misaligned accesses are too much trouble to fix
2688 * up; also, they usually indicate a page is not used
2689 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002690 *
2691 * If we're seeing too many writes to a page,
2692 * it may no longer be a page table, or we may be
2693 * forking, in which case it is better to unmap the
2694 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002695 */
2696 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002697 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002698 if (kvm_mmu_zap_page(vcpu->kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002699 goto restart;
Avi Kivity4cee5762007-11-18 16:37:07 +02002700 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002701 continue;
2702 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002703 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002704 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002705 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002706 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002707 page_offset <<= 1; /* 32->64 */
2708 /*
2709 * A 32-bit pde maps 4MB while the shadow pdes map
2710 * only 2MB. So we need to double the offset again
2711 * and zap two pdes instead of one.
2712 */
2713 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002714 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002715 page_offset <<= 1;
2716 npte = 2;
2717 }
Avi Kivityfce06572007-05-01 16:44:05 +03002718 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002719 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002720 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002721 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002722 }
Avi Kivity4db35312007-11-21 15:28:32 +02002723 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002724 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002725 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002726 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002727 if (gentry)
2728 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Avi Kivity79539ce2007-11-21 02:06:21 +02002729 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002730 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002731 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002732 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002733 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002734 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002735 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2736 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2737 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002738 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002739}
2740
Avi Kivitya4360362007-01-05 16:36:45 -08002741int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2742{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002743 gpa_t gpa;
2744 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002745
Avi Kivity60f24782009-08-27 13:37:06 +03002746 if (tdp_enabled)
2747 return 0;
2748
Gleb Natapov1871c602010-02-10 14:21:32 +02002749 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002750
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002751 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002752 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002753 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002754 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002755}
Avi Kivity577bdc42008-07-19 08:57:05 +03002756EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002757
Avi Kivity22d95b12007-09-14 20:26:06 +03002758void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002759{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002760 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2761 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002762 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002763
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002764 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002765 struct kvm_mmu_page, link);
2766 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002767 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002768 }
2769}
Avi Kivityebeace82007-01-05 16:36:47 -08002770
Avi Kivity30677142007-10-28 18:48:59 +02002771int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2772{
2773 int r;
2774 enum emulation_result er;
2775
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002776 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002777 if (r < 0)
2778 goto out;
2779
2780 if (!r) {
2781 r = 1;
2782 goto out;
2783 }
2784
Avi Kivityb733bfb2007-10-28 18:52:05 +02002785 r = mmu_topup_memory_caches(vcpu);
2786 if (r)
2787 goto out;
2788
Avi Kivity851ba692009-08-24 11:10:17 +03002789 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002790
2791 switch (er) {
2792 case EMULATE_DONE:
2793 return 1;
2794 case EMULATE_DO_MMIO:
2795 ++vcpu->stat.mmio_exits;
2796 return 0;
2797 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002798 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2799 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002800 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002801 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002802 default:
2803 BUG();
2804 }
2805out:
Avi Kivity30677142007-10-28 18:48:59 +02002806 return r;
2807}
2808EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2809
Marcelo Tosattia7052892008-09-23 13:18:35 -03002810void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2811{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002812 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002813 kvm_mmu_flush_tlb(vcpu);
2814 ++vcpu->stat.invlpg;
2815}
2816EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2817
Joerg Roedel18552672008-02-07 13:47:41 +01002818void kvm_enable_tdp(void)
2819{
2820 tdp_enabled = true;
2821}
2822EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2823
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002824void kvm_disable_tdp(void)
2825{
2826 tdp_enabled = false;
2827}
2828EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2829
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830static void free_mmu_pages(struct kvm_vcpu *vcpu)
2831{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002832 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833}
2834
2835static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2836{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002837 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 int i;
2839
2840 ASSERT(vcpu);
2841
Avi Kivity17ac10a2007-01-05 16:36:40 -08002842 /*
2843 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2844 * Therefore we need to allocate shadow page tables in the first
2845 * 4GB of memory, which happens to fit the DMA32 zone.
2846 */
2847 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2848 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002849 return -ENOMEM;
2850
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002851 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002852 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002853 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002854
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856}
2857
Ingo Molnar8018c272006-12-29 16:50:01 -08002858int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002861 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862
Ingo Molnar8018c272006-12-29 16:50:01 -08002863 return alloc_mmu_pages(vcpu);
2864}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865
Ingo Molnar8018c272006-12-29 16:50:01 -08002866int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2867{
2868 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002869 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002870
Ingo Molnar8018c272006-12-29 16:50:01 -08002871 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872}
2873
2874void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2875{
2876 ASSERT(vcpu);
2877
2878 destroy_kvm_mmu(vcpu);
2879 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002880 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881}
2882
Avi Kivity90cb0522007-07-17 13:04:56 +03002883void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884{
Avi Kivity4db35312007-11-21 15:28:32 +02002885 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002887 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 int i;
2889 u64 *pt;
2890
Sheng Yang291f26b2008-10-16 17:30:57 +08002891 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 continue;
2893
Avi Kivity4db35312007-11-21 15:28:32 +02002894 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2896 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002897 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 }
Avi Kivity171d5952008-08-27 16:40:51 +03002900 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002902
Avi Kivity90cb0522007-07-17 13:04:56 +03002903void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002904{
Avi Kivity4db35312007-11-21 15:28:32 +02002905 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002906
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002907 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08002908restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002909 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002910 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002911 goto restart;
2912
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002913 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002914
Avi Kivity90cb0522007-07-17 13:04:56 +03002915 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002916}
2917
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002918static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002919{
2920 struct kvm_mmu_page *page;
2921
2922 page = container_of(kvm->arch.active_mmu_pages.prev,
2923 struct kvm_mmu_page, link);
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002924 return kvm_mmu_zap_page(kvm, page) + 1;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002925}
2926
2927static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2928{
2929 struct kvm *kvm;
2930 struct kvm *kvm_freed = NULL;
2931 int cache_count = 0;
2932
2933 spin_lock(&kvm_lock);
2934
2935 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002936 int npages, idx, freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002937
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002938 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002939 spin_lock(&kvm->mmu_lock);
2940 npages = kvm->arch.n_alloc_mmu_pages -
2941 kvm->arch.n_free_mmu_pages;
2942 cache_count += npages;
2943 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002944 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm);
2945 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002946 kvm_freed = kvm;
2947 }
2948 nr_to_scan--;
2949
2950 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002951 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002952 }
2953 if (kvm_freed)
2954 list_move_tail(&kvm_freed->vm_list, &vm_list);
2955
2956 spin_unlock(&kvm_lock);
2957
2958 return cache_count;
2959}
2960
2961static struct shrinker mmu_shrinker = {
2962 .shrink = mmu_shrink,
2963 .seeks = DEFAULT_SEEKS * 10,
2964};
2965
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002966static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002967{
2968 if (pte_chain_cache)
2969 kmem_cache_destroy(pte_chain_cache);
2970 if (rmap_desc_cache)
2971 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002972 if (mmu_page_header_cache)
2973 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002974}
2975
Izik Eidus3ee16c82008-03-30 15:17:21 +03002976void kvm_mmu_module_exit(void)
2977{
2978 mmu_destroy_caches();
2979 unregister_shrinker(&mmu_shrinker);
2980}
2981
Avi Kivityb5a33a72007-04-15 16:31:09 +03002982int kvm_mmu_module_init(void)
2983{
2984 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2985 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002986 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002987 if (!pte_chain_cache)
2988 goto nomem;
2989 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2990 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002991 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002992 if (!rmap_desc_cache)
2993 goto nomem;
2994
Avi Kivityd3d25b02007-05-30 12:34:53 +03002995 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2996 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002997 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002998 if (!mmu_page_header_cache)
2999 goto nomem;
3000
Izik Eidus3ee16c82008-03-30 15:17:21 +03003001 register_shrinker(&mmu_shrinker);
3002
Avi Kivityb5a33a72007-04-15 16:31:09 +03003003 return 0;
3004
3005nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003006 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003007 return -ENOMEM;
3008}
3009
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003010/*
3011 * Caculate mmu pages needed for kvm.
3012 */
3013unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3014{
3015 int i;
3016 unsigned int nr_mmu_pages;
3017 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003018 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003019
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003020 slots = kvm_memslots(kvm);
3021
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003022 for (i = 0; i < slots->nmemslots; i++)
3023 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003024
3025 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3026 nr_mmu_pages = max(nr_mmu_pages,
3027 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3028
3029 return nr_mmu_pages;
3030}
3031
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003032static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3033 unsigned len)
3034{
3035 if (len > buffer->len)
3036 return NULL;
3037 return buffer->ptr;
3038}
3039
3040static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3041 unsigned len)
3042{
3043 void *ret;
3044
3045 ret = pv_mmu_peek_buffer(buffer, len);
3046 if (!ret)
3047 return ret;
3048 buffer->ptr += len;
3049 buffer->len -= len;
3050 buffer->processed += len;
3051 return ret;
3052}
3053
3054static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3055 gpa_t addr, gpa_t value)
3056{
3057 int bytes = 8;
3058 int r;
3059
3060 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3061 bytes = 4;
3062
3063 r = mmu_topup_memory_caches(vcpu);
3064 if (r)
3065 return r;
3066
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003067 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003068 return -EFAULT;
3069
3070 return 1;
3071}
3072
3073static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3074{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003075 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003076 return 1;
3077}
3078
3079static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3080{
3081 spin_lock(&vcpu->kvm->mmu_lock);
3082 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3083 spin_unlock(&vcpu->kvm->mmu_lock);
3084 return 1;
3085}
3086
3087static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3088 struct kvm_pv_mmu_op_buffer *buffer)
3089{
3090 struct kvm_mmu_op_header *header;
3091
3092 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3093 if (!header)
3094 return 0;
3095 switch (header->op) {
3096 case KVM_MMU_OP_WRITE_PTE: {
3097 struct kvm_mmu_op_write_pte *wpte;
3098
3099 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3100 if (!wpte)
3101 return 0;
3102 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3103 wpte->pte_val);
3104 }
3105 case KVM_MMU_OP_FLUSH_TLB: {
3106 struct kvm_mmu_op_flush_tlb *ftlb;
3107
3108 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3109 if (!ftlb)
3110 return 0;
3111 return kvm_pv_mmu_flush_tlb(vcpu);
3112 }
3113 case KVM_MMU_OP_RELEASE_PT: {
3114 struct kvm_mmu_op_release_pt *rpt;
3115
3116 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3117 if (!rpt)
3118 return 0;
3119 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3120 }
3121 default: return 0;
3122 }
3123}
3124
3125int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3126 gpa_t addr, unsigned long *ret)
3127{
3128 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003129 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003130
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003131 buffer->ptr = buffer->buf;
3132 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3133 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003134
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003135 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003136 if (r)
3137 goto out;
3138
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003139 while (buffer->len) {
3140 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003141 if (r < 0)
3142 goto out;
3143 if (r == 0)
3144 break;
3145 }
3146
3147 r = 1;
3148out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003149 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003150 return r;
3151}
3152
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003153int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3154{
3155 struct kvm_shadow_walk_iterator iterator;
3156 int nr_sptes = 0;
3157
3158 spin_lock(&vcpu->kvm->mmu_lock);
3159 for_each_shadow_entry(vcpu, addr, iterator) {
3160 sptes[iterator.level-1] = *iterator.sptep;
3161 nr_sptes++;
3162 if (!is_shadow_present_pte(*iterator.sptep))
3163 break;
3164 }
3165 spin_unlock(&vcpu->kvm->mmu_lock);
3166
3167 return nr_sptes;
3168}
3169EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3170
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003171#ifdef AUDIT
3172
3173static const char *audit_msg;
3174
3175static gva_t canonicalize(gva_t gva)
3176{
3177#ifdef CONFIG_X86_64
3178 gva = (long long)(gva << 16) >> 16;
3179#endif
3180 return gva;
3181}
3182
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003183
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003184typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003185
3186static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3187 inspect_spte_fn fn)
3188{
3189 int i;
3190
3191 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3192 u64 ent = sp->spt[i];
3193
3194 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003195 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003196 struct kvm_mmu_page *child;
3197 child = page_header(ent & PT64_BASE_ADDR_MASK);
3198 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003199 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003200 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003201 }
3202 }
3203}
3204
3205static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3206{
3207 int i;
3208 struct kvm_mmu_page *sp;
3209
3210 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3211 return;
3212 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3213 hpa_t root = vcpu->arch.mmu.root_hpa;
3214 sp = page_header(root);
3215 __mmu_spte_walk(vcpu->kvm, sp, fn);
3216 return;
3217 }
3218 for (i = 0; i < 4; ++i) {
3219 hpa_t root = vcpu->arch.mmu.pae_root[i];
3220
3221 if (root && VALID_PAGE(root)) {
3222 root &= PT64_BASE_ADDR_MASK;
3223 sp = page_header(root);
3224 __mmu_spte_walk(vcpu->kvm, sp, fn);
3225 }
3226 }
3227 return;
3228}
3229
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003230static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3231 gva_t va, int level)
3232{
3233 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3234 int i;
3235 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3236
3237 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3238 u64 ent = pt[i];
3239
Avi Kivityc7addb92007-09-16 18:58:32 +02003240 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003241 continue;
3242
3243 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003244 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3245 audit_mappings_page(vcpu, ent, va, level - 1);
3246 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003247 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003248 gfn_t gfn = gpa >> PAGE_SHIFT;
3249 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3250 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003251
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003252 if (is_error_pfn(pfn)) {
3253 kvm_release_pfn_clean(pfn);
3254 continue;
3255 }
3256
Avi Kivityc7addb92007-09-16 18:58:32 +02003257 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003258 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003259 printk(KERN_ERR "xx audit error: (%s) levels %d"
3260 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003261 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003262 va, gpa, hpa, ent,
3263 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003264 else if (ent == shadow_notrap_nonpresent_pte
3265 && !is_error_hpa(hpa))
3266 printk(KERN_ERR "audit: (%s) notrap shadow,"
3267 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003268 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003269
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003270 }
3271 }
3272}
3273
3274static void audit_mappings(struct kvm_vcpu *vcpu)
3275{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003276 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003277
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003278 if (vcpu->arch.mmu.root_level == 4)
3279 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003280 else
3281 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003282 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003283 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003284 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003285 i << 30,
3286 2);
3287}
3288
3289static int count_rmaps(struct kvm_vcpu *vcpu)
3290{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003291 struct kvm *kvm = vcpu->kvm;
3292 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003293 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003294 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003295
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003296 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003297 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003298 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003299 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003300 struct kvm_rmap_desc *d;
3301
3302 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003303 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003304
Izik Eidus290fc382007-09-27 14:11:22 +02003305 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003306 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003307 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003308 ++nmaps;
3309 continue;
3310 }
Izik Eidus290fc382007-09-27 14:11:22 +02003311 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003312 while (d) {
3313 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003314 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315 ++nmaps;
3316 else
3317 break;
3318 d = d->more;
3319 }
3320 }
3321 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003322 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003323 return nmaps;
3324}
3325
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003326void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003327{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003328 unsigned long *rmapp;
3329 struct kvm_mmu_page *rev_sp;
3330 gfn_t gfn;
3331
3332 if (*sptep & PT_WRITABLE_MASK) {
3333 rev_sp = page_header(__pa(sptep));
3334 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3335
3336 if (!gfn_to_memslot(kvm, gfn)) {
3337 if (!printk_ratelimit())
3338 return;
3339 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3340 audit_msg, gfn);
3341 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003342 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003343 rev_sp->gfn);
3344 dump_stack();
3345 return;
3346 }
3347
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003348 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003349 rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003350 if (!*rmapp) {
3351 if (!printk_ratelimit())
3352 return;
3353 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3354 audit_msg, *sptep);
3355 dump_stack();
3356 }
3357 }
3358
3359}
3360
3361void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3362{
3363 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3364}
3365
3366static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3367{
Avi Kivity4db35312007-11-21 15:28:32 +02003368 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369 int i;
3370
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003371 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003372 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003373
Avi Kivity4db35312007-11-21 15:28:32 +02003374 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003375 continue;
3376
3377 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3378 u64 ent = pt[i];
3379
3380 if (!(ent & PT_PRESENT_MASK))
3381 continue;
3382 if (!(ent & PT_WRITABLE_MASK))
3383 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003384 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003385 }
3386 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003387 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003388}
3389
3390static void audit_rmap(struct kvm_vcpu *vcpu)
3391{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003392 check_writable_mappings_rmap(vcpu);
3393 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003394}
3395
3396static void audit_write_protection(struct kvm_vcpu *vcpu)
3397{
Avi Kivity4db35312007-11-21 15:28:32 +02003398 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003399 struct kvm_memory_slot *slot;
3400 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003401 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003402 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003403
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003404 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003405 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003406 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003407 if (sp->unsync)
3408 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003409
Avi Kivity4db35312007-11-21 15:28:32 +02003410 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003411 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003412 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003413
3414 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3415 while (spte) {
3416 if (*spte & PT_WRITABLE_MASK)
3417 printk(KERN_ERR "%s: (%s) shadow page has "
3418 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003419 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003420 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003421 spte = rmap_next(vcpu->kvm, rmapp, spte);
3422 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003423 }
3424}
3425
3426static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3427{
3428 int olddbg = dbg;
3429
3430 dbg = 0;
3431 audit_msg = msg;
3432 audit_rmap(vcpu);
3433 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003434 if (strcmp("pre pte write", audit_msg) != 0)
3435 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003436 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003437 dbg = olddbg;
3438}
3439
3440#endif