blob: bc614f91f5ba82f31ce1fe44bb46200d44e7a75a [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 Kivity6aa8b732006-12-10 02:21:36 -080021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020028#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030029#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050030#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
32#include <asm/page.h>
33#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020034#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020035#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030069static int dbg = 0;
70module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080071#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
Marcelo Tosatti582801a2008-09-23 13:18:41 -030073static int oos_shadow = 1;
74module_param(oos_shadow, bool, 0644);
75
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080076#ifndef MMU_DEBUG
77#define ASSERT(x) do { } while (0)
78#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080079#define ASSERT(x) \
80 if (!(x)) { \
81 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
82 __FILE__, __LINE__, #x); \
83 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080084#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivity6aa8b732006-12-10 02:21:36 -080086#define PT_FIRST_AVAIL_BITS_SHIFT 9
87#define PT64_SECOND_AVAIL_BITS_SHIFT 52
88
Avi Kivity6aa8b732006-12-10 02:21:36 -080089#define VALID_PAGE(x) ((x) != INVALID_PAGE)
90
91#define PT64_LEVEL_BITS 9
92
93#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040094 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
96#define PT64_LEVEL_MASK(level) \
97 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
98
99#define PT64_INDEX(address, level)\
100 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
101
102
103#define PT32_LEVEL_BITS 10
104
105#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400106 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107
108#define PT32_LEVEL_MASK(level) \
109 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
110
111#define PT32_INDEX(address, level)\
112 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
113
114
Avi Kivity27aba762007-03-09 13:04:31 +0200115#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116#define PT64_DIR_BASE_ADDR_MASK \
117 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
118
119#define PT32_BASE_ADDR_MASK PAGE_MASK
120#define PT32_DIR_BASE_ADDR_MASK \
121 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
122
Avi Kivity79539ce2007-11-21 02:06:21 +0200123#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
124 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125
126#define PFERR_PRESENT_MASK (1U << 0)
127#define PFERR_WRITE_MASK (1U << 1)
128#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800129#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800130#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132#define PT_DIRECTORY_LEVEL 2
133#define PT_PAGE_TABLE_LEVEL 1
134
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800135#define RMAP_EXT 4
136
Avi Kivityfe135d22007-12-09 16:15:46 +0200137#define ACC_EXEC_MASK 1
138#define ACC_WRITE_MASK PT_WRITABLE_MASK
139#define ACC_USER_MASK PT_USER_MASK
140#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
141
Avi Kivity135f8c22008-08-21 17:49:56 +0300142#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
143
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800144struct kvm_rmap_desc {
145 u64 *shadow_ptes[RMAP_EXT];
146 struct kvm_rmap_desc *more;
147};
148
Avi Kivity2d111232008-12-25 14:39:47 +0200149struct kvm_shadow_walk_iterator {
150 u64 addr;
151 hpa_t shadow_addr;
152 int level;
153 u64 *sptep;
154 unsigned index;
155};
156
157#define for_each_shadow_entry(_vcpu, _addr, _walker) \
158 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
159 shadow_walk_okay(&(_walker)); \
160 shadow_walk_next(&(_walker)))
161
162
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300163struct kvm_unsync_walk {
164 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
165};
166
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300167typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
168
Avi Kivityb5a33a72007-04-15 16:31:09 +0300169static struct kmem_cache *pte_chain_cache;
170static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300171static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300172
Avi Kivityc7addb92007-09-16 18:58:32 +0200173static u64 __read_mostly shadow_trap_nonpresent_pte;
174static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800175static u64 __read_mostly shadow_base_present_pte;
176static u64 __read_mostly shadow_nx_mask;
177static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
178static u64 __read_mostly shadow_user_mask;
179static u64 __read_mostly shadow_accessed_mask;
180static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200181
Dong, Eddie82725b22009-03-30 16:21:08 +0800182static inline u64 rsvd_bits(int s, int e)
183{
184 return ((1ULL << (e - s + 1)) - 1) << s;
185}
186
Avi Kivityc7addb92007-09-16 18:58:32 +0200187void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
188{
189 shadow_trap_nonpresent_pte = trap_pte;
190 shadow_notrap_nonpresent_pte = notrap_pte;
191}
192EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
193
Sheng Yang7b523452008-04-25 21:13:50 +0800194void kvm_mmu_set_base_ptes(u64 base_pte)
195{
196 shadow_base_present_pte = base_pte;
197}
198EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
199
200void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800201 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800202{
203 shadow_user_mask = user_mask;
204 shadow_accessed_mask = accessed_mask;
205 shadow_dirty_mask = dirty_mask;
206 shadow_nx_mask = nx_mask;
207 shadow_x_mask = x_mask;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static int is_write_protection(struct kvm_vcpu *vcpu)
212{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800213 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214}
215
216static int is_cpuid_PSE36(void)
217{
218 return 1;
219}
220
Avi Kivity73b10872007-01-26 00:56:41 -0800221static int is_nx(struct kvm_vcpu *vcpu)
222{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800223 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800224}
225
Avi Kivityc7addb92007-09-16 18:58:32 +0200226static int is_shadow_present_pte(u64 pte)
227{
Avi Kivityc7addb92007-09-16 18:58:32 +0200228 return pte != shadow_trap_nonpresent_pte
229 && pte != shadow_notrap_nonpresent_pte;
230}
231
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300232static int is_large_pte(u64 pte)
233{
234 return pte & PT_PAGE_SIZE_MASK;
235}
236
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237static int is_writeble_pte(unsigned long pte)
238{
239 return pte & PT_WRITABLE_MASK;
240}
241
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200242static int is_dirty_pte(unsigned long pte)
243{
Sheng Yang7b523452008-04-25 21:13:50 +0800244 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200245}
246
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800247static int is_rmap_pte(u64 pte)
248{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200249 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800250}
251
Anthony Liguori35149e22008-04-02 14:46:56 -0500252static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200253{
Anthony Liguori35149e22008-04-02 14:46:56 -0500254 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200255}
256
Avi Kivityda9285212007-11-21 13:54:47 +0200257static gfn_t pse36_gfn_delta(u32 gpte)
258{
259 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
260
261 return (gpte & PT32_DIR_PSE36_MASK) << shift;
262}
263
Avi Kivitye663ee62007-05-31 15:46:04 +0300264static void set_shadow_pte(u64 *sptep, u64 spte)
265{
266#ifdef CONFIG_X86_64
267 set_64bit((unsigned long *)sptep, spte);
268#else
269 set_64bit((unsigned long long *)sptep, spte);
270#endif
271}
272
Avi Kivitye2dec932007-01-05 16:36:54 -0800273static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300274 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800275{
276 void *obj;
277
278 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800279 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800280 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300281 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800282 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800283 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800284 cache->objects[cache->nobjs++] = obj;
285 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800286 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800287}
288
289static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
290{
291 while (mc->nobjs)
292 kfree(mc->objects[--mc->nobjs]);
293}
294
Avi Kivityc1158e62007-07-20 08:18:27 +0300295static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300296 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300297{
298 struct page *page;
299
300 if (cache->nobjs >= min)
301 return 0;
302 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300303 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300304 if (!page)
305 return -ENOMEM;
306 set_page_private(page, 0);
307 cache->objects[cache->nobjs++] = page_address(page);
308 }
309 return 0;
310}
311
312static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
313{
314 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300315 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300316}
317
Avi Kivity8c438502007-04-16 11:53:17 +0300318static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
319{
320 int r;
321
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800322 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300323 pte_chain_cache, 4);
324 if (r)
325 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800326 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200327 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300328 if (r)
329 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800330 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300331 if (r)
332 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300334 mmu_page_header_cache, 4);
335out:
Avi Kivity8c438502007-04-16 11:53:17 +0300336 return r;
337}
338
Avi Kivity714b93d2007-01-05 16:36:53 -0800339static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
340{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800341 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
342 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
343 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
344 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800345}
346
347static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
348 size_t size)
349{
350 void *p;
351
352 BUG_ON(!mc->nobjs);
353 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800354 return p;
355}
356
Avi Kivity714b93d2007-01-05 16:36:53 -0800357static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
358{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800360 sizeof(struct kvm_pte_chain));
361}
362
Avi Kivity90cb0522007-07-17 13:04:56 +0300363static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800364{
Avi Kivity90cb0522007-07-17 13:04:56 +0300365 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800366}
367
368static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800370 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800371 sizeof(struct kvm_rmap_desc));
372}
373
Avi Kivity90cb0522007-07-17 13:04:56 +0300374static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800375{
Avi Kivity90cb0522007-07-17 13:04:56 +0300376 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800377}
378
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800379/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300380 * Return the pointer to the largepage write count for a given
381 * gfn, handling slots that are not large page aligned.
382 */
383static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
384{
385 unsigned long idx;
386
387 idx = (gfn / KVM_PAGES_PER_HPAGE) -
388 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
389 return &slot->lpage_info[idx].write_count;
390}
391
392static void account_shadowed(struct kvm *kvm, gfn_t gfn)
393{
394 int *write_count;
395
Izik Eidus28430992008-10-03 17:40:32 +0300396 gfn = unalias_gfn(kvm, gfn);
397 write_count = slot_largepage_idx(gfn,
398 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300399 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300400}
401
402static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
403{
404 int *write_count;
405
Izik Eidus28430992008-10-03 17:40:32 +0300406 gfn = unalias_gfn(kvm, gfn);
407 write_count = slot_largepage_idx(gfn,
408 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409 *write_count -= 1;
410 WARN_ON(*write_count < 0);
411}
412
413static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
414{
Izik Eidus28430992008-10-03 17:40:32 +0300415 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300416 int *largepage_idx;
417
Izik Eidus28430992008-10-03 17:40:32 +0300418 gfn = unalias_gfn(kvm, gfn);
419 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300420 if (slot) {
421 largepage_idx = slot_largepage_idx(gfn, slot);
422 return *largepage_idx;
423 }
424
425 return 1;
426}
427
428static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
429{
430 struct vm_area_struct *vma;
431 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300432 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300433
434 addr = gfn_to_hva(kvm, gfn);
435 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300436 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300437
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300438 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439 vma = find_vma(current->mm, addr);
440 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300441 ret = 1;
442 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300443
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300444 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445}
446
447static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
448{
449 struct kvm_memory_slot *slot;
450
451 if (has_wrprotected_page(vcpu->kvm, large_gfn))
452 return 0;
453
454 if (!host_largepage_backed(vcpu->kvm, large_gfn))
455 return 0;
456
457 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
458 if (slot && slot->dirty_bitmap)
459 return 0;
460
461 return 1;
462}
463
464/*
Izik Eidus290fc382007-09-27 14:11:22 +0200465 * Take gfn and return the reverse mapping to it.
466 * Note: gfn must be unaliased before this function get called
467 */
468
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300469static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200470{
471 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200473
474 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475 if (!lpage)
476 return &slot->rmap[gfn - slot->base_gfn];
477
478 idx = (gfn / KVM_PAGES_PER_HPAGE) -
479 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
480
481 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200482}
483
484/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800485 * Reverse mapping data structures:
486 *
Izik Eidus290fc382007-09-27 14:11:22 +0200487 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
488 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800489 *
Izik Eidus290fc382007-09-27 14:11:22 +0200490 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
491 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800492 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494{
Avi Kivity4db35312007-11-21 15:28:32 +0200495 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200497 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498 int i;
499
500 if (!is_rmap_pte(*spte))
501 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200502 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200503 sp = page_header(__pa(spte));
504 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300505 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200506 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800507 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200508 *rmapp = (unsigned long)spte;
509 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800510 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800511 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200512 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800513 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200514 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800515 } else {
516 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200517 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800518 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
519 desc = desc->more;
520 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800521 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800522 desc = desc->more;
523 }
524 for (i = 0; desc->shadow_ptes[i]; ++i)
525 ;
526 desc->shadow_ptes[i] = spte;
527 }
528}
529
Izik Eidus290fc382007-09-27 14:11:22 +0200530static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800531 struct kvm_rmap_desc *desc,
532 int i,
533 struct kvm_rmap_desc *prev_desc)
534{
535 int j;
536
537 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
538 ;
539 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000540 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541 if (j != 0)
542 return;
543 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200544 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800545 else
546 if (prev_desc)
547 prev_desc->more = desc->more;
548 else
Izik Eidus290fc382007-09-27 14:11:22 +0200549 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300550 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800551}
552
Izik Eidus290fc382007-09-27 14:11:22 +0200553static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800554{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555 struct kvm_rmap_desc *desc;
556 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200557 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500558 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200559 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800560 int i;
561
562 if (!is_rmap_pte(*spte))
563 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200564 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500565 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800566 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500567 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200568 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500569 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200570 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500571 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300572 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200573 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800574 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
575 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200576 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200578 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800579 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
580 spte, *spte);
581 BUG();
582 }
Izik Eidus290fc382007-09-27 14:11:22 +0200583 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800584 } else {
585 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200586 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800587 prev_desc = NULL;
588 while (desc) {
589 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
590 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200591 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800592 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800593 prev_desc);
594 return;
595 }
596 prev_desc = desc;
597 desc = desc->more;
598 }
599 BUG();
600 }
601}
602
Izik Eidus98348e92007-10-16 14:42:30 +0200603static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800604{
Avi Kivity374cbac2007-01-05 16:36:43 -0800605 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200606 struct kvm_rmap_desc *prev_desc;
607 u64 *prev_spte;
608 int i;
609
610 if (!*rmapp)
611 return NULL;
612 else if (!(*rmapp & 1)) {
613 if (!spte)
614 return (u64 *)*rmapp;
615 return NULL;
616 }
617 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
618 prev_desc = NULL;
619 prev_spte = NULL;
620 while (desc) {
621 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
622 if (prev_spte == spte)
623 return desc->shadow_ptes[i];
624 prev_spte = desc->shadow_ptes[i];
625 }
626 desc = desc->more;
627 }
628 return NULL;
629}
630
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200631static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200632{
Izik Eidus290fc382007-09-27 14:11:22 +0200633 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800634 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800635 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800636
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500637 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300638 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800639
Izik Eidus98348e92007-10-16 14:42:30 +0200640 spte = rmap_next(kvm, rmapp, NULL);
641 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800642 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800643 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800644 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800645 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200646 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800647 write_protected = 1;
648 }
Izik Eidus9647c142007-10-16 14:43:46 +0200649 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800650 }
Izik Eidus855149a2008-03-20 18:17:24 +0200651 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500652 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200653
654 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500655 pfn = spte_to_pfn(*spte);
656 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200657 }
658
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300659 /* check for huge page mappings */
660 rmapp = gfn_to_rmap(kvm, gfn, 1);
661 spte = rmap_next(kvm, rmapp, NULL);
662 while (spte) {
663 BUG_ON(!spte);
664 BUG_ON(!(*spte & PT_PRESENT_MASK));
665 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
666 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
667 if (is_writeble_pte(*spte)) {
668 rmap_remove(kvm, spte);
669 --kvm->stat.lpages;
670 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300671 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300672 write_protected = 1;
673 }
674 spte = rmap_next(kvm, rmapp, spte);
675 }
676
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200677 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800678}
679
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200680static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
681{
682 u64 *spte;
683 int need_tlb_flush = 0;
684
685 while ((spte = rmap_next(kvm, rmapp, NULL))) {
686 BUG_ON(!(*spte & PT_PRESENT_MASK));
687 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
688 rmap_remove(kvm, spte);
689 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
690 need_tlb_flush = 1;
691 }
692 return need_tlb_flush;
693}
694
695static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
696 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
697{
698 int i;
699 int retval = 0;
700
701 /*
702 * If mmap_sem isn't taken, we can look the memslots with only
703 * the mmu_lock by skipping over the slots with userspace_addr == 0.
704 */
705 for (i = 0; i < kvm->nmemslots; i++) {
706 struct kvm_memory_slot *memslot = &kvm->memslots[i];
707 unsigned long start = memslot->userspace_addr;
708 unsigned long end;
709
710 /* mmu_lock protects userspace_addr */
711 if (!start)
712 continue;
713
714 end = start + (memslot->npages << PAGE_SHIFT);
715 if (hva >= start && hva < end) {
716 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
717 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
718 retval |= handler(kvm,
719 &memslot->lpage_info[
720 gfn_offset /
721 KVM_PAGES_PER_HPAGE].rmap_pde);
722 }
723 }
724
725 return retval;
726}
727
728int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
729{
730 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
731}
732
733static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
734{
735 u64 *spte;
736 int young = 0;
737
Sheng Yang534e38b2008-09-08 15:12:30 +0800738 /* always return old for EPT */
739 if (!shadow_accessed_mask)
740 return 0;
741
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200742 spte = rmap_next(kvm, rmapp, NULL);
743 while (spte) {
744 int _young;
745 u64 _spte = *spte;
746 BUG_ON(!(_spte & PT_PRESENT_MASK));
747 _young = _spte & PT_ACCESSED_MASK;
748 if (_young) {
749 young = 1;
750 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
751 }
752 spte = rmap_next(kvm, rmapp, spte);
753 }
754 return young;
755}
756
757int kvm_age_hva(struct kvm *kvm, unsigned long hva)
758{
759 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
760}
761
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800762#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300763static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764{
Avi Kivity139bdb22007-01-05 16:36:50 -0800765 u64 *pos;
766 u64 *end;
767
Avi Kivity47ad8e62007-05-06 15:50:58 +0300768 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300769 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800770 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800771 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 return 1;
775}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800776#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777
Avi Kivity4db35312007-11-21 15:28:32 +0200778static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800779{
Avi Kivity4db35312007-11-21 15:28:32 +0200780 ASSERT(is_empty_shadow_page(sp->spt));
781 list_del(&sp->link);
782 __free_page(virt_to_page(sp->spt));
783 __free_page(virt_to_page(sp->gfns));
784 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800785 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800786}
787
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800788static unsigned kvm_page_table_hashfn(gfn_t gfn)
789{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200790 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800791}
792
Avi Kivity25c0de22007-01-05 16:36:42 -0800793static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
794 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795{
Avi Kivity4db35312007-11-21 15:28:32 +0200796 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800798 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
799 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
800 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200801 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800802 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200803 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800804 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200805 sp->multimapped = 0;
806 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800807 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200808 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809}
810
Avi Kivity714b93d2007-01-05 16:36:53 -0800811static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200812 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800813{
814 struct kvm_pte_chain *pte_chain;
815 struct hlist_node *node;
816 int i;
817
818 if (!parent_pte)
819 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200820 if (!sp->multimapped) {
821 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822
823 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200824 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800825 return;
826 }
Avi Kivity4db35312007-11-21 15:28:32 +0200827 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800828 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200829 INIT_HLIST_HEAD(&sp->parent_ptes);
830 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800831 pte_chain->parent_ptes[0] = old;
832 }
Avi Kivity4db35312007-11-21 15:28:32 +0200833 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800834 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
835 continue;
836 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
837 if (!pte_chain->parent_ptes[i]) {
838 pte_chain->parent_ptes[i] = parent_pte;
839 return;
840 }
841 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800842 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200844 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845 pte_chain->parent_ptes[0] = parent_pte;
846}
847
Avi Kivity4db35312007-11-21 15:28:32 +0200848static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800849 u64 *parent_pte)
850{
851 struct kvm_pte_chain *pte_chain;
852 struct hlist_node *node;
853 int i;
854
Avi Kivity4db35312007-11-21 15:28:32 +0200855 if (!sp->multimapped) {
856 BUG_ON(sp->parent_pte != parent_pte);
857 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800858 return;
859 }
Avi Kivity4db35312007-11-21 15:28:32 +0200860 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800861 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
862 if (!pte_chain->parent_ptes[i])
863 break;
864 if (pte_chain->parent_ptes[i] != parent_pte)
865 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800866 while (i + 1 < NR_PTE_CHAIN_ENTRIES
867 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800868 pte_chain->parent_ptes[i]
869 = pte_chain->parent_ptes[i + 1];
870 ++i;
871 }
872 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800873 if (i == 0) {
874 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300875 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200876 if (hlist_empty(&sp->parent_ptes)) {
877 sp->multimapped = 0;
878 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800879 }
880 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800881 return;
882 }
883 BUG();
884}
885
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300886
887static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
888 mmu_parent_walk_fn fn)
889{
890 struct kvm_pte_chain *pte_chain;
891 struct hlist_node *node;
892 struct kvm_mmu_page *parent_sp;
893 int i;
894
895 if (!sp->multimapped && sp->parent_pte) {
896 parent_sp = page_header(__pa(sp->parent_pte));
897 fn(vcpu, parent_sp);
898 mmu_parent_walk(vcpu, parent_sp, fn);
899 return;
900 }
901 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
902 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
903 if (!pte_chain->parent_ptes[i])
904 break;
905 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
906 fn(vcpu, parent_sp);
907 mmu_parent_walk(vcpu, parent_sp, fn);
908 }
909}
910
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300911static void kvm_mmu_update_unsync_bitmap(u64 *spte)
912{
913 unsigned int index;
914 struct kvm_mmu_page *sp = page_header(__pa(spte));
915
916 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200917 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
918 sp->unsync_children++;
919 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300920}
921
922static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
923{
924 struct kvm_pte_chain *pte_chain;
925 struct hlist_node *node;
926 int i;
927
928 if (!sp->parent_pte)
929 return;
930
931 if (!sp->multimapped) {
932 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
933 return;
934 }
935
936 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
937 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
938 if (!pte_chain->parent_ptes[i])
939 break;
940 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
941 }
942}
943
944static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
945{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300946 kvm_mmu_update_parents_unsync(sp);
947 return 1;
948}
949
950static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
951 struct kvm_mmu_page *sp)
952{
953 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
954 kvm_mmu_update_parents_unsync(sp);
955}
956
Avi Kivityd761a502008-05-29 14:55:03 +0300957static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
958 struct kvm_mmu_page *sp)
959{
960 int i;
961
962 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
963 sp->spt[i] = shadow_trap_nonpresent_pte;
964}
965
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300966static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
967 struct kvm_mmu_page *sp)
968{
969 return 1;
970}
971
Marcelo Tosattia7052892008-09-23 13:18:35 -0300972static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
973{
974}
975
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200976#define KVM_PAGE_ARRAY_NR 16
977
978struct kvm_mmu_pages {
979 struct mmu_page_and_offset {
980 struct kvm_mmu_page *sp;
981 unsigned int idx;
982 } page[KVM_PAGE_ARRAY_NR];
983 unsigned int nr;
984};
985
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300986#define for_each_unsync_children(bitmap, idx) \
987 for (idx = find_first_bit(bitmap, 512); \
988 idx < 512; \
989 idx = find_next_bit(bitmap, 512, idx+1))
990
Hannes Edercded19f2009-02-21 02:19:13 +0100991static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
992 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300993{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200994 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300995
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200996 if (sp->unsync)
997 for (i=0; i < pvec->nr; i++)
998 if (pvec->page[i].sp == sp)
999 return 0;
1000
1001 pvec->page[pvec->nr].sp = sp;
1002 pvec->page[pvec->nr].idx = idx;
1003 pvec->nr++;
1004 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1005}
1006
1007static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1008 struct kvm_mmu_pages *pvec)
1009{
1010 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001011
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001012 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001013 u64 ent = sp->spt[i];
1014
Marcelo Tosatti87917232008-12-22 18:49:30 -02001015 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001016 struct kvm_mmu_page *child;
1017 child = page_header(ent & PT64_BASE_ADDR_MASK);
1018
1019 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001020 if (mmu_pages_add(pvec, child, i))
1021 return -ENOSPC;
1022
1023 ret = __mmu_unsync_walk(child, pvec);
1024 if (!ret)
1025 __clear_bit(i, sp->unsync_child_bitmap);
1026 else if (ret > 0)
1027 nr_unsync_leaf += ret;
1028 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001029 return ret;
1030 }
1031
1032 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001033 nr_unsync_leaf++;
1034 if (mmu_pages_add(pvec, child, i))
1035 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001036 }
1037 }
1038 }
1039
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001040 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001041 sp->unsync_children = 0;
1042
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001043 return nr_unsync_leaf;
1044}
1045
1046static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1047 struct kvm_mmu_pages *pvec)
1048{
1049 if (!sp->unsync_children)
1050 return 0;
1051
1052 mmu_pages_add(pvec, sp, 0);
1053 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001054}
1055
Avi Kivity4db35312007-11-21 15:28:32 +02001056static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001057{
1058 unsigned index;
1059 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001060 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001061 struct hlist_node *node;
1062
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001063 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001064 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001065 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001066 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001067 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001068 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001069 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001070 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001071 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001072 }
1073 return NULL;
1074}
1075
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001076static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1077{
1078 WARN_ON(!sp->unsync);
1079 sp->unsync = 0;
1080 --kvm->stat.mmu_unsync;
1081}
1082
1083static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1084
1085static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1086{
1087 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1088 kvm_mmu_zap_page(vcpu->kvm, sp);
1089 return 1;
1090 }
1091
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001092 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1093 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001094 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001095 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1096 kvm_mmu_zap_page(vcpu->kvm, sp);
1097 return 1;
1098 }
1099
1100 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001101 return 0;
1102}
1103
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001104struct mmu_page_path {
1105 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1106 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001107};
1108
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001109#define for_each_sp(pvec, sp, parents, i) \
1110 for (i = mmu_pages_next(&pvec, &parents, -1), \
1111 sp = pvec.page[i].sp; \
1112 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1113 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001114
Hannes Edercded19f2009-02-21 02:19:13 +01001115static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1116 struct mmu_page_path *parents,
1117 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001118{
1119 int n;
1120
1121 for (n = i+1; n < pvec->nr; n++) {
1122 struct kvm_mmu_page *sp = pvec->page[n].sp;
1123
1124 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1125 parents->idx[0] = pvec->page[n].idx;
1126 return n;
1127 }
1128
1129 parents->parent[sp->role.level-2] = sp;
1130 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1131 }
1132
1133 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001134}
1135
Hannes Edercded19f2009-02-21 02:19:13 +01001136static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001137{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001138 struct kvm_mmu_page *sp;
1139 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001140
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001141 do {
1142 unsigned int idx = parents->idx[level];
1143
1144 sp = parents->parent[level];
1145 if (!sp)
1146 return;
1147
1148 --sp->unsync_children;
1149 WARN_ON((int)sp->unsync_children < 0);
1150 __clear_bit(idx, sp->unsync_child_bitmap);
1151 level++;
1152 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1153}
1154
1155static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1156 struct mmu_page_path *parents,
1157 struct kvm_mmu_pages *pvec)
1158{
1159 parents->parent[parent->role.level-1] = NULL;
1160 pvec->nr = 0;
1161}
1162
1163static void mmu_sync_children(struct kvm_vcpu *vcpu,
1164 struct kvm_mmu_page *parent)
1165{
1166 int i;
1167 struct kvm_mmu_page *sp;
1168 struct mmu_page_path parents;
1169 struct kvm_mmu_pages pages;
1170
1171 kvm_mmu_pages_init(parent, &parents, &pages);
1172 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001173 int protected = 0;
1174
1175 for_each_sp(pages, sp, parents, i)
1176 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1177
1178 if (protected)
1179 kvm_flush_remote_tlbs(vcpu->kvm);
1180
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001181 for_each_sp(pages, sp, parents, i) {
1182 kvm_sync_page(vcpu, sp);
1183 mmu_pages_clear_parents(&parents);
1184 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001185 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001186 kvm_mmu_pages_init(parent, &parents, &pages);
1187 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001188}
1189
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001190static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1191 gfn_t gfn,
1192 gva_t gaddr,
1193 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001194 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001195 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001196 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001197{
1198 union kvm_mmu_page_role role;
1199 unsigned index;
1200 unsigned quadrant;
1201 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001202 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001203 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001204
Avi Kivitya770f6f2008-12-21 19:20:09 +02001205 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001206 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001207 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001208 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001209 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001210 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1211 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1212 role.quadrant = quadrant;
1213 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001214 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001215 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001216 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001217 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001218 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1219 if (sp->gfn == gfn) {
1220 if (sp->unsync)
1221 if (kvm_sync_page(vcpu, sp))
1222 continue;
1223
1224 if (sp->role.word != role.word)
1225 continue;
1226
Avi Kivity4db35312007-11-21 15:28:32 +02001227 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001228 if (sp->unsync_children) {
1229 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1230 kvm_mmu_mark_parents_unsync(vcpu, sp);
1231 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001232 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001233 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001234 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001235 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001236 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1237 if (!sp)
1238 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001239 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001240 sp->gfn = gfn;
1241 sp->role = role;
1242 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001243 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001244 if (rmap_write_protect(vcpu->kvm, gfn))
1245 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001246 account_shadowed(vcpu->kvm, gfn);
1247 }
Avi Kivity131d8272008-05-29 14:56:28 +03001248 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1249 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1250 else
1251 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001252 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001253}
1254
Avi Kivity2d111232008-12-25 14:39:47 +02001255static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1256 struct kvm_vcpu *vcpu, u64 addr)
1257{
1258 iterator->addr = addr;
1259 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1260 iterator->level = vcpu->arch.mmu.shadow_root_level;
1261 if (iterator->level == PT32E_ROOT_LEVEL) {
1262 iterator->shadow_addr
1263 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1264 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1265 --iterator->level;
1266 if (!iterator->shadow_addr)
1267 iterator->level = 0;
1268 }
1269}
1270
1271static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1272{
1273 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1274 return false;
1275 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1276 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1277 return true;
1278}
1279
1280static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1281{
1282 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1283 --iterator->level;
1284}
1285
Avi Kivity90cb0522007-07-17 13:04:56 +03001286static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001287 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001288{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001289 unsigned i;
1290 u64 *pt;
1291 u64 ent;
1292
Avi Kivity4db35312007-11-21 15:28:32 +02001293 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001294
Avi Kivity4db35312007-11-21 15:28:32 +02001295 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001296 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001297 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001298 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001299 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001300 }
1301 return;
1302 }
1303
1304 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1305 ent = pt[i];
1306
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001307 if (is_shadow_present_pte(ent)) {
1308 if (!is_large_pte(ent)) {
1309 ent &= PT64_BASE_ADDR_MASK;
1310 mmu_page_remove_parent_pte(page_header(ent),
1311 &pt[i]);
1312 } else {
1313 --kvm->stat.lpages;
1314 rmap_remove(kvm, &pt[i]);
1315 }
1316 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001317 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001318 }
Avi Kivitya4360362007-01-05 16:36:45 -08001319}
1320
Avi Kivity4db35312007-11-21 15:28:32 +02001321static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001322{
Avi Kivity4db35312007-11-21 15:28:32 +02001323 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001324}
1325
Avi Kivity12b7d282007-09-23 14:10:49 +02001326static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1327{
1328 int i;
1329
1330 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1331 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001332 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001333}
1334
Avi Kivity31aa2b42008-07-11 17:59:46 +03001335static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001336{
1337 u64 *parent_pte;
1338
Avi Kivity4db35312007-11-21 15:28:32 +02001339 while (sp->multimapped || sp->parent_pte) {
1340 if (!sp->multimapped)
1341 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001342 else {
1343 struct kvm_pte_chain *chain;
1344
Avi Kivity4db35312007-11-21 15:28:32 +02001345 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001346 struct kvm_pte_chain, link);
1347 parent_pte = chain->parent_ptes[0];
1348 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001349 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001350 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001351 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001352 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001353}
1354
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001355static int mmu_zap_unsync_children(struct kvm *kvm,
1356 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001357{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001358 int i, zapped = 0;
1359 struct mmu_page_path parents;
1360 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001361
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001362 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001363 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001364
1365 kvm_mmu_pages_init(parent, &parents, &pages);
1366 while (mmu_unsync_walk(parent, &pages)) {
1367 struct kvm_mmu_page *sp;
1368
1369 for_each_sp(pages, sp, parents, i) {
1370 kvm_mmu_zap_page(kvm, sp);
1371 mmu_pages_clear_parents(&parents);
1372 }
1373 zapped += pages.nr;
1374 kvm_mmu_pages_init(parent, &parents, &pages);
1375 }
1376
1377 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001378}
1379
Marcelo Tosatti07385412008-09-23 13:18:37 -03001380static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001381{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001382 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001383 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001384 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001385 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001386 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001387 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001388 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001389 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001390 if (sp->unsync)
1391 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001392 if (!sp->root_count) {
1393 hlist_del(&sp->hash_link);
1394 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001395 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001396 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001397 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001398 kvm_reload_remote_mmus(kvm);
1399 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001400 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001401 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001402}
1403
Izik Eidus82ce2c92007-10-02 18:52:55 +02001404/*
1405 * Changing the number of mmu pages allocated to the vm
1406 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1407 */
1408void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1409{
1410 /*
1411 * If we set the number of mmu pages to be smaller be than the
1412 * number of actived pages , we must to free some mmu pages before we
1413 * change the value
1414 */
1415
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001416 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001417 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001418 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1419 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001420
1421 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1422 struct kvm_mmu_page *page;
1423
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001424 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001425 struct kvm_mmu_page, link);
1426 kvm_mmu_zap_page(kvm, page);
1427 n_used_mmu_pages--;
1428 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001429 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001430 }
1431 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001432 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1433 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001434
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001435 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001436}
1437
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001438static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001439{
1440 unsigned index;
1441 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001442 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001443 struct hlist_node *node, *n;
1444 int r;
1445
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001446 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001447 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001448 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001449 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001450 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001451 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001452 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001453 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001454 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001455 if (kvm_mmu_zap_page(kvm, sp))
1456 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001457 }
1458 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001459}
1460
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001461static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001462{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001463 unsigned index;
1464 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001465 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001466 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001467
Avi Kivity4677a3b2009-01-06 13:00:27 +02001468 index = kvm_page_table_hashfn(gfn);
1469 bucket = &kvm->arch.mmu_page_hash[index];
1470 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001471 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001472 && !sp->role.invalid) {
1473 pgprintk("%s: zap %lx %x\n",
1474 __func__, gfn, sp->role.word);
1475 kvm_mmu_zap_page(kvm, sp);
1476 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001477 }
1478}
1479
Avi Kivity38c335f2007-11-21 14:20:22 +02001480static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Avi Kivity38c335f2007-11-21 14:20:22 +02001482 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001483 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484
Sheng Yang291f26b2008-10-16 17:30:57 +08001485 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486}
1487
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001488static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1489{
1490 int i;
1491 u64 *pt = sp->spt;
1492
1493 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1494 return;
1495
1496 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1497 if (pt[i] == shadow_notrap_nonpresent_pte)
1498 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1499 }
1500}
1501
Avi Kivity039576c2007-03-20 12:46:50 +02001502struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1503{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001504 struct page *page;
1505
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001506 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001507
1508 if (gpa == UNMAPPED_GVA)
1509 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001510
Izik Eidus72dc67a2008-02-10 18:04:15 +02001511 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001512
1513 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001514}
1515
Sheng Yang74be52e2008-10-09 16:01:56 +08001516/*
1517 * The function is based on mtrr_type_lookup() in
1518 * arch/x86/kernel/cpu/mtrr/generic.c
1519 */
1520static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1521 u64 start, u64 end)
1522{
1523 int i;
1524 u64 base, mask;
1525 u8 prev_match, curr_match;
1526 int num_var_ranges = KVM_NR_VAR_MTRR;
1527
1528 if (!mtrr_state->enabled)
1529 return 0xFF;
1530
1531 /* Make end inclusive end, instead of exclusive */
1532 end--;
1533
1534 /* Look in fixed ranges. Just return the type as per start */
1535 if (mtrr_state->have_fixed && (start < 0x100000)) {
1536 int idx;
1537
1538 if (start < 0x80000) {
1539 idx = 0;
1540 idx += (start >> 16);
1541 return mtrr_state->fixed_ranges[idx];
1542 } else if (start < 0xC0000) {
1543 idx = 1 * 8;
1544 idx += ((start - 0x80000) >> 14);
1545 return mtrr_state->fixed_ranges[idx];
1546 } else if (start < 0x1000000) {
1547 idx = 3 * 8;
1548 idx += ((start - 0xC0000) >> 12);
1549 return mtrr_state->fixed_ranges[idx];
1550 }
1551 }
1552
1553 /*
1554 * Look in variable ranges
1555 * Look of multiple ranges matching this address and pick type
1556 * as per MTRR precedence
1557 */
1558 if (!(mtrr_state->enabled & 2))
1559 return mtrr_state->def_type;
1560
1561 prev_match = 0xFF;
1562 for (i = 0; i < num_var_ranges; ++i) {
1563 unsigned short start_state, end_state;
1564
1565 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1566 continue;
1567
1568 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1569 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1570 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1571 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1572
1573 start_state = ((start & mask) == (base & mask));
1574 end_state = ((end & mask) == (base & mask));
1575 if (start_state != end_state)
1576 return 0xFE;
1577
1578 if ((start & mask) != (base & mask))
1579 continue;
1580
1581 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1582 if (prev_match == 0xFF) {
1583 prev_match = curr_match;
1584 continue;
1585 }
1586
1587 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1588 curr_match == MTRR_TYPE_UNCACHABLE)
1589 return MTRR_TYPE_UNCACHABLE;
1590
1591 if ((prev_match == MTRR_TYPE_WRBACK &&
1592 curr_match == MTRR_TYPE_WRTHROUGH) ||
1593 (prev_match == MTRR_TYPE_WRTHROUGH &&
1594 curr_match == MTRR_TYPE_WRBACK)) {
1595 prev_match = MTRR_TYPE_WRTHROUGH;
1596 curr_match = MTRR_TYPE_WRTHROUGH;
1597 }
1598
1599 if (prev_match != curr_match)
1600 return MTRR_TYPE_UNCACHABLE;
1601 }
1602
1603 if (prev_match != 0xFF)
1604 return prev_match;
1605
1606 return mtrr_state->def_type;
1607}
1608
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001609u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001610{
1611 u8 mtrr;
1612
1613 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1614 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1615 if (mtrr == 0xfe || mtrr == 0xff)
1616 mtrr = MTRR_TYPE_WRBACK;
1617 return mtrr;
1618}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001619EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001620
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001621static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1622{
1623 unsigned index;
1624 struct hlist_head *bucket;
1625 struct kvm_mmu_page *s;
1626 struct hlist_node *node, *n;
1627
1628 index = kvm_page_table_hashfn(sp->gfn);
1629 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1630 /* don't unsync if pagetable is shadowed with multiple roles */
1631 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001632 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001633 continue;
1634 if (s->role.word != sp->role.word)
1635 return 1;
1636 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001637 ++vcpu->kvm->stat.mmu_unsync;
1638 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001639
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001640 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001641
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001642 mmu_convert_notrap(sp);
1643 return 0;
1644}
1645
1646static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1647 bool can_unsync)
1648{
1649 struct kvm_mmu_page *shadow;
1650
1651 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1652 if (shadow) {
1653 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1654 return 1;
1655 if (shadow->unsync)
1656 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001657 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001658 return kvm_unsync_page(vcpu, shadow);
1659 return 1;
1660 }
1661 return 0;
1662}
1663
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001664static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1665 unsigned pte_access, int user_fault,
1666 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001667 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001668 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001669{
1670 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001671 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001672
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001673 /*
1674 * We don't set the accessed bit, since we sometimes want to see
1675 * whether the guest actually used the pte (in order to detect
1676 * demand paging).
1677 */
Sheng Yang7b523452008-04-25 21:13:50 +08001678 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001679 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001680 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001681 if (!dirty)
1682 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001683 if (pte_access & ACC_EXEC_MASK)
1684 spte |= shadow_x_mask;
1685 else
1686 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001687 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001688 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001689 if (largepage)
1690 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001691 if (tdp_enabled)
1692 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1693 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001694
Anthony Liguori35149e22008-04-02 14:46:56 -05001695 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001696
1697 if ((pte_access & ACC_WRITE_MASK)
1698 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001699
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001700 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1701 ret = 1;
1702 spte = shadow_trap_nonpresent_pte;
1703 goto set_pte;
1704 }
1705
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001706 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001707
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001708 /*
1709 * Optimization: for pte sync, if spte was writable the hash
1710 * lookup is unnecessary (and expensive). Write protection
1711 * is responsibility of mmu_get_page / kvm_sync_page.
1712 * Same reasoning can be applied to dirty page accounting.
1713 */
1714 if (!can_unsync && is_writeble_pte(*shadow_pte))
1715 goto set_pte;
1716
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001717 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001718 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001719 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001720 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001721 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001722 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001723 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001724 }
1725 }
1726
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727 if (pte_access & ACC_WRITE_MASK)
1728 mark_page_dirty(vcpu->kvm, gfn);
1729
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001730set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001731 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001732 return ret;
1733}
1734
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001735static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1736 unsigned pt_access, unsigned pte_access,
1737 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001738 int *ptwrite, int largepage, gfn_t gfn,
1739 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001740{
1741 int was_rmapped = 0;
1742 int was_writeble = is_writeble_pte(*shadow_pte);
1743
1744 pgprintk("%s: spte %llx access %x write_fault %d"
1745 " user_fault %d gfn %lx\n",
1746 __func__, *shadow_pte, pt_access,
1747 write_fault, user_fault, gfn);
1748
1749 if (is_rmap_pte(*shadow_pte)) {
1750 /*
1751 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1752 * the parent of the now unreachable PTE.
1753 */
1754 if (largepage && !is_large_pte(*shadow_pte)) {
1755 struct kvm_mmu_page *child;
1756 u64 pte = *shadow_pte;
1757
1758 child = page_header(pte & PT64_BASE_ADDR_MASK);
1759 mmu_page_remove_parent_pte(child, shadow_pte);
1760 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1761 pgprintk("hfn old %lx new %lx\n",
1762 spte_to_pfn(*shadow_pte), pfn);
1763 rmap_remove(vcpu->kvm, shadow_pte);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001764 } else
1765 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001766 }
1767 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001768 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001769 if (write_fault)
1770 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001771 kvm_x86_ops->tlb_flush(vcpu);
1772 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001773
1774 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1775 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1776 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1777 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1778 *shadow_pte, shadow_pte);
1779 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001780 ++vcpu->kvm->stat.lpages;
1781
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001782 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1783 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001784 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001785 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001786 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001787 } else {
1788 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001789 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001790 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001791 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001792 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001793 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001794 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001795 vcpu->arch.last_pte_gfn = gfn;
1796 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001797}
1798
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1800{
1801}
1802
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001803static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001804 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805{
Avi Kivity9f652d212008-12-25 14:54:25 +02001806 struct kvm_shadow_walk_iterator iterator;
1807 struct kvm_mmu_page *sp;
1808 int pt_write = 0;
1809 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810
Avi Kivity9f652d212008-12-25 14:54:25 +02001811 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1812 if (iterator.level == PT_PAGE_TABLE_LEVEL
1813 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1814 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1815 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001816 largepage, gfn, pfn, false);
Avi Kivity9f652d212008-12-25 14:54:25 +02001817 ++vcpu->stat.pf_fixed;
1818 break;
1819 }
1820
1821 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1822 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1823 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1824 iterator.level - 1,
1825 1, ACC_ALL, iterator.sptep);
1826 if (!sp) {
1827 pgprintk("nonpaging_map: ENOMEM\n");
1828 kvm_release_pfn_clean(pfn);
1829 return -ENOMEM;
1830 }
1831
1832 set_shadow_pte(iterator.sptep,
1833 __pa(sp->spt)
1834 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1835 | shadow_user_mask | shadow_x_mask);
1836 }
1837 }
1838 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839}
1840
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001841static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1842{
1843 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001844 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001845 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001846 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001847
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001848 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1849 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1850 largepage = 1;
1851 }
1852
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001853 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001854 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001855 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001856
Avi Kivityd196e342008-01-24 11:44:11 +02001857 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001858 if (is_error_pfn(pfn)) {
1859 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001860 return 1;
1861 }
1862
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001863 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001864 if (mmu_notifier_retry(vcpu, mmu_seq))
1865 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001866 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001867 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001868 spin_unlock(&vcpu->kvm->mmu_lock);
1869
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001870
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001871 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001872
1873out_unlock:
1874 spin_unlock(&vcpu->kvm->mmu_lock);
1875 kvm_release_pfn_clean(pfn);
1876 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001877}
1878
1879
Avi Kivity17ac10a2007-01-05 16:36:40 -08001880static void mmu_free_roots(struct kvm_vcpu *vcpu)
1881{
1882 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001883 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001884
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001885 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001886 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001887 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001888 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1889 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001890
Avi Kivity4db35312007-11-21 15:28:32 +02001891 sp = page_header(root);
1892 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001893 if (!sp->root_count && sp->role.invalid)
1894 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001895 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001896 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001897 return;
1898 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001899 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001900 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001901
Avi Kivity417726a2007-04-12 17:35:58 +03001902 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001903 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001904 sp = page_header(root);
1905 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001906 if (!sp->root_count && sp->role.invalid)
1907 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001908 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001909 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001910 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001911 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001912 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001913}
1914
1915static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1916{
1917 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001918 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001919 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001920 int direct = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001921
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001922 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001923
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001924 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1925 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001926
1927 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001928 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001929 direct = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001930 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001931 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001932 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001933 root = __pa(sp->spt);
1934 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001936 return;
1937 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001938 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001939 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001940 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001941 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001942 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001943
1944 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001945 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1946 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1947 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001948 continue;
1949 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001950 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1951 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001952 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001953 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001954 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001955 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001956 root = __pa(sp->spt);
1957 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001958 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001959 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001960 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001961}
1962
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001963static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1964{
1965 int i;
1966 struct kvm_mmu_page *sp;
1967
1968 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1969 return;
1970 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1971 hpa_t root = vcpu->arch.mmu.root_hpa;
1972 sp = page_header(root);
1973 mmu_sync_children(vcpu, sp);
1974 return;
1975 }
1976 for (i = 0; i < 4; ++i) {
1977 hpa_t root = vcpu->arch.mmu.pae_root[i];
1978
1979 if (root) {
1980 root &= PT64_BASE_ADDR_MASK;
1981 sp = page_header(root);
1982 mmu_sync_children(vcpu, sp);
1983 }
1984 }
1985}
1986
1987void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1988{
1989 spin_lock(&vcpu->kvm->mmu_lock);
1990 mmu_sync_roots(vcpu);
1991 spin_unlock(&vcpu->kvm->mmu_lock);
1992}
1993
Avi Kivity6aa8b732006-12-10 02:21:36 -08001994static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1995{
1996 return vaddr;
1997}
1998
1999static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002000 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001{
Avi Kivitye8332402007-12-09 18:43:00 +02002002 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002003 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002005 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002006 r = mmu_topup_memory_caches(vcpu);
2007 if (r)
2008 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002009
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012
Avi Kivitye8332402007-12-09 18:43:00 +02002013 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014
Avi Kivitye8332402007-12-09 18:43:00 +02002015 return nonpaging_map(vcpu, gva & PAGE_MASK,
2016 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017}
2018
Joerg Roedelfb72d162008-02-07 13:47:44 +01002019static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2020 u32 error_code)
2021{
Anthony Liguori35149e22008-04-02 14:46:56 -05002022 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002023 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002024 int largepage = 0;
2025 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002026 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002027
2028 ASSERT(vcpu);
2029 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2030
2031 r = mmu_topup_memory_caches(vcpu);
2032 if (r)
2033 return r;
2034
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002035 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2036 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2037 largepage = 1;
2038 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002039 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002040 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002041 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002042 if (is_error_pfn(pfn)) {
2043 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002044 return 1;
2045 }
2046 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002047 if (mmu_notifier_retry(vcpu, mmu_seq))
2048 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002049 kvm_mmu_free_some_pages(vcpu);
2050 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002051 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002052 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002053
2054 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002055
2056out_unlock:
2057 spin_unlock(&vcpu->kvm->mmu_lock);
2058 kvm_release_pfn_clean(pfn);
2059 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002060}
2061
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062static void nonpaging_free(struct kvm_vcpu *vcpu)
2063{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002064 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065}
2066
2067static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2068{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002069 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070
2071 context->new_cr3 = nonpaging_new_cr3;
2072 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073 context->gva_to_gpa = nonpaging_gva_to_gpa;
2074 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002075 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002076 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002077 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002078 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002080 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 return 0;
2082}
2083
Avi Kivityd835dfe2007-11-21 02:57:59 +02002084void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002086 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002087 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088}
2089
2090static void paging_new_cr3(struct kvm_vcpu *vcpu)
2091{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002092 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002093 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094}
2095
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096static void inject_page_fault(struct kvm_vcpu *vcpu,
2097 u64 addr,
2098 u32 err_code)
2099{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002100 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101}
2102
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103static void paging_free(struct kvm_vcpu *vcpu)
2104{
2105 nonpaging_free(vcpu);
2106}
2107
Dong, Eddie82725b22009-03-30 16:21:08 +08002108static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2109{
2110 int bit7;
2111
2112 bit7 = (gpte >> 7) & 1;
2113 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2114}
2115
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116#define PTTYPE 64
2117#include "paging_tmpl.h"
2118#undef PTTYPE
2119
2120#define PTTYPE 32
2121#include "paging_tmpl.h"
2122#undef PTTYPE
2123
Dong, Eddie82725b22009-03-30 16:21:08 +08002124static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2125{
2126 struct kvm_mmu *context = &vcpu->arch.mmu;
2127 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2128 u64 exb_bit_rsvd = 0;
2129
2130 if (!is_nx(vcpu))
2131 exb_bit_rsvd = rsvd_bits(63, 63);
2132 switch (level) {
2133 case PT32_ROOT_LEVEL:
2134 /* no rsvd bits for 2 level 4K page table entries */
2135 context->rsvd_bits_mask[0][1] = 0;
2136 context->rsvd_bits_mask[0][0] = 0;
2137 if (is_cpuid_PSE36())
2138 /* 36bits PSE 4MB page */
2139 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2140 else
2141 /* 32 bits PSE 4MB page */
2142 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2143 context->rsvd_bits_mask[1][0] = ~0ull;
2144 break;
2145 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002146 context->rsvd_bits_mask[0][2] =
2147 rsvd_bits(maxphyaddr, 63) |
2148 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002149 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002150 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002151 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2152 rsvd_bits(maxphyaddr, 62); /* PTE */
2153 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2154 rsvd_bits(maxphyaddr, 62) |
2155 rsvd_bits(13, 20); /* large page */
2156 context->rsvd_bits_mask[1][0] = ~0ull;
2157 break;
2158 case PT64_ROOT_LEVEL:
2159 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2160 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2161 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2162 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2163 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002164 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002165 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2166 rsvd_bits(maxphyaddr, 51);
2167 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2168 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2169 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002170 rsvd_bits(maxphyaddr, 51) |
2171 rsvd_bits(13, 20); /* large page */
Dong, Eddie82725b22009-03-30 16:21:08 +08002172 context->rsvd_bits_mask[1][0] = ~0ull;
2173 break;
2174 }
2175}
2176
Avi Kivity17ac10a2007-01-05 16:36:40 -08002177static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002179 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180
2181 ASSERT(is_pae(vcpu));
2182 context->new_cr3 = paging_new_cr3;
2183 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002185 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002186 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002187 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002189 context->root_level = level;
2190 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002191 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 return 0;
2193}
2194
Avi Kivity17ac10a2007-01-05 16:36:40 -08002195static int paging64_init_context(struct kvm_vcpu *vcpu)
2196{
Dong, Eddie82725b22009-03-30 16:21:08 +08002197 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002198 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2199}
2200
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201static int paging32_init_context(struct kvm_vcpu *vcpu)
2202{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002203 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204
Dong, Eddie82725b22009-03-30 16:21:08 +08002205 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 context->new_cr3 = paging_new_cr3;
2207 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208 context->gva_to_gpa = paging32_gva_to_gpa;
2209 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002210 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002211 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002212 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213 context->root_level = PT32_ROOT_LEVEL;
2214 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002215 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 return 0;
2217}
2218
2219static int paging32E_init_context(struct kvm_vcpu *vcpu)
2220{
Dong, Eddie82725b22009-03-30 16:21:08 +08002221 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002222 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223}
2224
Joerg Roedelfb72d162008-02-07 13:47:44 +01002225static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2226{
2227 struct kvm_mmu *context = &vcpu->arch.mmu;
2228
2229 context->new_cr3 = nonpaging_new_cr3;
2230 context->page_fault = tdp_page_fault;
2231 context->free = nonpaging_free;
2232 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002233 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002234 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002235 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002236 context->root_hpa = INVALID_PAGE;
2237
2238 if (!is_paging(vcpu)) {
2239 context->gva_to_gpa = nonpaging_gva_to_gpa;
2240 context->root_level = 0;
2241 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002242 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002243 context->gva_to_gpa = paging64_gva_to_gpa;
2244 context->root_level = PT64_ROOT_LEVEL;
2245 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002246 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002247 context->gva_to_gpa = paging64_gva_to_gpa;
2248 context->root_level = PT32E_ROOT_LEVEL;
2249 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002250 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002251 context->gva_to_gpa = paging32_gva_to_gpa;
2252 context->root_level = PT32_ROOT_LEVEL;
2253 }
2254
2255 return 0;
2256}
2257
2258static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002260 int r;
2261
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002263 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264
2265 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002266 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002267 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002268 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002270 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002272 r = paging32_init_context(vcpu);
2273
2274 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2275
2276 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277}
2278
Joerg Roedelfb72d162008-02-07 13:47:44 +01002279static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2280{
Anthony Liguori35149e22008-04-02 14:46:56 -05002281 vcpu->arch.update_pte.pfn = bad_pfn;
2282
Joerg Roedelfb72d162008-02-07 13:47:44 +01002283 if (tdp_enabled)
2284 return init_kvm_tdp_mmu(vcpu);
2285 else
2286 return init_kvm_softmmu(vcpu);
2287}
2288
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2290{
2291 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002292 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2293 vcpu->arch.mmu.free(vcpu);
2294 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 }
2296}
2297
2298int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2299{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002300 destroy_kvm_mmu(vcpu);
2301 return init_kvm_mmu(vcpu);
2302}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002303EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002304
2305int kvm_mmu_load(struct kvm_vcpu *vcpu)
2306{
Avi Kivity714b93d2007-01-05 16:36:53 -08002307 int r;
2308
Avi Kivitye2dec932007-01-05 16:36:54 -08002309 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002310 if (r)
2311 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002312 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002313 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002314 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002315 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002316 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002317 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002318 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002319out:
2320 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002322EXPORT_SYMBOL_GPL(kvm_mmu_load);
2323
2324void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2325{
2326 mmu_free_roots(vcpu);
2327}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328
Avi Kivity09072da2007-05-01 14:16:52 +03002329static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002330 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002331 u64 *spte)
2332{
2333 u64 pte;
2334 struct kvm_mmu_page *child;
2335
2336 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002337 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002338 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2339 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002340 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002341 else {
2342 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002343 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002344 }
2345 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002346 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002347 if (is_large_pte(pte))
2348 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002349}
2350
Avi Kivity00284252007-05-01 16:53:31 +03002351static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002352 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002353 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002354 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002355{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002356 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2357 if (!vcpu->arch.update_pte.largepage ||
2358 sp->role.glevels == PT32_ROOT_LEVEL) {
2359 ++vcpu->kvm->stat.mmu_pde_zapped;
2360 return;
2361 }
2362 }
Avi Kivity00284252007-05-01 16:53:31 +03002363
Avi Kivity4cee5762007-11-18 16:37:07 +02002364 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002365 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002366 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002367 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002368 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002369}
2370
Avi Kivity79539ce2007-11-21 02:06:21 +02002371static bool need_remote_flush(u64 old, u64 new)
2372{
2373 if (!is_shadow_present_pte(old))
2374 return false;
2375 if (!is_shadow_present_pte(new))
2376 return true;
2377 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2378 return true;
2379 old ^= PT64_NX_MASK;
2380 new ^= PT64_NX_MASK;
2381 return (old & ~new & PT64_PERM_MASK) != 0;
2382}
2383
2384static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2385{
2386 if (need_remote_flush(old, new))
2387 kvm_flush_remote_tlbs(vcpu->kvm);
2388 else
2389 kvm_mmu_flush_tlb(vcpu);
2390}
2391
Avi Kivity12b7d282007-09-23 14:10:49 +02002392static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2393{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002394 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002395
Sheng Yang7b523452008-04-25 21:13:50 +08002396 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002397}
2398
Avi Kivityd7824ff2007-12-30 12:29:05 +02002399static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2400 const u8 *new, int bytes)
2401{
2402 gfn_t gfn;
2403 int r;
2404 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002405 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002406
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002407 vcpu->arch.update_pte.largepage = 0;
2408
Avi Kivityd7824ff2007-12-30 12:29:05 +02002409 if (bytes != 4 && bytes != 8)
2410 return;
2411
2412 /*
2413 * Assume that the pte write on a page table of the same type
2414 * as the current vcpu paging mode. This is nearly always true
2415 * (might be false while changing modes). Note it is verified later
2416 * by update_pte().
2417 */
2418 if (is_pae(vcpu)) {
2419 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2420 if ((bytes == 4) && (gpa % 4 == 0)) {
2421 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2422 if (r)
2423 return;
2424 memcpy((void *)&gpte + (gpa % 8), new, 4);
2425 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2426 memcpy((void *)&gpte, new, 8);
2427 }
2428 } else {
2429 if ((bytes == 4) && (gpa % 4 == 0))
2430 memcpy((void *)&gpte, new, 4);
2431 }
2432 if (!is_present_pte(gpte))
2433 return;
2434 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002435
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002436 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2437 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2438 vcpu->arch.update_pte.largepage = 1;
2439 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002440 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002441 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002442 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002443
Anthony Liguori35149e22008-04-02 14:46:56 -05002444 if (is_error_pfn(pfn)) {
2445 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002446 return;
2447 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002448 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002449 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002450}
2451
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002452static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2453{
2454 u64 *spte = vcpu->arch.last_pte_updated;
2455
2456 if (spte
2457 && vcpu->arch.last_pte_gfn == gfn
2458 && shadow_accessed_mask
2459 && !(*spte & shadow_accessed_mask)
2460 && is_shadow_present_pte(*spte))
2461 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2462}
2463
Avi Kivity09072da2007-05-01 14:16:52 +03002464void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002465 const u8 *new, int bytes,
2466 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002467{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002468 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002469 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002470 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002471 struct hlist_head *bucket;
2472 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002473 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002474 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002475 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002476 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002477 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002478 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002479 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002480 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002481 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002482 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002483 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002484
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002485 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002486 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002487 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002488 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002489 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002490 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002491 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002492 if (guest_initiated) {
2493 if (gfn == vcpu->arch.last_pt_write_gfn
2494 && !last_updated_pte_accessed(vcpu)) {
2495 ++vcpu->arch.last_pt_write_count;
2496 if (vcpu->arch.last_pt_write_count >= 3)
2497 flooded = 1;
2498 } else {
2499 vcpu->arch.last_pt_write_gfn = gfn;
2500 vcpu->arch.last_pt_write_count = 1;
2501 vcpu->arch.last_pte_updated = NULL;
2502 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002503 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002504 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002505 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002506 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002507 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002508 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002509 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002510 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002511 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002512 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002513 /*
2514 * Misaligned accesses are too much trouble to fix
2515 * up; also, they usually indicate a page is not used
2516 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002517 *
2518 * If we're seeing too many writes to a page,
2519 * it may no longer be a page table, or we may be
2520 * forking, in which case it is better to unmap the
2521 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002522 */
2523 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002524 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002525 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2526 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002527 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002528 continue;
2529 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002530 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002531 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002532 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002533 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002534 page_offset <<= 1; /* 32->64 */
2535 /*
2536 * A 32-bit pde maps 4MB while the shadow pdes map
2537 * only 2MB. So we need to double the offset again
2538 * and zap two pdes instead of one.
2539 */
2540 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002541 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002542 page_offset <<= 1;
2543 npte = 2;
2544 }
Avi Kivityfce06572007-05-01 16:44:05 +03002545 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002546 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002547 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002548 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002549 }
Avi Kivity4db35312007-11-21 15:28:32 +02002550 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002551 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2552 gentry = 0;
2553 r = kvm_read_guest_atomic(vcpu->kvm,
2554 gpa & ~(u64)(pte_size - 1),
2555 &gentry, pte_size);
2556 new = (const void *)&gentry;
2557 if (r < 0)
2558 new = NULL;
2559 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002560 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002561 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002562 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002563 if (new)
2564 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002565 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002566 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002567 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002568 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002569 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002570 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002571 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2572 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2573 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002574 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002575}
2576
Avi Kivitya4360362007-01-05 16:36:45 -08002577int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2578{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002579 gpa_t gpa;
2580 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002581
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002582 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002583
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002584 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002585 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002586 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002587 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002588}
Avi Kivity577bdc42008-07-19 08:57:05 +03002589EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002590
Avi Kivity22d95b12007-09-14 20:26:06 +03002591void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002592{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002593 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002594 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002595
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002596 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002597 struct kvm_mmu_page, link);
2598 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002599 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002600 }
2601}
Avi Kivityebeace82007-01-05 16:36:47 -08002602
Avi Kivity30677142007-10-28 18:48:59 +02002603int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2604{
2605 int r;
2606 enum emulation_result er;
2607
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002608 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002609 if (r < 0)
2610 goto out;
2611
2612 if (!r) {
2613 r = 1;
2614 goto out;
2615 }
2616
Avi Kivityb733bfb2007-10-28 18:52:05 +02002617 r = mmu_topup_memory_caches(vcpu);
2618 if (r)
2619 goto out;
2620
Avi Kivity30677142007-10-28 18:48:59 +02002621 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002622
2623 switch (er) {
2624 case EMULATE_DONE:
2625 return 1;
2626 case EMULATE_DO_MMIO:
2627 ++vcpu->stat.mmio_exits;
2628 return 0;
2629 case EMULATE_FAIL:
2630 kvm_report_emulation_failure(vcpu, "pagetable");
2631 return 1;
2632 default:
2633 BUG();
2634 }
2635out:
Avi Kivity30677142007-10-28 18:48:59 +02002636 return r;
2637}
2638EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2639
Marcelo Tosattia7052892008-09-23 13:18:35 -03002640void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2641{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002642 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002643 kvm_mmu_flush_tlb(vcpu);
2644 ++vcpu->stat.invlpg;
2645}
2646EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2647
Joerg Roedel18552672008-02-07 13:47:41 +01002648void kvm_enable_tdp(void)
2649{
2650 tdp_enabled = true;
2651}
2652EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2653
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002654void kvm_disable_tdp(void)
2655{
2656 tdp_enabled = false;
2657}
2658EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2659
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660static void free_mmu_pages(struct kvm_vcpu *vcpu)
2661{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002662 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663}
2664
2665static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2666{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002667 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 int i;
2669
2670 ASSERT(vcpu);
2671
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002672 if (vcpu->kvm->arch.n_requested_mmu_pages)
2673 vcpu->kvm->arch.n_free_mmu_pages =
2674 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002675 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002676 vcpu->kvm->arch.n_free_mmu_pages =
2677 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002678 /*
2679 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2680 * Therefore we need to allocate shadow page tables in the first
2681 * 4GB of memory, which happens to fit the DMA32 zone.
2682 */
2683 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2684 if (!page)
2685 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002686 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002687 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002688 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002689
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 return 0;
2691
2692error_1:
2693 free_mmu_pages(vcpu);
2694 return -ENOMEM;
2695}
2696
Ingo Molnar8018c272006-12-29 16:50:01 -08002697int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002700 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701
Ingo Molnar8018c272006-12-29 16:50:01 -08002702 return alloc_mmu_pages(vcpu);
2703}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704
Ingo Molnar8018c272006-12-29 16:50:01 -08002705int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2706{
2707 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002708 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002709
Ingo Molnar8018c272006-12-29 16:50:01 -08002710 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711}
2712
2713void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2714{
2715 ASSERT(vcpu);
2716
2717 destroy_kvm_mmu(vcpu);
2718 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002719 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720}
2721
Avi Kivity90cb0522007-07-17 13:04:56 +03002722void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723{
Avi Kivity4db35312007-11-21 15:28:32 +02002724 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725
Avi Kivity2245a282008-08-27 16:32:24 +03002726 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002727 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 int i;
2729 u64 *pt;
2730
Sheng Yang291f26b2008-10-16 17:30:57 +08002731 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 continue;
2733
Avi Kivity4db35312007-11-21 15:28:32 +02002734 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2736 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002737 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 }
Avi Kivity171d5952008-08-27 16:40:51 +03002740 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002741 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002743
Avi Kivity90cb0522007-07-17 13:04:56 +03002744void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002745{
Avi Kivity4db35312007-11-21 15:28:32 +02002746 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002747
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002748 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002749 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002750 if (kvm_mmu_zap_page(kvm, sp))
2751 node = container_of(kvm->arch.active_mmu_pages.next,
2752 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002753 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002754
Avi Kivity90cb0522007-07-17 13:04:56 +03002755 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002756}
2757
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002758static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002759{
2760 struct kvm_mmu_page *page;
2761
2762 page = container_of(kvm->arch.active_mmu_pages.prev,
2763 struct kvm_mmu_page, link);
2764 kvm_mmu_zap_page(kvm, page);
2765}
2766
2767static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2768{
2769 struct kvm *kvm;
2770 struct kvm *kvm_freed = NULL;
2771 int cache_count = 0;
2772
2773 spin_lock(&kvm_lock);
2774
2775 list_for_each_entry(kvm, &vm_list, vm_list) {
2776 int npages;
2777
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002778 if (!down_read_trylock(&kvm->slots_lock))
2779 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002780 spin_lock(&kvm->mmu_lock);
2781 npages = kvm->arch.n_alloc_mmu_pages -
2782 kvm->arch.n_free_mmu_pages;
2783 cache_count += npages;
2784 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2785 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2786 cache_count--;
2787 kvm_freed = kvm;
2788 }
2789 nr_to_scan--;
2790
2791 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002792 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002793 }
2794 if (kvm_freed)
2795 list_move_tail(&kvm_freed->vm_list, &vm_list);
2796
2797 spin_unlock(&kvm_lock);
2798
2799 return cache_count;
2800}
2801
2802static struct shrinker mmu_shrinker = {
2803 .shrink = mmu_shrink,
2804 .seeks = DEFAULT_SEEKS * 10,
2805};
2806
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002807static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002808{
2809 if (pte_chain_cache)
2810 kmem_cache_destroy(pte_chain_cache);
2811 if (rmap_desc_cache)
2812 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002813 if (mmu_page_header_cache)
2814 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002815}
2816
Izik Eidus3ee16c82008-03-30 15:17:21 +03002817void kvm_mmu_module_exit(void)
2818{
2819 mmu_destroy_caches();
2820 unregister_shrinker(&mmu_shrinker);
2821}
2822
Avi Kivityb5a33a72007-04-15 16:31:09 +03002823int kvm_mmu_module_init(void)
2824{
2825 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2826 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002827 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002828 if (!pte_chain_cache)
2829 goto nomem;
2830 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2831 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002832 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002833 if (!rmap_desc_cache)
2834 goto nomem;
2835
Avi Kivityd3d25b02007-05-30 12:34:53 +03002836 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2837 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002838 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002839 if (!mmu_page_header_cache)
2840 goto nomem;
2841
Izik Eidus3ee16c82008-03-30 15:17:21 +03002842 register_shrinker(&mmu_shrinker);
2843
Avi Kivityb5a33a72007-04-15 16:31:09 +03002844 return 0;
2845
2846nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002847 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002848 return -ENOMEM;
2849}
2850
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002851/*
2852 * Caculate mmu pages needed for kvm.
2853 */
2854unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2855{
2856 int i;
2857 unsigned int nr_mmu_pages;
2858 unsigned int nr_pages = 0;
2859
2860 for (i = 0; i < kvm->nmemslots; i++)
2861 nr_pages += kvm->memslots[i].npages;
2862
2863 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2864 nr_mmu_pages = max(nr_mmu_pages,
2865 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2866
2867 return nr_mmu_pages;
2868}
2869
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002870static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2871 unsigned len)
2872{
2873 if (len > buffer->len)
2874 return NULL;
2875 return buffer->ptr;
2876}
2877
2878static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2879 unsigned len)
2880{
2881 void *ret;
2882
2883 ret = pv_mmu_peek_buffer(buffer, len);
2884 if (!ret)
2885 return ret;
2886 buffer->ptr += len;
2887 buffer->len -= len;
2888 buffer->processed += len;
2889 return ret;
2890}
2891
2892static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2893 gpa_t addr, gpa_t value)
2894{
2895 int bytes = 8;
2896 int r;
2897
2898 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2899 bytes = 4;
2900
2901 r = mmu_topup_memory_caches(vcpu);
2902 if (r)
2903 return r;
2904
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002905 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002906 return -EFAULT;
2907
2908 return 1;
2909}
2910
2911static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2912{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002913 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002914 return 1;
2915}
2916
2917static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2918{
2919 spin_lock(&vcpu->kvm->mmu_lock);
2920 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2921 spin_unlock(&vcpu->kvm->mmu_lock);
2922 return 1;
2923}
2924
2925static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2926 struct kvm_pv_mmu_op_buffer *buffer)
2927{
2928 struct kvm_mmu_op_header *header;
2929
2930 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2931 if (!header)
2932 return 0;
2933 switch (header->op) {
2934 case KVM_MMU_OP_WRITE_PTE: {
2935 struct kvm_mmu_op_write_pte *wpte;
2936
2937 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2938 if (!wpte)
2939 return 0;
2940 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2941 wpte->pte_val);
2942 }
2943 case KVM_MMU_OP_FLUSH_TLB: {
2944 struct kvm_mmu_op_flush_tlb *ftlb;
2945
2946 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2947 if (!ftlb)
2948 return 0;
2949 return kvm_pv_mmu_flush_tlb(vcpu);
2950 }
2951 case KVM_MMU_OP_RELEASE_PT: {
2952 struct kvm_mmu_op_release_pt *rpt;
2953
2954 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2955 if (!rpt)
2956 return 0;
2957 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2958 }
2959 default: return 0;
2960 }
2961}
2962
2963int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2964 gpa_t addr, unsigned long *ret)
2965{
2966 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002967 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002968
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002969 buffer->ptr = buffer->buf;
2970 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2971 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002972
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002973 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002974 if (r)
2975 goto out;
2976
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002977 while (buffer->len) {
2978 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002979 if (r < 0)
2980 goto out;
2981 if (r == 0)
2982 break;
2983 }
2984
2985 r = 1;
2986out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002987 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002988 return r;
2989}
2990
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002991#ifdef AUDIT
2992
2993static const char *audit_msg;
2994
2995static gva_t canonicalize(gva_t gva)
2996{
2997#ifdef CONFIG_X86_64
2998 gva = (long long)(gva << 16) >> 16;
2999#endif
3000 return gva;
3001}
3002
3003static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3004 gva_t va, int level)
3005{
3006 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3007 int i;
3008 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3009
3010 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3011 u64 ent = pt[i];
3012
Avi Kivityc7addb92007-09-16 18:58:32 +02003013 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003014 continue;
3015
3016 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003017 if (level > 1) {
3018 if (ent == shadow_notrap_nonpresent_pte)
3019 printk(KERN_ERR "audit: (%s) nontrapping pte"
3020 " in nonleaf level: levels %d gva %lx"
3021 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003022 vcpu->arch.mmu.root_level, va, level, ent);
Jan Kiszka34382532009-04-25 12:43:21 +02003023 else
3024 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003025 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003026 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003027 gfn_t gfn = gpa >> PAGE_SHIFT;
3028 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3029 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003030
Avi Kivityc7addb92007-09-16 18:58:32 +02003031 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003032 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003033 printk(KERN_ERR "xx audit error: (%s) levels %d"
3034 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003035 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003036 va, gpa, hpa, ent,
3037 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003038 else if (ent == shadow_notrap_nonpresent_pte
3039 && !is_error_hpa(hpa))
3040 printk(KERN_ERR "audit: (%s) notrap shadow,"
3041 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003042 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003043
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003044 }
3045 }
3046}
3047
3048static void audit_mappings(struct kvm_vcpu *vcpu)
3049{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003050 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003051
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003052 if (vcpu->arch.mmu.root_level == 4)
3053 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003054 else
3055 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003056 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003057 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003058 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003059 i << 30,
3060 2);
3061}
3062
3063static int count_rmaps(struct kvm_vcpu *vcpu)
3064{
3065 int nmaps = 0;
3066 int i, j, k;
3067
3068 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3069 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3070 struct kvm_rmap_desc *d;
3071
3072 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003073 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003074
Izik Eidus290fc382007-09-27 14:11:22 +02003075 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003076 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003077 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003078 ++nmaps;
3079 continue;
3080 }
Izik Eidus290fc382007-09-27 14:11:22 +02003081 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003082 while (d) {
3083 for (k = 0; k < RMAP_EXT; ++k)
3084 if (d->shadow_ptes[k])
3085 ++nmaps;
3086 else
3087 break;
3088 d = d->more;
3089 }
3090 }
3091 }
3092 return nmaps;
3093}
3094
3095static int count_writable_mappings(struct kvm_vcpu *vcpu)
3096{
3097 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003098 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003099 int i;
3100
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003101 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003102 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003103
Avi Kivity4db35312007-11-21 15:28:32 +02003104 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003105 continue;
3106
3107 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3108 u64 ent = pt[i];
3109
3110 if (!(ent & PT_PRESENT_MASK))
3111 continue;
3112 if (!(ent & PT_WRITABLE_MASK))
3113 continue;
3114 ++nmaps;
3115 }
3116 }
3117 return nmaps;
3118}
3119
3120static void audit_rmap(struct kvm_vcpu *vcpu)
3121{
3122 int n_rmap = count_rmaps(vcpu);
3123 int n_actual = count_writable_mappings(vcpu);
3124
3125 if (n_rmap != n_actual)
3126 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003127 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003128}
3129
3130static void audit_write_protection(struct kvm_vcpu *vcpu)
3131{
Avi Kivity4db35312007-11-21 15:28:32 +02003132 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003133 struct kvm_memory_slot *slot;
3134 unsigned long *rmapp;
3135 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003136
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003137 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003138 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003139 continue;
3140
Avi Kivity4db35312007-11-21 15:28:32 +02003141 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003142 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003143 rmapp = &slot->rmap[gfn - slot->base_gfn];
3144 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003145 printk(KERN_ERR "%s: (%s) shadow page has writable"
3146 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003147 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003148 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003149 }
3150}
3151
3152static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3153{
3154 int olddbg = dbg;
3155
3156 dbg = 0;
3157 audit_msg = msg;
3158 audit_rmap(vcpu);
3159 audit_write_protection(vcpu);
3160 audit_mappings(vcpu);
3161 dbg = olddbg;
3162}
3163
3164#endif