blob: 913ef4b7939ad7e5cbc0d578c494d4469cad0c99 [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 Kivity836a1b32010-01-21 15:31:49 +020021#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030022#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040025#include <linux/types.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/highmem.h>
29#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020030#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030031#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050032#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020033#include <linux/srcu.h>
Avi Kivitye4956062007-06-28 14:15:57 -040034
35#include <asm/page.h>
36#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020037#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020038#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040039
Joerg Roedel18552672008-02-07 13:47:41 +010040/*
41 * When setting this variable to true it enables Two-Dimensional-Paging
42 * where the hardware walks 2 page tables:
43 * 1. the guest-virtual to guest-physical
44 * 2. while doing 1. it walks guest-physical to host-physical
45 * If the hardware supports that we don't need to do shadow paging.
46 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050047bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010048
Avi Kivity37a7d8b2007-01-05 16:36:56 -080049#undef MMU_DEBUG
50
51#undef AUDIT
52
53#ifdef AUDIT
54static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
55#else
56static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
57#endif
58
59#ifdef MMU_DEBUG
60
61#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
62#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
63
64#else
65
66#define pgprintk(x...) do { } while (0)
67#define rmap_printk(x...) do { } while (0)
68
69#endif
70
71#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030072static int dbg = 0;
73module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080074#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080075
Marcelo Tosatti582801a2008-09-23 13:18:41 -030076static int oos_shadow = 1;
77module_param(oos_shadow, bool, 0644);
78
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080079#ifndef MMU_DEBUG
80#define ASSERT(x) do { } while (0)
81#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080082#define ASSERT(x) \
83 if (!(x)) { \
84 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
85 __FILE__, __LINE__, #x); \
86 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080087#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080088
Avi Kivity6aa8b732006-12-10 02:21:36 -080089#define PT_FIRST_AVAIL_BITS_SHIFT 9
90#define PT64_SECOND_AVAIL_BITS_SHIFT 52
91
Avi Kivity6aa8b732006-12-10 02:21:36 -080092#define VALID_PAGE(x) ((x) != INVALID_PAGE)
93
94#define PT64_LEVEL_BITS 9
95
96#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040097 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
99#define PT64_LEVEL_MASK(level) \
100 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
101
102#define PT64_INDEX(address, level)\
103 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
104
105
106#define PT32_LEVEL_BITS 10
107
108#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400109 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110
111#define PT32_LEVEL_MASK(level) \
112 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200113#define PT32_LVL_OFFSET_MASK(level) \
114 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
115 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116
117#define PT32_INDEX(address, level)\
118 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
119
120
Avi Kivity27aba762007-03-09 13:04:31 +0200121#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122#define PT64_DIR_BASE_ADDR_MASK \
123 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200124#define PT64_LVL_ADDR_MASK(level) \
125 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
126 * PT64_LEVEL_BITS))) - 1))
127#define PT64_LVL_OFFSET_MASK(level) \
128 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
129 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
131#define PT32_BASE_ADDR_MASK PAGE_MASK
132#define PT32_DIR_BASE_ADDR_MASK \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200134#define PT32_LVL_ADDR_MASK(level) \
135 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137
Avi Kivity79539ce2007-11-21 02:06:21 +0200138#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
139 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PFERR_PRESENT_MASK (1U << 0)
142#define PFERR_WRITE_MASK (1U << 1)
143#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800144#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800145#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800147#define RMAP_EXT 4
148
Avi Kivityfe135d22007-12-09 16:15:46 +0200149#define ACC_EXEC_MASK 1
150#define ACC_WRITE_MASK PT_WRITABLE_MASK
151#define ACC_USER_MASK PT_USER_MASK
152#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
153
Avi Kivity07420172009-07-06 12:21:32 +0300154#define CREATE_TRACE_POINTS
155#include "mmutrace.h"
156
Izik Eidus14032832009-09-23 21:47:17 +0300157#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
158
Avi Kivity135f8c22008-08-21 17:49:56 +0300159#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
160
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800161struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300162 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163 struct kvm_rmap_desc *more;
164};
165
Avi Kivity2d111232008-12-25 14:39:47 +0200166struct kvm_shadow_walk_iterator {
167 u64 addr;
168 hpa_t shadow_addr;
169 int level;
170 u64 *sptep;
171 unsigned index;
172};
173
174#define for_each_shadow_entry(_vcpu, _addr, _walker) \
175 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
176 shadow_walk_okay(&(_walker)); \
177 shadow_walk_next(&(_walker)))
178
179
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300180struct kvm_unsync_walk {
181 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
182};
183
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300184typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
185
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186static struct kmem_cache *pte_chain_cache;
187static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300188static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300189
Avi Kivityc7addb92007-09-16 18:58:32 +0200190static u64 __read_mostly shadow_trap_nonpresent_pte;
191static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800192static u64 __read_mostly shadow_base_present_pte;
193static u64 __read_mostly shadow_nx_mask;
194static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
195static u64 __read_mostly shadow_user_mask;
196static u64 __read_mostly shadow_accessed_mask;
197static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200198
Dong, Eddie82725b22009-03-30 16:21:08 +0800199static inline u64 rsvd_bits(int s, int e)
200{
201 return ((1ULL << (e - s + 1)) - 1) << s;
202}
203
Avi Kivityc7addb92007-09-16 18:58:32 +0200204void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
205{
206 shadow_trap_nonpresent_pte = trap_pte;
207 shadow_notrap_nonpresent_pte = notrap_pte;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
210
Sheng Yang7b523452008-04-25 21:13:50 +0800211void kvm_mmu_set_base_ptes(u64 base_pte)
212{
213 shadow_base_present_pte = base_pte;
214}
215EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
216
217void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800218 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800219{
220 shadow_user_mask = user_mask;
221 shadow_accessed_mask = accessed_mask;
222 shadow_dirty_mask = dirty_mask;
223 shadow_nx_mask = nx_mask;
224 shadow_x_mask = x_mask;
225}
226EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_write_protection(struct kvm_vcpu *vcpu)
229{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200230 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231}
232
233static int is_cpuid_PSE36(void)
234{
235 return 1;
236}
237
Avi Kivity73b10872007-01-26 00:56:41 -0800238static int is_nx(struct kvm_vcpu *vcpu)
239{
Avi Kivityf6801df2010-01-21 15:31:50 +0200240 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800241}
242
Avi Kivityc7addb92007-09-16 18:58:32 +0200243static int is_shadow_present_pte(u64 pte)
244{
Avi Kivityc7addb92007-09-16 18:58:32 +0200245 return pte != shadow_trap_nonpresent_pte
246 && pte != shadow_notrap_nonpresent_pte;
247}
248
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300249static int is_large_pte(u64 pte)
250{
251 return pte & PT_PAGE_SIZE_MASK;
252}
253
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900254static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255{
256 return pte & PT_WRITABLE_MASK;
257}
258
Avi Kivity43a37952009-06-10 14:12:05 +0300259static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200260{
Avi Kivity439e2182009-06-10 12:56:54 +0300261 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200262}
263
Avi Kivity43a37952009-06-10 14:12:05 +0300264static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800265{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200266 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800267}
268
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300269static int is_last_spte(u64 pte, int level)
270{
271 if (level == PT_PAGE_TABLE_LEVEL)
272 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200273 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300274 return 1;
275 return 0;
276}
277
Anthony Liguori35149e22008-04-02 14:46:56 -0500278static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200279{
Anthony Liguori35149e22008-04-02 14:46:56 -0500280 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200281}
282
Avi Kivityda928522007-11-21 13:54:47 +0200283static gfn_t pse36_gfn_delta(u32 gpte)
284{
285 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
286
287 return (gpte & PT32_DIR_PSE36_MASK) << shift;
288}
289
Avi Kivityd555c332009-06-10 14:24:23 +0300290static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300291{
292#ifdef CONFIG_X86_64
293 set_64bit((unsigned long *)sptep, spte);
294#else
295 set_64bit((unsigned long long *)sptep, spte);
296#endif
297}
298
Avi Kivitye2dec932007-01-05 16:36:54 -0800299static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300300 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800301{
302 void *obj;
303
304 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800305 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800306 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300307 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800308 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800309 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800310 cache->objects[cache->nobjs++] = obj;
311 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800312 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800313}
314
315static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
316{
317 while (mc->nobjs)
318 kfree(mc->objects[--mc->nobjs]);
319}
320
Avi Kivityc1158e62007-07-20 08:18:27 +0300321static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300322 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300323{
324 struct page *page;
325
326 if (cache->nobjs >= min)
327 return 0;
328 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300329 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300330 if (!page)
331 return -ENOMEM;
332 set_page_private(page, 0);
333 cache->objects[cache->nobjs++] = page_address(page);
334 }
335 return 0;
336}
337
338static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
339{
340 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300341 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300342}
343
Avi Kivity8c438502007-04-16 11:53:17 +0300344static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
345{
346 int r;
347
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800348 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300349 pte_chain_cache, 4);
350 if (r)
351 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200353 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300354 if (r)
355 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800356 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
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(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300360 mmu_page_header_cache, 4);
361out:
Avi Kivity8c438502007-04-16 11:53:17 +0300362 return r;
363}
364
Avi Kivity714b93d2007-01-05 16:36:53 -0800365static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
366{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800367 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
368 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
369 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
370 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800371}
372
373static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
374 size_t size)
375{
376 void *p;
377
378 BUG_ON(!mc->nobjs);
379 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800380 return p;
381}
382
Avi Kivity714b93d2007-01-05 16:36:53 -0800383static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
384{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800385 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800386 sizeof(struct kvm_pte_chain));
387}
388
Avi Kivity90cb0522007-07-17 13:04:56 +0300389static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800390{
Avi Kivity90cb0522007-07-17 13:04:56 +0300391 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800392}
393
394static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
395{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800396 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800397 sizeof(struct kvm_rmap_desc));
398}
399
Avi Kivity90cb0522007-07-17 13:04:56 +0300400static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800401{
Avi Kivity90cb0522007-07-17 13:04:56 +0300402 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800403}
404
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300406 * Return the pointer to the largepage write count for a given
407 * gfn, handling slots that are not large page aligned.
408 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200409static int *slot_largepage_idx(gfn_t gfn,
410 struct kvm_memory_slot *slot,
411 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300412{
413 unsigned long idx;
414
Joerg Roedeld25797b2009-07-27 16:30:43 +0200415 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
416 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
417 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418}
419
420static void account_shadowed(struct kvm *kvm, gfn_t gfn)
421{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200422 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200424 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300425
Izik Eidus28430992008-10-03 17:40:32 +0300426 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200427
428 slot = gfn_to_memslot_unaliased(kvm, gfn);
429 for (i = PT_DIRECTORY_LEVEL;
430 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
431 write_count = slot_largepage_idx(gfn, slot, i);
432 *write_count += 1;
433 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434}
435
436static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
437{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200440 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441
Izik Eidus28430992008-10-03 17:40:32 +0300442 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200443 for (i = PT_DIRECTORY_LEVEL;
444 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
445 slot = gfn_to_memslot_unaliased(kvm, gfn);
446 write_count = slot_largepage_idx(gfn, slot, i);
447 *write_count -= 1;
448 WARN_ON(*write_count < 0);
449 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450}
451
Joerg Roedeld25797b2009-07-27 16:30:43 +0200452static int has_wrprotected_page(struct kvm *kvm,
453 gfn_t gfn,
454 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455{
Izik Eidus28430992008-10-03 17:40:32 +0300456 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300457 int *largepage_idx;
458
Izik Eidus28430992008-10-03 17:40:32 +0300459 gfn = unalias_gfn(kvm, gfn);
460 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200462 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300463 return *largepage_idx;
464 }
465
466 return 1;
467}
468
Joerg Roedeld25797b2009-07-27 16:30:43 +0200469static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100471 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200472 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300473
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100474 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475
Joerg Roedeld25797b2009-07-27 16:30:43 +0200476 for (i = PT_PAGE_TABLE_LEVEL;
477 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
478 if (page_size >= KVM_HPAGE_SIZE(i))
479 ret = i;
480 else
481 break;
482 }
483
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300484 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300485}
486
Joerg Roedeld25797b2009-07-27 16:30:43 +0200487static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300488{
489 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800490 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300491
492 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
493 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200494 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300495
Joerg Roedeld25797b2009-07-27 16:30:43 +0200496 host_level = host_mapping_level(vcpu->kvm, large_gfn);
497
498 if (host_level == PT_PAGE_TABLE_LEVEL)
499 return host_level;
500
Sheng Yang878403b2010-01-05 19:02:29 +0800501 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
502 kvm_x86_ops->get_lpage_level() : host_level;
503
504 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
506 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200507
508 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300509}
510
511/*
Izik Eidus290fc382007-09-27 14:11:22 +0200512 * Take gfn and return the reverse mapping to it.
513 * Note: gfn must be unaliased before this function get called
514 */
515
Joerg Roedel44ad9942009-07-27 16:30:42 +0200516static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200517{
518 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300519 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200520
521 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200522 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300523 return &slot->rmap[gfn - slot->base_gfn];
524
Joerg Roedel44ad9942009-07-27 16:30:42 +0200525 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
526 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300527
Joerg Roedel44ad9942009-07-27 16:30:42 +0200528 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200529}
530
531/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800532 * Reverse mapping data structures:
533 *
Izik Eidus290fc382007-09-27 14:11:22 +0200534 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
535 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800536 *
Izik Eidus290fc382007-09-27 14:11:22 +0200537 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
538 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300539 *
540 * Returns the number of rmap entries before the spte was added or zero if
541 * the spte was not added.
542 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200544static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800545{
Avi Kivity4db35312007-11-21 15:28:32 +0200546 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200548 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300549 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550
Avi Kivity43a37952009-06-10 14:12:05 +0300551 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300552 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200553 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200554 sp = page_header(__pa(spte));
555 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200556 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200557 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200559 *rmapp = (unsigned long)spte;
560 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800562 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300563 desc->sptes[0] = (u64 *)*rmapp;
564 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200565 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566 } else {
567 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200568 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300569 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800570 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300571 count += RMAP_EXT;
572 }
Avi Kivityd555c332009-06-10 14:24:23 +0300573 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800574 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 desc = desc->more;
576 }
Avi Kivityd555c332009-06-10 14:24:23 +0300577 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800578 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300579 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300581 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800582}
583
Izik Eidus290fc382007-09-27 14:11:22 +0200584static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800585 struct kvm_rmap_desc *desc,
586 int i,
587 struct kvm_rmap_desc *prev_desc)
588{
589 int j;
590
Avi Kivityd555c332009-06-10 14:24:23 +0300591 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800592 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300593 desc->sptes[i] = desc->sptes[j];
594 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800595 if (j != 0)
596 return;
597 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300598 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800599 else
600 if (prev_desc)
601 prev_desc->more = desc->more;
602 else
Izik Eidus290fc382007-09-27 14:11:22 +0200603 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300604 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800605}
606
Izik Eidus290fc382007-09-27 14:11:22 +0200607static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800608{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609 struct kvm_rmap_desc *desc;
610 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200611 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500612 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200613 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800614 int i;
615
Avi Kivity43a37952009-06-10 14:12:05 +0300616 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800617 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200618 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500619 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800620 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500621 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900622 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300623 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200624 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200625 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
627 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200628 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200630 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800631 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
632 spte, *spte);
633 BUG();
634 }
Izik Eidus290fc382007-09-27 14:11:22 +0200635 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 } else {
637 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200638 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 prev_desc = NULL;
640 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300641 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
642 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200643 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800644 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800645 prev_desc);
646 return;
647 }
648 prev_desc = desc;
649 desc = desc->more;
650 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200651 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800652 BUG();
653 }
654}
655
Izik Eidus98348e92007-10-16 14:42:30 +0200656static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800657{
Avi Kivity374cbac2007-01-05 16:36:43 -0800658 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200659 struct kvm_rmap_desc *prev_desc;
660 u64 *prev_spte;
661 int i;
662
663 if (!*rmapp)
664 return NULL;
665 else if (!(*rmapp & 1)) {
666 if (!spte)
667 return (u64 *)*rmapp;
668 return NULL;
669 }
670 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
671 prev_desc = NULL;
672 prev_spte = NULL;
673 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300674 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200675 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300676 return desc->sptes[i];
677 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200678 }
679 desc = desc->more;
680 }
681 return NULL;
682}
683
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200684static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200685{
Izik Eidus290fc382007-09-27 14:11:22 +0200686 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800687 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200688 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800689
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500690 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200691 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800692
Izik Eidus98348e92007-10-16 14:42:30 +0200693 spte = rmap_next(kvm, rmapp, NULL);
694 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800695 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800696 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800697 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900698 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300699 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800700 write_protected = 1;
701 }
Izik Eidus9647c142007-10-16 14:43:46 +0200702 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800703 }
Izik Eidus855149a2008-03-20 18:17:24 +0200704 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500705 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200706
707 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500708 pfn = spte_to_pfn(*spte);
709 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200710 }
711
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300712 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200713 for (i = PT_DIRECTORY_LEVEL;
714 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
715 rmapp = gfn_to_rmap(kvm, gfn, i);
716 spte = rmap_next(kvm, rmapp, NULL);
717 while (spte) {
718 BUG_ON(!spte);
719 BUG_ON(!(*spte & PT_PRESENT_MASK));
720 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
721 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900722 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200723 rmap_remove(kvm, spte);
724 --kvm->stat.lpages;
725 __set_spte(spte, shadow_trap_nonpresent_pte);
726 spte = NULL;
727 write_protected = 1;
728 }
729 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300730 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300731 }
732
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200733 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800734}
735
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000736static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
737 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200738{
739 u64 *spte;
740 int need_tlb_flush = 0;
741
742 while ((spte = rmap_next(kvm, rmapp, NULL))) {
743 BUG_ON(!(*spte & PT_PRESENT_MASK));
744 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
745 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300746 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200747 need_tlb_flush = 1;
748 }
749 return need_tlb_flush;
750}
751
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000752static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
753 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300754{
755 int need_flush = 0;
756 u64 *spte, new_spte;
757 pte_t *ptep = (pte_t *)data;
758 pfn_t new_pfn;
759
760 WARN_ON(pte_huge(*ptep));
761 new_pfn = pte_pfn(*ptep);
762 spte = rmap_next(kvm, rmapp, NULL);
763 while (spte) {
764 BUG_ON(!is_shadow_present_pte(*spte));
765 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
766 need_flush = 1;
767 if (pte_write(*ptep)) {
768 rmap_remove(kvm, spte);
769 __set_spte(spte, shadow_trap_nonpresent_pte);
770 spte = rmap_next(kvm, rmapp, NULL);
771 } else {
772 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
773 new_spte |= (u64)new_pfn << PAGE_SHIFT;
774
775 new_spte &= ~PT_WRITABLE_MASK;
776 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900777 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300778 kvm_set_pfn_dirty(spte_to_pfn(*spte));
779 __set_spte(spte, new_spte);
780 spte = rmap_next(kvm, rmapp, spte);
781 }
782 }
783 if (need_flush)
784 kvm_flush_remote_tlbs(kvm);
785
786 return 0;
787}
788
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000789static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
790 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300791 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000792 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200793{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200794 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200795 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200796 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200797
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200798 slots = rcu_dereference(kvm->memslots);
799
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200800 for (i = 0; i < slots->nmemslots; i++) {
801 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200802 unsigned long start = memslot->userspace_addr;
803 unsigned long end;
804
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200805 end = start + (memslot->npages << PAGE_SHIFT);
806 if (hva >= start && hva < end) {
807 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200808
Izik Eidus3da0dd42009-09-23 21:47:18 +0300809 retval |= handler(kvm, &memslot->rmap[gfn_offset],
810 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200811
812 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
813 int idx = gfn_offset;
814 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
815 retval |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300816 &memslot->lpage_info[j][idx].rmap_pde,
817 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200818 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200819 }
820 }
821
822 return retval;
823}
824
825int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
826{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300827 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200828}
829
Izik Eidus3da0dd42009-09-23 21:47:18 +0300830void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
831{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000832 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300833}
834
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000835static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
836 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200837{
838 u64 *spte;
839 int young = 0;
840
Sheng Yang534e38b2008-09-08 15:12:30 +0800841 /* always return old for EPT */
842 if (!shadow_accessed_mask)
843 return 0;
844
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200845 spte = rmap_next(kvm, rmapp, NULL);
846 while (spte) {
847 int _young;
848 u64 _spte = *spte;
849 BUG_ON(!(_spte & PT_PRESENT_MASK));
850 _young = _spte & PT_ACCESSED_MASK;
851 if (_young) {
852 young = 1;
853 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
854 }
855 spte = rmap_next(kvm, rmapp, spte);
856 }
857 return young;
858}
859
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300860#define RMAP_RECYCLE_THRESHOLD 1000
861
Joerg Roedel852e3c12009-07-27 16:30:44 +0200862static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300863{
864 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200865 struct kvm_mmu_page *sp;
866
867 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300868
869 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200870 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300871
Izik Eidus3da0dd42009-09-23 21:47:18 +0300872 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300873 kvm_flush_remote_tlbs(vcpu->kvm);
874}
875
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200876int kvm_age_hva(struct kvm *kvm, unsigned long hva)
877{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300878 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200879}
880
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800881#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300882static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883{
Avi Kivity139bdb22007-01-05 16:36:50 -0800884 u64 *pos;
885 u64 *end;
886
Avi Kivity47ad8e62007-05-06 15:50:58 +0300887 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300888 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800889 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800890 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800892 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 return 1;
894}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800895#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896
Avi Kivity4db35312007-11-21 15:28:32 +0200897static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800898{
Avi Kivity4db35312007-11-21 15:28:32 +0200899 ASSERT(is_empty_shadow_page(sp->spt));
900 list_del(&sp->link);
901 __free_page(virt_to_page(sp->spt));
902 __free_page(virt_to_page(sp->gfns));
903 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800904 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800905}
906
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800907static unsigned kvm_page_table_hashfn(gfn_t gfn)
908{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200909 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800910}
911
Avi Kivity25c0de22007-01-05 16:36:42 -0800912static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
913 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914{
Avi Kivity4db35312007-11-21 15:28:32 +0200915 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800917 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
918 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
919 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200920 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800921 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200922 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800923 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200924 sp->multimapped = 0;
925 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800926 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200927 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928}
929
Avi Kivity714b93d2007-01-05 16:36:53 -0800930static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200931 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800932{
933 struct kvm_pte_chain *pte_chain;
934 struct hlist_node *node;
935 int i;
936
937 if (!parent_pte)
938 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200939 if (!sp->multimapped) {
940 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800941
942 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200943 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800944 return;
945 }
Avi Kivity4db35312007-11-21 15:28:32 +0200946 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800947 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200948 INIT_HLIST_HEAD(&sp->parent_ptes);
949 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800950 pte_chain->parent_ptes[0] = old;
951 }
Avi Kivity4db35312007-11-21 15:28:32 +0200952 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800953 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
954 continue;
955 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
956 if (!pte_chain->parent_ptes[i]) {
957 pte_chain->parent_ptes[i] = parent_pte;
958 return;
959 }
960 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800961 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800962 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200963 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 pte_chain->parent_ptes[0] = parent_pte;
965}
966
Avi Kivity4db35312007-11-21 15:28:32 +0200967static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800968 u64 *parent_pte)
969{
970 struct kvm_pte_chain *pte_chain;
971 struct hlist_node *node;
972 int i;
973
Avi Kivity4db35312007-11-21 15:28:32 +0200974 if (!sp->multimapped) {
975 BUG_ON(sp->parent_pte != parent_pte);
976 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800977 return;
978 }
Avi Kivity4db35312007-11-21 15:28:32 +0200979 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800980 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
981 if (!pte_chain->parent_ptes[i])
982 break;
983 if (pte_chain->parent_ptes[i] != parent_pte)
984 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800985 while (i + 1 < NR_PTE_CHAIN_ENTRIES
986 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800987 pte_chain->parent_ptes[i]
988 = pte_chain->parent_ptes[i + 1];
989 ++i;
990 }
991 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800992 if (i == 0) {
993 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300994 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200995 if (hlist_empty(&sp->parent_ptes)) {
996 sp->multimapped = 0;
997 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800998 }
999 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001000 return;
1001 }
1002 BUG();
1003}
1004
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001005
1006static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1007 mmu_parent_walk_fn fn)
1008{
1009 struct kvm_pte_chain *pte_chain;
1010 struct hlist_node *node;
1011 struct kvm_mmu_page *parent_sp;
1012 int i;
1013
1014 if (!sp->multimapped && sp->parent_pte) {
1015 parent_sp = page_header(__pa(sp->parent_pte));
1016 fn(vcpu, parent_sp);
1017 mmu_parent_walk(vcpu, parent_sp, fn);
1018 return;
1019 }
1020 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1021 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1022 if (!pte_chain->parent_ptes[i])
1023 break;
1024 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1025 fn(vcpu, parent_sp);
1026 mmu_parent_walk(vcpu, parent_sp, fn);
1027 }
1028}
1029
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001030static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1031{
1032 unsigned int index;
1033 struct kvm_mmu_page *sp = page_header(__pa(spte));
1034
1035 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001036 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1037 sp->unsync_children++;
1038 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001039}
1040
1041static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1042{
1043 struct kvm_pte_chain *pte_chain;
1044 struct hlist_node *node;
1045 int i;
1046
1047 if (!sp->parent_pte)
1048 return;
1049
1050 if (!sp->multimapped) {
1051 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1052 return;
1053 }
1054
1055 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1056 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1057 if (!pte_chain->parent_ptes[i])
1058 break;
1059 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1060 }
1061}
1062
1063static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1064{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001065 kvm_mmu_update_parents_unsync(sp);
1066 return 1;
1067}
1068
1069static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1070 struct kvm_mmu_page *sp)
1071{
1072 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1073 kvm_mmu_update_parents_unsync(sp);
1074}
1075
Avi Kivityd761a502008-05-29 14:55:03 +03001076static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1077 struct kvm_mmu_page *sp)
1078{
1079 int i;
1080
1081 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1082 sp->spt[i] = shadow_trap_nonpresent_pte;
1083}
1084
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001085static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1086 struct kvm_mmu_page *sp)
1087{
1088 return 1;
1089}
1090
Marcelo Tosattia7052892008-09-23 13:18:35 -03001091static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1092{
1093}
1094
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001095#define KVM_PAGE_ARRAY_NR 16
1096
1097struct kvm_mmu_pages {
1098 struct mmu_page_and_offset {
1099 struct kvm_mmu_page *sp;
1100 unsigned int idx;
1101 } page[KVM_PAGE_ARRAY_NR];
1102 unsigned int nr;
1103};
1104
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001105#define for_each_unsync_children(bitmap, idx) \
1106 for (idx = find_first_bit(bitmap, 512); \
1107 idx < 512; \
1108 idx = find_next_bit(bitmap, 512, idx+1))
1109
Hannes Edercded19f2009-02-21 02:19:13 +01001110static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1111 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001112{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001113 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001114
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001115 if (sp->unsync)
1116 for (i=0; i < pvec->nr; i++)
1117 if (pvec->page[i].sp == sp)
1118 return 0;
1119
1120 pvec->page[pvec->nr].sp = sp;
1121 pvec->page[pvec->nr].idx = idx;
1122 pvec->nr++;
1123 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1124}
1125
1126static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1127 struct kvm_mmu_pages *pvec)
1128{
1129 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001130
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001131 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001132 u64 ent = sp->spt[i];
1133
Marcelo Tosatti87917232008-12-22 18:49:30 -02001134 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001135 struct kvm_mmu_page *child;
1136 child = page_header(ent & PT64_BASE_ADDR_MASK);
1137
1138 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001139 if (mmu_pages_add(pvec, child, i))
1140 return -ENOSPC;
1141
1142 ret = __mmu_unsync_walk(child, pvec);
1143 if (!ret)
1144 __clear_bit(i, sp->unsync_child_bitmap);
1145 else if (ret > 0)
1146 nr_unsync_leaf += ret;
1147 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001148 return ret;
1149 }
1150
1151 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001152 nr_unsync_leaf++;
1153 if (mmu_pages_add(pvec, child, i))
1154 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001155 }
1156 }
1157 }
1158
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001159 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001160 sp->unsync_children = 0;
1161
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001162 return nr_unsync_leaf;
1163}
1164
1165static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1166 struct kvm_mmu_pages *pvec)
1167{
1168 if (!sp->unsync_children)
1169 return 0;
1170
1171 mmu_pages_add(pvec, sp, 0);
1172 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001173}
1174
Avi Kivity4db35312007-11-21 15:28:32 +02001175static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001176{
1177 unsigned index;
1178 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001179 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001180 struct hlist_node *node;
1181
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001182 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001183 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001184 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001185 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001186 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001187 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001188 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001189 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001190 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001191 }
1192 return NULL;
1193}
1194
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001195static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1196{
1197 WARN_ON(!sp->unsync);
1198 sp->unsync = 0;
1199 --kvm->stat.mmu_unsync;
1200}
1201
1202static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1203
1204static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1205{
1206 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1207 kvm_mmu_zap_page(vcpu->kvm, sp);
1208 return 1;
1209 }
1210
Avi Kivityf691fe12009-07-06 15:58:14 +03001211 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001212 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1213 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001214 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1216 kvm_mmu_zap_page(vcpu->kvm, sp);
1217 return 1;
1218 }
1219
1220 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221 return 0;
1222}
1223
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001224struct mmu_page_path {
1225 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1226 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001227};
1228
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001229#define for_each_sp(pvec, sp, parents, i) \
1230 for (i = mmu_pages_next(&pvec, &parents, -1), \
1231 sp = pvec.page[i].sp; \
1232 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1233 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001234
Hannes Edercded19f2009-02-21 02:19:13 +01001235static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1236 struct mmu_page_path *parents,
1237 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001238{
1239 int n;
1240
1241 for (n = i+1; n < pvec->nr; n++) {
1242 struct kvm_mmu_page *sp = pvec->page[n].sp;
1243
1244 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1245 parents->idx[0] = pvec->page[n].idx;
1246 return n;
1247 }
1248
1249 parents->parent[sp->role.level-2] = sp;
1250 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1251 }
1252
1253 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001254}
1255
Hannes Edercded19f2009-02-21 02:19:13 +01001256static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001257{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001258 struct kvm_mmu_page *sp;
1259 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001260
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001261 do {
1262 unsigned int idx = parents->idx[level];
1263
1264 sp = parents->parent[level];
1265 if (!sp)
1266 return;
1267
1268 --sp->unsync_children;
1269 WARN_ON((int)sp->unsync_children < 0);
1270 __clear_bit(idx, sp->unsync_child_bitmap);
1271 level++;
1272 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1273}
1274
1275static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1276 struct mmu_page_path *parents,
1277 struct kvm_mmu_pages *pvec)
1278{
1279 parents->parent[parent->role.level-1] = NULL;
1280 pvec->nr = 0;
1281}
1282
1283static void mmu_sync_children(struct kvm_vcpu *vcpu,
1284 struct kvm_mmu_page *parent)
1285{
1286 int i;
1287 struct kvm_mmu_page *sp;
1288 struct mmu_page_path parents;
1289 struct kvm_mmu_pages pages;
1290
1291 kvm_mmu_pages_init(parent, &parents, &pages);
1292 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001293 int protected = 0;
1294
1295 for_each_sp(pages, sp, parents, i)
1296 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1297
1298 if (protected)
1299 kvm_flush_remote_tlbs(vcpu->kvm);
1300
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001301 for_each_sp(pages, sp, parents, i) {
1302 kvm_sync_page(vcpu, sp);
1303 mmu_pages_clear_parents(&parents);
1304 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001305 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001306 kvm_mmu_pages_init(parent, &parents, &pages);
1307 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001308}
1309
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001310static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1311 gfn_t gfn,
1312 gva_t gaddr,
1313 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001314 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001315 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001316 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001317{
1318 union kvm_mmu_page_role role;
1319 unsigned index;
1320 unsigned quadrant;
1321 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001322 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001323 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001324
Avi Kivitya770f6f2008-12-21 19:20:09 +02001325 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001326 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001327 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001328 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001329 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001330 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1331 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1332 role.quadrant = quadrant;
1333 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001334 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001335 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001336 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1337 if (sp->gfn == gfn) {
1338 if (sp->unsync)
1339 if (kvm_sync_page(vcpu, sp))
1340 continue;
1341
1342 if (sp->role.word != role.word)
1343 continue;
1344
Avi Kivity4db35312007-11-21 15:28:32 +02001345 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001346 if (sp->unsync_children) {
1347 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1348 kvm_mmu_mark_parents_unsync(vcpu, sp);
1349 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001350 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001351 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001352 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001353 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001354 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1355 if (!sp)
1356 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001357 sp->gfn = gfn;
1358 sp->role = role;
1359 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001360 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001361 if (rmap_write_protect(vcpu->kvm, gfn))
1362 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001363 account_shadowed(vcpu->kvm, gfn);
1364 }
Avi Kivity131d8272008-05-29 14:56:28 +03001365 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1366 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1367 else
1368 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001369 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001370 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001371}
1372
Avi Kivity2d111232008-12-25 14:39:47 +02001373static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1374 struct kvm_vcpu *vcpu, u64 addr)
1375{
1376 iterator->addr = addr;
1377 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1378 iterator->level = vcpu->arch.mmu.shadow_root_level;
1379 if (iterator->level == PT32E_ROOT_LEVEL) {
1380 iterator->shadow_addr
1381 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1382 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1383 --iterator->level;
1384 if (!iterator->shadow_addr)
1385 iterator->level = 0;
1386 }
1387}
1388
1389static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1390{
1391 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1392 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001393
1394 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1395 if (is_large_pte(*iterator->sptep))
1396 return false;
1397
Avi Kivity2d111232008-12-25 14:39:47 +02001398 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1399 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1400 return true;
1401}
1402
1403static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1404{
1405 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1406 --iterator->level;
1407}
1408
Avi Kivity90cb0522007-07-17 13:04:56 +03001409static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001410 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001411{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001412 unsigned i;
1413 u64 *pt;
1414 u64 ent;
1415
Avi Kivity4db35312007-11-21 15:28:32 +02001416 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001417
Avi Kivity697fe2e2007-01-05 16:36:46 -08001418 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1419 ent = pt[i];
1420
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001421 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001422 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001423 ent &= PT64_BASE_ADDR_MASK;
1424 mmu_page_remove_parent_pte(page_header(ent),
1425 &pt[i]);
1426 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001427 if (is_large_pte(ent))
1428 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001429 rmap_remove(kvm, &pt[i]);
1430 }
1431 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001432 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001433 }
Avi Kivitya4360362007-01-05 16:36:45 -08001434}
1435
Avi Kivity4db35312007-11-21 15:28:32 +02001436static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001437{
Avi Kivity4db35312007-11-21 15:28:32 +02001438 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001439}
1440
Avi Kivity12b7d282007-09-23 14:10:49 +02001441static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1442{
1443 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001444 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001445
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001446 kvm_for_each_vcpu(i, vcpu, kvm)
1447 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001448}
1449
Avi Kivity31aa2b42008-07-11 17:59:46 +03001450static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001451{
1452 u64 *parent_pte;
1453
Avi Kivity4db35312007-11-21 15:28:32 +02001454 while (sp->multimapped || sp->parent_pte) {
1455 if (!sp->multimapped)
1456 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001457 else {
1458 struct kvm_pte_chain *chain;
1459
Avi Kivity4db35312007-11-21 15:28:32 +02001460 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001461 struct kvm_pte_chain, link);
1462 parent_pte = chain->parent_ptes[0];
1463 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001464 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001465 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001466 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001467 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001468}
1469
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001470static int mmu_zap_unsync_children(struct kvm *kvm,
1471 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001472{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001473 int i, zapped = 0;
1474 struct mmu_page_path parents;
1475 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001476
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001477 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001478 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001479
1480 kvm_mmu_pages_init(parent, &parents, &pages);
1481 while (mmu_unsync_walk(parent, &pages)) {
1482 struct kvm_mmu_page *sp;
1483
1484 for_each_sp(pages, sp, parents, i) {
1485 kvm_mmu_zap_page(kvm, sp);
1486 mmu_pages_clear_parents(&parents);
1487 }
1488 zapped += pages.nr;
1489 kvm_mmu_pages_init(parent, &parents, &pages);
1490 }
1491
1492 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001493}
1494
Marcelo Tosatti07385412008-09-23 13:18:37 -03001495static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001496{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001497 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001498
1499 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001500 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001501 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001502 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001503 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001504 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001505 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001506 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001507 if (sp->unsync)
1508 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001509 if (!sp->root_count) {
1510 hlist_del(&sp->hash_link);
1511 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001512 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001513 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001514 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001515 kvm_reload_remote_mmus(kvm);
1516 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001517 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001518 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001519}
1520
Izik Eidus82ce2c92007-10-02 18:52:55 +02001521/*
1522 * Changing the number of mmu pages allocated to the vm
1523 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1524 */
1525void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1526{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001527 int used_pages;
1528
1529 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1530 used_pages = max(0, used_pages);
1531
Izik Eidus82ce2c92007-10-02 18:52:55 +02001532 /*
1533 * If we set the number of mmu pages to be smaller be than the
1534 * number of actived pages , we must to free some mmu pages before we
1535 * change the value
1536 */
1537
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001538 if (used_pages > kvm_nr_mmu_pages) {
1539 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001540 struct kvm_mmu_page *page;
1541
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001542 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001543 struct kvm_mmu_page, link);
1544 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001545 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001546 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001547 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001548 }
1549 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001550 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1551 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001552
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001553 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001554}
1555
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001556static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001557{
1558 unsigned index;
1559 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001560 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001561 struct hlist_node *node, *n;
1562 int r;
1563
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001564 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001565 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001566 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001567 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001568 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001569 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001570 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001571 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001572 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001573 if (kvm_mmu_zap_page(kvm, sp))
1574 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001575 }
1576 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001577}
1578
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001579static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001580{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001581 unsigned index;
1582 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001583 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001584 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001585
Avi Kivity4677a3b2009-01-06 13:00:27 +02001586 index = kvm_page_table_hashfn(gfn);
1587 bucket = &kvm->arch.mmu_page_hash[index];
1588 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001589 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001590 && !sp->role.invalid) {
1591 pgprintk("%s: zap %lx %x\n",
1592 __func__, gfn, sp->role.word);
1593 kvm_mmu_zap_page(kvm, sp);
1594 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001595 }
1596}
1597
Avi Kivity38c335f2007-11-21 14:20:22 +02001598static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001600 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001601 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602
Sheng Yang291f26b2008-10-16 17:30:57 +08001603 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604}
1605
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001606static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1607{
1608 int i;
1609 u64 *pt = sp->spt;
1610
1611 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1612 return;
1613
1614 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1615 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001616 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001617 }
1618}
1619
Avi Kivity039576c2007-03-20 12:46:50 +02001620struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1621{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001622 struct page *page;
1623
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001624 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001625
1626 if (gpa == UNMAPPED_GVA)
1627 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001628
Izik Eidus72dc67a2008-02-10 18:04:15 +02001629 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001630
1631 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001632}
1633
Sheng Yang74be52e2008-10-09 16:01:56 +08001634/*
1635 * The function is based on mtrr_type_lookup() in
1636 * arch/x86/kernel/cpu/mtrr/generic.c
1637 */
1638static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1639 u64 start, u64 end)
1640{
1641 int i;
1642 u64 base, mask;
1643 u8 prev_match, curr_match;
1644 int num_var_ranges = KVM_NR_VAR_MTRR;
1645
1646 if (!mtrr_state->enabled)
1647 return 0xFF;
1648
1649 /* Make end inclusive end, instead of exclusive */
1650 end--;
1651
1652 /* Look in fixed ranges. Just return the type as per start */
1653 if (mtrr_state->have_fixed && (start < 0x100000)) {
1654 int idx;
1655
1656 if (start < 0x80000) {
1657 idx = 0;
1658 idx += (start >> 16);
1659 return mtrr_state->fixed_ranges[idx];
1660 } else if (start < 0xC0000) {
1661 idx = 1 * 8;
1662 idx += ((start - 0x80000) >> 14);
1663 return mtrr_state->fixed_ranges[idx];
1664 } else if (start < 0x1000000) {
1665 idx = 3 * 8;
1666 idx += ((start - 0xC0000) >> 12);
1667 return mtrr_state->fixed_ranges[idx];
1668 }
1669 }
1670
1671 /*
1672 * Look in variable ranges
1673 * Look of multiple ranges matching this address and pick type
1674 * as per MTRR precedence
1675 */
1676 if (!(mtrr_state->enabled & 2))
1677 return mtrr_state->def_type;
1678
1679 prev_match = 0xFF;
1680 for (i = 0; i < num_var_ranges; ++i) {
1681 unsigned short start_state, end_state;
1682
1683 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1684 continue;
1685
1686 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1687 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1688 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1689 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1690
1691 start_state = ((start & mask) == (base & mask));
1692 end_state = ((end & mask) == (base & mask));
1693 if (start_state != end_state)
1694 return 0xFE;
1695
1696 if ((start & mask) != (base & mask))
1697 continue;
1698
1699 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1700 if (prev_match == 0xFF) {
1701 prev_match = curr_match;
1702 continue;
1703 }
1704
1705 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1706 curr_match == MTRR_TYPE_UNCACHABLE)
1707 return MTRR_TYPE_UNCACHABLE;
1708
1709 if ((prev_match == MTRR_TYPE_WRBACK &&
1710 curr_match == MTRR_TYPE_WRTHROUGH) ||
1711 (prev_match == MTRR_TYPE_WRTHROUGH &&
1712 curr_match == MTRR_TYPE_WRBACK)) {
1713 prev_match = MTRR_TYPE_WRTHROUGH;
1714 curr_match = MTRR_TYPE_WRTHROUGH;
1715 }
1716
1717 if (prev_match != curr_match)
1718 return MTRR_TYPE_UNCACHABLE;
1719 }
1720
1721 if (prev_match != 0xFF)
1722 return prev_match;
1723
1724 return mtrr_state->def_type;
1725}
1726
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001727u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001728{
1729 u8 mtrr;
1730
1731 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1732 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1733 if (mtrr == 0xfe || mtrr == 0xff)
1734 mtrr = MTRR_TYPE_WRBACK;
1735 return mtrr;
1736}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001737EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001738
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001739static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1740{
1741 unsigned index;
1742 struct hlist_head *bucket;
1743 struct kvm_mmu_page *s;
1744 struct hlist_node *node, *n;
1745
Avi Kivityf691fe12009-07-06 15:58:14 +03001746 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001747 index = kvm_page_table_hashfn(sp->gfn);
1748 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1749 /* don't unsync if pagetable is shadowed with multiple roles */
1750 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001751 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001752 continue;
1753 if (s->role.word != sp->role.word)
1754 return 1;
1755 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001756 ++vcpu->kvm->stat.mmu_unsync;
1757 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001758
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001759 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001760
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001761 mmu_convert_notrap(sp);
1762 return 0;
1763}
1764
1765static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1766 bool can_unsync)
1767{
1768 struct kvm_mmu_page *shadow;
1769
1770 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1771 if (shadow) {
1772 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1773 return 1;
1774 if (shadow->unsync)
1775 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001776 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001777 return kvm_unsync_page(vcpu, shadow);
1778 return 1;
1779 }
1780 return 0;
1781}
1782
Avi Kivityd555c332009-06-10 14:24:23 +03001783static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001784 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001785 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001786 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001787 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001788{
1789 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001790 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001791
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001792 /*
1793 * We don't set the accessed bit, since we sometimes want to see
1794 * whether the guest actually used the pte (in order to detect
1795 * demand paging).
1796 */
Sheng Yang7b523452008-04-25 21:13:50 +08001797 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001798 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001799 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001800 if (!dirty)
1801 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001802 if (pte_access & ACC_EXEC_MASK)
1803 spte |= shadow_x_mask;
1804 else
1805 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001806 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001807 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001808 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001809 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001810 if (tdp_enabled)
1811 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1812 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001813
Izik Eidus14032832009-09-23 21:47:17 +03001814 if (reset_host_protection)
1815 spte |= SPTE_HOST_WRITEABLE;
1816
Anthony Liguori35149e22008-04-02 14:46:56 -05001817 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001818
1819 if ((pte_access & ACC_WRITE_MASK)
1820 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001821
Joerg Roedel852e3c12009-07-27 16:30:44 +02001822 if (level > PT_PAGE_TABLE_LEVEL &&
1823 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001824 ret = 1;
1825 spte = shadow_trap_nonpresent_pte;
1826 goto set_pte;
1827 }
1828
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001829 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001830
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001831 /*
1832 * Optimization: for pte sync, if spte was writable the hash
1833 * lookup is unnecessary (and expensive). Write protection
1834 * is responsibility of mmu_get_page / kvm_sync_page.
1835 * Same reasoning can be applied to dirty page accounting.
1836 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001837 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001838 goto set_pte;
1839
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001840 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001841 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001842 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001843 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001844 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001845 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001846 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001847 }
1848 }
1849
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001850 if (pte_access & ACC_WRITE_MASK)
1851 mark_page_dirty(vcpu->kvm, gfn);
1852
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001853set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001854 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001855 return ret;
1856}
1857
Avi Kivityd555c332009-06-10 14:24:23 +03001858static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001859 unsigned pt_access, unsigned pte_access,
1860 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001861 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001862 pfn_t pfn, bool speculative,
1863 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001864{
1865 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001866 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001867 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001868
1869 pgprintk("%s: spte %llx access %x write_fault %d"
1870 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001871 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001872 write_fault, user_fault, gfn);
1873
Avi Kivityd555c332009-06-10 14:24:23 +03001874 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001875 /*
1876 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1877 * the parent of the now unreachable PTE.
1878 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001879 if (level > PT_PAGE_TABLE_LEVEL &&
1880 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001881 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001882 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001883
1884 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001885 mmu_page_remove_parent_pte(child, sptep);
1886 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001887 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001888 spte_to_pfn(*sptep), pfn);
1889 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001890 } else
1891 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001892 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001893
Avi Kivityd555c332009-06-10 14:24:23 +03001894 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001895 dirty, level, gfn, pfn, speculative, true,
1896 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001897 if (write_fault)
1898 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001899 kvm_x86_ops->tlb_flush(vcpu);
1900 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001901
Avi Kivityd555c332009-06-10 14:24:23 +03001902 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001903 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001904 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001905 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1906 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001907 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001908 ++vcpu->kvm->stat.lpages;
1909
Avi Kivityd555c332009-06-10 14:24:23 +03001910 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001911 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001912 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001913 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001914 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001915 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001916 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001917 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001918 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001919 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001920 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001921 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001922 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001923 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001924 vcpu->arch.last_pte_gfn = gfn;
1925 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001926}
1927
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1929{
1930}
1931
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001932static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001933 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934{
Avi Kivity9f652d22008-12-25 14:54:25 +02001935 struct kvm_shadow_walk_iterator iterator;
1936 struct kvm_mmu_page *sp;
1937 int pt_write = 0;
1938 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939
Avi Kivity9f652d22008-12-25 14:54:25 +02001940 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001941 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001942 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1943 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001944 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001945 ++vcpu->stat.pf_fixed;
1946 break;
1947 }
1948
1949 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1950 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1951 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1952 iterator.level - 1,
1953 1, ACC_ALL, iterator.sptep);
1954 if (!sp) {
1955 pgprintk("nonpaging_map: ENOMEM\n");
1956 kvm_release_pfn_clean(pfn);
1957 return -ENOMEM;
1958 }
1959
Avi Kivityd555c332009-06-10 14:24:23 +03001960 __set_spte(iterator.sptep,
1961 __pa(sp->spt)
1962 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1963 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001964 }
1965 }
1966 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967}
1968
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001969static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1970{
1971 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001972 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001973 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001974 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001975
Joerg Roedel852e3c12009-07-27 16:30:44 +02001976 level = mapping_level(vcpu, gfn);
1977
1978 /*
1979 * This path builds a PAE pagetable - so we can map 2mb pages at
1980 * maximum. Therefore check if the level is larger than that.
1981 */
1982 if (level > PT_DIRECTORY_LEVEL)
1983 level = PT_DIRECTORY_LEVEL;
1984
1985 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001986
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001987 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001988 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001989 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001990
Avi Kivityd196e342008-01-24 11:44:11 +02001991 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001992 if (is_error_pfn(pfn)) {
1993 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001994 return 1;
1995 }
1996
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001997 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001998 if (mmu_notifier_retry(vcpu, mmu_seq))
1999 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002000 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002001 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002002 spin_unlock(&vcpu->kvm->mmu_lock);
2003
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002004
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002005 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002006
2007out_unlock:
2008 spin_unlock(&vcpu->kvm->mmu_lock);
2009 kvm_release_pfn_clean(pfn);
2010 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002011}
2012
2013
Avi Kivity17ac10a2007-01-05 16:36:40 -08002014static void mmu_free_roots(struct kvm_vcpu *vcpu)
2015{
2016 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002017 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002018
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002019 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002020 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002021 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002022 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2023 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002024
Avi Kivity4db35312007-11-21 15:28:32 +02002025 sp = page_header(root);
2026 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002027 if (!sp->root_count && sp->role.invalid)
2028 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002029 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002030 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002031 return;
2032 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002033 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002034 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002035
Avi Kivity417726a2007-04-12 17:35:58 +03002036 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002037 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002038 sp = page_header(root);
2039 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002040 if (!sp->root_count && sp->role.invalid)
2041 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002042 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002043 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002044 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002045 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002046 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002047}
2048
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002049static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2050{
2051 int ret = 0;
2052
2053 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2054 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2055 ret = 1;
2056 }
2057
2058 return ret;
2059}
2060
2061static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002062{
2063 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002064 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002065 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002066 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002067 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002068
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002069 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002070
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002071 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2072 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002073
2074 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002075 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002076 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002077 if (mmu_check_root(vcpu, root_gfn))
2078 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002079 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002080 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002081 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002082 root = __pa(sp->spt);
2083 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002084 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002085 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002086 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002087 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002088 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002089 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002090 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002091 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002092
2093 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002094 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002095 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002096 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002097 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002098 continue;
2099 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002100 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002101 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002102 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002103 if (mmu_check_root(vcpu, root_gfn))
2104 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002105 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002106 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002107 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002108 root = __pa(sp->spt);
2109 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002111 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002112 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002113 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002114}
2115
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002116static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2117{
2118 int i;
2119 struct kvm_mmu_page *sp;
2120
2121 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2122 return;
2123 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2124 hpa_t root = vcpu->arch.mmu.root_hpa;
2125 sp = page_header(root);
2126 mmu_sync_children(vcpu, sp);
2127 return;
2128 }
2129 for (i = 0; i < 4; ++i) {
2130 hpa_t root = vcpu->arch.mmu.pae_root[i];
2131
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002132 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002133 root &= PT64_BASE_ADDR_MASK;
2134 sp = page_header(root);
2135 mmu_sync_children(vcpu, sp);
2136 }
2137 }
2138}
2139
2140void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2141{
2142 spin_lock(&vcpu->kvm->mmu_lock);
2143 mmu_sync_roots(vcpu);
2144 spin_unlock(&vcpu->kvm->mmu_lock);
2145}
2146
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2148{
2149 return vaddr;
2150}
2151
2152static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002153 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154{
Avi Kivitye8332402007-12-09 18:43:00 +02002155 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002156 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002158 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002159 r = mmu_topup_memory_caches(vcpu);
2160 if (r)
2161 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002164 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165
Avi Kivitye8332402007-12-09 18:43:00 +02002166 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167
Avi Kivitye8332402007-12-09 18:43:00 +02002168 return nonpaging_map(vcpu, gva & PAGE_MASK,
2169 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170}
2171
Joerg Roedelfb72d162008-02-07 13:47:44 +01002172static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2173 u32 error_code)
2174{
Anthony Liguori35149e22008-04-02 14:46:56 -05002175 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002176 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002177 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002178 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002179 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002180
2181 ASSERT(vcpu);
2182 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2183
2184 r = mmu_topup_memory_caches(vcpu);
2185 if (r)
2186 return r;
2187
Joerg Roedel852e3c12009-07-27 16:30:44 +02002188 level = mapping_level(vcpu, gfn);
2189
2190 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2191
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002192 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002193 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002194 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002195 if (is_error_pfn(pfn)) {
2196 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002197 return 1;
2198 }
2199 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002200 if (mmu_notifier_retry(vcpu, mmu_seq))
2201 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002202 kvm_mmu_free_some_pages(vcpu);
2203 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002204 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002205 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002206
2207 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002208
2209out_unlock:
2210 spin_unlock(&vcpu->kvm->mmu_lock);
2211 kvm_release_pfn_clean(pfn);
2212 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002213}
2214
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215static void nonpaging_free(struct kvm_vcpu *vcpu)
2216{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002217 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218}
2219
2220static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2221{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002222 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223
2224 context->new_cr3 = nonpaging_new_cr3;
2225 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 context->gva_to_gpa = nonpaging_gva_to_gpa;
2227 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002228 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002229 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002230 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002231 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002233 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234 return 0;
2235}
2236
Avi Kivityd835dfe2007-11-21 02:57:59 +02002237void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002239 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002240 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241}
2242
2243static void paging_new_cr3(struct kvm_vcpu *vcpu)
2244{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002245 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002246 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247}
2248
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249static void inject_page_fault(struct kvm_vcpu *vcpu,
2250 u64 addr,
2251 u32 err_code)
2252{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002253 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254}
2255
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256static void paging_free(struct kvm_vcpu *vcpu)
2257{
2258 nonpaging_free(vcpu);
2259}
2260
Dong, Eddie82725b22009-03-30 16:21:08 +08002261static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2262{
2263 int bit7;
2264
2265 bit7 = (gpte >> 7) & 1;
2266 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2267}
2268
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269#define PTTYPE 64
2270#include "paging_tmpl.h"
2271#undef PTTYPE
2272
2273#define PTTYPE 32
2274#include "paging_tmpl.h"
2275#undef PTTYPE
2276
Dong, Eddie82725b22009-03-30 16:21:08 +08002277static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2278{
2279 struct kvm_mmu *context = &vcpu->arch.mmu;
2280 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2281 u64 exb_bit_rsvd = 0;
2282
2283 if (!is_nx(vcpu))
2284 exb_bit_rsvd = rsvd_bits(63, 63);
2285 switch (level) {
2286 case PT32_ROOT_LEVEL:
2287 /* no rsvd bits for 2 level 4K page table entries */
2288 context->rsvd_bits_mask[0][1] = 0;
2289 context->rsvd_bits_mask[0][0] = 0;
2290 if (is_cpuid_PSE36())
2291 /* 36bits PSE 4MB page */
2292 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2293 else
2294 /* 32 bits PSE 4MB page */
2295 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002296 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002297 break;
2298 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002299 context->rsvd_bits_mask[0][2] =
2300 rsvd_bits(maxphyaddr, 63) |
2301 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002302 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002303 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002304 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2305 rsvd_bits(maxphyaddr, 62); /* PTE */
2306 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2307 rsvd_bits(maxphyaddr, 62) |
2308 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002309 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002310 break;
2311 case PT64_ROOT_LEVEL:
2312 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2313 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2314 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2315 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2316 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002317 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002318 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2319 rsvd_bits(maxphyaddr, 51);
2320 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002321 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2322 rsvd_bits(maxphyaddr, 51) |
2323 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002324 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002325 rsvd_bits(maxphyaddr, 51) |
2326 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002327 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002328 break;
2329 }
2330}
2331
Avi Kivity17ac10a2007-01-05 16:36:40 -08002332static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002334 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335
2336 ASSERT(is_pae(vcpu));
2337 context->new_cr3 = paging_new_cr3;
2338 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002340 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002341 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002342 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002344 context->root_level = level;
2345 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002346 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 return 0;
2348}
2349
Avi Kivity17ac10a2007-01-05 16:36:40 -08002350static int paging64_init_context(struct kvm_vcpu *vcpu)
2351{
Dong, Eddie82725b22009-03-30 16:21:08 +08002352 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002353 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2354}
2355
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356static int paging32_init_context(struct kvm_vcpu *vcpu)
2357{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002358 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
Dong, Eddie82725b22009-03-30 16:21:08 +08002360 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 context->new_cr3 = paging_new_cr3;
2362 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 context->gva_to_gpa = paging32_gva_to_gpa;
2364 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002365 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002366 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002367 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 context->root_level = PT32_ROOT_LEVEL;
2369 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002370 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 return 0;
2372}
2373
2374static int paging32E_init_context(struct kvm_vcpu *vcpu)
2375{
Dong, Eddie82725b22009-03-30 16:21:08 +08002376 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002377 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Joerg Roedelfb72d162008-02-07 13:47:44 +01002380static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2381{
2382 struct kvm_mmu *context = &vcpu->arch.mmu;
2383
2384 context->new_cr3 = nonpaging_new_cr3;
2385 context->page_fault = tdp_page_fault;
2386 context->free = nonpaging_free;
2387 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002388 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002389 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002390 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002391 context->root_hpa = INVALID_PAGE;
2392
2393 if (!is_paging(vcpu)) {
2394 context->gva_to_gpa = nonpaging_gva_to_gpa;
2395 context->root_level = 0;
2396 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002397 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002398 context->gva_to_gpa = paging64_gva_to_gpa;
2399 context->root_level = PT64_ROOT_LEVEL;
2400 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002401 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002402 context->gva_to_gpa = paging64_gva_to_gpa;
2403 context->root_level = PT32E_ROOT_LEVEL;
2404 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002405 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002406 context->gva_to_gpa = paging32_gva_to_gpa;
2407 context->root_level = PT32_ROOT_LEVEL;
2408 }
2409
2410 return 0;
2411}
2412
2413static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002415 int r;
2416
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002418 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419
2420 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002421 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002422 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002423 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002425 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002427 r = paging32_init_context(vcpu);
2428
2429 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2430
2431 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432}
2433
Joerg Roedelfb72d162008-02-07 13:47:44 +01002434static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2435{
Anthony Liguori35149e22008-04-02 14:46:56 -05002436 vcpu->arch.update_pte.pfn = bad_pfn;
2437
Joerg Roedelfb72d162008-02-07 13:47:44 +01002438 if (tdp_enabled)
2439 return init_kvm_tdp_mmu(vcpu);
2440 else
2441 return init_kvm_softmmu(vcpu);
2442}
2443
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2445{
2446 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002447 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2448 vcpu->arch.mmu.free(vcpu);
2449 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450 }
2451}
2452
2453int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2454{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002455 destroy_kvm_mmu(vcpu);
2456 return init_kvm_mmu(vcpu);
2457}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002458EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002459
2460int kvm_mmu_load(struct kvm_vcpu *vcpu)
2461{
Avi Kivity714b93d2007-01-05 16:36:53 -08002462 int r;
2463
Avi Kivitye2dec932007-01-05 16:36:54 -08002464 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002465 if (r)
2466 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002467 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002468 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002469 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002470 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002471 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002472 if (r)
2473 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002474 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002475 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002476out:
2477 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002479EXPORT_SYMBOL_GPL(kvm_mmu_load);
2480
2481void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2482{
2483 mmu_free_roots(vcpu);
2484}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485
Avi Kivity09072da2007-05-01 14:16:52 +03002486static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002487 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002488 u64 *spte)
2489{
2490 u64 pte;
2491 struct kvm_mmu_page *child;
2492
2493 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002494 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002495 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002496 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002497 else {
2498 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002499 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002500 }
2501 }
Avi Kivityd555c332009-06-10 14:24:23 +03002502 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002503 if (is_large_pte(pte))
2504 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002505}
2506
Avi Kivity00284252007-05-01 16:53:31 +03002507static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002508 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002509 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002510 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002511{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002512 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002513 ++vcpu->kvm->stat.mmu_pde_zapped;
2514 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002515 }
Avi Kivity00284252007-05-01 16:53:31 +03002516
Avi Kivity4cee5762007-11-18 16:37:07 +02002517 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002518 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002519 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002520 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002521 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002522}
2523
Avi Kivity79539ce2007-11-21 02:06:21 +02002524static bool need_remote_flush(u64 old, u64 new)
2525{
2526 if (!is_shadow_present_pte(old))
2527 return false;
2528 if (!is_shadow_present_pte(new))
2529 return true;
2530 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2531 return true;
2532 old ^= PT64_NX_MASK;
2533 new ^= PT64_NX_MASK;
2534 return (old & ~new & PT64_PERM_MASK) != 0;
2535}
2536
2537static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2538{
2539 if (need_remote_flush(old, new))
2540 kvm_flush_remote_tlbs(vcpu->kvm);
2541 else
2542 kvm_mmu_flush_tlb(vcpu);
2543}
2544
Avi Kivity12b7d282007-09-23 14:10:49 +02002545static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2546{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002547 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002548
Sheng Yang7b523452008-04-25 21:13:50 +08002549 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002550}
2551
Avi Kivityd7824ff2007-12-30 12:29:05 +02002552static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2553 const u8 *new, int bytes)
2554{
2555 gfn_t gfn;
2556 int r;
2557 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002558 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002559
2560 if (bytes != 4 && bytes != 8)
2561 return;
2562
2563 /*
2564 * Assume that the pte write on a page table of the same type
2565 * as the current vcpu paging mode. This is nearly always true
2566 * (might be false while changing modes). Note it is verified later
2567 * by update_pte().
2568 */
2569 if (is_pae(vcpu)) {
2570 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2571 if ((bytes == 4) && (gpa % 4 == 0)) {
2572 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2573 if (r)
2574 return;
2575 memcpy((void *)&gpte + (gpa % 8), new, 4);
2576 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2577 memcpy((void *)&gpte, new, 8);
2578 }
2579 } else {
2580 if ((bytes == 4) && (gpa % 4 == 0))
2581 memcpy((void *)&gpte, new, 4);
2582 }
Avi Kivity43a37952009-06-10 14:12:05 +03002583 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002584 return;
2585 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002586
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002587 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002588 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002589 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002590
Anthony Liguori35149e22008-04-02 14:46:56 -05002591 if (is_error_pfn(pfn)) {
2592 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002593 return;
2594 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002595 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002596 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002597}
2598
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002599static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2600{
2601 u64 *spte = vcpu->arch.last_pte_updated;
2602
2603 if (spte
2604 && vcpu->arch.last_pte_gfn == gfn
2605 && shadow_accessed_mask
2606 && !(*spte & shadow_accessed_mask)
2607 && is_shadow_present_pte(*spte))
2608 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2609}
2610
Avi Kivity09072da2007-05-01 14:16:52 +03002611void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002612 const u8 *new, int bytes,
2613 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002614{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002615 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002616 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002617 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002618 struct hlist_head *bucket;
2619 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002620 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002621 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002622 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002623 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002624 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002625 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002626 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002627 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002628 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002629 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002630 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002631
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002632 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002633 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002634 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002635 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002636 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002637 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002638 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002639 if (guest_initiated) {
2640 if (gfn == vcpu->arch.last_pt_write_gfn
2641 && !last_updated_pte_accessed(vcpu)) {
2642 ++vcpu->arch.last_pt_write_count;
2643 if (vcpu->arch.last_pt_write_count >= 3)
2644 flooded = 1;
2645 } else {
2646 vcpu->arch.last_pt_write_gfn = gfn;
2647 vcpu->arch.last_pt_write_count = 1;
2648 vcpu->arch.last_pte_updated = NULL;
2649 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002650 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002651 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002652 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002653 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002654 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002655 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002656 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002657 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002658 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002659 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002660 /*
2661 * Misaligned accesses are too much trouble to fix
2662 * up; also, they usually indicate a page is not used
2663 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002664 *
2665 * If we're seeing too many writes to a page,
2666 * it may no longer be a page table, or we may be
2667 * forking, in which case it is better to unmap the
2668 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002669 */
2670 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002671 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002672 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2673 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002674 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002675 continue;
2676 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002677 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002678 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002679 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002680 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002681 page_offset <<= 1; /* 32->64 */
2682 /*
2683 * A 32-bit pde maps 4MB while the shadow pdes map
2684 * only 2MB. So we need to double the offset again
2685 * and zap two pdes instead of one.
2686 */
2687 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002688 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002689 page_offset <<= 1;
2690 npte = 2;
2691 }
Avi Kivityfce06572007-05-01 16:44:05 +03002692 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002693 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002694 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002695 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002696 }
Avi Kivity4db35312007-11-21 15:28:32 +02002697 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002698 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2699 gentry = 0;
2700 r = kvm_read_guest_atomic(vcpu->kvm,
2701 gpa & ~(u64)(pte_size - 1),
2702 &gentry, pte_size);
2703 new = (const void *)&gentry;
2704 if (r < 0)
2705 new = NULL;
2706 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002707 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002708 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002709 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002710 if (new)
2711 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002712 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002713 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002714 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002715 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002716 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002717 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002718 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2719 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2720 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002721 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002722}
2723
Avi Kivitya4360362007-01-05 16:36:45 -08002724int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2725{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002726 gpa_t gpa;
2727 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002728
Avi Kivity60f24782009-08-27 13:37:06 +03002729 if (tdp_enabled)
2730 return 0;
2731
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002732 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002733
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002734 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002735 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002736 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002737 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002738}
Avi Kivity577bdc42008-07-19 08:57:05 +03002739EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002740
Avi Kivity22d95b12007-09-14 20:26:06 +03002741void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002742{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002743 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2744 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002745 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002746
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002747 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002748 struct kvm_mmu_page, link);
2749 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002750 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002751 }
2752}
Avi Kivityebeace82007-01-05 16:36:47 -08002753
Avi Kivity30677142007-10-28 18:48:59 +02002754int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2755{
2756 int r;
2757 enum emulation_result er;
2758
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002759 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002760 if (r < 0)
2761 goto out;
2762
2763 if (!r) {
2764 r = 1;
2765 goto out;
2766 }
2767
Avi Kivityb733bfb2007-10-28 18:52:05 +02002768 r = mmu_topup_memory_caches(vcpu);
2769 if (r)
2770 goto out;
2771
Avi Kivity851ba692009-08-24 11:10:17 +03002772 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002773
2774 switch (er) {
2775 case EMULATE_DONE:
2776 return 1;
2777 case EMULATE_DO_MMIO:
2778 ++vcpu->stat.mmio_exits;
2779 return 0;
2780 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002781 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2782 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002783 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002784 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002785 default:
2786 BUG();
2787 }
2788out:
Avi Kivity30677142007-10-28 18:48:59 +02002789 return r;
2790}
2791EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2792
Marcelo Tosattia7052892008-09-23 13:18:35 -03002793void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2794{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002795 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002796 kvm_mmu_flush_tlb(vcpu);
2797 ++vcpu->stat.invlpg;
2798}
2799EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2800
Joerg Roedel18552672008-02-07 13:47:41 +01002801void kvm_enable_tdp(void)
2802{
2803 tdp_enabled = true;
2804}
2805EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2806
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002807void kvm_disable_tdp(void)
2808{
2809 tdp_enabled = false;
2810}
2811EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2812
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813static void free_mmu_pages(struct kvm_vcpu *vcpu)
2814{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002815 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816}
2817
2818static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2819{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002820 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 int i;
2822
2823 ASSERT(vcpu);
2824
Avi Kivity17ac10a2007-01-05 16:36:40 -08002825 /*
2826 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2827 * Therefore we need to allocate shadow page tables in the first
2828 * 4GB of memory, which happens to fit the DMA32 zone.
2829 */
2830 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2831 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002832 return -ENOMEM;
2833
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002834 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002835 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002836 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002837
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839}
2840
Ingo Molnar8018c272006-12-29 16:50:01 -08002841int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002844 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845
Ingo Molnar8018c272006-12-29 16:50:01 -08002846 return alloc_mmu_pages(vcpu);
2847}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848
Ingo Molnar8018c272006-12-29 16:50:01 -08002849int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2850{
2851 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002852 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002853
Ingo Molnar8018c272006-12-29 16:50:01 -08002854 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855}
2856
2857void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2858{
2859 ASSERT(vcpu);
2860
2861 destroy_kvm_mmu(vcpu);
2862 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002863 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864}
2865
Avi Kivity90cb0522007-07-17 13:04:56 +03002866void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867{
Avi Kivity4db35312007-11-21 15:28:32 +02002868 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002870 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 int i;
2872 u64 *pt;
2873
Sheng Yang291f26b2008-10-16 17:30:57 +08002874 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 continue;
2876
Avi Kivity4db35312007-11-21 15:28:32 +02002877 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2879 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002880 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 }
Avi Kivity171d5952008-08-27 16:40:51 +03002883 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002885
Avi Kivity90cb0522007-07-17 13:04:56 +03002886void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002887{
Avi Kivity4db35312007-11-21 15:28:32 +02002888 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002889
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002890 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002891 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002892 if (kvm_mmu_zap_page(kvm, sp))
2893 node = container_of(kvm->arch.active_mmu_pages.next,
2894 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002895 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002896
Avi Kivity90cb0522007-07-17 13:04:56 +03002897 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002898}
2899
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002900static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002901{
2902 struct kvm_mmu_page *page;
2903
2904 page = container_of(kvm->arch.active_mmu_pages.prev,
2905 struct kvm_mmu_page, link);
2906 kvm_mmu_zap_page(kvm, page);
2907}
2908
2909static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2910{
2911 struct kvm *kvm;
2912 struct kvm *kvm_freed = NULL;
2913 int cache_count = 0;
2914
2915 spin_lock(&kvm_lock);
2916
2917 list_for_each_entry(kvm, &vm_list, vm_list) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002918 int npages, idx;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002919
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002920 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002921 spin_lock(&kvm->mmu_lock);
2922 npages = kvm->arch.n_alloc_mmu_pages -
2923 kvm->arch.n_free_mmu_pages;
2924 cache_count += npages;
2925 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2926 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2927 cache_count--;
2928 kvm_freed = kvm;
2929 }
2930 nr_to_scan--;
2931
2932 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002933 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002934 }
2935 if (kvm_freed)
2936 list_move_tail(&kvm_freed->vm_list, &vm_list);
2937
2938 spin_unlock(&kvm_lock);
2939
2940 return cache_count;
2941}
2942
2943static struct shrinker mmu_shrinker = {
2944 .shrink = mmu_shrink,
2945 .seeks = DEFAULT_SEEKS * 10,
2946};
2947
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002948static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002949{
2950 if (pte_chain_cache)
2951 kmem_cache_destroy(pte_chain_cache);
2952 if (rmap_desc_cache)
2953 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002954 if (mmu_page_header_cache)
2955 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002956}
2957
Izik Eidus3ee16c82008-03-30 15:17:21 +03002958void kvm_mmu_module_exit(void)
2959{
2960 mmu_destroy_caches();
2961 unregister_shrinker(&mmu_shrinker);
2962}
2963
Avi Kivityb5a33a72007-04-15 16:31:09 +03002964int kvm_mmu_module_init(void)
2965{
2966 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2967 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002968 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002969 if (!pte_chain_cache)
2970 goto nomem;
2971 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2972 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002973 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002974 if (!rmap_desc_cache)
2975 goto nomem;
2976
Avi Kivityd3d25b02007-05-30 12:34:53 +03002977 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2978 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002979 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002980 if (!mmu_page_header_cache)
2981 goto nomem;
2982
Izik Eidus3ee16c82008-03-30 15:17:21 +03002983 register_shrinker(&mmu_shrinker);
2984
Avi Kivityb5a33a72007-04-15 16:31:09 +03002985 return 0;
2986
2987nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002988 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002989 return -ENOMEM;
2990}
2991
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002992/*
2993 * Caculate mmu pages needed for kvm.
2994 */
2995unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2996{
2997 int i;
2998 unsigned int nr_mmu_pages;
2999 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003000 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003001
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003002 slots = rcu_dereference(kvm->memslots);
3003 for (i = 0; i < slots->nmemslots; i++)
3004 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003005
3006 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3007 nr_mmu_pages = max(nr_mmu_pages,
3008 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3009
3010 return nr_mmu_pages;
3011}
3012
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003013static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3014 unsigned len)
3015{
3016 if (len > buffer->len)
3017 return NULL;
3018 return buffer->ptr;
3019}
3020
3021static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3022 unsigned len)
3023{
3024 void *ret;
3025
3026 ret = pv_mmu_peek_buffer(buffer, len);
3027 if (!ret)
3028 return ret;
3029 buffer->ptr += len;
3030 buffer->len -= len;
3031 buffer->processed += len;
3032 return ret;
3033}
3034
3035static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3036 gpa_t addr, gpa_t value)
3037{
3038 int bytes = 8;
3039 int r;
3040
3041 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3042 bytes = 4;
3043
3044 r = mmu_topup_memory_caches(vcpu);
3045 if (r)
3046 return r;
3047
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003048 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003049 return -EFAULT;
3050
3051 return 1;
3052}
3053
3054static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3055{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003056 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003057 return 1;
3058}
3059
3060static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3061{
3062 spin_lock(&vcpu->kvm->mmu_lock);
3063 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3064 spin_unlock(&vcpu->kvm->mmu_lock);
3065 return 1;
3066}
3067
3068static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3069 struct kvm_pv_mmu_op_buffer *buffer)
3070{
3071 struct kvm_mmu_op_header *header;
3072
3073 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3074 if (!header)
3075 return 0;
3076 switch (header->op) {
3077 case KVM_MMU_OP_WRITE_PTE: {
3078 struct kvm_mmu_op_write_pte *wpte;
3079
3080 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3081 if (!wpte)
3082 return 0;
3083 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3084 wpte->pte_val);
3085 }
3086 case KVM_MMU_OP_FLUSH_TLB: {
3087 struct kvm_mmu_op_flush_tlb *ftlb;
3088
3089 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3090 if (!ftlb)
3091 return 0;
3092 return kvm_pv_mmu_flush_tlb(vcpu);
3093 }
3094 case KVM_MMU_OP_RELEASE_PT: {
3095 struct kvm_mmu_op_release_pt *rpt;
3096
3097 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3098 if (!rpt)
3099 return 0;
3100 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3101 }
3102 default: return 0;
3103 }
3104}
3105
3106int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3107 gpa_t addr, unsigned long *ret)
3108{
3109 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003110 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003111
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003112 buffer->ptr = buffer->buf;
3113 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3114 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003115
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003116 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003117 if (r)
3118 goto out;
3119
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003120 while (buffer->len) {
3121 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003122 if (r < 0)
3123 goto out;
3124 if (r == 0)
3125 break;
3126 }
3127
3128 r = 1;
3129out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003130 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003131 return r;
3132}
3133
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003134int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3135{
3136 struct kvm_shadow_walk_iterator iterator;
3137 int nr_sptes = 0;
3138
3139 spin_lock(&vcpu->kvm->mmu_lock);
3140 for_each_shadow_entry(vcpu, addr, iterator) {
3141 sptes[iterator.level-1] = *iterator.sptep;
3142 nr_sptes++;
3143 if (!is_shadow_present_pte(*iterator.sptep))
3144 break;
3145 }
3146 spin_unlock(&vcpu->kvm->mmu_lock);
3147
3148 return nr_sptes;
3149}
3150EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3151
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003152#ifdef AUDIT
3153
3154static const char *audit_msg;
3155
3156static gva_t canonicalize(gva_t gva)
3157{
3158#ifdef CONFIG_X86_64
3159 gva = (long long)(gva << 16) >> 16;
3160#endif
3161 return gva;
3162}
3163
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003164
3165typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3166 u64 *sptep);
3167
3168static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3169 inspect_spte_fn fn)
3170{
3171 int i;
3172
3173 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3174 u64 ent = sp->spt[i];
3175
3176 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003177 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003178 struct kvm_mmu_page *child;
3179 child = page_header(ent & PT64_BASE_ADDR_MASK);
3180 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003181 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003182 fn(kvm, sp, &sp->spt[i]);
3183 }
3184 }
3185}
3186
3187static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3188{
3189 int i;
3190 struct kvm_mmu_page *sp;
3191
3192 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3193 return;
3194 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3195 hpa_t root = vcpu->arch.mmu.root_hpa;
3196 sp = page_header(root);
3197 __mmu_spte_walk(vcpu->kvm, sp, fn);
3198 return;
3199 }
3200 for (i = 0; i < 4; ++i) {
3201 hpa_t root = vcpu->arch.mmu.pae_root[i];
3202
3203 if (root && VALID_PAGE(root)) {
3204 root &= PT64_BASE_ADDR_MASK;
3205 sp = page_header(root);
3206 __mmu_spte_walk(vcpu->kvm, sp, fn);
3207 }
3208 }
3209 return;
3210}
3211
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003212static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3213 gva_t va, int level)
3214{
3215 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3216 int i;
3217 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3218
3219 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3220 u64 ent = pt[i];
3221
Avi Kivityc7addb92007-09-16 18:58:32 +02003222 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003223 continue;
3224
3225 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003226 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3227 audit_mappings_page(vcpu, ent, va, level - 1);
3228 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003229 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003230 gfn_t gfn = gpa >> PAGE_SHIFT;
3231 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3232 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003233
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003234 if (is_error_pfn(pfn)) {
3235 kvm_release_pfn_clean(pfn);
3236 continue;
3237 }
3238
Avi Kivityc7addb92007-09-16 18:58:32 +02003239 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003240 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003241 printk(KERN_ERR "xx audit error: (%s) levels %d"
3242 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003243 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003244 va, gpa, hpa, ent,
3245 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003246 else if (ent == shadow_notrap_nonpresent_pte
3247 && !is_error_hpa(hpa))
3248 printk(KERN_ERR "audit: (%s) notrap shadow,"
3249 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003250 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003251
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003252 }
3253 }
3254}
3255
3256static void audit_mappings(struct kvm_vcpu *vcpu)
3257{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003258 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003259
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003260 if (vcpu->arch.mmu.root_level == 4)
3261 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003262 else
3263 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003264 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003265 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003266 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003267 i << 30,
3268 2);
3269}
3270
3271static int count_rmaps(struct kvm_vcpu *vcpu)
3272{
3273 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003274 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003275
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003276 idx = srcu_read_lock(&kvm->srcu);
3277 slots = rcu_dereference(kvm->memslots);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003278 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003279 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003280 struct kvm_rmap_desc *d;
3281
3282 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003283 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003284
Izik Eidus290fc382007-09-27 14:11:22 +02003285 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003286 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003287 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003288 ++nmaps;
3289 continue;
3290 }
Izik Eidus290fc382007-09-27 14:11:22 +02003291 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003292 while (d) {
3293 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003294 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003295 ++nmaps;
3296 else
3297 break;
3298 d = d->more;
3299 }
3300 }
3301 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003302 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303 return nmaps;
3304}
3305
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003306void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003307{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003308 unsigned long *rmapp;
3309 struct kvm_mmu_page *rev_sp;
3310 gfn_t gfn;
3311
3312 if (*sptep & PT_WRITABLE_MASK) {
3313 rev_sp = page_header(__pa(sptep));
3314 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3315
3316 if (!gfn_to_memslot(kvm, gfn)) {
3317 if (!printk_ratelimit())
3318 return;
3319 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3320 audit_msg, gfn);
3321 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3322 audit_msg, sptep - rev_sp->spt,
3323 rev_sp->gfn);
3324 dump_stack();
3325 return;
3326 }
3327
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003328 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3329 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003330 if (!*rmapp) {
3331 if (!printk_ratelimit())
3332 return;
3333 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3334 audit_msg, *sptep);
3335 dump_stack();
3336 }
3337 }
3338
3339}
3340
3341void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3342{
3343 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3344}
3345
3346static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3347{
Avi Kivity4db35312007-11-21 15:28:32 +02003348 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003349 int i;
3350
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003351 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003352 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003353
Avi Kivity4db35312007-11-21 15:28:32 +02003354 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003355 continue;
3356
3357 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3358 u64 ent = pt[i];
3359
3360 if (!(ent & PT_PRESENT_MASK))
3361 continue;
3362 if (!(ent & PT_WRITABLE_MASK))
3363 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003364 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003365 }
3366 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003367 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003368}
3369
3370static void audit_rmap(struct kvm_vcpu *vcpu)
3371{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003372 check_writable_mappings_rmap(vcpu);
3373 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003374}
3375
3376static void audit_write_protection(struct kvm_vcpu *vcpu)
3377{
Avi Kivity4db35312007-11-21 15:28:32 +02003378 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003379 struct kvm_memory_slot *slot;
3380 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003381 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003382 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003383
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003384 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003385 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003386 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003387 if (sp->unsync)
3388 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003389
Avi Kivity4db35312007-11-21 15:28:32 +02003390 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003391 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003392 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003393
3394 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3395 while (spte) {
3396 if (*spte & PT_WRITABLE_MASK)
3397 printk(KERN_ERR "%s: (%s) shadow page has "
3398 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003399 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003400 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003401 spte = rmap_next(vcpu->kvm, rmapp, spte);
3402 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003403 }
3404}
3405
3406static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3407{
3408 int olddbg = dbg;
3409
3410 dbg = 0;
3411 audit_msg = msg;
3412 audit_rmap(vcpu);
3413 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003414 if (strcmp("pre pte write", audit_msg) != 0)
3415 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003416 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003417 dbg = olddbg;
3418}
3419
3420#endif