blob: b0037a77e56bf2870b077404fa17f4b5126e3dd5 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
Avi Kivity221d0592010-05-23 18:37:00 +030010 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020022#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030023#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/highmem.h>
30#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020031#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030032#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050033#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020034#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080036#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
38#include <asm/page.h>
39#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020040#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020041#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040042
Joerg Roedel18552672008-02-07 13:47:41 +010043/*
44 * When setting this variable to true it enables Two-Dimensional-Paging
45 * where the hardware walks 2 page tables:
46 * 1. the guest-virtual to guest-physical
47 * 2. while doing 1. it walks guest-physical to host-physical
48 * If the hardware supports that we don't need to do shadow paging.
49 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050050bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010051
Avi Kivity37a7d8b2007-01-05 16:36:56 -080052#undef MMU_DEBUG
53
54#undef AUDIT
55
56#ifdef AUDIT
57static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
58#else
59static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
60#endif
61
62#ifdef MMU_DEBUG
63
64#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
65#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
66
67#else
68
69#define pgprintk(x...) do { } while (0)
70#define rmap_printk(x...) do { } while (0)
71
72#endif
73
74#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030075static int dbg = 0;
76module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080077#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Marcelo Tosatti582801a2008-09-23 13:18:41 -030079static int oos_shadow = 1;
80module_param(oos_shadow, bool, 0644);
81
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080082#ifndef MMU_DEBUG
83#define ASSERT(x) do { } while (0)
84#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080085#define ASSERT(x) \
86 if (!(x)) { \
87 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
88 __FILE__, __LINE__, #x); \
89 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080090#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
Xiao Guangrong957ed9e2010-08-22 19:12:48 +080092#define PTE_PREFETCH_NUM 8
93
Avi Kivity6aa8b732006-12-10 02:21:36 -080094#define PT_FIRST_AVAIL_BITS_SHIFT 9
95#define PT64_SECOND_AVAIL_BITS_SHIFT 52
96
Avi Kivity6aa8b732006-12-10 02:21:36 -080097#define PT64_LEVEL_BITS 9
98
99#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400100 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
102#define PT64_LEVEL_MASK(level) \
103 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
104
105#define PT64_INDEX(address, level)\
106 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
107
108
109#define PT32_LEVEL_BITS 10
110
111#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400112 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
114#define PT32_LEVEL_MASK(level) \
115 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200116#define PT32_LVL_OFFSET_MASK(level) \
117 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
118 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
120#define PT32_INDEX(address, level)\
121 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
122
123
Avi Kivity27aba762007-03-09 13:04:31 +0200124#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125#define PT64_DIR_BASE_ADDR_MASK \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200127#define PT64_LVL_ADDR_MASK(level) \
128 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
129 * PT64_LEVEL_BITS))) - 1))
130#define PT64_LVL_OFFSET_MASK(level) \
131 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
132 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133
134#define PT32_BASE_ADDR_MASK PAGE_MASK
135#define PT32_DIR_BASE_ADDR_MASK \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200137#define PT32_LVL_ADDR_MASK(level) \
138 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
Avi Kivity79539ce2007-11-21 02:06:21 +0200141#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
142 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800144#define RMAP_EXT 4
145
Avi Kivityfe135d22007-12-09 16:15:46 +0200146#define ACC_EXEC_MASK 1
147#define ACC_WRITE_MASK PT_WRITABLE_MASK
148#define ACC_USER_MASK PT_USER_MASK
149#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
150
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200151#include <trace/events/kvm.h>
152
Avi Kivity07420172009-07-06 12:21:32 +0300153#define CREATE_TRACE_POINTS
154#include "mmutrace.h"
155
Izik Eidus14032832009-09-23 21:47:17 +0300156#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
157
Avi Kivity135f8c22008-08-21 17:49:56 +0300158#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
159
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300161 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800162 struct kvm_rmap_desc *more;
163};
164
Avi Kivity2d111232008-12-25 14:39:47 +0200165struct kvm_shadow_walk_iterator {
166 u64 addr;
167 hpa_t shadow_addr;
168 int level;
169 u64 *sptep;
170 unsigned index;
171};
172
173#define for_each_shadow_entry(_vcpu, _addr, _walker) \
174 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
175 shadow_walk_okay(&(_walker)); \
176 shadow_walk_next(&(_walker)))
177
Xiao Guangrong1047df12010-06-11 21:35:15 +0800178typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300179
Avi Kivityb5a33a72007-04-15 16:31:09 +0300180static struct kmem_cache *pte_chain_cache;
181static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300182static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700183static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300184
Avi Kivityc7addb92007-09-16 18:58:32 +0200185static u64 __read_mostly shadow_trap_nonpresent_pte;
186static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800187static u64 __read_mostly shadow_base_present_pte;
188static u64 __read_mostly shadow_nx_mask;
189static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
190static u64 __read_mostly shadow_user_mask;
191static u64 __read_mostly shadow_accessed_mask;
192static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200193
Dong, Eddie82725b22009-03-30 16:21:08 +0800194static inline u64 rsvd_bits(int s, int e)
195{
196 return ((1ULL << (e - s + 1)) - 1) << s;
197}
198
Avi Kivityc7addb92007-09-16 18:58:32 +0200199void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
200{
201 shadow_trap_nonpresent_pte = trap_pte;
202 shadow_notrap_nonpresent_pte = notrap_pte;
203}
204EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
205
Sheng Yang7b523452008-04-25 21:13:50 +0800206void kvm_mmu_set_base_ptes(u64 base_pte)
207{
208 shadow_base_present_pte = base_pte;
209}
210EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
211
212void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800213 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800214{
215 shadow_user_mask = user_mask;
216 shadow_accessed_mask = accessed_mask;
217 shadow_dirty_mask = dirty_mask;
218 shadow_nx_mask = nx_mask;
219 shadow_x_mask = x_mask;
220}
221EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
222
Avi Kivity3dbe1412010-05-12 11:48:18 +0300223static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200225 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226}
227
228static int is_cpuid_PSE36(void)
229{
230 return 1;
231}
232
Avi Kivity73b10872007-01-26 00:56:41 -0800233static int is_nx(struct kvm_vcpu *vcpu)
234{
Avi Kivityf6801df2010-01-21 15:31:50 +0200235 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800236}
237
Avi Kivityc7addb92007-09-16 18:58:32 +0200238static int is_shadow_present_pte(u64 pte)
239{
Avi Kivityc7addb92007-09-16 18:58:32 +0200240 return pte != shadow_trap_nonpresent_pte
241 && pte != shadow_notrap_nonpresent_pte;
242}
243
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300244static int is_large_pte(u64 pte)
245{
246 return pte & PT_PAGE_SIZE_MASK;
247}
248
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900249static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250{
251 return pte & PT_WRITABLE_MASK;
252}
253
Avi Kivity43a37952009-06-10 14:12:05 +0300254static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200255{
Avi Kivity439e2182009-06-10 12:56:54 +0300256 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200257}
258
Avi Kivity43a37952009-06-10 14:12:05 +0300259static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800260{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200261 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800262}
263
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300264static int is_last_spte(u64 pte, int level)
265{
266 if (level == PT_PAGE_TABLE_LEVEL)
267 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200268 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300269 return 1;
270 return 0;
271}
272
Anthony Liguori35149e22008-04-02 14:46:56 -0500273static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200274{
Anthony Liguori35149e22008-04-02 14:46:56 -0500275 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200276}
277
Avi Kivityda928522007-11-21 13:54:47 +0200278static gfn_t pse36_gfn_delta(u32 gpte)
279{
280 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
281
282 return (gpte & PT32_DIR_PSE36_MASK) << shift;
283}
284
Avi Kivityd555c332009-06-10 14:24:23 +0300285static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300286{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700287 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300288}
289
Avi Kivitya9221dd2010-06-06 14:48:06 +0300290static u64 __xchg_spte(u64 *sptep, u64 new_spte)
291{
292#ifdef CONFIG_X86_64
293 return xchg(sptep, new_spte);
294#else
295 u64 old_spte;
296
297 do {
298 old_spte = *sptep;
299 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
300
301 return old_spte;
302#endif
303}
304
Xiao Guangrong8672b722010-08-02 16:14:04 +0800305static bool spte_has_volatile_bits(u64 spte)
306{
307 if (!shadow_accessed_mask)
308 return false;
309
310 if (!is_shadow_present_pte(spte))
311 return false;
312
Xiao Guangrong41327792010-08-02 16:15:08 +0800313 if ((spte & shadow_accessed_mask) &&
314 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800315 return false;
316
317 return true;
318}
319
Xiao Guangrong41327792010-08-02 16:15:08 +0800320static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
321{
322 return (old_spte & bit_mask) && !(new_spte & bit_mask);
323}
324
Avi Kivityb79b93f2010-06-06 15:46:44 +0300325static void update_spte(u64 *sptep, u64 new_spte)
326{
Xiao Guangrong41327792010-08-02 16:15:08 +0800327 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300328
Xiao Guangrong41327792010-08-02 16:15:08 +0800329 WARN_ON(!is_rmap_spte(new_spte));
330
331 new_spte |= old_spte & shadow_dirty_mask;
332
333 mask = shadow_accessed_mask;
334 if (is_writable_pte(old_spte))
335 mask |= shadow_dirty_mask;
336
337 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300338 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800339 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300340 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800341
342 if (!shadow_accessed_mask)
343 return;
344
345 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
346 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
347 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
348 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300349}
350
Avi Kivitye2dec932007-01-05 16:36:54 -0800351static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300352 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800353{
354 void *obj;
355
356 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800357 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800358 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300359 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800360 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800361 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 cache->objects[cache->nobjs++] = obj;
363 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800364 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800365}
366
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800367static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
368 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800369{
370 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800371 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800372}
373
Avi Kivityc1158e62007-07-20 08:18:27 +0300374static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300375 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300376{
377 struct page *page;
378
379 if (cache->nobjs >= min)
380 return 0;
381 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300382 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300383 if (!page)
384 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300385 cache->objects[cache->nobjs++] = page_address(page);
386 }
387 return 0;
388}
389
390static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
391{
392 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300393 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300394}
395
Avi Kivity8c438502007-04-16 11:53:17 +0300396static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
397{
398 int r;
399
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800400 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300401 pte_chain_cache, 4);
402 if (r)
403 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800404 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800405 rmap_desc_cache, 4 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300406 if (r)
407 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800408 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300409 if (r)
410 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800411 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300412 mmu_page_header_cache, 4);
413out:
Avi Kivity8c438502007-04-16 11:53:17 +0300414 return r;
415}
416
Avi Kivity714b93d2007-01-05 16:36:53 -0800417static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
418{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800419 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
420 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800421 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800422 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
423 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800424}
425
426static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
427 size_t size)
428{
429 void *p;
430
431 BUG_ON(!mc->nobjs);
432 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800433 return p;
434}
435
Avi Kivity714b93d2007-01-05 16:36:53 -0800436static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
437{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800438 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800439 sizeof(struct kvm_pte_chain));
440}
441
Avi Kivity90cb0522007-07-17 13:04:56 +0300442static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800443{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800444 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800445}
446
447static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
448{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800449 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800450 sizeof(struct kvm_rmap_desc));
451}
452
Avi Kivity90cb0522007-07-17 13:04:56 +0300453static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800454{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800455 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800456}
457
Lai Jiangshan2032a932010-05-26 16:49:59 +0800458static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
459{
460 if (!sp->role.direct)
461 return sp->gfns[index];
462
463 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
464}
465
466static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
467{
468 if (sp->role.direct)
469 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
470 else
471 sp->gfns[index] = gfn;
472}
473
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800474/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475 * Return the pointer to the largepage write count for a given
476 * gfn, handling slots that are not large page aligned.
477 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200478static int *slot_largepage_idx(gfn_t gfn,
479 struct kvm_memory_slot *slot,
480 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481{
482 unsigned long idx;
483
Joerg Roedel82855412010-07-01 16:00:11 +0200484 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
485 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Joerg Roedeld25797b2009-07-27 16:30:43 +0200486 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300487}
488
489static void account_shadowed(struct kvm *kvm, gfn_t gfn)
490{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200491 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200493 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300494
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300495 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200496 for (i = PT_DIRECTORY_LEVEL;
497 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
498 write_count = slot_largepage_idx(gfn, slot, i);
499 *write_count += 1;
500 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300501}
502
503static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
504{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200507 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300508
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300509 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200510 for (i = PT_DIRECTORY_LEVEL;
511 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200512 write_count = slot_largepage_idx(gfn, slot, i);
513 *write_count -= 1;
514 WARN_ON(*write_count < 0);
515 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300516}
517
Joerg Roedeld25797b2009-07-27 16:30:43 +0200518static int has_wrprotected_page(struct kvm *kvm,
519 gfn_t gfn,
520 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300521{
Izik Eidus28430992008-10-03 17:40:32 +0300522 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300523 int *largepage_idx;
524
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300525 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300526 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200527 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300528 return *largepage_idx;
529 }
530
531 return 1;
532}
533
Joerg Roedeld25797b2009-07-27 16:30:43 +0200534static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300535{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100536 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200537 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300538
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100539 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300540
Joerg Roedeld25797b2009-07-27 16:30:43 +0200541 for (i = PT_PAGE_TABLE_LEVEL;
542 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
543 if (page_size >= KVM_HPAGE_SIZE(i))
544 ret = i;
545 else
546 break;
547 }
548
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300549 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300550}
551
Joerg Roedeld25797b2009-07-27 16:30:43 +0200552static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300553{
554 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800555 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300556
557 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
558 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200559 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300560
Joerg Roedeld25797b2009-07-27 16:30:43 +0200561 host_level = host_mapping_level(vcpu->kvm, large_gfn);
562
563 if (host_level == PT_PAGE_TABLE_LEVEL)
564 return host_level;
565
Sheng Yang878403b2010-01-05 19:02:29 +0800566 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
567 kvm_x86_ops->get_lpage_level() : host_level;
568
569 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200570 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
571 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200572
573 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300574}
575
576/*
Izik Eidus290fc382007-09-27 14:11:22 +0200577 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200578 */
579
Joerg Roedel44ad9942009-07-27 16:30:42 +0200580static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200581{
582 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300583 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200584
585 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200586 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300587 return &slot->rmap[gfn - slot->base_gfn];
588
Joerg Roedel82855412010-07-01 16:00:11 +0200589 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
590 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300591
Joerg Roedel44ad9942009-07-27 16:30:42 +0200592 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200593}
594
595/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 * Reverse mapping data structures:
597 *
Izik Eidus290fc382007-09-27 14:11:22 +0200598 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
599 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 *
Izik Eidus290fc382007-09-27 14:11:22 +0200601 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
602 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300603 *
604 * Returns the number of rmap entries before the spte was added or zero if
605 * the spte was not added.
606 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800607 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200608static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609{
Avi Kivity4db35312007-11-21 15:28:32 +0200610 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800611 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200612 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300613 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800614
Avi Kivity43a37952009-06-10 14:12:05 +0300615 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300616 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200617 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800618 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200619 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200620 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800621 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200622 *rmapp = (unsigned long)spte;
623 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800624 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800625 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300626 desc->sptes[0] = (u64 *)*rmapp;
627 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200628 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 } else {
630 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200631 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300632 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800633 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300634 count += RMAP_EXT;
635 }
Avi Kivityd555c332009-06-10 14:24:23 +0300636 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800637 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800638 desc = desc->more;
639 }
Avi Kivityd555c332009-06-10 14:24:23 +0300640 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800641 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300642 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300644 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800645}
646
Izik Eidus290fc382007-09-27 14:11:22 +0200647static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800648 struct kvm_rmap_desc *desc,
649 int i,
650 struct kvm_rmap_desc *prev_desc)
651{
652 int j;
653
Avi Kivityd555c332009-06-10 14:24:23 +0300654 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800655 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300656 desc->sptes[i] = desc->sptes[j];
657 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800658 if (j != 0)
659 return;
660 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300661 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800662 else
663 if (prev_desc)
664 prev_desc->more = desc->more;
665 else
Izik Eidus290fc382007-09-27 14:11:22 +0200666 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300667 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800668}
669
Izik Eidus290fc382007-09-27 14:11:22 +0200670static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800671{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800672 struct kvm_rmap_desc *desc;
673 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200674 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800675 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200676 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800677 int i;
678
Avi Kivity4db35312007-11-21 15:28:32 +0200679 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800680 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
681 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200682 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800683 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800684 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200685 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800686 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200687 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800688 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800689 BUG();
690 }
Izik Eidus290fc382007-09-27 14:11:22 +0200691 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800692 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800693 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200694 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800695 prev_desc = NULL;
696 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300697 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
698 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200699 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800700 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800701 prev_desc);
702 return;
703 }
704 prev_desc = desc;
705 desc = desc->more;
706 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800707 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800708 BUG();
709 }
710}
711
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800712static void set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300713{
Avi Kivityce061862010-06-06 14:38:12 +0300714 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800715 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300716
Xiao Guangrong8672b722010-08-02 16:14:04 +0800717 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800718 __set_spte(sptep, new_spte);
Xiao Guangrong8672b722010-08-02 16:14:04 +0800719 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800720 old_spte = __xchg_spte(sptep, new_spte);
721
Avi Kivitya9221dd2010-06-06 14:48:06 +0300722 if (!is_rmap_spte(old_spte))
Avi Kivityce061862010-06-06 14:38:12 +0300723 return;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800724
Avi Kivitya9221dd2010-06-06 14:48:06 +0300725 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800726 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300727 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800728 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300729 kvm_set_pfn_dirty(pfn);
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800730}
731
732static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
733{
734 set_spte_track_bits(sptep, new_spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300735 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300736}
737
Izik Eidus98348e92007-10-16 14:42:30 +0200738static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800739{
Avi Kivity374cbac2007-01-05 16:36:43 -0800740 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200741 u64 *prev_spte;
742 int i;
743
744 if (!*rmapp)
745 return NULL;
746 else if (!(*rmapp & 1)) {
747 if (!spte)
748 return (u64 *)*rmapp;
749 return NULL;
750 }
751 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200752 prev_spte = NULL;
753 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300754 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200755 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300756 return desc->sptes[i];
757 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200758 }
759 desc = desc->more;
760 }
761 return NULL;
762}
763
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200764static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200765{
Izik Eidus290fc382007-09-27 14:11:22 +0200766 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800767 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200768 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800769
Joerg Roedel44ad9942009-07-27 16:30:42 +0200770 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800771
Izik Eidus98348e92007-10-16 14:42:30 +0200772 spte = rmap_next(kvm, rmapp, NULL);
773 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800774 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800775 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800776 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900777 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300778 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800779 write_protected = 1;
780 }
Izik Eidus9647c142007-10-16 14:43:46 +0200781 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800782 }
Izik Eidus855149a2008-03-20 18:17:24 +0200783
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300784 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200785 for (i = PT_DIRECTORY_LEVEL;
786 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
787 rmapp = gfn_to_rmap(kvm, gfn, i);
788 spte = rmap_next(kvm, rmapp, NULL);
789 while (spte) {
790 BUG_ON(!spte);
791 BUG_ON(!(*spte & PT_PRESENT_MASK));
792 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
793 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900794 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300795 drop_spte(kvm, spte,
796 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200797 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200798 spte = NULL;
799 write_protected = 1;
800 }
801 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300802 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300803 }
804
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200805 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800806}
807
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000808static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
809 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200810{
811 u64 *spte;
812 int need_tlb_flush = 0;
813
814 while ((spte = rmap_next(kvm, rmapp, NULL))) {
815 BUG_ON(!(*spte & PT_PRESENT_MASK));
816 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300817 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200818 need_tlb_flush = 1;
819 }
820 return need_tlb_flush;
821}
822
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000823static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
824 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300825{
826 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800827 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300828 pte_t *ptep = (pte_t *)data;
829 pfn_t new_pfn;
830
831 WARN_ON(pte_huge(*ptep));
832 new_pfn = pte_pfn(*ptep);
833 spte = rmap_next(kvm, rmapp, NULL);
834 while (spte) {
835 BUG_ON(!is_shadow_present_pte(*spte));
836 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
837 need_flush = 1;
838 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300839 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300840 spte = rmap_next(kvm, rmapp, NULL);
841 } else {
842 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
843 new_spte |= (u64)new_pfn << PAGE_SHIFT;
844
845 new_spte &= ~PT_WRITABLE_MASK;
846 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300847 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800848 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300849 spte = rmap_next(kvm, rmapp, spte);
850 }
851 }
852 if (need_flush)
853 kvm_flush_remote_tlbs(kvm);
854
855 return 0;
856}
857
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000858static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
859 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300860 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000861 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200862{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200863 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200864 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200865 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200866 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200867
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800868 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200869
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200870 for (i = 0; i < slots->nmemslots; i++) {
871 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200872 unsigned long start = memslot->userspace_addr;
873 unsigned long end;
874
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200875 end = start + (memslot->npages << PAGE_SHIFT);
876 if (hva >= start && hva < end) {
877 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200878
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200879 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200880
881 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200882 unsigned long idx;
883 int sh;
884
885 sh = KVM_HPAGE_GFN_SHIFT(PT_DIRECTORY_LEVEL+j);
886 idx = ((memslot->base_gfn+gfn_offset) >> sh) -
887 (memslot->base_gfn >> sh);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200888 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300889 &memslot->lpage_info[j][idx].rmap_pde,
890 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200891 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200892 trace_kvm_age_page(hva, memslot, ret);
893 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200894 }
895 }
896
897 return retval;
898}
899
900int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
901{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300902 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200903}
904
Izik Eidus3da0dd42009-09-23 21:47:18 +0300905void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
906{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000907 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300908}
909
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000910static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
911 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200912{
913 u64 *spte;
914 int young = 0;
915
Rik van Riel6316e1c2010-02-03 16:11:03 -0500916 /*
917 * Emulate the accessed bit for EPT, by checking if this page has
918 * an EPT mapping, and clearing it if it does. On the next access,
919 * a new EPT mapping will be established.
920 * This has some overhead, but not as much as the cost of swapping
921 * out actively used pages or breaking up actively used hugepages.
922 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800923 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500924 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800925
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200926 spte = rmap_next(kvm, rmapp, NULL);
927 while (spte) {
928 int _young;
929 u64 _spte = *spte;
930 BUG_ON(!(_spte & PT_PRESENT_MASK));
931 _young = _spte & PT_ACCESSED_MASK;
932 if (_young) {
933 young = 1;
934 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
935 }
936 spte = rmap_next(kvm, rmapp, spte);
937 }
938 return young;
939}
940
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300941#define RMAP_RECYCLE_THRESHOLD 1000
942
Joerg Roedel852e3c12009-07-27 16:30:44 +0200943static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300944{
945 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200946 struct kvm_mmu_page *sp;
947
948 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300949
Joerg Roedel852e3c12009-07-27 16:30:44 +0200950 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300951
Izik Eidus3da0dd42009-09-23 21:47:18 +0300952 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300953 kvm_flush_remote_tlbs(vcpu->kvm);
954}
955
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200956int kvm_age_hva(struct kvm *kvm, unsigned long hva)
957{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300958 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200959}
960
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800961#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300962static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963{
Avi Kivity139bdb22007-01-05 16:36:50 -0800964 u64 *pos;
965 u64 *end;
966
Avi Kivity47ad8e62007-05-06 15:50:58 +0300967 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300968 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800969 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800970 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800972 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 return 1;
974}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800975#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
Dave Hansen45221ab2010-08-19 18:11:37 -0700977/*
978 * This value is the sum of all of the kvm instances's
979 * kvm->arch.n_used_mmu_pages values. We need a global,
980 * aggregate version in order to make the slab shrinker
981 * faster
982 */
983static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
984{
985 kvm->arch.n_used_mmu_pages += nr;
986 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
987}
988
Avi Kivity4db35312007-11-21 15:28:32 +0200989static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800990{
Avi Kivity4db35312007-11-21 15:28:32 +0200991 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800992 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200993 list_del(&sp->link);
994 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800995 if (!sp->role.direct)
996 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800997 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -0700998 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -0800999}
1000
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001001static unsigned kvm_page_table_hashfn(gfn_t gfn)
1002{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001003 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001004}
1005
Avi Kivity25c0de22007-01-05 16:36:42 -08001006static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001007 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008{
Avi Kivity4db35312007-11-21 15:28:32 +02001009 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001011 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1012 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001013 if (!direct)
1014 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1015 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001016 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001017 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001018 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001019 sp->multimapped = 0;
1020 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001021 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001022 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023}
1024
Avi Kivity714b93d2007-01-05 16:36:53 -08001025static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001026 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001027{
1028 struct kvm_pte_chain *pte_chain;
1029 struct hlist_node *node;
1030 int i;
1031
1032 if (!parent_pte)
1033 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001034 if (!sp->multimapped) {
1035 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001036
1037 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001038 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001039 return;
1040 }
Avi Kivity4db35312007-11-21 15:28:32 +02001041 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001042 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001043 INIT_HLIST_HEAD(&sp->parent_ptes);
1044 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001045 pte_chain->parent_ptes[0] = old;
1046 }
Avi Kivity4db35312007-11-21 15:28:32 +02001047 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001048 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1049 continue;
1050 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1051 if (!pte_chain->parent_ptes[i]) {
1052 pte_chain->parent_ptes[i] = parent_pte;
1053 return;
1054 }
1055 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001056 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001057 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001058 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001059 pte_chain->parent_ptes[0] = parent_pte;
1060}
1061
Avi Kivity4db35312007-11-21 15:28:32 +02001062static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001063 u64 *parent_pte)
1064{
1065 struct kvm_pte_chain *pte_chain;
1066 struct hlist_node *node;
1067 int i;
1068
Avi Kivity4db35312007-11-21 15:28:32 +02001069 if (!sp->multimapped) {
1070 BUG_ON(sp->parent_pte != parent_pte);
1071 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001072 return;
1073 }
Avi Kivity4db35312007-11-21 15:28:32 +02001074 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001075 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1076 if (!pte_chain->parent_ptes[i])
1077 break;
1078 if (pte_chain->parent_ptes[i] != parent_pte)
1079 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001080 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1081 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001082 pte_chain->parent_ptes[i]
1083 = pte_chain->parent_ptes[i + 1];
1084 ++i;
1085 }
1086 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001087 if (i == 0) {
1088 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001089 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001090 if (hlist_empty(&sp->parent_ptes)) {
1091 sp->multimapped = 0;
1092 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001093 }
1094 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001095 return;
1096 }
1097 BUG();
1098}
1099
Xiao Guangrong6b184932010-04-16 21:29:17 +08001100static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001101{
1102 struct kvm_pte_chain *pte_chain;
1103 struct hlist_node *node;
1104 struct kvm_mmu_page *parent_sp;
1105 int i;
1106
1107 if (!sp->multimapped && sp->parent_pte) {
1108 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001109 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001110 return;
1111 }
1112
1113 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1114 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001115 u64 *spte = pte_chain->parent_ptes[i];
1116
1117 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001118 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001119 parent_sp = page_header(__pa(spte));
1120 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001121 }
1122}
1123
Xiao Guangrong1047df12010-06-11 21:35:15 +08001124static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001125static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001126{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001127 mmu_parent_walk(sp, mark_unsync);
1128}
1129
1130static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1131{
1132 unsigned int index;
1133
1134 index = spte - sp->spt;
1135 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1136 return;
1137 if (sp->unsync_children++)
1138 return;
1139 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001140}
1141
Avi Kivityd761a502008-05-29 14:55:03 +03001142static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1143 struct kvm_mmu_page *sp)
1144{
1145 int i;
1146
1147 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1148 sp->spt[i] = shadow_trap_nonpresent_pte;
1149}
1150
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001151static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001152 struct kvm_mmu_page *sp, bool clear_unsync)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001153{
1154 return 1;
1155}
1156
Marcelo Tosattia7052892008-09-23 13:18:35 -03001157static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1158{
1159}
1160
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001161#define KVM_PAGE_ARRAY_NR 16
1162
1163struct kvm_mmu_pages {
1164 struct mmu_page_and_offset {
1165 struct kvm_mmu_page *sp;
1166 unsigned int idx;
1167 } page[KVM_PAGE_ARRAY_NR];
1168 unsigned int nr;
1169};
1170
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001171#define for_each_unsync_children(bitmap, idx) \
1172 for (idx = find_first_bit(bitmap, 512); \
1173 idx < 512; \
1174 idx = find_next_bit(bitmap, 512, idx+1))
1175
Hannes Edercded19f2009-02-21 02:19:13 +01001176static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1177 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001178{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001179 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001180
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001181 if (sp->unsync)
1182 for (i=0; i < pvec->nr; i++)
1183 if (pvec->page[i].sp == sp)
1184 return 0;
1185
1186 pvec->page[pvec->nr].sp = sp;
1187 pvec->page[pvec->nr].idx = idx;
1188 pvec->nr++;
1189 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1190}
1191
1192static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1193 struct kvm_mmu_pages *pvec)
1194{
1195 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001196
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001197 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001198 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001199 u64 ent = sp->spt[i];
1200
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001201 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1202 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001203
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001204 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001205
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001206 if (child->unsync_children) {
1207 if (mmu_pages_add(pvec, child, i))
1208 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001210 ret = __mmu_unsync_walk(child, pvec);
1211 if (!ret)
1212 goto clear_child_bitmap;
1213 else if (ret > 0)
1214 nr_unsync_leaf += ret;
1215 else
1216 return ret;
1217 } else if (child->unsync) {
1218 nr_unsync_leaf++;
1219 if (mmu_pages_add(pvec, child, i))
1220 return -ENOSPC;
1221 } else
1222 goto clear_child_bitmap;
1223
1224 continue;
1225
1226clear_child_bitmap:
1227 __clear_bit(i, sp->unsync_child_bitmap);
1228 sp->unsync_children--;
1229 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230 }
1231
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001232
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001233 return nr_unsync_leaf;
1234}
1235
1236static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1237 struct kvm_mmu_pages *pvec)
1238{
1239 if (!sp->unsync_children)
1240 return 0;
1241
1242 mmu_pages_add(pvec, sp, 0);
1243 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001244}
1245
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001246static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1247{
1248 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001249 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250 sp->unsync = 0;
1251 --kvm->stat.mmu_unsync;
1252}
1253
Xiao Guangrong77758342010-06-04 21:53:54 +08001254static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1255 struct list_head *invalid_list);
1256static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1257 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001258
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001259#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1260 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001261 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1262 if ((sp)->gfn != (gfn)) {} else
1263
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001264#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1265 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001266 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1267 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1268 (sp)->role.invalid) {} else
1269
Xiao Guangrongf918b442010-06-11 21:30:36 +08001270/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001271static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001272 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001273{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001274 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001275 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001276 return 1;
1277 }
1278
Xiao Guangrongf918b442010-06-11 21:30:36 +08001279 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001280 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001281
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001282 if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001283 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001284 return 1;
1285 }
1286
1287 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001288 return 0;
1289}
1290
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001291static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1292 struct kvm_mmu_page *sp)
1293{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001294 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001295 int ret;
1296
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001297 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001298 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001299 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1300
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001301 return ret;
1302}
1303
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001304static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1305 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001306{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001307 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001308}
1309
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001310/* @gfn should be write-protected at the call site */
1311static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1312{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001313 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001314 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001315 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001316 bool flush = false;
1317
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001318 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001319 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001320 continue;
1321
1322 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1323 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001324 (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001325 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001326 continue;
1327 }
1328 kvm_unlink_unsync_page(vcpu->kvm, s);
1329 flush = true;
1330 }
1331
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001332 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001333 if (flush)
1334 kvm_mmu_flush_tlb(vcpu);
1335}
1336
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001337struct mmu_page_path {
1338 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1339 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001340};
1341
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001342#define for_each_sp(pvec, sp, parents, i) \
1343 for (i = mmu_pages_next(&pvec, &parents, -1), \
1344 sp = pvec.page[i].sp; \
1345 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1346 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001347
Hannes Edercded19f2009-02-21 02:19:13 +01001348static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1349 struct mmu_page_path *parents,
1350 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001351{
1352 int n;
1353
1354 for (n = i+1; n < pvec->nr; n++) {
1355 struct kvm_mmu_page *sp = pvec->page[n].sp;
1356
1357 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1358 parents->idx[0] = pvec->page[n].idx;
1359 return n;
1360 }
1361
1362 parents->parent[sp->role.level-2] = sp;
1363 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1364 }
1365
1366 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001367}
1368
Hannes Edercded19f2009-02-21 02:19:13 +01001369static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001370{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001371 struct kvm_mmu_page *sp;
1372 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001374 do {
1375 unsigned int idx = parents->idx[level];
1376
1377 sp = parents->parent[level];
1378 if (!sp)
1379 return;
1380
1381 --sp->unsync_children;
1382 WARN_ON((int)sp->unsync_children < 0);
1383 __clear_bit(idx, sp->unsync_child_bitmap);
1384 level++;
1385 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1386}
1387
1388static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1389 struct mmu_page_path *parents,
1390 struct kvm_mmu_pages *pvec)
1391{
1392 parents->parent[parent->role.level-1] = NULL;
1393 pvec->nr = 0;
1394}
1395
1396static void mmu_sync_children(struct kvm_vcpu *vcpu,
1397 struct kvm_mmu_page *parent)
1398{
1399 int i;
1400 struct kvm_mmu_page *sp;
1401 struct mmu_page_path parents;
1402 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001403 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001404
1405 kvm_mmu_pages_init(parent, &parents, &pages);
1406 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001407 int protected = 0;
1408
1409 for_each_sp(pages, sp, parents, i)
1410 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1411
1412 if (protected)
1413 kvm_flush_remote_tlbs(vcpu->kvm);
1414
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001415 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001416 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001417 mmu_pages_clear_parents(&parents);
1418 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001419 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001420 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001421 kvm_mmu_pages_init(parent, &parents, &pages);
1422 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001423}
1424
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001425static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1426 gfn_t gfn,
1427 gva_t gaddr,
1428 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001429 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001430 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001431 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001432{
1433 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001434 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001435 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001436 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001437 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001438
Avi Kivitya770f6f2008-12-21 19:20:09 +02001439 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001440 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001441 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001442 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001443 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001444 role.access = access;
Gui Jianfengb66d8002010-05-31 17:11:39 +08001445 if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001446 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1447 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1448 role.quadrant = quadrant;
1449 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001450 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001451 if (!need_sync && sp->unsync)
1452 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001453
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001454 if (sp->role.word != role.word)
1455 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001456
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001457 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1458 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001459
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001460 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1461 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001462 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001463 kvm_mmu_mark_parents_unsync(sp);
1464 } else if (sp->unsync)
1465 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001466
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001467 trace_kvm_mmu_get_page(sp, false);
1468 return sp;
1469 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001470 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001471 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001472 if (!sp)
1473 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001474 sp->gfn = gfn;
1475 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001476 hlist_add_head(&sp->hash_link,
1477 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001478 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001479 if (rmap_write_protect(vcpu->kvm, gfn))
1480 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001481 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1482 kvm_sync_pages(vcpu, gfn);
1483
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001484 account_shadowed(vcpu->kvm, gfn);
1485 }
Avi Kivity131d8272008-05-29 14:56:28 +03001486 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1487 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1488 else
1489 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001490 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001491 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001492}
1493
Avi Kivity2d111232008-12-25 14:39:47 +02001494static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1495 struct kvm_vcpu *vcpu, u64 addr)
1496{
1497 iterator->addr = addr;
1498 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1499 iterator->level = vcpu->arch.mmu.shadow_root_level;
1500 if (iterator->level == PT32E_ROOT_LEVEL) {
1501 iterator->shadow_addr
1502 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1503 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1504 --iterator->level;
1505 if (!iterator->shadow_addr)
1506 iterator->level = 0;
1507 }
1508}
1509
1510static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1511{
1512 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1513 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001514
1515 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1516 if (is_large_pte(*iterator->sptep))
1517 return false;
1518
Avi Kivity2d111232008-12-25 14:39:47 +02001519 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1520 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1521 return true;
1522}
1523
1524static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1525{
1526 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1527 --iterator->level;
1528}
1529
Avi Kivity32ef26a2010-07-13 14:27:04 +03001530static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1531{
1532 u64 spte;
1533
1534 spte = __pa(sp->spt)
1535 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1536 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001537 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001538}
1539
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001540static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1541{
1542 if (is_large_pte(*sptep)) {
1543 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1544 kvm_flush_remote_tlbs(vcpu->kvm);
1545 }
1546}
1547
Avi Kivitya357bd22010-07-13 14:27:07 +03001548static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1549 unsigned direct_access)
1550{
1551 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1552 struct kvm_mmu_page *child;
1553
1554 /*
1555 * For the direct sp, if the guest pte's dirty bit
1556 * changed form clean to dirty, it will corrupt the
1557 * sp's access: allow writable in the read-only sp,
1558 * so we should update the spte at this point to get
1559 * a new sp with the correct access.
1560 */
1561 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1562 if (child->role.access == direct_access)
1563 return;
1564
1565 mmu_page_remove_parent_pte(child, sptep);
1566 __set_spte(sptep, shadow_trap_nonpresent_pte);
1567 kvm_flush_remote_tlbs(vcpu->kvm);
1568 }
1569}
1570
Avi Kivity90cb0522007-07-17 13:04:56 +03001571static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001572 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001573{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001574 unsigned i;
1575 u64 *pt;
1576 u64 ent;
1577
Avi Kivity4db35312007-11-21 15:28:32 +02001578 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001579
Avi Kivity697fe2e2007-01-05 16:36:46 -08001580 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1581 ent = pt[i];
1582
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001583 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001584 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001585 ent &= PT64_BASE_ADDR_MASK;
1586 mmu_page_remove_parent_pte(page_header(ent),
1587 &pt[i]);
1588 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001589 if (is_large_pte(ent))
1590 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001591 drop_spte(kvm, &pt[i],
1592 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001593 }
1594 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001595 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001596 }
Avi Kivitya4360362007-01-05 16:36:45 -08001597}
1598
Avi Kivity4db35312007-11-21 15:28:32 +02001599static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001600{
Avi Kivity4db35312007-11-21 15:28:32 +02001601 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001602}
1603
Avi Kivity12b7d282007-09-23 14:10:49 +02001604static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1605{
1606 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001607 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001608
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001609 kvm_for_each_vcpu(i, vcpu, kvm)
1610 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001611}
1612
Avi Kivity31aa2b42008-07-11 17:59:46 +03001613static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001614{
1615 u64 *parent_pte;
1616
Avi Kivity4db35312007-11-21 15:28:32 +02001617 while (sp->multimapped || sp->parent_pte) {
1618 if (!sp->multimapped)
1619 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001620 else {
1621 struct kvm_pte_chain *chain;
1622
Avi Kivity4db35312007-11-21 15:28:32 +02001623 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001624 struct kvm_pte_chain, link);
1625 parent_pte = chain->parent_ptes[0];
1626 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001627 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001628 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001629 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001630 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001631}
1632
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001633static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001634 struct kvm_mmu_page *parent,
1635 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001636{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001637 int i, zapped = 0;
1638 struct mmu_page_path parents;
1639 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001640
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001641 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001642 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001643
1644 kvm_mmu_pages_init(parent, &parents, &pages);
1645 while (mmu_unsync_walk(parent, &pages)) {
1646 struct kvm_mmu_page *sp;
1647
1648 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001649 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001650 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001651 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001652 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001653 kvm_mmu_pages_init(parent, &parents, &pages);
1654 }
1655
1656 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001657}
1658
Xiao Guangrong77758342010-06-04 21:53:54 +08001659static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1660 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001661{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001662 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001663
Xiao Guangrong77758342010-06-04 21:53:54 +08001664 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001665 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001666 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001667 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001668 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001669 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001670 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001671 if (sp->unsync)
1672 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001673 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001674 /* Count self */
1675 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001676 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001677 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001678 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001679 kvm_reload_remote_mmus(kvm);
1680 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001681
1682 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001683 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001684 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001685}
1686
Xiao Guangrong77758342010-06-04 21:53:54 +08001687static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1688 struct list_head *invalid_list)
1689{
1690 struct kvm_mmu_page *sp;
1691
1692 if (list_empty(invalid_list))
1693 return;
1694
1695 kvm_flush_remote_tlbs(kvm);
1696
1697 do {
1698 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1699 WARN_ON(!sp->role.invalid || sp->root_count);
1700 kvm_mmu_free_page(kvm, sp);
1701 } while (!list_empty(invalid_list));
1702
1703}
1704
Izik Eidus82ce2c92007-10-02 18:52:55 +02001705/*
1706 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001707 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001708 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001709void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001710{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001711 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001712 /*
1713 * If we set the number of mmu pages to be smaller be than the
1714 * number of actived pages , we must to free some mmu pages before we
1715 * change the value
1716 */
1717
Dave Hansen49d5ca22010-08-19 18:11:28 -07001718 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1719 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001720 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001721 struct kvm_mmu_page *page;
1722
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001723 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001724 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001725 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1726 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001727 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001728 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001729 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001730
Dave Hansen49d5ca22010-08-19 18:11:28 -07001731 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001732}
1733
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001734static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001735{
Avi Kivity4db35312007-11-21 15:28:32 +02001736 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001737 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001738 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001739 int r;
1740
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001741 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001742 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001743
1744 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001745 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1746 sp->role.word);
1747 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001748 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001749 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001750 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001751 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001752}
1753
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001754static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001755{
Avi Kivity4db35312007-11-21 15:28:32 +02001756 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001757 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001758 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001759
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001760 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001761 pgprintk("%s: zap %lx %x\n",
1762 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001763 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001764 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001765 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001766}
1767
Avi Kivity38c335f2007-11-21 14:20:22 +02001768static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001770 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001771 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772
Sheng Yang291f26b2008-10-16 17:30:57 +08001773 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774}
1775
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001776static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1777{
1778 int i;
1779 u64 *pt = sp->spt;
1780
1781 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1782 return;
1783
1784 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1785 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001786 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001787 }
1788}
1789
Sheng Yang74be52e2008-10-09 16:01:56 +08001790/*
1791 * The function is based on mtrr_type_lookup() in
1792 * arch/x86/kernel/cpu/mtrr/generic.c
1793 */
1794static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1795 u64 start, u64 end)
1796{
1797 int i;
1798 u64 base, mask;
1799 u8 prev_match, curr_match;
1800 int num_var_ranges = KVM_NR_VAR_MTRR;
1801
1802 if (!mtrr_state->enabled)
1803 return 0xFF;
1804
1805 /* Make end inclusive end, instead of exclusive */
1806 end--;
1807
1808 /* Look in fixed ranges. Just return the type as per start */
1809 if (mtrr_state->have_fixed && (start < 0x100000)) {
1810 int idx;
1811
1812 if (start < 0x80000) {
1813 idx = 0;
1814 idx += (start >> 16);
1815 return mtrr_state->fixed_ranges[idx];
1816 } else if (start < 0xC0000) {
1817 idx = 1 * 8;
1818 idx += ((start - 0x80000) >> 14);
1819 return mtrr_state->fixed_ranges[idx];
1820 } else if (start < 0x1000000) {
1821 idx = 3 * 8;
1822 idx += ((start - 0xC0000) >> 12);
1823 return mtrr_state->fixed_ranges[idx];
1824 }
1825 }
1826
1827 /*
1828 * Look in variable ranges
1829 * Look of multiple ranges matching this address and pick type
1830 * as per MTRR precedence
1831 */
1832 if (!(mtrr_state->enabled & 2))
1833 return mtrr_state->def_type;
1834
1835 prev_match = 0xFF;
1836 for (i = 0; i < num_var_ranges; ++i) {
1837 unsigned short start_state, end_state;
1838
1839 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1840 continue;
1841
1842 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1843 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1844 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1845 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1846
1847 start_state = ((start & mask) == (base & mask));
1848 end_state = ((end & mask) == (base & mask));
1849 if (start_state != end_state)
1850 return 0xFE;
1851
1852 if ((start & mask) != (base & mask))
1853 continue;
1854
1855 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1856 if (prev_match == 0xFF) {
1857 prev_match = curr_match;
1858 continue;
1859 }
1860
1861 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1862 curr_match == MTRR_TYPE_UNCACHABLE)
1863 return MTRR_TYPE_UNCACHABLE;
1864
1865 if ((prev_match == MTRR_TYPE_WRBACK &&
1866 curr_match == MTRR_TYPE_WRTHROUGH) ||
1867 (prev_match == MTRR_TYPE_WRTHROUGH &&
1868 curr_match == MTRR_TYPE_WRBACK)) {
1869 prev_match = MTRR_TYPE_WRTHROUGH;
1870 curr_match = MTRR_TYPE_WRTHROUGH;
1871 }
1872
1873 if (prev_match != curr_match)
1874 return MTRR_TYPE_UNCACHABLE;
1875 }
1876
1877 if (prev_match != 0xFF)
1878 return prev_match;
1879
1880 return mtrr_state->def_type;
1881}
1882
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001883u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001884{
1885 u8 mtrr;
1886
1887 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1888 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1889 if (mtrr == 0xfe || mtrr == 0xff)
1890 mtrr = MTRR_TYPE_WRBACK;
1891 return mtrr;
1892}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001893EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001894
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001895static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001896{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001897 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001898 ++vcpu->kvm->stat.mmu_unsync;
1899 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001900
Xiao Guangrong6b184932010-04-16 21:29:17 +08001901 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001902 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001903}
1904
1905static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1906{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001907 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001908 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001909
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001910 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001911 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001912 continue;
1913 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1914 __kvm_unsync_page(vcpu, s);
1915 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001916}
1917
1918static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1919 bool can_unsync)
1920{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001921 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001922 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001923 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001924
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001925 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001926 if (!can_unsync)
1927 return 1;
1928
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001929 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001930 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001931
1932 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001933 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001934 return 1;
1935 need_unsync = true;
1936 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001937 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001938 if (need_unsync)
1939 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001940 return 0;
1941}
1942
Avi Kivityd555c332009-06-10 14:24:23 +03001943static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001944 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001945 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001946 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001947 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001948{
1949 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001950 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001951
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001952 /*
1953 * We don't set the accessed bit, since we sometimes want to see
1954 * whether the guest actually used the pte (in order to detect
1955 * demand paging).
1956 */
Xiao Guangrong41327792010-08-02 16:15:08 +08001957 spte = shadow_base_present_pte;
Avi Kivity947da532008-03-18 11:05:52 +02001958 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001959 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001960 if (!dirty)
1961 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001962 if (pte_access & ACC_EXEC_MASK)
1963 spte |= shadow_x_mask;
1964 else
1965 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001966 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001967 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001968 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001969 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001970 if (tdp_enabled)
1971 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1972 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973
Izik Eidus14032832009-09-23 21:47:17 +03001974 if (reset_host_protection)
1975 spte |= SPTE_HOST_WRITEABLE;
1976
Anthony Liguori35149e22008-04-02 14:46:56 -05001977 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001978
1979 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001980 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1981 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001982
Joerg Roedel852e3c12009-07-27 16:30:44 +02001983 if (level > PT_PAGE_TABLE_LEVEL &&
1984 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001985 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001986 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1987 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001988 }
1989
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001990 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001991
Avi Kivity69325a12010-05-27 14:35:58 +03001992 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1993 spte &= ~PT_USER_MASK;
1994
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001995 /*
1996 * Optimization: for pte sync, if spte was writable the hash
1997 * lookup is unnecessary (and expensive). Write protection
1998 * is responsibility of mmu_get_page / kvm_sync_page.
1999 * Same reasoning can be applied to dirty page accounting.
2000 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002001 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002002 goto set_pte;
2003
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002004 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002005 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002006 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002007 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002008 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002009 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002010 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002011 }
2012 }
2013
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002014 if (pte_access & ACC_WRITE_MASK)
2015 mark_page_dirty(vcpu->kvm, gfn);
2016
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002017set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002018 update_spte(sptep, spte);
Avi Kivitybe38d272010-06-06 14:31:27 +03002019done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002020 return ret;
2021}
2022
Avi Kivityd555c332009-06-10 14:24:23 +03002023static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002024 unsigned pt_access, unsigned pte_access,
2025 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002026 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002027 pfn_t pfn, bool speculative,
2028 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002029{
2030 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002031 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002032
2033 pgprintk("%s: spte %llx access %x write_fault %d"
2034 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002035 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002036 write_fault, user_fault, gfn);
2037
Avi Kivityd555c332009-06-10 14:24:23 +03002038 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002039 /*
2040 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2041 * the parent of the now unreachable PTE.
2042 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002043 if (level > PT_PAGE_TABLE_LEVEL &&
2044 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002045 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002046 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002047
2048 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002049 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002050 __set_spte(sptep, shadow_trap_nonpresent_pte);
2051 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002052 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002053 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002054 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002055 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002056 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002057 } else
2058 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002059 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002060
Avi Kivityd555c332009-06-10 14:24:23 +03002061 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002062 dirty, level, gfn, pfn, speculative, true,
2063 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002064 if (write_fault)
2065 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002066 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002067 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002068
Avi Kivityd555c332009-06-10 14:24:23 +03002069 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002070 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002071 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002072 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2073 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002074 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002075 ++vcpu->kvm->stat.lpages;
2076
Avi Kivityd555c332009-06-10 14:24:23 +03002077 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002078 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002079 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002080 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002081 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002082 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002083 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002084 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002085 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002086 vcpu->arch.last_pte_gfn = gfn;
2087 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002088}
2089
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2091{
2092}
2093
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002094static struct kvm_memory_slot *
2095pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2096{
2097 struct kvm_memory_slot *slot;
2098
2099 slot = gfn_to_memslot(vcpu->kvm, gfn);
2100 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2101 (no_dirty_log && slot->dirty_bitmap))
2102 slot = NULL;
2103
2104 return slot;
2105}
2106
2107static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2108 bool no_dirty_log)
2109{
2110 struct kvm_memory_slot *slot;
2111 unsigned long hva;
2112
2113 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2114 if (!slot) {
2115 get_page(bad_page);
2116 return page_to_pfn(bad_page);
2117 }
2118
2119 hva = gfn_to_hva_memslot(slot, gfn);
2120
2121 return hva_to_pfn_atomic(vcpu->kvm, hva);
2122}
2123
2124static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2125 struct kvm_mmu_page *sp,
2126 u64 *start, u64 *end)
2127{
2128 struct page *pages[PTE_PREFETCH_NUM];
2129 unsigned access = sp->role.access;
2130 int i, ret;
2131 gfn_t gfn;
2132
2133 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2134 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2135 return -1;
2136
2137 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2138 if (ret <= 0)
2139 return -1;
2140
2141 for (i = 0; i < ret; i++, gfn++, start++)
2142 mmu_set_spte(vcpu, start, ACC_ALL,
2143 access, 0, 0, 1, NULL,
2144 sp->role.level, gfn,
2145 page_to_pfn(pages[i]), true, true);
2146
2147 return 0;
2148}
2149
2150static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2151 struct kvm_mmu_page *sp, u64 *sptep)
2152{
2153 u64 *spte, *start = NULL;
2154 int i;
2155
2156 WARN_ON(!sp->role.direct);
2157
2158 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2159 spte = sp->spt + i;
2160
2161 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2162 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2163 if (!start)
2164 continue;
2165 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2166 break;
2167 start = NULL;
2168 } else if (!start)
2169 start = spte;
2170 }
2171}
2172
2173static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2174{
2175 struct kvm_mmu_page *sp;
2176
2177 /*
2178 * Since it's no accessed bit on EPT, it's no way to
2179 * distinguish between actually accessed translations
2180 * and prefetched, so disable pte prefetch if EPT is
2181 * enabled.
2182 */
2183 if (!shadow_accessed_mask)
2184 return;
2185
2186 sp = page_header(__pa(sptep));
2187 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2188 return;
2189
2190 __direct_pte_prefetch(vcpu, sp, sptep);
2191}
2192
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002193static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002194 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195{
Avi Kivity9f652d22008-12-25 14:54:25 +02002196 struct kvm_shadow_walk_iterator iterator;
2197 struct kvm_mmu_page *sp;
2198 int pt_write = 0;
2199 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200
Avi Kivity9f652d22008-12-25 14:54:25 +02002201 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002202 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002203 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2204 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002205 level, gfn, pfn, false, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002206 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002207 ++vcpu->stat.pf_fixed;
2208 break;
2209 }
2210
2211 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002212 u64 base_addr = iterator.addr;
2213
2214 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2215 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002216 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2217 iterator.level - 1,
2218 1, ACC_ALL, iterator.sptep);
2219 if (!sp) {
2220 pgprintk("nonpaging_map: ENOMEM\n");
2221 kvm_release_pfn_clean(pfn);
2222 return -ENOMEM;
2223 }
2224
Avi Kivityd555c332009-06-10 14:24:23 +03002225 __set_spte(iterator.sptep,
2226 __pa(sp->spt)
2227 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2228 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002229 }
2230 }
2231 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232}
2233
Huang Yingbf998152010-05-31 14:28:19 +08002234static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2235{
2236 char buf[1];
2237 void __user *hva;
2238 int r;
2239
2240 /* Touch the page, so send SIGBUS */
2241 hva = (void __user *)gfn_to_hva(kvm, gfn);
2242 r = copy_from_user(buf, hva, 1);
2243}
2244
2245static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2246{
2247 kvm_release_pfn_clean(pfn);
2248 if (is_hwpoison_pfn(pfn)) {
2249 kvm_send_hwpoison_signal(kvm, gfn);
2250 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002251 } else if (is_fault_pfn(pfn))
2252 return -EFAULT;
2253
Huang Yingbf998152010-05-31 14:28:19 +08002254 return 1;
2255}
2256
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002257static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2258{
2259 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002260 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002261 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002262 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002263
Joerg Roedel852e3c12009-07-27 16:30:44 +02002264 level = mapping_level(vcpu, gfn);
2265
2266 /*
2267 * This path builds a PAE pagetable - so we can map 2mb pages at
2268 * maximum. Therefore check if the level is larger than that.
2269 */
2270 if (level > PT_DIRECTORY_LEVEL)
2271 level = PT_DIRECTORY_LEVEL;
2272
2273 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002274
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002275 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002276 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002277 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002278
Avi Kivityd196e342008-01-24 11:44:11 +02002279 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002280 if (is_error_pfn(pfn))
2281 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002282
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002283 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002284 if (mmu_notifier_retry(vcpu, mmu_seq))
2285 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002286 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002287 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002288 spin_unlock(&vcpu->kvm->mmu_lock);
2289
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002290
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002291 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002292
2293out_unlock:
2294 spin_unlock(&vcpu->kvm->mmu_lock);
2295 kvm_release_pfn_clean(pfn);
2296 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002297}
2298
2299
Avi Kivity17ac10a2007-01-05 16:36:40 -08002300static void mmu_free_roots(struct kvm_vcpu *vcpu)
2301{
2302 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002303 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002304 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002305
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002306 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002307 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002308 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002309 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2310 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002311
Avi Kivity4db35312007-11-21 15:28:32 +02002312 sp = page_header(root);
2313 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002314 if (!sp->root_count && sp->role.invalid) {
2315 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2316 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2317 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002318 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002319 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002320 return;
2321 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002322 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002323 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002324
Avi Kivity417726a2007-04-12 17:35:58 +03002325 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002326 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002327 sp = page_header(root);
2328 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002329 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002330 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2331 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002332 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002333 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002334 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002335 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002336 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002337 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002338}
2339
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002340static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2341{
2342 int ret = 0;
2343
2344 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002345 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002346 ret = 1;
2347 }
2348
2349 return ret;
2350}
2351
2352static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002353{
2354 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002355 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002356 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002357 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002358 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002359
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002360 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002361
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002362 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2363 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002364
2365 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002366 if (mmu_check_root(vcpu, root_gfn))
2367 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002368 if (tdp_enabled) {
2369 direct = 1;
2370 root_gfn = 0;
2371 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002372 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002373 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002374 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002375 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002376 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002377 root = __pa(sp->spt);
2378 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002379 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002380 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002381 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002382 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002383 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002384 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002385 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002386
2387 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002388 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002389 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002390 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002391 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002392 continue;
2393 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002394 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002395 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002396 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002397 if (mmu_check_root(vcpu, root_gfn))
2398 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002399 if (tdp_enabled) {
2400 direct = 1;
2401 root_gfn = i << 30;
2402 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002403 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002404 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002405 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002406 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002407 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002408 root = __pa(sp->spt);
2409 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002410 spin_unlock(&vcpu->kvm->mmu_lock);
2411
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002412 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002413 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002414 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002415 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002416}
2417
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002418static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2419{
2420 int i;
2421 struct kvm_mmu_page *sp;
2422
2423 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2424 return;
2425 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2426 hpa_t root = vcpu->arch.mmu.root_hpa;
2427 sp = page_header(root);
2428 mmu_sync_children(vcpu, sp);
2429 return;
2430 }
2431 for (i = 0; i < 4; ++i) {
2432 hpa_t root = vcpu->arch.mmu.pae_root[i];
2433
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002434 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002435 root &= PT64_BASE_ADDR_MASK;
2436 sp = page_header(root);
2437 mmu_sync_children(vcpu, sp);
2438 }
2439 }
2440}
2441
2442void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2443{
2444 spin_lock(&vcpu->kvm->mmu_lock);
2445 mmu_sync_roots(vcpu);
2446 spin_unlock(&vcpu->kvm->mmu_lock);
2447}
2448
Gleb Natapov1871c602010-02-10 14:21:32 +02002449static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2450 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451{
Gleb Natapov1871c602010-02-10 14:21:32 +02002452 if (error)
2453 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 return vaddr;
2455}
2456
2457static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002458 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459{
Avi Kivitye8332402007-12-09 18:43:00 +02002460 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002461 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002463 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002464 r = mmu_topup_memory_caches(vcpu);
2465 if (r)
2466 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002467
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002469 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470
Avi Kivitye8332402007-12-09 18:43:00 +02002471 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472
Avi Kivitye8332402007-12-09 18:43:00 +02002473 return nonpaging_map(vcpu, gva & PAGE_MASK,
2474 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475}
2476
Joerg Roedelfb72d162008-02-07 13:47:44 +01002477static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2478 u32 error_code)
2479{
Anthony Liguori35149e22008-04-02 14:46:56 -05002480 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002481 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002482 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002483 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002484 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002485
2486 ASSERT(vcpu);
2487 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2488
2489 r = mmu_topup_memory_caches(vcpu);
2490 if (r)
2491 return r;
2492
Joerg Roedel852e3c12009-07-27 16:30:44 +02002493 level = mapping_level(vcpu, gfn);
2494
2495 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2496
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002497 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002498 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002499 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002500 if (is_error_pfn(pfn))
2501 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002502 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002503 if (mmu_notifier_retry(vcpu, mmu_seq))
2504 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002505 kvm_mmu_free_some_pages(vcpu);
2506 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002507 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002508 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002509
2510 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002511
2512out_unlock:
2513 spin_unlock(&vcpu->kvm->mmu_lock);
2514 kvm_release_pfn_clean(pfn);
2515 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002516}
2517
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518static void nonpaging_free(struct kvm_vcpu *vcpu)
2519{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002520 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521}
2522
2523static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2524{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002525 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526
2527 context->new_cr3 = nonpaging_new_cr3;
2528 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 context->gva_to_gpa = nonpaging_gva_to_gpa;
2530 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002531 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002532 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002533 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002534 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002536 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 return 0;
2538}
2539
Avi Kivityd835dfe2007-11-21 02:57:59 +02002540void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002542 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002543 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544}
2545
2546static void paging_new_cr3(struct kvm_vcpu *vcpu)
2547{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002548 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002549 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550}
2551
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552static void inject_page_fault(struct kvm_vcpu *vcpu,
2553 u64 addr,
2554 u32 err_code)
2555{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002556 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557}
2558
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559static void paging_free(struct kvm_vcpu *vcpu)
2560{
2561 nonpaging_free(vcpu);
2562}
2563
Dong, Eddie82725b22009-03-30 16:21:08 +08002564static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2565{
2566 int bit7;
2567
2568 bit7 = (gpte >> 7) & 1;
2569 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2570}
2571
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572#define PTTYPE 64
2573#include "paging_tmpl.h"
2574#undef PTTYPE
2575
2576#define PTTYPE 32
2577#include "paging_tmpl.h"
2578#undef PTTYPE
2579
Dong, Eddie82725b22009-03-30 16:21:08 +08002580static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2581{
2582 struct kvm_mmu *context = &vcpu->arch.mmu;
2583 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2584 u64 exb_bit_rsvd = 0;
2585
2586 if (!is_nx(vcpu))
2587 exb_bit_rsvd = rsvd_bits(63, 63);
2588 switch (level) {
2589 case PT32_ROOT_LEVEL:
2590 /* no rsvd bits for 2 level 4K page table entries */
2591 context->rsvd_bits_mask[0][1] = 0;
2592 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002593 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2594
2595 if (!is_pse(vcpu)) {
2596 context->rsvd_bits_mask[1][1] = 0;
2597 break;
2598 }
2599
Dong, Eddie82725b22009-03-30 16:21:08 +08002600 if (is_cpuid_PSE36())
2601 /* 36bits PSE 4MB page */
2602 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2603 else
2604 /* 32 bits PSE 4MB page */
2605 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002606 break;
2607 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002608 context->rsvd_bits_mask[0][2] =
2609 rsvd_bits(maxphyaddr, 63) |
2610 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002611 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002612 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002613 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2614 rsvd_bits(maxphyaddr, 62); /* PTE */
2615 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2616 rsvd_bits(maxphyaddr, 62) |
2617 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002618 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002619 break;
2620 case PT64_ROOT_LEVEL:
2621 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2622 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2623 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2624 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2625 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002626 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002627 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2628 rsvd_bits(maxphyaddr, 51);
2629 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002630 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2631 rsvd_bits(maxphyaddr, 51) |
2632 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002633 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002634 rsvd_bits(maxphyaddr, 51) |
2635 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002636 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002637 break;
2638 }
2639}
2640
Avi Kivity17ac10a2007-01-05 16:36:40 -08002641static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002643 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644
2645 ASSERT(is_pae(vcpu));
2646 context->new_cr3 = paging_new_cr3;
2647 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002649 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002650 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002651 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002653 context->root_level = level;
2654 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002655 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 return 0;
2657}
2658
Avi Kivity17ac10a2007-01-05 16:36:40 -08002659static int paging64_init_context(struct kvm_vcpu *vcpu)
2660{
Dong, Eddie82725b22009-03-30 16:21:08 +08002661 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002662 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2663}
2664
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665static int paging32_init_context(struct kvm_vcpu *vcpu)
2666{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002667 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668
Dong, Eddie82725b22009-03-30 16:21:08 +08002669 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 context->new_cr3 = paging_new_cr3;
2671 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 context->gva_to_gpa = paging32_gva_to_gpa;
2673 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002674 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002675 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002676 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 context->root_level = PT32_ROOT_LEVEL;
2678 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002679 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 return 0;
2681}
2682
2683static int paging32E_init_context(struct kvm_vcpu *vcpu)
2684{
Dong, Eddie82725b22009-03-30 16:21:08 +08002685 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002686 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687}
2688
Joerg Roedelfb72d162008-02-07 13:47:44 +01002689static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2690{
2691 struct kvm_mmu *context = &vcpu->arch.mmu;
2692
2693 context->new_cr3 = nonpaging_new_cr3;
2694 context->page_fault = tdp_page_fault;
2695 context->free = nonpaging_free;
2696 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002697 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002698 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002699 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002700 context->root_hpa = INVALID_PAGE;
2701
2702 if (!is_paging(vcpu)) {
2703 context->gva_to_gpa = nonpaging_gva_to_gpa;
2704 context->root_level = 0;
2705 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002706 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002707 context->gva_to_gpa = paging64_gva_to_gpa;
2708 context->root_level = PT64_ROOT_LEVEL;
2709 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002710 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002711 context->gva_to_gpa = paging64_gva_to_gpa;
2712 context->root_level = PT32E_ROOT_LEVEL;
2713 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002714 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002715 context->gva_to_gpa = paging32_gva_to_gpa;
2716 context->root_level = PT32_ROOT_LEVEL;
2717 }
2718
2719 return 0;
2720}
2721
2722static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002724 int r;
2725
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002727 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728
2729 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002730 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002731 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002732 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002734 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002736 r = paging32_init_context(vcpu);
2737
Avi Kivity5b7e0102010-04-14 19:20:03 +03002738 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002739 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002740
2741 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742}
2743
Joerg Roedelfb72d162008-02-07 13:47:44 +01002744static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2745{
Anthony Liguori35149e22008-04-02 14:46:56 -05002746 vcpu->arch.update_pte.pfn = bad_pfn;
2747
Joerg Roedelfb72d162008-02-07 13:47:44 +01002748 if (tdp_enabled)
2749 return init_kvm_tdp_mmu(vcpu);
2750 else
2751 return init_kvm_softmmu(vcpu);
2752}
2753
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2755{
2756 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002757 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2758 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002759 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760}
2761
2762int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2763{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002764 destroy_kvm_mmu(vcpu);
2765 return init_kvm_mmu(vcpu);
2766}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002767EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002768
2769int kvm_mmu_load(struct kvm_vcpu *vcpu)
2770{
Avi Kivity714b93d2007-01-05 16:36:53 -08002771 int r;
2772
Avi Kivitye2dec932007-01-05 16:36:54 -08002773 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002774 if (r)
2775 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002776 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002777 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002778 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002779 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002780 if (r)
2781 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002782 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002783 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002784out:
2785 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002787EXPORT_SYMBOL_GPL(kvm_mmu_load);
2788
2789void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2790{
2791 mmu_free_roots(vcpu);
2792}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793
Avi Kivity09072da2007-05-01 14:16:52 +03002794static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002795 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002796 u64 *spte)
2797{
2798 u64 pte;
2799 struct kvm_mmu_page *child;
2800
2801 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002802 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002803 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002804 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002805 else {
2806 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002807 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002808 }
2809 }
Avi Kivityd555c332009-06-10 14:24:23 +03002810 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002811 if (is_large_pte(pte))
2812 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002813}
2814
Avi Kivity00284252007-05-01 16:53:31 +03002815static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002816 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002817 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002818 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002819{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002820 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002821 ++vcpu->kvm->stat.mmu_pde_zapped;
2822 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002823 }
Avi Kivity00284252007-05-01 16:53:31 +03002824
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002825 if (is_rsvd_bits_set(vcpu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
2826 return;
2827
Avi Kivity4cee5762007-11-18 16:37:07 +02002828 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002829 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002830 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002831 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002832 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002833}
2834
Avi Kivity79539ce2007-11-21 02:06:21 +02002835static bool need_remote_flush(u64 old, u64 new)
2836{
2837 if (!is_shadow_present_pte(old))
2838 return false;
2839 if (!is_shadow_present_pte(new))
2840 return true;
2841 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2842 return true;
2843 old ^= PT64_NX_MASK;
2844 new ^= PT64_NX_MASK;
2845 return (old & ~new & PT64_PERM_MASK) != 0;
2846}
2847
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002848static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2849 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002850{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002851 if (zap_page)
2852 return;
2853
2854 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002855 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002856 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002857 kvm_mmu_flush_tlb(vcpu);
2858}
2859
Avi Kivity12b7d282007-09-23 14:10:49 +02002860static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2861{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002862 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002863
Sheng Yang7b523452008-04-25 21:13:50 +08002864 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002865}
2866
Avi Kivityd7824ff2007-12-30 12:29:05 +02002867static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002868 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002869{
2870 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002871 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002872
Avi Kivity43a37952009-06-10 14:12:05 +03002873 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002874 return;
2875 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002876
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002877 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002878 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002879 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002880
Anthony Liguori35149e22008-04-02 14:46:56 -05002881 if (is_error_pfn(pfn)) {
2882 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002883 return;
2884 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002885 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002886 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002887}
2888
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002889static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2890{
2891 u64 *spte = vcpu->arch.last_pte_updated;
2892
2893 if (spte
2894 && vcpu->arch.last_pte_gfn == gfn
2895 && shadow_accessed_mask
2896 && !(*spte & shadow_accessed_mask)
2897 && is_shadow_present_pte(*spte))
2898 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2899}
2900
Avi Kivity09072da2007-05-01 14:16:52 +03002901void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002902 const u8 *new, int bytes,
2903 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002904{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002905 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002906 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02002907 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002908 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002909 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002910 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002911 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002912 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002913 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002914 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002915 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002916 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002917 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002918 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002919 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002920 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002921 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002922 bool remote_flush, local_flush, zap_page;
2923
2924 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002925
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002926 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002927
Avi Kivity08e850c2010-03-15 13:59:57 +02002928 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2929
2930 /*
2931 * Assume that the pte write on a page table of the same type
2932 * as the current vcpu paging mode. This is nearly always true
2933 * (might be false while changing modes). Note it is verified later
2934 * by update_pte().
2935 */
2936 if ((is_pae(vcpu) && bytes == 4) || !new) {
2937 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2938 if (is_pae(vcpu)) {
2939 gpa &= ~(gpa_t)7;
2940 bytes = 8;
2941 }
2942 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2943 if (r)
2944 gentry = 0;
2945 new = (const u8 *)&gentry;
2946 }
2947
Avi Kivity72016f32010-03-15 13:59:53 +02002948 switch (bytes) {
2949 case 4:
2950 gentry = *(const u32 *)new;
2951 break;
2952 case 8:
2953 gentry = *(const u64 *)new;
2954 break;
2955 default:
2956 gentry = 0;
2957 break;
2958 }
2959
Avi Kivity72016f32010-03-15 13:59:53 +02002960 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002961 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002962 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2963 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002964 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002965 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002966 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002967 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002968 if (guest_initiated) {
2969 if (gfn == vcpu->arch.last_pt_write_gfn
2970 && !last_updated_pte_accessed(vcpu)) {
2971 ++vcpu->arch.last_pt_write_count;
2972 if (vcpu->arch.last_pt_write_count >= 3)
2973 flooded = 1;
2974 } else {
2975 vcpu->arch.last_pt_write_gfn = gfn;
2976 vcpu->arch.last_pt_write_count = 1;
2977 vcpu->arch.last_pte_updated = NULL;
2978 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002979 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002980
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002981 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002982 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002983 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002984 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002985 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002986 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002987 /*
2988 * Misaligned accesses are too much trouble to fix
2989 * up; also, they usually indicate a page is not used
2990 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002991 *
2992 * If we're seeing too many writes to a page,
2993 * it may no longer be a page table, or we may be
2994 * forking, in which case it is better to unmap the
2995 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002996 */
2997 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002998 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002999 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003000 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003001 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003002 continue;
3003 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003004 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003005 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003006 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003007 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003008 page_offset <<= 1; /* 32->64 */
3009 /*
3010 * A 32-bit pde maps 4MB while the shadow pdes map
3011 * only 2MB. So we need to double the offset again
3012 * and zap two pdes instead of one.
3013 */
3014 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003015 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003016 page_offset <<= 1;
3017 npte = 2;
3018 }
Avi Kivityfce06572007-05-01 16:44:05 +03003019 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003020 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003021 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003022 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003023 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003024 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003025 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003026 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003027 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003028 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003029 if (gentry &&
3030 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3031 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003032 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003033 if (!remote_flush && need_remote_flush(entry, *spte))
3034 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003035 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003036 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003037 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003038 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003039 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityc7addb92007-09-16 18:58:32 +02003040 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003041 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003042 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3043 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3044 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003045 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003046}
3047
Avi Kivitya4360362007-01-05 16:36:45 -08003048int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3049{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003050 gpa_t gpa;
3051 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003052
Avi Kivity60f24782009-08-27 13:37:06 +03003053 if (tdp_enabled)
3054 return 0;
3055
Gleb Natapov1871c602010-02-10 14:21:32 +02003056 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003057
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003058 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003059 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003060 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003061 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003062}
Avi Kivity577bdc42008-07-19 08:57:05 +03003063EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003064
Avi Kivity22d95b12007-09-14 20:26:06 +03003065void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003066{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003067 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003068
Dave Hansene0df7b92010-08-19 18:11:05 -07003069 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003070 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003071 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003072
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003073 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003074 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003075 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003076 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003077 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003078 }
3079}
Avi Kivityebeace82007-01-05 16:36:47 -08003080
Avi Kivity30677142007-10-28 18:48:59 +02003081int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3082{
3083 int r;
3084 enum emulation_result er;
3085
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003086 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02003087 if (r < 0)
3088 goto out;
3089
3090 if (!r) {
3091 r = 1;
3092 goto out;
3093 }
3094
Avi Kivityb733bfb2007-10-28 18:52:05 +02003095 r = mmu_topup_memory_caches(vcpu);
3096 if (r)
3097 goto out;
3098
Avi Kivity851ba692009-08-24 11:10:17 +03003099 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02003100
3101 switch (er) {
3102 case EMULATE_DONE:
3103 return 1;
3104 case EMULATE_DO_MMIO:
3105 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003106 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003107 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003108 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003109 default:
3110 BUG();
3111 }
3112out:
Avi Kivity30677142007-10-28 18:48:59 +02003113 return r;
3114}
3115EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3116
Marcelo Tosattia7052892008-09-23 13:18:35 -03003117void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3118{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003119 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003120 kvm_mmu_flush_tlb(vcpu);
3121 ++vcpu->stat.invlpg;
3122}
3123EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3124
Joerg Roedel18552672008-02-07 13:47:41 +01003125void kvm_enable_tdp(void)
3126{
3127 tdp_enabled = true;
3128}
3129EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3130
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003131void kvm_disable_tdp(void)
3132{
3133 tdp_enabled = false;
3134}
3135EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3136
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137static void free_mmu_pages(struct kvm_vcpu *vcpu)
3138{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003139 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140}
3141
3142static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3143{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003144 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 int i;
3146
3147 ASSERT(vcpu);
3148
Avi Kivity17ac10a2007-01-05 16:36:40 -08003149 /*
3150 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3151 * Therefore we need to allocate shadow page tables in the first
3152 * 4GB of memory, which happens to fit the DMA32 zone.
3153 */
3154 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3155 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003156 return -ENOMEM;
3157
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003158 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003159 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003160 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003161
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163}
3164
Ingo Molnar8018c272006-12-29 16:50:01 -08003165int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003168 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169
Ingo Molnar8018c272006-12-29 16:50:01 -08003170 return alloc_mmu_pages(vcpu);
3171}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172
Ingo Molnar8018c272006-12-29 16:50:01 -08003173int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3174{
3175 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003176 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003177
Ingo Molnar8018c272006-12-29 16:50:01 -08003178 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179}
3180
3181void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3182{
3183 ASSERT(vcpu);
3184
3185 destroy_kvm_mmu(vcpu);
3186 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08003187 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188}
3189
Avi Kivity90cb0522007-07-17 13:04:56 +03003190void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191{
Avi Kivity4db35312007-11-21 15:28:32 +02003192 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003194 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 int i;
3196 u64 *pt;
3197
Sheng Yang291f26b2008-10-16 17:30:57 +08003198 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 continue;
3200
Avi Kivity4db35312007-11-21 15:28:32 +02003201 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3203 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003204 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 }
Avi Kivity171d5952008-08-27 16:40:51 +03003207 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003209
Avi Kivity90cb0522007-07-17 13:04:56 +03003210void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003211{
Avi Kivity4db35312007-11-21 15:28:32 +02003212 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003213 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003214
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003215 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003216restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003217 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003218 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003219 goto restart;
3220
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003221 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003222 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003223}
3224
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003225static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3226 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003227{
3228 struct kvm_mmu_page *page;
3229
3230 page = container_of(kvm->arch.active_mmu_pages.prev,
3231 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003232 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003233}
3234
Dave Chinner7f8275d2010-07-19 14:56:17 +10003235static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003236{
3237 struct kvm *kvm;
3238 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003239
3240 if (nr_to_scan == 0)
3241 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003242
3243 spin_lock(&kvm_lock);
3244
3245 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003246 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003247 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003248
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003249 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003250 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003251 if (!kvm_freed && nr_to_scan > 0 &&
3252 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003253 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3254 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003255 kvm_freed = kvm;
3256 }
3257 nr_to_scan--;
3258
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003259 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003260 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003261 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003262 }
3263 if (kvm_freed)
3264 list_move_tail(&kvm_freed->vm_list, &vm_list);
3265
3266 spin_unlock(&kvm_lock);
3267
Dave Hansen45221ab2010-08-19 18:11:37 -07003268out:
3269 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003270}
3271
3272static struct shrinker mmu_shrinker = {
3273 .shrink = mmu_shrink,
3274 .seeks = DEFAULT_SEEKS * 10,
3275};
3276
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003277static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003278{
3279 if (pte_chain_cache)
3280 kmem_cache_destroy(pte_chain_cache);
3281 if (rmap_desc_cache)
3282 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003283 if (mmu_page_header_cache)
3284 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003285}
3286
Izik Eidus3ee16c82008-03-30 15:17:21 +03003287void kvm_mmu_module_exit(void)
3288{
3289 mmu_destroy_caches();
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003290 percpu_counter_destroy(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003291 unregister_shrinker(&mmu_shrinker);
3292}
3293
Avi Kivityb5a33a72007-04-15 16:31:09 +03003294int kvm_mmu_module_init(void)
3295{
3296 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3297 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003298 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003299 if (!pte_chain_cache)
3300 goto nomem;
3301 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3302 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003303 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003304 if (!rmap_desc_cache)
3305 goto nomem;
3306
Avi Kivityd3d25b02007-05-30 12:34:53 +03003307 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3308 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003309 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003310 if (!mmu_page_header_cache)
3311 goto nomem;
3312
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003313 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3314 goto nomem;
3315
Izik Eidus3ee16c82008-03-30 15:17:21 +03003316 register_shrinker(&mmu_shrinker);
3317
Avi Kivityb5a33a72007-04-15 16:31:09 +03003318 return 0;
3319
3320nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003321 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003322 return -ENOMEM;
3323}
3324
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003325/*
3326 * Caculate mmu pages needed for kvm.
3327 */
3328unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3329{
3330 int i;
3331 unsigned int nr_mmu_pages;
3332 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003333 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003334
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003335 slots = kvm_memslots(kvm);
3336
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003337 for (i = 0; i < slots->nmemslots; i++)
3338 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003339
3340 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3341 nr_mmu_pages = max(nr_mmu_pages,
3342 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3343
3344 return nr_mmu_pages;
3345}
3346
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003347static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3348 unsigned len)
3349{
3350 if (len > buffer->len)
3351 return NULL;
3352 return buffer->ptr;
3353}
3354
3355static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3356 unsigned len)
3357{
3358 void *ret;
3359
3360 ret = pv_mmu_peek_buffer(buffer, len);
3361 if (!ret)
3362 return ret;
3363 buffer->ptr += len;
3364 buffer->len -= len;
3365 buffer->processed += len;
3366 return ret;
3367}
3368
3369static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3370 gpa_t addr, gpa_t value)
3371{
3372 int bytes = 8;
3373 int r;
3374
3375 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3376 bytes = 4;
3377
3378 r = mmu_topup_memory_caches(vcpu);
3379 if (r)
3380 return r;
3381
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003382 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003383 return -EFAULT;
3384
3385 return 1;
3386}
3387
3388static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3389{
Avi Kivity23902182010-06-10 17:02:16 +03003390 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003391 return 1;
3392}
3393
3394static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3395{
3396 spin_lock(&vcpu->kvm->mmu_lock);
3397 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3398 spin_unlock(&vcpu->kvm->mmu_lock);
3399 return 1;
3400}
3401
3402static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3403 struct kvm_pv_mmu_op_buffer *buffer)
3404{
3405 struct kvm_mmu_op_header *header;
3406
3407 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3408 if (!header)
3409 return 0;
3410 switch (header->op) {
3411 case KVM_MMU_OP_WRITE_PTE: {
3412 struct kvm_mmu_op_write_pte *wpte;
3413
3414 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3415 if (!wpte)
3416 return 0;
3417 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3418 wpte->pte_val);
3419 }
3420 case KVM_MMU_OP_FLUSH_TLB: {
3421 struct kvm_mmu_op_flush_tlb *ftlb;
3422
3423 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3424 if (!ftlb)
3425 return 0;
3426 return kvm_pv_mmu_flush_tlb(vcpu);
3427 }
3428 case KVM_MMU_OP_RELEASE_PT: {
3429 struct kvm_mmu_op_release_pt *rpt;
3430
3431 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3432 if (!rpt)
3433 return 0;
3434 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3435 }
3436 default: return 0;
3437 }
3438}
3439
3440int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3441 gpa_t addr, unsigned long *ret)
3442{
3443 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003444 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003445
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003446 buffer->ptr = buffer->buf;
3447 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3448 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003449
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003450 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003451 if (r)
3452 goto out;
3453
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003454 while (buffer->len) {
3455 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003456 if (r < 0)
3457 goto out;
3458 if (r == 0)
3459 break;
3460 }
3461
3462 r = 1;
3463out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003464 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003465 return r;
3466}
3467
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003468int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3469{
3470 struct kvm_shadow_walk_iterator iterator;
3471 int nr_sptes = 0;
3472
3473 spin_lock(&vcpu->kvm->mmu_lock);
3474 for_each_shadow_entry(vcpu, addr, iterator) {
3475 sptes[iterator.level-1] = *iterator.sptep;
3476 nr_sptes++;
3477 if (!is_shadow_present_pte(*iterator.sptep))
3478 break;
3479 }
3480 spin_unlock(&vcpu->kvm->mmu_lock);
3481
3482 return nr_sptes;
3483}
3484EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3485
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003486#ifdef AUDIT
3487
3488static const char *audit_msg;
3489
3490static gva_t canonicalize(gva_t gva)
3491{
3492#ifdef CONFIG_X86_64
3493 gva = (long long)(gva << 16) >> 16;
3494#endif
3495 return gva;
3496}
3497
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003498
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003499typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003500
3501static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3502 inspect_spte_fn fn)
3503{
3504 int i;
3505
3506 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3507 u64 ent = sp->spt[i];
3508
3509 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003510 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003511 struct kvm_mmu_page *child;
3512 child = page_header(ent & PT64_BASE_ADDR_MASK);
3513 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003514 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003515 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003516 }
3517 }
3518}
3519
3520static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3521{
3522 int i;
3523 struct kvm_mmu_page *sp;
3524
3525 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3526 return;
3527 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3528 hpa_t root = vcpu->arch.mmu.root_hpa;
3529 sp = page_header(root);
3530 __mmu_spte_walk(vcpu->kvm, sp, fn);
3531 return;
3532 }
3533 for (i = 0; i < 4; ++i) {
3534 hpa_t root = vcpu->arch.mmu.pae_root[i];
3535
3536 if (root && VALID_PAGE(root)) {
3537 root &= PT64_BASE_ADDR_MASK;
3538 sp = page_header(root);
3539 __mmu_spte_walk(vcpu->kvm, sp, fn);
3540 }
3541 }
3542 return;
3543}
3544
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003545static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3546 gva_t va, int level)
3547{
3548 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3549 int i;
3550 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3551
3552 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3553 u64 ent = pt[i];
3554
Avi Kivityc7addb92007-09-16 18:58:32 +02003555 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003556 continue;
3557
3558 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003559 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3560 audit_mappings_page(vcpu, ent, va, level - 1);
3561 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003562 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003563 gfn_t gfn = gpa >> PAGE_SHIFT;
3564 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3565 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003566
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003567 if (is_error_pfn(pfn)) {
3568 kvm_release_pfn_clean(pfn);
3569 continue;
3570 }
3571
Avi Kivityc7addb92007-09-16 18:58:32 +02003572 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003573 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003574 printk(KERN_ERR "xx audit error: (%s) levels %d"
3575 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003576 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003577 va, gpa, hpa, ent,
3578 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003579 else if (ent == shadow_notrap_nonpresent_pte
3580 && !is_error_hpa(hpa))
3581 printk(KERN_ERR "audit: (%s) notrap shadow,"
3582 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003583 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003584
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003585 }
3586 }
3587}
3588
3589static void audit_mappings(struct kvm_vcpu *vcpu)
3590{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003591 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003592
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003593 if (vcpu->arch.mmu.root_level == 4)
3594 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003595 else
3596 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003597 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003598 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003599 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003600 i << 30,
3601 2);
3602}
3603
3604static int count_rmaps(struct kvm_vcpu *vcpu)
3605{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003606 struct kvm *kvm = vcpu->kvm;
3607 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003608 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003609 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003610
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003611 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003612 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003613 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003614 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003615 struct kvm_rmap_desc *d;
3616
3617 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003618 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003619
Izik Eidus290fc382007-09-27 14:11:22 +02003620 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003621 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003622 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003623 ++nmaps;
3624 continue;
3625 }
Izik Eidus290fc382007-09-27 14:11:22 +02003626 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003627 while (d) {
3628 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003629 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003630 ++nmaps;
3631 else
3632 break;
3633 d = d->more;
3634 }
3635 }
3636 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003637 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003638 return nmaps;
3639}
3640
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003641void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003642{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003643 unsigned long *rmapp;
3644 struct kvm_mmu_page *rev_sp;
3645 gfn_t gfn;
3646
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003647 if (is_writable_pte(*sptep)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003648 rev_sp = page_header(__pa(sptep));
Lai Jiangshan2032a932010-05-26 16:49:59 +08003649 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003650
3651 if (!gfn_to_memslot(kvm, gfn)) {
3652 if (!printk_ratelimit())
3653 return;
3654 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3655 audit_msg, gfn);
3656 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003657 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003658 rev_sp->gfn);
3659 dump_stack();
3660 return;
3661 }
3662
Lai Jiangshan2032a932010-05-26 16:49:59 +08003663 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003664 if (!*rmapp) {
3665 if (!printk_ratelimit())
3666 return;
3667 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3668 audit_msg, *sptep);
3669 dump_stack();
3670 }
3671 }
3672
3673}
3674
3675void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3676{
3677 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3678}
3679
3680static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3681{
Avi Kivity4db35312007-11-21 15:28:32 +02003682 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003683 int i;
3684
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003685 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003686 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003687
Avi Kivity4db35312007-11-21 15:28:32 +02003688 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003689 continue;
3690
3691 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3692 u64 ent = pt[i];
3693
3694 if (!(ent & PT_PRESENT_MASK))
3695 continue;
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003696 if (!is_writable_pte(ent))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003697 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003698 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003699 }
3700 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003701 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003702}
3703
3704static void audit_rmap(struct kvm_vcpu *vcpu)
3705{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003706 check_writable_mappings_rmap(vcpu);
3707 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003708}
3709
3710static void audit_write_protection(struct kvm_vcpu *vcpu)
3711{
Avi Kivity4db35312007-11-21 15:28:32 +02003712 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003713 struct kvm_memory_slot *slot;
3714 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003715 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003716 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003717
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003718 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003719 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003720 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003721 if (sp->unsync)
3722 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003723
Avi Kivitya1f4d3952010-06-21 11:44:20 +03003724 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003725 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003726
3727 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3728 while (spte) {
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003729 if (is_writable_pte(*spte))
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003730 printk(KERN_ERR "%s: (%s) shadow page has "
3731 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003732 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003733 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003734 spte = rmap_next(vcpu->kvm, rmapp, spte);
3735 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003736 }
3737}
3738
3739static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3740{
3741 int olddbg = dbg;
3742
3743 dbg = 0;
3744 audit_msg = msg;
3745 audit_rmap(vcpu);
3746 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003747 if (strcmp("pre pte write", audit_msg) != 0)
3748 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003749 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003750 dbg = olddbg;
3751}
3752
3753#endif