blob: dd76765310ce458bb673435932bc3eeed5e5bea3 [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 Guangrong8672b722010-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 Guangrong8672b722010-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 Kivitya1f4d392010-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 Kivitya1f4d392010-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 Kivitya1f4d392010-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 Guangrong8672b722010-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 Guangrong8672b722010-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 Guangrong8672b722010-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 Kivityf6e2c022009-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 Kivityf6e2c022009-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;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001451 if (!vcpu->arch.mmu.direct_map
1452 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001453 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1454 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1455 role.quadrant = quadrant;
1456 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001457 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001458 if (!need_sync && sp->unsync)
1459 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001460
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001461 if (sp->role.word != role.word)
1462 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001463
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001464 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1465 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001466
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001467 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1468 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001469 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001470 kvm_mmu_mark_parents_unsync(sp);
1471 } else if (sp->unsync)
1472 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001473
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001474 trace_kvm_mmu_get_page(sp, false);
1475 return sp;
1476 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001477 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001478 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001479 if (!sp)
1480 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001481 sp->gfn = gfn;
1482 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001483 hlist_add_head(&sp->hash_link,
1484 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001485 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001486 if (rmap_write_protect(vcpu->kvm, gfn))
1487 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001488 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1489 kvm_sync_pages(vcpu, gfn);
1490
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001491 account_shadowed(vcpu->kvm, gfn);
1492 }
Avi Kivity131d8272008-05-29 14:56:28 +03001493 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1494 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1495 else
1496 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001497 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001498 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001499}
1500
Avi Kivity2d111232008-12-25 14:39:47 +02001501static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1502 struct kvm_vcpu *vcpu, u64 addr)
1503{
1504 iterator->addr = addr;
1505 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1506 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001507
1508 if (iterator->level == PT64_ROOT_LEVEL &&
1509 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1510 !vcpu->arch.mmu.direct_map)
1511 --iterator->level;
1512
Avi Kivity2d111232008-12-25 14:39:47 +02001513 if (iterator->level == PT32E_ROOT_LEVEL) {
1514 iterator->shadow_addr
1515 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1516 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1517 --iterator->level;
1518 if (!iterator->shadow_addr)
1519 iterator->level = 0;
1520 }
1521}
1522
1523static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1524{
1525 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1526 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001527
1528 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1529 if (is_large_pte(*iterator->sptep))
1530 return false;
1531
Avi Kivity2d111232008-12-25 14:39:47 +02001532 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1533 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1534 return true;
1535}
1536
1537static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1538{
1539 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1540 --iterator->level;
1541}
1542
Avi Kivity32ef26a2010-07-13 14:27:04 +03001543static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1544{
1545 u64 spte;
1546
1547 spte = __pa(sp->spt)
1548 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1549 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001550 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001551}
1552
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001553static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1554{
1555 if (is_large_pte(*sptep)) {
1556 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1557 kvm_flush_remote_tlbs(vcpu->kvm);
1558 }
1559}
1560
Avi Kivitya357bd22010-07-13 14:27:07 +03001561static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1562 unsigned direct_access)
1563{
1564 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1565 struct kvm_mmu_page *child;
1566
1567 /*
1568 * For the direct sp, if the guest pte's dirty bit
1569 * changed form clean to dirty, it will corrupt the
1570 * sp's access: allow writable in the read-only sp,
1571 * so we should update the spte at this point to get
1572 * a new sp with the correct access.
1573 */
1574 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1575 if (child->role.access == direct_access)
1576 return;
1577
1578 mmu_page_remove_parent_pte(child, sptep);
1579 __set_spte(sptep, shadow_trap_nonpresent_pte);
1580 kvm_flush_remote_tlbs(vcpu->kvm);
1581 }
1582}
1583
Avi Kivity90cb0522007-07-17 13:04:56 +03001584static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001585 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001586{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001587 unsigned i;
1588 u64 *pt;
1589 u64 ent;
1590
Avi Kivity4db35312007-11-21 15:28:32 +02001591 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001592
Avi Kivity697fe2e2007-01-05 16:36:46 -08001593 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1594 ent = pt[i];
1595
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001596 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001597 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001598 ent &= PT64_BASE_ADDR_MASK;
1599 mmu_page_remove_parent_pte(page_header(ent),
1600 &pt[i]);
1601 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001602 if (is_large_pte(ent))
1603 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001604 drop_spte(kvm, &pt[i],
1605 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001606 }
1607 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001608 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001609 }
Avi Kivitya4360362007-01-05 16:36:45 -08001610}
1611
Avi Kivity4db35312007-11-21 15:28:32 +02001612static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001613{
Avi Kivity4db35312007-11-21 15:28:32 +02001614 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001615}
1616
Avi Kivity12b7d282007-09-23 14:10:49 +02001617static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1618{
1619 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001620 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001621
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001622 kvm_for_each_vcpu(i, vcpu, kvm)
1623 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001624}
1625
Avi Kivity31aa2b42008-07-11 17:59:46 +03001626static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001627{
1628 u64 *parent_pte;
1629
Avi Kivity4db35312007-11-21 15:28:32 +02001630 while (sp->multimapped || sp->parent_pte) {
1631 if (!sp->multimapped)
1632 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001633 else {
1634 struct kvm_pte_chain *chain;
1635
Avi Kivity4db35312007-11-21 15:28:32 +02001636 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001637 struct kvm_pte_chain, link);
1638 parent_pte = chain->parent_ptes[0];
1639 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001640 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001641 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001642 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001643 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001644}
1645
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001646static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001647 struct kvm_mmu_page *parent,
1648 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001649{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001650 int i, zapped = 0;
1651 struct mmu_page_path parents;
1652 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001653
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001654 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001656
1657 kvm_mmu_pages_init(parent, &parents, &pages);
1658 while (mmu_unsync_walk(parent, &pages)) {
1659 struct kvm_mmu_page *sp;
1660
1661 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001662 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001663 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001664 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001665 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001666 kvm_mmu_pages_init(parent, &parents, &pages);
1667 }
1668
1669 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001670}
1671
Xiao Guangrong77758342010-06-04 21:53:54 +08001672static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1673 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001674{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001675 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001676
Xiao Guangrong77758342010-06-04 21:53:54 +08001677 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001678 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001679 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001680 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001681 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001682 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001683 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001684 if (sp->unsync)
1685 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001686 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001687 /* Count self */
1688 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001689 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001690 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001691 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001692 kvm_reload_remote_mmus(kvm);
1693 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001694
1695 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001696 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001697 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001698}
1699
Xiao Guangrong77758342010-06-04 21:53:54 +08001700static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1701 struct list_head *invalid_list)
1702{
1703 struct kvm_mmu_page *sp;
1704
1705 if (list_empty(invalid_list))
1706 return;
1707
1708 kvm_flush_remote_tlbs(kvm);
1709
1710 do {
1711 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1712 WARN_ON(!sp->role.invalid || sp->root_count);
1713 kvm_mmu_free_page(kvm, sp);
1714 } while (!list_empty(invalid_list));
1715
1716}
1717
Izik Eidus82ce2c92007-10-02 18:52:55 +02001718/*
1719 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001720 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001721 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001722void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001723{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001724 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001725 /*
1726 * If we set the number of mmu pages to be smaller be than the
1727 * number of actived pages , we must to free some mmu pages before we
1728 * change the value
1729 */
1730
Dave Hansen49d5ca22010-08-19 18:11:28 -07001731 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1732 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001733 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001734 struct kvm_mmu_page *page;
1735
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001736 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001737 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001738 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1739 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001740 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001741 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001742 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001743
Dave Hansen49d5ca22010-08-19 18:11:28 -07001744 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001745}
1746
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001747static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001748{
Avi Kivity4db35312007-11-21 15:28:32 +02001749 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001750 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001751 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001752 int r;
1753
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001754 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001755 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001756
1757 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001758 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001759 sp->role.word);
1760 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001761 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001762 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001763 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001764 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001765}
1766
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001767static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001768{
Avi Kivity4db35312007-11-21 15:28:32 +02001769 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001770 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001771 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001772
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001773 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001774 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001775 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001776 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001777 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001778 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001779}
1780
Avi Kivity38c335f2007-11-21 14:20:22 +02001781static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001783 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001784 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785
Sheng Yang291f26b2008-10-16 17:30:57 +08001786 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787}
1788
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001789static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1790{
1791 int i;
1792 u64 *pt = sp->spt;
1793
1794 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1795 return;
1796
1797 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1798 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001799 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001800 }
1801}
1802
Sheng Yang74be52e2008-10-09 16:01:56 +08001803/*
1804 * The function is based on mtrr_type_lookup() in
1805 * arch/x86/kernel/cpu/mtrr/generic.c
1806 */
1807static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1808 u64 start, u64 end)
1809{
1810 int i;
1811 u64 base, mask;
1812 u8 prev_match, curr_match;
1813 int num_var_ranges = KVM_NR_VAR_MTRR;
1814
1815 if (!mtrr_state->enabled)
1816 return 0xFF;
1817
1818 /* Make end inclusive end, instead of exclusive */
1819 end--;
1820
1821 /* Look in fixed ranges. Just return the type as per start */
1822 if (mtrr_state->have_fixed && (start < 0x100000)) {
1823 int idx;
1824
1825 if (start < 0x80000) {
1826 idx = 0;
1827 idx += (start >> 16);
1828 return mtrr_state->fixed_ranges[idx];
1829 } else if (start < 0xC0000) {
1830 idx = 1 * 8;
1831 idx += ((start - 0x80000) >> 14);
1832 return mtrr_state->fixed_ranges[idx];
1833 } else if (start < 0x1000000) {
1834 idx = 3 * 8;
1835 idx += ((start - 0xC0000) >> 12);
1836 return mtrr_state->fixed_ranges[idx];
1837 }
1838 }
1839
1840 /*
1841 * Look in variable ranges
1842 * Look of multiple ranges matching this address and pick type
1843 * as per MTRR precedence
1844 */
1845 if (!(mtrr_state->enabled & 2))
1846 return mtrr_state->def_type;
1847
1848 prev_match = 0xFF;
1849 for (i = 0; i < num_var_ranges; ++i) {
1850 unsigned short start_state, end_state;
1851
1852 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1853 continue;
1854
1855 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1856 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1857 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1858 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1859
1860 start_state = ((start & mask) == (base & mask));
1861 end_state = ((end & mask) == (base & mask));
1862 if (start_state != end_state)
1863 return 0xFE;
1864
1865 if ((start & mask) != (base & mask))
1866 continue;
1867
1868 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1869 if (prev_match == 0xFF) {
1870 prev_match = curr_match;
1871 continue;
1872 }
1873
1874 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1875 curr_match == MTRR_TYPE_UNCACHABLE)
1876 return MTRR_TYPE_UNCACHABLE;
1877
1878 if ((prev_match == MTRR_TYPE_WRBACK &&
1879 curr_match == MTRR_TYPE_WRTHROUGH) ||
1880 (prev_match == MTRR_TYPE_WRTHROUGH &&
1881 curr_match == MTRR_TYPE_WRBACK)) {
1882 prev_match = MTRR_TYPE_WRTHROUGH;
1883 curr_match = MTRR_TYPE_WRTHROUGH;
1884 }
1885
1886 if (prev_match != curr_match)
1887 return MTRR_TYPE_UNCACHABLE;
1888 }
1889
1890 if (prev_match != 0xFF)
1891 return prev_match;
1892
1893 return mtrr_state->def_type;
1894}
1895
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001896u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001897{
1898 u8 mtrr;
1899
1900 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1901 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1902 if (mtrr == 0xfe || mtrr == 0xff)
1903 mtrr = MTRR_TYPE_WRBACK;
1904 return mtrr;
1905}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001906EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001907
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001908static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001909{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001910 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001911 ++vcpu->kvm->stat.mmu_unsync;
1912 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001913
Xiao Guangrong6b184932010-04-16 21:29:17 +08001914 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001915 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001916}
1917
1918static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1919{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001920 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001921 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001922
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001923 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001924 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001925 continue;
1926 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1927 __kvm_unsync_page(vcpu, s);
1928 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001929}
1930
1931static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1932 bool can_unsync)
1933{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001934 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001935 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001936 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001937
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001938 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001939 if (!can_unsync)
1940 return 1;
1941
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001942 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001943 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001944
1945 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001946 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001947 return 1;
1948 need_unsync = true;
1949 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001950 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001951 if (need_unsync)
1952 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001953 return 0;
1954}
1955
Avi Kivityd555c332009-06-10 14:24:23 +03001956static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001957 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001958 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001959 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001960 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001961{
1962 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001963 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001964
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001965 /*
1966 * We don't set the accessed bit, since we sometimes want to see
1967 * whether the guest actually used the pte (in order to detect
1968 * demand paging).
1969 */
Xiao Guangrong41327792010-08-02 16:15:08 +08001970 spte = shadow_base_present_pte;
Avi Kivity947da532008-03-18 11:05:52 +02001971 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001972 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973 if (!dirty)
1974 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001975 if (pte_access & ACC_EXEC_MASK)
1976 spte |= shadow_x_mask;
1977 else
1978 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001979 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001980 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001981 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001982 spte |= PT_PAGE_SIZE_MASK;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001983 if (vcpu->arch.mmu.direct_map)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001984 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1985 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001986
Izik Eidus14032832009-09-23 21:47:17 +03001987 if (reset_host_protection)
1988 spte |= SPTE_HOST_WRITEABLE;
1989
Anthony Liguori35149e22008-04-02 14:46:56 -05001990 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001991
1992 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001993 || (!vcpu->arch.mmu.direct_map && write_fault
1994 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001995
Joerg Roedel852e3c12009-07-27 16:30:44 +02001996 if (level > PT_PAGE_TABLE_LEVEL &&
1997 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001998 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001999 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
2000 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002001 }
2002
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002003 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002004
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002005 if (!vcpu->arch.mmu.direct_map
2006 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03002007 spte &= ~PT_USER_MASK;
2008
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002009 /*
2010 * Optimization: for pte sync, if spte was writable the hash
2011 * lookup is unnecessary (and expensive). Write protection
2012 * is responsibility of mmu_get_page / kvm_sync_page.
2013 * Same reasoning can be applied to dirty page accounting.
2014 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002015 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002016 goto set_pte;
2017
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002018 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002019 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002020 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002021 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002022 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002023 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002024 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002025 }
2026 }
2027
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002028 if (pte_access & ACC_WRITE_MASK)
2029 mark_page_dirty(vcpu->kvm, gfn);
2030
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002031set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002032 update_spte(sptep, spte);
Avi Kivitybe38d272010-06-06 14:31:27 +03002033done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002034 return ret;
2035}
2036
Avi Kivityd555c332009-06-10 14:24:23 +03002037static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002038 unsigned pt_access, unsigned pte_access,
2039 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002040 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002041 pfn_t pfn, bool speculative,
2042 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002043{
2044 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002045 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002046
2047 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002048 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002049 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002050 write_fault, user_fault, gfn);
2051
Avi Kivityd555c332009-06-10 14:24:23 +03002052 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002053 /*
2054 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2055 * the parent of the now unreachable PTE.
2056 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002057 if (level > PT_PAGE_TABLE_LEVEL &&
2058 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002059 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002060 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002061
2062 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002063 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002064 __set_spte(sptep, shadow_trap_nonpresent_pte);
2065 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002066 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002067 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002068 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002069 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002070 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002071 } else
2072 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002073 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002074
Avi Kivityd555c332009-06-10 14:24:23 +03002075 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002076 dirty, level, gfn, pfn, speculative, true,
2077 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002078 if (write_fault)
2079 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002080 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002081 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002082
Avi Kivityd555c332009-06-10 14:24:23 +03002083 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002084 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002085 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002086 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2087 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002088 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002089 ++vcpu->kvm->stat.lpages;
2090
Avi Kivityd555c332009-06-10 14:24:23 +03002091 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002092 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002093 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002094 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002095 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002096 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002097 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002098 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002099 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002100 vcpu->arch.last_pte_gfn = gfn;
2101 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002102}
2103
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2105{
2106}
2107
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002108static struct kvm_memory_slot *
2109pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2110{
2111 struct kvm_memory_slot *slot;
2112
2113 slot = gfn_to_memslot(vcpu->kvm, gfn);
2114 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2115 (no_dirty_log && slot->dirty_bitmap))
2116 slot = NULL;
2117
2118 return slot;
2119}
2120
2121static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2122 bool no_dirty_log)
2123{
2124 struct kvm_memory_slot *slot;
2125 unsigned long hva;
2126
2127 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2128 if (!slot) {
2129 get_page(bad_page);
2130 return page_to_pfn(bad_page);
2131 }
2132
2133 hva = gfn_to_hva_memslot(slot, gfn);
2134
2135 return hva_to_pfn_atomic(vcpu->kvm, hva);
2136}
2137
2138static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2139 struct kvm_mmu_page *sp,
2140 u64 *start, u64 *end)
2141{
2142 struct page *pages[PTE_PREFETCH_NUM];
2143 unsigned access = sp->role.access;
2144 int i, ret;
2145 gfn_t gfn;
2146
2147 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2148 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2149 return -1;
2150
2151 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2152 if (ret <= 0)
2153 return -1;
2154
2155 for (i = 0; i < ret; i++, gfn++, start++)
2156 mmu_set_spte(vcpu, start, ACC_ALL,
2157 access, 0, 0, 1, NULL,
2158 sp->role.level, gfn,
2159 page_to_pfn(pages[i]), true, true);
2160
2161 return 0;
2162}
2163
2164static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2165 struct kvm_mmu_page *sp, u64 *sptep)
2166{
2167 u64 *spte, *start = NULL;
2168 int i;
2169
2170 WARN_ON(!sp->role.direct);
2171
2172 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2173 spte = sp->spt + i;
2174
2175 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2176 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2177 if (!start)
2178 continue;
2179 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2180 break;
2181 start = NULL;
2182 } else if (!start)
2183 start = spte;
2184 }
2185}
2186
2187static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2188{
2189 struct kvm_mmu_page *sp;
2190
2191 /*
2192 * Since it's no accessed bit on EPT, it's no way to
2193 * distinguish between actually accessed translations
2194 * and prefetched, so disable pte prefetch if EPT is
2195 * enabled.
2196 */
2197 if (!shadow_accessed_mask)
2198 return;
2199
2200 sp = page_header(__pa(sptep));
2201 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2202 return;
2203
2204 __direct_pte_prefetch(vcpu, sp, sptep);
2205}
2206
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002207static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002208 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209{
Avi Kivity9f652d22008-12-25 14:54:25 +02002210 struct kvm_shadow_walk_iterator iterator;
2211 struct kvm_mmu_page *sp;
2212 int pt_write = 0;
2213 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214
Avi Kivity9f652d22008-12-25 14:54:25 +02002215 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002216 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002217 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2218 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002219 level, gfn, pfn, false, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002220 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002221 ++vcpu->stat.pf_fixed;
2222 break;
2223 }
2224
2225 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002226 u64 base_addr = iterator.addr;
2227
2228 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2229 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002230 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2231 iterator.level - 1,
2232 1, ACC_ALL, iterator.sptep);
2233 if (!sp) {
2234 pgprintk("nonpaging_map: ENOMEM\n");
2235 kvm_release_pfn_clean(pfn);
2236 return -ENOMEM;
2237 }
2238
Avi Kivityd555c332009-06-10 14:24:23 +03002239 __set_spte(iterator.sptep,
2240 __pa(sp->spt)
2241 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2242 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002243 }
2244 }
2245 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246}
2247
Huang Yingbf998152010-05-31 14:28:19 +08002248static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2249{
2250 char buf[1];
2251 void __user *hva;
2252 int r;
2253
2254 /* Touch the page, so send SIGBUS */
2255 hva = (void __user *)gfn_to_hva(kvm, gfn);
2256 r = copy_from_user(buf, hva, 1);
2257}
2258
2259static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2260{
2261 kvm_release_pfn_clean(pfn);
2262 if (is_hwpoison_pfn(pfn)) {
2263 kvm_send_hwpoison_signal(kvm, gfn);
2264 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002265 } else if (is_fault_pfn(pfn))
2266 return -EFAULT;
2267
Huang Yingbf998152010-05-31 14:28:19 +08002268 return 1;
2269}
2270
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002271static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2272{
2273 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002274 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002275 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002276 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002277
Joerg Roedel852e3c12009-07-27 16:30:44 +02002278 level = mapping_level(vcpu, gfn);
2279
2280 /*
2281 * This path builds a PAE pagetable - so we can map 2mb pages at
2282 * maximum. Therefore check if the level is larger than that.
2283 */
2284 if (level > PT_DIRECTORY_LEVEL)
2285 level = PT_DIRECTORY_LEVEL;
2286
2287 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002288
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002289 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002290 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002291 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002292
Avi Kivityd196e342008-01-24 11:44:11 +02002293 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002294 if (is_error_pfn(pfn))
2295 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002296
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002297 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002298 if (mmu_notifier_retry(vcpu, mmu_seq))
2299 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002300 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002301 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002302 spin_unlock(&vcpu->kvm->mmu_lock);
2303
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002304
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002305 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002306
2307out_unlock:
2308 spin_unlock(&vcpu->kvm->mmu_lock);
2309 kvm_release_pfn_clean(pfn);
2310 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002311}
2312
2313
Avi Kivity17ac10a2007-01-05 16:36:40 -08002314static void mmu_free_roots(struct kvm_vcpu *vcpu)
2315{
2316 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002317 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002318 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002319
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002320 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002321 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002322 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002323 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2324 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2325 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002326 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002327
Avi Kivity4db35312007-11-21 15:28:32 +02002328 sp = page_header(root);
2329 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002330 if (!sp->root_count && sp->role.invalid) {
2331 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2332 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2333 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002334 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002335 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002336 return;
2337 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002338 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002339 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002340
Avi Kivity417726a2007-04-12 17:35:58 +03002341 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002342 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002343 sp = page_header(root);
2344 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002345 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002346 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2347 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002348 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002350 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002351 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002352 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002353 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002354}
2355
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002356static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2357{
2358 int ret = 0;
2359
2360 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002361 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002362 ret = 1;
2363 }
2364
2365 return ret;
2366}
2367
Joerg Roedel651dd372010-09-10 17:30:59 +02002368static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2369{
2370 struct kvm_mmu_page *sp;
2371 int i;
2372
2373 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2374 spin_lock(&vcpu->kvm->mmu_lock);
2375 kvm_mmu_free_some_pages(vcpu);
2376 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2377 1, ACC_ALL, NULL);
2378 ++sp->root_count;
2379 spin_unlock(&vcpu->kvm->mmu_lock);
2380 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2381 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2382 for (i = 0; i < 4; ++i) {
2383 hpa_t root = vcpu->arch.mmu.pae_root[i];
2384
2385 ASSERT(!VALID_PAGE(root));
2386 spin_lock(&vcpu->kvm->mmu_lock);
2387 kvm_mmu_free_some_pages(vcpu);
2388 sp = kvm_mmu_get_page(vcpu, i << 30, i << 30,
2389 PT32_ROOT_LEVEL, 1, ACC_ALL,
2390 NULL);
2391 root = __pa(sp->spt);
2392 ++sp->root_count;
2393 spin_unlock(&vcpu->kvm->mmu_lock);
2394 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2395 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2396 }
2397 } else
2398 BUG();
2399
2400 return 0;
2401}
2402
2403static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002404{
Avi Kivity4db35312007-11-21 15:28:32 +02002405 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002406 u64 pdptr, pm_mask;
2407 gfn_t root_gfn;
2408 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002409
Joerg Roedel5777ed32010-09-10 17:30:42 +02002410 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002411
Joerg Roedel651dd372010-09-10 17:30:59 +02002412 if (mmu_check_root(vcpu, root_gfn))
2413 return 1;
2414
2415 /*
2416 * Do we shadow a long mode page table? If so we need to
2417 * write-protect the guests page table root.
2418 */
2419 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002420 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002421
2422 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002423
Avi Kivity8facbbf2010-05-04 12:58:32 +03002424 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002425 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002426 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2427 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002428 root = __pa(sp->spt);
2429 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002430 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002431 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002432 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002433 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002434
Joerg Roedel651dd372010-09-10 17:30:59 +02002435 /*
2436 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002437 * or a PAE 3-level page table. In either case we need to be aware that
2438 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002439 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002440 pm_mask = PT_PRESENT_MASK;
2441 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2442 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2443
Avi Kivity17ac10a2007-01-05 16:36:40 -08002444 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002445 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002446
2447 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002448 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002449 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002450 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002451 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002452 continue;
2453 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002454 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002455 if (mmu_check_root(vcpu, root_gfn))
2456 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002457 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002458 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002459 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002460 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002461 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002462 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002463 root = __pa(sp->spt);
2464 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002465 spin_unlock(&vcpu->kvm->mmu_lock);
2466
Joerg Roedel81407ca2010-09-10 17:31:00 +02002467 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
2468 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002469 }
Joerg Roedel81407ca2010-09-10 17:31:00 +02002470
2471 /*
2472 * If we shadow a 32 bit page table with a long mode page
2473 * table we enter this path.
2474 */
2475 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2476 if (vcpu->arch.mmu.lm_root == NULL) {
2477 /*
2478 * The additional page necessary for this is only
2479 * allocated on demand.
2480 */
2481
2482 u64 *lm_root;
2483
2484 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2485 if (lm_root == NULL)
2486 return 1;
2487
2488 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2489
2490 vcpu->arch.mmu.lm_root = lm_root;
2491 }
2492
2493 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2494 }
2495
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002496 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002497}
2498
Joerg Roedel651dd372010-09-10 17:30:59 +02002499static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2500{
2501 if (vcpu->arch.mmu.direct_map)
2502 return mmu_alloc_direct_roots(vcpu);
2503 else
2504 return mmu_alloc_shadow_roots(vcpu);
2505}
2506
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002507static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2508{
2509 int i;
2510 struct kvm_mmu_page *sp;
2511
Joerg Roedel81407ca2010-09-10 17:31:00 +02002512 if (vcpu->arch.mmu.direct_map)
2513 return;
2514
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002515 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2516 return;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002517 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002518 hpa_t root = vcpu->arch.mmu.root_hpa;
2519 sp = page_header(root);
2520 mmu_sync_children(vcpu, sp);
2521 return;
2522 }
2523 for (i = 0; i < 4; ++i) {
2524 hpa_t root = vcpu->arch.mmu.pae_root[i];
2525
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002526 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002527 root &= PT64_BASE_ADDR_MASK;
2528 sp = page_header(root);
2529 mmu_sync_children(vcpu, sp);
2530 }
2531 }
2532}
2533
2534void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2535{
2536 spin_lock(&vcpu->kvm->mmu_lock);
2537 mmu_sync_roots(vcpu);
2538 spin_unlock(&vcpu->kvm->mmu_lock);
2539}
2540
Gleb Natapov1871c602010-02-10 14:21:32 +02002541static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2542 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543{
Gleb Natapov1871c602010-02-10 14:21:32 +02002544 if (error)
2545 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 return vaddr;
2547}
2548
Joerg Roedel6539e732010-09-10 17:30:50 +02002549static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
2550 u32 access, u32 *error)
2551{
2552 if (error)
2553 *error = 0;
2554 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2555}
2556
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002558 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559{
Avi Kivitye8332402007-12-09 18:43:00 +02002560 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002561 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002563 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002564 r = mmu_topup_memory_caches(vcpu);
2565 if (r)
2566 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002567
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002569 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570
Avi Kivitye8332402007-12-09 18:43:00 +02002571 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572
Avi Kivitye8332402007-12-09 18:43:00 +02002573 return nonpaging_map(vcpu, gva & PAGE_MASK,
2574 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575}
2576
Joerg Roedelfb72d162008-02-07 13:47:44 +01002577static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2578 u32 error_code)
2579{
Anthony Liguori35149e22008-04-02 14:46:56 -05002580 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002581 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002582 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002583 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002584 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002585
2586 ASSERT(vcpu);
2587 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2588
2589 r = mmu_topup_memory_caches(vcpu);
2590 if (r)
2591 return r;
2592
Joerg Roedel852e3c12009-07-27 16:30:44 +02002593 level = mapping_level(vcpu, gfn);
2594
2595 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2596
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002597 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002598 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002599 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002600 if (is_error_pfn(pfn))
2601 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002602 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002603 if (mmu_notifier_retry(vcpu, mmu_seq))
2604 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002605 kvm_mmu_free_some_pages(vcpu);
2606 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002607 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002608 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002609
2610 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002611
2612out_unlock:
2613 spin_unlock(&vcpu->kvm->mmu_lock);
2614 kvm_release_pfn_clean(pfn);
2615 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002616}
2617
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618static void nonpaging_free(struct kvm_vcpu *vcpu)
2619{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002620 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621}
2622
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002623static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2624 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 context->new_cr3 = nonpaging_new_cr3;
2627 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 context->gva_to_gpa = nonpaging_gva_to_gpa;
2629 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002630 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002631 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002632 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002633 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002635 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002636 context->direct_map = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 return 0;
2638}
2639
Avi Kivityd835dfe2007-11-21 02:57:59 +02002640void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002642 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002643 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644}
2645
2646static void paging_new_cr3(struct kvm_vcpu *vcpu)
2647{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002648 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002649 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650}
2651
Joerg Roedel5777ed32010-09-10 17:30:42 +02002652static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2653{
2654 return vcpu->arch.cr3;
2655}
2656
Joerg Roedel8df25a32010-09-10 17:30:46 +02002657static void inject_page_fault(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658{
Joerg Roedel8df25a32010-09-10 17:30:46 +02002659 vcpu->arch.mmu.inject_page_fault(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660}
2661
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662static void paging_free(struct kvm_vcpu *vcpu)
2663{
2664 nonpaging_free(vcpu);
2665}
2666
Joerg Roedel3241f222010-09-10 17:30:45 +02002667static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002668{
2669 int bit7;
2670
2671 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002672 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002673}
2674
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675#define PTTYPE 64
2676#include "paging_tmpl.h"
2677#undef PTTYPE
2678
2679#define PTTYPE 32
2680#include "paging_tmpl.h"
2681#undef PTTYPE
2682
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002683static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2684 struct kvm_mmu *context,
2685 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002686{
Dong, Eddie82725b22009-03-30 16:21:08 +08002687 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2688 u64 exb_bit_rsvd = 0;
2689
2690 if (!is_nx(vcpu))
2691 exb_bit_rsvd = rsvd_bits(63, 63);
2692 switch (level) {
2693 case PT32_ROOT_LEVEL:
2694 /* no rsvd bits for 2 level 4K page table entries */
2695 context->rsvd_bits_mask[0][1] = 0;
2696 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002697 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2698
2699 if (!is_pse(vcpu)) {
2700 context->rsvd_bits_mask[1][1] = 0;
2701 break;
2702 }
2703
Dong, Eddie82725b22009-03-30 16:21:08 +08002704 if (is_cpuid_PSE36())
2705 /* 36bits PSE 4MB page */
2706 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2707 else
2708 /* 32 bits PSE 4MB page */
2709 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002710 break;
2711 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002712 context->rsvd_bits_mask[0][2] =
2713 rsvd_bits(maxphyaddr, 63) |
2714 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002715 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002716 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002717 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2718 rsvd_bits(maxphyaddr, 62); /* PTE */
2719 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2720 rsvd_bits(maxphyaddr, 62) |
2721 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002722 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002723 break;
2724 case PT64_ROOT_LEVEL:
2725 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2726 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2727 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2728 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2729 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002730 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002731 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2732 rsvd_bits(maxphyaddr, 51);
2733 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002734 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2735 rsvd_bits(maxphyaddr, 51) |
2736 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002737 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002738 rsvd_bits(maxphyaddr, 51) |
2739 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002740 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002741 break;
2742 }
2743}
2744
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002745static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2746 struct kvm_mmu *context,
2747 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002749 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750
2751 ASSERT(is_pae(vcpu));
2752 context->new_cr3 = paging_new_cr3;
2753 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002755 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002756 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002757 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002759 context->root_level = level;
2760 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002761 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002762 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 return 0;
2764}
2765
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002766static int paging64_init_context(struct kvm_vcpu *vcpu,
2767 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002768{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002769 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002770}
2771
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002772static int paging32_init_context(struct kvm_vcpu *vcpu,
2773 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002775 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776
2777 context->new_cr3 = paging_new_cr3;
2778 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 context->gva_to_gpa = paging32_gva_to_gpa;
2780 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002781 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002782 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002783 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 context->root_level = PT32_ROOT_LEVEL;
2785 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002786 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002787 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 return 0;
2789}
2790
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002791static int paging32E_init_context(struct kvm_vcpu *vcpu,
2792 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002794 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795}
2796
Joerg Roedelfb72d162008-02-07 13:47:44 +01002797static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2798{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002799 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002800
2801 context->new_cr3 = nonpaging_new_cr3;
2802 context->page_fault = tdp_page_fault;
2803 context->free = nonpaging_free;
2804 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002805 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002806 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002807 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002808 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002809 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002810 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002811 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002812 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002813
2814 if (!is_paging(vcpu)) {
2815 context->gva_to_gpa = nonpaging_gva_to_gpa;
2816 context->root_level = 0;
2817 } else if (is_long_mode(vcpu)) {
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002818 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002819 context->gva_to_gpa = paging64_gva_to_gpa;
2820 context->root_level = PT64_ROOT_LEVEL;
2821 } else if (is_pae(vcpu)) {
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002822 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002823 context->gva_to_gpa = paging64_gva_to_gpa;
2824 context->root_level = PT32E_ROOT_LEVEL;
2825 } else {
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002826 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002827 context->gva_to_gpa = paging32_gva_to_gpa;
2828 context->root_level = PT32_ROOT_LEVEL;
2829 }
2830
2831 return 0;
2832}
2833
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002834int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002836 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002838 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839
2840 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002841 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002842 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002843 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002845 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002847 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002848
Avi Kivity5b7e0102010-04-14 19:20:03 +03002849 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02002850 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002851
2852 return r;
2853}
2854EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2855
2856static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2857{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002858 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002859
Joerg Roedel14dfe852010-09-10 17:30:49 +02002860 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2861 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2862 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02002863
2864 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865}
2866
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002867static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2868{
2869 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2870
2871 g_context->get_cr3 = get_cr3;
2872 g_context->inject_page_fault = kvm_inject_page_fault;
2873
2874 /*
2875 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
2876 * translation of l2_gpa to l1_gpa addresses is done using the
2877 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
2878 * functions between mmu and nested_mmu are swapped.
2879 */
2880 if (!is_paging(vcpu)) {
2881 g_context->root_level = 0;
2882 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
2883 } else if (is_long_mode(vcpu)) {
2884 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
2885 g_context->root_level = PT64_ROOT_LEVEL;
2886 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2887 } else if (is_pae(vcpu)) {
2888 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
2889 g_context->root_level = PT32E_ROOT_LEVEL;
2890 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2891 } else {
2892 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
2893 g_context->root_level = PT32_ROOT_LEVEL;
2894 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
2895 }
2896
2897 return 0;
2898}
2899
Joerg Roedelfb72d162008-02-07 13:47:44 +01002900static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2901{
Anthony Liguori35149e22008-04-02 14:46:56 -05002902 vcpu->arch.update_pte.pfn = bad_pfn;
2903
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002904 if (mmu_is_nested(vcpu))
2905 return init_kvm_nested_mmu(vcpu);
2906 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002907 return init_kvm_tdp_mmu(vcpu);
2908 else
2909 return init_kvm_softmmu(vcpu);
2910}
2911
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2913{
2914 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002915 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2916 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002917 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918}
2919
2920int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2921{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002922 destroy_kvm_mmu(vcpu);
2923 return init_kvm_mmu(vcpu);
2924}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002925EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002926
2927int kvm_mmu_load(struct kvm_vcpu *vcpu)
2928{
Avi Kivity714b93d2007-01-05 16:36:53 -08002929 int r;
2930
Avi Kivitye2dec932007-01-05 16:36:54 -08002931 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002932 if (r)
2933 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002934 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002935 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002936 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002937 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002938 if (r)
2939 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002940 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02002941 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002942out:
2943 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002945EXPORT_SYMBOL_GPL(kvm_mmu_load);
2946
2947void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2948{
2949 mmu_free_roots(vcpu);
2950}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951
Avi Kivity09072da2007-05-01 14:16:52 +03002952static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002953 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002954 u64 *spte)
2955{
2956 u64 pte;
2957 struct kvm_mmu_page *child;
2958
2959 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002960 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002961 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002962 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002963 else {
2964 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002965 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002966 }
2967 }
Avi Kivityd555c332009-06-10 14:24:23 +03002968 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002969 if (is_large_pte(pte))
2970 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002971}
2972
Avi Kivity00284252007-05-01 16:53:31 +03002973static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002974 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002975 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002976 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002977{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002978 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002979 ++vcpu->kvm->stat.mmu_pde_zapped;
2980 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002981 }
Avi Kivity00284252007-05-01 16:53:31 +03002982
Joerg Roedel3241f222010-09-10 17:30:45 +02002983 if (is_rsvd_bits_set(&vcpu->arch.mmu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002984 return;
2985
Avi Kivity4cee5762007-11-18 16:37:07 +02002986 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002987 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002988 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002989 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002990 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002991}
2992
Avi Kivity79539ce2007-11-21 02:06:21 +02002993static bool need_remote_flush(u64 old, u64 new)
2994{
2995 if (!is_shadow_present_pte(old))
2996 return false;
2997 if (!is_shadow_present_pte(new))
2998 return true;
2999 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3000 return true;
3001 old ^= PT64_NX_MASK;
3002 new ^= PT64_NX_MASK;
3003 return (old & ~new & PT64_PERM_MASK) != 0;
3004}
3005
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003006static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3007 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003008{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003009 if (zap_page)
3010 return;
3011
3012 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003013 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003014 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003015 kvm_mmu_flush_tlb(vcpu);
3016}
3017
Avi Kivity12b7d282007-09-23 14:10:49 +02003018static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3019{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003020 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003021
Sheng Yang7b523452008-04-25 21:13:50 +08003022 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003023}
3024
Avi Kivityd7824ff2007-12-30 12:29:05 +02003025static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02003026 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02003027{
3028 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003029 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003030
Avi Kivity43a37952009-06-10 14:12:05 +03003031 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02003032 return;
3033 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003034
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003035 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003036 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05003037 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003038
Anthony Liguori35149e22008-04-02 14:46:56 -05003039 if (is_error_pfn(pfn)) {
3040 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02003041 return;
3042 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02003043 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003044 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003045}
3046
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003047static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3048{
3049 u64 *spte = vcpu->arch.last_pte_updated;
3050
3051 if (spte
3052 && vcpu->arch.last_pte_gfn == gfn
3053 && shadow_accessed_mask
3054 && !(*spte & shadow_accessed_mask)
3055 && is_shadow_present_pte(*spte))
3056 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3057}
3058
Avi Kivity09072da2007-05-01 14:16:52 +03003059void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003060 const u8 *new, int bytes,
3061 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003062{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003063 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003064 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003065 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003066 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003067 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02003068 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003069 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003070 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003071 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003072 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003073 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03003074 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003075 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003076 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02003077 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02003078 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02003079 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003080 bool remote_flush, local_flush, zap_page;
3081
3082 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003083
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003084 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003085
Avi Kivity08e850c2010-03-15 13:59:57 +02003086 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3087
3088 /*
3089 * Assume that the pte write on a page table of the same type
3090 * as the current vcpu paging mode. This is nearly always true
3091 * (might be false while changing modes). Note it is verified later
3092 * by update_pte().
3093 */
3094 if ((is_pae(vcpu) && bytes == 4) || !new) {
3095 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3096 if (is_pae(vcpu)) {
3097 gpa &= ~(gpa_t)7;
3098 bytes = 8;
3099 }
3100 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3101 if (r)
3102 gentry = 0;
3103 new = (const u8 *)&gentry;
3104 }
3105
Avi Kivity72016f32010-03-15 13:59:53 +02003106 switch (bytes) {
3107 case 4:
3108 gentry = *(const u32 *)new;
3109 break;
3110 case 8:
3111 gentry = *(const u64 *)new;
3112 break;
3113 default:
3114 gentry = 0;
3115 break;
3116 }
3117
Avi Kivity72016f32010-03-15 13:59:53 +02003118 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003119 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003120 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3121 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003122 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02003123 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003124 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003125 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003126 if (guest_initiated) {
3127 if (gfn == vcpu->arch.last_pt_write_gfn
3128 && !last_updated_pte_accessed(vcpu)) {
3129 ++vcpu->arch.last_pt_write_count;
3130 if (vcpu->arch.last_pt_write_count >= 3)
3131 flooded = 1;
3132 } else {
3133 vcpu->arch.last_pt_write_gfn = gfn;
3134 vcpu->arch.last_pt_write_count = 1;
3135 vcpu->arch.last_pte_updated = NULL;
3136 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003137 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003138
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003139 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003140 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003141 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003142 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003143 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003144 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003145 /*
3146 * Misaligned accesses are too much trouble to fix
3147 * up; also, they usually indicate a page is not used
3148 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003149 *
3150 * If we're seeing too many writes to a page,
3151 * it may no longer be a page table, or we may be
3152 * forking, in which case it is better to unmap the
3153 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003154 */
3155 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003156 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003157 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003158 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003159 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003160 continue;
3161 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003162 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003163 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003164 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003165 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003166 page_offset <<= 1; /* 32->64 */
3167 /*
3168 * A 32-bit pde maps 4MB while the shadow pdes map
3169 * only 2MB. So we need to double the offset again
3170 * and zap two pdes instead of one.
3171 */
3172 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003173 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003174 page_offset <<= 1;
3175 npte = 2;
3176 }
Avi Kivityfce06572007-05-01 16:44:05 +03003177 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003178 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003179 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003180 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003181 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003182 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003183 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003184 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003185 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003186 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003187 if (gentry &&
3188 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3189 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003190 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003191 if (!remote_flush && need_remote_flush(entry, *spte))
3192 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003193 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003194 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003195 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003196 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003197 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003198 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003199 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003200 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3201 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3202 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003203 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003204}
3205
Avi Kivitya4360362007-01-05 16:36:45 -08003206int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3207{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003208 gpa_t gpa;
3209 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003210
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003211 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003212 return 0;
3213
Gleb Natapov1871c602010-02-10 14:21:32 +02003214 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003215
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003216 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003217 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003218 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003219 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003220}
Avi Kivity577bdc42008-07-19 08:57:05 +03003221EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003222
Avi Kivity22d95b12007-09-14 20:26:06 +03003223void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003224{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003225 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003226
Dave Hansene0df7b92010-08-19 18:11:05 -07003227 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003228 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003229 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003230
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003231 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003232 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003233 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003234 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003235 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003236 }
3237}
Avi Kivityebeace82007-01-05 16:36:47 -08003238
Avi Kivity30677142007-10-28 18:48:59 +02003239int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3240{
3241 int r;
3242 enum emulation_result er;
3243
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003244 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02003245 if (r < 0)
3246 goto out;
3247
3248 if (!r) {
3249 r = 1;
3250 goto out;
3251 }
3252
Avi Kivityb733bfb2007-10-28 18:52:05 +02003253 r = mmu_topup_memory_caches(vcpu);
3254 if (r)
3255 goto out;
3256
Avi Kivity851ba692009-08-24 11:10:17 +03003257 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02003258
3259 switch (er) {
3260 case EMULATE_DONE:
3261 return 1;
3262 case EMULATE_DO_MMIO:
3263 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003264 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003265 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003266 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003267 default:
3268 BUG();
3269 }
3270out:
Avi Kivity30677142007-10-28 18:48:59 +02003271 return r;
3272}
3273EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3274
Marcelo Tosattia7052892008-09-23 13:18:35 -03003275void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3276{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003277 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003278 kvm_mmu_flush_tlb(vcpu);
3279 ++vcpu->stat.invlpg;
3280}
3281EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3282
Joerg Roedel18552672008-02-07 13:47:41 +01003283void kvm_enable_tdp(void)
3284{
3285 tdp_enabled = true;
3286}
3287EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3288
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003289void kvm_disable_tdp(void)
3290{
3291 tdp_enabled = false;
3292}
3293EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3294
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295static void free_mmu_pages(struct kvm_vcpu *vcpu)
3296{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003297 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003298 if (vcpu->arch.mmu.lm_root != NULL)
3299 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300}
3301
3302static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3303{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003304 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 int i;
3306
3307 ASSERT(vcpu);
3308
Avi Kivity17ac10a2007-01-05 16:36:40 -08003309 /*
3310 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3311 * Therefore we need to allocate shadow page tables in the first
3312 * 4GB of memory, which happens to fit the DMA32 zone.
3313 */
3314 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3315 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003316 return -ENOMEM;
3317
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003318 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003319 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003320 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003321
Avi Kivity6aa8b732006-12-10 02:21:36 -08003322 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323}
3324
Ingo Molnar8018c272006-12-29 16:50:01 -08003325int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003328 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329
Ingo Molnar8018c272006-12-29 16:50:01 -08003330 return alloc_mmu_pages(vcpu);
3331}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332
Ingo Molnar8018c272006-12-29 16:50:01 -08003333int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3334{
3335 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003336 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003337
Ingo Molnar8018c272006-12-29 16:50:01 -08003338 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339}
3340
3341void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3342{
3343 ASSERT(vcpu);
3344
3345 destroy_kvm_mmu(vcpu);
3346 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08003347 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348}
3349
Avi Kivity90cb0522007-07-17 13:04:56 +03003350void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003351{
Avi Kivity4db35312007-11-21 15:28:32 +02003352 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003354 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355 int i;
3356 u64 *pt;
3357
Sheng Yang291f26b2008-10-16 17:30:57 +08003358 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 continue;
3360
Avi Kivity4db35312007-11-21 15:28:32 +02003361 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3363 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003364 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 }
Avi Kivity171d5952008-08-27 16:40:51 +03003367 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369
Avi Kivity90cb0522007-07-17 13:04:56 +03003370void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003371{
Avi Kivity4db35312007-11-21 15:28:32 +02003372 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003373 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003374
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003375 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003376restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003377 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003378 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003379 goto restart;
3380
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003381 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003382 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003383}
3384
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003385static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3386 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003387{
3388 struct kvm_mmu_page *page;
3389
3390 page = container_of(kvm->arch.active_mmu_pages.prev,
3391 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003392 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003393}
3394
Dave Chinner7f8275d2010-07-19 14:56:17 +10003395static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003396{
3397 struct kvm *kvm;
3398 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003399
3400 if (nr_to_scan == 0)
3401 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003402
3403 spin_lock(&kvm_lock);
3404
3405 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003406 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003407 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003408
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003409 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003410 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003411 if (!kvm_freed && nr_to_scan > 0 &&
3412 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003413 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3414 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003415 kvm_freed = kvm;
3416 }
3417 nr_to_scan--;
3418
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003419 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003420 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003421 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003422 }
3423 if (kvm_freed)
3424 list_move_tail(&kvm_freed->vm_list, &vm_list);
3425
3426 spin_unlock(&kvm_lock);
3427
Dave Hansen45221ab2010-08-19 18:11:37 -07003428out:
3429 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003430}
3431
3432static struct shrinker mmu_shrinker = {
3433 .shrink = mmu_shrink,
3434 .seeks = DEFAULT_SEEKS * 10,
3435};
3436
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003437static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003438{
3439 if (pte_chain_cache)
3440 kmem_cache_destroy(pte_chain_cache);
3441 if (rmap_desc_cache)
3442 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003443 if (mmu_page_header_cache)
3444 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003445}
3446
Izik Eidus3ee16c82008-03-30 15:17:21 +03003447void kvm_mmu_module_exit(void)
3448{
3449 mmu_destroy_caches();
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003450 percpu_counter_destroy(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003451 unregister_shrinker(&mmu_shrinker);
3452}
3453
Avi Kivityb5a33a72007-04-15 16:31:09 +03003454int kvm_mmu_module_init(void)
3455{
3456 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3457 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003458 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003459 if (!pte_chain_cache)
3460 goto nomem;
3461 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3462 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003463 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003464 if (!rmap_desc_cache)
3465 goto nomem;
3466
Avi Kivityd3d25b02007-05-30 12:34:53 +03003467 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3468 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003469 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003470 if (!mmu_page_header_cache)
3471 goto nomem;
3472
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003473 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3474 goto nomem;
3475
Izik Eidus3ee16c82008-03-30 15:17:21 +03003476 register_shrinker(&mmu_shrinker);
3477
Avi Kivityb5a33a72007-04-15 16:31:09 +03003478 return 0;
3479
3480nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003481 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003482 return -ENOMEM;
3483}
3484
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003485/*
3486 * Caculate mmu pages needed for kvm.
3487 */
3488unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3489{
3490 int i;
3491 unsigned int nr_mmu_pages;
3492 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003493 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003494
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003495 slots = kvm_memslots(kvm);
3496
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003497 for (i = 0; i < slots->nmemslots; i++)
3498 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003499
3500 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3501 nr_mmu_pages = max(nr_mmu_pages,
3502 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3503
3504 return nr_mmu_pages;
3505}
3506
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003507static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3508 unsigned len)
3509{
3510 if (len > buffer->len)
3511 return NULL;
3512 return buffer->ptr;
3513}
3514
3515static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3516 unsigned len)
3517{
3518 void *ret;
3519
3520 ret = pv_mmu_peek_buffer(buffer, len);
3521 if (!ret)
3522 return ret;
3523 buffer->ptr += len;
3524 buffer->len -= len;
3525 buffer->processed += len;
3526 return ret;
3527}
3528
3529static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3530 gpa_t addr, gpa_t value)
3531{
3532 int bytes = 8;
3533 int r;
3534
3535 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3536 bytes = 4;
3537
3538 r = mmu_topup_memory_caches(vcpu);
3539 if (r)
3540 return r;
3541
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003542 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003543 return -EFAULT;
3544
3545 return 1;
3546}
3547
3548static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3549{
Avi Kivity23902182010-06-10 17:02:16 +03003550 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003551 return 1;
3552}
3553
3554static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3555{
3556 spin_lock(&vcpu->kvm->mmu_lock);
3557 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3558 spin_unlock(&vcpu->kvm->mmu_lock);
3559 return 1;
3560}
3561
3562static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3563 struct kvm_pv_mmu_op_buffer *buffer)
3564{
3565 struct kvm_mmu_op_header *header;
3566
3567 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3568 if (!header)
3569 return 0;
3570 switch (header->op) {
3571 case KVM_MMU_OP_WRITE_PTE: {
3572 struct kvm_mmu_op_write_pte *wpte;
3573
3574 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3575 if (!wpte)
3576 return 0;
3577 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3578 wpte->pte_val);
3579 }
3580 case KVM_MMU_OP_FLUSH_TLB: {
3581 struct kvm_mmu_op_flush_tlb *ftlb;
3582
3583 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3584 if (!ftlb)
3585 return 0;
3586 return kvm_pv_mmu_flush_tlb(vcpu);
3587 }
3588 case KVM_MMU_OP_RELEASE_PT: {
3589 struct kvm_mmu_op_release_pt *rpt;
3590
3591 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3592 if (!rpt)
3593 return 0;
3594 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3595 }
3596 default: return 0;
3597 }
3598}
3599
3600int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3601 gpa_t addr, unsigned long *ret)
3602{
3603 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003604 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003605
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003606 buffer->ptr = buffer->buf;
3607 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3608 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003609
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003610 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003611 if (r)
3612 goto out;
3613
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003614 while (buffer->len) {
3615 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003616 if (r < 0)
3617 goto out;
3618 if (r == 0)
3619 break;
3620 }
3621
3622 r = 1;
3623out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003624 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003625 return r;
3626}
3627
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003628int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3629{
3630 struct kvm_shadow_walk_iterator iterator;
3631 int nr_sptes = 0;
3632
3633 spin_lock(&vcpu->kvm->mmu_lock);
3634 for_each_shadow_entry(vcpu, addr, iterator) {
3635 sptes[iterator.level-1] = *iterator.sptep;
3636 nr_sptes++;
3637 if (!is_shadow_present_pte(*iterator.sptep))
3638 break;
3639 }
3640 spin_unlock(&vcpu->kvm->mmu_lock);
3641
3642 return nr_sptes;
3643}
3644EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3645
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003646#ifdef CONFIG_KVM_MMU_AUDIT
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08003647#include "mmu_audit.c"
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003648#endif