blob: b39ec626040e2ea8cdb4dda0a23f38b45469ddc4 [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;
Sheng Yang64d4d522008-10-09 16:01:57 +0800181static u64 __read_mostly shadow_mt_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200182
Dong, Eddie82725b22009-03-30 16:21:08 +0800183static inline u64 rsvd_bits(int s, int e)
184{
185 return ((1ULL << (e - s + 1)) - 1) << s;
186}
187
Avi Kivityc7addb92007-09-16 18:58:32 +0200188void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
189{
190 shadow_trap_nonpresent_pte = trap_pte;
191 shadow_notrap_nonpresent_pte = notrap_pte;
192}
193EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
194
Sheng Yang7b523452008-04-25 21:13:50 +0800195void kvm_mmu_set_base_ptes(u64 base_pte)
196{
197 shadow_base_present_pte = base_pte;
198}
199EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
200
201void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang64d4d522008-10-09 16:01:57 +0800202 u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800203{
204 shadow_user_mask = user_mask;
205 shadow_accessed_mask = accessed_mask;
206 shadow_dirty_mask = dirty_mask;
207 shadow_nx_mask = nx_mask;
208 shadow_x_mask = x_mask;
Sheng Yang64d4d522008-10-09 16:01:57 +0800209 shadow_mt_mask = mt_mask;
Sheng Yang7b523452008-04-25 21:13:50 +0800210}
211EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
212
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213static int is_write_protection(struct kvm_vcpu *vcpu)
214{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800215 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216}
217
218static int is_cpuid_PSE36(void)
219{
220 return 1;
221}
222
Avi Kivity73b10872007-01-26 00:56:41 -0800223static int is_nx(struct kvm_vcpu *vcpu)
224{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800225 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800226}
227
Avi Kivityc7addb92007-09-16 18:58:32 +0200228static int is_shadow_present_pte(u64 pte)
229{
Avi Kivityc7addb92007-09-16 18:58:32 +0200230 return pte != shadow_trap_nonpresent_pte
231 && pte != shadow_notrap_nonpresent_pte;
232}
233
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300234static int is_large_pte(u64 pte)
235{
236 return pte & PT_PAGE_SIZE_MASK;
237}
238
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239static int is_writeble_pte(unsigned long pte)
240{
241 return pte & PT_WRITABLE_MASK;
242}
243
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200244static int is_dirty_pte(unsigned long pte)
245{
Sheng Yang7b523452008-04-25 21:13:50 +0800246 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200247}
248
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800249static int is_rmap_pte(u64 pte)
250{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200251 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800252}
253
Anthony Liguori35149e22008-04-02 14:46:56 -0500254static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200255{
Anthony Liguori35149e22008-04-02 14:46:56 -0500256 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200257}
258
Avi Kivityda9285212007-11-21 13:54:47 +0200259static gfn_t pse36_gfn_delta(u32 gpte)
260{
261 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
262
263 return (gpte & PT32_DIR_PSE36_MASK) << shift;
264}
265
Avi Kivitye663ee62007-05-31 15:46:04 +0300266static void set_shadow_pte(u64 *sptep, u64 spte)
267{
268#ifdef CONFIG_X86_64
269 set_64bit((unsigned long *)sptep, spte);
270#else
271 set_64bit((unsigned long long *)sptep, spte);
272#endif
273}
274
Avi Kivitye2dec932007-01-05 16:36:54 -0800275static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300276 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800277{
278 void *obj;
279
280 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800281 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800282 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300283 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800284 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800285 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800286 cache->objects[cache->nobjs++] = obj;
287 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800288 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800289}
290
291static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
292{
293 while (mc->nobjs)
294 kfree(mc->objects[--mc->nobjs]);
295}
296
Avi Kivityc1158e62007-07-20 08:18:27 +0300297static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300298 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300299{
300 struct page *page;
301
302 if (cache->nobjs >= min)
303 return 0;
304 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300305 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300306 if (!page)
307 return -ENOMEM;
308 set_page_private(page, 0);
309 cache->objects[cache->nobjs++] = page_address(page);
310 }
311 return 0;
312}
313
314static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
315{
316 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300317 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300318}
319
Avi Kivity8c438502007-04-16 11:53:17 +0300320static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
321{
322 int r;
323
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800324 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300325 pte_chain_cache, 4);
326 if (r)
327 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800328 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200329 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300330 if (r)
331 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800332 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300333 if (r)
334 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800335 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300336 mmu_page_header_cache, 4);
337out:
Avi Kivity8c438502007-04-16 11:53:17 +0300338 return r;
339}
340
Avi Kivity714b93d2007-01-05 16:36:53 -0800341static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
342{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
344 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
345 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
346 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800347}
348
349static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
350 size_t size)
351{
352 void *p;
353
354 BUG_ON(!mc->nobjs);
355 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800356 return p;
357}
358
Avi Kivity714b93d2007-01-05 16:36:53 -0800359static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
360{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800361 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 sizeof(struct kvm_pte_chain));
363}
364
Avi Kivity90cb0522007-07-17 13:04:56 +0300365static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800366{
Avi Kivity90cb0522007-07-17 13:04:56 +0300367 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800368}
369
370static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
371{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800372 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800373 sizeof(struct kvm_rmap_desc));
374}
375
Avi Kivity90cb0522007-07-17 13:04:56 +0300376static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800377{
Avi Kivity90cb0522007-07-17 13:04:56 +0300378 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800379}
380
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800381/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300382 * Return the pointer to the largepage write count for a given
383 * gfn, handling slots that are not large page aligned.
384 */
385static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
386{
387 unsigned long idx;
388
389 idx = (gfn / KVM_PAGES_PER_HPAGE) -
390 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
391 return &slot->lpage_info[idx].write_count;
392}
393
394static void account_shadowed(struct kvm *kvm, gfn_t gfn)
395{
396 int *write_count;
397
Izik Eidus28430992008-10-03 17:40:32 +0300398 gfn = unalias_gfn(kvm, gfn);
399 write_count = slot_largepage_idx(gfn,
400 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300401 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300402}
403
404static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
405{
406 int *write_count;
407
Izik Eidus28430992008-10-03 17:40:32 +0300408 gfn = unalias_gfn(kvm, gfn);
409 write_count = slot_largepage_idx(gfn,
410 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300411 *write_count -= 1;
412 WARN_ON(*write_count < 0);
413}
414
415static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
416{
Izik Eidus28430992008-10-03 17:40:32 +0300417 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418 int *largepage_idx;
419
Izik Eidus28430992008-10-03 17:40:32 +0300420 gfn = unalias_gfn(kvm, gfn);
421 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300422 if (slot) {
423 largepage_idx = slot_largepage_idx(gfn, slot);
424 return *largepage_idx;
425 }
426
427 return 1;
428}
429
430static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
431{
432 struct vm_area_struct *vma;
433 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300434 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300435
436 addr = gfn_to_hva(kvm, gfn);
437 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300438 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300440 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441 vma = find_vma(current->mm, addr);
442 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300443 ret = 1;
444 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300446 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300447}
448
449static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
450{
451 struct kvm_memory_slot *slot;
452
453 if (has_wrprotected_page(vcpu->kvm, large_gfn))
454 return 0;
455
456 if (!host_largepage_backed(vcpu->kvm, large_gfn))
457 return 0;
458
459 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
460 if (slot && slot->dirty_bitmap)
461 return 0;
462
463 return 1;
464}
465
466/*
Izik Eidus290fc382007-09-27 14:11:22 +0200467 * Take gfn and return the reverse mapping to it.
468 * Note: gfn must be unaliased before this function get called
469 */
470
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300471static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200472{
473 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200475
476 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477 if (!lpage)
478 return &slot->rmap[gfn - slot->base_gfn];
479
480 idx = (gfn / KVM_PAGES_PER_HPAGE) -
481 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
482
483 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200484}
485
486/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800487 * Reverse mapping data structures:
488 *
Izik Eidus290fc382007-09-27 14:11:22 +0200489 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
490 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 *
Izik Eidus290fc382007-09-27 14:11:22 +0200492 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
493 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300495static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496{
Avi Kivity4db35312007-11-21 15:28:32 +0200497 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200499 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 int i;
501
502 if (!is_rmap_pte(*spte))
503 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200504 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200505 sp = page_header(__pa(spte));
506 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300507 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200508 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800509 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200510 *rmapp = (unsigned long)spte;
511 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800512 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800513 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200514 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800515 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200516 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800517 } else {
518 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200519 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800520 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
521 desc = desc->more;
522 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800523 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 desc = desc->more;
525 }
526 for (i = 0; desc->shadow_ptes[i]; ++i)
527 ;
528 desc->shadow_ptes[i] = spte;
529 }
530}
531
Izik Eidus290fc382007-09-27 14:11:22 +0200532static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 struct kvm_rmap_desc *desc,
534 int i,
535 struct kvm_rmap_desc *prev_desc)
536{
537 int j;
538
539 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
540 ;
541 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000542 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543 if (j != 0)
544 return;
545 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200546 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 else
548 if (prev_desc)
549 prev_desc->more = desc->more;
550 else
Izik Eidus290fc382007-09-27 14:11:22 +0200551 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300552 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553}
554
Izik Eidus290fc382007-09-27 14:11:22 +0200555static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 struct kvm_rmap_desc *desc;
558 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200559 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500560 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200561 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 int i;
563
564 if (!is_rmap_pte(*spte))
565 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200566 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500567 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800568 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500569 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200570 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500571 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200572 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500573 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300574 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200575 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800576 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
577 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200578 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800579 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200580 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
582 spte, *spte);
583 BUG();
584 }
Izik Eidus290fc382007-09-27 14:11:22 +0200585 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 } else {
587 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200588 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 prev_desc = NULL;
590 while (desc) {
591 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
592 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200593 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800594 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800595 prev_desc);
596 return;
597 }
598 prev_desc = desc;
599 desc = desc->more;
600 }
601 BUG();
602 }
603}
604
Izik Eidus98348e92007-10-16 14:42:30 +0200605static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800606{
Avi Kivity374cbac2007-01-05 16:36:43 -0800607 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200608 struct kvm_rmap_desc *prev_desc;
609 u64 *prev_spte;
610 int i;
611
612 if (!*rmapp)
613 return NULL;
614 else if (!(*rmapp & 1)) {
615 if (!spte)
616 return (u64 *)*rmapp;
617 return NULL;
618 }
619 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
620 prev_desc = NULL;
621 prev_spte = NULL;
622 while (desc) {
623 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
624 if (prev_spte == spte)
625 return desc->shadow_ptes[i];
626 prev_spte = desc->shadow_ptes[i];
627 }
628 desc = desc->more;
629 }
630 return NULL;
631}
632
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200633static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200634{
Izik Eidus290fc382007-09-27 14:11:22 +0200635 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800636 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800637 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800638
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500639 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300640 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800641
Izik Eidus98348e92007-10-16 14:42:30 +0200642 spte = rmap_next(kvm, rmapp, NULL);
643 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800644 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800645 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800646 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800647 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200648 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800649 write_protected = 1;
650 }
Izik Eidus9647c142007-10-16 14:43:46 +0200651 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800652 }
Izik Eidus855149a2008-03-20 18:17:24 +0200653 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500654 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200655
656 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500657 pfn = spte_to_pfn(*spte);
658 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200659 }
660
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300661 /* check for huge page mappings */
662 rmapp = gfn_to_rmap(kvm, gfn, 1);
663 spte = rmap_next(kvm, rmapp, NULL);
664 while (spte) {
665 BUG_ON(!spte);
666 BUG_ON(!(*spte & PT_PRESENT_MASK));
667 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
668 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
669 if (is_writeble_pte(*spte)) {
670 rmap_remove(kvm, spte);
671 --kvm->stat.lpages;
672 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300673 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300674 write_protected = 1;
675 }
676 spte = rmap_next(kvm, rmapp, spte);
677 }
678
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200679 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800680}
681
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200682static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
683{
684 u64 *spte;
685 int need_tlb_flush = 0;
686
687 while ((spte = rmap_next(kvm, rmapp, NULL))) {
688 BUG_ON(!(*spte & PT_PRESENT_MASK));
689 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
690 rmap_remove(kvm, spte);
691 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
692 need_tlb_flush = 1;
693 }
694 return need_tlb_flush;
695}
696
697static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
698 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
699{
700 int i;
701 int retval = 0;
702
703 /*
704 * If mmap_sem isn't taken, we can look the memslots with only
705 * the mmu_lock by skipping over the slots with userspace_addr == 0.
706 */
707 for (i = 0; i < kvm->nmemslots; i++) {
708 struct kvm_memory_slot *memslot = &kvm->memslots[i];
709 unsigned long start = memslot->userspace_addr;
710 unsigned long end;
711
712 /* mmu_lock protects userspace_addr */
713 if (!start)
714 continue;
715
716 end = start + (memslot->npages << PAGE_SHIFT);
717 if (hva >= start && hva < end) {
718 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
719 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
720 retval |= handler(kvm,
721 &memslot->lpage_info[
722 gfn_offset /
723 KVM_PAGES_PER_HPAGE].rmap_pde);
724 }
725 }
726
727 return retval;
728}
729
730int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
731{
732 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
733}
734
735static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
736{
737 u64 *spte;
738 int young = 0;
739
Sheng Yang534e38b2008-09-08 15:12:30 +0800740 /* always return old for EPT */
741 if (!shadow_accessed_mask)
742 return 0;
743
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200744 spte = rmap_next(kvm, rmapp, NULL);
745 while (spte) {
746 int _young;
747 u64 _spte = *spte;
748 BUG_ON(!(_spte & PT_PRESENT_MASK));
749 _young = _spte & PT_ACCESSED_MASK;
750 if (_young) {
751 young = 1;
752 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
753 }
754 spte = rmap_next(kvm, rmapp, spte);
755 }
756 return young;
757}
758
759int kvm_age_hva(struct kvm *kvm, unsigned long hva)
760{
761 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
762}
763
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800764#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300765static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766{
Avi Kivity139bdb22007-01-05 16:36:50 -0800767 u64 *pos;
768 u64 *end;
769
Avi Kivity47ad8e62007-05-06 15:50:58 +0300770 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300771 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800772 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800773 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800775 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 return 1;
777}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800778#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779
Avi Kivity4db35312007-11-21 15:28:32 +0200780static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800781{
Avi Kivity4db35312007-11-21 15:28:32 +0200782 ASSERT(is_empty_shadow_page(sp->spt));
783 list_del(&sp->link);
784 __free_page(virt_to_page(sp->spt));
785 __free_page(virt_to_page(sp->gfns));
786 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800787 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800788}
789
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800790static unsigned kvm_page_table_hashfn(gfn_t gfn)
791{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200792 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800793}
794
Avi Kivity25c0de22007-01-05 16:36:42 -0800795static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
796 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797{
Avi Kivity4db35312007-11-21 15:28:32 +0200798 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800800 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
801 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
802 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200803 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800804 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200805 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800806 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200807 sp->multimapped = 0;
808 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800809 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200810 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811}
812
Avi Kivity714b93d2007-01-05 16:36:53 -0800813static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200814 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800815{
816 struct kvm_pte_chain *pte_chain;
817 struct hlist_node *node;
818 int i;
819
820 if (!parent_pte)
821 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200822 if (!sp->multimapped) {
823 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800824
825 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200826 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800827 return;
828 }
Avi Kivity4db35312007-11-21 15:28:32 +0200829 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800830 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200831 INIT_HLIST_HEAD(&sp->parent_ptes);
832 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800833 pte_chain->parent_ptes[0] = old;
834 }
Avi Kivity4db35312007-11-21 15:28:32 +0200835 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800836 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
837 continue;
838 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
839 if (!pte_chain->parent_ptes[i]) {
840 pte_chain->parent_ptes[i] = parent_pte;
841 return;
842 }
843 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800844 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200846 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800847 pte_chain->parent_ptes[0] = parent_pte;
848}
849
Avi Kivity4db35312007-11-21 15:28:32 +0200850static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800851 u64 *parent_pte)
852{
853 struct kvm_pte_chain *pte_chain;
854 struct hlist_node *node;
855 int i;
856
Avi Kivity4db35312007-11-21 15:28:32 +0200857 if (!sp->multimapped) {
858 BUG_ON(sp->parent_pte != parent_pte);
859 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800860 return;
861 }
Avi Kivity4db35312007-11-21 15:28:32 +0200862 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800863 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
864 if (!pte_chain->parent_ptes[i])
865 break;
866 if (pte_chain->parent_ptes[i] != parent_pte)
867 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800868 while (i + 1 < NR_PTE_CHAIN_ENTRIES
869 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800870 pte_chain->parent_ptes[i]
871 = pte_chain->parent_ptes[i + 1];
872 ++i;
873 }
874 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800875 if (i == 0) {
876 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300877 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200878 if (hlist_empty(&sp->parent_ptes)) {
879 sp->multimapped = 0;
880 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800881 }
882 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800883 return;
884 }
885 BUG();
886}
887
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300888
889static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
890 mmu_parent_walk_fn fn)
891{
892 struct kvm_pte_chain *pte_chain;
893 struct hlist_node *node;
894 struct kvm_mmu_page *parent_sp;
895 int i;
896
897 if (!sp->multimapped && sp->parent_pte) {
898 parent_sp = page_header(__pa(sp->parent_pte));
899 fn(vcpu, parent_sp);
900 mmu_parent_walk(vcpu, parent_sp, fn);
901 return;
902 }
903 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
904 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
905 if (!pte_chain->parent_ptes[i])
906 break;
907 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
908 fn(vcpu, parent_sp);
909 mmu_parent_walk(vcpu, parent_sp, fn);
910 }
911}
912
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300913static void kvm_mmu_update_unsync_bitmap(u64 *spte)
914{
915 unsigned int index;
916 struct kvm_mmu_page *sp = page_header(__pa(spte));
917
918 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200919 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
920 sp->unsync_children++;
921 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300922}
923
924static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
925{
926 struct kvm_pte_chain *pte_chain;
927 struct hlist_node *node;
928 int i;
929
930 if (!sp->parent_pte)
931 return;
932
933 if (!sp->multimapped) {
934 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
935 return;
936 }
937
938 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
939 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
940 if (!pte_chain->parent_ptes[i])
941 break;
942 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
943 }
944}
945
946static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
947{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300948 kvm_mmu_update_parents_unsync(sp);
949 return 1;
950}
951
952static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
953 struct kvm_mmu_page *sp)
954{
955 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
956 kvm_mmu_update_parents_unsync(sp);
957}
958
Avi Kivityd761a502008-05-29 14:55:03 +0300959static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
960 struct kvm_mmu_page *sp)
961{
962 int i;
963
964 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
965 sp->spt[i] = shadow_trap_nonpresent_pte;
966}
967
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300968static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
969 struct kvm_mmu_page *sp)
970{
971 return 1;
972}
973
Marcelo Tosattia7052892008-09-23 13:18:35 -0300974static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
975{
976}
977
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200978#define KVM_PAGE_ARRAY_NR 16
979
980struct kvm_mmu_pages {
981 struct mmu_page_and_offset {
982 struct kvm_mmu_page *sp;
983 unsigned int idx;
984 } page[KVM_PAGE_ARRAY_NR];
985 unsigned int nr;
986};
987
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300988#define for_each_unsync_children(bitmap, idx) \
989 for (idx = find_first_bit(bitmap, 512); \
990 idx < 512; \
991 idx = find_next_bit(bitmap, 512, idx+1))
992
Hannes Edercded19f2009-02-21 02:19:13 +0100993static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
994 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300995{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200996 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300997
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200998 if (sp->unsync)
999 for (i=0; i < pvec->nr; i++)
1000 if (pvec->page[i].sp == sp)
1001 return 0;
1002
1003 pvec->page[pvec->nr].sp = sp;
1004 pvec->page[pvec->nr].idx = idx;
1005 pvec->nr++;
1006 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1007}
1008
1009static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1010 struct kvm_mmu_pages *pvec)
1011{
1012 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001013
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001014 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001015 u64 ent = sp->spt[i];
1016
Marcelo Tosatti87917232008-12-22 18:49:30 -02001017 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001018 struct kvm_mmu_page *child;
1019 child = page_header(ent & PT64_BASE_ADDR_MASK);
1020
1021 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001022 if (mmu_pages_add(pvec, child, i))
1023 return -ENOSPC;
1024
1025 ret = __mmu_unsync_walk(child, pvec);
1026 if (!ret)
1027 __clear_bit(i, sp->unsync_child_bitmap);
1028 else if (ret > 0)
1029 nr_unsync_leaf += ret;
1030 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001031 return ret;
1032 }
1033
1034 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001035 nr_unsync_leaf++;
1036 if (mmu_pages_add(pvec, child, i))
1037 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001038 }
1039 }
1040 }
1041
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001042 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001043 sp->unsync_children = 0;
1044
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001045 return nr_unsync_leaf;
1046}
1047
1048static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1049 struct kvm_mmu_pages *pvec)
1050{
1051 if (!sp->unsync_children)
1052 return 0;
1053
1054 mmu_pages_add(pvec, sp, 0);
1055 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001056}
1057
Avi Kivity4db35312007-11-21 15:28:32 +02001058static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001059{
1060 unsigned index;
1061 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001062 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001063 struct hlist_node *node;
1064
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001065 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001066 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001067 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001068 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001069 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001070 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001071 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001072 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001073 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001074 }
1075 return NULL;
1076}
1077
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001078static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1079{
1080 WARN_ON(!sp->unsync);
1081 sp->unsync = 0;
1082 --kvm->stat.mmu_unsync;
1083}
1084
1085static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1086
1087static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1088{
1089 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1090 kvm_mmu_zap_page(vcpu->kvm, sp);
1091 return 1;
1092 }
1093
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001094 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1095 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001096 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001097 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1098 kvm_mmu_zap_page(vcpu->kvm, sp);
1099 return 1;
1100 }
1101
1102 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001103 return 0;
1104}
1105
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001106struct mmu_page_path {
1107 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1108 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001109};
1110
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111#define for_each_sp(pvec, sp, parents, i) \
1112 for (i = mmu_pages_next(&pvec, &parents, -1), \
1113 sp = pvec.page[i].sp; \
1114 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1115 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001116
Hannes Edercded19f2009-02-21 02:19:13 +01001117static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1118 struct mmu_page_path *parents,
1119 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001120{
1121 int n;
1122
1123 for (n = i+1; n < pvec->nr; n++) {
1124 struct kvm_mmu_page *sp = pvec->page[n].sp;
1125
1126 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1127 parents->idx[0] = pvec->page[n].idx;
1128 return n;
1129 }
1130
1131 parents->parent[sp->role.level-2] = sp;
1132 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1133 }
1134
1135 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001136}
1137
Hannes Edercded19f2009-02-21 02:19:13 +01001138static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001139{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001140 struct kvm_mmu_page *sp;
1141 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001142
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001143 do {
1144 unsigned int idx = parents->idx[level];
1145
1146 sp = parents->parent[level];
1147 if (!sp)
1148 return;
1149
1150 --sp->unsync_children;
1151 WARN_ON((int)sp->unsync_children < 0);
1152 __clear_bit(idx, sp->unsync_child_bitmap);
1153 level++;
1154 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1155}
1156
1157static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1158 struct mmu_page_path *parents,
1159 struct kvm_mmu_pages *pvec)
1160{
1161 parents->parent[parent->role.level-1] = NULL;
1162 pvec->nr = 0;
1163}
1164
1165static void mmu_sync_children(struct kvm_vcpu *vcpu,
1166 struct kvm_mmu_page *parent)
1167{
1168 int i;
1169 struct kvm_mmu_page *sp;
1170 struct mmu_page_path parents;
1171 struct kvm_mmu_pages pages;
1172
1173 kvm_mmu_pages_init(parent, &parents, &pages);
1174 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001175 int protected = 0;
1176
1177 for_each_sp(pages, sp, parents, i)
1178 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1179
1180 if (protected)
1181 kvm_flush_remote_tlbs(vcpu->kvm);
1182
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001183 for_each_sp(pages, sp, parents, i) {
1184 kvm_sync_page(vcpu, sp);
1185 mmu_pages_clear_parents(&parents);
1186 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001187 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001188 kvm_mmu_pages_init(parent, &parents, &pages);
1189 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001190}
1191
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001192static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1193 gfn_t gfn,
1194 gva_t gaddr,
1195 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001196 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001197 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001198 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001199{
1200 union kvm_mmu_page_role role;
1201 unsigned index;
1202 unsigned quadrant;
1203 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001204 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001205 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001206
Avi Kivitya770f6f2008-12-21 19:20:09 +02001207 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001208 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001209 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001210 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001211 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001212 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1213 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1214 role.quadrant = quadrant;
1215 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001216 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001217 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001218 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001219 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001220 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1221 if (sp->gfn == gfn) {
1222 if (sp->unsync)
1223 if (kvm_sync_page(vcpu, sp))
1224 continue;
1225
1226 if (sp->role.word != role.word)
1227 continue;
1228
Avi Kivity4db35312007-11-21 15:28:32 +02001229 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001230 if (sp->unsync_children) {
1231 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1232 kvm_mmu_mark_parents_unsync(vcpu, sp);
1233 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001234 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001235 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001236 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001237 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001238 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1239 if (!sp)
1240 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001241 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001242 sp->gfn = gfn;
1243 sp->role = role;
1244 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001245 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001246 if (rmap_write_protect(vcpu->kvm, gfn))
1247 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248 account_shadowed(vcpu->kvm, gfn);
1249 }
Avi Kivity131d8272008-05-29 14:56:28 +03001250 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1251 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1252 else
1253 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001254 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001255}
1256
Avi Kivity2d111232008-12-25 14:39:47 +02001257static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1258 struct kvm_vcpu *vcpu, u64 addr)
1259{
1260 iterator->addr = addr;
1261 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1262 iterator->level = vcpu->arch.mmu.shadow_root_level;
1263 if (iterator->level == PT32E_ROOT_LEVEL) {
1264 iterator->shadow_addr
1265 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1266 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1267 --iterator->level;
1268 if (!iterator->shadow_addr)
1269 iterator->level = 0;
1270 }
1271}
1272
1273static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1274{
1275 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1276 return false;
1277 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1278 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1279 return true;
1280}
1281
1282static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1283{
1284 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1285 --iterator->level;
1286}
1287
Avi Kivity90cb0522007-07-17 13:04:56 +03001288static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001289 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001290{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001291 unsigned i;
1292 u64 *pt;
1293 u64 ent;
1294
Avi Kivity4db35312007-11-21 15:28:32 +02001295 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001296
Avi Kivity4db35312007-11-21 15:28:32 +02001297 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001298 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001299 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001300 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001301 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001302 }
1303 return;
1304 }
1305
1306 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1307 ent = pt[i];
1308
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001309 if (is_shadow_present_pte(ent)) {
1310 if (!is_large_pte(ent)) {
1311 ent &= PT64_BASE_ADDR_MASK;
1312 mmu_page_remove_parent_pte(page_header(ent),
1313 &pt[i]);
1314 } else {
1315 --kvm->stat.lpages;
1316 rmap_remove(kvm, &pt[i]);
1317 }
1318 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001319 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001320 }
Avi Kivitya4360362007-01-05 16:36:45 -08001321}
1322
Avi Kivity4db35312007-11-21 15:28:32 +02001323static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001324{
Avi Kivity4db35312007-11-21 15:28:32 +02001325 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001326}
1327
Avi Kivity12b7d282007-09-23 14:10:49 +02001328static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1329{
1330 int i;
1331
1332 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1333 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001334 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001335}
1336
Avi Kivity31aa2b42008-07-11 17:59:46 +03001337static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001338{
1339 u64 *parent_pte;
1340
Avi Kivity4db35312007-11-21 15:28:32 +02001341 while (sp->multimapped || sp->parent_pte) {
1342 if (!sp->multimapped)
1343 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001344 else {
1345 struct kvm_pte_chain *chain;
1346
Avi Kivity4db35312007-11-21 15:28:32 +02001347 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001348 struct kvm_pte_chain, link);
1349 parent_pte = chain->parent_ptes[0];
1350 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001351 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001352 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001353 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001354 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001355}
1356
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001357static int mmu_zap_unsync_children(struct kvm *kvm,
1358 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001359{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001360 int i, zapped = 0;
1361 struct mmu_page_path parents;
1362 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001363
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001364 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001365 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001366
1367 kvm_mmu_pages_init(parent, &parents, &pages);
1368 while (mmu_unsync_walk(parent, &pages)) {
1369 struct kvm_mmu_page *sp;
1370
1371 for_each_sp(pages, sp, parents, i) {
1372 kvm_mmu_zap_page(kvm, sp);
1373 mmu_pages_clear_parents(&parents);
1374 }
1375 zapped += pages.nr;
1376 kvm_mmu_pages_init(parent, &parents, &pages);
1377 }
1378
1379 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001380}
1381
Marcelo Tosatti07385412008-09-23 13:18:37 -03001382static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001383{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001384 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001385 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001386 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001387 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001388 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001389 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001390 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001391 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001392 if (sp->unsync)
1393 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001394 if (!sp->root_count) {
1395 hlist_del(&sp->hash_link);
1396 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001397 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001398 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001399 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001400 kvm_reload_remote_mmus(kvm);
1401 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001402 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001403 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001404}
1405
Izik Eidus82ce2c92007-10-02 18:52:55 +02001406/*
1407 * Changing the number of mmu pages allocated to the vm
1408 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1409 */
1410void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1411{
1412 /*
1413 * If we set the number of mmu pages to be smaller be than the
1414 * number of actived pages , we must to free some mmu pages before we
1415 * change the value
1416 */
1417
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001418 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001419 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001420 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1421 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001422
1423 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1424 struct kvm_mmu_page *page;
1425
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001426 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001427 struct kvm_mmu_page, link);
1428 kvm_mmu_zap_page(kvm, page);
1429 n_used_mmu_pages--;
1430 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001431 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001432 }
1433 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001434 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1435 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001436
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001437 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001438}
1439
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001440static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001441{
1442 unsigned index;
1443 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001444 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001445 struct hlist_node *node, *n;
1446 int r;
1447
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001448 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001449 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001450 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001451 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001452 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001453 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001454 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001455 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001456 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001457 if (kvm_mmu_zap_page(kvm, sp))
1458 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001459 }
1460 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001461}
1462
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001463static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001464{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001465 unsigned index;
1466 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001467 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001468 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001469
Avi Kivity4677a3b2009-01-06 13:00:27 +02001470 index = kvm_page_table_hashfn(gfn);
1471 bucket = &kvm->arch.mmu_page_hash[index];
1472 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001473 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001474 && !sp->role.invalid) {
1475 pgprintk("%s: zap %lx %x\n",
1476 __func__, gfn, sp->role.word);
1477 kvm_mmu_zap_page(kvm, sp);
1478 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001479 }
1480}
1481
Avi Kivity38c335f2007-11-21 14:20:22 +02001482static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483{
Avi Kivity38c335f2007-11-21 14:20:22 +02001484 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001485 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
Sheng Yang291f26b2008-10-16 17:30:57 +08001487 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001490static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1491{
1492 int i;
1493 u64 *pt = sp->spt;
1494
1495 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1496 return;
1497
1498 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1499 if (pt[i] == shadow_notrap_nonpresent_pte)
1500 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1501 }
1502}
1503
Avi Kivity039576c2007-03-20 12:46:50 +02001504struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1505{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001506 struct page *page;
1507
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001508 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001509
1510 if (gpa == UNMAPPED_GVA)
1511 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001512
Izik Eidus72dc67a2008-02-10 18:04:15 +02001513 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001514
1515 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001516}
1517
Sheng Yang74be52e2008-10-09 16:01:56 +08001518/*
1519 * The function is based on mtrr_type_lookup() in
1520 * arch/x86/kernel/cpu/mtrr/generic.c
1521 */
1522static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1523 u64 start, u64 end)
1524{
1525 int i;
1526 u64 base, mask;
1527 u8 prev_match, curr_match;
1528 int num_var_ranges = KVM_NR_VAR_MTRR;
1529
1530 if (!mtrr_state->enabled)
1531 return 0xFF;
1532
1533 /* Make end inclusive end, instead of exclusive */
1534 end--;
1535
1536 /* Look in fixed ranges. Just return the type as per start */
1537 if (mtrr_state->have_fixed && (start < 0x100000)) {
1538 int idx;
1539
1540 if (start < 0x80000) {
1541 idx = 0;
1542 idx += (start >> 16);
1543 return mtrr_state->fixed_ranges[idx];
1544 } else if (start < 0xC0000) {
1545 idx = 1 * 8;
1546 idx += ((start - 0x80000) >> 14);
1547 return mtrr_state->fixed_ranges[idx];
1548 } else if (start < 0x1000000) {
1549 idx = 3 * 8;
1550 idx += ((start - 0xC0000) >> 12);
1551 return mtrr_state->fixed_ranges[idx];
1552 }
1553 }
1554
1555 /*
1556 * Look in variable ranges
1557 * Look of multiple ranges matching this address and pick type
1558 * as per MTRR precedence
1559 */
1560 if (!(mtrr_state->enabled & 2))
1561 return mtrr_state->def_type;
1562
1563 prev_match = 0xFF;
1564 for (i = 0; i < num_var_ranges; ++i) {
1565 unsigned short start_state, end_state;
1566
1567 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1568 continue;
1569
1570 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1571 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1572 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1573 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1574
1575 start_state = ((start & mask) == (base & mask));
1576 end_state = ((end & mask) == (base & mask));
1577 if (start_state != end_state)
1578 return 0xFE;
1579
1580 if ((start & mask) != (base & mask))
1581 continue;
1582
1583 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1584 if (prev_match == 0xFF) {
1585 prev_match = curr_match;
1586 continue;
1587 }
1588
1589 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1590 curr_match == MTRR_TYPE_UNCACHABLE)
1591 return MTRR_TYPE_UNCACHABLE;
1592
1593 if ((prev_match == MTRR_TYPE_WRBACK &&
1594 curr_match == MTRR_TYPE_WRTHROUGH) ||
1595 (prev_match == MTRR_TYPE_WRTHROUGH &&
1596 curr_match == MTRR_TYPE_WRBACK)) {
1597 prev_match = MTRR_TYPE_WRTHROUGH;
1598 curr_match = MTRR_TYPE_WRTHROUGH;
1599 }
1600
1601 if (prev_match != curr_match)
1602 return MTRR_TYPE_UNCACHABLE;
1603 }
1604
1605 if (prev_match != 0xFF)
1606 return prev_match;
1607
1608 return mtrr_state->def_type;
1609}
1610
1611static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1612{
1613 u8 mtrr;
1614
1615 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1616 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1617 if (mtrr == 0xfe || mtrr == 0xff)
1618 mtrr = MTRR_TYPE_WRBACK;
1619 return mtrr;
1620}
1621
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001622static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1623{
1624 unsigned index;
1625 struct hlist_head *bucket;
1626 struct kvm_mmu_page *s;
1627 struct hlist_node *node, *n;
1628
1629 index = kvm_page_table_hashfn(sp->gfn);
1630 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1631 /* don't unsync if pagetable is shadowed with multiple roles */
1632 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001633 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001634 continue;
1635 if (s->role.word != sp->role.word)
1636 return 1;
1637 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001638 ++vcpu->kvm->stat.mmu_unsync;
1639 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001640
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001641 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001642
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001643 mmu_convert_notrap(sp);
1644 return 0;
1645}
1646
1647static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1648 bool can_unsync)
1649{
1650 struct kvm_mmu_page *shadow;
1651
1652 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1653 if (shadow) {
1654 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1655 return 1;
1656 if (shadow->unsync)
1657 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001658 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001659 return kvm_unsync_page(vcpu, shadow);
1660 return 1;
1661 }
1662 return 0;
1663}
1664
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001665static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1666 unsigned pte_access, int user_fault,
1667 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001668 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001669 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001670{
1671 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001672 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001673 u64 mt_mask = shadow_mt_mask;
1674
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001675 /*
1676 * We don't set the accessed bit, since we sometimes want to see
1677 * whether the guest actually used the pte (in order to detect
1678 * demand paging).
1679 */
Sheng Yang7b523452008-04-25 21:13:50 +08001680 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001681 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001682 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001683 if (!dirty)
1684 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001685 if (pte_access & ACC_EXEC_MASK)
1686 spte |= shadow_x_mask;
1687 else
1688 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001689 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001690 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001691 if (largepage)
1692 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001693 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001694 if (!kvm_is_mmio_pfn(pfn)) {
1695 mt_mask = get_memory_type(vcpu, gfn) <<
1696 kvm_x86_ops->get_mt_mask_shift();
1697 mt_mask |= VMX_EPT_IGMT_BIT;
1698 } else
1699 mt_mask = MTRR_TYPE_UNCACHABLE <<
1700 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001701 spte |= mt_mask;
1702 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001703
Anthony Liguori35149e22008-04-02 14:46:56 -05001704 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001705
1706 if ((pte_access & ACC_WRITE_MASK)
1707 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001708
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001709 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1710 ret = 1;
1711 spte = shadow_trap_nonpresent_pte;
1712 goto set_pte;
1713 }
1714
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001715 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001716
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001717 /*
1718 * Optimization: for pte sync, if spte was writable the hash
1719 * lookup is unnecessary (and expensive). Write protection
1720 * is responsibility of mmu_get_page / kvm_sync_page.
1721 * Same reasoning can be applied to dirty page accounting.
1722 */
1723 if (!can_unsync && is_writeble_pte(*shadow_pte))
1724 goto set_pte;
1725
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001726 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001728 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001729 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001730 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001731 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001732 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001733 }
1734 }
1735
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001736 if (pte_access & ACC_WRITE_MASK)
1737 mark_page_dirty(vcpu->kvm, gfn);
1738
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001739set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001741 return ret;
1742}
1743
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001744static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1745 unsigned pt_access, unsigned pte_access,
1746 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001747 int *ptwrite, int largepage, gfn_t gfn,
1748 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001749{
1750 int was_rmapped = 0;
1751 int was_writeble = is_writeble_pte(*shadow_pte);
1752
1753 pgprintk("%s: spte %llx access %x write_fault %d"
1754 " user_fault %d gfn %lx\n",
1755 __func__, *shadow_pte, pt_access,
1756 write_fault, user_fault, gfn);
1757
1758 if (is_rmap_pte(*shadow_pte)) {
1759 /*
1760 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1761 * the parent of the now unreachable PTE.
1762 */
1763 if (largepage && !is_large_pte(*shadow_pte)) {
1764 struct kvm_mmu_page *child;
1765 u64 pte = *shadow_pte;
1766
1767 child = page_header(pte & PT64_BASE_ADDR_MASK);
1768 mmu_page_remove_parent_pte(child, shadow_pte);
1769 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1770 pgprintk("hfn old %lx new %lx\n",
1771 spte_to_pfn(*shadow_pte), pfn);
1772 rmap_remove(vcpu->kvm, shadow_pte);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001773 } else
1774 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001775 }
1776 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001777 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001778 if (write_fault)
1779 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001780 kvm_x86_ops->tlb_flush(vcpu);
1781 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001782
1783 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1784 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1785 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1786 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1787 *shadow_pte, shadow_pte);
1788 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001789 ++vcpu->kvm->stat.lpages;
1790
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001791 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1792 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001793 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001794 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001795 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001796 } else {
1797 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001798 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001799 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001800 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001801 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001802 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001803 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001804 vcpu->arch.last_pte_gfn = gfn;
1805 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001806}
1807
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1809{
1810}
1811
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001812static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001813 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814{
Avi Kivity9f652d212008-12-25 14:54:25 +02001815 struct kvm_shadow_walk_iterator iterator;
1816 struct kvm_mmu_page *sp;
1817 int pt_write = 0;
1818 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819
Avi Kivity9f652d212008-12-25 14:54:25 +02001820 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1821 if (iterator.level == PT_PAGE_TABLE_LEVEL
1822 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1823 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1824 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001825 largepage, gfn, pfn, false);
Avi Kivity9f652d212008-12-25 14:54:25 +02001826 ++vcpu->stat.pf_fixed;
1827 break;
1828 }
1829
1830 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1831 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1832 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1833 iterator.level - 1,
1834 1, ACC_ALL, iterator.sptep);
1835 if (!sp) {
1836 pgprintk("nonpaging_map: ENOMEM\n");
1837 kvm_release_pfn_clean(pfn);
1838 return -ENOMEM;
1839 }
1840
1841 set_shadow_pte(iterator.sptep,
1842 __pa(sp->spt)
1843 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1844 | shadow_user_mask | shadow_x_mask);
1845 }
1846 }
1847 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848}
1849
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001850static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1851{
1852 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001853 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001854 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001855 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001856
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001857 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1858 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1859 largepage = 1;
1860 }
1861
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001862 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001863 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001864 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001865
Avi Kivityd196e342008-01-24 11:44:11 +02001866 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001867 if (is_error_pfn(pfn)) {
1868 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001869 return 1;
1870 }
1871
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001872 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001873 if (mmu_notifier_retry(vcpu, mmu_seq))
1874 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001875 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001876 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001877 spin_unlock(&vcpu->kvm->mmu_lock);
1878
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001879
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001880 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001881
1882out_unlock:
1883 spin_unlock(&vcpu->kvm->mmu_lock);
1884 kvm_release_pfn_clean(pfn);
1885 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001886}
1887
1888
Avi Kivity17ac10a2007-01-05 16:36:40 -08001889static void mmu_free_roots(struct kvm_vcpu *vcpu)
1890{
1891 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001892 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001893
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001894 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001895 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001896 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001897 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1898 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001899
Avi Kivity4db35312007-11-21 15:28:32 +02001900 sp = page_header(root);
1901 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001902 if (!sp->root_count && sp->role.invalid)
1903 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001904 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001905 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001906 return;
1907 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001908 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001909 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001910
Avi Kivity417726a2007-04-12 17:35:58 +03001911 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001912 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001913 sp = page_header(root);
1914 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001915 if (!sp->root_count && sp->role.invalid)
1916 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001917 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001918 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001919 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001920 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001921 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001922}
1923
1924static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1925{
1926 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001927 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001928 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001929 int direct = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001930
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001931 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001932
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001933 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1934 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001935
1936 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001937 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001938 direct = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001939 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001940 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001941 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001942 root = __pa(sp->spt);
1943 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001944 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001945 return;
1946 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001947 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001948 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001949 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001950 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001951 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001952
1953 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001954 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1955 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1956 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001957 continue;
1958 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001959 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1960 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001961 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001962 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001963 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001964 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001965 root = __pa(sp->spt);
1966 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001967 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001968 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001969 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001970}
1971
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001972static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1973{
1974 int i;
1975 struct kvm_mmu_page *sp;
1976
1977 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1978 return;
1979 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1980 hpa_t root = vcpu->arch.mmu.root_hpa;
1981 sp = page_header(root);
1982 mmu_sync_children(vcpu, sp);
1983 return;
1984 }
1985 for (i = 0; i < 4; ++i) {
1986 hpa_t root = vcpu->arch.mmu.pae_root[i];
1987
1988 if (root) {
1989 root &= PT64_BASE_ADDR_MASK;
1990 sp = page_header(root);
1991 mmu_sync_children(vcpu, sp);
1992 }
1993 }
1994}
1995
1996void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1997{
1998 spin_lock(&vcpu->kvm->mmu_lock);
1999 mmu_sync_roots(vcpu);
2000 spin_unlock(&vcpu->kvm->mmu_lock);
2001}
2002
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2004{
2005 return vaddr;
2006}
2007
2008static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002009 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010{
Avi Kivitye8332402007-12-09 18:43:00 +02002011 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002012 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002014 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002015 r = mmu_topup_memory_caches(vcpu);
2016 if (r)
2017 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002018
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002020 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021
Avi Kivitye8332402007-12-09 18:43:00 +02002022 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023
Avi Kivitye8332402007-12-09 18:43:00 +02002024 return nonpaging_map(vcpu, gva & PAGE_MASK,
2025 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026}
2027
Joerg Roedelfb72d162008-02-07 13:47:44 +01002028static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2029 u32 error_code)
2030{
Anthony Liguori35149e22008-04-02 14:46:56 -05002031 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002032 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002033 int largepage = 0;
2034 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002035 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002036
2037 ASSERT(vcpu);
2038 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2039
2040 r = mmu_topup_memory_caches(vcpu);
2041 if (r)
2042 return r;
2043
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002044 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2045 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2046 largepage = 1;
2047 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002048 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002049 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002050 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002051 if (is_error_pfn(pfn)) {
2052 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002053 return 1;
2054 }
2055 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002056 if (mmu_notifier_retry(vcpu, mmu_seq))
2057 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002058 kvm_mmu_free_some_pages(vcpu);
2059 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002060 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002061 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002062
2063 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002064
2065out_unlock:
2066 spin_unlock(&vcpu->kvm->mmu_lock);
2067 kvm_release_pfn_clean(pfn);
2068 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002069}
2070
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071static void nonpaging_free(struct kvm_vcpu *vcpu)
2072{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002073 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074}
2075
2076static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2077{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002078 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079
2080 context->new_cr3 = nonpaging_new_cr3;
2081 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 context->gva_to_gpa = nonpaging_gva_to_gpa;
2083 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002084 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002085 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002086 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002087 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002089 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 return 0;
2091}
2092
Avi Kivityd835dfe2007-11-21 02:57:59 +02002093void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002095 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002096 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097}
2098
2099static void paging_new_cr3(struct kvm_vcpu *vcpu)
2100{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002101 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002102 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103}
2104
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105static void inject_page_fault(struct kvm_vcpu *vcpu,
2106 u64 addr,
2107 u32 err_code)
2108{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002109 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110}
2111
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112static void paging_free(struct kvm_vcpu *vcpu)
2113{
2114 nonpaging_free(vcpu);
2115}
2116
Dong, Eddie82725b22009-03-30 16:21:08 +08002117static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2118{
2119 int bit7;
2120
2121 bit7 = (gpte >> 7) & 1;
2122 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2123}
2124
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125#define PTTYPE 64
2126#include "paging_tmpl.h"
2127#undef PTTYPE
2128
2129#define PTTYPE 32
2130#include "paging_tmpl.h"
2131#undef PTTYPE
2132
Dong, Eddie82725b22009-03-30 16:21:08 +08002133static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2134{
2135 struct kvm_mmu *context = &vcpu->arch.mmu;
2136 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2137 u64 exb_bit_rsvd = 0;
2138
2139 if (!is_nx(vcpu))
2140 exb_bit_rsvd = rsvd_bits(63, 63);
2141 switch (level) {
2142 case PT32_ROOT_LEVEL:
2143 /* no rsvd bits for 2 level 4K page table entries */
2144 context->rsvd_bits_mask[0][1] = 0;
2145 context->rsvd_bits_mask[0][0] = 0;
2146 if (is_cpuid_PSE36())
2147 /* 36bits PSE 4MB page */
2148 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2149 else
2150 /* 32 bits PSE 4MB page */
2151 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2152 context->rsvd_bits_mask[1][0] = ~0ull;
2153 break;
2154 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002155 context->rsvd_bits_mask[0][2] =
2156 rsvd_bits(maxphyaddr, 63) |
2157 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002158 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002159 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002160 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2161 rsvd_bits(maxphyaddr, 62); /* PTE */
2162 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2163 rsvd_bits(maxphyaddr, 62) |
2164 rsvd_bits(13, 20); /* large page */
2165 context->rsvd_bits_mask[1][0] = ~0ull;
2166 break;
2167 case PT64_ROOT_LEVEL:
2168 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2169 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2170 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2171 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2172 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002173 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002174 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2175 rsvd_bits(maxphyaddr, 51);
2176 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2177 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2178 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002179 rsvd_bits(maxphyaddr, 51) |
2180 rsvd_bits(13, 20); /* large page */
Dong, Eddie82725b22009-03-30 16:21:08 +08002181 context->rsvd_bits_mask[1][0] = ~0ull;
2182 break;
2183 }
2184}
2185
Avi Kivity17ac10a2007-01-05 16:36:40 -08002186static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002188 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189
2190 ASSERT(is_pae(vcpu));
2191 context->new_cr3 = paging_new_cr3;
2192 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002194 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002195 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002196 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002198 context->root_level = level;
2199 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002200 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201 return 0;
2202}
2203
Avi Kivity17ac10a2007-01-05 16:36:40 -08002204static int paging64_init_context(struct kvm_vcpu *vcpu)
2205{
Dong, Eddie82725b22009-03-30 16:21:08 +08002206 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002207 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2208}
2209
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210static int paging32_init_context(struct kvm_vcpu *vcpu)
2211{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002212 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213
Dong, Eddie82725b22009-03-30 16:21:08 +08002214 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215 context->new_cr3 = paging_new_cr3;
2216 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217 context->gva_to_gpa = paging32_gva_to_gpa;
2218 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002219 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002220 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002221 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222 context->root_level = PT32_ROOT_LEVEL;
2223 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002224 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225 return 0;
2226}
2227
2228static int paging32E_init_context(struct kvm_vcpu *vcpu)
2229{
Dong, Eddie82725b22009-03-30 16:21:08 +08002230 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002231 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232}
2233
Joerg Roedelfb72d162008-02-07 13:47:44 +01002234static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2235{
2236 struct kvm_mmu *context = &vcpu->arch.mmu;
2237
2238 context->new_cr3 = nonpaging_new_cr3;
2239 context->page_fault = tdp_page_fault;
2240 context->free = nonpaging_free;
2241 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002242 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002243 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002244 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002245 context->root_hpa = INVALID_PAGE;
2246
2247 if (!is_paging(vcpu)) {
2248 context->gva_to_gpa = nonpaging_gva_to_gpa;
2249 context->root_level = 0;
2250 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002251 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002252 context->gva_to_gpa = paging64_gva_to_gpa;
2253 context->root_level = PT64_ROOT_LEVEL;
2254 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002255 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002256 context->gva_to_gpa = paging64_gva_to_gpa;
2257 context->root_level = PT32E_ROOT_LEVEL;
2258 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002259 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002260 context->gva_to_gpa = paging32_gva_to_gpa;
2261 context->root_level = PT32_ROOT_LEVEL;
2262 }
2263
2264 return 0;
2265}
2266
2267static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002269 int r;
2270
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002272 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273
2274 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002275 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002276 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002277 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002279 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002281 r = paging32_init_context(vcpu);
2282
2283 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2284
2285 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286}
2287
Joerg Roedelfb72d162008-02-07 13:47:44 +01002288static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2289{
Anthony Liguori35149e22008-04-02 14:46:56 -05002290 vcpu->arch.update_pte.pfn = bad_pfn;
2291
Joerg Roedelfb72d162008-02-07 13:47:44 +01002292 if (tdp_enabled)
2293 return init_kvm_tdp_mmu(vcpu);
2294 else
2295 return init_kvm_softmmu(vcpu);
2296}
2297
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2299{
2300 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002301 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2302 vcpu->arch.mmu.free(vcpu);
2303 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 }
2305}
2306
2307int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2308{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002309 destroy_kvm_mmu(vcpu);
2310 return init_kvm_mmu(vcpu);
2311}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002312EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002313
2314int kvm_mmu_load(struct kvm_vcpu *vcpu)
2315{
Avi Kivity714b93d2007-01-05 16:36:53 -08002316 int r;
2317
Avi Kivitye2dec932007-01-05 16:36:54 -08002318 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002319 if (r)
2320 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002321 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002322 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002323 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002324 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002325 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002326 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002327 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002328out:
2329 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002331EXPORT_SYMBOL_GPL(kvm_mmu_load);
2332
2333void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2334{
2335 mmu_free_roots(vcpu);
2336}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337
Avi Kivity09072da2007-05-01 14:16:52 +03002338static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002339 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002340 u64 *spte)
2341{
2342 u64 pte;
2343 struct kvm_mmu_page *child;
2344
2345 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002346 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002347 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2348 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002349 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002350 else {
2351 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002352 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002353 }
2354 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002355 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002356 if (is_large_pte(pte))
2357 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002358}
2359
Avi Kivity00284252007-05-01 16:53:31 +03002360static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002361 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002362 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002363 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002364{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002365 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2366 if (!vcpu->arch.update_pte.largepage ||
2367 sp->role.glevels == PT32_ROOT_LEVEL) {
2368 ++vcpu->kvm->stat.mmu_pde_zapped;
2369 return;
2370 }
2371 }
Avi Kivity00284252007-05-01 16:53:31 +03002372
Avi Kivity4cee5762007-11-18 16:37:07 +02002373 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002374 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002375 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002376 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002377 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002378}
2379
Avi Kivity79539ce2007-11-21 02:06:21 +02002380static bool need_remote_flush(u64 old, u64 new)
2381{
2382 if (!is_shadow_present_pte(old))
2383 return false;
2384 if (!is_shadow_present_pte(new))
2385 return true;
2386 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2387 return true;
2388 old ^= PT64_NX_MASK;
2389 new ^= PT64_NX_MASK;
2390 return (old & ~new & PT64_PERM_MASK) != 0;
2391}
2392
2393static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2394{
2395 if (need_remote_flush(old, new))
2396 kvm_flush_remote_tlbs(vcpu->kvm);
2397 else
2398 kvm_mmu_flush_tlb(vcpu);
2399}
2400
Avi Kivity12b7d282007-09-23 14:10:49 +02002401static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2402{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002403 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002404
Sheng Yang7b523452008-04-25 21:13:50 +08002405 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002406}
2407
Avi Kivityd7824ff2007-12-30 12:29:05 +02002408static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2409 const u8 *new, int bytes)
2410{
2411 gfn_t gfn;
2412 int r;
2413 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002414 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002415
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002416 vcpu->arch.update_pte.largepage = 0;
2417
Avi Kivityd7824ff2007-12-30 12:29:05 +02002418 if (bytes != 4 && bytes != 8)
2419 return;
2420
2421 /*
2422 * Assume that the pte write on a page table of the same type
2423 * as the current vcpu paging mode. This is nearly always true
2424 * (might be false while changing modes). Note it is verified later
2425 * by update_pte().
2426 */
2427 if (is_pae(vcpu)) {
2428 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2429 if ((bytes == 4) && (gpa % 4 == 0)) {
2430 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2431 if (r)
2432 return;
2433 memcpy((void *)&gpte + (gpa % 8), new, 4);
2434 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2435 memcpy((void *)&gpte, new, 8);
2436 }
2437 } else {
2438 if ((bytes == 4) && (gpa % 4 == 0))
2439 memcpy((void *)&gpte, new, 4);
2440 }
2441 if (!is_present_pte(gpte))
2442 return;
2443 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002444
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002445 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2446 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2447 vcpu->arch.update_pte.largepage = 1;
2448 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002449 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002450 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002451 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002452
Anthony Liguori35149e22008-04-02 14:46:56 -05002453 if (is_error_pfn(pfn)) {
2454 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002455 return;
2456 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002457 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002458 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002459}
2460
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002461static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2462{
2463 u64 *spte = vcpu->arch.last_pte_updated;
2464
2465 if (spte
2466 && vcpu->arch.last_pte_gfn == gfn
2467 && shadow_accessed_mask
2468 && !(*spte & shadow_accessed_mask)
2469 && is_shadow_present_pte(*spte))
2470 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2471}
2472
Avi Kivity09072da2007-05-01 14:16:52 +03002473void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002474 const u8 *new, int bytes,
2475 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002476{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002477 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002478 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002479 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002480 struct hlist_head *bucket;
2481 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002482 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002483 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002484 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002485 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002486 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002487 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002488 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002489 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002490 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002491 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002492 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002493
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002494 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002495 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002496 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002497 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002498 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002499 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002500 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002501 if (guest_initiated) {
2502 if (gfn == vcpu->arch.last_pt_write_gfn
2503 && !last_updated_pte_accessed(vcpu)) {
2504 ++vcpu->arch.last_pt_write_count;
2505 if (vcpu->arch.last_pt_write_count >= 3)
2506 flooded = 1;
2507 } else {
2508 vcpu->arch.last_pt_write_gfn = gfn;
2509 vcpu->arch.last_pt_write_count = 1;
2510 vcpu->arch.last_pte_updated = NULL;
2511 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002512 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002513 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002514 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002515 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002516 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002517 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002518 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002519 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002520 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002521 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002522 /*
2523 * Misaligned accesses are too much trouble to fix
2524 * up; also, they usually indicate a page is not used
2525 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002526 *
2527 * If we're seeing too many writes to a page,
2528 * it may no longer be a page table, or we may be
2529 * forking, in which case it is better to unmap the
2530 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002531 */
2532 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002533 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002534 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2535 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002536 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002537 continue;
2538 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002539 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002540 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002541 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002542 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002543 page_offset <<= 1; /* 32->64 */
2544 /*
2545 * A 32-bit pde maps 4MB while the shadow pdes map
2546 * only 2MB. So we need to double the offset again
2547 * and zap two pdes instead of one.
2548 */
2549 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002550 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002551 page_offset <<= 1;
2552 npte = 2;
2553 }
Avi Kivityfce06572007-05-01 16:44:05 +03002554 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002555 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002556 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002557 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002558 }
Avi Kivity4db35312007-11-21 15:28:32 +02002559 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002560 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2561 gentry = 0;
2562 r = kvm_read_guest_atomic(vcpu->kvm,
2563 gpa & ~(u64)(pte_size - 1),
2564 &gentry, pte_size);
2565 new = (const void *)&gentry;
2566 if (r < 0)
2567 new = NULL;
2568 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002569 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002570 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002571 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002572 if (new)
2573 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002574 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002575 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002576 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002577 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002578 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002579 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002580 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2581 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2582 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002583 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002584}
2585
Avi Kivitya4360362007-01-05 16:36:45 -08002586int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2587{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002588 gpa_t gpa;
2589 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002590
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002591 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002592
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002593 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002594 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002595 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002596 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002597}
Avi Kivity577bdc42008-07-19 08:57:05 +03002598EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002599
Avi Kivity22d95b12007-09-14 20:26:06 +03002600void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002601{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002602 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002603 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002604
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002605 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002606 struct kvm_mmu_page, link);
2607 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002608 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002609 }
2610}
Avi Kivityebeace82007-01-05 16:36:47 -08002611
Avi Kivity30677142007-10-28 18:48:59 +02002612int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2613{
2614 int r;
2615 enum emulation_result er;
2616
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002617 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002618 if (r < 0)
2619 goto out;
2620
2621 if (!r) {
2622 r = 1;
2623 goto out;
2624 }
2625
Avi Kivityb733bfb2007-10-28 18:52:05 +02002626 r = mmu_topup_memory_caches(vcpu);
2627 if (r)
2628 goto out;
2629
Avi Kivity30677142007-10-28 18:48:59 +02002630 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002631
2632 switch (er) {
2633 case EMULATE_DONE:
2634 return 1;
2635 case EMULATE_DO_MMIO:
2636 ++vcpu->stat.mmio_exits;
2637 return 0;
2638 case EMULATE_FAIL:
2639 kvm_report_emulation_failure(vcpu, "pagetable");
2640 return 1;
2641 default:
2642 BUG();
2643 }
2644out:
Avi Kivity30677142007-10-28 18:48:59 +02002645 return r;
2646}
2647EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2648
Marcelo Tosattia7052892008-09-23 13:18:35 -03002649void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2650{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002651 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002652 kvm_mmu_flush_tlb(vcpu);
2653 ++vcpu->stat.invlpg;
2654}
2655EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2656
Joerg Roedel18552672008-02-07 13:47:41 +01002657void kvm_enable_tdp(void)
2658{
2659 tdp_enabled = true;
2660}
2661EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2662
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002663void kvm_disable_tdp(void)
2664{
2665 tdp_enabled = false;
2666}
2667EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2668
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669static void free_mmu_pages(struct kvm_vcpu *vcpu)
2670{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002671 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672}
2673
2674static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2675{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002676 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 int i;
2678
2679 ASSERT(vcpu);
2680
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002681 if (vcpu->kvm->arch.n_requested_mmu_pages)
2682 vcpu->kvm->arch.n_free_mmu_pages =
2683 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002684 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002685 vcpu->kvm->arch.n_free_mmu_pages =
2686 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002687 /*
2688 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2689 * Therefore we need to allocate shadow page tables in the first
2690 * 4GB of memory, which happens to fit the DMA32 zone.
2691 */
2692 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2693 if (!page)
2694 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002695 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002696 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002697 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002698
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 return 0;
2700
2701error_1:
2702 free_mmu_pages(vcpu);
2703 return -ENOMEM;
2704}
2705
Ingo Molnar8018c272006-12-29 16:50:01 -08002706int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002709 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710
Ingo Molnar8018c272006-12-29 16:50:01 -08002711 return alloc_mmu_pages(vcpu);
2712}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713
Ingo Molnar8018c272006-12-29 16:50:01 -08002714int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2715{
2716 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002717 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002718
Ingo Molnar8018c272006-12-29 16:50:01 -08002719 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720}
2721
2722void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2723{
2724 ASSERT(vcpu);
2725
2726 destroy_kvm_mmu(vcpu);
2727 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002728 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729}
2730
Avi Kivity90cb0522007-07-17 13:04:56 +03002731void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732{
Avi Kivity4db35312007-11-21 15:28:32 +02002733 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734
Avi Kivity2245a282008-08-27 16:32:24 +03002735 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002736 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 int i;
2738 u64 *pt;
2739
Sheng Yang291f26b2008-10-16 17:30:57 +08002740 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 continue;
2742
Avi Kivity4db35312007-11-21 15:28:32 +02002743 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2745 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002746 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 }
Avi Kivity171d5952008-08-27 16:40:51 +03002749 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002750 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002752
Avi Kivity90cb0522007-07-17 13:04:56 +03002753void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002754{
Avi Kivity4db35312007-11-21 15:28:32 +02002755 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002756
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002757 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002758 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002759 if (kvm_mmu_zap_page(kvm, sp))
2760 node = container_of(kvm->arch.active_mmu_pages.next,
2761 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002762 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002763
Avi Kivity90cb0522007-07-17 13:04:56 +03002764 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002765}
2766
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002767static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002768{
2769 struct kvm_mmu_page *page;
2770
2771 page = container_of(kvm->arch.active_mmu_pages.prev,
2772 struct kvm_mmu_page, link);
2773 kvm_mmu_zap_page(kvm, page);
2774}
2775
2776static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2777{
2778 struct kvm *kvm;
2779 struct kvm *kvm_freed = NULL;
2780 int cache_count = 0;
2781
2782 spin_lock(&kvm_lock);
2783
2784 list_for_each_entry(kvm, &vm_list, vm_list) {
2785 int npages;
2786
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002787 if (!down_read_trylock(&kvm->slots_lock))
2788 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002789 spin_lock(&kvm->mmu_lock);
2790 npages = kvm->arch.n_alloc_mmu_pages -
2791 kvm->arch.n_free_mmu_pages;
2792 cache_count += npages;
2793 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2794 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2795 cache_count--;
2796 kvm_freed = kvm;
2797 }
2798 nr_to_scan--;
2799
2800 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002801 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002802 }
2803 if (kvm_freed)
2804 list_move_tail(&kvm_freed->vm_list, &vm_list);
2805
2806 spin_unlock(&kvm_lock);
2807
2808 return cache_count;
2809}
2810
2811static struct shrinker mmu_shrinker = {
2812 .shrink = mmu_shrink,
2813 .seeks = DEFAULT_SEEKS * 10,
2814};
2815
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002816static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002817{
2818 if (pte_chain_cache)
2819 kmem_cache_destroy(pte_chain_cache);
2820 if (rmap_desc_cache)
2821 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002822 if (mmu_page_header_cache)
2823 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002824}
2825
Izik Eidus3ee16c82008-03-30 15:17:21 +03002826void kvm_mmu_module_exit(void)
2827{
2828 mmu_destroy_caches();
2829 unregister_shrinker(&mmu_shrinker);
2830}
2831
Avi Kivityb5a33a72007-04-15 16:31:09 +03002832int kvm_mmu_module_init(void)
2833{
2834 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2835 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002836 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002837 if (!pte_chain_cache)
2838 goto nomem;
2839 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2840 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002841 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002842 if (!rmap_desc_cache)
2843 goto nomem;
2844
Avi Kivityd3d25b02007-05-30 12:34:53 +03002845 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2846 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002847 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002848 if (!mmu_page_header_cache)
2849 goto nomem;
2850
Izik Eidus3ee16c82008-03-30 15:17:21 +03002851 register_shrinker(&mmu_shrinker);
2852
Avi Kivityb5a33a72007-04-15 16:31:09 +03002853 return 0;
2854
2855nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002856 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002857 return -ENOMEM;
2858}
2859
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002860/*
2861 * Caculate mmu pages needed for kvm.
2862 */
2863unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2864{
2865 int i;
2866 unsigned int nr_mmu_pages;
2867 unsigned int nr_pages = 0;
2868
2869 for (i = 0; i < kvm->nmemslots; i++)
2870 nr_pages += kvm->memslots[i].npages;
2871
2872 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2873 nr_mmu_pages = max(nr_mmu_pages,
2874 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2875
2876 return nr_mmu_pages;
2877}
2878
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002879static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2880 unsigned len)
2881{
2882 if (len > buffer->len)
2883 return NULL;
2884 return buffer->ptr;
2885}
2886
2887static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2888 unsigned len)
2889{
2890 void *ret;
2891
2892 ret = pv_mmu_peek_buffer(buffer, len);
2893 if (!ret)
2894 return ret;
2895 buffer->ptr += len;
2896 buffer->len -= len;
2897 buffer->processed += len;
2898 return ret;
2899}
2900
2901static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2902 gpa_t addr, gpa_t value)
2903{
2904 int bytes = 8;
2905 int r;
2906
2907 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2908 bytes = 4;
2909
2910 r = mmu_topup_memory_caches(vcpu);
2911 if (r)
2912 return r;
2913
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002914 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002915 return -EFAULT;
2916
2917 return 1;
2918}
2919
2920static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2921{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002922 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002923 return 1;
2924}
2925
2926static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2927{
2928 spin_lock(&vcpu->kvm->mmu_lock);
2929 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2930 spin_unlock(&vcpu->kvm->mmu_lock);
2931 return 1;
2932}
2933
2934static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2935 struct kvm_pv_mmu_op_buffer *buffer)
2936{
2937 struct kvm_mmu_op_header *header;
2938
2939 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2940 if (!header)
2941 return 0;
2942 switch (header->op) {
2943 case KVM_MMU_OP_WRITE_PTE: {
2944 struct kvm_mmu_op_write_pte *wpte;
2945
2946 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2947 if (!wpte)
2948 return 0;
2949 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2950 wpte->pte_val);
2951 }
2952 case KVM_MMU_OP_FLUSH_TLB: {
2953 struct kvm_mmu_op_flush_tlb *ftlb;
2954
2955 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2956 if (!ftlb)
2957 return 0;
2958 return kvm_pv_mmu_flush_tlb(vcpu);
2959 }
2960 case KVM_MMU_OP_RELEASE_PT: {
2961 struct kvm_mmu_op_release_pt *rpt;
2962
2963 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2964 if (!rpt)
2965 return 0;
2966 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2967 }
2968 default: return 0;
2969 }
2970}
2971
2972int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2973 gpa_t addr, unsigned long *ret)
2974{
2975 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002976 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002977
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002978 buffer->ptr = buffer->buf;
2979 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2980 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002981
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002982 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002983 if (r)
2984 goto out;
2985
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002986 while (buffer->len) {
2987 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002988 if (r < 0)
2989 goto out;
2990 if (r == 0)
2991 break;
2992 }
2993
2994 r = 1;
2995out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002996 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002997 return r;
2998}
2999
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003000#ifdef AUDIT
3001
3002static const char *audit_msg;
3003
3004static gva_t canonicalize(gva_t gva)
3005{
3006#ifdef CONFIG_X86_64
3007 gva = (long long)(gva << 16) >> 16;
3008#endif
3009 return gva;
3010}
3011
3012static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3013 gva_t va, int level)
3014{
3015 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3016 int i;
3017 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3018
3019 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3020 u64 ent = pt[i];
3021
Avi Kivityc7addb92007-09-16 18:58:32 +02003022 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003023 continue;
3024
3025 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003026 if (level > 1) {
3027 if (ent == shadow_notrap_nonpresent_pte)
3028 printk(KERN_ERR "audit: (%s) nontrapping pte"
3029 " in nonleaf level: levels %d gva %lx"
3030 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003031 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003032
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003033 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003034 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003035 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003036 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003037
Avi Kivityc7addb92007-09-16 18:58:32 +02003038 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003039 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003040 printk(KERN_ERR "xx audit error: (%s) levels %d"
3041 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003042 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003043 va, gpa, hpa, ent,
3044 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003045 else if (ent == shadow_notrap_nonpresent_pte
3046 && !is_error_hpa(hpa))
3047 printk(KERN_ERR "audit: (%s) notrap shadow,"
3048 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003049 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003050
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003051 }
3052 }
3053}
3054
3055static void audit_mappings(struct kvm_vcpu *vcpu)
3056{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003057 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003058
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003059 if (vcpu->arch.mmu.root_level == 4)
3060 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003061 else
3062 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003063 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003064 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003065 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003066 i << 30,
3067 2);
3068}
3069
3070static int count_rmaps(struct kvm_vcpu *vcpu)
3071{
3072 int nmaps = 0;
3073 int i, j, k;
3074
3075 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3076 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3077 struct kvm_rmap_desc *d;
3078
3079 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003080 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003081
Izik Eidus290fc382007-09-27 14:11:22 +02003082 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003083 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003084 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003085 ++nmaps;
3086 continue;
3087 }
Izik Eidus290fc382007-09-27 14:11:22 +02003088 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003089 while (d) {
3090 for (k = 0; k < RMAP_EXT; ++k)
3091 if (d->shadow_ptes[k])
3092 ++nmaps;
3093 else
3094 break;
3095 d = d->more;
3096 }
3097 }
3098 }
3099 return nmaps;
3100}
3101
3102static int count_writable_mappings(struct kvm_vcpu *vcpu)
3103{
3104 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003105 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003106 int i;
3107
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003108 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003109 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003110
Avi Kivity4db35312007-11-21 15:28:32 +02003111 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003112 continue;
3113
3114 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3115 u64 ent = pt[i];
3116
3117 if (!(ent & PT_PRESENT_MASK))
3118 continue;
3119 if (!(ent & PT_WRITABLE_MASK))
3120 continue;
3121 ++nmaps;
3122 }
3123 }
3124 return nmaps;
3125}
3126
3127static void audit_rmap(struct kvm_vcpu *vcpu)
3128{
3129 int n_rmap = count_rmaps(vcpu);
3130 int n_actual = count_writable_mappings(vcpu);
3131
3132 if (n_rmap != n_actual)
3133 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003134 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003135}
3136
3137static void audit_write_protection(struct kvm_vcpu *vcpu)
3138{
Avi Kivity4db35312007-11-21 15:28:32 +02003139 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003140 struct kvm_memory_slot *slot;
3141 unsigned long *rmapp;
3142 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003143
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003144 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003145 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003146 continue;
3147
Avi Kivity4db35312007-11-21 15:28:32 +02003148 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003149 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003150 rmapp = &slot->rmap[gfn - slot->base_gfn];
3151 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003152 printk(KERN_ERR "%s: (%s) shadow page has writable"
3153 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003154 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003155 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003156 }
3157}
3158
3159static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3160{
3161 int olddbg = dbg;
3162
3163 dbg = 0;
3164 audit_msg = msg;
3165 audit_rmap(vcpu);
3166 audit_write_protection(vcpu);
3167 audit_mappings(vcpu);
3168 dbg = olddbg;
3169}
3170
3171#endif