blob: 881ad918455c51efa1b6d712739a9a4d7cc7a761 [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.
Avi Kivity221d0592010-05-23 18:37:00 +030010 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020022#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030023#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/highmem.h>
30#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020031#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030032#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050033#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020034#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080036#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
38#include <asm/page.h>
39#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020040#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020041#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040042
Joerg Roedel18552672008-02-07 13:47:41 +010043/*
44 * When setting this variable to true it enables Two-Dimensional-Paging
45 * where the hardware walks 2 page tables:
46 * 1. the guest-virtual to guest-physical
47 * 2. while doing 1. it walks guest-physical to host-physical
48 * If the hardware supports that we don't need to do shadow paging.
49 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050050bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010051
Avi Kivity37a7d8b2007-01-05 16:36:56 -080052#undef MMU_DEBUG
53
54#undef AUDIT
55
56#ifdef AUDIT
57static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
58#else
59static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
60#endif
61
62#ifdef MMU_DEBUG
63
64#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
65#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
66
67#else
68
69#define pgprintk(x...) do { } while (0)
70#define rmap_printk(x...) do { } while (0)
71
72#endif
73
74#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030075static int dbg = 0;
76module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080077#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Marcelo Tosatti582801a2008-09-23 13:18:41 -030079static int oos_shadow = 1;
80module_param(oos_shadow, bool, 0644);
81
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080082#ifndef MMU_DEBUG
83#define ASSERT(x) do { } while (0)
84#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080085#define ASSERT(x) \
86 if (!(x)) { \
87 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
88 __FILE__, __LINE__, #x); \
89 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080090#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
Avi Kivity6aa8b732006-12-10 02:21:36 -080092#define PT_FIRST_AVAIL_BITS_SHIFT 9
93#define PT64_SECOND_AVAIL_BITS_SHIFT 52
94
Avi Kivity6aa8b732006-12-10 02:21:36 -080095#define VALID_PAGE(x) ((x) != INVALID_PAGE)
96
97#define PT64_LEVEL_BITS 9
98
99#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400100 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
102#define PT64_LEVEL_MASK(level) \
103 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
104
105#define PT64_INDEX(address, level)\
106 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
107
108
109#define PT32_LEVEL_BITS 10
110
111#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400112 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
114#define PT32_LEVEL_MASK(level) \
115 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200116#define PT32_LVL_OFFSET_MASK(level) \
117 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
118 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
120#define PT32_INDEX(address, level)\
121 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
122
123
Avi Kivity27aba762007-03-09 13:04:31 +0200124#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125#define PT64_DIR_BASE_ADDR_MASK \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200127#define PT64_LVL_ADDR_MASK(level) \
128 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
129 * PT64_LEVEL_BITS))) - 1))
130#define PT64_LVL_OFFSET_MASK(level) \
131 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
132 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133
134#define PT32_BASE_ADDR_MASK PAGE_MASK
135#define PT32_DIR_BASE_ADDR_MASK \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200137#define PT32_LVL_ADDR_MASK(level) \
138 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
Avi Kivity79539ce2007-11-21 02:06:21 +0200141#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
142 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800144#define RMAP_EXT 4
145
Avi Kivityfe135d22007-12-09 16:15:46 +0200146#define ACC_EXEC_MASK 1
147#define ACC_WRITE_MASK PT_WRITABLE_MASK
148#define ACC_USER_MASK PT_USER_MASK
149#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
150
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200151#include <trace/events/kvm.h>
152
Avi Kivity07420172009-07-06 12:21:32 +0300153#define CREATE_TRACE_POINTS
154#include "mmutrace.h"
155
Izik Eidus14032832009-09-23 21:47:17 +0300156#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
157
Avi Kivity135f8c22008-08-21 17:49:56 +0300158#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
159
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300161 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800162 struct kvm_rmap_desc *more;
163};
164
Avi Kivity2d111232008-12-25 14:39:47 +0200165struct kvm_shadow_walk_iterator {
166 u64 addr;
167 hpa_t shadow_addr;
168 int level;
169 u64 *sptep;
170 unsigned index;
171};
172
173#define for_each_shadow_entry(_vcpu, _addr, _walker) \
174 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
175 shadow_walk_okay(&(_walker)); \
176 shadow_walk_next(&(_walker)))
177
Xiao Guangrong6b184932010-04-16 21:29:17 +0800178typedef int (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300179
Avi Kivityb5a33a72007-04-15 16:31:09 +0300180static struct kmem_cache *pte_chain_cache;
181static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300182static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300183
Avi Kivityc7addb92007-09-16 18:58:32 +0200184static u64 __read_mostly shadow_trap_nonpresent_pte;
185static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800186static u64 __read_mostly shadow_base_present_pte;
187static u64 __read_mostly shadow_nx_mask;
188static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
189static u64 __read_mostly shadow_user_mask;
190static u64 __read_mostly shadow_accessed_mask;
191static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200192
Dong, Eddie82725b22009-03-30 16:21:08 +0800193static inline u64 rsvd_bits(int s, int e)
194{
195 return ((1ULL << (e - s + 1)) - 1) << s;
196}
197
Avi Kivityc7addb92007-09-16 18:58:32 +0200198void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
199{
200 shadow_trap_nonpresent_pte = trap_pte;
201 shadow_notrap_nonpresent_pte = notrap_pte;
202}
203EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
204
Sheng Yang7b523452008-04-25 21:13:50 +0800205void kvm_mmu_set_base_ptes(u64 base_pte)
206{
207 shadow_base_present_pte = base_pte;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
210
211void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800212 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800213{
214 shadow_user_mask = user_mask;
215 shadow_accessed_mask = accessed_mask;
216 shadow_dirty_mask = dirty_mask;
217 shadow_nx_mask = nx_mask;
218 shadow_x_mask = x_mask;
219}
220EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
221
Avi Kivity3dbe1412010-05-12 11:48:18 +0300222static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200224 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225}
226
227static int is_cpuid_PSE36(void)
228{
229 return 1;
230}
231
Avi Kivity73b10872007-01-26 00:56:41 -0800232static int is_nx(struct kvm_vcpu *vcpu)
233{
Avi Kivityf6801df2010-01-21 15:31:50 +0200234 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800235}
236
Avi Kivityc7addb92007-09-16 18:58:32 +0200237static int is_shadow_present_pte(u64 pte)
238{
Avi Kivityc7addb92007-09-16 18:58:32 +0200239 return pte != shadow_trap_nonpresent_pte
240 && pte != shadow_notrap_nonpresent_pte;
241}
242
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300243static int is_large_pte(u64 pte)
244{
245 return pte & PT_PAGE_SIZE_MASK;
246}
247
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900248static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249{
250 return pte & PT_WRITABLE_MASK;
251}
252
Avi Kivity43a37952009-06-10 14:12:05 +0300253static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200254{
Avi Kivity439e2182009-06-10 12:56:54 +0300255 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200256}
257
Avi Kivity43a37952009-06-10 14:12:05 +0300258static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800259{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200260 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800261}
262
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300263static int is_last_spte(u64 pte, int level)
264{
265 if (level == PT_PAGE_TABLE_LEVEL)
266 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200267 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300268 return 1;
269 return 0;
270}
271
Anthony Liguori35149e22008-04-02 14:46:56 -0500272static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200273{
Anthony Liguori35149e22008-04-02 14:46:56 -0500274 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200275}
276
Avi Kivityda9285212007-11-21 13:54:47 +0200277static gfn_t pse36_gfn_delta(u32 gpte)
278{
279 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
280
281 return (gpte & PT32_DIR_PSE36_MASK) << shift;
282}
283
Avi Kivityd555c332009-06-10 14:24:23 +0300284static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300285{
286#ifdef CONFIG_X86_64
287 set_64bit((unsigned long *)sptep, spte);
288#else
289 set_64bit((unsigned long long *)sptep, spte);
290#endif
291}
292
Avi Kivitye2dec932007-01-05 16:36:54 -0800293static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300294 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800295{
296 void *obj;
297
298 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800299 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800300 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300301 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800302 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800303 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800304 cache->objects[cache->nobjs++] = obj;
305 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800306 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800307}
308
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800309static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
310 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800311{
312 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800313 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800314}
315
Avi Kivityc1158e62007-07-20 08:18:27 +0300316static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300317 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300318{
319 struct page *page;
320
321 if (cache->nobjs >= min)
322 return 0;
323 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300325 if (!page)
326 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300327 cache->objects[cache->nobjs++] = page_address(page);
328 }
329 return 0;
330}
331
332static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
333{
334 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300335 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300336}
337
Avi Kivity8c438502007-04-16 11:53:17 +0300338static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
339{
340 int r;
341
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300343 pte_chain_cache, 4);
344 if (r)
345 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800346 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200347 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300348 if (r)
349 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300351 if (r)
352 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800353 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300354 mmu_page_header_cache, 4);
355out:
Avi Kivity8c438502007-04-16 11:53:17 +0300356 return r;
357}
358
Avi Kivity714b93d2007-01-05 16:36:53 -0800359static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
360{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800361 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
362 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800363 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800364 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
365 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800366}
367
368static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
369 size_t size)
370{
371 void *p;
372
373 BUG_ON(!mc->nobjs);
374 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800375 return p;
376}
377
Avi Kivity714b93d2007-01-05 16:36:53 -0800378static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
379{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800380 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800381 sizeof(struct kvm_pte_chain));
382}
383
Avi Kivity90cb0522007-07-17 13:04:56 +0300384static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800385{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800386 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800387}
388
389static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
390{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800391 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800392 sizeof(struct kvm_rmap_desc));
393}
394
Avi Kivity90cb0522007-07-17 13:04:56 +0300395static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800396{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800397 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800398}
399
Lai Jiangshan2032a932010-05-26 16:49:59 +0800400static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
401{
402 if (!sp->role.direct)
403 return sp->gfns[index];
404
405 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
406}
407
408static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
409{
410 if (sp->role.direct)
411 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
412 else
413 sp->gfns[index] = gfn;
414}
415
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800416/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300417 * Return the pointer to the largepage write count for a given
418 * gfn, handling slots that are not large page aligned.
419 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200420static int *slot_largepage_idx(gfn_t gfn,
421 struct kvm_memory_slot *slot,
422 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423{
424 unsigned long idx;
425
Joerg Roedeld25797b2009-07-27 16:30:43 +0200426 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
427 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
428 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300429}
430
431static void account_shadowed(struct kvm *kvm, gfn_t gfn)
432{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200433 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200435 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300436
Izik Eidus28430992008-10-03 17:40:32 +0300437 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438
439 slot = gfn_to_memslot_unaliased(kvm, gfn);
440 for (i = PT_DIRECTORY_LEVEL;
441 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
442 write_count = slot_largepage_idx(gfn, slot, i);
443 *write_count += 1;
444 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445}
446
447static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
448{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200449 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200451 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452
Izik Eidus28430992008-10-03 17:40:32 +0300453 gfn = unalias_gfn(kvm, gfn);
Wei Yongjun77a1a712010-04-16 16:21:42 +0800454 slot = gfn_to_memslot_unaliased(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200455 for (i = PT_DIRECTORY_LEVEL;
456 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200457 write_count = slot_largepage_idx(gfn, slot, i);
458 *write_count -= 1;
459 WARN_ON(*write_count < 0);
460 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461}
462
Joerg Roedeld25797b2009-07-27 16:30:43 +0200463static int has_wrprotected_page(struct kvm *kvm,
464 gfn_t gfn,
465 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466{
Izik Eidus28430992008-10-03 17:40:32 +0300467 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468 int *largepage_idx;
469
Izik Eidus28430992008-10-03 17:40:32 +0300470 gfn = unalias_gfn(kvm, gfn);
471 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200473 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474 return *largepage_idx;
475 }
476
477 return 1;
478}
479
Joerg Roedeld25797b2009-07-27 16:30:43 +0200480static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100482 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200483 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300484
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100485 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486
Joerg Roedeld25797b2009-07-27 16:30:43 +0200487 for (i = PT_PAGE_TABLE_LEVEL;
488 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
489 if (page_size >= KVM_HPAGE_SIZE(i))
490 ret = i;
491 else
492 break;
493 }
494
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300495 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300496}
497
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499{
500 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800501 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502
503 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
504 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506
Joerg Roedeld25797b2009-07-27 16:30:43 +0200507 host_level = host_mapping_level(vcpu->kvm, large_gfn);
508
509 if (host_level == PT_PAGE_TABLE_LEVEL)
510 return host_level;
511
Sheng Yang878403b2010-01-05 19:02:29 +0800512 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
513 kvm_x86_ops->get_lpage_level() : host_level;
514
515 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
517 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200518
519 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300520}
521
522/*
Izik Eidus290fc382007-09-27 14:11:22 +0200523 * Take gfn and return the reverse mapping to it.
524 * Note: gfn must be unaliased before this function get called
525 */
526
Joerg Roedel44ad9942009-07-27 16:30:42 +0200527static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200528{
529 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300530 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200531
532 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200533 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534 return &slot->rmap[gfn - slot->base_gfn];
535
Joerg Roedel44ad9942009-07-27 16:30:42 +0200536 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
537 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300538
Joerg Roedel44ad9942009-07-27 16:30:42 +0200539 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200540}
541
542/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543 * Reverse mapping data structures:
544 *
Izik Eidus290fc382007-09-27 14:11:22 +0200545 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
546 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 *
Izik Eidus290fc382007-09-27 14:11:22 +0200548 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
549 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300550 *
551 * Returns the number of rmap entries before the spte was added or zero if
552 * the spte was not added.
553 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800554 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200555static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556{
Avi Kivity4db35312007-11-21 15:28:32 +0200557 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200559 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300560 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561
Avi Kivity43a37952009-06-10 14:12:05 +0300562 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300563 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200564 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200565 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800566 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200567 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200568 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800569 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200570 *rmapp = (unsigned long)spte;
571 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800573 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300574 desc->sptes[0] = (u64 *)*rmapp;
575 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200576 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 } else {
578 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200579 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300580 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300582 count += RMAP_EXT;
583 }
Avi Kivityd555c332009-06-10 14:24:23 +0300584 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800585 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 desc = desc->more;
587 }
Avi Kivityd555c332009-06-10 14:24:23 +0300588 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300590 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800591 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300592 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800593}
594
Izik Eidus290fc382007-09-27 14:11:22 +0200595static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 struct kvm_rmap_desc *desc,
597 int i,
598 struct kvm_rmap_desc *prev_desc)
599{
600 int j;
601
Avi Kivityd555c332009-06-10 14:24:23 +0300602 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800603 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300604 desc->sptes[i] = desc->sptes[j];
605 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 if (j != 0)
607 return;
608 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300609 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800610 else
611 if (prev_desc)
612 prev_desc->more = desc->more;
613 else
Izik Eidus290fc382007-09-27 14:11:22 +0200614 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300615 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800616}
617
Izik Eidus290fc382007-09-27 14:11:22 +0200618static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620 struct kvm_rmap_desc *desc;
621 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200622 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500623 pfn_t pfn;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800624 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200625 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626 int i;
627
Avi Kivity43a37952009-06-10 14:12:05 +0300628 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200630 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500631 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800632 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500633 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900634 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300635 kvm_set_pfn_dirty(pfn);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800636 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
637 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200638 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
640 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200641 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200643 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800644 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
645 spte, *spte);
646 BUG();
647 }
Izik Eidus290fc382007-09-27 14:11:22 +0200648 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800649 } else {
650 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200651 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800652 prev_desc = NULL;
653 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300654 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
655 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200656 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800657 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800658 prev_desc);
659 return;
660 }
661 prev_desc = desc;
662 desc = desc->more;
663 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200664 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800665 BUG();
666 }
667}
668
Izik Eidus98348e92007-10-16 14:42:30 +0200669static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800670{
Avi Kivity374cbac2007-01-05 16:36:43 -0800671 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200672 u64 *prev_spte;
673 int i;
674
675 if (!*rmapp)
676 return NULL;
677 else if (!(*rmapp & 1)) {
678 if (!spte)
679 return (u64 *)*rmapp;
680 return NULL;
681 }
682 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200683 prev_spte = NULL;
684 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300685 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200686 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300687 return desc->sptes[i];
688 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200689 }
690 desc = desc->more;
691 }
692 return NULL;
693}
694
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200695static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200696{
Izik Eidus290fc382007-09-27 14:11:22 +0200697 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800698 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200699 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800700
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500701 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200702 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800703
Izik Eidus98348e92007-10-16 14:42:30 +0200704 spte = rmap_next(kvm, rmapp, NULL);
705 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800706 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800707 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800708 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900709 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300710 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800711 write_protected = 1;
712 }
Izik Eidus9647c142007-10-16 14:43:46 +0200713 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800714 }
Izik Eidus855149a2008-03-20 18:17:24 +0200715 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500716 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200717
718 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500719 pfn = spte_to_pfn(*spte);
720 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200721 }
722
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300723 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200724 for (i = PT_DIRECTORY_LEVEL;
725 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
726 rmapp = gfn_to_rmap(kvm, gfn, i);
727 spte = rmap_next(kvm, rmapp, NULL);
728 while (spte) {
729 BUG_ON(!spte);
730 BUG_ON(!(*spte & PT_PRESENT_MASK));
731 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
732 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900733 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200734 rmap_remove(kvm, spte);
735 --kvm->stat.lpages;
736 __set_spte(spte, shadow_trap_nonpresent_pte);
737 spte = NULL;
738 write_protected = 1;
739 }
740 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300741 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300742 }
743
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200744 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800745}
746
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000747static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
748 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200749{
750 u64 *spte;
751 int need_tlb_flush = 0;
752
753 while ((spte = rmap_next(kvm, rmapp, NULL))) {
754 BUG_ON(!(*spte & PT_PRESENT_MASK));
755 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
756 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300757 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200758 need_tlb_flush = 1;
759 }
760 return need_tlb_flush;
761}
762
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000763static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
764 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300765{
766 int need_flush = 0;
767 u64 *spte, new_spte;
768 pte_t *ptep = (pte_t *)data;
769 pfn_t new_pfn;
770
771 WARN_ON(pte_huge(*ptep));
772 new_pfn = pte_pfn(*ptep);
773 spte = rmap_next(kvm, rmapp, NULL);
774 while (spte) {
775 BUG_ON(!is_shadow_present_pte(*spte));
776 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
777 need_flush = 1;
778 if (pte_write(*ptep)) {
779 rmap_remove(kvm, spte);
780 __set_spte(spte, shadow_trap_nonpresent_pte);
781 spte = rmap_next(kvm, rmapp, NULL);
782 } else {
783 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
784 new_spte |= (u64)new_pfn << PAGE_SHIFT;
785
786 new_spte &= ~PT_WRITABLE_MASK;
787 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900788 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300789 kvm_set_pfn_dirty(spte_to_pfn(*spte));
790 __set_spte(spte, new_spte);
791 spte = rmap_next(kvm, rmapp, spte);
792 }
793 }
794 if (need_flush)
795 kvm_flush_remote_tlbs(kvm);
796
797 return 0;
798}
799
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000800static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
801 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300802 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000803 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200804{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200805 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200806 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200807 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200808 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200809
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800810 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200811
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200812 for (i = 0; i < slots->nmemslots; i++) {
813 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200814 unsigned long start = memslot->userspace_addr;
815 unsigned long end;
816
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200817 end = start + (memslot->npages << PAGE_SHIFT);
818 if (hva >= start && hva < end) {
819 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200820
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200821 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200822
823 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
824 int idx = gfn_offset;
825 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200826 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300827 &memslot->lpage_info[j][idx].rmap_pde,
828 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200829 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200830 trace_kvm_age_page(hva, memslot, ret);
831 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200832 }
833 }
834
835 return retval;
836}
837
838int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
839{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300840 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200841}
842
Izik Eidus3da0dd42009-09-23 21:47:18 +0300843void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
844{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000845 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300846}
847
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000848static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
849 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200850{
851 u64 *spte;
852 int young = 0;
853
Rik van Riel6316e1c2010-02-03 16:11:03 -0500854 /*
855 * Emulate the accessed bit for EPT, by checking if this page has
856 * an EPT mapping, and clearing it if it does. On the next access,
857 * a new EPT mapping will be established.
858 * This has some overhead, but not as much as the cost of swapping
859 * out actively used pages or breaking up actively used hugepages.
860 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800861 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500862 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800863
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200864 spte = rmap_next(kvm, rmapp, NULL);
865 while (spte) {
866 int _young;
867 u64 _spte = *spte;
868 BUG_ON(!(_spte & PT_PRESENT_MASK));
869 _young = _spte & PT_ACCESSED_MASK;
870 if (_young) {
871 young = 1;
872 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
873 }
874 spte = rmap_next(kvm, rmapp, spte);
875 }
876 return young;
877}
878
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300879#define RMAP_RECYCLE_THRESHOLD 1000
880
Joerg Roedel852e3c12009-07-27 16:30:44 +0200881static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300882{
883 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200884 struct kvm_mmu_page *sp;
885
886 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300887
888 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200889 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300890
Izik Eidus3da0dd42009-09-23 21:47:18 +0300891 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300892 kvm_flush_remote_tlbs(vcpu->kvm);
893}
894
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200895int kvm_age_hva(struct kvm *kvm, unsigned long hva)
896{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300897 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200898}
899
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800900#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300901static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902{
Avi Kivity139bdb22007-01-05 16:36:50 -0800903 u64 *pos;
904 u64 *end;
905
Avi Kivity47ad8e62007-05-06 15:50:58 +0300906 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300907 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800908 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800909 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800911 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 return 1;
913}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800914#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
Avi Kivity4db35312007-11-21 15:28:32 +0200916static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800917{
Avi Kivity4db35312007-11-21 15:28:32 +0200918 ASSERT(is_empty_shadow_page(sp->spt));
919 list_del(&sp->link);
920 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800921 if (!sp->role.direct)
922 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800923 kmem_cache_free(mmu_page_header_cache, sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800924 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800925}
926
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927static unsigned kvm_page_table_hashfn(gfn_t gfn)
928{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200929 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800930}
931
Avi Kivity25c0de22007-01-05 16:36:42 -0800932static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +0800933 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934{
Avi Kivity4db35312007-11-21 15:28:32 +0200935 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800937 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
938 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800939 if (!direct)
940 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
941 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200942 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800943 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800944 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200945 sp->multimapped = 0;
946 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800947 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200948 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949}
950
Avi Kivity714b93d2007-01-05 16:36:53 -0800951static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200952 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800953{
954 struct kvm_pte_chain *pte_chain;
955 struct hlist_node *node;
956 int i;
957
958 if (!parent_pte)
959 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200960 if (!sp->multimapped) {
961 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800962
963 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200964 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800965 return;
966 }
Avi Kivity4db35312007-11-21 15:28:32 +0200967 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800968 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200969 INIT_HLIST_HEAD(&sp->parent_ptes);
970 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800971 pte_chain->parent_ptes[0] = old;
972 }
Avi Kivity4db35312007-11-21 15:28:32 +0200973 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800974 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
975 continue;
976 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
977 if (!pte_chain->parent_ptes[i]) {
978 pte_chain->parent_ptes[i] = parent_pte;
979 return;
980 }
981 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800982 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800983 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200984 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800985 pte_chain->parent_ptes[0] = parent_pte;
986}
987
Avi Kivity4db35312007-11-21 15:28:32 +0200988static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800989 u64 *parent_pte)
990{
991 struct kvm_pte_chain *pte_chain;
992 struct hlist_node *node;
993 int i;
994
Avi Kivity4db35312007-11-21 15:28:32 +0200995 if (!sp->multimapped) {
996 BUG_ON(sp->parent_pte != parent_pte);
997 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800998 return;
999 }
Avi Kivity4db35312007-11-21 15:28:32 +02001000 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001001 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1002 if (!pte_chain->parent_ptes[i])
1003 break;
1004 if (pte_chain->parent_ptes[i] != parent_pte)
1005 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001006 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1007 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001008 pte_chain->parent_ptes[i]
1009 = pte_chain->parent_ptes[i + 1];
1010 ++i;
1011 }
1012 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001013 if (i == 0) {
1014 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001015 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001016 if (hlist_empty(&sp->parent_ptes)) {
1017 sp->multimapped = 0;
1018 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001019 }
1020 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001021 return;
1022 }
1023 BUG();
1024}
1025
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001026
Xiao Guangrong6b184932010-04-16 21:29:17 +08001027static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001028{
1029 struct kvm_pte_chain *pte_chain;
1030 struct hlist_node *node;
1031 struct kvm_mmu_page *parent_sp;
1032 int i;
1033
1034 if (!sp->multimapped && sp->parent_pte) {
1035 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001036 fn(parent_sp);
1037 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001038 return;
1039 }
1040 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1041 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1042 if (!pte_chain->parent_ptes[i])
1043 break;
1044 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001045 fn(parent_sp);
1046 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001047 }
1048}
1049
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001050static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1051{
1052 unsigned int index;
1053 struct kvm_mmu_page *sp = page_header(__pa(spte));
1054
1055 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001056 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1057 sp->unsync_children++;
1058 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001059}
1060
1061static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1062{
1063 struct kvm_pte_chain *pte_chain;
1064 struct hlist_node *node;
1065 int i;
1066
1067 if (!sp->parent_pte)
1068 return;
1069
1070 if (!sp->multimapped) {
1071 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1072 return;
1073 }
1074
1075 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1076 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1077 if (!pte_chain->parent_ptes[i])
1078 break;
1079 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1080 }
1081}
1082
Xiao Guangrong6b184932010-04-16 21:29:17 +08001083static int unsync_walk_fn(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001084{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001085 kvm_mmu_update_parents_unsync(sp);
1086 return 1;
1087}
1088
Xiao Guangrong6b184932010-04-16 21:29:17 +08001089static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001090{
Xiao Guangrong6b184932010-04-16 21:29:17 +08001091 mmu_parent_walk(sp, unsync_walk_fn);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001092 kvm_mmu_update_parents_unsync(sp);
1093}
1094
Avi Kivityd761a502008-05-29 14:55:03 +03001095static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1096 struct kvm_mmu_page *sp)
1097{
1098 int i;
1099
1100 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1101 sp->spt[i] = shadow_trap_nonpresent_pte;
1102}
1103
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001104static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1105 struct kvm_mmu_page *sp)
1106{
1107 return 1;
1108}
1109
Marcelo Tosattia7052892008-09-23 13:18:35 -03001110static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1111{
1112}
1113
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001114#define KVM_PAGE_ARRAY_NR 16
1115
1116struct kvm_mmu_pages {
1117 struct mmu_page_and_offset {
1118 struct kvm_mmu_page *sp;
1119 unsigned int idx;
1120 } page[KVM_PAGE_ARRAY_NR];
1121 unsigned int nr;
1122};
1123
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124#define for_each_unsync_children(bitmap, idx) \
1125 for (idx = find_first_bit(bitmap, 512); \
1126 idx < 512; \
1127 idx = find_next_bit(bitmap, 512, idx+1))
1128
Hannes Edercded19f2009-02-21 02:19:13 +01001129static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1130 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001131{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001132 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001133
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001134 if (sp->unsync)
1135 for (i=0; i < pvec->nr; i++)
1136 if (pvec->page[i].sp == sp)
1137 return 0;
1138
1139 pvec->page[pvec->nr].sp = sp;
1140 pvec->page[pvec->nr].idx = idx;
1141 pvec->nr++;
1142 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1143}
1144
1145static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1146 struct kvm_mmu_pages *pvec)
1147{
1148 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001149
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001150 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001151 u64 ent = sp->spt[i];
1152
Marcelo Tosatti87917232008-12-22 18:49:30 -02001153 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001154 struct kvm_mmu_page *child;
1155 child = page_header(ent & PT64_BASE_ADDR_MASK);
1156
1157 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001158 if (mmu_pages_add(pvec, child, i))
1159 return -ENOSPC;
1160
1161 ret = __mmu_unsync_walk(child, pvec);
1162 if (!ret)
1163 __clear_bit(i, sp->unsync_child_bitmap);
1164 else if (ret > 0)
1165 nr_unsync_leaf += ret;
1166 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001167 return ret;
1168 }
1169
1170 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001171 nr_unsync_leaf++;
1172 if (mmu_pages_add(pvec, child, i))
1173 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001174 }
1175 }
1176 }
1177
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001178 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001179 sp->unsync_children = 0;
1180
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001181 return nr_unsync_leaf;
1182}
1183
1184static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1185 struct kvm_mmu_pages *pvec)
1186{
1187 if (!sp->unsync_children)
1188 return 0;
1189
1190 mmu_pages_add(pvec, sp, 0);
1191 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001192}
1193
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001194static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1195{
1196 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001197 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001198 sp->unsync = 0;
1199 --kvm->stat.mmu_unsync;
1200}
1201
1202static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1203
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001204#define for_each_gfn_sp(kvm, sp, gfn, pos, n) \
1205 hlist_for_each_entry_safe(sp, pos, n, \
1206 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1207 if ((sp)->gfn != (gfn)) {} else
1208
1209#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos, n) \
1210 hlist_for_each_entry_safe(sp, pos, n, \
1211 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1212 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1213 (sp)->role.invalid) {} else
1214
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001215static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1216 bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001217{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001218 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001219 kvm_mmu_zap_page(vcpu->kvm, sp);
1220 return 1;
1221 }
1222
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001223 if (clear_unsync) {
1224 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1225 kvm_flush_remote_tlbs(vcpu->kvm);
1226 kvm_unlink_unsync_page(vcpu->kvm, sp);
1227 }
1228
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001229 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1230 kvm_mmu_zap_page(vcpu->kvm, sp);
1231 return 1;
1232 }
1233
1234 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001235 return 0;
1236}
1237
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001238static void mmu_convert_notrap(struct kvm_mmu_page *sp);
1239static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1240 struct kvm_mmu_page *sp)
1241{
1242 int ret;
1243
1244 ret = __kvm_sync_page(vcpu, sp, false);
1245 if (!ret)
1246 mmu_convert_notrap(sp);
1247 return ret;
1248}
1249
1250static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1251{
1252 return __kvm_sync_page(vcpu, sp, true);
1253}
1254
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001255/* @gfn should be write-protected at the call site */
1256static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1257{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001258 struct kvm_mmu_page *s;
1259 struct hlist_node *node, *n;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001260 bool flush = false;
1261
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001262 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node, n) {
1263 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001264 continue;
1265
1266 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1267 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
1268 (vcpu->arch.mmu.sync_page(vcpu, s))) {
1269 kvm_mmu_zap_page(vcpu->kvm, s);
1270 continue;
1271 }
1272 kvm_unlink_unsync_page(vcpu->kvm, s);
1273 flush = true;
1274 }
1275
1276 if (flush)
1277 kvm_mmu_flush_tlb(vcpu);
1278}
1279
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001280struct mmu_page_path {
1281 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1282 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001283};
1284
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001285#define for_each_sp(pvec, sp, parents, i) \
1286 for (i = mmu_pages_next(&pvec, &parents, -1), \
1287 sp = pvec.page[i].sp; \
1288 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1289 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001290
Hannes Edercded19f2009-02-21 02:19:13 +01001291static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1292 struct mmu_page_path *parents,
1293 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001294{
1295 int n;
1296
1297 for (n = i+1; n < pvec->nr; n++) {
1298 struct kvm_mmu_page *sp = pvec->page[n].sp;
1299
1300 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1301 parents->idx[0] = pvec->page[n].idx;
1302 return n;
1303 }
1304
1305 parents->parent[sp->role.level-2] = sp;
1306 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1307 }
1308
1309 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001310}
1311
Hannes Edercded19f2009-02-21 02:19:13 +01001312static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001313{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001314 struct kvm_mmu_page *sp;
1315 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001316
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001317 do {
1318 unsigned int idx = parents->idx[level];
1319
1320 sp = parents->parent[level];
1321 if (!sp)
1322 return;
1323
1324 --sp->unsync_children;
1325 WARN_ON((int)sp->unsync_children < 0);
1326 __clear_bit(idx, sp->unsync_child_bitmap);
1327 level++;
1328 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1329}
1330
1331static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1332 struct mmu_page_path *parents,
1333 struct kvm_mmu_pages *pvec)
1334{
1335 parents->parent[parent->role.level-1] = NULL;
1336 pvec->nr = 0;
1337}
1338
1339static void mmu_sync_children(struct kvm_vcpu *vcpu,
1340 struct kvm_mmu_page *parent)
1341{
1342 int i;
1343 struct kvm_mmu_page *sp;
1344 struct mmu_page_path parents;
1345 struct kvm_mmu_pages pages;
1346
1347 kvm_mmu_pages_init(parent, &parents, &pages);
1348 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001349 int protected = 0;
1350
1351 for_each_sp(pages, sp, parents, i)
1352 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1353
1354 if (protected)
1355 kvm_flush_remote_tlbs(vcpu->kvm);
1356
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001357 for_each_sp(pages, sp, parents, i) {
1358 kvm_sync_page(vcpu, sp);
1359 mmu_pages_clear_parents(&parents);
1360 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001361 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001362 kvm_mmu_pages_init(parent, &parents, &pages);
1363 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001364}
1365
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001366static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1367 gfn_t gfn,
1368 gva_t gaddr,
1369 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001370 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001371 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001372 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001373{
1374 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001375 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001376 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001377 struct hlist_node *node, *tmp;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001378 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001379
Avi Kivitya770f6f2008-12-21 19:20:09 +02001380 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001381 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001382 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001383 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001384 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001385 role.access = access;
Gui Jianfengb66d8002010-05-31 17:11:39 +08001386 if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001387 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1388 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1389 role.quadrant = quadrant;
1390 }
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001391 for_each_gfn_sp(vcpu->kvm, sp, gfn, node, tmp) {
1392 if (!need_sync && sp->unsync)
1393 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001394
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001395 if (sp->role.word != role.word)
1396 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001397
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001398 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1399 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001400
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001401 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1402 if (sp->unsync_children) {
1403 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1404 kvm_mmu_mark_parents_unsync(sp);
1405 } else if (sp->unsync)
1406 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001407
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001408 trace_kvm_mmu_get_page(sp, false);
1409 return sp;
1410 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001411 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001412 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001413 if (!sp)
1414 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001415 sp->gfn = gfn;
1416 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001417 hlist_add_head(&sp->hash_link,
1418 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001419 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001420 if (rmap_write_protect(vcpu->kvm, gfn))
1421 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001422 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1423 kvm_sync_pages(vcpu, gfn);
1424
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001425 account_shadowed(vcpu->kvm, gfn);
1426 }
Avi Kivity131d8272008-05-29 14:56:28 +03001427 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1428 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1429 else
1430 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001431 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001432 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001433}
1434
Avi Kivity2d111232008-12-25 14:39:47 +02001435static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1436 struct kvm_vcpu *vcpu, u64 addr)
1437{
1438 iterator->addr = addr;
1439 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1440 iterator->level = vcpu->arch.mmu.shadow_root_level;
1441 if (iterator->level == PT32E_ROOT_LEVEL) {
1442 iterator->shadow_addr
1443 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1444 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1445 --iterator->level;
1446 if (!iterator->shadow_addr)
1447 iterator->level = 0;
1448 }
1449}
1450
1451static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1452{
1453 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1454 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001455
1456 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1457 if (is_large_pte(*iterator->sptep))
1458 return false;
1459
Avi Kivity2d111232008-12-25 14:39:47 +02001460 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1461 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1462 return true;
1463}
1464
1465static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1466{
1467 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1468 --iterator->level;
1469}
1470
Avi Kivity90cb0522007-07-17 13:04:56 +03001471static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001472 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001473{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001474 unsigned i;
1475 u64 *pt;
1476 u64 ent;
1477
Avi Kivity4db35312007-11-21 15:28:32 +02001478 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001479
Avi Kivity697fe2e2007-01-05 16:36:46 -08001480 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1481 ent = pt[i];
1482
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001483 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001484 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001485 ent &= PT64_BASE_ADDR_MASK;
1486 mmu_page_remove_parent_pte(page_header(ent),
1487 &pt[i]);
1488 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001489 if (is_large_pte(ent))
1490 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001491 rmap_remove(kvm, &pt[i]);
1492 }
1493 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001494 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001495 }
Avi Kivitya4360362007-01-05 16:36:45 -08001496}
1497
Avi Kivity4db35312007-11-21 15:28:32 +02001498static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001499{
Avi Kivity4db35312007-11-21 15:28:32 +02001500 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001501}
1502
Avi Kivity12b7d282007-09-23 14:10:49 +02001503static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1504{
1505 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001506 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001507
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001508 kvm_for_each_vcpu(i, vcpu, kvm)
1509 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001510}
1511
Avi Kivity31aa2b42008-07-11 17:59:46 +03001512static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001513{
1514 u64 *parent_pte;
1515
Avi Kivity4db35312007-11-21 15:28:32 +02001516 while (sp->multimapped || sp->parent_pte) {
1517 if (!sp->multimapped)
1518 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001519 else {
1520 struct kvm_pte_chain *chain;
1521
Avi Kivity4db35312007-11-21 15:28:32 +02001522 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001523 struct kvm_pte_chain, link);
1524 parent_pte = chain->parent_ptes[0];
1525 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001526 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001527 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001528 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001529 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001530}
1531
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001532static int mmu_zap_unsync_children(struct kvm *kvm,
1533 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001534{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001535 int i, zapped = 0;
1536 struct mmu_page_path parents;
1537 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001538
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001539 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001540 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001541
1542 kvm_mmu_pages_init(parent, &parents, &pages);
1543 while (mmu_unsync_walk(parent, &pages)) {
1544 struct kvm_mmu_page *sp;
1545
1546 for_each_sp(pages, sp, parents, i) {
1547 kvm_mmu_zap_page(kvm, sp);
1548 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001549 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001550 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001551 kvm_mmu_pages_init(parent, &parents, &pages);
1552 }
1553
1554 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001555}
1556
Marcelo Tosatti07385412008-09-23 13:18:37 -03001557static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001558{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001559 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001560
1561 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001562 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001563 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001564 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001565 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001566 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001567 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001568 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001569 if (sp->unsync)
1570 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001571 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001572 /* Count self */
1573 ret++;
Avi Kivity4db35312007-11-21 15:28:32 +02001574 hlist_del(&sp->hash_link);
1575 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001576 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001577 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001578 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001579 kvm_reload_remote_mmus(kvm);
1580 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001581 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001582 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001583}
1584
Izik Eidus82ce2c92007-10-02 18:52:55 +02001585/*
1586 * Changing the number of mmu pages allocated to the vm
1587 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1588 */
1589void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1590{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001591 int used_pages;
1592
1593 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1594 used_pages = max(0, used_pages);
1595
Izik Eidus82ce2c92007-10-02 18:52:55 +02001596 /*
1597 * If we set the number of mmu pages to be smaller be than the
1598 * number of actived pages , we must to free some mmu pages before we
1599 * change the value
1600 */
1601
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001602 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001603 while (used_pages > kvm_nr_mmu_pages &&
1604 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001605 struct kvm_mmu_page *page;
1606
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001607 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001608 struct kvm_mmu_page, link);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001609 used_pages -= kvm_mmu_zap_page(kvm, page);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001610 }
Xiao Guangrong77662e02010-04-16 16:34:42 +08001611 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001612 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001613 }
1614 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001615 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1616 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001617
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001618 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001619}
1620
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001621static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001622{
Avi Kivity4db35312007-11-21 15:28:32 +02001623 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001624 struct hlist_node *node, *n;
1625 int r;
1626
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001627 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001628 r = 0;
Xiao Guangrong3246af02010-04-16 16:35:54 +08001629restart:
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001630 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node, n) {
1631 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1632 sp->role.word);
1633 r = 1;
1634 if (kvm_mmu_zap_page(kvm, sp))
1635 goto restart;
1636 }
Avi Kivitya4360362007-01-05 16:36:45 -08001637 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001638}
1639
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001640static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001641{
Avi Kivity4db35312007-11-21 15:28:32 +02001642 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001643 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001644
Xiao Guangrong3246af02010-04-16 16:35:54 +08001645restart:
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001646 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node, nn) {
1647 pgprintk("%s: zap %lx %x\n",
1648 __func__, gfn, sp->role.word);
1649 if (kvm_mmu_zap_page(kvm, sp))
1650 goto restart;
Avi Kivity97a0a012007-05-31 15:08:29 +03001651 }
1652}
1653
Avi Kivity38c335f2007-11-21 14:20:22 +02001654static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001656 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001657 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658
Sheng Yang291f26b2008-10-16 17:30:57 +08001659 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660}
1661
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001662static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1663{
1664 int i;
1665 u64 *pt = sp->spt;
1666
1667 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1668 return;
1669
1670 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1671 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001672 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001673 }
1674}
1675
Sheng Yang74be52e2008-10-09 16:01:56 +08001676/*
1677 * The function is based on mtrr_type_lookup() in
1678 * arch/x86/kernel/cpu/mtrr/generic.c
1679 */
1680static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1681 u64 start, u64 end)
1682{
1683 int i;
1684 u64 base, mask;
1685 u8 prev_match, curr_match;
1686 int num_var_ranges = KVM_NR_VAR_MTRR;
1687
1688 if (!mtrr_state->enabled)
1689 return 0xFF;
1690
1691 /* Make end inclusive end, instead of exclusive */
1692 end--;
1693
1694 /* Look in fixed ranges. Just return the type as per start */
1695 if (mtrr_state->have_fixed && (start < 0x100000)) {
1696 int idx;
1697
1698 if (start < 0x80000) {
1699 idx = 0;
1700 idx += (start >> 16);
1701 return mtrr_state->fixed_ranges[idx];
1702 } else if (start < 0xC0000) {
1703 idx = 1 * 8;
1704 idx += ((start - 0x80000) >> 14);
1705 return mtrr_state->fixed_ranges[idx];
1706 } else if (start < 0x1000000) {
1707 idx = 3 * 8;
1708 idx += ((start - 0xC0000) >> 12);
1709 return mtrr_state->fixed_ranges[idx];
1710 }
1711 }
1712
1713 /*
1714 * Look in variable ranges
1715 * Look of multiple ranges matching this address and pick type
1716 * as per MTRR precedence
1717 */
1718 if (!(mtrr_state->enabled & 2))
1719 return mtrr_state->def_type;
1720
1721 prev_match = 0xFF;
1722 for (i = 0; i < num_var_ranges; ++i) {
1723 unsigned short start_state, end_state;
1724
1725 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1726 continue;
1727
1728 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1729 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1730 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1731 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1732
1733 start_state = ((start & mask) == (base & mask));
1734 end_state = ((end & mask) == (base & mask));
1735 if (start_state != end_state)
1736 return 0xFE;
1737
1738 if ((start & mask) != (base & mask))
1739 continue;
1740
1741 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1742 if (prev_match == 0xFF) {
1743 prev_match = curr_match;
1744 continue;
1745 }
1746
1747 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1748 curr_match == MTRR_TYPE_UNCACHABLE)
1749 return MTRR_TYPE_UNCACHABLE;
1750
1751 if ((prev_match == MTRR_TYPE_WRBACK &&
1752 curr_match == MTRR_TYPE_WRTHROUGH) ||
1753 (prev_match == MTRR_TYPE_WRTHROUGH &&
1754 curr_match == MTRR_TYPE_WRBACK)) {
1755 prev_match = MTRR_TYPE_WRTHROUGH;
1756 curr_match = MTRR_TYPE_WRTHROUGH;
1757 }
1758
1759 if (prev_match != curr_match)
1760 return MTRR_TYPE_UNCACHABLE;
1761 }
1762
1763 if (prev_match != 0xFF)
1764 return prev_match;
1765
1766 return mtrr_state->def_type;
1767}
1768
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001769u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001770{
1771 u8 mtrr;
1772
1773 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1774 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1775 if (mtrr == 0xfe || mtrr == 0xff)
1776 mtrr = MTRR_TYPE_WRBACK;
1777 return mtrr;
1778}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001779EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001780
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001781static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001782{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001783 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001784 ++vcpu->kvm->stat.mmu_unsync;
1785 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001786
Xiao Guangrong6b184932010-04-16 21:29:17 +08001787 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001788 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001789}
1790
1791static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1792{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001793 struct kvm_mmu_page *s;
1794 struct hlist_node *node, *n;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001795
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001796 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node, n) {
1797 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001798 continue;
1799 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1800 __kvm_unsync_page(vcpu, s);
1801 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001802}
1803
1804static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1805 bool can_unsync)
1806{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001807 struct kvm_mmu_page *s;
1808 struct hlist_node *node, *n;
1809 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001810
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001811 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node, n) {
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001812 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001813 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001814
1815 if (!need_unsync && !s->unsync) {
1816 if (!can_unsync || !oos_shadow)
1817 return 1;
1818 need_unsync = true;
1819 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001820 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001821 if (need_unsync)
1822 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001823 return 0;
1824}
1825
Avi Kivityd555c332009-06-10 14:24:23 +03001826static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001827 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001828 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001829 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001830 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001831{
1832 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001833 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001834
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001835 /*
1836 * We don't set the accessed bit, since we sometimes want to see
1837 * whether the guest actually used the pte (in order to detect
1838 * demand paging).
1839 */
Sheng Yang7b523452008-04-25 21:13:50 +08001840 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001841 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001842 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001843 if (!dirty)
1844 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001845 if (pte_access & ACC_EXEC_MASK)
1846 spte |= shadow_x_mask;
1847 else
1848 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001849 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001850 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001851 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001852 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001853 if (tdp_enabled)
1854 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1855 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001856
Izik Eidus14032832009-09-23 21:47:17 +03001857 if (reset_host_protection)
1858 spte |= SPTE_HOST_WRITEABLE;
1859
Anthony Liguori35149e22008-04-02 14:46:56 -05001860 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001861
1862 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001863 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1864 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001865
Joerg Roedel852e3c12009-07-27 16:30:44 +02001866 if (level > PT_PAGE_TABLE_LEVEL &&
1867 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001868 ret = 1;
Xiao Guangrong6d742292010-05-13 10:07:00 +08001869 rmap_remove(vcpu->kvm, sptep);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001870 spte = shadow_trap_nonpresent_pte;
1871 goto set_pte;
1872 }
1873
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001874 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001875
Avi Kivity69325a12010-05-27 14:35:58 +03001876 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1877 spte &= ~PT_USER_MASK;
1878
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001879 /*
1880 * Optimization: for pte sync, if spte was writable the hash
1881 * lookup is unnecessary (and expensive). Write protection
1882 * is responsibility of mmu_get_page / kvm_sync_page.
1883 * Same reasoning can be applied to dirty page accounting.
1884 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001885 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001886 goto set_pte;
1887
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001888 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001889 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001890 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001891 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001892 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001893 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001894 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001895 }
1896 }
1897
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001898 if (pte_access & ACC_WRITE_MASK)
1899 mark_page_dirty(vcpu->kvm, gfn);
1900
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001901set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001902 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001903 return ret;
1904}
1905
Avi Kivityd555c332009-06-10 14:24:23 +03001906static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001907 unsigned pt_access, unsigned pte_access,
1908 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001909 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001910 pfn_t pfn, bool speculative,
1911 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001912{
1913 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001914 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001915 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001916
1917 pgprintk("%s: spte %llx access %x write_fault %d"
1918 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001919 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001920 write_fault, user_fault, gfn);
1921
Avi Kivityd555c332009-06-10 14:24:23 +03001922 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001923 /*
1924 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1925 * the parent of the now unreachable PTE.
1926 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001927 if (level > PT_PAGE_TABLE_LEVEL &&
1928 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001929 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001930 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001931
1932 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001933 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001934 __set_spte(sptep, shadow_trap_nonpresent_pte);
1935 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001936 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001937 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001938 spte_to_pfn(*sptep), pfn);
1939 rmap_remove(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08001940 __set_spte(sptep, shadow_trap_nonpresent_pte);
1941 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001942 } else
1943 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001944 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001945
Avi Kivityd555c332009-06-10 14:24:23 +03001946 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001947 dirty, level, gfn, pfn, speculative, true,
1948 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001949 if (write_fault)
1950 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001951 kvm_x86_ops->tlb_flush(vcpu);
1952 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001953
Avi Kivityd555c332009-06-10 14:24:23 +03001954 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001955 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001956 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001957 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1958 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001959 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001960 ++vcpu->kvm->stat.lpages;
1961
Avi Kivityd555c332009-06-10 14:24:23 +03001962 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001963 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001964 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001965 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001966 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001967 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001968 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001969 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001970 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001971 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001972 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001974 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001975 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001976 vcpu->arch.last_pte_gfn = gfn;
1977 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001978}
1979
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1981{
1982}
1983
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001984static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001985 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986{
Avi Kivity9f652d212008-12-25 14:54:25 +02001987 struct kvm_shadow_walk_iterator iterator;
1988 struct kvm_mmu_page *sp;
1989 int pt_write = 0;
1990 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991
Avi Kivity9f652d212008-12-25 14:54:25 +02001992 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001993 if (iterator.level == level) {
Avi Kivity9f652d212008-12-25 14:54:25 +02001994 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1995 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001996 level, gfn, pfn, false, true);
Avi Kivity9f652d212008-12-25 14:54:25 +02001997 ++vcpu->stat.pf_fixed;
1998 break;
1999 }
2000
2001 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002002 u64 base_addr = iterator.addr;
2003
2004 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2005 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d212008-12-25 14:54:25 +02002006 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2007 iterator.level - 1,
2008 1, ACC_ALL, iterator.sptep);
2009 if (!sp) {
2010 pgprintk("nonpaging_map: ENOMEM\n");
2011 kvm_release_pfn_clean(pfn);
2012 return -ENOMEM;
2013 }
2014
Avi Kivityd555c332009-06-10 14:24:23 +03002015 __set_spte(iterator.sptep,
2016 __pa(sp->spt)
2017 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2018 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02002019 }
2020 }
2021 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022}
2023
Huang Yingbf998152010-05-31 14:28:19 +08002024static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2025{
2026 char buf[1];
2027 void __user *hva;
2028 int r;
2029
2030 /* Touch the page, so send SIGBUS */
2031 hva = (void __user *)gfn_to_hva(kvm, gfn);
2032 r = copy_from_user(buf, hva, 1);
2033}
2034
2035static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2036{
2037 kvm_release_pfn_clean(pfn);
2038 if (is_hwpoison_pfn(pfn)) {
2039 kvm_send_hwpoison_signal(kvm, gfn);
2040 return 0;
2041 }
2042 return 1;
2043}
2044
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002045static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2046{
2047 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002048 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002049 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002050 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002051
Joerg Roedel852e3c12009-07-27 16:30:44 +02002052 level = mapping_level(vcpu, gfn);
2053
2054 /*
2055 * This path builds a PAE pagetable - so we can map 2mb pages at
2056 * maximum. Therefore check if the level is larger than that.
2057 */
2058 if (level > PT_DIRECTORY_LEVEL)
2059 level = PT_DIRECTORY_LEVEL;
2060
2061 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002062
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002063 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002064 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002065 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002066
Avi Kivityd196e342008-01-24 11:44:11 +02002067 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002068 if (is_error_pfn(pfn))
2069 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002070
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002071 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002072 if (mmu_notifier_retry(vcpu, mmu_seq))
2073 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002074 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002075 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002076 spin_unlock(&vcpu->kvm->mmu_lock);
2077
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002078
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002079 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002080
2081out_unlock:
2082 spin_unlock(&vcpu->kvm->mmu_lock);
2083 kvm_release_pfn_clean(pfn);
2084 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002085}
2086
2087
Avi Kivity17ac10a2007-01-05 16:36:40 -08002088static void mmu_free_roots(struct kvm_vcpu *vcpu)
2089{
2090 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002091 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002092
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002093 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002094 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002095 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002096 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2097 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002098
Avi Kivity4db35312007-11-21 15:28:32 +02002099 sp = page_header(root);
2100 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002101 if (!sp->root_count && sp->role.invalid)
2102 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002103 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002104 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002105 return;
2106 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002107 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002108 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002109
Avi Kivity417726a2007-04-12 17:35:58 +03002110 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002111 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002112 sp = page_header(root);
2113 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002114 if (!sp->root_count && sp->role.invalid)
2115 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002116 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002117 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002118 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002119 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002120 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002121}
2122
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002123static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2124{
2125 int ret = 0;
2126
2127 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2128 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2129 ret = 1;
2130 }
2131
2132 return ret;
2133}
2134
2135static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002136{
2137 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002138 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002139 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002140 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002141 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002142
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002143 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002144
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002145 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2146 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002147
2148 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002149 if (mmu_check_root(vcpu, root_gfn))
2150 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002151 if (tdp_enabled) {
2152 direct = 1;
2153 root_gfn = 0;
2154 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002155 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002156 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002157 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002158 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002159 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002160 root = __pa(sp->spt);
2161 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002162 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002163 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002164 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002165 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002166 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002167 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002168 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002169
2170 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002171 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002172 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002173 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002174 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002175 continue;
2176 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002177 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002178 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002179 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002180 if (mmu_check_root(vcpu, root_gfn))
2181 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002182 if (tdp_enabled) {
2183 direct = 1;
2184 root_gfn = i << 30;
2185 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002186 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002187 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002188 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002189 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002190 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002191 root = __pa(sp->spt);
2192 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002193 spin_unlock(&vcpu->kvm->mmu_lock);
2194
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002195 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002196 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002197 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002198 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002199}
2200
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002201static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2202{
2203 int i;
2204 struct kvm_mmu_page *sp;
2205
2206 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2207 return;
2208 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2209 hpa_t root = vcpu->arch.mmu.root_hpa;
2210 sp = page_header(root);
2211 mmu_sync_children(vcpu, sp);
2212 return;
2213 }
2214 for (i = 0; i < 4; ++i) {
2215 hpa_t root = vcpu->arch.mmu.pae_root[i];
2216
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002217 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002218 root &= PT64_BASE_ADDR_MASK;
2219 sp = page_header(root);
2220 mmu_sync_children(vcpu, sp);
2221 }
2222 }
2223}
2224
2225void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2226{
2227 spin_lock(&vcpu->kvm->mmu_lock);
2228 mmu_sync_roots(vcpu);
2229 spin_unlock(&vcpu->kvm->mmu_lock);
2230}
2231
Gleb Natapov1871c602010-02-10 14:21:32 +02002232static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2233 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234{
Gleb Natapov1871c602010-02-10 14:21:32 +02002235 if (error)
2236 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237 return vaddr;
2238}
2239
2240static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002241 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242{
Avi Kivitye8332402007-12-09 18:43:00 +02002243 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002244 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002246 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002247 r = mmu_topup_memory_caches(vcpu);
2248 if (r)
2249 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002250
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002252 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253
Avi Kivitye8332402007-12-09 18:43:00 +02002254 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255
Avi Kivitye8332402007-12-09 18:43:00 +02002256 return nonpaging_map(vcpu, gva & PAGE_MASK,
2257 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258}
2259
Joerg Roedelfb72d162008-02-07 13:47:44 +01002260static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2261 u32 error_code)
2262{
Anthony Liguori35149e22008-04-02 14:46:56 -05002263 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002264 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002265 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002266 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002267 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002268
2269 ASSERT(vcpu);
2270 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2271
2272 r = mmu_topup_memory_caches(vcpu);
2273 if (r)
2274 return r;
2275
Joerg Roedel852e3c12009-07-27 16:30:44 +02002276 level = mapping_level(vcpu, gfn);
2277
2278 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2279
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002280 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002281 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002282 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002283 if (is_error_pfn(pfn))
2284 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002285 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002286 if (mmu_notifier_retry(vcpu, mmu_seq))
2287 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002288 kvm_mmu_free_some_pages(vcpu);
2289 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002290 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002291 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002292
2293 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002294
2295out_unlock:
2296 spin_unlock(&vcpu->kvm->mmu_lock);
2297 kvm_release_pfn_clean(pfn);
2298 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002299}
2300
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301static void nonpaging_free(struct kvm_vcpu *vcpu)
2302{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002303 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304}
2305
2306static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2307{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002308 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309
2310 context->new_cr3 = nonpaging_new_cr3;
2311 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 context->gva_to_gpa = nonpaging_gva_to_gpa;
2313 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002314 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002315 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002316 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002317 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002319 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 return 0;
2321}
2322
Avi Kivityd835dfe2007-11-21 02:57:59 +02002323void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002325 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002326 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327}
2328
2329static void paging_new_cr3(struct kvm_vcpu *vcpu)
2330{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002331 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002332 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333}
2334
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335static void inject_page_fault(struct kvm_vcpu *vcpu,
2336 u64 addr,
2337 u32 err_code)
2338{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002339 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340}
2341
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342static void paging_free(struct kvm_vcpu *vcpu)
2343{
2344 nonpaging_free(vcpu);
2345}
2346
Dong, Eddie82725b22009-03-30 16:21:08 +08002347static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2348{
2349 int bit7;
2350
2351 bit7 = (gpte >> 7) & 1;
2352 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2353}
2354
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355#define PTTYPE 64
2356#include "paging_tmpl.h"
2357#undef PTTYPE
2358
2359#define PTTYPE 32
2360#include "paging_tmpl.h"
2361#undef PTTYPE
2362
Dong, Eddie82725b22009-03-30 16:21:08 +08002363static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2364{
2365 struct kvm_mmu *context = &vcpu->arch.mmu;
2366 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2367 u64 exb_bit_rsvd = 0;
2368
2369 if (!is_nx(vcpu))
2370 exb_bit_rsvd = rsvd_bits(63, 63);
2371 switch (level) {
2372 case PT32_ROOT_LEVEL:
2373 /* no rsvd bits for 2 level 4K page table entries */
2374 context->rsvd_bits_mask[0][1] = 0;
2375 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002376 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2377
2378 if (!is_pse(vcpu)) {
2379 context->rsvd_bits_mask[1][1] = 0;
2380 break;
2381 }
2382
Dong, Eddie82725b22009-03-30 16:21:08 +08002383 if (is_cpuid_PSE36())
2384 /* 36bits PSE 4MB page */
2385 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2386 else
2387 /* 32 bits PSE 4MB page */
2388 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002389 break;
2390 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002391 context->rsvd_bits_mask[0][2] =
2392 rsvd_bits(maxphyaddr, 63) |
2393 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002394 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002395 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002396 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2397 rsvd_bits(maxphyaddr, 62); /* PTE */
2398 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2399 rsvd_bits(maxphyaddr, 62) |
2400 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002401 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002402 break;
2403 case PT64_ROOT_LEVEL:
2404 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2405 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2406 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2407 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2408 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002409 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002410 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2411 rsvd_bits(maxphyaddr, 51);
2412 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002413 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2414 rsvd_bits(maxphyaddr, 51) |
2415 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002416 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002417 rsvd_bits(maxphyaddr, 51) |
2418 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002419 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002420 break;
2421 }
2422}
2423
Avi Kivity17ac10a2007-01-05 16:36:40 -08002424static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002426 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
2428 ASSERT(is_pae(vcpu));
2429 context->new_cr3 = paging_new_cr3;
2430 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002432 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002433 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002434 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002436 context->root_level = level;
2437 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002438 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 return 0;
2440}
2441
Avi Kivity17ac10a2007-01-05 16:36:40 -08002442static int paging64_init_context(struct kvm_vcpu *vcpu)
2443{
Dong, Eddie82725b22009-03-30 16:21:08 +08002444 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002445 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2446}
2447
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448static int paging32_init_context(struct kvm_vcpu *vcpu)
2449{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002450 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451
Dong, Eddie82725b22009-03-30 16:21:08 +08002452 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 context->new_cr3 = paging_new_cr3;
2454 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 context->gva_to_gpa = paging32_gva_to_gpa;
2456 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002457 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002458 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002459 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 context->root_level = PT32_ROOT_LEVEL;
2461 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002462 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 return 0;
2464}
2465
2466static int paging32E_init_context(struct kvm_vcpu *vcpu)
2467{
Dong, Eddie82725b22009-03-30 16:21:08 +08002468 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002469 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470}
2471
Joerg Roedelfb72d162008-02-07 13:47:44 +01002472static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2473{
2474 struct kvm_mmu *context = &vcpu->arch.mmu;
2475
2476 context->new_cr3 = nonpaging_new_cr3;
2477 context->page_fault = tdp_page_fault;
2478 context->free = nonpaging_free;
2479 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002480 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002481 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002482 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002483 context->root_hpa = INVALID_PAGE;
2484
2485 if (!is_paging(vcpu)) {
2486 context->gva_to_gpa = nonpaging_gva_to_gpa;
2487 context->root_level = 0;
2488 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002489 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002490 context->gva_to_gpa = paging64_gva_to_gpa;
2491 context->root_level = PT64_ROOT_LEVEL;
2492 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002493 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002494 context->gva_to_gpa = paging64_gva_to_gpa;
2495 context->root_level = PT32E_ROOT_LEVEL;
2496 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002497 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002498 context->gva_to_gpa = paging32_gva_to_gpa;
2499 context->root_level = PT32_ROOT_LEVEL;
2500 }
2501
2502 return 0;
2503}
2504
2505static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002507 int r;
2508
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002510 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511
2512 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002513 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002514 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002515 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002517 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002519 r = paging32_init_context(vcpu);
2520
Avi Kivity5b7e0102010-04-14 19:20:03 +03002521 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002522 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002523
2524 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525}
2526
Joerg Roedelfb72d162008-02-07 13:47:44 +01002527static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2528{
Anthony Liguori35149e22008-04-02 14:46:56 -05002529 vcpu->arch.update_pte.pfn = bad_pfn;
2530
Joerg Roedelfb72d162008-02-07 13:47:44 +01002531 if (tdp_enabled)
2532 return init_kvm_tdp_mmu(vcpu);
2533 else
2534 return init_kvm_softmmu(vcpu);
2535}
2536
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2538{
2539 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002540 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2541 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002542 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543}
2544
2545int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2546{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002547 destroy_kvm_mmu(vcpu);
2548 return init_kvm_mmu(vcpu);
2549}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002550EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002551
2552int kvm_mmu_load(struct kvm_vcpu *vcpu)
2553{
Avi Kivity714b93d2007-01-05 16:36:53 -08002554 int r;
2555
Avi Kivitye2dec932007-01-05 16:36:54 -08002556 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002557 if (r)
2558 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002559 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002560 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002561 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002562 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002563 if (r)
2564 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002565 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002566 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002567out:
2568 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002570EXPORT_SYMBOL_GPL(kvm_mmu_load);
2571
2572void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2573{
2574 mmu_free_roots(vcpu);
2575}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576
Avi Kivity09072da2007-05-01 14:16:52 +03002577static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002578 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002579 u64 *spte)
2580{
2581 u64 pte;
2582 struct kvm_mmu_page *child;
2583
2584 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002585 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002586 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002587 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002588 else {
2589 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002590 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002591 }
2592 }
Avi Kivityd555c332009-06-10 14:24:23 +03002593 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002594 if (is_large_pte(pte))
2595 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002596}
2597
Avi Kivity00284252007-05-01 16:53:31 +03002598static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002599 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002600 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002601 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002602{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002603 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002604 ++vcpu->kvm->stat.mmu_pde_zapped;
2605 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002606 }
Avi Kivity00284252007-05-01 16:53:31 +03002607
Avi Kivity4cee5762007-11-18 16:37:07 +02002608 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002609 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002610 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002611 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002612 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002613}
2614
Avi Kivity79539ce2007-11-21 02:06:21 +02002615static bool need_remote_flush(u64 old, u64 new)
2616{
2617 if (!is_shadow_present_pte(old))
2618 return false;
2619 if (!is_shadow_present_pte(new))
2620 return true;
2621 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2622 return true;
2623 old ^= PT64_NX_MASK;
2624 new ^= PT64_NX_MASK;
2625 return (old & ~new & PT64_PERM_MASK) != 0;
2626}
2627
2628static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2629{
2630 if (need_remote_flush(old, new))
2631 kvm_flush_remote_tlbs(vcpu->kvm);
2632 else
2633 kvm_mmu_flush_tlb(vcpu);
2634}
2635
Avi Kivity12b7d282007-09-23 14:10:49 +02002636static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2637{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002638 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002639
Sheng Yang7b523452008-04-25 21:13:50 +08002640 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002641}
2642
Avi Kivityd7824ff2007-12-30 12:29:05 +02002643static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002644 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002645{
2646 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002647 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002648
Avi Kivity43a37952009-06-10 14:12:05 +03002649 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002650 return;
2651 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002652
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002653 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002654 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002655 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002656
Anthony Liguori35149e22008-04-02 14:46:56 -05002657 if (is_error_pfn(pfn)) {
2658 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002659 return;
2660 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002661 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002662 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002663}
2664
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002665static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2666{
2667 u64 *spte = vcpu->arch.last_pte_updated;
2668
2669 if (spte
2670 && vcpu->arch.last_pte_gfn == gfn
2671 && shadow_accessed_mask
2672 && !(*spte & shadow_accessed_mask)
2673 && is_shadow_present_pte(*spte))
2674 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2675}
2676
Avi Kivity09072da2007-05-01 14:16:52 +03002677void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002678 const u8 *new, int bytes,
2679 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002680{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002681 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002682 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002683 struct hlist_node *node, *n;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002684 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002685 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002686 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002687 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002688 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002689 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002690 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002691 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002692 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002693 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002694 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002695 int invlpg_counter;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002696
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002697 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002698
Avi Kivity08e850c2010-03-15 13:59:57 +02002699 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2700
2701 /*
2702 * Assume that the pte write on a page table of the same type
2703 * as the current vcpu paging mode. This is nearly always true
2704 * (might be false while changing modes). Note it is verified later
2705 * by update_pte().
2706 */
2707 if ((is_pae(vcpu) && bytes == 4) || !new) {
2708 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2709 if (is_pae(vcpu)) {
2710 gpa &= ~(gpa_t)7;
2711 bytes = 8;
2712 }
2713 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2714 if (r)
2715 gentry = 0;
2716 new = (const u8 *)&gentry;
2717 }
2718
Avi Kivity72016f32010-03-15 13:59:53 +02002719 switch (bytes) {
2720 case 4:
2721 gentry = *(const u32 *)new;
2722 break;
2723 case 8:
2724 gentry = *(const u64 *)new;
2725 break;
2726 default:
2727 gentry = 0;
2728 break;
2729 }
2730
Avi Kivity72016f32010-03-15 13:59:53 +02002731 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002732 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002733 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2734 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002735 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002736 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002737 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002738 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002739 if (guest_initiated) {
2740 if (gfn == vcpu->arch.last_pt_write_gfn
2741 && !last_updated_pte_accessed(vcpu)) {
2742 ++vcpu->arch.last_pt_write_count;
2743 if (vcpu->arch.last_pt_write_count >= 3)
2744 flooded = 1;
2745 } else {
2746 vcpu->arch.last_pt_write_gfn = gfn;
2747 vcpu->arch.last_pt_write_count = 1;
2748 vcpu->arch.last_pte_updated = NULL;
2749 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002750 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002751
2752restart:
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002753 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node, n) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002754 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002755 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002756 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002757 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002758 /*
2759 * Misaligned accesses are too much trouble to fix
2760 * up; also, they usually indicate a page is not used
2761 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002762 *
2763 * If we're seeing too many writes to a page,
2764 * it may no longer be a page table, or we may be
2765 * forking, in which case it is better to unmap the
2766 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002767 */
2768 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002769 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002770 if (kvm_mmu_zap_page(vcpu->kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002771 goto restart;
Avi Kivity4cee5762007-11-18 16:37:07 +02002772 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002773 continue;
2774 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002775 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002776 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002777 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002778 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002779 page_offset <<= 1; /* 32->64 */
2780 /*
2781 * A 32-bit pde maps 4MB while the shadow pdes map
2782 * only 2MB. So we need to double the offset again
2783 * and zap two pdes instead of one.
2784 */
2785 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002786 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002787 page_offset <<= 1;
2788 npte = 2;
2789 }
Avi Kivityfce06572007-05-01 16:44:05 +03002790 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002791 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002792 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002793 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002794 }
Avi Kivity4db35312007-11-21 15:28:32 +02002795 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002796 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002797 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002798 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002799 if (gentry)
2800 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Avi Kivity79539ce2007-11-21 02:06:21 +02002801 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002802 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002803 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002804 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002805 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002806 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002807 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2808 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2809 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002810 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002811}
2812
Avi Kivitya4360362007-01-05 16:36:45 -08002813int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2814{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002815 gpa_t gpa;
2816 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002817
Avi Kivity60f24782009-08-27 13:37:06 +03002818 if (tdp_enabled)
2819 return 0;
2820
Gleb Natapov1871c602010-02-10 14:21:32 +02002821 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002822
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002823 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002824 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002825 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002826 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002827}
Avi Kivity577bdc42008-07-19 08:57:05 +03002828EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002829
Avi Kivity22d95b12007-09-14 20:26:06 +03002830void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002831{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002832 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2833 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002834 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002835
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002836 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002837 struct kvm_mmu_page, link);
2838 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002839 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002840 }
2841}
Avi Kivityebeace82007-01-05 16:36:47 -08002842
Avi Kivity30677142007-10-28 18:48:59 +02002843int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2844{
2845 int r;
2846 enum emulation_result er;
2847
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002849 if (r < 0)
2850 goto out;
2851
2852 if (!r) {
2853 r = 1;
2854 goto out;
2855 }
2856
Avi Kivityb733bfb2007-10-28 18:52:05 +02002857 r = mmu_topup_memory_caches(vcpu);
2858 if (r)
2859 goto out;
2860
Avi Kivity851ba692009-08-24 11:10:17 +03002861 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002862
2863 switch (er) {
2864 case EMULATE_DONE:
2865 return 1;
2866 case EMULATE_DO_MMIO:
2867 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002868 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02002869 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002870 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002871 default:
2872 BUG();
2873 }
2874out:
Avi Kivity30677142007-10-28 18:48:59 +02002875 return r;
2876}
2877EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2878
Marcelo Tosattia7052892008-09-23 13:18:35 -03002879void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2880{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002881 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002882 kvm_mmu_flush_tlb(vcpu);
2883 ++vcpu->stat.invlpg;
2884}
2885EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2886
Joerg Roedel18552672008-02-07 13:47:41 +01002887void kvm_enable_tdp(void)
2888{
2889 tdp_enabled = true;
2890}
2891EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2892
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002893void kvm_disable_tdp(void)
2894{
2895 tdp_enabled = false;
2896}
2897EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2898
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899static void free_mmu_pages(struct kvm_vcpu *vcpu)
2900{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002901 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902}
2903
2904static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2905{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002906 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 int i;
2908
2909 ASSERT(vcpu);
2910
Avi Kivity17ac10a2007-01-05 16:36:40 -08002911 /*
2912 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2913 * Therefore we need to allocate shadow page tables in the first
2914 * 4GB of memory, which happens to fit the DMA32 zone.
2915 */
2916 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2917 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002918 return -ENOMEM;
2919
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002920 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002921 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002922 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002923
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925}
2926
Ingo Molnar8018c272006-12-29 16:50:01 -08002927int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002930 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931
Ingo Molnar8018c272006-12-29 16:50:01 -08002932 return alloc_mmu_pages(vcpu);
2933}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934
Ingo Molnar8018c272006-12-29 16:50:01 -08002935int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2936{
2937 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002938 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002939
Ingo Molnar8018c272006-12-29 16:50:01 -08002940 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941}
2942
2943void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2944{
2945 ASSERT(vcpu);
2946
2947 destroy_kvm_mmu(vcpu);
2948 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002949 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950}
2951
Avi Kivity90cb0522007-07-17 13:04:56 +03002952void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953{
Avi Kivity4db35312007-11-21 15:28:32 +02002954 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002956 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 int i;
2958 u64 *pt;
2959
Sheng Yang291f26b2008-10-16 17:30:57 +08002960 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 continue;
2962
Avi Kivity4db35312007-11-21 15:28:32 +02002963 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2965 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08002966 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 }
Avi Kivity171d5952008-08-27 16:40:51 +03002969 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002971
Avi Kivity90cb0522007-07-17 13:04:56 +03002972void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002973{
Avi Kivity4db35312007-11-21 15:28:32 +02002974 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002975
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002976 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08002977restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002978 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002979 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002980 goto restart;
2981
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002982 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002983
Avi Kivity90cb0522007-07-17 13:04:56 +03002984 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002985}
2986
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002987static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002988{
2989 struct kvm_mmu_page *page;
2990
2991 page = container_of(kvm->arch.active_mmu_pages.prev,
2992 struct kvm_mmu_page, link);
Gui Jianfeng54a4f022010-05-05 09:03:49 +08002993 return kvm_mmu_zap_page(kvm, page);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002994}
2995
Dave Chinner7f8275d2010-07-19 14:56:17 +10002996static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002997{
2998 struct kvm *kvm;
2999 struct kvm *kvm_freed = NULL;
3000 int cache_count = 0;
3001
3002 spin_lock(&kvm_lock);
3003
3004 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003005 int npages, idx, freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003006
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003007 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003008 spin_lock(&kvm->mmu_lock);
3009 npages = kvm->arch.n_alloc_mmu_pages -
3010 kvm->arch.n_free_mmu_pages;
3011 cache_count += npages;
3012 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003013 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm);
3014 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003015 kvm_freed = kvm;
3016 }
3017 nr_to_scan--;
3018
3019 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003020 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003021 }
3022 if (kvm_freed)
3023 list_move_tail(&kvm_freed->vm_list, &vm_list);
3024
3025 spin_unlock(&kvm_lock);
3026
3027 return cache_count;
3028}
3029
3030static struct shrinker mmu_shrinker = {
3031 .shrink = mmu_shrink,
3032 .seeks = DEFAULT_SEEKS * 10,
3033};
3034
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003035static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003036{
3037 if (pte_chain_cache)
3038 kmem_cache_destroy(pte_chain_cache);
3039 if (rmap_desc_cache)
3040 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003041 if (mmu_page_header_cache)
3042 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003043}
3044
Izik Eidus3ee16c82008-03-30 15:17:21 +03003045void kvm_mmu_module_exit(void)
3046{
3047 mmu_destroy_caches();
3048 unregister_shrinker(&mmu_shrinker);
3049}
3050
Avi Kivityb5a33a72007-04-15 16:31:09 +03003051int kvm_mmu_module_init(void)
3052{
3053 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3054 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003055 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003056 if (!pte_chain_cache)
3057 goto nomem;
3058 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3059 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003060 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003061 if (!rmap_desc_cache)
3062 goto nomem;
3063
Avi Kivityd3d25b02007-05-30 12:34:53 +03003064 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3065 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003066 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003067 if (!mmu_page_header_cache)
3068 goto nomem;
3069
Izik Eidus3ee16c82008-03-30 15:17:21 +03003070 register_shrinker(&mmu_shrinker);
3071
Avi Kivityb5a33a72007-04-15 16:31:09 +03003072 return 0;
3073
3074nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003075 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003076 return -ENOMEM;
3077}
3078
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003079/*
3080 * Caculate mmu pages needed for kvm.
3081 */
3082unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3083{
3084 int i;
3085 unsigned int nr_mmu_pages;
3086 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003087 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003088
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003089 slots = kvm_memslots(kvm);
3090
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003091 for (i = 0; i < slots->nmemslots; i++)
3092 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003093
3094 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3095 nr_mmu_pages = max(nr_mmu_pages,
3096 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3097
3098 return nr_mmu_pages;
3099}
3100
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003101static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3102 unsigned len)
3103{
3104 if (len > buffer->len)
3105 return NULL;
3106 return buffer->ptr;
3107}
3108
3109static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3110 unsigned len)
3111{
3112 void *ret;
3113
3114 ret = pv_mmu_peek_buffer(buffer, len);
3115 if (!ret)
3116 return ret;
3117 buffer->ptr += len;
3118 buffer->len -= len;
3119 buffer->processed += len;
3120 return ret;
3121}
3122
3123static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3124 gpa_t addr, gpa_t value)
3125{
3126 int bytes = 8;
3127 int r;
3128
3129 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3130 bytes = 4;
3131
3132 r = mmu_topup_memory_caches(vcpu);
3133 if (r)
3134 return r;
3135
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003136 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003137 return -EFAULT;
3138
3139 return 1;
3140}
3141
3142static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3143{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003144 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003145 return 1;
3146}
3147
3148static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3149{
3150 spin_lock(&vcpu->kvm->mmu_lock);
3151 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3152 spin_unlock(&vcpu->kvm->mmu_lock);
3153 return 1;
3154}
3155
3156static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3157 struct kvm_pv_mmu_op_buffer *buffer)
3158{
3159 struct kvm_mmu_op_header *header;
3160
3161 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3162 if (!header)
3163 return 0;
3164 switch (header->op) {
3165 case KVM_MMU_OP_WRITE_PTE: {
3166 struct kvm_mmu_op_write_pte *wpte;
3167
3168 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3169 if (!wpte)
3170 return 0;
3171 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3172 wpte->pte_val);
3173 }
3174 case KVM_MMU_OP_FLUSH_TLB: {
3175 struct kvm_mmu_op_flush_tlb *ftlb;
3176
3177 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3178 if (!ftlb)
3179 return 0;
3180 return kvm_pv_mmu_flush_tlb(vcpu);
3181 }
3182 case KVM_MMU_OP_RELEASE_PT: {
3183 struct kvm_mmu_op_release_pt *rpt;
3184
3185 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3186 if (!rpt)
3187 return 0;
3188 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3189 }
3190 default: return 0;
3191 }
3192}
3193
3194int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3195 gpa_t addr, unsigned long *ret)
3196{
3197 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003198 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003199
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003200 buffer->ptr = buffer->buf;
3201 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3202 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003203
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003204 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003205 if (r)
3206 goto out;
3207
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003208 while (buffer->len) {
3209 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003210 if (r < 0)
3211 goto out;
3212 if (r == 0)
3213 break;
3214 }
3215
3216 r = 1;
3217out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003218 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003219 return r;
3220}
3221
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003222int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3223{
3224 struct kvm_shadow_walk_iterator iterator;
3225 int nr_sptes = 0;
3226
3227 spin_lock(&vcpu->kvm->mmu_lock);
3228 for_each_shadow_entry(vcpu, addr, iterator) {
3229 sptes[iterator.level-1] = *iterator.sptep;
3230 nr_sptes++;
3231 if (!is_shadow_present_pte(*iterator.sptep))
3232 break;
3233 }
3234 spin_unlock(&vcpu->kvm->mmu_lock);
3235
3236 return nr_sptes;
3237}
3238EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3239
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003240#ifdef AUDIT
3241
3242static const char *audit_msg;
3243
3244static gva_t canonicalize(gva_t gva)
3245{
3246#ifdef CONFIG_X86_64
3247 gva = (long long)(gva << 16) >> 16;
3248#endif
3249 return gva;
3250}
3251
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003252
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003253typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003254
3255static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3256 inspect_spte_fn fn)
3257{
3258 int i;
3259
3260 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3261 u64 ent = sp->spt[i];
3262
3263 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003264 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003265 struct kvm_mmu_page *child;
3266 child = page_header(ent & PT64_BASE_ADDR_MASK);
3267 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003268 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003269 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003270 }
3271 }
3272}
3273
3274static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3275{
3276 int i;
3277 struct kvm_mmu_page *sp;
3278
3279 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3280 return;
3281 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3282 hpa_t root = vcpu->arch.mmu.root_hpa;
3283 sp = page_header(root);
3284 __mmu_spte_walk(vcpu->kvm, sp, fn);
3285 return;
3286 }
3287 for (i = 0; i < 4; ++i) {
3288 hpa_t root = vcpu->arch.mmu.pae_root[i];
3289
3290 if (root && VALID_PAGE(root)) {
3291 root &= PT64_BASE_ADDR_MASK;
3292 sp = page_header(root);
3293 __mmu_spte_walk(vcpu->kvm, sp, fn);
3294 }
3295 }
3296 return;
3297}
3298
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003299static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3300 gva_t va, int level)
3301{
3302 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3303 int i;
3304 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3305
3306 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3307 u64 ent = pt[i];
3308
Avi Kivityc7addb92007-09-16 18:58:32 +02003309 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003310 continue;
3311
3312 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003313 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3314 audit_mappings_page(vcpu, ent, va, level - 1);
3315 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003316 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003317 gfn_t gfn = gpa >> PAGE_SHIFT;
3318 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3319 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003320
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003321 if (is_error_pfn(pfn)) {
3322 kvm_release_pfn_clean(pfn);
3323 continue;
3324 }
3325
Avi Kivityc7addb92007-09-16 18:58:32 +02003326 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003327 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003328 printk(KERN_ERR "xx audit error: (%s) levels %d"
3329 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003330 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003331 va, gpa, hpa, ent,
3332 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003333 else if (ent == shadow_notrap_nonpresent_pte
3334 && !is_error_hpa(hpa))
3335 printk(KERN_ERR "audit: (%s) notrap shadow,"
3336 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003337 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003338
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003339 }
3340 }
3341}
3342
3343static void audit_mappings(struct kvm_vcpu *vcpu)
3344{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003345 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003346
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003347 if (vcpu->arch.mmu.root_level == 4)
3348 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003349 else
3350 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003351 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003352 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003353 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003354 i << 30,
3355 2);
3356}
3357
3358static int count_rmaps(struct kvm_vcpu *vcpu)
3359{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003360 struct kvm *kvm = vcpu->kvm;
3361 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003362 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003363 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003364
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003365 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003366 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003368 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369 struct kvm_rmap_desc *d;
3370
3371 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003372 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003373
Izik Eidus290fc382007-09-27 14:11:22 +02003374 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003375 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003376 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003377 ++nmaps;
3378 continue;
3379 }
Izik Eidus290fc382007-09-27 14:11:22 +02003380 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003381 while (d) {
3382 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003383 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003384 ++nmaps;
3385 else
3386 break;
3387 d = d->more;
3388 }
3389 }
3390 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003391 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003392 return nmaps;
3393}
3394
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003395void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003396{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003397 unsigned long *rmapp;
3398 struct kvm_mmu_page *rev_sp;
3399 gfn_t gfn;
3400
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003401 if (is_writable_pte(*sptep)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003402 rev_sp = page_header(__pa(sptep));
Lai Jiangshan2032a932010-05-26 16:49:59 +08003403 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003404
3405 if (!gfn_to_memslot(kvm, gfn)) {
3406 if (!printk_ratelimit())
3407 return;
3408 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3409 audit_msg, gfn);
3410 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003411 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003412 rev_sp->gfn);
3413 dump_stack();
3414 return;
3415 }
3416
Lai Jiangshan2032a932010-05-26 16:49:59 +08003417 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003418 if (!*rmapp) {
3419 if (!printk_ratelimit())
3420 return;
3421 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3422 audit_msg, *sptep);
3423 dump_stack();
3424 }
3425 }
3426
3427}
3428
3429void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3430{
3431 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3432}
3433
3434static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3435{
Avi Kivity4db35312007-11-21 15:28:32 +02003436 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003437 int i;
3438
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003439 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003440 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003441
Avi Kivity4db35312007-11-21 15:28:32 +02003442 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003443 continue;
3444
3445 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3446 u64 ent = pt[i];
3447
3448 if (!(ent & PT_PRESENT_MASK))
3449 continue;
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003450 if (!is_writable_pte(ent))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003451 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003452 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003453 }
3454 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003455 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003456}
3457
3458static void audit_rmap(struct kvm_vcpu *vcpu)
3459{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003460 check_writable_mappings_rmap(vcpu);
3461 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003462}
3463
3464static void audit_write_protection(struct kvm_vcpu *vcpu)
3465{
Avi Kivity4db35312007-11-21 15:28:32 +02003466 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003467 struct kvm_memory_slot *slot;
3468 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003469 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003470 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003471
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003472 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003473 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003474 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003475 if (sp->unsync)
3476 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003477
Avi Kivity4db35312007-11-21 15:28:32 +02003478 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003479 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003480 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003481
3482 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3483 while (spte) {
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003484 if (is_writable_pte(*spte))
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003485 printk(KERN_ERR "%s: (%s) shadow page has "
3486 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003487 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003488 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003489 spte = rmap_next(vcpu->kvm, rmapp, spte);
3490 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003491 }
3492}
3493
3494static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3495{
3496 int olddbg = dbg;
3497
3498 dbg = 0;
3499 audit_msg = msg;
3500 audit_rmap(vcpu);
3501 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003502 if (strcmp("pre pte write", audit_msg) != 0)
3503 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003504 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003505 dbg = olddbg;
3506}
3507
3508#endif