blob: 741373e8ca777a318ed7e487ef8982a181ea24f4 [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.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020021#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030022#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040025#include <linux/types.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/highmem.h>
29#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020030#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030031#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050032#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020033#include <linux/srcu.h>
Avi Kivitye4956062007-06-28 14:15:57 -040034
35#include <asm/page.h>
36#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020037#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020038#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040039
Joerg Roedel18552672008-02-07 13:47:41 +010040/*
41 * When setting this variable to true it enables Two-Dimensional-Paging
42 * where the hardware walks 2 page tables:
43 * 1. the guest-virtual to guest-physical
44 * 2. while doing 1. it walks guest-physical to host-physical
45 * If the hardware supports that we don't need to do shadow paging.
46 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050047bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010048
Avi Kivity37a7d8b2007-01-05 16:36:56 -080049#undef MMU_DEBUG
50
51#undef AUDIT
52
53#ifdef AUDIT
54static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
55#else
56static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
57#endif
58
59#ifdef MMU_DEBUG
60
61#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
62#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
63
64#else
65
66#define pgprintk(x...) do { } while (0)
67#define rmap_printk(x...) do { } while (0)
68
69#endif
70
71#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030072static int dbg = 0;
73module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080074#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080075
Marcelo Tosatti582801a2008-09-23 13:18:41 -030076static int oos_shadow = 1;
77module_param(oos_shadow, bool, 0644);
78
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080079#ifndef MMU_DEBUG
80#define ASSERT(x) do { } while (0)
81#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080082#define ASSERT(x) \
83 if (!(x)) { \
84 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
85 __FILE__, __LINE__, #x); \
86 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080087#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080088
Avi Kivity6aa8b732006-12-10 02:21:36 -080089#define PT_FIRST_AVAIL_BITS_SHIFT 9
90#define PT64_SECOND_AVAIL_BITS_SHIFT 52
91
Avi Kivity6aa8b732006-12-10 02:21:36 -080092#define VALID_PAGE(x) ((x) != INVALID_PAGE)
93
94#define PT64_LEVEL_BITS 9
95
96#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040097 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
99#define PT64_LEVEL_MASK(level) \
100 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
101
102#define PT64_INDEX(address, level)\
103 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
104
105
106#define PT32_LEVEL_BITS 10
107
108#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400109 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110
111#define PT32_LEVEL_MASK(level) \
112 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200113#define PT32_LVL_OFFSET_MASK(level) \
114 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
115 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116
117#define PT32_INDEX(address, level)\
118 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
119
120
Avi Kivity27aba762007-03-09 13:04:31 +0200121#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122#define PT64_DIR_BASE_ADDR_MASK \
123 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200124#define PT64_LVL_ADDR_MASK(level) \
125 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
126 * PT64_LEVEL_BITS))) - 1))
127#define PT64_LVL_OFFSET_MASK(level) \
128 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
129 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
131#define PT32_BASE_ADDR_MASK PAGE_MASK
132#define PT32_DIR_BASE_ADDR_MASK \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200134#define PT32_LVL_ADDR_MASK(level) \
135 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137
Avi Kivity79539ce2007-11-21 02:06:21 +0200138#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
139 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800141#define RMAP_EXT 4
142
Avi Kivityfe135d22007-12-09 16:15:46 +0200143#define ACC_EXEC_MASK 1
144#define ACC_WRITE_MASK PT_WRITABLE_MASK
145#define ACC_USER_MASK PT_USER_MASK
146#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
147
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200148#include <trace/events/kvm.h>
149
150#undef TRACE_INCLUDE_FILE
Avi Kivity07420172009-07-06 12:21:32 +0300151#define CREATE_TRACE_POINTS
152#include "mmutrace.h"
153
Izik Eidus14032832009-09-23 21:47:17 +0300154#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
155
Avi Kivity135f8c22008-08-21 17:49:56 +0300156#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
157
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800158struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300159 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800160 struct kvm_rmap_desc *more;
161};
162
Avi Kivity2d111232008-12-25 14:39:47 +0200163struct kvm_shadow_walk_iterator {
164 u64 addr;
165 hpa_t shadow_addr;
166 int level;
167 u64 *sptep;
168 unsigned index;
169};
170
171#define for_each_shadow_entry(_vcpu, _addr, _walker) \
172 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
173 shadow_walk_okay(&(_walker)); \
174 shadow_walk_next(&(_walker)))
175
176
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300177struct kvm_unsync_walk {
178 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
179};
180
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300181typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
182
Avi Kivityb5a33a72007-04-15 16:31:09 +0300183static struct kmem_cache *pte_chain_cache;
184static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300185static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186
Avi Kivityc7addb92007-09-16 18:58:32 +0200187static u64 __read_mostly shadow_trap_nonpresent_pte;
188static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800189static u64 __read_mostly shadow_base_present_pte;
190static u64 __read_mostly shadow_nx_mask;
191static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
192static u64 __read_mostly shadow_user_mask;
193static u64 __read_mostly shadow_accessed_mask;
194static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200195
Dong, Eddie82725b22009-03-30 16:21:08 +0800196static inline u64 rsvd_bits(int s, int e)
197{
198 return ((1ULL << (e - s + 1)) - 1) << s;
199}
200
Avi Kivityc7addb92007-09-16 18:58:32 +0200201void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
202{
203 shadow_trap_nonpresent_pte = trap_pte;
204 shadow_notrap_nonpresent_pte = notrap_pte;
205}
206EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
207
Sheng Yang7b523452008-04-25 21:13:50 +0800208void kvm_mmu_set_base_ptes(u64 base_pte)
209{
210 shadow_base_present_pte = base_pte;
211}
212EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
213
214void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800215 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800216{
217 shadow_user_mask = user_mask;
218 shadow_accessed_mask = accessed_mask;
219 shadow_dirty_mask = dirty_mask;
220 shadow_nx_mask = nx_mask;
221 shadow_x_mask = x_mask;
222}
223EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
224
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225static int is_write_protection(struct kvm_vcpu *vcpu)
226{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200227 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228}
229
230static int is_cpuid_PSE36(void)
231{
232 return 1;
233}
234
Avi Kivity73b10872007-01-26 00:56:41 -0800235static int is_nx(struct kvm_vcpu *vcpu)
236{
Avi Kivityf6801df2010-01-21 15:31:50 +0200237 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800238}
239
Avi Kivityc7addb92007-09-16 18:58:32 +0200240static int is_shadow_present_pte(u64 pte)
241{
Avi Kivityc7addb92007-09-16 18:58:32 +0200242 return pte != shadow_trap_nonpresent_pte
243 && pte != shadow_notrap_nonpresent_pte;
244}
245
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300246static int is_large_pte(u64 pte)
247{
248 return pte & PT_PAGE_SIZE_MASK;
249}
250
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900251static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252{
253 return pte & PT_WRITABLE_MASK;
254}
255
Avi Kivity43a37952009-06-10 14:12:05 +0300256static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200257{
Avi Kivity439e2182009-06-10 12:56:54 +0300258 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200259}
260
Avi Kivity43a37952009-06-10 14:12:05 +0300261static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800262{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200263 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800264}
265
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300266static int is_last_spte(u64 pte, int level)
267{
268 if (level == PT_PAGE_TABLE_LEVEL)
269 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200270 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300271 return 1;
272 return 0;
273}
274
Anthony Liguori35149e22008-04-02 14:46:56 -0500275static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200276{
Anthony Liguori35149e22008-04-02 14:46:56 -0500277 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200278}
279
Avi Kivityda928522007-11-21 13:54:47 +0200280static gfn_t pse36_gfn_delta(u32 gpte)
281{
282 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
283
284 return (gpte & PT32_DIR_PSE36_MASK) << shift;
285}
286
Avi Kivityd555c332009-06-10 14:24:23 +0300287static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300288{
289#ifdef CONFIG_X86_64
290 set_64bit((unsigned long *)sptep, spte);
291#else
292 set_64bit((unsigned long long *)sptep, spte);
293#endif
294}
295
Avi Kivitye2dec932007-01-05 16:36:54 -0800296static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300297 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800298{
299 void *obj;
300
301 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800302 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800303 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300304 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800305 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800306 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800307 cache->objects[cache->nobjs++] = obj;
308 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800309 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800310}
311
312static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
313{
314 while (mc->nobjs)
315 kfree(mc->objects[--mc->nobjs]);
316}
317
Avi Kivityc1158e62007-07-20 08:18:27 +0300318static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300319 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300320{
321 struct page *page;
322
323 if (cache->nobjs >= min)
324 return 0;
325 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300326 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300327 if (!page)
328 return -ENOMEM;
329 set_page_private(page, 0);
330 cache->objects[cache->nobjs++] = page_address(page);
331 }
332 return 0;
333}
334
335static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
336{
337 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300338 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300339}
340
Avi Kivity8c438502007-04-16 11:53:17 +0300341static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
342{
343 int r;
344
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800345 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300346 pte_chain_cache, 4);
347 if (r)
348 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800349 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200350 rmap_desc_cache, 4);
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_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300354 if (r)
355 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800356 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300357 mmu_page_header_cache, 4);
358out:
Avi Kivity8c438502007-04-16 11:53:17 +0300359 return r;
360}
361
Avi Kivity714b93d2007-01-05 16:36:53 -0800362static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
363{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800364 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
365 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
366 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
367 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800368}
369
370static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
371 size_t size)
372{
373 void *p;
374
375 BUG_ON(!mc->nobjs);
376 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800377 return p;
378}
379
Avi Kivity714b93d2007-01-05 16:36:53 -0800380static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
381{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800382 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800383 sizeof(struct kvm_pte_chain));
384}
385
Avi Kivity90cb0522007-07-17 13:04:56 +0300386static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800387{
Avi Kivity90cb0522007-07-17 13:04:56 +0300388 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800389}
390
391static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
392{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800393 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800394 sizeof(struct kvm_rmap_desc));
395}
396
Avi Kivity90cb0522007-07-17 13:04:56 +0300397static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800398{
Avi Kivity90cb0522007-07-17 13:04:56 +0300399 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800400}
401
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800402/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300403 * Return the pointer to the largepage write count for a given
404 * gfn, handling slots that are not large page aligned.
405 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200406static int *slot_largepage_idx(gfn_t gfn,
407 struct kvm_memory_slot *slot,
408 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409{
410 unsigned long idx;
411
Joerg Roedeld25797b2009-07-27 16:30:43 +0200412 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
413 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
414 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300415}
416
417static void account_shadowed(struct kvm *kvm, gfn_t gfn)
418{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200419 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300420 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200421 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300422
Izik Eidus28430992008-10-03 17:40:32 +0300423 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200424
425 slot = gfn_to_memslot_unaliased(kvm, gfn);
426 for (i = PT_DIRECTORY_LEVEL;
427 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
428 write_count = slot_largepage_idx(gfn, slot, i);
429 *write_count += 1;
430 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300431}
432
433static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
434{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200435 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300436 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200437 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300438
Izik Eidus28430992008-10-03 17:40:32 +0300439 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200440 for (i = PT_DIRECTORY_LEVEL;
441 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
442 slot = gfn_to_memslot_unaliased(kvm, gfn);
443 write_count = slot_largepage_idx(gfn, slot, i);
444 *write_count -= 1;
445 WARN_ON(*write_count < 0);
446 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300447}
448
Joerg Roedeld25797b2009-07-27 16:30:43 +0200449static int has_wrprotected_page(struct kvm *kvm,
450 gfn_t gfn,
451 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452{
Izik Eidus28430992008-10-03 17:40:32 +0300453 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300454 int *largepage_idx;
455
Izik Eidus28430992008-10-03 17:40:32 +0300456 gfn = unalias_gfn(kvm, gfn);
457 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300458 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200459 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300460 return *largepage_idx;
461 }
462
463 return 1;
464}
465
Joerg Roedeld25797b2009-07-27 16:30:43 +0200466static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300467{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100468 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200469 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100471 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472
Joerg Roedeld25797b2009-07-27 16:30:43 +0200473 for (i = PT_PAGE_TABLE_LEVEL;
474 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
475 if (page_size >= KVM_HPAGE_SIZE(i))
476 ret = i;
477 else
478 break;
479 }
480
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300481 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482}
483
Joerg Roedeld25797b2009-07-27 16:30:43 +0200484static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300485{
486 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800487 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300488
489 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
490 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200491 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492
Joerg Roedeld25797b2009-07-27 16:30:43 +0200493 host_level = host_mapping_level(vcpu->kvm, large_gfn);
494
495 if (host_level == PT_PAGE_TABLE_LEVEL)
496 return host_level;
497
Sheng Yang878403b2010-01-05 19:02:29 +0800498 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
499 kvm_x86_ops->get_lpage_level() : host_level;
500
501 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200502 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
503 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200504
505 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506}
507
508/*
Izik Eidus290fc382007-09-27 14:11:22 +0200509 * Take gfn and return the reverse mapping to it.
510 * Note: gfn must be unaliased before this function get called
511 */
512
Joerg Roedel44ad9942009-07-27 16:30:42 +0200513static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200514{
515 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300516 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200517
518 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200519 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300520 return &slot->rmap[gfn - slot->base_gfn];
521
Joerg Roedel44ad9942009-07-27 16:30:42 +0200522 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
523 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300524
Joerg Roedel44ad9942009-07-27 16:30:42 +0200525 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200526}
527
528/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800529 * Reverse mapping data structures:
530 *
Izik Eidus290fc382007-09-27 14:11:22 +0200531 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
532 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 *
Izik Eidus290fc382007-09-27 14:11:22 +0200534 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
535 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300536 *
537 * Returns the number of rmap entries before the spte was added or zero if
538 * the spte was not added.
539 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800540 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200541static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800542{
Avi Kivity4db35312007-11-21 15:28:32 +0200543 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800544 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200545 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300546 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547
Avi Kivity43a37952009-06-10 14:12:05 +0300548 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300549 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200550 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200551 sp = page_header(__pa(spte));
552 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200553 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200554 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200556 *rmapp = (unsigned long)spte;
557 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800559 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300560 desc->sptes[0] = (u64 *)*rmapp;
561 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200562 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 } else {
564 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200565 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300566 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800567 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300568 count += RMAP_EXT;
569 }
Avi Kivityd555c332009-06-10 14:24:23 +0300570 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800571 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 desc = desc->more;
573 }
Avi Kivityd555c332009-06-10 14:24:23 +0300574 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300576 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300578 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800579}
580
Izik Eidus290fc382007-09-27 14:11:22 +0200581static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800582 struct kvm_rmap_desc *desc,
583 int i,
584 struct kvm_rmap_desc *prev_desc)
585{
586 int j;
587
Avi Kivityd555c332009-06-10 14:24:23 +0300588 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300590 desc->sptes[i] = desc->sptes[j];
591 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800592 if (j != 0)
593 return;
594 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300595 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 else
597 if (prev_desc)
598 prev_desc->more = desc->more;
599 else
Izik Eidus290fc382007-09-27 14:11:22 +0200600 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300601 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800602}
603
Izik Eidus290fc382007-09-27 14:11:22 +0200604static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800605{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 struct kvm_rmap_desc *desc;
607 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200608 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500609 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200610 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800611 int i;
612
Avi Kivity43a37952009-06-10 14:12:05 +0300613 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800614 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200615 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500616 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800617 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500618 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900619 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300620 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200621 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200622 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
624 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200625 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200627 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800628 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
629 spte, *spte);
630 BUG();
631 }
Izik Eidus290fc382007-09-27 14:11:22 +0200632 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800633 } else {
634 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200635 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 prev_desc = NULL;
637 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300638 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
639 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200640 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800641 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 prev_desc);
643 return;
644 }
645 prev_desc = desc;
646 desc = desc->more;
647 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200648 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800649 BUG();
650 }
651}
652
Izik Eidus98348e92007-10-16 14:42:30 +0200653static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800654{
Avi Kivity374cbac2007-01-05 16:36:43 -0800655 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200656 struct kvm_rmap_desc *prev_desc;
657 u64 *prev_spte;
658 int i;
659
660 if (!*rmapp)
661 return NULL;
662 else if (!(*rmapp & 1)) {
663 if (!spte)
664 return (u64 *)*rmapp;
665 return NULL;
666 }
667 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
668 prev_desc = NULL;
669 prev_spte = NULL;
670 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300671 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200672 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300673 return desc->sptes[i];
674 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200675 }
676 desc = desc->more;
677 }
678 return NULL;
679}
680
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200681static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200682{
Izik Eidus290fc382007-09-27 14:11:22 +0200683 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800684 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200685 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800686
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500687 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200688 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800689
Izik Eidus98348e92007-10-16 14:42:30 +0200690 spte = rmap_next(kvm, rmapp, NULL);
691 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800692 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800693 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800694 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900695 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300696 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800697 write_protected = 1;
698 }
Izik Eidus9647c142007-10-16 14:43:46 +0200699 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800700 }
Izik Eidus855149a2008-03-20 18:17:24 +0200701 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500702 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200703
704 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500705 pfn = spte_to_pfn(*spte);
706 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200707 }
708
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300709 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200710 for (i = PT_DIRECTORY_LEVEL;
711 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
712 rmapp = gfn_to_rmap(kvm, gfn, i);
713 spte = rmap_next(kvm, rmapp, NULL);
714 while (spte) {
715 BUG_ON(!spte);
716 BUG_ON(!(*spte & PT_PRESENT_MASK));
717 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
718 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900719 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200720 rmap_remove(kvm, spte);
721 --kvm->stat.lpages;
722 __set_spte(spte, shadow_trap_nonpresent_pte);
723 spte = NULL;
724 write_protected = 1;
725 }
726 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300727 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300728 }
729
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200730 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800731}
732
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000733static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
734 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200735{
736 u64 *spte;
737 int need_tlb_flush = 0;
738
739 while ((spte = rmap_next(kvm, rmapp, NULL))) {
740 BUG_ON(!(*spte & PT_PRESENT_MASK));
741 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
742 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300743 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200744 need_tlb_flush = 1;
745 }
746 return need_tlb_flush;
747}
748
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000749static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
750 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300751{
752 int need_flush = 0;
753 u64 *spte, new_spte;
754 pte_t *ptep = (pte_t *)data;
755 pfn_t new_pfn;
756
757 WARN_ON(pte_huge(*ptep));
758 new_pfn = pte_pfn(*ptep);
759 spte = rmap_next(kvm, rmapp, NULL);
760 while (spte) {
761 BUG_ON(!is_shadow_present_pte(*spte));
762 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
763 need_flush = 1;
764 if (pte_write(*ptep)) {
765 rmap_remove(kvm, spte);
766 __set_spte(spte, shadow_trap_nonpresent_pte);
767 spte = rmap_next(kvm, rmapp, NULL);
768 } else {
769 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
770 new_spte |= (u64)new_pfn << PAGE_SHIFT;
771
772 new_spte &= ~PT_WRITABLE_MASK;
773 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900774 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300775 kvm_set_pfn_dirty(spte_to_pfn(*spte));
776 __set_spte(spte, new_spte);
777 spte = rmap_next(kvm, rmapp, spte);
778 }
779 }
780 if (need_flush)
781 kvm_flush_remote_tlbs(kvm);
782
783 return 0;
784}
785
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000786static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
787 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300788 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000789 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200790{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200791 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200792 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200793 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200794 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200795
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200796 slots = rcu_dereference(kvm->memslots);
797
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200798 for (i = 0; i < slots->nmemslots; i++) {
799 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200800 unsigned long start = memslot->userspace_addr;
801 unsigned long end;
802
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200803 end = start + (memslot->npages << PAGE_SHIFT);
804 if (hva >= start && hva < end) {
805 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200806
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200807 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200808
809 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
810 int idx = gfn_offset;
811 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200812 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300813 &memslot->lpage_info[j][idx].rmap_pde,
814 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200815 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200816 trace_kvm_age_page(hva, memslot, ret);
817 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200818 }
819 }
820
821 return retval;
822}
823
824int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
825{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300826 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200827}
828
Izik Eidus3da0dd42009-09-23 21:47:18 +0300829void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
830{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000831 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300832}
833
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000834static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
835 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200836{
837 u64 *spte;
838 int young = 0;
839
Rik van Riel6316e1c2010-02-03 16:11:03 -0500840 /*
841 * Emulate the accessed bit for EPT, by checking if this page has
842 * an EPT mapping, and clearing it if it does. On the next access,
843 * a new EPT mapping will be established.
844 * This has some overhead, but not as much as the cost of swapping
845 * out actively used pages or breaking up actively used hugepages.
846 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800847 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500848 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800849
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200850 spte = rmap_next(kvm, rmapp, NULL);
851 while (spte) {
852 int _young;
853 u64 _spte = *spte;
854 BUG_ON(!(_spte & PT_PRESENT_MASK));
855 _young = _spte & PT_ACCESSED_MASK;
856 if (_young) {
857 young = 1;
858 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
859 }
860 spte = rmap_next(kvm, rmapp, spte);
861 }
862 return young;
863}
864
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300865#define RMAP_RECYCLE_THRESHOLD 1000
866
Joerg Roedel852e3c12009-07-27 16:30:44 +0200867static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300868{
869 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200870 struct kvm_mmu_page *sp;
871
872 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300873
874 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200875 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300876
Izik Eidus3da0dd42009-09-23 21:47:18 +0300877 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300878 kvm_flush_remote_tlbs(vcpu->kvm);
879}
880
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200881int kvm_age_hva(struct kvm *kvm, unsigned long hva)
882{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300883 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200884}
885
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800886#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300887static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888{
Avi Kivity139bdb22007-01-05 16:36:50 -0800889 u64 *pos;
890 u64 *end;
891
Avi Kivity47ad8e62007-05-06 15:50:58 +0300892 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300893 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800894 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800895 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800897 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 return 1;
899}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800900#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901
Avi Kivity4db35312007-11-21 15:28:32 +0200902static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800903{
Avi Kivity4db35312007-11-21 15:28:32 +0200904 ASSERT(is_empty_shadow_page(sp->spt));
905 list_del(&sp->link);
906 __free_page(virt_to_page(sp->spt));
907 __free_page(virt_to_page(sp->gfns));
908 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800909 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800910}
911
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800912static unsigned kvm_page_table_hashfn(gfn_t gfn)
913{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200914 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800915}
916
Avi Kivity25c0de22007-01-05 16:36:42 -0800917static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
918 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919{
Avi Kivity4db35312007-11-21 15:28:32 +0200920 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800922 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
923 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
924 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200925 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800926 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200927 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800928 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200929 sp->multimapped = 0;
930 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800931 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200932 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933}
934
Avi Kivity714b93d2007-01-05 16:36:53 -0800935static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200936 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800937{
938 struct kvm_pte_chain *pte_chain;
939 struct hlist_node *node;
940 int i;
941
942 if (!parent_pte)
943 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200944 if (!sp->multimapped) {
945 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946
947 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200948 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800949 return;
950 }
Avi Kivity4db35312007-11-21 15:28:32 +0200951 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800952 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200953 INIT_HLIST_HEAD(&sp->parent_ptes);
954 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800955 pte_chain->parent_ptes[0] = old;
956 }
Avi Kivity4db35312007-11-21 15:28:32 +0200957 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800958 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
959 continue;
960 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
961 if (!pte_chain->parent_ptes[i]) {
962 pte_chain->parent_ptes[i] = parent_pte;
963 return;
964 }
965 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800966 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800967 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200968 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800969 pte_chain->parent_ptes[0] = parent_pte;
970}
971
Avi Kivity4db35312007-11-21 15:28:32 +0200972static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800973 u64 *parent_pte)
974{
975 struct kvm_pte_chain *pte_chain;
976 struct hlist_node *node;
977 int i;
978
Avi Kivity4db35312007-11-21 15:28:32 +0200979 if (!sp->multimapped) {
980 BUG_ON(sp->parent_pte != parent_pte);
981 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800982 return;
983 }
Avi Kivity4db35312007-11-21 15:28:32 +0200984 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800985 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
986 if (!pte_chain->parent_ptes[i])
987 break;
988 if (pte_chain->parent_ptes[i] != parent_pte)
989 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800990 while (i + 1 < NR_PTE_CHAIN_ENTRIES
991 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800992 pte_chain->parent_ptes[i]
993 = pte_chain->parent_ptes[i + 1];
994 ++i;
995 }
996 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800997 if (i == 0) {
998 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300999 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001000 if (hlist_empty(&sp->parent_ptes)) {
1001 sp->multimapped = 0;
1002 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001003 }
1004 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001005 return;
1006 }
1007 BUG();
1008}
1009
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001010
1011static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1012 mmu_parent_walk_fn fn)
1013{
1014 struct kvm_pte_chain *pte_chain;
1015 struct hlist_node *node;
1016 struct kvm_mmu_page *parent_sp;
1017 int i;
1018
1019 if (!sp->multimapped && sp->parent_pte) {
1020 parent_sp = page_header(__pa(sp->parent_pte));
1021 fn(vcpu, parent_sp);
1022 mmu_parent_walk(vcpu, parent_sp, fn);
1023 return;
1024 }
1025 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1026 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1027 if (!pte_chain->parent_ptes[i])
1028 break;
1029 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1030 fn(vcpu, parent_sp);
1031 mmu_parent_walk(vcpu, parent_sp, fn);
1032 }
1033}
1034
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001035static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1036{
1037 unsigned int index;
1038 struct kvm_mmu_page *sp = page_header(__pa(spte));
1039
1040 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001041 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1042 sp->unsync_children++;
1043 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001044}
1045
1046static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1047{
1048 struct kvm_pte_chain *pte_chain;
1049 struct hlist_node *node;
1050 int i;
1051
1052 if (!sp->parent_pte)
1053 return;
1054
1055 if (!sp->multimapped) {
1056 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1057 return;
1058 }
1059
1060 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1061 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1062 if (!pte_chain->parent_ptes[i])
1063 break;
1064 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1065 }
1066}
1067
1068static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1069{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001070 kvm_mmu_update_parents_unsync(sp);
1071 return 1;
1072}
1073
1074static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1075 struct kvm_mmu_page *sp)
1076{
1077 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1078 kvm_mmu_update_parents_unsync(sp);
1079}
1080
Avi Kivityd761a502008-05-29 14:55:03 +03001081static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1082 struct kvm_mmu_page *sp)
1083{
1084 int i;
1085
1086 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1087 sp->spt[i] = shadow_trap_nonpresent_pte;
1088}
1089
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001090static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1091 struct kvm_mmu_page *sp)
1092{
1093 return 1;
1094}
1095
Marcelo Tosattia7052892008-09-23 13:18:35 -03001096static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1097{
1098}
1099
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001100#define KVM_PAGE_ARRAY_NR 16
1101
1102struct kvm_mmu_pages {
1103 struct mmu_page_and_offset {
1104 struct kvm_mmu_page *sp;
1105 unsigned int idx;
1106 } page[KVM_PAGE_ARRAY_NR];
1107 unsigned int nr;
1108};
1109
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001110#define for_each_unsync_children(bitmap, idx) \
1111 for (idx = find_first_bit(bitmap, 512); \
1112 idx < 512; \
1113 idx = find_next_bit(bitmap, 512, idx+1))
1114
Hannes Edercded19f2009-02-21 02:19:13 +01001115static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1116 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001117{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001118 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001119
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001120 if (sp->unsync)
1121 for (i=0; i < pvec->nr; i++)
1122 if (pvec->page[i].sp == sp)
1123 return 0;
1124
1125 pvec->page[pvec->nr].sp = sp;
1126 pvec->page[pvec->nr].idx = idx;
1127 pvec->nr++;
1128 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1129}
1130
1131static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1132 struct kvm_mmu_pages *pvec)
1133{
1134 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001135
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001136 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001137 u64 ent = sp->spt[i];
1138
Marcelo Tosatti87917232008-12-22 18:49:30 -02001139 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001140 struct kvm_mmu_page *child;
1141 child = page_header(ent & PT64_BASE_ADDR_MASK);
1142
1143 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001144 if (mmu_pages_add(pvec, child, i))
1145 return -ENOSPC;
1146
1147 ret = __mmu_unsync_walk(child, pvec);
1148 if (!ret)
1149 __clear_bit(i, sp->unsync_child_bitmap);
1150 else if (ret > 0)
1151 nr_unsync_leaf += ret;
1152 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001153 return ret;
1154 }
1155
1156 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001157 nr_unsync_leaf++;
1158 if (mmu_pages_add(pvec, child, i))
1159 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001160 }
1161 }
1162 }
1163
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001164 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001165 sp->unsync_children = 0;
1166
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001167 return nr_unsync_leaf;
1168}
1169
1170static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1171 struct kvm_mmu_pages *pvec)
1172{
1173 if (!sp->unsync_children)
1174 return 0;
1175
1176 mmu_pages_add(pvec, sp, 0);
1177 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001178}
1179
Avi Kivity4db35312007-11-21 15:28:32 +02001180static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001181{
1182 unsigned index;
1183 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001184 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001185 struct hlist_node *node;
1186
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001187 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001188 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001189 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001190 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001191 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001192 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001193 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001194 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001195 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001196 }
1197 return NULL;
1198}
1199
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001200static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1201{
1202 WARN_ON(!sp->unsync);
1203 sp->unsync = 0;
1204 --kvm->stat.mmu_unsync;
1205}
1206
1207static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1208
1209static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1210{
1211 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1212 kvm_mmu_zap_page(vcpu->kvm, sp);
1213 return 1;
1214 }
1215
Avi Kivityf691fe12009-07-06 15:58:14 +03001216 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001217 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1218 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001219 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001220 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1221 kvm_mmu_zap_page(vcpu->kvm, sp);
1222 return 1;
1223 }
1224
1225 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001226 return 0;
1227}
1228
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001229struct mmu_page_path {
1230 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1231 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001232};
1233
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001234#define for_each_sp(pvec, sp, parents, i) \
1235 for (i = mmu_pages_next(&pvec, &parents, -1), \
1236 sp = pvec.page[i].sp; \
1237 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1238 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001239
Hannes Edercded19f2009-02-21 02:19:13 +01001240static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1241 struct mmu_page_path *parents,
1242 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001243{
1244 int n;
1245
1246 for (n = i+1; n < pvec->nr; n++) {
1247 struct kvm_mmu_page *sp = pvec->page[n].sp;
1248
1249 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1250 parents->idx[0] = pvec->page[n].idx;
1251 return n;
1252 }
1253
1254 parents->parent[sp->role.level-2] = sp;
1255 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1256 }
1257
1258 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001259}
1260
Hannes Edercded19f2009-02-21 02:19:13 +01001261static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001262{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001263 struct kvm_mmu_page *sp;
1264 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001265
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001266 do {
1267 unsigned int idx = parents->idx[level];
1268
1269 sp = parents->parent[level];
1270 if (!sp)
1271 return;
1272
1273 --sp->unsync_children;
1274 WARN_ON((int)sp->unsync_children < 0);
1275 __clear_bit(idx, sp->unsync_child_bitmap);
1276 level++;
1277 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1278}
1279
1280static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1281 struct mmu_page_path *parents,
1282 struct kvm_mmu_pages *pvec)
1283{
1284 parents->parent[parent->role.level-1] = NULL;
1285 pvec->nr = 0;
1286}
1287
1288static void mmu_sync_children(struct kvm_vcpu *vcpu,
1289 struct kvm_mmu_page *parent)
1290{
1291 int i;
1292 struct kvm_mmu_page *sp;
1293 struct mmu_page_path parents;
1294 struct kvm_mmu_pages pages;
1295
1296 kvm_mmu_pages_init(parent, &parents, &pages);
1297 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001298 int protected = 0;
1299
1300 for_each_sp(pages, sp, parents, i)
1301 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1302
1303 if (protected)
1304 kvm_flush_remote_tlbs(vcpu->kvm);
1305
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001306 for_each_sp(pages, sp, parents, i) {
1307 kvm_sync_page(vcpu, sp);
1308 mmu_pages_clear_parents(&parents);
1309 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001310 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001311 kvm_mmu_pages_init(parent, &parents, &pages);
1312 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001313}
1314
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001315static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1316 gfn_t gfn,
1317 gva_t gaddr,
1318 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001319 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001320 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001321 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001322{
1323 union kvm_mmu_page_role role;
1324 unsigned index;
1325 unsigned quadrant;
1326 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001327 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001328 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001329
Avi Kivitya770f6f2008-12-21 19:20:09 +02001330 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001331 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001332 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001333 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001334 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001335 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1336 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1337 role.quadrant = quadrant;
1338 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001339 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001340 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001341 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1342 if (sp->gfn == gfn) {
1343 if (sp->unsync)
1344 if (kvm_sync_page(vcpu, sp))
1345 continue;
1346
1347 if (sp->role.word != role.word)
1348 continue;
1349
Avi Kivity4db35312007-11-21 15:28:32 +02001350 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001351 if (sp->unsync_children) {
1352 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1353 kvm_mmu_mark_parents_unsync(vcpu, sp);
1354 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001355 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001356 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001357 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001358 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001359 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1360 if (!sp)
1361 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001362 sp->gfn = gfn;
1363 sp->role = role;
1364 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001365 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001366 if (rmap_write_protect(vcpu->kvm, gfn))
1367 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001368 account_shadowed(vcpu->kvm, gfn);
1369 }
Avi Kivity131d8272008-05-29 14:56:28 +03001370 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1371 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1372 else
1373 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001374 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001375 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001376}
1377
Avi Kivity2d111232008-12-25 14:39:47 +02001378static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1379 struct kvm_vcpu *vcpu, u64 addr)
1380{
1381 iterator->addr = addr;
1382 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1383 iterator->level = vcpu->arch.mmu.shadow_root_level;
1384 if (iterator->level == PT32E_ROOT_LEVEL) {
1385 iterator->shadow_addr
1386 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1387 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1388 --iterator->level;
1389 if (!iterator->shadow_addr)
1390 iterator->level = 0;
1391 }
1392}
1393
1394static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1395{
1396 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1397 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001398
1399 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1400 if (is_large_pte(*iterator->sptep))
1401 return false;
1402
Avi Kivity2d111232008-12-25 14:39:47 +02001403 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1404 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1405 return true;
1406}
1407
1408static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1409{
1410 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1411 --iterator->level;
1412}
1413
Avi Kivity90cb0522007-07-17 13:04:56 +03001414static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001415 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001416{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001417 unsigned i;
1418 u64 *pt;
1419 u64 ent;
1420
Avi Kivity4db35312007-11-21 15:28:32 +02001421 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001422
Avi Kivity697fe2e2007-01-05 16:36:46 -08001423 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1424 ent = pt[i];
1425
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001426 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001427 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001428 ent &= PT64_BASE_ADDR_MASK;
1429 mmu_page_remove_parent_pte(page_header(ent),
1430 &pt[i]);
1431 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001432 if (is_large_pte(ent))
1433 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001434 rmap_remove(kvm, &pt[i]);
1435 }
1436 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001437 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001438 }
Avi Kivitya4360362007-01-05 16:36:45 -08001439}
1440
Avi Kivity4db35312007-11-21 15:28:32 +02001441static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001442{
Avi Kivity4db35312007-11-21 15:28:32 +02001443 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001444}
1445
Avi Kivity12b7d282007-09-23 14:10:49 +02001446static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1447{
1448 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001449 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001450
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001451 kvm_for_each_vcpu(i, vcpu, kvm)
1452 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001453}
1454
Avi Kivity31aa2b42008-07-11 17:59:46 +03001455static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001456{
1457 u64 *parent_pte;
1458
Avi Kivity4db35312007-11-21 15:28:32 +02001459 while (sp->multimapped || sp->parent_pte) {
1460 if (!sp->multimapped)
1461 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001462 else {
1463 struct kvm_pte_chain *chain;
1464
Avi Kivity4db35312007-11-21 15:28:32 +02001465 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001466 struct kvm_pte_chain, link);
1467 parent_pte = chain->parent_ptes[0];
1468 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001469 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001470 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001471 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001472 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001473}
1474
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001475static int mmu_zap_unsync_children(struct kvm *kvm,
1476 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001477{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001478 int i, zapped = 0;
1479 struct mmu_page_path parents;
1480 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001481
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001482 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001483 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001484
1485 kvm_mmu_pages_init(parent, &parents, &pages);
1486 while (mmu_unsync_walk(parent, &pages)) {
1487 struct kvm_mmu_page *sp;
1488
1489 for_each_sp(pages, sp, parents, i) {
1490 kvm_mmu_zap_page(kvm, sp);
1491 mmu_pages_clear_parents(&parents);
1492 }
1493 zapped += pages.nr;
1494 kvm_mmu_pages_init(parent, &parents, &pages);
1495 }
1496
1497 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001498}
1499
Marcelo Tosatti07385412008-09-23 13:18:37 -03001500static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001501{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001502 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001503
1504 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001505 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001506 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001507 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001508 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001509 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001510 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001511 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001512 if (sp->unsync)
1513 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001514 if (!sp->root_count) {
1515 hlist_del(&sp->hash_link);
1516 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001517 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001518 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001519 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001520 kvm_reload_remote_mmus(kvm);
1521 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001522 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001523 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001524}
1525
Izik Eidus82ce2c92007-10-02 18:52:55 +02001526/*
1527 * Changing the number of mmu pages allocated to the vm
1528 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1529 */
1530void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1531{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001532 int used_pages;
1533
1534 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1535 used_pages = max(0, used_pages);
1536
Izik Eidus82ce2c92007-10-02 18:52:55 +02001537 /*
1538 * If we set the number of mmu pages to be smaller be than the
1539 * number of actived pages , we must to free some mmu pages before we
1540 * change the value
1541 */
1542
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001543 if (used_pages > kvm_nr_mmu_pages) {
1544 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001545 struct kvm_mmu_page *page;
1546
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001547 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001548 struct kvm_mmu_page, link);
1549 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001550 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001551 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001552 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001553 }
1554 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001555 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1556 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001557
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001558 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001559}
1560
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001561static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001562{
1563 unsigned index;
1564 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001565 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001566 struct hlist_node *node, *n;
1567 int r;
1568
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001569 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001570 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001571 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001572 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001573 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001574 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001575 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001576 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001577 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001578 if (kvm_mmu_zap_page(kvm, sp))
1579 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001580 }
1581 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001582}
1583
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001584static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001585{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001586 unsigned index;
1587 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001588 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001589 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001590
Avi Kivity4677a3b2009-01-06 13:00:27 +02001591 index = kvm_page_table_hashfn(gfn);
1592 bucket = &kvm->arch.mmu_page_hash[index];
1593 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001594 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001595 && !sp->role.invalid) {
1596 pgprintk("%s: zap %lx %x\n",
1597 __func__, gfn, sp->role.word);
1598 kvm_mmu_zap_page(kvm, sp);
1599 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001600 }
1601}
1602
Avi Kivity38c335f2007-11-21 14:20:22 +02001603static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001605 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001606 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607
Sheng Yang291f26b2008-10-16 17:30:57 +08001608 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609}
1610
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001611static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1612{
1613 int i;
1614 u64 *pt = sp->spt;
1615
1616 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1617 return;
1618
1619 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1620 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001621 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001622 }
1623}
1624
Avi Kivity039576c2007-03-20 12:46:50 +02001625struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1626{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001627 struct page *page;
1628
Gleb Natapov1871c602010-02-10 14:21:32 +02001629 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Avi Kivity039576c2007-03-20 12:46:50 +02001630
1631 if (gpa == UNMAPPED_GVA)
1632 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001633
Izik Eidus72dc67a2008-02-10 18:04:15 +02001634 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001635
1636 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001637}
1638
Sheng Yang74be52e2008-10-09 16:01:56 +08001639/*
1640 * The function is based on mtrr_type_lookup() in
1641 * arch/x86/kernel/cpu/mtrr/generic.c
1642 */
1643static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1644 u64 start, u64 end)
1645{
1646 int i;
1647 u64 base, mask;
1648 u8 prev_match, curr_match;
1649 int num_var_ranges = KVM_NR_VAR_MTRR;
1650
1651 if (!mtrr_state->enabled)
1652 return 0xFF;
1653
1654 /* Make end inclusive end, instead of exclusive */
1655 end--;
1656
1657 /* Look in fixed ranges. Just return the type as per start */
1658 if (mtrr_state->have_fixed && (start < 0x100000)) {
1659 int idx;
1660
1661 if (start < 0x80000) {
1662 idx = 0;
1663 idx += (start >> 16);
1664 return mtrr_state->fixed_ranges[idx];
1665 } else if (start < 0xC0000) {
1666 idx = 1 * 8;
1667 idx += ((start - 0x80000) >> 14);
1668 return mtrr_state->fixed_ranges[idx];
1669 } else if (start < 0x1000000) {
1670 idx = 3 * 8;
1671 idx += ((start - 0xC0000) >> 12);
1672 return mtrr_state->fixed_ranges[idx];
1673 }
1674 }
1675
1676 /*
1677 * Look in variable ranges
1678 * Look of multiple ranges matching this address and pick type
1679 * as per MTRR precedence
1680 */
1681 if (!(mtrr_state->enabled & 2))
1682 return mtrr_state->def_type;
1683
1684 prev_match = 0xFF;
1685 for (i = 0; i < num_var_ranges; ++i) {
1686 unsigned short start_state, end_state;
1687
1688 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1689 continue;
1690
1691 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1692 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1693 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1694 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1695
1696 start_state = ((start & mask) == (base & mask));
1697 end_state = ((end & mask) == (base & mask));
1698 if (start_state != end_state)
1699 return 0xFE;
1700
1701 if ((start & mask) != (base & mask))
1702 continue;
1703
1704 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1705 if (prev_match == 0xFF) {
1706 prev_match = curr_match;
1707 continue;
1708 }
1709
1710 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1711 curr_match == MTRR_TYPE_UNCACHABLE)
1712 return MTRR_TYPE_UNCACHABLE;
1713
1714 if ((prev_match == MTRR_TYPE_WRBACK &&
1715 curr_match == MTRR_TYPE_WRTHROUGH) ||
1716 (prev_match == MTRR_TYPE_WRTHROUGH &&
1717 curr_match == MTRR_TYPE_WRBACK)) {
1718 prev_match = MTRR_TYPE_WRTHROUGH;
1719 curr_match = MTRR_TYPE_WRTHROUGH;
1720 }
1721
1722 if (prev_match != curr_match)
1723 return MTRR_TYPE_UNCACHABLE;
1724 }
1725
1726 if (prev_match != 0xFF)
1727 return prev_match;
1728
1729 return mtrr_state->def_type;
1730}
1731
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001732u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001733{
1734 u8 mtrr;
1735
1736 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1737 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1738 if (mtrr == 0xfe || mtrr == 0xff)
1739 mtrr = MTRR_TYPE_WRBACK;
1740 return mtrr;
1741}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001742EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001743
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001744static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1745{
1746 unsigned index;
1747 struct hlist_head *bucket;
1748 struct kvm_mmu_page *s;
1749 struct hlist_node *node, *n;
1750
Avi Kivityf691fe12009-07-06 15:58:14 +03001751 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001752 index = kvm_page_table_hashfn(sp->gfn);
1753 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1754 /* don't unsync if pagetable is shadowed with multiple roles */
1755 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001756 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001757 continue;
1758 if (s->role.word != sp->role.word)
1759 return 1;
1760 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001761 ++vcpu->kvm->stat.mmu_unsync;
1762 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001763
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001764 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001765
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001766 mmu_convert_notrap(sp);
1767 return 0;
1768}
1769
1770static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1771 bool can_unsync)
1772{
1773 struct kvm_mmu_page *shadow;
1774
1775 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1776 if (shadow) {
1777 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1778 return 1;
1779 if (shadow->unsync)
1780 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001781 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001782 return kvm_unsync_page(vcpu, shadow);
1783 return 1;
1784 }
1785 return 0;
1786}
1787
Avi Kivityd555c332009-06-10 14:24:23 +03001788static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001789 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001790 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001791 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001792 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001793{
1794 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001795 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001796
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001797 /*
1798 * We don't set the accessed bit, since we sometimes want to see
1799 * whether the guest actually used the pte (in order to detect
1800 * demand paging).
1801 */
Sheng Yang7b523452008-04-25 21:13:50 +08001802 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001803 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001804 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001805 if (!dirty)
1806 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001807 if (pte_access & ACC_EXEC_MASK)
1808 spte |= shadow_x_mask;
1809 else
1810 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001811 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001812 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001813 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001814 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001815 if (tdp_enabled)
1816 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1817 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001818
Izik Eidus14032832009-09-23 21:47:17 +03001819 if (reset_host_protection)
1820 spte |= SPTE_HOST_WRITEABLE;
1821
Anthony Liguori35149e22008-04-02 14:46:56 -05001822 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001823
1824 if ((pte_access & ACC_WRITE_MASK)
1825 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001826
Joerg Roedel852e3c12009-07-27 16:30:44 +02001827 if (level > PT_PAGE_TABLE_LEVEL &&
1828 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001829 ret = 1;
1830 spte = shadow_trap_nonpresent_pte;
1831 goto set_pte;
1832 }
1833
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001834 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001835
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001836 /*
1837 * Optimization: for pte sync, if spte was writable the hash
1838 * lookup is unnecessary (and expensive). Write protection
1839 * is responsibility of mmu_get_page / kvm_sync_page.
1840 * Same reasoning can be applied to dirty page accounting.
1841 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001842 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001843 goto set_pte;
1844
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001845 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001846 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001847 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001848 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001849 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001850 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001851 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001852 }
1853 }
1854
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001855 if (pte_access & ACC_WRITE_MASK)
1856 mark_page_dirty(vcpu->kvm, gfn);
1857
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001858set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001859 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001860 return ret;
1861}
1862
Avi Kivityd555c332009-06-10 14:24:23 +03001863static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001864 unsigned pt_access, unsigned pte_access,
1865 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001866 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001867 pfn_t pfn, bool speculative,
1868 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001869{
1870 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001871 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001872 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001873
1874 pgprintk("%s: spte %llx access %x write_fault %d"
1875 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001876 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001877 write_fault, user_fault, gfn);
1878
Avi Kivityd555c332009-06-10 14:24:23 +03001879 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001880 /*
1881 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1882 * the parent of the now unreachable PTE.
1883 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001884 if (level > PT_PAGE_TABLE_LEVEL &&
1885 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001886 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001887 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001888
1889 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001890 mmu_page_remove_parent_pte(child, sptep);
1891 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001892 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001893 spte_to_pfn(*sptep), pfn);
1894 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001895 } else
1896 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001897 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001898
Avi Kivityd555c332009-06-10 14:24:23 +03001899 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001900 dirty, level, gfn, pfn, speculative, true,
1901 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001902 if (write_fault)
1903 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001904 kvm_x86_ops->tlb_flush(vcpu);
1905 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001906
Avi Kivityd555c332009-06-10 14:24:23 +03001907 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001908 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001909 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001910 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1911 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001912 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001913 ++vcpu->kvm->stat.lpages;
1914
Avi Kivityd555c332009-06-10 14:24:23 +03001915 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001916 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001917 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001918 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001919 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001920 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001921 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001922 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001923 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001924 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001925 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001926 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001927 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001928 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001929 vcpu->arch.last_pte_gfn = gfn;
1930 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001931}
1932
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1934{
1935}
1936
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001937static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001938 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939{
Avi Kivity9f652d22008-12-25 14:54:25 +02001940 struct kvm_shadow_walk_iterator iterator;
1941 struct kvm_mmu_page *sp;
1942 int pt_write = 0;
1943 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944
Avi Kivity9f652d22008-12-25 14:54:25 +02001945 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001946 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001947 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1948 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001949 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001950 ++vcpu->stat.pf_fixed;
1951 break;
1952 }
1953
1954 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1955 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1956 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1957 iterator.level - 1,
1958 1, ACC_ALL, iterator.sptep);
1959 if (!sp) {
1960 pgprintk("nonpaging_map: ENOMEM\n");
1961 kvm_release_pfn_clean(pfn);
1962 return -ENOMEM;
1963 }
1964
Avi Kivityd555c332009-06-10 14:24:23 +03001965 __set_spte(iterator.sptep,
1966 __pa(sp->spt)
1967 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1968 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001969 }
1970 }
1971 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972}
1973
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001974static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1975{
1976 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001977 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001978 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001979 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001980
Joerg Roedel852e3c12009-07-27 16:30:44 +02001981 level = mapping_level(vcpu, gfn);
1982
1983 /*
1984 * This path builds a PAE pagetable - so we can map 2mb pages at
1985 * maximum. Therefore check if the level is larger than that.
1986 */
1987 if (level > PT_DIRECTORY_LEVEL)
1988 level = PT_DIRECTORY_LEVEL;
1989
1990 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001991
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001992 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001993 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001994 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001995
Avi Kivityd196e342008-01-24 11:44:11 +02001996 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001997 if (is_error_pfn(pfn)) {
1998 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001999 return 1;
2000 }
2001
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002002 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002003 if (mmu_notifier_retry(vcpu, mmu_seq))
2004 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002005 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002006 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002007 spin_unlock(&vcpu->kvm->mmu_lock);
2008
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002009
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002010 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002011
2012out_unlock:
2013 spin_unlock(&vcpu->kvm->mmu_lock);
2014 kvm_release_pfn_clean(pfn);
2015 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002016}
2017
2018
Avi Kivity17ac10a2007-01-05 16:36:40 -08002019static void mmu_free_roots(struct kvm_vcpu *vcpu)
2020{
2021 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002022 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002023
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002024 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002025 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002026 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002027 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2028 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002029
Avi Kivity4db35312007-11-21 15:28:32 +02002030 sp = page_header(root);
2031 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002032 if (!sp->root_count && sp->role.invalid)
2033 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002034 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002035 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002036 return;
2037 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002039 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002040
Avi Kivity417726a2007-04-12 17:35:58 +03002041 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002042 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002043 sp = page_header(root);
2044 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002045 if (!sp->root_count && sp->role.invalid)
2046 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002047 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002048 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002049 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002050 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002051 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002052}
2053
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002054static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2055{
2056 int ret = 0;
2057
2058 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2059 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2060 ret = 1;
2061 }
2062
2063 return ret;
2064}
2065
2066static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002067{
2068 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002069 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002070 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002071 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002072 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002073
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002074 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002075
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002076 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2077 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002078
2079 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002080 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002081 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002082 if (mmu_check_root(vcpu, root_gfn))
2083 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002084 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002085 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002086 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002087 root = __pa(sp->spt);
2088 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002089 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002090 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002091 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002092 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002093 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002094 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002095 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002096 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002097
2098 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002099 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002100 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002101 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002102 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002103 continue;
2104 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002105 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002106 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002107 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002108 if (mmu_check_root(vcpu, root_gfn))
2109 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002110 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002111 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002112 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002113 root = __pa(sp->spt);
2114 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002115 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002116 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002117 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002118 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002119}
2120
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002121static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2122{
2123 int i;
2124 struct kvm_mmu_page *sp;
2125
2126 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2127 return;
2128 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2129 hpa_t root = vcpu->arch.mmu.root_hpa;
2130 sp = page_header(root);
2131 mmu_sync_children(vcpu, sp);
2132 return;
2133 }
2134 for (i = 0; i < 4; ++i) {
2135 hpa_t root = vcpu->arch.mmu.pae_root[i];
2136
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002137 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002138 root &= PT64_BASE_ADDR_MASK;
2139 sp = page_header(root);
2140 mmu_sync_children(vcpu, sp);
2141 }
2142 }
2143}
2144
2145void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2146{
2147 spin_lock(&vcpu->kvm->mmu_lock);
2148 mmu_sync_roots(vcpu);
2149 spin_unlock(&vcpu->kvm->mmu_lock);
2150}
2151
Gleb Natapov1871c602010-02-10 14:21:32 +02002152static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2153 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154{
Gleb Natapov1871c602010-02-10 14:21:32 +02002155 if (error)
2156 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157 return vaddr;
2158}
2159
2160static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002161 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162{
Avi Kivitye8332402007-12-09 18:43:00 +02002163 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002164 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002166 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002167 r = mmu_topup_memory_caches(vcpu);
2168 if (r)
2169 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002170
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002172 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173
Avi Kivitye8332402007-12-09 18:43:00 +02002174 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175
Avi Kivitye8332402007-12-09 18:43:00 +02002176 return nonpaging_map(vcpu, gva & PAGE_MASK,
2177 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178}
2179
Joerg Roedelfb72d162008-02-07 13:47:44 +01002180static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2181 u32 error_code)
2182{
Anthony Liguori35149e22008-04-02 14:46:56 -05002183 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002184 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002185 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002186 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002187 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002188
2189 ASSERT(vcpu);
2190 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2191
2192 r = mmu_topup_memory_caches(vcpu);
2193 if (r)
2194 return r;
2195
Joerg Roedel852e3c12009-07-27 16:30:44 +02002196 level = mapping_level(vcpu, gfn);
2197
2198 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2199
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002200 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002201 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002202 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002203 if (is_error_pfn(pfn)) {
2204 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002205 return 1;
2206 }
2207 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002208 if (mmu_notifier_retry(vcpu, mmu_seq))
2209 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002210 kvm_mmu_free_some_pages(vcpu);
2211 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002212 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002213 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002214
2215 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002216
2217out_unlock:
2218 spin_unlock(&vcpu->kvm->mmu_lock);
2219 kvm_release_pfn_clean(pfn);
2220 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002221}
2222
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223static void nonpaging_free(struct kvm_vcpu *vcpu)
2224{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002225 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226}
2227
2228static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2229{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002230 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231
2232 context->new_cr3 = nonpaging_new_cr3;
2233 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234 context->gva_to_gpa = nonpaging_gva_to_gpa;
2235 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002236 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002237 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002238 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002239 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002241 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242 return 0;
2243}
2244
Avi Kivityd835dfe2007-11-21 02:57:59 +02002245void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002247 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002248 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249}
2250
2251static void paging_new_cr3(struct kvm_vcpu *vcpu)
2252{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002253 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002254 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255}
2256
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257static void inject_page_fault(struct kvm_vcpu *vcpu,
2258 u64 addr,
2259 u32 err_code)
2260{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002261 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262}
2263
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264static void paging_free(struct kvm_vcpu *vcpu)
2265{
2266 nonpaging_free(vcpu);
2267}
2268
Dong, Eddie82725b22009-03-30 16:21:08 +08002269static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2270{
2271 int bit7;
2272
2273 bit7 = (gpte >> 7) & 1;
2274 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2275}
2276
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277#define PTTYPE 64
2278#include "paging_tmpl.h"
2279#undef PTTYPE
2280
2281#define PTTYPE 32
2282#include "paging_tmpl.h"
2283#undef PTTYPE
2284
Dong, Eddie82725b22009-03-30 16:21:08 +08002285static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2286{
2287 struct kvm_mmu *context = &vcpu->arch.mmu;
2288 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2289 u64 exb_bit_rsvd = 0;
2290
2291 if (!is_nx(vcpu))
2292 exb_bit_rsvd = rsvd_bits(63, 63);
2293 switch (level) {
2294 case PT32_ROOT_LEVEL:
2295 /* no rsvd bits for 2 level 4K page table entries */
2296 context->rsvd_bits_mask[0][1] = 0;
2297 context->rsvd_bits_mask[0][0] = 0;
2298 if (is_cpuid_PSE36())
2299 /* 36bits PSE 4MB page */
2300 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2301 else
2302 /* 32 bits PSE 4MB page */
2303 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002304 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002305 break;
2306 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002307 context->rsvd_bits_mask[0][2] =
2308 rsvd_bits(maxphyaddr, 63) |
2309 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002310 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002311 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002312 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2313 rsvd_bits(maxphyaddr, 62); /* PTE */
2314 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2315 rsvd_bits(maxphyaddr, 62) |
2316 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002317 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002318 break;
2319 case PT64_ROOT_LEVEL:
2320 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2321 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2322 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2323 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2324 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002325 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002326 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2327 rsvd_bits(maxphyaddr, 51);
2328 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002329 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2330 rsvd_bits(maxphyaddr, 51) |
2331 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002332 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002333 rsvd_bits(maxphyaddr, 51) |
2334 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002335 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002336 break;
2337 }
2338}
2339
Avi Kivity17ac10a2007-01-05 16:36:40 -08002340static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002342 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343
2344 ASSERT(is_pae(vcpu));
2345 context->new_cr3 = paging_new_cr3;
2346 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002348 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002349 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002350 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002352 context->root_level = level;
2353 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002354 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 return 0;
2356}
2357
Avi Kivity17ac10a2007-01-05 16:36:40 -08002358static int paging64_init_context(struct kvm_vcpu *vcpu)
2359{
Dong, Eddie82725b22009-03-30 16:21:08 +08002360 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002361 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2362}
2363
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364static int paging32_init_context(struct kvm_vcpu *vcpu)
2365{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002366 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367
Dong, Eddie82725b22009-03-30 16:21:08 +08002368 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 context->new_cr3 = paging_new_cr3;
2370 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 context->gva_to_gpa = paging32_gva_to_gpa;
2372 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002373 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002374 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002375 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 context->root_level = PT32_ROOT_LEVEL;
2377 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002378 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 return 0;
2380}
2381
2382static int paging32E_init_context(struct kvm_vcpu *vcpu)
2383{
Dong, Eddie82725b22009-03-30 16:21:08 +08002384 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002385 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Joerg Roedelfb72d162008-02-07 13:47:44 +01002388static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2389{
2390 struct kvm_mmu *context = &vcpu->arch.mmu;
2391
2392 context->new_cr3 = nonpaging_new_cr3;
2393 context->page_fault = tdp_page_fault;
2394 context->free = nonpaging_free;
2395 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002396 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002397 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002398 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002399 context->root_hpa = INVALID_PAGE;
2400
2401 if (!is_paging(vcpu)) {
2402 context->gva_to_gpa = nonpaging_gva_to_gpa;
2403 context->root_level = 0;
2404 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002405 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002406 context->gva_to_gpa = paging64_gva_to_gpa;
2407 context->root_level = PT64_ROOT_LEVEL;
2408 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002409 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002410 context->gva_to_gpa = paging64_gva_to_gpa;
2411 context->root_level = PT32E_ROOT_LEVEL;
2412 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002413 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002414 context->gva_to_gpa = paging32_gva_to_gpa;
2415 context->root_level = PT32_ROOT_LEVEL;
2416 }
2417
2418 return 0;
2419}
2420
2421static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002423 int r;
2424
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002426 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
2428 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002429 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002430 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002431 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002433 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002435 r = paging32_init_context(vcpu);
2436
2437 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2438
2439 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440}
2441
Joerg Roedelfb72d162008-02-07 13:47:44 +01002442static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2443{
Anthony Liguori35149e22008-04-02 14:46:56 -05002444 vcpu->arch.update_pte.pfn = bad_pfn;
2445
Joerg Roedelfb72d162008-02-07 13:47:44 +01002446 if (tdp_enabled)
2447 return init_kvm_tdp_mmu(vcpu);
2448 else
2449 return init_kvm_softmmu(vcpu);
2450}
2451
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2453{
2454 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002455 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2456 vcpu->arch.mmu.free(vcpu);
2457 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 }
2459}
2460
2461int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2462{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002463 destroy_kvm_mmu(vcpu);
2464 return init_kvm_mmu(vcpu);
2465}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002466EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002467
2468int kvm_mmu_load(struct kvm_vcpu *vcpu)
2469{
Avi Kivity714b93d2007-01-05 16:36:53 -08002470 int r;
2471
Avi Kivitye2dec932007-01-05 16:36:54 -08002472 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002473 if (r)
2474 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002475 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002476 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002477 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002478 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002479 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002480 if (r)
2481 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002482 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002483 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002484out:
2485 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002487EXPORT_SYMBOL_GPL(kvm_mmu_load);
2488
2489void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2490{
2491 mmu_free_roots(vcpu);
2492}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493
Avi Kivity09072da2007-05-01 14:16:52 +03002494static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002495 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002496 u64 *spte)
2497{
2498 u64 pte;
2499 struct kvm_mmu_page *child;
2500
2501 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002502 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002503 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002504 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002505 else {
2506 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002507 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002508 }
2509 }
Avi Kivityd555c332009-06-10 14:24:23 +03002510 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002511 if (is_large_pte(pte))
2512 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002513}
2514
Avi Kivity00284252007-05-01 16:53:31 +03002515static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002516 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002517 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002518 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002519{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002520 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002521 ++vcpu->kvm->stat.mmu_pde_zapped;
2522 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002523 }
Avi Kivity00284252007-05-01 16:53:31 +03002524
Avi Kivity4cee5762007-11-18 16:37:07 +02002525 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002526 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002527 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002528 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002529 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002530}
2531
Avi Kivity79539ce2007-11-21 02:06:21 +02002532static bool need_remote_flush(u64 old, u64 new)
2533{
2534 if (!is_shadow_present_pte(old))
2535 return false;
2536 if (!is_shadow_present_pte(new))
2537 return true;
2538 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2539 return true;
2540 old ^= PT64_NX_MASK;
2541 new ^= PT64_NX_MASK;
2542 return (old & ~new & PT64_PERM_MASK) != 0;
2543}
2544
2545static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2546{
2547 if (need_remote_flush(old, new))
2548 kvm_flush_remote_tlbs(vcpu->kvm);
2549 else
2550 kvm_mmu_flush_tlb(vcpu);
2551}
2552
Avi Kivity12b7d282007-09-23 14:10:49 +02002553static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2554{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002555 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002556
Sheng Yang7b523452008-04-25 21:13:50 +08002557 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002558}
2559
Avi Kivityd7824ff2007-12-30 12:29:05 +02002560static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2561 const u8 *new, int bytes)
2562{
2563 gfn_t gfn;
2564 int r;
2565 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002566 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002567
2568 if (bytes != 4 && bytes != 8)
2569 return;
2570
2571 /*
2572 * Assume that the pte write on a page table of the same type
2573 * as the current vcpu paging mode. This is nearly always true
2574 * (might be false while changing modes). Note it is verified later
2575 * by update_pte().
2576 */
2577 if (is_pae(vcpu)) {
2578 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2579 if ((bytes == 4) && (gpa % 4 == 0)) {
2580 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2581 if (r)
2582 return;
2583 memcpy((void *)&gpte + (gpa % 8), new, 4);
2584 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2585 memcpy((void *)&gpte, new, 8);
2586 }
2587 } else {
2588 if ((bytes == 4) && (gpa % 4 == 0))
2589 memcpy((void *)&gpte, new, 4);
2590 }
Avi Kivity43a37952009-06-10 14:12:05 +03002591 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002592 return;
2593 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002594
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002595 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002596 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002597 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002598
Anthony Liguori35149e22008-04-02 14:46:56 -05002599 if (is_error_pfn(pfn)) {
2600 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002601 return;
2602 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002603 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002604 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002605}
2606
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002607static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2608{
2609 u64 *spte = vcpu->arch.last_pte_updated;
2610
2611 if (spte
2612 && vcpu->arch.last_pte_gfn == gfn
2613 && shadow_accessed_mask
2614 && !(*spte & shadow_accessed_mask)
2615 && is_shadow_present_pte(*spte))
2616 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2617}
2618
Avi Kivity09072da2007-05-01 14:16:52 +03002619void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002620 const u8 *new, int bytes,
2621 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002622{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002623 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002624 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002625 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002626 struct hlist_head *bucket;
2627 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002628 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002629 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002630 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002631 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002632 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002633 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002634 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002635 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002636 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002637 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002638 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002639
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002640 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002641 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002642 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002643 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002644 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002645 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002646 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002647 if (guest_initiated) {
2648 if (gfn == vcpu->arch.last_pt_write_gfn
2649 && !last_updated_pte_accessed(vcpu)) {
2650 ++vcpu->arch.last_pt_write_count;
2651 if (vcpu->arch.last_pt_write_count >= 3)
2652 flooded = 1;
2653 } else {
2654 vcpu->arch.last_pt_write_gfn = gfn;
2655 vcpu->arch.last_pt_write_count = 1;
2656 vcpu->arch.last_pte_updated = NULL;
2657 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002658 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002659 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002660 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002661 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002662 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002663 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002664 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002665 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002666 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002667 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002668 /*
2669 * Misaligned accesses are too much trouble to fix
2670 * up; also, they usually indicate a page is not used
2671 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002672 *
2673 * If we're seeing too many writes to a page,
2674 * it may no longer be a page table, or we may be
2675 * forking, in which case it is better to unmap the
2676 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002677 */
2678 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002679 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002680 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2681 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002682 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002683 continue;
2684 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002685 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002686 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002687 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002688 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002689 page_offset <<= 1; /* 32->64 */
2690 /*
2691 * A 32-bit pde maps 4MB while the shadow pdes map
2692 * only 2MB. So we need to double the offset again
2693 * and zap two pdes instead of one.
2694 */
2695 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002696 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002697 page_offset <<= 1;
2698 npte = 2;
2699 }
Avi Kivityfce06572007-05-01 16:44:05 +03002700 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002701 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002702 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002703 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002704 }
Avi Kivity4db35312007-11-21 15:28:32 +02002705 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002706 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2707 gentry = 0;
2708 r = kvm_read_guest_atomic(vcpu->kvm,
2709 gpa & ~(u64)(pte_size - 1),
2710 &gentry, pte_size);
2711 new = (const void *)&gentry;
2712 if (r < 0)
2713 new = NULL;
2714 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002715 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002716 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002717 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002718 if (new)
2719 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002720 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002721 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002722 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002723 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002724 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002725 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002726 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2727 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2728 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002729 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002730}
2731
Avi Kivitya4360362007-01-05 16:36:45 -08002732int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2733{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002734 gpa_t gpa;
2735 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002736
Avi Kivity60f24782009-08-27 13:37:06 +03002737 if (tdp_enabled)
2738 return 0;
2739
Gleb Natapov1871c602010-02-10 14:21:32 +02002740 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002741
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002742 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002743 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002744 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002745 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002746}
Avi Kivity577bdc42008-07-19 08:57:05 +03002747EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002748
Avi Kivity22d95b12007-09-14 20:26:06 +03002749void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002750{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002751 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2752 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002753 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002754
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002755 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002756 struct kvm_mmu_page, link);
2757 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002758 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002759 }
2760}
Avi Kivityebeace82007-01-05 16:36:47 -08002761
Avi Kivity30677142007-10-28 18:48:59 +02002762int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2763{
2764 int r;
2765 enum emulation_result er;
2766
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002767 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002768 if (r < 0)
2769 goto out;
2770
2771 if (!r) {
2772 r = 1;
2773 goto out;
2774 }
2775
Avi Kivityb733bfb2007-10-28 18:52:05 +02002776 r = mmu_topup_memory_caches(vcpu);
2777 if (r)
2778 goto out;
2779
Avi Kivity851ba692009-08-24 11:10:17 +03002780 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002781
2782 switch (er) {
2783 case EMULATE_DONE:
2784 return 1;
2785 case EMULATE_DO_MMIO:
2786 ++vcpu->stat.mmio_exits;
2787 return 0;
2788 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002789 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2790 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002791 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002792 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002793 default:
2794 BUG();
2795 }
2796out:
Avi Kivity30677142007-10-28 18:48:59 +02002797 return r;
2798}
2799EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2800
Marcelo Tosattia7052892008-09-23 13:18:35 -03002801void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2802{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002803 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002804 kvm_mmu_flush_tlb(vcpu);
2805 ++vcpu->stat.invlpg;
2806}
2807EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2808
Joerg Roedel18552672008-02-07 13:47:41 +01002809void kvm_enable_tdp(void)
2810{
2811 tdp_enabled = true;
2812}
2813EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2814
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002815void kvm_disable_tdp(void)
2816{
2817 tdp_enabled = false;
2818}
2819EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2820
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821static void free_mmu_pages(struct kvm_vcpu *vcpu)
2822{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002823 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824}
2825
2826static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2827{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002828 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 int i;
2830
2831 ASSERT(vcpu);
2832
Avi Kivity17ac10a2007-01-05 16:36:40 -08002833 /*
2834 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2835 * Therefore we need to allocate shadow page tables in the first
2836 * 4GB of memory, which happens to fit the DMA32 zone.
2837 */
2838 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2839 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002840 return -ENOMEM;
2841
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002842 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002843 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002844 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002845
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847}
2848
Ingo Molnar8018c272006-12-29 16:50:01 -08002849int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002852 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853
Ingo Molnar8018c272006-12-29 16:50:01 -08002854 return alloc_mmu_pages(vcpu);
2855}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856
Ingo Molnar8018c272006-12-29 16:50:01 -08002857int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2858{
2859 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002860 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002861
Ingo Molnar8018c272006-12-29 16:50:01 -08002862 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863}
2864
2865void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2866{
2867 ASSERT(vcpu);
2868
2869 destroy_kvm_mmu(vcpu);
2870 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002871 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872}
2873
Avi Kivity90cb0522007-07-17 13:04:56 +03002874void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875{
Avi Kivity4db35312007-11-21 15:28:32 +02002876 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002878 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 int i;
2880 u64 *pt;
2881
Sheng Yang291f26b2008-10-16 17:30:57 +08002882 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 continue;
2884
Avi Kivity4db35312007-11-21 15:28:32 +02002885 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2887 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002888 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 }
Avi Kivity171d5952008-08-27 16:40:51 +03002891 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002893
Avi Kivity90cb0522007-07-17 13:04:56 +03002894void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002895{
Avi Kivity4db35312007-11-21 15:28:32 +02002896 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002897
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002898 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002899 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002900 if (kvm_mmu_zap_page(kvm, sp))
2901 node = container_of(kvm->arch.active_mmu_pages.next,
2902 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002903 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002904
Avi Kivity90cb0522007-07-17 13:04:56 +03002905 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002906}
2907
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002908static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002909{
2910 struct kvm_mmu_page *page;
2911
2912 page = container_of(kvm->arch.active_mmu_pages.prev,
2913 struct kvm_mmu_page, link);
2914 kvm_mmu_zap_page(kvm, page);
2915}
2916
2917static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2918{
2919 struct kvm *kvm;
2920 struct kvm *kvm_freed = NULL;
2921 int cache_count = 0;
2922
2923 spin_lock(&kvm_lock);
2924
2925 list_for_each_entry(kvm, &vm_list, vm_list) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002926 int npages, idx;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002927
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002928 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002929 spin_lock(&kvm->mmu_lock);
2930 npages = kvm->arch.n_alloc_mmu_pages -
2931 kvm->arch.n_free_mmu_pages;
2932 cache_count += npages;
2933 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2934 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2935 cache_count--;
2936 kvm_freed = kvm;
2937 }
2938 nr_to_scan--;
2939
2940 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002941 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002942 }
2943 if (kvm_freed)
2944 list_move_tail(&kvm_freed->vm_list, &vm_list);
2945
2946 spin_unlock(&kvm_lock);
2947
2948 return cache_count;
2949}
2950
2951static struct shrinker mmu_shrinker = {
2952 .shrink = mmu_shrink,
2953 .seeks = DEFAULT_SEEKS * 10,
2954};
2955
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002956static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002957{
2958 if (pte_chain_cache)
2959 kmem_cache_destroy(pte_chain_cache);
2960 if (rmap_desc_cache)
2961 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002962 if (mmu_page_header_cache)
2963 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002964}
2965
Izik Eidus3ee16c82008-03-30 15:17:21 +03002966void kvm_mmu_module_exit(void)
2967{
2968 mmu_destroy_caches();
2969 unregister_shrinker(&mmu_shrinker);
2970}
2971
Avi Kivityb5a33a72007-04-15 16:31:09 +03002972int kvm_mmu_module_init(void)
2973{
2974 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2975 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002976 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002977 if (!pte_chain_cache)
2978 goto nomem;
2979 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2980 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002981 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002982 if (!rmap_desc_cache)
2983 goto nomem;
2984
Avi Kivityd3d25b02007-05-30 12:34:53 +03002985 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2986 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002987 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002988 if (!mmu_page_header_cache)
2989 goto nomem;
2990
Izik Eidus3ee16c82008-03-30 15:17:21 +03002991 register_shrinker(&mmu_shrinker);
2992
Avi Kivityb5a33a72007-04-15 16:31:09 +03002993 return 0;
2994
2995nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002996 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002997 return -ENOMEM;
2998}
2999
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003000/*
3001 * Caculate mmu pages needed for kvm.
3002 */
3003unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3004{
3005 int i;
3006 unsigned int nr_mmu_pages;
3007 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003008 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003009
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003010 slots = rcu_dereference(kvm->memslots);
3011 for (i = 0; i < slots->nmemslots; i++)
3012 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003013
3014 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3015 nr_mmu_pages = max(nr_mmu_pages,
3016 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3017
3018 return nr_mmu_pages;
3019}
3020
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003021static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3022 unsigned len)
3023{
3024 if (len > buffer->len)
3025 return NULL;
3026 return buffer->ptr;
3027}
3028
3029static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3030 unsigned len)
3031{
3032 void *ret;
3033
3034 ret = pv_mmu_peek_buffer(buffer, len);
3035 if (!ret)
3036 return ret;
3037 buffer->ptr += len;
3038 buffer->len -= len;
3039 buffer->processed += len;
3040 return ret;
3041}
3042
3043static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3044 gpa_t addr, gpa_t value)
3045{
3046 int bytes = 8;
3047 int r;
3048
3049 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3050 bytes = 4;
3051
3052 r = mmu_topup_memory_caches(vcpu);
3053 if (r)
3054 return r;
3055
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003056 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003057 return -EFAULT;
3058
3059 return 1;
3060}
3061
3062static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3063{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003064 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003065 return 1;
3066}
3067
3068static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3069{
3070 spin_lock(&vcpu->kvm->mmu_lock);
3071 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3072 spin_unlock(&vcpu->kvm->mmu_lock);
3073 return 1;
3074}
3075
3076static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3077 struct kvm_pv_mmu_op_buffer *buffer)
3078{
3079 struct kvm_mmu_op_header *header;
3080
3081 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3082 if (!header)
3083 return 0;
3084 switch (header->op) {
3085 case KVM_MMU_OP_WRITE_PTE: {
3086 struct kvm_mmu_op_write_pte *wpte;
3087
3088 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3089 if (!wpte)
3090 return 0;
3091 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3092 wpte->pte_val);
3093 }
3094 case KVM_MMU_OP_FLUSH_TLB: {
3095 struct kvm_mmu_op_flush_tlb *ftlb;
3096
3097 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3098 if (!ftlb)
3099 return 0;
3100 return kvm_pv_mmu_flush_tlb(vcpu);
3101 }
3102 case KVM_MMU_OP_RELEASE_PT: {
3103 struct kvm_mmu_op_release_pt *rpt;
3104
3105 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3106 if (!rpt)
3107 return 0;
3108 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3109 }
3110 default: return 0;
3111 }
3112}
3113
3114int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3115 gpa_t addr, unsigned long *ret)
3116{
3117 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003118 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003119
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003120 buffer->ptr = buffer->buf;
3121 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3122 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003123
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003124 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003125 if (r)
3126 goto out;
3127
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003128 while (buffer->len) {
3129 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003130 if (r < 0)
3131 goto out;
3132 if (r == 0)
3133 break;
3134 }
3135
3136 r = 1;
3137out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003138 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003139 return r;
3140}
3141
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003142int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3143{
3144 struct kvm_shadow_walk_iterator iterator;
3145 int nr_sptes = 0;
3146
3147 spin_lock(&vcpu->kvm->mmu_lock);
3148 for_each_shadow_entry(vcpu, addr, iterator) {
3149 sptes[iterator.level-1] = *iterator.sptep;
3150 nr_sptes++;
3151 if (!is_shadow_present_pte(*iterator.sptep))
3152 break;
3153 }
3154 spin_unlock(&vcpu->kvm->mmu_lock);
3155
3156 return nr_sptes;
3157}
3158EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3159
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003160#ifdef AUDIT
3161
3162static const char *audit_msg;
3163
3164static gva_t canonicalize(gva_t gva)
3165{
3166#ifdef CONFIG_X86_64
3167 gva = (long long)(gva << 16) >> 16;
3168#endif
3169 return gva;
3170}
3171
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003172
3173typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3174 u64 *sptep);
3175
3176static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3177 inspect_spte_fn fn)
3178{
3179 int i;
3180
3181 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3182 u64 ent = sp->spt[i];
3183
3184 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003185 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003186 struct kvm_mmu_page *child;
3187 child = page_header(ent & PT64_BASE_ADDR_MASK);
3188 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003189 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003190 fn(kvm, sp, &sp->spt[i]);
3191 }
3192 }
3193}
3194
3195static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3196{
3197 int i;
3198 struct kvm_mmu_page *sp;
3199
3200 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3201 return;
3202 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3203 hpa_t root = vcpu->arch.mmu.root_hpa;
3204 sp = page_header(root);
3205 __mmu_spte_walk(vcpu->kvm, sp, fn);
3206 return;
3207 }
3208 for (i = 0; i < 4; ++i) {
3209 hpa_t root = vcpu->arch.mmu.pae_root[i];
3210
3211 if (root && VALID_PAGE(root)) {
3212 root &= PT64_BASE_ADDR_MASK;
3213 sp = page_header(root);
3214 __mmu_spte_walk(vcpu->kvm, sp, fn);
3215 }
3216 }
3217 return;
3218}
3219
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003220static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3221 gva_t va, int level)
3222{
3223 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3224 int i;
3225 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3226
3227 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3228 u64 ent = pt[i];
3229
Avi Kivityc7addb92007-09-16 18:58:32 +02003230 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003231 continue;
3232
3233 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003234 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3235 audit_mappings_page(vcpu, ent, va, level - 1);
3236 else {
Gleb Natapov1871c602010-02-10 14:21:32 +02003237 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
Jan Kiszka34382532009-04-25 12:43:21 +02003238 gfn_t gfn = gpa >> PAGE_SHIFT;
3239 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3240 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003241
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003242 if (is_error_pfn(pfn)) {
3243 kvm_release_pfn_clean(pfn);
3244 continue;
3245 }
3246
Avi Kivityc7addb92007-09-16 18:58:32 +02003247 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003248 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003249 printk(KERN_ERR "xx audit error: (%s) levels %d"
3250 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003251 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003252 va, gpa, hpa, ent,
3253 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003254 else if (ent == shadow_notrap_nonpresent_pte
3255 && !is_error_hpa(hpa))
3256 printk(KERN_ERR "audit: (%s) notrap shadow,"
3257 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003258 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003259
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003260 }
3261 }
3262}
3263
3264static void audit_mappings(struct kvm_vcpu *vcpu)
3265{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003266 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003267
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003268 if (vcpu->arch.mmu.root_level == 4)
3269 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003270 else
3271 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003272 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003273 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003274 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003275 i << 30,
3276 2);
3277}
3278
3279static int count_rmaps(struct kvm_vcpu *vcpu)
3280{
3281 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003282 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003283
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003284 idx = srcu_read_lock(&kvm->srcu);
3285 slots = rcu_dereference(kvm->memslots);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003286 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003287 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003288 struct kvm_rmap_desc *d;
3289
3290 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003291 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003292
Izik Eidus290fc382007-09-27 14:11:22 +02003293 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003294 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003295 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003296 ++nmaps;
3297 continue;
3298 }
Izik Eidus290fc382007-09-27 14:11:22 +02003299 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003300 while (d) {
3301 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003302 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303 ++nmaps;
3304 else
3305 break;
3306 d = d->more;
3307 }
3308 }
3309 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003310 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003311 return nmaps;
3312}
3313
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003314void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003316 unsigned long *rmapp;
3317 struct kvm_mmu_page *rev_sp;
3318 gfn_t gfn;
3319
3320 if (*sptep & PT_WRITABLE_MASK) {
3321 rev_sp = page_header(__pa(sptep));
3322 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3323
3324 if (!gfn_to_memslot(kvm, gfn)) {
3325 if (!printk_ratelimit())
3326 return;
3327 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3328 audit_msg, gfn);
3329 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3330 audit_msg, sptep - rev_sp->spt,
3331 rev_sp->gfn);
3332 dump_stack();
3333 return;
3334 }
3335
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003336 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3337 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003338 if (!*rmapp) {
3339 if (!printk_ratelimit())
3340 return;
3341 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3342 audit_msg, *sptep);
3343 dump_stack();
3344 }
3345 }
3346
3347}
3348
3349void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3350{
3351 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3352}
3353
3354static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3355{
Avi Kivity4db35312007-11-21 15:28:32 +02003356 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003357 int i;
3358
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003359 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003360 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003361
Avi Kivity4db35312007-11-21 15:28:32 +02003362 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003363 continue;
3364
3365 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3366 u64 ent = pt[i];
3367
3368 if (!(ent & PT_PRESENT_MASK))
3369 continue;
3370 if (!(ent & PT_WRITABLE_MASK))
3371 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003372 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003373 }
3374 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003375 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003376}
3377
3378static void audit_rmap(struct kvm_vcpu *vcpu)
3379{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003380 check_writable_mappings_rmap(vcpu);
3381 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003382}
3383
3384static void audit_write_protection(struct kvm_vcpu *vcpu)
3385{
Avi Kivity4db35312007-11-21 15:28:32 +02003386 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003387 struct kvm_memory_slot *slot;
3388 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003389 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003390 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003391
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003392 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003393 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003394 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003395 if (sp->unsync)
3396 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003397
Avi Kivity4db35312007-11-21 15:28:32 +02003398 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003399 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003400 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003401
3402 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3403 while (spte) {
3404 if (*spte & PT_WRITABLE_MASK)
3405 printk(KERN_ERR "%s: (%s) shadow page has "
3406 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003407 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003408 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003409 spte = rmap_next(vcpu->kvm, rmapp, spte);
3410 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003411 }
3412}
3413
3414static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3415{
3416 int olddbg = dbg;
3417
3418 dbg = 0;
3419 audit_msg = msg;
3420 audit_rmap(vcpu);
3421 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003422 if (strcmp("pre pte write", audit_msg) != 0)
3423 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003424 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003425 dbg = olddbg;
3426}
3427
3428#endif