blob: aa98fca03ed7a6d104e91b0ae783a84397ffe498 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
Avi Kivity221d0592010-05-23 18:37:00 +030010 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020022#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030023#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/highmem.h>
30#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020031#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030032#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050033#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020034#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080036#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
38#include <asm/page.h>
39#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020040#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020041#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040042
Joerg Roedel18552672008-02-07 13:47:41 +010043/*
44 * When setting this variable to true it enables Two-Dimensional-Paging
45 * where the hardware walks 2 page tables:
46 * 1. the guest-virtual to guest-physical
47 * 2. while doing 1. it walks guest-physical to host-physical
48 * If the hardware supports that we don't need to do shadow paging.
49 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050050bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010051
Avi Kivity37a7d8b2007-01-05 16:36:56 -080052#undef MMU_DEBUG
53
54#undef AUDIT
55
56#ifdef AUDIT
57static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
58#else
59static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
60#endif
61
62#ifdef MMU_DEBUG
63
64#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
65#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
66
67#else
68
69#define pgprintk(x...) do { } while (0)
70#define rmap_printk(x...) do { } while (0)
71
72#endif
73
74#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030075static int dbg = 0;
76module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080077#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Marcelo Tosatti582801a2008-09-23 13:18:41 -030079static int oos_shadow = 1;
80module_param(oos_shadow, bool, 0644);
81
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080082#ifndef MMU_DEBUG
83#define ASSERT(x) do { } while (0)
84#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080085#define ASSERT(x) \
86 if (!(x)) { \
87 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
88 __FILE__, __LINE__, #x); \
89 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080090#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
Avi Kivity6aa8b732006-12-10 02:21:36 -080092#define PT_FIRST_AVAIL_BITS_SHIFT 9
93#define PT64_SECOND_AVAIL_BITS_SHIFT 52
94
Avi Kivity6aa8b732006-12-10 02:21:36 -080095#define VALID_PAGE(x) ((x) != INVALID_PAGE)
96
97#define PT64_LEVEL_BITS 9
98
99#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400100 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
102#define PT64_LEVEL_MASK(level) \
103 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
104
105#define PT64_INDEX(address, level)\
106 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
107
108
109#define PT32_LEVEL_BITS 10
110
111#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400112 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
114#define PT32_LEVEL_MASK(level) \
115 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200116#define PT32_LVL_OFFSET_MASK(level) \
117 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
118 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119
120#define PT32_INDEX(address, level)\
121 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
122
123
Avi Kivity27aba762007-03-09 13:04:31 +0200124#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125#define PT64_DIR_BASE_ADDR_MASK \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200127#define PT64_LVL_ADDR_MASK(level) \
128 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
129 * PT64_LEVEL_BITS))) - 1))
130#define PT64_LVL_OFFSET_MASK(level) \
131 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
132 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133
134#define PT32_BASE_ADDR_MASK PAGE_MASK
135#define PT32_DIR_BASE_ADDR_MASK \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200137#define PT32_LVL_ADDR_MASK(level) \
138 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
Avi Kivity79539ce2007-11-21 02:06:21 +0200141#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
142 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800144#define RMAP_EXT 4
145
Avi Kivityfe135d22007-12-09 16:15:46 +0200146#define ACC_EXEC_MASK 1
147#define ACC_WRITE_MASK PT_WRITABLE_MASK
148#define ACC_USER_MASK PT_USER_MASK
149#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
150
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200151#include <trace/events/kvm.h>
152
Avi Kivity07420172009-07-06 12:21:32 +0300153#define CREATE_TRACE_POINTS
154#include "mmutrace.h"
155
Izik Eidus14032832009-09-23 21:47:17 +0300156#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
157
Avi Kivity135f8c22008-08-21 17:49:56 +0300158#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
159
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300161 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800162 struct kvm_rmap_desc *more;
163};
164
Avi Kivity2d111232008-12-25 14:39:47 +0200165struct kvm_shadow_walk_iterator {
166 u64 addr;
167 hpa_t shadow_addr;
168 int level;
169 u64 *sptep;
170 unsigned index;
171};
172
173#define for_each_shadow_entry(_vcpu, _addr, _walker) \
174 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
175 shadow_walk_okay(&(_walker)); \
176 shadow_walk_next(&(_walker)))
177
Xiao Guangrong6b184932010-04-16 21:29:17 +0800178typedef int (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300179
Avi Kivityb5a33a72007-04-15 16:31:09 +0300180static struct kmem_cache *pte_chain_cache;
181static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300182static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300183
Avi Kivityc7addb92007-09-16 18:58:32 +0200184static u64 __read_mostly shadow_trap_nonpresent_pte;
185static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800186static u64 __read_mostly shadow_base_present_pte;
187static u64 __read_mostly shadow_nx_mask;
188static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
189static u64 __read_mostly shadow_user_mask;
190static u64 __read_mostly shadow_accessed_mask;
191static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200192
Dong, Eddie82725b22009-03-30 16:21:08 +0800193static inline u64 rsvd_bits(int s, int e)
194{
195 return ((1ULL << (e - s + 1)) - 1) << s;
196}
197
Avi Kivityc7addb92007-09-16 18:58:32 +0200198void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
199{
200 shadow_trap_nonpresent_pte = trap_pte;
201 shadow_notrap_nonpresent_pte = notrap_pte;
202}
203EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
204
Sheng Yang7b523452008-04-25 21:13:50 +0800205void kvm_mmu_set_base_ptes(u64 base_pte)
206{
207 shadow_base_present_pte = base_pte;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
210
211void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800212 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800213{
214 shadow_user_mask = user_mask;
215 shadow_accessed_mask = accessed_mask;
216 shadow_dirty_mask = dirty_mask;
217 shadow_nx_mask = nx_mask;
218 shadow_x_mask = x_mask;
219}
220EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
221
Avi Kivity3dbe1412010-05-12 11:48:18 +0300222static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200224 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225}
226
227static int is_cpuid_PSE36(void)
228{
229 return 1;
230}
231
Avi Kivity73b10872007-01-26 00:56:41 -0800232static int is_nx(struct kvm_vcpu *vcpu)
233{
Avi Kivityf6801df2010-01-21 15:31:50 +0200234 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800235}
236
Avi Kivityc7addb92007-09-16 18:58:32 +0200237static int is_shadow_present_pte(u64 pte)
238{
Avi Kivityc7addb92007-09-16 18:58:32 +0200239 return pte != shadow_trap_nonpresent_pte
240 && pte != shadow_notrap_nonpresent_pte;
241}
242
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300243static int is_large_pte(u64 pte)
244{
245 return pte & PT_PAGE_SIZE_MASK;
246}
247
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900248static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249{
250 return pte & PT_WRITABLE_MASK;
251}
252
Avi Kivity43a37952009-06-10 14:12:05 +0300253static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200254{
Avi Kivity439e2182009-06-10 12:56:54 +0300255 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200256}
257
Avi Kivity43a37952009-06-10 14:12:05 +0300258static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800259{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200260 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800261}
262
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300263static int is_last_spte(u64 pte, int level)
264{
265 if (level == PT_PAGE_TABLE_LEVEL)
266 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200267 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300268 return 1;
269 return 0;
270}
271
Anthony Liguori35149e22008-04-02 14:46:56 -0500272static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200273{
Anthony Liguori35149e22008-04-02 14:46:56 -0500274 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200275}
276
Avi 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
Izik Eidus28430992008-10-03 17:40:32 +0300437 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438
439 slot = gfn_to_memslot_unaliased(kvm, gfn);
440 for (i = PT_DIRECTORY_LEVEL;
441 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
442 write_count = slot_largepage_idx(gfn, slot, i);
443 *write_count += 1;
444 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445}
446
447static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
448{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200449 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200451 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452
Izik Eidus28430992008-10-03 17:40:32 +0300453 gfn = unalias_gfn(kvm, gfn);
Wei Yongjun77a1a712010-04-16 16:21:42 +0800454 slot = gfn_to_memslot_unaliased(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200455 for (i = PT_DIRECTORY_LEVEL;
456 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200457 write_count = slot_largepage_idx(gfn, slot, i);
458 *write_count -= 1;
459 WARN_ON(*write_count < 0);
460 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461}
462
Joerg Roedeld25797b2009-07-27 16:30:43 +0200463static int has_wrprotected_page(struct kvm *kvm,
464 gfn_t gfn,
465 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466{
Izik Eidus28430992008-10-03 17:40:32 +0300467 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468 int *largepage_idx;
469
Izik Eidus28430992008-10-03 17:40:32 +0300470 gfn = unalias_gfn(kvm, gfn);
471 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200473 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474 return *largepage_idx;
475 }
476
477 return 1;
478}
479
Joerg Roedeld25797b2009-07-27 16:30:43 +0200480static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100482 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200483 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300484
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100485 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486
Joerg Roedeld25797b2009-07-27 16:30:43 +0200487 for (i = PT_PAGE_TABLE_LEVEL;
488 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
489 if (page_size >= KVM_HPAGE_SIZE(i))
490 ret = i;
491 else
492 break;
493 }
494
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300495 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300496}
497
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499{
500 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800501 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502
503 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
504 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506
Joerg Roedeld25797b2009-07-27 16:30:43 +0200507 host_level = host_mapping_level(vcpu->kvm, large_gfn);
508
509 if (host_level == PT_PAGE_TABLE_LEVEL)
510 return host_level;
511
Sheng Yang878403b2010-01-05 19:02:29 +0800512 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
513 kvm_x86_ops->get_lpage_level() : host_level;
514
515 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
517 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200518
519 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300520}
521
522/*
Izik Eidus290fc382007-09-27 14:11:22 +0200523 * Take gfn and return the reverse mapping to it.
524 * Note: gfn must be unaliased before this function get called
525 */
526
Joerg Roedel44ad9942009-07-27 16:30:42 +0200527static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200528{
529 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300530 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200531
532 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200533 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534 return &slot->rmap[gfn - slot->base_gfn];
535
Joerg Roedel44ad9942009-07-27 16:30:42 +0200536 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
537 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300538
Joerg Roedel44ad9942009-07-27 16:30:42 +0200539 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200540}
541
542/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543 * Reverse mapping data structures:
544 *
Izik Eidus290fc382007-09-27 14:11:22 +0200545 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
546 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 *
Izik Eidus290fc382007-09-27 14:11:22 +0200548 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
549 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300550 *
551 * Returns the number of rmap entries before the spte was added or zero if
552 * the spte was not added.
553 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800554 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200555static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556{
Avi Kivity4db35312007-11-21 15:28:32 +0200557 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200559 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300560 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561
Avi Kivity43a37952009-06-10 14:12:05 +0300562 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300563 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200564 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200565 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800566 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200567 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200568 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800569 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200570 *rmapp = (unsigned long)spte;
571 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800573 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300574 desc->sptes[0] = (u64 *)*rmapp;
575 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200576 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 } else {
578 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200579 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300580 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300582 count += RMAP_EXT;
583 }
Avi Kivityd555c332009-06-10 14:24:23 +0300584 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800585 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 desc = desc->more;
587 }
Avi Kivityd555c332009-06-10 14:24:23 +0300588 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300590 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800591 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300592 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800593}
594
Izik Eidus290fc382007-09-27 14:11:22 +0200595static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 struct kvm_rmap_desc *desc,
597 int i,
598 struct kvm_rmap_desc *prev_desc)
599{
600 int j;
601
Avi Kivityd555c332009-06-10 14:24:23 +0300602 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800603 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300604 desc->sptes[i] = desc->sptes[j];
605 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 if (j != 0)
607 return;
608 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300609 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800610 else
611 if (prev_desc)
612 prev_desc->more = desc->more;
613 else
Izik Eidus290fc382007-09-27 14:11:22 +0200614 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300615 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800616}
617
Izik Eidus290fc382007-09-27 14:11:22 +0200618static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800620 struct kvm_rmap_desc *desc;
621 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200622 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500623 pfn_t pfn;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800624 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200625 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626 int i;
627
Avi Kivity43a37952009-06-10 14:12:05 +0300628 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200630 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500631 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800632 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500633 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900634 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300635 kvm_set_pfn_dirty(pfn);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800636 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
637 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200638 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
640 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200641 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200643 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800644 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
645 spte, *spte);
646 BUG();
647 }
Izik Eidus290fc382007-09-27 14:11:22 +0200648 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800649 } else {
650 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200651 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800652 prev_desc = NULL;
653 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300654 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
655 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200656 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800657 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800658 prev_desc);
659 return;
660 }
661 prev_desc = desc;
662 desc = desc->more;
663 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200664 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800665 BUG();
666 }
667}
668
Izik Eidus98348e92007-10-16 14:42:30 +0200669static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800670{
Avi Kivity374cbac2007-01-05 16:36:43 -0800671 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200672 u64 *prev_spte;
673 int i;
674
675 if (!*rmapp)
676 return NULL;
677 else if (!(*rmapp & 1)) {
678 if (!spte)
679 return (u64 *)*rmapp;
680 return NULL;
681 }
682 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200683 prev_spte = NULL;
684 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300685 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200686 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300687 return desc->sptes[i];
688 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200689 }
690 desc = desc->more;
691 }
692 return NULL;
693}
694
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200695static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200696{
Izik Eidus290fc382007-09-27 14:11:22 +0200697 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800698 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200699 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800700
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500701 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200702 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800703
Izik Eidus98348e92007-10-16 14:42:30 +0200704 spte = rmap_next(kvm, rmapp, NULL);
705 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800706 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800707 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800708 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900709 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300710 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800711 write_protected = 1;
712 }
Izik Eidus9647c142007-10-16 14:43:46 +0200713 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800714 }
Izik Eidus855149a2008-03-20 18:17:24 +0200715 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500716 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200717
718 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500719 pfn = spte_to_pfn(*spte);
720 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200721 }
722
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300723 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200724 for (i = PT_DIRECTORY_LEVEL;
725 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
726 rmapp = gfn_to_rmap(kvm, gfn, i);
727 spte = rmap_next(kvm, rmapp, NULL);
728 while (spte) {
729 BUG_ON(!spte);
730 BUG_ON(!(*spte & PT_PRESENT_MASK));
731 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
732 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900733 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200734 rmap_remove(kvm, spte);
735 --kvm->stat.lpages;
736 __set_spte(spte, shadow_trap_nonpresent_pte);
737 spte = NULL;
738 write_protected = 1;
739 }
740 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300741 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300742 }
743
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200744 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800745}
746
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000747static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
748 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200749{
750 u64 *spte;
751 int need_tlb_flush = 0;
752
753 while ((spte = rmap_next(kvm, rmapp, NULL))) {
754 BUG_ON(!(*spte & PT_PRESENT_MASK));
755 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
756 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300757 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200758 need_tlb_flush = 1;
759 }
760 return need_tlb_flush;
761}
762
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000763static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
764 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300765{
766 int need_flush = 0;
767 u64 *spte, new_spte;
768 pte_t *ptep = (pte_t *)data;
769 pfn_t new_pfn;
770
771 WARN_ON(pte_huge(*ptep));
772 new_pfn = pte_pfn(*ptep);
773 spte = rmap_next(kvm, rmapp, NULL);
774 while (spte) {
775 BUG_ON(!is_shadow_present_pte(*spte));
776 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
777 need_flush = 1;
778 if (pte_write(*ptep)) {
779 rmap_remove(kvm, spte);
780 __set_spte(spte, shadow_trap_nonpresent_pte);
781 spte = rmap_next(kvm, rmapp, NULL);
782 } else {
783 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
784 new_spte |= (u64)new_pfn << PAGE_SHIFT;
785
786 new_spte &= ~PT_WRITABLE_MASK;
787 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900788 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300789 kvm_set_pfn_dirty(spte_to_pfn(*spte));
790 __set_spte(spte, new_spte);
791 spte = rmap_next(kvm, rmapp, spte);
792 }
793 }
794 if (need_flush)
795 kvm_flush_remote_tlbs(kvm);
796
797 return 0;
798}
799
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000800static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
801 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300802 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000803 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200804{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200805 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200806 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200807 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200808 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200809
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800810 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200811
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200812 for (i = 0; i < slots->nmemslots; i++) {
813 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200814 unsigned long start = memslot->userspace_addr;
815 unsigned long end;
816
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200817 end = start + (memslot->npages << PAGE_SHIFT);
818 if (hva >= start && hva < end) {
819 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200820
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200821 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200822
823 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
824 int idx = gfn_offset;
825 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200826 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300827 &memslot->lpage_info[j][idx].rmap_pde,
828 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200829 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200830 trace_kvm_age_page(hva, memslot, ret);
831 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200832 }
833 }
834
835 return retval;
836}
837
838int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
839{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300840 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200841}
842
Izik Eidus3da0dd42009-09-23 21:47:18 +0300843void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
844{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000845 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300846}
847
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000848static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
849 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200850{
851 u64 *spte;
852 int young = 0;
853
Rik van Riel6316e1c2010-02-03 16:11:03 -0500854 /*
855 * Emulate the accessed bit for EPT, by checking if this page has
856 * an EPT mapping, and clearing it if it does. On the next access,
857 * a new EPT mapping will be established.
858 * This has some overhead, but not as much as the cost of swapping
859 * out actively used pages or breaking up actively used hugepages.
860 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800861 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500862 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800863
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200864 spte = rmap_next(kvm, rmapp, NULL);
865 while (spte) {
866 int _young;
867 u64 _spte = *spte;
868 BUG_ON(!(_spte & PT_PRESENT_MASK));
869 _young = _spte & PT_ACCESSED_MASK;
870 if (_young) {
871 young = 1;
872 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
873 }
874 spte = rmap_next(kvm, rmapp, spte);
875 }
876 return young;
877}
878
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300879#define RMAP_RECYCLE_THRESHOLD 1000
880
Joerg Roedel852e3c12009-07-27 16:30:44 +0200881static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300882{
883 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200884 struct kvm_mmu_page *sp;
885
886 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300887
888 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200889 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300890
Izik Eidus3da0dd42009-09-23 21:47:18 +0300891 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300892 kvm_flush_remote_tlbs(vcpu->kvm);
893}
894
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200895int kvm_age_hva(struct kvm *kvm, unsigned long hva)
896{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300897 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200898}
899
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800900#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300901static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902{
Avi Kivity139bdb22007-01-05 16:36:50 -0800903 u64 *pos;
904 u64 *end;
905
Avi Kivity47ad8e62007-05-06 15:50:58 +0300906 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300907 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800908 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800909 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800911 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 return 1;
913}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800914#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
Avi Kivity4db35312007-11-21 15:28:32 +0200916static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800917{
Avi Kivity4db35312007-11-21 15:28:32 +0200918 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800919 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200920 list_del(&sp->link);
921 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800922 if (!sp->role.direct)
923 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800924 kmem_cache_free(mmu_page_header_cache, sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800925 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800926}
927
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800928static unsigned kvm_page_table_hashfn(gfn_t gfn)
929{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200930 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800931}
932
Avi Kivity25c0de22007-01-05 16:36:42 -0800933static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +0800934 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935{
Avi Kivity4db35312007-11-21 15:28:32 +0200936 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800938 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
939 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800940 if (!direct)
941 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
942 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200943 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800944 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +0800945 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200946 sp->multimapped = 0;
947 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800948 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200949 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950}
951
Avi Kivity714b93d2007-01-05 16:36:53 -0800952static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200953 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800954{
955 struct kvm_pte_chain *pte_chain;
956 struct hlist_node *node;
957 int i;
958
959 if (!parent_pte)
960 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200961 if (!sp->multimapped) {
962 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800963
964 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200965 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800966 return;
967 }
Avi Kivity4db35312007-11-21 15:28:32 +0200968 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800969 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200970 INIT_HLIST_HEAD(&sp->parent_ptes);
971 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800972 pte_chain->parent_ptes[0] = old;
973 }
Avi Kivity4db35312007-11-21 15:28:32 +0200974 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800975 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
976 continue;
977 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
978 if (!pte_chain->parent_ptes[i]) {
979 pte_chain->parent_ptes[i] = parent_pte;
980 return;
981 }
982 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800983 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200985 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800986 pte_chain->parent_ptes[0] = parent_pte;
987}
988
Avi Kivity4db35312007-11-21 15:28:32 +0200989static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800990 u64 *parent_pte)
991{
992 struct kvm_pte_chain *pte_chain;
993 struct hlist_node *node;
994 int i;
995
Avi Kivity4db35312007-11-21 15:28:32 +0200996 if (!sp->multimapped) {
997 BUG_ON(sp->parent_pte != parent_pte);
998 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800999 return;
1000 }
Avi Kivity4db35312007-11-21 15:28:32 +02001001 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001002 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1003 if (!pte_chain->parent_ptes[i])
1004 break;
1005 if (pte_chain->parent_ptes[i] != parent_pte)
1006 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001007 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1008 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001009 pte_chain->parent_ptes[i]
1010 = pte_chain->parent_ptes[i + 1];
1011 ++i;
1012 }
1013 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001014 if (i == 0) {
1015 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001016 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001017 if (hlist_empty(&sp->parent_ptes)) {
1018 sp->multimapped = 0;
1019 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001020 }
1021 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001022 return;
1023 }
1024 BUG();
1025}
1026
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001027
Xiao Guangrong6b184932010-04-16 21:29:17 +08001028static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001029{
1030 struct kvm_pte_chain *pte_chain;
1031 struct hlist_node *node;
1032 struct kvm_mmu_page *parent_sp;
1033 int i;
1034
1035 if (!sp->multimapped && sp->parent_pte) {
1036 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001037 fn(parent_sp);
1038 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001039 return;
1040 }
1041 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1042 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1043 if (!pte_chain->parent_ptes[i])
1044 break;
1045 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
Xiao Guangrong6b184932010-04-16 21:29:17 +08001046 fn(parent_sp);
1047 mmu_parent_walk(parent_sp, fn);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001048 }
1049}
1050
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001051static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1052{
1053 unsigned int index;
1054 struct kvm_mmu_page *sp = page_header(__pa(spte));
1055
1056 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001057 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1058 sp->unsync_children++;
1059 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001060}
1061
1062static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1063{
1064 struct kvm_pte_chain *pte_chain;
1065 struct hlist_node *node;
1066 int i;
1067
1068 if (!sp->parent_pte)
1069 return;
1070
1071 if (!sp->multimapped) {
1072 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1073 return;
1074 }
1075
1076 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1077 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1078 if (!pte_chain->parent_ptes[i])
1079 break;
1080 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1081 }
1082}
1083
Xiao Guangrong6b184932010-04-16 21:29:17 +08001084static int unsync_walk_fn(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001085{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001086 kvm_mmu_update_parents_unsync(sp);
1087 return 1;
1088}
1089
Xiao Guangrong6b184932010-04-16 21:29:17 +08001090static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001091{
Xiao Guangrong6b184932010-04-16 21:29:17 +08001092 mmu_parent_walk(sp, unsync_walk_fn);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001093 kvm_mmu_update_parents_unsync(sp);
1094}
1095
Avi Kivityd761a502008-05-29 14:55:03 +03001096static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1097 struct kvm_mmu_page *sp)
1098{
1099 int i;
1100
1101 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1102 sp->spt[i] = shadow_trap_nonpresent_pte;
1103}
1104
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001105static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1106 struct kvm_mmu_page *sp)
1107{
1108 return 1;
1109}
1110
Marcelo Tosattia7052892008-09-23 13:18:35 -03001111static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1112{
1113}
1114
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001115#define KVM_PAGE_ARRAY_NR 16
1116
1117struct kvm_mmu_pages {
1118 struct mmu_page_and_offset {
1119 struct kvm_mmu_page *sp;
1120 unsigned int idx;
1121 } page[KVM_PAGE_ARRAY_NR];
1122 unsigned int nr;
1123};
1124
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001125#define for_each_unsync_children(bitmap, idx) \
1126 for (idx = find_first_bit(bitmap, 512); \
1127 idx < 512; \
1128 idx = find_next_bit(bitmap, 512, idx+1))
1129
Hannes Edercded19f2009-02-21 02:19:13 +01001130static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1131 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001132{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001133 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001134
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001135 if (sp->unsync)
1136 for (i=0; i < pvec->nr; i++)
1137 if (pvec->page[i].sp == sp)
1138 return 0;
1139
1140 pvec->page[pvec->nr].sp = sp;
1141 pvec->page[pvec->nr].idx = idx;
1142 pvec->nr++;
1143 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1144}
1145
1146static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1147 struct kvm_mmu_pages *pvec)
1148{
1149 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001150
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001151 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001152 u64 ent = sp->spt[i];
1153
Marcelo Tosatti87917232008-12-22 18:49:30 -02001154 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001155 struct kvm_mmu_page *child;
1156 child = page_header(ent & PT64_BASE_ADDR_MASK);
1157
1158 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001159 if (mmu_pages_add(pvec, child, i))
1160 return -ENOSPC;
1161
1162 ret = __mmu_unsync_walk(child, pvec);
1163 if (!ret)
1164 __clear_bit(i, sp->unsync_child_bitmap);
1165 else if (ret > 0)
1166 nr_unsync_leaf += ret;
1167 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001168 return ret;
1169 }
1170
1171 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001172 nr_unsync_leaf++;
1173 if (mmu_pages_add(pvec, child, i))
1174 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001175 }
1176 }
1177 }
1178
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001179 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001180 sp->unsync_children = 0;
1181
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001182 return nr_unsync_leaf;
1183}
1184
1185static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1186 struct kvm_mmu_pages *pvec)
1187{
1188 if (!sp->unsync_children)
1189 return 0;
1190
1191 mmu_pages_add(pvec, sp, 0);
1192 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193}
1194
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001195static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1196{
1197 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001198 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001199 sp->unsync = 0;
1200 --kvm->stat.mmu_unsync;
1201}
1202
Xiao Guangrong77758342010-06-04 21:53:54 +08001203static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1204 struct list_head *invalid_list);
1205static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1206 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001207
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001208#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1209 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001210 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1211 if ((sp)->gfn != (gfn)) {} else
1212
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001213#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1214 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001215 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1216 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1217 (sp)->role.invalid) {} else
1218
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001219static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001220 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001222 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001223 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001224 return 1;
1225 }
1226
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001227 if (clear_unsync) {
1228 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1229 kvm_flush_remote_tlbs(vcpu->kvm);
1230 kvm_unlink_unsync_page(vcpu->kvm, sp);
1231 }
1232
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001234 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001235 return 1;
1236 }
1237
1238 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001239 return 0;
1240}
1241
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001242static void mmu_convert_notrap(struct kvm_mmu_page *sp);
1243static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1244 struct kvm_mmu_page *sp)
1245{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001246 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001247 int ret;
1248
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001249 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001250 if (!ret)
1251 mmu_convert_notrap(sp);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001252 else
1253 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1254
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001255 return ret;
1256}
1257
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001258static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1259 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001260{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001261 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001262}
1263
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001264/* @gfn should be write-protected at the call site */
1265static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1266{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001267 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001268 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001269 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001270 bool flush = false;
1271
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001272 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001273 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001274 continue;
1275
1276 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1277 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
1278 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001279 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001280 continue;
1281 }
1282 kvm_unlink_unsync_page(vcpu->kvm, s);
1283 flush = true;
1284 }
1285
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001286 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001287 if (flush)
1288 kvm_mmu_flush_tlb(vcpu);
1289}
1290
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001291struct mmu_page_path {
1292 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1293 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001294};
1295
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001296#define for_each_sp(pvec, sp, parents, i) \
1297 for (i = mmu_pages_next(&pvec, &parents, -1), \
1298 sp = pvec.page[i].sp; \
1299 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1300 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001301
Hannes Edercded19f2009-02-21 02:19:13 +01001302static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1303 struct mmu_page_path *parents,
1304 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001305{
1306 int n;
1307
1308 for (n = i+1; n < pvec->nr; n++) {
1309 struct kvm_mmu_page *sp = pvec->page[n].sp;
1310
1311 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1312 parents->idx[0] = pvec->page[n].idx;
1313 return n;
1314 }
1315
1316 parents->parent[sp->role.level-2] = sp;
1317 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1318 }
1319
1320 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001321}
1322
Hannes Edercded19f2009-02-21 02:19:13 +01001323static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001324{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001325 struct kvm_mmu_page *sp;
1326 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001327
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001328 do {
1329 unsigned int idx = parents->idx[level];
1330
1331 sp = parents->parent[level];
1332 if (!sp)
1333 return;
1334
1335 --sp->unsync_children;
1336 WARN_ON((int)sp->unsync_children < 0);
1337 __clear_bit(idx, sp->unsync_child_bitmap);
1338 level++;
1339 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1340}
1341
1342static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1343 struct mmu_page_path *parents,
1344 struct kvm_mmu_pages *pvec)
1345{
1346 parents->parent[parent->role.level-1] = NULL;
1347 pvec->nr = 0;
1348}
1349
1350static void mmu_sync_children(struct kvm_vcpu *vcpu,
1351 struct kvm_mmu_page *parent)
1352{
1353 int i;
1354 struct kvm_mmu_page *sp;
1355 struct mmu_page_path parents;
1356 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001357 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001358
1359 kvm_mmu_pages_init(parent, &parents, &pages);
1360 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001361 int protected = 0;
1362
1363 for_each_sp(pages, sp, parents, i)
1364 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1365
1366 if (protected)
1367 kvm_flush_remote_tlbs(vcpu->kvm);
1368
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001369 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001370 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001371 mmu_pages_clear_parents(&parents);
1372 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001373 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001374 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001375 kvm_mmu_pages_init(parent, &parents, &pages);
1376 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001377}
1378
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001379static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1380 gfn_t gfn,
1381 gva_t gaddr,
1382 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001383 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001384 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001385 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001386{
1387 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001388 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001389 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001390 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001391 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001392
Avi Kivitya770f6f2008-12-21 19:20:09 +02001393 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001394 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001395 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001396 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001397 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001398 role.access = access;
Gui Jianfengb66d8002010-05-31 17:11:39 +08001399 if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001400 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1401 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1402 role.quadrant = quadrant;
1403 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001404 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001405 if (!need_sync && sp->unsync)
1406 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001407
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001408 if (sp->role.word != role.word)
1409 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001410
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001411 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1412 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001413
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001414 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1415 if (sp->unsync_children) {
1416 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1417 kvm_mmu_mark_parents_unsync(sp);
1418 } else if (sp->unsync)
1419 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001420
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001421 trace_kvm_mmu_get_page(sp, false);
1422 return sp;
1423 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001424 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001425 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001426 if (!sp)
1427 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001428 sp->gfn = gfn;
1429 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001430 hlist_add_head(&sp->hash_link,
1431 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001432 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001433 if (rmap_write_protect(vcpu->kvm, gfn))
1434 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001435 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1436 kvm_sync_pages(vcpu, gfn);
1437
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001438 account_shadowed(vcpu->kvm, gfn);
1439 }
Avi Kivity131d8272008-05-29 14:56:28 +03001440 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1441 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1442 else
1443 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001444 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001445 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001446}
1447
Avi Kivity2d111232008-12-25 14:39:47 +02001448static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1449 struct kvm_vcpu *vcpu, u64 addr)
1450{
1451 iterator->addr = addr;
1452 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1453 iterator->level = vcpu->arch.mmu.shadow_root_level;
1454 if (iterator->level == PT32E_ROOT_LEVEL) {
1455 iterator->shadow_addr
1456 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1457 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1458 --iterator->level;
1459 if (!iterator->shadow_addr)
1460 iterator->level = 0;
1461 }
1462}
1463
1464static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1465{
1466 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1467 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001468
1469 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1470 if (is_large_pte(*iterator->sptep))
1471 return false;
1472
Avi Kivity2d111232008-12-25 14:39:47 +02001473 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1474 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1475 return true;
1476}
1477
1478static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1479{
1480 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1481 --iterator->level;
1482}
1483
Avi Kivity90cb0522007-07-17 13:04:56 +03001484static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001485 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001486{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001487 unsigned i;
1488 u64 *pt;
1489 u64 ent;
1490
Avi Kivity4db35312007-11-21 15:28:32 +02001491 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001492
Avi Kivity697fe2e2007-01-05 16:36:46 -08001493 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1494 ent = pt[i];
1495
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001496 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001497 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001498 ent &= PT64_BASE_ADDR_MASK;
1499 mmu_page_remove_parent_pte(page_header(ent),
1500 &pt[i]);
1501 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001502 if (is_large_pte(ent))
1503 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001504 rmap_remove(kvm, &pt[i]);
1505 }
1506 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001507 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001508 }
Avi Kivitya4360362007-01-05 16:36:45 -08001509}
1510
Avi Kivity4db35312007-11-21 15:28:32 +02001511static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001512{
Avi Kivity4db35312007-11-21 15:28:32 +02001513 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001514}
1515
Avi Kivity12b7d282007-09-23 14:10:49 +02001516static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1517{
1518 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001519 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001520
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001521 kvm_for_each_vcpu(i, vcpu, kvm)
1522 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001523}
1524
Avi Kivity31aa2b42008-07-11 17:59:46 +03001525static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001526{
1527 u64 *parent_pte;
1528
Avi Kivity4db35312007-11-21 15:28:32 +02001529 while (sp->multimapped || sp->parent_pte) {
1530 if (!sp->multimapped)
1531 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001532 else {
1533 struct kvm_pte_chain *chain;
1534
Avi Kivity4db35312007-11-21 15:28:32 +02001535 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001536 struct kvm_pte_chain, link);
1537 parent_pte = chain->parent_ptes[0];
1538 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001539 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001540 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001541 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001542 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001543}
1544
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001545static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001546 struct kvm_mmu_page *parent,
1547 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001548{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001549 int i, zapped = 0;
1550 struct mmu_page_path parents;
1551 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001552
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001553 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001554 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001555
1556 kvm_mmu_pages_init(parent, &parents, &pages);
1557 while (mmu_unsync_walk(parent, &pages)) {
1558 struct kvm_mmu_page *sp;
1559
1560 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001561 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001562 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001563 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001564 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001565 kvm_mmu_pages_init(parent, &parents, &pages);
1566 }
1567
1568 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001569}
1570
Xiao Guangrong77758342010-06-04 21:53:54 +08001571static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1572 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001573{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001574 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001575
Xiao Guangrong77758342010-06-04 21:53:54 +08001576 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001577 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001578 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001579 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001580 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001581 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001582 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001583 if (sp->unsync)
1584 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001585 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001586 /* Count self */
1587 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001588 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001589 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001590 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001591 kvm_reload_remote_mmus(kvm);
1592 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001593
1594 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001595 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001596 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001597}
1598
Xiao Guangrong77758342010-06-04 21:53:54 +08001599static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1600 struct list_head *invalid_list)
1601{
1602 struct kvm_mmu_page *sp;
1603
1604 if (list_empty(invalid_list))
1605 return;
1606
1607 kvm_flush_remote_tlbs(kvm);
1608
1609 do {
1610 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1611 WARN_ON(!sp->role.invalid || sp->root_count);
1612 kvm_mmu_free_page(kvm, sp);
1613 } while (!list_empty(invalid_list));
1614
1615}
1616
Izik Eidus82ce2c92007-10-02 18:52:55 +02001617/*
1618 * Changing the number of mmu pages allocated to the vm
1619 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1620 */
1621void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1622{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001623 int used_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001624 LIST_HEAD(invalid_list);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001625
1626 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1627 used_pages = max(0, used_pages);
1628
Izik Eidus82ce2c92007-10-02 18:52:55 +02001629 /*
1630 * If we set the number of mmu pages to be smaller be than the
1631 * number of actived pages , we must to free some mmu pages before we
1632 * change the value
1633 */
1634
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001635 if (used_pages > kvm_nr_mmu_pages) {
Xiao Guangrong77662e02010-04-16 16:34:42 +08001636 while (used_pages > kvm_nr_mmu_pages &&
1637 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001638 struct kvm_mmu_page *page;
1639
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001640 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001641 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001642 used_pages -= kvm_mmu_prepare_zap_page(kvm, page,
1643 &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001644 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001645 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001646 kvm_nr_mmu_pages = used_pages;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001647 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001648 }
1649 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001650 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1651 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001652
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001653 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001654}
1655
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001656static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001657{
Avi Kivity4db35312007-11-21 15:28:32 +02001658 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001659 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001660 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001661 int r;
1662
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001663 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001664 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001665
1666 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001667 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1668 sp->role.word);
1669 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001670 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001671 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001672 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001673 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001674}
1675
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001676static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001677{
Avi Kivity4db35312007-11-21 15:28:32 +02001678 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001679 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001680 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001681
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001682 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001683 pgprintk("%s: zap %lx %x\n",
1684 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001685 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001686 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001687 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001688}
1689
Avi Kivity38c335f2007-11-21 14:20:22 +02001690static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001692 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001693 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694
Sheng Yang291f26b2008-10-16 17:30:57 +08001695 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696}
1697
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001698static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1699{
1700 int i;
1701 u64 *pt = sp->spt;
1702
1703 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1704 return;
1705
1706 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1707 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001708 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001709 }
1710}
1711
Sheng Yang74be52e2008-10-09 16:01:56 +08001712/*
1713 * The function is based on mtrr_type_lookup() in
1714 * arch/x86/kernel/cpu/mtrr/generic.c
1715 */
1716static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1717 u64 start, u64 end)
1718{
1719 int i;
1720 u64 base, mask;
1721 u8 prev_match, curr_match;
1722 int num_var_ranges = KVM_NR_VAR_MTRR;
1723
1724 if (!mtrr_state->enabled)
1725 return 0xFF;
1726
1727 /* Make end inclusive end, instead of exclusive */
1728 end--;
1729
1730 /* Look in fixed ranges. Just return the type as per start */
1731 if (mtrr_state->have_fixed && (start < 0x100000)) {
1732 int idx;
1733
1734 if (start < 0x80000) {
1735 idx = 0;
1736 idx += (start >> 16);
1737 return mtrr_state->fixed_ranges[idx];
1738 } else if (start < 0xC0000) {
1739 idx = 1 * 8;
1740 idx += ((start - 0x80000) >> 14);
1741 return mtrr_state->fixed_ranges[idx];
1742 } else if (start < 0x1000000) {
1743 idx = 3 * 8;
1744 idx += ((start - 0xC0000) >> 12);
1745 return mtrr_state->fixed_ranges[idx];
1746 }
1747 }
1748
1749 /*
1750 * Look in variable ranges
1751 * Look of multiple ranges matching this address and pick type
1752 * as per MTRR precedence
1753 */
1754 if (!(mtrr_state->enabled & 2))
1755 return mtrr_state->def_type;
1756
1757 prev_match = 0xFF;
1758 for (i = 0; i < num_var_ranges; ++i) {
1759 unsigned short start_state, end_state;
1760
1761 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1762 continue;
1763
1764 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1765 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1766 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1767 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1768
1769 start_state = ((start & mask) == (base & mask));
1770 end_state = ((end & mask) == (base & mask));
1771 if (start_state != end_state)
1772 return 0xFE;
1773
1774 if ((start & mask) != (base & mask))
1775 continue;
1776
1777 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1778 if (prev_match == 0xFF) {
1779 prev_match = curr_match;
1780 continue;
1781 }
1782
1783 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1784 curr_match == MTRR_TYPE_UNCACHABLE)
1785 return MTRR_TYPE_UNCACHABLE;
1786
1787 if ((prev_match == MTRR_TYPE_WRBACK &&
1788 curr_match == MTRR_TYPE_WRTHROUGH) ||
1789 (prev_match == MTRR_TYPE_WRTHROUGH &&
1790 curr_match == MTRR_TYPE_WRBACK)) {
1791 prev_match = MTRR_TYPE_WRTHROUGH;
1792 curr_match = MTRR_TYPE_WRTHROUGH;
1793 }
1794
1795 if (prev_match != curr_match)
1796 return MTRR_TYPE_UNCACHABLE;
1797 }
1798
1799 if (prev_match != 0xFF)
1800 return prev_match;
1801
1802 return mtrr_state->def_type;
1803}
1804
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001805u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001806{
1807 u8 mtrr;
1808
1809 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1810 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1811 if (mtrr == 0xfe || mtrr == 0xff)
1812 mtrr = MTRR_TYPE_WRBACK;
1813 return mtrr;
1814}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001815EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001816
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001817static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001818{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001819 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001820 ++vcpu->kvm->stat.mmu_unsync;
1821 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001822
Xiao Guangrong6b184932010-04-16 21:29:17 +08001823 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001824 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001825}
1826
1827static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1828{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001829 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001830 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001831
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001832 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001833 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001834 continue;
1835 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1836 __kvm_unsync_page(vcpu, s);
1837 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001838}
1839
1840static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1841 bool can_unsync)
1842{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001843 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001844 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001845 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001846
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001847 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001848 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001849 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001850
1851 if (!need_unsync && !s->unsync) {
1852 if (!can_unsync || !oos_shadow)
1853 return 1;
1854 need_unsync = true;
1855 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001856 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001857 if (need_unsync)
1858 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001859 return 0;
1860}
1861
Avi Kivityd555c332009-06-10 14:24:23 +03001862static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001863 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001864 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001865 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001866 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001867{
1868 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001869 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001870
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001871 /*
1872 * We don't set the accessed bit, since we sometimes want to see
1873 * whether the guest actually used the pte (in order to detect
1874 * demand paging).
1875 */
Sheng Yang7b523452008-04-25 21:13:50 +08001876 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001877 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001878 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001879 if (!dirty)
1880 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001881 if (pte_access & ACC_EXEC_MASK)
1882 spte |= shadow_x_mask;
1883 else
1884 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001885 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001886 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001887 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001888 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001889 if (tdp_enabled)
1890 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1891 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001892
Izik Eidus14032832009-09-23 21:47:17 +03001893 if (reset_host_protection)
1894 spte |= SPTE_HOST_WRITEABLE;
1895
Anthony Liguori35149e22008-04-02 14:46:56 -05001896 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001897
1898 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity8184dd32010-05-27 14:22:51 +03001899 || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
1900 && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001901
Joerg Roedel852e3c12009-07-27 16:30:44 +02001902 if (level > PT_PAGE_TABLE_LEVEL &&
1903 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001904 ret = 1;
Xiao Guangrong6d742292010-05-13 10:07:00 +08001905 rmap_remove(vcpu->kvm, sptep);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001906 spte = shadow_trap_nonpresent_pte;
1907 goto set_pte;
1908 }
1909
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001910 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001911
Avi Kivity69325a12010-05-27 14:35:58 +03001912 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1913 spte &= ~PT_USER_MASK;
1914
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001915 /*
1916 * Optimization: for pte sync, if spte was writable the hash
1917 * lookup is unnecessary (and expensive). Write protection
1918 * is responsibility of mmu_get_page / kvm_sync_page.
1919 * Same reasoning can be applied to dirty page accounting.
1920 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001921 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001922 goto set_pte;
1923
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001924 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001925 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001926 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001927 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001928 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001929 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001930 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001931 }
1932 }
1933
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001934 if (pte_access & ACC_WRITE_MASK)
1935 mark_page_dirty(vcpu->kvm, gfn);
1936
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001937set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001938 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001939 return ret;
1940}
1941
Avi Kivityd555c332009-06-10 14:24:23 +03001942static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001943 unsigned pt_access, unsigned pte_access,
1944 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001945 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001946 pfn_t pfn, bool speculative,
1947 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001948{
1949 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001950 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001951 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001952
1953 pgprintk("%s: spte %llx access %x write_fault %d"
1954 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001955 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001956 write_fault, user_fault, gfn);
1957
Avi Kivityd555c332009-06-10 14:24:23 +03001958 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001959 /*
1960 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1961 * the parent of the now unreachable PTE.
1962 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001963 if (level > PT_PAGE_TABLE_LEVEL &&
1964 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001965 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001966 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001967
1968 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001969 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03001970 __set_spte(sptep, shadow_trap_nonpresent_pte);
1971 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03001972 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001973 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001974 spte_to_pfn(*sptep), pfn);
1975 rmap_remove(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08001976 __set_spte(sptep, shadow_trap_nonpresent_pte);
1977 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001978 } else
1979 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001980 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001981
Avi Kivityd555c332009-06-10 14:24:23 +03001982 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001983 dirty, level, gfn, pfn, speculative, true,
1984 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001985 if (write_fault)
1986 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08001987 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001988 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001989
Avi Kivityd555c332009-06-10 14:24:23 +03001990 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001991 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001992 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001993 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1994 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001995 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001996 ++vcpu->kvm->stat.lpages;
1997
Avi Kivityd555c332009-06-10 14:24:23 +03001998 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001999 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002000 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03002001 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002002 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002003 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02002004 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002005 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05002006 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02002007 else
Anthony Liguori35149e22008-04-02 14:46:56 -05002008 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002009 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002010 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002011 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002012 vcpu->arch.last_pte_gfn = gfn;
2013 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002014}
2015
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2017{
2018}
2019
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002020static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002021 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022{
Avi Kivity9f652d22008-12-25 14:54:25 +02002023 struct kvm_shadow_walk_iterator iterator;
2024 struct kvm_mmu_page *sp;
2025 int pt_write = 0;
2026 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027
Avi Kivity9f652d22008-12-25 14:54:25 +02002028 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002029 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002030 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2031 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002032 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02002033 ++vcpu->stat.pf_fixed;
2034 break;
2035 }
2036
2037 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002038 u64 base_addr = iterator.addr;
2039
2040 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2041 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002042 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2043 iterator.level - 1,
2044 1, ACC_ALL, iterator.sptep);
2045 if (!sp) {
2046 pgprintk("nonpaging_map: ENOMEM\n");
2047 kvm_release_pfn_clean(pfn);
2048 return -ENOMEM;
2049 }
2050
Avi Kivityd555c332009-06-10 14:24:23 +03002051 __set_spte(iterator.sptep,
2052 __pa(sp->spt)
2053 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2054 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002055 }
2056 }
2057 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058}
2059
Huang Yingbf998152010-05-31 14:28:19 +08002060static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2061{
2062 char buf[1];
2063 void __user *hva;
2064 int r;
2065
2066 /* Touch the page, so send SIGBUS */
2067 hva = (void __user *)gfn_to_hva(kvm, gfn);
2068 r = copy_from_user(buf, hva, 1);
2069}
2070
2071static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2072{
2073 kvm_release_pfn_clean(pfn);
2074 if (is_hwpoison_pfn(pfn)) {
2075 kvm_send_hwpoison_signal(kvm, gfn);
2076 return 0;
2077 }
2078 return 1;
2079}
2080
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002081static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2082{
2083 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002084 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002085 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002086 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002087
Joerg Roedel852e3c12009-07-27 16:30:44 +02002088 level = mapping_level(vcpu, gfn);
2089
2090 /*
2091 * This path builds a PAE pagetable - so we can map 2mb pages at
2092 * maximum. Therefore check if the level is larger than that.
2093 */
2094 if (level > PT_DIRECTORY_LEVEL)
2095 level = PT_DIRECTORY_LEVEL;
2096
2097 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002098
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002099 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002100 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002101 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002102
Avi Kivityd196e342008-01-24 11:44:11 +02002103 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002104 if (is_error_pfn(pfn))
2105 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002106
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002107 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002108 if (mmu_notifier_retry(vcpu, mmu_seq))
2109 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002110 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002111 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002112 spin_unlock(&vcpu->kvm->mmu_lock);
2113
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002114
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002115 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002116
2117out_unlock:
2118 spin_unlock(&vcpu->kvm->mmu_lock);
2119 kvm_release_pfn_clean(pfn);
2120 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002121}
2122
2123
Avi Kivity17ac10a2007-01-05 16:36:40 -08002124static void mmu_free_roots(struct kvm_vcpu *vcpu)
2125{
2126 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002127 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002128 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002129
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002130 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002131 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002132 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002133 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2134 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002135
Avi Kivity4db35312007-11-21 15:28:32 +02002136 sp = page_header(root);
2137 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002138 if (!sp->root_count && sp->role.invalid) {
2139 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2140 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2141 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002142 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002143 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002144 return;
2145 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002146 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002147 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002148
Avi Kivity417726a2007-04-12 17:35:58 +03002149 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002150 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002151 sp = page_header(root);
2152 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002153 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002154 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2155 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002156 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002157 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002158 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002159 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002160 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002161 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002162}
2163
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002164static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2165{
2166 int ret = 0;
2167
2168 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2169 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2170 ret = 1;
2171 }
2172
2173 return ret;
2174}
2175
2176static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002177{
2178 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002179 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002180 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002181 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002182 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002183
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002184 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002185
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002186 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2187 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002188
2189 ASSERT(!VALID_PAGE(root));
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002190 if (mmu_check_root(vcpu, root_gfn))
2191 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002192 if (tdp_enabled) {
2193 direct = 1;
2194 root_gfn = 0;
2195 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002196 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002197 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002198 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002199 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002200 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002201 root = __pa(sp->spt);
2202 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002203 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002204 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002205 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002206 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002207 direct = !is_paging(vcpu);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002208 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002209 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002210
2211 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002212 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002213 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002214 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002215 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002216 continue;
2217 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002218 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002219 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002220 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002221 if (mmu_check_root(vcpu, root_gfn))
2222 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002223 if (tdp_enabled) {
2224 direct = 1;
2225 root_gfn = i << 30;
2226 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002227 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002228 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002229 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002230 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002231 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002232 root = __pa(sp->spt);
2233 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002234 spin_unlock(&vcpu->kvm->mmu_lock);
2235
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002236 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002237 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002239 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002240}
2241
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002242static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2243{
2244 int i;
2245 struct kvm_mmu_page *sp;
2246
2247 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2248 return;
2249 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2250 hpa_t root = vcpu->arch.mmu.root_hpa;
2251 sp = page_header(root);
2252 mmu_sync_children(vcpu, sp);
2253 return;
2254 }
2255 for (i = 0; i < 4; ++i) {
2256 hpa_t root = vcpu->arch.mmu.pae_root[i];
2257
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002258 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002259 root &= PT64_BASE_ADDR_MASK;
2260 sp = page_header(root);
2261 mmu_sync_children(vcpu, sp);
2262 }
2263 }
2264}
2265
2266void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2267{
2268 spin_lock(&vcpu->kvm->mmu_lock);
2269 mmu_sync_roots(vcpu);
2270 spin_unlock(&vcpu->kvm->mmu_lock);
2271}
2272
Gleb Natapov1871c602010-02-10 14:21:32 +02002273static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2274 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275{
Gleb Natapov1871c602010-02-10 14:21:32 +02002276 if (error)
2277 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 return vaddr;
2279}
2280
2281static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002282 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283{
Avi Kivitye8332402007-12-09 18:43:00 +02002284 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002285 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002287 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002288 r = mmu_topup_memory_caches(vcpu);
2289 if (r)
2290 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002291
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002293 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294
Avi Kivitye8332402007-12-09 18:43:00 +02002295 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296
Avi Kivitye8332402007-12-09 18:43:00 +02002297 return nonpaging_map(vcpu, gva & PAGE_MASK,
2298 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299}
2300
Joerg Roedelfb72d162008-02-07 13:47:44 +01002301static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2302 u32 error_code)
2303{
Anthony Liguori35149e22008-04-02 14:46:56 -05002304 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002305 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002306 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002307 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002308 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002309
2310 ASSERT(vcpu);
2311 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2312
2313 r = mmu_topup_memory_caches(vcpu);
2314 if (r)
2315 return r;
2316
Joerg Roedel852e3c12009-07-27 16:30:44 +02002317 level = mapping_level(vcpu, gfn);
2318
2319 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2320
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002321 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002322 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002323 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Huang Yingbf998152010-05-31 14:28:19 +08002324 if (is_error_pfn(pfn))
2325 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002326 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002327 if (mmu_notifier_retry(vcpu, mmu_seq))
2328 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002329 kvm_mmu_free_some_pages(vcpu);
2330 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002331 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002332 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002333
2334 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002335
2336out_unlock:
2337 spin_unlock(&vcpu->kvm->mmu_lock);
2338 kvm_release_pfn_clean(pfn);
2339 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002340}
2341
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342static void nonpaging_free(struct kvm_vcpu *vcpu)
2343{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002344 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345}
2346
2347static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2348{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350
2351 context->new_cr3 = nonpaging_new_cr3;
2352 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 context->gva_to_gpa = nonpaging_gva_to_gpa;
2354 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002355 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002356 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002357 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002358 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002360 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 return 0;
2362}
2363
Avi Kivityd835dfe2007-11-21 02:57:59 +02002364void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002366 ++vcpu->stat.tlb_flush;
Xiao Guangrong3b5d1322010-06-08 20:07:01 +08002367 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368}
2369
2370static void paging_new_cr3(struct kvm_vcpu *vcpu)
2371{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002372 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002373 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374}
2375
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376static void inject_page_fault(struct kvm_vcpu *vcpu,
2377 u64 addr,
2378 u32 err_code)
2379{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002380 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381}
2382
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383static void paging_free(struct kvm_vcpu *vcpu)
2384{
2385 nonpaging_free(vcpu);
2386}
2387
Dong, Eddie82725b22009-03-30 16:21:08 +08002388static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2389{
2390 int bit7;
2391
2392 bit7 = (gpte >> 7) & 1;
2393 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2394}
2395
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396#define PTTYPE 64
2397#include "paging_tmpl.h"
2398#undef PTTYPE
2399
2400#define PTTYPE 32
2401#include "paging_tmpl.h"
2402#undef PTTYPE
2403
Dong, Eddie82725b22009-03-30 16:21:08 +08002404static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2405{
2406 struct kvm_mmu *context = &vcpu->arch.mmu;
2407 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2408 u64 exb_bit_rsvd = 0;
2409
2410 if (!is_nx(vcpu))
2411 exb_bit_rsvd = rsvd_bits(63, 63);
2412 switch (level) {
2413 case PT32_ROOT_LEVEL:
2414 /* no rsvd bits for 2 level 4K page table entries */
2415 context->rsvd_bits_mask[0][1] = 0;
2416 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002417 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2418
2419 if (!is_pse(vcpu)) {
2420 context->rsvd_bits_mask[1][1] = 0;
2421 break;
2422 }
2423
Dong, Eddie82725b22009-03-30 16:21:08 +08002424 if (is_cpuid_PSE36())
2425 /* 36bits PSE 4MB page */
2426 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2427 else
2428 /* 32 bits PSE 4MB page */
2429 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002430 break;
2431 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002432 context->rsvd_bits_mask[0][2] =
2433 rsvd_bits(maxphyaddr, 63) |
2434 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002435 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002436 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002437 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2438 rsvd_bits(maxphyaddr, 62); /* PTE */
2439 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2440 rsvd_bits(maxphyaddr, 62) |
2441 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002442 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002443 break;
2444 case PT64_ROOT_LEVEL:
2445 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2446 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2447 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2448 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2449 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002450 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002451 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2452 rsvd_bits(maxphyaddr, 51);
2453 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002454 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2455 rsvd_bits(maxphyaddr, 51) |
2456 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002457 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002458 rsvd_bits(maxphyaddr, 51) |
2459 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002460 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002461 break;
2462 }
2463}
2464
Avi Kivity17ac10a2007-01-05 16:36:40 -08002465static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002467 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468
2469 ASSERT(is_pae(vcpu));
2470 context->new_cr3 = paging_new_cr3;
2471 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002473 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002474 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002475 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002477 context->root_level = level;
2478 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002479 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 return 0;
2481}
2482
Avi Kivity17ac10a2007-01-05 16:36:40 -08002483static int paging64_init_context(struct kvm_vcpu *vcpu)
2484{
Dong, Eddie82725b22009-03-30 16:21:08 +08002485 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002486 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2487}
2488
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489static int paging32_init_context(struct kvm_vcpu *vcpu)
2490{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002491 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492
Dong, Eddie82725b22009-03-30 16:21:08 +08002493 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 context->new_cr3 = paging_new_cr3;
2495 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 context->gva_to_gpa = paging32_gva_to_gpa;
2497 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002498 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002499 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002500 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 context->root_level = PT32_ROOT_LEVEL;
2502 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002503 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 return 0;
2505}
2506
2507static int paging32E_init_context(struct kvm_vcpu *vcpu)
2508{
Dong, Eddie82725b22009-03-30 16:21:08 +08002509 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002510 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511}
2512
Joerg Roedelfb72d162008-02-07 13:47:44 +01002513static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2514{
2515 struct kvm_mmu *context = &vcpu->arch.mmu;
2516
2517 context->new_cr3 = nonpaging_new_cr3;
2518 context->page_fault = tdp_page_fault;
2519 context->free = nonpaging_free;
2520 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002521 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002522 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002523 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002524 context->root_hpa = INVALID_PAGE;
2525
2526 if (!is_paging(vcpu)) {
2527 context->gva_to_gpa = nonpaging_gva_to_gpa;
2528 context->root_level = 0;
2529 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002530 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002531 context->gva_to_gpa = paging64_gva_to_gpa;
2532 context->root_level = PT64_ROOT_LEVEL;
2533 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002534 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002535 context->gva_to_gpa = paging64_gva_to_gpa;
2536 context->root_level = PT32E_ROOT_LEVEL;
2537 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002538 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002539 context->gva_to_gpa = paging32_gva_to_gpa;
2540 context->root_level = PT32_ROOT_LEVEL;
2541 }
2542
2543 return 0;
2544}
2545
2546static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002548 int r;
2549
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002551 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552
2553 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002554 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002555 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002556 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002558 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002560 r = paging32_init_context(vcpu);
2561
Avi Kivity5b7e0102010-04-14 19:20:03 +03002562 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Avi Kivity3dbe1412010-05-12 11:48:18 +03002563 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002564
2565 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566}
2567
Joerg Roedelfb72d162008-02-07 13:47:44 +01002568static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2569{
Anthony Liguori35149e22008-04-02 14:46:56 -05002570 vcpu->arch.update_pte.pfn = bad_pfn;
2571
Joerg Roedelfb72d162008-02-07 13:47:44 +01002572 if (tdp_enabled)
2573 return init_kvm_tdp_mmu(vcpu);
2574 else
2575 return init_kvm_softmmu(vcpu);
2576}
2577
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2579{
2580 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002581 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2582 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002583 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584}
2585
2586int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2587{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002588 destroy_kvm_mmu(vcpu);
2589 return init_kvm_mmu(vcpu);
2590}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002591EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002592
2593int kvm_mmu_load(struct kvm_vcpu *vcpu)
2594{
Avi Kivity714b93d2007-01-05 16:36:53 -08002595 int r;
2596
Avi Kivitye2dec932007-01-05 16:36:54 -08002597 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002598 if (r)
2599 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002600 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03002601 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002602 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002603 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002604 if (r)
2605 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002606 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002607 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002608out:
2609 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002611EXPORT_SYMBOL_GPL(kvm_mmu_load);
2612
2613void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2614{
2615 mmu_free_roots(vcpu);
2616}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617
Avi Kivity09072da2007-05-01 14:16:52 +03002618static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002619 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002620 u64 *spte)
2621{
2622 u64 pte;
2623 struct kvm_mmu_page *child;
2624
2625 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002626 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002627 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002628 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002629 else {
2630 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002631 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002632 }
2633 }
Avi Kivityd555c332009-06-10 14:24:23 +03002634 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002635 if (is_large_pte(pte))
2636 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002637}
2638
Avi Kivity00284252007-05-01 16:53:31 +03002639static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002640 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002641 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002642 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002643{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002644 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002645 ++vcpu->kvm->stat.mmu_pde_zapped;
2646 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002647 }
Avi Kivity00284252007-05-01 16:53:31 +03002648
Avi Kivity4cee5762007-11-18 16:37:07 +02002649 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002650 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002651 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002652 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002653 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002654}
2655
Avi Kivity79539ce2007-11-21 02:06:21 +02002656static bool need_remote_flush(u64 old, u64 new)
2657{
2658 if (!is_shadow_present_pte(old))
2659 return false;
2660 if (!is_shadow_present_pte(new))
2661 return true;
2662 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2663 return true;
2664 old ^= PT64_NX_MASK;
2665 new ^= PT64_NX_MASK;
2666 return (old & ~new & PT64_PERM_MASK) != 0;
2667}
2668
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002669static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
2670 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002671{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002672 if (zap_page)
2673 return;
2674
2675 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002676 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002677 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02002678 kvm_mmu_flush_tlb(vcpu);
2679}
2680
Avi Kivity12b7d282007-09-23 14:10:49 +02002681static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2682{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002683 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002684
Sheng Yang7b523452008-04-25 21:13:50 +08002685 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002686}
2687
Avi Kivityd7824ff2007-12-30 12:29:05 +02002688static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02002689 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02002690{
2691 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002692 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002693
Avi Kivity43a37952009-06-10 14:12:05 +03002694 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002695 return;
2696 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002697
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002698 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002699 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002700 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002701
Anthony Liguori35149e22008-04-02 14:46:56 -05002702 if (is_error_pfn(pfn)) {
2703 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002704 return;
2705 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002706 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002707 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002708}
2709
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002710static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2711{
2712 u64 *spte = vcpu->arch.last_pte_updated;
2713
2714 if (spte
2715 && vcpu->arch.last_pte_gfn == gfn
2716 && shadow_accessed_mask
2717 && !(*spte & shadow_accessed_mask)
2718 && is_shadow_present_pte(*spte))
2719 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2720}
2721
Avi Kivity09072da2007-05-01 14:16:52 +03002722void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002723 const u8 *new, int bytes,
2724 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002725{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002726 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002727 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002728 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002729 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002730 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002731 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002732 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002733 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002734 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002735 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002736 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002737 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002738 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002739 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002740 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02002741 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002742 bool remote_flush, local_flush, zap_page;
2743
2744 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002745
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002746 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02002747
Avi Kivity08e850c2010-03-15 13:59:57 +02002748 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2749
2750 /*
2751 * Assume that the pte write on a page table of the same type
2752 * as the current vcpu paging mode. This is nearly always true
2753 * (might be false while changing modes). Note it is verified later
2754 * by update_pte().
2755 */
2756 if ((is_pae(vcpu) && bytes == 4) || !new) {
2757 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2758 if (is_pae(vcpu)) {
2759 gpa &= ~(gpa_t)7;
2760 bytes = 8;
2761 }
2762 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2763 if (r)
2764 gentry = 0;
2765 new = (const u8 *)&gentry;
2766 }
2767
Avi Kivity72016f32010-03-15 13:59:53 +02002768 switch (bytes) {
2769 case 4:
2770 gentry = *(const u32 *)new;
2771 break;
2772 case 8:
2773 gentry = *(const u64 *)new;
2774 break;
2775 default:
2776 gentry = 0;
2777 break;
2778 }
2779
Avi Kivity72016f32010-03-15 13:59:53 +02002780 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002781 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02002782 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2783 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002784 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002785 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002786 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002787 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002788 if (guest_initiated) {
2789 if (gfn == vcpu->arch.last_pt_write_gfn
2790 && !last_updated_pte_accessed(vcpu)) {
2791 ++vcpu->arch.last_pt_write_count;
2792 if (vcpu->arch.last_pt_write_count >= 3)
2793 flooded = 1;
2794 } else {
2795 vcpu->arch.last_pt_write_gfn = gfn;
2796 vcpu->arch.last_pt_write_count = 1;
2797 vcpu->arch.last_pte_updated = NULL;
2798 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002799 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08002800
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002801 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03002802 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002803 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002804 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002805 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002806 /*
2807 * Misaligned accesses are too much trouble to fix
2808 * up; also, they usually indicate a page is not used
2809 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002810 *
2811 * If we're seeing too many writes to a page,
2812 * it may no longer be a page table, or we may be
2813 * forking, in which case it is better to unmap the
2814 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002815 */
2816 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002817 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002818 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002819 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002820 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002821 continue;
2822 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002823 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002824 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002825 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03002826 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002827 page_offset <<= 1; /* 32->64 */
2828 /*
2829 * A 32-bit pde maps 4MB while the shadow pdes map
2830 * only 2MB. So we need to double the offset again
2831 * and zap two pdes instead of one.
2832 */
2833 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002834 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002835 page_offset <<= 1;
2836 npte = 2;
2837 }
Avi Kivityfce06572007-05-01 16:44:05 +03002838 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002839 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002840 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002841 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002842 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002843 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02002844 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02002845 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002846 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002847 mmu_pte_write_zap_pte(vcpu, sp, spte);
Avi Kivity72016f32010-03-15 13:59:53 +02002848 if (gentry)
2849 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002850 if (!remote_flush && need_remote_flush(entry, *spte))
2851 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02002852 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002853 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002854 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08002855 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002856 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityc7addb92007-09-16 18:58:32 +02002857 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002858 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002859 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2860 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2861 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002862 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002863}
2864
Avi Kivitya4360362007-01-05 16:36:45 -08002865int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2866{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002867 gpa_t gpa;
2868 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002869
Avi Kivity60f24782009-08-27 13:37:06 +03002870 if (tdp_enabled)
2871 return 0;
2872
Gleb Natapov1871c602010-02-10 14:21:32 +02002873 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002874
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002875 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002876 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002877 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002878 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002879}
Avi Kivity577bdc42008-07-19 08:57:05 +03002880EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002881
Avi Kivity22d95b12007-09-14 20:26:06 +03002882void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002883{
Xiao Guangrong103ad252010-06-04 21:54:38 +08002884 int free_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002885 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08002886
2887 free_pages = vcpu->kvm->arch.n_free_mmu_pages;
2888 while (free_pages < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03002889 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002890 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002891
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002892 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002893 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002894 free_pages += kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2895 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02002896 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002897 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002898 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08002899}
Avi Kivityebeace82007-01-05 16:36:47 -08002900
Avi Kivity30677142007-10-28 18:48:59 +02002901int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2902{
2903 int r;
2904 enum emulation_result er;
2905
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002906 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002907 if (r < 0)
2908 goto out;
2909
2910 if (!r) {
2911 r = 1;
2912 goto out;
2913 }
2914
Avi Kivityb733bfb2007-10-28 18:52:05 +02002915 r = mmu_topup_memory_caches(vcpu);
2916 if (r)
2917 goto out;
2918
Avi Kivity851ba692009-08-24 11:10:17 +03002919 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002920
2921 switch (er) {
2922 case EMULATE_DONE:
2923 return 1;
2924 case EMULATE_DO_MMIO:
2925 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002926 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02002927 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002928 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002929 default:
2930 BUG();
2931 }
2932out:
Avi Kivity30677142007-10-28 18:48:59 +02002933 return r;
2934}
2935EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2936
Marcelo Tosattia7052892008-09-23 13:18:35 -03002937void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2938{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002939 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002940 kvm_mmu_flush_tlb(vcpu);
2941 ++vcpu->stat.invlpg;
2942}
2943EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2944
Joerg Roedel18552672008-02-07 13:47:41 +01002945void kvm_enable_tdp(void)
2946{
2947 tdp_enabled = true;
2948}
2949EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2950
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002951void kvm_disable_tdp(void)
2952{
2953 tdp_enabled = false;
2954}
2955EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2956
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957static void free_mmu_pages(struct kvm_vcpu *vcpu)
2958{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002959 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960}
2961
2962static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2963{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002964 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 int i;
2966
2967 ASSERT(vcpu);
2968
Avi Kivity17ac10a2007-01-05 16:36:40 -08002969 /*
2970 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2971 * Therefore we need to allocate shadow page tables in the first
2972 * 4GB of memory, which happens to fit the DMA32 zone.
2973 */
2974 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2975 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002976 return -ENOMEM;
2977
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002978 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002979 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002980 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002981
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983}
2984
Ingo Molnar8018c272006-12-29 16:50:01 -08002985int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002988 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989
Ingo Molnar8018c272006-12-29 16:50:01 -08002990 return alloc_mmu_pages(vcpu);
2991}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992
Ingo Molnar8018c272006-12-29 16:50:01 -08002993int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2994{
2995 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002996 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002997
Ingo Molnar8018c272006-12-29 16:50:01 -08002998 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999}
3000
3001void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3002{
3003 ASSERT(vcpu);
3004
3005 destroy_kvm_mmu(vcpu);
3006 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08003007 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008}
3009
Avi Kivity90cb0522007-07-17 13:04:56 +03003010void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011{
Avi Kivity4db35312007-11-21 15:28:32 +02003012 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003014 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 int i;
3016 u64 *pt;
3017
Sheng Yang291f26b2008-10-16 17:30:57 +08003018 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 continue;
3020
Avi Kivity4db35312007-11-21 15:28:32 +02003021 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3023 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003024 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 }
Avi Kivity171d5952008-08-27 16:40:51 +03003027 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003029
Avi Kivity90cb0522007-07-17 13:04:56 +03003030void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003031{
Avi Kivity4db35312007-11-21 15:28:32 +02003032 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003033 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003034
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003035 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003036restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003037 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003038 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003039 goto restart;
3040
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003041 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003042 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003043}
3044
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003045static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3046 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003047{
3048 struct kvm_mmu_page *page;
3049
3050 page = container_of(kvm->arch.active_mmu_pages.prev,
3051 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003052 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003053}
3054
Dave Chinner7f8275d2010-07-19 14:56:17 +10003055static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003056{
3057 struct kvm *kvm;
3058 struct kvm *kvm_freed = NULL;
3059 int cache_count = 0;
3060
3061 spin_lock(&kvm_lock);
3062
3063 list_for_each_entry(kvm, &vm_list, vm_list) {
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003064 int npages, idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003065 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003066
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003067 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003068 spin_lock(&kvm->mmu_lock);
3069 npages = kvm->arch.n_alloc_mmu_pages -
3070 kvm->arch.n_free_mmu_pages;
3071 cache_count += npages;
3072 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003073 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3074 &invalid_list);
Gui Jianfengd35b8dd2010-04-27 10:39:49 +08003075 cache_count -= freed_pages;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003076 kvm_freed = kvm;
3077 }
3078 nr_to_scan--;
3079
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003080 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003081 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003082 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003083 }
3084 if (kvm_freed)
3085 list_move_tail(&kvm_freed->vm_list, &vm_list);
3086
3087 spin_unlock(&kvm_lock);
3088
3089 return cache_count;
3090}
3091
3092static struct shrinker mmu_shrinker = {
3093 .shrink = mmu_shrink,
3094 .seeks = DEFAULT_SEEKS * 10,
3095};
3096
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003097static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003098{
3099 if (pte_chain_cache)
3100 kmem_cache_destroy(pte_chain_cache);
3101 if (rmap_desc_cache)
3102 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003103 if (mmu_page_header_cache)
3104 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003105}
3106
Izik Eidus3ee16c82008-03-30 15:17:21 +03003107void kvm_mmu_module_exit(void)
3108{
3109 mmu_destroy_caches();
3110 unregister_shrinker(&mmu_shrinker);
3111}
3112
Avi Kivityb5a33a72007-04-15 16:31:09 +03003113int kvm_mmu_module_init(void)
3114{
3115 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3116 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003117 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003118 if (!pte_chain_cache)
3119 goto nomem;
3120 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3121 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003122 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003123 if (!rmap_desc_cache)
3124 goto nomem;
3125
Avi Kivityd3d25b02007-05-30 12:34:53 +03003126 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3127 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003128 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003129 if (!mmu_page_header_cache)
3130 goto nomem;
3131
Izik Eidus3ee16c82008-03-30 15:17:21 +03003132 register_shrinker(&mmu_shrinker);
3133
Avi Kivityb5a33a72007-04-15 16:31:09 +03003134 return 0;
3135
3136nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003137 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003138 return -ENOMEM;
3139}
3140
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003141/*
3142 * Caculate mmu pages needed for kvm.
3143 */
3144unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3145{
3146 int i;
3147 unsigned int nr_mmu_pages;
3148 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003149 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003150
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003151 slots = kvm_memslots(kvm);
3152
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003153 for (i = 0; i < slots->nmemslots; i++)
3154 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003155
3156 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3157 nr_mmu_pages = max(nr_mmu_pages,
3158 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3159
3160 return nr_mmu_pages;
3161}
3162
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003163static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3164 unsigned len)
3165{
3166 if (len > buffer->len)
3167 return NULL;
3168 return buffer->ptr;
3169}
3170
3171static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3172 unsigned len)
3173{
3174 void *ret;
3175
3176 ret = pv_mmu_peek_buffer(buffer, len);
3177 if (!ret)
3178 return ret;
3179 buffer->ptr += len;
3180 buffer->len -= len;
3181 buffer->processed += len;
3182 return ret;
3183}
3184
3185static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3186 gpa_t addr, gpa_t value)
3187{
3188 int bytes = 8;
3189 int r;
3190
3191 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3192 bytes = 4;
3193
3194 r = mmu_topup_memory_caches(vcpu);
3195 if (r)
3196 return r;
3197
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003198 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003199 return -EFAULT;
3200
3201 return 1;
3202}
3203
3204static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3205{
Avi Kivity23902182010-06-10 17:02:16 +03003206 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003207 return 1;
3208}
3209
3210static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3211{
3212 spin_lock(&vcpu->kvm->mmu_lock);
3213 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3214 spin_unlock(&vcpu->kvm->mmu_lock);
3215 return 1;
3216}
3217
3218static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3219 struct kvm_pv_mmu_op_buffer *buffer)
3220{
3221 struct kvm_mmu_op_header *header;
3222
3223 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3224 if (!header)
3225 return 0;
3226 switch (header->op) {
3227 case KVM_MMU_OP_WRITE_PTE: {
3228 struct kvm_mmu_op_write_pte *wpte;
3229
3230 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3231 if (!wpte)
3232 return 0;
3233 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3234 wpte->pte_val);
3235 }
3236 case KVM_MMU_OP_FLUSH_TLB: {
3237 struct kvm_mmu_op_flush_tlb *ftlb;
3238
3239 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3240 if (!ftlb)
3241 return 0;
3242 return kvm_pv_mmu_flush_tlb(vcpu);
3243 }
3244 case KVM_MMU_OP_RELEASE_PT: {
3245 struct kvm_mmu_op_release_pt *rpt;
3246
3247 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3248 if (!rpt)
3249 return 0;
3250 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3251 }
3252 default: return 0;
3253 }
3254}
3255
3256int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3257 gpa_t addr, unsigned long *ret)
3258{
3259 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003260 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003261
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003262 buffer->ptr = buffer->buf;
3263 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3264 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003265
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003266 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003267 if (r)
3268 goto out;
3269
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003270 while (buffer->len) {
3271 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003272 if (r < 0)
3273 goto out;
3274 if (r == 0)
3275 break;
3276 }
3277
3278 r = 1;
3279out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003280 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003281 return r;
3282}
3283
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003284int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3285{
3286 struct kvm_shadow_walk_iterator iterator;
3287 int nr_sptes = 0;
3288
3289 spin_lock(&vcpu->kvm->mmu_lock);
3290 for_each_shadow_entry(vcpu, addr, iterator) {
3291 sptes[iterator.level-1] = *iterator.sptep;
3292 nr_sptes++;
3293 if (!is_shadow_present_pte(*iterator.sptep))
3294 break;
3295 }
3296 spin_unlock(&vcpu->kvm->mmu_lock);
3297
3298 return nr_sptes;
3299}
3300EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3301
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003302#ifdef AUDIT
3303
3304static const char *audit_msg;
3305
3306static gva_t canonicalize(gva_t gva)
3307{
3308#ifdef CONFIG_X86_64
3309 gva = (long long)(gva << 16) >> 16;
3310#endif
3311 return gva;
3312}
3313
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003314
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003315typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003316
3317static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3318 inspect_spte_fn fn)
3319{
3320 int i;
3321
3322 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3323 u64 ent = sp->spt[i];
3324
3325 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003326 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003327 struct kvm_mmu_page *child;
3328 child = page_header(ent & PT64_BASE_ADDR_MASK);
3329 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003330 } else
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003331 fn(kvm, &sp->spt[i]);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003332 }
3333 }
3334}
3335
3336static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3337{
3338 int i;
3339 struct kvm_mmu_page *sp;
3340
3341 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3342 return;
3343 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3344 hpa_t root = vcpu->arch.mmu.root_hpa;
3345 sp = page_header(root);
3346 __mmu_spte_walk(vcpu->kvm, sp, fn);
3347 return;
3348 }
3349 for (i = 0; i < 4; ++i) {
3350 hpa_t root = vcpu->arch.mmu.pae_root[i];
3351
3352 if (root && VALID_PAGE(root)) {
3353 root &= PT64_BASE_ADDR_MASK;
3354 sp = page_header(root);
3355 __mmu_spte_walk(vcpu->kvm, sp, fn);
3356 }
3357 }
3358 return;
3359}
3360
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003361static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3362 gva_t va, int level)
3363{
3364 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3365 int i;
3366 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3367
3368 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3369 u64 ent = pt[i];
3370
Avi Kivityc7addb92007-09-16 18:58:32 +02003371 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003372 continue;
3373
3374 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003375 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3376 audit_mappings_page(vcpu, ent, va, level - 1);
3377 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003378 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003379 gfn_t gfn = gpa >> PAGE_SHIFT;
3380 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3381 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003382
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003383 if (is_error_pfn(pfn)) {
3384 kvm_release_pfn_clean(pfn);
3385 continue;
3386 }
3387
Avi Kivityc7addb92007-09-16 18:58:32 +02003388 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003389 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003390 printk(KERN_ERR "xx audit error: (%s) levels %d"
3391 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003392 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003393 va, gpa, hpa, ent,
3394 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003395 else if (ent == shadow_notrap_nonpresent_pte
3396 && !is_error_hpa(hpa))
3397 printk(KERN_ERR "audit: (%s) notrap shadow,"
3398 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003399 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003400
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003401 }
3402 }
3403}
3404
3405static void audit_mappings(struct kvm_vcpu *vcpu)
3406{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003407 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003409 if (vcpu->arch.mmu.root_level == 4)
3410 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003411 else
3412 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003413 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003414 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003415 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003416 i << 30,
3417 2);
3418}
3419
3420static int count_rmaps(struct kvm_vcpu *vcpu)
3421{
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003422 struct kvm *kvm = vcpu->kvm;
3423 struct kvm_memslots *slots;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003424 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003425 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003426
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003427 idx = srcu_read_lock(&kvm->srcu);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003428 slots = kvm_memslots(kvm);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003429 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003430 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003431 struct kvm_rmap_desc *d;
3432
3433 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003434 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003435
Izik Eidus290fc382007-09-27 14:11:22 +02003436 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003437 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003438 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003439 ++nmaps;
3440 continue;
3441 }
Izik Eidus290fc382007-09-27 14:11:22 +02003442 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003443 while (d) {
3444 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003445 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003446 ++nmaps;
3447 else
3448 break;
3449 d = d->more;
3450 }
3451 }
3452 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003453 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003454 return nmaps;
3455}
3456
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003457void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003458{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003459 unsigned long *rmapp;
3460 struct kvm_mmu_page *rev_sp;
3461 gfn_t gfn;
3462
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003463 if (is_writable_pte(*sptep)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003464 rev_sp = page_header(__pa(sptep));
Lai Jiangshan2032a932010-05-26 16:49:59 +08003465 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003466
3467 if (!gfn_to_memslot(kvm, gfn)) {
3468 if (!printk_ratelimit())
3469 return;
3470 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3471 audit_msg, gfn);
3472 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003473 audit_msg, (long int)(sptep - rev_sp->spt),
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003474 rev_sp->gfn);
3475 dump_stack();
3476 return;
3477 }
3478
Lai Jiangshan2032a932010-05-26 16:49:59 +08003479 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003480 if (!*rmapp) {
3481 if (!printk_ratelimit())
3482 return;
3483 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3484 audit_msg, *sptep);
3485 dump_stack();
3486 }
3487 }
3488
3489}
3490
3491void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3492{
3493 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3494}
3495
3496static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3497{
Avi Kivity4db35312007-11-21 15:28:32 +02003498 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003499 int i;
3500
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003501 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003502 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003503
Avi Kivity4db35312007-11-21 15:28:32 +02003504 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003505 continue;
3506
3507 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3508 u64 ent = pt[i];
3509
3510 if (!(ent & PT_PRESENT_MASK))
3511 continue;
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003512 if (!is_writable_pte(ent))
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003513 continue;
Xiao Guangrong805d32d2010-04-01 16:50:45 +08003514 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003515 }
3516 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003517 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003518}
3519
3520static void audit_rmap(struct kvm_vcpu *vcpu)
3521{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003522 check_writable_mappings_rmap(vcpu);
3523 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003524}
3525
3526static void audit_write_protection(struct kvm_vcpu *vcpu)
3527{
Avi Kivity4db35312007-11-21 15:28:32 +02003528 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003529 struct kvm_memory_slot *slot;
3530 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003531 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003532 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003533
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003534 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003535 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003536 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003537 if (sp->unsync)
3538 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003539
Avi Kivity4db35312007-11-21 15:28:32 +02003540 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003541 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003542 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003543
3544 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3545 while (spte) {
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003546 if (is_writable_pte(*spte))
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003547 printk(KERN_ERR "%s: (%s) shadow page has "
3548 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003549 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003550 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003551 spte = rmap_next(vcpu->kvm, rmapp, spte);
3552 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003553 }
3554}
3555
3556static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3557{
3558 int olddbg = dbg;
3559
3560 dbg = 0;
3561 audit_msg = msg;
3562 audit_rmap(vcpu);
3563 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003564 if (strcmp("pre pte write", audit_msg) != 0)
3565 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003566 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003567 dbg = olddbg;
3568}
3569
3570#endif