blob: c8acb9609ca482530923e4f4d87b926205b5c513 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
Avi Kivity221d0592010-05-23 18:37:00 +030010 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020022#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030023#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/highmem.h>
30#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020031#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030032#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050033#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020034#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080036#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
38#include <asm/page.h>
39#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020040#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020041#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040042
Joerg Roedel18552672008-02-07 13:47:41 +010043/*
44 * When setting this variable to true it enables Two-Dimensional-Paging
45 * where the hardware walks 2 page tables:
46 * 1. the guest-virtual to guest-physical
47 * 2. while doing 1. it walks guest-physical to host-physical
48 * If the hardware supports that we don't need to do shadow paging.
49 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050050bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010051
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080052enum {
53 AUDIT_PRE_PAGE_FAULT,
54 AUDIT_POST_PAGE_FAULT,
55 AUDIT_PRE_PTE_WRITE,
56 AUDIT_POST_PTE_WRITE
57};
58
59char *audit_point_name[] = {
60 "pre page fault",
61 "post page fault",
62 "pre pte write",
63 "post pte write"
64};
65
Avi Kivity37a7d8b2007-01-05 16:36:56 -080066#undef MMU_DEBUG
67
Avi Kivity37a7d8b2007-01-05 16:36:56 -080068#ifdef MMU_DEBUG
69
70#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
71#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
72
73#else
74
75#define pgprintk(x...) do { } while (0)
76#define rmap_printk(x...) do { } while (0)
77
78#endif
79
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080080#ifdef MMU_DEBUG
Avi Kivity6ada8cc2008-06-22 16:45:24 +030081static int dbg = 0;
82module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080083#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Marcelo Tosatti582801a2008-09-23 13:18:41 -030085static int oos_shadow = 1;
86module_param(oos_shadow, bool, 0644);
87
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080088#ifndef MMU_DEBUG
89#define ASSERT(x) do { } while (0)
90#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080091#define ASSERT(x) \
92 if (!(x)) { \
93 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
94 __FILE__, __LINE__, #x); \
95 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080096#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Xiao Guangrong957ed9e2010-08-22 19:12:48 +080098#define PTE_PREFETCH_NUM 8
99
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100#define PT_FIRST_AVAIL_BITS_SHIFT 9
101#define PT64_SECOND_AVAIL_BITS_SHIFT 52
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103#define PT64_LEVEL_BITS 9
104
105#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400106 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107
108#define PT64_LEVEL_MASK(level) \
109 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
110
111#define PT64_INDEX(address, level)\
112 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
113
114
115#define PT32_LEVEL_BITS 10
116
117#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400118 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
120#define PT32_LEVEL_MASK(level) \
121 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200122#define PT32_LVL_OFFSET_MASK(level) \
123 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
124 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125
126#define PT32_INDEX(address, level)\
127 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
128
129
Avi Kivity27aba762007-03-09 13:04:31 +0200130#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define PT64_DIR_BASE_ADDR_MASK \
132 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200133#define PT64_LVL_ADDR_MASK(level) \
134 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
135 * PT64_LEVEL_BITS))) - 1))
136#define PT64_LVL_OFFSET_MASK(level) \
137 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
138 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139
140#define PT32_BASE_ADDR_MASK PAGE_MASK
141#define PT32_DIR_BASE_ADDR_MASK \
142 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200143#define PT32_LVL_ADDR_MASK(level) \
144 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
145 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146
Avi Kivity79539ce2007-11-21 02:06:21 +0200147#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
148 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800150#define RMAP_EXT 4
151
Avi Kivityfe135d22007-12-09 16:15:46 +0200152#define ACC_EXEC_MASK 1
153#define ACC_WRITE_MASK PT_WRITABLE_MASK
154#define ACC_USER_MASK PT_USER_MASK
155#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
156
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200157#include <trace/events/kvm.h>
158
Avi Kivity07420172009-07-06 12:21:32 +0300159#define CREATE_TRACE_POINTS
160#include "mmutrace.h"
161
Izik Eidus14032832009-09-23 21:47:17 +0300162#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
163
Avi Kivity135f8c22008-08-21 17:49:56 +0300164#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
165
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800166struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300167 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800168 struct kvm_rmap_desc *more;
169};
170
Avi Kivity2d111232008-12-25 14:39:47 +0200171struct kvm_shadow_walk_iterator {
172 u64 addr;
173 hpa_t shadow_addr;
174 int level;
175 u64 *sptep;
176 unsigned index;
177};
178
179#define for_each_shadow_entry(_vcpu, _addr, _walker) \
180 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
181 shadow_walk_okay(&(_walker)); \
182 shadow_walk_next(&(_walker)))
183
Xiao Guangrong1047df12010-06-11 21:35:15 +0800184typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300185
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186static struct kmem_cache *pte_chain_cache;
187static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300188static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700189static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300190
Avi Kivityc7addb92007-09-16 18:58:32 +0200191static u64 __read_mostly shadow_trap_nonpresent_pte;
192static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800193static u64 __read_mostly shadow_base_present_pte;
194static u64 __read_mostly shadow_nx_mask;
195static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
196static u64 __read_mostly shadow_user_mask;
197static u64 __read_mostly shadow_accessed_mask;
198static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200199
Dong, Eddie82725b22009-03-30 16:21:08 +0800200static inline u64 rsvd_bits(int s, int e)
201{
202 return ((1ULL << (e - s + 1)) - 1) << s;
203}
204
Avi Kivityc7addb92007-09-16 18:58:32 +0200205void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
206{
207 shadow_trap_nonpresent_pte = trap_pte;
208 shadow_notrap_nonpresent_pte = notrap_pte;
209}
210EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
211
Sheng Yang7b523452008-04-25 21:13:50 +0800212void kvm_mmu_set_base_ptes(u64 base_pte)
213{
214 shadow_base_present_pte = base_pte;
215}
216EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
217
218void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800219 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800220{
221 shadow_user_mask = user_mask;
222 shadow_accessed_mask = accessed_mask;
223 shadow_dirty_mask = dirty_mask;
224 shadow_nx_mask = nx_mask;
225 shadow_x_mask = x_mask;
226}
227EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
228
Avi Kivity3dbe1412010-05-12 11:48:18 +0300229static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200231 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232}
233
234static int is_cpuid_PSE36(void)
235{
236 return 1;
237}
238
Avi Kivity73b10872007-01-26 00:56:41 -0800239static int is_nx(struct kvm_vcpu *vcpu)
240{
Avi Kivityf6801df2010-01-21 15:31:50 +0200241 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800242}
243
Avi Kivityc7addb92007-09-16 18:58:32 +0200244static int is_shadow_present_pte(u64 pte)
245{
Avi Kivityc7addb92007-09-16 18:58:32 +0200246 return pte != shadow_trap_nonpresent_pte
247 && pte != shadow_notrap_nonpresent_pte;
248}
249
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300250static int is_large_pte(u64 pte)
251{
252 return pte & PT_PAGE_SIZE_MASK;
253}
254
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900255static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256{
257 return pte & PT_WRITABLE_MASK;
258}
259
Avi Kivity43a37952009-06-10 14:12:05 +0300260static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200261{
Avi Kivity439e2182009-06-10 12:56:54 +0300262 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200263}
264
Avi Kivity43a37952009-06-10 14:12:05 +0300265static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800266{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200267 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800268}
269
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300270static int is_last_spte(u64 pte, int level)
271{
272 if (level == PT_PAGE_TABLE_LEVEL)
273 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200274 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300275 return 1;
276 return 0;
277}
278
Anthony Liguori35149e22008-04-02 14:46:56 -0500279static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200280{
Anthony Liguori35149e22008-04-02 14:46:56 -0500281 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200282}
283
Avi Kivityda928522007-11-21 13:54:47 +0200284static gfn_t pse36_gfn_delta(u32 gpte)
285{
286 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
287
288 return (gpte & PT32_DIR_PSE36_MASK) << shift;
289}
290
Avi Kivityd555c332009-06-10 14:24:23 +0300291static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300292{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700293 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300294}
295
Avi Kivitya9221dd2010-06-06 14:48:06 +0300296static u64 __xchg_spte(u64 *sptep, u64 new_spte)
297{
298#ifdef CONFIG_X86_64
299 return xchg(sptep, new_spte);
300#else
301 u64 old_spte;
302
303 do {
304 old_spte = *sptep;
305 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
306
307 return old_spte;
308#endif
309}
310
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800311static bool spte_has_volatile_bits(u64 spte)
312{
313 if (!shadow_accessed_mask)
314 return false;
315
316 if (!is_shadow_present_pte(spte))
317 return false;
318
Xiao Guangrong41327792010-08-02 16:15:08 +0800319 if ((spte & shadow_accessed_mask) &&
320 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800321 return false;
322
323 return true;
324}
325
Xiao Guangrong41327792010-08-02 16:15:08 +0800326static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
327{
328 return (old_spte & bit_mask) && !(new_spte & bit_mask);
329}
330
Avi Kivityb79b93f2010-06-06 15:46:44 +0300331static void update_spte(u64 *sptep, u64 new_spte)
332{
Xiao Guangrong41327792010-08-02 16:15:08 +0800333 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300334
Xiao Guangrong41327792010-08-02 16:15:08 +0800335 WARN_ON(!is_rmap_spte(new_spte));
336
337 new_spte |= old_spte & shadow_dirty_mask;
338
339 mask = shadow_accessed_mask;
340 if (is_writable_pte(old_spte))
341 mask |= shadow_dirty_mask;
342
343 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300344 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800345 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300346 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800347
348 if (!shadow_accessed_mask)
349 return;
350
351 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
352 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
353 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
354 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300355}
356
Avi Kivitye2dec932007-01-05 16:36:54 -0800357static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300358 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800359{
360 void *obj;
361
362 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800363 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800364 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300365 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800366 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800367 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800368 cache->objects[cache->nobjs++] = obj;
369 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800370 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800371}
372
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800373static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
374 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800375{
376 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800377 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800378}
379
Avi Kivityc1158e62007-07-20 08:18:27 +0300380static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300381 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300382{
383 struct page *page;
384
385 if (cache->nobjs >= min)
386 return 0;
387 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300388 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300389 if (!page)
390 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300391 cache->objects[cache->nobjs++] = page_address(page);
392 }
393 return 0;
394}
395
396static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
397{
398 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300399 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300400}
401
Avi Kivity8c438502007-04-16 11:53:17 +0300402static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
403{
404 int r;
405
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800406 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300407 pte_chain_cache, 4);
408 if (r)
409 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800410 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800411 rmap_desc_cache, 4 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300412 if (r)
413 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800414 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300415 if (r)
416 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800417 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300418 mmu_page_header_cache, 4);
419out:
Avi Kivity8c438502007-04-16 11:53:17 +0300420 return r;
421}
422
Avi Kivity714b93d2007-01-05 16:36:53 -0800423static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
424{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800425 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
426 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800427 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800428 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
429 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800430}
431
432static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
433 size_t size)
434{
435 void *p;
436
437 BUG_ON(!mc->nobjs);
438 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800439 return p;
440}
441
Avi Kivity714b93d2007-01-05 16:36:53 -0800442static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
443{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800444 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800445 sizeof(struct kvm_pte_chain));
446}
447
Avi Kivity90cb0522007-07-17 13:04:56 +0300448static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800449{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800450 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800451}
452
453static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
454{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800455 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800456 sizeof(struct kvm_rmap_desc));
457}
458
Avi Kivity90cb0522007-07-17 13:04:56 +0300459static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800460{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800461 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800462}
463
Lai Jiangshan2032a932010-05-26 16:49:59 +0800464static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
465{
466 if (!sp->role.direct)
467 return sp->gfns[index];
468
469 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
470}
471
472static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
473{
474 if (sp->role.direct)
475 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
476 else
477 sp->gfns[index] = gfn;
478}
479
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800480/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481 * Return the pointer to the largepage write count for a given
482 * gfn, handling slots that are not large page aligned.
483 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200484static int *slot_largepage_idx(gfn_t gfn,
485 struct kvm_memory_slot *slot,
486 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300487{
488 unsigned long idx;
489
Joerg Roedel82855412010-07-01 16:00:11 +0200490 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
491 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Joerg Roedeld25797b2009-07-27 16:30:43 +0200492 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493}
494
495static void account_shadowed(struct kvm *kvm, gfn_t gfn)
496{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200497 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300498 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200499 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300500
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300501 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200502 for (i = PT_DIRECTORY_LEVEL;
503 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
504 write_count = slot_largepage_idx(gfn, slot, i);
505 *write_count += 1;
506 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300507}
508
509static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
510{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200511 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300512 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200513 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300514
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300515 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516 for (i = PT_DIRECTORY_LEVEL;
517 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200518 write_count = slot_largepage_idx(gfn, slot, i);
519 *write_count -= 1;
520 WARN_ON(*write_count < 0);
521 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300522}
523
Joerg Roedeld25797b2009-07-27 16:30:43 +0200524static int has_wrprotected_page(struct kvm *kvm,
525 gfn_t gfn,
526 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300527{
Izik Eidus28430992008-10-03 17:40:32 +0300528 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300529 int *largepage_idx;
530
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300531 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300532 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200533 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534 return *largepage_idx;
535 }
536
537 return 1;
538}
539
Joerg Roedeld25797b2009-07-27 16:30:43 +0200540static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300541{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100542 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200543 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300544
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100545 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300546
Joerg Roedeld25797b2009-07-27 16:30:43 +0200547 for (i = PT_PAGE_TABLE_LEVEL;
548 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
549 if (page_size >= KVM_HPAGE_SIZE(i))
550 ret = i;
551 else
552 break;
553 }
554
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300555 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300556}
557
Joerg Roedeld25797b2009-07-27 16:30:43 +0200558static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300559{
560 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800561 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300562
563 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
564 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200565 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300566
Joerg Roedeld25797b2009-07-27 16:30:43 +0200567 host_level = host_mapping_level(vcpu->kvm, large_gfn);
568
569 if (host_level == PT_PAGE_TABLE_LEVEL)
570 return host_level;
571
Sheng Yang878403b2010-01-05 19:02:29 +0800572 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
573 kvm_x86_ops->get_lpage_level() : host_level;
574
575 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200576 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
577 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200578
579 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300580}
581
582/*
Izik Eidus290fc382007-09-27 14:11:22 +0200583 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200584 */
585
Joerg Roedel44ad9942009-07-27 16:30:42 +0200586static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200587{
588 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300589 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200590
591 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200592 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300593 return &slot->rmap[gfn - slot->base_gfn];
594
Joerg Roedel82855412010-07-01 16:00:11 +0200595 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
596 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300597
Joerg Roedel44ad9942009-07-27 16:30:42 +0200598 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200599}
600
601/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800602 * Reverse mapping data structures:
603 *
Izik Eidus290fc382007-09-27 14:11:22 +0200604 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
605 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 *
Izik Eidus290fc382007-09-27 14:11:22 +0200607 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
608 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300609 *
610 * Returns the number of rmap entries before the spte was added or zero if
611 * the spte was not added.
612 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200614static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615{
Avi Kivity4db35312007-11-21 15:28:32 +0200616 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800617 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200618 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300619 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620
Avi Kivity43a37952009-06-10 14:12:05 +0300621 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300622 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200623 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800624 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200625 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200626 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800627 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200628 *rmapp = (unsigned long)spte;
629 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800630 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800631 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300632 desc->sptes[0] = (u64 *)*rmapp;
633 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200634 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800635 } else {
636 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200637 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300638 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300640 count += RMAP_EXT;
641 }
Avi Kivityd555c332009-06-10 14:24:23 +0300642 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800643 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800644 desc = desc->more;
645 }
Avi Kivityd555c332009-06-10 14:24:23 +0300646 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800647 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300648 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800649 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300650 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800651}
652
Izik Eidus290fc382007-09-27 14:11:22 +0200653static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800654 struct kvm_rmap_desc *desc,
655 int i,
656 struct kvm_rmap_desc *prev_desc)
657{
658 int j;
659
Avi Kivityd555c332009-06-10 14:24:23 +0300660 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800661 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300662 desc->sptes[i] = desc->sptes[j];
663 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800664 if (j != 0)
665 return;
666 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300667 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800668 else
669 if (prev_desc)
670 prev_desc->more = desc->more;
671 else
Izik Eidus290fc382007-09-27 14:11:22 +0200672 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300673 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800674}
675
Izik Eidus290fc382007-09-27 14:11:22 +0200676static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800677{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800678 struct kvm_rmap_desc *desc;
679 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200680 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800681 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200682 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800683 int i;
684
Avi Kivity4db35312007-11-21 15:28:32 +0200685 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800686 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
687 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200688 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800689 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800690 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200691 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800692 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200693 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800694 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800695 BUG();
696 }
Izik Eidus290fc382007-09-27 14:11:22 +0200697 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800698 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800699 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200700 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800701 prev_desc = NULL;
702 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300703 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
704 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200705 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800706 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800707 prev_desc);
708 return;
709 }
710 prev_desc = desc;
711 desc = desc->more;
712 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800713 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800714 BUG();
715 }
716}
717
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800718static void set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300719{
Avi Kivityce061862010-06-06 14:38:12 +0300720 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800721 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300722
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800723 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800724 __set_spte(sptep, new_spte);
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800725 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800726 old_spte = __xchg_spte(sptep, new_spte);
727
Avi Kivitya9221dd2010-06-06 14:48:06 +0300728 if (!is_rmap_spte(old_spte))
Avi Kivityce061862010-06-06 14:38:12 +0300729 return;
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800730
Avi Kivitya9221dd2010-06-06 14:48:06 +0300731 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800732 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300733 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800734 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300735 kvm_set_pfn_dirty(pfn);
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800736}
737
738static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
739{
740 set_spte_track_bits(sptep, new_spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300741 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300742}
743
Izik Eidus98348e92007-10-16 14:42:30 +0200744static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800745{
Avi Kivity374cbac2007-01-05 16:36:43 -0800746 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200747 u64 *prev_spte;
748 int i;
749
750 if (!*rmapp)
751 return NULL;
752 else if (!(*rmapp & 1)) {
753 if (!spte)
754 return (u64 *)*rmapp;
755 return NULL;
756 }
757 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200758 prev_spte = NULL;
759 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300760 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200761 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300762 return desc->sptes[i];
763 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200764 }
765 desc = desc->more;
766 }
767 return NULL;
768}
769
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200770static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200771{
Izik Eidus290fc382007-09-27 14:11:22 +0200772 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800773 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200774 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800775
Joerg Roedel44ad9942009-07-27 16:30:42 +0200776 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800777
Izik Eidus98348e92007-10-16 14:42:30 +0200778 spte = rmap_next(kvm, rmapp, NULL);
779 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800780 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800781 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800782 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900783 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300784 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800785 write_protected = 1;
786 }
Izik Eidus9647c142007-10-16 14:43:46 +0200787 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800788 }
Izik Eidus855149a2008-03-20 18:17:24 +0200789
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300790 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200791 for (i = PT_DIRECTORY_LEVEL;
792 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
793 rmapp = gfn_to_rmap(kvm, gfn, i);
794 spte = rmap_next(kvm, rmapp, NULL);
795 while (spte) {
796 BUG_ON(!spte);
797 BUG_ON(!(*spte & PT_PRESENT_MASK));
798 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
799 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900800 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300801 drop_spte(kvm, spte,
802 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200803 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200804 spte = NULL;
805 write_protected = 1;
806 }
807 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300808 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300809 }
810
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200811 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800812}
813
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000814static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
815 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816{
817 u64 *spte;
818 int need_tlb_flush = 0;
819
820 while ((spte = rmap_next(kvm, rmapp, NULL))) {
821 BUG_ON(!(*spte & PT_PRESENT_MASK));
822 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300823 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200824 need_tlb_flush = 1;
825 }
826 return need_tlb_flush;
827}
828
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000829static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
830 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300831{
832 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800833 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300834 pte_t *ptep = (pte_t *)data;
835 pfn_t new_pfn;
836
837 WARN_ON(pte_huge(*ptep));
838 new_pfn = pte_pfn(*ptep);
839 spte = rmap_next(kvm, rmapp, NULL);
840 while (spte) {
841 BUG_ON(!is_shadow_present_pte(*spte));
842 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
843 need_flush = 1;
844 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300845 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300846 spte = rmap_next(kvm, rmapp, NULL);
847 } else {
848 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
849 new_spte |= (u64)new_pfn << PAGE_SHIFT;
850
851 new_spte &= ~PT_WRITABLE_MASK;
852 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300853 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800854 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300855 spte = rmap_next(kvm, rmapp, spte);
856 }
857 }
858 if (need_flush)
859 kvm_flush_remote_tlbs(kvm);
860
861 return 0;
862}
863
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000864static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
865 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300866 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000867 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200868{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200869 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200870 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200871 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200872 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200873
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800874 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200875
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200876 for (i = 0; i < slots->nmemslots; i++) {
877 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200878 unsigned long start = memslot->userspace_addr;
879 unsigned long end;
880
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200881 end = start + (memslot->npages << PAGE_SHIFT);
882 if (hva >= start && hva < end) {
883 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200884
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200885 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200886
887 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200888 unsigned long idx;
889 int sh;
890
891 sh = KVM_HPAGE_GFN_SHIFT(PT_DIRECTORY_LEVEL+j);
892 idx = ((memslot->base_gfn+gfn_offset) >> sh) -
893 (memslot->base_gfn >> sh);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200894 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300895 &memslot->lpage_info[j][idx].rmap_pde,
896 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200897 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200898 trace_kvm_age_page(hva, memslot, ret);
899 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200900 }
901 }
902
903 return retval;
904}
905
906int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
907{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300908 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200909}
910
Izik Eidus3da0dd42009-09-23 21:47:18 +0300911void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
912{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000913 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300914}
915
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000916static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
917 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200918{
919 u64 *spte;
920 int young = 0;
921
Rik van Riel6316e1c2010-02-03 16:11:03 -0500922 /*
923 * Emulate the accessed bit for EPT, by checking if this page has
924 * an EPT mapping, and clearing it if it does. On the next access,
925 * a new EPT mapping will be established.
926 * This has some overhead, but not as much as the cost of swapping
927 * out actively used pages or breaking up actively used hugepages.
928 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800929 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500930 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800931
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200932 spte = rmap_next(kvm, rmapp, NULL);
933 while (spte) {
934 int _young;
935 u64 _spte = *spte;
936 BUG_ON(!(_spte & PT_PRESENT_MASK));
937 _young = _spte & PT_ACCESSED_MASK;
938 if (_young) {
939 young = 1;
940 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
941 }
942 spte = rmap_next(kvm, rmapp, spte);
943 }
944 return young;
945}
946
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300947#define RMAP_RECYCLE_THRESHOLD 1000
948
Joerg Roedel852e3c12009-07-27 16:30:44 +0200949static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300950{
951 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200952 struct kvm_mmu_page *sp;
953
954 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300955
Joerg Roedel852e3c12009-07-27 16:30:44 +0200956 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300957
Izik Eidus3da0dd42009-09-23 21:47:18 +0300958 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300959 kvm_flush_remote_tlbs(vcpu->kvm);
960}
961
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200962int kvm_age_hva(struct kvm *kvm, unsigned long hva)
963{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300964 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200965}
966
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800967#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300968static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969{
Avi Kivity139bdb22007-01-05 16:36:50 -0800970 u64 *pos;
971 u64 *end;
972
Avi Kivity47ad8e62007-05-06 15:50:58 +0300973 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300974 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800975 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800976 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800978 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 return 1;
980}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800981#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Dave Hansen45221ab2010-08-19 18:11:37 -0700983/*
984 * This value is the sum of all of the kvm instances's
985 * kvm->arch.n_used_mmu_pages values. We need a global,
986 * aggregate version in order to make the slab shrinker
987 * faster
988 */
989static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
990{
991 kvm->arch.n_used_mmu_pages += nr;
992 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
993}
994
Avi Kivity4db35312007-11-21 15:28:32 +0200995static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800996{
Avi Kivity4db35312007-11-21 15:28:32 +0200997 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800998 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200999 list_del(&sp->link);
1000 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001001 if (!sp->role.direct)
1002 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001003 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001004 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001005}
1006
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007static unsigned kvm_page_table_hashfn(gfn_t gfn)
1008{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001009 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001010}
1011
Avi Kivity25c0de22007-01-05 16:36:42 -08001012static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001013 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014{
Avi Kivity4db35312007-11-21 15:28:32 +02001015 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001017 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1018 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001019 if (!direct)
1020 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1021 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001022 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001023 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001024 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001025 sp->multimapped = 0;
1026 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001027 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001028 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029}
1030
Avi Kivity714b93d2007-01-05 16:36:53 -08001031static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001032 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001033{
1034 struct kvm_pte_chain *pte_chain;
1035 struct hlist_node *node;
1036 int i;
1037
1038 if (!parent_pte)
1039 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001040 if (!sp->multimapped) {
1041 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001042
1043 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001044 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001045 return;
1046 }
Avi Kivity4db35312007-11-21 15:28:32 +02001047 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001048 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001049 INIT_HLIST_HEAD(&sp->parent_ptes);
1050 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001051 pte_chain->parent_ptes[0] = old;
1052 }
Avi Kivity4db35312007-11-21 15:28:32 +02001053 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001054 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1055 continue;
1056 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1057 if (!pte_chain->parent_ptes[i]) {
1058 pte_chain->parent_ptes[i] = parent_pte;
1059 return;
1060 }
1061 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001062 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001063 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001064 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001065 pte_chain->parent_ptes[0] = parent_pte;
1066}
1067
Avi Kivity4db35312007-11-21 15:28:32 +02001068static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001069 u64 *parent_pte)
1070{
1071 struct kvm_pte_chain *pte_chain;
1072 struct hlist_node *node;
1073 int i;
1074
Avi Kivity4db35312007-11-21 15:28:32 +02001075 if (!sp->multimapped) {
1076 BUG_ON(sp->parent_pte != parent_pte);
1077 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001078 return;
1079 }
Avi Kivity4db35312007-11-21 15:28:32 +02001080 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001081 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1082 if (!pte_chain->parent_ptes[i])
1083 break;
1084 if (pte_chain->parent_ptes[i] != parent_pte)
1085 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001086 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1087 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001088 pte_chain->parent_ptes[i]
1089 = pte_chain->parent_ptes[i + 1];
1090 ++i;
1091 }
1092 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001093 if (i == 0) {
1094 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001095 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001096 if (hlist_empty(&sp->parent_ptes)) {
1097 sp->multimapped = 0;
1098 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001099 }
1100 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001101 return;
1102 }
1103 BUG();
1104}
1105
Xiao Guangrong6b184932010-04-16 21:29:17 +08001106static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001107{
1108 struct kvm_pte_chain *pte_chain;
1109 struct hlist_node *node;
1110 struct kvm_mmu_page *parent_sp;
1111 int i;
1112
1113 if (!sp->multimapped && sp->parent_pte) {
1114 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001115 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001116 return;
1117 }
1118
1119 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1120 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001121 u64 *spte = pte_chain->parent_ptes[i];
1122
1123 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001125 parent_sp = page_header(__pa(spte));
1126 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001127 }
1128}
1129
Xiao Guangrong1047df12010-06-11 21:35:15 +08001130static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001131static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001132{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001133 mmu_parent_walk(sp, mark_unsync);
1134}
1135
1136static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1137{
1138 unsigned int index;
1139
1140 index = spte - sp->spt;
1141 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1142 return;
1143 if (sp->unsync_children++)
1144 return;
1145 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001146}
1147
Avi Kivityd761a502008-05-29 14:55:03 +03001148static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1149 struct kvm_mmu_page *sp)
1150{
1151 int i;
1152
1153 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1154 sp->spt[i] = shadow_trap_nonpresent_pte;
1155}
1156
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001157static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001158 struct kvm_mmu_page *sp, bool clear_unsync)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001159{
1160 return 1;
1161}
1162
Marcelo Tosattia7052892008-09-23 13:18:35 -03001163static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1164{
1165}
1166
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001167#define KVM_PAGE_ARRAY_NR 16
1168
1169struct kvm_mmu_pages {
1170 struct mmu_page_and_offset {
1171 struct kvm_mmu_page *sp;
1172 unsigned int idx;
1173 } page[KVM_PAGE_ARRAY_NR];
1174 unsigned int nr;
1175};
1176
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001177#define for_each_unsync_children(bitmap, idx) \
1178 for (idx = find_first_bit(bitmap, 512); \
1179 idx < 512; \
1180 idx = find_next_bit(bitmap, 512, idx+1))
1181
Hannes Edercded19f2009-02-21 02:19:13 +01001182static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1183 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001184{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001185 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001186
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001187 if (sp->unsync)
1188 for (i=0; i < pvec->nr; i++)
1189 if (pvec->page[i].sp == sp)
1190 return 0;
1191
1192 pvec->page[pvec->nr].sp = sp;
1193 pvec->page[pvec->nr].idx = idx;
1194 pvec->nr++;
1195 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1196}
1197
1198static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1199 struct kvm_mmu_pages *pvec)
1200{
1201 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001202
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001203 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001204 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001205 u64 ent = sp->spt[i];
1206
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001207 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1208 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001210 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001211
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001212 if (child->unsync_children) {
1213 if (mmu_pages_add(pvec, child, i))
1214 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001216 ret = __mmu_unsync_walk(child, pvec);
1217 if (!ret)
1218 goto clear_child_bitmap;
1219 else if (ret > 0)
1220 nr_unsync_leaf += ret;
1221 else
1222 return ret;
1223 } else if (child->unsync) {
1224 nr_unsync_leaf++;
1225 if (mmu_pages_add(pvec, child, i))
1226 return -ENOSPC;
1227 } else
1228 goto clear_child_bitmap;
1229
1230 continue;
1231
1232clear_child_bitmap:
1233 __clear_bit(i, sp->unsync_child_bitmap);
1234 sp->unsync_children--;
1235 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001236 }
1237
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001238
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001239 return nr_unsync_leaf;
1240}
1241
1242static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1243 struct kvm_mmu_pages *pvec)
1244{
1245 if (!sp->unsync_children)
1246 return 0;
1247
1248 mmu_pages_add(pvec, sp, 0);
1249 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250}
1251
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001252static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1253{
1254 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001255 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256 sp->unsync = 0;
1257 --kvm->stat.mmu_unsync;
1258}
1259
Xiao Guangrong77758342010-06-04 21:53:54 +08001260static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1261 struct list_head *invalid_list);
1262static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1263 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001264
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001265#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1266 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001267 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1268 if ((sp)->gfn != (gfn)) {} else
1269
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001270#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1271 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001272 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1273 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1274 (sp)->role.invalid) {} else
1275
Xiao Guangrongf918b442010-06-11 21:30:36 +08001276/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001277static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001278 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001279{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001280 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001281 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001282 return 1;
1283 }
1284
Xiao Guangrongf918b442010-06-11 21:30:36 +08001285 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001286 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001287
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001288 if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001289 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001290 return 1;
1291 }
1292
1293 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001294 return 0;
1295}
1296
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001297static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1298 struct kvm_mmu_page *sp)
1299{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001300 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001301 int ret;
1302
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001303 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001304 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001305 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1306
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001307 return ret;
1308}
1309
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001310static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1311 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001312{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001313 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001314}
1315
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001316/* @gfn should be write-protected at the call site */
1317static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1318{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001319 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001320 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001321 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001322 bool flush = false;
1323
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001324 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001325 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001326 continue;
1327
1328 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1329 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001330 (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001331 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001332 continue;
1333 }
1334 kvm_unlink_unsync_page(vcpu->kvm, s);
1335 flush = true;
1336 }
1337
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001338 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001339 if (flush)
1340 kvm_mmu_flush_tlb(vcpu);
1341}
1342
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001343struct mmu_page_path {
1344 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1345 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001346};
1347
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001348#define for_each_sp(pvec, sp, parents, i) \
1349 for (i = mmu_pages_next(&pvec, &parents, -1), \
1350 sp = pvec.page[i].sp; \
1351 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1352 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001353
Hannes Edercded19f2009-02-21 02:19:13 +01001354static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1355 struct mmu_page_path *parents,
1356 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001357{
1358 int n;
1359
1360 for (n = i+1; n < pvec->nr; n++) {
1361 struct kvm_mmu_page *sp = pvec->page[n].sp;
1362
1363 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1364 parents->idx[0] = pvec->page[n].idx;
1365 return n;
1366 }
1367
1368 parents->parent[sp->role.level-2] = sp;
1369 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1370 }
1371
1372 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373}
1374
Hannes Edercded19f2009-02-21 02:19:13 +01001375static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001376{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001377 struct kvm_mmu_page *sp;
1378 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001379
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001380 do {
1381 unsigned int idx = parents->idx[level];
1382
1383 sp = parents->parent[level];
1384 if (!sp)
1385 return;
1386
1387 --sp->unsync_children;
1388 WARN_ON((int)sp->unsync_children < 0);
1389 __clear_bit(idx, sp->unsync_child_bitmap);
1390 level++;
1391 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1392}
1393
1394static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1395 struct mmu_page_path *parents,
1396 struct kvm_mmu_pages *pvec)
1397{
1398 parents->parent[parent->role.level-1] = NULL;
1399 pvec->nr = 0;
1400}
1401
1402static void mmu_sync_children(struct kvm_vcpu *vcpu,
1403 struct kvm_mmu_page *parent)
1404{
1405 int i;
1406 struct kvm_mmu_page *sp;
1407 struct mmu_page_path parents;
1408 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001409 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001410
1411 kvm_mmu_pages_init(parent, &parents, &pages);
1412 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001413 int protected = 0;
1414
1415 for_each_sp(pages, sp, parents, i)
1416 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1417
1418 if (protected)
1419 kvm_flush_remote_tlbs(vcpu->kvm);
1420
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001421 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001422 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001423 mmu_pages_clear_parents(&parents);
1424 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001425 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001426 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001427 kvm_mmu_pages_init(parent, &parents, &pages);
1428 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001429}
1430
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001431static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1432 gfn_t gfn,
1433 gva_t gaddr,
1434 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001435 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001436 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001437 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001438{
1439 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001440 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001441 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001442 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001443 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001444
Avi Kivitya770f6f2008-12-21 19:20:09 +02001445 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001446 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001447 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001448 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001449 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001450 role.access = access;
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 Kivityf6e2c02b2009-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;
1507 if (iterator->level == PT32E_ROOT_LEVEL) {
1508 iterator->shadow_addr
1509 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1510 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1511 --iterator->level;
1512 if (!iterator->shadow_addr)
1513 iterator->level = 0;
1514 }
1515}
1516
1517static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1518{
1519 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1520 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001521
1522 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1523 if (is_large_pte(*iterator->sptep))
1524 return false;
1525
Avi Kivity2d111232008-12-25 14:39:47 +02001526 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1527 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1528 return true;
1529}
1530
1531static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1532{
1533 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1534 --iterator->level;
1535}
1536
Avi Kivity32ef26a2010-07-13 14:27:04 +03001537static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1538{
1539 u64 spte;
1540
1541 spte = __pa(sp->spt)
1542 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1543 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001544 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001545}
1546
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001547static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1548{
1549 if (is_large_pte(*sptep)) {
1550 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1551 kvm_flush_remote_tlbs(vcpu->kvm);
1552 }
1553}
1554
Avi Kivitya357bd22010-07-13 14:27:07 +03001555static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1556 unsigned direct_access)
1557{
1558 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1559 struct kvm_mmu_page *child;
1560
1561 /*
1562 * For the direct sp, if the guest pte's dirty bit
1563 * changed form clean to dirty, it will corrupt the
1564 * sp's access: allow writable in the read-only sp,
1565 * so we should update the spte at this point to get
1566 * a new sp with the correct access.
1567 */
1568 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1569 if (child->role.access == direct_access)
1570 return;
1571
1572 mmu_page_remove_parent_pte(child, sptep);
1573 __set_spte(sptep, shadow_trap_nonpresent_pte);
1574 kvm_flush_remote_tlbs(vcpu->kvm);
1575 }
1576}
1577
Avi Kivity90cb0522007-07-17 13:04:56 +03001578static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001579 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001580{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001581 unsigned i;
1582 u64 *pt;
1583 u64 ent;
1584
Avi Kivity4db35312007-11-21 15:28:32 +02001585 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001586
Avi Kivity697fe2e2007-01-05 16:36:46 -08001587 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1588 ent = pt[i];
1589
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001590 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001591 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001592 ent &= PT64_BASE_ADDR_MASK;
1593 mmu_page_remove_parent_pte(page_header(ent),
1594 &pt[i]);
1595 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001596 if (is_large_pte(ent))
1597 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001598 drop_spte(kvm, &pt[i],
1599 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001600 }
1601 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001602 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001603 }
Avi Kivitya4360362007-01-05 16:36:45 -08001604}
1605
Avi Kivity4db35312007-11-21 15:28:32 +02001606static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001607{
Avi Kivity4db35312007-11-21 15:28:32 +02001608 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001609}
1610
Avi Kivity12b7d282007-09-23 14:10:49 +02001611static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1612{
1613 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001614 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001615
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001616 kvm_for_each_vcpu(i, vcpu, kvm)
1617 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001618}
1619
Avi Kivity31aa2b42008-07-11 17:59:46 +03001620static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001621{
1622 u64 *parent_pte;
1623
Avi Kivity4db35312007-11-21 15:28:32 +02001624 while (sp->multimapped || sp->parent_pte) {
1625 if (!sp->multimapped)
1626 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001627 else {
1628 struct kvm_pte_chain *chain;
1629
Avi Kivity4db35312007-11-21 15:28:32 +02001630 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001631 struct kvm_pte_chain, link);
1632 parent_pte = chain->parent_ptes[0];
1633 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001634 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001635 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001636 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001637 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001638}
1639
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001640static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001641 struct kvm_mmu_page *parent,
1642 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001643{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001644 int i, zapped = 0;
1645 struct mmu_page_path parents;
1646 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001647
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001648 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001649 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001650
1651 kvm_mmu_pages_init(parent, &parents, &pages);
1652 while (mmu_unsync_walk(parent, &pages)) {
1653 struct kvm_mmu_page *sp;
1654
1655 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001656 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001657 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001658 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001659 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001660 kvm_mmu_pages_init(parent, &parents, &pages);
1661 }
1662
1663 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001664}
1665
Xiao Guangrong77758342010-06-04 21:53:54 +08001666static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1667 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001668{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001669 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001670
Xiao Guangrong77758342010-06-04 21:53:54 +08001671 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001672 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001673 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001674 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001675 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001676 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001677 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001678 if (sp->unsync)
1679 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001680 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001681 /* Count self */
1682 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001683 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001684 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001685 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001686 kvm_reload_remote_mmus(kvm);
1687 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001688
1689 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001690 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001691 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001692}
1693
Xiao Guangrong77758342010-06-04 21:53:54 +08001694static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1695 struct list_head *invalid_list)
1696{
1697 struct kvm_mmu_page *sp;
1698
1699 if (list_empty(invalid_list))
1700 return;
1701
1702 kvm_flush_remote_tlbs(kvm);
1703
1704 do {
1705 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1706 WARN_ON(!sp->role.invalid || sp->root_count);
1707 kvm_mmu_free_page(kvm, sp);
1708 } while (!list_empty(invalid_list));
1709
1710}
1711
Izik Eidus82ce2c92007-10-02 18:52:55 +02001712/*
1713 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001714 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001715 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001716void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001717{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001718 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001719 /*
1720 * If we set the number of mmu pages to be smaller be than the
1721 * number of actived pages , we must to free some mmu pages before we
1722 * change the value
1723 */
1724
Dave Hansen49d5ca22010-08-19 18:11:28 -07001725 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1726 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001727 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001728 struct kvm_mmu_page *page;
1729
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001730 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001731 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001732 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1733 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001734 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001735 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001736 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001737
Dave Hansen49d5ca22010-08-19 18:11:28 -07001738 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001739}
1740
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001741static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001742{
Avi Kivity4db35312007-11-21 15:28:32 +02001743 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001744 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001745 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001746 int r;
1747
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001748 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001749 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001750
1751 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001752 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001753 sp->role.word);
1754 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001755 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001756 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001757 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001758 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001759}
1760
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001761static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001762{
Avi Kivity4db35312007-11-21 15:28:32 +02001763 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001764 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001765 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001766
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001767 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001768 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001769 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001770 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001771 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001772 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001773}
1774
Avi Kivity38c335f2007-11-21 14:20:22 +02001775static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001777 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001778 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779
Sheng Yang291f26b2008-10-16 17:30:57 +08001780 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781}
1782
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001783static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1784{
1785 int i;
1786 u64 *pt = sp->spt;
1787
1788 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1789 return;
1790
1791 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1792 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001793 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001794 }
1795}
1796
Sheng Yang74be52e2008-10-09 16:01:56 +08001797/*
1798 * The function is based on mtrr_type_lookup() in
1799 * arch/x86/kernel/cpu/mtrr/generic.c
1800 */
1801static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1802 u64 start, u64 end)
1803{
1804 int i;
1805 u64 base, mask;
1806 u8 prev_match, curr_match;
1807 int num_var_ranges = KVM_NR_VAR_MTRR;
1808
1809 if (!mtrr_state->enabled)
1810 return 0xFF;
1811
1812 /* Make end inclusive end, instead of exclusive */
1813 end--;
1814
1815 /* Look in fixed ranges. Just return the type as per start */
1816 if (mtrr_state->have_fixed && (start < 0x100000)) {
1817 int idx;
1818
1819 if (start < 0x80000) {
1820 idx = 0;
1821 idx += (start >> 16);
1822 return mtrr_state->fixed_ranges[idx];
1823 } else if (start < 0xC0000) {
1824 idx = 1 * 8;
1825 idx += ((start - 0x80000) >> 14);
1826 return mtrr_state->fixed_ranges[idx];
1827 } else if (start < 0x1000000) {
1828 idx = 3 * 8;
1829 idx += ((start - 0xC0000) >> 12);
1830 return mtrr_state->fixed_ranges[idx];
1831 }
1832 }
1833
1834 /*
1835 * Look in variable ranges
1836 * Look of multiple ranges matching this address and pick type
1837 * as per MTRR precedence
1838 */
1839 if (!(mtrr_state->enabled & 2))
1840 return mtrr_state->def_type;
1841
1842 prev_match = 0xFF;
1843 for (i = 0; i < num_var_ranges; ++i) {
1844 unsigned short start_state, end_state;
1845
1846 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1847 continue;
1848
1849 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1850 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1851 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1852 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1853
1854 start_state = ((start & mask) == (base & mask));
1855 end_state = ((end & mask) == (base & mask));
1856 if (start_state != end_state)
1857 return 0xFE;
1858
1859 if ((start & mask) != (base & mask))
1860 continue;
1861
1862 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1863 if (prev_match == 0xFF) {
1864 prev_match = curr_match;
1865 continue;
1866 }
1867
1868 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1869 curr_match == MTRR_TYPE_UNCACHABLE)
1870 return MTRR_TYPE_UNCACHABLE;
1871
1872 if ((prev_match == MTRR_TYPE_WRBACK &&
1873 curr_match == MTRR_TYPE_WRTHROUGH) ||
1874 (prev_match == MTRR_TYPE_WRTHROUGH &&
1875 curr_match == MTRR_TYPE_WRBACK)) {
1876 prev_match = MTRR_TYPE_WRTHROUGH;
1877 curr_match = MTRR_TYPE_WRTHROUGH;
1878 }
1879
1880 if (prev_match != curr_match)
1881 return MTRR_TYPE_UNCACHABLE;
1882 }
1883
1884 if (prev_match != 0xFF)
1885 return prev_match;
1886
1887 return mtrr_state->def_type;
1888}
1889
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001890u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001891{
1892 u8 mtrr;
1893
1894 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1895 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1896 if (mtrr == 0xfe || mtrr == 0xff)
1897 mtrr = MTRR_TYPE_WRBACK;
1898 return mtrr;
1899}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001900EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001901
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001902static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001903{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001904 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001905 ++vcpu->kvm->stat.mmu_unsync;
1906 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001907
Xiao Guangrong6b184932010-04-16 21:29:17 +08001908 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001909 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001910}
1911
1912static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1913{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001914 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001915 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001916
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001917 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001918 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001919 continue;
1920 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1921 __kvm_unsync_page(vcpu, s);
1922 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001923}
1924
1925static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1926 bool can_unsync)
1927{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001928 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001929 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001930 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001931
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001932 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001933 if (!can_unsync)
1934 return 1;
1935
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001936 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001937 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001938
1939 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001940 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001941 return 1;
1942 need_unsync = true;
1943 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001944 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001945 if (need_unsync)
1946 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001947 return 0;
1948}
1949
Avi Kivityd555c332009-06-10 14:24:23 +03001950static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001951 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001952 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001953 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001954 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001955{
1956 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001957 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001958
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001959 /*
1960 * We don't set the accessed bit, since we sometimes want to see
1961 * whether the guest actually used the pte (in order to detect
1962 * demand paging).
1963 */
Xiao Guangrong41327792010-08-02 16:15:08 +08001964 spte = shadow_base_present_pte;
Avi Kivity947da532008-03-18 11:05:52 +02001965 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001966 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001967 if (!dirty)
1968 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001969 if (pte_access & ACC_EXEC_MASK)
1970 spte |= shadow_x_mask;
1971 else
1972 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001974 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001975 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001976 spte |= PT_PAGE_SIZE_MASK;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001977 if (vcpu->arch.mmu.direct_map)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001978 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1979 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001980
Izik Eidus14032832009-09-23 21:47:17 +03001981 if (reset_host_protection)
1982 spte |= SPTE_HOST_WRITEABLE;
1983
Anthony Liguori35149e22008-04-02 14:46:56 -05001984 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001985
1986 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001987 || (!vcpu->arch.mmu.direct_map && write_fault
1988 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001989
Joerg Roedel852e3c12009-07-27 16:30:44 +02001990 if (level > PT_PAGE_TABLE_LEVEL &&
1991 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001992 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001993 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1994 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001995 }
1996
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001997 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001998
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001999 if (!vcpu->arch.mmu.direct_map
2000 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03002001 spte &= ~PT_USER_MASK;
2002
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002003 /*
2004 * Optimization: for pte sync, if spte was writable the hash
2005 * lookup is unnecessary (and expensive). Write protection
2006 * is responsibility of mmu_get_page / kvm_sync_page.
2007 * Same reasoning can be applied to dirty page accounting.
2008 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002009 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002010 goto set_pte;
2011
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002012 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002013 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002014 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002015 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002016 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002017 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002018 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002019 }
2020 }
2021
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002022 if (pte_access & ACC_WRITE_MASK)
2023 mark_page_dirty(vcpu->kvm, gfn);
2024
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002025set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002026 update_spte(sptep, spte);
Avi Kivitybe38d272010-06-06 14:31:27 +03002027done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002028 return ret;
2029}
2030
Avi Kivityd555c332009-06-10 14:24:23 +03002031static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002032 unsigned pt_access, unsigned pte_access,
2033 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002034 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002035 pfn_t pfn, bool speculative,
2036 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002037{
2038 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002039 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002040
2041 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002042 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002043 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002044 write_fault, user_fault, gfn);
2045
Avi Kivityd555c332009-06-10 14:24:23 +03002046 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002047 /*
2048 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2049 * the parent of the now unreachable PTE.
2050 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002051 if (level > PT_PAGE_TABLE_LEVEL &&
2052 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002053 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002054 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002055
2056 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002057 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002058 __set_spte(sptep, shadow_trap_nonpresent_pte);
2059 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002060 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002061 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002062 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002063 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002064 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002065 } else
2066 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002067 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002068
Avi Kivityd555c332009-06-10 14:24:23 +03002069 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002070 dirty, level, gfn, pfn, speculative, true,
2071 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002072 if (write_fault)
2073 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002074 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002075 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002076
Avi Kivityd555c332009-06-10 14:24:23 +03002077 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002078 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002079 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002080 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2081 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002082 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002083 ++vcpu->kvm->stat.lpages;
2084
Avi Kivityd555c332009-06-10 14:24:23 +03002085 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002086 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002087 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002088 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002089 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002090 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002091 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002092 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002093 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002094 vcpu->arch.last_pte_gfn = gfn;
2095 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002096}
2097
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2099{
2100}
2101
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002102static struct kvm_memory_slot *
2103pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2104{
2105 struct kvm_memory_slot *slot;
2106
2107 slot = gfn_to_memslot(vcpu->kvm, gfn);
2108 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2109 (no_dirty_log && slot->dirty_bitmap))
2110 slot = NULL;
2111
2112 return slot;
2113}
2114
2115static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2116 bool no_dirty_log)
2117{
2118 struct kvm_memory_slot *slot;
2119 unsigned long hva;
2120
2121 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2122 if (!slot) {
2123 get_page(bad_page);
2124 return page_to_pfn(bad_page);
2125 }
2126
2127 hva = gfn_to_hva_memslot(slot, gfn);
2128
2129 return hva_to_pfn_atomic(vcpu->kvm, hva);
2130}
2131
2132static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2133 struct kvm_mmu_page *sp,
2134 u64 *start, u64 *end)
2135{
2136 struct page *pages[PTE_PREFETCH_NUM];
2137 unsigned access = sp->role.access;
2138 int i, ret;
2139 gfn_t gfn;
2140
2141 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2142 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2143 return -1;
2144
2145 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2146 if (ret <= 0)
2147 return -1;
2148
2149 for (i = 0; i < ret; i++, gfn++, start++)
2150 mmu_set_spte(vcpu, start, ACC_ALL,
2151 access, 0, 0, 1, NULL,
2152 sp->role.level, gfn,
2153 page_to_pfn(pages[i]), true, true);
2154
2155 return 0;
2156}
2157
2158static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2159 struct kvm_mmu_page *sp, u64 *sptep)
2160{
2161 u64 *spte, *start = NULL;
2162 int i;
2163
2164 WARN_ON(!sp->role.direct);
2165
2166 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2167 spte = sp->spt + i;
2168
2169 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2170 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2171 if (!start)
2172 continue;
2173 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2174 break;
2175 start = NULL;
2176 } else if (!start)
2177 start = spte;
2178 }
2179}
2180
2181static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2182{
2183 struct kvm_mmu_page *sp;
2184
2185 /*
2186 * Since it's no accessed bit on EPT, it's no way to
2187 * distinguish between actually accessed translations
2188 * and prefetched, so disable pte prefetch if EPT is
2189 * enabled.
2190 */
2191 if (!shadow_accessed_mask)
2192 return;
2193
2194 sp = page_header(__pa(sptep));
2195 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2196 return;
2197
2198 __direct_pte_prefetch(vcpu, sp, sptep);
2199}
2200
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002201static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002202 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
Avi Kivity9f652d22008-12-25 14:54:25 +02002204 struct kvm_shadow_walk_iterator iterator;
2205 struct kvm_mmu_page *sp;
2206 int pt_write = 0;
2207 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208
Avi Kivity9f652d22008-12-25 14:54:25 +02002209 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002210 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002211 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2212 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002213 level, gfn, pfn, false, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002214 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002215 ++vcpu->stat.pf_fixed;
2216 break;
2217 }
2218
2219 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002220 u64 base_addr = iterator.addr;
2221
2222 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2223 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002224 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2225 iterator.level - 1,
2226 1, ACC_ALL, iterator.sptep);
2227 if (!sp) {
2228 pgprintk("nonpaging_map: ENOMEM\n");
2229 kvm_release_pfn_clean(pfn);
2230 return -ENOMEM;
2231 }
2232
Avi Kivityd555c332009-06-10 14:24:23 +03002233 __set_spte(iterator.sptep,
2234 __pa(sp->spt)
2235 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2236 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002237 }
2238 }
2239 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240}
2241
Huang Yingbf998152010-05-31 14:28:19 +08002242static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2243{
2244 char buf[1];
2245 void __user *hva;
2246 int r;
2247
2248 /* Touch the page, so send SIGBUS */
2249 hva = (void __user *)gfn_to_hva(kvm, gfn);
2250 r = copy_from_user(buf, hva, 1);
2251}
2252
2253static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2254{
2255 kvm_release_pfn_clean(pfn);
2256 if (is_hwpoison_pfn(pfn)) {
2257 kvm_send_hwpoison_signal(kvm, gfn);
2258 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002259 } else if (is_fault_pfn(pfn))
2260 return -EFAULT;
2261
Huang Yingbf998152010-05-31 14:28:19 +08002262 return 1;
2263}
2264
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002265static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2266{
2267 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002268 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002269 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002270 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002271
Joerg Roedel852e3c12009-07-27 16:30:44 +02002272 level = mapping_level(vcpu, gfn);
2273
2274 /*
2275 * This path builds a PAE pagetable - so we can map 2mb pages at
2276 * maximum. Therefore check if the level is larger than that.
2277 */
2278 if (level > PT_DIRECTORY_LEVEL)
2279 level = PT_DIRECTORY_LEVEL;
2280
2281 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002282
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002283 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002284 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002285 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002286
Avi Kivityd196e342008-01-24 11:44:11 +02002287 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002288 if (is_error_pfn(pfn))
2289 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002290
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002291 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002292 if (mmu_notifier_retry(vcpu, mmu_seq))
2293 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002294 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002295 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002296 spin_unlock(&vcpu->kvm->mmu_lock);
2297
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002298
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002299 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002300
2301out_unlock:
2302 spin_unlock(&vcpu->kvm->mmu_lock);
2303 kvm_release_pfn_clean(pfn);
2304 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002305}
2306
2307
Avi Kivity17ac10a2007-01-05 16:36:40 -08002308static void mmu_free_roots(struct kvm_vcpu *vcpu)
2309{
2310 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002311 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002312 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002313
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002314 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002315 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002316 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002317 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2318 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002319
Avi Kivity4db35312007-11-21 15:28:32 +02002320 sp = page_header(root);
2321 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002322 if (!sp->root_count && sp->role.invalid) {
2323 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2324 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2325 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002326 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002327 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002328 return;
2329 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002330 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002331 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002332
Avi Kivity417726a2007-04-12 17:35:58 +03002333 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002334 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002335 sp = page_header(root);
2336 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002337 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002338 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2339 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002340 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002341 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002342 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002343 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002344 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002345 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002346}
2347
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002348static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2349{
2350 int ret = 0;
2351
2352 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002353 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002354 ret = 1;
2355 }
2356
2357 return ret;
2358}
2359
2360static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002361{
2362 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002363 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002364 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002365 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002366 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002367
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002368 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002369
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002370 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2371 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002372
2373 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002374 if (mmu_check_root(vcpu, root_gfn))
2375 return 1;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002376 if (vcpu->arch.mmu.direct_map) {
Eric Northup5a7388c2010-04-26 17:00:05 -07002377 direct = 1;
2378 root_gfn = 0;
2379 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002380 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002381 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002382 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002383 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002384 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002385 root = __pa(sp->spt);
2386 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002387 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002388 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002389 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002390 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002391 direct = !is_paging(vcpu);
Joerg Roedelf87f9282010-09-02 17:29:45 +02002392
2393 if (mmu_check_root(vcpu, root_gfn))
2394 return 1;
2395
Avi Kivity17ac10a2007-01-05 16:36:40 -08002396 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002397 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002398
2399 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002400 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002401 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002402 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002403 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002404 continue;
2405 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002406 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002407 if (mmu_check_root(vcpu, root_gfn))
2408 return 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002409 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002410 root_gfn = 0;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002411 if (vcpu->arch.mmu.direct_map) {
Eric Northup5a7388c2010-04-26 17:00:05 -07002412 direct = 1;
2413 root_gfn = i << 30;
2414 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002415 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002416 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002417 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002418 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002419 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002420 root = __pa(sp->spt);
2421 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002422 spin_unlock(&vcpu->kvm->mmu_lock);
2423
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002424 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002425 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002426 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002427 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002428}
2429
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002430static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2431{
2432 int i;
2433 struct kvm_mmu_page *sp;
2434
2435 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2436 return;
2437 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2438 hpa_t root = vcpu->arch.mmu.root_hpa;
2439 sp = page_header(root);
2440 mmu_sync_children(vcpu, sp);
2441 return;
2442 }
2443 for (i = 0; i < 4; ++i) {
2444 hpa_t root = vcpu->arch.mmu.pae_root[i];
2445
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002446 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002447 root &= PT64_BASE_ADDR_MASK;
2448 sp = page_header(root);
2449 mmu_sync_children(vcpu, sp);
2450 }
2451 }
2452}
2453
2454void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2455{
2456 spin_lock(&vcpu->kvm->mmu_lock);
2457 mmu_sync_roots(vcpu);
2458 spin_unlock(&vcpu->kvm->mmu_lock);
2459}
2460
Gleb Natapov1871c602010-02-10 14:21:32 +02002461static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2462 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463{
Gleb Natapov1871c602010-02-10 14:21:32 +02002464 if (error)
2465 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 return vaddr;
2467}
2468
2469static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002470 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471{
Avi Kivitye8332402007-12-09 18:43:00 +02002472 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002473 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002475 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002476 r = mmu_topup_memory_caches(vcpu);
2477 if (r)
2478 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002479
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002481 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482
Avi Kivitye8332402007-12-09 18:43:00 +02002483 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484
Avi Kivitye8332402007-12-09 18:43:00 +02002485 return nonpaging_map(vcpu, gva & PAGE_MASK,
2486 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487}
2488
Joerg Roedelfb72d162008-02-07 13:47:44 +01002489static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2490 u32 error_code)
2491{
Anthony Liguori35149e22008-04-02 14:46:56 -05002492 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002493 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002494 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002495 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002496 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002497
2498 ASSERT(vcpu);
2499 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2500
2501 r = mmu_topup_memory_caches(vcpu);
2502 if (r)
2503 return r;
2504
Joerg Roedel852e3c12009-07-27 16:30:44 +02002505 level = mapping_level(vcpu, gfn);
2506
2507 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2508
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002509 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002510 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002511 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002512 if (is_error_pfn(pfn))
2513 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002514 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002515 if (mmu_notifier_retry(vcpu, mmu_seq))
2516 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002517 kvm_mmu_free_some_pages(vcpu);
2518 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002519 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002520 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002521
2522 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002523
2524out_unlock:
2525 spin_unlock(&vcpu->kvm->mmu_lock);
2526 kvm_release_pfn_clean(pfn);
2527 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002528}
2529
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530static void nonpaging_free(struct kvm_vcpu *vcpu)
2531{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002532 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533}
2534
2535static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2536{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002537 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538
2539 context->new_cr3 = nonpaging_new_cr3;
2540 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 context->gva_to_gpa = nonpaging_gva_to_gpa;
2542 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002543 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002544 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002545 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002546 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002548 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002549 context->direct_map = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 return 0;
2551}
2552
Avi Kivityd835dfe2007-11-21 02:57:59 +02002553void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002555 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002556 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557}
2558
2559static void paging_new_cr3(struct kvm_vcpu *vcpu)
2560{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002561 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002562 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563}
2564
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565static void inject_page_fault(struct kvm_vcpu *vcpu,
2566 u64 addr,
2567 u32 err_code)
2568{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002569 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570}
2571
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572static void paging_free(struct kvm_vcpu *vcpu)
2573{
2574 nonpaging_free(vcpu);
2575}
2576
Dong, Eddie82725b22009-03-30 16:21:08 +08002577static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2578{
2579 int bit7;
2580
2581 bit7 = (gpte >> 7) & 1;
2582 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2583}
2584
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585#define PTTYPE 64
2586#include "paging_tmpl.h"
2587#undef PTTYPE
2588
2589#define PTTYPE 32
2590#include "paging_tmpl.h"
2591#undef PTTYPE
2592
Dong, Eddie82725b22009-03-30 16:21:08 +08002593static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2594{
2595 struct kvm_mmu *context = &vcpu->arch.mmu;
2596 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2597 u64 exb_bit_rsvd = 0;
2598
2599 if (!is_nx(vcpu))
2600 exb_bit_rsvd = rsvd_bits(63, 63);
2601 switch (level) {
2602 case PT32_ROOT_LEVEL:
2603 /* no rsvd bits for 2 level 4K page table entries */
2604 context->rsvd_bits_mask[0][1] = 0;
2605 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002606 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2607
2608 if (!is_pse(vcpu)) {
2609 context->rsvd_bits_mask[1][1] = 0;
2610 break;
2611 }
2612
Dong, Eddie82725b22009-03-30 16:21:08 +08002613 if (is_cpuid_PSE36())
2614 /* 36bits PSE 4MB page */
2615 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2616 else
2617 /* 32 bits PSE 4MB page */
2618 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002619 break;
2620 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002621 context->rsvd_bits_mask[0][2] =
2622 rsvd_bits(maxphyaddr, 63) |
2623 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002624 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002625 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002626 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2627 rsvd_bits(maxphyaddr, 62); /* PTE */
2628 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2629 rsvd_bits(maxphyaddr, 62) |
2630 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002631 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002632 break;
2633 case PT64_ROOT_LEVEL:
2634 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2635 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2636 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2637 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2638 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002639 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002640 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2641 rsvd_bits(maxphyaddr, 51);
2642 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002643 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2644 rsvd_bits(maxphyaddr, 51) |
2645 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002646 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002647 rsvd_bits(maxphyaddr, 51) |
2648 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002649 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002650 break;
2651 }
2652}
2653
Avi Kivity17ac10a2007-01-05 16:36:40 -08002654static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002656 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657
2658 ASSERT(is_pae(vcpu));
2659 context->new_cr3 = paging_new_cr3;
2660 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002662 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002663 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002664 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002666 context->root_level = level;
2667 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002668 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002669 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 return 0;
2671}
2672
Avi Kivity17ac10a2007-01-05 16:36:40 -08002673static int paging64_init_context(struct kvm_vcpu *vcpu)
2674{
Dong, Eddie82725b22009-03-30 16:21:08 +08002675 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002676 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2677}
2678
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679static int paging32_init_context(struct kvm_vcpu *vcpu)
2680{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002681 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682
Dong, Eddie82725b22009-03-30 16:21:08 +08002683 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 context->new_cr3 = paging_new_cr3;
2685 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 context->gva_to_gpa = paging32_gva_to_gpa;
2687 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002688 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002689 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002690 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 context->root_level = PT32_ROOT_LEVEL;
2692 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002693 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002694 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 return 0;
2696}
2697
2698static int paging32E_init_context(struct kvm_vcpu *vcpu)
2699{
Dong, Eddie82725b22009-03-30 16:21:08 +08002700 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002701 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702}
2703
Joerg Roedelfb72d162008-02-07 13:47:44 +01002704static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2705{
2706 struct kvm_mmu *context = &vcpu->arch.mmu;
2707
2708 context->new_cr3 = nonpaging_new_cr3;
2709 context->page_fault = tdp_page_fault;
2710 context->free = nonpaging_free;
2711 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002712 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002713 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002714 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002715 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002716 context->direct_map = true;
Joerg Roedelf43addd2010-09-10 17:30:40 +02002717 context->set_cr3 = kvm_x86_ops->set_cr3;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002718
2719 if (!is_paging(vcpu)) {
2720 context->gva_to_gpa = nonpaging_gva_to_gpa;
2721 context->root_level = 0;
2722 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002723 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002724 context->gva_to_gpa = paging64_gva_to_gpa;
2725 context->root_level = PT64_ROOT_LEVEL;
2726 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002727 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002728 context->gva_to_gpa = paging64_gva_to_gpa;
2729 context->root_level = PT32E_ROOT_LEVEL;
2730 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002731 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002732 context->gva_to_gpa = paging32_gva_to_gpa;
2733 context->root_level = PT32_ROOT_LEVEL;
2734 }
2735
2736 return 0;
2737}
2738
2739static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002741 int r;
2742
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002744 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745
2746 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002747 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002748 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002749 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002751 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002753 r = paging32_init_context(vcpu);
2754
Avi Kivity5b7e0102010-04-14 19:20:03 +03002755 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02002756 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
2757 vcpu->arch.mmu.set_cr3 = kvm_x86_ops->set_cr3;
Avi Kivitya770f6f2008-12-21 19:20:09 +02002758
2759 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760}
2761
Joerg Roedelfb72d162008-02-07 13:47:44 +01002762static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2763{
Anthony Liguori35149e22008-04-02 14:46:56 -05002764 vcpu->arch.update_pte.pfn = bad_pfn;
2765
Joerg Roedelfb72d162008-02-07 13:47:44 +01002766 if (tdp_enabled)
2767 return init_kvm_tdp_mmu(vcpu);
2768 else
2769 return init_kvm_softmmu(vcpu);
2770}
2771
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2773{
2774 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002775 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2776 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002777 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778}
2779
2780int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2781{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002782 destroy_kvm_mmu(vcpu);
2783 return init_kvm_mmu(vcpu);
2784}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002785EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002786
2787int kvm_mmu_load(struct kvm_vcpu *vcpu)
2788{
Avi Kivity714b93d2007-01-05 16:36:53 -08002789 int r;
2790
Avi Kivitye2dec932007-01-05 16:36:54 -08002791 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002792 if (r)
2793 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002794 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002795 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002796 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002797 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002798 if (r)
2799 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002800 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02002801 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002802out:
2803 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002805EXPORT_SYMBOL_GPL(kvm_mmu_load);
2806
2807void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2808{
2809 mmu_free_roots(vcpu);
2810}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811
Avi Kivity09072da2007-05-01 14:16:52 +03002812static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002813 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002814 u64 *spte)
2815{
2816 u64 pte;
2817 struct kvm_mmu_page *child;
2818
2819 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002820 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002821 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002822 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002823 else {
2824 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002825 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002826 }
2827 }
Avi Kivityd555c332009-06-10 14:24:23 +03002828 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002829 if (is_large_pte(pte))
2830 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002831}
2832
Avi Kivity00284252007-05-01 16:53:31 +03002833static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002834 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002835 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002836 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002837{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002838 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002839 ++vcpu->kvm->stat.mmu_pde_zapped;
2840 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002841 }
Avi Kivity00284252007-05-01 16:53:31 +03002842
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002843 if (is_rsvd_bits_set(vcpu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
2844 return;
2845
Avi Kivity4cee5762007-11-18 16:37:07 +02002846 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002847 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002848 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002849 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002850 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002851}
2852
Avi Kivity79539ce2007-11-21 02:06:21 +02002853static bool need_remote_flush(u64 old, u64 new)
2854{
2855 if (!is_shadow_present_pte(old))
2856 return false;
2857 if (!is_shadow_present_pte(new))
2858 return true;
2859 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2860 return true;
2861 old ^= PT64_NX_MASK;
2862 new ^= PT64_NX_MASK;
2863 return (old & ~new & PT64_PERM_MASK) != 0;
2864}
2865
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002866static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2867 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002868{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002869 if (zap_page)
2870 return;
2871
2872 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002873 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002874 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002875 kvm_mmu_flush_tlb(vcpu);
2876}
2877
Avi Kivity12b7d282007-09-23 14:10:49 +02002878static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2879{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002880 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002881
Sheng Yang7b523452008-04-25 21:13:50 +08002882 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002883}
2884
Avi Kivityd7824ff2007-12-30 12:29:05 +02002885static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002886 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002887{
2888 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002889 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002890
Avi Kivity43a37952009-06-10 14:12:05 +03002891 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002892 return;
2893 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002894
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002895 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002896 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002897 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002898
Anthony Liguori35149e22008-04-02 14:46:56 -05002899 if (is_error_pfn(pfn)) {
2900 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002901 return;
2902 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002903 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002904 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002905}
2906
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002907static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2908{
2909 u64 *spte = vcpu->arch.last_pte_updated;
2910
2911 if (spte
2912 && vcpu->arch.last_pte_gfn == gfn
2913 && shadow_accessed_mask
2914 && !(*spte & shadow_accessed_mask)
2915 && is_shadow_present_pte(*spte))
2916 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2917}
2918
Avi Kivity09072da2007-05-01 14:16:52 +03002919void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002920 const u8 *new, int bytes,
2921 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002922{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002923 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002924 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02002925 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002926 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002927 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002928 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002929 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002930 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002931 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002932 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002933 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002934 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002935 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002936 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002937 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002938 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002939 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002940 bool remote_flush, local_flush, zap_page;
2941
2942 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002943
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002944 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002945
Avi Kivity08e850c2010-03-15 13:59:57 +02002946 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2947
2948 /*
2949 * Assume that the pte write on a page table of the same type
2950 * as the current vcpu paging mode. This is nearly always true
2951 * (might be false while changing modes). Note it is verified later
2952 * by update_pte().
2953 */
2954 if ((is_pae(vcpu) && bytes == 4) || !new) {
2955 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2956 if (is_pae(vcpu)) {
2957 gpa &= ~(gpa_t)7;
2958 bytes = 8;
2959 }
2960 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2961 if (r)
2962 gentry = 0;
2963 new = (const u8 *)&gentry;
2964 }
2965
Avi Kivity72016f32010-03-15 13:59:53 +02002966 switch (bytes) {
2967 case 4:
2968 gentry = *(const u32 *)new;
2969 break;
2970 case 8:
2971 gentry = *(const u64 *)new;
2972 break;
2973 default:
2974 gentry = 0;
2975 break;
2976 }
2977
Avi Kivity72016f32010-03-15 13:59:53 +02002978 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002979 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002980 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2981 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002982 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002983 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002984 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08002985 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002986 if (guest_initiated) {
2987 if (gfn == vcpu->arch.last_pt_write_gfn
2988 && !last_updated_pte_accessed(vcpu)) {
2989 ++vcpu->arch.last_pt_write_count;
2990 if (vcpu->arch.last_pt_write_count >= 3)
2991 flooded = 1;
2992 } else {
2993 vcpu->arch.last_pt_write_gfn = gfn;
2994 vcpu->arch.last_pt_write_count = 1;
2995 vcpu->arch.last_pte_updated = NULL;
2996 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002997 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002998
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08002999 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003000 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003001 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003002 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003003 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003004 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003005 /*
3006 * Misaligned accesses are too much trouble to fix
3007 * up; also, they usually indicate a page is not used
3008 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003009 *
3010 * If we're seeing too many writes to a page,
3011 * it may no longer be a page table, or we may be
3012 * forking, in which case it is better to unmap the
3013 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003014 */
3015 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003016 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003017 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003018 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003019 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003020 continue;
3021 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003022 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003023 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003024 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003025 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003026 page_offset <<= 1; /* 32->64 */
3027 /*
3028 * A 32-bit pde maps 4MB while the shadow pdes map
3029 * only 2MB. So we need to double the offset again
3030 * and zap two pdes instead of one.
3031 */
3032 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003033 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003034 page_offset <<= 1;
3035 npte = 2;
3036 }
Avi Kivityfce06572007-05-01 16:44:05 +03003037 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003038 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003039 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003040 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003041 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003042 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003043 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003044 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003045 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003046 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003047 if (gentry &&
3048 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3049 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003050 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003051 if (!remote_flush && need_remote_flush(entry, *spte))
3052 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003053 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003054 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003055 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003056 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003057 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003058 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003059 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003060 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3061 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3062 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003063 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003064}
3065
Avi Kivitya4360362007-01-05 16:36:45 -08003066int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3067{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003068 gpa_t gpa;
3069 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003070
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003071 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003072 return 0;
3073
Gleb Natapov1871c602010-02-10 14:21:32 +02003074 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003075
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003076 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003077 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003078 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003079 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003080}
Avi Kivity577bdc42008-07-19 08:57:05 +03003081EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003082
Avi Kivity22d95b12007-09-14 20:26:06 +03003083void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003084{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003085 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003086
Dave Hansene0df7b92010-08-19 18:11:05 -07003087 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003088 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003089 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003090
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003091 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003092 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003093 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003094 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003095 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003096 }
3097}
Avi Kivityebeace82007-01-05 16:36:47 -08003098
Avi Kivity30677142007-10-28 18:48:59 +02003099int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3100{
3101 int r;
3102 enum emulation_result er;
3103
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003104 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02003105 if (r < 0)
3106 goto out;
3107
3108 if (!r) {
3109 r = 1;
3110 goto out;
3111 }
3112
Avi Kivityb733bfb2007-10-28 18:52:05 +02003113 r = mmu_topup_memory_caches(vcpu);
3114 if (r)
3115 goto out;
3116
Avi Kivity851ba692009-08-24 11:10:17 +03003117 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02003118
3119 switch (er) {
3120 case EMULATE_DONE:
3121 return 1;
3122 case EMULATE_DO_MMIO:
3123 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003124 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003125 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003126 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003127 default:
3128 BUG();
3129 }
3130out:
Avi Kivity30677142007-10-28 18:48:59 +02003131 return r;
3132}
3133EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3134
Marcelo Tosattia7052892008-09-23 13:18:35 -03003135void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3136{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003137 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003138 kvm_mmu_flush_tlb(vcpu);
3139 ++vcpu->stat.invlpg;
3140}
3141EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3142
Joerg Roedel18552672008-02-07 13:47:41 +01003143void kvm_enable_tdp(void)
3144{
3145 tdp_enabled = true;
3146}
3147EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3148
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003149void kvm_disable_tdp(void)
3150{
3151 tdp_enabled = false;
3152}
3153EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3154
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155static void free_mmu_pages(struct kvm_vcpu *vcpu)
3156{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003157 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158}
3159
3160static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3161{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003162 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 int i;
3164
3165 ASSERT(vcpu);
3166
Avi Kivity17ac10a2007-01-05 16:36:40 -08003167 /*
3168 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3169 * Therefore we need to allocate shadow page tables in the first
3170 * 4GB of memory, which happens to fit the DMA32 zone.
3171 */
3172 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3173 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003174 return -ENOMEM;
3175
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003176 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003177 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003178 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003179
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181}
3182
Ingo Molnar8018c272006-12-29 16:50:01 -08003183int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003186 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187
Ingo Molnar8018c272006-12-29 16:50:01 -08003188 return alloc_mmu_pages(vcpu);
3189}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190
Ingo Molnar8018c272006-12-29 16:50:01 -08003191int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3192{
3193 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003194 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003195
Ingo Molnar8018c272006-12-29 16:50:01 -08003196 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197}
3198
3199void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3200{
3201 ASSERT(vcpu);
3202
3203 destroy_kvm_mmu(vcpu);
3204 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08003205 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206}
3207
Avi Kivity90cb0522007-07-17 13:04:56 +03003208void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209{
Avi Kivity4db35312007-11-21 15:28:32 +02003210 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003212 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 int i;
3214 u64 *pt;
3215
Sheng Yang291f26b2008-10-16 17:30:57 +08003216 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 continue;
3218
Avi Kivity4db35312007-11-21 15:28:32 +02003219 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3221 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003222 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 }
Avi Kivity171d5952008-08-27 16:40:51 +03003225 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003227
Avi Kivity90cb0522007-07-17 13:04:56 +03003228void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003229{
Avi Kivity4db35312007-11-21 15:28:32 +02003230 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003231 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003232
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003233 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003234restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003235 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003236 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003237 goto restart;
3238
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003239 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003240 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003241}
3242
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003243static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3244 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003245{
3246 struct kvm_mmu_page *page;
3247
3248 page = container_of(kvm->arch.active_mmu_pages.prev,
3249 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003250 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003251}
3252
Dave Chinner7f8275d2010-07-19 14:56:17 +10003253static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003254{
3255 struct kvm *kvm;
3256 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003257
3258 if (nr_to_scan == 0)
3259 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003260
3261 spin_lock(&kvm_lock);
3262
3263 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003264 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003265 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003266
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003267 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003268 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003269 if (!kvm_freed && nr_to_scan > 0 &&
3270 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003271 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3272 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003273 kvm_freed = kvm;
3274 }
3275 nr_to_scan--;
3276
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003277 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003278 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003279 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003280 }
3281 if (kvm_freed)
3282 list_move_tail(&kvm_freed->vm_list, &vm_list);
3283
3284 spin_unlock(&kvm_lock);
3285
Dave Hansen45221ab2010-08-19 18:11:37 -07003286out:
3287 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003288}
3289
3290static struct shrinker mmu_shrinker = {
3291 .shrink = mmu_shrink,
3292 .seeks = DEFAULT_SEEKS * 10,
3293};
3294
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003295static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003296{
3297 if (pte_chain_cache)
3298 kmem_cache_destroy(pte_chain_cache);
3299 if (rmap_desc_cache)
3300 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003301 if (mmu_page_header_cache)
3302 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003303}
3304
Izik Eidus3ee16c82008-03-30 15:17:21 +03003305void kvm_mmu_module_exit(void)
3306{
3307 mmu_destroy_caches();
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003308 percpu_counter_destroy(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003309 unregister_shrinker(&mmu_shrinker);
3310}
3311
Avi Kivityb5a33a72007-04-15 16:31:09 +03003312int kvm_mmu_module_init(void)
3313{
3314 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3315 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003316 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003317 if (!pte_chain_cache)
3318 goto nomem;
3319 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3320 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003321 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003322 if (!rmap_desc_cache)
3323 goto nomem;
3324
Avi Kivityd3d25b02007-05-30 12:34:53 +03003325 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3326 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003327 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003328 if (!mmu_page_header_cache)
3329 goto nomem;
3330
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003331 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3332 goto nomem;
3333
Izik Eidus3ee16c82008-03-30 15:17:21 +03003334 register_shrinker(&mmu_shrinker);
3335
Avi Kivityb5a33a72007-04-15 16:31:09 +03003336 return 0;
3337
3338nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003339 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003340 return -ENOMEM;
3341}
3342
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003343/*
3344 * Caculate mmu pages needed for kvm.
3345 */
3346unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3347{
3348 int i;
3349 unsigned int nr_mmu_pages;
3350 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003351 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003352
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003353 slots = kvm_memslots(kvm);
3354
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003355 for (i = 0; i < slots->nmemslots; i++)
3356 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003357
3358 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3359 nr_mmu_pages = max(nr_mmu_pages,
3360 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3361
3362 return nr_mmu_pages;
3363}
3364
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003365static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3366 unsigned len)
3367{
3368 if (len > buffer->len)
3369 return NULL;
3370 return buffer->ptr;
3371}
3372
3373static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3374 unsigned len)
3375{
3376 void *ret;
3377
3378 ret = pv_mmu_peek_buffer(buffer, len);
3379 if (!ret)
3380 return ret;
3381 buffer->ptr += len;
3382 buffer->len -= len;
3383 buffer->processed += len;
3384 return ret;
3385}
3386
3387static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3388 gpa_t addr, gpa_t value)
3389{
3390 int bytes = 8;
3391 int r;
3392
3393 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3394 bytes = 4;
3395
3396 r = mmu_topup_memory_caches(vcpu);
3397 if (r)
3398 return r;
3399
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003400 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003401 return -EFAULT;
3402
3403 return 1;
3404}
3405
3406static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3407{
Avi Kivity23902182010-06-10 17:02:16 +03003408 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003409 return 1;
3410}
3411
3412static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3413{
3414 spin_lock(&vcpu->kvm->mmu_lock);
3415 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3416 spin_unlock(&vcpu->kvm->mmu_lock);
3417 return 1;
3418}
3419
3420static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3421 struct kvm_pv_mmu_op_buffer *buffer)
3422{
3423 struct kvm_mmu_op_header *header;
3424
3425 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3426 if (!header)
3427 return 0;
3428 switch (header->op) {
3429 case KVM_MMU_OP_WRITE_PTE: {
3430 struct kvm_mmu_op_write_pte *wpte;
3431
3432 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3433 if (!wpte)
3434 return 0;
3435 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3436 wpte->pte_val);
3437 }
3438 case KVM_MMU_OP_FLUSH_TLB: {
3439 struct kvm_mmu_op_flush_tlb *ftlb;
3440
3441 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3442 if (!ftlb)
3443 return 0;
3444 return kvm_pv_mmu_flush_tlb(vcpu);
3445 }
3446 case KVM_MMU_OP_RELEASE_PT: {
3447 struct kvm_mmu_op_release_pt *rpt;
3448
3449 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3450 if (!rpt)
3451 return 0;
3452 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3453 }
3454 default: return 0;
3455 }
3456}
3457
3458int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3459 gpa_t addr, unsigned long *ret)
3460{
3461 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003462 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003463
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003464 buffer->ptr = buffer->buf;
3465 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3466 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003467
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003468 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003469 if (r)
3470 goto out;
3471
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003472 while (buffer->len) {
3473 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003474 if (r < 0)
3475 goto out;
3476 if (r == 0)
3477 break;
3478 }
3479
3480 r = 1;
3481out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003482 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003483 return r;
3484}
3485
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003486int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3487{
3488 struct kvm_shadow_walk_iterator iterator;
3489 int nr_sptes = 0;
3490
3491 spin_lock(&vcpu->kvm->mmu_lock);
3492 for_each_shadow_entry(vcpu, addr, iterator) {
3493 sptes[iterator.level-1] = *iterator.sptep;
3494 nr_sptes++;
3495 if (!is_shadow_present_pte(*iterator.sptep))
3496 break;
3497 }
3498 spin_unlock(&vcpu->kvm->mmu_lock);
3499
3500 return nr_sptes;
3501}
3502EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3503
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003504#ifdef CONFIG_KVM_MMU_AUDIT
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08003505#include "mmu_audit.c"
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003506#endif