blob: 0ef5bb2b4043af68c70ce46dbe484e3a887f8a21 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020028#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030029#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050030#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
32#include <asm/page.h>
33#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020034#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020035#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030069static int dbg = 0;
70module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080071#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
Marcelo Tosatti582801a2008-09-23 13:18:41 -030073static int oos_shadow = 1;
74module_param(oos_shadow, bool, 0644);
75
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080076#ifndef MMU_DEBUG
77#define ASSERT(x) do { } while (0)
78#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080079#define ASSERT(x) \
80 if (!(x)) { \
81 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
82 __FILE__, __LINE__, #x); \
83 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080084#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivity6aa8b732006-12-10 02:21:36 -080086#define PT_FIRST_AVAIL_BITS_SHIFT 9
87#define PT64_SECOND_AVAIL_BITS_SHIFT 52
88
Avi Kivity6aa8b732006-12-10 02:21:36 -080089#define VALID_PAGE(x) ((x) != INVALID_PAGE)
90
91#define PT64_LEVEL_BITS 9
92
93#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040094 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
96#define PT64_LEVEL_MASK(level) \
97 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
98
99#define PT64_INDEX(address, level)\
100 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
101
102
103#define PT32_LEVEL_BITS 10
104
105#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400106 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107
108#define PT32_LEVEL_MASK(level) \
109 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
110
111#define PT32_INDEX(address, level)\
112 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
113
114
Avi Kivity27aba762007-03-09 13:04:31 +0200115#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116#define PT64_DIR_BASE_ADDR_MASK \
117 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
118
119#define PT32_BASE_ADDR_MASK PAGE_MASK
120#define PT32_DIR_BASE_ADDR_MASK \
121 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
122
Avi Kivity79539ce2007-11-21 02:06:21 +0200123#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
124 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125
126#define PFERR_PRESENT_MASK (1U << 0)
127#define PFERR_WRITE_MASK (1U << 1)
128#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800129#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800130#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132#define PT_DIRECTORY_LEVEL 2
133#define PT_PAGE_TABLE_LEVEL 1
134
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800135#define RMAP_EXT 4
136
Avi Kivityfe135d22007-12-09 16:15:46 +0200137#define ACC_EXEC_MASK 1
138#define ACC_WRITE_MASK PT_WRITABLE_MASK
139#define ACC_USER_MASK PT_USER_MASK
140#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
141
Avi Kivity135f8c22008-08-21 17:49:56 +0300142#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
143
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800144struct kvm_rmap_desc {
145 u64 *shadow_ptes[RMAP_EXT];
146 struct kvm_rmap_desc *more;
147};
148
Avi Kivity2d111232008-12-25 14:39:47 +0200149struct kvm_shadow_walk_iterator {
150 u64 addr;
151 hpa_t shadow_addr;
152 int level;
153 u64 *sptep;
154 unsigned index;
155};
156
157#define for_each_shadow_entry(_vcpu, _addr, _walker) \
158 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
159 shadow_walk_okay(&(_walker)); \
160 shadow_walk_next(&(_walker)))
161
162
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300163struct kvm_unsync_walk {
164 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
165};
166
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300167typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
168
Avi Kivityb5a33a72007-04-15 16:31:09 +0300169static struct kmem_cache *pte_chain_cache;
170static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300171static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300172
Avi Kivityc7addb92007-09-16 18:58:32 +0200173static u64 __read_mostly shadow_trap_nonpresent_pte;
174static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800175static u64 __read_mostly shadow_base_present_pte;
176static u64 __read_mostly shadow_nx_mask;
177static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
178static u64 __read_mostly shadow_user_mask;
179static u64 __read_mostly shadow_accessed_mask;
180static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200181
Dong, Eddie82725b22009-03-30 16:21:08 +0800182static inline u64 rsvd_bits(int s, int e)
183{
184 return ((1ULL << (e - s + 1)) - 1) << s;
185}
186
Avi Kivityc7addb92007-09-16 18:58:32 +0200187void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
188{
189 shadow_trap_nonpresent_pte = trap_pte;
190 shadow_notrap_nonpresent_pte = notrap_pte;
191}
192EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
193
Sheng Yang7b523452008-04-25 21:13:50 +0800194void kvm_mmu_set_base_ptes(u64 base_pte)
195{
196 shadow_base_present_pte = base_pte;
197}
198EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
199
200void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800201 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800202{
203 shadow_user_mask = user_mask;
204 shadow_accessed_mask = accessed_mask;
205 shadow_dirty_mask = dirty_mask;
206 shadow_nx_mask = nx_mask;
207 shadow_x_mask = x_mask;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static int is_write_protection(struct kvm_vcpu *vcpu)
212{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800213 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214}
215
216static int is_cpuid_PSE36(void)
217{
218 return 1;
219}
220
Avi Kivity73b10872007-01-26 00:56:41 -0800221static int is_nx(struct kvm_vcpu *vcpu)
222{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800223 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800224}
225
Avi Kivityc7addb92007-09-16 18:58:32 +0200226static int is_shadow_present_pte(u64 pte)
227{
Avi Kivityc7addb92007-09-16 18:58:32 +0200228 return pte != shadow_trap_nonpresent_pte
229 && pte != shadow_notrap_nonpresent_pte;
230}
231
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300232static int is_large_pte(u64 pte)
233{
234 return pte & PT_PAGE_SIZE_MASK;
235}
236
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237static int is_writeble_pte(unsigned long pte)
238{
239 return pte & PT_WRITABLE_MASK;
240}
241
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200242static int is_dirty_pte(unsigned long pte)
243{
Sheng Yang7b523452008-04-25 21:13:50 +0800244 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200245}
246
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800247static int is_rmap_pte(u64 pte)
248{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200249 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800250}
251
Anthony Liguori35149e22008-04-02 14:46:56 -0500252static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200253{
Anthony Liguori35149e22008-04-02 14:46:56 -0500254 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200255}
256
Avi Kivityda928522007-11-21 13:54:47 +0200257static gfn_t pse36_gfn_delta(u32 gpte)
258{
259 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
260
261 return (gpte & PT32_DIR_PSE36_MASK) << shift;
262}
263
Avi Kivitye663ee62007-05-31 15:46:04 +0300264static void set_shadow_pte(u64 *sptep, u64 spte)
265{
266#ifdef CONFIG_X86_64
267 set_64bit((unsigned long *)sptep, spte);
268#else
269 set_64bit((unsigned long long *)sptep, spte);
270#endif
271}
272
Avi Kivitye2dec932007-01-05 16:36:54 -0800273static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300274 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800275{
276 void *obj;
277
278 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800279 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800280 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300281 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800282 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800283 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800284 cache->objects[cache->nobjs++] = obj;
285 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800286 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800287}
288
289static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
290{
291 while (mc->nobjs)
292 kfree(mc->objects[--mc->nobjs]);
293}
294
Avi Kivityc1158e62007-07-20 08:18:27 +0300295static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300296 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300297{
298 struct page *page;
299
300 if (cache->nobjs >= min)
301 return 0;
302 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300303 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300304 if (!page)
305 return -ENOMEM;
306 set_page_private(page, 0);
307 cache->objects[cache->nobjs++] = page_address(page);
308 }
309 return 0;
310}
311
312static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
313{
314 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300315 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300316}
317
Avi Kivity8c438502007-04-16 11:53:17 +0300318static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
319{
320 int r;
321
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800322 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300323 pte_chain_cache, 4);
324 if (r)
325 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800326 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200327 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300328 if (r)
329 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800330 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300331 if (r)
332 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300334 mmu_page_header_cache, 4);
335out:
Avi Kivity8c438502007-04-16 11:53:17 +0300336 return r;
337}
338
Avi Kivity714b93d2007-01-05 16:36:53 -0800339static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
340{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800341 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
342 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
343 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
344 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800345}
346
347static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
348 size_t size)
349{
350 void *p;
351
352 BUG_ON(!mc->nobjs);
353 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800354 return p;
355}
356
Avi Kivity714b93d2007-01-05 16:36:53 -0800357static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
358{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800360 sizeof(struct kvm_pte_chain));
361}
362
Avi Kivity90cb0522007-07-17 13:04:56 +0300363static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800364{
Avi Kivity90cb0522007-07-17 13:04:56 +0300365 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800366}
367
368static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800370 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800371 sizeof(struct kvm_rmap_desc));
372}
373
Avi Kivity90cb0522007-07-17 13:04:56 +0300374static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800375{
Avi Kivity90cb0522007-07-17 13:04:56 +0300376 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800377}
378
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800379/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300380 * Return the pointer to the largepage write count for a given
381 * gfn, handling slots that are not large page aligned.
382 */
383static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
384{
385 unsigned long idx;
386
387 idx = (gfn / KVM_PAGES_PER_HPAGE) -
388 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
389 return &slot->lpage_info[idx].write_count;
390}
391
392static void account_shadowed(struct kvm *kvm, gfn_t gfn)
393{
394 int *write_count;
395
Izik Eidus28430992008-10-03 17:40:32 +0300396 gfn = unalias_gfn(kvm, gfn);
397 write_count = slot_largepage_idx(gfn,
398 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300399 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300400}
401
402static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
403{
404 int *write_count;
405
Izik Eidus28430992008-10-03 17:40:32 +0300406 gfn = unalias_gfn(kvm, gfn);
407 write_count = slot_largepage_idx(gfn,
408 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409 *write_count -= 1;
410 WARN_ON(*write_count < 0);
411}
412
413static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
414{
Izik Eidus28430992008-10-03 17:40:32 +0300415 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300416 int *largepage_idx;
417
Izik Eidus28430992008-10-03 17:40:32 +0300418 gfn = unalias_gfn(kvm, gfn);
419 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300420 if (slot) {
421 largepage_idx = slot_largepage_idx(gfn, slot);
422 return *largepage_idx;
423 }
424
425 return 1;
426}
427
428static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
429{
430 struct vm_area_struct *vma;
431 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300432 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300433
434 addr = gfn_to_hva(kvm, gfn);
435 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300436 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300437
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300438 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439 vma = find_vma(current->mm, addr);
440 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300441 ret = 1;
442 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300443
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300444 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445}
446
447static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
448{
449 struct kvm_memory_slot *slot;
450
451 if (has_wrprotected_page(vcpu->kvm, large_gfn))
452 return 0;
453
454 if (!host_largepage_backed(vcpu->kvm, large_gfn))
455 return 0;
456
457 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
458 if (slot && slot->dirty_bitmap)
459 return 0;
460
461 return 1;
462}
463
464/*
Izik Eidus290fc382007-09-27 14:11:22 +0200465 * Take gfn and return the reverse mapping to it.
466 * Note: gfn must be unaliased before this function get called
467 */
468
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300469static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200470{
471 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200473
474 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475 if (!lpage)
476 return &slot->rmap[gfn - slot->base_gfn];
477
478 idx = (gfn / KVM_PAGES_PER_HPAGE) -
479 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
480
481 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200482}
483
484/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800485 * Reverse mapping data structures:
486 *
Izik Eidus290fc382007-09-27 14:11:22 +0200487 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
488 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800489 *
Izik Eidus290fc382007-09-27 14:11:22 +0200490 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
491 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300492 *
493 * Returns the number of rmap entries before the spte was added or zero if
494 * the spte was not added.
495 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496 */
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300497static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498{
Avi Kivity4db35312007-11-21 15:28:32 +0200499 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200501 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300502 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800503
504 if (!is_rmap_pte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300505 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200506 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200507 sp = page_header(__pa(spte));
508 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300509 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200510 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800511 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200512 *rmapp = (unsigned long)spte;
513 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800514 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800515 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200516 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800517 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200518 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800519 } else {
520 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200521 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300522 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800523 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300524 count += RMAP_EXT;
525 }
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800526 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800527 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800528 desc = desc->more;
529 }
530 for (i = 0; desc->shadow_ptes[i]; ++i)
531 ;
532 desc->shadow_ptes[i] = spte;
533 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300534 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800535}
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
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300764#define RMAP_RECYCLE_THRESHOLD 1000
765
766static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
767{
768 unsigned long *rmapp;
769
770 gfn = unalias_gfn(vcpu->kvm, gfn);
771 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
772
773 kvm_unmap_rmapp(vcpu->kvm, rmapp);
774 kvm_flush_remote_tlbs(vcpu->kvm);
775}
776
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200777int kvm_age_hva(struct kvm *kvm, unsigned long hva)
778{
779 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
780}
781
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800782#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300783static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784{
Avi Kivity139bdb22007-01-05 16:36:50 -0800785 u64 *pos;
786 u64 *end;
787
Avi Kivity47ad8e62007-05-06 15:50:58 +0300788 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300789 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800790 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800791 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800793 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 return 1;
795}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800796#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797
Avi Kivity4db35312007-11-21 15:28:32 +0200798static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800799{
Avi Kivity4db35312007-11-21 15:28:32 +0200800 ASSERT(is_empty_shadow_page(sp->spt));
801 list_del(&sp->link);
802 __free_page(virt_to_page(sp->spt));
803 __free_page(virt_to_page(sp->gfns));
804 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800805 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800806}
807
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800808static unsigned kvm_page_table_hashfn(gfn_t gfn)
809{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200810 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800811}
812
Avi Kivity25c0de22007-01-05 16:36:42 -0800813static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
814 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815{
Avi Kivity4db35312007-11-21 15:28:32 +0200816 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800818 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
819 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
820 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200821 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800822 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200823 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800824 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200825 sp->multimapped = 0;
826 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800827 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200828 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829}
830
Avi Kivity714b93d2007-01-05 16:36:53 -0800831static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200832 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800833{
834 struct kvm_pte_chain *pte_chain;
835 struct hlist_node *node;
836 int i;
837
838 if (!parent_pte)
839 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200840 if (!sp->multimapped) {
841 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800842
843 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200844 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845 return;
846 }
Avi Kivity4db35312007-11-21 15:28:32 +0200847 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800848 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200849 INIT_HLIST_HEAD(&sp->parent_ptes);
850 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800851 pte_chain->parent_ptes[0] = old;
852 }
Avi Kivity4db35312007-11-21 15:28:32 +0200853 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800854 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
855 continue;
856 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
857 if (!pte_chain->parent_ptes[i]) {
858 pte_chain->parent_ptes[i] = parent_pte;
859 return;
860 }
861 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800862 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800863 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200864 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800865 pte_chain->parent_ptes[0] = parent_pte;
866}
867
Avi Kivity4db35312007-11-21 15:28:32 +0200868static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800869 u64 *parent_pte)
870{
871 struct kvm_pte_chain *pte_chain;
872 struct hlist_node *node;
873 int i;
874
Avi Kivity4db35312007-11-21 15:28:32 +0200875 if (!sp->multimapped) {
876 BUG_ON(sp->parent_pte != parent_pte);
877 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800878 return;
879 }
Avi Kivity4db35312007-11-21 15:28:32 +0200880 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800881 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
882 if (!pte_chain->parent_ptes[i])
883 break;
884 if (pte_chain->parent_ptes[i] != parent_pte)
885 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800886 while (i + 1 < NR_PTE_CHAIN_ENTRIES
887 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800888 pte_chain->parent_ptes[i]
889 = pte_chain->parent_ptes[i + 1];
890 ++i;
891 }
892 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800893 if (i == 0) {
894 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300895 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200896 if (hlist_empty(&sp->parent_ptes)) {
897 sp->multimapped = 0;
898 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800899 }
900 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800901 return;
902 }
903 BUG();
904}
905
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300906
907static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
908 mmu_parent_walk_fn fn)
909{
910 struct kvm_pte_chain *pte_chain;
911 struct hlist_node *node;
912 struct kvm_mmu_page *parent_sp;
913 int i;
914
915 if (!sp->multimapped && sp->parent_pte) {
916 parent_sp = page_header(__pa(sp->parent_pte));
917 fn(vcpu, parent_sp);
918 mmu_parent_walk(vcpu, parent_sp, fn);
919 return;
920 }
921 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
922 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
923 if (!pte_chain->parent_ptes[i])
924 break;
925 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
926 fn(vcpu, parent_sp);
927 mmu_parent_walk(vcpu, parent_sp, fn);
928 }
929}
930
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300931static void kvm_mmu_update_unsync_bitmap(u64 *spte)
932{
933 unsigned int index;
934 struct kvm_mmu_page *sp = page_header(__pa(spte));
935
936 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200937 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
938 sp->unsync_children++;
939 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300940}
941
942static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
943{
944 struct kvm_pte_chain *pte_chain;
945 struct hlist_node *node;
946 int i;
947
948 if (!sp->parent_pte)
949 return;
950
951 if (!sp->multimapped) {
952 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
953 return;
954 }
955
956 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
957 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
958 if (!pte_chain->parent_ptes[i])
959 break;
960 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
961 }
962}
963
964static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
965{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300966 kvm_mmu_update_parents_unsync(sp);
967 return 1;
968}
969
970static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
971 struct kvm_mmu_page *sp)
972{
973 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
974 kvm_mmu_update_parents_unsync(sp);
975}
976
Avi Kivityd761a502008-05-29 14:55:03 +0300977static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
978 struct kvm_mmu_page *sp)
979{
980 int i;
981
982 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
983 sp->spt[i] = shadow_trap_nonpresent_pte;
984}
985
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300986static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
987 struct kvm_mmu_page *sp)
988{
989 return 1;
990}
991
Marcelo Tosattia7052892008-09-23 13:18:35 -0300992static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
993{
994}
995
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200996#define KVM_PAGE_ARRAY_NR 16
997
998struct kvm_mmu_pages {
999 struct mmu_page_and_offset {
1000 struct kvm_mmu_page *sp;
1001 unsigned int idx;
1002 } page[KVM_PAGE_ARRAY_NR];
1003 unsigned int nr;
1004};
1005
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001006#define for_each_unsync_children(bitmap, idx) \
1007 for (idx = find_first_bit(bitmap, 512); \
1008 idx < 512; \
1009 idx = find_next_bit(bitmap, 512, idx+1))
1010
Hannes Edercded19f2009-02-21 02:19:13 +01001011static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1012 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001013{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001014 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001015
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001016 if (sp->unsync)
1017 for (i=0; i < pvec->nr; i++)
1018 if (pvec->page[i].sp == sp)
1019 return 0;
1020
1021 pvec->page[pvec->nr].sp = sp;
1022 pvec->page[pvec->nr].idx = idx;
1023 pvec->nr++;
1024 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1025}
1026
1027static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1028 struct kvm_mmu_pages *pvec)
1029{
1030 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001031
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001032 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001033 u64 ent = sp->spt[i];
1034
Marcelo Tosatti87917232008-12-22 18:49:30 -02001035 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001036 struct kvm_mmu_page *child;
1037 child = page_header(ent & PT64_BASE_ADDR_MASK);
1038
1039 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001040 if (mmu_pages_add(pvec, child, i))
1041 return -ENOSPC;
1042
1043 ret = __mmu_unsync_walk(child, pvec);
1044 if (!ret)
1045 __clear_bit(i, sp->unsync_child_bitmap);
1046 else if (ret > 0)
1047 nr_unsync_leaf += ret;
1048 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001049 return ret;
1050 }
1051
1052 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001053 nr_unsync_leaf++;
1054 if (mmu_pages_add(pvec, child, i))
1055 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001056 }
1057 }
1058 }
1059
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001060 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001061 sp->unsync_children = 0;
1062
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001063 return nr_unsync_leaf;
1064}
1065
1066static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1067 struct kvm_mmu_pages *pvec)
1068{
1069 if (!sp->unsync_children)
1070 return 0;
1071
1072 mmu_pages_add(pvec, sp, 0);
1073 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001074}
1075
Avi Kivity4db35312007-11-21 15:28:32 +02001076static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001077{
1078 unsigned index;
1079 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001080 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001081 struct hlist_node *node;
1082
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001083 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001084 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001085 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001086 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001087 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001088 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001089 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001090 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001091 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001092 }
1093 return NULL;
1094}
1095
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001096static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1097{
1098 WARN_ON(!sp->unsync);
1099 sp->unsync = 0;
1100 --kvm->stat.mmu_unsync;
1101}
1102
1103static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1104
1105static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1106{
1107 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1108 kvm_mmu_zap_page(vcpu->kvm, sp);
1109 return 1;
1110 }
1111
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001112 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1113 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001114 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001115 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1116 kvm_mmu_zap_page(vcpu->kvm, sp);
1117 return 1;
1118 }
1119
1120 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001121 return 0;
1122}
1123
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001124struct mmu_page_path {
1125 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1126 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001127};
1128
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001129#define for_each_sp(pvec, sp, parents, i) \
1130 for (i = mmu_pages_next(&pvec, &parents, -1), \
1131 sp = pvec.page[i].sp; \
1132 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1133 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001134
Hannes Edercded19f2009-02-21 02:19:13 +01001135static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1136 struct mmu_page_path *parents,
1137 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001138{
1139 int n;
1140
1141 for (n = i+1; n < pvec->nr; n++) {
1142 struct kvm_mmu_page *sp = pvec->page[n].sp;
1143
1144 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1145 parents->idx[0] = pvec->page[n].idx;
1146 return n;
1147 }
1148
1149 parents->parent[sp->role.level-2] = sp;
1150 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1151 }
1152
1153 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001154}
1155
Hannes Edercded19f2009-02-21 02:19:13 +01001156static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001157{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001158 struct kvm_mmu_page *sp;
1159 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001160
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001161 do {
1162 unsigned int idx = parents->idx[level];
1163
1164 sp = parents->parent[level];
1165 if (!sp)
1166 return;
1167
1168 --sp->unsync_children;
1169 WARN_ON((int)sp->unsync_children < 0);
1170 __clear_bit(idx, sp->unsync_child_bitmap);
1171 level++;
1172 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1173}
1174
1175static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1176 struct mmu_page_path *parents,
1177 struct kvm_mmu_pages *pvec)
1178{
1179 parents->parent[parent->role.level-1] = NULL;
1180 pvec->nr = 0;
1181}
1182
1183static void mmu_sync_children(struct kvm_vcpu *vcpu,
1184 struct kvm_mmu_page *parent)
1185{
1186 int i;
1187 struct kvm_mmu_page *sp;
1188 struct mmu_page_path parents;
1189 struct kvm_mmu_pages pages;
1190
1191 kvm_mmu_pages_init(parent, &parents, &pages);
1192 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001193 int protected = 0;
1194
1195 for_each_sp(pages, sp, parents, i)
1196 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1197
1198 if (protected)
1199 kvm_flush_remote_tlbs(vcpu->kvm);
1200
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001201 for_each_sp(pages, sp, parents, i) {
1202 kvm_sync_page(vcpu, sp);
1203 mmu_pages_clear_parents(&parents);
1204 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001205 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001206 kvm_mmu_pages_init(parent, &parents, &pages);
1207 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001208}
1209
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001210static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1211 gfn_t gfn,
1212 gva_t gaddr,
1213 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001214 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001215 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001216 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001217{
1218 union kvm_mmu_page_role role;
1219 unsigned index;
1220 unsigned quadrant;
1221 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001222 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001223 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001224
Avi Kivitya770f6f2008-12-21 19:20:09 +02001225 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001226 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001227 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001228 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001229 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001230 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1231 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1232 role.quadrant = quadrant;
1233 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001234 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001235 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001236 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001237 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001238 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1239 if (sp->gfn == gfn) {
1240 if (sp->unsync)
1241 if (kvm_sync_page(vcpu, sp))
1242 continue;
1243
1244 if (sp->role.word != role.word)
1245 continue;
1246
Avi Kivity4db35312007-11-21 15:28:32 +02001247 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001248 if (sp->unsync_children) {
1249 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1250 kvm_mmu_mark_parents_unsync(vcpu, sp);
1251 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001252 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001253 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001254 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001255 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001256 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1257 if (!sp)
1258 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001259 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001260 sp->gfn = gfn;
1261 sp->role = role;
1262 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001263 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001264 if (rmap_write_protect(vcpu->kvm, gfn))
1265 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001266 account_shadowed(vcpu->kvm, gfn);
1267 }
Avi Kivity131d8272008-05-29 14:56:28 +03001268 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1269 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1270 else
1271 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001272 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001273}
1274
Avi Kivity2d111232008-12-25 14:39:47 +02001275static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1276 struct kvm_vcpu *vcpu, u64 addr)
1277{
1278 iterator->addr = addr;
1279 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1280 iterator->level = vcpu->arch.mmu.shadow_root_level;
1281 if (iterator->level == PT32E_ROOT_LEVEL) {
1282 iterator->shadow_addr
1283 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1284 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1285 --iterator->level;
1286 if (!iterator->shadow_addr)
1287 iterator->level = 0;
1288 }
1289}
1290
1291static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1292{
1293 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1294 return false;
1295 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1296 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1297 return true;
1298}
1299
1300static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1301{
1302 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1303 --iterator->level;
1304}
1305
Avi Kivity90cb0522007-07-17 13:04:56 +03001306static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001307 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001308{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001309 unsigned i;
1310 u64 *pt;
1311 u64 ent;
1312
Avi Kivity4db35312007-11-21 15:28:32 +02001313 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001314
Avi Kivity4db35312007-11-21 15:28:32 +02001315 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001316 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001317 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001318 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001319 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001320 }
1321 return;
1322 }
1323
1324 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1325 ent = pt[i];
1326
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001327 if (is_shadow_present_pte(ent)) {
1328 if (!is_large_pte(ent)) {
1329 ent &= PT64_BASE_ADDR_MASK;
1330 mmu_page_remove_parent_pte(page_header(ent),
1331 &pt[i]);
1332 } else {
1333 --kvm->stat.lpages;
1334 rmap_remove(kvm, &pt[i]);
1335 }
1336 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001337 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001338 }
Avi Kivitya4360362007-01-05 16:36:45 -08001339}
1340
Avi Kivity4db35312007-11-21 15:28:32 +02001341static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001342{
Avi Kivity4db35312007-11-21 15:28:32 +02001343 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001344}
1345
Avi Kivity12b7d282007-09-23 14:10:49 +02001346static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1347{
1348 int i;
1349
1350 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1351 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001352 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001353}
1354
Avi Kivity31aa2b42008-07-11 17:59:46 +03001355static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001356{
1357 u64 *parent_pte;
1358
Avi Kivity4db35312007-11-21 15:28:32 +02001359 while (sp->multimapped || sp->parent_pte) {
1360 if (!sp->multimapped)
1361 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001362 else {
1363 struct kvm_pte_chain *chain;
1364
Avi Kivity4db35312007-11-21 15:28:32 +02001365 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001366 struct kvm_pte_chain, link);
1367 parent_pte = chain->parent_ptes[0];
1368 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001369 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001370 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001371 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001372 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001373}
1374
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001375static int mmu_zap_unsync_children(struct kvm *kvm,
1376 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001377{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001378 int i, zapped = 0;
1379 struct mmu_page_path parents;
1380 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001381
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001382 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001383 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001384
1385 kvm_mmu_pages_init(parent, &parents, &pages);
1386 while (mmu_unsync_walk(parent, &pages)) {
1387 struct kvm_mmu_page *sp;
1388
1389 for_each_sp(pages, sp, parents, i) {
1390 kvm_mmu_zap_page(kvm, sp);
1391 mmu_pages_clear_parents(&parents);
1392 }
1393 zapped += pages.nr;
1394 kvm_mmu_pages_init(parent, &parents, &pages);
1395 }
1396
1397 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001398}
1399
Marcelo Tosatti07385412008-09-23 13:18:37 -03001400static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001401{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001402 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001403 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001404 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001405 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001406 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001407 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001408 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001409 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001410 if (sp->unsync)
1411 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001412 if (!sp->root_count) {
1413 hlist_del(&sp->hash_link);
1414 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001415 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001416 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001417 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001418 kvm_reload_remote_mmus(kvm);
1419 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001420 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001421 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001422}
1423
Izik Eidus82ce2c92007-10-02 18:52:55 +02001424/*
1425 * Changing the number of mmu pages allocated to the vm
1426 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1427 */
1428void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1429{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001430 int used_pages;
1431
1432 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1433 used_pages = max(0, used_pages);
1434
Izik Eidus82ce2c92007-10-02 18:52:55 +02001435 /*
1436 * If we set the number of mmu pages to be smaller be than the
1437 * number of actived pages , we must to free some mmu pages before we
1438 * change the value
1439 */
1440
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001441 if (used_pages > kvm_nr_mmu_pages) {
1442 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001443 struct kvm_mmu_page *page;
1444
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001445 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001446 struct kvm_mmu_page, link);
1447 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001448 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001449 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001450 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001451 }
1452 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001453 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1454 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001455
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001456 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001457}
1458
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001459static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001460{
1461 unsigned index;
1462 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001463 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001464 struct hlist_node *node, *n;
1465 int r;
1466
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001467 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001468 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001469 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001470 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001471 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001472 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001473 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001474 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001475 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001476 if (kvm_mmu_zap_page(kvm, sp))
1477 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001478 }
1479 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001480}
1481
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001482static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001483{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001484 unsigned index;
1485 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001486 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001487 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001488
Avi Kivity4677a3b2009-01-06 13:00:27 +02001489 index = kvm_page_table_hashfn(gfn);
1490 bucket = &kvm->arch.mmu_page_hash[index];
1491 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001492 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001493 && !sp->role.invalid) {
1494 pgprintk("%s: zap %lx %x\n",
1495 __func__, gfn, sp->role.word);
1496 kvm_mmu_zap_page(kvm, sp);
1497 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001498 }
1499}
1500
Avi Kivity38c335f2007-11-21 14:20:22 +02001501static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502{
Avi Kivity38c335f2007-11-21 14:20:22 +02001503 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001504 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505
Sheng Yang291f26b2008-10-16 17:30:57 +08001506 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507}
1508
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001509static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1510{
1511 int i;
1512 u64 *pt = sp->spt;
1513
1514 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1515 return;
1516
1517 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1518 if (pt[i] == shadow_notrap_nonpresent_pte)
1519 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1520 }
1521}
1522
Avi Kivity039576c2007-03-20 12:46:50 +02001523struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1524{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001525 struct page *page;
1526
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001527 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001528
1529 if (gpa == UNMAPPED_GVA)
1530 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001531
Izik Eidus72dc67a2008-02-10 18:04:15 +02001532 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001533
1534 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001535}
1536
Sheng Yang74be52e2008-10-09 16:01:56 +08001537/*
1538 * The function is based on mtrr_type_lookup() in
1539 * arch/x86/kernel/cpu/mtrr/generic.c
1540 */
1541static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1542 u64 start, u64 end)
1543{
1544 int i;
1545 u64 base, mask;
1546 u8 prev_match, curr_match;
1547 int num_var_ranges = KVM_NR_VAR_MTRR;
1548
1549 if (!mtrr_state->enabled)
1550 return 0xFF;
1551
1552 /* Make end inclusive end, instead of exclusive */
1553 end--;
1554
1555 /* Look in fixed ranges. Just return the type as per start */
1556 if (mtrr_state->have_fixed && (start < 0x100000)) {
1557 int idx;
1558
1559 if (start < 0x80000) {
1560 idx = 0;
1561 idx += (start >> 16);
1562 return mtrr_state->fixed_ranges[idx];
1563 } else if (start < 0xC0000) {
1564 idx = 1 * 8;
1565 idx += ((start - 0x80000) >> 14);
1566 return mtrr_state->fixed_ranges[idx];
1567 } else if (start < 0x1000000) {
1568 idx = 3 * 8;
1569 idx += ((start - 0xC0000) >> 12);
1570 return mtrr_state->fixed_ranges[idx];
1571 }
1572 }
1573
1574 /*
1575 * Look in variable ranges
1576 * Look of multiple ranges matching this address and pick type
1577 * as per MTRR precedence
1578 */
1579 if (!(mtrr_state->enabled & 2))
1580 return mtrr_state->def_type;
1581
1582 prev_match = 0xFF;
1583 for (i = 0; i < num_var_ranges; ++i) {
1584 unsigned short start_state, end_state;
1585
1586 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1587 continue;
1588
1589 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1590 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1591 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1592 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1593
1594 start_state = ((start & mask) == (base & mask));
1595 end_state = ((end & mask) == (base & mask));
1596 if (start_state != end_state)
1597 return 0xFE;
1598
1599 if ((start & mask) != (base & mask))
1600 continue;
1601
1602 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1603 if (prev_match == 0xFF) {
1604 prev_match = curr_match;
1605 continue;
1606 }
1607
1608 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1609 curr_match == MTRR_TYPE_UNCACHABLE)
1610 return MTRR_TYPE_UNCACHABLE;
1611
1612 if ((prev_match == MTRR_TYPE_WRBACK &&
1613 curr_match == MTRR_TYPE_WRTHROUGH) ||
1614 (prev_match == MTRR_TYPE_WRTHROUGH &&
1615 curr_match == MTRR_TYPE_WRBACK)) {
1616 prev_match = MTRR_TYPE_WRTHROUGH;
1617 curr_match = MTRR_TYPE_WRTHROUGH;
1618 }
1619
1620 if (prev_match != curr_match)
1621 return MTRR_TYPE_UNCACHABLE;
1622 }
1623
1624 if (prev_match != 0xFF)
1625 return prev_match;
1626
1627 return mtrr_state->def_type;
1628}
1629
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001630u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001631{
1632 u8 mtrr;
1633
1634 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1635 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1636 if (mtrr == 0xfe || mtrr == 0xff)
1637 mtrr = MTRR_TYPE_WRBACK;
1638 return mtrr;
1639}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001640EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001641
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001642static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1643{
1644 unsigned index;
1645 struct hlist_head *bucket;
1646 struct kvm_mmu_page *s;
1647 struct hlist_node *node, *n;
1648
1649 index = kvm_page_table_hashfn(sp->gfn);
1650 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1651 /* don't unsync if pagetable is shadowed with multiple roles */
1652 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001653 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001654 continue;
1655 if (s->role.word != sp->role.word)
1656 return 1;
1657 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001658 ++vcpu->kvm->stat.mmu_unsync;
1659 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001660
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001661 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001662
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001663 mmu_convert_notrap(sp);
1664 return 0;
1665}
1666
1667static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1668 bool can_unsync)
1669{
1670 struct kvm_mmu_page *shadow;
1671
1672 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1673 if (shadow) {
1674 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1675 return 1;
1676 if (shadow->unsync)
1677 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001678 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001679 return kvm_unsync_page(vcpu, shadow);
1680 return 1;
1681 }
1682 return 0;
1683}
1684
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001685static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1686 unsigned pte_access, int user_fault,
1687 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001688 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001689 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001690{
1691 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001692 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001693
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001694 /*
1695 * We don't set the accessed bit, since we sometimes want to see
1696 * whether the guest actually used the pte (in order to detect
1697 * demand paging).
1698 */
Sheng Yang7b523452008-04-25 21:13:50 +08001699 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001700 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001701 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001702 if (!dirty)
1703 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001704 if (pte_access & ACC_EXEC_MASK)
1705 spte |= shadow_x_mask;
1706 else
1707 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001708 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001709 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001710 if (largepage)
1711 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001712 if (tdp_enabled)
1713 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1714 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001715
Anthony Liguori35149e22008-04-02 14:46:56 -05001716 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001717
1718 if ((pte_access & ACC_WRITE_MASK)
1719 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001720
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001721 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1722 ret = 1;
1723 spte = shadow_trap_nonpresent_pte;
1724 goto set_pte;
1725 }
1726
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001728
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001729 /*
1730 * Optimization: for pte sync, if spte was writable the hash
1731 * lookup is unnecessary (and expensive). Write protection
1732 * is responsibility of mmu_get_page / kvm_sync_page.
1733 * Same reasoning can be applied to dirty page accounting.
1734 */
1735 if (!can_unsync && is_writeble_pte(*shadow_pte))
1736 goto set_pte;
1737
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001738 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001739 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001740 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001741 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001742 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001743 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001744 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001745 }
1746 }
1747
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001748 if (pte_access & ACC_WRITE_MASK)
1749 mark_page_dirty(vcpu->kvm, gfn);
1750
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001751set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001752 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001753 return ret;
1754}
1755
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001756static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1757 unsigned pt_access, unsigned pte_access,
1758 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001759 int *ptwrite, int largepage, gfn_t gfn,
1760 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001761{
1762 int was_rmapped = 0;
1763 int was_writeble = is_writeble_pte(*shadow_pte);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001764 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001765
1766 pgprintk("%s: spte %llx access %x write_fault %d"
1767 " user_fault %d gfn %lx\n",
1768 __func__, *shadow_pte, pt_access,
1769 write_fault, user_fault, gfn);
1770
1771 if (is_rmap_pte(*shadow_pte)) {
1772 /*
1773 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1774 * the parent of the now unreachable PTE.
1775 */
1776 if (largepage && !is_large_pte(*shadow_pte)) {
1777 struct kvm_mmu_page *child;
1778 u64 pte = *shadow_pte;
1779
1780 child = page_header(pte & PT64_BASE_ADDR_MASK);
1781 mmu_page_remove_parent_pte(child, shadow_pte);
1782 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1783 pgprintk("hfn old %lx new %lx\n",
1784 spte_to_pfn(*shadow_pte), pfn);
1785 rmap_remove(vcpu->kvm, shadow_pte);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001786 } else
1787 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001788 }
1789 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001790 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001791 if (write_fault)
1792 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001793 kvm_x86_ops->tlb_flush(vcpu);
1794 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001795
1796 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1797 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1798 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1799 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1800 *shadow_pte, shadow_pte);
1801 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001802 ++vcpu->kvm->stat.lpages;
1803
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001804 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1805 if (!was_rmapped) {
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001806 rmap_count = rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001807 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001808 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001809 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1810 rmap_recycle(vcpu, gfn, largepage);
Izik Eidus75e68e62008-01-12 23:49:09 +02001811 } else {
1812 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001813 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001814 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001815 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001816 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001817 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001818 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001819 vcpu->arch.last_pte_gfn = gfn;
1820 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001821}
1822
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1824{
1825}
1826
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001827static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001828 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829{
Avi Kivity9f652d22008-12-25 14:54:25 +02001830 struct kvm_shadow_walk_iterator iterator;
1831 struct kvm_mmu_page *sp;
1832 int pt_write = 0;
1833 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834
Avi Kivity9f652d22008-12-25 14:54:25 +02001835 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1836 if (iterator.level == PT_PAGE_TABLE_LEVEL
1837 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1838 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1839 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001840 largepage, gfn, pfn, false);
Avi Kivity9f652d22008-12-25 14:54:25 +02001841 ++vcpu->stat.pf_fixed;
1842 break;
1843 }
1844
1845 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1846 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1847 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1848 iterator.level - 1,
1849 1, ACC_ALL, iterator.sptep);
1850 if (!sp) {
1851 pgprintk("nonpaging_map: ENOMEM\n");
1852 kvm_release_pfn_clean(pfn);
1853 return -ENOMEM;
1854 }
1855
1856 set_shadow_pte(iterator.sptep,
1857 __pa(sp->spt)
1858 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1859 | shadow_user_mask | shadow_x_mask);
1860 }
1861 }
1862 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863}
1864
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001865static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1866{
1867 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001868 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001869 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001870 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001871
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001872 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1873 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1874 largepage = 1;
1875 }
1876
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001877 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001878 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001879 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001880
Avi Kivityd196e342008-01-24 11:44:11 +02001881 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001882 if (is_error_pfn(pfn)) {
1883 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001884 return 1;
1885 }
1886
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001887 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001888 if (mmu_notifier_retry(vcpu, mmu_seq))
1889 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001890 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001891 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001892 spin_unlock(&vcpu->kvm->mmu_lock);
1893
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001894
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001895 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001896
1897out_unlock:
1898 spin_unlock(&vcpu->kvm->mmu_lock);
1899 kvm_release_pfn_clean(pfn);
1900 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001901}
1902
1903
Avi Kivity17ac10a2007-01-05 16:36:40 -08001904static void mmu_free_roots(struct kvm_vcpu *vcpu)
1905{
1906 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001907 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001908
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001909 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001910 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001911 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001912 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1913 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001914
Avi Kivity4db35312007-11-21 15:28:32 +02001915 sp = page_header(root);
1916 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001917 if (!sp->root_count && sp->role.invalid)
1918 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001919 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001920 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001921 return;
1922 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001923 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001924 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001925
Avi Kivity417726a2007-04-12 17:35:58 +03001926 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001927 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001928 sp = page_header(root);
1929 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001930 if (!sp->root_count && sp->role.invalid)
1931 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001932 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001933 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001934 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001935 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001936 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001937}
1938
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001939static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1940{
1941 int ret = 0;
1942
1943 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1944 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1945 ret = 1;
1946 }
1947
1948 return ret;
1949}
1950
1951static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08001952{
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;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001966 if (mmu_check_root(vcpu, root_gfn))
1967 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001968 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001969 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001970 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001971 root = __pa(sp->spt);
1972 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001973 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001974 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001975 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001976 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001977 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001978 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001979 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001980 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001981
1982 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001983 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1984 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1985 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001986 continue;
1987 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001988 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1989 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001990 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001991 if (mmu_check_root(vcpu, root_gfn))
1992 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001993 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001994 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001995 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001996 root = __pa(sp->spt);
1997 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001998 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001999 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002000 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002001 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002002}
2003
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002004static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2005{
2006 int i;
2007 struct kvm_mmu_page *sp;
2008
2009 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2010 return;
2011 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2012 hpa_t root = vcpu->arch.mmu.root_hpa;
2013 sp = page_header(root);
2014 mmu_sync_children(vcpu, sp);
2015 return;
2016 }
2017 for (i = 0; i < 4; ++i) {
2018 hpa_t root = vcpu->arch.mmu.pae_root[i];
2019
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002020 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002021 root &= PT64_BASE_ADDR_MASK;
2022 sp = page_header(root);
2023 mmu_sync_children(vcpu, sp);
2024 }
2025 }
2026}
2027
2028void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2029{
2030 spin_lock(&vcpu->kvm->mmu_lock);
2031 mmu_sync_roots(vcpu);
2032 spin_unlock(&vcpu->kvm->mmu_lock);
2033}
2034
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2036{
2037 return vaddr;
2038}
2039
2040static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002041 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042{
Avi Kivitye8332402007-12-09 18:43:00 +02002043 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002044 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002046 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002047 r = mmu_topup_memory_caches(vcpu);
2048 if (r)
2049 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002050
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053
Avi Kivitye8332402007-12-09 18:43:00 +02002054 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055
Avi Kivitye8332402007-12-09 18:43:00 +02002056 return nonpaging_map(vcpu, gva & PAGE_MASK,
2057 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058}
2059
Joerg Roedelfb72d162008-02-07 13:47:44 +01002060static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2061 u32 error_code)
2062{
Anthony Liguori35149e22008-04-02 14:46:56 -05002063 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002064 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002065 int largepage = 0;
2066 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002067 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002068
2069 ASSERT(vcpu);
2070 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2071
2072 r = mmu_topup_memory_caches(vcpu);
2073 if (r)
2074 return r;
2075
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002076 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2077 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2078 largepage = 1;
2079 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002080 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002081 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002082 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002083 if (is_error_pfn(pfn)) {
2084 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002085 return 1;
2086 }
2087 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002088 if (mmu_notifier_retry(vcpu, mmu_seq))
2089 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002090 kvm_mmu_free_some_pages(vcpu);
2091 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002092 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002093 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002094
2095 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002096
2097out_unlock:
2098 spin_unlock(&vcpu->kvm->mmu_lock);
2099 kvm_release_pfn_clean(pfn);
2100 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002101}
2102
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103static void nonpaging_free(struct kvm_vcpu *vcpu)
2104{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002105 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106}
2107
2108static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2109{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111
2112 context->new_cr3 = nonpaging_new_cr3;
2113 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 context->gva_to_gpa = nonpaging_gva_to_gpa;
2115 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002116 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002117 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002118 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002119 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002121 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 return 0;
2123}
2124
Avi Kivityd835dfe2007-11-21 02:57:59 +02002125void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002127 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002128 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129}
2130
2131static void paging_new_cr3(struct kvm_vcpu *vcpu)
2132{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002133 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002134 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137static void inject_page_fault(struct kvm_vcpu *vcpu,
2138 u64 addr,
2139 u32 err_code)
2140{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002141 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142}
2143
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144static void paging_free(struct kvm_vcpu *vcpu)
2145{
2146 nonpaging_free(vcpu);
2147}
2148
Dong, Eddie82725b22009-03-30 16:21:08 +08002149static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2150{
2151 int bit7;
2152
2153 bit7 = (gpte >> 7) & 1;
2154 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2155}
2156
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157#define PTTYPE 64
2158#include "paging_tmpl.h"
2159#undef PTTYPE
2160
2161#define PTTYPE 32
2162#include "paging_tmpl.h"
2163#undef PTTYPE
2164
Dong, Eddie82725b22009-03-30 16:21:08 +08002165static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2166{
2167 struct kvm_mmu *context = &vcpu->arch.mmu;
2168 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2169 u64 exb_bit_rsvd = 0;
2170
2171 if (!is_nx(vcpu))
2172 exb_bit_rsvd = rsvd_bits(63, 63);
2173 switch (level) {
2174 case PT32_ROOT_LEVEL:
2175 /* no rsvd bits for 2 level 4K page table entries */
2176 context->rsvd_bits_mask[0][1] = 0;
2177 context->rsvd_bits_mask[0][0] = 0;
2178 if (is_cpuid_PSE36())
2179 /* 36bits PSE 4MB page */
2180 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2181 else
2182 /* 32 bits PSE 4MB page */
2183 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002184 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002185 break;
2186 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002187 context->rsvd_bits_mask[0][2] =
2188 rsvd_bits(maxphyaddr, 63) |
2189 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002190 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002191 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002192 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2193 rsvd_bits(maxphyaddr, 62); /* PTE */
2194 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2195 rsvd_bits(maxphyaddr, 62) |
2196 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002197 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002198 break;
2199 case PT64_ROOT_LEVEL:
2200 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2201 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2202 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2203 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2204 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002205 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002206 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2207 rsvd_bits(maxphyaddr, 51);
2208 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2209 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2210 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002211 rsvd_bits(maxphyaddr, 51) |
2212 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002213 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002214 break;
2215 }
2216}
2217
Avi Kivity17ac10a2007-01-05 16:36:40 -08002218static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002220 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221
2222 ASSERT(is_pae(vcpu));
2223 context->new_cr3 = paging_new_cr3;
2224 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002226 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002227 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002228 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002230 context->root_level = level;
2231 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002232 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 return 0;
2234}
2235
Avi Kivity17ac10a2007-01-05 16:36:40 -08002236static int paging64_init_context(struct kvm_vcpu *vcpu)
2237{
Dong, Eddie82725b22009-03-30 16:21:08 +08002238 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002239 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2240}
2241
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242static int paging32_init_context(struct kvm_vcpu *vcpu)
2243{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002244 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245
Dong, Eddie82725b22009-03-30 16:21:08 +08002246 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 context->new_cr3 = paging_new_cr3;
2248 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 context->gva_to_gpa = paging32_gva_to_gpa;
2250 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002251 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002252 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002253 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 context->root_level = PT32_ROOT_LEVEL;
2255 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002256 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 return 0;
2258}
2259
2260static int paging32E_init_context(struct kvm_vcpu *vcpu)
2261{
Dong, Eddie82725b22009-03-30 16:21:08 +08002262 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002263 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264}
2265
Joerg Roedelfb72d162008-02-07 13:47:44 +01002266static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2267{
2268 struct kvm_mmu *context = &vcpu->arch.mmu;
2269
2270 context->new_cr3 = nonpaging_new_cr3;
2271 context->page_fault = tdp_page_fault;
2272 context->free = nonpaging_free;
2273 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002274 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002275 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002276 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002277 context->root_hpa = INVALID_PAGE;
2278
2279 if (!is_paging(vcpu)) {
2280 context->gva_to_gpa = nonpaging_gva_to_gpa;
2281 context->root_level = 0;
2282 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002283 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002284 context->gva_to_gpa = paging64_gva_to_gpa;
2285 context->root_level = PT64_ROOT_LEVEL;
2286 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002287 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002288 context->gva_to_gpa = paging64_gva_to_gpa;
2289 context->root_level = PT32E_ROOT_LEVEL;
2290 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002291 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002292 context->gva_to_gpa = paging32_gva_to_gpa;
2293 context->root_level = PT32_ROOT_LEVEL;
2294 }
2295
2296 return 0;
2297}
2298
2299static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002301 int r;
2302
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002304 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305
2306 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002307 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002308 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002309 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002311 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002313 r = paging32_init_context(vcpu);
2314
2315 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2316
2317 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318}
2319
Joerg Roedelfb72d162008-02-07 13:47:44 +01002320static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2321{
Anthony Liguori35149e22008-04-02 14:46:56 -05002322 vcpu->arch.update_pte.pfn = bad_pfn;
2323
Joerg Roedelfb72d162008-02-07 13:47:44 +01002324 if (tdp_enabled)
2325 return init_kvm_tdp_mmu(vcpu);
2326 else
2327 return init_kvm_softmmu(vcpu);
2328}
2329
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2331{
2332 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002333 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2334 vcpu->arch.mmu.free(vcpu);
2335 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336 }
2337}
2338
2339int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2340{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002341 destroy_kvm_mmu(vcpu);
2342 return init_kvm_mmu(vcpu);
2343}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002344EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002345
2346int kvm_mmu_load(struct kvm_vcpu *vcpu)
2347{
Avi Kivity714b93d2007-01-05 16:36:53 -08002348 int r;
2349
Avi Kivitye2dec932007-01-05 16:36:54 -08002350 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002351 if (r)
2352 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002353 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002354 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002355 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002356 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002357 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002358 if (r)
2359 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002360 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002361 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002362out:
2363 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002365EXPORT_SYMBOL_GPL(kvm_mmu_load);
2366
2367void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2368{
2369 mmu_free_roots(vcpu);
2370}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371
Avi Kivity09072da2007-05-01 14:16:52 +03002372static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002373 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002374 u64 *spte)
2375{
2376 u64 pte;
2377 struct kvm_mmu_page *child;
2378
2379 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002380 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002381 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2382 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002383 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002384 else {
2385 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002386 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002387 }
2388 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002389 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002390 if (is_large_pte(pte))
2391 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002392}
2393
Avi Kivity00284252007-05-01 16:53:31 +03002394static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002395 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002396 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002397 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002398{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002399 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2400 if (!vcpu->arch.update_pte.largepage ||
2401 sp->role.glevels == PT32_ROOT_LEVEL) {
2402 ++vcpu->kvm->stat.mmu_pde_zapped;
2403 return;
2404 }
2405 }
Avi Kivity00284252007-05-01 16:53:31 +03002406
Avi Kivity4cee5762007-11-18 16:37:07 +02002407 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002408 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002409 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002410 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002411 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002412}
2413
Avi Kivity79539ce2007-11-21 02:06:21 +02002414static bool need_remote_flush(u64 old, u64 new)
2415{
2416 if (!is_shadow_present_pte(old))
2417 return false;
2418 if (!is_shadow_present_pte(new))
2419 return true;
2420 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2421 return true;
2422 old ^= PT64_NX_MASK;
2423 new ^= PT64_NX_MASK;
2424 return (old & ~new & PT64_PERM_MASK) != 0;
2425}
2426
2427static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2428{
2429 if (need_remote_flush(old, new))
2430 kvm_flush_remote_tlbs(vcpu->kvm);
2431 else
2432 kvm_mmu_flush_tlb(vcpu);
2433}
2434
Avi Kivity12b7d282007-09-23 14:10:49 +02002435static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2436{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002437 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002438
Sheng Yang7b523452008-04-25 21:13:50 +08002439 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002440}
2441
Avi Kivityd7824ff2007-12-30 12:29:05 +02002442static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2443 const u8 *new, int bytes)
2444{
2445 gfn_t gfn;
2446 int r;
2447 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002448 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002449
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002450 vcpu->arch.update_pte.largepage = 0;
2451
Avi Kivityd7824ff2007-12-30 12:29:05 +02002452 if (bytes != 4 && bytes != 8)
2453 return;
2454
2455 /*
2456 * Assume that the pte write on a page table of the same type
2457 * as the current vcpu paging mode. This is nearly always true
2458 * (might be false while changing modes). Note it is verified later
2459 * by update_pte().
2460 */
2461 if (is_pae(vcpu)) {
2462 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2463 if ((bytes == 4) && (gpa % 4 == 0)) {
2464 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2465 if (r)
2466 return;
2467 memcpy((void *)&gpte + (gpa % 8), new, 4);
2468 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2469 memcpy((void *)&gpte, new, 8);
2470 }
2471 } else {
2472 if ((bytes == 4) && (gpa % 4 == 0))
2473 memcpy((void *)&gpte, new, 4);
2474 }
2475 if (!is_present_pte(gpte))
2476 return;
2477 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002478
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002479 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2480 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2481 vcpu->arch.update_pte.largepage = 1;
2482 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002483 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002484 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002485 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002486
Anthony Liguori35149e22008-04-02 14:46:56 -05002487 if (is_error_pfn(pfn)) {
2488 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002489 return;
2490 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002491 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002492 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002493}
2494
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002495static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2496{
2497 u64 *spte = vcpu->arch.last_pte_updated;
2498
2499 if (spte
2500 && vcpu->arch.last_pte_gfn == gfn
2501 && shadow_accessed_mask
2502 && !(*spte & shadow_accessed_mask)
2503 && is_shadow_present_pte(*spte))
2504 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2505}
2506
Avi Kivity09072da2007-05-01 14:16:52 +03002507void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002508 const u8 *new, int bytes,
2509 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002510{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002511 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002512 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002513 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002514 struct hlist_head *bucket;
2515 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002516 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002517 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002518 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002519 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002520 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002521 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002522 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002523 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002524 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002525 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002526 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002527
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002528 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002529 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002530 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002531 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002532 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002533 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002534 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002535 if (guest_initiated) {
2536 if (gfn == vcpu->arch.last_pt_write_gfn
2537 && !last_updated_pte_accessed(vcpu)) {
2538 ++vcpu->arch.last_pt_write_count;
2539 if (vcpu->arch.last_pt_write_count >= 3)
2540 flooded = 1;
2541 } else {
2542 vcpu->arch.last_pt_write_gfn = gfn;
2543 vcpu->arch.last_pt_write_count = 1;
2544 vcpu->arch.last_pte_updated = NULL;
2545 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002546 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002547 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002548 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002549 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002550 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002551 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002552 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002553 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002554 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002555 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002556 /*
2557 * Misaligned accesses are too much trouble to fix
2558 * up; also, they usually indicate a page is not used
2559 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002560 *
2561 * If we're seeing too many writes to a page,
2562 * it may no longer be a page table, or we may be
2563 * forking, in which case it is better to unmap the
2564 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002565 */
2566 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002567 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002568 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2569 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002570 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002571 continue;
2572 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002573 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002574 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002575 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002576 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002577 page_offset <<= 1; /* 32->64 */
2578 /*
2579 * A 32-bit pde maps 4MB while the shadow pdes map
2580 * only 2MB. So we need to double the offset again
2581 * and zap two pdes instead of one.
2582 */
2583 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002584 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002585 page_offset <<= 1;
2586 npte = 2;
2587 }
Avi Kivityfce06572007-05-01 16:44:05 +03002588 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002589 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002590 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002591 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002592 }
Avi Kivity4db35312007-11-21 15:28:32 +02002593 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002594 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2595 gentry = 0;
2596 r = kvm_read_guest_atomic(vcpu->kvm,
2597 gpa & ~(u64)(pte_size - 1),
2598 &gentry, pte_size);
2599 new = (const void *)&gentry;
2600 if (r < 0)
2601 new = NULL;
2602 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002603 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002604 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002605 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002606 if (new)
2607 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002608 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002609 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002610 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002611 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002612 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002613 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002614 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2615 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2616 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002617 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002618}
2619
Avi Kivitya4360362007-01-05 16:36:45 -08002620int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2621{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002622 gpa_t gpa;
2623 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002624
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002625 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002626
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002627 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002628 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002629 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002630 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002631}
Avi Kivity577bdc42008-07-19 08:57:05 +03002632EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002633
Avi Kivity22d95b12007-09-14 20:26:06 +03002634void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002635{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002636 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002637 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002638
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002639 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002640 struct kvm_mmu_page, link);
2641 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002642 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002643 }
2644}
Avi Kivityebeace82007-01-05 16:36:47 -08002645
Avi Kivity30677142007-10-28 18:48:59 +02002646int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2647{
2648 int r;
2649 enum emulation_result er;
2650
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002651 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002652 if (r < 0)
2653 goto out;
2654
2655 if (!r) {
2656 r = 1;
2657 goto out;
2658 }
2659
Avi Kivityb733bfb2007-10-28 18:52:05 +02002660 r = mmu_topup_memory_caches(vcpu);
2661 if (r)
2662 goto out;
2663
Avi Kivity30677142007-10-28 18:48:59 +02002664 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002665
2666 switch (er) {
2667 case EMULATE_DONE:
2668 return 1;
2669 case EMULATE_DO_MMIO:
2670 ++vcpu->stat.mmio_exits;
2671 return 0;
2672 case EMULATE_FAIL:
2673 kvm_report_emulation_failure(vcpu, "pagetable");
2674 return 1;
2675 default:
2676 BUG();
2677 }
2678out:
Avi Kivity30677142007-10-28 18:48:59 +02002679 return r;
2680}
2681EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2682
Marcelo Tosattia7052892008-09-23 13:18:35 -03002683void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2684{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002685 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002686 kvm_mmu_flush_tlb(vcpu);
2687 ++vcpu->stat.invlpg;
2688}
2689EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2690
Joerg Roedel18552672008-02-07 13:47:41 +01002691void kvm_enable_tdp(void)
2692{
2693 tdp_enabled = true;
2694}
2695EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2696
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002697void kvm_disable_tdp(void)
2698{
2699 tdp_enabled = false;
2700}
2701EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2702
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703static void free_mmu_pages(struct kvm_vcpu *vcpu)
2704{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002705 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706}
2707
2708static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2709{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002710 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 int i;
2712
2713 ASSERT(vcpu);
2714
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002715 if (vcpu->kvm->arch.n_requested_mmu_pages)
2716 vcpu->kvm->arch.n_free_mmu_pages =
2717 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002718 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002719 vcpu->kvm->arch.n_free_mmu_pages =
2720 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002721 /*
2722 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2723 * Therefore we need to allocate shadow page tables in the first
2724 * 4GB of memory, which happens to fit the DMA32 zone.
2725 */
2726 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2727 if (!page)
2728 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002729 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002730 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002731 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002732
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 return 0;
2734
2735error_1:
2736 free_mmu_pages(vcpu);
2737 return -ENOMEM;
2738}
2739
Ingo Molnar8018c272006-12-29 16:50:01 -08002740int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002743 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744
Ingo Molnar8018c272006-12-29 16:50:01 -08002745 return alloc_mmu_pages(vcpu);
2746}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747
Ingo Molnar8018c272006-12-29 16:50:01 -08002748int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2749{
2750 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002751 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002752
Ingo Molnar8018c272006-12-29 16:50:01 -08002753 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754}
2755
2756void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2757{
2758 ASSERT(vcpu);
2759
2760 destroy_kvm_mmu(vcpu);
2761 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002762 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763}
2764
Avi Kivity90cb0522007-07-17 13:04:56 +03002765void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766{
Avi Kivity4db35312007-11-21 15:28:32 +02002767 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002769 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 int i;
2771 u64 *pt;
2772
Sheng Yang291f26b2008-10-16 17:30:57 +08002773 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 continue;
2775
Avi Kivity4db35312007-11-21 15:28:32 +02002776 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2778 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002779 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 }
Avi Kivity171d5952008-08-27 16:40:51 +03002782 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002784
Avi Kivity90cb0522007-07-17 13:04:56 +03002785void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002786{
Avi Kivity4db35312007-11-21 15:28:32 +02002787 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002788
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002789 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002790 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002791 if (kvm_mmu_zap_page(kvm, sp))
2792 node = container_of(kvm->arch.active_mmu_pages.next,
2793 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002794 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002795
Avi Kivity90cb0522007-07-17 13:04:56 +03002796 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002797}
2798
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002799static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002800{
2801 struct kvm_mmu_page *page;
2802
2803 page = container_of(kvm->arch.active_mmu_pages.prev,
2804 struct kvm_mmu_page, link);
2805 kvm_mmu_zap_page(kvm, page);
2806}
2807
2808static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2809{
2810 struct kvm *kvm;
2811 struct kvm *kvm_freed = NULL;
2812 int cache_count = 0;
2813
2814 spin_lock(&kvm_lock);
2815
2816 list_for_each_entry(kvm, &vm_list, vm_list) {
2817 int npages;
2818
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002819 if (!down_read_trylock(&kvm->slots_lock))
2820 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002821 spin_lock(&kvm->mmu_lock);
2822 npages = kvm->arch.n_alloc_mmu_pages -
2823 kvm->arch.n_free_mmu_pages;
2824 cache_count += npages;
2825 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2826 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2827 cache_count--;
2828 kvm_freed = kvm;
2829 }
2830 nr_to_scan--;
2831
2832 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002833 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002834 }
2835 if (kvm_freed)
2836 list_move_tail(&kvm_freed->vm_list, &vm_list);
2837
2838 spin_unlock(&kvm_lock);
2839
2840 return cache_count;
2841}
2842
2843static struct shrinker mmu_shrinker = {
2844 .shrink = mmu_shrink,
2845 .seeks = DEFAULT_SEEKS * 10,
2846};
2847
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002848static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002849{
2850 if (pte_chain_cache)
2851 kmem_cache_destroy(pte_chain_cache);
2852 if (rmap_desc_cache)
2853 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002854 if (mmu_page_header_cache)
2855 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002856}
2857
Izik Eidus3ee16c82008-03-30 15:17:21 +03002858void kvm_mmu_module_exit(void)
2859{
2860 mmu_destroy_caches();
2861 unregister_shrinker(&mmu_shrinker);
2862}
2863
Avi Kivityb5a33a72007-04-15 16:31:09 +03002864int kvm_mmu_module_init(void)
2865{
2866 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2867 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002868 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002869 if (!pte_chain_cache)
2870 goto nomem;
2871 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2872 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002873 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002874 if (!rmap_desc_cache)
2875 goto nomem;
2876
Avi Kivityd3d25b02007-05-30 12:34:53 +03002877 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2878 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002879 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002880 if (!mmu_page_header_cache)
2881 goto nomem;
2882
Izik Eidus3ee16c82008-03-30 15:17:21 +03002883 register_shrinker(&mmu_shrinker);
2884
Avi Kivityb5a33a72007-04-15 16:31:09 +03002885 return 0;
2886
2887nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002888 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002889 return -ENOMEM;
2890}
2891
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002892/*
2893 * Caculate mmu pages needed for kvm.
2894 */
2895unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2896{
2897 int i;
2898 unsigned int nr_mmu_pages;
2899 unsigned int nr_pages = 0;
2900
2901 for (i = 0; i < kvm->nmemslots; i++)
2902 nr_pages += kvm->memslots[i].npages;
2903
2904 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2905 nr_mmu_pages = max(nr_mmu_pages,
2906 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2907
2908 return nr_mmu_pages;
2909}
2910
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002911static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2912 unsigned len)
2913{
2914 if (len > buffer->len)
2915 return NULL;
2916 return buffer->ptr;
2917}
2918
2919static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2920 unsigned len)
2921{
2922 void *ret;
2923
2924 ret = pv_mmu_peek_buffer(buffer, len);
2925 if (!ret)
2926 return ret;
2927 buffer->ptr += len;
2928 buffer->len -= len;
2929 buffer->processed += len;
2930 return ret;
2931}
2932
2933static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2934 gpa_t addr, gpa_t value)
2935{
2936 int bytes = 8;
2937 int r;
2938
2939 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2940 bytes = 4;
2941
2942 r = mmu_topup_memory_caches(vcpu);
2943 if (r)
2944 return r;
2945
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002946 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002947 return -EFAULT;
2948
2949 return 1;
2950}
2951
2952static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2953{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002954 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002955 return 1;
2956}
2957
2958static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2959{
2960 spin_lock(&vcpu->kvm->mmu_lock);
2961 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2962 spin_unlock(&vcpu->kvm->mmu_lock);
2963 return 1;
2964}
2965
2966static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2967 struct kvm_pv_mmu_op_buffer *buffer)
2968{
2969 struct kvm_mmu_op_header *header;
2970
2971 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2972 if (!header)
2973 return 0;
2974 switch (header->op) {
2975 case KVM_MMU_OP_WRITE_PTE: {
2976 struct kvm_mmu_op_write_pte *wpte;
2977
2978 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2979 if (!wpte)
2980 return 0;
2981 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2982 wpte->pte_val);
2983 }
2984 case KVM_MMU_OP_FLUSH_TLB: {
2985 struct kvm_mmu_op_flush_tlb *ftlb;
2986
2987 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2988 if (!ftlb)
2989 return 0;
2990 return kvm_pv_mmu_flush_tlb(vcpu);
2991 }
2992 case KVM_MMU_OP_RELEASE_PT: {
2993 struct kvm_mmu_op_release_pt *rpt;
2994
2995 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2996 if (!rpt)
2997 return 0;
2998 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2999 }
3000 default: return 0;
3001 }
3002}
3003
3004int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3005 gpa_t addr, unsigned long *ret)
3006{
3007 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003008 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003009
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003010 buffer->ptr = buffer->buf;
3011 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3012 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003013
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003014 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003015 if (r)
3016 goto out;
3017
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003018 while (buffer->len) {
3019 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003020 if (r < 0)
3021 goto out;
3022 if (r == 0)
3023 break;
3024 }
3025
3026 r = 1;
3027out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003028 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003029 return r;
3030}
3031
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003032#ifdef AUDIT
3033
3034static const char *audit_msg;
3035
3036static gva_t canonicalize(gva_t gva)
3037{
3038#ifdef CONFIG_X86_64
3039 gva = (long long)(gva << 16) >> 16;
3040#endif
3041 return gva;
3042}
3043
3044static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3045 gva_t va, int level)
3046{
3047 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3048 int i;
3049 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3050
3051 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3052 u64 ent = pt[i];
3053
Avi Kivityc7addb92007-09-16 18:58:32 +02003054 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003055 continue;
3056
3057 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003058 if (level > 1) {
3059 if (ent == shadow_notrap_nonpresent_pte)
3060 printk(KERN_ERR "audit: (%s) nontrapping pte"
3061 " in nonleaf level: levels %d gva %lx"
3062 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003063 vcpu->arch.mmu.root_level, va, level, ent);
Jan Kiszka34382532009-04-25 12:43:21 +02003064 else
3065 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003066 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003067 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003068 gfn_t gfn = gpa >> PAGE_SHIFT;
3069 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3070 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003071
Avi Kivityc7addb92007-09-16 18:58:32 +02003072 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003073 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003074 printk(KERN_ERR "xx audit error: (%s) levels %d"
3075 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003076 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003077 va, gpa, hpa, ent,
3078 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003079 else if (ent == shadow_notrap_nonpresent_pte
3080 && !is_error_hpa(hpa))
3081 printk(KERN_ERR "audit: (%s) notrap shadow,"
3082 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003083 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003084
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003085 }
3086 }
3087}
3088
3089static void audit_mappings(struct kvm_vcpu *vcpu)
3090{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003091 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003092
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003093 if (vcpu->arch.mmu.root_level == 4)
3094 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003095 else
3096 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003097 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003098 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003099 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003100 i << 30,
3101 2);
3102}
3103
3104static int count_rmaps(struct kvm_vcpu *vcpu)
3105{
3106 int nmaps = 0;
3107 int i, j, k;
3108
3109 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3110 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3111 struct kvm_rmap_desc *d;
3112
3113 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003114 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003115
Izik Eidus290fc382007-09-27 14:11:22 +02003116 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003117 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003118 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003119 ++nmaps;
3120 continue;
3121 }
Izik Eidus290fc382007-09-27 14:11:22 +02003122 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003123 while (d) {
3124 for (k = 0; k < RMAP_EXT; ++k)
3125 if (d->shadow_ptes[k])
3126 ++nmaps;
3127 else
3128 break;
3129 d = d->more;
3130 }
3131 }
3132 }
3133 return nmaps;
3134}
3135
3136static int count_writable_mappings(struct kvm_vcpu *vcpu)
3137{
3138 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003139 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003140 int i;
3141
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003142 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003143 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003144
Avi Kivity4db35312007-11-21 15:28:32 +02003145 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003146 continue;
3147
3148 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3149 u64 ent = pt[i];
3150
3151 if (!(ent & PT_PRESENT_MASK))
3152 continue;
3153 if (!(ent & PT_WRITABLE_MASK))
3154 continue;
3155 ++nmaps;
3156 }
3157 }
3158 return nmaps;
3159}
3160
3161static void audit_rmap(struct kvm_vcpu *vcpu)
3162{
3163 int n_rmap = count_rmaps(vcpu);
3164 int n_actual = count_writable_mappings(vcpu);
3165
3166 if (n_rmap != n_actual)
3167 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003168 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003169}
3170
3171static void audit_write_protection(struct kvm_vcpu *vcpu)
3172{
Avi Kivity4db35312007-11-21 15:28:32 +02003173 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003174 struct kvm_memory_slot *slot;
3175 unsigned long *rmapp;
3176 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003177
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003178 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003179 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003180 continue;
3181
Avi Kivity4db35312007-11-21 15:28:32 +02003182 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003183 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003184 rmapp = &slot->rmap[gfn - slot->base_gfn];
3185 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003186 printk(KERN_ERR "%s: (%s) shadow page has writable"
3187 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003188 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003189 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003190 }
3191}
3192
3193static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3194{
3195 int olddbg = dbg;
3196
3197 dbg = 0;
3198 audit_msg = msg;
3199 audit_rmap(vcpu);
3200 audit_write_protection(vcpu);
3201 audit_mappings(vcpu);
3202 dbg = olddbg;
3203}
3204
3205#endif