blob: ca07ed083b59a4d1cc8d436215528dbcf9b3a384 [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 Guangrong1047df12010-06-11 21:35:15 +0800178typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
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 Kivityda928522007-11-21 13:54:47 +0200277static gfn_t pse36_gfn_delta(u32 gpte)
278{
279 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
280
281 return (gpte & PT32_DIR_PSE36_MASK) << shift;
282}
283
Avi Kivityd555c332009-06-10 14:24:23 +0300284static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300285{
286#ifdef CONFIG_X86_64
287 set_64bit((unsigned long *)sptep, spte);
288#else
289 set_64bit((unsigned long long *)sptep, spte);
290#endif
291}
292
Avi Kivitye2dec932007-01-05 16:36:54 -0800293static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300294 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800295{
296 void *obj;
297
298 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800299 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800300 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300301 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800302 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800303 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800304 cache->objects[cache->nobjs++] = obj;
305 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800306 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800307}
308
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800309static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
310 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800311{
312 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800313 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800314}
315
Avi Kivityc1158e62007-07-20 08:18:27 +0300316static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300317 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300318{
319 struct page *page;
320
321 if (cache->nobjs >= min)
322 return 0;
323 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300325 if (!page)
326 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300327 cache->objects[cache->nobjs++] = page_address(page);
328 }
329 return 0;
330}
331
332static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
333{
334 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300335 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300336}
337
Avi Kivity8c438502007-04-16 11:53:17 +0300338static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
339{
340 int r;
341
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300343 pte_chain_cache, 4);
344 if (r)
345 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800346 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200347 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300348 if (r)
349 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300351 if (r)
352 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800353 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300354 mmu_page_header_cache, 4);
355out:
Avi Kivity8c438502007-04-16 11:53:17 +0300356 return r;
357}
358
Avi Kivity714b93d2007-01-05 16:36:53 -0800359static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
360{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800361 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
362 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800363 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800364 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
365 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800366}
367
368static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
369 size_t size)
370{
371 void *p;
372
373 BUG_ON(!mc->nobjs);
374 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800375 return p;
376}
377
Avi Kivity714b93d2007-01-05 16:36:53 -0800378static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
379{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800380 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800381 sizeof(struct kvm_pte_chain));
382}
383
Avi Kivity90cb0522007-07-17 13:04:56 +0300384static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800385{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800386 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800387}
388
389static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
390{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800391 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800392 sizeof(struct kvm_rmap_desc));
393}
394
Avi Kivity90cb0522007-07-17 13:04:56 +0300395static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800396{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800397 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800398}
399
Lai Jiangshan2032a932010-05-26 16:49:59 +0800400static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
401{
402 if (!sp->role.direct)
403 return sp->gfns[index];
404
405 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
406}
407
408static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
409{
410 if (sp->role.direct)
411 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
412 else
413 sp->gfns[index] = gfn;
414}
415
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800416/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300417 * Return the pointer to the largepage write count for a given
418 * gfn, handling slots that are not large page aligned.
419 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200420static int *slot_largepage_idx(gfn_t gfn,
421 struct kvm_memory_slot *slot,
422 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423{
424 unsigned long idx;
425
Joerg Roedeld25797b2009-07-27 16:30:43 +0200426 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
427 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
428 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300429}
430
431static void account_shadowed(struct kvm *kvm, gfn_t gfn)
432{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200433 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200435 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300436
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300437 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438 for (i = PT_DIRECTORY_LEVEL;
439 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
440 write_count = slot_largepage_idx(gfn, slot, i);
441 *write_count += 1;
442 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300443}
444
445static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
446{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200447 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200449 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300451 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200452 for (i = PT_DIRECTORY_LEVEL;
453 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200454 write_count = slot_largepage_idx(gfn, slot, i);
455 *write_count -= 1;
456 WARN_ON(*write_count < 0);
457 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300458}
459
Joerg Roedeld25797b2009-07-27 16:30:43 +0200460static int has_wrprotected_page(struct kvm *kvm,
461 gfn_t gfn,
462 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300463{
Izik Eidus28430992008-10-03 17:40:32 +0300464 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300465 int *largepage_idx;
466
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300467 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200469 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470 return *largepage_idx;
471 }
472
473 return 1;
474}
475
Joerg Roedeld25797b2009-07-27 16:30:43 +0200476static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100478 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200479 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300480
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100481 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482
Joerg Roedeld25797b2009-07-27 16:30:43 +0200483 for (i = PT_PAGE_TABLE_LEVEL;
484 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
485 if (page_size >= KVM_HPAGE_SIZE(i))
486 ret = i;
487 else
488 break;
489 }
490
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300491 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492}
493
Joerg Roedeld25797b2009-07-27 16:30:43 +0200494static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300495{
496 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800497 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300498
499 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
500 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200501 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502
Joerg Roedeld25797b2009-07-27 16:30:43 +0200503 host_level = host_mapping_level(vcpu->kvm, large_gfn);
504
505 if (host_level == PT_PAGE_TABLE_LEVEL)
506 return host_level;
507
Sheng Yang878403b2010-01-05 19:02:29 +0800508 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
509 kvm_x86_ops->get_lpage_level() : host_level;
510
511 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200512 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
513 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200514
515 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300516}
517
518/*
Izik Eidus290fc382007-09-27 14:11:22 +0200519 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200520 */
521
Joerg Roedel44ad9942009-07-27 16:30:42 +0200522static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200523{
524 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300525 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200526
527 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200528 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300529 return &slot->rmap[gfn - slot->base_gfn];
530
Joerg Roedel44ad9942009-07-27 16:30:42 +0200531 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
532 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300533
Joerg Roedel44ad9942009-07-27 16:30:42 +0200534 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200535}
536
537/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 * Reverse mapping data structures:
539 *
Izik Eidus290fc382007-09-27 14:11:22 +0200540 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
541 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800542 *
Izik Eidus290fc382007-09-27 14:11:22 +0200543 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
544 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300545 *
546 * Returns the number of rmap entries before the spte was added or zero if
547 * the spte was not added.
548 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800549 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200550static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800551{
Avi Kivity4db35312007-11-21 15:28:32 +0200552 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200554 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300555 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556
Avi Kivity43a37952009-06-10 14:12:05 +0300557 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300558 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200559 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800560 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200561 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200562 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200564 *rmapp = (unsigned long)spte;
565 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800567 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300568 desc->sptes[0] = (u64 *)*rmapp;
569 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200570 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 } else {
572 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200573 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300574 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300576 count += RMAP_EXT;
577 }
Avi Kivityd555c332009-06-10 14:24:23 +0300578 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800579 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 desc = desc->more;
581 }
Avi Kivityd555c332009-06-10 14:24:23 +0300582 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800583 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300584 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800585 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300586 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800587}
588
Izik Eidus290fc382007-09-27 14:11:22 +0200589static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800590 struct kvm_rmap_desc *desc,
591 int i,
592 struct kvm_rmap_desc *prev_desc)
593{
594 int j;
595
Avi Kivityd555c332009-06-10 14:24:23 +0300596 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800597 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300598 desc->sptes[i] = desc->sptes[j];
599 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 if (j != 0)
601 return;
602 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300603 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800604 else
605 if (prev_desc)
606 prev_desc->more = desc->more;
607 else
Izik Eidus290fc382007-09-27 14:11:22 +0200608 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300609 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800610}
611
Izik Eidus290fc382007-09-27 14:11:22 +0200612static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800614 struct kvm_rmap_desc *desc;
615 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200616 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500617 pfn_t pfn;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800618 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200619 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620 int i;
621
Avi Kivity43a37952009-06-10 14:12:05 +0300622 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200624 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500625 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800626 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500627 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900628 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300629 kvm_set_pfn_dirty(pfn);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800630 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
631 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200632 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800633 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
634 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200635 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200637 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800638 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
639 spte, *spte);
640 BUG();
641 }
Izik Eidus290fc382007-09-27 14:11:22 +0200642 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 } else {
644 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200645 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800646 prev_desc = NULL;
647 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300648 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
649 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200650 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800651 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800652 prev_desc);
653 return;
654 }
655 prev_desc = desc;
656 desc = desc->more;
657 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200658 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800659 BUG();
660 }
661}
662
Izik Eidus98348e92007-10-16 14:42:30 +0200663static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800664{
Avi Kivity374cbac2007-01-05 16:36:43 -0800665 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200666 u64 *prev_spte;
667 int i;
668
669 if (!*rmapp)
670 return NULL;
671 else if (!(*rmapp & 1)) {
672 if (!spte)
673 return (u64 *)*rmapp;
674 return NULL;
675 }
676 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200677 prev_spte = NULL;
678 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300679 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200680 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300681 return desc->sptes[i];
682 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200683 }
684 desc = desc->more;
685 }
686 return NULL;
687}
688
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200689static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200690{
Izik Eidus290fc382007-09-27 14:11:22 +0200691 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800692 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200693 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800694
Joerg Roedel44ad9942009-07-27 16:30:42 +0200695 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800696
Izik Eidus98348e92007-10-16 14:42:30 +0200697 spte = rmap_next(kvm, rmapp, NULL);
698 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800699 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800700 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800701 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900702 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300703 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800704 write_protected = 1;
705 }
Izik Eidus9647c142007-10-16 14:43:46 +0200706 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800707 }
Izik Eidus855149a2008-03-20 18:17:24 +0200708 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500709 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200710
711 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500712 pfn = spte_to_pfn(*spte);
713 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200714 }
715
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300716 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200717 for (i = PT_DIRECTORY_LEVEL;
718 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
719 rmapp = gfn_to_rmap(kvm, gfn, i);
720 spte = rmap_next(kvm, rmapp, NULL);
721 while (spte) {
722 BUG_ON(!spte);
723 BUG_ON(!(*spte & PT_PRESENT_MASK));
724 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
725 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900726 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200727 rmap_remove(kvm, spte);
728 --kvm->stat.lpages;
729 __set_spte(spte, shadow_trap_nonpresent_pte);
730 spte = NULL;
731 write_protected = 1;
732 }
733 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300734 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300735 }
736
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200737 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800738}
739
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000740static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
741 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200742{
743 u64 *spte;
744 int need_tlb_flush = 0;
745
746 while ((spte = rmap_next(kvm, rmapp, NULL))) {
747 BUG_ON(!(*spte & PT_PRESENT_MASK));
748 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
749 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300750 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200751 need_tlb_flush = 1;
752 }
753 return need_tlb_flush;
754}
755
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000756static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
757 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300758{
759 int need_flush = 0;
760 u64 *spte, new_spte;
761 pte_t *ptep = (pte_t *)data;
762 pfn_t new_pfn;
763
764 WARN_ON(pte_huge(*ptep));
765 new_pfn = pte_pfn(*ptep);
766 spte = rmap_next(kvm, rmapp, NULL);
767 while (spte) {
768 BUG_ON(!is_shadow_present_pte(*spte));
769 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
770 need_flush = 1;
771 if (pte_write(*ptep)) {
772 rmap_remove(kvm, spte);
773 __set_spte(spte, shadow_trap_nonpresent_pte);
774 spte = rmap_next(kvm, rmapp, NULL);
775 } else {
776 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
777 new_spte |= (u64)new_pfn << PAGE_SHIFT;
778
779 new_spte &= ~PT_WRITABLE_MASK;
780 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900781 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300782 kvm_set_pfn_dirty(spte_to_pfn(*spte));
783 __set_spte(spte, new_spte);
784 spte = rmap_next(kvm, rmapp, spte);
785 }
786 }
787 if (need_flush)
788 kvm_flush_remote_tlbs(kvm);
789
790 return 0;
791}
792
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000793static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
794 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300795 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000796 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200797{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200798 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200799 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200800 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200801 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200802
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800803 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200804
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200805 for (i = 0; i < slots->nmemslots; i++) {
806 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200807 unsigned long start = memslot->userspace_addr;
808 unsigned long end;
809
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200810 end = start + (memslot->npages << PAGE_SHIFT);
811 if (hva >= start && hva < end) {
812 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200813
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200814 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200815
816 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
817 int idx = gfn_offset;
818 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200819 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300820 &memslot->lpage_info[j][idx].rmap_pde,
821 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200822 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200823 trace_kvm_age_page(hva, memslot, ret);
824 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200825 }
826 }
827
828 return retval;
829}
830
831int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
832{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300833 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200834}
835
Izik Eidus3da0dd42009-09-23 21:47:18 +0300836void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
837{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000838 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300839}
840
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000841static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
842 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200843{
844 u64 *spte;
845 int young = 0;
846
Rik van Riel6316e1c2010-02-03 16:11:03 -0500847 /*
848 * Emulate the accessed bit for EPT, by checking if this page has
849 * an EPT mapping, and clearing it if it does. On the next access,
850 * a new EPT mapping will be established.
851 * This has some overhead, but not as much as the cost of swapping
852 * out actively used pages or breaking up actively used hugepages.
853 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800854 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500855 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800856
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200857 spte = rmap_next(kvm, rmapp, NULL);
858 while (spte) {
859 int _young;
860 u64 _spte = *spte;
861 BUG_ON(!(_spte & PT_PRESENT_MASK));
862 _young = _spte & PT_ACCESSED_MASK;
863 if (_young) {
864 young = 1;
865 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
866 }
867 spte = rmap_next(kvm, rmapp, spte);
868 }
869 return young;
870}
871
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300872#define RMAP_RECYCLE_THRESHOLD 1000
873
Joerg Roedel852e3c12009-07-27 16:30:44 +0200874static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300875{
876 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200877 struct kvm_mmu_page *sp;
878
879 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300880
Joerg Roedel852e3c12009-07-27 16:30:44 +0200881 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300882
Izik Eidus3da0dd42009-09-23 21:47:18 +0300883 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300884 kvm_flush_remote_tlbs(vcpu->kvm);
885}
886
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200887int kvm_age_hva(struct kvm *kvm, unsigned long hva)
888{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300889 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200890}
891
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800892#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300893static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894{
Avi Kivity139bdb22007-01-05 16:36:50 -0800895 u64 *pos;
896 u64 *end;
897
Avi Kivity47ad8e62007-05-06 15:50:58 +0300898 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300899 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800900 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800901 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800903 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 return 1;
905}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800906#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907
Avi Kivity4db35312007-11-21 15:28:32 +0200908static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800909{
Avi Kivity4db35312007-11-21 15:28:32 +0200910 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800911 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200912 list_del(&sp->link);
913 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800914 if (!sp->role.direct)
915 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800916 kmem_cache_free(mmu_page_header_cache, sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800917 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800918}
919
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800920static unsigned kvm_page_table_hashfn(gfn_t gfn)
921{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200922 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800923}
924
Avi Kivity25c0de22007-01-05 16:36:42 -0800925static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +0800926 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927{
Avi Kivity4db35312007-11-21 15:28:32 +0200928 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800930 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
931 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800932 if (!direct)
933 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
934 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200935 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800936 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800937 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200938 sp->multimapped = 0;
939 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800940 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200941 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942}
943
Avi Kivity714b93d2007-01-05 16:36:53 -0800944static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200945 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946{
947 struct kvm_pte_chain *pte_chain;
948 struct hlist_node *node;
949 int i;
950
951 if (!parent_pte)
952 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200953 if (!sp->multimapped) {
954 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800955
956 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200957 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800958 return;
959 }
Avi Kivity4db35312007-11-21 15:28:32 +0200960 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800961 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200962 INIT_HLIST_HEAD(&sp->parent_ptes);
963 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 pte_chain->parent_ptes[0] = old;
965 }
Avi Kivity4db35312007-11-21 15:28:32 +0200966 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800967 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
968 continue;
969 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
970 if (!pte_chain->parent_ptes[i]) {
971 pte_chain->parent_ptes[i] = parent_pte;
972 return;
973 }
974 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800975 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800976 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200977 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800978 pte_chain->parent_ptes[0] = parent_pte;
979}
980
Avi Kivity4db35312007-11-21 15:28:32 +0200981static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800982 u64 *parent_pte)
983{
984 struct kvm_pte_chain *pte_chain;
985 struct hlist_node *node;
986 int i;
987
Avi Kivity4db35312007-11-21 15:28:32 +0200988 if (!sp->multimapped) {
989 BUG_ON(sp->parent_pte != parent_pte);
990 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800991 return;
992 }
Avi Kivity4db35312007-11-21 15:28:32 +0200993 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800994 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
995 if (!pte_chain->parent_ptes[i])
996 break;
997 if (pte_chain->parent_ptes[i] != parent_pte)
998 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800999 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1000 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001001 pte_chain->parent_ptes[i]
1002 = pte_chain->parent_ptes[i + 1];
1003 ++i;
1004 }
1005 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001006 if (i == 0) {
1007 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001008 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001009 if (hlist_empty(&sp->parent_ptes)) {
1010 sp->multimapped = 0;
1011 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001012 }
1013 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001014 return;
1015 }
1016 BUG();
1017}
1018
Xiao Guangrong6b184932010-04-16 21:29:17 +08001019static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001020{
1021 struct kvm_pte_chain *pte_chain;
1022 struct hlist_node *node;
1023 struct kvm_mmu_page *parent_sp;
1024 int i;
1025
1026 if (!sp->multimapped && sp->parent_pte) {
1027 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001028 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001029 return;
1030 }
1031
1032 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1033 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001034 u64 *spte = pte_chain->parent_ptes[i];
1035
1036 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001037 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001038 parent_sp = page_header(__pa(spte));
1039 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001040 }
1041}
1042
Xiao Guangrong1047df12010-06-11 21:35:15 +08001043static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001044static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001045{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001046 mmu_parent_walk(sp, mark_unsync);
1047}
1048
1049static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1050{
1051 unsigned int index;
1052
1053 index = spte - sp->spt;
1054 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1055 return;
1056 if (sp->unsync_children++)
1057 return;
1058 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001059}
1060
Avi Kivityd761a502008-05-29 14:55:03 +03001061static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1062 struct kvm_mmu_page *sp)
1063{
1064 int i;
1065
1066 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1067 sp->spt[i] = shadow_trap_nonpresent_pte;
1068}
1069
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001070static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001071 struct kvm_mmu_page *sp, bool clear_unsync)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001072{
1073 return 1;
1074}
1075
Marcelo Tosattia7052892008-09-23 13:18:35 -03001076static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1077{
1078}
1079
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001080#define KVM_PAGE_ARRAY_NR 16
1081
1082struct kvm_mmu_pages {
1083 struct mmu_page_and_offset {
1084 struct kvm_mmu_page *sp;
1085 unsigned int idx;
1086 } page[KVM_PAGE_ARRAY_NR];
1087 unsigned int nr;
1088};
1089
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001090#define for_each_unsync_children(bitmap, idx) \
1091 for (idx = find_first_bit(bitmap, 512); \
1092 idx < 512; \
1093 idx = find_next_bit(bitmap, 512, idx+1))
1094
Hannes Edercded19f2009-02-21 02:19:13 +01001095static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1096 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001097{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001098 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001099
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001100 if (sp->unsync)
1101 for (i=0; i < pvec->nr; i++)
1102 if (pvec->page[i].sp == sp)
1103 return 0;
1104
1105 pvec->page[pvec->nr].sp = sp;
1106 pvec->page[pvec->nr].idx = idx;
1107 pvec->nr++;
1108 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1109}
1110
1111static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1112 struct kvm_mmu_pages *pvec)
1113{
1114 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001115
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001116 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001117 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001118 u64 ent = sp->spt[i];
1119
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001120 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1121 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001122
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001123 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001124
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001125 if (child->unsync_children) {
1126 if (mmu_pages_add(pvec, child, i))
1127 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001129 ret = __mmu_unsync_walk(child, pvec);
1130 if (!ret)
1131 goto clear_child_bitmap;
1132 else if (ret > 0)
1133 nr_unsync_leaf += ret;
1134 else
1135 return ret;
1136 } else if (child->unsync) {
1137 nr_unsync_leaf++;
1138 if (mmu_pages_add(pvec, child, i))
1139 return -ENOSPC;
1140 } else
1141 goto clear_child_bitmap;
1142
1143 continue;
1144
1145clear_child_bitmap:
1146 __clear_bit(i, sp->unsync_child_bitmap);
1147 sp->unsync_children--;
1148 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001149 }
1150
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001151
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001152 return nr_unsync_leaf;
1153}
1154
1155static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1156 struct kvm_mmu_pages *pvec)
1157{
1158 if (!sp->unsync_children)
1159 return 0;
1160
1161 mmu_pages_add(pvec, sp, 0);
1162 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001163}
1164
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001165static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1166{
1167 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001168 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001169 sp->unsync = 0;
1170 --kvm->stat.mmu_unsync;
1171}
1172
Xiao Guangrong77758342010-06-04 21:53:54 +08001173static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1174 struct list_head *invalid_list);
1175static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1176 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001177
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001178#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1179 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001180 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1181 if ((sp)->gfn != (gfn)) {} else
1182
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001183#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1184 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001185 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1186 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1187 (sp)->role.invalid) {} else
1188
Xiao Guangrongf918b442010-06-11 21:30:36 +08001189/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001190static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001191 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001192{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001193 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001194 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001195 return 1;
1196 }
1197
Xiao Guangrongf918b442010-06-11 21:30:36 +08001198 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001199 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001200
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001201 if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001202 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001203 return 1;
1204 }
1205
1206 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001207 return 0;
1208}
1209
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001210static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1211 struct kvm_mmu_page *sp)
1212{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001213 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001214 int ret;
1215
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001216 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001217 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001218 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1219
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001220 return ret;
1221}
1222
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001223static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1224 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001225{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001226 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001227}
1228
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001229/* @gfn should be write-protected at the call site */
1230static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1231{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001232 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001233 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001234 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001235 bool flush = false;
1236
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001237 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001238 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001239 continue;
1240
1241 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1242 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001243 (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001244 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001245 continue;
1246 }
1247 kvm_unlink_unsync_page(vcpu->kvm, s);
1248 flush = true;
1249 }
1250
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001251 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001252 if (flush)
1253 kvm_mmu_flush_tlb(vcpu);
1254}
1255
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001256struct mmu_page_path {
1257 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1258 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001259};
1260
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001261#define for_each_sp(pvec, sp, parents, i) \
1262 for (i = mmu_pages_next(&pvec, &parents, -1), \
1263 sp = pvec.page[i].sp; \
1264 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1265 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001266
Hannes Edercded19f2009-02-21 02:19:13 +01001267static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1268 struct mmu_page_path *parents,
1269 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001270{
1271 int n;
1272
1273 for (n = i+1; n < pvec->nr; n++) {
1274 struct kvm_mmu_page *sp = pvec->page[n].sp;
1275
1276 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1277 parents->idx[0] = pvec->page[n].idx;
1278 return n;
1279 }
1280
1281 parents->parent[sp->role.level-2] = sp;
1282 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1283 }
1284
1285 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001286}
1287
Hannes Edercded19f2009-02-21 02:19:13 +01001288static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001289{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001290 struct kvm_mmu_page *sp;
1291 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001292
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001293 do {
1294 unsigned int idx = parents->idx[level];
1295
1296 sp = parents->parent[level];
1297 if (!sp)
1298 return;
1299
1300 --sp->unsync_children;
1301 WARN_ON((int)sp->unsync_children < 0);
1302 __clear_bit(idx, sp->unsync_child_bitmap);
1303 level++;
1304 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1305}
1306
1307static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1308 struct mmu_page_path *parents,
1309 struct kvm_mmu_pages *pvec)
1310{
1311 parents->parent[parent->role.level-1] = NULL;
1312 pvec->nr = 0;
1313}
1314
1315static void mmu_sync_children(struct kvm_vcpu *vcpu,
1316 struct kvm_mmu_page *parent)
1317{
1318 int i;
1319 struct kvm_mmu_page *sp;
1320 struct mmu_page_path parents;
1321 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001322 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001323
1324 kvm_mmu_pages_init(parent, &parents, &pages);
1325 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001326 int protected = 0;
1327
1328 for_each_sp(pages, sp, parents, i)
1329 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1330
1331 if (protected)
1332 kvm_flush_remote_tlbs(vcpu->kvm);
1333
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001334 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001335 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001336 mmu_pages_clear_parents(&parents);
1337 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001338 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001339 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001340 kvm_mmu_pages_init(parent, &parents, &pages);
1341 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001342}
1343
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001344static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1345 gfn_t gfn,
1346 gva_t gaddr,
1347 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001348 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001349 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001350 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001351{
1352 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001353 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001354 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001355 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001356 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001357
Avi Kivitya770f6f2008-12-21 19:20:09 +02001358 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001359 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001360 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001361 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001362 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001363 role.access = access;
Gui Jianfengb66d8002010-05-31 17:11:39 +08001364 if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001365 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1366 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1367 role.quadrant = quadrant;
1368 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001369 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001370 if (!need_sync && sp->unsync)
1371 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001372
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001373 if (sp->role.word != role.word)
1374 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001375
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001376 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1377 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001378
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001379 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1380 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001381 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001382 kvm_mmu_mark_parents_unsync(sp);
1383 } else if (sp->unsync)
1384 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001385
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001386 trace_kvm_mmu_get_page(sp, false);
1387 return sp;
1388 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001389 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001390 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001391 if (!sp)
1392 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001393 sp->gfn = gfn;
1394 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001395 hlist_add_head(&sp->hash_link,
1396 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001397 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001398 if (rmap_write_protect(vcpu->kvm, gfn))
1399 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001400 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1401 kvm_sync_pages(vcpu, gfn);
1402
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001403 account_shadowed(vcpu->kvm, gfn);
1404 }
Avi Kivity131d8272008-05-29 14:56:28 +03001405 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1406 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1407 else
1408 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001409 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001410 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001411}
1412
Avi Kivity2d111232008-12-25 14:39:47 +02001413static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1414 struct kvm_vcpu *vcpu, u64 addr)
1415{
1416 iterator->addr = addr;
1417 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1418 iterator->level = vcpu->arch.mmu.shadow_root_level;
1419 if (iterator->level == PT32E_ROOT_LEVEL) {
1420 iterator->shadow_addr
1421 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1422 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1423 --iterator->level;
1424 if (!iterator->shadow_addr)
1425 iterator->level = 0;
1426 }
1427}
1428
1429static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1430{
1431 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1432 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001433
1434 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1435 if (is_large_pte(*iterator->sptep))
1436 return false;
1437
Avi Kivity2d111232008-12-25 14:39:47 +02001438 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1439 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1440 return true;
1441}
1442
1443static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1444{
1445 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1446 --iterator->level;
1447}
1448
Avi Kivity90cb0522007-07-17 13:04:56 +03001449static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001450 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001451{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001452 unsigned i;
1453 u64 *pt;
1454 u64 ent;
1455
Avi Kivity4db35312007-11-21 15:28:32 +02001456 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001457
Avi Kivity697fe2e2007-01-05 16:36:46 -08001458 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1459 ent = pt[i];
1460
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001461 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001462 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001463 ent &= PT64_BASE_ADDR_MASK;
1464 mmu_page_remove_parent_pte(page_header(ent),
1465 &pt[i]);
1466 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001467 if (is_large_pte(ent))
1468 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001469 rmap_remove(kvm, &pt[i]);
1470 }
1471 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001472 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001473 }
Avi Kivitya4360362007-01-05 16:36:45 -08001474}
1475
Avi Kivity4db35312007-11-21 15:28:32 +02001476static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001477{
Avi Kivity4db35312007-11-21 15:28:32 +02001478 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001479}
1480
Avi Kivity12b7d282007-09-23 14:10:49 +02001481static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1482{
1483 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001484 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001485
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001486 kvm_for_each_vcpu(i, vcpu, kvm)
1487 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001488}
1489
Avi Kivity31aa2b42008-07-11 17:59:46 +03001490static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001491{
1492 u64 *parent_pte;
1493
Avi Kivity4db35312007-11-21 15:28:32 +02001494 while (sp->multimapped || sp->parent_pte) {
1495 if (!sp->multimapped)
1496 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001497 else {
1498 struct kvm_pte_chain *chain;
1499
Avi Kivity4db35312007-11-21 15:28:32 +02001500 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001501 struct kvm_pte_chain, link);
1502 parent_pte = chain->parent_ptes[0];
1503 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001504 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001505 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001506 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001507 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001508}
1509
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001510static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001511 struct kvm_mmu_page *parent,
1512 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001513{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001514 int i, zapped = 0;
1515 struct mmu_page_path parents;
1516 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001517
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001518 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001519 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001520
1521 kvm_mmu_pages_init(parent, &parents, &pages);
1522 while (mmu_unsync_walk(parent, &pages)) {
1523 struct kvm_mmu_page *sp;
1524
1525 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001526 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001527 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001528 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001529 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001530 kvm_mmu_pages_init(parent, &parents, &pages);
1531 }
1532
1533 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001534}
1535
Xiao Guangrong77758342010-06-04 21:53:54 +08001536static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1537 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001538{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001539 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001540
Xiao Guangrong77758342010-06-04 21:53:54 +08001541 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001542 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001543 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001544 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001545 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001546 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001547 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001548 if (sp->unsync)
1549 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001550 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001551 /* Count self */
1552 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001553 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001554 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001555 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001556 kvm_reload_remote_mmus(kvm);
1557 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001558
1559 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001560 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001561 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001562}
1563
Xiao Guangrong77758342010-06-04 21:53:54 +08001564static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1565 struct list_head *invalid_list)
1566{
1567 struct kvm_mmu_page *sp;
1568
1569 if (list_empty(invalid_list))
1570 return;
1571
1572 kvm_flush_remote_tlbs(kvm);
1573
1574 do {
1575 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1576 WARN_ON(!sp->role.invalid || sp->root_count);
1577 kvm_mmu_free_page(kvm, sp);
1578 } while (!list_empty(invalid_list));
1579
1580}
1581
Izik Eidus82ce2c92007-10-02 18:52:55 +02001582/*
1583 * Changing the number of mmu pages allocated to the vm
1584 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1585 */
1586void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1587{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001588 int used_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001589 LIST_HEAD(invalid_list);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001590
1591 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1592 used_pages = max(0, used_pages);
1593
Izik Eidus82ce2c92007-10-02 18:52:55 +02001594 /*
1595 * If we set the number of mmu pages to be smaller be than the
1596 * number of actived pages , we must to free some mmu pages before we
1597 * change the value
1598 */
1599
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001600 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001601 while (used_pages > kvm_nr_mmu_pages &&
1602 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001603 struct kvm_mmu_page *page;
1604
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001605 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001606 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001607 used_pages -= kvm_mmu_prepare_zap_page(kvm, page,
1608 &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001609 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001610 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001611 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001612 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001613 }
1614 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001615 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1616 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001617
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001618 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001619}
1620
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001621static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001622{
Avi Kivity4db35312007-11-21 15:28:32 +02001623 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001624 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001625 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001626 int r;
1627
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001628 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001629 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001630
1631 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001632 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1633 sp->role.word);
1634 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001635 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001636 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001637 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001638 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001639}
1640
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001641static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001642{
Avi Kivity4db35312007-11-21 15:28:32 +02001643 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001644 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001645 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001646
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001647 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001648 pgprintk("%s: zap %lx %x\n",
1649 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001650 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001651 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001652 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001653}
1654
Avi Kivity38c335f2007-11-21 14:20:22 +02001655static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001657 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001658 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659
Sheng Yang291f26b2008-10-16 17:30:57 +08001660 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661}
1662
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001663static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1664{
1665 int i;
1666 u64 *pt = sp->spt;
1667
1668 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1669 return;
1670
1671 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1672 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001673 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001674 }
1675}
1676
Sheng Yang74be52e2008-10-09 16:01:56 +08001677/*
1678 * The function is based on mtrr_type_lookup() in
1679 * arch/x86/kernel/cpu/mtrr/generic.c
1680 */
1681static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1682 u64 start, u64 end)
1683{
1684 int i;
1685 u64 base, mask;
1686 u8 prev_match, curr_match;
1687 int num_var_ranges = KVM_NR_VAR_MTRR;
1688
1689 if (!mtrr_state->enabled)
1690 return 0xFF;
1691
1692 /* Make end inclusive end, instead of exclusive */
1693 end--;
1694
1695 /* Look in fixed ranges. Just return the type as per start */
1696 if (mtrr_state->have_fixed && (start < 0x100000)) {
1697 int idx;
1698
1699 if (start < 0x80000) {
1700 idx = 0;
1701 idx += (start >> 16);
1702 return mtrr_state->fixed_ranges[idx];
1703 } else if (start < 0xC0000) {
1704 idx = 1 * 8;
1705 idx += ((start - 0x80000) >> 14);
1706 return mtrr_state->fixed_ranges[idx];
1707 } else if (start < 0x1000000) {
1708 idx = 3 * 8;
1709 idx += ((start - 0xC0000) >> 12);
1710 return mtrr_state->fixed_ranges[idx];
1711 }
1712 }
1713
1714 /*
1715 * Look in variable ranges
1716 * Look of multiple ranges matching this address and pick type
1717 * as per MTRR precedence
1718 */
1719 if (!(mtrr_state->enabled & 2))
1720 return mtrr_state->def_type;
1721
1722 prev_match = 0xFF;
1723 for (i = 0; i < num_var_ranges; ++i) {
1724 unsigned short start_state, end_state;
1725
1726 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1727 continue;
1728
1729 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1730 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1731 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1732 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1733
1734 start_state = ((start & mask) == (base & mask));
1735 end_state = ((end & mask) == (base & mask));
1736 if (start_state != end_state)
1737 return 0xFE;
1738
1739 if ((start & mask) != (base & mask))
1740 continue;
1741
1742 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1743 if (prev_match == 0xFF) {
1744 prev_match = curr_match;
1745 continue;
1746 }
1747
1748 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1749 curr_match == MTRR_TYPE_UNCACHABLE)
1750 return MTRR_TYPE_UNCACHABLE;
1751
1752 if ((prev_match == MTRR_TYPE_WRBACK &&
1753 curr_match == MTRR_TYPE_WRTHROUGH) ||
1754 (prev_match == MTRR_TYPE_WRTHROUGH &&
1755 curr_match == MTRR_TYPE_WRBACK)) {
1756 prev_match = MTRR_TYPE_WRTHROUGH;
1757 curr_match = MTRR_TYPE_WRTHROUGH;
1758 }
1759
1760 if (prev_match != curr_match)
1761 return MTRR_TYPE_UNCACHABLE;
1762 }
1763
1764 if (prev_match != 0xFF)
1765 return prev_match;
1766
1767 return mtrr_state->def_type;
1768}
1769
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001770u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001771{
1772 u8 mtrr;
1773
1774 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1775 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1776 if (mtrr == 0xfe || mtrr == 0xff)
1777 mtrr = MTRR_TYPE_WRBACK;
1778 return mtrr;
1779}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001780EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001781
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001782static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001783{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001784 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001785 ++vcpu->kvm->stat.mmu_unsync;
1786 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001787
Xiao Guangrong6b184932010-04-16 21:29:17 +08001788 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001789 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001790}
1791
1792static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1793{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001794 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001795 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001796
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001797 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001798 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001799 continue;
1800 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1801 __kvm_unsync_page(vcpu, s);
1802 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001803}
1804
1805static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1806 bool can_unsync)
1807{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001808 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001809 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001810 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001811
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001812 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001813 if (!can_unsync)
1814 return 1;
1815
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001816 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001817 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001818
1819 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001820 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001821 return 1;
1822 need_unsync = true;
1823 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001824 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001825 if (need_unsync)
1826 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001827 return 0;
1828}
1829
Avi Kivityd555c332009-06-10 14:24:23 +03001830static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001831 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001832 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001833 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001834 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001835{
1836 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001837 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001838
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001839 /*
1840 * We don't set the accessed bit, since we sometimes want to see
1841 * whether the guest actually used the pte (in order to detect
1842 * demand paging).
1843 */
Sheng Yang7b523452008-04-25 21:13:50 +08001844 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001845 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001846 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001847 if (!dirty)
1848 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001849 if (pte_access & ACC_EXEC_MASK)
1850 spte |= shadow_x_mask;
1851 else
1852 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001853 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001854 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001855 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001856 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001857 if (tdp_enabled)
1858 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1859 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001860
Izik Eidus14032832009-09-23 21:47:17 +03001861 if (reset_host_protection)
1862 spte |= SPTE_HOST_WRITEABLE;
1863
Anthony Liguori35149e22008-04-02 14:46:56 -05001864 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001865
1866 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001867 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1868 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001869
Joerg Roedel852e3c12009-07-27 16:30:44 +02001870 if (level > PT_PAGE_TABLE_LEVEL &&
1871 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001872 ret = 1;
Xiao Guangrong6d742292010-05-13 10:07:00 +08001873 rmap_remove(vcpu->kvm, sptep);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001874 spte = shadow_trap_nonpresent_pte;
1875 goto set_pte;
1876 }
1877
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001878 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001879
Avi Kivity69325a12010-05-27 14:35:58 +03001880 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1881 spte &= ~PT_USER_MASK;
1882
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001883 /*
1884 * Optimization: for pte sync, if spte was writable the hash
1885 * lookup is unnecessary (and expensive). Write protection
1886 * is responsibility of mmu_get_page / kvm_sync_page.
1887 * Same reasoning can be applied to dirty page accounting.
1888 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001889 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001890 goto set_pte;
1891
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001892 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001893 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001894 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001895 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001896 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001897 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001898 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001899 }
1900 }
1901
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001902 if (pte_access & ACC_WRITE_MASK)
1903 mark_page_dirty(vcpu->kvm, gfn);
1904
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001905set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001906 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001907 return ret;
1908}
1909
Avi Kivityd555c332009-06-10 14:24:23 +03001910static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001911 unsigned pt_access, unsigned pte_access,
1912 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001913 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001914 pfn_t pfn, bool speculative,
1915 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001916{
1917 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001918 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001919 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001920
1921 pgprintk("%s: spte %llx access %x write_fault %d"
1922 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001923 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001924 write_fault, user_fault, gfn);
1925
Avi Kivityd555c332009-06-10 14:24:23 +03001926 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001927 /*
1928 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1929 * the parent of the now unreachable PTE.
1930 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001931 if (level > PT_PAGE_TABLE_LEVEL &&
1932 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001933 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001934 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001935
1936 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001937 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001938 __set_spte(sptep, shadow_trap_nonpresent_pte);
1939 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001940 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001941 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001942 spte_to_pfn(*sptep), pfn);
1943 rmap_remove(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08001944 __set_spte(sptep, shadow_trap_nonpresent_pte);
1945 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001946 } else
1947 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001948 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001949
Avi Kivityd555c332009-06-10 14:24:23 +03001950 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001951 dirty, level, gfn, pfn, speculative, true,
1952 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001953 if (write_fault)
1954 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08001955 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001956 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001957
Avi Kivityd555c332009-06-10 14:24:23 +03001958 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001959 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001960 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001961 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1962 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001963 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001964 ++vcpu->kvm->stat.lpages;
1965
Avi Kivityd555c332009-06-10 14:24:23 +03001966 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001967 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001968 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001969 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001970 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001971 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001972 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001973 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001974 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001975 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001976 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001977 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001978 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001979 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001980 vcpu->arch.last_pte_gfn = gfn;
1981 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001982}
1983
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1985{
1986}
1987
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001988static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001989 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990{
Avi Kivity9f652d22008-12-25 14:54:25 +02001991 struct kvm_shadow_walk_iterator iterator;
1992 struct kvm_mmu_page *sp;
1993 int pt_write = 0;
1994 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995
Avi Kivity9f652d22008-12-25 14:54:25 +02001996 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001997 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001998 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1999 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002000 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02002001 ++vcpu->stat.pf_fixed;
2002 break;
2003 }
2004
2005 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002006 u64 base_addr = iterator.addr;
2007
2008 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2009 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002010 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2011 iterator.level - 1,
2012 1, ACC_ALL, iterator.sptep);
2013 if (!sp) {
2014 pgprintk("nonpaging_map: ENOMEM\n");
2015 kvm_release_pfn_clean(pfn);
2016 return -ENOMEM;
2017 }
2018
Avi Kivityd555c332009-06-10 14:24:23 +03002019 __set_spte(iterator.sptep,
2020 __pa(sp->spt)
2021 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2022 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002023 }
2024 }
2025 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026}
2027
Huang Yingbf998152010-05-31 14:28:19 +08002028static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2029{
2030 char buf[1];
2031 void __user *hva;
2032 int r;
2033
2034 /* Touch the page, so send SIGBUS */
2035 hva = (void __user *)gfn_to_hva(kvm, gfn);
2036 r = copy_from_user(buf, hva, 1);
2037}
2038
2039static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2040{
2041 kvm_release_pfn_clean(pfn);
2042 if (is_hwpoison_pfn(pfn)) {
2043 kvm_send_hwpoison_signal(kvm, gfn);
2044 return 0;
2045 }
2046 return 1;
2047}
2048
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002049static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2050{
2051 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002052 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002053 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002054 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002055
Joerg Roedel852e3c12009-07-27 16:30:44 +02002056 level = mapping_level(vcpu, gfn);
2057
2058 /*
2059 * This path builds a PAE pagetable - so we can map 2mb pages at
2060 * maximum. Therefore check if the level is larger than that.
2061 */
2062 if (level > PT_DIRECTORY_LEVEL)
2063 level = PT_DIRECTORY_LEVEL;
2064
2065 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002066
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002067 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002068 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002069 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002070
Avi Kivityd196e342008-01-24 11:44:11 +02002071 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002072 if (is_error_pfn(pfn))
2073 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002074
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002075 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002076 if (mmu_notifier_retry(vcpu, mmu_seq))
2077 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002078 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002079 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002080 spin_unlock(&vcpu->kvm->mmu_lock);
2081
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002082
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002083 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002084
2085out_unlock:
2086 spin_unlock(&vcpu->kvm->mmu_lock);
2087 kvm_release_pfn_clean(pfn);
2088 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002089}
2090
2091
Avi Kivity17ac10a2007-01-05 16:36:40 -08002092static void mmu_free_roots(struct kvm_vcpu *vcpu)
2093{
2094 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002095 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002096 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002097
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002098 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002099 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002100 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002101 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2102 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002103
Avi Kivity4db35312007-11-21 15:28:32 +02002104 sp = page_header(root);
2105 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002106 if (!sp->root_count && sp->role.invalid) {
2107 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2108 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2109 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002111 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002112 return;
2113 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002114 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002115 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002116
Avi Kivity417726a2007-04-12 17:35:58 +03002117 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002118 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002119 sp = page_header(root);
2120 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002121 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002122 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2123 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002124 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002125 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002126 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002127 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002128 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002129 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002130}
2131
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002132static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2133{
2134 int ret = 0;
2135
2136 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002137 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002138 ret = 1;
2139 }
2140
2141 return ret;
2142}
2143
2144static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002145{
2146 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002147 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002148 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002149 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002150 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002151
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002152 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002153
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002154 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2155 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002156
2157 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002158 if (mmu_check_root(vcpu, root_gfn))
2159 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002160 if (tdp_enabled) {
2161 direct = 1;
2162 root_gfn = 0;
2163 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002164 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002165 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002166 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002167 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002168 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002169 root = __pa(sp->spt);
2170 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002171 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002172 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002173 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002174 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002175 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002176 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002177 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002178
2179 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002180 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002181 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002182 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002183 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002184 continue;
2185 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002186 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002187 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002188 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002189 if (mmu_check_root(vcpu, root_gfn))
2190 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002191 if (tdp_enabled) {
2192 direct = 1;
2193 root_gfn = i << 30;
2194 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002195 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002196 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002197 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002198 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002199 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002200 root = __pa(sp->spt);
2201 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002202 spin_unlock(&vcpu->kvm->mmu_lock);
2203
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002204 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002205 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002206 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002207 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002208}
2209
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002210static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2211{
2212 int i;
2213 struct kvm_mmu_page *sp;
2214
2215 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2216 return;
2217 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2218 hpa_t root = vcpu->arch.mmu.root_hpa;
2219 sp = page_header(root);
2220 mmu_sync_children(vcpu, sp);
2221 return;
2222 }
2223 for (i = 0; i < 4; ++i) {
2224 hpa_t root = vcpu->arch.mmu.pae_root[i];
2225
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002226 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002227 root &= PT64_BASE_ADDR_MASK;
2228 sp = page_header(root);
2229 mmu_sync_children(vcpu, sp);
2230 }
2231 }
2232}
2233
2234void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2235{
2236 spin_lock(&vcpu->kvm->mmu_lock);
2237 mmu_sync_roots(vcpu);
2238 spin_unlock(&vcpu->kvm->mmu_lock);
2239}
2240
Gleb Natapov1871c602010-02-10 14:21:32 +02002241static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2242 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243{
Gleb Natapov1871c602010-02-10 14:21:32 +02002244 if (error)
2245 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 return vaddr;
2247}
2248
2249static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002250 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251{
Avi Kivitye8332402007-12-09 18:43:00 +02002252 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002253 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002255 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002256 r = mmu_topup_memory_caches(vcpu);
2257 if (r)
2258 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002259
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002261 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262
Avi Kivitye8332402007-12-09 18:43:00 +02002263 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264
Avi Kivitye8332402007-12-09 18:43:00 +02002265 return nonpaging_map(vcpu, gva & PAGE_MASK,
2266 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267}
2268
Joerg Roedelfb72d162008-02-07 13:47:44 +01002269static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2270 u32 error_code)
2271{
Anthony Liguori35149e22008-04-02 14:46:56 -05002272 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002273 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002274 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002275 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002276 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002277
2278 ASSERT(vcpu);
2279 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2280
2281 r = mmu_topup_memory_caches(vcpu);
2282 if (r)
2283 return r;
2284
Joerg Roedel852e3c12009-07-27 16:30:44 +02002285 level = mapping_level(vcpu, gfn);
2286
2287 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2288
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002289 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002290 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002291 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002292 if (is_error_pfn(pfn))
2293 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002294 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002295 if (mmu_notifier_retry(vcpu, mmu_seq))
2296 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002297 kvm_mmu_free_some_pages(vcpu);
2298 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002299 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002300 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002301
2302 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002303
2304out_unlock:
2305 spin_unlock(&vcpu->kvm->mmu_lock);
2306 kvm_release_pfn_clean(pfn);
2307 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002308}
2309
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310static void nonpaging_free(struct kvm_vcpu *vcpu)
2311{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002312 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313}
2314
2315static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2316{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002317 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318
2319 context->new_cr3 = nonpaging_new_cr3;
2320 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 context->gva_to_gpa = nonpaging_gva_to_gpa;
2322 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002323 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002324 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002325 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002326 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002328 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 return 0;
2330}
2331
Avi Kivityd835dfe2007-11-21 02:57:59 +02002332void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002334 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002335 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336}
2337
2338static void paging_new_cr3(struct kvm_vcpu *vcpu)
2339{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002340 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002341 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342}
2343
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344static void inject_page_fault(struct kvm_vcpu *vcpu,
2345 u64 addr,
2346 u32 err_code)
2347{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002348 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349}
2350
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351static void paging_free(struct kvm_vcpu *vcpu)
2352{
2353 nonpaging_free(vcpu);
2354}
2355
Dong, Eddie82725b22009-03-30 16:21:08 +08002356static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2357{
2358 int bit7;
2359
2360 bit7 = (gpte >> 7) & 1;
2361 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2362}
2363
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364#define PTTYPE 64
2365#include "paging_tmpl.h"
2366#undef PTTYPE
2367
2368#define PTTYPE 32
2369#include "paging_tmpl.h"
2370#undef PTTYPE
2371
Dong, Eddie82725b22009-03-30 16:21:08 +08002372static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2373{
2374 struct kvm_mmu *context = &vcpu->arch.mmu;
2375 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2376 u64 exb_bit_rsvd = 0;
2377
2378 if (!is_nx(vcpu))
2379 exb_bit_rsvd = rsvd_bits(63, 63);
2380 switch (level) {
2381 case PT32_ROOT_LEVEL:
2382 /* no rsvd bits for 2 level 4K page table entries */
2383 context->rsvd_bits_mask[0][1] = 0;
2384 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002385 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2386
2387 if (!is_pse(vcpu)) {
2388 context->rsvd_bits_mask[1][1] = 0;
2389 break;
2390 }
2391
Dong, Eddie82725b22009-03-30 16:21:08 +08002392 if (is_cpuid_PSE36())
2393 /* 36bits PSE 4MB page */
2394 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2395 else
2396 /* 32 bits PSE 4MB page */
2397 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002398 break;
2399 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002400 context->rsvd_bits_mask[0][2] =
2401 rsvd_bits(maxphyaddr, 63) |
2402 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002403 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002404 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002405 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2406 rsvd_bits(maxphyaddr, 62); /* PTE */
2407 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2408 rsvd_bits(maxphyaddr, 62) |
2409 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002410 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002411 break;
2412 case PT64_ROOT_LEVEL:
2413 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2414 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2415 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2416 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2417 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002418 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002419 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2420 rsvd_bits(maxphyaddr, 51);
2421 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002422 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2423 rsvd_bits(maxphyaddr, 51) |
2424 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002425 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002426 rsvd_bits(maxphyaddr, 51) |
2427 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002428 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002429 break;
2430 }
2431}
2432
Avi Kivity17ac10a2007-01-05 16:36:40 -08002433static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436
2437 ASSERT(is_pae(vcpu));
2438 context->new_cr3 = paging_new_cr3;
2439 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002441 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002442 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002443 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002445 context->root_level = level;
2446 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002447 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448 return 0;
2449}
2450
Avi Kivity17ac10a2007-01-05 16:36:40 -08002451static int paging64_init_context(struct kvm_vcpu *vcpu)
2452{
Dong, Eddie82725b22009-03-30 16:21:08 +08002453 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002454 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2455}
2456
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457static int paging32_init_context(struct kvm_vcpu *vcpu)
2458{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002459 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460
Dong, Eddie82725b22009-03-30 16:21:08 +08002461 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 context->new_cr3 = paging_new_cr3;
2463 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 context->gva_to_gpa = paging32_gva_to_gpa;
2465 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002466 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002467 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002468 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 context->root_level = PT32_ROOT_LEVEL;
2470 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002471 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 return 0;
2473}
2474
2475static int paging32E_init_context(struct kvm_vcpu *vcpu)
2476{
Dong, Eddie82725b22009-03-30 16:21:08 +08002477 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002478 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479}
2480
Joerg Roedelfb72d162008-02-07 13:47:44 +01002481static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2482{
2483 struct kvm_mmu *context = &vcpu->arch.mmu;
2484
2485 context->new_cr3 = nonpaging_new_cr3;
2486 context->page_fault = tdp_page_fault;
2487 context->free = nonpaging_free;
2488 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002489 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002490 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002491 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002492 context->root_hpa = INVALID_PAGE;
2493
2494 if (!is_paging(vcpu)) {
2495 context->gva_to_gpa = nonpaging_gva_to_gpa;
2496 context->root_level = 0;
2497 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002498 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002499 context->gva_to_gpa = paging64_gva_to_gpa;
2500 context->root_level = PT64_ROOT_LEVEL;
2501 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002502 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002503 context->gva_to_gpa = paging64_gva_to_gpa;
2504 context->root_level = PT32E_ROOT_LEVEL;
2505 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002506 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002507 context->gva_to_gpa = paging32_gva_to_gpa;
2508 context->root_level = PT32_ROOT_LEVEL;
2509 }
2510
2511 return 0;
2512}
2513
2514static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002516 int r;
2517
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002519 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520
2521 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002522 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002523 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002524 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002526 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002528 r = paging32_init_context(vcpu);
2529
Avi Kivity5b7e0102010-04-14 19:20:03 +03002530 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002531 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002532
2533 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534}
2535
Joerg Roedelfb72d162008-02-07 13:47:44 +01002536static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2537{
Anthony Liguori35149e22008-04-02 14:46:56 -05002538 vcpu->arch.update_pte.pfn = bad_pfn;
2539
Joerg Roedelfb72d162008-02-07 13:47:44 +01002540 if (tdp_enabled)
2541 return init_kvm_tdp_mmu(vcpu);
2542 else
2543 return init_kvm_softmmu(vcpu);
2544}
2545
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2547{
2548 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002549 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2550 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002551 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552}
2553
2554int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2555{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002556 destroy_kvm_mmu(vcpu);
2557 return init_kvm_mmu(vcpu);
2558}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002559EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002560
2561int kvm_mmu_load(struct kvm_vcpu *vcpu)
2562{
Avi Kivity714b93d2007-01-05 16:36:53 -08002563 int r;
2564
Avi Kivitye2dec932007-01-05 16:36:54 -08002565 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002566 if (r)
2567 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002568 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002569 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002570 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002571 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002572 if (r)
2573 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002574 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002575 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002576out:
2577 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002579EXPORT_SYMBOL_GPL(kvm_mmu_load);
2580
2581void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2582{
2583 mmu_free_roots(vcpu);
2584}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585
Avi Kivity09072da2007-05-01 14:16:52 +03002586static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002587 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002588 u64 *spte)
2589{
2590 u64 pte;
2591 struct kvm_mmu_page *child;
2592
2593 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002594 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002595 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002596 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002597 else {
2598 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002599 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002600 }
2601 }
Avi Kivityd555c332009-06-10 14:24:23 +03002602 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002603 if (is_large_pte(pte))
2604 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002605}
2606
Avi Kivity00284252007-05-01 16:53:31 +03002607static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002608 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002609 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002610 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002611{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002612 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002613 ++vcpu->kvm->stat.mmu_pde_zapped;
2614 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002615 }
Avi Kivity00284252007-05-01 16:53:31 +03002616
Avi Kivity4cee5762007-11-18 16:37:07 +02002617 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002618 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002619 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002620 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002621 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002622}
2623
Avi Kivity79539ce2007-11-21 02:06:21 +02002624static bool need_remote_flush(u64 old, u64 new)
2625{
2626 if (!is_shadow_present_pte(old))
2627 return false;
2628 if (!is_shadow_present_pte(new))
2629 return true;
2630 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2631 return true;
2632 old ^= PT64_NX_MASK;
2633 new ^= PT64_NX_MASK;
2634 return (old & ~new & PT64_PERM_MASK) != 0;
2635}
2636
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002637static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2638 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002639{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002640 if (zap_page)
2641 return;
2642
2643 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002644 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002645 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002646 kvm_mmu_flush_tlb(vcpu);
2647}
2648
Avi Kivity12b7d282007-09-23 14:10:49 +02002649static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2650{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002651 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002652
Sheng Yang7b523452008-04-25 21:13:50 +08002653 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002654}
2655
Avi Kivityd7824ff2007-12-30 12:29:05 +02002656static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002657 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002658{
2659 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002660 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002661
Avi Kivity43a37952009-06-10 14:12:05 +03002662 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002663 return;
2664 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002665
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002666 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002667 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002668 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002669
Anthony Liguori35149e22008-04-02 14:46:56 -05002670 if (is_error_pfn(pfn)) {
2671 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002672 return;
2673 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002674 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002675 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002676}
2677
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002678static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2679{
2680 u64 *spte = vcpu->arch.last_pte_updated;
2681
2682 if (spte
2683 && vcpu->arch.last_pte_gfn == gfn
2684 && shadow_accessed_mask
2685 && !(*spte & shadow_accessed_mask)
2686 && is_shadow_present_pte(*spte))
2687 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2688}
2689
Avi Kivity09072da2007-05-01 14:16:52 +03002690void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002691 const u8 *new, int bytes,
2692 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002693{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002694 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002695 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002696 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002697 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002698 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002699 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002700 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002701 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002702 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002703 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002704 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002705 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002706 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002707 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002708 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002709 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002710 bool remote_flush, local_flush, zap_page;
2711
2712 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002713
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002714 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002715
Avi Kivity08e850c2010-03-15 13:59:57 +02002716 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2717
2718 /*
2719 * Assume that the pte write on a page table of the same type
2720 * as the current vcpu paging mode. This is nearly always true
2721 * (might be false while changing modes). Note it is verified later
2722 * by update_pte().
2723 */
2724 if ((is_pae(vcpu) && bytes == 4) || !new) {
2725 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2726 if (is_pae(vcpu)) {
2727 gpa &= ~(gpa_t)7;
2728 bytes = 8;
2729 }
2730 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2731 if (r)
2732 gentry = 0;
2733 new = (const u8 *)&gentry;
2734 }
2735
Avi Kivity72016f32010-03-15 13:59:53 +02002736 switch (bytes) {
2737 case 4:
2738 gentry = *(const u32 *)new;
2739 break;
2740 case 8:
2741 gentry = *(const u64 *)new;
2742 break;
2743 default:
2744 gentry = 0;
2745 break;
2746 }
2747
Avi Kivity72016f32010-03-15 13:59:53 +02002748 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002749 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002750 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2751 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002752 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002753 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002754 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002755 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002756 if (guest_initiated) {
2757 if (gfn == vcpu->arch.last_pt_write_gfn
2758 && !last_updated_pte_accessed(vcpu)) {
2759 ++vcpu->arch.last_pt_write_count;
2760 if (vcpu->arch.last_pt_write_count >= 3)
2761 flooded = 1;
2762 } else {
2763 vcpu->arch.last_pt_write_gfn = gfn;
2764 vcpu->arch.last_pt_write_count = 1;
2765 vcpu->arch.last_pte_updated = NULL;
2766 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002767 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002768
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002769 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002770 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002771 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002772 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002773 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002774 /*
2775 * Misaligned accesses are too much trouble to fix
2776 * up; also, they usually indicate a page is not used
2777 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002778 *
2779 * If we're seeing too many writes to a page,
2780 * it may no longer be a page table, or we may be
2781 * forking, in which case it is better to unmap the
2782 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002783 */
2784 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002785 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002786 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002787 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002788 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002789 continue;
2790 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002791 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002792 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002793 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002794 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002795 page_offset <<= 1; /* 32->64 */
2796 /*
2797 * A 32-bit pde maps 4MB while the shadow pdes map
2798 * only 2MB. So we need to double the offset again
2799 * and zap two pdes instead of one.
2800 */
2801 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002802 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002803 page_offset <<= 1;
2804 npte = 2;
2805 }
Avi Kivityfce06572007-05-01 16:44:05 +03002806 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002807 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002808 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002809 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002810 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002811 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02002812 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002813 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002814 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002815 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002816 if (gentry)
2817 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002818 if (!remote_flush && need_remote_flush(entry, *spte))
2819 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02002820 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002821 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002822 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002823 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002824 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityc7addb92007-09-16 18:58:32 +02002825 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002826 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002827 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2828 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2829 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002830 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002831}
2832
Avi Kivitya4360362007-01-05 16:36:45 -08002833int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2834{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002835 gpa_t gpa;
2836 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002837
Avi Kivity60f24782009-08-27 13:37:06 +03002838 if (tdp_enabled)
2839 return 0;
2840
Gleb Natapov1871c602010-02-10 14:21:32 +02002841 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002842
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002843 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002844 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002845 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002846 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002847}
Avi Kivity577bdc42008-07-19 08:57:05 +03002848EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002849
Avi Kivity22d95b12007-09-14 20:26:06 +03002850void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002851{
Xiao Guangrong103ad252010-06-04 21:54:38 +08002852 int free_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002853 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08002854
2855 free_pages = vcpu->kvm->arch.n_free_mmu_pages;
2856 while (free_pages < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03002857 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002858 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002859
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002860 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002861 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002862 free_pages += kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2863 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002864 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002865 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002866 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08002867}
Avi Kivityebeace82007-01-05 16:36:47 -08002868
Avi Kivity30677142007-10-28 18:48:59 +02002869int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2870{
2871 int r;
2872 enum emulation_result er;
2873
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002874 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002875 if (r < 0)
2876 goto out;
2877
2878 if (!r) {
2879 r = 1;
2880 goto out;
2881 }
2882
Avi Kivityb733bfb2007-10-28 18:52:05 +02002883 r = mmu_topup_memory_caches(vcpu);
2884 if (r)
2885 goto out;
2886
Avi Kivity851ba692009-08-24 11:10:17 +03002887 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002888
2889 switch (er) {
2890 case EMULATE_DONE:
2891 return 1;
2892 case EMULATE_DO_MMIO:
2893 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002894 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02002895 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002896 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002897 default:
2898 BUG();
2899 }
2900out:
Avi Kivity30677142007-10-28 18:48:59 +02002901 return r;
2902}
2903EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2904
Marcelo Tosattia7052892008-09-23 13:18:35 -03002905void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2906{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002907 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002908 kvm_mmu_flush_tlb(vcpu);
2909 ++vcpu->stat.invlpg;
2910}
2911EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2912
Joerg Roedel18552672008-02-07 13:47:41 +01002913void kvm_enable_tdp(void)
2914{
2915 tdp_enabled = true;
2916}
2917EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2918
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002919void kvm_disable_tdp(void)
2920{
2921 tdp_enabled = false;
2922}
2923EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2924
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925static void free_mmu_pages(struct kvm_vcpu *vcpu)
2926{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002927 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928}
2929
2930static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2931{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002932 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 int i;
2934
2935 ASSERT(vcpu);
2936
Avi Kivity17ac10a2007-01-05 16:36:40 -08002937 /*
2938 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2939 * Therefore we need to allocate shadow page tables in the first
2940 * 4GB of memory, which happens to fit the DMA32 zone.
2941 */
2942 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2943 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002944 return -ENOMEM;
2945
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002946 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002947 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002948 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002949
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951}
2952
Ingo Molnar8018c272006-12-29 16:50:01 -08002953int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002956 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957
Ingo Molnar8018c272006-12-29 16:50:01 -08002958 return alloc_mmu_pages(vcpu);
2959}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960
Ingo Molnar8018c272006-12-29 16:50:01 -08002961int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2962{
2963 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002964 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002965
Ingo Molnar8018c272006-12-29 16:50:01 -08002966 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967}
2968
2969void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2970{
2971 ASSERT(vcpu);
2972
2973 destroy_kvm_mmu(vcpu);
2974 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002975 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976}
2977
Avi Kivity90cb0522007-07-17 13:04:56 +03002978void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979{
Avi Kivity4db35312007-11-21 15:28:32 +02002980 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002982 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 int i;
2984 u64 *pt;
2985
Sheng Yang291f26b2008-10-16 17:30:57 +08002986 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987 continue;
2988
Avi Kivity4db35312007-11-21 15:28:32 +02002989 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2991 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08002992 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 }
Avi Kivity171d5952008-08-27 16:40:51 +03002995 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002997
Avi Kivity90cb0522007-07-17 13:04:56 +03002998void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002999{
Avi Kivity4db35312007-11-21 15:28:32 +02003000 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003001 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003002
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003003 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003004restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003005 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003006 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003007 goto restart;
3008
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003009 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003010 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003011}
3012
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003013static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3014 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003015{
3016 struct kvm_mmu_page *page;
3017
3018 page = container_of(kvm->arch.active_mmu_pages.prev,
3019 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003020 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003021}
3022
Dave Chinner7f8275d2010-07-19 14:56:17 +10003023static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003024{
3025 struct kvm *kvm;
3026 struct kvm *kvm_freed = NULL;
3027 int cache_count = 0;
3028
3029 spin_lock(&kvm_lock);
3030
3031 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003032 int npages, idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003033 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003034
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003035 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003036 spin_lock(&kvm->mmu_lock);
3037 npages = kvm->arch.n_alloc_mmu_pages -
3038 kvm->arch.n_free_mmu_pages;
3039 cache_count += npages;
3040 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003041 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3042 &invalid_list);
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003043 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003044 kvm_freed = kvm;
3045 }
3046 nr_to_scan--;
3047
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003048 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003049 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003050 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003051 }
3052 if (kvm_freed)
3053 list_move_tail(&kvm_freed->vm_list, &vm_list);
3054
3055 spin_unlock(&kvm_lock);
3056
3057 return cache_count;
3058}
3059
3060static struct shrinker mmu_shrinker = {
3061 .shrink = mmu_shrink,
3062 .seeks = DEFAULT_SEEKS * 10,
3063};
3064
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003065static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003066{
3067 if (pte_chain_cache)
3068 kmem_cache_destroy(pte_chain_cache);
3069 if (rmap_desc_cache)
3070 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003071 if (mmu_page_header_cache)
3072 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003073}
3074
Izik Eidus3ee16c82008-03-30 15:17:21 +03003075void kvm_mmu_module_exit(void)
3076{
3077 mmu_destroy_caches();
3078 unregister_shrinker(&mmu_shrinker);
3079}
3080
Avi Kivityb5a33a72007-04-15 16:31:09 +03003081int kvm_mmu_module_init(void)
3082{
3083 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3084 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003085 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003086 if (!pte_chain_cache)
3087 goto nomem;
3088 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3089 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003090 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003091 if (!rmap_desc_cache)
3092 goto nomem;
3093
Avi Kivityd3d25b02007-05-30 12:34:53 +03003094 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3095 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003096 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003097 if (!mmu_page_header_cache)
3098 goto nomem;
3099
Izik Eidus3ee16c82008-03-30 15:17:21 +03003100 register_shrinker(&mmu_shrinker);
3101
Avi Kivityb5a33a72007-04-15 16:31:09 +03003102 return 0;
3103
3104nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003105 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003106 return -ENOMEM;
3107}
3108
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003109/*
3110 * Caculate mmu pages needed for kvm.
3111 */
3112unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3113{
3114 int i;
3115 unsigned int nr_mmu_pages;
3116 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003117 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003118
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003119 slots = kvm_memslots(kvm);
3120
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003121 for (i = 0; i < slots->nmemslots; i++)
3122 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003123
3124 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3125 nr_mmu_pages = max(nr_mmu_pages,
3126 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3127
3128 return nr_mmu_pages;
3129}
3130
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003131static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3132 unsigned len)
3133{
3134 if (len > buffer->len)
3135 return NULL;
3136 return buffer->ptr;
3137}
3138
3139static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3140 unsigned len)
3141{
3142 void *ret;
3143
3144 ret = pv_mmu_peek_buffer(buffer, len);
3145 if (!ret)
3146 return ret;
3147 buffer->ptr += len;
3148 buffer->len -= len;
3149 buffer->processed += len;
3150 return ret;
3151}
3152
3153static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3154 gpa_t addr, gpa_t value)
3155{
3156 int bytes = 8;
3157 int r;
3158
3159 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3160 bytes = 4;
3161
3162 r = mmu_topup_memory_caches(vcpu);
3163 if (r)
3164 return r;
3165
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003166 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003167 return -EFAULT;
3168
3169 return 1;
3170}
3171
3172static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3173{
Avi Kivity23902182010-06-10 17:02:16 +03003174 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003175 return 1;
3176}
3177
3178static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3179{
3180 spin_lock(&vcpu->kvm->mmu_lock);
3181 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3182 spin_unlock(&vcpu->kvm->mmu_lock);
3183 return 1;
3184}
3185
3186static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3187 struct kvm_pv_mmu_op_buffer *buffer)
3188{
3189 struct kvm_mmu_op_header *header;
3190
3191 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3192 if (!header)
3193 return 0;
3194 switch (header->op) {
3195 case KVM_MMU_OP_WRITE_PTE: {
3196 struct kvm_mmu_op_write_pte *wpte;
3197
3198 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3199 if (!wpte)
3200 return 0;
3201 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3202 wpte->pte_val);
3203 }
3204 case KVM_MMU_OP_FLUSH_TLB: {
3205 struct kvm_mmu_op_flush_tlb *ftlb;
3206
3207 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3208 if (!ftlb)
3209 return 0;
3210 return kvm_pv_mmu_flush_tlb(vcpu);
3211 }
3212 case KVM_MMU_OP_RELEASE_PT: {
3213 struct kvm_mmu_op_release_pt *rpt;
3214
3215 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3216 if (!rpt)
3217 return 0;
3218 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3219 }
3220 default: return 0;
3221 }
3222}
3223
3224int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3225 gpa_t addr, unsigned long *ret)
3226{
3227 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003228 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003229
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003230 buffer->ptr = buffer->buf;
3231 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3232 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003233
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003234 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003235 if (r)
3236 goto out;
3237
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003238 while (buffer->len) {
3239 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003240 if (r < 0)
3241 goto out;
3242 if (r == 0)
3243 break;
3244 }
3245
3246 r = 1;
3247out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003248 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003249 return r;
3250}
3251
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003252int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3253{
3254 struct kvm_shadow_walk_iterator iterator;
3255 int nr_sptes = 0;
3256
3257 spin_lock(&vcpu->kvm->mmu_lock);
3258 for_each_shadow_entry(vcpu, addr, iterator) {
3259 sptes[iterator.level-1] = *iterator.sptep;
3260 nr_sptes++;
3261 if (!is_shadow_present_pte(*iterator.sptep))
3262 break;
3263 }
3264 spin_unlock(&vcpu->kvm->mmu_lock);
3265
3266 return nr_sptes;
3267}
3268EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3269
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003270#ifdef AUDIT
3271
3272static const char *audit_msg;
3273
3274static gva_t canonicalize(gva_t gva)
3275{
3276#ifdef CONFIG_X86_64
3277 gva = (long long)(gva << 16) >> 16;
3278#endif
3279 return gva;
3280}
3281
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003282
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003283typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003284
3285static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3286 inspect_spte_fn fn)
3287{
3288 int i;
3289
3290 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3291 u64 ent = sp->spt[i];
3292
3293 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003294 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003295 struct kvm_mmu_page *child;
3296 child = page_header(ent & PT64_BASE_ADDR_MASK);
3297 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003298 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003299 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003300 }
3301 }
3302}
3303
3304static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3305{
3306 int i;
3307 struct kvm_mmu_page *sp;
3308
3309 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3310 return;
3311 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3312 hpa_t root = vcpu->arch.mmu.root_hpa;
3313 sp = page_header(root);
3314 __mmu_spte_walk(vcpu->kvm, sp, fn);
3315 return;
3316 }
3317 for (i = 0; i < 4; ++i) {
3318 hpa_t root = vcpu->arch.mmu.pae_root[i];
3319
3320 if (root && VALID_PAGE(root)) {
3321 root &= PT64_BASE_ADDR_MASK;
3322 sp = page_header(root);
3323 __mmu_spte_walk(vcpu->kvm, sp, fn);
3324 }
3325 }
3326 return;
3327}
3328
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003329static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3330 gva_t va, int level)
3331{
3332 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3333 int i;
3334 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3335
3336 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3337 u64 ent = pt[i];
3338
Avi Kivityc7addb92007-09-16 18:58:32 +02003339 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003340 continue;
3341
3342 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003343 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3344 audit_mappings_page(vcpu, ent, va, level - 1);
3345 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003346 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003347 gfn_t gfn = gpa >> PAGE_SHIFT;
3348 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3349 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003350
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003351 if (is_error_pfn(pfn)) {
3352 kvm_release_pfn_clean(pfn);
3353 continue;
3354 }
3355
Avi Kivityc7addb92007-09-16 18:58:32 +02003356 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003357 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003358 printk(KERN_ERR "xx audit error: (%s) levels %d"
3359 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003360 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003361 va, gpa, hpa, ent,
3362 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003363 else if (ent == shadow_notrap_nonpresent_pte
3364 && !is_error_hpa(hpa))
3365 printk(KERN_ERR "audit: (%s) notrap shadow,"
3366 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003367 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003368
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369 }
3370 }
3371}
3372
3373static void audit_mappings(struct kvm_vcpu *vcpu)
3374{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003375 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003376
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003377 if (vcpu->arch.mmu.root_level == 4)
3378 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003379 else
3380 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003381 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003382 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003383 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003384 i << 30,
3385 2);
3386}
3387
3388static int count_rmaps(struct kvm_vcpu *vcpu)
3389{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003390 struct kvm *kvm = vcpu->kvm;
3391 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003392 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003393 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003394
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003395 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003396 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003397 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003398 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003399 struct kvm_rmap_desc *d;
3400
3401 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003402 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003403
Izik Eidus290fc382007-09-27 14:11:22 +02003404 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003405 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003406 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003407 ++nmaps;
3408 continue;
3409 }
Izik Eidus290fc382007-09-27 14:11:22 +02003410 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003411 while (d) {
3412 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003413 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003414 ++nmaps;
3415 else
3416 break;
3417 d = d->more;
3418 }
3419 }
3420 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003421 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003422 return nmaps;
3423}
3424
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003425void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003426{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003427 unsigned long *rmapp;
3428 struct kvm_mmu_page *rev_sp;
3429 gfn_t gfn;
3430
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003431 if (is_writable_pte(*sptep)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003432 rev_sp = page_header(__pa(sptep));
Lai Jiangshan2032a932010-05-26 16:49:59 +08003433 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003434
3435 if (!gfn_to_memslot(kvm, gfn)) {
3436 if (!printk_ratelimit())
3437 return;
3438 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3439 audit_msg, gfn);
3440 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003441 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003442 rev_sp->gfn);
3443 dump_stack();
3444 return;
3445 }
3446
Lai Jiangshan2032a932010-05-26 16:49:59 +08003447 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003448 if (!*rmapp) {
3449 if (!printk_ratelimit())
3450 return;
3451 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3452 audit_msg, *sptep);
3453 dump_stack();
3454 }
3455 }
3456
3457}
3458
3459void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3460{
3461 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3462}
3463
3464static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3465{
Avi Kivity4db35312007-11-21 15:28:32 +02003466 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003467 int i;
3468
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003469 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003470 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003471
Avi Kivity4db35312007-11-21 15:28:32 +02003472 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003473 continue;
3474
3475 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3476 u64 ent = pt[i];
3477
3478 if (!(ent & PT_PRESENT_MASK))
3479 continue;
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003480 if (!is_writable_pte(ent))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003481 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003482 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003483 }
3484 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003485 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003486}
3487
3488static void audit_rmap(struct kvm_vcpu *vcpu)
3489{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003490 check_writable_mappings_rmap(vcpu);
3491 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003492}
3493
3494static void audit_write_protection(struct kvm_vcpu *vcpu)
3495{
Avi Kivity4db35312007-11-21 15:28:32 +02003496 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003497 struct kvm_memory_slot *slot;
3498 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003499 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003500 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003501
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003502 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003503 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003504 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003505 if (sp->unsync)
3506 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003507
Avi Kivitya1f4d3952010-06-21 11:44:20 +03003508 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003509 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003510
3511 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3512 while (spte) {
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003513 if (is_writable_pte(*spte))
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003514 printk(KERN_ERR "%s: (%s) shadow page has "
3515 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003516 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003517 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003518 spte = rmap_next(vcpu->kvm, rmapp, spte);
3519 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003520 }
3521}
3522
3523static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3524{
3525 int olddbg = dbg;
3526
3527 dbg = 0;
3528 audit_msg = msg;
3529 audit_rmap(vcpu);
3530 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003531 if (strcmp("pre pte write", audit_msg) != 0)
3532 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003533 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003534 dbg = olddbg;
3535}
3536
3537#endif