blob: ba119dae890ebf75f40bc63a479e18804a572bc8 [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
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800400/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300401 * Return the pointer to the largepage write count for a given
402 * gfn, handling slots that are not large page aligned.
403 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200404static int *slot_largepage_idx(gfn_t gfn,
405 struct kvm_memory_slot *slot,
406 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300407{
408 unsigned long idx;
409
Joerg Roedeld25797b2009-07-27 16:30:43 +0200410 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
411 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
412 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300413}
414
415static void account_shadowed(struct kvm *kvm, gfn_t gfn)
416{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200417 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200419 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300420
Izik Eidus28430992008-10-03 17:40:32 +0300421 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200422
423 slot = gfn_to_memslot_unaliased(kvm, gfn);
424 for (i = PT_DIRECTORY_LEVEL;
425 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
426 write_count = slot_largepage_idx(gfn, slot, i);
427 *write_count += 1;
428 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300429}
430
431static void unaccount_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);
Wei Yongjun77a1a712010-04-16 16:21:42 +0800438 slot = gfn_to_memslot_unaliased(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200439 for (i = PT_DIRECTORY_LEVEL;
440 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200441 write_count = slot_largepage_idx(gfn, slot, i);
442 *write_count -= 1;
443 WARN_ON(*write_count < 0);
444 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445}
446
Joerg Roedeld25797b2009-07-27 16:30:43 +0200447static int has_wrprotected_page(struct kvm *kvm,
448 gfn_t gfn,
449 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450{
Izik Eidus28430992008-10-03 17:40:32 +0300451 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452 int *largepage_idx;
453
Izik Eidus28430992008-10-03 17:40:32 +0300454 gfn = unalias_gfn(kvm, gfn);
455 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300456 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200457 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300458 return *largepage_idx;
459 }
460
461 return 1;
462}
463
Joerg Roedeld25797b2009-07-27 16:30:43 +0200464static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300465{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100466 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200467 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100469 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470
Joerg Roedeld25797b2009-07-27 16:30:43 +0200471 for (i = PT_PAGE_TABLE_LEVEL;
472 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
473 if (page_size >= KVM_HPAGE_SIZE(i))
474 ret = i;
475 else
476 break;
477 }
478
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300479 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300480}
481
Joerg Roedeld25797b2009-07-27 16:30:43 +0200482static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300483{
484 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800485 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486
487 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
488 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200489 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300490
Joerg Roedeld25797b2009-07-27 16:30:43 +0200491 host_level = host_mapping_level(vcpu->kvm, large_gfn);
492
493 if (host_level == PT_PAGE_TABLE_LEVEL)
494 return host_level;
495
Sheng Yang878403b2010-01-05 19:02:29 +0800496 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
497 kvm_x86_ops->get_lpage_level() : host_level;
498
499 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200500 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
501 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200502
503 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300504}
505
506/*
Izik Eidus290fc382007-09-27 14:11:22 +0200507 * Take gfn and return the reverse mapping to it.
508 * Note: gfn must be unaliased before this function get called
509 */
510
Joerg Roedel44ad9942009-07-27 16:30:42 +0200511static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200512{
513 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300514 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200515
516 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200517 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300518 return &slot->rmap[gfn - slot->base_gfn];
519
Joerg Roedel44ad9942009-07-27 16:30:42 +0200520 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
521 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300522
Joerg Roedel44ad9942009-07-27 16:30:42 +0200523 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200524}
525
526/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527 * Reverse mapping data structures:
528 *
Izik Eidus290fc382007-09-27 14:11:22 +0200529 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
530 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800531 *
Izik Eidus290fc382007-09-27 14:11:22 +0200532 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
533 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300534 *
535 * Returns the number of rmap entries before the spte was added or zero if
536 * the spte was not added.
537 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200539static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800540{
Avi Kivity4db35312007-11-21 15:28:32 +0200541 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800542 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200543 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300544 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800545
Avi Kivity43a37952009-06-10 14:12:05 +0300546 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300547 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200548 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200549 sp = page_header(__pa(spte));
550 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200551 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200552 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200554 *rmapp = (unsigned long)spte;
555 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800557 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300558 desc->sptes[0] = (u64 *)*rmapp;
559 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200560 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 } else {
562 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200563 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300564 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800565 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300566 count += RMAP_EXT;
567 }
Avi Kivityd555c332009-06-10 14:24:23 +0300568 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800569 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800570 desc = desc->more;
571 }
Avi Kivityd555c332009-06-10 14:24:23 +0300572 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800573 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300574 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300576 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577}
578
Izik Eidus290fc382007-09-27 14:11:22 +0200579static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 struct kvm_rmap_desc *desc,
581 int i,
582 struct kvm_rmap_desc *prev_desc)
583{
584 int j;
585
Avi Kivityd555c332009-06-10 14:24:23 +0300586 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800587 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300588 desc->sptes[i] = desc->sptes[j];
589 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800590 if (j != 0)
591 return;
592 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300593 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 else
595 if (prev_desc)
596 prev_desc->more = desc->more;
597 else
Izik Eidus290fc382007-09-27 14:11:22 +0200598 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300599 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600}
601
Izik Eidus290fc382007-09-27 14:11:22 +0200602static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800603{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800604 struct kvm_rmap_desc *desc;
605 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200606 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500607 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200608 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609 int i;
610
Avi Kivity43a37952009-06-10 14:12:05 +0300611 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800612 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200613 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500614 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800615 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500616 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900617 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300618 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200619 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200620 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800621 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
622 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200623 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800624 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200625 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
627 spte, *spte);
628 BUG();
629 }
Izik Eidus290fc382007-09-27 14:11:22 +0200630 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800631 } else {
632 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200633 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800634 prev_desc = NULL;
635 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300636 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
637 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200638 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800639 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800640 prev_desc);
641 return;
642 }
643 prev_desc = desc;
644 desc = desc->more;
645 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200646 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800647 BUG();
648 }
649}
650
Izik Eidus98348e92007-10-16 14:42:30 +0200651static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800652{
Avi Kivity374cbac2007-01-05 16:36:43 -0800653 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200654 u64 *prev_spte;
655 int i;
656
657 if (!*rmapp)
658 return NULL;
659 else if (!(*rmapp & 1)) {
660 if (!spte)
661 return (u64 *)*rmapp;
662 return NULL;
663 }
664 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200665 prev_spte = NULL;
666 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300667 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200668 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300669 return desc->sptes[i];
670 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200671 }
672 desc = desc->more;
673 }
674 return NULL;
675}
676
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200677static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200678{
Izik Eidus290fc382007-09-27 14:11:22 +0200679 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800680 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200681 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800682
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500683 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200684 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800685
Izik Eidus98348e92007-10-16 14:42:30 +0200686 spte = rmap_next(kvm, rmapp, NULL);
687 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800688 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800689 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800690 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900691 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300692 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800693 write_protected = 1;
694 }
Izik Eidus9647c142007-10-16 14:43:46 +0200695 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800696 }
Izik Eidus855149a2008-03-20 18:17:24 +0200697 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500698 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200699
700 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500701 pfn = spte_to_pfn(*spte);
702 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200703 }
704
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300705 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200706 for (i = PT_DIRECTORY_LEVEL;
707 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
708 rmapp = gfn_to_rmap(kvm, gfn, i);
709 spte = rmap_next(kvm, rmapp, NULL);
710 while (spte) {
711 BUG_ON(!spte);
712 BUG_ON(!(*spte & PT_PRESENT_MASK));
713 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
714 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900715 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200716 rmap_remove(kvm, spte);
717 --kvm->stat.lpages;
718 __set_spte(spte, shadow_trap_nonpresent_pte);
719 spte = NULL;
720 write_protected = 1;
721 }
722 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300723 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300724 }
725
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200726 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800727}
728
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000729static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
730 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200731{
732 u64 *spte;
733 int need_tlb_flush = 0;
734
735 while ((spte = rmap_next(kvm, rmapp, NULL))) {
736 BUG_ON(!(*spte & PT_PRESENT_MASK));
737 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
738 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300739 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200740 need_tlb_flush = 1;
741 }
742 return need_tlb_flush;
743}
744
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000745static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
746 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300747{
748 int need_flush = 0;
749 u64 *spte, new_spte;
750 pte_t *ptep = (pte_t *)data;
751 pfn_t new_pfn;
752
753 WARN_ON(pte_huge(*ptep));
754 new_pfn = pte_pfn(*ptep);
755 spte = rmap_next(kvm, rmapp, NULL);
756 while (spte) {
757 BUG_ON(!is_shadow_present_pte(*spte));
758 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
759 need_flush = 1;
760 if (pte_write(*ptep)) {
761 rmap_remove(kvm, spte);
762 __set_spte(spte, shadow_trap_nonpresent_pte);
763 spte = rmap_next(kvm, rmapp, NULL);
764 } else {
765 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
766 new_spte |= (u64)new_pfn << PAGE_SHIFT;
767
768 new_spte &= ~PT_WRITABLE_MASK;
769 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900770 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300771 kvm_set_pfn_dirty(spte_to_pfn(*spte));
772 __set_spte(spte, new_spte);
773 spte = rmap_next(kvm, rmapp, spte);
774 }
775 }
776 if (need_flush)
777 kvm_flush_remote_tlbs(kvm);
778
779 return 0;
780}
781
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000782static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
783 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300784 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000785 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200786{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200787 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200788 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200789 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200790 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200791
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800792 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200793
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200794 for (i = 0; i < slots->nmemslots; i++) {
795 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200796 unsigned long start = memslot->userspace_addr;
797 unsigned long end;
798
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200799 end = start + (memslot->npages << PAGE_SHIFT);
800 if (hva >= start && hva < end) {
801 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200802
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200803 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200804
805 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
806 int idx = gfn_offset;
807 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200808 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300809 &memslot->lpage_info[j][idx].rmap_pde,
810 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200811 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200812 trace_kvm_age_page(hva, memslot, ret);
813 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200814 }
815 }
816
817 return retval;
818}
819
820int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
821{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300822 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200823}
824
Izik Eidus3da0dd42009-09-23 21:47:18 +0300825void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
826{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000827 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300828}
829
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000830static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
831 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200832{
833 u64 *spte;
834 int young = 0;
835
Rik van Riel6316e1c2010-02-03 16:11:03 -0500836 /*
837 * Emulate the accessed bit for EPT, by checking if this page has
838 * an EPT mapping, and clearing it if it does. On the next access,
839 * a new EPT mapping will be established.
840 * This has some overhead, but not as much as the cost of swapping
841 * out actively used pages or breaking up actively used hugepages.
842 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800843 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500844 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800845
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200846 spte = rmap_next(kvm, rmapp, NULL);
847 while (spte) {
848 int _young;
849 u64 _spte = *spte;
850 BUG_ON(!(_spte & PT_PRESENT_MASK));
851 _young = _spte & PT_ACCESSED_MASK;
852 if (_young) {
853 young = 1;
854 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
855 }
856 spte = rmap_next(kvm, rmapp, spte);
857 }
858 return young;
859}
860
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300861#define RMAP_RECYCLE_THRESHOLD 1000
862
Joerg Roedel852e3c12009-07-27 16:30:44 +0200863static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300864{
865 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200866 struct kvm_mmu_page *sp;
867
868 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300869
870 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200871 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300872
Izik Eidus3da0dd42009-09-23 21:47:18 +0300873 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300874 kvm_flush_remote_tlbs(vcpu->kvm);
875}
876
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200877int kvm_age_hva(struct kvm *kvm, unsigned long hva)
878{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300879 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200880}
881
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800882#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300883static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884{
Avi Kivity139bdb22007-01-05 16:36:50 -0800885 u64 *pos;
886 u64 *end;
887
Avi Kivity47ad8e62007-05-06 15:50:58 +0300888 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300889 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800890 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800891 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800893 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 return 1;
895}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800896#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897
Avi Kivity4db35312007-11-21 15:28:32 +0200898static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800899{
Avi Kivity4db35312007-11-21 15:28:32 +0200900 ASSERT(is_empty_shadow_page(sp->spt));
901 list_del(&sp->link);
902 __free_page(virt_to_page(sp->spt));
903 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800904 kmem_cache_free(mmu_page_header_cache, sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800905 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800906}
907
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800908static unsigned kvm_page_table_hashfn(gfn_t gfn)
909{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200910 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911}
912
Avi Kivity25c0de22007-01-05 16:36:42 -0800913static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
914 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915{
Avi Kivity4db35312007-11-21 15:28:32 +0200916 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800918 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
919 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
920 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200921 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800922 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800923 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200924 sp->multimapped = 0;
925 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800926 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200927 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928}
929
Avi Kivity714b93d2007-01-05 16:36:53 -0800930static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200931 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800932{
933 struct kvm_pte_chain *pte_chain;
934 struct hlist_node *node;
935 int i;
936
937 if (!parent_pte)
938 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200939 if (!sp->multimapped) {
940 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800941
942 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200943 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800944 return;
945 }
Avi Kivity4db35312007-11-21 15:28:32 +0200946 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800947 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200948 INIT_HLIST_HEAD(&sp->parent_ptes);
949 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800950 pte_chain->parent_ptes[0] = old;
951 }
Avi Kivity4db35312007-11-21 15:28:32 +0200952 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800953 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
954 continue;
955 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
956 if (!pte_chain->parent_ptes[i]) {
957 pte_chain->parent_ptes[i] = parent_pte;
958 return;
959 }
960 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800961 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800962 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200963 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 pte_chain->parent_ptes[0] = parent_pte;
965}
966
Avi Kivity4db35312007-11-21 15:28:32 +0200967static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800968 u64 *parent_pte)
969{
970 struct kvm_pte_chain *pte_chain;
971 struct hlist_node *node;
972 int i;
973
Avi Kivity4db35312007-11-21 15:28:32 +0200974 if (!sp->multimapped) {
975 BUG_ON(sp->parent_pte != parent_pte);
976 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800977 return;
978 }
Avi Kivity4db35312007-11-21 15:28:32 +0200979 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800980 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
981 if (!pte_chain->parent_ptes[i])
982 break;
983 if (pte_chain->parent_ptes[i] != parent_pte)
984 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800985 while (i + 1 < NR_PTE_CHAIN_ENTRIES
986 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800987 pte_chain->parent_ptes[i]
988 = pte_chain->parent_ptes[i + 1];
989 ++i;
990 }
991 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800992 if (i == 0) {
993 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300994 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200995 if (hlist_empty(&sp->parent_ptes)) {
996 sp->multimapped = 0;
997 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800998 }
999 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001000 return;
1001 }
1002 BUG();
1003}
1004
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001005
Xiao Guangrong6b184932010-04-16 21:29:17 +08001006static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001007{
1008 struct kvm_pte_chain *pte_chain;
1009 struct hlist_node *node;
1010 struct kvm_mmu_page *parent_sp;
1011 int i;
1012
1013 if (!sp->multimapped && sp->parent_pte) {
1014 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001015 fn(parent_sp);
1016 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001017 return;
1018 }
1019 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1020 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1021 if (!pte_chain->parent_ptes[i])
1022 break;
1023 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001024 fn(parent_sp);
1025 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001026 }
1027}
1028
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001029static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1030{
1031 unsigned int index;
1032 struct kvm_mmu_page *sp = page_header(__pa(spte));
1033
1034 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001035 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1036 sp->unsync_children++;
1037 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001038}
1039
1040static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1041{
1042 struct kvm_pte_chain *pte_chain;
1043 struct hlist_node *node;
1044 int i;
1045
1046 if (!sp->parent_pte)
1047 return;
1048
1049 if (!sp->multimapped) {
1050 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1051 return;
1052 }
1053
1054 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1055 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1056 if (!pte_chain->parent_ptes[i])
1057 break;
1058 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1059 }
1060}
1061
Xiao Guangrong6b184932010-04-16 21:29:17 +08001062static int unsync_walk_fn(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001063{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001064 kvm_mmu_update_parents_unsync(sp);
1065 return 1;
1066}
1067
Xiao Guangrong6b184932010-04-16 21:29:17 +08001068static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001069{
Xiao Guangrong6b184932010-04-16 21:29:17 +08001070 mmu_parent_walk(sp, unsync_walk_fn);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001071 kvm_mmu_update_parents_unsync(sp);
1072}
1073
Avi Kivityd761a502008-05-29 14:55:03 +03001074static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1075 struct kvm_mmu_page *sp)
1076{
1077 int i;
1078
1079 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1080 sp->spt[i] = shadow_trap_nonpresent_pte;
1081}
1082
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001083static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1084 struct kvm_mmu_page *sp)
1085{
1086 return 1;
1087}
1088
Marcelo Tosattia7052892008-09-23 13:18:35 -03001089static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1090{
1091}
1092
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001093#define KVM_PAGE_ARRAY_NR 16
1094
1095struct kvm_mmu_pages {
1096 struct mmu_page_and_offset {
1097 struct kvm_mmu_page *sp;
1098 unsigned int idx;
1099 } page[KVM_PAGE_ARRAY_NR];
1100 unsigned int nr;
1101};
1102
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001103#define for_each_unsync_children(bitmap, idx) \
1104 for (idx = find_first_bit(bitmap, 512); \
1105 idx < 512; \
1106 idx = find_next_bit(bitmap, 512, idx+1))
1107
Hannes Edercded19f2009-02-21 02:19:13 +01001108static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1109 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001110{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001112
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001113 if (sp->unsync)
1114 for (i=0; i < pvec->nr; i++)
1115 if (pvec->page[i].sp == sp)
1116 return 0;
1117
1118 pvec->page[pvec->nr].sp = sp;
1119 pvec->page[pvec->nr].idx = idx;
1120 pvec->nr++;
1121 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1122}
1123
1124static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1125 struct kvm_mmu_pages *pvec)
1126{
1127 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001129 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001130 u64 ent = sp->spt[i];
1131
Marcelo Tosatti87917232008-12-22 18:49:30 -02001132 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001133 struct kvm_mmu_page *child;
1134 child = page_header(ent & PT64_BASE_ADDR_MASK);
1135
1136 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001137 if (mmu_pages_add(pvec, child, i))
1138 return -ENOSPC;
1139
1140 ret = __mmu_unsync_walk(child, pvec);
1141 if (!ret)
1142 __clear_bit(i, sp->unsync_child_bitmap);
1143 else if (ret > 0)
1144 nr_unsync_leaf += ret;
1145 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001146 return ret;
1147 }
1148
1149 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001150 nr_unsync_leaf++;
1151 if (mmu_pages_add(pvec, child, i))
1152 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001153 }
1154 }
1155 }
1156
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001157 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001158 sp->unsync_children = 0;
1159
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001160 return nr_unsync_leaf;
1161}
1162
1163static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1164 struct kvm_mmu_pages *pvec)
1165{
1166 if (!sp->unsync_children)
1167 return 0;
1168
1169 mmu_pages_add(pvec, sp, 0);
1170 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001171}
1172
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001173static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1174{
1175 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001176 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001177 sp->unsync = 0;
1178 --kvm->stat.mmu_unsync;
1179}
1180
1181static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1182
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001183static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1184 bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001185{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001186 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001187 kvm_mmu_zap_page(vcpu->kvm, sp);
1188 return 1;
1189 }
1190
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001191 if (clear_unsync) {
1192 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1193 kvm_flush_remote_tlbs(vcpu->kvm);
1194 kvm_unlink_unsync_page(vcpu->kvm, sp);
1195 }
1196
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001197 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1198 kvm_mmu_zap_page(vcpu->kvm, sp);
1199 return 1;
1200 }
1201
1202 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001203 return 0;
1204}
1205
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001206static void mmu_convert_notrap(struct kvm_mmu_page *sp);
1207static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1208 struct kvm_mmu_page *sp)
1209{
1210 int ret;
1211
1212 ret = __kvm_sync_page(vcpu, sp, false);
1213 if (!ret)
1214 mmu_convert_notrap(sp);
1215 return ret;
1216}
1217
1218static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1219{
1220 return __kvm_sync_page(vcpu, sp, true);
1221}
1222
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001223struct mmu_page_path {
1224 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1225 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001226};
1227
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001228#define for_each_sp(pvec, sp, parents, i) \
1229 for (i = mmu_pages_next(&pvec, &parents, -1), \
1230 sp = pvec.page[i].sp; \
1231 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1232 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233
Hannes Edercded19f2009-02-21 02:19:13 +01001234static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1235 struct mmu_page_path *parents,
1236 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001237{
1238 int n;
1239
1240 for (n = i+1; n < pvec->nr; n++) {
1241 struct kvm_mmu_page *sp = pvec->page[n].sp;
1242
1243 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1244 parents->idx[0] = pvec->page[n].idx;
1245 return n;
1246 }
1247
1248 parents->parent[sp->role.level-2] = sp;
1249 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1250 }
1251
1252 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001253}
1254
Hannes Edercded19f2009-02-21 02:19:13 +01001255static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001257 struct kvm_mmu_page *sp;
1258 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001259
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001260 do {
1261 unsigned int idx = parents->idx[level];
1262
1263 sp = parents->parent[level];
1264 if (!sp)
1265 return;
1266
1267 --sp->unsync_children;
1268 WARN_ON((int)sp->unsync_children < 0);
1269 __clear_bit(idx, sp->unsync_child_bitmap);
1270 level++;
1271 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1272}
1273
1274static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1275 struct mmu_page_path *parents,
1276 struct kvm_mmu_pages *pvec)
1277{
1278 parents->parent[parent->role.level-1] = NULL;
1279 pvec->nr = 0;
1280}
1281
1282static void mmu_sync_children(struct kvm_vcpu *vcpu,
1283 struct kvm_mmu_page *parent)
1284{
1285 int i;
1286 struct kvm_mmu_page *sp;
1287 struct mmu_page_path parents;
1288 struct kvm_mmu_pages pages;
1289
1290 kvm_mmu_pages_init(parent, &parents, &pages);
1291 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001292 int protected = 0;
1293
1294 for_each_sp(pages, sp, parents, i)
1295 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1296
1297 if (protected)
1298 kvm_flush_remote_tlbs(vcpu->kvm);
1299
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001300 for_each_sp(pages, sp, parents, i) {
1301 kvm_sync_page(vcpu, sp);
1302 mmu_pages_clear_parents(&parents);
1303 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001304 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001305 kvm_mmu_pages_init(parent, &parents, &pages);
1306 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001307}
1308
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001309static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1310 gfn_t gfn,
1311 gva_t gaddr,
1312 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001313 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001314 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001315 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001316{
1317 union kvm_mmu_page_role role;
1318 unsigned index;
1319 unsigned quadrant;
1320 struct hlist_head *bucket;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001321 struct kvm_mmu_page *sp, *unsync_sp = NULL;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001322 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001323
Avi Kivitya770f6f2008-12-21 19:20:09 +02001324 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001325 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001326 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001327 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001328 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001329 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001330 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001331 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1332 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1333 role.quadrant = quadrant;
1334 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001335 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001336 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001337 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1338 if (sp->gfn == gfn) {
1339 if (sp->unsync)
Xiao Guangronge02aa902010-05-15 18:52:34 +08001340 unsync_sp = sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001341
1342 if (sp->role.word != role.word)
1343 continue;
1344
Xiao Guangronge02aa902010-05-15 18:52:34 +08001345 if (!direct && unsync_sp &&
1346 kvm_sync_page_transient(vcpu, unsync_sp)) {
1347 unsync_sp = NULL;
1348 break;
1349 }
1350
Avi Kivity4db35312007-11-21 15:28:32 +02001351 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001352 if (sp->unsync_children) {
1353 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001354 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001355 } else if (sp->unsync)
1356 kvm_mmu_mark_parents_unsync(sp);
1357
Avi Kivityf691fe12009-07-06 15:58:14 +03001358 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001359 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001360 }
Xiao Guangronge02aa902010-05-15 18:52:34 +08001361 if (!direct && unsync_sp)
1362 kvm_sync_page(vcpu, unsync_sp);
1363
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001364 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001365 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1366 if (!sp)
1367 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001368 sp->gfn = gfn;
1369 sp->role = role;
1370 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001371 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001372 if (rmap_write_protect(vcpu->kvm, gfn))
1373 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001374 account_shadowed(vcpu->kvm, gfn);
1375 }
Avi Kivity131d8272008-05-29 14:56:28 +03001376 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1377 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1378 else
1379 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001380 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001381 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001382}
1383
Avi Kivity2d111232008-12-25 14:39:47 +02001384static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1385 struct kvm_vcpu *vcpu, u64 addr)
1386{
1387 iterator->addr = addr;
1388 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1389 iterator->level = vcpu->arch.mmu.shadow_root_level;
1390 if (iterator->level == PT32E_ROOT_LEVEL) {
1391 iterator->shadow_addr
1392 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1393 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1394 --iterator->level;
1395 if (!iterator->shadow_addr)
1396 iterator->level = 0;
1397 }
1398}
1399
1400static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1401{
1402 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1403 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001404
1405 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1406 if (is_large_pte(*iterator->sptep))
1407 return false;
1408
Avi Kivity2d111232008-12-25 14:39:47 +02001409 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1410 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1411 return true;
1412}
1413
1414static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1415{
1416 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1417 --iterator->level;
1418}
1419
Avi Kivity90cb0522007-07-17 13:04:56 +03001420static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001421 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001422{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001423 unsigned i;
1424 u64 *pt;
1425 u64 ent;
1426
Avi Kivity4db35312007-11-21 15:28:32 +02001427 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001428
Avi Kivity697fe2e2007-01-05 16:36:46 -08001429 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1430 ent = pt[i];
1431
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001432 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001433 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001434 ent &= PT64_BASE_ADDR_MASK;
1435 mmu_page_remove_parent_pte(page_header(ent),
1436 &pt[i]);
1437 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001438 if (is_large_pte(ent))
1439 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001440 rmap_remove(kvm, &pt[i]);
1441 }
1442 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001443 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001444 }
Avi Kivitya4360362007-01-05 16:36:45 -08001445}
1446
Avi Kivity4db35312007-11-21 15:28:32 +02001447static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001448{
Avi Kivity4db35312007-11-21 15:28:32 +02001449 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001450}
1451
Avi Kivity12b7d282007-09-23 14:10:49 +02001452static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1453{
1454 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001455 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001456
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001457 kvm_for_each_vcpu(i, vcpu, kvm)
1458 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001459}
1460
Avi Kivity31aa2b42008-07-11 17:59:46 +03001461static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001462{
1463 u64 *parent_pte;
1464
Avi Kivity4db35312007-11-21 15:28:32 +02001465 while (sp->multimapped || sp->parent_pte) {
1466 if (!sp->multimapped)
1467 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001468 else {
1469 struct kvm_pte_chain *chain;
1470
Avi Kivity4db35312007-11-21 15:28:32 +02001471 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001472 struct kvm_pte_chain, link);
1473 parent_pte = chain->parent_ptes[0];
1474 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001475 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001476 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001477 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001478 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001479}
1480
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001481static int mmu_zap_unsync_children(struct kvm *kvm,
1482 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001483{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001484 int i, zapped = 0;
1485 struct mmu_page_path parents;
1486 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001487
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001488 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001489 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001490
1491 kvm_mmu_pages_init(parent, &parents, &pages);
1492 while (mmu_unsync_walk(parent, &pages)) {
1493 struct kvm_mmu_page *sp;
1494
1495 for_each_sp(pages, sp, parents, i) {
1496 kvm_mmu_zap_page(kvm, sp);
1497 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001498 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001499 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001500 kvm_mmu_pages_init(parent, &parents, &pages);
1501 }
1502
1503 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001504}
1505
Marcelo Tosatti07385412008-09-23 13:18:37 -03001506static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001507{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001508 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001509
1510 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001511 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001512 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001513 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001514 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001515 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001516 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001517 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001518 if (sp->unsync)
1519 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001520 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001521 /* Count self */
1522 ret++;
Avi Kivity4db35312007-11-21 15:28:32 +02001523 hlist_del(&sp->hash_link);
1524 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001525 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001526 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001527 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001528 kvm_reload_remote_mmus(kvm);
1529 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001530 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001531 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001532}
1533
Izik Eidus82ce2c92007-10-02 18:52:55 +02001534/*
1535 * Changing the number of mmu pages allocated to the vm
1536 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1537 */
1538void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1539{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001540 int used_pages;
1541
1542 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1543 used_pages = max(0, used_pages);
1544
Izik Eidus82ce2c92007-10-02 18:52:55 +02001545 /*
1546 * If we set the number of mmu pages to be smaller be than the
1547 * number of actived pages , we must to free some mmu pages before we
1548 * change the value
1549 */
1550
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001551 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001552 while (used_pages > kvm_nr_mmu_pages &&
1553 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001554 struct kvm_mmu_page *page;
1555
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001556 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001557 struct kvm_mmu_page, link);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001558 used_pages -= kvm_mmu_zap_page(kvm, page);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001559 }
Xiao Guangrong77662e02010-04-16 16:34:42 +08001560 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001561 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001562 }
1563 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001564 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1565 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001566
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001567 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001568}
1569
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001570static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001571{
1572 unsigned index;
1573 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001574 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001575 struct hlist_node *node, *n;
1576 int r;
1577
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001578 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001579 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001580 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001581 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001582restart:
Avi Kivity4db35312007-11-21 15:28:32 +02001583 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001584 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001585 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001586 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001587 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001588 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001589 goto restart;
Avi Kivitya4360362007-01-05 16:36:45 -08001590 }
1591 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001592}
1593
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001594static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001595{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001596 unsigned index;
1597 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001598 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001599 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001600
Avi Kivity4677a3b2009-01-06 13:00:27 +02001601 index = kvm_page_table_hashfn(gfn);
1602 bucket = &kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08001603restart:
Avi Kivity4677a3b2009-01-06 13:00:27 +02001604 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001605 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001606 && !sp->role.invalid) {
1607 pgprintk("%s: zap %lx %x\n",
1608 __func__, gfn, sp->role.word);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001609 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08001610 goto restart;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001611 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001612 }
1613}
1614
Avi Kivity38c335f2007-11-21 14:20:22 +02001615static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001617 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001618 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619
Sheng Yang291f26b2008-10-16 17:30:57 +08001620 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621}
1622
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001623static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1624{
1625 int i;
1626 u64 *pt = sp->spt;
1627
1628 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1629 return;
1630
1631 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1632 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001633 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001634 }
1635}
1636
Sheng Yang74be52e2008-10-09 16:01:56 +08001637/*
1638 * The function is based on mtrr_type_lookup() in
1639 * arch/x86/kernel/cpu/mtrr/generic.c
1640 */
1641static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1642 u64 start, u64 end)
1643{
1644 int i;
1645 u64 base, mask;
1646 u8 prev_match, curr_match;
1647 int num_var_ranges = KVM_NR_VAR_MTRR;
1648
1649 if (!mtrr_state->enabled)
1650 return 0xFF;
1651
1652 /* Make end inclusive end, instead of exclusive */
1653 end--;
1654
1655 /* Look in fixed ranges. Just return the type as per start */
1656 if (mtrr_state->have_fixed && (start < 0x100000)) {
1657 int idx;
1658
1659 if (start < 0x80000) {
1660 idx = 0;
1661 idx += (start >> 16);
1662 return mtrr_state->fixed_ranges[idx];
1663 } else if (start < 0xC0000) {
1664 idx = 1 * 8;
1665 idx += ((start - 0x80000) >> 14);
1666 return mtrr_state->fixed_ranges[idx];
1667 } else if (start < 0x1000000) {
1668 idx = 3 * 8;
1669 idx += ((start - 0xC0000) >> 12);
1670 return mtrr_state->fixed_ranges[idx];
1671 }
1672 }
1673
1674 /*
1675 * Look in variable ranges
1676 * Look of multiple ranges matching this address and pick type
1677 * as per MTRR precedence
1678 */
1679 if (!(mtrr_state->enabled & 2))
1680 return mtrr_state->def_type;
1681
1682 prev_match = 0xFF;
1683 for (i = 0; i < num_var_ranges; ++i) {
1684 unsigned short start_state, end_state;
1685
1686 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1687 continue;
1688
1689 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1690 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1691 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1692 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1693
1694 start_state = ((start & mask) == (base & mask));
1695 end_state = ((end & mask) == (base & mask));
1696 if (start_state != end_state)
1697 return 0xFE;
1698
1699 if ((start & mask) != (base & mask))
1700 continue;
1701
1702 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1703 if (prev_match == 0xFF) {
1704 prev_match = curr_match;
1705 continue;
1706 }
1707
1708 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1709 curr_match == MTRR_TYPE_UNCACHABLE)
1710 return MTRR_TYPE_UNCACHABLE;
1711
1712 if ((prev_match == MTRR_TYPE_WRBACK &&
1713 curr_match == MTRR_TYPE_WRTHROUGH) ||
1714 (prev_match == MTRR_TYPE_WRTHROUGH &&
1715 curr_match == MTRR_TYPE_WRBACK)) {
1716 prev_match = MTRR_TYPE_WRTHROUGH;
1717 curr_match = MTRR_TYPE_WRTHROUGH;
1718 }
1719
1720 if (prev_match != curr_match)
1721 return MTRR_TYPE_UNCACHABLE;
1722 }
1723
1724 if (prev_match != 0xFF)
1725 return prev_match;
1726
1727 return mtrr_state->def_type;
1728}
1729
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001730u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001731{
1732 u8 mtrr;
1733
1734 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1735 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1736 if (mtrr == 0xfe || mtrr == 0xff)
1737 mtrr = MTRR_TYPE_WRBACK;
1738 return mtrr;
1739}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001740EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001741
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001742static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001743{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001744 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001745 ++vcpu->kvm->stat.mmu_unsync;
1746 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001747
Xiao Guangrong6b184932010-04-16 21:29:17 +08001748 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001749 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001750}
1751
1752static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1753{
1754 struct hlist_head *bucket;
1755 struct kvm_mmu_page *s;
1756 struct hlist_node *node, *n;
1757 unsigned index;
1758
1759 index = kvm_page_table_hashfn(gfn);
1760 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1761
1762 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1763 if (s->gfn != gfn || s->role.direct || s->unsync ||
1764 s->role.invalid)
1765 continue;
1766 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1767 __kvm_unsync_page(vcpu, s);
1768 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001769}
1770
1771static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1772 bool can_unsync)
1773{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001774 unsigned index;
1775 struct hlist_head *bucket;
1776 struct kvm_mmu_page *s;
1777 struct hlist_node *node, *n;
1778 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001779
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001780 index = kvm_page_table_hashfn(gfn);
1781 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1782 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1783 if (s->gfn != gfn || s->role.direct || s->role.invalid)
1784 continue;
1785
1786 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001787 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001788
1789 if (!need_unsync && !s->unsync) {
1790 if (!can_unsync || !oos_shadow)
1791 return 1;
1792 need_unsync = true;
1793 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001794 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001795 if (need_unsync)
1796 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001797 return 0;
1798}
1799
Avi Kivityd555c332009-06-10 14:24:23 +03001800static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001801 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001802 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001803 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001804 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001805{
1806 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001807 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001808
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001809 /*
1810 * We don't set the accessed bit, since we sometimes want to see
1811 * whether the guest actually used the pte (in order to detect
1812 * demand paging).
1813 */
Sheng Yang7b523452008-04-25 21:13:50 +08001814 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001815 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001816 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817 if (!dirty)
1818 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001819 if (pte_access & ACC_EXEC_MASK)
1820 spte |= shadow_x_mask;
1821 else
1822 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001823 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001824 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001825 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001826 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001827 if (tdp_enabled)
1828 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1829 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001830
Izik Eidus14032832009-09-23 21:47:17 +03001831 if (reset_host_protection)
1832 spte |= SPTE_HOST_WRITEABLE;
1833
Anthony Liguori35149e22008-04-02 14:46:56 -05001834 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001835
1836 if ((pte_access & ACC_WRITE_MASK)
1837 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001838
Joerg Roedel852e3c12009-07-27 16:30:44 +02001839 if (level > PT_PAGE_TABLE_LEVEL &&
1840 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001841 ret = 1;
Xiao Guangrong6d742292010-05-13 10:07:00 +08001842 rmap_remove(vcpu->kvm, sptep);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001843 spte = shadow_trap_nonpresent_pte;
1844 goto set_pte;
1845 }
1846
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001847 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001848
Avi Kivity69325a12010-05-27 14:35:58 +03001849 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1850 spte &= ~PT_USER_MASK;
1851
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001852 /*
1853 * Optimization: for pte sync, if spte was writable the hash
1854 * lookup is unnecessary (and expensive). Write protection
1855 * is responsibility of mmu_get_page / kvm_sync_page.
1856 * Same reasoning can be applied to dirty page accounting.
1857 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001858 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001859 goto set_pte;
1860
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001861 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001862 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001863 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001864 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001865 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001866 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001867 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001868 }
1869 }
1870
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001871 if (pte_access & ACC_WRITE_MASK)
1872 mark_page_dirty(vcpu->kvm, gfn);
1873
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001874set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001875 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001876 return ret;
1877}
1878
Avi Kivityd555c332009-06-10 14:24:23 +03001879static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001880 unsigned pt_access, unsigned pte_access,
1881 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001882 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001883 pfn_t pfn, bool speculative,
1884 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001885{
1886 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001887 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001888 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001889
1890 pgprintk("%s: spte %llx access %x write_fault %d"
1891 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001892 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001893 write_fault, user_fault, gfn);
1894
Avi Kivityd555c332009-06-10 14:24:23 +03001895 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001896 /*
1897 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1898 * the parent of the now unreachable PTE.
1899 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001900 if (level > PT_PAGE_TABLE_LEVEL &&
1901 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001902 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001903 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001904
1905 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001906 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001907 __set_spte(sptep, shadow_trap_nonpresent_pte);
1908 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001909 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001910 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001911 spte_to_pfn(*sptep), pfn);
1912 rmap_remove(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08001913 __set_spte(sptep, shadow_trap_nonpresent_pte);
1914 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001915 } else
1916 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001917 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001918
Avi Kivityd555c332009-06-10 14:24:23 +03001919 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001920 dirty, level, gfn, pfn, speculative, true,
1921 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001922 if (write_fault)
1923 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001924 kvm_x86_ops->tlb_flush(vcpu);
1925 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001926
Avi Kivityd555c332009-06-10 14:24:23 +03001927 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001928 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001929 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001930 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1931 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001932 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001933 ++vcpu->kvm->stat.lpages;
1934
Avi Kivityd555c332009-06-10 14:24:23 +03001935 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001936 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001937 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001938 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001939 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001940 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001941 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001942 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001943 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001944 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001945 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001946 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001947 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001948 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001949 vcpu->arch.last_pte_gfn = gfn;
1950 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001951}
1952
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1954{
1955}
1956
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001957static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001958 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959{
Avi Kivity9f652d212008-12-25 14:54:25 +02001960 struct kvm_shadow_walk_iterator iterator;
1961 struct kvm_mmu_page *sp;
1962 int pt_write = 0;
1963 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964
Avi Kivity9f652d212008-12-25 14:54:25 +02001965 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001966 if (iterator.level == level) {
Avi Kivity9f652d212008-12-25 14:54:25 +02001967 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1968 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001969 level, gfn, pfn, false, true);
Avi Kivity9f652d212008-12-25 14:54:25 +02001970 ++vcpu->stat.pf_fixed;
1971 break;
1972 }
1973
1974 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1975 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1976 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1977 iterator.level - 1,
1978 1, ACC_ALL, iterator.sptep);
1979 if (!sp) {
1980 pgprintk("nonpaging_map: ENOMEM\n");
1981 kvm_release_pfn_clean(pfn);
1982 return -ENOMEM;
1983 }
1984
Avi Kivityd555c332009-06-10 14:24:23 +03001985 __set_spte(iterator.sptep,
1986 __pa(sp->spt)
1987 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1988 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02001989 }
1990 }
1991 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992}
1993
Huang Yingbf998152010-05-31 14:28:19 +08001994static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
1995{
1996 char buf[1];
1997 void __user *hva;
1998 int r;
1999
2000 /* Touch the page, so send SIGBUS */
2001 hva = (void __user *)gfn_to_hva(kvm, gfn);
2002 r = copy_from_user(buf, hva, 1);
2003}
2004
2005static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2006{
2007 kvm_release_pfn_clean(pfn);
2008 if (is_hwpoison_pfn(pfn)) {
2009 kvm_send_hwpoison_signal(kvm, gfn);
2010 return 0;
2011 }
2012 return 1;
2013}
2014
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002015static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2016{
2017 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002018 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002019 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002020 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002021
Joerg Roedel852e3c12009-07-27 16:30:44 +02002022 level = mapping_level(vcpu, gfn);
2023
2024 /*
2025 * This path builds a PAE pagetable - so we can map 2mb pages at
2026 * maximum. Therefore check if the level is larger than that.
2027 */
2028 if (level > PT_DIRECTORY_LEVEL)
2029 level = PT_DIRECTORY_LEVEL;
2030
2031 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002032
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002033 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002034 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002035 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002036
Avi Kivityd196e342008-01-24 11:44:11 +02002037 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002038 if (is_error_pfn(pfn))
2039 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002040
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002041 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002042 if (mmu_notifier_retry(vcpu, mmu_seq))
2043 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002044 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002045 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002046 spin_unlock(&vcpu->kvm->mmu_lock);
2047
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002048
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002049 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002050
2051out_unlock:
2052 spin_unlock(&vcpu->kvm->mmu_lock);
2053 kvm_release_pfn_clean(pfn);
2054 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002055}
2056
2057
Avi Kivity17ac10a2007-01-05 16:36:40 -08002058static void mmu_free_roots(struct kvm_vcpu *vcpu)
2059{
2060 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002061 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002062
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002063 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002064 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002065 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002066 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2067 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002068
Avi Kivity4db35312007-11-21 15:28:32 +02002069 sp = page_header(root);
2070 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002071 if (!sp->root_count && sp->role.invalid)
2072 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002073 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002074 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002075 return;
2076 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002077 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002078 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002079
Avi Kivity417726a2007-04-12 17:35:58 +03002080 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002081 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002082 sp = page_header(root);
2083 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002084 if (!sp->root_count && sp->role.invalid)
2085 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002086 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002087 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002088 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002089 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002090 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002091}
2092
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002093static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2094{
2095 int ret = 0;
2096
2097 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2098 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2099 ret = 1;
2100 }
2101
2102 return ret;
2103}
2104
2105static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002106{
2107 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002108 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002109 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002110 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002111 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002112
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002114
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002115 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2116 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002117
2118 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002119 if (mmu_check_root(vcpu, root_gfn))
2120 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002121 if (tdp_enabled) {
2122 direct = 1;
2123 root_gfn = 0;
2124 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002125 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002126 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002127 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002128 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002129 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002130 root = __pa(sp->spt);
2131 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002132 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002133 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002134 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002135 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002136 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002137 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002138 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002139
2140 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002141 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002142 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002143 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002144 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002145 continue;
2146 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002147 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002149 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002150 if (mmu_check_root(vcpu, root_gfn))
2151 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002152 if (tdp_enabled) {
2153 direct = 1;
2154 root_gfn = i << 30;
2155 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002156 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002157 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002158 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002159 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002160 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002161 root = __pa(sp->spt);
2162 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002163 spin_unlock(&vcpu->kvm->mmu_lock);
2164
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002165 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002166 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002167 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002168 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002169}
2170
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002171static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2172{
2173 int i;
2174 struct kvm_mmu_page *sp;
2175
2176 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2177 return;
2178 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2179 hpa_t root = vcpu->arch.mmu.root_hpa;
2180 sp = page_header(root);
2181 mmu_sync_children(vcpu, sp);
2182 return;
2183 }
2184 for (i = 0; i < 4; ++i) {
2185 hpa_t root = vcpu->arch.mmu.pae_root[i];
2186
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002187 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002188 root &= PT64_BASE_ADDR_MASK;
2189 sp = page_header(root);
2190 mmu_sync_children(vcpu, sp);
2191 }
2192 }
2193}
2194
2195void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2196{
2197 spin_lock(&vcpu->kvm->mmu_lock);
2198 mmu_sync_roots(vcpu);
2199 spin_unlock(&vcpu->kvm->mmu_lock);
2200}
2201
Gleb Natapov1871c602010-02-10 14:21:32 +02002202static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2203 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204{
Gleb Natapov1871c602010-02-10 14:21:32 +02002205 if (error)
2206 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 return vaddr;
2208}
2209
2210static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002211 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212{
Avi Kivitye8332402007-12-09 18:43:00 +02002213 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002214 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002216 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002217 r = mmu_topup_memory_caches(vcpu);
2218 if (r)
2219 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002220
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002222 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223
Avi Kivitye8332402007-12-09 18:43:00 +02002224 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225
Avi Kivitye8332402007-12-09 18:43:00 +02002226 return nonpaging_map(vcpu, gva & PAGE_MASK,
2227 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228}
2229
Joerg Roedelfb72d162008-02-07 13:47:44 +01002230static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2231 u32 error_code)
2232{
Anthony Liguori35149e22008-04-02 14:46:56 -05002233 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002234 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002235 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002236 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002237 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002238
2239 ASSERT(vcpu);
2240 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2241
2242 r = mmu_topup_memory_caches(vcpu);
2243 if (r)
2244 return r;
2245
Joerg Roedel852e3c12009-07-27 16:30:44 +02002246 level = mapping_level(vcpu, gfn);
2247
2248 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2249
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002250 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002251 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002252 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002253 if (is_error_pfn(pfn))
2254 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002255 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002256 if (mmu_notifier_retry(vcpu, mmu_seq))
2257 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002258 kvm_mmu_free_some_pages(vcpu);
2259 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002260 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002261 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002262
2263 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002264
2265out_unlock:
2266 spin_unlock(&vcpu->kvm->mmu_lock);
2267 kvm_release_pfn_clean(pfn);
2268 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002269}
2270
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271static void nonpaging_free(struct kvm_vcpu *vcpu)
2272{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002273 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274}
2275
2276static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2277{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002278 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
2280 context->new_cr3 = nonpaging_new_cr3;
2281 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 context->gva_to_gpa = nonpaging_gva_to_gpa;
2283 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002284 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002285 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002286 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002287 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002289 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 return 0;
2291}
2292
Avi Kivityd835dfe2007-11-21 02:57:59 +02002293void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002295 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002296 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297}
2298
2299static void paging_new_cr3(struct kvm_vcpu *vcpu)
2300{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002301 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002302 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303}
2304
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305static void inject_page_fault(struct kvm_vcpu *vcpu,
2306 u64 addr,
2307 u32 err_code)
2308{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002309 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310}
2311
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312static void paging_free(struct kvm_vcpu *vcpu)
2313{
2314 nonpaging_free(vcpu);
2315}
2316
Dong, Eddie82725b22009-03-30 16:21:08 +08002317static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2318{
2319 int bit7;
2320
2321 bit7 = (gpte >> 7) & 1;
2322 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2323}
2324
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325#define PTTYPE 64
2326#include "paging_tmpl.h"
2327#undef PTTYPE
2328
2329#define PTTYPE 32
2330#include "paging_tmpl.h"
2331#undef PTTYPE
2332
Dong, Eddie82725b22009-03-30 16:21:08 +08002333static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2334{
2335 struct kvm_mmu *context = &vcpu->arch.mmu;
2336 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2337 u64 exb_bit_rsvd = 0;
2338
2339 if (!is_nx(vcpu))
2340 exb_bit_rsvd = rsvd_bits(63, 63);
2341 switch (level) {
2342 case PT32_ROOT_LEVEL:
2343 /* no rsvd bits for 2 level 4K page table entries */
2344 context->rsvd_bits_mask[0][1] = 0;
2345 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002346 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2347
2348 if (!is_pse(vcpu)) {
2349 context->rsvd_bits_mask[1][1] = 0;
2350 break;
2351 }
2352
Dong, Eddie82725b22009-03-30 16:21:08 +08002353 if (is_cpuid_PSE36())
2354 /* 36bits PSE 4MB page */
2355 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2356 else
2357 /* 32 bits PSE 4MB page */
2358 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002359 break;
2360 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002361 context->rsvd_bits_mask[0][2] =
2362 rsvd_bits(maxphyaddr, 63) |
2363 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002364 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002365 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002366 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2367 rsvd_bits(maxphyaddr, 62); /* PTE */
2368 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2369 rsvd_bits(maxphyaddr, 62) |
2370 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002371 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002372 break;
2373 case PT64_ROOT_LEVEL:
2374 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2375 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2376 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2377 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2378 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002379 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002380 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2381 rsvd_bits(maxphyaddr, 51);
2382 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002383 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2384 rsvd_bits(maxphyaddr, 51) |
2385 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002386 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002387 rsvd_bits(maxphyaddr, 51) |
2388 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002389 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002390 break;
2391 }
2392}
2393
Avi Kivity17ac10a2007-01-05 16:36:40 -08002394static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002396 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397
2398 ASSERT(is_pae(vcpu));
2399 context->new_cr3 = paging_new_cr3;
2400 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002402 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002403 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002404 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002406 context->root_level = level;
2407 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002408 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 return 0;
2410}
2411
Avi Kivity17ac10a2007-01-05 16:36:40 -08002412static int paging64_init_context(struct kvm_vcpu *vcpu)
2413{
Dong, Eddie82725b22009-03-30 16:21:08 +08002414 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002415 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2416}
2417
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418static int paging32_init_context(struct kvm_vcpu *vcpu)
2419{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002420 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421
Dong, Eddie82725b22009-03-30 16:21:08 +08002422 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 context->new_cr3 = paging_new_cr3;
2424 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 context->gva_to_gpa = paging32_gva_to_gpa;
2426 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002427 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002428 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002429 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430 context->root_level = PT32_ROOT_LEVEL;
2431 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002432 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 return 0;
2434}
2435
2436static int paging32E_init_context(struct kvm_vcpu *vcpu)
2437{
Dong, Eddie82725b22009-03-30 16:21:08 +08002438 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002439 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440}
2441
Joerg Roedelfb72d162008-02-07 13:47:44 +01002442static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2443{
2444 struct kvm_mmu *context = &vcpu->arch.mmu;
2445
2446 context->new_cr3 = nonpaging_new_cr3;
2447 context->page_fault = tdp_page_fault;
2448 context->free = nonpaging_free;
2449 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002450 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002451 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002452 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002453 context->root_hpa = INVALID_PAGE;
2454
2455 if (!is_paging(vcpu)) {
2456 context->gva_to_gpa = nonpaging_gva_to_gpa;
2457 context->root_level = 0;
2458 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002459 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002460 context->gva_to_gpa = paging64_gva_to_gpa;
2461 context->root_level = PT64_ROOT_LEVEL;
2462 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002463 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002464 context->gva_to_gpa = paging64_gva_to_gpa;
2465 context->root_level = PT32E_ROOT_LEVEL;
2466 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002467 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002468 context->gva_to_gpa = paging32_gva_to_gpa;
2469 context->root_level = PT32_ROOT_LEVEL;
2470 }
2471
2472 return 0;
2473}
2474
2475static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002477 int r;
2478
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002480 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481
2482 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002483 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002484 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002485 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002487 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002489 r = paging32_init_context(vcpu);
2490
Avi Kivity5b7e0102010-04-14 19:20:03 +03002491 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002492 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002493
2494 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495}
2496
Joerg Roedelfb72d162008-02-07 13:47:44 +01002497static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2498{
Anthony Liguori35149e22008-04-02 14:46:56 -05002499 vcpu->arch.update_pte.pfn = bad_pfn;
2500
Joerg Roedelfb72d162008-02-07 13:47:44 +01002501 if (tdp_enabled)
2502 return init_kvm_tdp_mmu(vcpu);
2503 else
2504 return init_kvm_softmmu(vcpu);
2505}
2506
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2508{
2509 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002510 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2511 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002512 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513}
2514
2515int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2516{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002517 destroy_kvm_mmu(vcpu);
2518 return init_kvm_mmu(vcpu);
2519}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002520EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002521
2522int kvm_mmu_load(struct kvm_vcpu *vcpu)
2523{
Avi Kivity714b93d2007-01-05 16:36:53 -08002524 int r;
2525
Avi Kivitye2dec932007-01-05 16:36:54 -08002526 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002527 if (r)
2528 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002529 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002530 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002531 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002532 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002533 if (r)
2534 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002535 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002536 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002537out:
2538 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002540EXPORT_SYMBOL_GPL(kvm_mmu_load);
2541
2542void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2543{
2544 mmu_free_roots(vcpu);
2545}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546
Avi Kivity09072da2007-05-01 14:16:52 +03002547static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002548 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002549 u64 *spte)
2550{
2551 u64 pte;
2552 struct kvm_mmu_page *child;
2553
2554 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002555 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002556 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002557 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002558 else {
2559 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002560 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002561 }
2562 }
Avi Kivityd555c332009-06-10 14:24:23 +03002563 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002564 if (is_large_pte(pte))
2565 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002566}
2567
Avi Kivity00284252007-05-01 16:53:31 +03002568static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002569 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002570 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002571 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002572{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002573 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002574 ++vcpu->kvm->stat.mmu_pde_zapped;
2575 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002576 }
Avi Kivity00284252007-05-01 16:53:31 +03002577
Avi Kivity4cee5762007-11-18 16:37:07 +02002578 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002579 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002580 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002581 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002582 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002583}
2584
Avi Kivity79539ce2007-11-21 02:06:21 +02002585static bool need_remote_flush(u64 old, u64 new)
2586{
2587 if (!is_shadow_present_pte(old))
2588 return false;
2589 if (!is_shadow_present_pte(new))
2590 return true;
2591 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2592 return true;
2593 old ^= PT64_NX_MASK;
2594 new ^= PT64_NX_MASK;
2595 return (old & ~new & PT64_PERM_MASK) != 0;
2596}
2597
2598static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2599{
2600 if (need_remote_flush(old, new))
2601 kvm_flush_remote_tlbs(vcpu->kvm);
2602 else
2603 kvm_mmu_flush_tlb(vcpu);
2604}
2605
Avi Kivity12b7d282007-09-23 14:10:49 +02002606static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2607{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002608 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002609
Sheng Yang7b523452008-04-25 21:13:50 +08002610 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002611}
2612
Avi Kivityd7824ff2007-12-30 12:29:05 +02002613static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002614 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002615{
2616 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002617 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002618
Avi Kivity43a37952009-06-10 14:12:05 +03002619 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002620 return;
2621 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002622
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002623 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002624 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002625 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002626
Anthony Liguori35149e22008-04-02 14:46:56 -05002627 if (is_error_pfn(pfn)) {
2628 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002629 return;
2630 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002631 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002632 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002633}
2634
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002635static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2636{
2637 u64 *spte = vcpu->arch.last_pte_updated;
2638
2639 if (spte
2640 && vcpu->arch.last_pte_gfn == gfn
2641 && shadow_accessed_mask
2642 && !(*spte & shadow_accessed_mask)
2643 && is_shadow_present_pte(*spte))
2644 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2645}
2646
Avi Kivity09072da2007-05-01 14:16:52 +03002647void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002648 const u8 *new, int bytes,
2649 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002650{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002651 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002652 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002653 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002654 struct hlist_head *bucket;
2655 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002656 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002657 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002658 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002659 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002660 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002661 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002662 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002663 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002664 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002665 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002666 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002667 int invlpg_counter;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002668
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002669 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002670
Avi Kivity08e850c2010-03-15 13:59:57 +02002671 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2672
2673 /*
2674 * Assume that the pte write on a page table of the same type
2675 * as the current vcpu paging mode. This is nearly always true
2676 * (might be false while changing modes). Note it is verified later
2677 * by update_pte().
2678 */
2679 if ((is_pae(vcpu) && bytes == 4) || !new) {
2680 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2681 if (is_pae(vcpu)) {
2682 gpa &= ~(gpa_t)7;
2683 bytes = 8;
2684 }
2685 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2686 if (r)
2687 gentry = 0;
2688 new = (const u8 *)&gentry;
2689 }
2690
Avi Kivity72016f32010-03-15 13:59:53 +02002691 switch (bytes) {
2692 case 4:
2693 gentry = *(const u32 *)new;
2694 break;
2695 case 8:
2696 gentry = *(const u64 *)new;
2697 break;
2698 default:
2699 gentry = 0;
2700 break;
2701 }
2702
Avi Kivity72016f32010-03-15 13:59:53 +02002703 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002704 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002705 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2706 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002707 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002708 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002709 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002710 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002711 if (guest_initiated) {
2712 if (gfn == vcpu->arch.last_pt_write_gfn
2713 && !last_updated_pte_accessed(vcpu)) {
2714 ++vcpu->arch.last_pt_write_count;
2715 if (vcpu->arch.last_pt_write_count >= 3)
2716 flooded = 1;
2717 } else {
2718 vcpu->arch.last_pt_write_gfn = gfn;
2719 vcpu->arch.last_pt_write_count = 1;
2720 vcpu->arch.last_pte_updated = NULL;
2721 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002722 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002723 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002724 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Xiao Guangrong3246af02010-04-16 16:35:54 +08002725
2726restart:
Avi Kivity4db35312007-11-21 15:28:32 +02002727 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002728 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002729 continue;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002730 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002731 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002732 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002733 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002734 /*
2735 * Misaligned accesses are too much trouble to fix
2736 * up; also, they usually indicate a page is not used
2737 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002738 *
2739 * If we're seeing too many writes to a page,
2740 * it may no longer be a page table, or we may be
2741 * forking, in which case it is better to unmap the
2742 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002743 */
2744 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002745 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002746 if (kvm_mmu_zap_page(vcpu->kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002747 goto restart;
Avi Kivity4cee5762007-11-18 16:37:07 +02002748 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002749 continue;
2750 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002751 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002752 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002753 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002754 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002755 page_offset <<= 1; /* 32->64 */
2756 /*
2757 * A 32-bit pde maps 4MB while the shadow pdes map
2758 * only 2MB. So we need to double the offset again
2759 * and zap two pdes instead of one.
2760 */
2761 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002762 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002763 page_offset <<= 1;
2764 npte = 2;
2765 }
Avi Kivityfce06572007-05-01 16:44:05 +03002766 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002767 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002768 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002769 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002770 }
Avi Kivity4db35312007-11-21 15:28:32 +02002771 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002772 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002773 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002774 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002775 if (gentry)
2776 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Avi Kivity79539ce2007-11-21 02:06:21 +02002777 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002778 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002779 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002780 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002781 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002782 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002783 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2784 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2785 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002786 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002787}
2788
Avi Kivitya4360362007-01-05 16:36:45 -08002789int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2790{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002791 gpa_t gpa;
2792 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002793
Avi Kivity60f24782009-08-27 13:37:06 +03002794 if (tdp_enabled)
2795 return 0;
2796
Gleb Natapov1871c602010-02-10 14:21:32 +02002797 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002798
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002799 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002800 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002801 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002802 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002803}
Avi Kivity577bdc42008-07-19 08:57:05 +03002804EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002805
Avi Kivity22d95b12007-09-14 20:26:06 +03002806void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002807{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002808 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2809 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002810 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002811
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002812 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002813 struct kvm_mmu_page, link);
2814 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002815 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002816 }
2817}
Avi Kivityebeace82007-01-05 16:36:47 -08002818
Avi Kivity30677142007-10-28 18:48:59 +02002819int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2820{
2821 int r;
2822 enum emulation_result er;
2823
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002824 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002825 if (r < 0)
2826 goto out;
2827
2828 if (!r) {
2829 r = 1;
2830 goto out;
2831 }
2832
Avi Kivityb733bfb2007-10-28 18:52:05 +02002833 r = mmu_topup_memory_caches(vcpu);
2834 if (r)
2835 goto out;
2836
Avi Kivity851ba692009-08-24 11:10:17 +03002837 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002838
2839 switch (er) {
2840 case EMULATE_DONE:
2841 return 1;
2842 case EMULATE_DO_MMIO:
2843 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002844 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02002845 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002846 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002847 default:
2848 BUG();
2849 }
2850out:
Avi Kivity30677142007-10-28 18:48:59 +02002851 return r;
2852}
2853EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2854
Marcelo Tosattia7052892008-09-23 13:18:35 -03002855void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2856{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002857 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002858 kvm_mmu_flush_tlb(vcpu);
2859 ++vcpu->stat.invlpg;
2860}
2861EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2862
Joerg Roedel18552672008-02-07 13:47:41 +01002863void kvm_enable_tdp(void)
2864{
2865 tdp_enabled = true;
2866}
2867EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2868
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002869void kvm_disable_tdp(void)
2870{
2871 tdp_enabled = false;
2872}
2873EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2874
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875static void free_mmu_pages(struct kvm_vcpu *vcpu)
2876{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002877 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878}
2879
2880static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2881{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002882 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 int i;
2884
2885 ASSERT(vcpu);
2886
Avi Kivity17ac10a2007-01-05 16:36:40 -08002887 /*
2888 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2889 * Therefore we need to allocate shadow page tables in the first
2890 * 4GB of memory, which happens to fit the DMA32 zone.
2891 */
2892 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2893 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002894 return -ENOMEM;
2895
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002896 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002897 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002898 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002899
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
2902
Ingo Molnar8018c272006-12-29 16:50:01 -08002903int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002906 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907
Ingo Molnar8018c272006-12-29 16:50:01 -08002908 return alloc_mmu_pages(vcpu);
2909}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910
Ingo Molnar8018c272006-12-29 16:50:01 -08002911int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2912{
2913 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002914 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002915
Ingo Molnar8018c272006-12-29 16:50:01 -08002916 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917}
2918
2919void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2920{
2921 ASSERT(vcpu);
2922
2923 destroy_kvm_mmu(vcpu);
2924 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002925 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926}
2927
Avi Kivity90cb0522007-07-17 13:04:56 +03002928void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929{
Avi Kivity4db35312007-11-21 15:28:32 +02002930 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002932 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 int i;
2934 u64 *pt;
2935
Sheng Yang291f26b2008-10-16 17:30:57 +08002936 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937 continue;
2938
Avi Kivity4db35312007-11-21 15:28:32 +02002939 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2941 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002942 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 }
Avi Kivity171d5952008-08-27 16:40:51 +03002945 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002947
Avi Kivity90cb0522007-07-17 13:04:56 +03002948void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002949{
Avi Kivity4db35312007-11-21 15:28:32 +02002950 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002951
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002952 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08002953restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002954 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002955 if (kvm_mmu_zap_page(kvm, sp))
Xiao Guangrong3246af02010-04-16 16:35:54 +08002956 goto restart;
2957
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002958 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002959
Avi Kivity90cb0522007-07-17 13:04:56 +03002960 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002961}
2962
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002963static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002964{
2965 struct kvm_mmu_page *page;
2966
2967 page = container_of(kvm->arch.active_mmu_pages.prev,
2968 struct kvm_mmu_page, link);
Gui Jianfeng54a4f022010-05-05 09:03:49 +08002969 return kvm_mmu_zap_page(kvm, page);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002970}
2971
Dave Chinner7f8275d2010-07-19 14:56:17 +10002972static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002973{
2974 struct kvm *kvm;
2975 struct kvm *kvm_freed = NULL;
2976 int cache_count = 0;
2977
2978 spin_lock(&kvm_lock);
2979
2980 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002981 int npages, idx, freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002982
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002983 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002984 spin_lock(&kvm->mmu_lock);
2985 npages = kvm->arch.n_alloc_mmu_pages -
2986 kvm->arch.n_free_mmu_pages;
2987 cache_count += npages;
2988 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08002989 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm);
2990 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002991 kvm_freed = kvm;
2992 }
2993 nr_to_scan--;
2994
2995 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002996 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002997 }
2998 if (kvm_freed)
2999 list_move_tail(&kvm_freed->vm_list, &vm_list);
3000
3001 spin_unlock(&kvm_lock);
3002
3003 return cache_count;
3004}
3005
3006static struct shrinker mmu_shrinker = {
3007 .shrink = mmu_shrink,
3008 .seeks = DEFAULT_SEEKS * 10,
3009};
3010
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003011static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003012{
3013 if (pte_chain_cache)
3014 kmem_cache_destroy(pte_chain_cache);
3015 if (rmap_desc_cache)
3016 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003017 if (mmu_page_header_cache)
3018 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003019}
3020
Izik Eidus3ee16c82008-03-30 15:17:21 +03003021void kvm_mmu_module_exit(void)
3022{
3023 mmu_destroy_caches();
3024 unregister_shrinker(&mmu_shrinker);
3025}
3026
Avi Kivityb5a33a72007-04-15 16:31:09 +03003027int kvm_mmu_module_init(void)
3028{
3029 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3030 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003031 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003032 if (!pte_chain_cache)
3033 goto nomem;
3034 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3035 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003036 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003037 if (!rmap_desc_cache)
3038 goto nomem;
3039
Avi Kivityd3d25b02007-05-30 12:34:53 +03003040 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3041 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003042 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003043 if (!mmu_page_header_cache)
3044 goto nomem;
3045
Izik Eidus3ee16c82008-03-30 15:17:21 +03003046 register_shrinker(&mmu_shrinker);
3047
Avi Kivityb5a33a72007-04-15 16:31:09 +03003048 return 0;
3049
3050nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003051 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003052 return -ENOMEM;
3053}
3054
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003055/*
3056 * Caculate mmu pages needed for kvm.
3057 */
3058unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3059{
3060 int i;
3061 unsigned int nr_mmu_pages;
3062 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003063 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003064
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003065 slots = kvm_memslots(kvm);
3066
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003067 for (i = 0; i < slots->nmemslots; i++)
3068 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003069
3070 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3071 nr_mmu_pages = max(nr_mmu_pages,
3072 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3073
3074 return nr_mmu_pages;
3075}
3076
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003077static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3078 unsigned len)
3079{
3080 if (len > buffer->len)
3081 return NULL;
3082 return buffer->ptr;
3083}
3084
3085static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3086 unsigned len)
3087{
3088 void *ret;
3089
3090 ret = pv_mmu_peek_buffer(buffer, len);
3091 if (!ret)
3092 return ret;
3093 buffer->ptr += len;
3094 buffer->len -= len;
3095 buffer->processed += len;
3096 return ret;
3097}
3098
3099static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3100 gpa_t addr, gpa_t value)
3101{
3102 int bytes = 8;
3103 int r;
3104
3105 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3106 bytes = 4;
3107
3108 r = mmu_topup_memory_caches(vcpu);
3109 if (r)
3110 return r;
3111
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003112 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003113 return -EFAULT;
3114
3115 return 1;
3116}
3117
3118static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3119{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003120 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003121 return 1;
3122}
3123
3124static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3125{
3126 spin_lock(&vcpu->kvm->mmu_lock);
3127 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3128 spin_unlock(&vcpu->kvm->mmu_lock);
3129 return 1;
3130}
3131
3132static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3133 struct kvm_pv_mmu_op_buffer *buffer)
3134{
3135 struct kvm_mmu_op_header *header;
3136
3137 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3138 if (!header)
3139 return 0;
3140 switch (header->op) {
3141 case KVM_MMU_OP_WRITE_PTE: {
3142 struct kvm_mmu_op_write_pte *wpte;
3143
3144 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3145 if (!wpte)
3146 return 0;
3147 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3148 wpte->pte_val);
3149 }
3150 case KVM_MMU_OP_FLUSH_TLB: {
3151 struct kvm_mmu_op_flush_tlb *ftlb;
3152
3153 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3154 if (!ftlb)
3155 return 0;
3156 return kvm_pv_mmu_flush_tlb(vcpu);
3157 }
3158 case KVM_MMU_OP_RELEASE_PT: {
3159 struct kvm_mmu_op_release_pt *rpt;
3160
3161 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3162 if (!rpt)
3163 return 0;
3164 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3165 }
3166 default: return 0;
3167 }
3168}
3169
3170int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3171 gpa_t addr, unsigned long *ret)
3172{
3173 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003174 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003175
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003176 buffer->ptr = buffer->buf;
3177 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3178 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003179
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003180 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003181 if (r)
3182 goto out;
3183
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003184 while (buffer->len) {
3185 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003186 if (r < 0)
3187 goto out;
3188 if (r == 0)
3189 break;
3190 }
3191
3192 r = 1;
3193out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003194 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003195 return r;
3196}
3197
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003198int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3199{
3200 struct kvm_shadow_walk_iterator iterator;
3201 int nr_sptes = 0;
3202
3203 spin_lock(&vcpu->kvm->mmu_lock);
3204 for_each_shadow_entry(vcpu, addr, iterator) {
3205 sptes[iterator.level-1] = *iterator.sptep;
3206 nr_sptes++;
3207 if (!is_shadow_present_pte(*iterator.sptep))
3208 break;
3209 }
3210 spin_unlock(&vcpu->kvm->mmu_lock);
3211
3212 return nr_sptes;
3213}
3214EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3215
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003216#ifdef AUDIT
3217
3218static const char *audit_msg;
3219
3220static gva_t canonicalize(gva_t gva)
3221{
3222#ifdef CONFIG_X86_64
3223 gva = (long long)(gva << 16) >> 16;
3224#endif
3225 return gva;
3226}
3227
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003228
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003229typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003230
3231static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3232 inspect_spte_fn fn)
3233{
3234 int i;
3235
3236 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3237 u64 ent = sp->spt[i];
3238
3239 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003240 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003241 struct kvm_mmu_page *child;
3242 child = page_header(ent & PT64_BASE_ADDR_MASK);
3243 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003244 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003245 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003246 }
3247 }
3248}
3249
3250static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3251{
3252 int i;
3253 struct kvm_mmu_page *sp;
3254
3255 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3256 return;
3257 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3258 hpa_t root = vcpu->arch.mmu.root_hpa;
3259 sp = page_header(root);
3260 __mmu_spte_walk(vcpu->kvm, sp, fn);
3261 return;
3262 }
3263 for (i = 0; i < 4; ++i) {
3264 hpa_t root = vcpu->arch.mmu.pae_root[i];
3265
3266 if (root && VALID_PAGE(root)) {
3267 root &= PT64_BASE_ADDR_MASK;
3268 sp = page_header(root);
3269 __mmu_spte_walk(vcpu->kvm, sp, fn);
3270 }
3271 }
3272 return;
3273}
3274
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003275static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3276 gva_t va, int level)
3277{
3278 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3279 int i;
3280 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3281
3282 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3283 u64 ent = pt[i];
3284
Avi Kivityc7addb92007-09-16 18:58:32 +02003285 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003286 continue;
3287
3288 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003289 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3290 audit_mappings_page(vcpu, ent, va, level - 1);
3291 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003292 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003293 gfn_t gfn = gpa >> PAGE_SHIFT;
3294 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3295 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003296
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003297 if (is_error_pfn(pfn)) {
3298 kvm_release_pfn_clean(pfn);
3299 continue;
3300 }
3301
Avi Kivityc7addb92007-09-16 18:58:32 +02003302 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003304 printk(KERN_ERR "xx audit error: (%s) levels %d"
3305 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003306 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003307 va, gpa, hpa, ent,
3308 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003309 else if (ent == shadow_notrap_nonpresent_pte
3310 && !is_error_hpa(hpa))
3311 printk(KERN_ERR "audit: (%s) notrap shadow,"
3312 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003313 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003314
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315 }
3316 }
3317}
3318
3319static void audit_mappings(struct kvm_vcpu *vcpu)
3320{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003321 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003322
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003323 if (vcpu->arch.mmu.root_level == 4)
3324 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003325 else
3326 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003327 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003328 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003329 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003330 i << 30,
3331 2);
3332}
3333
3334static int count_rmaps(struct kvm_vcpu *vcpu)
3335{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003336 struct kvm *kvm = vcpu->kvm;
3337 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003338 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003339 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003340
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003341 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003342 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003343 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003344 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003345 struct kvm_rmap_desc *d;
3346
3347 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003348 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003349
Izik Eidus290fc382007-09-27 14:11:22 +02003350 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003351 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003352 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003353 ++nmaps;
3354 continue;
3355 }
Izik Eidus290fc382007-09-27 14:11:22 +02003356 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003357 while (d) {
3358 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003359 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003360 ++nmaps;
3361 else
3362 break;
3363 d = d->more;
3364 }
3365 }
3366 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003367 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003368 return nmaps;
3369}
3370
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003371void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003372{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003373 unsigned long *rmapp;
3374 struct kvm_mmu_page *rev_sp;
3375 gfn_t gfn;
3376
3377 if (*sptep & PT_WRITABLE_MASK) {
3378 rev_sp = page_header(__pa(sptep));
3379 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3380
3381 if (!gfn_to_memslot(kvm, gfn)) {
3382 if (!printk_ratelimit())
3383 return;
3384 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3385 audit_msg, gfn);
3386 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003387 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003388 rev_sp->gfn);
3389 dump_stack();
3390 return;
3391 }
3392
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003393 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003394 rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003395 if (!*rmapp) {
3396 if (!printk_ratelimit())
3397 return;
3398 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3399 audit_msg, *sptep);
3400 dump_stack();
3401 }
3402 }
3403
3404}
3405
3406void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3407{
3408 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3409}
3410
3411static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3412{
Avi Kivity4db35312007-11-21 15:28:32 +02003413 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003414 int i;
3415
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003416 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003417 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003418
Avi Kivity4db35312007-11-21 15:28:32 +02003419 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003420 continue;
3421
3422 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3423 u64 ent = pt[i];
3424
3425 if (!(ent & PT_PRESENT_MASK))
3426 continue;
3427 if (!(ent & PT_WRITABLE_MASK))
3428 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003429 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003430 }
3431 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003432 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003433}
3434
3435static void audit_rmap(struct kvm_vcpu *vcpu)
3436{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003437 check_writable_mappings_rmap(vcpu);
3438 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003439}
3440
3441static void audit_write_protection(struct kvm_vcpu *vcpu)
3442{
Avi Kivity4db35312007-11-21 15:28:32 +02003443 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003444 struct kvm_memory_slot *slot;
3445 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003446 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003447 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003448
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003449 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003450 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003451 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003452 if (sp->unsync)
3453 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003454
Avi Kivity4db35312007-11-21 15:28:32 +02003455 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003456 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003457 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003458
3459 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3460 while (spte) {
3461 if (*spte & PT_WRITABLE_MASK)
3462 printk(KERN_ERR "%s: (%s) shadow page has "
3463 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003464 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003465 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003466 spte = rmap_next(vcpu->kvm, rmapp, spte);
3467 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003468 }
3469}
3470
3471static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3472{
3473 int olddbg = dbg;
3474
3475 dbg = 0;
3476 audit_msg = msg;
3477 audit_rmap(vcpu);
3478 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003479 if (strcmp("pre pte write", audit_msg) != 0)
3480 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003481 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003482 dbg = olddbg;
3483}
3484
3485#endif