blob: f8bf42a259953975b60770b32d35c58a2ab468ed [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030021#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020032#include <linux/srcu.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033
34#include <asm/page.h>
35#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020036#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020037#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040038
Joerg Roedel18552672008-02-07 13:47:41 +010039/*
40 * When setting this variable to true it enables Two-Dimensional-Paging
41 * where the hardware walks 2 page tables:
42 * 1. the guest-virtual to guest-physical
43 * 2. while doing 1. it walks guest-physical to host-physical
44 * If the hardware supports that we don't need to do shadow paging.
45 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050046bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010047
Avi Kivity37a7d8b2007-01-05 16:36:56 -080048#undef MMU_DEBUG
49
50#undef AUDIT
51
52#ifdef AUDIT
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
54#else
55static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
56#endif
57
58#ifdef MMU_DEBUG
59
60#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
61#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
62
63#else
64
65#define pgprintk(x...) do { } while (0)
66#define rmap_printk(x...) do { } while (0)
67
68#endif
69
70#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030071static int dbg = 0;
72module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080073#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080074
Marcelo Tosatti582801a2008-09-23 13:18:41 -030075static int oos_shadow = 1;
76module_param(oos_shadow, bool, 0644);
77
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080078#ifndef MMU_DEBUG
79#define ASSERT(x) do { } while (0)
80#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080081#define ASSERT(x) \
82 if (!(x)) { \
83 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
84 __FILE__, __LINE__, #x); \
85 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080086#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080087
Avi Kivity6aa8b732006-12-10 02:21:36 -080088#define PT_FIRST_AVAIL_BITS_SHIFT 9
89#define PT64_SECOND_AVAIL_BITS_SHIFT 52
90
Avi Kivity6aa8b732006-12-10 02:21:36 -080091#define VALID_PAGE(x) ((x) != INVALID_PAGE)
92
93#define PT64_LEVEL_BITS 9
94
95#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040096 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
98#define PT64_LEVEL_MASK(level) \
99 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
100
101#define PT64_INDEX(address, level)\
102 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
103
104
105#define PT32_LEVEL_BITS 10
106
107#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400108 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109
110#define PT32_LEVEL_MASK(level) \
111 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200112#define PT32_LVL_OFFSET_MASK(level) \
113 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
114 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115
116#define PT32_INDEX(address, level)\
117 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
118
119
Avi Kivity27aba762007-03-09 13:04:31 +0200120#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121#define PT64_DIR_BASE_ADDR_MASK \
122 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200123#define PT64_LVL_ADDR_MASK(level) \
124 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
125 * PT64_LEVEL_BITS))) - 1))
126#define PT64_LVL_OFFSET_MASK(level) \
127 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
128 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129
130#define PT32_BASE_ADDR_MASK PAGE_MASK
131#define PT32_DIR_BASE_ADDR_MASK \
132 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200133#define PT32_LVL_ADDR_MASK(level) \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
135 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136
Avi Kivity79539ce2007-11-21 02:06:21 +0200137#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
138 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139
140#define PFERR_PRESENT_MASK (1U << 0)
141#define PFERR_WRITE_MASK (1U << 1)
142#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800143#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800144#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
Joerg Roedele04da982009-07-27 16:30:45 +0200146#define PT_PDPE_LEVEL 3
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147#define PT_DIRECTORY_LEVEL 2
148#define PT_PAGE_TABLE_LEVEL 1
149
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 Kivity07420172009-07-06 12:21:32 +0300157#define CREATE_TRACE_POINTS
158#include "mmutrace.h"
159
Izik Eidus14032832009-09-23 21:47:17 +0300160#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
161
Avi Kivity135f8c22008-08-21 17:49:56 +0300162#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
163
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800164struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300165 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800166 struct kvm_rmap_desc *more;
167};
168
Avi Kivity2d111232008-12-25 14:39:47 +0200169struct kvm_shadow_walk_iterator {
170 u64 addr;
171 hpa_t shadow_addr;
172 int level;
173 u64 *sptep;
174 unsigned index;
175};
176
177#define for_each_shadow_entry(_vcpu, _addr, _walker) \
178 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
179 shadow_walk_okay(&(_walker)); \
180 shadow_walk_next(&(_walker)))
181
182
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300183struct kvm_unsync_walk {
184 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
185};
186
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300187typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
188
Avi Kivityb5a33a72007-04-15 16:31:09 +0300189static struct kmem_cache *pte_chain_cache;
190static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300191static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300192
Avi Kivityc7addb92007-09-16 18:58:32 +0200193static u64 __read_mostly shadow_trap_nonpresent_pte;
194static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800195static u64 __read_mostly shadow_base_present_pte;
196static u64 __read_mostly shadow_nx_mask;
197static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
198static u64 __read_mostly shadow_user_mask;
199static u64 __read_mostly shadow_accessed_mask;
200static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200201
Dong, Eddie82725b22009-03-30 16:21:08 +0800202static inline u64 rsvd_bits(int s, int e)
203{
204 return ((1ULL << (e - s + 1)) - 1) << s;
205}
206
Avi Kivityc7addb92007-09-16 18:58:32 +0200207void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
208{
209 shadow_trap_nonpresent_pte = trap_pte;
210 shadow_notrap_nonpresent_pte = notrap_pte;
211}
212EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
213
Sheng Yang7b523452008-04-25 21:13:50 +0800214void kvm_mmu_set_base_ptes(u64 base_pte)
215{
216 shadow_base_present_pte = base_pte;
217}
218EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
219
220void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800221 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800222{
223 shadow_user_mask = user_mask;
224 shadow_accessed_mask = accessed_mask;
225 shadow_dirty_mask = dirty_mask;
226 shadow_nx_mask = nx_mask;
227 shadow_x_mask = x_mask;
228}
229EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
230
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231static int is_write_protection(struct kvm_vcpu *vcpu)
232{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800233 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234}
235
236static int is_cpuid_PSE36(void)
237{
238 return 1;
239}
240
Avi Kivity73b10872007-01-26 00:56:41 -0800241static int is_nx(struct kvm_vcpu *vcpu)
242{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800243 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800244}
245
Avi Kivityc7addb92007-09-16 18:58:32 +0200246static int is_shadow_present_pte(u64 pte)
247{
Avi Kivityc7addb92007-09-16 18:58:32 +0200248 return pte != shadow_trap_nonpresent_pte
249 && pte != shadow_notrap_nonpresent_pte;
250}
251
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300252static int is_large_pte(u64 pte)
253{
254 return pte & PT_PAGE_SIZE_MASK;
255}
256
Avi Kivity6aa8b732006-12-10 02:21:36 -0800257static int is_writeble_pte(unsigned long pte)
258{
259 return pte & PT_WRITABLE_MASK;
260}
261
Avi Kivity43a37952009-06-10 14:12:05 +0300262static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200263{
Avi Kivity439e2182009-06-10 12:56:54 +0300264 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200265}
266
Avi Kivity43a37952009-06-10 14:12:05 +0300267static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800268{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200269 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800270}
271
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300272static int is_last_spte(u64 pte, int level)
273{
274 if (level == PT_PAGE_TABLE_LEVEL)
275 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200276 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300277 return 1;
278 return 0;
279}
280
Anthony Liguori35149e22008-04-02 14:46:56 -0500281static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200282{
Anthony Liguori35149e22008-04-02 14:46:56 -0500283 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200284}
285
Avi Kivityda928522007-11-21 13:54:47 +0200286static gfn_t pse36_gfn_delta(u32 gpte)
287{
288 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
289
290 return (gpte & PT32_DIR_PSE36_MASK) << shift;
291}
292
Avi Kivityd555c332009-06-10 14:24:23 +0300293static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300294{
295#ifdef CONFIG_X86_64
296 set_64bit((unsigned long *)sptep, spte);
297#else
298 set_64bit((unsigned long long *)sptep, spte);
299#endif
300}
301
Avi Kivitye2dec932007-01-05 16:36:54 -0800302static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300303 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800304{
305 void *obj;
306
307 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800308 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800309 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300310 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800311 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800312 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800313 cache->objects[cache->nobjs++] = obj;
314 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800315 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800316}
317
318static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
319{
320 while (mc->nobjs)
321 kfree(mc->objects[--mc->nobjs]);
322}
323
Avi Kivityc1158e62007-07-20 08:18:27 +0300324static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300325 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300326{
327 struct page *page;
328
329 if (cache->nobjs >= min)
330 return 0;
331 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300332 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300333 if (!page)
334 return -ENOMEM;
335 set_page_private(page, 0);
336 cache->objects[cache->nobjs++] = page_address(page);
337 }
338 return 0;
339}
340
341static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
342{
343 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300344 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300345}
346
Avi Kivity8c438502007-04-16 11:53:17 +0300347static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
348{
349 int r;
350
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800351 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300352 pte_chain_cache, 4);
353 if (r)
354 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800355 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200356 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300357 if (r)
358 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300360 if (r)
361 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800362 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300363 mmu_page_header_cache, 4);
364out:
Avi Kivity8c438502007-04-16 11:53:17 +0300365 return r;
366}
367
Avi Kivity714b93d2007-01-05 16:36:53 -0800368static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800370 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
371 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
372 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
373 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800374}
375
376static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
377 size_t size)
378{
379 void *p;
380
381 BUG_ON(!mc->nobjs);
382 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800383 return p;
384}
385
Avi Kivity714b93d2007-01-05 16:36:53 -0800386static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
387{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800388 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800389 sizeof(struct kvm_pte_chain));
390}
391
Avi Kivity90cb0522007-07-17 13:04:56 +0300392static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800393{
Avi Kivity90cb0522007-07-17 13:04:56 +0300394 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800395}
396
397static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
398{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800399 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800400 sizeof(struct kvm_rmap_desc));
401}
402
Avi Kivity90cb0522007-07-17 13:04:56 +0300403static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800404{
Avi Kivity90cb0522007-07-17 13:04:56 +0300405 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800406}
407
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800408/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409 * Return the pointer to the largepage write count for a given
410 * gfn, handling slots that are not large page aligned.
411 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200412static int *slot_largepage_idx(gfn_t gfn,
413 struct kvm_memory_slot *slot,
414 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300415{
416 unsigned long idx;
417
Joerg Roedeld25797b2009-07-27 16:30:43 +0200418 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
419 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
420 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421}
422
423static void account_shadowed(struct kvm *kvm, gfn_t gfn)
424{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200425 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300426 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200427 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300428
Izik Eidus28430992008-10-03 17:40:32 +0300429 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200430
431 slot = gfn_to_memslot_unaliased(kvm, gfn);
432 for (i = PT_DIRECTORY_LEVEL;
433 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
434 write_count = slot_largepage_idx(gfn, slot, i);
435 *write_count += 1;
436 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300437}
438
439static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
440{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200441 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300442 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200443 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300444
Izik Eidus28430992008-10-03 17:40:32 +0300445 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200446 for (i = PT_DIRECTORY_LEVEL;
447 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
448 slot = gfn_to_memslot_unaliased(kvm, gfn);
449 write_count = slot_largepage_idx(gfn, slot, i);
450 *write_count -= 1;
451 WARN_ON(*write_count < 0);
452 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453}
454
Joerg Roedeld25797b2009-07-27 16:30:43 +0200455static int has_wrprotected_page(struct kvm *kvm,
456 gfn_t gfn,
457 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300458{
Izik Eidus28430992008-10-03 17:40:32 +0300459 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300460 int *largepage_idx;
461
Izik Eidus28430992008-10-03 17:40:32 +0300462 gfn = unalias_gfn(kvm, gfn);
463 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300464 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200465 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466 return *largepage_idx;
467 }
468
469 return 1;
470}
471
Joerg Roedeld25797b2009-07-27 16:30:43 +0200472static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300473{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200474 unsigned long page_size = PAGE_SIZE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475 struct vm_area_struct *vma;
476 unsigned long addr;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200477 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300478
479 addr = gfn_to_hva(kvm, gfn);
480 if (kvm_is_error_hva(addr))
Sheng Yang82b70052010-01-05 19:02:28 +0800481 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300483 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300484 vma = find_vma(current->mm, addr);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200485 if (!vma)
486 goto out;
487
488 page_size = vma_kernel_pagesize(vma);
489
490out:
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300491 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492
Joerg Roedeld25797b2009-07-27 16:30:43 +0200493 for (i = PT_PAGE_TABLE_LEVEL;
494 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
495 if (page_size >= KVM_HPAGE_SIZE(i))
496 ret = i;
497 else
498 break;
499 }
500
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300501 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502}
503
Joerg Roedeld25797b2009-07-27 16:30:43 +0200504static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300505{
506 struct kvm_memory_slot *slot;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200507 int host_level;
508 int level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300509
510 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
511 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200512 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300513
Joerg Roedeld25797b2009-07-27 16:30:43 +0200514 host_level = host_mapping_level(vcpu->kvm, large_gfn);
515
516 if (host_level == PT_PAGE_TABLE_LEVEL)
517 return host_level;
518
Sheng Yang82b70052010-01-05 19:02:28 +0800519 for (level = PT_DIRECTORY_LEVEL; level <= host_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200520 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
521 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200522
523 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300524}
525
526/*
Izik Eidus290fc382007-09-27 14:11:22 +0200527 * Take gfn and return the reverse mapping to it.
528 * Note: gfn must be unaliased before this function get called
529 */
530
Joerg Roedel44ad9942009-07-27 16:30:42 +0200531static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200532{
533 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200535
536 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200537 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300538 return &slot->rmap[gfn - slot->base_gfn];
539
Joerg Roedel44ad9942009-07-27 16:30:42 +0200540 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
541 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300542
Joerg Roedel44ad9942009-07-27 16:30:42 +0200543 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200544}
545
546/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 * Reverse mapping data structures:
548 *
Izik Eidus290fc382007-09-27 14:11:22 +0200549 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
550 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800551 *
Izik Eidus290fc382007-09-27 14:11:22 +0200552 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
553 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300554 *
555 * Returns the number of rmap entries before the spte was added or zero if
556 * the spte was not added.
557 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200559static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800560{
Avi Kivity4db35312007-11-21 15:28:32 +0200561 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200563 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300564 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800565
Avi Kivity43a37952009-06-10 14:12:05 +0300566 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300567 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200568 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200569 sp = page_header(__pa(spte));
570 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200571 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200572 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800573 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200574 *rmapp = (unsigned long)spte;
575 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800576 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800577 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300578 desc->sptes[0] = (u64 *)*rmapp;
579 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200580 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 } else {
582 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200583 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300584 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800585 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300586 count += RMAP_EXT;
587 }
Avi Kivityd555c332009-06-10 14:24:23 +0300588 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800589 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800590 desc = desc->more;
591 }
Avi Kivityd555c332009-06-10 14:24:23 +0300592 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800593 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300594 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800595 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300596 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800597}
598
Izik Eidus290fc382007-09-27 14:11:22 +0200599static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 struct kvm_rmap_desc *desc,
601 int i,
602 struct kvm_rmap_desc *prev_desc)
603{
604 int j;
605
Avi Kivityd555c332009-06-10 14:24:23 +0300606 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800607 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300608 desc->sptes[i] = desc->sptes[j];
609 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800610 if (j != 0)
611 return;
612 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300613 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800614 else
615 if (prev_desc)
616 prev_desc->more = desc->more;
617 else
Izik Eidus290fc382007-09-27 14:11:22 +0200618 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300619 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620}
621
Izik Eidus290fc382007-09-27 14:11:22 +0200622static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800624 struct kvm_rmap_desc *desc;
625 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200626 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500627 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200628 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 int i;
630
Avi Kivity43a37952009-06-10 14:12:05 +0300631 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800632 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200633 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500634 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800635 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500636 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200637 if (is_writeble_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300638 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200639 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200640 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800641 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
642 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200643 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800644 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200645 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800646 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
647 spte, *spte);
648 BUG();
649 }
Izik Eidus290fc382007-09-27 14:11:22 +0200650 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800651 } else {
652 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200653 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800654 prev_desc = NULL;
655 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300656 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
657 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200658 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800659 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800660 prev_desc);
661 return;
662 }
663 prev_desc = desc;
664 desc = desc->more;
665 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200666 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800667 BUG();
668 }
669}
670
Izik Eidus98348e92007-10-16 14:42:30 +0200671static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800672{
Avi Kivity374cbac2007-01-05 16:36:43 -0800673 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200674 struct kvm_rmap_desc *prev_desc;
675 u64 *prev_spte;
676 int i;
677
678 if (!*rmapp)
679 return NULL;
680 else if (!(*rmapp & 1)) {
681 if (!spte)
682 return (u64 *)*rmapp;
683 return NULL;
684 }
685 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
686 prev_desc = NULL;
687 prev_spte = NULL;
688 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300689 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200690 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300691 return desc->sptes[i];
692 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200693 }
694 desc = desc->more;
695 }
696 return NULL;
697}
698
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200699static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200700{
Izik Eidus290fc382007-09-27 14:11:22 +0200701 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800702 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200703 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800704
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500705 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200706 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800707
Izik Eidus98348e92007-10-16 14:42:30 +0200708 spte = rmap_next(kvm, rmapp, NULL);
709 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800710 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800711 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800712 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800713 if (is_writeble_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300714 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800715 write_protected = 1;
716 }
Izik Eidus9647c142007-10-16 14:43:46 +0200717 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800718 }
Izik Eidus855149a2008-03-20 18:17:24 +0200719 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500720 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200721
722 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500723 pfn = spte_to_pfn(*spte);
724 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200725 }
726
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300727 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200728 for (i = PT_DIRECTORY_LEVEL;
729 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
730 rmapp = gfn_to_rmap(kvm, gfn, i);
731 spte = rmap_next(kvm, rmapp, NULL);
732 while (spte) {
733 BUG_ON(!spte);
734 BUG_ON(!(*spte & PT_PRESENT_MASK));
735 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
736 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
737 if (is_writeble_pte(*spte)) {
738 rmap_remove(kvm, spte);
739 --kvm->stat.lpages;
740 __set_spte(spte, shadow_trap_nonpresent_pte);
741 spte = NULL;
742 write_protected = 1;
743 }
744 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300745 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300746 }
747
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200748 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800749}
750
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000751static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
752 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200753{
754 u64 *spte;
755 int need_tlb_flush = 0;
756
757 while ((spte = rmap_next(kvm, rmapp, NULL))) {
758 BUG_ON(!(*spte & PT_PRESENT_MASK));
759 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
760 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300761 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200762 need_tlb_flush = 1;
763 }
764 return need_tlb_flush;
765}
766
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000767static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
768 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300769{
770 int need_flush = 0;
771 u64 *spte, new_spte;
772 pte_t *ptep = (pte_t *)data;
773 pfn_t new_pfn;
774
775 WARN_ON(pte_huge(*ptep));
776 new_pfn = pte_pfn(*ptep);
777 spte = rmap_next(kvm, rmapp, NULL);
778 while (spte) {
779 BUG_ON(!is_shadow_present_pte(*spte));
780 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
781 need_flush = 1;
782 if (pte_write(*ptep)) {
783 rmap_remove(kvm, spte);
784 __set_spte(spte, shadow_trap_nonpresent_pte);
785 spte = rmap_next(kvm, rmapp, NULL);
786 } else {
787 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
788 new_spte |= (u64)new_pfn << PAGE_SHIFT;
789
790 new_spte &= ~PT_WRITABLE_MASK;
791 new_spte &= ~SPTE_HOST_WRITEABLE;
792 if (is_writeble_pte(*spte))
793 kvm_set_pfn_dirty(spte_to_pfn(*spte));
794 __set_spte(spte, new_spte);
795 spte = rmap_next(kvm, rmapp, spte);
796 }
797 }
798 if (need_flush)
799 kvm_flush_remote_tlbs(kvm);
800
801 return 0;
802}
803
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000804static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
805 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300806 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000807 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200808{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200809 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200810 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200811 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200812
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200813 slots = rcu_dereference(kvm->memslots);
814
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200815 for (i = 0; i < slots->nmemslots; i++) {
816 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200817 unsigned long start = memslot->userspace_addr;
818 unsigned long end;
819
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200820 end = start + (memslot->npages << PAGE_SHIFT);
821 if (hva >= start && hva < end) {
822 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200823
Izik Eidus3da0dd42009-09-23 21:47:18 +0300824 retval |= handler(kvm, &memslot->rmap[gfn_offset],
825 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200826
827 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
828 int idx = gfn_offset;
829 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
830 retval |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300831 &memslot->lpage_info[j][idx].rmap_pde,
832 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200833 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200834 }
835 }
836
837 return retval;
838}
839
840int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
841{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300842 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200843}
844
Izik Eidus3da0dd42009-09-23 21:47:18 +0300845void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
846{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000847 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300848}
849
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000850static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
851 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200852{
853 u64 *spte;
854 int young = 0;
855
Sheng Yang534e38b2008-09-08 15:12:30 +0800856 /* always return old for EPT */
857 if (!shadow_accessed_mask)
858 return 0;
859
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200860 spte = rmap_next(kvm, rmapp, NULL);
861 while (spte) {
862 int _young;
863 u64 _spte = *spte;
864 BUG_ON(!(_spte & PT_PRESENT_MASK));
865 _young = _spte & PT_ACCESSED_MASK;
866 if (_young) {
867 young = 1;
868 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
869 }
870 spte = rmap_next(kvm, rmapp, spte);
871 }
872 return young;
873}
874
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300875#define RMAP_RECYCLE_THRESHOLD 1000
876
Joerg Roedel852e3c12009-07-27 16:30:44 +0200877static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300878{
879 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200880 struct kvm_mmu_page *sp;
881
882 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300883
884 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200885 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300886
Izik Eidus3da0dd42009-09-23 21:47:18 +0300887 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300888 kvm_flush_remote_tlbs(vcpu->kvm);
889}
890
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200891int kvm_age_hva(struct kvm *kvm, unsigned long hva)
892{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300893 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200894}
895
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800896#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300897static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898{
Avi Kivity139bdb22007-01-05 16:36:50 -0800899 u64 *pos;
900 u64 *end;
901
Avi Kivity47ad8e62007-05-06 15:50:58 +0300902 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300903 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800904 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800905 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800907 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 return 1;
909}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800910#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911
Avi Kivity4db35312007-11-21 15:28:32 +0200912static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800913{
Avi Kivity4db35312007-11-21 15:28:32 +0200914 ASSERT(is_empty_shadow_page(sp->spt));
915 list_del(&sp->link);
916 __free_page(virt_to_page(sp->spt));
917 __free_page(virt_to_page(sp->gfns));
918 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800919 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800920}
921
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800922static unsigned kvm_page_table_hashfn(gfn_t gfn)
923{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200924 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800925}
926
Avi Kivity25c0de22007-01-05 16:36:42 -0800927static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
928 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929{
Avi Kivity4db35312007-11-21 15:28:32 +0200930 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800932 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
933 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
934 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200935 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800936 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200937 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800938 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200939 sp->multimapped = 0;
940 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800941 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200942 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943}
944
Avi Kivity714b93d2007-01-05 16:36:53 -0800945static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200946 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947{
948 struct kvm_pte_chain *pte_chain;
949 struct hlist_node *node;
950 int i;
951
952 if (!parent_pte)
953 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200954 if (!sp->multimapped) {
955 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800956
957 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200958 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800959 return;
960 }
Avi Kivity4db35312007-11-21 15:28:32 +0200961 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800962 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200963 INIT_HLIST_HEAD(&sp->parent_ptes);
964 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800965 pte_chain->parent_ptes[0] = old;
966 }
Avi Kivity4db35312007-11-21 15:28:32 +0200967 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800968 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
969 continue;
970 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
971 if (!pte_chain->parent_ptes[i]) {
972 pte_chain->parent_ptes[i] = parent_pte;
973 return;
974 }
975 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800976 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800977 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200978 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800979 pte_chain->parent_ptes[0] = parent_pte;
980}
981
Avi Kivity4db35312007-11-21 15:28:32 +0200982static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800983 u64 *parent_pte)
984{
985 struct kvm_pte_chain *pte_chain;
986 struct hlist_node *node;
987 int i;
988
Avi Kivity4db35312007-11-21 15:28:32 +0200989 if (!sp->multimapped) {
990 BUG_ON(sp->parent_pte != parent_pte);
991 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800992 return;
993 }
Avi Kivity4db35312007-11-21 15:28:32 +0200994 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800995 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
996 if (!pte_chain->parent_ptes[i])
997 break;
998 if (pte_chain->parent_ptes[i] != parent_pte)
999 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001000 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1001 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001002 pte_chain->parent_ptes[i]
1003 = pte_chain->parent_ptes[i + 1];
1004 ++i;
1005 }
1006 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001007 if (i == 0) {
1008 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001009 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001010 if (hlist_empty(&sp->parent_ptes)) {
1011 sp->multimapped = 0;
1012 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001013 }
1014 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001015 return;
1016 }
1017 BUG();
1018}
1019
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001020
1021static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1022 mmu_parent_walk_fn fn)
1023{
1024 struct kvm_pte_chain *pte_chain;
1025 struct hlist_node *node;
1026 struct kvm_mmu_page *parent_sp;
1027 int i;
1028
1029 if (!sp->multimapped && sp->parent_pte) {
1030 parent_sp = page_header(__pa(sp->parent_pte));
1031 fn(vcpu, parent_sp);
1032 mmu_parent_walk(vcpu, parent_sp, fn);
1033 return;
1034 }
1035 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1036 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1037 if (!pte_chain->parent_ptes[i])
1038 break;
1039 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1040 fn(vcpu, parent_sp);
1041 mmu_parent_walk(vcpu, parent_sp, fn);
1042 }
1043}
1044
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001045static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1046{
1047 unsigned int index;
1048 struct kvm_mmu_page *sp = page_header(__pa(spte));
1049
1050 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001051 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1052 sp->unsync_children++;
1053 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001054}
1055
1056static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1057{
1058 struct kvm_pte_chain *pte_chain;
1059 struct hlist_node *node;
1060 int i;
1061
1062 if (!sp->parent_pte)
1063 return;
1064
1065 if (!sp->multimapped) {
1066 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1067 return;
1068 }
1069
1070 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1071 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1072 if (!pte_chain->parent_ptes[i])
1073 break;
1074 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1075 }
1076}
1077
1078static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1079{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001080 kvm_mmu_update_parents_unsync(sp);
1081 return 1;
1082}
1083
1084static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1085 struct kvm_mmu_page *sp)
1086{
1087 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1088 kvm_mmu_update_parents_unsync(sp);
1089}
1090
Avi Kivityd761a502008-05-29 14:55:03 +03001091static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1092 struct kvm_mmu_page *sp)
1093{
1094 int i;
1095
1096 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1097 sp->spt[i] = shadow_trap_nonpresent_pte;
1098}
1099
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001100static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1101 struct kvm_mmu_page *sp)
1102{
1103 return 1;
1104}
1105
Marcelo Tosattia7052892008-09-23 13:18:35 -03001106static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1107{
1108}
1109
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001110#define KVM_PAGE_ARRAY_NR 16
1111
1112struct kvm_mmu_pages {
1113 struct mmu_page_and_offset {
1114 struct kvm_mmu_page *sp;
1115 unsigned int idx;
1116 } page[KVM_PAGE_ARRAY_NR];
1117 unsigned int nr;
1118};
1119
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001120#define for_each_unsync_children(bitmap, idx) \
1121 for (idx = find_first_bit(bitmap, 512); \
1122 idx < 512; \
1123 idx = find_next_bit(bitmap, 512, idx+1))
1124
Hannes Edercded19f2009-02-21 02:19:13 +01001125static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1126 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001127{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001128 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001129
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001130 if (sp->unsync)
1131 for (i=0; i < pvec->nr; i++)
1132 if (pvec->page[i].sp == sp)
1133 return 0;
1134
1135 pvec->page[pvec->nr].sp = sp;
1136 pvec->page[pvec->nr].idx = idx;
1137 pvec->nr++;
1138 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1139}
1140
1141static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1142 struct kvm_mmu_pages *pvec)
1143{
1144 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001145
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001146 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001147 u64 ent = sp->spt[i];
1148
Marcelo Tosatti87917232008-12-22 18:49:30 -02001149 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001150 struct kvm_mmu_page *child;
1151 child = page_header(ent & PT64_BASE_ADDR_MASK);
1152
1153 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001154 if (mmu_pages_add(pvec, child, i))
1155 return -ENOSPC;
1156
1157 ret = __mmu_unsync_walk(child, pvec);
1158 if (!ret)
1159 __clear_bit(i, sp->unsync_child_bitmap);
1160 else if (ret > 0)
1161 nr_unsync_leaf += ret;
1162 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001163 return ret;
1164 }
1165
1166 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001167 nr_unsync_leaf++;
1168 if (mmu_pages_add(pvec, child, i))
1169 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001170 }
1171 }
1172 }
1173
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001174 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001175 sp->unsync_children = 0;
1176
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001177 return nr_unsync_leaf;
1178}
1179
1180static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1181 struct kvm_mmu_pages *pvec)
1182{
1183 if (!sp->unsync_children)
1184 return 0;
1185
1186 mmu_pages_add(pvec, sp, 0);
1187 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001188}
1189
Avi Kivity4db35312007-11-21 15:28:32 +02001190static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001191{
1192 unsigned index;
1193 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001194 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001195 struct hlist_node *node;
1196
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001197 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001198 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001199 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001200 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001201 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001202 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001203 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001204 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001205 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001206 }
1207 return NULL;
1208}
1209
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001210static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1211{
1212 WARN_ON(!sp->unsync);
1213 sp->unsync = 0;
1214 --kvm->stat.mmu_unsync;
1215}
1216
1217static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1218
1219static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1220{
1221 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1222 kvm_mmu_zap_page(vcpu->kvm, sp);
1223 return 1;
1224 }
1225
Avi Kivityf691fe12009-07-06 15:58:14 +03001226 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001227 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1228 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001229 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1231 kvm_mmu_zap_page(vcpu->kvm, sp);
1232 return 1;
1233 }
1234
1235 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001236 return 0;
1237}
1238
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001239struct mmu_page_path {
1240 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1241 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001242};
1243
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001244#define for_each_sp(pvec, sp, parents, i) \
1245 for (i = mmu_pages_next(&pvec, &parents, -1), \
1246 sp = pvec.page[i].sp; \
1247 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1248 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001249
Hannes Edercded19f2009-02-21 02:19:13 +01001250static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1251 struct mmu_page_path *parents,
1252 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001253{
1254 int n;
1255
1256 for (n = i+1; n < pvec->nr; n++) {
1257 struct kvm_mmu_page *sp = pvec->page[n].sp;
1258
1259 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1260 parents->idx[0] = pvec->page[n].idx;
1261 return n;
1262 }
1263
1264 parents->parent[sp->role.level-2] = sp;
1265 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1266 }
1267
1268 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001269}
1270
Hannes Edercded19f2009-02-21 02:19:13 +01001271static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001272{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001273 struct kvm_mmu_page *sp;
1274 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001275
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001276 do {
1277 unsigned int idx = parents->idx[level];
1278
1279 sp = parents->parent[level];
1280 if (!sp)
1281 return;
1282
1283 --sp->unsync_children;
1284 WARN_ON((int)sp->unsync_children < 0);
1285 __clear_bit(idx, sp->unsync_child_bitmap);
1286 level++;
1287 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1288}
1289
1290static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1291 struct mmu_page_path *parents,
1292 struct kvm_mmu_pages *pvec)
1293{
1294 parents->parent[parent->role.level-1] = NULL;
1295 pvec->nr = 0;
1296}
1297
1298static void mmu_sync_children(struct kvm_vcpu *vcpu,
1299 struct kvm_mmu_page *parent)
1300{
1301 int i;
1302 struct kvm_mmu_page *sp;
1303 struct mmu_page_path parents;
1304 struct kvm_mmu_pages pages;
1305
1306 kvm_mmu_pages_init(parent, &parents, &pages);
1307 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001308 int protected = 0;
1309
1310 for_each_sp(pages, sp, parents, i)
1311 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1312
1313 if (protected)
1314 kvm_flush_remote_tlbs(vcpu->kvm);
1315
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001316 for_each_sp(pages, sp, parents, i) {
1317 kvm_sync_page(vcpu, sp);
1318 mmu_pages_clear_parents(&parents);
1319 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001320 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001321 kvm_mmu_pages_init(parent, &parents, &pages);
1322 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001323}
1324
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001325static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1326 gfn_t gfn,
1327 gva_t gaddr,
1328 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001329 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001330 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001331 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001332{
1333 union kvm_mmu_page_role role;
1334 unsigned index;
1335 unsigned quadrant;
1336 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001337 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001338 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001339
Avi Kivitya770f6f2008-12-21 19:20:09 +02001340 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001341 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001342 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001343 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001344 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001345 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1346 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1347 role.quadrant = quadrant;
1348 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001349 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001350 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001351 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1352 if (sp->gfn == gfn) {
1353 if (sp->unsync)
1354 if (kvm_sync_page(vcpu, sp))
1355 continue;
1356
1357 if (sp->role.word != role.word)
1358 continue;
1359
Avi Kivity4db35312007-11-21 15:28:32 +02001360 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001361 if (sp->unsync_children) {
1362 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1363 kvm_mmu_mark_parents_unsync(vcpu, sp);
1364 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001365 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001366 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001367 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001368 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001369 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1370 if (!sp)
1371 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001372 sp->gfn = gfn;
1373 sp->role = role;
1374 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001375 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001376 if (rmap_write_protect(vcpu->kvm, gfn))
1377 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001378 account_shadowed(vcpu->kvm, gfn);
1379 }
Avi Kivity131d8272008-05-29 14:56:28 +03001380 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1381 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1382 else
1383 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001384 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001385 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001386}
1387
Avi Kivity2d111232008-12-25 14:39:47 +02001388static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1389 struct kvm_vcpu *vcpu, u64 addr)
1390{
1391 iterator->addr = addr;
1392 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1393 iterator->level = vcpu->arch.mmu.shadow_root_level;
1394 if (iterator->level == PT32E_ROOT_LEVEL) {
1395 iterator->shadow_addr
1396 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1397 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1398 --iterator->level;
1399 if (!iterator->shadow_addr)
1400 iterator->level = 0;
1401 }
1402}
1403
1404static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1405{
1406 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1407 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001408
1409 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1410 if (is_large_pte(*iterator->sptep))
1411 return false;
1412
Avi Kivity2d111232008-12-25 14:39:47 +02001413 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1414 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1415 return true;
1416}
1417
1418static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1419{
1420 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1421 --iterator->level;
1422}
1423
Avi Kivity90cb0522007-07-17 13:04:56 +03001424static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001425 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001426{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001427 unsigned i;
1428 u64 *pt;
1429 u64 ent;
1430
Avi Kivity4db35312007-11-21 15:28:32 +02001431 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001432
Avi Kivity697fe2e2007-01-05 16:36:46 -08001433 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1434 ent = pt[i];
1435
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001436 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001437 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001438 ent &= PT64_BASE_ADDR_MASK;
1439 mmu_page_remove_parent_pte(page_header(ent),
1440 &pt[i]);
1441 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001442 if (is_large_pte(ent))
1443 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001444 rmap_remove(kvm, &pt[i]);
1445 }
1446 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001447 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001448 }
Avi Kivitya4360362007-01-05 16:36:45 -08001449}
1450
Avi Kivity4db35312007-11-21 15:28:32 +02001451static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001452{
Avi Kivity4db35312007-11-21 15:28:32 +02001453 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001454}
1455
Avi Kivity12b7d282007-09-23 14:10:49 +02001456static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1457{
1458 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001459 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001460
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001461 kvm_for_each_vcpu(i, vcpu, kvm)
1462 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001463}
1464
Avi Kivity31aa2b42008-07-11 17:59:46 +03001465static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001466{
1467 u64 *parent_pte;
1468
Avi Kivity4db35312007-11-21 15:28:32 +02001469 while (sp->multimapped || sp->parent_pte) {
1470 if (!sp->multimapped)
1471 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001472 else {
1473 struct kvm_pte_chain *chain;
1474
Avi Kivity4db35312007-11-21 15:28:32 +02001475 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001476 struct kvm_pte_chain, link);
1477 parent_pte = chain->parent_ptes[0];
1478 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001479 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001480 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001481 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001482 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001483}
1484
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001485static int mmu_zap_unsync_children(struct kvm *kvm,
1486 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001487{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001488 int i, zapped = 0;
1489 struct mmu_page_path parents;
1490 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001491
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001492 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001493 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001494
1495 kvm_mmu_pages_init(parent, &parents, &pages);
1496 while (mmu_unsync_walk(parent, &pages)) {
1497 struct kvm_mmu_page *sp;
1498
1499 for_each_sp(pages, sp, parents, i) {
1500 kvm_mmu_zap_page(kvm, sp);
1501 mmu_pages_clear_parents(&parents);
1502 }
1503 zapped += pages.nr;
1504 kvm_mmu_pages_init(parent, &parents, &pages);
1505 }
1506
1507 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001508}
1509
Marcelo Tosatti07385412008-09-23 13:18:37 -03001510static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001511{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001512 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001513
1514 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001515 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001516 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001517 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001518 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001519 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001520 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001521 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001522 if (sp->unsync)
1523 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001524 if (!sp->root_count) {
1525 hlist_del(&sp->hash_link);
1526 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001527 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001528 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001529 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001530 kvm_reload_remote_mmus(kvm);
1531 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001532 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001533 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001534}
1535
Izik Eidus82ce2c92007-10-02 18:52:55 +02001536/*
1537 * Changing the number of mmu pages allocated to the vm
1538 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1539 */
1540void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1541{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001542 int used_pages;
1543
1544 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1545 used_pages = max(0, used_pages);
1546
Izik Eidus82ce2c92007-10-02 18:52:55 +02001547 /*
1548 * If we set the number of mmu pages to be smaller be than the
1549 * number of actived pages , we must to free some mmu pages before we
1550 * change the value
1551 */
1552
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001553 if (used_pages > kvm_nr_mmu_pages) {
1554 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001555 struct kvm_mmu_page *page;
1556
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001557 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001558 struct kvm_mmu_page, link);
1559 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001560 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001561 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001562 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001563 }
1564 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001565 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1566 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001567
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001568 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001569}
1570
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001571static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001572{
1573 unsigned index;
1574 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001575 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001576 struct hlist_node *node, *n;
1577 int r;
1578
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001579 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001580 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001581 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001582 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001583 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001584 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001585 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001586 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001587 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001588 if (kvm_mmu_zap_page(kvm, sp))
1589 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001590 }
1591 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001592}
1593
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001594static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001595{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001596 unsigned index;
1597 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001598 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001599 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001600
Avi Kivity4677a3b2009-01-06 13:00:27 +02001601 index = kvm_page_table_hashfn(gfn);
1602 bucket = &kvm->arch.mmu_page_hash[index];
1603 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001604 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001605 && !sp->role.invalid) {
1606 pgprintk("%s: zap %lx %x\n",
1607 __func__, gfn, sp->role.word);
1608 kvm_mmu_zap_page(kvm, sp);
1609 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001610 }
1611}
1612
Avi Kivity38c335f2007-11-21 14:20:22 +02001613static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001615 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001616 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617
Sheng Yang291f26b2008-10-16 17:30:57 +08001618 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619}
1620
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001621static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1622{
1623 int i;
1624 u64 *pt = sp->spt;
1625
1626 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1627 return;
1628
1629 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1630 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001631 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001632 }
1633}
1634
Avi Kivity039576c2007-03-20 12:46:50 +02001635struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1636{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001637 struct page *page;
1638
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001639 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001640
1641 if (gpa == UNMAPPED_GVA)
1642 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001643
Izik Eidus72dc67a2008-02-10 18:04:15 +02001644 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001645
1646 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001647}
1648
Sheng Yang74be52e2008-10-09 16:01:56 +08001649/*
1650 * The function is based on mtrr_type_lookup() in
1651 * arch/x86/kernel/cpu/mtrr/generic.c
1652 */
1653static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1654 u64 start, u64 end)
1655{
1656 int i;
1657 u64 base, mask;
1658 u8 prev_match, curr_match;
1659 int num_var_ranges = KVM_NR_VAR_MTRR;
1660
1661 if (!mtrr_state->enabled)
1662 return 0xFF;
1663
1664 /* Make end inclusive end, instead of exclusive */
1665 end--;
1666
1667 /* Look in fixed ranges. Just return the type as per start */
1668 if (mtrr_state->have_fixed && (start < 0x100000)) {
1669 int idx;
1670
1671 if (start < 0x80000) {
1672 idx = 0;
1673 idx += (start >> 16);
1674 return mtrr_state->fixed_ranges[idx];
1675 } else if (start < 0xC0000) {
1676 idx = 1 * 8;
1677 idx += ((start - 0x80000) >> 14);
1678 return mtrr_state->fixed_ranges[idx];
1679 } else if (start < 0x1000000) {
1680 idx = 3 * 8;
1681 idx += ((start - 0xC0000) >> 12);
1682 return mtrr_state->fixed_ranges[idx];
1683 }
1684 }
1685
1686 /*
1687 * Look in variable ranges
1688 * Look of multiple ranges matching this address and pick type
1689 * as per MTRR precedence
1690 */
1691 if (!(mtrr_state->enabled & 2))
1692 return mtrr_state->def_type;
1693
1694 prev_match = 0xFF;
1695 for (i = 0; i < num_var_ranges; ++i) {
1696 unsigned short start_state, end_state;
1697
1698 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1699 continue;
1700
1701 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1702 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1703 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1704 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1705
1706 start_state = ((start & mask) == (base & mask));
1707 end_state = ((end & mask) == (base & mask));
1708 if (start_state != end_state)
1709 return 0xFE;
1710
1711 if ((start & mask) != (base & mask))
1712 continue;
1713
1714 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1715 if (prev_match == 0xFF) {
1716 prev_match = curr_match;
1717 continue;
1718 }
1719
1720 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1721 curr_match == MTRR_TYPE_UNCACHABLE)
1722 return MTRR_TYPE_UNCACHABLE;
1723
1724 if ((prev_match == MTRR_TYPE_WRBACK &&
1725 curr_match == MTRR_TYPE_WRTHROUGH) ||
1726 (prev_match == MTRR_TYPE_WRTHROUGH &&
1727 curr_match == MTRR_TYPE_WRBACK)) {
1728 prev_match = MTRR_TYPE_WRTHROUGH;
1729 curr_match = MTRR_TYPE_WRTHROUGH;
1730 }
1731
1732 if (prev_match != curr_match)
1733 return MTRR_TYPE_UNCACHABLE;
1734 }
1735
1736 if (prev_match != 0xFF)
1737 return prev_match;
1738
1739 return mtrr_state->def_type;
1740}
1741
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001742u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001743{
1744 u8 mtrr;
1745
1746 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1747 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1748 if (mtrr == 0xfe || mtrr == 0xff)
1749 mtrr = MTRR_TYPE_WRBACK;
1750 return mtrr;
1751}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001752EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001753
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001754static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1755{
1756 unsigned index;
1757 struct hlist_head *bucket;
1758 struct kvm_mmu_page *s;
1759 struct hlist_node *node, *n;
1760
Avi Kivityf691fe12009-07-06 15:58:14 +03001761 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001762 index = kvm_page_table_hashfn(sp->gfn);
1763 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1764 /* don't unsync if pagetable is shadowed with multiple roles */
1765 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001766 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001767 continue;
1768 if (s->role.word != sp->role.word)
1769 return 1;
1770 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001771 ++vcpu->kvm->stat.mmu_unsync;
1772 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001773
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001774 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001775
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001776 mmu_convert_notrap(sp);
1777 return 0;
1778}
1779
1780static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1781 bool can_unsync)
1782{
1783 struct kvm_mmu_page *shadow;
1784
1785 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1786 if (shadow) {
1787 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1788 return 1;
1789 if (shadow->unsync)
1790 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001791 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001792 return kvm_unsync_page(vcpu, shadow);
1793 return 1;
1794 }
1795 return 0;
1796}
1797
Avi Kivityd555c332009-06-10 14:24:23 +03001798static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001799 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001800 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001801 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001802 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001803{
1804 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001805 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001806
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001807 /*
1808 * We don't set the accessed bit, since we sometimes want to see
1809 * whether the guest actually used the pte (in order to detect
1810 * demand paging).
1811 */
Sheng Yang7b523452008-04-25 21:13:50 +08001812 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001813 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001814 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001815 if (!dirty)
1816 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001817 if (pte_access & ACC_EXEC_MASK)
1818 spte |= shadow_x_mask;
1819 else
1820 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001821 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001822 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001823 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001824 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001825 if (tdp_enabled)
1826 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1827 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001828
Izik Eidus14032832009-09-23 21:47:17 +03001829 if (reset_host_protection)
1830 spte |= SPTE_HOST_WRITEABLE;
1831
Anthony Liguori35149e22008-04-02 14:46:56 -05001832 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001833
1834 if ((pte_access & ACC_WRITE_MASK)
1835 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001836
Joerg Roedel852e3c12009-07-27 16:30:44 +02001837 if (level > PT_PAGE_TABLE_LEVEL &&
1838 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001839 ret = 1;
1840 spte = shadow_trap_nonpresent_pte;
1841 goto set_pte;
1842 }
1843
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001844 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001845
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001846 /*
1847 * Optimization: for pte sync, if spte was writable the hash
1848 * lookup is unnecessary (and expensive). Write protection
1849 * is responsibility of mmu_get_page / kvm_sync_page.
1850 * Same reasoning can be applied to dirty page accounting.
1851 */
Avi Kivityd555c332009-06-10 14:24:23 +03001852 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001853 goto set_pte;
1854
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001855 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001856 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001857 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001858 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001859 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001860 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001861 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001862 }
1863 }
1864
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001865 if (pte_access & ACC_WRITE_MASK)
1866 mark_page_dirty(vcpu->kvm, gfn);
1867
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001868set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001869 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001870 return ret;
1871}
1872
Avi Kivityd555c332009-06-10 14:24:23 +03001873static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001874 unsigned pt_access, unsigned pte_access,
1875 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001876 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001877 pfn_t pfn, bool speculative,
1878 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001879{
1880 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001881 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001882 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001883
1884 pgprintk("%s: spte %llx access %x write_fault %d"
1885 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001886 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001887 write_fault, user_fault, gfn);
1888
Avi Kivityd555c332009-06-10 14:24:23 +03001889 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001890 /*
1891 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1892 * the parent of the now unreachable PTE.
1893 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001894 if (level > PT_PAGE_TABLE_LEVEL &&
1895 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001896 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001897 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001898
1899 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001900 mmu_page_remove_parent_pte(child, sptep);
1901 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001902 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001903 spte_to_pfn(*sptep), pfn);
1904 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001905 } else
1906 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001907 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001908
Avi Kivityd555c332009-06-10 14:24:23 +03001909 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001910 dirty, level, gfn, pfn, speculative, true,
1911 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001912 if (write_fault)
1913 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001914 kvm_x86_ops->tlb_flush(vcpu);
1915 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001916
Avi Kivityd555c332009-06-10 14:24:23 +03001917 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001918 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001919 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001920 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1921 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001922 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001923 ++vcpu->kvm->stat.lpages;
1924
Avi Kivityd555c332009-06-10 14:24:23 +03001925 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001926 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001927 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001928 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001929 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001930 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001931 } else {
1932 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001933 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001934 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001935 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001936 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001937 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001938 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001939 vcpu->arch.last_pte_gfn = gfn;
1940 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001941}
1942
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1944{
1945}
1946
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001947static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001948 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949{
Avi Kivity9f652d22008-12-25 14:54:25 +02001950 struct kvm_shadow_walk_iterator iterator;
1951 struct kvm_mmu_page *sp;
1952 int pt_write = 0;
1953 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954
Avi Kivity9f652d22008-12-25 14:54:25 +02001955 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001956 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001957 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1958 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001959 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001960 ++vcpu->stat.pf_fixed;
1961 break;
1962 }
1963
1964 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1965 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1966 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1967 iterator.level - 1,
1968 1, ACC_ALL, iterator.sptep);
1969 if (!sp) {
1970 pgprintk("nonpaging_map: ENOMEM\n");
1971 kvm_release_pfn_clean(pfn);
1972 return -ENOMEM;
1973 }
1974
Avi Kivityd555c332009-06-10 14:24:23 +03001975 __set_spte(iterator.sptep,
1976 __pa(sp->spt)
1977 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1978 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001979 }
1980 }
1981 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982}
1983
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001984static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1985{
1986 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001987 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001988 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001989 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001990
Joerg Roedel852e3c12009-07-27 16:30:44 +02001991 level = mapping_level(vcpu, gfn);
1992
1993 /*
1994 * This path builds a PAE pagetable - so we can map 2mb pages at
1995 * maximum. Therefore check if the level is larger than that.
1996 */
1997 if (level > PT_DIRECTORY_LEVEL)
1998 level = PT_DIRECTORY_LEVEL;
1999
2000 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002001
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002002 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002003 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002004 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002005
Avi Kivityd196e342008-01-24 11:44:11 +02002006 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05002007 if (is_error_pfn(pfn)) {
2008 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002009 return 1;
2010 }
2011
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002012 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002013 if (mmu_notifier_retry(vcpu, mmu_seq))
2014 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002015 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002016 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002017 spin_unlock(&vcpu->kvm->mmu_lock);
2018
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002019
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002020 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002021
2022out_unlock:
2023 spin_unlock(&vcpu->kvm->mmu_lock);
2024 kvm_release_pfn_clean(pfn);
2025 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002026}
2027
2028
Avi Kivity17ac10a2007-01-05 16:36:40 -08002029static void mmu_free_roots(struct kvm_vcpu *vcpu)
2030{
2031 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002032 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002033
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002034 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002035 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002036 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002037 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2038 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002039
Avi Kivity4db35312007-11-21 15:28:32 +02002040 sp = page_header(root);
2041 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002042 if (!sp->root_count && sp->role.invalid)
2043 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002044 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002045 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002046 return;
2047 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002048 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002049 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002050
Avi Kivity417726a2007-04-12 17:35:58 +03002051 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002052 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002053 sp = page_header(root);
2054 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002055 if (!sp->root_count && sp->role.invalid)
2056 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002057 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002058 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002059 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002060 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002061 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002062}
2063
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002064static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2065{
2066 int ret = 0;
2067
2068 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2069 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2070 ret = 1;
2071 }
2072
2073 return ret;
2074}
2075
2076static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002077{
2078 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002079 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002080 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002081 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002082 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002083
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002084 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002085
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002086 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2087 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002088
2089 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002090 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002091 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002092 if (mmu_check_root(vcpu, root_gfn))
2093 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002094 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002095 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002096 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002097 root = __pa(sp->spt);
2098 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002099 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002100 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002101 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002102 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002103 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002104 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002105 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002106 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002107
2108 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002109 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002110 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002111 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002112 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002113 continue;
2114 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002115 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002116 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002117 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002118 if (mmu_check_root(vcpu, root_gfn))
2119 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002120 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002121 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002122 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002123 root = __pa(sp->spt);
2124 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002125 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002126 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002127 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002128 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002129}
2130
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002131static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2132{
2133 int i;
2134 struct kvm_mmu_page *sp;
2135
2136 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2137 return;
2138 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2139 hpa_t root = vcpu->arch.mmu.root_hpa;
2140 sp = page_header(root);
2141 mmu_sync_children(vcpu, sp);
2142 return;
2143 }
2144 for (i = 0; i < 4; ++i) {
2145 hpa_t root = vcpu->arch.mmu.pae_root[i];
2146
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002147 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002148 root &= PT64_BASE_ADDR_MASK;
2149 sp = page_header(root);
2150 mmu_sync_children(vcpu, sp);
2151 }
2152 }
2153}
2154
2155void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2156{
2157 spin_lock(&vcpu->kvm->mmu_lock);
2158 mmu_sync_roots(vcpu);
2159 spin_unlock(&vcpu->kvm->mmu_lock);
2160}
2161
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2163{
2164 return vaddr;
2165}
2166
2167static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002168 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169{
Avi Kivitye8332402007-12-09 18:43:00 +02002170 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002171 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002173 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002174 r = mmu_topup_memory_caches(vcpu);
2175 if (r)
2176 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002177
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002179 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180
Avi Kivitye8332402007-12-09 18:43:00 +02002181 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182
Avi Kivitye8332402007-12-09 18:43:00 +02002183 return nonpaging_map(vcpu, gva & PAGE_MASK,
2184 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185}
2186
Joerg Roedelfb72d162008-02-07 13:47:44 +01002187static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2188 u32 error_code)
2189{
Anthony Liguori35149e22008-04-02 14:46:56 -05002190 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002191 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002192 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002193 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002194 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002195
2196 ASSERT(vcpu);
2197 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2198
2199 r = mmu_topup_memory_caches(vcpu);
2200 if (r)
2201 return r;
2202
Joerg Roedel852e3c12009-07-27 16:30:44 +02002203 level = mapping_level(vcpu, gfn);
2204
2205 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2206
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002207 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002208 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002209 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002210 if (is_error_pfn(pfn)) {
2211 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002212 return 1;
2213 }
2214 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002215 if (mmu_notifier_retry(vcpu, mmu_seq))
2216 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002217 kvm_mmu_free_some_pages(vcpu);
2218 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002219 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002220 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002221
2222 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002223
2224out_unlock:
2225 spin_unlock(&vcpu->kvm->mmu_lock);
2226 kvm_release_pfn_clean(pfn);
2227 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002228}
2229
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230static void nonpaging_free(struct kvm_vcpu *vcpu)
2231{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002232 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233}
2234
2235static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2236{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002237 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238
2239 context->new_cr3 = nonpaging_new_cr3;
2240 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241 context->gva_to_gpa = nonpaging_gva_to_gpa;
2242 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002243 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002244 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002245 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002246 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002248 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 return 0;
2250}
2251
Avi Kivityd835dfe2007-11-21 02:57:59 +02002252void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002254 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002255 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256}
2257
2258static void paging_new_cr3(struct kvm_vcpu *vcpu)
2259{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002260 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002261 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262}
2263
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264static void inject_page_fault(struct kvm_vcpu *vcpu,
2265 u64 addr,
2266 u32 err_code)
2267{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002268 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269}
2270
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271static void paging_free(struct kvm_vcpu *vcpu)
2272{
2273 nonpaging_free(vcpu);
2274}
2275
Dong, Eddie82725b22009-03-30 16:21:08 +08002276static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2277{
2278 int bit7;
2279
2280 bit7 = (gpte >> 7) & 1;
2281 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2282}
2283
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284#define PTTYPE 64
2285#include "paging_tmpl.h"
2286#undef PTTYPE
2287
2288#define PTTYPE 32
2289#include "paging_tmpl.h"
2290#undef PTTYPE
2291
Dong, Eddie82725b22009-03-30 16:21:08 +08002292static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2293{
2294 struct kvm_mmu *context = &vcpu->arch.mmu;
2295 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2296 u64 exb_bit_rsvd = 0;
2297
2298 if (!is_nx(vcpu))
2299 exb_bit_rsvd = rsvd_bits(63, 63);
2300 switch (level) {
2301 case PT32_ROOT_LEVEL:
2302 /* no rsvd bits for 2 level 4K page table entries */
2303 context->rsvd_bits_mask[0][1] = 0;
2304 context->rsvd_bits_mask[0][0] = 0;
2305 if (is_cpuid_PSE36())
2306 /* 36bits PSE 4MB page */
2307 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2308 else
2309 /* 32 bits PSE 4MB page */
2310 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002311 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002312 break;
2313 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002314 context->rsvd_bits_mask[0][2] =
2315 rsvd_bits(maxphyaddr, 63) |
2316 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002317 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002318 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002319 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2320 rsvd_bits(maxphyaddr, 62); /* PTE */
2321 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2322 rsvd_bits(maxphyaddr, 62) |
2323 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002324 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002325 break;
2326 case PT64_ROOT_LEVEL:
2327 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2328 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2329 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2330 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2331 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002332 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002333 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2334 rsvd_bits(maxphyaddr, 51);
2335 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002336 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2337 rsvd_bits(maxphyaddr, 51) |
2338 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002339 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002340 rsvd_bits(maxphyaddr, 51) |
2341 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002342 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002343 break;
2344 }
2345}
2346
Avi Kivity17ac10a2007-01-05 16:36:40 -08002347static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350
2351 ASSERT(is_pae(vcpu));
2352 context->new_cr3 = paging_new_cr3;
2353 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002355 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002356 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002357 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002359 context->root_level = level;
2360 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002361 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362 return 0;
2363}
2364
Avi Kivity17ac10a2007-01-05 16:36:40 -08002365static int paging64_init_context(struct kvm_vcpu *vcpu)
2366{
Dong, Eddie82725b22009-03-30 16:21:08 +08002367 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002368 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2369}
2370
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371static int paging32_init_context(struct kvm_vcpu *vcpu)
2372{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002373 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
Dong, Eddie82725b22009-03-30 16:21:08 +08002375 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 context->new_cr3 = paging_new_cr3;
2377 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 context->gva_to_gpa = paging32_gva_to_gpa;
2379 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002380 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002381 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002382 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 context->root_level = PT32_ROOT_LEVEL;
2384 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002385 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 return 0;
2387}
2388
2389static int paging32E_init_context(struct kvm_vcpu *vcpu)
2390{
Dong, Eddie82725b22009-03-30 16:21:08 +08002391 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002392 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393}
2394
Joerg Roedelfb72d162008-02-07 13:47:44 +01002395static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2396{
2397 struct kvm_mmu *context = &vcpu->arch.mmu;
2398
2399 context->new_cr3 = nonpaging_new_cr3;
2400 context->page_fault = tdp_page_fault;
2401 context->free = nonpaging_free;
2402 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002403 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002404 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002405 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002406 context->root_hpa = INVALID_PAGE;
2407
2408 if (!is_paging(vcpu)) {
2409 context->gva_to_gpa = nonpaging_gva_to_gpa;
2410 context->root_level = 0;
2411 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002412 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002413 context->gva_to_gpa = paging64_gva_to_gpa;
2414 context->root_level = PT64_ROOT_LEVEL;
2415 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002416 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002417 context->gva_to_gpa = paging64_gva_to_gpa;
2418 context->root_level = PT32E_ROOT_LEVEL;
2419 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002420 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002421 context->gva_to_gpa = paging32_gva_to_gpa;
2422 context->root_level = PT32_ROOT_LEVEL;
2423 }
2424
2425 return 0;
2426}
2427
2428static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002430 int r;
2431
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002433 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434
2435 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002436 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002437 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002438 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002440 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002442 r = paging32_init_context(vcpu);
2443
2444 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2445
2446 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447}
2448
Joerg Roedelfb72d162008-02-07 13:47:44 +01002449static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2450{
Anthony Liguori35149e22008-04-02 14:46:56 -05002451 vcpu->arch.update_pte.pfn = bad_pfn;
2452
Joerg Roedelfb72d162008-02-07 13:47:44 +01002453 if (tdp_enabled)
2454 return init_kvm_tdp_mmu(vcpu);
2455 else
2456 return init_kvm_softmmu(vcpu);
2457}
2458
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2460{
2461 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002462 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2463 vcpu->arch.mmu.free(vcpu);
2464 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 }
2466}
2467
2468int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2469{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002470 destroy_kvm_mmu(vcpu);
2471 return init_kvm_mmu(vcpu);
2472}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002473EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002474
2475int kvm_mmu_load(struct kvm_vcpu *vcpu)
2476{
Avi Kivity714b93d2007-01-05 16:36:53 -08002477 int r;
2478
Avi Kivitye2dec932007-01-05 16:36:54 -08002479 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002480 if (r)
2481 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002482 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002483 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002484 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002485 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002486 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002487 if (r)
2488 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002489 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002490 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002491out:
2492 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002494EXPORT_SYMBOL_GPL(kvm_mmu_load);
2495
2496void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2497{
2498 mmu_free_roots(vcpu);
2499}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500
Avi Kivity09072da2007-05-01 14:16:52 +03002501static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002502 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002503 u64 *spte)
2504{
2505 u64 pte;
2506 struct kvm_mmu_page *child;
2507
2508 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002509 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002510 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002511 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002512 else {
2513 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002514 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002515 }
2516 }
Avi Kivityd555c332009-06-10 14:24:23 +03002517 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002518 if (is_large_pte(pte))
2519 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002520}
2521
Avi Kivity00284252007-05-01 16:53:31 +03002522static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002523 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002524 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002525 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002526{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002527 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002528 ++vcpu->kvm->stat.mmu_pde_zapped;
2529 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002530 }
Avi Kivity00284252007-05-01 16:53:31 +03002531
Avi Kivity4cee5762007-11-18 16:37:07 +02002532 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002533 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002534 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002535 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002536 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002537}
2538
Avi Kivity79539ce2007-11-21 02:06:21 +02002539static bool need_remote_flush(u64 old, u64 new)
2540{
2541 if (!is_shadow_present_pte(old))
2542 return false;
2543 if (!is_shadow_present_pte(new))
2544 return true;
2545 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2546 return true;
2547 old ^= PT64_NX_MASK;
2548 new ^= PT64_NX_MASK;
2549 return (old & ~new & PT64_PERM_MASK) != 0;
2550}
2551
2552static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2553{
2554 if (need_remote_flush(old, new))
2555 kvm_flush_remote_tlbs(vcpu->kvm);
2556 else
2557 kvm_mmu_flush_tlb(vcpu);
2558}
2559
Avi Kivity12b7d282007-09-23 14:10:49 +02002560static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2561{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002562 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002563
Sheng Yang7b523452008-04-25 21:13:50 +08002564 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002565}
2566
Avi Kivityd7824ff2007-12-30 12:29:05 +02002567static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2568 const u8 *new, int bytes)
2569{
2570 gfn_t gfn;
2571 int r;
2572 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002573 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002574
2575 if (bytes != 4 && bytes != 8)
2576 return;
2577
2578 /*
2579 * Assume that the pte write on a page table of the same type
2580 * as the current vcpu paging mode. This is nearly always true
2581 * (might be false while changing modes). Note it is verified later
2582 * by update_pte().
2583 */
2584 if (is_pae(vcpu)) {
2585 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2586 if ((bytes == 4) && (gpa % 4 == 0)) {
2587 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2588 if (r)
2589 return;
2590 memcpy((void *)&gpte + (gpa % 8), new, 4);
2591 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2592 memcpy((void *)&gpte, new, 8);
2593 }
2594 } else {
2595 if ((bytes == 4) && (gpa % 4 == 0))
2596 memcpy((void *)&gpte, new, 4);
2597 }
Avi Kivity43a37952009-06-10 14:12:05 +03002598 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002599 return;
2600 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002601
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002602 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002603 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002604 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002605
Anthony Liguori35149e22008-04-02 14:46:56 -05002606 if (is_error_pfn(pfn)) {
2607 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002608 return;
2609 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002610 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002611 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002612}
2613
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002614static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2615{
2616 u64 *spte = vcpu->arch.last_pte_updated;
2617
2618 if (spte
2619 && vcpu->arch.last_pte_gfn == gfn
2620 && shadow_accessed_mask
2621 && !(*spte & shadow_accessed_mask)
2622 && is_shadow_present_pte(*spte))
2623 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2624}
2625
Avi Kivity09072da2007-05-01 14:16:52 +03002626void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002627 const u8 *new, int bytes,
2628 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002629{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002630 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002631 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002632 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002633 struct hlist_head *bucket;
2634 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002635 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002636 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002637 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002638 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002639 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002640 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002641 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002642 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002643 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002644 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002645 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002646
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002647 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002648 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002649 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002650 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002651 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002652 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002653 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002654 if (guest_initiated) {
2655 if (gfn == vcpu->arch.last_pt_write_gfn
2656 && !last_updated_pte_accessed(vcpu)) {
2657 ++vcpu->arch.last_pt_write_count;
2658 if (vcpu->arch.last_pt_write_count >= 3)
2659 flooded = 1;
2660 } else {
2661 vcpu->arch.last_pt_write_gfn = gfn;
2662 vcpu->arch.last_pt_write_count = 1;
2663 vcpu->arch.last_pte_updated = NULL;
2664 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002665 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002666 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002667 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002668 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002669 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002670 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002671 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002672 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002673 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002674 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002675 /*
2676 * Misaligned accesses are too much trouble to fix
2677 * up; also, they usually indicate a page is not used
2678 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002679 *
2680 * If we're seeing too many writes to a page,
2681 * it may no longer be a page table, or we may be
2682 * forking, in which case it is better to unmap the
2683 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002684 */
2685 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002686 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002687 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2688 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002689 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002690 continue;
2691 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002692 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002693 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002694 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002695 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002696 page_offset <<= 1; /* 32->64 */
2697 /*
2698 * A 32-bit pde maps 4MB while the shadow pdes map
2699 * only 2MB. So we need to double the offset again
2700 * and zap two pdes instead of one.
2701 */
2702 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002703 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002704 page_offset <<= 1;
2705 npte = 2;
2706 }
Avi Kivityfce06572007-05-01 16:44:05 +03002707 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002708 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002709 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002710 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002711 }
Avi Kivity4db35312007-11-21 15:28:32 +02002712 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002713 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2714 gentry = 0;
2715 r = kvm_read_guest_atomic(vcpu->kvm,
2716 gpa & ~(u64)(pte_size - 1),
2717 &gentry, pte_size);
2718 new = (const void *)&gentry;
2719 if (r < 0)
2720 new = NULL;
2721 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002722 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002723 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002724 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002725 if (new)
2726 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002727 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002728 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002729 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002730 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002731 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002732 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002733 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2734 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2735 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002736 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002737}
2738
Avi Kivitya4360362007-01-05 16:36:45 -08002739int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2740{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002741 gpa_t gpa;
2742 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002743
Avi Kivity60f24782009-08-27 13:37:06 +03002744 if (tdp_enabled)
2745 return 0;
2746
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002747 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002748
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002749 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002750 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002751 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002752 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002753}
Avi Kivity577bdc42008-07-19 08:57:05 +03002754EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002755
Avi Kivity22d95b12007-09-14 20:26:06 +03002756void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002757{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002758 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2759 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002760 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002761
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002762 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002763 struct kvm_mmu_page, link);
2764 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002765 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002766 }
2767}
Avi Kivityebeace82007-01-05 16:36:47 -08002768
Avi Kivity30677142007-10-28 18:48:59 +02002769int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2770{
2771 int r;
2772 enum emulation_result er;
2773
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002774 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002775 if (r < 0)
2776 goto out;
2777
2778 if (!r) {
2779 r = 1;
2780 goto out;
2781 }
2782
Avi Kivityb733bfb2007-10-28 18:52:05 +02002783 r = mmu_topup_memory_caches(vcpu);
2784 if (r)
2785 goto out;
2786
Avi Kivity851ba692009-08-24 11:10:17 +03002787 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002788
2789 switch (er) {
2790 case EMULATE_DONE:
2791 return 1;
2792 case EMULATE_DO_MMIO:
2793 ++vcpu->stat.mmio_exits;
2794 return 0;
2795 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002796 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2797 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002798 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002799 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002800 default:
2801 BUG();
2802 }
2803out:
Avi Kivity30677142007-10-28 18:48:59 +02002804 return r;
2805}
2806EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2807
Marcelo Tosattia7052892008-09-23 13:18:35 -03002808void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2809{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002810 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002811 kvm_mmu_flush_tlb(vcpu);
2812 ++vcpu->stat.invlpg;
2813}
2814EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2815
Joerg Roedel18552672008-02-07 13:47:41 +01002816void kvm_enable_tdp(void)
2817{
2818 tdp_enabled = true;
2819}
2820EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2821
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002822void kvm_disable_tdp(void)
2823{
2824 tdp_enabled = false;
2825}
2826EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2827
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828static void free_mmu_pages(struct kvm_vcpu *vcpu)
2829{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002830 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831}
2832
2833static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2834{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002835 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 int i;
2837
2838 ASSERT(vcpu);
2839
Avi Kivity17ac10a2007-01-05 16:36:40 -08002840 /*
2841 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2842 * Therefore we need to allocate shadow page tables in the first
2843 * 4GB of memory, which happens to fit the DMA32 zone.
2844 */
2845 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2846 if (!page)
2847 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002849 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002850 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002851
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 return 0;
2853
2854error_1:
2855 free_mmu_pages(vcpu);
2856 return -ENOMEM;
2857}
2858
Ingo Molnar8018c272006-12-29 16:50:01 -08002859int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002862 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863
Ingo Molnar8018c272006-12-29 16:50:01 -08002864 return alloc_mmu_pages(vcpu);
2865}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866
Ingo Molnar8018c272006-12-29 16:50:01 -08002867int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2868{
2869 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002870 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002871
Ingo Molnar8018c272006-12-29 16:50:01 -08002872 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873}
2874
2875void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2876{
2877 ASSERT(vcpu);
2878
2879 destroy_kvm_mmu(vcpu);
2880 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002881 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Avi Kivity90cb0522007-07-17 13:04:56 +03002884void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885{
Avi Kivity4db35312007-11-21 15:28:32 +02002886 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002888 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 int i;
2890 u64 *pt;
2891
Sheng Yang291f26b2008-10-16 17:30:57 +08002892 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 continue;
2894
Avi Kivity4db35312007-11-21 15:28:32 +02002895 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2897 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002898 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 }
Avi Kivity171d5952008-08-27 16:40:51 +03002901 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002903
Avi Kivity90cb0522007-07-17 13:04:56 +03002904void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002905{
Avi Kivity4db35312007-11-21 15:28:32 +02002906 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002907
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002908 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002909 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002910 if (kvm_mmu_zap_page(kvm, sp))
2911 node = container_of(kvm->arch.active_mmu_pages.next,
2912 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002913 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002914
Avi Kivity90cb0522007-07-17 13:04:56 +03002915 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002916}
2917
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002918static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002919{
2920 struct kvm_mmu_page *page;
2921
2922 page = container_of(kvm->arch.active_mmu_pages.prev,
2923 struct kvm_mmu_page, link);
2924 kvm_mmu_zap_page(kvm, page);
2925}
2926
2927static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2928{
2929 struct kvm *kvm;
2930 struct kvm *kvm_freed = NULL;
2931 int cache_count = 0;
2932
2933 spin_lock(&kvm_lock);
2934
2935 list_for_each_entry(kvm, &vm_list, vm_list) {
2936 int npages;
2937
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002938 if (!down_read_trylock(&kvm->slots_lock))
2939 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002940 spin_lock(&kvm->mmu_lock);
2941 npages = kvm->arch.n_alloc_mmu_pages -
2942 kvm->arch.n_free_mmu_pages;
2943 cache_count += npages;
2944 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2945 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2946 cache_count--;
2947 kvm_freed = kvm;
2948 }
2949 nr_to_scan--;
2950
2951 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002952 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002953 }
2954 if (kvm_freed)
2955 list_move_tail(&kvm_freed->vm_list, &vm_list);
2956
2957 spin_unlock(&kvm_lock);
2958
2959 return cache_count;
2960}
2961
2962static struct shrinker mmu_shrinker = {
2963 .shrink = mmu_shrink,
2964 .seeks = DEFAULT_SEEKS * 10,
2965};
2966
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002967static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002968{
2969 if (pte_chain_cache)
2970 kmem_cache_destroy(pte_chain_cache);
2971 if (rmap_desc_cache)
2972 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002973 if (mmu_page_header_cache)
2974 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002975}
2976
Izik Eidus3ee16c82008-03-30 15:17:21 +03002977void kvm_mmu_module_exit(void)
2978{
2979 mmu_destroy_caches();
2980 unregister_shrinker(&mmu_shrinker);
2981}
2982
Avi Kivityb5a33a72007-04-15 16:31:09 +03002983int kvm_mmu_module_init(void)
2984{
2985 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2986 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002987 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002988 if (!pte_chain_cache)
2989 goto nomem;
2990 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2991 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002992 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002993 if (!rmap_desc_cache)
2994 goto nomem;
2995
Avi Kivityd3d25b02007-05-30 12:34:53 +03002996 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2997 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002998 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002999 if (!mmu_page_header_cache)
3000 goto nomem;
3001
Izik Eidus3ee16c82008-03-30 15:17:21 +03003002 register_shrinker(&mmu_shrinker);
3003
Avi Kivityb5a33a72007-04-15 16:31:09 +03003004 return 0;
3005
3006nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003007 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003008 return -ENOMEM;
3009}
3010
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003011/*
3012 * Caculate mmu pages needed for kvm.
3013 */
3014unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3015{
3016 int i;
3017 unsigned int nr_mmu_pages;
3018 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003019 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003020
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003021 slots = rcu_dereference(kvm->memslots);
3022 for (i = 0; i < slots->nmemslots; i++)
3023 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003024
3025 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3026 nr_mmu_pages = max(nr_mmu_pages,
3027 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3028
3029 return nr_mmu_pages;
3030}
3031
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003032static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3033 unsigned len)
3034{
3035 if (len > buffer->len)
3036 return NULL;
3037 return buffer->ptr;
3038}
3039
3040static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3041 unsigned len)
3042{
3043 void *ret;
3044
3045 ret = pv_mmu_peek_buffer(buffer, len);
3046 if (!ret)
3047 return ret;
3048 buffer->ptr += len;
3049 buffer->len -= len;
3050 buffer->processed += len;
3051 return ret;
3052}
3053
3054static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3055 gpa_t addr, gpa_t value)
3056{
3057 int bytes = 8;
3058 int r;
3059
3060 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3061 bytes = 4;
3062
3063 r = mmu_topup_memory_caches(vcpu);
3064 if (r)
3065 return r;
3066
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003067 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003068 return -EFAULT;
3069
3070 return 1;
3071}
3072
3073static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3074{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003075 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003076 return 1;
3077}
3078
3079static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3080{
3081 spin_lock(&vcpu->kvm->mmu_lock);
3082 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3083 spin_unlock(&vcpu->kvm->mmu_lock);
3084 return 1;
3085}
3086
3087static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3088 struct kvm_pv_mmu_op_buffer *buffer)
3089{
3090 struct kvm_mmu_op_header *header;
3091
3092 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3093 if (!header)
3094 return 0;
3095 switch (header->op) {
3096 case KVM_MMU_OP_WRITE_PTE: {
3097 struct kvm_mmu_op_write_pte *wpte;
3098
3099 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3100 if (!wpte)
3101 return 0;
3102 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3103 wpte->pte_val);
3104 }
3105 case KVM_MMU_OP_FLUSH_TLB: {
3106 struct kvm_mmu_op_flush_tlb *ftlb;
3107
3108 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3109 if (!ftlb)
3110 return 0;
3111 return kvm_pv_mmu_flush_tlb(vcpu);
3112 }
3113 case KVM_MMU_OP_RELEASE_PT: {
3114 struct kvm_mmu_op_release_pt *rpt;
3115
3116 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3117 if (!rpt)
3118 return 0;
3119 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3120 }
3121 default: return 0;
3122 }
3123}
3124
3125int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3126 gpa_t addr, unsigned long *ret)
3127{
3128 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003129 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003130
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003131 buffer->ptr = buffer->buf;
3132 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3133 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003134
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003135 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003136 if (r)
3137 goto out;
3138
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003139 while (buffer->len) {
3140 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003141 if (r < 0)
3142 goto out;
3143 if (r == 0)
3144 break;
3145 }
3146
3147 r = 1;
3148out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003149 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003150 return r;
3151}
3152
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003153int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3154{
3155 struct kvm_shadow_walk_iterator iterator;
3156 int nr_sptes = 0;
3157
3158 spin_lock(&vcpu->kvm->mmu_lock);
3159 for_each_shadow_entry(vcpu, addr, iterator) {
3160 sptes[iterator.level-1] = *iterator.sptep;
3161 nr_sptes++;
3162 if (!is_shadow_present_pte(*iterator.sptep))
3163 break;
3164 }
3165 spin_unlock(&vcpu->kvm->mmu_lock);
3166
3167 return nr_sptes;
3168}
3169EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3170
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003171#ifdef AUDIT
3172
3173static const char *audit_msg;
3174
3175static gva_t canonicalize(gva_t gva)
3176{
3177#ifdef CONFIG_X86_64
3178 gva = (long long)(gva << 16) >> 16;
3179#endif
3180 return gva;
3181}
3182
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003183
3184typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3185 u64 *sptep);
3186
3187static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3188 inspect_spte_fn fn)
3189{
3190 int i;
3191
3192 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3193 u64 ent = sp->spt[i];
3194
3195 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003196 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003197 struct kvm_mmu_page *child;
3198 child = page_header(ent & PT64_BASE_ADDR_MASK);
3199 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003200 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003201 fn(kvm, sp, &sp->spt[i]);
3202 }
3203 }
3204}
3205
3206static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3207{
3208 int i;
3209 struct kvm_mmu_page *sp;
3210
3211 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3212 return;
3213 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3214 hpa_t root = vcpu->arch.mmu.root_hpa;
3215 sp = page_header(root);
3216 __mmu_spte_walk(vcpu->kvm, sp, fn);
3217 return;
3218 }
3219 for (i = 0; i < 4; ++i) {
3220 hpa_t root = vcpu->arch.mmu.pae_root[i];
3221
3222 if (root && VALID_PAGE(root)) {
3223 root &= PT64_BASE_ADDR_MASK;
3224 sp = page_header(root);
3225 __mmu_spte_walk(vcpu->kvm, sp, fn);
3226 }
3227 }
3228 return;
3229}
3230
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003231static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3232 gva_t va, int level)
3233{
3234 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3235 int i;
3236 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3237
3238 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3239 u64 ent = pt[i];
3240
Avi Kivityc7addb92007-09-16 18:58:32 +02003241 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003242 continue;
3243
3244 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003245 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3246 audit_mappings_page(vcpu, ent, va, level - 1);
3247 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003248 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003249 gfn_t gfn = gpa >> PAGE_SHIFT;
3250 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3251 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003252
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003253 if (is_error_pfn(pfn)) {
3254 kvm_release_pfn_clean(pfn);
3255 continue;
3256 }
3257
Avi Kivityc7addb92007-09-16 18:58:32 +02003258 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003259 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003260 printk(KERN_ERR "xx audit error: (%s) levels %d"
3261 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003262 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003263 va, gpa, hpa, ent,
3264 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003265 else if (ent == shadow_notrap_nonpresent_pte
3266 && !is_error_hpa(hpa))
3267 printk(KERN_ERR "audit: (%s) notrap shadow,"
3268 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003269 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003270
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003271 }
3272 }
3273}
3274
3275static void audit_mappings(struct kvm_vcpu *vcpu)
3276{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003277 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003278
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003279 if (vcpu->arch.mmu.root_level == 4)
3280 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003281 else
3282 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003283 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003284 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003285 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003286 i << 30,
3287 2);
3288}
3289
3290static int count_rmaps(struct kvm_vcpu *vcpu)
3291{
3292 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003293 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003294
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003295 idx = srcu_read_lock(&kvm->srcu);
3296 slots = rcu_dereference(kvm->memslots);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003297 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003298 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003299 struct kvm_rmap_desc *d;
3300
3301 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003302 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303
Izik Eidus290fc382007-09-27 14:11:22 +02003304 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003305 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003306 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003307 ++nmaps;
3308 continue;
3309 }
Izik Eidus290fc382007-09-27 14:11:22 +02003310 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003311 while (d) {
3312 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003313 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003314 ++nmaps;
3315 else
3316 break;
3317 d = d->more;
3318 }
3319 }
3320 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003321 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003322 return nmaps;
3323}
3324
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003325void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003326{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003327 unsigned long *rmapp;
3328 struct kvm_mmu_page *rev_sp;
3329 gfn_t gfn;
3330
3331 if (*sptep & PT_WRITABLE_MASK) {
3332 rev_sp = page_header(__pa(sptep));
3333 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3334
3335 if (!gfn_to_memslot(kvm, gfn)) {
3336 if (!printk_ratelimit())
3337 return;
3338 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3339 audit_msg, gfn);
3340 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3341 audit_msg, sptep - rev_sp->spt,
3342 rev_sp->gfn);
3343 dump_stack();
3344 return;
3345 }
3346
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003347 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3348 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003349 if (!*rmapp) {
3350 if (!printk_ratelimit())
3351 return;
3352 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3353 audit_msg, *sptep);
3354 dump_stack();
3355 }
3356 }
3357
3358}
3359
3360void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3361{
3362 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3363}
3364
3365static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3366{
Avi Kivity4db35312007-11-21 15:28:32 +02003367 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003368 int i;
3369
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003370 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003371 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003372
Avi Kivity4db35312007-11-21 15:28:32 +02003373 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003374 continue;
3375
3376 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3377 u64 ent = pt[i];
3378
3379 if (!(ent & PT_PRESENT_MASK))
3380 continue;
3381 if (!(ent & PT_WRITABLE_MASK))
3382 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003383 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003384 }
3385 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003386 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003387}
3388
3389static void audit_rmap(struct kvm_vcpu *vcpu)
3390{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003391 check_writable_mappings_rmap(vcpu);
3392 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003393}
3394
3395static void audit_write_protection(struct kvm_vcpu *vcpu)
3396{
Avi Kivity4db35312007-11-21 15:28:32 +02003397 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003398 struct kvm_memory_slot *slot;
3399 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003400 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003401 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003402
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003403 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003404 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003405 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003406 if (sp->unsync)
3407 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003408
Avi Kivity4db35312007-11-21 15:28:32 +02003409 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003410 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003411 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003412
3413 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3414 while (spte) {
3415 if (*spte & PT_WRITABLE_MASK)
3416 printk(KERN_ERR "%s: (%s) shadow page has "
3417 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003418 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003419 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003420 spte = rmap_next(vcpu->kvm, rmapp, spte);
3421 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003422 }
3423}
3424
3425static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3426{
3427 int olddbg = dbg;
3428
3429 dbg = 0;
3430 audit_msg = msg;
3431 audit_rmap(vcpu);
3432 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003433 if (strcmp("pre pte write", audit_msg) != 0)
3434 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003435 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003436 dbg = olddbg;
3437}
3438
3439#endif