blob: dc4d954efacd11e6f98f6c168e8c2ae7ed38487a [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
141#define PFERR_PRESENT_MASK (1U << 0)
142#define PFERR_WRITE_MASK (1U << 1)
143#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800144#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800145#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800147#define RMAP_EXT 4
148
Avi Kivityfe135d22007-12-09 16:15:46 +0200149#define ACC_EXEC_MASK 1
150#define ACC_WRITE_MASK PT_WRITABLE_MASK
151#define ACC_USER_MASK PT_USER_MASK
152#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
153
Avi Kivity07420172009-07-06 12:21:32 +0300154#define CREATE_TRACE_POINTS
155#include "mmutrace.h"
156
Izik Eidus14032832009-09-23 21:47:17 +0300157#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
158
Avi Kivity135f8c22008-08-21 17:49:56 +0300159#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
160
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800161struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300162 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163 struct kvm_rmap_desc *more;
164};
165
Avi Kivity2d111232008-12-25 14:39:47 +0200166struct kvm_shadow_walk_iterator {
167 u64 addr;
168 hpa_t shadow_addr;
169 int level;
170 u64 *sptep;
171 unsigned index;
172};
173
174#define for_each_shadow_entry(_vcpu, _addr, _walker) \
175 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
176 shadow_walk_okay(&(_walker)); \
177 shadow_walk_next(&(_walker)))
178
179
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300180struct kvm_unsync_walk {
181 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
182};
183
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300184typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
185
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186static struct kmem_cache *pte_chain_cache;
187static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300188static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300189
Avi Kivityc7addb92007-09-16 18:58:32 +0200190static u64 __read_mostly shadow_trap_nonpresent_pte;
191static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800192static u64 __read_mostly shadow_base_present_pte;
193static u64 __read_mostly shadow_nx_mask;
194static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
195static u64 __read_mostly shadow_user_mask;
196static u64 __read_mostly shadow_accessed_mask;
197static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200198
Dong, Eddie82725b22009-03-30 16:21:08 +0800199static inline u64 rsvd_bits(int s, int e)
200{
201 return ((1ULL << (e - s + 1)) - 1) << s;
202}
203
Avi Kivityc7addb92007-09-16 18:58:32 +0200204void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
205{
206 shadow_trap_nonpresent_pte = trap_pte;
207 shadow_notrap_nonpresent_pte = notrap_pte;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
210
Sheng Yang7b523452008-04-25 21:13:50 +0800211void kvm_mmu_set_base_ptes(u64 base_pte)
212{
213 shadow_base_present_pte = base_pte;
214}
215EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
216
217void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800218 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800219{
220 shadow_user_mask = user_mask;
221 shadow_accessed_mask = accessed_mask;
222 shadow_dirty_mask = dirty_mask;
223 shadow_nx_mask = nx_mask;
224 shadow_x_mask = x_mask;
225}
226EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_write_protection(struct kvm_vcpu *vcpu)
229{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200230 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231}
232
233static int is_cpuid_PSE36(void)
234{
235 return 1;
236}
237
Avi Kivity73b10872007-01-26 00:56:41 -0800238static int is_nx(struct kvm_vcpu *vcpu)
239{
Avi Kivityf6801df2010-01-21 15:31:50 +0200240 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800241}
242
Avi Kivityc7addb92007-09-16 18:58:32 +0200243static int is_shadow_present_pte(u64 pte)
244{
Avi Kivityc7addb92007-09-16 18:58:32 +0200245 return pte != shadow_trap_nonpresent_pte
246 && pte != shadow_notrap_nonpresent_pte;
247}
248
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300249static int is_large_pte(u64 pte)
250{
251 return pte & PT_PAGE_SIZE_MASK;
252}
253
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900254static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255{
256 return pte & PT_WRITABLE_MASK;
257}
258
Avi Kivity43a37952009-06-10 14:12:05 +0300259static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200260{
Avi Kivity439e2182009-06-10 12:56:54 +0300261 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200262}
263
Avi Kivity43a37952009-06-10 14:12:05 +0300264static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800265{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200266 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800267}
268
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300269static int is_last_spte(u64 pte, int level)
270{
271 if (level == PT_PAGE_TABLE_LEVEL)
272 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200273 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300274 return 1;
275 return 0;
276}
277
Anthony Liguori35149e22008-04-02 14:46:56 -0500278static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200279{
Anthony Liguori35149e22008-04-02 14:46:56 -0500280 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200281}
282
Avi Kivityda928522007-11-21 13:54:47 +0200283static gfn_t pse36_gfn_delta(u32 gpte)
284{
285 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
286
287 return (gpte & PT32_DIR_PSE36_MASK) << shift;
288}
289
Avi Kivityd555c332009-06-10 14:24:23 +0300290static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300291{
292#ifdef CONFIG_X86_64
293 set_64bit((unsigned long *)sptep, spte);
294#else
295 set_64bit((unsigned long long *)sptep, spte);
296#endif
297}
298
Avi Kivitye2dec932007-01-05 16:36:54 -0800299static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300300 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800301{
302 void *obj;
303
304 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800305 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800306 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300307 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800308 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800309 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800310 cache->objects[cache->nobjs++] = obj;
311 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800312 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800313}
314
315static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
316{
317 while (mc->nobjs)
318 kfree(mc->objects[--mc->nobjs]);
319}
320
Avi Kivityc1158e62007-07-20 08:18:27 +0300321static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300322 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300323{
324 struct page *page;
325
326 if (cache->nobjs >= min)
327 return 0;
328 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300329 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300330 if (!page)
331 return -ENOMEM;
332 set_page_private(page, 0);
333 cache->objects[cache->nobjs++] = page_address(page);
334 }
335 return 0;
336}
337
338static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
339{
340 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300341 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300342}
343
Avi Kivity8c438502007-04-16 11:53:17 +0300344static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
345{
346 int r;
347
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800348 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300349 pte_chain_cache, 4);
350 if (r)
351 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200353 rmap_desc_cache, 4);
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_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300357 if (r)
358 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300360 mmu_page_header_cache, 4);
361out:
Avi Kivity8c438502007-04-16 11:53:17 +0300362 return r;
363}
364
Avi Kivity714b93d2007-01-05 16:36:53 -0800365static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
366{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800367 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
368 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
369 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
370 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800371}
372
373static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
374 size_t size)
375{
376 void *p;
377
378 BUG_ON(!mc->nobjs);
379 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800380 return p;
381}
382
Avi Kivity714b93d2007-01-05 16:36:53 -0800383static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
384{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800385 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800386 sizeof(struct kvm_pte_chain));
387}
388
Avi Kivity90cb0522007-07-17 13:04:56 +0300389static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800390{
Avi Kivity90cb0522007-07-17 13:04:56 +0300391 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800392}
393
394static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
395{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800396 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800397 sizeof(struct kvm_rmap_desc));
398}
399
Avi Kivity90cb0522007-07-17 13:04:56 +0300400static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800401{
Avi Kivity90cb0522007-07-17 13:04:56 +0300402 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800403}
404
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300406 * Return the pointer to the largepage write count for a given
407 * gfn, handling slots that are not large page aligned.
408 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200409static int *slot_largepage_idx(gfn_t gfn,
410 struct kvm_memory_slot *slot,
411 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300412{
413 unsigned long idx;
414
Joerg Roedeld25797b2009-07-27 16:30:43 +0200415 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
416 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
417 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418}
419
420static void account_shadowed(struct kvm *kvm, gfn_t gfn)
421{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200422 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200424 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300425
Izik Eidus28430992008-10-03 17:40:32 +0300426 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200427
428 slot = gfn_to_memslot_unaliased(kvm, gfn);
429 for (i = PT_DIRECTORY_LEVEL;
430 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
431 write_count = slot_largepage_idx(gfn, slot, i);
432 *write_count += 1;
433 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434}
435
436static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
437{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200440 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441
Izik Eidus28430992008-10-03 17:40:32 +0300442 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200443 for (i = PT_DIRECTORY_LEVEL;
444 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
445 slot = gfn_to_memslot_unaliased(kvm, gfn);
446 write_count = slot_largepage_idx(gfn, slot, i);
447 *write_count -= 1;
448 WARN_ON(*write_count < 0);
449 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450}
451
Joerg Roedeld25797b2009-07-27 16:30:43 +0200452static int has_wrprotected_page(struct kvm *kvm,
453 gfn_t gfn,
454 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455{
Izik Eidus28430992008-10-03 17:40:32 +0300456 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300457 int *largepage_idx;
458
Izik Eidus28430992008-10-03 17:40:32 +0300459 gfn = unalias_gfn(kvm, gfn);
460 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200462 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300463 return *largepage_idx;
464 }
465
466 return 1;
467}
468
Joerg Roedeld25797b2009-07-27 16:30:43 +0200469static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200471 unsigned long page_size = PAGE_SIZE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472 struct vm_area_struct *vma;
473 unsigned long addr;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200474 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475
476 addr = gfn_to_hva(kvm, gfn);
477 if (kvm_is_error_hva(addr))
Sheng Yang82b70052010-01-05 19:02:28 +0800478 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300480 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481 vma = find_vma(current->mm, addr);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200482 if (!vma)
483 goto out;
484
485 page_size = vma_kernel_pagesize(vma);
486
487out:
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300488 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300489
Joerg Roedeld25797b2009-07-27 16:30:43 +0200490 for (i = PT_PAGE_TABLE_LEVEL;
491 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
492 if (page_size >= KVM_HPAGE_SIZE(i))
493 ret = i;
494 else
495 break;
496 }
497
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300498 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499}
500
Joerg Roedeld25797b2009-07-27 16:30:43 +0200501static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502{
503 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800504 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300505
506 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
507 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200508 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300509
Joerg Roedeld25797b2009-07-27 16:30:43 +0200510 host_level = host_mapping_level(vcpu->kvm, large_gfn);
511
512 if (host_level == PT_PAGE_TABLE_LEVEL)
513 return host_level;
514
Sheng Yang878403b2010-01-05 19:02:29 +0800515 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
516 kvm_x86_ops->get_lpage_level() : host_level;
517
518 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200519 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
520 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200521
522 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300523}
524
525/*
Izik Eidus290fc382007-09-27 14:11:22 +0200526 * Take gfn and return the reverse mapping to it.
527 * Note: gfn must be unaliased before this function get called
528 */
529
Joerg Roedel44ad9942009-07-27 16:30:42 +0200530static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200531{
532 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300533 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200534
535 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200536 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300537 return &slot->rmap[gfn - slot->base_gfn];
538
Joerg Roedel44ad9942009-07-27 16:30:42 +0200539 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
540 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300541
Joerg Roedel44ad9942009-07-27 16:30:42 +0200542 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200543}
544
545/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546 * Reverse mapping data structures:
547 *
Izik Eidus290fc382007-09-27 14:11:22 +0200548 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
549 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550 *
Izik Eidus290fc382007-09-27 14:11:22 +0200551 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
552 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300553 *
554 * Returns the number of rmap entries before the spte was added or zero if
555 * the spte was not added.
556 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200558static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559{
Avi Kivity4db35312007-11-21 15:28:32 +0200560 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200562 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300563 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800564
Avi Kivity43a37952009-06-10 14:12:05 +0300565 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300566 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200567 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200568 sp = page_header(__pa(spte));
569 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200570 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200571 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200573 *rmapp = (unsigned long)spte;
574 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800576 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300577 desc->sptes[0] = (u64 *)*rmapp;
578 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200579 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 } else {
581 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200582 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300583 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800584 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300585 count += RMAP_EXT;
586 }
Avi Kivityd555c332009-06-10 14:24:23 +0300587 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800588 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 desc = desc->more;
590 }
Avi Kivityd555c332009-06-10 14:24:23 +0300591 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800592 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300593 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300595 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596}
597
Izik Eidus290fc382007-09-27 14:11:22 +0200598static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800599 struct kvm_rmap_desc *desc,
600 int i,
601 struct kvm_rmap_desc *prev_desc)
602{
603 int j;
604
Avi Kivityd555c332009-06-10 14:24:23 +0300605 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300607 desc->sptes[i] = desc->sptes[j];
608 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609 if (j != 0)
610 return;
611 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300612 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613 else
614 if (prev_desc)
615 prev_desc->more = desc->more;
616 else
Izik Eidus290fc382007-09-27 14:11:22 +0200617 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300618 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619}
620
Izik Eidus290fc382007-09-27 14:11:22 +0200621static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800622{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623 struct kvm_rmap_desc *desc;
624 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200625 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500626 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200627 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800628 int i;
629
Avi Kivity43a37952009-06-10 14:12:05 +0300630 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800631 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200632 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500633 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800634 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500635 kvm_set_pfn_accessed(pfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900636 if (is_writable_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300637 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200638 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200639 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800640 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
641 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200642 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200644 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800645 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
646 spte, *spte);
647 BUG();
648 }
Izik Eidus290fc382007-09-27 14:11:22 +0200649 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800650 } else {
651 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200652 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800653 prev_desc = NULL;
654 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300655 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
656 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200657 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800658 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800659 prev_desc);
660 return;
661 }
662 prev_desc = desc;
663 desc = desc->more;
664 }
Avi Kivity186a3e52009-12-02 15:17:00 +0200665 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800666 BUG();
667 }
668}
669
Izik Eidus98348e92007-10-16 14:42:30 +0200670static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800671{
Avi Kivity374cbac2007-01-05 16:36:43 -0800672 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200673 struct kvm_rmap_desc *prev_desc;
674 u64 *prev_spte;
675 int i;
676
677 if (!*rmapp)
678 return NULL;
679 else if (!(*rmapp & 1)) {
680 if (!spte)
681 return (u64 *)*rmapp;
682 return NULL;
683 }
684 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
685 prev_desc = NULL;
686 prev_spte = NULL;
687 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300688 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200689 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300690 return desc->sptes[i];
691 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200692 }
693 desc = desc->more;
694 }
695 return NULL;
696}
697
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200698static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200699{
Izik Eidus290fc382007-09-27 14:11:22 +0200700 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800701 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200702 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800703
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500704 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200705 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800706
Izik Eidus98348e92007-10-16 14:42:30 +0200707 spte = rmap_next(kvm, rmapp, NULL);
708 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800709 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800710 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800711 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900712 if (is_writable_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300713 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800714 write_protected = 1;
715 }
Izik Eidus9647c142007-10-16 14:43:46 +0200716 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800717 }
Izik Eidus855149a2008-03-20 18:17:24 +0200718 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500719 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200720
721 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500722 pfn = spte_to_pfn(*spte);
723 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200724 }
725
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300726 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200727 for (i = PT_DIRECTORY_LEVEL;
728 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
729 rmapp = gfn_to_rmap(kvm, gfn, i);
730 spte = rmap_next(kvm, rmapp, NULL);
731 while (spte) {
732 BUG_ON(!spte);
733 BUG_ON(!(*spte & PT_PRESENT_MASK));
734 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
735 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900736 if (is_writable_pte(*spte)) {
Joerg Roedel44ad9942009-07-27 16:30:42 +0200737 rmap_remove(kvm, spte);
738 --kvm->stat.lpages;
739 __set_spte(spte, shadow_trap_nonpresent_pte);
740 spte = NULL;
741 write_protected = 1;
742 }
743 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300744 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300745 }
746
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200747 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800748}
749
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000750static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
751 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200752{
753 u64 *spte;
754 int need_tlb_flush = 0;
755
756 while ((spte = rmap_next(kvm, rmapp, NULL))) {
757 BUG_ON(!(*spte & PT_PRESENT_MASK));
758 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
759 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300760 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200761 need_tlb_flush = 1;
762 }
763 return need_tlb_flush;
764}
765
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000766static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
767 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300768{
769 int need_flush = 0;
770 u64 *spte, new_spte;
771 pte_t *ptep = (pte_t *)data;
772 pfn_t new_pfn;
773
774 WARN_ON(pte_huge(*ptep));
775 new_pfn = pte_pfn(*ptep);
776 spte = rmap_next(kvm, rmapp, NULL);
777 while (spte) {
778 BUG_ON(!is_shadow_present_pte(*spte));
779 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
780 need_flush = 1;
781 if (pte_write(*ptep)) {
782 rmap_remove(kvm, spte);
783 __set_spte(spte, shadow_trap_nonpresent_pte);
784 spte = rmap_next(kvm, rmapp, NULL);
785 } else {
786 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
787 new_spte |= (u64)new_pfn << PAGE_SHIFT;
788
789 new_spte &= ~PT_WRITABLE_MASK;
790 new_spte &= ~SPTE_HOST_WRITEABLE;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900791 if (is_writable_pte(*spte))
Izik Eidus3da0dd42009-09-23 21:47:18 +0300792 kvm_set_pfn_dirty(spte_to_pfn(*spte));
793 __set_spte(spte, new_spte);
794 spte = rmap_next(kvm, rmapp, spte);
795 }
796 }
797 if (need_flush)
798 kvm_flush_remote_tlbs(kvm);
799
800 return 0;
801}
802
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000803static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
804 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300805 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000806 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200807{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200808 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200809 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200810 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200811
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200812 slots = rcu_dereference(kvm->memslots);
813
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200814 for (i = 0; i < slots->nmemslots; i++) {
815 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816 unsigned long start = memslot->userspace_addr;
817 unsigned long end;
818
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200819 end = start + (memslot->npages << PAGE_SHIFT);
820 if (hva >= start && hva < end) {
821 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200822
Izik Eidus3da0dd42009-09-23 21:47:18 +0300823 retval |= handler(kvm, &memslot->rmap[gfn_offset],
824 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200825
826 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
827 int idx = gfn_offset;
828 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
829 retval |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300830 &memslot->lpage_info[j][idx].rmap_pde,
831 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200832 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200833 }
834 }
835
836 return retval;
837}
838
839int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
840{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300841 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200842}
843
Izik Eidus3da0dd42009-09-23 21:47:18 +0300844void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
845{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000846 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300847}
848
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000849static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
850 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200851{
852 u64 *spte;
853 int young = 0;
854
Sheng Yang534e38b2008-09-08 15:12:30 +0800855 /* always return old for EPT */
856 if (!shadow_accessed_mask)
857 return 0;
858
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200859 spte = rmap_next(kvm, rmapp, NULL);
860 while (spte) {
861 int _young;
862 u64 _spte = *spte;
863 BUG_ON(!(_spte & PT_PRESENT_MASK));
864 _young = _spte & PT_ACCESSED_MASK;
865 if (_young) {
866 young = 1;
867 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
868 }
869 spte = rmap_next(kvm, rmapp, spte);
870 }
871 return young;
872}
873
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300874#define RMAP_RECYCLE_THRESHOLD 1000
875
Joerg Roedel852e3c12009-07-27 16:30:44 +0200876static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300877{
878 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200879 struct kvm_mmu_page *sp;
880
881 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300882
883 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200884 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300885
Izik Eidus3da0dd42009-09-23 21:47:18 +0300886 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300887 kvm_flush_remote_tlbs(vcpu->kvm);
888}
889
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200890int kvm_age_hva(struct kvm *kvm, unsigned long hva)
891{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300892 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200893}
894
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800895#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300896static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897{
Avi Kivity139bdb22007-01-05 16:36:50 -0800898 u64 *pos;
899 u64 *end;
900
Avi Kivity47ad8e62007-05-06 15:50:58 +0300901 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300902 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800903 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800904 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800906 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 return 1;
908}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800909#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910
Avi Kivity4db35312007-11-21 15:28:32 +0200911static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800912{
Avi Kivity4db35312007-11-21 15:28:32 +0200913 ASSERT(is_empty_shadow_page(sp->spt));
914 list_del(&sp->link);
915 __free_page(virt_to_page(sp->spt));
916 __free_page(virt_to_page(sp->gfns));
917 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800918 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800919}
920
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800921static unsigned kvm_page_table_hashfn(gfn_t gfn)
922{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200923 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924}
925
Avi Kivity25c0de22007-01-05 16:36:42 -0800926static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
927 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928{
Avi Kivity4db35312007-11-21 15:28:32 +0200929 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800931 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
932 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
933 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200934 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800935 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200936 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800937 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200938 sp->multimapped = 0;
939 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800940 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200941 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942}
943
Avi Kivity714b93d2007-01-05 16:36:53 -0800944static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200945 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946{
947 struct kvm_pte_chain *pte_chain;
948 struct hlist_node *node;
949 int i;
950
951 if (!parent_pte)
952 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200953 if (!sp->multimapped) {
954 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800955
956 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200957 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800958 return;
959 }
Avi Kivity4db35312007-11-21 15:28:32 +0200960 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800961 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200962 INIT_HLIST_HEAD(&sp->parent_ptes);
963 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 pte_chain->parent_ptes[0] = old;
965 }
Avi Kivity4db35312007-11-21 15:28:32 +0200966 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800967 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
968 continue;
969 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
970 if (!pte_chain->parent_ptes[i]) {
971 pte_chain->parent_ptes[i] = parent_pte;
972 return;
973 }
974 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800975 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800976 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200977 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800978 pte_chain->parent_ptes[0] = parent_pte;
979}
980
Avi Kivity4db35312007-11-21 15:28:32 +0200981static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800982 u64 *parent_pte)
983{
984 struct kvm_pte_chain *pte_chain;
985 struct hlist_node *node;
986 int i;
987
Avi Kivity4db35312007-11-21 15:28:32 +0200988 if (!sp->multimapped) {
989 BUG_ON(sp->parent_pte != parent_pte);
990 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800991 return;
992 }
Avi Kivity4db35312007-11-21 15:28:32 +0200993 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800994 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
995 if (!pte_chain->parent_ptes[i])
996 break;
997 if (pte_chain->parent_ptes[i] != parent_pte)
998 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800999 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1000 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001001 pte_chain->parent_ptes[i]
1002 = pte_chain->parent_ptes[i + 1];
1003 ++i;
1004 }
1005 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001006 if (i == 0) {
1007 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001008 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001009 if (hlist_empty(&sp->parent_ptes)) {
1010 sp->multimapped = 0;
1011 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001012 }
1013 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001014 return;
1015 }
1016 BUG();
1017}
1018
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001019
1020static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1021 mmu_parent_walk_fn fn)
1022{
1023 struct kvm_pte_chain *pte_chain;
1024 struct hlist_node *node;
1025 struct kvm_mmu_page *parent_sp;
1026 int i;
1027
1028 if (!sp->multimapped && sp->parent_pte) {
1029 parent_sp = page_header(__pa(sp->parent_pte));
1030 fn(vcpu, parent_sp);
1031 mmu_parent_walk(vcpu, parent_sp, fn);
1032 return;
1033 }
1034 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1035 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1036 if (!pte_chain->parent_ptes[i])
1037 break;
1038 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1039 fn(vcpu, parent_sp);
1040 mmu_parent_walk(vcpu, parent_sp, fn);
1041 }
1042}
1043
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001044static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1045{
1046 unsigned int index;
1047 struct kvm_mmu_page *sp = page_header(__pa(spte));
1048
1049 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001050 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1051 sp->unsync_children++;
1052 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001053}
1054
1055static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1056{
1057 struct kvm_pte_chain *pte_chain;
1058 struct hlist_node *node;
1059 int i;
1060
1061 if (!sp->parent_pte)
1062 return;
1063
1064 if (!sp->multimapped) {
1065 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1066 return;
1067 }
1068
1069 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1070 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1071 if (!pte_chain->parent_ptes[i])
1072 break;
1073 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1074 }
1075}
1076
1077static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1078{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001079 kvm_mmu_update_parents_unsync(sp);
1080 return 1;
1081}
1082
1083static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1084 struct kvm_mmu_page *sp)
1085{
1086 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1087 kvm_mmu_update_parents_unsync(sp);
1088}
1089
Avi Kivityd761a502008-05-29 14:55:03 +03001090static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1091 struct kvm_mmu_page *sp)
1092{
1093 int i;
1094
1095 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1096 sp->spt[i] = shadow_trap_nonpresent_pte;
1097}
1098
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001099static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1100 struct kvm_mmu_page *sp)
1101{
1102 return 1;
1103}
1104
Marcelo Tosattia7052892008-09-23 13:18:35 -03001105static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1106{
1107}
1108
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001109#define KVM_PAGE_ARRAY_NR 16
1110
1111struct kvm_mmu_pages {
1112 struct mmu_page_and_offset {
1113 struct kvm_mmu_page *sp;
1114 unsigned int idx;
1115 } page[KVM_PAGE_ARRAY_NR];
1116 unsigned int nr;
1117};
1118
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001119#define for_each_unsync_children(bitmap, idx) \
1120 for (idx = find_first_bit(bitmap, 512); \
1121 idx < 512; \
1122 idx = find_next_bit(bitmap, 512, idx+1))
1123
Hannes Edercded19f2009-02-21 02:19:13 +01001124static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1125 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001126{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001127 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001129 if (sp->unsync)
1130 for (i=0; i < pvec->nr; i++)
1131 if (pvec->page[i].sp == sp)
1132 return 0;
1133
1134 pvec->page[pvec->nr].sp = sp;
1135 pvec->page[pvec->nr].idx = idx;
1136 pvec->nr++;
1137 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1138}
1139
1140static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1141 struct kvm_mmu_pages *pvec)
1142{
1143 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001144
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001145 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001146 u64 ent = sp->spt[i];
1147
Marcelo Tosatti87917232008-12-22 18:49:30 -02001148 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001149 struct kvm_mmu_page *child;
1150 child = page_header(ent & PT64_BASE_ADDR_MASK);
1151
1152 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001153 if (mmu_pages_add(pvec, child, i))
1154 return -ENOSPC;
1155
1156 ret = __mmu_unsync_walk(child, pvec);
1157 if (!ret)
1158 __clear_bit(i, sp->unsync_child_bitmap);
1159 else if (ret > 0)
1160 nr_unsync_leaf += ret;
1161 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001162 return ret;
1163 }
1164
1165 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001166 nr_unsync_leaf++;
1167 if (mmu_pages_add(pvec, child, i))
1168 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001169 }
1170 }
1171 }
1172
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001173 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001174 sp->unsync_children = 0;
1175
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001176 return nr_unsync_leaf;
1177}
1178
1179static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1180 struct kvm_mmu_pages *pvec)
1181{
1182 if (!sp->unsync_children)
1183 return 0;
1184
1185 mmu_pages_add(pvec, sp, 0);
1186 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001187}
1188
Avi Kivity4db35312007-11-21 15:28:32 +02001189static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001190{
1191 unsigned index;
1192 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001193 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001194 struct hlist_node *node;
1195
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001196 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001197 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001198 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001199 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001200 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001201 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001202 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001203 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001204 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001205 }
1206 return NULL;
1207}
1208
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1210{
1211 WARN_ON(!sp->unsync);
1212 sp->unsync = 0;
1213 --kvm->stat.mmu_unsync;
1214}
1215
1216static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1217
1218static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1219{
1220 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1221 kvm_mmu_zap_page(vcpu->kvm, sp);
1222 return 1;
1223 }
1224
Avi Kivityf691fe12009-07-06 15:58:14 +03001225 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001226 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1227 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001228 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001229 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1230 kvm_mmu_zap_page(vcpu->kvm, sp);
1231 return 1;
1232 }
1233
1234 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001235 return 0;
1236}
1237
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001238struct mmu_page_path {
1239 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1240 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001241};
1242
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001243#define for_each_sp(pvec, sp, parents, i) \
1244 for (i = mmu_pages_next(&pvec, &parents, -1), \
1245 sp = pvec.page[i].sp; \
1246 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1247 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248
Hannes Edercded19f2009-02-21 02:19:13 +01001249static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1250 struct mmu_page_path *parents,
1251 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001252{
1253 int n;
1254
1255 for (n = i+1; n < pvec->nr; n++) {
1256 struct kvm_mmu_page *sp = pvec->page[n].sp;
1257
1258 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1259 parents->idx[0] = pvec->page[n].idx;
1260 return n;
1261 }
1262
1263 parents->parent[sp->role.level-2] = sp;
1264 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1265 }
1266
1267 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001268}
1269
Hannes Edercded19f2009-02-21 02:19:13 +01001270static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001271{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001272 struct kvm_mmu_page *sp;
1273 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001274
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001275 do {
1276 unsigned int idx = parents->idx[level];
1277
1278 sp = parents->parent[level];
1279 if (!sp)
1280 return;
1281
1282 --sp->unsync_children;
1283 WARN_ON((int)sp->unsync_children < 0);
1284 __clear_bit(idx, sp->unsync_child_bitmap);
1285 level++;
1286 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1287}
1288
1289static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1290 struct mmu_page_path *parents,
1291 struct kvm_mmu_pages *pvec)
1292{
1293 parents->parent[parent->role.level-1] = NULL;
1294 pvec->nr = 0;
1295}
1296
1297static void mmu_sync_children(struct kvm_vcpu *vcpu,
1298 struct kvm_mmu_page *parent)
1299{
1300 int i;
1301 struct kvm_mmu_page *sp;
1302 struct mmu_page_path parents;
1303 struct kvm_mmu_pages pages;
1304
1305 kvm_mmu_pages_init(parent, &parents, &pages);
1306 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001307 int protected = 0;
1308
1309 for_each_sp(pages, sp, parents, i)
1310 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1311
1312 if (protected)
1313 kvm_flush_remote_tlbs(vcpu->kvm);
1314
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001315 for_each_sp(pages, sp, parents, i) {
1316 kvm_sync_page(vcpu, sp);
1317 mmu_pages_clear_parents(&parents);
1318 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001319 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001320 kvm_mmu_pages_init(parent, &parents, &pages);
1321 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001322}
1323
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001324static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1325 gfn_t gfn,
1326 gva_t gaddr,
1327 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001328 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001329 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001330 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001331{
1332 union kvm_mmu_page_role role;
1333 unsigned index;
1334 unsigned quadrant;
1335 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001336 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001337 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001338
Avi Kivitya770f6f2008-12-21 19:20:09 +02001339 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001340 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001341 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001342 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001343 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001344 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1345 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1346 role.quadrant = quadrant;
1347 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001348 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001349 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001350 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1351 if (sp->gfn == gfn) {
1352 if (sp->unsync)
1353 if (kvm_sync_page(vcpu, sp))
1354 continue;
1355
1356 if (sp->role.word != role.word)
1357 continue;
1358
Avi Kivity4db35312007-11-21 15:28:32 +02001359 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001360 if (sp->unsync_children) {
1361 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1362 kvm_mmu_mark_parents_unsync(vcpu, sp);
1363 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001364 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001365 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001366 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001367 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001368 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1369 if (!sp)
1370 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001371 sp->gfn = gfn;
1372 sp->role = role;
1373 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001374 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001375 if (rmap_write_protect(vcpu->kvm, gfn))
1376 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001377 account_shadowed(vcpu->kvm, gfn);
1378 }
Avi Kivity131d8272008-05-29 14:56:28 +03001379 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1380 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1381 else
1382 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001383 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001384 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001385}
1386
Avi Kivity2d111232008-12-25 14:39:47 +02001387static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1388 struct kvm_vcpu *vcpu, u64 addr)
1389{
1390 iterator->addr = addr;
1391 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1392 iterator->level = vcpu->arch.mmu.shadow_root_level;
1393 if (iterator->level == PT32E_ROOT_LEVEL) {
1394 iterator->shadow_addr
1395 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1396 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1397 --iterator->level;
1398 if (!iterator->shadow_addr)
1399 iterator->level = 0;
1400 }
1401}
1402
1403static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1404{
1405 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1406 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001407
1408 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1409 if (is_large_pte(*iterator->sptep))
1410 return false;
1411
Avi Kivity2d111232008-12-25 14:39:47 +02001412 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1413 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1414 return true;
1415}
1416
1417static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1418{
1419 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1420 --iterator->level;
1421}
1422
Avi Kivity90cb0522007-07-17 13:04:56 +03001423static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001424 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001425{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001426 unsigned i;
1427 u64 *pt;
1428 u64 ent;
1429
Avi Kivity4db35312007-11-21 15:28:32 +02001430 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001431
Avi Kivity697fe2e2007-01-05 16:36:46 -08001432 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1433 ent = pt[i];
1434
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001435 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001436 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001437 ent &= PT64_BASE_ADDR_MASK;
1438 mmu_page_remove_parent_pte(page_header(ent),
1439 &pt[i]);
1440 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001441 if (is_large_pte(ent))
1442 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001443 rmap_remove(kvm, &pt[i]);
1444 }
1445 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001446 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001447 }
Avi Kivitya4360362007-01-05 16:36:45 -08001448}
1449
Avi Kivity4db35312007-11-21 15:28:32 +02001450static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001451{
Avi Kivity4db35312007-11-21 15:28:32 +02001452 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001453}
1454
Avi Kivity12b7d282007-09-23 14:10:49 +02001455static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1456{
1457 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001458 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001459
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001460 kvm_for_each_vcpu(i, vcpu, kvm)
1461 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001462}
1463
Avi Kivity31aa2b42008-07-11 17:59:46 +03001464static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001465{
1466 u64 *parent_pte;
1467
Avi Kivity4db35312007-11-21 15:28:32 +02001468 while (sp->multimapped || sp->parent_pte) {
1469 if (!sp->multimapped)
1470 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001471 else {
1472 struct kvm_pte_chain *chain;
1473
Avi Kivity4db35312007-11-21 15:28:32 +02001474 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001475 struct kvm_pte_chain, link);
1476 parent_pte = chain->parent_ptes[0];
1477 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001478 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001479 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001480 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001481 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001482}
1483
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001484static int mmu_zap_unsync_children(struct kvm *kvm,
1485 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001486{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001487 int i, zapped = 0;
1488 struct mmu_page_path parents;
1489 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001490
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001491 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001492 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001493
1494 kvm_mmu_pages_init(parent, &parents, &pages);
1495 while (mmu_unsync_walk(parent, &pages)) {
1496 struct kvm_mmu_page *sp;
1497
1498 for_each_sp(pages, sp, parents, i) {
1499 kvm_mmu_zap_page(kvm, sp);
1500 mmu_pages_clear_parents(&parents);
1501 }
1502 zapped += pages.nr;
1503 kvm_mmu_pages_init(parent, &parents, &pages);
1504 }
1505
1506 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001507}
1508
Marcelo Tosatti07385412008-09-23 13:18:37 -03001509static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001510{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001511 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001512
1513 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001514 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001515 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001516 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001517 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001518 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001519 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001520 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001521 if (sp->unsync)
1522 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001523 if (!sp->root_count) {
1524 hlist_del(&sp->hash_link);
1525 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001526 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001527 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001528 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001529 kvm_reload_remote_mmus(kvm);
1530 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001531 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001532 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001533}
1534
Izik Eidus82ce2c92007-10-02 18:52:55 +02001535/*
1536 * Changing the number of mmu pages allocated to the vm
1537 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1538 */
1539void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1540{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001541 int used_pages;
1542
1543 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1544 used_pages = max(0, used_pages);
1545
Izik Eidus82ce2c92007-10-02 18:52:55 +02001546 /*
1547 * If we set the number of mmu pages to be smaller be than the
1548 * number of actived pages , we must to free some mmu pages before we
1549 * change the value
1550 */
1551
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001552 if (used_pages > kvm_nr_mmu_pages) {
1553 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001554 struct kvm_mmu_page *page;
1555
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001556 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001557 struct kvm_mmu_page, link);
1558 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001559 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001560 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001561 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001562 }
1563 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001564 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1565 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001566
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001567 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001568}
1569
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001570static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001571{
1572 unsigned index;
1573 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001574 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001575 struct hlist_node *node, *n;
1576 int r;
1577
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001578 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001579 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001580 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001581 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001582 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001583 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001584 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001585 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001586 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001587 if (kvm_mmu_zap_page(kvm, sp))
1588 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001589 }
1590 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001591}
1592
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001593static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001594{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001595 unsigned index;
1596 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001597 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001598 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001599
Avi Kivity4677a3b2009-01-06 13:00:27 +02001600 index = kvm_page_table_hashfn(gfn);
1601 bucket = &kvm->arch.mmu_page_hash[index];
1602 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001603 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001604 && !sp->role.invalid) {
1605 pgprintk("%s: zap %lx %x\n",
1606 __func__, gfn, sp->role.word);
1607 kvm_mmu_zap_page(kvm, sp);
1608 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001609 }
1610}
1611
Avi Kivity38c335f2007-11-21 14:20:22 +02001612static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001614 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001615 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616
Sheng Yang291f26b2008-10-16 17:30:57 +08001617 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618}
1619
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001620static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1621{
1622 int i;
1623 u64 *pt = sp->spt;
1624
1625 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1626 return;
1627
1628 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1629 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001630 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001631 }
1632}
1633
Avi Kivity039576c2007-03-20 12:46:50 +02001634struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1635{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001636 struct page *page;
1637
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001638 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001639
1640 if (gpa == UNMAPPED_GVA)
1641 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001642
Izik Eidus72dc67a2008-02-10 18:04:15 +02001643 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001644
1645 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001646}
1647
Sheng Yang74be52e2008-10-09 16:01:56 +08001648/*
1649 * The function is based on mtrr_type_lookup() in
1650 * arch/x86/kernel/cpu/mtrr/generic.c
1651 */
1652static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1653 u64 start, u64 end)
1654{
1655 int i;
1656 u64 base, mask;
1657 u8 prev_match, curr_match;
1658 int num_var_ranges = KVM_NR_VAR_MTRR;
1659
1660 if (!mtrr_state->enabled)
1661 return 0xFF;
1662
1663 /* Make end inclusive end, instead of exclusive */
1664 end--;
1665
1666 /* Look in fixed ranges. Just return the type as per start */
1667 if (mtrr_state->have_fixed && (start < 0x100000)) {
1668 int idx;
1669
1670 if (start < 0x80000) {
1671 idx = 0;
1672 idx += (start >> 16);
1673 return mtrr_state->fixed_ranges[idx];
1674 } else if (start < 0xC0000) {
1675 idx = 1 * 8;
1676 idx += ((start - 0x80000) >> 14);
1677 return mtrr_state->fixed_ranges[idx];
1678 } else if (start < 0x1000000) {
1679 idx = 3 * 8;
1680 idx += ((start - 0xC0000) >> 12);
1681 return mtrr_state->fixed_ranges[idx];
1682 }
1683 }
1684
1685 /*
1686 * Look in variable ranges
1687 * Look of multiple ranges matching this address and pick type
1688 * as per MTRR precedence
1689 */
1690 if (!(mtrr_state->enabled & 2))
1691 return mtrr_state->def_type;
1692
1693 prev_match = 0xFF;
1694 for (i = 0; i < num_var_ranges; ++i) {
1695 unsigned short start_state, end_state;
1696
1697 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1698 continue;
1699
1700 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1701 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1702 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1703 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1704
1705 start_state = ((start & mask) == (base & mask));
1706 end_state = ((end & mask) == (base & mask));
1707 if (start_state != end_state)
1708 return 0xFE;
1709
1710 if ((start & mask) != (base & mask))
1711 continue;
1712
1713 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1714 if (prev_match == 0xFF) {
1715 prev_match = curr_match;
1716 continue;
1717 }
1718
1719 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1720 curr_match == MTRR_TYPE_UNCACHABLE)
1721 return MTRR_TYPE_UNCACHABLE;
1722
1723 if ((prev_match == MTRR_TYPE_WRBACK &&
1724 curr_match == MTRR_TYPE_WRTHROUGH) ||
1725 (prev_match == MTRR_TYPE_WRTHROUGH &&
1726 curr_match == MTRR_TYPE_WRBACK)) {
1727 prev_match = MTRR_TYPE_WRTHROUGH;
1728 curr_match = MTRR_TYPE_WRTHROUGH;
1729 }
1730
1731 if (prev_match != curr_match)
1732 return MTRR_TYPE_UNCACHABLE;
1733 }
1734
1735 if (prev_match != 0xFF)
1736 return prev_match;
1737
1738 return mtrr_state->def_type;
1739}
1740
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001741u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001742{
1743 u8 mtrr;
1744
1745 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1746 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1747 if (mtrr == 0xfe || mtrr == 0xff)
1748 mtrr = MTRR_TYPE_WRBACK;
1749 return mtrr;
1750}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001751EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001752
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001753static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1754{
1755 unsigned index;
1756 struct hlist_head *bucket;
1757 struct kvm_mmu_page *s;
1758 struct hlist_node *node, *n;
1759
Avi Kivityf691fe12009-07-06 15:58:14 +03001760 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001761 index = kvm_page_table_hashfn(sp->gfn);
1762 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1763 /* don't unsync if pagetable is shadowed with multiple roles */
1764 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001765 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001766 continue;
1767 if (s->role.word != sp->role.word)
1768 return 1;
1769 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001770 ++vcpu->kvm->stat.mmu_unsync;
1771 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001772
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001773 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001774
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001775 mmu_convert_notrap(sp);
1776 return 0;
1777}
1778
1779static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1780 bool can_unsync)
1781{
1782 struct kvm_mmu_page *shadow;
1783
1784 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1785 if (shadow) {
1786 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1787 return 1;
1788 if (shadow->unsync)
1789 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001790 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001791 return kvm_unsync_page(vcpu, shadow);
1792 return 1;
1793 }
1794 return 0;
1795}
1796
Avi Kivityd555c332009-06-10 14:24:23 +03001797static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001798 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001799 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001800 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001801 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001802{
1803 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001804 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001805
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001806 /*
1807 * We don't set the accessed bit, since we sometimes want to see
1808 * whether the guest actually used the pte (in order to detect
1809 * demand paging).
1810 */
Sheng Yang7b523452008-04-25 21:13:50 +08001811 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001812 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001813 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001814 if (!dirty)
1815 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001816 if (pte_access & ACC_EXEC_MASK)
1817 spte |= shadow_x_mask;
1818 else
1819 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001820 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001821 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001822 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001823 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001824 if (tdp_enabled)
1825 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1826 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001827
Izik Eidus14032832009-09-23 21:47:17 +03001828 if (reset_host_protection)
1829 spte |= SPTE_HOST_WRITEABLE;
1830
Anthony Liguori35149e22008-04-02 14:46:56 -05001831 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001832
1833 if ((pte_access & ACC_WRITE_MASK)
1834 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001835
Joerg Roedel852e3c12009-07-27 16:30:44 +02001836 if (level > PT_PAGE_TABLE_LEVEL &&
1837 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001838 ret = 1;
1839 spte = shadow_trap_nonpresent_pte;
1840 goto set_pte;
1841 }
1842
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001843 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001844
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001845 /*
1846 * Optimization: for pte sync, if spte was writable the hash
1847 * lookup is unnecessary (and expensive). Write protection
1848 * is responsibility of mmu_get_page / kvm_sync_page.
1849 * Same reasoning can be applied to dirty page accounting.
1850 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001851 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001852 goto set_pte;
1853
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001854 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001855 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001856 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001857 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001858 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001859 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001860 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001861 }
1862 }
1863
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001864 if (pte_access & ACC_WRITE_MASK)
1865 mark_page_dirty(vcpu->kvm, gfn);
1866
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001867set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001868 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001869 return ret;
1870}
1871
Avi Kivityd555c332009-06-10 14:24:23 +03001872static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001873 unsigned pt_access, unsigned pte_access,
1874 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001875 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001876 pfn_t pfn, bool speculative,
1877 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001878{
1879 int was_rmapped = 0;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001880 int was_writable = is_writable_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001881 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001882
1883 pgprintk("%s: spte %llx access %x write_fault %d"
1884 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001885 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001886 write_fault, user_fault, gfn);
1887
Avi Kivityd555c332009-06-10 14:24:23 +03001888 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001889 /*
1890 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1891 * the parent of the now unreachable PTE.
1892 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001893 if (level > PT_PAGE_TABLE_LEVEL &&
1894 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001895 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001896 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001897
1898 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001899 mmu_page_remove_parent_pte(child, sptep);
1900 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001901 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001902 spte_to_pfn(*sptep), pfn);
1903 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001904 } else
1905 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001906 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001907
Avi Kivityd555c332009-06-10 14:24:23 +03001908 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001909 dirty, level, gfn, pfn, speculative, true,
1910 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001911 if (write_fault)
1912 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001913 kvm_x86_ops->tlb_flush(vcpu);
1914 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001915
Avi Kivityd555c332009-06-10 14:24:23 +03001916 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001917 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001918 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001919 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1920 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001921 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001922 ++vcpu->kvm->stat.lpages;
1923
Avi Kivityd555c332009-06-10 14:24:23 +03001924 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001925 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001926 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001927 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001928 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001929 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001930 } else {
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001931 if (was_writable)
Anthony Liguori35149e22008-04-02 14:46:56 -05001932 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001933 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001934 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001935 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001936 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001937 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001938 vcpu->arch.last_pte_gfn = gfn;
1939 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001940}
1941
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1943{
1944}
1945
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001946static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001947 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948{
Avi Kivity9f652d22008-12-25 14:54:25 +02001949 struct kvm_shadow_walk_iterator iterator;
1950 struct kvm_mmu_page *sp;
1951 int pt_write = 0;
1952 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953
Avi Kivity9f652d22008-12-25 14:54:25 +02001954 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001955 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001956 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1957 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001958 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001959 ++vcpu->stat.pf_fixed;
1960 break;
1961 }
1962
1963 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1964 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1965 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1966 iterator.level - 1,
1967 1, ACC_ALL, iterator.sptep);
1968 if (!sp) {
1969 pgprintk("nonpaging_map: ENOMEM\n");
1970 kvm_release_pfn_clean(pfn);
1971 return -ENOMEM;
1972 }
1973
Avi Kivityd555c332009-06-10 14:24:23 +03001974 __set_spte(iterator.sptep,
1975 __pa(sp->spt)
1976 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1977 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001978 }
1979 }
1980 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981}
1982
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001983static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1984{
1985 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001986 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001987 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001988 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001989
Joerg Roedel852e3c12009-07-27 16:30:44 +02001990 level = mapping_level(vcpu, gfn);
1991
1992 /*
1993 * This path builds a PAE pagetable - so we can map 2mb pages at
1994 * maximum. Therefore check if the level is larger than that.
1995 */
1996 if (level > PT_DIRECTORY_LEVEL)
1997 level = PT_DIRECTORY_LEVEL;
1998
1999 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002000
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002001 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002002 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002003 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002004
Avi Kivityd196e342008-01-24 11:44:11 +02002005 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05002006 if (is_error_pfn(pfn)) {
2007 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002008 return 1;
2009 }
2010
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002011 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002012 if (mmu_notifier_retry(vcpu, mmu_seq))
2013 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002014 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002015 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002016 spin_unlock(&vcpu->kvm->mmu_lock);
2017
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002018
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002019 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002020
2021out_unlock:
2022 spin_unlock(&vcpu->kvm->mmu_lock);
2023 kvm_release_pfn_clean(pfn);
2024 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002025}
2026
2027
Avi Kivity17ac10a2007-01-05 16:36:40 -08002028static void mmu_free_roots(struct kvm_vcpu *vcpu)
2029{
2030 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002031 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002032
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002033 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002034 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002035 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002036 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2037 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038
Avi Kivity4db35312007-11-21 15:28:32 +02002039 sp = page_header(root);
2040 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002041 if (!sp->root_count && sp->role.invalid)
2042 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002043 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002044 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002045 return;
2046 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002047 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002048 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002049
Avi Kivity417726a2007-04-12 17:35:58 +03002050 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002051 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002052 sp = page_header(root);
2053 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002054 if (!sp->root_count && sp->role.invalid)
2055 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002056 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002057 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002058 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002059 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002060 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002061}
2062
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002063static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2064{
2065 int ret = 0;
2066
2067 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2068 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2069 ret = 1;
2070 }
2071
2072 return ret;
2073}
2074
2075static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002076{
2077 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002078 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002079 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002080 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002081 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002082
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002083 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002084
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002085 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2086 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002087
2088 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002089 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002090 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002091 if (mmu_check_root(vcpu, root_gfn))
2092 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002093 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002094 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002095 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002096 root = __pa(sp->spt);
2097 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002098 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002099 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002100 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002101 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002102 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002103 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002104 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002105 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002106
2107 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002108 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002109 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002110 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002111 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002112 continue;
2113 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002114 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002115 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002116 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002117 if (mmu_check_root(vcpu, root_gfn))
2118 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002119 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002120 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002121 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002122 root = __pa(sp->spt);
2123 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002124 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002125 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002126 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002127 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002128}
2129
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002130static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2131{
2132 int i;
2133 struct kvm_mmu_page *sp;
2134
2135 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2136 return;
2137 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2138 hpa_t root = vcpu->arch.mmu.root_hpa;
2139 sp = page_header(root);
2140 mmu_sync_children(vcpu, sp);
2141 return;
2142 }
2143 for (i = 0; i < 4; ++i) {
2144 hpa_t root = vcpu->arch.mmu.pae_root[i];
2145
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002146 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002147 root &= PT64_BASE_ADDR_MASK;
2148 sp = page_header(root);
2149 mmu_sync_children(vcpu, sp);
2150 }
2151 }
2152}
2153
2154void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2155{
2156 spin_lock(&vcpu->kvm->mmu_lock);
2157 mmu_sync_roots(vcpu);
2158 spin_unlock(&vcpu->kvm->mmu_lock);
2159}
2160
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2162{
2163 return vaddr;
2164}
2165
2166static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002167 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168{
Avi Kivitye8332402007-12-09 18:43:00 +02002169 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002170 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002172 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002173 r = mmu_topup_memory_caches(vcpu);
2174 if (r)
2175 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002176
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002178 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179
Avi Kivitye8332402007-12-09 18:43:00 +02002180 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181
Avi Kivitye8332402007-12-09 18:43:00 +02002182 return nonpaging_map(vcpu, gva & PAGE_MASK,
2183 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184}
2185
Joerg Roedelfb72d162008-02-07 13:47:44 +01002186static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2187 u32 error_code)
2188{
Anthony Liguori35149e22008-04-02 14:46:56 -05002189 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002190 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002191 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002192 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002193 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002194
2195 ASSERT(vcpu);
2196 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2197
2198 r = mmu_topup_memory_caches(vcpu);
2199 if (r)
2200 return r;
2201
Joerg Roedel852e3c12009-07-27 16:30:44 +02002202 level = mapping_level(vcpu, gfn);
2203
2204 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2205
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002206 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002207 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002208 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002209 if (is_error_pfn(pfn)) {
2210 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002211 return 1;
2212 }
2213 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002214 if (mmu_notifier_retry(vcpu, mmu_seq))
2215 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002216 kvm_mmu_free_some_pages(vcpu);
2217 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002218 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002219 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002220
2221 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002222
2223out_unlock:
2224 spin_unlock(&vcpu->kvm->mmu_lock);
2225 kvm_release_pfn_clean(pfn);
2226 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002227}
2228
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229static void nonpaging_free(struct kvm_vcpu *vcpu)
2230{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002231 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232}
2233
2234static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2235{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002236 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237
2238 context->new_cr3 = nonpaging_new_cr3;
2239 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 context->gva_to_gpa = nonpaging_gva_to_gpa;
2241 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002242 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002243 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002244 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002245 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002247 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248 return 0;
2249}
2250
Avi Kivityd835dfe2007-11-21 02:57:59 +02002251void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002253 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002254 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255}
2256
2257static void paging_new_cr3(struct kvm_vcpu *vcpu)
2258{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002259 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002260 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261}
2262
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263static void inject_page_fault(struct kvm_vcpu *vcpu,
2264 u64 addr,
2265 u32 err_code)
2266{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002267 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268}
2269
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270static void paging_free(struct kvm_vcpu *vcpu)
2271{
2272 nonpaging_free(vcpu);
2273}
2274
Dong, Eddie82725b22009-03-30 16:21:08 +08002275static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2276{
2277 int bit7;
2278
2279 bit7 = (gpte >> 7) & 1;
2280 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2281}
2282
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283#define PTTYPE 64
2284#include "paging_tmpl.h"
2285#undef PTTYPE
2286
2287#define PTTYPE 32
2288#include "paging_tmpl.h"
2289#undef PTTYPE
2290
Dong, Eddie82725b22009-03-30 16:21:08 +08002291static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2292{
2293 struct kvm_mmu *context = &vcpu->arch.mmu;
2294 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2295 u64 exb_bit_rsvd = 0;
2296
2297 if (!is_nx(vcpu))
2298 exb_bit_rsvd = rsvd_bits(63, 63);
2299 switch (level) {
2300 case PT32_ROOT_LEVEL:
2301 /* no rsvd bits for 2 level 4K page table entries */
2302 context->rsvd_bits_mask[0][1] = 0;
2303 context->rsvd_bits_mask[0][0] = 0;
2304 if (is_cpuid_PSE36())
2305 /* 36bits PSE 4MB page */
2306 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2307 else
2308 /* 32 bits PSE 4MB page */
2309 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002310 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002311 break;
2312 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002313 context->rsvd_bits_mask[0][2] =
2314 rsvd_bits(maxphyaddr, 63) |
2315 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002316 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002317 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002318 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2319 rsvd_bits(maxphyaddr, 62); /* PTE */
2320 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2321 rsvd_bits(maxphyaddr, 62) |
2322 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002323 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002324 break;
2325 case PT64_ROOT_LEVEL:
2326 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2327 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2328 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2329 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2330 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002331 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002332 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2333 rsvd_bits(maxphyaddr, 51);
2334 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002335 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2336 rsvd_bits(maxphyaddr, 51) |
2337 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002338 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002339 rsvd_bits(maxphyaddr, 51) |
2340 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002341 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002342 break;
2343 }
2344}
2345
Avi Kivity17ac10a2007-01-05 16:36:40 -08002346static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002348 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349
2350 ASSERT(is_pae(vcpu));
2351 context->new_cr3 = paging_new_cr3;
2352 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002354 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002355 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002356 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002358 context->root_level = level;
2359 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002360 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 return 0;
2362}
2363
Avi Kivity17ac10a2007-01-05 16:36:40 -08002364static int paging64_init_context(struct kvm_vcpu *vcpu)
2365{
Dong, Eddie82725b22009-03-30 16:21:08 +08002366 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002367 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2368}
2369
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370static int paging32_init_context(struct kvm_vcpu *vcpu)
2371{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002372 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Dong, Eddie82725b22009-03-30 16:21:08 +08002374 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375 context->new_cr3 = paging_new_cr3;
2376 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 context->gva_to_gpa = paging32_gva_to_gpa;
2378 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002379 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002380 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002381 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 context->root_level = PT32_ROOT_LEVEL;
2383 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002384 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 return 0;
2386}
2387
2388static int paging32E_init_context(struct kvm_vcpu *vcpu)
2389{
Dong, Eddie82725b22009-03-30 16:21:08 +08002390 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002391 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392}
2393
Joerg Roedelfb72d162008-02-07 13:47:44 +01002394static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2395{
2396 struct kvm_mmu *context = &vcpu->arch.mmu;
2397
2398 context->new_cr3 = nonpaging_new_cr3;
2399 context->page_fault = tdp_page_fault;
2400 context->free = nonpaging_free;
2401 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002402 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002403 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002404 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002405 context->root_hpa = INVALID_PAGE;
2406
2407 if (!is_paging(vcpu)) {
2408 context->gva_to_gpa = nonpaging_gva_to_gpa;
2409 context->root_level = 0;
2410 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002411 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002412 context->gva_to_gpa = paging64_gva_to_gpa;
2413 context->root_level = PT64_ROOT_LEVEL;
2414 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002415 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002416 context->gva_to_gpa = paging64_gva_to_gpa;
2417 context->root_level = PT32E_ROOT_LEVEL;
2418 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002419 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002420 context->gva_to_gpa = paging32_gva_to_gpa;
2421 context->root_level = PT32_ROOT_LEVEL;
2422 }
2423
2424 return 0;
2425}
2426
2427static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002429 int r;
2430
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002432 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433
2434 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002435 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002436 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002437 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002439 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002441 r = paging32_init_context(vcpu);
2442
2443 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2444
2445 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446}
2447
Joerg Roedelfb72d162008-02-07 13:47:44 +01002448static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2449{
Anthony Liguori35149e22008-04-02 14:46:56 -05002450 vcpu->arch.update_pte.pfn = bad_pfn;
2451
Joerg Roedelfb72d162008-02-07 13:47:44 +01002452 if (tdp_enabled)
2453 return init_kvm_tdp_mmu(vcpu);
2454 else
2455 return init_kvm_softmmu(vcpu);
2456}
2457
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2459{
2460 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002461 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2462 vcpu->arch.mmu.free(vcpu);
2463 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 }
2465}
2466
2467int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2468{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002469 destroy_kvm_mmu(vcpu);
2470 return init_kvm_mmu(vcpu);
2471}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002472EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002473
2474int kvm_mmu_load(struct kvm_vcpu *vcpu)
2475{
Avi Kivity714b93d2007-01-05 16:36:53 -08002476 int r;
2477
Avi Kivitye2dec932007-01-05 16:36:54 -08002478 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002479 if (r)
2480 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002481 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002482 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002483 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002484 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002485 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002486 if (r)
2487 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002488 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002489 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002490out:
2491 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002493EXPORT_SYMBOL_GPL(kvm_mmu_load);
2494
2495void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2496{
2497 mmu_free_roots(vcpu);
2498}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499
Avi Kivity09072da2007-05-01 14:16:52 +03002500static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002501 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002502 u64 *spte)
2503{
2504 u64 pte;
2505 struct kvm_mmu_page *child;
2506
2507 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002508 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002509 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002510 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002511 else {
2512 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002513 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002514 }
2515 }
Avi Kivityd555c332009-06-10 14:24:23 +03002516 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002517 if (is_large_pte(pte))
2518 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002519}
2520
Avi Kivity00284252007-05-01 16:53:31 +03002521static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002522 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002523 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002524 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002525{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002526 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002527 ++vcpu->kvm->stat.mmu_pde_zapped;
2528 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002529 }
Avi Kivity00284252007-05-01 16:53:31 +03002530
Avi Kivity4cee5762007-11-18 16:37:07 +02002531 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002532 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002533 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002534 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002535 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002536}
2537
Avi Kivity79539ce2007-11-21 02:06:21 +02002538static bool need_remote_flush(u64 old, u64 new)
2539{
2540 if (!is_shadow_present_pte(old))
2541 return false;
2542 if (!is_shadow_present_pte(new))
2543 return true;
2544 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2545 return true;
2546 old ^= PT64_NX_MASK;
2547 new ^= PT64_NX_MASK;
2548 return (old & ~new & PT64_PERM_MASK) != 0;
2549}
2550
2551static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2552{
2553 if (need_remote_flush(old, new))
2554 kvm_flush_remote_tlbs(vcpu->kvm);
2555 else
2556 kvm_mmu_flush_tlb(vcpu);
2557}
2558
Avi Kivity12b7d282007-09-23 14:10:49 +02002559static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2560{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002561 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002562
Sheng Yang7b523452008-04-25 21:13:50 +08002563 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002564}
2565
Avi Kivityd7824ff2007-12-30 12:29:05 +02002566static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2567 const u8 *new, int bytes)
2568{
2569 gfn_t gfn;
2570 int r;
2571 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002572 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002573
2574 if (bytes != 4 && bytes != 8)
2575 return;
2576
2577 /*
2578 * Assume that the pte write on a page table of the same type
2579 * as the current vcpu paging mode. This is nearly always true
2580 * (might be false while changing modes). Note it is verified later
2581 * by update_pte().
2582 */
2583 if (is_pae(vcpu)) {
2584 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2585 if ((bytes == 4) && (gpa % 4 == 0)) {
2586 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2587 if (r)
2588 return;
2589 memcpy((void *)&gpte + (gpa % 8), new, 4);
2590 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2591 memcpy((void *)&gpte, new, 8);
2592 }
2593 } else {
2594 if ((bytes == 4) && (gpa % 4 == 0))
2595 memcpy((void *)&gpte, new, 4);
2596 }
Avi Kivity43a37952009-06-10 14:12:05 +03002597 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002598 return;
2599 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002600
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002601 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002602 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002603 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002604
Anthony Liguori35149e22008-04-02 14:46:56 -05002605 if (is_error_pfn(pfn)) {
2606 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002607 return;
2608 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002609 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002610 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002611}
2612
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002613static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2614{
2615 u64 *spte = vcpu->arch.last_pte_updated;
2616
2617 if (spte
2618 && vcpu->arch.last_pte_gfn == gfn
2619 && shadow_accessed_mask
2620 && !(*spte & shadow_accessed_mask)
2621 && is_shadow_present_pte(*spte))
2622 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2623}
2624
Avi Kivity09072da2007-05-01 14:16:52 +03002625void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002626 const u8 *new, int bytes,
2627 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002628{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002629 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002630 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002631 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002632 struct hlist_head *bucket;
2633 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002634 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002635 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002636 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002637 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002638 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002639 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002640 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002641 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002642 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002643 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002644 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002645
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002646 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002647 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002648 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002649 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002650 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002651 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002652 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002653 if (guest_initiated) {
2654 if (gfn == vcpu->arch.last_pt_write_gfn
2655 && !last_updated_pte_accessed(vcpu)) {
2656 ++vcpu->arch.last_pt_write_count;
2657 if (vcpu->arch.last_pt_write_count >= 3)
2658 flooded = 1;
2659 } else {
2660 vcpu->arch.last_pt_write_gfn = gfn;
2661 vcpu->arch.last_pt_write_count = 1;
2662 vcpu->arch.last_pte_updated = NULL;
2663 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002664 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002665 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002666 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002667 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002668 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002669 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002670 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002671 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002672 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002673 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002674 /*
2675 * Misaligned accesses are too much trouble to fix
2676 * up; also, they usually indicate a page is not used
2677 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002678 *
2679 * If we're seeing too many writes to a page,
2680 * it may no longer be a page table, or we may be
2681 * forking, in which case it is better to unmap the
2682 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002683 */
2684 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002685 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002686 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2687 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002688 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002689 continue;
2690 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002691 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002692 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002693 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002694 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002695 page_offset <<= 1; /* 32->64 */
2696 /*
2697 * A 32-bit pde maps 4MB while the shadow pdes map
2698 * only 2MB. So we need to double the offset again
2699 * and zap two pdes instead of one.
2700 */
2701 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002702 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002703 page_offset <<= 1;
2704 npte = 2;
2705 }
Avi Kivityfce06572007-05-01 16:44:05 +03002706 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002707 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002708 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002709 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002710 }
Avi Kivity4db35312007-11-21 15:28:32 +02002711 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002712 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2713 gentry = 0;
2714 r = kvm_read_guest_atomic(vcpu->kvm,
2715 gpa & ~(u64)(pte_size - 1),
2716 &gentry, pte_size);
2717 new = (const void *)&gentry;
2718 if (r < 0)
2719 new = NULL;
2720 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002721 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002722 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002723 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002724 if (new)
2725 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002726 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002727 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002728 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002729 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002730 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002731 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002732 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2733 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2734 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002735 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002736}
2737
Avi Kivitya4360362007-01-05 16:36:45 -08002738int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2739{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002740 gpa_t gpa;
2741 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002742
Avi Kivity60f24782009-08-27 13:37:06 +03002743 if (tdp_enabled)
2744 return 0;
2745
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002746 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002747
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002748 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002749 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002750 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002751 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002752}
Avi Kivity577bdc42008-07-19 08:57:05 +03002753EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002754
Avi Kivity22d95b12007-09-14 20:26:06 +03002755void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002756{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002757 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2758 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002759 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002760
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002761 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002762 struct kvm_mmu_page, link);
2763 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002764 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002765 }
2766}
Avi Kivityebeace82007-01-05 16:36:47 -08002767
Avi Kivity30677142007-10-28 18:48:59 +02002768int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2769{
2770 int r;
2771 enum emulation_result er;
2772
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002773 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002774 if (r < 0)
2775 goto out;
2776
2777 if (!r) {
2778 r = 1;
2779 goto out;
2780 }
2781
Avi Kivityb733bfb2007-10-28 18:52:05 +02002782 r = mmu_topup_memory_caches(vcpu);
2783 if (r)
2784 goto out;
2785
Avi Kivity851ba692009-08-24 11:10:17 +03002786 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002787
2788 switch (er) {
2789 case EMULATE_DONE:
2790 return 1;
2791 case EMULATE_DO_MMIO:
2792 ++vcpu->stat.mmio_exits;
2793 return 0;
2794 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002795 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2796 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02002797 vcpu->run->internal.ndata = 0;
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002798 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002799 default:
2800 BUG();
2801 }
2802out:
Avi Kivity30677142007-10-28 18:48:59 +02002803 return r;
2804}
2805EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2806
Marcelo Tosattia7052892008-09-23 13:18:35 -03002807void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2808{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002809 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002810 kvm_mmu_flush_tlb(vcpu);
2811 ++vcpu->stat.invlpg;
2812}
2813EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2814
Joerg Roedel18552672008-02-07 13:47:41 +01002815void kvm_enable_tdp(void)
2816{
2817 tdp_enabled = true;
2818}
2819EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2820
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002821void kvm_disable_tdp(void)
2822{
2823 tdp_enabled = false;
2824}
2825EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2826
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827static void free_mmu_pages(struct kvm_vcpu *vcpu)
2828{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002829 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830}
2831
2832static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2833{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002834 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 int i;
2836
2837 ASSERT(vcpu);
2838
Avi Kivity17ac10a2007-01-05 16:36:40 -08002839 /*
2840 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2841 * Therefore we need to allocate shadow page tables in the first
2842 * 4GB of memory, which happens to fit the DMA32 zone.
2843 */
2844 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2845 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08002846 return -ENOMEM;
2847
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002849 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002850 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002851
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853}
2854
Ingo Molnar8018c272006-12-29 16:50:01 -08002855int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002858 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859
Ingo Molnar8018c272006-12-29 16:50:01 -08002860 return alloc_mmu_pages(vcpu);
2861}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862
Ingo Molnar8018c272006-12-29 16:50:01 -08002863int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2864{
2865 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002866 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002867
Ingo Molnar8018c272006-12-29 16:50:01 -08002868 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869}
2870
2871void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2872{
2873 ASSERT(vcpu);
2874
2875 destroy_kvm_mmu(vcpu);
2876 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002877 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878}
2879
Avi Kivity90cb0522007-07-17 13:04:56 +03002880void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881{
Avi Kivity4db35312007-11-21 15:28:32 +02002882 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002884 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 int i;
2886 u64 *pt;
2887
Sheng Yang291f26b2008-10-16 17:30:57 +08002888 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 continue;
2890
Avi Kivity4db35312007-11-21 15:28:32 +02002891 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2893 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002894 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 }
Avi Kivity171d5952008-08-27 16:40:51 +03002897 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002899
Avi Kivity90cb0522007-07-17 13:04:56 +03002900void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002901{
Avi Kivity4db35312007-11-21 15:28:32 +02002902 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002903
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002904 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002905 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002906 if (kvm_mmu_zap_page(kvm, sp))
2907 node = container_of(kvm->arch.active_mmu_pages.next,
2908 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002909 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002910
Avi Kivity90cb0522007-07-17 13:04:56 +03002911 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002912}
2913
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002914static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002915{
2916 struct kvm_mmu_page *page;
2917
2918 page = container_of(kvm->arch.active_mmu_pages.prev,
2919 struct kvm_mmu_page, link);
2920 kvm_mmu_zap_page(kvm, page);
2921}
2922
2923static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2924{
2925 struct kvm *kvm;
2926 struct kvm *kvm_freed = NULL;
2927 int cache_count = 0;
2928
2929 spin_lock(&kvm_lock);
2930
2931 list_for_each_entry(kvm, &vm_list, vm_list) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002932 int npages, idx;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002933
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002934 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002935 spin_lock(&kvm->mmu_lock);
2936 npages = kvm->arch.n_alloc_mmu_pages -
2937 kvm->arch.n_free_mmu_pages;
2938 cache_count += npages;
2939 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2940 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2941 cache_count--;
2942 kvm_freed = kvm;
2943 }
2944 nr_to_scan--;
2945
2946 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002947 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002948 }
2949 if (kvm_freed)
2950 list_move_tail(&kvm_freed->vm_list, &vm_list);
2951
2952 spin_unlock(&kvm_lock);
2953
2954 return cache_count;
2955}
2956
2957static struct shrinker mmu_shrinker = {
2958 .shrink = mmu_shrink,
2959 .seeks = DEFAULT_SEEKS * 10,
2960};
2961
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002962static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002963{
2964 if (pte_chain_cache)
2965 kmem_cache_destroy(pte_chain_cache);
2966 if (rmap_desc_cache)
2967 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002968 if (mmu_page_header_cache)
2969 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002970}
2971
Izik Eidus3ee16c82008-03-30 15:17:21 +03002972void kvm_mmu_module_exit(void)
2973{
2974 mmu_destroy_caches();
2975 unregister_shrinker(&mmu_shrinker);
2976}
2977
Avi Kivityb5a33a72007-04-15 16:31:09 +03002978int kvm_mmu_module_init(void)
2979{
2980 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2981 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002982 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002983 if (!pte_chain_cache)
2984 goto nomem;
2985 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2986 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002987 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002988 if (!rmap_desc_cache)
2989 goto nomem;
2990
Avi Kivityd3d25b02007-05-30 12:34:53 +03002991 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2992 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002993 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002994 if (!mmu_page_header_cache)
2995 goto nomem;
2996
Izik Eidus3ee16c82008-03-30 15:17:21 +03002997 register_shrinker(&mmu_shrinker);
2998
Avi Kivityb5a33a72007-04-15 16:31:09 +03002999 return 0;
3000
3001nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003002 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003003 return -ENOMEM;
3004}
3005
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003006/*
3007 * Caculate mmu pages needed for kvm.
3008 */
3009unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3010{
3011 int i;
3012 unsigned int nr_mmu_pages;
3013 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003014 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003015
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003016 slots = rcu_dereference(kvm->memslots);
3017 for (i = 0; i < slots->nmemslots; i++)
3018 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003019
3020 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3021 nr_mmu_pages = max(nr_mmu_pages,
3022 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3023
3024 return nr_mmu_pages;
3025}
3026
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003027static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3028 unsigned len)
3029{
3030 if (len > buffer->len)
3031 return NULL;
3032 return buffer->ptr;
3033}
3034
3035static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3036 unsigned len)
3037{
3038 void *ret;
3039
3040 ret = pv_mmu_peek_buffer(buffer, len);
3041 if (!ret)
3042 return ret;
3043 buffer->ptr += len;
3044 buffer->len -= len;
3045 buffer->processed += len;
3046 return ret;
3047}
3048
3049static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3050 gpa_t addr, gpa_t value)
3051{
3052 int bytes = 8;
3053 int r;
3054
3055 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3056 bytes = 4;
3057
3058 r = mmu_topup_memory_caches(vcpu);
3059 if (r)
3060 return r;
3061
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003062 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003063 return -EFAULT;
3064
3065 return 1;
3066}
3067
3068static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3069{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003070 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003071 return 1;
3072}
3073
3074static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3075{
3076 spin_lock(&vcpu->kvm->mmu_lock);
3077 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3078 spin_unlock(&vcpu->kvm->mmu_lock);
3079 return 1;
3080}
3081
3082static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3083 struct kvm_pv_mmu_op_buffer *buffer)
3084{
3085 struct kvm_mmu_op_header *header;
3086
3087 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3088 if (!header)
3089 return 0;
3090 switch (header->op) {
3091 case KVM_MMU_OP_WRITE_PTE: {
3092 struct kvm_mmu_op_write_pte *wpte;
3093
3094 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3095 if (!wpte)
3096 return 0;
3097 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3098 wpte->pte_val);
3099 }
3100 case KVM_MMU_OP_FLUSH_TLB: {
3101 struct kvm_mmu_op_flush_tlb *ftlb;
3102
3103 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3104 if (!ftlb)
3105 return 0;
3106 return kvm_pv_mmu_flush_tlb(vcpu);
3107 }
3108 case KVM_MMU_OP_RELEASE_PT: {
3109 struct kvm_mmu_op_release_pt *rpt;
3110
3111 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3112 if (!rpt)
3113 return 0;
3114 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3115 }
3116 default: return 0;
3117 }
3118}
3119
3120int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3121 gpa_t addr, unsigned long *ret)
3122{
3123 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003124 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003125
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003126 buffer->ptr = buffer->buf;
3127 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3128 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003129
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003130 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003131 if (r)
3132 goto out;
3133
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003134 while (buffer->len) {
3135 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003136 if (r < 0)
3137 goto out;
3138 if (r == 0)
3139 break;
3140 }
3141
3142 r = 1;
3143out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003144 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003145 return r;
3146}
3147
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003148int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3149{
3150 struct kvm_shadow_walk_iterator iterator;
3151 int nr_sptes = 0;
3152
3153 spin_lock(&vcpu->kvm->mmu_lock);
3154 for_each_shadow_entry(vcpu, addr, iterator) {
3155 sptes[iterator.level-1] = *iterator.sptep;
3156 nr_sptes++;
3157 if (!is_shadow_present_pte(*iterator.sptep))
3158 break;
3159 }
3160 spin_unlock(&vcpu->kvm->mmu_lock);
3161
3162 return nr_sptes;
3163}
3164EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3165
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003166#ifdef AUDIT
3167
3168static const char *audit_msg;
3169
3170static gva_t canonicalize(gva_t gva)
3171{
3172#ifdef CONFIG_X86_64
3173 gva = (long long)(gva << 16) >> 16;
3174#endif
3175 return gva;
3176}
3177
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003178
3179typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3180 u64 *sptep);
3181
3182static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3183 inspect_spte_fn fn)
3184{
3185 int i;
3186
3187 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3188 u64 ent = sp->spt[i];
3189
3190 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003191 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003192 struct kvm_mmu_page *child;
3193 child = page_header(ent & PT64_BASE_ADDR_MASK);
3194 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003195 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003196 fn(kvm, sp, &sp->spt[i]);
3197 }
3198 }
3199}
3200
3201static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3202{
3203 int i;
3204 struct kvm_mmu_page *sp;
3205
3206 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3207 return;
3208 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3209 hpa_t root = vcpu->arch.mmu.root_hpa;
3210 sp = page_header(root);
3211 __mmu_spte_walk(vcpu->kvm, sp, fn);
3212 return;
3213 }
3214 for (i = 0; i < 4; ++i) {
3215 hpa_t root = vcpu->arch.mmu.pae_root[i];
3216
3217 if (root && VALID_PAGE(root)) {
3218 root &= PT64_BASE_ADDR_MASK;
3219 sp = page_header(root);
3220 __mmu_spte_walk(vcpu->kvm, sp, fn);
3221 }
3222 }
3223 return;
3224}
3225
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003226static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3227 gva_t va, int level)
3228{
3229 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3230 int i;
3231 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3232
3233 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3234 u64 ent = pt[i];
3235
Avi Kivityc7addb92007-09-16 18:58:32 +02003236 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003237 continue;
3238
3239 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003240 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3241 audit_mappings_page(vcpu, ent, va, level - 1);
3242 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003243 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003244 gfn_t gfn = gpa >> PAGE_SHIFT;
3245 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3246 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003247
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003248 if (is_error_pfn(pfn)) {
3249 kvm_release_pfn_clean(pfn);
3250 continue;
3251 }
3252
Avi Kivityc7addb92007-09-16 18:58:32 +02003253 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003254 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003255 printk(KERN_ERR "xx audit error: (%s) levels %d"
3256 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003257 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003258 va, gpa, hpa, ent,
3259 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003260 else if (ent == shadow_notrap_nonpresent_pte
3261 && !is_error_hpa(hpa))
3262 printk(KERN_ERR "audit: (%s) notrap shadow,"
3263 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003264 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003265
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003266 }
3267 }
3268}
3269
3270static void audit_mappings(struct kvm_vcpu *vcpu)
3271{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003272 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003273
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003274 if (vcpu->arch.mmu.root_level == 4)
3275 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003276 else
3277 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003278 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003279 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003280 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003281 i << 30,
3282 2);
3283}
3284
3285static int count_rmaps(struct kvm_vcpu *vcpu)
3286{
3287 int nmaps = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003288 int i, j, k, idx;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003289
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003290 idx = srcu_read_lock(&kvm->srcu);
3291 slots = rcu_dereference(kvm->memslots);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003292 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003293 struct kvm_memory_slot *m = &slots->memslots[i];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003294 struct kvm_rmap_desc *d;
3295
3296 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003297 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003298
Izik Eidus290fc382007-09-27 14:11:22 +02003299 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003300 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003301 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003302 ++nmaps;
3303 continue;
3304 }
Izik Eidus290fc382007-09-27 14:11:22 +02003305 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003306 while (d) {
3307 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003308 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003309 ++nmaps;
3310 else
3311 break;
3312 d = d->more;
3313 }
3314 }
3315 }
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003316 srcu_read_unlock(&kvm->srcu, idx);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003317 return nmaps;
3318}
3319
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003320void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003321{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003322 unsigned long *rmapp;
3323 struct kvm_mmu_page *rev_sp;
3324 gfn_t gfn;
3325
3326 if (*sptep & PT_WRITABLE_MASK) {
3327 rev_sp = page_header(__pa(sptep));
3328 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3329
3330 if (!gfn_to_memslot(kvm, gfn)) {
3331 if (!printk_ratelimit())
3332 return;
3333 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3334 audit_msg, gfn);
3335 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3336 audit_msg, sptep - rev_sp->spt,
3337 rev_sp->gfn);
3338 dump_stack();
3339 return;
3340 }
3341
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003342 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3343 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003344 if (!*rmapp) {
3345 if (!printk_ratelimit())
3346 return;
3347 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3348 audit_msg, *sptep);
3349 dump_stack();
3350 }
3351 }
3352
3353}
3354
3355void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3356{
3357 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3358}
3359
3360static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3361{
Avi Kivity4db35312007-11-21 15:28:32 +02003362 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003363 int i;
3364
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003365 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003366 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367
Avi Kivity4db35312007-11-21 15:28:32 +02003368 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369 continue;
3370
3371 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3372 u64 ent = pt[i];
3373
3374 if (!(ent & PT_PRESENT_MASK))
3375 continue;
3376 if (!(ent & PT_WRITABLE_MASK))
3377 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003378 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003379 }
3380 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003381 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003382}
3383
3384static void audit_rmap(struct kvm_vcpu *vcpu)
3385{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003386 check_writable_mappings_rmap(vcpu);
3387 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003388}
3389
3390static void audit_write_protection(struct kvm_vcpu *vcpu)
3391{
Avi Kivity4db35312007-11-21 15:28:32 +02003392 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003393 struct kvm_memory_slot *slot;
3394 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003395 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003396 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003397
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003398 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003399 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003400 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003401 if (sp->unsync)
3402 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003403
Avi Kivity4db35312007-11-21 15:28:32 +02003404 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003405 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003406 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003407
3408 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3409 while (spte) {
3410 if (*spte & PT_WRITABLE_MASK)
3411 printk(KERN_ERR "%s: (%s) shadow page has "
3412 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003413 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003414 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003415 spte = rmap_next(vcpu->kvm, rmapp, spte);
3416 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003417 }
3418}
3419
3420static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3421{
3422 int olddbg = dbg;
3423
3424 dbg = 0;
3425 audit_msg = msg;
3426 audit_rmap(vcpu);
3427 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003428 if (strcmp("pre pte write", audit_msg) != 0)
3429 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003430 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003431 dbg = olddbg;
3432}
3433
3434#endif