blob: b2c8e28021c953edf87ce6235e293903ef0f5932 [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 Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_present_pte(unsigned long pte)
229{
230 return pte & PT_PRESENT_MASK;
231}
232
Avi Kivityc7addb92007-09-16 18:58:32 +0200233static int is_shadow_present_pte(u64 pte)
234{
Avi Kivityc7addb92007-09-16 18:58:32 +0200235 return pte != shadow_trap_nonpresent_pte
236 && pte != shadow_notrap_nonpresent_pte;
237}
238
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300239static int is_large_pte(u64 pte)
240{
241 return pte & PT_PAGE_SIZE_MASK;
242}
243
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244static int is_writeble_pte(unsigned long pte)
245{
246 return pte & PT_WRITABLE_MASK;
247}
248
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200249static int is_dirty_pte(unsigned long pte)
250{
Sheng Yang7b523452008-04-25 21:13:50 +0800251 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200252}
253
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800254static int is_rmap_pte(u64 pte)
255{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200256 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800257}
258
Anthony Liguori35149e22008-04-02 14:46:56 -0500259static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200260{
Anthony Liguori35149e22008-04-02 14:46:56 -0500261 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200262}
263
Avi Kivityda928522007-11-21 13:54:47 +0200264static gfn_t pse36_gfn_delta(u32 gpte)
265{
266 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
267
268 return (gpte & PT32_DIR_PSE36_MASK) << shift;
269}
270
Avi Kivitye663ee62007-05-31 15:46:04 +0300271static void set_shadow_pte(u64 *sptep, u64 spte)
272{
273#ifdef CONFIG_X86_64
274 set_64bit((unsigned long *)sptep, spte);
275#else
276 set_64bit((unsigned long long *)sptep, spte);
277#endif
278}
279
Avi Kivitye2dec932007-01-05 16:36:54 -0800280static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300281 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800282{
283 void *obj;
284
285 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800286 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800287 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300288 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800289 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800290 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800291 cache->objects[cache->nobjs++] = obj;
292 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800293 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800294}
295
296static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
297{
298 while (mc->nobjs)
299 kfree(mc->objects[--mc->nobjs]);
300}
301
Avi Kivityc1158e62007-07-20 08:18:27 +0300302static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300303 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300304{
305 struct page *page;
306
307 if (cache->nobjs >= min)
308 return 0;
309 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300310 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300311 if (!page)
312 return -ENOMEM;
313 set_page_private(page, 0);
314 cache->objects[cache->nobjs++] = page_address(page);
315 }
316 return 0;
317}
318
319static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
320{
321 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300322 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300323}
324
Avi Kivity8c438502007-04-16 11:53:17 +0300325static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
326{
327 int r;
328
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800329 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300330 pte_chain_cache, 4);
331 if (r)
332 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200334 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300335 if (r)
336 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800337 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300338 if (r)
339 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800340 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300341 mmu_page_header_cache, 4);
342out:
Avi Kivity8c438502007-04-16 11:53:17 +0300343 return r;
344}
345
Avi Kivity714b93d2007-01-05 16:36:53 -0800346static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
347{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800348 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
349 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
350 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
351 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800352}
353
354static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
355 size_t size)
356{
357 void *p;
358
359 BUG_ON(!mc->nobjs);
360 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800361 return p;
362}
363
Avi Kivity714b93d2007-01-05 16:36:53 -0800364static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
365{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800366 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800367 sizeof(struct kvm_pte_chain));
368}
369
Avi Kivity90cb0522007-07-17 13:04:56 +0300370static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800371{
Avi Kivity90cb0522007-07-17 13:04:56 +0300372 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800373}
374
375static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800377 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800378 sizeof(struct kvm_rmap_desc));
379}
380
Avi Kivity90cb0522007-07-17 13:04:56 +0300381static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800382{
Avi Kivity90cb0522007-07-17 13:04:56 +0300383 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800384}
385
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800386/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300387 * Return the pointer to the largepage write count for a given
388 * gfn, handling slots that are not large page aligned.
389 */
390static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
391{
392 unsigned long idx;
393
394 idx = (gfn / KVM_PAGES_PER_HPAGE) -
395 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
396 return &slot->lpage_info[idx].write_count;
397}
398
399static void account_shadowed(struct kvm *kvm, gfn_t gfn)
400{
401 int *write_count;
402
Izik Eidus28430992008-10-03 17:40:32 +0300403 gfn = unalias_gfn(kvm, gfn);
404 write_count = slot_largepage_idx(gfn,
405 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300406 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300407}
408
409static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
410{
411 int *write_count;
412
Izik Eidus28430992008-10-03 17:40:32 +0300413 gfn = unalias_gfn(kvm, gfn);
414 write_count = slot_largepage_idx(gfn,
415 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300416 *write_count -= 1;
417 WARN_ON(*write_count < 0);
418}
419
420static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
421{
Izik Eidus28430992008-10-03 17:40:32 +0300422 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423 int *largepage_idx;
424
Izik Eidus28430992008-10-03 17:40:32 +0300425 gfn = unalias_gfn(kvm, gfn);
426 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300427 if (slot) {
428 largepage_idx = slot_largepage_idx(gfn, slot);
429 return *largepage_idx;
430 }
431
432 return 1;
433}
434
435static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
436{
437 struct vm_area_struct *vma;
438 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300439 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300440
441 addr = gfn_to_hva(kvm, gfn);
442 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300443 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300444
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300445 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300446 vma = find_vma(current->mm, addr);
447 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300448 ret = 1;
449 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300451 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452}
453
454static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
455{
456 struct kvm_memory_slot *slot;
457
458 if (has_wrprotected_page(vcpu->kvm, large_gfn))
459 return 0;
460
461 if (!host_largepage_backed(vcpu->kvm, large_gfn))
462 return 0;
463
464 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
465 if (slot && slot->dirty_bitmap)
466 return 0;
467
468 return 1;
469}
470
471/*
Izik Eidus290fc382007-09-27 14:11:22 +0200472 * Take gfn and return the reverse mapping to it.
473 * Note: gfn must be unaliased before this function get called
474 */
475
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200477{
478 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200480
481 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482 if (!lpage)
483 return &slot->rmap[gfn - slot->base_gfn];
484
485 idx = (gfn / KVM_PAGES_PER_HPAGE) -
486 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
487
488 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200489}
490
491/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800492 * Reverse mapping data structures:
493 *
Izik Eidus290fc382007-09-27 14:11:22 +0200494 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
495 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496 *
Izik Eidus290fc382007-09-27 14:11:22 +0200497 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
498 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800499 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300500static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800501{
Avi Kivity4db35312007-11-21 15:28:32 +0200502 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800503 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200504 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800505 int i;
506
507 if (!is_rmap_pte(*spte))
508 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200509 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200510 sp = page_header(__pa(spte));
511 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300512 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200513 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800514 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200515 *rmapp = (unsigned long)spte;
516 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800517 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800518 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200519 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800520 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200521 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800522 } else {
523 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200524 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800525 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
526 desc = desc->more;
527 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800528 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800529 desc = desc->more;
530 }
531 for (i = 0; desc->shadow_ptes[i]; ++i)
532 ;
533 desc->shadow_ptes[i] = spte;
534 }
535}
536
Izik Eidus290fc382007-09-27 14:11:22 +0200537static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 struct kvm_rmap_desc *desc,
539 int i,
540 struct kvm_rmap_desc *prev_desc)
541{
542 int j;
543
544 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
545 ;
546 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000547 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548 if (j != 0)
549 return;
550 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200551 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 else
553 if (prev_desc)
554 prev_desc->more = desc->more;
555 else
Izik Eidus290fc382007-09-27 14:11:22 +0200556 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300557 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558}
559
Izik Eidus290fc382007-09-27 14:11:22 +0200560static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 struct kvm_rmap_desc *desc;
563 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200564 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500565 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200566 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800567 int i;
568
569 if (!is_rmap_pte(*spte))
570 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200571 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500572 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800573 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500574 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200575 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500576 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200577 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500578 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300579 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200580 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
582 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200583 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800584 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200585 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
587 spte, *spte);
588 BUG();
589 }
Izik Eidus290fc382007-09-27 14:11:22 +0200590 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800591 } else {
592 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200593 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 prev_desc = NULL;
595 while (desc) {
596 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
597 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200598 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800599 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 prev_desc);
601 return;
602 }
603 prev_desc = desc;
604 desc = desc->more;
605 }
606 BUG();
607 }
608}
609
Izik Eidus98348e92007-10-16 14:42:30 +0200610static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800611{
Avi Kivity374cbac2007-01-05 16:36:43 -0800612 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200613 struct kvm_rmap_desc *prev_desc;
614 u64 *prev_spte;
615 int i;
616
617 if (!*rmapp)
618 return NULL;
619 else if (!(*rmapp & 1)) {
620 if (!spte)
621 return (u64 *)*rmapp;
622 return NULL;
623 }
624 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
625 prev_desc = NULL;
626 prev_spte = NULL;
627 while (desc) {
628 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
629 if (prev_spte == spte)
630 return desc->shadow_ptes[i];
631 prev_spte = desc->shadow_ptes[i];
632 }
633 desc = desc->more;
634 }
635 return NULL;
636}
637
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200638static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200639{
Izik Eidus290fc382007-09-27 14:11:22 +0200640 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800641 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800642 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800643
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500644 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300645 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800646
Izik Eidus98348e92007-10-16 14:42:30 +0200647 spte = rmap_next(kvm, rmapp, NULL);
648 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800649 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800650 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800651 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800652 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200653 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800654 write_protected = 1;
655 }
Izik Eidus9647c142007-10-16 14:43:46 +0200656 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800657 }
Izik Eidus855149a2008-03-20 18:17:24 +0200658 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500659 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200660
661 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500662 pfn = spte_to_pfn(*spte);
663 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200664 }
665
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300666 /* check for huge page mappings */
667 rmapp = gfn_to_rmap(kvm, gfn, 1);
668 spte = rmap_next(kvm, rmapp, NULL);
669 while (spte) {
670 BUG_ON(!spte);
671 BUG_ON(!(*spte & PT_PRESENT_MASK));
672 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
673 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
674 if (is_writeble_pte(*spte)) {
675 rmap_remove(kvm, spte);
676 --kvm->stat.lpages;
677 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300678 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300679 write_protected = 1;
680 }
681 spte = rmap_next(kvm, rmapp, spte);
682 }
683
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200684 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800685}
686
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200687static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
688{
689 u64 *spte;
690 int need_tlb_flush = 0;
691
692 while ((spte = rmap_next(kvm, rmapp, NULL))) {
693 BUG_ON(!(*spte & PT_PRESENT_MASK));
694 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
695 rmap_remove(kvm, spte);
696 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
697 need_tlb_flush = 1;
698 }
699 return need_tlb_flush;
700}
701
702static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
703 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
704{
705 int i;
706 int retval = 0;
707
708 /*
709 * If mmap_sem isn't taken, we can look the memslots with only
710 * the mmu_lock by skipping over the slots with userspace_addr == 0.
711 */
712 for (i = 0; i < kvm->nmemslots; i++) {
713 struct kvm_memory_slot *memslot = &kvm->memslots[i];
714 unsigned long start = memslot->userspace_addr;
715 unsigned long end;
716
717 /* mmu_lock protects userspace_addr */
718 if (!start)
719 continue;
720
721 end = start + (memslot->npages << PAGE_SHIFT);
722 if (hva >= start && hva < end) {
723 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
724 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
725 retval |= handler(kvm,
726 &memslot->lpage_info[
727 gfn_offset /
728 KVM_PAGES_PER_HPAGE].rmap_pde);
729 }
730 }
731
732 return retval;
733}
734
735int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
736{
737 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
738}
739
740static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
741{
742 u64 *spte;
743 int young = 0;
744
Sheng Yang534e38b2008-09-08 15:12:30 +0800745 /* always return old for EPT */
746 if (!shadow_accessed_mask)
747 return 0;
748
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200749 spte = rmap_next(kvm, rmapp, NULL);
750 while (spte) {
751 int _young;
752 u64 _spte = *spte;
753 BUG_ON(!(_spte & PT_PRESENT_MASK));
754 _young = _spte & PT_ACCESSED_MASK;
755 if (_young) {
756 young = 1;
757 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
758 }
759 spte = rmap_next(kvm, rmapp, spte);
760 }
761 return young;
762}
763
764int kvm_age_hva(struct kvm *kvm, unsigned long hva)
765{
766 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
767}
768
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800769#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300770static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771{
Avi Kivity139bdb22007-01-05 16:36:50 -0800772 u64 *pos;
773 u64 *end;
774
Avi Kivity47ad8e62007-05-06 15:50:58 +0300775 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300776 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800777 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800778 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800780 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 return 1;
782}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800783#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
Avi Kivity4db35312007-11-21 15:28:32 +0200785static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800786{
Avi Kivity4db35312007-11-21 15:28:32 +0200787 ASSERT(is_empty_shadow_page(sp->spt));
788 list_del(&sp->link);
789 __free_page(virt_to_page(sp->spt));
790 __free_page(virt_to_page(sp->gfns));
791 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800792 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800793}
794
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800795static unsigned kvm_page_table_hashfn(gfn_t gfn)
796{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200797 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800798}
799
Avi Kivity25c0de22007-01-05 16:36:42 -0800800static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
801 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802{
Avi Kivity4db35312007-11-21 15:28:32 +0200803 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800805 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
806 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
807 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200808 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800809 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200810 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800811 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200812 sp->multimapped = 0;
813 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800814 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200815 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816}
817
Avi Kivity714b93d2007-01-05 16:36:53 -0800818static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200819 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800820{
821 struct kvm_pte_chain *pte_chain;
822 struct hlist_node *node;
823 int i;
824
825 if (!parent_pte)
826 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200827 if (!sp->multimapped) {
828 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800829
830 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200831 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800832 return;
833 }
Avi Kivity4db35312007-11-21 15:28:32 +0200834 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800835 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200836 INIT_HLIST_HEAD(&sp->parent_ptes);
837 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800838 pte_chain->parent_ptes[0] = old;
839 }
Avi Kivity4db35312007-11-21 15:28:32 +0200840 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800841 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
842 continue;
843 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
844 if (!pte_chain->parent_ptes[i]) {
845 pte_chain->parent_ptes[i] = parent_pte;
846 return;
847 }
848 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800849 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800850 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200851 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852 pte_chain->parent_ptes[0] = parent_pte;
853}
854
Avi Kivity4db35312007-11-21 15:28:32 +0200855static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800856 u64 *parent_pte)
857{
858 struct kvm_pte_chain *pte_chain;
859 struct hlist_node *node;
860 int i;
861
Avi Kivity4db35312007-11-21 15:28:32 +0200862 if (!sp->multimapped) {
863 BUG_ON(sp->parent_pte != parent_pte);
864 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800865 return;
866 }
Avi Kivity4db35312007-11-21 15:28:32 +0200867 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800868 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
869 if (!pte_chain->parent_ptes[i])
870 break;
871 if (pte_chain->parent_ptes[i] != parent_pte)
872 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800873 while (i + 1 < NR_PTE_CHAIN_ENTRIES
874 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 pte_chain->parent_ptes[i]
876 = pte_chain->parent_ptes[i + 1];
877 ++i;
878 }
879 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800880 if (i == 0) {
881 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300882 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200883 if (hlist_empty(&sp->parent_ptes)) {
884 sp->multimapped = 0;
885 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800886 }
887 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800888 return;
889 }
890 BUG();
891}
892
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300893
894static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
895 mmu_parent_walk_fn fn)
896{
897 struct kvm_pte_chain *pte_chain;
898 struct hlist_node *node;
899 struct kvm_mmu_page *parent_sp;
900 int i;
901
902 if (!sp->multimapped && sp->parent_pte) {
903 parent_sp = page_header(__pa(sp->parent_pte));
904 fn(vcpu, parent_sp);
905 mmu_parent_walk(vcpu, parent_sp, fn);
906 return;
907 }
908 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
909 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
910 if (!pte_chain->parent_ptes[i])
911 break;
912 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
913 fn(vcpu, parent_sp);
914 mmu_parent_walk(vcpu, parent_sp, fn);
915 }
916}
917
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300918static void kvm_mmu_update_unsync_bitmap(u64 *spte)
919{
920 unsigned int index;
921 struct kvm_mmu_page *sp = page_header(__pa(spte));
922
923 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200924 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
925 sp->unsync_children++;
926 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300927}
928
929static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
930{
931 struct kvm_pte_chain *pte_chain;
932 struct hlist_node *node;
933 int i;
934
935 if (!sp->parent_pte)
936 return;
937
938 if (!sp->multimapped) {
939 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
940 return;
941 }
942
943 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
944 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
945 if (!pte_chain->parent_ptes[i])
946 break;
947 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
948 }
949}
950
951static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
952{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300953 kvm_mmu_update_parents_unsync(sp);
954 return 1;
955}
956
957static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
958 struct kvm_mmu_page *sp)
959{
960 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
961 kvm_mmu_update_parents_unsync(sp);
962}
963
Avi Kivityd761a502008-05-29 14:55:03 +0300964static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
965 struct kvm_mmu_page *sp)
966{
967 int i;
968
969 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
970 sp->spt[i] = shadow_trap_nonpresent_pte;
971}
972
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300973static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
974 struct kvm_mmu_page *sp)
975{
976 return 1;
977}
978
Marcelo Tosattia7052892008-09-23 13:18:35 -0300979static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
980{
981}
982
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200983#define KVM_PAGE_ARRAY_NR 16
984
985struct kvm_mmu_pages {
986 struct mmu_page_and_offset {
987 struct kvm_mmu_page *sp;
988 unsigned int idx;
989 } page[KVM_PAGE_ARRAY_NR];
990 unsigned int nr;
991};
992
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300993#define for_each_unsync_children(bitmap, idx) \
994 for (idx = find_first_bit(bitmap, 512); \
995 idx < 512; \
996 idx = find_next_bit(bitmap, 512, idx+1))
997
Hannes Edercded19f2009-02-21 02:19:13 +0100998static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
999 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001000{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001001 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001002
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001003 if (sp->unsync)
1004 for (i=0; i < pvec->nr; i++)
1005 if (pvec->page[i].sp == sp)
1006 return 0;
1007
1008 pvec->page[pvec->nr].sp = sp;
1009 pvec->page[pvec->nr].idx = idx;
1010 pvec->nr++;
1011 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1012}
1013
1014static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1015 struct kvm_mmu_pages *pvec)
1016{
1017 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001018
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001019 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001020 u64 ent = sp->spt[i];
1021
Marcelo Tosatti87917232008-12-22 18:49:30 -02001022 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001023 struct kvm_mmu_page *child;
1024 child = page_header(ent & PT64_BASE_ADDR_MASK);
1025
1026 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001027 if (mmu_pages_add(pvec, child, i))
1028 return -ENOSPC;
1029
1030 ret = __mmu_unsync_walk(child, pvec);
1031 if (!ret)
1032 __clear_bit(i, sp->unsync_child_bitmap);
1033 else if (ret > 0)
1034 nr_unsync_leaf += ret;
1035 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001036 return ret;
1037 }
1038
1039 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001040 nr_unsync_leaf++;
1041 if (mmu_pages_add(pvec, child, i))
1042 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001043 }
1044 }
1045 }
1046
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001047 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001048 sp->unsync_children = 0;
1049
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001050 return nr_unsync_leaf;
1051}
1052
1053static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1054 struct kvm_mmu_pages *pvec)
1055{
1056 if (!sp->unsync_children)
1057 return 0;
1058
1059 mmu_pages_add(pvec, sp, 0);
1060 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001061}
1062
Avi Kivity4db35312007-11-21 15:28:32 +02001063static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001064{
1065 unsigned index;
1066 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001067 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001068 struct hlist_node *node;
1069
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001070 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001071 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001072 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001073 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001074 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001075 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001076 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001077 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001078 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001079 }
1080 return NULL;
1081}
1082
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001083static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1084{
1085 list_del(&sp->oos_link);
1086 --kvm->stat.mmu_unsync_global;
1087}
1088
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001089static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1090{
1091 WARN_ON(!sp->unsync);
1092 sp->unsync = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001093 if (sp->global)
1094 kvm_unlink_unsync_global(kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001095 --kvm->stat.mmu_unsync;
1096}
1097
1098static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1099
1100static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1101{
1102 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1103 kvm_mmu_zap_page(vcpu->kvm, sp);
1104 return 1;
1105 }
1106
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001107 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1108 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001109 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001110 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1111 kvm_mmu_zap_page(vcpu->kvm, sp);
1112 return 1;
1113 }
1114
1115 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001116 return 0;
1117}
1118
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001119struct mmu_page_path {
1120 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1121 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001122};
1123
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001124#define for_each_sp(pvec, sp, parents, i) \
1125 for (i = mmu_pages_next(&pvec, &parents, -1), \
1126 sp = pvec.page[i].sp; \
1127 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1128 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001129
Hannes Edercded19f2009-02-21 02:19:13 +01001130static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1131 struct mmu_page_path *parents,
1132 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001133{
1134 int n;
1135
1136 for (n = i+1; n < pvec->nr; n++) {
1137 struct kvm_mmu_page *sp = pvec->page[n].sp;
1138
1139 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1140 parents->idx[0] = pvec->page[n].idx;
1141 return n;
1142 }
1143
1144 parents->parent[sp->role.level-2] = sp;
1145 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1146 }
1147
1148 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001149}
1150
Hannes Edercded19f2009-02-21 02:19:13 +01001151static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001152{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001153 struct kvm_mmu_page *sp;
1154 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001155
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001156 do {
1157 unsigned int idx = parents->idx[level];
1158
1159 sp = parents->parent[level];
1160 if (!sp)
1161 return;
1162
1163 --sp->unsync_children;
1164 WARN_ON((int)sp->unsync_children < 0);
1165 __clear_bit(idx, sp->unsync_child_bitmap);
1166 level++;
1167 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1168}
1169
1170static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1171 struct mmu_page_path *parents,
1172 struct kvm_mmu_pages *pvec)
1173{
1174 parents->parent[parent->role.level-1] = NULL;
1175 pvec->nr = 0;
1176}
1177
1178static void mmu_sync_children(struct kvm_vcpu *vcpu,
1179 struct kvm_mmu_page *parent)
1180{
1181 int i;
1182 struct kvm_mmu_page *sp;
1183 struct mmu_page_path parents;
1184 struct kvm_mmu_pages pages;
1185
1186 kvm_mmu_pages_init(parent, &parents, &pages);
1187 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001188 int protected = 0;
1189
1190 for_each_sp(pages, sp, parents, i)
1191 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1192
1193 if (protected)
1194 kvm_flush_remote_tlbs(vcpu->kvm);
1195
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001196 for_each_sp(pages, sp, parents, i) {
1197 kvm_sync_page(vcpu, sp);
1198 mmu_pages_clear_parents(&parents);
1199 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001200 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001201 kvm_mmu_pages_init(parent, &parents, &pages);
1202 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001203}
1204
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001205static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1206 gfn_t gfn,
1207 gva_t gaddr,
1208 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001209 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001210 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001211 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001212{
1213 union kvm_mmu_page_role role;
1214 unsigned index;
1215 unsigned quadrant;
1216 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001217 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001218 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001219
Avi Kivitya770f6f2008-12-21 19:20:09 +02001220 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001221 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001222 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001223 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001224 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001225 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1226 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1227 role.quadrant = quadrant;
1228 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001229 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001230 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001231 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001232 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1234 if (sp->gfn == gfn) {
1235 if (sp->unsync)
1236 if (kvm_sync_page(vcpu, sp))
1237 continue;
1238
1239 if (sp->role.word != role.word)
1240 continue;
1241
Avi Kivity4db35312007-11-21 15:28:32 +02001242 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001243 if (sp->unsync_children) {
1244 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1245 kvm_mmu_mark_parents_unsync(vcpu, sp);
1246 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001247 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001248 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001249 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001250 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001251 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1252 if (!sp)
1253 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001254 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001255 sp->gfn = gfn;
1256 sp->role = role;
Marcelo Tosattibf47a762009-04-05 14:54:46 -03001257 sp->global = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001258 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001259 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001260 if (rmap_write_protect(vcpu->kvm, gfn))
1261 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001262 account_shadowed(vcpu->kvm, gfn);
1263 }
Avi Kivity131d8272008-05-29 14:56:28 +03001264 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1265 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1266 else
1267 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001268 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001269}
1270
Avi Kivity2d111232008-12-25 14:39:47 +02001271static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1272 struct kvm_vcpu *vcpu, u64 addr)
1273{
1274 iterator->addr = addr;
1275 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1276 iterator->level = vcpu->arch.mmu.shadow_root_level;
1277 if (iterator->level == PT32E_ROOT_LEVEL) {
1278 iterator->shadow_addr
1279 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1280 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1281 --iterator->level;
1282 if (!iterator->shadow_addr)
1283 iterator->level = 0;
1284 }
1285}
1286
1287static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1288{
1289 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1290 return false;
1291 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1292 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1293 return true;
1294}
1295
1296static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1297{
1298 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1299 --iterator->level;
1300}
1301
Avi Kivity90cb0522007-07-17 13:04:56 +03001302static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001303 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001304{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001305 unsigned i;
1306 u64 *pt;
1307 u64 ent;
1308
Avi Kivity4db35312007-11-21 15:28:32 +02001309 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001310
Avi Kivity4db35312007-11-21 15:28:32 +02001311 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001312 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001313 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001314 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001315 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001316 }
1317 return;
1318 }
1319
1320 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1321 ent = pt[i];
1322
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001323 if (is_shadow_present_pte(ent)) {
1324 if (!is_large_pte(ent)) {
1325 ent &= PT64_BASE_ADDR_MASK;
1326 mmu_page_remove_parent_pte(page_header(ent),
1327 &pt[i]);
1328 } else {
1329 --kvm->stat.lpages;
1330 rmap_remove(kvm, &pt[i]);
1331 }
1332 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001333 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001334 }
Avi Kivitya4360362007-01-05 16:36:45 -08001335}
1336
Avi Kivity4db35312007-11-21 15:28:32 +02001337static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001338{
Avi Kivity4db35312007-11-21 15:28:32 +02001339 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001340}
1341
Avi Kivity12b7d282007-09-23 14:10:49 +02001342static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1343{
1344 int i;
1345
1346 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1347 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001348 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001349}
1350
Avi Kivity31aa2b42008-07-11 17:59:46 +03001351static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001352{
1353 u64 *parent_pte;
1354
Avi Kivity4db35312007-11-21 15:28:32 +02001355 while (sp->multimapped || sp->parent_pte) {
1356 if (!sp->multimapped)
1357 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001358 else {
1359 struct kvm_pte_chain *chain;
1360
Avi Kivity4db35312007-11-21 15:28:32 +02001361 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001362 struct kvm_pte_chain, link);
1363 parent_pte = chain->parent_ptes[0];
1364 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001365 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001366 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001367 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001368 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001369}
1370
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001371static int mmu_zap_unsync_children(struct kvm *kvm,
1372 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001374 int i, zapped = 0;
1375 struct mmu_page_path parents;
1376 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001377
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001378 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001379 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001380
1381 kvm_mmu_pages_init(parent, &parents, &pages);
1382 while (mmu_unsync_walk(parent, &pages)) {
1383 struct kvm_mmu_page *sp;
1384
1385 for_each_sp(pages, sp, parents, i) {
1386 kvm_mmu_zap_page(kvm, sp);
1387 mmu_pages_clear_parents(&parents);
1388 }
1389 zapped += pages.nr;
1390 kvm_mmu_pages_init(parent, &parents, &pages);
1391 }
1392
1393 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001394}
1395
Marcelo Tosatti07385412008-09-23 13:18:37 -03001396static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001397{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001398 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001399 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001400 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001401 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001402 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001403 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001404 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001405 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001406 if (sp->unsync)
1407 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001408 if (!sp->root_count) {
1409 hlist_del(&sp->hash_link);
1410 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001411 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001412 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001413 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001414 kvm_reload_remote_mmus(kvm);
1415 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001416 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001417 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001418}
1419
Izik Eidus82ce2c92007-10-02 18:52:55 +02001420/*
1421 * Changing the number of mmu pages allocated to the vm
1422 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1423 */
1424void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1425{
1426 /*
1427 * If we set the number of mmu pages to be smaller be than the
1428 * number of actived pages , we must to free some mmu pages before we
1429 * change the value
1430 */
1431
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001432 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001433 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001434 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1435 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001436
1437 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1438 struct kvm_mmu_page *page;
1439
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001440 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001441 struct kvm_mmu_page, link);
1442 kvm_mmu_zap_page(kvm, page);
1443 n_used_mmu_pages--;
1444 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001445 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001446 }
1447 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001448 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1449 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001450
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001451 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001452}
1453
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001454static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001455{
1456 unsigned index;
1457 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001458 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001459 struct hlist_node *node, *n;
1460 int r;
1461
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001462 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001463 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001464 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001465 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001466 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001467 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001468 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001469 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001470 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001471 if (kvm_mmu_zap_page(kvm, sp))
1472 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001473 }
1474 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001475}
1476
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001477static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001478{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001479 unsigned index;
1480 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001481 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001482 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001483
Avi Kivity4677a3b2009-01-06 13:00:27 +02001484 index = kvm_page_table_hashfn(gfn);
1485 bucket = &kvm->arch.mmu_page_hash[index];
1486 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001487 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001488 && !sp->role.invalid) {
1489 pgprintk("%s: zap %lx %x\n",
1490 __func__, gfn, sp->role.word);
1491 kvm_mmu_zap_page(kvm, sp);
1492 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001493 }
1494}
1495
Avi Kivity38c335f2007-11-21 14:20:22 +02001496static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497{
Avi Kivity38c335f2007-11-21 14:20:22 +02001498 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001499 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500
Sheng Yang291f26b2008-10-16 17:30:57 +08001501 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502}
1503
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001504static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1505{
1506 int i;
1507 u64 *pt = sp->spt;
1508
1509 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1510 return;
1511
1512 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1513 if (pt[i] == shadow_notrap_nonpresent_pte)
1514 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1515 }
1516}
1517
Avi Kivity039576c2007-03-20 12:46:50 +02001518struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1519{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001520 struct page *page;
1521
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001522 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001523
1524 if (gpa == UNMAPPED_GVA)
1525 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001526
Izik Eidus72dc67a2008-02-10 18:04:15 +02001527 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001528
1529 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001530}
1531
Sheng Yang74be52e2008-10-09 16:01:56 +08001532/*
1533 * The function is based on mtrr_type_lookup() in
1534 * arch/x86/kernel/cpu/mtrr/generic.c
1535 */
1536static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1537 u64 start, u64 end)
1538{
1539 int i;
1540 u64 base, mask;
1541 u8 prev_match, curr_match;
1542 int num_var_ranges = KVM_NR_VAR_MTRR;
1543
1544 if (!mtrr_state->enabled)
1545 return 0xFF;
1546
1547 /* Make end inclusive end, instead of exclusive */
1548 end--;
1549
1550 /* Look in fixed ranges. Just return the type as per start */
1551 if (mtrr_state->have_fixed && (start < 0x100000)) {
1552 int idx;
1553
1554 if (start < 0x80000) {
1555 idx = 0;
1556 idx += (start >> 16);
1557 return mtrr_state->fixed_ranges[idx];
1558 } else if (start < 0xC0000) {
1559 idx = 1 * 8;
1560 idx += ((start - 0x80000) >> 14);
1561 return mtrr_state->fixed_ranges[idx];
1562 } else if (start < 0x1000000) {
1563 idx = 3 * 8;
1564 idx += ((start - 0xC0000) >> 12);
1565 return mtrr_state->fixed_ranges[idx];
1566 }
1567 }
1568
1569 /*
1570 * Look in variable ranges
1571 * Look of multiple ranges matching this address and pick type
1572 * as per MTRR precedence
1573 */
1574 if (!(mtrr_state->enabled & 2))
1575 return mtrr_state->def_type;
1576
1577 prev_match = 0xFF;
1578 for (i = 0; i < num_var_ranges; ++i) {
1579 unsigned short start_state, end_state;
1580
1581 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1582 continue;
1583
1584 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1585 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1586 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1587 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1588
1589 start_state = ((start & mask) == (base & mask));
1590 end_state = ((end & mask) == (base & mask));
1591 if (start_state != end_state)
1592 return 0xFE;
1593
1594 if ((start & mask) != (base & mask))
1595 continue;
1596
1597 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1598 if (prev_match == 0xFF) {
1599 prev_match = curr_match;
1600 continue;
1601 }
1602
1603 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1604 curr_match == MTRR_TYPE_UNCACHABLE)
1605 return MTRR_TYPE_UNCACHABLE;
1606
1607 if ((prev_match == MTRR_TYPE_WRBACK &&
1608 curr_match == MTRR_TYPE_WRTHROUGH) ||
1609 (prev_match == MTRR_TYPE_WRTHROUGH &&
1610 curr_match == MTRR_TYPE_WRBACK)) {
1611 prev_match = MTRR_TYPE_WRTHROUGH;
1612 curr_match = MTRR_TYPE_WRTHROUGH;
1613 }
1614
1615 if (prev_match != curr_match)
1616 return MTRR_TYPE_UNCACHABLE;
1617 }
1618
1619 if (prev_match != 0xFF)
1620 return prev_match;
1621
1622 return mtrr_state->def_type;
1623}
1624
1625static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1626{
1627 u8 mtrr;
1628
1629 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1630 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1631 if (mtrr == 0xfe || mtrr == 0xff)
1632 mtrr = MTRR_TYPE_WRBACK;
1633 return mtrr;
1634}
1635
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001636static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1637{
1638 unsigned index;
1639 struct hlist_head *bucket;
1640 struct kvm_mmu_page *s;
1641 struct hlist_node *node, *n;
1642
1643 index = kvm_page_table_hashfn(sp->gfn);
1644 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1645 /* don't unsync if pagetable is shadowed with multiple roles */
1646 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001647 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001648 continue;
1649 if (s->role.word != sp->role.word)
1650 return 1;
1651 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001652 ++vcpu->kvm->stat.mmu_unsync;
1653 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001654
1655 if (sp->global) {
1656 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1657 ++vcpu->kvm->stat.mmu_unsync_global;
1658 } else
1659 kvm_mmu_mark_parents_unsync(vcpu, sp);
1660
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001661 mmu_convert_notrap(sp);
1662 return 0;
1663}
1664
1665static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1666 bool can_unsync)
1667{
1668 struct kvm_mmu_page *shadow;
1669
1670 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1671 if (shadow) {
1672 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1673 return 1;
1674 if (shadow->unsync)
1675 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001676 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001677 return kvm_unsync_page(vcpu, shadow);
1678 return 1;
1679 }
1680 return 0;
1681}
1682
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001683static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1684 unsigned pte_access, int user_fault,
1685 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001686 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001687 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001688{
1689 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001690 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001691 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001692 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1693
1694 if (!global && sp->global) {
1695 sp->global = 0;
1696 if (sp->unsync) {
1697 kvm_unlink_unsync_global(vcpu->kvm, sp);
1698 kvm_mmu_mark_parents_unsync(vcpu, sp);
1699 }
1700 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001701
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001702 /*
1703 * We don't set the accessed bit, since we sometimes want to see
1704 * whether the guest actually used the pte (in order to detect
1705 * demand paging).
1706 */
Sheng Yang7b523452008-04-25 21:13:50 +08001707 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001708 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001709 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001710 if (!dirty)
1711 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001712 if (pte_access & ACC_EXEC_MASK)
1713 spte |= shadow_x_mask;
1714 else
1715 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001716 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001717 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001718 if (largepage)
1719 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001720 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001721 if (!kvm_is_mmio_pfn(pfn)) {
1722 mt_mask = get_memory_type(vcpu, gfn) <<
1723 kvm_x86_ops->get_mt_mask_shift();
1724 mt_mask |= VMX_EPT_IGMT_BIT;
1725 } else
1726 mt_mask = MTRR_TYPE_UNCACHABLE <<
1727 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001728 spte |= mt_mask;
1729 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001730
Anthony Liguori35149e22008-04-02 14:46:56 -05001731 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001732
1733 if ((pte_access & ACC_WRITE_MASK)
1734 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001735
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001736 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1737 ret = 1;
1738 spte = shadow_trap_nonpresent_pte;
1739 goto set_pte;
1740 }
1741
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001742 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001743
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001744 /*
1745 * Optimization: for pte sync, if spte was writable the hash
1746 * lookup is unnecessary (and expensive). Write protection
1747 * is responsibility of mmu_get_page / kvm_sync_page.
1748 * Same reasoning can be applied to dirty page accounting.
1749 */
1750 if (!can_unsync && is_writeble_pte(*shadow_pte))
1751 goto set_pte;
1752
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001753 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001754 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001755 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001756 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001757 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001758 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001759 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001760 }
1761 }
1762
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001763 if (pte_access & ACC_WRITE_MASK)
1764 mark_page_dirty(vcpu->kvm, gfn);
1765
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001766set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001767 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001768 return ret;
1769}
1770
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001771static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1772 unsigned pt_access, unsigned pte_access,
1773 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001774 int *ptwrite, int largepage, int global,
1775 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001776{
1777 int was_rmapped = 0;
1778 int was_writeble = is_writeble_pte(*shadow_pte);
1779
1780 pgprintk("%s: spte %llx access %x write_fault %d"
1781 " user_fault %d gfn %lx\n",
1782 __func__, *shadow_pte, pt_access,
1783 write_fault, user_fault, gfn);
1784
1785 if (is_rmap_pte(*shadow_pte)) {
1786 /*
1787 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1788 * the parent of the now unreachable PTE.
1789 */
1790 if (largepage && !is_large_pte(*shadow_pte)) {
1791 struct kvm_mmu_page *child;
1792 u64 pte = *shadow_pte;
1793
1794 child = page_header(pte & PT64_BASE_ADDR_MASK);
1795 mmu_page_remove_parent_pte(child, shadow_pte);
1796 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1797 pgprintk("hfn old %lx new %lx\n",
1798 spte_to_pfn(*shadow_pte), pfn);
1799 rmap_remove(vcpu->kvm, shadow_pte);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001800 } else
1801 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001802 }
1803 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001804 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001805 if (write_fault)
1806 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001807 kvm_x86_ops->tlb_flush(vcpu);
1808 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001809
1810 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1811 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1812 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1813 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1814 *shadow_pte, shadow_pte);
1815 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001816 ++vcpu->kvm->stat.lpages;
1817
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001818 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1819 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001820 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001821 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001822 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001823 } else {
1824 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001825 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001826 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001827 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001828 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001829 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001830 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001831 vcpu->arch.last_pte_gfn = gfn;
1832 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001833}
1834
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1836{
1837}
1838
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001839static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001840 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841{
Avi Kivity9f652d22008-12-25 14:54:25 +02001842 struct kvm_shadow_walk_iterator iterator;
1843 struct kvm_mmu_page *sp;
1844 int pt_write = 0;
1845 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846
Avi Kivity9f652d22008-12-25 14:54:25 +02001847 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1848 if (iterator.level == PT_PAGE_TABLE_LEVEL
1849 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1850 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1851 0, write, 1, &pt_write,
1852 largepage, 0, gfn, pfn, false);
1853 ++vcpu->stat.pf_fixed;
1854 break;
1855 }
1856
1857 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1858 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1859 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1860 iterator.level - 1,
1861 1, ACC_ALL, iterator.sptep);
1862 if (!sp) {
1863 pgprintk("nonpaging_map: ENOMEM\n");
1864 kvm_release_pfn_clean(pfn);
1865 return -ENOMEM;
1866 }
1867
1868 set_shadow_pte(iterator.sptep,
1869 __pa(sp->spt)
1870 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1871 | shadow_user_mask | shadow_x_mask);
1872 }
1873 }
1874 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875}
1876
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001877static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1878{
1879 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001880 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001881 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001882 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001883
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001884 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1885 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1886 largepage = 1;
1887 }
1888
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001889 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001890 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001891 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001892
Avi Kivityd196e342008-01-24 11:44:11 +02001893 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001894 if (is_error_pfn(pfn)) {
1895 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001896 return 1;
1897 }
1898
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001899 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001900 if (mmu_notifier_retry(vcpu, mmu_seq))
1901 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001902 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001903 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001904 spin_unlock(&vcpu->kvm->mmu_lock);
1905
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001906
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001907 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001908
1909out_unlock:
1910 spin_unlock(&vcpu->kvm->mmu_lock);
1911 kvm_release_pfn_clean(pfn);
1912 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001913}
1914
1915
Avi Kivity17ac10a2007-01-05 16:36:40 -08001916static void mmu_free_roots(struct kvm_vcpu *vcpu)
1917{
1918 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001919 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001920
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001921 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001922 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001923 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001924 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1925 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001926
Avi Kivity4db35312007-11-21 15:28:32 +02001927 sp = page_header(root);
1928 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001929 if (!sp->root_count && sp->role.invalid)
1930 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001931 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001932 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001933 return;
1934 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001935 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001936 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001937
Avi Kivity417726a2007-04-12 17:35:58 +03001938 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001939 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001940 sp = page_header(root);
1941 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001942 if (!sp->root_count && sp->role.invalid)
1943 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001944 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001945 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001946 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001947 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001948 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001949}
1950
1951static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1952{
1953 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001954 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001955 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001956 int direct = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001957
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001958 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001959
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001960 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1961 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001962
1963 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001964 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001965 direct = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001966 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001967 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001968 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001969 root = __pa(sp->spt);
1970 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001971 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001972 return;
1973 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001974 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001975 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001976 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001977 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001978 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001979
1980 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001981 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1982 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1983 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001984 continue;
1985 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001986 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1987 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001988 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001989 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001990 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001991 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001992 root = __pa(sp->spt);
1993 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001994 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001995 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001996 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001997}
1998
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001999static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2000{
2001 int i;
2002 struct kvm_mmu_page *sp;
2003
2004 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2005 return;
2006 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2007 hpa_t root = vcpu->arch.mmu.root_hpa;
2008 sp = page_header(root);
2009 mmu_sync_children(vcpu, sp);
2010 return;
2011 }
2012 for (i = 0; i < 4; ++i) {
2013 hpa_t root = vcpu->arch.mmu.pae_root[i];
2014
2015 if (root) {
2016 root &= PT64_BASE_ADDR_MASK;
2017 sp = page_header(root);
2018 mmu_sync_children(vcpu, sp);
2019 }
2020 }
2021}
2022
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002023static void mmu_sync_global(struct kvm_vcpu *vcpu)
2024{
2025 struct kvm *kvm = vcpu->kvm;
2026 struct kvm_mmu_page *sp, *n;
2027
2028 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2029 kvm_sync_page(vcpu, sp);
2030}
2031
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002032void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2033{
2034 spin_lock(&vcpu->kvm->mmu_lock);
2035 mmu_sync_roots(vcpu);
2036 spin_unlock(&vcpu->kvm->mmu_lock);
2037}
2038
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002039void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2040{
2041 spin_lock(&vcpu->kvm->mmu_lock);
2042 mmu_sync_global(vcpu);
2043 spin_unlock(&vcpu->kvm->mmu_lock);
2044}
2045
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2047{
2048 return vaddr;
2049}
2050
2051static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002052 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053{
Avi Kivitye8332402007-12-09 18:43:00 +02002054 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002055 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002057 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002058 r = mmu_topup_memory_caches(vcpu);
2059 if (r)
2060 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002061
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002063 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064
Avi Kivitye8332402007-12-09 18:43:00 +02002065 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066
Avi Kivitye8332402007-12-09 18:43:00 +02002067 return nonpaging_map(vcpu, gva & PAGE_MASK,
2068 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002069}
2070
Joerg Roedelfb72d162008-02-07 13:47:44 +01002071static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2072 u32 error_code)
2073{
Anthony Liguori35149e22008-04-02 14:46:56 -05002074 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002075 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002076 int largepage = 0;
2077 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002078 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002079
2080 ASSERT(vcpu);
2081 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2082
2083 r = mmu_topup_memory_caches(vcpu);
2084 if (r)
2085 return r;
2086
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002087 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2088 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2089 largepage = 1;
2090 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002091 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002092 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002093 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002094 if (is_error_pfn(pfn)) {
2095 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002096 return 1;
2097 }
2098 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002099 if (mmu_notifier_retry(vcpu, mmu_seq))
2100 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002101 kvm_mmu_free_some_pages(vcpu);
2102 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002103 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002104 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002105
2106 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002107
2108out_unlock:
2109 spin_unlock(&vcpu->kvm->mmu_lock);
2110 kvm_release_pfn_clean(pfn);
2111 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void nonpaging_free(struct kvm_vcpu *vcpu)
2115{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002116 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117}
2118
2119static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2120{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002121 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
2123 context->new_cr3 = nonpaging_new_cr3;
2124 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 context->gva_to_gpa = nonpaging_gva_to_gpa;
2126 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002127 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002128 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002129 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002130 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002132 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 return 0;
2134}
2135
Avi Kivityd835dfe2007-11-21 02:57:59 +02002136void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002138 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002139 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140}
2141
2142static void paging_new_cr3(struct kvm_vcpu *vcpu)
2143{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002144 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002145 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146}
2147
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148static void inject_page_fault(struct kvm_vcpu *vcpu,
2149 u64 addr,
2150 u32 err_code)
2151{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002152 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155static void paging_free(struct kvm_vcpu *vcpu)
2156{
2157 nonpaging_free(vcpu);
2158}
2159
Dong, Eddie82725b22009-03-30 16:21:08 +08002160static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2161{
2162 int bit7;
2163
2164 bit7 = (gpte >> 7) & 1;
2165 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2166}
2167
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168#define PTTYPE 64
2169#include "paging_tmpl.h"
2170#undef PTTYPE
2171
2172#define PTTYPE 32
2173#include "paging_tmpl.h"
2174#undef PTTYPE
2175
Dong, Eddie82725b22009-03-30 16:21:08 +08002176static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2177{
2178 struct kvm_mmu *context = &vcpu->arch.mmu;
2179 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2180 u64 exb_bit_rsvd = 0;
2181
2182 if (!is_nx(vcpu))
2183 exb_bit_rsvd = rsvd_bits(63, 63);
2184 switch (level) {
2185 case PT32_ROOT_LEVEL:
2186 /* no rsvd bits for 2 level 4K page table entries */
2187 context->rsvd_bits_mask[0][1] = 0;
2188 context->rsvd_bits_mask[0][0] = 0;
2189 if (is_cpuid_PSE36())
2190 /* 36bits PSE 4MB page */
2191 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2192 else
2193 /* 32 bits PSE 4MB page */
2194 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2195 context->rsvd_bits_mask[1][0] = ~0ull;
2196 break;
2197 case PT32E_ROOT_LEVEL:
2198 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2199 rsvd_bits(maxphyaddr, 62); /* PDE */
2200 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2201 rsvd_bits(maxphyaddr, 62); /* PTE */
2202 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2203 rsvd_bits(maxphyaddr, 62) |
2204 rsvd_bits(13, 20); /* large page */
2205 context->rsvd_bits_mask[1][0] = ~0ull;
2206 break;
2207 case PT64_ROOT_LEVEL:
2208 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2209 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2210 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2211 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2212 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2213 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2214 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2215 rsvd_bits(maxphyaddr, 51);
2216 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2217 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2218 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2219 rsvd_bits(maxphyaddr, 51) | rsvd_bits(13, 20);
2220 context->rsvd_bits_mask[1][0] = ~0ull;
2221 break;
2222 }
2223}
2224
Avi Kivity17ac10a2007-01-05 16:36:40 -08002225static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002227 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228
2229 ASSERT(is_pae(vcpu));
2230 context->new_cr3 = paging_new_cr3;
2231 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002233 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002234 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002235 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002237 context->root_level = level;
2238 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002239 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 return 0;
2241}
2242
Avi Kivity17ac10a2007-01-05 16:36:40 -08002243static int paging64_init_context(struct kvm_vcpu *vcpu)
2244{
Dong, Eddie82725b22009-03-30 16:21:08 +08002245 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002246 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2247}
2248
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249static int paging32_init_context(struct kvm_vcpu *vcpu)
2250{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002251 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252
Dong, Eddie82725b22009-03-30 16:21:08 +08002253 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 context->new_cr3 = paging_new_cr3;
2255 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 context->gva_to_gpa = paging32_gva_to_gpa;
2257 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002258 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002259 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002260 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 context->root_level = PT32_ROOT_LEVEL;
2262 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002263 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264 return 0;
2265}
2266
2267static int paging32E_init_context(struct kvm_vcpu *vcpu)
2268{
Dong, Eddie82725b22009-03-30 16:21:08 +08002269 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002270 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271}
2272
Joerg Roedelfb72d162008-02-07 13:47:44 +01002273static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2274{
2275 struct kvm_mmu *context = &vcpu->arch.mmu;
2276
2277 context->new_cr3 = nonpaging_new_cr3;
2278 context->page_fault = tdp_page_fault;
2279 context->free = nonpaging_free;
2280 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002281 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002282 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002283 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002284 context->root_hpa = INVALID_PAGE;
2285
2286 if (!is_paging(vcpu)) {
2287 context->gva_to_gpa = nonpaging_gva_to_gpa;
2288 context->root_level = 0;
2289 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002290 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002291 context->gva_to_gpa = paging64_gva_to_gpa;
2292 context->root_level = PT64_ROOT_LEVEL;
2293 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002294 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002295 context->gva_to_gpa = paging64_gva_to_gpa;
2296 context->root_level = PT32E_ROOT_LEVEL;
2297 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002298 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002299 context->gva_to_gpa = paging32_gva_to_gpa;
2300 context->root_level = PT32_ROOT_LEVEL;
2301 }
2302
2303 return 0;
2304}
2305
2306static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002308 int r;
2309
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002311 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
2313 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002314 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002315 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002316 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002318 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002320 r = paging32_init_context(vcpu);
2321
2322 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2323
2324 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325}
2326
Joerg Roedelfb72d162008-02-07 13:47:44 +01002327static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2328{
Anthony Liguori35149e22008-04-02 14:46:56 -05002329 vcpu->arch.update_pte.pfn = bad_pfn;
2330
Joerg Roedelfb72d162008-02-07 13:47:44 +01002331 if (tdp_enabled)
2332 return init_kvm_tdp_mmu(vcpu);
2333 else
2334 return init_kvm_softmmu(vcpu);
2335}
2336
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2338{
2339 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002340 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2341 vcpu->arch.mmu.free(vcpu);
2342 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 }
2344}
2345
2346int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2347{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002348 destroy_kvm_mmu(vcpu);
2349 return init_kvm_mmu(vcpu);
2350}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002351EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002352
2353int kvm_mmu_load(struct kvm_vcpu *vcpu)
2354{
Avi Kivity714b93d2007-01-05 16:36:53 -08002355 int r;
2356
Avi Kivitye2dec932007-01-05 16:36:54 -08002357 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002358 if (r)
2359 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002360 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002361 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002362 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002363 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002364 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002365 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002366 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002367out:
2368 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002370EXPORT_SYMBOL_GPL(kvm_mmu_load);
2371
2372void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2373{
2374 mmu_free_roots(vcpu);
2375}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376
Avi Kivity09072da2007-05-01 14:16:52 +03002377static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002378 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002379 u64 *spte)
2380{
2381 u64 pte;
2382 struct kvm_mmu_page *child;
2383
2384 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002385 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002386 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2387 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002388 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002389 else {
2390 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002391 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002392 }
2393 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002394 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002395 if (is_large_pte(pte))
2396 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002397}
2398
Avi Kivity00284252007-05-01 16:53:31 +03002399static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002400 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002401 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002402 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002403{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002404 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2405 if (!vcpu->arch.update_pte.largepage ||
2406 sp->role.glevels == PT32_ROOT_LEVEL) {
2407 ++vcpu->kvm->stat.mmu_pde_zapped;
2408 return;
2409 }
2410 }
Avi Kivity00284252007-05-01 16:53:31 +03002411
Avi Kivity4cee5762007-11-18 16:37:07 +02002412 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002413 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002414 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002415 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002416 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002417}
2418
Avi Kivity79539ce2007-11-21 02:06:21 +02002419static bool need_remote_flush(u64 old, u64 new)
2420{
2421 if (!is_shadow_present_pte(old))
2422 return false;
2423 if (!is_shadow_present_pte(new))
2424 return true;
2425 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2426 return true;
2427 old ^= PT64_NX_MASK;
2428 new ^= PT64_NX_MASK;
2429 return (old & ~new & PT64_PERM_MASK) != 0;
2430}
2431
2432static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2433{
2434 if (need_remote_flush(old, new))
2435 kvm_flush_remote_tlbs(vcpu->kvm);
2436 else
2437 kvm_mmu_flush_tlb(vcpu);
2438}
2439
Avi Kivity12b7d282007-09-23 14:10:49 +02002440static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2441{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002442 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002443
Sheng Yang7b523452008-04-25 21:13:50 +08002444 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002445}
2446
Avi Kivityd7824ff2007-12-30 12:29:05 +02002447static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2448 const u8 *new, int bytes)
2449{
2450 gfn_t gfn;
2451 int r;
2452 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002453 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002454
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002455 vcpu->arch.update_pte.largepage = 0;
2456
Avi Kivityd7824ff2007-12-30 12:29:05 +02002457 if (bytes != 4 && bytes != 8)
2458 return;
2459
2460 /*
2461 * Assume that the pte write on a page table of the same type
2462 * as the current vcpu paging mode. This is nearly always true
2463 * (might be false while changing modes). Note it is verified later
2464 * by update_pte().
2465 */
2466 if (is_pae(vcpu)) {
2467 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2468 if ((bytes == 4) && (gpa % 4 == 0)) {
2469 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2470 if (r)
2471 return;
2472 memcpy((void *)&gpte + (gpa % 8), new, 4);
2473 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2474 memcpy((void *)&gpte, new, 8);
2475 }
2476 } else {
2477 if ((bytes == 4) && (gpa % 4 == 0))
2478 memcpy((void *)&gpte, new, 4);
2479 }
2480 if (!is_present_pte(gpte))
2481 return;
2482 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002483
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002484 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2485 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2486 vcpu->arch.update_pte.largepage = 1;
2487 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002488 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002489 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002490 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002491
Anthony Liguori35149e22008-04-02 14:46:56 -05002492 if (is_error_pfn(pfn)) {
2493 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002494 return;
2495 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002496 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002497 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002498}
2499
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002500static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2501{
2502 u64 *spte = vcpu->arch.last_pte_updated;
2503
2504 if (spte
2505 && vcpu->arch.last_pte_gfn == gfn
2506 && shadow_accessed_mask
2507 && !(*spte & shadow_accessed_mask)
2508 && is_shadow_present_pte(*spte))
2509 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2510}
2511
Avi Kivity09072da2007-05-01 14:16:52 +03002512void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002513 const u8 *new, int bytes,
2514 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002515{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002516 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002517 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002518 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002519 struct hlist_head *bucket;
2520 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002521 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002522 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002523 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002524 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002525 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002526 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002527 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002528 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002529 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002530 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002531 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002532
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002533 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002534 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002535 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002536 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002537 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002538 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002539 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002540 if (guest_initiated) {
2541 if (gfn == vcpu->arch.last_pt_write_gfn
2542 && !last_updated_pte_accessed(vcpu)) {
2543 ++vcpu->arch.last_pt_write_count;
2544 if (vcpu->arch.last_pt_write_count >= 3)
2545 flooded = 1;
2546 } else {
2547 vcpu->arch.last_pt_write_gfn = gfn;
2548 vcpu->arch.last_pt_write_count = 1;
2549 vcpu->arch.last_pte_updated = NULL;
2550 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002551 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002552 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002553 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002554 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002555 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002556 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002557 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002558 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002559 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002560 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002561 /*
2562 * Misaligned accesses are too much trouble to fix
2563 * up; also, they usually indicate a page is not used
2564 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002565 *
2566 * If we're seeing too many writes to a page,
2567 * it may no longer be a page table, or we may be
2568 * forking, in which case it is better to unmap the
2569 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002570 */
2571 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002572 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002573 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2574 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002575 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002576 continue;
2577 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002578 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002579 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002580 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002581 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002582 page_offset <<= 1; /* 32->64 */
2583 /*
2584 * A 32-bit pde maps 4MB while the shadow pdes map
2585 * only 2MB. So we need to double the offset again
2586 * and zap two pdes instead of one.
2587 */
2588 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002589 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002590 page_offset <<= 1;
2591 npte = 2;
2592 }
Avi Kivityfce06572007-05-01 16:44:05 +03002593 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002594 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002595 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002596 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002597 }
Avi Kivity4db35312007-11-21 15:28:32 +02002598 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002599 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2600 gentry = 0;
2601 r = kvm_read_guest_atomic(vcpu->kvm,
2602 gpa & ~(u64)(pte_size - 1),
2603 &gentry, pte_size);
2604 new = (const void *)&gentry;
2605 if (r < 0)
2606 new = NULL;
2607 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002608 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002609 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002610 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002611 if (new)
2612 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002613 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002614 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002615 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002616 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002617 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002618 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002619 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2620 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2621 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002622 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002623}
2624
Avi Kivitya4360362007-01-05 16:36:45 -08002625int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2626{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002627 gpa_t gpa;
2628 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002629
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002630 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002631
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002632 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002633 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002634 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002635 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002636}
Avi Kivity577bdc42008-07-19 08:57:05 +03002637EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002638
Avi Kivity22d95b12007-09-14 20:26:06 +03002639void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002640{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002641 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002642 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002643
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002644 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002645 struct kvm_mmu_page, link);
2646 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002647 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002648 }
2649}
Avi Kivityebeace82007-01-05 16:36:47 -08002650
Avi Kivity30677142007-10-28 18:48:59 +02002651int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2652{
2653 int r;
2654 enum emulation_result er;
2655
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002656 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002657 if (r < 0)
2658 goto out;
2659
2660 if (!r) {
2661 r = 1;
2662 goto out;
2663 }
2664
Avi Kivityb733bfb2007-10-28 18:52:05 +02002665 r = mmu_topup_memory_caches(vcpu);
2666 if (r)
2667 goto out;
2668
Avi Kivity30677142007-10-28 18:48:59 +02002669 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002670
2671 switch (er) {
2672 case EMULATE_DONE:
2673 return 1;
2674 case EMULATE_DO_MMIO:
2675 ++vcpu->stat.mmio_exits;
2676 return 0;
2677 case EMULATE_FAIL:
2678 kvm_report_emulation_failure(vcpu, "pagetable");
2679 return 1;
2680 default:
2681 BUG();
2682 }
2683out:
Avi Kivity30677142007-10-28 18:48:59 +02002684 return r;
2685}
2686EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2687
Marcelo Tosattia7052892008-09-23 13:18:35 -03002688void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2689{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002690 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002691 kvm_mmu_flush_tlb(vcpu);
2692 ++vcpu->stat.invlpg;
2693}
2694EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2695
Joerg Roedel18552672008-02-07 13:47:41 +01002696void kvm_enable_tdp(void)
2697{
2698 tdp_enabled = true;
2699}
2700EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2701
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002702void kvm_disable_tdp(void)
2703{
2704 tdp_enabled = false;
2705}
2706EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2707
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708static void free_mmu_pages(struct kvm_vcpu *vcpu)
2709{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002710 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711}
2712
2713static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2714{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002715 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 int i;
2717
2718 ASSERT(vcpu);
2719
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002720 if (vcpu->kvm->arch.n_requested_mmu_pages)
2721 vcpu->kvm->arch.n_free_mmu_pages =
2722 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002723 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002724 vcpu->kvm->arch.n_free_mmu_pages =
2725 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002726 /*
2727 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2728 * Therefore we need to allocate shadow page tables in the first
2729 * 4GB of memory, which happens to fit the DMA32 zone.
2730 */
2731 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2732 if (!page)
2733 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002734 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002735 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002736 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002737
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 return 0;
2739
2740error_1:
2741 free_mmu_pages(vcpu);
2742 return -ENOMEM;
2743}
2744
Ingo Molnar8018c272006-12-29 16:50:01 -08002745int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002748 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749
Ingo Molnar8018c272006-12-29 16:50:01 -08002750 return alloc_mmu_pages(vcpu);
2751}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752
Ingo Molnar8018c272006-12-29 16:50:01 -08002753int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2754{
2755 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002756 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002757
Ingo Molnar8018c272006-12-29 16:50:01 -08002758 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759}
2760
2761void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2762{
2763 ASSERT(vcpu);
2764
2765 destroy_kvm_mmu(vcpu);
2766 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002767 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768}
2769
Avi Kivity90cb0522007-07-17 13:04:56 +03002770void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771{
Avi Kivity4db35312007-11-21 15:28:32 +02002772 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773
Avi Kivity2245a282008-08-27 16:32:24 +03002774 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002775 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 int i;
2777 u64 *pt;
2778
Sheng Yang291f26b2008-10-16 17:30:57 +08002779 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 continue;
2781
Avi Kivity4db35312007-11-21 15:28:32 +02002782 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2784 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002785 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787 }
Avi Kivity171d5952008-08-27 16:40:51 +03002788 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002789 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002791
Avi Kivity90cb0522007-07-17 13:04:56 +03002792void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002793{
Avi Kivity4db35312007-11-21 15:28:32 +02002794 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002795
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002796 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002797 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002798 if (kvm_mmu_zap_page(kvm, sp))
2799 node = container_of(kvm->arch.active_mmu_pages.next,
2800 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002801 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002802
Avi Kivity90cb0522007-07-17 13:04:56 +03002803 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002804}
2805
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002806static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002807{
2808 struct kvm_mmu_page *page;
2809
2810 page = container_of(kvm->arch.active_mmu_pages.prev,
2811 struct kvm_mmu_page, link);
2812 kvm_mmu_zap_page(kvm, page);
2813}
2814
2815static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2816{
2817 struct kvm *kvm;
2818 struct kvm *kvm_freed = NULL;
2819 int cache_count = 0;
2820
2821 spin_lock(&kvm_lock);
2822
2823 list_for_each_entry(kvm, &vm_list, vm_list) {
2824 int npages;
2825
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002826 if (!down_read_trylock(&kvm->slots_lock))
2827 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002828 spin_lock(&kvm->mmu_lock);
2829 npages = kvm->arch.n_alloc_mmu_pages -
2830 kvm->arch.n_free_mmu_pages;
2831 cache_count += npages;
2832 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2833 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2834 cache_count--;
2835 kvm_freed = kvm;
2836 }
2837 nr_to_scan--;
2838
2839 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002840 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002841 }
2842 if (kvm_freed)
2843 list_move_tail(&kvm_freed->vm_list, &vm_list);
2844
2845 spin_unlock(&kvm_lock);
2846
2847 return cache_count;
2848}
2849
2850static struct shrinker mmu_shrinker = {
2851 .shrink = mmu_shrink,
2852 .seeks = DEFAULT_SEEKS * 10,
2853};
2854
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002855static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002856{
2857 if (pte_chain_cache)
2858 kmem_cache_destroy(pte_chain_cache);
2859 if (rmap_desc_cache)
2860 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002861 if (mmu_page_header_cache)
2862 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002863}
2864
Izik Eidus3ee16c82008-03-30 15:17:21 +03002865void kvm_mmu_module_exit(void)
2866{
2867 mmu_destroy_caches();
2868 unregister_shrinker(&mmu_shrinker);
2869}
2870
Avi Kivityb5a33a72007-04-15 16:31:09 +03002871int kvm_mmu_module_init(void)
2872{
2873 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2874 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002875 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002876 if (!pte_chain_cache)
2877 goto nomem;
2878 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2879 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002880 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002881 if (!rmap_desc_cache)
2882 goto nomem;
2883
Avi Kivityd3d25b02007-05-30 12:34:53 +03002884 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2885 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002886 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002887 if (!mmu_page_header_cache)
2888 goto nomem;
2889
Izik Eidus3ee16c82008-03-30 15:17:21 +03002890 register_shrinker(&mmu_shrinker);
2891
Avi Kivityb5a33a72007-04-15 16:31:09 +03002892 return 0;
2893
2894nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002895 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002896 return -ENOMEM;
2897}
2898
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002899/*
2900 * Caculate mmu pages needed for kvm.
2901 */
2902unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2903{
2904 int i;
2905 unsigned int nr_mmu_pages;
2906 unsigned int nr_pages = 0;
2907
2908 for (i = 0; i < kvm->nmemslots; i++)
2909 nr_pages += kvm->memslots[i].npages;
2910
2911 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2912 nr_mmu_pages = max(nr_mmu_pages,
2913 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2914
2915 return nr_mmu_pages;
2916}
2917
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002918static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2919 unsigned len)
2920{
2921 if (len > buffer->len)
2922 return NULL;
2923 return buffer->ptr;
2924}
2925
2926static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2927 unsigned len)
2928{
2929 void *ret;
2930
2931 ret = pv_mmu_peek_buffer(buffer, len);
2932 if (!ret)
2933 return ret;
2934 buffer->ptr += len;
2935 buffer->len -= len;
2936 buffer->processed += len;
2937 return ret;
2938}
2939
2940static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2941 gpa_t addr, gpa_t value)
2942{
2943 int bytes = 8;
2944 int r;
2945
2946 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2947 bytes = 4;
2948
2949 r = mmu_topup_memory_caches(vcpu);
2950 if (r)
2951 return r;
2952
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002953 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002954 return -EFAULT;
2955
2956 return 1;
2957}
2958
2959static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2960{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002961 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002962 return 1;
2963}
2964
2965static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2966{
2967 spin_lock(&vcpu->kvm->mmu_lock);
2968 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2969 spin_unlock(&vcpu->kvm->mmu_lock);
2970 return 1;
2971}
2972
2973static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2974 struct kvm_pv_mmu_op_buffer *buffer)
2975{
2976 struct kvm_mmu_op_header *header;
2977
2978 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2979 if (!header)
2980 return 0;
2981 switch (header->op) {
2982 case KVM_MMU_OP_WRITE_PTE: {
2983 struct kvm_mmu_op_write_pte *wpte;
2984
2985 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2986 if (!wpte)
2987 return 0;
2988 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2989 wpte->pte_val);
2990 }
2991 case KVM_MMU_OP_FLUSH_TLB: {
2992 struct kvm_mmu_op_flush_tlb *ftlb;
2993
2994 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2995 if (!ftlb)
2996 return 0;
2997 return kvm_pv_mmu_flush_tlb(vcpu);
2998 }
2999 case KVM_MMU_OP_RELEASE_PT: {
3000 struct kvm_mmu_op_release_pt *rpt;
3001
3002 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3003 if (!rpt)
3004 return 0;
3005 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3006 }
3007 default: return 0;
3008 }
3009}
3010
3011int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3012 gpa_t addr, unsigned long *ret)
3013{
3014 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003015 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003016
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003017 buffer->ptr = buffer->buf;
3018 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3019 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003020
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003021 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003022 if (r)
3023 goto out;
3024
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003025 while (buffer->len) {
3026 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003027 if (r < 0)
3028 goto out;
3029 if (r == 0)
3030 break;
3031 }
3032
3033 r = 1;
3034out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003035 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003036 return r;
3037}
3038
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003039#ifdef AUDIT
3040
3041static const char *audit_msg;
3042
3043static gva_t canonicalize(gva_t gva)
3044{
3045#ifdef CONFIG_X86_64
3046 gva = (long long)(gva << 16) >> 16;
3047#endif
3048 return gva;
3049}
3050
3051static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3052 gva_t va, int level)
3053{
3054 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3055 int i;
3056 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3057
3058 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3059 u64 ent = pt[i];
3060
Avi Kivityc7addb92007-09-16 18:58:32 +02003061 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003062 continue;
3063
3064 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003065 if (level > 1) {
3066 if (ent == shadow_notrap_nonpresent_pte)
3067 printk(KERN_ERR "audit: (%s) nontrapping pte"
3068 " in nonleaf level: levels %d gva %lx"
3069 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003070 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003071
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003072 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003073 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003074 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003075 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003076
Avi Kivityc7addb92007-09-16 18:58:32 +02003077 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003078 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003079 printk(KERN_ERR "xx audit error: (%s) levels %d"
3080 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003081 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003082 va, gpa, hpa, ent,
3083 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003084 else if (ent == shadow_notrap_nonpresent_pte
3085 && !is_error_hpa(hpa))
3086 printk(KERN_ERR "audit: (%s) notrap shadow,"
3087 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003088 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003089
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003090 }
3091 }
3092}
3093
3094static void audit_mappings(struct kvm_vcpu *vcpu)
3095{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003096 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003097
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003098 if (vcpu->arch.mmu.root_level == 4)
3099 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003100 else
3101 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003102 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003103 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003104 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003105 i << 30,
3106 2);
3107}
3108
3109static int count_rmaps(struct kvm_vcpu *vcpu)
3110{
3111 int nmaps = 0;
3112 int i, j, k;
3113
3114 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3115 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3116 struct kvm_rmap_desc *d;
3117
3118 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003119 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003120
Izik Eidus290fc382007-09-27 14:11:22 +02003121 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003122 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003123 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003124 ++nmaps;
3125 continue;
3126 }
Izik Eidus290fc382007-09-27 14:11:22 +02003127 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003128 while (d) {
3129 for (k = 0; k < RMAP_EXT; ++k)
3130 if (d->shadow_ptes[k])
3131 ++nmaps;
3132 else
3133 break;
3134 d = d->more;
3135 }
3136 }
3137 }
3138 return nmaps;
3139}
3140
3141static int count_writable_mappings(struct kvm_vcpu *vcpu)
3142{
3143 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003144 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003145 int i;
3146
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003147 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003148 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003149
Avi Kivity4db35312007-11-21 15:28:32 +02003150 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003151 continue;
3152
3153 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3154 u64 ent = pt[i];
3155
3156 if (!(ent & PT_PRESENT_MASK))
3157 continue;
3158 if (!(ent & PT_WRITABLE_MASK))
3159 continue;
3160 ++nmaps;
3161 }
3162 }
3163 return nmaps;
3164}
3165
3166static void audit_rmap(struct kvm_vcpu *vcpu)
3167{
3168 int n_rmap = count_rmaps(vcpu);
3169 int n_actual = count_writable_mappings(vcpu);
3170
3171 if (n_rmap != n_actual)
3172 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003173 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003174}
3175
3176static void audit_write_protection(struct kvm_vcpu *vcpu)
3177{
Avi Kivity4db35312007-11-21 15:28:32 +02003178 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003179 struct kvm_memory_slot *slot;
3180 unsigned long *rmapp;
3181 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003182
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003183 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003184 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003185 continue;
3186
Avi Kivity4db35312007-11-21 15:28:32 +02003187 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003188 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003189 rmapp = &slot->rmap[gfn - slot->base_gfn];
3190 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003191 printk(KERN_ERR "%s: (%s) shadow page has writable"
3192 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003193 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003194 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003195 }
3196}
3197
3198static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3199{
3200 int olddbg = dbg;
3201
3202 dbg = 0;
3203 audit_msg = msg;
3204 audit_rmap(vcpu);
3205 audit_write_protection(vcpu);
3206 audit_mappings(vcpu);
3207 dbg = olddbg;
3208}
3209
3210#endif