blob: c5501bc101068401cc7066d2933c9a14135b0010 [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 Kivityf6e2c02b2009-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 Kivityf6e2c02b2009-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) {
1381 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1382 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 Kivityf6e2c02b2009-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 Kivityf6e2c02b2009-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 Guangrong9cf5cf52010-05-24 15:40:07 +08001813 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001814 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001815
1816 if (!need_unsync && !s->unsync) {
1817 if (!can_unsync || !oos_shadow)
1818 return 1;
1819 need_unsync = true;
1820 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001821 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001822 if (need_unsync)
1823 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001824 return 0;
1825}
1826
Avi Kivityd555c332009-06-10 14:24:23 +03001827static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001828 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001829 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001830 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001831 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001832{
1833 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001834 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001835
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001836 /*
1837 * We don't set the accessed bit, since we sometimes want to see
1838 * whether the guest actually used the pte (in order to detect
1839 * demand paging).
1840 */
Sheng Yang7b523452008-04-25 21:13:50 +08001841 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001842 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001843 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001844 if (!dirty)
1845 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001846 if (pte_access & ACC_EXEC_MASK)
1847 spte |= shadow_x_mask;
1848 else
1849 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001850 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001851 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001852 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001853 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001854 if (tdp_enabled)
1855 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1856 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001857
Izik Eidus14032832009-09-23 21:47:17 +03001858 if (reset_host_protection)
1859 spte |= SPTE_HOST_WRITEABLE;
1860
Anthony Liguori35149e22008-04-02 14:46:56 -05001861 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001862
1863 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001864 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1865 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001866
Joerg Roedel852e3c12009-07-27 16:30:44 +02001867 if (level > PT_PAGE_TABLE_LEVEL &&
1868 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001869 ret = 1;
Xiao Guangrong6d742292010-05-13 10:07:00 +08001870 rmap_remove(vcpu->kvm, sptep);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001871 spte = shadow_trap_nonpresent_pte;
1872 goto set_pte;
1873 }
1874
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001875 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001876
Avi Kivity69325a12010-05-27 14:35:58 +03001877 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1878 spte &= ~PT_USER_MASK;
1879
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001880 /*
1881 * Optimization: for pte sync, if spte was writable the hash
1882 * lookup is unnecessary (and expensive). Write protection
1883 * is responsibility of mmu_get_page / kvm_sync_page.
1884 * Same reasoning can be applied to dirty page accounting.
1885 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001886 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001887 goto set_pte;
1888
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001889 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001890 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001891 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001892 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001893 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001894 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001895 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001896 }
1897 }
1898
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001899 if (pte_access & ACC_WRITE_MASK)
1900 mark_page_dirty(vcpu->kvm, gfn);
1901
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001902set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001903 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001904 return ret;
1905}
1906
Avi Kivityd555c332009-06-10 14:24:23 +03001907static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001908 unsigned pt_access, unsigned pte_access,
1909 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001910 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001911 pfn_t pfn, bool speculative,
1912 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001913{
1914 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001915 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001916 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001917
1918 pgprintk("%s: spte %llx access %x write_fault %d"
1919 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001920 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001921 write_fault, user_fault, gfn);
1922
Avi Kivityd555c332009-06-10 14:24:23 +03001923 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001924 /*
1925 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1926 * the parent of the now unreachable PTE.
1927 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001928 if (level > PT_PAGE_TABLE_LEVEL &&
1929 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001930 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001931 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001932
1933 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001934 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001935 __set_spte(sptep, shadow_trap_nonpresent_pte);
1936 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001937 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001938 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001939 spte_to_pfn(*sptep), pfn);
1940 rmap_remove(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08001941 __set_spte(sptep, shadow_trap_nonpresent_pte);
1942 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001943 } else
1944 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001945 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001946
Avi Kivityd555c332009-06-10 14:24:23 +03001947 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001948 dirty, level, gfn, pfn, speculative, true,
1949 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001950 if (write_fault)
1951 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08001952 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001953 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001954
Avi Kivityd555c332009-06-10 14:24:23 +03001955 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001956 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001957 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001958 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1959 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001960 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001961 ++vcpu->kvm->stat.lpages;
1962
Avi Kivityd555c332009-06-10 14:24:23 +03001963 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001964 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001965 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001966 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001967 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001968 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001969 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001970 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001971 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001972 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001973 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001974 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001975 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001976 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001977 vcpu->arch.last_pte_gfn = gfn;
1978 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001979}
1980
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1982{
1983}
1984
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001985static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001986 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987{
Avi Kivity9f652d22008-12-25 14:54:25 +02001988 struct kvm_shadow_walk_iterator iterator;
1989 struct kvm_mmu_page *sp;
1990 int pt_write = 0;
1991 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992
Avi Kivity9f652d22008-12-25 14:54:25 +02001993 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001994 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001995 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1996 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001997 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001998 ++vcpu->stat.pf_fixed;
1999 break;
2000 }
2001
2002 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002003 u64 base_addr = iterator.addr;
2004
2005 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2006 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002007 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2008 iterator.level - 1,
2009 1, ACC_ALL, iterator.sptep);
2010 if (!sp) {
2011 pgprintk("nonpaging_map: ENOMEM\n");
2012 kvm_release_pfn_clean(pfn);
2013 return -ENOMEM;
2014 }
2015
Avi Kivityd555c332009-06-10 14:24:23 +03002016 __set_spte(iterator.sptep,
2017 __pa(sp->spt)
2018 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2019 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002020 }
2021 }
2022 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023}
2024
Huang Yingbf998152010-05-31 14:28:19 +08002025static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2026{
2027 char buf[1];
2028 void __user *hva;
2029 int r;
2030
2031 /* Touch the page, so send SIGBUS */
2032 hva = (void __user *)gfn_to_hva(kvm, gfn);
2033 r = copy_from_user(buf, hva, 1);
2034}
2035
2036static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2037{
2038 kvm_release_pfn_clean(pfn);
2039 if (is_hwpoison_pfn(pfn)) {
2040 kvm_send_hwpoison_signal(kvm, gfn);
2041 return 0;
2042 }
2043 return 1;
2044}
2045
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002046static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2047{
2048 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002049 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002050 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002051 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002052
Joerg Roedel852e3c12009-07-27 16:30:44 +02002053 level = mapping_level(vcpu, gfn);
2054
2055 /*
2056 * This path builds a PAE pagetable - so we can map 2mb pages at
2057 * maximum. Therefore check if the level is larger than that.
2058 */
2059 if (level > PT_DIRECTORY_LEVEL)
2060 level = PT_DIRECTORY_LEVEL;
2061
2062 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002063
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002064 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002065 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002066 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002067
Avi Kivityd196e342008-01-24 11:44:11 +02002068 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002069 if (is_error_pfn(pfn))
2070 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002071
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002072 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002073 if (mmu_notifier_retry(vcpu, mmu_seq))
2074 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002075 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002076 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002077 spin_unlock(&vcpu->kvm->mmu_lock);
2078
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002079
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002080 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002081
2082out_unlock:
2083 spin_unlock(&vcpu->kvm->mmu_lock);
2084 kvm_release_pfn_clean(pfn);
2085 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002086}
2087
2088
Avi Kivity17ac10a2007-01-05 16:36:40 -08002089static void mmu_free_roots(struct kvm_vcpu *vcpu)
2090{
2091 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002092 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002093 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002094
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002095 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002096 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002097 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002098 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2099 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002100
Avi Kivity4db35312007-11-21 15:28:32 +02002101 sp = page_header(root);
2102 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002103 if (!sp->root_count && sp->role.invalid) {
2104 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2105 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2106 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002107 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002108 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002109 return;
2110 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002111 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002112 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002113
Avi Kivity417726a2007-04-12 17:35:58 +03002114 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002115 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002116 sp = page_header(root);
2117 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002118 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002119 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2120 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002121 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002122 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002123 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002124 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002125 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002126 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002127}
2128
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002129static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2130{
2131 int ret = 0;
2132
2133 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2134 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2135 ret = 1;
2136 }
2137
2138 return ret;
2139}
2140
2141static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002142{
2143 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002144 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002145 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002146 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002147 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002148
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002149 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002150
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002151 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2152 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002153
2154 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002155 if (mmu_check_root(vcpu, root_gfn))
2156 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002157 if (tdp_enabled) {
2158 direct = 1;
2159 root_gfn = 0;
2160 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002161 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002162 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002163 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002164 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002165 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002166 root = __pa(sp->spt);
2167 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002168 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002169 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002170 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002171 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002172 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002173 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002174 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002175
2176 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002177 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002178 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002179 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002180 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002181 continue;
2182 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002183 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002184 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002185 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002186 if (mmu_check_root(vcpu, root_gfn))
2187 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002188 if (tdp_enabled) {
2189 direct = 1;
2190 root_gfn = i << 30;
2191 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002192 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002193 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002194 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002195 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002196 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002197 root = __pa(sp->spt);
2198 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002199 spin_unlock(&vcpu->kvm->mmu_lock);
2200
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002201 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002202 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002203 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002204 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002205}
2206
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002207static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2208{
2209 int i;
2210 struct kvm_mmu_page *sp;
2211
2212 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2213 return;
2214 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2215 hpa_t root = vcpu->arch.mmu.root_hpa;
2216 sp = page_header(root);
2217 mmu_sync_children(vcpu, sp);
2218 return;
2219 }
2220 for (i = 0; i < 4; ++i) {
2221 hpa_t root = vcpu->arch.mmu.pae_root[i];
2222
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002223 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002224 root &= PT64_BASE_ADDR_MASK;
2225 sp = page_header(root);
2226 mmu_sync_children(vcpu, sp);
2227 }
2228 }
2229}
2230
2231void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2232{
2233 spin_lock(&vcpu->kvm->mmu_lock);
2234 mmu_sync_roots(vcpu);
2235 spin_unlock(&vcpu->kvm->mmu_lock);
2236}
2237
Gleb Natapov1871c602010-02-10 14:21:32 +02002238static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2239 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240{
Gleb Natapov1871c602010-02-10 14:21:32 +02002241 if (error)
2242 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 return vaddr;
2244}
2245
2246static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002247 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248{
Avi Kivitye8332402007-12-09 18:43:00 +02002249 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002250 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002252 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002253 r = mmu_topup_memory_caches(vcpu);
2254 if (r)
2255 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002256
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002258 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259
Avi Kivitye8332402007-12-09 18:43:00 +02002260 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261
Avi Kivitye8332402007-12-09 18:43:00 +02002262 return nonpaging_map(vcpu, gva & PAGE_MASK,
2263 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264}
2265
Joerg Roedelfb72d162008-02-07 13:47:44 +01002266static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2267 u32 error_code)
2268{
Anthony Liguori35149e22008-04-02 14:46:56 -05002269 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002270 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002271 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002272 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002273 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002274
2275 ASSERT(vcpu);
2276 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2277
2278 r = mmu_topup_memory_caches(vcpu);
2279 if (r)
2280 return r;
2281
Joerg Roedel852e3c12009-07-27 16:30:44 +02002282 level = mapping_level(vcpu, gfn);
2283
2284 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2285
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002286 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002287 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002288 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002289 if (is_error_pfn(pfn))
2290 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002291 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002292 if (mmu_notifier_retry(vcpu, mmu_seq))
2293 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002294 kvm_mmu_free_some_pages(vcpu);
2295 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002296 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002297 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002298
2299 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002300
2301out_unlock:
2302 spin_unlock(&vcpu->kvm->mmu_lock);
2303 kvm_release_pfn_clean(pfn);
2304 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002305}
2306
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307static void nonpaging_free(struct kvm_vcpu *vcpu)
2308{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002309 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310}
2311
2312static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2313{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002314 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
2316 context->new_cr3 = nonpaging_new_cr3;
2317 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 context->gva_to_gpa = nonpaging_gva_to_gpa;
2319 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002320 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002321 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002322 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002323 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002325 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 return 0;
2327}
2328
Avi Kivityd835dfe2007-11-21 02:57:59 +02002329void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002331 ++vcpu->stat.tlb_flush;
Xiao Guangrong3b5d1322010-06-08 20:07:01 +08002332 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333}
2334
2335static void paging_new_cr3(struct kvm_vcpu *vcpu)
2336{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002337 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002338 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339}
2340
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341static void inject_page_fault(struct kvm_vcpu *vcpu,
2342 u64 addr,
2343 u32 err_code)
2344{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002345 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346}
2347
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348static void paging_free(struct kvm_vcpu *vcpu)
2349{
2350 nonpaging_free(vcpu);
2351}
2352
Dong, Eddie82725b22009-03-30 16:21:08 +08002353static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2354{
2355 int bit7;
2356
2357 bit7 = (gpte >> 7) & 1;
2358 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2359}
2360
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361#define PTTYPE 64
2362#include "paging_tmpl.h"
2363#undef PTTYPE
2364
2365#define PTTYPE 32
2366#include "paging_tmpl.h"
2367#undef PTTYPE
2368
Dong, Eddie82725b22009-03-30 16:21:08 +08002369static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2370{
2371 struct kvm_mmu *context = &vcpu->arch.mmu;
2372 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2373 u64 exb_bit_rsvd = 0;
2374
2375 if (!is_nx(vcpu))
2376 exb_bit_rsvd = rsvd_bits(63, 63);
2377 switch (level) {
2378 case PT32_ROOT_LEVEL:
2379 /* no rsvd bits for 2 level 4K page table entries */
2380 context->rsvd_bits_mask[0][1] = 0;
2381 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002382 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2383
2384 if (!is_pse(vcpu)) {
2385 context->rsvd_bits_mask[1][1] = 0;
2386 break;
2387 }
2388
Dong, Eddie82725b22009-03-30 16:21:08 +08002389 if (is_cpuid_PSE36())
2390 /* 36bits PSE 4MB page */
2391 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2392 else
2393 /* 32 bits PSE 4MB page */
2394 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002395 break;
2396 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002397 context->rsvd_bits_mask[0][2] =
2398 rsvd_bits(maxphyaddr, 63) |
2399 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002400 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002401 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002402 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2403 rsvd_bits(maxphyaddr, 62); /* PTE */
2404 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2405 rsvd_bits(maxphyaddr, 62) |
2406 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002407 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002408 break;
2409 case PT64_ROOT_LEVEL:
2410 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2411 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2412 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2413 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2414 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002415 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002416 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2417 rsvd_bits(maxphyaddr, 51);
2418 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002419 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2420 rsvd_bits(maxphyaddr, 51) |
2421 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002422 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002423 rsvd_bits(maxphyaddr, 51) |
2424 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002425 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002426 break;
2427 }
2428}
2429
Avi Kivity17ac10a2007-01-05 16:36:40 -08002430static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002432 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433
2434 ASSERT(is_pae(vcpu));
2435 context->new_cr3 = paging_new_cr3;
2436 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002438 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002439 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002440 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002442 context->root_level = level;
2443 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002444 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 return 0;
2446}
2447
Avi Kivity17ac10a2007-01-05 16:36:40 -08002448static int paging64_init_context(struct kvm_vcpu *vcpu)
2449{
Dong, Eddie82725b22009-03-30 16:21:08 +08002450 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002451 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2452}
2453
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454static int paging32_init_context(struct kvm_vcpu *vcpu)
2455{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002456 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457
Dong, Eddie82725b22009-03-30 16:21:08 +08002458 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 context->new_cr3 = paging_new_cr3;
2460 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 context->gva_to_gpa = paging32_gva_to_gpa;
2462 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002463 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002464 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002465 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 context->root_level = PT32_ROOT_LEVEL;
2467 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002468 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 return 0;
2470}
2471
2472static int paging32E_init_context(struct kvm_vcpu *vcpu)
2473{
Dong, Eddie82725b22009-03-30 16:21:08 +08002474 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002475 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476}
2477
Joerg Roedelfb72d162008-02-07 13:47:44 +01002478static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2479{
2480 struct kvm_mmu *context = &vcpu->arch.mmu;
2481
2482 context->new_cr3 = nonpaging_new_cr3;
2483 context->page_fault = tdp_page_fault;
2484 context->free = nonpaging_free;
2485 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002486 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002487 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002488 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002489 context->root_hpa = INVALID_PAGE;
2490
2491 if (!is_paging(vcpu)) {
2492 context->gva_to_gpa = nonpaging_gva_to_gpa;
2493 context->root_level = 0;
2494 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002495 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002496 context->gva_to_gpa = paging64_gva_to_gpa;
2497 context->root_level = PT64_ROOT_LEVEL;
2498 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002499 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002500 context->gva_to_gpa = paging64_gva_to_gpa;
2501 context->root_level = PT32E_ROOT_LEVEL;
2502 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002503 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002504 context->gva_to_gpa = paging32_gva_to_gpa;
2505 context->root_level = PT32_ROOT_LEVEL;
2506 }
2507
2508 return 0;
2509}
2510
2511static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002513 int r;
2514
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002516 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517
2518 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002519 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002520 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002521 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002523 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002525 r = paging32_init_context(vcpu);
2526
Avi Kivity5b7e0102010-04-14 19:20:03 +03002527 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002528 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002529
2530 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531}
2532
Joerg Roedelfb72d162008-02-07 13:47:44 +01002533static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2534{
Anthony Liguori35149e22008-04-02 14:46:56 -05002535 vcpu->arch.update_pte.pfn = bad_pfn;
2536
Joerg Roedelfb72d162008-02-07 13:47:44 +01002537 if (tdp_enabled)
2538 return init_kvm_tdp_mmu(vcpu);
2539 else
2540 return init_kvm_softmmu(vcpu);
2541}
2542
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2544{
2545 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002546 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2547 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002548 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549}
2550
2551int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2552{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002553 destroy_kvm_mmu(vcpu);
2554 return init_kvm_mmu(vcpu);
2555}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002556EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002557
2558int kvm_mmu_load(struct kvm_vcpu *vcpu)
2559{
Avi Kivity714b93d2007-01-05 16:36:53 -08002560 int r;
2561
Avi Kivitye2dec932007-01-05 16:36:54 -08002562 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002563 if (r)
2564 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002565 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002566 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002567 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002568 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002569 if (r)
2570 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002571 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002572 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002573out:
2574 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002576EXPORT_SYMBOL_GPL(kvm_mmu_load);
2577
2578void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2579{
2580 mmu_free_roots(vcpu);
2581}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582
Avi Kivity09072da2007-05-01 14:16:52 +03002583static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002584 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002585 u64 *spte)
2586{
2587 u64 pte;
2588 struct kvm_mmu_page *child;
2589
2590 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002591 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002592 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002593 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002594 else {
2595 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002596 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002597 }
2598 }
Avi Kivityd555c332009-06-10 14:24:23 +03002599 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002600 if (is_large_pte(pte))
2601 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002602}
2603
Avi Kivity00284252007-05-01 16:53:31 +03002604static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002605 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002606 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002607 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002608{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002609 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002610 ++vcpu->kvm->stat.mmu_pde_zapped;
2611 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002612 }
Avi Kivity00284252007-05-01 16:53:31 +03002613
Avi Kivity4cee5762007-11-18 16:37:07 +02002614 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002615 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002616 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002617 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002618 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002619}
2620
Avi Kivity79539ce2007-11-21 02:06:21 +02002621static bool need_remote_flush(u64 old, u64 new)
2622{
2623 if (!is_shadow_present_pte(old))
2624 return false;
2625 if (!is_shadow_present_pte(new))
2626 return true;
2627 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2628 return true;
2629 old ^= PT64_NX_MASK;
2630 new ^= PT64_NX_MASK;
2631 return (old & ~new & PT64_PERM_MASK) != 0;
2632}
2633
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002634static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2635 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002636{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002637 if (zap_page)
2638 return;
2639
2640 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002641 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002642 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002643 kvm_mmu_flush_tlb(vcpu);
2644}
2645
Avi Kivity12b7d282007-09-23 14:10:49 +02002646static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2647{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002648 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002649
Sheng Yang7b523452008-04-25 21:13:50 +08002650 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002651}
2652
Avi Kivityd7824ff2007-12-30 12:29:05 +02002653static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002654 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002655{
2656 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002657 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002658
Avi Kivity43a37952009-06-10 14:12:05 +03002659 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002660 return;
2661 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002662
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002663 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002664 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002665 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002666
Anthony Liguori35149e22008-04-02 14:46:56 -05002667 if (is_error_pfn(pfn)) {
2668 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002669 return;
2670 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002671 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002672 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002673}
2674
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002675static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2676{
2677 u64 *spte = vcpu->arch.last_pte_updated;
2678
2679 if (spte
2680 && vcpu->arch.last_pte_gfn == gfn
2681 && shadow_accessed_mask
2682 && !(*spte & shadow_accessed_mask)
2683 && is_shadow_present_pte(*spte))
2684 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2685}
2686
Avi Kivity09072da2007-05-01 14:16:52 +03002687void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002688 const u8 *new, int bytes,
2689 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002690{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002691 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002692 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002693 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002694 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002695 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002696 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002697 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002698 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002699 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002700 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002701 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002702 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002703 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002704 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002705 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002706 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002707 bool remote_flush, local_flush, zap_page;
2708
2709 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002710
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002711 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002712
Avi Kivity08e850c2010-03-15 13:59:57 +02002713 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2714
2715 /*
2716 * Assume that the pte write on a page table of the same type
2717 * as the current vcpu paging mode. This is nearly always true
2718 * (might be false while changing modes). Note it is verified later
2719 * by update_pte().
2720 */
2721 if ((is_pae(vcpu) && bytes == 4) || !new) {
2722 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2723 if (is_pae(vcpu)) {
2724 gpa &= ~(gpa_t)7;
2725 bytes = 8;
2726 }
2727 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2728 if (r)
2729 gentry = 0;
2730 new = (const u8 *)&gentry;
2731 }
2732
Avi Kivity72016f32010-03-15 13:59:53 +02002733 switch (bytes) {
2734 case 4:
2735 gentry = *(const u32 *)new;
2736 break;
2737 case 8:
2738 gentry = *(const u64 *)new;
2739 break;
2740 default:
2741 gentry = 0;
2742 break;
2743 }
2744
Avi Kivity72016f32010-03-15 13:59:53 +02002745 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002746 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002747 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2748 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002749 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002750 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002751 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002752 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002753 if (guest_initiated) {
2754 if (gfn == vcpu->arch.last_pt_write_gfn
2755 && !last_updated_pte_accessed(vcpu)) {
2756 ++vcpu->arch.last_pt_write_count;
2757 if (vcpu->arch.last_pt_write_count >= 3)
2758 flooded = 1;
2759 } else {
2760 vcpu->arch.last_pt_write_gfn = gfn;
2761 vcpu->arch.last_pt_write_count = 1;
2762 vcpu->arch.last_pte_updated = NULL;
2763 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002764 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002765
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002766 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002767 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002768 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002769 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002770 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002771 /*
2772 * Misaligned accesses are too much trouble to fix
2773 * up; also, they usually indicate a page is not used
2774 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002775 *
2776 * If we're seeing too many writes to a page,
2777 * it may no longer be a page table, or we may be
2778 * forking, in which case it is better to unmap the
2779 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002780 */
2781 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002782 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002783 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002784 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002785 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002786 continue;
2787 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002788 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002789 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002790 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002791 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002792 page_offset <<= 1; /* 32->64 */
2793 /*
2794 * A 32-bit pde maps 4MB while the shadow pdes map
2795 * only 2MB. So we need to double the offset again
2796 * and zap two pdes instead of one.
2797 */
2798 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002799 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002800 page_offset <<= 1;
2801 npte = 2;
2802 }
Avi Kivityfce06572007-05-01 16:44:05 +03002803 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002804 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002805 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002806 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002807 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002808 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02002809 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002810 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002811 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002812 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002813 if (gentry)
2814 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002815 if (!remote_flush && need_remote_flush(entry, *spte))
2816 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02002817 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002818 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002819 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002820 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002821 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityc7addb92007-09-16 18:58:32 +02002822 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002823 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002824 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2825 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2826 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002827 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002828}
2829
Avi Kivitya4360362007-01-05 16:36:45 -08002830int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2831{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002832 gpa_t gpa;
2833 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002834
Avi Kivity60f24782009-08-27 13:37:06 +03002835 if (tdp_enabled)
2836 return 0;
2837
Gleb Natapov1871c602010-02-10 14:21:32 +02002838 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002839
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002840 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002841 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002842 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002843 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002844}
Avi Kivity577bdc42008-07-19 08:57:05 +03002845EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002846
Avi Kivity22d95b12007-09-14 20:26:06 +03002847void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002848{
Xiao Guangrong103ad252010-06-04 21:54:38 +08002849 int free_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002850 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08002851
2852 free_pages = vcpu->kvm->arch.n_free_mmu_pages;
2853 while (free_pages < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03002854 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002855 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002856
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002857 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002858 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002859 free_pages += kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2860 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002861 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002862 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002863 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08002864}
Avi Kivityebeace82007-01-05 16:36:47 -08002865
Avi Kivity30677142007-10-28 18:48:59 +02002866int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2867{
2868 int r;
2869 enum emulation_result er;
2870
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002871 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002872 if (r < 0)
2873 goto out;
2874
2875 if (!r) {
2876 r = 1;
2877 goto out;
2878 }
2879
Avi Kivityb733bfb2007-10-28 18:52:05 +02002880 r = mmu_topup_memory_caches(vcpu);
2881 if (r)
2882 goto out;
2883
Avi Kivity851ba692009-08-24 11:10:17 +03002884 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002885
2886 switch (er) {
2887 case EMULATE_DONE:
2888 return 1;
2889 case EMULATE_DO_MMIO:
2890 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002891 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02002892 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002893 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002894 default:
2895 BUG();
2896 }
2897out:
Avi Kivity30677142007-10-28 18:48:59 +02002898 return r;
2899}
2900EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2901
Marcelo Tosattia7052892008-09-23 13:18:35 -03002902void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2903{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002904 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002905 kvm_mmu_flush_tlb(vcpu);
2906 ++vcpu->stat.invlpg;
2907}
2908EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2909
Joerg Roedel18552672008-02-07 13:47:41 +01002910void kvm_enable_tdp(void)
2911{
2912 tdp_enabled = true;
2913}
2914EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2915
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002916void kvm_disable_tdp(void)
2917{
2918 tdp_enabled = false;
2919}
2920EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2921
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922static void free_mmu_pages(struct kvm_vcpu *vcpu)
2923{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002924 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925}
2926
2927static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2928{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002929 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 int i;
2931
2932 ASSERT(vcpu);
2933
Avi Kivity17ac10a2007-01-05 16:36:40 -08002934 /*
2935 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2936 * Therefore we need to allocate shadow page tables in the first
2937 * 4GB of memory, which happens to fit the DMA32 zone.
2938 */
2939 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2940 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002941 return -ENOMEM;
2942
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002943 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002944 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002945 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002946
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948}
2949
Ingo Molnar8018c272006-12-29 16:50:01 -08002950int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002953 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954
Ingo Molnar8018c272006-12-29 16:50:01 -08002955 return alloc_mmu_pages(vcpu);
2956}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957
Ingo Molnar8018c272006-12-29 16:50:01 -08002958int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2959{
2960 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002961 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002962
Ingo Molnar8018c272006-12-29 16:50:01 -08002963 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964}
2965
2966void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2967{
2968 ASSERT(vcpu);
2969
2970 destroy_kvm_mmu(vcpu);
2971 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002972 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973}
2974
Avi Kivity90cb0522007-07-17 13:04:56 +03002975void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976{
Avi Kivity4db35312007-11-21 15:28:32 +02002977 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002979 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 int i;
2981 u64 *pt;
2982
Sheng Yang291f26b2008-10-16 17:30:57 +08002983 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 continue;
2985
Avi Kivity4db35312007-11-21 15:28:32 +02002986 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2988 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08002989 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 }
Avi Kivity171d5952008-08-27 16:40:51 +03002992 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002994
Avi Kivity90cb0522007-07-17 13:04:56 +03002995void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002996{
Avi Kivity4db35312007-11-21 15:28:32 +02002997 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002998 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03002999
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003000 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003001restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003002 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003003 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003004 goto restart;
3005
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003006 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003007 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003008}
3009
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003010static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3011 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003012{
3013 struct kvm_mmu_page *page;
3014
3015 page = container_of(kvm->arch.active_mmu_pages.prev,
3016 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003017 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003018}
3019
Dave Chinner7f8275d2010-07-19 14:56:17 +10003020static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003021{
3022 struct kvm *kvm;
3023 struct kvm *kvm_freed = NULL;
3024 int cache_count = 0;
3025
3026 spin_lock(&kvm_lock);
3027
3028 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003029 int npages, idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003030 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003031
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003032 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003033 spin_lock(&kvm->mmu_lock);
3034 npages = kvm->arch.n_alloc_mmu_pages -
3035 kvm->arch.n_free_mmu_pages;
3036 cache_count += npages;
3037 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003038 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3039 &invalid_list);
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003040 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003041 kvm_freed = kvm;
3042 }
3043 nr_to_scan--;
3044
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003045 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003046 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003047 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003048 }
3049 if (kvm_freed)
3050 list_move_tail(&kvm_freed->vm_list, &vm_list);
3051
3052 spin_unlock(&kvm_lock);
3053
3054 return cache_count;
3055}
3056
3057static struct shrinker mmu_shrinker = {
3058 .shrink = mmu_shrink,
3059 .seeks = DEFAULT_SEEKS * 10,
3060};
3061
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003062static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003063{
3064 if (pte_chain_cache)
3065 kmem_cache_destroy(pte_chain_cache);
3066 if (rmap_desc_cache)
3067 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003068 if (mmu_page_header_cache)
3069 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003070}
3071
Izik Eidus3ee16c82008-03-30 15:17:21 +03003072void kvm_mmu_module_exit(void)
3073{
3074 mmu_destroy_caches();
3075 unregister_shrinker(&mmu_shrinker);
3076}
3077
Avi Kivityb5a33a72007-04-15 16:31:09 +03003078int kvm_mmu_module_init(void)
3079{
3080 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3081 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003082 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003083 if (!pte_chain_cache)
3084 goto nomem;
3085 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3086 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003087 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003088 if (!rmap_desc_cache)
3089 goto nomem;
3090
Avi Kivityd3d25b02007-05-30 12:34:53 +03003091 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3092 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003093 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003094 if (!mmu_page_header_cache)
3095 goto nomem;
3096
Izik Eidus3ee16c82008-03-30 15:17:21 +03003097 register_shrinker(&mmu_shrinker);
3098
Avi Kivityb5a33a72007-04-15 16:31:09 +03003099 return 0;
3100
3101nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003102 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003103 return -ENOMEM;
3104}
3105
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003106/*
3107 * Caculate mmu pages needed for kvm.
3108 */
3109unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3110{
3111 int i;
3112 unsigned int nr_mmu_pages;
3113 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003114 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003115
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003116 slots = kvm_memslots(kvm);
3117
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003118 for (i = 0; i < slots->nmemslots; i++)
3119 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003120
3121 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3122 nr_mmu_pages = max(nr_mmu_pages,
3123 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3124
3125 return nr_mmu_pages;
3126}
3127
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003128static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3129 unsigned len)
3130{
3131 if (len > buffer->len)
3132 return NULL;
3133 return buffer->ptr;
3134}
3135
3136static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3137 unsigned len)
3138{
3139 void *ret;
3140
3141 ret = pv_mmu_peek_buffer(buffer, len);
3142 if (!ret)
3143 return ret;
3144 buffer->ptr += len;
3145 buffer->len -= len;
3146 buffer->processed += len;
3147 return ret;
3148}
3149
3150static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3151 gpa_t addr, gpa_t value)
3152{
3153 int bytes = 8;
3154 int r;
3155
3156 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3157 bytes = 4;
3158
3159 r = mmu_topup_memory_caches(vcpu);
3160 if (r)
3161 return r;
3162
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003163 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003164 return -EFAULT;
3165
3166 return 1;
3167}
3168
3169static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3170{
Avi Kivity23902182010-06-10 17:02:16 +03003171 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003172 return 1;
3173}
3174
3175static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3176{
3177 spin_lock(&vcpu->kvm->mmu_lock);
3178 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3179 spin_unlock(&vcpu->kvm->mmu_lock);
3180 return 1;
3181}
3182
3183static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3184 struct kvm_pv_mmu_op_buffer *buffer)
3185{
3186 struct kvm_mmu_op_header *header;
3187
3188 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3189 if (!header)
3190 return 0;
3191 switch (header->op) {
3192 case KVM_MMU_OP_WRITE_PTE: {
3193 struct kvm_mmu_op_write_pte *wpte;
3194
3195 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3196 if (!wpte)
3197 return 0;
3198 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3199 wpte->pte_val);
3200 }
3201 case KVM_MMU_OP_FLUSH_TLB: {
3202 struct kvm_mmu_op_flush_tlb *ftlb;
3203
3204 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3205 if (!ftlb)
3206 return 0;
3207 return kvm_pv_mmu_flush_tlb(vcpu);
3208 }
3209 case KVM_MMU_OP_RELEASE_PT: {
3210 struct kvm_mmu_op_release_pt *rpt;
3211
3212 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3213 if (!rpt)
3214 return 0;
3215 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3216 }
3217 default: return 0;
3218 }
3219}
3220
3221int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3222 gpa_t addr, unsigned long *ret)
3223{
3224 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003225 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003226
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003227 buffer->ptr = buffer->buf;
3228 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3229 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003230
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003231 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003232 if (r)
3233 goto out;
3234
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003235 while (buffer->len) {
3236 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003237 if (r < 0)
3238 goto out;
3239 if (r == 0)
3240 break;
3241 }
3242
3243 r = 1;
3244out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003245 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003246 return r;
3247}
3248
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003249int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3250{
3251 struct kvm_shadow_walk_iterator iterator;
3252 int nr_sptes = 0;
3253
3254 spin_lock(&vcpu->kvm->mmu_lock);
3255 for_each_shadow_entry(vcpu, addr, iterator) {
3256 sptes[iterator.level-1] = *iterator.sptep;
3257 nr_sptes++;
3258 if (!is_shadow_present_pte(*iterator.sptep))
3259 break;
3260 }
3261 spin_unlock(&vcpu->kvm->mmu_lock);
3262
3263 return nr_sptes;
3264}
3265EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3266
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003267#ifdef AUDIT
3268
3269static const char *audit_msg;
3270
3271static gva_t canonicalize(gva_t gva)
3272{
3273#ifdef CONFIG_X86_64
3274 gva = (long long)(gva << 16) >> 16;
3275#endif
3276 return gva;
3277}
3278
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003279
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003280typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003281
3282static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3283 inspect_spte_fn fn)
3284{
3285 int i;
3286
3287 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3288 u64 ent = sp->spt[i];
3289
3290 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003291 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003292 struct kvm_mmu_page *child;
3293 child = page_header(ent & PT64_BASE_ADDR_MASK);
3294 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003295 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003296 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003297 }
3298 }
3299}
3300
3301static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3302{
3303 int i;
3304 struct kvm_mmu_page *sp;
3305
3306 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3307 return;
3308 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3309 hpa_t root = vcpu->arch.mmu.root_hpa;
3310 sp = page_header(root);
3311 __mmu_spte_walk(vcpu->kvm, sp, fn);
3312 return;
3313 }
3314 for (i = 0; i < 4; ++i) {
3315 hpa_t root = vcpu->arch.mmu.pae_root[i];
3316
3317 if (root && VALID_PAGE(root)) {
3318 root &= PT64_BASE_ADDR_MASK;
3319 sp = page_header(root);
3320 __mmu_spte_walk(vcpu->kvm, sp, fn);
3321 }
3322 }
3323 return;
3324}
3325
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003326static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3327 gva_t va, int level)
3328{
3329 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3330 int i;
3331 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3332
3333 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3334 u64 ent = pt[i];
3335
Avi Kivityc7addb92007-09-16 18:58:32 +02003336 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003337 continue;
3338
3339 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003340 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3341 audit_mappings_page(vcpu, ent, va, level - 1);
3342 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003343 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003344 gfn_t gfn = gpa >> PAGE_SHIFT;
3345 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3346 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003347
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003348 if (is_error_pfn(pfn)) {
3349 kvm_release_pfn_clean(pfn);
3350 continue;
3351 }
3352
Avi Kivityc7addb92007-09-16 18:58:32 +02003353 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003354 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003355 printk(KERN_ERR "xx audit error: (%s) levels %d"
3356 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003357 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003358 va, gpa, hpa, ent,
3359 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003360 else if (ent == shadow_notrap_nonpresent_pte
3361 && !is_error_hpa(hpa))
3362 printk(KERN_ERR "audit: (%s) notrap shadow,"
3363 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003364 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003365
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003366 }
3367 }
3368}
3369
3370static void audit_mappings(struct kvm_vcpu *vcpu)
3371{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003372 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003373
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003374 if (vcpu->arch.mmu.root_level == 4)
3375 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003376 else
3377 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003378 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003379 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003380 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003381 i << 30,
3382 2);
3383}
3384
3385static int count_rmaps(struct kvm_vcpu *vcpu)
3386{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003387 struct kvm *kvm = vcpu->kvm;
3388 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003389 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003390 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003391
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003392 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003393 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003394 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003395 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003396 struct kvm_rmap_desc *d;
3397
3398 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003399 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003400
Izik Eidus290fc382007-09-27 14:11:22 +02003401 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003402 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003403 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003404 ++nmaps;
3405 continue;
3406 }
Izik Eidus290fc382007-09-27 14:11:22 +02003407 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003408 while (d) {
3409 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003410 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003411 ++nmaps;
3412 else
3413 break;
3414 d = d->more;
3415 }
3416 }
3417 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003418 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003419 return nmaps;
3420}
3421
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003422void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003423{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003424 unsigned long *rmapp;
3425 struct kvm_mmu_page *rev_sp;
3426 gfn_t gfn;
3427
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003428 if (is_writable_pte(*sptep)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003429 rev_sp = page_header(__pa(sptep));
Lai Jiangshan2032a932010-05-26 16:49:59 +08003430 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003431
3432 if (!gfn_to_memslot(kvm, gfn)) {
3433 if (!printk_ratelimit())
3434 return;
3435 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3436 audit_msg, gfn);
3437 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003438 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003439 rev_sp->gfn);
3440 dump_stack();
3441 return;
3442 }
3443
Lai Jiangshan2032a932010-05-26 16:49:59 +08003444 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003445 if (!*rmapp) {
3446 if (!printk_ratelimit())
3447 return;
3448 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3449 audit_msg, *sptep);
3450 dump_stack();
3451 }
3452 }
3453
3454}
3455
3456void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3457{
3458 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3459}
3460
3461static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3462{
Avi Kivity4db35312007-11-21 15:28:32 +02003463 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003464 int i;
3465
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003466 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003467 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003468
Avi Kivity4db35312007-11-21 15:28:32 +02003469 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003470 continue;
3471
3472 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3473 u64 ent = pt[i];
3474
3475 if (!(ent & PT_PRESENT_MASK))
3476 continue;
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003477 if (!is_writable_pte(ent))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003478 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003479 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003480 }
3481 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003482 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003483}
3484
3485static void audit_rmap(struct kvm_vcpu *vcpu)
3486{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003487 check_writable_mappings_rmap(vcpu);
3488 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003489}
3490
3491static void audit_write_protection(struct kvm_vcpu *vcpu)
3492{
Avi Kivity4db35312007-11-21 15:28:32 +02003493 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003494 struct kvm_memory_slot *slot;
3495 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003496 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003497 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003498
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003499 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003500 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003501 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003502 if (sp->unsync)
3503 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003504
Avi Kivitya1f4d3952010-06-21 11:44:20 +03003505 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003506 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003507
3508 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3509 while (spte) {
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003510 if (is_writable_pte(*spte))
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003511 printk(KERN_ERR "%s: (%s) shadow page has "
3512 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003513 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003514 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003515 spte = rmap_next(vcpu->kvm, rmapp, spte);
3516 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003517 }
3518}
3519
3520static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3521{
3522 int olddbg = dbg;
3523
3524 dbg = 0;
3525 audit_msg = msg;
3526 audit_rmap(vcpu);
3527 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003528 if (strcmp("pre pte write", audit_msg) != 0)
3529 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003530 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003531 dbg = olddbg;
3532}
3533
3534#endif