blob: d2dad65a45f8ca9007855fc448d69b4afe730c8c [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
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080052enum {
53 AUDIT_PRE_PAGE_FAULT,
54 AUDIT_POST_PAGE_FAULT,
55 AUDIT_PRE_PTE_WRITE,
56 AUDIT_POST_PTE_WRITE
57};
58
59char *audit_point_name[] = {
60 "pre page fault",
61 "post page fault",
62 "pre pte write",
63 "post pte write"
64};
65
Avi Kivity37a7d8b2007-01-05 16:36:56 -080066#undef MMU_DEBUG
67
Avi Kivity37a7d8b2007-01-05 16:36:56 -080068#ifdef MMU_DEBUG
69
70#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
71#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
72
73#else
74
75#define pgprintk(x...) do { } while (0)
76#define rmap_printk(x...) do { } while (0)
77
78#endif
79
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080080#ifdef MMU_DEBUG
Avi Kivity6ada8cc2008-06-22 16:45:24 +030081static int dbg = 0;
82module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080083#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Marcelo Tosatti582801a2008-09-23 13:18:41 -030085static int oos_shadow = 1;
86module_param(oos_shadow, bool, 0644);
87
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080088#ifndef MMU_DEBUG
89#define ASSERT(x) do { } while (0)
90#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080091#define ASSERT(x) \
92 if (!(x)) { \
93 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
94 __FILE__, __LINE__, #x); \
95 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080096#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Xiao Guangrong957ed9e2010-08-22 19:12:48 +080098#define PTE_PREFETCH_NUM 8
99
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100#define PT_FIRST_AVAIL_BITS_SHIFT 9
101#define PT64_SECOND_AVAIL_BITS_SHIFT 52
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103#define PT64_LEVEL_BITS 9
104
105#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400106 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107
108#define PT64_LEVEL_MASK(level) \
109 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
110
111#define PT64_INDEX(address, level)\
112 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
113
114
115#define PT32_LEVEL_BITS 10
116
117#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400118 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
120#define PT32_LEVEL_MASK(level) \
121 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200122#define PT32_LVL_OFFSET_MASK(level) \
123 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
124 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125
126#define PT32_INDEX(address, level)\
127 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
128
129
Avi Kivity27aba762007-03-09 13:04:31 +0200130#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define PT64_DIR_BASE_ADDR_MASK \
132 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200133#define PT64_LVL_ADDR_MASK(level) \
134 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
135 * PT64_LEVEL_BITS))) - 1))
136#define PT64_LVL_OFFSET_MASK(level) \
137 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
138 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139
140#define PT32_BASE_ADDR_MASK PAGE_MASK
141#define PT32_DIR_BASE_ADDR_MASK \
142 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200143#define PT32_LVL_ADDR_MASK(level) \
144 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
145 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146
Avi Kivity79539ce2007-11-21 02:06:21 +0200147#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
148 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800150#define RMAP_EXT 4
151
Avi Kivityfe135d22007-12-09 16:15:46 +0200152#define ACC_EXEC_MASK 1
153#define ACC_WRITE_MASK PT_WRITABLE_MASK
154#define ACC_USER_MASK PT_USER_MASK
155#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
156
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200157#include <trace/events/kvm.h>
158
Avi Kivity07420172009-07-06 12:21:32 +0300159#define CREATE_TRACE_POINTS
160#include "mmutrace.h"
161
Izik Eidus14032832009-09-23 21:47:17 +0300162#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
163
Avi Kivity135f8c22008-08-21 17:49:56 +0300164#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
165
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800166struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300167 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800168 struct kvm_rmap_desc *more;
169};
170
Avi Kivity2d111232008-12-25 14:39:47 +0200171struct kvm_shadow_walk_iterator {
172 u64 addr;
173 hpa_t shadow_addr;
174 int level;
175 u64 *sptep;
176 unsigned index;
177};
178
179#define for_each_shadow_entry(_vcpu, _addr, _walker) \
180 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
181 shadow_walk_okay(&(_walker)); \
182 shadow_walk_next(&(_walker)))
183
Xiao Guangrong1047df12010-06-11 21:35:15 +0800184typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300185
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186static struct kmem_cache *pte_chain_cache;
187static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300188static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700189static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300190
Avi Kivityc7addb92007-09-16 18:58:32 +0200191static u64 __read_mostly shadow_trap_nonpresent_pte;
192static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800193static u64 __read_mostly shadow_base_present_pte;
194static u64 __read_mostly shadow_nx_mask;
195static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
196static u64 __read_mostly shadow_user_mask;
197static u64 __read_mostly shadow_accessed_mask;
198static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200199
Dong, Eddie82725b22009-03-30 16:21:08 +0800200static inline u64 rsvd_bits(int s, int e)
201{
202 return ((1ULL << (e - s + 1)) - 1) << s;
203}
204
Avi Kivityc7addb92007-09-16 18:58:32 +0200205void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
206{
207 shadow_trap_nonpresent_pte = trap_pte;
208 shadow_notrap_nonpresent_pte = notrap_pte;
209}
210EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
211
Sheng Yang7b523452008-04-25 21:13:50 +0800212void kvm_mmu_set_base_ptes(u64 base_pte)
213{
214 shadow_base_present_pte = base_pte;
215}
216EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
217
218void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800219 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800220{
221 shadow_user_mask = user_mask;
222 shadow_accessed_mask = accessed_mask;
223 shadow_dirty_mask = dirty_mask;
224 shadow_nx_mask = nx_mask;
225 shadow_x_mask = x_mask;
226}
227EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
228
Avi Kivity3dbe1412010-05-12 11:48:18 +0300229static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200231 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232}
233
234static int is_cpuid_PSE36(void)
235{
236 return 1;
237}
238
Avi Kivity73b10872007-01-26 00:56:41 -0800239static int is_nx(struct kvm_vcpu *vcpu)
240{
Avi Kivityf6801df2010-01-21 15:31:50 +0200241 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800242}
243
Avi Kivityc7addb92007-09-16 18:58:32 +0200244static int is_shadow_present_pte(u64 pte)
245{
Avi Kivityc7addb92007-09-16 18:58:32 +0200246 return pte != shadow_trap_nonpresent_pte
247 && pte != shadow_notrap_nonpresent_pte;
248}
249
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300250static int is_large_pte(u64 pte)
251{
252 return pte & PT_PAGE_SIZE_MASK;
253}
254
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900255static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256{
257 return pte & PT_WRITABLE_MASK;
258}
259
Avi Kivity43a37952009-06-10 14:12:05 +0300260static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200261{
Avi Kivity439e2182009-06-10 12:56:54 +0300262 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200263}
264
Avi Kivity43a37952009-06-10 14:12:05 +0300265static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800266{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200267 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800268}
269
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300270static int is_last_spte(u64 pte, int level)
271{
272 if (level == PT_PAGE_TABLE_LEVEL)
273 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200274 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300275 return 1;
276 return 0;
277}
278
Anthony Liguori35149e22008-04-02 14:46:56 -0500279static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200280{
Anthony Liguori35149e22008-04-02 14:46:56 -0500281 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200282}
283
Avi Kivityda928522007-11-21 13:54:47 +0200284static gfn_t pse36_gfn_delta(u32 gpte)
285{
286 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
287
288 return (gpte & PT32_DIR_PSE36_MASK) << shift;
289}
290
Avi Kivityd555c332009-06-10 14:24:23 +0300291static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300292{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700293 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300294}
295
Avi Kivitya9221dd2010-06-06 14:48:06 +0300296static u64 __xchg_spte(u64 *sptep, u64 new_spte)
297{
298#ifdef CONFIG_X86_64
299 return xchg(sptep, new_spte);
300#else
301 u64 old_spte;
302
303 do {
304 old_spte = *sptep;
305 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
306
307 return old_spte;
308#endif
309}
310
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800311static bool spte_has_volatile_bits(u64 spte)
312{
313 if (!shadow_accessed_mask)
314 return false;
315
316 if (!is_shadow_present_pte(spte))
317 return false;
318
Xiao Guangrong41327792010-08-02 16:15:08 +0800319 if ((spte & shadow_accessed_mask) &&
320 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800321 return false;
322
323 return true;
324}
325
Xiao Guangrong41327792010-08-02 16:15:08 +0800326static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
327{
328 return (old_spte & bit_mask) && !(new_spte & bit_mask);
329}
330
Avi Kivityb79b93f2010-06-06 15:46:44 +0300331static void update_spte(u64 *sptep, u64 new_spte)
332{
Xiao Guangrong41327792010-08-02 16:15:08 +0800333 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300334
Xiao Guangrong41327792010-08-02 16:15:08 +0800335 WARN_ON(!is_rmap_spte(new_spte));
336
337 new_spte |= old_spte & shadow_dirty_mask;
338
339 mask = shadow_accessed_mask;
340 if (is_writable_pte(old_spte))
341 mask |= shadow_dirty_mask;
342
343 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300344 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800345 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300346 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800347
348 if (!shadow_accessed_mask)
349 return;
350
351 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
352 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
353 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
354 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300355}
356
Avi Kivitye2dec932007-01-05 16:36:54 -0800357static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300358 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800359{
360 void *obj;
361
362 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800363 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800364 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300365 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800366 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800367 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800368 cache->objects[cache->nobjs++] = obj;
369 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800370 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800371}
372
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800373static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
374 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800375{
376 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800377 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800378}
379
Avi Kivityc1158e62007-07-20 08:18:27 +0300380static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300381 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300382{
383 struct page *page;
384
385 if (cache->nobjs >= min)
386 return 0;
387 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300388 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300389 if (!page)
390 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300391 cache->objects[cache->nobjs++] = page_address(page);
392 }
393 return 0;
394}
395
396static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
397{
398 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300399 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300400}
401
Avi Kivity8c438502007-04-16 11:53:17 +0300402static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
403{
404 int r;
405
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800406 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300407 pte_chain_cache, 4);
408 if (r)
409 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800410 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800411 rmap_desc_cache, 4 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300412 if (r)
413 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800414 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300415 if (r)
416 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800417 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300418 mmu_page_header_cache, 4);
419out:
Avi Kivity8c438502007-04-16 11:53:17 +0300420 return r;
421}
422
Avi Kivity714b93d2007-01-05 16:36:53 -0800423static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
424{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800425 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
426 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800427 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800428 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
429 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800430}
431
432static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
433 size_t size)
434{
435 void *p;
436
437 BUG_ON(!mc->nobjs);
438 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800439 return p;
440}
441
Avi Kivity714b93d2007-01-05 16:36:53 -0800442static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
443{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800444 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800445 sizeof(struct kvm_pte_chain));
446}
447
Avi Kivity90cb0522007-07-17 13:04:56 +0300448static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800449{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800450 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800451}
452
453static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
454{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800455 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800456 sizeof(struct kvm_rmap_desc));
457}
458
Avi Kivity90cb0522007-07-17 13:04:56 +0300459static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800460{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800461 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800462}
463
Lai Jiangshan2032a932010-05-26 16:49:59 +0800464static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
465{
466 if (!sp->role.direct)
467 return sp->gfns[index];
468
469 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
470}
471
472static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
473{
474 if (sp->role.direct)
475 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
476 else
477 sp->gfns[index] = gfn;
478}
479
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800480/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481 * Return the pointer to the largepage write count for a given
482 * gfn, handling slots that are not large page aligned.
483 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200484static int *slot_largepage_idx(gfn_t gfn,
485 struct kvm_memory_slot *slot,
486 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300487{
488 unsigned long idx;
489
Joerg Roedel82855412010-07-01 16:00:11 +0200490 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
491 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Joerg Roedeld25797b2009-07-27 16:30:43 +0200492 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493}
494
495static void account_shadowed(struct kvm *kvm, gfn_t gfn)
496{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200497 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300498 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200499 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300500
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300501 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200502 for (i = PT_DIRECTORY_LEVEL;
503 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
504 write_count = slot_largepage_idx(gfn, slot, i);
505 *write_count += 1;
506 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300507}
508
509static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
510{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200511 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300512 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200513 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300514
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300515 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516 for (i = PT_DIRECTORY_LEVEL;
517 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200518 write_count = slot_largepage_idx(gfn, slot, i);
519 *write_count -= 1;
520 WARN_ON(*write_count < 0);
521 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300522}
523
Joerg Roedeld25797b2009-07-27 16:30:43 +0200524static int has_wrprotected_page(struct kvm *kvm,
525 gfn_t gfn,
526 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300527{
Izik Eidus28430992008-10-03 17:40:32 +0300528 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300529 int *largepage_idx;
530
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300531 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300532 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200533 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534 return *largepage_idx;
535 }
536
537 return 1;
538}
539
Joerg Roedeld25797b2009-07-27 16:30:43 +0200540static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300541{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100542 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200543 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300544
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100545 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300546
Joerg Roedeld25797b2009-07-27 16:30:43 +0200547 for (i = PT_PAGE_TABLE_LEVEL;
548 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
549 if (page_size >= KVM_HPAGE_SIZE(i))
550 ret = i;
551 else
552 break;
553 }
554
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300555 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300556}
557
Joerg Roedeld25797b2009-07-27 16:30:43 +0200558static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300559{
560 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800561 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300562
563 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
564 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200565 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300566
Joerg Roedeld25797b2009-07-27 16:30:43 +0200567 host_level = host_mapping_level(vcpu->kvm, large_gfn);
568
569 if (host_level == PT_PAGE_TABLE_LEVEL)
570 return host_level;
571
Sheng Yang878403b2010-01-05 19:02:29 +0800572 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
573 kvm_x86_ops->get_lpage_level() : host_level;
574
575 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200576 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
577 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200578
579 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300580}
581
582/*
Izik Eidus290fc382007-09-27 14:11:22 +0200583 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200584 */
585
Joerg Roedel44ad9942009-07-27 16:30:42 +0200586static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200587{
588 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300589 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200590
591 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200592 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300593 return &slot->rmap[gfn - slot->base_gfn];
594
Joerg Roedel82855412010-07-01 16:00:11 +0200595 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
596 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300597
Joerg Roedel44ad9942009-07-27 16:30:42 +0200598 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200599}
600
601/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800602 * Reverse mapping data structures:
603 *
Izik Eidus290fc382007-09-27 14:11:22 +0200604 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
605 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 *
Izik Eidus290fc382007-09-27 14:11:22 +0200607 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
608 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300609 *
610 * Returns the number of rmap entries before the spte was added or zero if
611 * the spte was not added.
612 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200614static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615{
Avi Kivity4db35312007-11-21 15:28:32 +0200616 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800617 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200618 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300619 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620
Avi Kivity43a37952009-06-10 14:12:05 +0300621 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300622 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200623 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800624 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200625 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200626 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800627 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200628 *rmapp = (unsigned long)spte;
629 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800630 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800631 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300632 desc->sptes[0] = (u64 *)*rmapp;
633 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200634 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800635 } else {
636 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200637 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300638 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300640 count += RMAP_EXT;
641 }
Avi Kivityd555c332009-06-10 14:24:23 +0300642 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800643 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800644 desc = desc->more;
645 }
Avi Kivityd555c332009-06-10 14:24:23 +0300646 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800647 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300648 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800649 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300650 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800651}
652
Izik Eidus290fc382007-09-27 14:11:22 +0200653static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800654 struct kvm_rmap_desc *desc,
655 int i,
656 struct kvm_rmap_desc *prev_desc)
657{
658 int j;
659
Avi Kivityd555c332009-06-10 14:24:23 +0300660 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800661 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300662 desc->sptes[i] = desc->sptes[j];
663 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800664 if (j != 0)
665 return;
666 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300667 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800668 else
669 if (prev_desc)
670 prev_desc->more = desc->more;
671 else
Izik Eidus290fc382007-09-27 14:11:22 +0200672 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300673 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800674}
675
Izik Eidus290fc382007-09-27 14:11:22 +0200676static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800677{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800678 struct kvm_rmap_desc *desc;
679 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200680 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800681 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200682 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800683 int i;
684
Avi Kivity4db35312007-11-21 15:28:32 +0200685 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800686 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
687 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200688 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800689 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800690 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200691 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800692 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200693 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800694 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800695 BUG();
696 }
Izik Eidus290fc382007-09-27 14:11:22 +0200697 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800698 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800699 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200700 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800701 prev_desc = NULL;
702 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300703 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
704 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200705 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800706 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800707 prev_desc);
708 return;
709 }
710 prev_desc = desc;
711 desc = desc->more;
712 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800713 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800714 BUG();
715 }
716}
717
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800718static void set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300719{
Avi Kivityce061862010-06-06 14:38:12 +0300720 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800721 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300722
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800723 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800724 __set_spte(sptep, new_spte);
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800725 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800726 old_spte = __xchg_spte(sptep, new_spte);
727
Avi Kivitya9221dd2010-06-06 14:48:06 +0300728 if (!is_rmap_spte(old_spte))
Avi Kivityce061862010-06-06 14:38:12 +0300729 return;
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800730
Avi Kivitya9221dd2010-06-06 14:48:06 +0300731 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800732 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300733 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800734 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300735 kvm_set_pfn_dirty(pfn);
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800736}
737
738static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
739{
740 set_spte_track_bits(sptep, new_spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300741 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300742}
743
Izik Eidus98348e92007-10-16 14:42:30 +0200744static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800745{
Avi Kivity374cbac2007-01-05 16:36:43 -0800746 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200747 u64 *prev_spte;
748 int i;
749
750 if (!*rmapp)
751 return NULL;
752 else if (!(*rmapp & 1)) {
753 if (!spte)
754 return (u64 *)*rmapp;
755 return NULL;
756 }
757 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200758 prev_spte = NULL;
759 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300760 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200761 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300762 return desc->sptes[i];
763 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200764 }
765 desc = desc->more;
766 }
767 return NULL;
768}
769
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200770static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200771{
Izik Eidus290fc382007-09-27 14:11:22 +0200772 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800773 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200774 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800775
Joerg Roedel44ad9942009-07-27 16:30:42 +0200776 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800777
Izik Eidus98348e92007-10-16 14:42:30 +0200778 spte = rmap_next(kvm, rmapp, NULL);
779 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800780 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800781 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800782 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900783 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300784 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800785 write_protected = 1;
786 }
Izik Eidus9647c142007-10-16 14:43:46 +0200787 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800788 }
Izik Eidus855149a2008-03-20 18:17:24 +0200789
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300790 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200791 for (i = PT_DIRECTORY_LEVEL;
792 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
793 rmapp = gfn_to_rmap(kvm, gfn, i);
794 spte = rmap_next(kvm, rmapp, NULL);
795 while (spte) {
796 BUG_ON(!spte);
797 BUG_ON(!(*spte & PT_PRESENT_MASK));
798 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
799 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900800 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300801 drop_spte(kvm, spte,
802 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200803 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200804 spte = NULL;
805 write_protected = 1;
806 }
807 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300808 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300809 }
810
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200811 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800812}
813
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000814static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
815 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816{
817 u64 *spte;
818 int need_tlb_flush = 0;
819
820 while ((spte = rmap_next(kvm, rmapp, NULL))) {
821 BUG_ON(!(*spte & PT_PRESENT_MASK));
822 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300823 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200824 need_tlb_flush = 1;
825 }
826 return need_tlb_flush;
827}
828
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000829static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
830 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300831{
832 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800833 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300834 pte_t *ptep = (pte_t *)data;
835 pfn_t new_pfn;
836
837 WARN_ON(pte_huge(*ptep));
838 new_pfn = pte_pfn(*ptep);
839 spte = rmap_next(kvm, rmapp, NULL);
840 while (spte) {
841 BUG_ON(!is_shadow_present_pte(*spte));
842 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
843 need_flush = 1;
844 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300845 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300846 spte = rmap_next(kvm, rmapp, NULL);
847 } else {
848 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
849 new_spte |= (u64)new_pfn << PAGE_SHIFT;
850
851 new_spte &= ~PT_WRITABLE_MASK;
852 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300853 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800854 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300855 spte = rmap_next(kvm, rmapp, spte);
856 }
857 }
858 if (need_flush)
859 kvm_flush_remote_tlbs(kvm);
860
861 return 0;
862}
863
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000864static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
865 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300866 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000867 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200868{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200869 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200870 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200871 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200872 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200873
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800874 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200875
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200876 for (i = 0; i < slots->nmemslots; i++) {
877 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200878 unsigned long start = memslot->userspace_addr;
879 unsigned long end;
880
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200881 end = start + (memslot->npages << PAGE_SHIFT);
882 if (hva >= start && hva < end) {
883 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200884
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200885 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200886
887 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200888 unsigned long idx;
889 int sh;
890
891 sh = KVM_HPAGE_GFN_SHIFT(PT_DIRECTORY_LEVEL+j);
892 idx = ((memslot->base_gfn+gfn_offset) >> sh) -
893 (memslot->base_gfn >> sh);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200894 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300895 &memslot->lpage_info[j][idx].rmap_pde,
896 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200897 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200898 trace_kvm_age_page(hva, memslot, ret);
899 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200900 }
901 }
902
903 return retval;
904}
905
906int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
907{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300908 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200909}
910
Izik Eidus3da0dd42009-09-23 21:47:18 +0300911void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
912{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000913 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300914}
915
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000916static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
917 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200918{
919 u64 *spte;
920 int young = 0;
921
Rik van Riel6316e1c2010-02-03 16:11:03 -0500922 /*
923 * Emulate the accessed bit for EPT, by checking if this page has
924 * an EPT mapping, and clearing it if it does. On the next access,
925 * a new EPT mapping will be established.
926 * This has some overhead, but not as much as the cost of swapping
927 * out actively used pages or breaking up actively used hugepages.
928 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800929 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500930 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800931
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200932 spte = rmap_next(kvm, rmapp, NULL);
933 while (spte) {
934 int _young;
935 u64 _spte = *spte;
936 BUG_ON(!(_spte & PT_PRESENT_MASK));
937 _young = _spte & PT_ACCESSED_MASK;
938 if (_young) {
939 young = 1;
940 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
941 }
942 spte = rmap_next(kvm, rmapp, spte);
943 }
944 return young;
945}
946
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300947#define RMAP_RECYCLE_THRESHOLD 1000
948
Joerg Roedel852e3c12009-07-27 16:30:44 +0200949static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300950{
951 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200952 struct kvm_mmu_page *sp;
953
954 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300955
Joerg Roedel852e3c12009-07-27 16:30:44 +0200956 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300957
Izik Eidus3da0dd42009-09-23 21:47:18 +0300958 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300959 kvm_flush_remote_tlbs(vcpu->kvm);
960}
961
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200962int kvm_age_hva(struct kvm *kvm, unsigned long hva)
963{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300964 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200965}
966
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800967#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300968static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969{
Avi Kivity139bdb22007-01-05 16:36:50 -0800970 u64 *pos;
971 u64 *end;
972
Avi Kivity47ad8e62007-05-06 15:50:58 +0300973 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300974 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800975 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800976 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800978 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 return 1;
980}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800981#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Dave Hansen45221ab2010-08-19 18:11:37 -0700983/*
984 * This value is the sum of all of the kvm instances's
985 * kvm->arch.n_used_mmu_pages values. We need a global,
986 * aggregate version in order to make the slab shrinker
987 * faster
988 */
989static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
990{
991 kvm->arch.n_used_mmu_pages += nr;
992 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
993}
994
Avi Kivity4db35312007-11-21 15:28:32 +0200995static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800996{
Avi Kivity4db35312007-11-21 15:28:32 +0200997 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800998 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200999 list_del(&sp->link);
1000 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001001 if (!sp->role.direct)
1002 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001003 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001004 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001005}
1006
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007static unsigned kvm_page_table_hashfn(gfn_t gfn)
1008{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001009 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001010}
1011
Avi Kivity25c0de22007-01-05 16:36:42 -08001012static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001013 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014{
Avi Kivity4db35312007-11-21 15:28:32 +02001015 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001017 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1018 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001019 if (!direct)
1020 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1021 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001022 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001023 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001024 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001025 sp->multimapped = 0;
1026 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001027 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001028 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029}
1030
Avi Kivity714b93d2007-01-05 16:36:53 -08001031static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001032 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001033{
1034 struct kvm_pte_chain *pte_chain;
1035 struct hlist_node *node;
1036 int i;
1037
1038 if (!parent_pte)
1039 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001040 if (!sp->multimapped) {
1041 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001042
1043 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001044 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001045 return;
1046 }
Avi Kivity4db35312007-11-21 15:28:32 +02001047 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001048 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001049 INIT_HLIST_HEAD(&sp->parent_ptes);
1050 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001051 pte_chain->parent_ptes[0] = old;
1052 }
Avi Kivity4db35312007-11-21 15:28:32 +02001053 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001054 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1055 continue;
1056 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1057 if (!pte_chain->parent_ptes[i]) {
1058 pte_chain->parent_ptes[i] = parent_pte;
1059 return;
1060 }
1061 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001062 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001063 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001064 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001065 pte_chain->parent_ptes[0] = parent_pte;
1066}
1067
Avi Kivity4db35312007-11-21 15:28:32 +02001068static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001069 u64 *parent_pte)
1070{
1071 struct kvm_pte_chain *pte_chain;
1072 struct hlist_node *node;
1073 int i;
1074
Avi Kivity4db35312007-11-21 15:28:32 +02001075 if (!sp->multimapped) {
1076 BUG_ON(sp->parent_pte != parent_pte);
1077 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001078 return;
1079 }
Avi Kivity4db35312007-11-21 15:28:32 +02001080 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001081 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1082 if (!pte_chain->parent_ptes[i])
1083 break;
1084 if (pte_chain->parent_ptes[i] != parent_pte)
1085 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001086 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1087 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001088 pte_chain->parent_ptes[i]
1089 = pte_chain->parent_ptes[i + 1];
1090 ++i;
1091 }
1092 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001093 if (i == 0) {
1094 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001095 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001096 if (hlist_empty(&sp->parent_ptes)) {
1097 sp->multimapped = 0;
1098 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001099 }
1100 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001101 return;
1102 }
1103 BUG();
1104}
1105
Xiao Guangrong6b184932010-04-16 21:29:17 +08001106static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001107{
1108 struct kvm_pte_chain *pte_chain;
1109 struct hlist_node *node;
1110 struct kvm_mmu_page *parent_sp;
1111 int i;
1112
1113 if (!sp->multimapped && sp->parent_pte) {
1114 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001115 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001116 return;
1117 }
1118
1119 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1120 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001121 u64 *spte = pte_chain->parent_ptes[i];
1122
1123 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001125 parent_sp = page_header(__pa(spte));
1126 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001127 }
1128}
1129
Xiao Guangrong1047df12010-06-11 21:35:15 +08001130static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001131static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001132{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001133 mmu_parent_walk(sp, mark_unsync);
1134}
1135
1136static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1137{
1138 unsigned int index;
1139
1140 index = spte - sp->spt;
1141 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1142 return;
1143 if (sp->unsync_children++)
1144 return;
1145 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001146}
1147
Avi Kivityd761a502008-05-29 14:55:03 +03001148static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1149 struct kvm_mmu_page *sp)
1150{
1151 int i;
1152
1153 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1154 sp->spt[i] = shadow_trap_nonpresent_pte;
1155}
1156
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001157static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001158 struct kvm_mmu_page *sp, bool clear_unsync)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001159{
1160 return 1;
1161}
1162
Marcelo Tosattia7052892008-09-23 13:18:35 -03001163static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1164{
1165}
1166
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001167#define KVM_PAGE_ARRAY_NR 16
1168
1169struct kvm_mmu_pages {
1170 struct mmu_page_and_offset {
1171 struct kvm_mmu_page *sp;
1172 unsigned int idx;
1173 } page[KVM_PAGE_ARRAY_NR];
1174 unsigned int nr;
1175};
1176
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001177#define for_each_unsync_children(bitmap, idx) \
1178 for (idx = find_first_bit(bitmap, 512); \
1179 idx < 512; \
1180 idx = find_next_bit(bitmap, 512, idx+1))
1181
Hannes Edercded19f2009-02-21 02:19:13 +01001182static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1183 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001184{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001185 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001186
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001187 if (sp->unsync)
1188 for (i=0; i < pvec->nr; i++)
1189 if (pvec->page[i].sp == sp)
1190 return 0;
1191
1192 pvec->page[pvec->nr].sp = sp;
1193 pvec->page[pvec->nr].idx = idx;
1194 pvec->nr++;
1195 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1196}
1197
1198static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1199 struct kvm_mmu_pages *pvec)
1200{
1201 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001202
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001203 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001204 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001205 u64 ent = sp->spt[i];
1206
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001207 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1208 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001210 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001211
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001212 if (child->unsync_children) {
1213 if (mmu_pages_add(pvec, child, i))
1214 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001216 ret = __mmu_unsync_walk(child, pvec);
1217 if (!ret)
1218 goto clear_child_bitmap;
1219 else if (ret > 0)
1220 nr_unsync_leaf += ret;
1221 else
1222 return ret;
1223 } else if (child->unsync) {
1224 nr_unsync_leaf++;
1225 if (mmu_pages_add(pvec, child, i))
1226 return -ENOSPC;
1227 } else
1228 goto clear_child_bitmap;
1229
1230 continue;
1231
1232clear_child_bitmap:
1233 __clear_bit(i, sp->unsync_child_bitmap);
1234 sp->unsync_children--;
1235 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001236 }
1237
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001238
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001239 return nr_unsync_leaf;
1240}
1241
1242static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1243 struct kvm_mmu_pages *pvec)
1244{
1245 if (!sp->unsync_children)
1246 return 0;
1247
1248 mmu_pages_add(pvec, sp, 0);
1249 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250}
1251
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001252static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1253{
1254 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001255 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256 sp->unsync = 0;
1257 --kvm->stat.mmu_unsync;
1258}
1259
Xiao Guangrong77758342010-06-04 21:53:54 +08001260static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1261 struct list_head *invalid_list);
1262static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1263 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001264
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001265#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1266 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001267 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1268 if ((sp)->gfn != (gfn)) {} else
1269
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001270#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1271 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001272 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1273 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1274 (sp)->role.invalid) {} else
1275
Xiao Guangrongf918b442010-06-11 21:30:36 +08001276/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001277static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001278 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001279{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001280 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001281 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001282 return 1;
1283 }
1284
Xiao Guangrongf918b442010-06-11 21:30:36 +08001285 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001286 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001287
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001288 if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001289 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001290 return 1;
1291 }
1292
1293 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001294 return 0;
1295}
1296
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001297static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1298 struct kvm_mmu_page *sp)
1299{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001300 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001301 int ret;
1302
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001303 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001304 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001305 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1306
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001307 return ret;
1308}
1309
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001310static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1311 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001312{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001313 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001314}
1315
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001316/* @gfn should be write-protected at the call site */
1317static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1318{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001319 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001320 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001321 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001322 bool flush = false;
1323
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001324 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001325 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001326 continue;
1327
1328 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1329 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001330 (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001331 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001332 continue;
1333 }
1334 kvm_unlink_unsync_page(vcpu->kvm, s);
1335 flush = true;
1336 }
1337
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001338 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001339 if (flush)
1340 kvm_mmu_flush_tlb(vcpu);
1341}
1342
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001343struct mmu_page_path {
1344 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1345 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001346};
1347
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001348#define for_each_sp(pvec, sp, parents, i) \
1349 for (i = mmu_pages_next(&pvec, &parents, -1), \
1350 sp = pvec.page[i].sp; \
1351 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1352 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001353
Hannes Edercded19f2009-02-21 02:19:13 +01001354static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1355 struct mmu_page_path *parents,
1356 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001357{
1358 int n;
1359
1360 for (n = i+1; n < pvec->nr; n++) {
1361 struct kvm_mmu_page *sp = pvec->page[n].sp;
1362
1363 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1364 parents->idx[0] = pvec->page[n].idx;
1365 return n;
1366 }
1367
1368 parents->parent[sp->role.level-2] = sp;
1369 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1370 }
1371
1372 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373}
1374
Hannes Edercded19f2009-02-21 02:19:13 +01001375static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001376{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001377 struct kvm_mmu_page *sp;
1378 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001379
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001380 do {
1381 unsigned int idx = parents->idx[level];
1382
1383 sp = parents->parent[level];
1384 if (!sp)
1385 return;
1386
1387 --sp->unsync_children;
1388 WARN_ON((int)sp->unsync_children < 0);
1389 __clear_bit(idx, sp->unsync_child_bitmap);
1390 level++;
1391 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1392}
1393
1394static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1395 struct mmu_page_path *parents,
1396 struct kvm_mmu_pages *pvec)
1397{
1398 parents->parent[parent->role.level-1] = NULL;
1399 pvec->nr = 0;
1400}
1401
1402static void mmu_sync_children(struct kvm_vcpu *vcpu,
1403 struct kvm_mmu_page *parent)
1404{
1405 int i;
1406 struct kvm_mmu_page *sp;
1407 struct mmu_page_path parents;
1408 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001409 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001410
1411 kvm_mmu_pages_init(parent, &parents, &pages);
1412 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001413 int protected = 0;
1414
1415 for_each_sp(pages, sp, parents, i)
1416 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1417
1418 if (protected)
1419 kvm_flush_remote_tlbs(vcpu->kvm);
1420
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001421 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001422 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001423 mmu_pages_clear_parents(&parents);
1424 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001425 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001426 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001427 kvm_mmu_pages_init(parent, &parents, &pages);
1428 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001429}
1430
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001431static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1432 gfn_t gfn,
1433 gva_t gaddr,
1434 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001435 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001436 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001437 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001438{
1439 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001440 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001441 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001442 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001443 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001444
Avi Kivitya770f6f2008-12-21 19:20:09 +02001445 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001446 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001447 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001448 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001449 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001450 role.access = access;
Gui Jianfengb66d8002010-05-31 17:11:39 +08001451 if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001452 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1453 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1454 role.quadrant = quadrant;
1455 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001456 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001457 if (!need_sync && sp->unsync)
1458 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001459
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001460 if (sp->role.word != role.word)
1461 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001462
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001463 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1464 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001465
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001466 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1467 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001468 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001469 kvm_mmu_mark_parents_unsync(sp);
1470 } else if (sp->unsync)
1471 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001472
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001473 trace_kvm_mmu_get_page(sp, false);
1474 return sp;
1475 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001476 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001477 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001478 if (!sp)
1479 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001480 sp->gfn = gfn;
1481 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001482 hlist_add_head(&sp->hash_link,
1483 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001484 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001485 if (rmap_write_protect(vcpu->kvm, gfn))
1486 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001487 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1488 kvm_sync_pages(vcpu, gfn);
1489
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001490 account_shadowed(vcpu->kvm, gfn);
1491 }
Avi Kivity131d8272008-05-29 14:56:28 +03001492 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1493 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1494 else
1495 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001496 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001497 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001498}
1499
Avi Kivity2d111232008-12-25 14:39:47 +02001500static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1501 struct kvm_vcpu *vcpu, u64 addr)
1502{
1503 iterator->addr = addr;
1504 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1505 iterator->level = vcpu->arch.mmu.shadow_root_level;
1506 if (iterator->level == PT32E_ROOT_LEVEL) {
1507 iterator->shadow_addr
1508 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1509 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1510 --iterator->level;
1511 if (!iterator->shadow_addr)
1512 iterator->level = 0;
1513 }
1514}
1515
1516static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1517{
1518 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1519 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001520
1521 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1522 if (is_large_pte(*iterator->sptep))
1523 return false;
1524
Avi Kivity2d111232008-12-25 14:39:47 +02001525 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1526 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1527 return true;
1528}
1529
1530static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1531{
1532 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1533 --iterator->level;
1534}
1535
Avi Kivity32ef26a2010-07-13 14:27:04 +03001536static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1537{
1538 u64 spte;
1539
1540 spte = __pa(sp->spt)
1541 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1542 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001543 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001544}
1545
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001546static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1547{
1548 if (is_large_pte(*sptep)) {
1549 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1550 kvm_flush_remote_tlbs(vcpu->kvm);
1551 }
1552}
1553
Avi Kivitya357bd22010-07-13 14:27:07 +03001554static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1555 unsigned direct_access)
1556{
1557 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1558 struct kvm_mmu_page *child;
1559
1560 /*
1561 * For the direct sp, if the guest pte's dirty bit
1562 * changed form clean to dirty, it will corrupt the
1563 * sp's access: allow writable in the read-only sp,
1564 * so we should update the spte at this point to get
1565 * a new sp with the correct access.
1566 */
1567 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1568 if (child->role.access == direct_access)
1569 return;
1570
1571 mmu_page_remove_parent_pte(child, sptep);
1572 __set_spte(sptep, shadow_trap_nonpresent_pte);
1573 kvm_flush_remote_tlbs(vcpu->kvm);
1574 }
1575}
1576
Avi Kivity90cb0522007-07-17 13:04:56 +03001577static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001578 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001579{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001580 unsigned i;
1581 u64 *pt;
1582 u64 ent;
1583
Avi Kivity4db35312007-11-21 15:28:32 +02001584 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001585
Avi Kivity697fe2e2007-01-05 16:36:46 -08001586 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1587 ent = pt[i];
1588
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001589 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001590 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001591 ent &= PT64_BASE_ADDR_MASK;
1592 mmu_page_remove_parent_pte(page_header(ent),
1593 &pt[i]);
1594 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001595 if (is_large_pte(ent))
1596 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001597 drop_spte(kvm, &pt[i],
1598 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001599 }
1600 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001601 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001602 }
Avi Kivitya4360362007-01-05 16:36:45 -08001603}
1604
Avi Kivity4db35312007-11-21 15:28:32 +02001605static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001606{
Avi Kivity4db35312007-11-21 15:28:32 +02001607 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001608}
1609
Avi Kivity12b7d282007-09-23 14:10:49 +02001610static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1611{
1612 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001613 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001614
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001615 kvm_for_each_vcpu(i, vcpu, kvm)
1616 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001617}
1618
Avi Kivity31aa2b42008-07-11 17:59:46 +03001619static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001620{
1621 u64 *parent_pte;
1622
Avi Kivity4db35312007-11-21 15:28:32 +02001623 while (sp->multimapped || sp->parent_pte) {
1624 if (!sp->multimapped)
1625 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001626 else {
1627 struct kvm_pte_chain *chain;
1628
Avi Kivity4db35312007-11-21 15:28:32 +02001629 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001630 struct kvm_pte_chain, link);
1631 parent_pte = chain->parent_ptes[0];
1632 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001633 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001634 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001635 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001636 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001637}
1638
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001639static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001640 struct kvm_mmu_page *parent,
1641 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001642{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001643 int i, zapped = 0;
1644 struct mmu_page_path parents;
1645 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001646
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001647 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001648 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001649
1650 kvm_mmu_pages_init(parent, &parents, &pages);
1651 while (mmu_unsync_walk(parent, &pages)) {
1652 struct kvm_mmu_page *sp;
1653
1654 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001655 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001656 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001657 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001658 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001659 kvm_mmu_pages_init(parent, &parents, &pages);
1660 }
1661
1662 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001663}
1664
Xiao Guangrong77758342010-06-04 21:53:54 +08001665static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1666 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001667{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001668 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001669
Xiao Guangrong77758342010-06-04 21:53:54 +08001670 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001671 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001672 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001673 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001674 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001675 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001676 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001677 if (sp->unsync)
1678 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001679 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001680 /* Count self */
1681 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001682 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001683 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001684 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001685 kvm_reload_remote_mmus(kvm);
1686 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001687
1688 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001689 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001690 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001691}
1692
Xiao Guangrong77758342010-06-04 21:53:54 +08001693static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1694 struct list_head *invalid_list)
1695{
1696 struct kvm_mmu_page *sp;
1697
1698 if (list_empty(invalid_list))
1699 return;
1700
1701 kvm_flush_remote_tlbs(kvm);
1702
1703 do {
1704 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1705 WARN_ON(!sp->role.invalid || sp->root_count);
1706 kvm_mmu_free_page(kvm, sp);
1707 } while (!list_empty(invalid_list));
1708
1709}
1710
Izik Eidus82ce2c92007-10-02 18:52:55 +02001711/*
1712 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001713 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001714 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001715void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001716{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001717 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001718 /*
1719 * If we set the number of mmu pages to be smaller be than the
1720 * number of actived pages , we must to free some mmu pages before we
1721 * change the value
1722 */
1723
Dave Hansen49d5ca22010-08-19 18:11:28 -07001724 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1725 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001726 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001727 struct kvm_mmu_page *page;
1728
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001729 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001730 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001731 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1732 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001733 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001734 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001735 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001736
Dave Hansen49d5ca22010-08-19 18:11:28 -07001737 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001738}
1739
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001740static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001741{
Avi Kivity4db35312007-11-21 15:28:32 +02001742 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001743 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001744 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001745 int r;
1746
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001747 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001748 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001749
1750 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001751 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001752 sp->role.word);
1753 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001754 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001755 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001756 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001757 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001758}
1759
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001760static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001761{
Avi Kivity4db35312007-11-21 15:28:32 +02001762 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001763 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001764 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001765
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001766 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001767 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001768 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001769 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001770 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001771 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001772}
1773
Avi Kivity38c335f2007-11-21 14:20:22 +02001774static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001776 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001777 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778
Sheng Yang291f26b2008-10-16 17:30:57 +08001779 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780}
1781
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001782static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1783{
1784 int i;
1785 u64 *pt = sp->spt;
1786
1787 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1788 return;
1789
1790 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1791 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001792 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001793 }
1794}
1795
Sheng Yang74be52e2008-10-09 16:01:56 +08001796/*
1797 * The function is based on mtrr_type_lookup() in
1798 * arch/x86/kernel/cpu/mtrr/generic.c
1799 */
1800static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1801 u64 start, u64 end)
1802{
1803 int i;
1804 u64 base, mask;
1805 u8 prev_match, curr_match;
1806 int num_var_ranges = KVM_NR_VAR_MTRR;
1807
1808 if (!mtrr_state->enabled)
1809 return 0xFF;
1810
1811 /* Make end inclusive end, instead of exclusive */
1812 end--;
1813
1814 /* Look in fixed ranges. Just return the type as per start */
1815 if (mtrr_state->have_fixed && (start < 0x100000)) {
1816 int idx;
1817
1818 if (start < 0x80000) {
1819 idx = 0;
1820 idx += (start >> 16);
1821 return mtrr_state->fixed_ranges[idx];
1822 } else if (start < 0xC0000) {
1823 idx = 1 * 8;
1824 idx += ((start - 0x80000) >> 14);
1825 return mtrr_state->fixed_ranges[idx];
1826 } else if (start < 0x1000000) {
1827 idx = 3 * 8;
1828 idx += ((start - 0xC0000) >> 12);
1829 return mtrr_state->fixed_ranges[idx];
1830 }
1831 }
1832
1833 /*
1834 * Look in variable ranges
1835 * Look of multiple ranges matching this address and pick type
1836 * as per MTRR precedence
1837 */
1838 if (!(mtrr_state->enabled & 2))
1839 return mtrr_state->def_type;
1840
1841 prev_match = 0xFF;
1842 for (i = 0; i < num_var_ranges; ++i) {
1843 unsigned short start_state, end_state;
1844
1845 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1846 continue;
1847
1848 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1849 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1850 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1851 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1852
1853 start_state = ((start & mask) == (base & mask));
1854 end_state = ((end & mask) == (base & mask));
1855 if (start_state != end_state)
1856 return 0xFE;
1857
1858 if ((start & mask) != (base & mask))
1859 continue;
1860
1861 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1862 if (prev_match == 0xFF) {
1863 prev_match = curr_match;
1864 continue;
1865 }
1866
1867 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1868 curr_match == MTRR_TYPE_UNCACHABLE)
1869 return MTRR_TYPE_UNCACHABLE;
1870
1871 if ((prev_match == MTRR_TYPE_WRBACK &&
1872 curr_match == MTRR_TYPE_WRTHROUGH) ||
1873 (prev_match == MTRR_TYPE_WRTHROUGH &&
1874 curr_match == MTRR_TYPE_WRBACK)) {
1875 prev_match = MTRR_TYPE_WRTHROUGH;
1876 curr_match = MTRR_TYPE_WRTHROUGH;
1877 }
1878
1879 if (prev_match != curr_match)
1880 return MTRR_TYPE_UNCACHABLE;
1881 }
1882
1883 if (prev_match != 0xFF)
1884 return prev_match;
1885
1886 return mtrr_state->def_type;
1887}
1888
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001889u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001890{
1891 u8 mtrr;
1892
1893 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1894 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1895 if (mtrr == 0xfe || mtrr == 0xff)
1896 mtrr = MTRR_TYPE_WRBACK;
1897 return mtrr;
1898}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001899EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001900
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001901static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001902{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001903 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001904 ++vcpu->kvm->stat.mmu_unsync;
1905 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001906
Xiao Guangrong6b184932010-04-16 21:29:17 +08001907 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001908 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001909}
1910
1911static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1912{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001913 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001914 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001915
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001916 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001917 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001918 continue;
1919 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1920 __kvm_unsync_page(vcpu, s);
1921 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001922}
1923
1924static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1925 bool can_unsync)
1926{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001927 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001928 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001929 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001930
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001931 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001932 if (!can_unsync)
1933 return 1;
1934
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001935 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001936 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001937
1938 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001939 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001940 return 1;
1941 need_unsync = true;
1942 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001943 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001944 if (need_unsync)
1945 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001946 return 0;
1947}
1948
Avi Kivityd555c332009-06-10 14:24:23 +03001949static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001950 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001951 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001952 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001953 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001954{
1955 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001956 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001957
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001958 /*
1959 * We don't set the accessed bit, since we sometimes want to see
1960 * whether the guest actually used the pte (in order to detect
1961 * demand paging).
1962 */
Xiao Guangrong41327792010-08-02 16:15:08 +08001963 spte = shadow_base_present_pte;
Avi Kivity947da532008-03-18 11:05:52 +02001964 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001965 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001966 if (!dirty)
1967 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001968 if (pte_access & ACC_EXEC_MASK)
1969 spte |= shadow_x_mask;
1970 else
1971 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001972 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001973 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001974 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001975 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001976 if (tdp_enabled)
1977 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1978 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001979
Izik Eidus14032832009-09-23 21:47:17 +03001980 if (reset_host_protection)
1981 spte |= SPTE_HOST_WRITEABLE;
1982
Anthony Liguori35149e22008-04-02 14:46:56 -05001983 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001984
1985 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001986 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1987 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001988
Joerg Roedel852e3c12009-07-27 16:30:44 +02001989 if (level > PT_PAGE_TABLE_LEVEL &&
1990 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001991 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001992 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1993 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001994 }
1995
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001996 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001997
Avi Kivity69325a12010-05-27 14:35:58 +03001998 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1999 spte &= ~PT_USER_MASK;
2000
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002001 /*
2002 * Optimization: for pte sync, if spte was writable the hash
2003 * lookup is unnecessary (and expensive). Write protection
2004 * is responsibility of mmu_get_page / kvm_sync_page.
2005 * Same reasoning can be applied to dirty page accounting.
2006 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002007 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002008 goto set_pte;
2009
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002010 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002011 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002012 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002013 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002014 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002015 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002016 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002017 }
2018 }
2019
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002020 if (pte_access & ACC_WRITE_MASK)
2021 mark_page_dirty(vcpu->kvm, gfn);
2022
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002023set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002024 update_spte(sptep, spte);
Avi Kivitybe38d272010-06-06 14:31:27 +03002025done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002026 return ret;
2027}
2028
Avi Kivityd555c332009-06-10 14:24:23 +03002029static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002030 unsigned pt_access, unsigned pte_access,
2031 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002032 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002033 pfn_t pfn, bool speculative,
2034 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002035{
2036 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002037 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002038
2039 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002040 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002041 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002042 write_fault, user_fault, gfn);
2043
Avi Kivityd555c332009-06-10 14:24:23 +03002044 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002045 /*
2046 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2047 * the parent of the now unreachable PTE.
2048 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002049 if (level > PT_PAGE_TABLE_LEVEL &&
2050 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002051 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002052 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002053
2054 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002055 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002056 __set_spte(sptep, shadow_trap_nonpresent_pte);
2057 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002058 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002059 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002060 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002061 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002062 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002063 } else
2064 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002065 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002066
Avi Kivityd555c332009-06-10 14:24:23 +03002067 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002068 dirty, level, gfn, pfn, speculative, true,
2069 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002070 if (write_fault)
2071 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002072 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002073 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002074
Avi Kivityd555c332009-06-10 14:24:23 +03002075 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002076 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002077 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002078 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2079 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002080 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002081 ++vcpu->kvm->stat.lpages;
2082
Avi Kivityd555c332009-06-10 14:24:23 +03002083 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002084 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002085 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002086 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002087 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002088 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002089 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002090 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002091 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002092 vcpu->arch.last_pte_gfn = gfn;
2093 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002094}
2095
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2097{
2098}
2099
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002100static struct kvm_memory_slot *
2101pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2102{
2103 struct kvm_memory_slot *slot;
2104
2105 slot = gfn_to_memslot(vcpu->kvm, gfn);
2106 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2107 (no_dirty_log && slot->dirty_bitmap))
2108 slot = NULL;
2109
2110 return slot;
2111}
2112
2113static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2114 bool no_dirty_log)
2115{
2116 struct kvm_memory_slot *slot;
2117 unsigned long hva;
2118
2119 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2120 if (!slot) {
2121 get_page(bad_page);
2122 return page_to_pfn(bad_page);
2123 }
2124
2125 hva = gfn_to_hva_memslot(slot, gfn);
2126
2127 return hva_to_pfn_atomic(vcpu->kvm, hva);
2128}
2129
2130static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2131 struct kvm_mmu_page *sp,
2132 u64 *start, u64 *end)
2133{
2134 struct page *pages[PTE_PREFETCH_NUM];
2135 unsigned access = sp->role.access;
2136 int i, ret;
2137 gfn_t gfn;
2138
2139 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2140 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2141 return -1;
2142
2143 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2144 if (ret <= 0)
2145 return -1;
2146
2147 for (i = 0; i < ret; i++, gfn++, start++)
2148 mmu_set_spte(vcpu, start, ACC_ALL,
2149 access, 0, 0, 1, NULL,
2150 sp->role.level, gfn,
2151 page_to_pfn(pages[i]), true, true);
2152
2153 return 0;
2154}
2155
2156static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2157 struct kvm_mmu_page *sp, u64 *sptep)
2158{
2159 u64 *spte, *start = NULL;
2160 int i;
2161
2162 WARN_ON(!sp->role.direct);
2163
2164 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2165 spte = sp->spt + i;
2166
2167 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2168 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2169 if (!start)
2170 continue;
2171 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2172 break;
2173 start = NULL;
2174 } else if (!start)
2175 start = spte;
2176 }
2177}
2178
2179static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2180{
2181 struct kvm_mmu_page *sp;
2182
2183 /*
2184 * Since it's no accessed bit on EPT, it's no way to
2185 * distinguish between actually accessed translations
2186 * and prefetched, so disable pte prefetch if EPT is
2187 * enabled.
2188 */
2189 if (!shadow_accessed_mask)
2190 return;
2191
2192 sp = page_header(__pa(sptep));
2193 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2194 return;
2195
2196 __direct_pte_prefetch(vcpu, sp, sptep);
2197}
2198
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002199static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002200 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201{
Avi Kivity9f652d22008-12-25 14:54:25 +02002202 struct kvm_shadow_walk_iterator iterator;
2203 struct kvm_mmu_page *sp;
2204 int pt_write = 0;
2205 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206
Avi Kivity9f652d22008-12-25 14:54:25 +02002207 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002208 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002209 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2210 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002211 level, gfn, pfn, false, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002212 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002213 ++vcpu->stat.pf_fixed;
2214 break;
2215 }
2216
2217 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002218 u64 base_addr = iterator.addr;
2219
2220 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2221 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002222 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2223 iterator.level - 1,
2224 1, ACC_ALL, iterator.sptep);
2225 if (!sp) {
2226 pgprintk("nonpaging_map: ENOMEM\n");
2227 kvm_release_pfn_clean(pfn);
2228 return -ENOMEM;
2229 }
2230
Avi Kivityd555c332009-06-10 14:24:23 +03002231 __set_spte(iterator.sptep,
2232 __pa(sp->spt)
2233 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2234 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002235 }
2236 }
2237 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238}
2239
Huang Yingbf998152010-05-31 14:28:19 +08002240static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2241{
2242 char buf[1];
2243 void __user *hva;
2244 int r;
2245
2246 /* Touch the page, so send SIGBUS */
2247 hva = (void __user *)gfn_to_hva(kvm, gfn);
2248 r = copy_from_user(buf, hva, 1);
2249}
2250
2251static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2252{
2253 kvm_release_pfn_clean(pfn);
2254 if (is_hwpoison_pfn(pfn)) {
2255 kvm_send_hwpoison_signal(kvm, gfn);
2256 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002257 } else if (is_fault_pfn(pfn))
2258 return -EFAULT;
2259
Huang Yingbf998152010-05-31 14:28:19 +08002260 return 1;
2261}
2262
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002263static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2264{
2265 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002266 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002267 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002268 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002269
Joerg Roedel852e3c12009-07-27 16:30:44 +02002270 level = mapping_level(vcpu, gfn);
2271
2272 /*
2273 * This path builds a PAE pagetable - so we can map 2mb pages at
2274 * maximum. Therefore check if the level is larger than that.
2275 */
2276 if (level > PT_DIRECTORY_LEVEL)
2277 level = PT_DIRECTORY_LEVEL;
2278
2279 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002280
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002281 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002282 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002283 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002284
Avi Kivityd196e342008-01-24 11:44:11 +02002285 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002286 if (is_error_pfn(pfn))
2287 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002288
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002289 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002290 if (mmu_notifier_retry(vcpu, mmu_seq))
2291 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002292 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002293 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002294 spin_unlock(&vcpu->kvm->mmu_lock);
2295
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002296
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002297 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002298
2299out_unlock:
2300 spin_unlock(&vcpu->kvm->mmu_lock);
2301 kvm_release_pfn_clean(pfn);
2302 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002303}
2304
2305
Avi Kivity17ac10a2007-01-05 16:36:40 -08002306static void mmu_free_roots(struct kvm_vcpu *vcpu)
2307{
2308 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002309 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002310 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002311
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002312 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002313 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002314 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002315 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2316 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002317
Avi Kivity4db35312007-11-21 15:28:32 +02002318 sp = page_header(root);
2319 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002320 if (!sp->root_count && sp->role.invalid) {
2321 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2322 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2323 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002324 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002325 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002326 return;
2327 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002328 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002329 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002330
Avi Kivity417726a2007-04-12 17:35:58 +03002331 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002332 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002333 sp = page_header(root);
2334 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002335 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002336 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2337 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002338 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002339 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002340 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002341 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002342 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002343 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002344}
2345
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002346static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2347{
2348 int ret = 0;
2349
2350 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002351 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002352 ret = 1;
2353 }
2354
2355 return ret;
2356}
2357
2358static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002359{
2360 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002361 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002362 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002363 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002364 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002365
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002366 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002367
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002368 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2369 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002370
2371 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002372 if (mmu_check_root(vcpu, root_gfn))
2373 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002374 if (tdp_enabled) {
2375 direct = 1;
2376 root_gfn = 0;
2377 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002378 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002379 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002380 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002381 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002382 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002383 root = __pa(sp->spt);
2384 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002385 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002386 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002387 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002388 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002389 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002390 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002391 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002392
2393 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002394 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002395 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002396 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002397 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002398 continue;
2399 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002400 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002401 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002402 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002403 if (mmu_check_root(vcpu, root_gfn))
2404 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002405 if (tdp_enabled) {
2406 direct = 1;
2407 root_gfn = i << 30;
2408 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002409 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002410 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002411 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002412 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002413 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002414 root = __pa(sp->spt);
2415 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002416 spin_unlock(&vcpu->kvm->mmu_lock);
2417
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002418 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002419 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002420 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002421 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002422}
2423
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002424static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2425{
2426 int i;
2427 struct kvm_mmu_page *sp;
2428
2429 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2430 return;
2431 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2432 hpa_t root = vcpu->arch.mmu.root_hpa;
2433 sp = page_header(root);
2434 mmu_sync_children(vcpu, sp);
2435 return;
2436 }
2437 for (i = 0; i < 4; ++i) {
2438 hpa_t root = vcpu->arch.mmu.pae_root[i];
2439
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002440 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002441 root &= PT64_BASE_ADDR_MASK;
2442 sp = page_header(root);
2443 mmu_sync_children(vcpu, sp);
2444 }
2445 }
2446}
2447
2448void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2449{
2450 spin_lock(&vcpu->kvm->mmu_lock);
2451 mmu_sync_roots(vcpu);
2452 spin_unlock(&vcpu->kvm->mmu_lock);
2453}
2454
Gleb Natapov1871c602010-02-10 14:21:32 +02002455static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2456 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457{
Gleb Natapov1871c602010-02-10 14:21:32 +02002458 if (error)
2459 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 return vaddr;
2461}
2462
2463static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002464 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465{
Avi Kivitye8332402007-12-09 18:43:00 +02002466 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002467 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002469 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002470 r = mmu_topup_memory_caches(vcpu);
2471 if (r)
2472 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002473
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002475 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476
Avi Kivitye8332402007-12-09 18:43:00 +02002477 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478
Avi Kivitye8332402007-12-09 18:43:00 +02002479 return nonpaging_map(vcpu, gva & PAGE_MASK,
2480 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481}
2482
Joerg Roedelfb72d162008-02-07 13:47:44 +01002483static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2484 u32 error_code)
2485{
Anthony Liguori35149e22008-04-02 14:46:56 -05002486 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002487 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002488 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002489 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002490 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002491
2492 ASSERT(vcpu);
2493 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2494
2495 r = mmu_topup_memory_caches(vcpu);
2496 if (r)
2497 return r;
2498
Joerg Roedel852e3c12009-07-27 16:30:44 +02002499 level = mapping_level(vcpu, gfn);
2500
2501 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2502
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002503 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002504 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002505 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002506 if (is_error_pfn(pfn))
2507 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002508 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002509 if (mmu_notifier_retry(vcpu, mmu_seq))
2510 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002511 kvm_mmu_free_some_pages(vcpu);
2512 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002513 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002514 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002515
2516 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002517
2518out_unlock:
2519 spin_unlock(&vcpu->kvm->mmu_lock);
2520 kvm_release_pfn_clean(pfn);
2521 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002522}
2523
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524static void nonpaging_free(struct kvm_vcpu *vcpu)
2525{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002526 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527}
2528
2529static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2530{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002531 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532
2533 context->new_cr3 = nonpaging_new_cr3;
2534 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 context->gva_to_gpa = nonpaging_gva_to_gpa;
2536 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002537 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002538 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002539 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002540 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002542 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543 return 0;
2544}
2545
Avi Kivityd835dfe2007-11-21 02:57:59 +02002546void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002548 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002549 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550}
2551
2552static void paging_new_cr3(struct kvm_vcpu *vcpu)
2553{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002554 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002555 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556}
2557
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558static void inject_page_fault(struct kvm_vcpu *vcpu,
2559 u64 addr,
2560 u32 err_code)
2561{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002562 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563}
2564
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565static void paging_free(struct kvm_vcpu *vcpu)
2566{
2567 nonpaging_free(vcpu);
2568}
2569
Dong, Eddie82725b22009-03-30 16:21:08 +08002570static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2571{
2572 int bit7;
2573
2574 bit7 = (gpte >> 7) & 1;
2575 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2576}
2577
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578#define PTTYPE 64
2579#include "paging_tmpl.h"
2580#undef PTTYPE
2581
2582#define PTTYPE 32
2583#include "paging_tmpl.h"
2584#undef PTTYPE
2585
Dong, Eddie82725b22009-03-30 16:21:08 +08002586static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2587{
2588 struct kvm_mmu *context = &vcpu->arch.mmu;
2589 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2590 u64 exb_bit_rsvd = 0;
2591
2592 if (!is_nx(vcpu))
2593 exb_bit_rsvd = rsvd_bits(63, 63);
2594 switch (level) {
2595 case PT32_ROOT_LEVEL:
2596 /* no rsvd bits for 2 level 4K page table entries */
2597 context->rsvd_bits_mask[0][1] = 0;
2598 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002599 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2600
2601 if (!is_pse(vcpu)) {
2602 context->rsvd_bits_mask[1][1] = 0;
2603 break;
2604 }
2605
Dong, Eddie82725b22009-03-30 16:21:08 +08002606 if (is_cpuid_PSE36())
2607 /* 36bits PSE 4MB page */
2608 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2609 else
2610 /* 32 bits PSE 4MB page */
2611 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002612 break;
2613 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002614 context->rsvd_bits_mask[0][2] =
2615 rsvd_bits(maxphyaddr, 63) |
2616 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002617 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002618 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002619 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2620 rsvd_bits(maxphyaddr, 62); /* PTE */
2621 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2622 rsvd_bits(maxphyaddr, 62) |
2623 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002624 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002625 break;
2626 case PT64_ROOT_LEVEL:
2627 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2628 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2629 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2630 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2631 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002632 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002633 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2634 rsvd_bits(maxphyaddr, 51);
2635 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002636 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2637 rsvd_bits(maxphyaddr, 51) |
2638 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002639 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002640 rsvd_bits(maxphyaddr, 51) |
2641 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002642 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002643 break;
2644 }
2645}
2646
Avi Kivity17ac10a2007-01-05 16:36:40 -08002647static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002649 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650
2651 ASSERT(is_pae(vcpu));
2652 context->new_cr3 = paging_new_cr3;
2653 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002655 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002656 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002657 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002659 context->root_level = level;
2660 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002661 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 return 0;
2663}
2664
Avi Kivity17ac10a2007-01-05 16:36:40 -08002665static int paging64_init_context(struct kvm_vcpu *vcpu)
2666{
Dong, Eddie82725b22009-03-30 16:21:08 +08002667 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002668 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2669}
2670
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671static int paging32_init_context(struct kvm_vcpu *vcpu)
2672{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002673 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674
Dong, Eddie82725b22009-03-30 16:21:08 +08002675 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 context->new_cr3 = paging_new_cr3;
2677 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 context->gva_to_gpa = paging32_gva_to_gpa;
2679 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002680 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002681 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002682 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 context->root_level = PT32_ROOT_LEVEL;
2684 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002685 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 return 0;
2687}
2688
2689static int paging32E_init_context(struct kvm_vcpu *vcpu)
2690{
Dong, Eddie82725b22009-03-30 16:21:08 +08002691 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002692 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693}
2694
Joerg Roedelfb72d162008-02-07 13:47:44 +01002695static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2696{
2697 struct kvm_mmu *context = &vcpu->arch.mmu;
2698
2699 context->new_cr3 = nonpaging_new_cr3;
2700 context->page_fault = tdp_page_fault;
2701 context->free = nonpaging_free;
2702 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002703 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002704 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002705 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002706 context->root_hpa = INVALID_PAGE;
2707
2708 if (!is_paging(vcpu)) {
2709 context->gva_to_gpa = nonpaging_gva_to_gpa;
2710 context->root_level = 0;
2711 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002712 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002713 context->gva_to_gpa = paging64_gva_to_gpa;
2714 context->root_level = PT64_ROOT_LEVEL;
2715 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002716 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002717 context->gva_to_gpa = paging64_gva_to_gpa;
2718 context->root_level = PT32E_ROOT_LEVEL;
2719 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002720 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002721 context->gva_to_gpa = paging32_gva_to_gpa;
2722 context->root_level = PT32_ROOT_LEVEL;
2723 }
2724
2725 return 0;
2726}
2727
2728static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002730 int r;
2731
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002733 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734
2735 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002736 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002737 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002738 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002740 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002742 r = paging32_init_context(vcpu);
2743
Avi Kivity5b7e0102010-04-14 19:20:03 +03002744 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002745 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002746
2747 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748}
2749
Joerg Roedelfb72d162008-02-07 13:47:44 +01002750static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2751{
Anthony Liguori35149e22008-04-02 14:46:56 -05002752 vcpu->arch.update_pte.pfn = bad_pfn;
2753
Joerg Roedelfb72d162008-02-07 13:47:44 +01002754 if (tdp_enabled)
2755 return init_kvm_tdp_mmu(vcpu);
2756 else
2757 return init_kvm_softmmu(vcpu);
2758}
2759
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2761{
2762 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002763 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2764 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002765 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766}
2767
2768int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2769{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002770 destroy_kvm_mmu(vcpu);
2771 return init_kvm_mmu(vcpu);
2772}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002773EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002774
2775int kvm_mmu_load(struct kvm_vcpu *vcpu)
2776{
Avi Kivity714b93d2007-01-05 16:36:53 -08002777 int r;
2778
Avi Kivitye2dec932007-01-05 16:36:54 -08002779 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002780 if (r)
2781 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002782 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002783 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002784 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002785 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002786 if (r)
2787 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002788 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002789 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002790out:
2791 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002793EXPORT_SYMBOL_GPL(kvm_mmu_load);
2794
2795void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2796{
2797 mmu_free_roots(vcpu);
2798}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799
Avi Kivity09072da2007-05-01 14:16:52 +03002800static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002801 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002802 u64 *spte)
2803{
2804 u64 pte;
2805 struct kvm_mmu_page *child;
2806
2807 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002808 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002809 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002810 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002811 else {
2812 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002813 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002814 }
2815 }
Avi Kivityd555c332009-06-10 14:24:23 +03002816 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002817 if (is_large_pte(pte))
2818 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002819}
2820
Avi Kivity00284252007-05-01 16:53:31 +03002821static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002822 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002823 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002824 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002825{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002826 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002827 ++vcpu->kvm->stat.mmu_pde_zapped;
2828 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002829 }
Avi Kivity00284252007-05-01 16:53:31 +03002830
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002831 if (is_rsvd_bits_set(vcpu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
2832 return;
2833
Avi Kivity4cee5762007-11-18 16:37:07 +02002834 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002835 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002836 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002837 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002838 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002839}
2840
Avi Kivity79539ce2007-11-21 02:06:21 +02002841static bool need_remote_flush(u64 old, u64 new)
2842{
2843 if (!is_shadow_present_pte(old))
2844 return false;
2845 if (!is_shadow_present_pte(new))
2846 return true;
2847 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2848 return true;
2849 old ^= PT64_NX_MASK;
2850 new ^= PT64_NX_MASK;
2851 return (old & ~new & PT64_PERM_MASK) != 0;
2852}
2853
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002854static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2855 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002856{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002857 if (zap_page)
2858 return;
2859
2860 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002861 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002862 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002863 kvm_mmu_flush_tlb(vcpu);
2864}
2865
Avi Kivity12b7d282007-09-23 14:10:49 +02002866static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2867{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002868 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002869
Sheng Yang7b523452008-04-25 21:13:50 +08002870 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002871}
2872
Avi Kivityd7824ff2007-12-30 12:29:05 +02002873static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002874 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002875{
2876 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002877 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002878
Avi Kivity43a37952009-06-10 14:12:05 +03002879 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002880 return;
2881 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002882
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002883 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002884 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002885 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002886
Anthony Liguori35149e22008-04-02 14:46:56 -05002887 if (is_error_pfn(pfn)) {
2888 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002889 return;
2890 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002891 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002892 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002893}
2894
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002895static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2896{
2897 u64 *spte = vcpu->arch.last_pte_updated;
2898
2899 if (spte
2900 && vcpu->arch.last_pte_gfn == gfn
2901 && shadow_accessed_mask
2902 && !(*spte & shadow_accessed_mask)
2903 && is_shadow_present_pte(*spte))
2904 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2905}
2906
Avi Kivity09072da2007-05-01 14:16:52 +03002907void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002908 const u8 *new, int bytes,
2909 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002910{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002911 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002912 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02002913 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002914 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002915 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002916 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002917 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002918 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002919 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002920 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002921 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002922 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002923 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002924 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002925 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002926 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002927 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002928 bool remote_flush, local_flush, zap_page;
2929
2930 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002931
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002932 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002933
Avi Kivity08e850c2010-03-15 13:59:57 +02002934 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2935
2936 /*
2937 * Assume that the pte write on a page table of the same type
2938 * as the current vcpu paging mode. This is nearly always true
2939 * (might be false while changing modes). Note it is verified later
2940 * by update_pte().
2941 */
2942 if ((is_pae(vcpu) && bytes == 4) || !new) {
2943 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2944 if (is_pae(vcpu)) {
2945 gpa &= ~(gpa_t)7;
2946 bytes = 8;
2947 }
2948 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2949 if (r)
2950 gentry = 0;
2951 new = (const u8 *)&gentry;
2952 }
2953
Avi Kivity72016f32010-03-15 13:59:53 +02002954 switch (bytes) {
2955 case 4:
2956 gentry = *(const u32 *)new;
2957 break;
2958 case 8:
2959 gentry = *(const u64 *)new;
2960 break;
2961 default:
2962 gentry = 0;
2963 break;
2964 }
2965
Avi Kivity72016f32010-03-15 13:59:53 +02002966 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002967 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002968 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2969 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002970 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002971 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002972 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08002973 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002974 if (guest_initiated) {
2975 if (gfn == vcpu->arch.last_pt_write_gfn
2976 && !last_updated_pte_accessed(vcpu)) {
2977 ++vcpu->arch.last_pt_write_count;
2978 if (vcpu->arch.last_pt_write_count >= 3)
2979 flooded = 1;
2980 } else {
2981 vcpu->arch.last_pt_write_gfn = gfn;
2982 vcpu->arch.last_pt_write_count = 1;
2983 vcpu->arch.last_pte_updated = NULL;
2984 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002985 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002986
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002987 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002988 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002989 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002990 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002991 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002992 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002993 /*
2994 * Misaligned accesses are too much trouble to fix
2995 * up; also, they usually indicate a page is not used
2996 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002997 *
2998 * If we're seeing too many writes to a page,
2999 * it may no longer be a page table, or we may be
3000 * forking, in which case it is better to unmap the
3001 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003002 */
3003 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003004 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003005 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003006 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003007 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003008 continue;
3009 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003010 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003011 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003012 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003013 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003014 page_offset <<= 1; /* 32->64 */
3015 /*
3016 * A 32-bit pde maps 4MB while the shadow pdes map
3017 * only 2MB. So we need to double the offset again
3018 * and zap two pdes instead of one.
3019 */
3020 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003021 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003022 page_offset <<= 1;
3023 npte = 2;
3024 }
Avi Kivityfce06572007-05-01 16:44:05 +03003025 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003026 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003027 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003028 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003029 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003030 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003031 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003032 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003033 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003034 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003035 if (gentry &&
3036 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3037 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003038 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003039 if (!remote_flush && need_remote_flush(entry, *spte))
3040 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003041 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003042 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003043 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003044 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003045 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003046 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003047 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003048 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3049 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3050 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003051 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003052}
3053
Avi Kivitya4360362007-01-05 16:36:45 -08003054int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3055{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003056 gpa_t gpa;
3057 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003058
Avi Kivity60f24782009-08-27 13:37:06 +03003059 if (tdp_enabled)
3060 return 0;
3061
Gleb Natapov1871c602010-02-10 14:21:32 +02003062 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003063
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003064 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003065 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003066 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003067 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003068}
Avi Kivity577bdc42008-07-19 08:57:05 +03003069EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003070
Avi Kivity22d95b12007-09-14 20:26:06 +03003071void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003072{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003073 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003074
Dave Hansene0df7b92010-08-19 18:11:05 -07003075 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003076 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003077 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003078
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003079 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003080 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003081 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003082 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003083 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003084 }
3085}
Avi Kivityebeace82007-01-05 16:36:47 -08003086
Avi Kivity30677142007-10-28 18:48:59 +02003087int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3088{
3089 int r;
3090 enum emulation_result er;
3091
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003092 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02003093 if (r < 0)
3094 goto out;
3095
3096 if (!r) {
3097 r = 1;
3098 goto out;
3099 }
3100
Avi Kivityb733bfb2007-10-28 18:52:05 +02003101 r = mmu_topup_memory_caches(vcpu);
3102 if (r)
3103 goto out;
3104
Avi Kivity851ba692009-08-24 11:10:17 +03003105 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02003106
3107 switch (er) {
3108 case EMULATE_DONE:
3109 return 1;
3110 case EMULATE_DO_MMIO:
3111 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003112 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003113 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003114 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003115 default:
3116 BUG();
3117 }
3118out:
Avi Kivity30677142007-10-28 18:48:59 +02003119 return r;
3120}
3121EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3122
Marcelo Tosattia7052892008-09-23 13:18:35 -03003123void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3124{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003125 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003126 kvm_mmu_flush_tlb(vcpu);
3127 ++vcpu->stat.invlpg;
3128}
3129EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3130
Joerg Roedel18552672008-02-07 13:47:41 +01003131void kvm_enable_tdp(void)
3132{
3133 tdp_enabled = true;
3134}
3135EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3136
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003137void kvm_disable_tdp(void)
3138{
3139 tdp_enabled = false;
3140}
3141EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3142
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143static void free_mmu_pages(struct kvm_vcpu *vcpu)
3144{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003145 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146}
3147
3148static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3149{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003150 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 int i;
3152
3153 ASSERT(vcpu);
3154
Avi Kivity17ac10a2007-01-05 16:36:40 -08003155 /*
3156 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3157 * Therefore we need to allocate shadow page tables in the first
3158 * 4GB of memory, which happens to fit the DMA32 zone.
3159 */
3160 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3161 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003162 return -ENOMEM;
3163
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003164 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003165 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003166 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003167
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169}
3170
Ingo Molnar8018c272006-12-29 16:50:01 -08003171int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003174 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175
Ingo Molnar8018c272006-12-29 16:50:01 -08003176 return alloc_mmu_pages(vcpu);
3177}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178
Ingo Molnar8018c272006-12-29 16:50:01 -08003179int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3180{
3181 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003182 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003183
Ingo Molnar8018c272006-12-29 16:50:01 -08003184 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185}
3186
3187void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3188{
3189 ASSERT(vcpu);
3190
3191 destroy_kvm_mmu(vcpu);
3192 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08003193 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194}
3195
Avi Kivity90cb0522007-07-17 13:04:56 +03003196void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197{
Avi Kivity4db35312007-11-21 15:28:32 +02003198 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003200 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 int i;
3202 u64 *pt;
3203
Sheng Yang291f26b2008-10-16 17:30:57 +08003204 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 continue;
3206
Avi Kivity4db35312007-11-21 15:28:32 +02003207 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3209 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003210 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 }
Avi Kivity171d5952008-08-27 16:40:51 +03003213 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003215
Avi Kivity90cb0522007-07-17 13:04:56 +03003216void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003217{
Avi Kivity4db35312007-11-21 15:28:32 +02003218 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003219 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003220
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003221 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003222restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003223 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003224 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003225 goto restart;
3226
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003227 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003228 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003229}
3230
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003231static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3232 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003233{
3234 struct kvm_mmu_page *page;
3235
3236 page = container_of(kvm->arch.active_mmu_pages.prev,
3237 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003238 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003239}
3240
Dave Chinner7f8275d2010-07-19 14:56:17 +10003241static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003242{
3243 struct kvm *kvm;
3244 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003245
3246 if (nr_to_scan == 0)
3247 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003248
3249 spin_lock(&kvm_lock);
3250
3251 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003252 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003253 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003254
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003255 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003256 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003257 if (!kvm_freed && nr_to_scan > 0 &&
3258 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003259 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3260 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003261 kvm_freed = kvm;
3262 }
3263 nr_to_scan--;
3264
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003265 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003266 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003267 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003268 }
3269 if (kvm_freed)
3270 list_move_tail(&kvm_freed->vm_list, &vm_list);
3271
3272 spin_unlock(&kvm_lock);
3273
Dave Hansen45221ab2010-08-19 18:11:37 -07003274out:
3275 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003276}
3277
3278static struct shrinker mmu_shrinker = {
3279 .shrink = mmu_shrink,
3280 .seeks = DEFAULT_SEEKS * 10,
3281};
3282
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003283static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003284{
3285 if (pte_chain_cache)
3286 kmem_cache_destroy(pte_chain_cache);
3287 if (rmap_desc_cache)
3288 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003289 if (mmu_page_header_cache)
3290 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003291}
3292
Izik Eidus3ee16c82008-03-30 15:17:21 +03003293void kvm_mmu_module_exit(void)
3294{
3295 mmu_destroy_caches();
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003296 percpu_counter_destroy(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003297 unregister_shrinker(&mmu_shrinker);
3298}
3299
Avi Kivityb5a33a72007-04-15 16:31:09 +03003300int kvm_mmu_module_init(void)
3301{
3302 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3303 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003304 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003305 if (!pte_chain_cache)
3306 goto nomem;
3307 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3308 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003309 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003310 if (!rmap_desc_cache)
3311 goto nomem;
3312
Avi Kivityd3d25b02007-05-30 12:34:53 +03003313 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3314 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003315 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003316 if (!mmu_page_header_cache)
3317 goto nomem;
3318
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003319 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3320 goto nomem;
3321
Izik Eidus3ee16c82008-03-30 15:17:21 +03003322 register_shrinker(&mmu_shrinker);
3323
Avi Kivityb5a33a72007-04-15 16:31:09 +03003324 return 0;
3325
3326nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003327 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003328 return -ENOMEM;
3329}
3330
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003331/*
3332 * Caculate mmu pages needed for kvm.
3333 */
3334unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3335{
3336 int i;
3337 unsigned int nr_mmu_pages;
3338 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003339 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003340
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003341 slots = kvm_memslots(kvm);
3342
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003343 for (i = 0; i < slots->nmemslots; i++)
3344 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003345
3346 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3347 nr_mmu_pages = max(nr_mmu_pages,
3348 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3349
3350 return nr_mmu_pages;
3351}
3352
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003353static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3354 unsigned len)
3355{
3356 if (len > buffer->len)
3357 return NULL;
3358 return buffer->ptr;
3359}
3360
3361static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3362 unsigned len)
3363{
3364 void *ret;
3365
3366 ret = pv_mmu_peek_buffer(buffer, len);
3367 if (!ret)
3368 return ret;
3369 buffer->ptr += len;
3370 buffer->len -= len;
3371 buffer->processed += len;
3372 return ret;
3373}
3374
3375static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3376 gpa_t addr, gpa_t value)
3377{
3378 int bytes = 8;
3379 int r;
3380
3381 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3382 bytes = 4;
3383
3384 r = mmu_topup_memory_caches(vcpu);
3385 if (r)
3386 return r;
3387
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003388 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003389 return -EFAULT;
3390
3391 return 1;
3392}
3393
3394static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3395{
Avi Kivity23902182010-06-10 17:02:16 +03003396 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003397 return 1;
3398}
3399
3400static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3401{
3402 spin_lock(&vcpu->kvm->mmu_lock);
3403 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3404 spin_unlock(&vcpu->kvm->mmu_lock);
3405 return 1;
3406}
3407
3408static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3409 struct kvm_pv_mmu_op_buffer *buffer)
3410{
3411 struct kvm_mmu_op_header *header;
3412
3413 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3414 if (!header)
3415 return 0;
3416 switch (header->op) {
3417 case KVM_MMU_OP_WRITE_PTE: {
3418 struct kvm_mmu_op_write_pte *wpte;
3419
3420 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3421 if (!wpte)
3422 return 0;
3423 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3424 wpte->pte_val);
3425 }
3426 case KVM_MMU_OP_FLUSH_TLB: {
3427 struct kvm_mmu_op_flush_tlb *ftlb;
3428
3429 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3430 if (!ftlb)
3431 return 0;
3432 return kvm_pv_mmu_flush_tlb(vcpu);
3433 }
3434 case KVM_MMU_OP_RELEASE_PT: {
3435 struct kvm_mmu_op_release_pt *rpt;
3436
3437 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3438 if (!rpt)
3439 return 0;
3440 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3441 }
3442 default: return 0;
3443 }
3444}
3445
3446int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3447 gpa_t addr, unsigned long *ret)
3448{
3449 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003450 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003451
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003452 buffer->ptr = buffer->buf;
3453 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3454 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003455
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003456 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003457 if (r)
3458 goto out;
3459
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003460 while (buffer->len) {
3461 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003462 if (r < 0)
3463 goto out;
3464 if (r == 0)
3465 break;
3466 }
3467
3468 r = 1;
3469out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003470 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003471 return r;
3472}
3473
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003474int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3475{
3476 struct kvm_shadow_walk_iterator iterator;
3477 int nr_sptes = 0;
3478
3479 spin_lock(&vcpu->kvm->mmu_lock);
3480 for_each_shadow_entry(vcpu, addr, iterator) {
3481 sptes[iterator.level-1] = *iterator.sptep;
3482 nr_sptes++;
3483 if (!is_shadow_present_pte(*iterator.sptep))
3484 break;
3485 }
3486 spin_unlock(&vcpu->kvm->mmu_lock);
3487
3488 return nr_sptes;
3489}
3490EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3491
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003492#ifdef CONFIG_KVM_MMU_AUDIT
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08003493#include "mmu_audit.c"
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003494#endif