blob: 8ee67e3fb9d0221fc58f35841577f83e14d2e0e7 [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 Kivity6de4f3a2009-05-31 22:58:47 +030021#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
33#include <asm/page.h>
34#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020035#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020036#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
Joerg Roedel18552672008-02-07 13:47:41 +010038/*
39 * When setting this variable to true it enables Two-Dimensional-Paging
40 * where the hardware walks 2 page tables:
41 * 1. the guest-virtual to guest-physical
42 * 2. while doing 1. it walks guest-physical to host-physical
43 * If the hardware supports that we don't need to do shadow paging.
44 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050045bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010046
Avi Kivity37a7d8b2007-01-05 16:36:56 -080047#undef MMU_DEBUG
48
49#undef AUDIT
50
51#ifdef AUDIT
52static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
53#else
54static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
55#endif
56
57#ifdef MMU_DEBUG
58
59#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
60#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
61
62#else
63
64#define pgprintk(x...) do { } while (0)
65#define rmap_printk(x...) do { } while (0)
66
67#endif
68
69#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030070static int dbg = 0;
71module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
Marcelo Tosatti582801a2008-09-23 13:18:41 -030074static int oos_shadow = 1;
75module_param(oos_shadow, bool, 0644);
76
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080077#ifndef MMU_DEBUG
78#define ASSERT(x) do { } while (0)
79#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080080#define ASSERT(x) \
81 if (!(x)) { \
82 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
83 __FILE__, __LINE__, #x); \
84 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080085#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
Avi Kivity6aa8b732006-12-10 02:21:36 -080087#define PT_FIRST_AVAIL_BITS_SHIFT 9
88#define PT64_SECOND_AVAIL_BITS_SHIFT 52
89
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define VALID_PAGE(x) ((x) != INVALID_PAGE)
91
92#define PT64_LEVEL_BITS 9
93
94#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040095 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
97#define PT64_LEVEL_MASK(level) \
98 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
99
100#define PT64_INDEX(address, level)\
101 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
102
103
104#define PT32_LEVEL_BITS 10
105
106#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400107 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108
109#define PT32_LEVEL_MASK(level) \
110 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
111
112#define PT32_INDEX(address, level)\
113 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
114
115
Avi Kivity27aba762007-03-09 13:04:31 +0200116#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117#define PT64_DIR_BASE_ADDR_MASK \
118 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
119
120#define PT32_BASE_ADDR_MASK PAGE_MASK
121#define PT32_DIR_BASE_ADDR_MASK \
122 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
123
Avi Kivity79539ce2007-11-21 02:06:21 +0200124#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
125 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126
127#define PFERR_PRESENT_MASK (1U << 0)
128#define PFERR_WRITE_MASK (1U << 1)
129#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800130#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800131#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define PT_DIRECTORY_LEVEL 2
134#define PT_PAGE_TABLE_LEVEL 1
135
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800136#define RMAP_EXT 4
137
Avi Kivityfe135d22007-12-09 16:15:46 +0200138#define ACC_EXEC_MASK 1
139#define ACC_WRITE_MASK PT_WRITABLE_MASK
140#define ACC_USER_MASK PT_USER_MASK
141#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
142
Avi Kivity135f8c22008-08-21 17:49:56 +0300143#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
144
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800145struct kvm_rmap_desc {
146 u64 *shadow_ptes[RMAP_EXT];
147 struct kvm_rmap_desc *more;
148};
149
Avi Kivity2d111232008-12-25 14:39:47 +0200150struct kvm_shadow_walk_iterator {
151 u64 addr;
152 hpa_t shadow_addr;
153 int level;
154 u64 *sptep;
155 unsigned index;
156};
157
158#define for_each_shadow_entry(_vcpu, _addr, _walker) \
159 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
160 shadow_walk_okay(&(_walker)); \
161 shadow_walk_next(&(_walker)))
162
163
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300164struct kvm_unsync_walk {
165 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
166};
167
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300168typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
169
Avi Kivityb5a33a72007-04-15 16:31:09 +0300170static struct kmem_cache *pte_chain_cache;
171static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300172static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300173
Avi Kivityc7addb92007-09-16 18:58:32 +0200174static u64 __read_mostly shadow_trap_nonpresent_pte;
175static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800176static u64 __read_mostly shadow_base_present_pte;
177static u64 __read_mostly shadow_nx_mask;
178static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
179static u64 __read_mostly shadow_user_mask;
180static u64 __read_mostly shadow_accessed_mask;
181static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200182
Dong, Eddie82725b22009-03-30 16:21:08 +0800183static inline u64 rsvd_bits(int s, int e)
184{
185 return ((1ULL << (e - s + 1)) - 1) << s;
186}
187
Avi Kivityc7addb92007-09-16 18:58:32 +0200188void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
189{
190 shadow_trap_nonpresent_pte = trap_pte;
191 shadow_notrap_nonpresent_pte = notrap_pte;
192}
193EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
194
Sheng Yang7b523452008-04-25 21:13:50 +0800195void kvm_mmu_set_base_ptes(u64 base_pte)
196{
197 shadow_base_present_pte = base_pte;
198}
199EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
200
201void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800202 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800203{
204 shadow_user_mask = user_mask;
205 shadow_accessed_mask = accessed_mask;
206 shadow_dirty_mask = dirty_mask;
207 shadow_nx_mask = nx_mask;
208 shadow_x_mask = x_mask;
209}
210EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static int is_write_protection(struct kvm_vcpu *vcpu)
213{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800214 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215}
216
217static int is_cpuid_PSE36(void)
218{
219 return 1;
220}
221
Avi Kivity73b10872007-01-26 00:56:41 -0800222static int is_nx(struct kvm_vcpu *vcpu)
223{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800224 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800225}
226
Avi Kivityc7addb92007-09-16 18:58:32 +0200227static int is_shadow_present_pte(u64 pte)
228{
Avi Kivityc7addb92007-09-16 18:58:32 +0200229 return pte != shadow_trap_nonpresent_pte
230 && pte != shadow_notrap_nonpresent_pte;
231}
232
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300233static int is_large_pte(u64 pte)
234{
235 return pte & PT_PAGE_SIZE_MASK;
236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static int is_writeble_pte(unsigned long pte)
239{
240 return pte & PT_WRITABLE_MASK;
241}
242
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200243static int is_dirty_pte(unsigned long pte)
244{
Sheng Yang7b523452008-04-25 21:13:50 +0800245 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200246}
247
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800248static int is_rmap_pte(u64 pte)
249{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200250 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800251}
252
Anthony Liguori35149e22008-04-02 14:46:56 -0500253static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200254{
Anthony Liguori35149e22008-04-02 14:46:56 -0500255 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200256}
257
Avi Kivityda928522007-11-21 13:54:47 +0200258static gfn_t pse36_gfn_delta(u32 gpte)
259{
260 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
261
262 return (gpte & PT32_DIR_PSE36_MASK) << shift;
263}
264
Avi Kivitye663ee62007-05-31 15:46:04 +0300265static void set_shadow_pte(u64 *sptep, u64 spte)
266{
267#ifdef CONFIG_X86_64
268 set_64bit((unsigned long *)sptep, spte);
269#else
270 set_64bit((unsigned long long *)sptep, spte);
271#endif
272}
273
Avi Kivitye2dec932007-01-05 16:36:54 -0800274static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300275 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800276{
277 void *obj;
278
279 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800280 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800281 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300282 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800283 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800284 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800285 cache->objects[cache->nobjs++] = obj;
286 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800287 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800288}
289
290static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
291{
292 while (mc->nobjs)
293 kfree(mc->objects[--mc->nobjs]);
294}
295
Avi Kivityc1158e62007-07-20 08:18:27 +0300296static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300297 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300298{
299 struct page *page;
300
301 if (cache->nobjs >= min)
302 return 0;
303 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300304 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300305 if (!page)
306 return -ENOMEM;
307 set_page_private(page, 0);
308 cache->objects[cache->nobjs++] = page_address(page);
309 }
310 return 0;
311}
312
313static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
314{
315 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300316 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300317}
318
Avi Kivity8c438502007-04-16 11:53:17 +0300319static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
320{
321 int r;
322
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800323 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 pte_chain_cache, 4);
325 if (r)
326 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800327 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200328 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300329 if (r)
330 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800331 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300332 if (r)
333 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800334 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300335 mmu_page_header_cache, 4);
336out:
Avi Kivity8c438502007-04-16 11:53:17 +0300337 return r;
338}
339
Avi Kivity714b93d2007-01-05 16:36:53 -0800340static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
341{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
343 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
344 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
345 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800346}
347
348static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
349 size_t size)
350{
351 void *p;
352
353 BUG_ON(!mc->nobjs);
354 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800355 return p;
356}
357
Avi Kivity714b93d2007-01-05 16:36:53 -0800358static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
359{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800360 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800361 sizeof(struct kvm_pte_chain));
362}
363
Avi Kivity90cb0522007-07-17 13:04:56 +0300364static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800365{
Avi Kivity90cb0522007-07-17 13:04:56 +0300366 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800367}
368
369static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
370{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800371 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800372 sizeof(struct kvm_rmap_desc));
373}
374
Avi Kivity90cb0522007-07-17 13:04:56 +0300375static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800376{
Avi Kivity90cb0522007-07-17 13:04:56 +0300377 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800378}
379
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800380/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300381 * Return the pointer to the largepage write count for a given
382 * gfn, handling slots that are not large page aligned.
383 */
384static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
385{
386 unsigned long idx;
387
388 idx = (gfn / KVM_PAGES_PER_HPAGE) -
389 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
390 return &slot->lpage_info[idx].write_count;
391}
392
393static void account_shadowed(struct kvm *kvm, gfn_t gfn)
394{
395 int *write_count;
396
Izik Eidus28430992008-10-03 17:40:32 +0300397 gfn = unalias_gfn(kvm, gfn);
398 write_count = slot_largepage_idx(gfn,
399 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300400 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300401}
402
403static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
404{
405 int *write_count;
406
Izik Eidus28430992008-10-03 17:40:32 +0300407 gfn = unalias_gfn(kvm, gfn);
408 write_count = slot_largepage_idx(gfn,
409 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300410 *write_count -= 1;
411 WARN_ON(*write_count < 0);
412}
413
414static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
415{
Izik Eidus28430992008-10-03 17:40:32 +0300416 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300417 int *largepage_idx;
418
Izik Eidus28430992008-10-03 17:40:32 +0300419 gfn = unalias_gfn(kvm, gfn);
420 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421 if (slot) {
422 largepage_idx = slot_largepage_idx(gfn, slot);
423 return *largepage_idx;
424 }
425
426 return 1;
427}
428
429static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
430{
431 struct vm_area_struct *vma;
432 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300433 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434
435 addr = gfn_to_hva(kvm, gfn);
436 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300437 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300438
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300439 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300440 vma = find_vma(current->mm, addr);
441 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300442 ret = 1;
443 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300444
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300445 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300446}
447
448static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
449{
450 struct kvm_memory_slot *slot;
451
452 if (has_wrprotected_page(vcpu->kvm, large_gfn))
453 return 0;
454
455 if (!host_largepage_backed(vcpu->kvm, large_gfn))
456 return 0;
457
458 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
459 if (slot && slot->dirty_bitmap)
460 return 0;
461
462 return 1;
463}
464
465/*
Izik Eidus290fc382007-09-27 14:11:22 +0200466 * Take gfn and return the reverse mapping to it.
467 * Note: gfn must be unaliased before this function get called
468 */
469
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200471{
472 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300473 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200474
475 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476 if (!lpage)
477 return &slot->rmap[gfn - slot->base_gfn];
478
479 idx = (gfn / KVM_PAGES_PER_HPAGE) -
480 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
481
482 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200483}
484
485/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 * Reverse mapping data structures:
487 *
Izik Eidus290fc382007-09-27 14:11:22 +0200488 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
489 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800490 *
Izik Eidus290fc382007-09-27 14:11:22 +0200491 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
492 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300493 *
494 * Returns the number of rmap entries before the spte was added or zero if
495 * the spte was not added.
496 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800497 */
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300498static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800499{
Avi Kivity4db35312007-11-21 15:28:32 +0200500 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800501 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200502 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300503 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800504
505 if (!is_rmap_pte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300506 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200507 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200508 sp = page_header(__pa(spte));
509 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300510 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200511 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800512 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200513 *rmapp = (unsigned long)spte;
514 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800515 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800516 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200517 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800518 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200519 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800520 } else {
521 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200522 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300523 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300525 count += RMAP_EXT;
526 }
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800528 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800529 desc = desc->more;
530 }
531 for (i = 0; desc->shadow_ptes[i]; ++i)
532 ;
533 desc->shadow_ptes[i] = spte;
534 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300535 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800536}
537
Izik Eidus290fc382007-09-27 14:11:22 +0200538static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800539 struct kvm_rmap_desc *desc,
540 int i,
541 struct kvm_rmap_desc *prev_desc)
542{
543 int j;
544
545 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
546 ;
547 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000548 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800549 if (j != 0)
550 return;
551 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200552 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553 else
554 if (prev_desc)
555 prev_desc->more = desc->more;
556 else
Izik Eidus290fc382007-09-27 14:11:22 +0200557 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300558 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559}
560
Izik Eidus290fc382007-09-27 14:11:22 +0200561static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 struct kvm_rmap_desc *desc;
564 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200565 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500566 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200567 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800568 int i;
569
570 if (!is_rmap_pte(*spte))
571 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200572 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500573 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800574 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500575 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200576 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500577 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200578 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500579 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300580 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200581 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800582 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
583 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200584 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800585 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200586 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800587 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
588 spte, *spte);
589 BUG();
590 }
Izik Eidus290fc382007-09-27 14:11:22 +0200591 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800592 } else {
593 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200594 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800595 prev_desc = NULL;
596 while (desc) {
597 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
598 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200599 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800600 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800601 prev_desc);
602 return;
603 }
604 prev_desc = desc;
605 desc = desc->more;
606 }
607 BUG();
608 }
609}
610
Izik Eidus98348e92007-10-16 14:42:30 +0200611static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800612{
Avi Kivity374cbac2007-01-05 16:36:43 -0800613 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200614 struct kvm_rmap_desc *prev_desc;
615 u64 *prev_spte;
616 int i;
617
618 if (!*rmapp)
619 return NULL;
620 else if (!(*rmapp & 1)) {
621 if (!spte)
622 return (u64 *)*rmapp;
623 return NULL;
624 }
625 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
626 prev_desc = NULL;
627 prev_spte = NULL;
628 while (desc) {
629 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
630 if (prev_spte == spte)
631 return desc->shadow_ptes[i];
632 prev_spte = desc->shadow_ptes[i];
633 }
634 desc = desc->more;
635 }
636 return NULL;
637}
638
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200639static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200640{
Izik Eidus290fc382007-09-27 14:11:22 +0200641 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800642 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800643 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800644
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500645 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300646 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800647
Izik Eidus98348e92007-10-16 14:42:30 +0200648 spte = rmap_next(kvm, rmapp, NULL);
649 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800650 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800651 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800652 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800653 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200654 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800655 write_protected = 1;
656 }
Izik Eidus9647c142007-10-16 14:43:46 +0200657 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800658 }
Izik Eidus855149a2008-03-20 18:17:24 +0200659 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500660 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200661
662 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500663 pfn = spte_to_pfn(*spte);
664 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200665 }
666
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300667 /* check for huge page mappings */
668 rmapp = gfn_to_rmap(kvm, gfn, 1);
669 spte = rmap_next(kvm, rmapp, NULL);
670 while (spte) {
671 BUG_ON(!spte);
672 BUG_ON(!(*spte & PT_PRESENT_MASK));
673 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
674 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
675 if (is_writeble_pte(*spte)) {
676 rmap_remove(kvm, spte);
677 --kvm->stat.lpages;
678 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300679 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300680 write_protected = 1;
681 }
682 spte = rmap_next(kvm, rmapp, spte);
683 }
684
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200685 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800686}
687
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200688static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
689{
690 u64 *spte;
691 int need_tlb_flush = 0;
692
693 while ((spte = rmap_next(kvm, rmapp, NULL))) {
694 BUG_ON(!(*spte & PT_PRESENT_MASK));
695 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
696 rmap_remove(kvm, spte);
697 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
698 need_tlb_flush = 1;
699 }
700 return need_tlb_flush;
701}
702
703static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
704 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
705{
706 int i;
707 int retval = 0;
708
709 /*
710 * If mmap_sem isn't taken, we can look the memslots with only
711 * the mmu_lock by skipping over the slots with userspace_addr == 0.
712 */
713 for (i = 0; i < kvm->nmemslots; i++) {
714 struct kvm_memory_slot *memslot = &kvm->memslots[i];
715 unsigned long start = memslot->userspace_addr;
716 unsigned long end;
717
718 /* mmu_lock protects userspace_addr */
719 if (!start)
720 continue;
721
722 end = start + (memslot->npages << PAGE_SHIFT);
723 if (hva >= start && hva < end) {
724 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
725 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
726 retval |= handler(kvm,
727 &memslot->lpage_info[
728 gfn_offset /
729 KVM_PAGES_PER_HPAGE].rmap_pde);
730 }
731 }
732
733 return retval;
734}
735
736int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
737{
738 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
739}
740
741static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
742{
743 u64 *spte;
744 int young = 0;
745
Sheng Yang534e38b2008-09-08 15:12:30 +0800746 /* always return old for EPT */
747 if (!shadow_accessed_mask)
748 return 0;
749
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200750 spte = rmap_next(kvm, rmapp, NULL);
751 while (spte) {
752 int _young;
753 u64 _spte = *spte;
754 BUG_ON(!(_spte & PT_PRESENT_MASK));
755 _young = _spte & PT_ACCESSED_MASK;
756 if (_young) {
757 young = 1;
758 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
759 }
760 spte = rmap_next(kvm, rmapp, spte);
761 }
762 return young;
763}
764
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300765#define RMAP_RECYCLE_THRESHOLD 1000
766
767static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
768{
769 unsigned long *rmapp;
770
771 gfn = unalias_gfn(vcpu->kvm, gfn);
772 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
773
774 kvm_unmap_rmapp(vcpu->kvm, rmapp);
775 kvm_flush_remote_tlbs(vcpu->kvm);
776}
777
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200778int kvm_age_hva(struct kvm *kvm, unsigned long hva)
779{
780 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
781}
782
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800783#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300784static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785{
Avi Kivity139bdb22007-01-05 16:36:50 -0800786 u64 *pos;
787 u64 *end;
788
Avi Kivity47ad8e62007-05-06 15:50:58 +0300789 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300790 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800791 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800792 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800794 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 return 1;
796}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800797#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798
Avi Kivity4db35312007-11-21 15:28:32 +0200799static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800800{
Avi Kivity4db35312007-11-21 15:28:32 +0200801 ASSERT(is_empty_shadow_page(sp->spt));
802 list_del(&sp->link);
803 __free_page(virt_to_page(sp->spt));
804 __free_page(virt_to_page(sp->gfns));
805 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800806 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800807}
808
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800809static unsigned kvm_page_table_hashfn(gfn_t gfn)
810{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200811 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800812}
813
Avi Kivity25c0de22007-01-05 16:36:42 -0800814static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
815 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816{
Avi Kivity4db35312007-11-21 15:28:32 +0200817 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800819 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
820 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
821 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200822 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800823 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200824 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800825 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200826 sp->multimapped = 0;
827 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800828 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200829 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830}
831
Avi Kivity714b93d2007-01-05 16:36:53 -0800832static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200833 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800834{
835 struct kvm_pte_chain *pte_chain;
836 struct hlist_node *node;
837 int i;
838
839 if (!parent_pte)
840 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200841 if (!sp->multimapped) {
842 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843
844 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200845 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 return;
847 }
Avi Kivity4db35312007-11-21 15:28:32 +0200848 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800849 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200850 INIT_HLIST_HEAD(&sp->parent_ptes);
851 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852 pte_chain->parent_ptes[0] = old;
853 }
Avi Kivity4db35312007-11-21 15:28:32 +0200854 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800855 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
856 continue;
857 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
858 if (!pte_chain->parent_ptes[i]) {
859 pte_chain->parent_ptes[i] = parent_pte;
860 return;
861 }
862 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800863 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800864 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200865 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800866 pte_chain->parent_ptes[0] = parent_pte;
867}
868
Avi Kivity4db35312007-11-21 15:28:32 +0200869static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800870 u64 *parent_pte)
871{
872 struct kvm_pte_chain *pte_chain;
873 struct hlist_node *node;
874 int i;
875
Avi Kivity4db35312007-11-21 15:28:32 +0200876 if (!sp->multimapped) {
877 BUG_ON(sp->parent_pte != parent_pte);
878 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800879 return;
880 }
Avi Kivity4db35312007-11-21 15:28:32 +0200881 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800882 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
883 if (!pte_chain->parent_ptes[i])
884 break;
885 if (pte_chain->parent_ptes[i] != parent_pte)
886 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800887 while (i + 1 < NR_PTE_CHAIN_ENTRIES
888 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800889 pte_chain->parent_ptes[i]
890 = pte_chain->parent_ptes[i + 1];
891 ++i;
892 }
893 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800894 if (i == 0) {
895 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300896 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200897 if (hlist_empty(&sp->parent_ptes)) {
898 sp->multimapped = 0;
899 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800900 }
901 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800902 return;
903 }
904 BUG();
905}
906
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300907
908static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
909 mmu_parent_walk_fn fn)
910{
911 struct kvm_pte_chain *pte_chain;
912 struct hlist_node *node;
913 struct kvm_mmu_page *parent_sp;
914 int i;
915
916 if (!sp->multimapped && sp->parent_pte) {
917 parent_sp = page_header(__pa(sp->parent_pte));
918 fn(vcpu, parent_sp);
919 mmu_parent_walk(vcpu, parent_sp, fn);
920 return;
921 }
922 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
923 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
924 if (!pte_chain->parent_ptes[i])
925 break;
926 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
927 fn(vcpu, parent_sp);
928 mmu_parent_walk(vcpu, parent_sp, fn);
929 }
930}
931
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300932static void kvm_mmu_update_unsync_bitmap(u64 *spte)
933{
934 unsigned int index;
935 struct kvm_mmu_page *sp = page_header(__pa(spte));
936
937 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200938 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
939 sp->unsync_children++;
940 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300941}
942
943static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
944{
945 struct kvm_pte_chain *pte_chain;
946 struct hlist_node *node;
947 int i;
948
949 if (!sp->parent_pte)
950 return;
951
952 if (!sp->multimapped) {
953 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
954 return;
955 }
956
957 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
958 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
959 if (!pte_chain->parent_ptes[i])
960 break;
961 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
962 }
963}
964
965static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
966{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300967 kvm_mmu_update_parents_unsync(sp);
968 return 1;
969}
970
971static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
972 struct kvm_mmu_page *sp)
973{
974 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
975 kvm_mmu_update_parents_unsync(sp);
976}
977
Avi Kivityd761a502008-05-29 14:55:03 +0300978static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
979 struct kvm_mmu_page *sp)
980{
981 int i;
982
983 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
984 sp->spt[i] = shadow_trap_nonpresent_pte;
985}
986
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300987static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
988 struct kvm_mmu_page *sp)
989{
990 return 1;
991}
992
Marcelo Tosattia7052892008-09-23 13:18:35 -0300993static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
994{
995}
996
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200997#define KVM_PAGE_ARRAY_NR 16
998
999struct kvm_mmu_pages {
1000 struct mmu_page_and_offset {
1001 struct kvm_mmu_page *sp;
1002 unsigned int idx;
1003 } page[KVM_PAGE_ARRAY_NR];
1004 unsigned int nr;
1005};
1006
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001007#define for_each_unsync_children(bitmap, idx) \
1008 for (idx = find_first_bit(bitmap, 512); \
1009 idx < 512; \
1010 idx = find_next_bit(bitmap, 512, idx+1))
1011
Hannes Edercded19f2009-02-21 02:19:13 +01001012static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1013 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001014{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001015 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001016
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001017 if (sp->unsync)
1018 for (i=0; i < pvec->nr; i++)
1019 if (pvec->page[i].sp == sp)
1020 return 0;
1021
1022 pvec->page[pvec->nr].sp = sp;
1023 pvec->page[pvec->nr].idx = idx;
1024 pvec->nr++;
1025 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1026}
1027
1028static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1029 struct kvm_mmu_pages *pvec)
1030{
1031 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001032
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001033 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001034 u64 ent = sp->spt[i];
1035
Marcelo Tosatti87917232008-12-22 18:49:30 -02001036 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001037 struct kvm_mmu_page *child;
1038 child = page_header(ent & PT64_BASE_ADDR_MASK);
1039
1040 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001041 if (mmu_pages_add(pvec, child, i))
1042 return -ENOSPC;
1043
1044 ret = __mmu_unsync_walk(child, pvec);
1045 if (!ret)
1046 __clear_bit(i, sp->unsync_child_bitmap);
1047 else if (ret > 0)
1048 nr_unsync_leaf += ret;
1049 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001050 return ret;
1051 }
1052
1053 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001054 nr_unsync_leaf++;
1055 if (mmu_pages_add(pvec, child, i))
1056 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001057 }
1058 }
1059 }
1060
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001061 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001062 sp->unsync_children = 0;
1063
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001064 return nr_unsync_leaf;
1065}
1066
1067static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1068 struct kvm_mmu_pages *pvec)
1069{
1070 if (!sp->unsync_children)
1071 return 0;
1072
1073 mmu_pages_add(pvec, sp, 0);
1074 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001075}
1076
Avi Kivity4db35312007-11-21 15:28:32 +02001077static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001078{
1079 unsigned index;
1080 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001081 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001082 struct hlist_node *node;
1083
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001084 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001085 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001086 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001087 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001088 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001089 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001090 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001091 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001092 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001093 }
1094 return NULL;
1095}
1096
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001097static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1098{
1099 WARN_ON(!sp->unsync);
1100 sp->unsync = 0;
1101 --kvm->stat.mmu_unsync;
1102}
1103
1104static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1105
1106static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1107{
1108 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1109 kvm_mmu_zap_page(vcpu->kvm, sp);
1110 return 1;
1111 }
1112
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001113 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1114 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001115 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001116 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1117 kvm_mmu_zap_page(vcpu->kvm, sp);
1118 return 1;
1119 }
1120
1121 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001122 return 0;
1123}
1124
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001125struct mmu_page_path {
1126 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1127 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128};
1129
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001130#define for_each_sp(pvec, sp, parents, i) \
1131 for (i = mmu_pages_next(&pvec, &parents, -1), \
1132 sp = pvec.page[i].sp; \
1133 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1134 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001135
Hannes Edercded19f2009-02-21 02:19:13 +01001136static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1137 struct mmu_page_path *parents,
1138 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001139{
1140 int n;
1141
1142 for (n = i+1; n < pvec->nr; n++) {
1143 struct kvm_mmu_page *sp = pvec->page[n].sp;
1144
1145 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1146 parents->idx[0] = pvec->page[n].idx;
1147 return n;
1148 }
1149
1150 parents->parent[sp->role.level-2] = sp;
1151 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1152 }
1153
1154 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001155}
1156
Hannes Edercded19f2009-02-21 02:19:13 +01001157static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001158{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001159 struct kvm_mmu_page *sp;
1160 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001161
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001162 do {
1163 unsigned int idx = parents->idx[level];
1164
1165 sp = parents->parent[level];
1166 if (!sp)
1167 return;
1168
1169 --sp->unsync_children;
1170 WARN_ON((int)sp->unsync_children < 0);
1171 __clear_bit(idx, sp->unsync_child_bitmap);
1172 level++;
1173 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1174}
1175
1176static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1177 struct mmu_page_path *parents,
1178 struct kvm_mmu_pages *pvec)
1179{
1180 parents->parent[parent->role.level-1] = NULL;
1181 pvec->nr = 0;
1182}
1183
1184static void mmu_sync_children(struct kvm_vcpu *vcpu,
1185 struct kvm_mmu_page *parent)
1186{
1187 int i;
1188 struct kvm_mmu_page *sp;
1189 struct mmu_page_path parents;
1190 struct kvm_mmu_pages pages;
1191
1192 kvm_mmu_pages_init(parent, &parents, &pages);
1193 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001194 int protected = 0;
1195
1196 for_each_sp(pages, sp, parents, i)
1197 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1198
1199 if (protected)
1200 kvm_flush_remote_tlbs(vcpu->kvm);
1201
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001202 for_each_sp(pages, sp, parents, i) {
1203 kvm_sync_page(vcpu, sp);
1204 mmu_pages_clear_parents(&parents);
1205 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001206 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001207 kvm_mmu_pages_init(parent, &parents, &pages);
1208 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209}
1210
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001211static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1212 gfn_t gfn,
1213 gva_t gaddr,
1214 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001215 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001216 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001217 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001218{
1219 union kvm_mmu_page_role role;
1220 unsigned index;
1221 unsigned quadrant;
1222 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001223 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001224 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001225
Avi Kivitya770f6f2008-12-21 19:20:09 +02001226 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001227 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001228 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001229 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001230 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001231 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1232 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1233 role.quadrant = quadrant;
1234 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001235 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001236 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001237 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001238 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001239 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1240 if (sp->gfn == gfn) {
1241 if (sp->unsync)
1242 if (kvm_sync_page(vcpu, sp))
1243 continue;
1244
1245 if (sp->role.word != role.word)
1246 continue;
1247
Avi Kivity4db35312007-11-21 15:28:32 +02001248 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001249 if (sp->unsync_children) {
1250 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1251 kvm_mmu_mark_parents_unsync(vcpu, sp);
1252 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001253 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001254 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001255 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001256 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001257 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1258 if (!sp)
1259 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001260 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001261 sp->gfn = gfn;
1262 sp->role = role;
1263 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001264 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001265 if (rmap_write_protect(vcpu->kvm, gfn))
1266 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001267 account_shadowed(vcpu->kvm, gfn);
1268 }
Avi Kivity131d8272008-05-29 14:56:28 +03001269 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1270 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1271 else
1272 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001273 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001274}
1275
Avi Kivity2d111232008-12-25 14:39:47 +02001276static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1277 struct kvm_vcpu *vcpu, u64 addr)
1278{
1279 iterator->addr = addr;
1280 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1281 iterator->level = vcpu->arch.mmu.shadow_root_level;
1282 if (iterator->level == PT32E_ROOT_LEVEL) {
1283 iterator->shadow_addr
1284 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1285 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1286 --iterator->level;
1287 if (!iterator->shadow_addr)
1288 iterator->level = 0;
1289 }
1290}
1291
1292static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1293{
1294 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1295 return false;
1296 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1297 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1298 return true;
1299}
1300
1301static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1302{
1303 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1304 --iterator->level;
1305}
1306
Avi Kivity90cb0522007-07-17 13:04:56 +03001307static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001308 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001309{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001310 unsigned i;
1311 u64 *pt;
1312 u64 ent;
1313
Avi Kivity4db35312007-11-21 15:28:32 +02001314 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001315
Avi Kivity4db35312007-11-21 15:28:32 +02001316 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001317 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001318 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001319 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001320 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001321 }
1322 return;
1323 }
1324
1325 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1326 ent = pt[i];
1327
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001328 if (is_shadow_present_pte(ent)) {
1329 if (!is_large_pte(ent)) {
1330 ent &= PT64_BASE_ADDR_MASK;
1331 mmu_page_remove_parent_pte(page_header(ent),
1332 &pt[i]);
1333 } else {
1334 --kvm->stat.lpages;
1335 rmap_remove(kvm, &pt[i]);
1336 }
1337 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001338 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001339 }
Avi Kivitya4360362007-01-05 16:36:45 -08001340}
1341
Avi Kivity4db35312007-11-21 15:28:32 +02001342static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001343{
Avi Kivity4db35312007-11-21 15:28:32 +02001344 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001345}
1346
Avi Kivity12b7d282007-09-23 14:10:49 +02001347static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1348{
1349 int i;
1350
1351 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1352 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001353 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001354}
1355
Avi Kivity31aa2b42008-07-11 17:59:46 +03001356static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001357{
1358 u64 *parent_pte;
1359
Avi Kivity4db35312007-11-21 15:28:32 +02001360 while (sp->multimapped || sp->parent_pte) {
1361 if (!sp->multimapped)
1362 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001363 else {
1364 struct kvm_pte_chain *chain;
1365
Avi Kivity4db35312007-11-21 15:28:32 +02001366 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001367 struct kvm_pte_chain, link);
1368 parent_pte = chain->parent_ptes[0];
1369 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001370 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001371 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001372 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001373 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001374}
1375
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001376static int mmu_zap_unsync_children(struct kvm *kvm,
1377 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001378{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001379 int i, zapped = 0;
1380 struct mmu_page_path parents;
1381 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001382
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001383 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001384 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001385
1386 kvm_mmu_pages_init(parent, &parents, &pages);
1387 while (mmu_unsync_walk(parent, &pages)) {
1388 struct kvm_mmu_page *sp;
1389
1390 for_each_sp(pages, sp, parents, i) {
1391 kvm_mmu_zap_page(kvm, sp);
1392 mmu_pages_clear_parents(&parents);
1393 }
1394 zapped += pages.nr;
1395 kvm_mmu_pages_init(parent, &parents, &pages);
1396 }
1397
1398 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001399}
1400
Marcelo Tosatti07385412008-09-23 13:18:37 -03001401static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001402{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001403 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001404 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001405 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001406 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001407 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001408 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001409 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001410 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001411 if (sp->unsync)
1412 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001413 if (!sp->root_count) {
1414 hlist_del(&sp->hash_link);
1415 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001416 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001417 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001418 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001419 kvm_reload_remote_mmus(kvm);
1420 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001421 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001422 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001423}
1424
Izik Eidus82ce2c92007-10-02 18:52:55 +02001425/*
1426 * Changing the number of mmu pages allocated to the vm
1427 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1428 */
1429void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1430{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001431 int used_pages;
1432
1433 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1434 used_pages = max(0, used_pages);
1435
Izik Eidus82ce2c92007-10-02 18:52:55 +02001436 /*
1437 * If we set the number of mmu pages to be smaller be than the
1438 * number of actived pages , we must to free some mmu pages before we
1439 * change the value
1440 */
1441
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001442 if (used_pages > kvm_nr_mmu_pages) {
1443 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001444 struct kvm_mmu_page *page;
1445
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001446 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001447 struct kvm_mmu_page, link);
1448 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001449 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001450 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001451 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001452 }
1453 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001454 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1455 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001456
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001457 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001458}
1459
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001460static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001461{
1462 unsigned index;
1463 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001464 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001465 struct hlist_node *node, *n;
1466 int r;
1467
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001468 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001469 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001470 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001471 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001472 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001473 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001474 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001475 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001476 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001477 if (kvm_mmu_zap_page(kvm, sp))
1478 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001479 }
1480 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001481}
1482
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001483static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001484{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001485 unsigned index;
1486 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001487 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001488 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001489
Avi Kivity4677a3b2009-01-06 13:00:27 +02001490 index = kvm_page_table_hashfn(gfn);
1491 bucket = &kvm->arch.mmu_page_hash[index];
1492 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001493 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001494 && !sp->role.invalid) {
1495 pgprintk("%s: zap %lx %x\n",
1496 __func__, gfn, sp->role.word);
1497 kvm_mmu_zap_page(kvm, sp);
1498 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001499 }
1500}
1501
Avi Kivity38c335f2007-11-21 14:20:22 +02001502static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503{
Avi Kivity38c335f2007-11-21 14:20:22 +02001504 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001505 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
Sheng Yang291f26b2008-10-16 17:30:57 +08001507 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508}
1509
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001510static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1511{
1512 int i;
1513 u64 *pt = sp->spt;
1514
1515 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1516 return;
1517
1518 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1519 if (pt[i] == shadow_notrap_nonpresent_pte)
1520 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1521 }
1522}
1523
Avi Kivity039576c2007-03-20 12:46:50 +02001524struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1525{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001526 struct page *page;
1527
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001528 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001529
1530 if (gpa == UNMAPPED_GVA)
1531 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001532
Izik Eidus72dc67a2008-02-10 18:04:15 +02001533 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001534
1535 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001536}
1537
Sheng Yang74be52e2008-10-09 16:01:56 +08001538/*
1539 * The function is based on mtrr_type_lookup() in
1540 * arch/x86/kernel/cpu/mtrr/generic.c
1541 */
1542static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1543 u64 start, u64 end)
1544{
1545 int i;
1546 u64 base, mask;
1547 u8 prev_match, curr_match;
1548 int num_var_ranges = KVM_NR_VAR_MTRR;
1549
1550 if (!mtrr_state->enabled)
1551 return 0xFF;
1552
1553 /* Make end inclusive end, instead of exclusive */
1554 end--;
1555
1556 /* Look in fixed ranges. Just return the type as per start */
1557 if (mtrr_state->have_fixed && (start < 0x100000)) {
1558 int idx;
1559
1560 if (start < 0x80000) {
1561 idx = 0;
1562 idx += (start >> 16);
1563 return mtrr_state->fixed_ranges[idx];
1564 } else if (start < 0xC0000) {
1565 idx = 1 * 8;
1566 idx += ((start - 0x80000) >> 14);
1567 return mtrr_state->fixed_ranges[idx];
1568 } else if (start < 0x1000000) {
1569 idx = 3 * 8;
1570 idx += ((start - 0xC0000) >> 12);
1571 return mtrr_state->fixed_ranges[idx];
1572 }
1573 }
1574
1575 /*
1576 * Look in variable ranges
1577 * Look of multiple ranges matching this address and pick type
1578 * as per MTRR precedence
1579 */
1580 if (!(mtrr_state->enabled & 2))
1581 return mtrr_state->def_type;
1582
1583 prev_match = 0xFF;
1584 for (i = 0; i < num_var_ranges; ++i) {
1585 unsigned short start_state, end_state;
1586
1587 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1588 continue;
1589
1590 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1591 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1592 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1593 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1594
1595 start_state = ((start & mask) == (base & mask));
1596 end_state = ((end & mask) == (base & mask));
1597 if (start_state != end_state)
1598 return 0xFE;
1599
1600 if ((start & mask) != (base & mask))
1601 continue;
1602
1603 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1604 if (prev_match == 0xFF) {
1605 prev_match = curr_match;
1606 continue;
1607 }
1608
1609 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1610 curr_match == MTRR_TYPE_UNCACHABLE)
1611 return MTRR_TYPE_UNCACHABLE;
1612
1613 if ((prev_match == MTRR_TYPE_WRBACK &&
1614 curr_match == MTRR_TYPE_WRTHROUGH) ||
1615 (prev_match == MTRR_TYPE_WRTHROUGH &&
1616 curr_match == MTRR_TYPE_WRBACK)) {
1617 prev_match = MTRR_TYPE_WRTHROUGH;
1618 curr_match = MTRR_TYPE_WRTHROUGH;
1619 }
1620
1621 if (prev_match != curr_match)
1622 return MTRR_TYPE_UNCACHABLE;
1623 }
1624
1625 if (prev_match != 0xFF)
1626 return prev_match;
1627
1628 return mtrr_state->def_type;
1629}
1630
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001631u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001632{
1633 u8 mtrr;
1634
1635 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1636 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1637 if (mtrr == 0xfe || mtrr == 0xff)
1638 mtrr = MTRR_TYPE_WRBACK;
1639 return mtrr;
1640}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001641EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001642
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001643static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1644{
1645 unsigned index;
1646 struct hlist_head *bucket;
1647 struct kvm_mmu_page *s;
1648 struct hlist_node *node, *n;
1649
1650 index = kvm_page_table_hashfn(sp->gfn);
1651 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1652 /* don't unsync if pagetable is shadowed with multiple roles */
1653 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001654 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655 continue;
1656 if (s->role.word != sp->role.word)
1657 return 1;
1658 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001659 ++vcpu->kvm->stat.mmu_unsync;
1660 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001661
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001662 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001663
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001664 mmu_convert_notrap(sp);
1665 return 0;
1666}
1667
1668static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1669 bool can_unsync)
1670{
1671 struct kvm_mmu_page *shadow;
1672
1673 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1674 if (shadow) {
1675 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1676 return 1;
1677 if (shadow->unsync)
1678 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001679 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001680 return kvm_unsync_page(vcpu, shadow);
1681 return 1;
1682 }
1683 return 0;
1684}
1685
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001686static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1687 unsigned pte_access, int user_fault,
1688 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001689 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001690 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001691{
1692 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001693 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001694
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001695 /*
1696 * We don't set the accessed bit, since we sometimes want to see
1697 * whether the guest actually used the pte (in order to detect
1698 * demand paging).
1699 */
Sheng Yang7b523452008-04-25 21:13:50 +08001700 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001701 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001702 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001703 if (!dirty)
1704 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001705 if (pte_access & ACC_EXEC_MASK)
1706 spte |= shadow_x_mask;
1707 else
1708 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001709 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001710 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001711 if (largepage)
1712 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001713 if (tdp_enabled)
1714 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1715 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001716
Anthony Liguori35149e22008-04-02 14:46:56 -05001717 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001718
1719 if ((pte_access & ACC_WRITE_MASK)
1720 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001721
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001722 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1723 ret = 1;
1724 spte = shadow_trap_nonpresent_pte;
1725 goto set_pte;
1726 }
1727
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001728 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001729
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001730 /*
1731 * Optimization: for pte sync, if spte was writable the hash
1732 * lookup is unnecessary (and expensive). Write protection
1733 * is responsibility of mmu_get_page / kvm_sync_page.
1734 * Same reasoning can be applied to dirty page accounting.
1735 */
1736 if (!can_unsync && is_writeble_pte(*shadow_pte))
1737 goto set_pte;
1738
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001739 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001741 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001742 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001743 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001744 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001745 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001746 }
1747 }
1748
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001749 if (pte_access & ACC_WRITE_MASK)
1750 mark_page_dirty(vcpu->kvm, gfn);
1751
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001752set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001753 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001754 return ret;
1755}
1756
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001757static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1758 unsigned pt_access, unsigned pte_access,
1759 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001760 int *ptwrite, int largepage, gfn_t gfn,
1761 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001762{
1763 int was_rmapped = 0;
1764 int was_writeble = is_writeble_pte(*shadow_pte);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001765 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001766
1767 pgprintk("%s: spte %llx access %x write_fault %d"
1768 " user_fault %d gfn %lx\n",
1769 __func__, *shadow_pte, pt_access,
1770 write_fault, user_fault, gfn);
1771
1772 if (is_rmap_pte(*shadow_pte)) {
1773 /*
1774 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1775 * the parent of the now unreachable PTE.
1776 */
1777 if (largepage && !is_large_pte(*shadow_pte)) {
1778 struct kvm_mmu_page *child;
1779 u64 pte = *shadow_pte;
1780
1781 child = page_header(pte & PT64_BASE_ADDR_MASK);
1782 mmu_page_remove_parent_pte(child, shadow_pte);
1783 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1784 pgprintk("hfn old %lx new %lx\n",
1785 spte_to_pfn(*shadow_pte), pfn);
1786 rmap_remove(vcpu->kvm, shadow_pte);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001787 } else
1788 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001789 }
1790 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001791 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001792 if (write_fault)
1793 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001794 kvm_x86_ops->tlb_flush(vcpu);
1795 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001796
1797 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1798 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1799 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1800 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1801 *shadow_pte, shadow_pte);
1802 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001803 ++vcpu->kvm->stat.lpages;
1804
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001805 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1806 if (!was_rmapped) {
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001807 rmap_count = rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001808 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001809 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001810 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1811 rmap_recycle(vcpu, gfn, largepage);
Izik Eidus75e68e62008-01-12 23:49:09 +02001812 } else {
1813 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001814 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001815 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001816 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001818 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001819 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001820 vcpu->arch.last_pte_gfn = gfn;
1821 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001822}
1823
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1825{
1826}
1827
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001828static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001829 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830{
Avi Kivity9f652d22008-12-25 14:54:25 +02001831 struct kvm_shadow_walk_iterator iterator;
1832 struct kvm_mmu_page *sp;
1833 int pt_write = 0;
1834 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835
Avi Kivity9f652d22008-12-25 14:54:25 +02001836 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1837 if (iterator.level == PT_PAGE_TABLE_LEVEL
1838 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1839 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1840 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001841 largepage, gfn, pfn, false);
Avi Kivity9f652d22008-12-25 14:54:25 +02001842 ++vcpu->stat.pf_fixed;
1843 break;
1844 }
1845
1846 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1847 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1848 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1849 iterator.level - 1,
1850 1, ACC_ALL, iterator.sptep);
1851 if (!sp) {
1852 pgprintk("nonpaging_map: ENOMEM\n");
1853 kvm_release_pfn_clean(pfn);
1854 return -ENOMEM;
1855 }
1856
1857 set_shadow_pte(iterator.sptep,
1858 __pa(sp->spt)
1859 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1860 | shadow_user_mask | shadow_x_mask);
1861 }
1862 }
1863 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864}
1865
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001866static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1867{
1868 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001869 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001870 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001871 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001872
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001873 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1874 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1875 largepage = 1;
1876 }
1877
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001878 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001879 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001880 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001881
Avi Kivityd196e342008-01-24 11:44:11 +02001882 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001883 if (is_error_pfn(pfn)) {
1884 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001885 return 1;
1886 }
1887
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001888 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001889 if (mmu_notifier_retry(vcpu, mmu_seq))
1890 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001891 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001892 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001893 spin_unlock(&vcpu->kvm->mmu_lock);
1894
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001895
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001896 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001897
1898out_unlock:
1899 spin_unlock(&vcpu->kvm->mmu_lock);
1900 kvm_release_pfn_clean(pfn);
1901 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001902}
1903
1904
Avi Kivity17ac10a2007-01-05 16:36:40 -08001905static void mmu_free_roots(struct kvm_vcpu *vcpu)
1906{
1907 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001908 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001909
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001910 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001911 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001912 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001913 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1914 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001915
Avi Kivity4db35312007-11-21 15:28:32 +02001916 sp = page_header(root);
1917 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001918 if (!sp->root_count && sp->role.invalid)
1919 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001920 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001921 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001922 return;
1923 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001924 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001925 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001926
Avi Kivity417726a2007-04-12 17:35:58 +03001927 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001928 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001929 sp = page_header(root);
1930 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001931 if (!sp->root_count && sp->role.invalid)
1932 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001933 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001934 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001935 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001936 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001937 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001938}
1939
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001940static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1941{
1942 int ret = 0;
1943
1944 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1945 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1946 ret = 1;
1947 }
1948
1949 return ret;
1950}
1951
1952static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08001953{
1954 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001955 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001956 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001957 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001958 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001959
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001960 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001961
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001962 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1963 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001964
1965 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001966 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001967 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001968 if (mmu_check_root(vcpu, root_gfn))
1969 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001970 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001971 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001972 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001973 root = __pa(sp->spt);
1974 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001975 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001976 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001977 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001978 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001979 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001980 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001981 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001982 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001983
1984 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001985 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001986 pdptr = kvm_pdptr_read(vcpu, i);
1987 if (!is_present_pte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001988 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001989 continue;
1990 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001991 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001992 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001993 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001994 if (mmu_check_root(vcpu, root_gfn))
1995 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001996 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001997 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001998 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001999 root = __pa(sp->spt);
2000 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002001 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002002 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002003 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002004 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002005}
2006
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002007static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2008{
2009 int i;
2010 struct kvm_mmu_page *sp;
2011
2012 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2013 return;
2014 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2015 hpa_t root = vcpu->arch.mmu.root_hpa;
2016 sp = page_header(root);
2017 mmu_sync_children(vcpu, sp);
2018 return;
2019 }
2020 for (i = 0; i < 4; ++i) {
2021 hpa_t root = vcpu->arch.mmu.pae_root[i];
2022
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002023 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002024 root &= PT64_BASE_ADDR_MASK;
2025 sp = page_header(root);
2026 mmu_sync_children(vcpu, sp);
2027 }
2028 }
2029}
2030
2031void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2032{
2033 spin_lock(&vcpu->kvm->mmu_lock);
2034 mmu_sync_roots(vcpu);
2035 spin_unlock(&vcpu->kvm->mmu_lock);
2036}
2037
Avi Kivity6aa8b732006-12-10 02:21:36 -08002038static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2039{
2040 return vaddr;
2041}
2042
2043static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002044 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045{
Avi Kivitye8332402007-12-09 18:43:00 +02002046 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002047 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002049 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002050 r = mmu_topup_memory_caches(vcpu);
2051 if (r)
2052 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002053
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002055 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056
Avi Kivitye8332402007-12-09 18:43:00 +02002057 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058
Avi Kivitye8332402007-12-09 18:43:00 +02002059 return nonpaging_map(vcpu, gva & PAGE_MASK,
2060 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061}
2062
Joerg Roedelfb72d162008-02-07 13:47:44 +01002063static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2064 u32 error_code)
2065{
Anthony Liguori35149e22008-04-02 14:46:56 -05002066 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002067 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002068 int largepage = 0;
2069 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002070 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002071
2072 ASSERT(vcpu);
2073 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2074
2075 r = mmu_topup_memory_caches(vcpu);
2076 if (r)
2077 return r;
2078
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002079 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2080 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2081 largepage = 1;
2082 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002083 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002084 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002085 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002086 if (is_error_pfn(pfn)) {
2087 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002088 return 1;
2089 }
2090 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002091 if (mmu_notifier_retry(vcpu, mmu_seq))
2092 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002093 kvm_mmu_free_some_pages(vcpu);
2094 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002095 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002096 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002097
2098 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002099
2100out_unlock:
2101 spin_unlock(&vcpu->kvm->mmu_lock);
2102 kvm_release_pfn_clean(pfn);
2103 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002104}
2105
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106static void nonpaging_free(struct kvm_vcpu *vcpu)
2107{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002108 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109}
2110
2111static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2112{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114
2115 context->new_cr3 = nonpaging_new_cr3;
2116 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 context->gva_to_gpa = nonpaging_gva_to_gpa;
2118 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002119 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002120 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002121 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002122 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002124 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 return 0;
2126}
2127
Avi Kivityd835dfe2007-11-21 02:57:59 +02002128void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002130 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002131 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132}
2133
2134static void paging_new_cr3(struct kvm_vcpu *vcpu)
2135{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002136 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002137 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138}
2139
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140static void inject_page_fault(struct kvm_vcpu *vcpu,
2141 u64 addr,
2142 u32 err_code)
2143{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002144 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145}
2146
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147static void paging_free(struct kvm_vcpu *vcpu)
2148{
2149 nonpaging_free(vcpu);
2150}
2151
Dong, Eddie82725b22009-03-30 16:21:08 +08002152static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2153{
2154 int bit7;
2155
2156 bit7 = (gpte >> 7) & 1;
2157 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2158}
2159
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160#define PTTYPE 64
2161#include "paging_tmpl.h"
2162#undef PTTYPE
2163
2164#define PTTYPE 32
2165#include "paging_tmpl.h"
2166#undef PTTYPE
2167
Dong, Eddie82725b22009-03-30 16:21:08 +08002168static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2169{
2170 struct kvm_mmu *context = &vcpu->arch.mmu;
2171 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2172 u64 exb_bit_rsvd = 0;
2173
2174 if (!is_nx(vcpu))
2175 exb_bit_rsvd = rsvd_bits(63, 63);
2176 switch (level) {
2177 case PT32_ROOT_LEVEL:
2178 /* no rsvd bits for 2 level 4K page table entries */
2179 context->rsvd_bits_mask[0][1] = 0;
2180 context->rsvd_bits_mask[0][0] = 0;
2181 if (is_cpuid_PSE36())
2182 /* 36bits PSE 4MB page */
2183 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2184 else
2185 /* 32 bits PSE 4MB page */
2186 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002187 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002188 break;
2189 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002190 context->rsvd_bits_mask[0][2] =
2191 rsvd_bits(maxphyaddr, 63) |
2192 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002193 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002194 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002195 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2196 rsvd_bits(maxphyaddr, 62); /* PTE */
2197 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2198 rsvd_bits(maxphyaddr, 62) |
2199 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002200 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002201 break;
2202 case PT64_ROOT_LEVEL:
2203 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2204 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2205 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2206 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2207 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002208 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002209 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2210 rsvd_bits(maxphyaddr, 51);
2211 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2212 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2213 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002214 rsvd_bits(maxphyaddr, 51) |
2215 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002216 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002217 break;
2218 }
2219}
2220
Avi Kivity17ac10a2007-01-05 16:36:40 -08002221static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002223 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224
2225 ASSERT(is_pae(vcpu));
2226 context->new_cr3 = paging_new_cr3;
2227 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002229 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002230 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002231 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002233 context->root_level = level;
2234 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002235 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 return 0;
2237}
2238
Avi Kivity17ac10a2007-01-05 16:36:40 -08002239static int paging64_init_context(struct kvm_vcpu *vcpu)
2240{
Dong, Eddie82725b22009-03-30 16:21:08 +08002241 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002242 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2243}
2244
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245static int paging32_init_context(struct kvm_vcpu *vcpu)
2246{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002247 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248
Dong, Eddie82725b22009-03-30 16:21:08 +08002249 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 context->new_cr3 = paging_new_cr3;
2251 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 context->gva_to_gpa = paging32_gva_to_gpa;
2253 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002254 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002255 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002256 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 context->root_level = PT32_ROOT_LEVEL;
2258 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002259 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 return 0;
2261}
2262
2263static int paging32E_init_context(struct kvm_vcpu *vcpu)
2264{
Dong, Eddie82725b22009-03-30 16:21:08 +08002265 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002266 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267}
2268
Joerg Roedelfb72d162008-02-07 13:47:44 +01002269static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2270{
2271 struct kvm_mmu *context = &vcpu->arch.mmu;
2272
2273 context->new_cr3 = nonpaging_new_cr3;
2274 context->page_fault = tdp_page_fault;
2275 context->free = nonpaging_free;
2276 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002277 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002278 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002279 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002280 context->root_hpa = INVALID_PAGE;
2281
2282 if (!is_paging(vcpu)) {
2283 context->gva_to_gpa = nonpaging_gva_to_gpa;
2284 context->root_level = 0;
2285 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002286 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002287 context->gva_to_gpa = paging64_gva_to_gpa;
2288 context->root_level = PT64_ROOT_LEVEL;
2289 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002290 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002291 context->gva_to_gpa = paging64_gva_to_gpa;
2292 context->root_level = PT32E_ROOT_LEVEL;
2293 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002294 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002295 context->gva_to_gpa = paging32_gva_to_gpa;
2296 context->root_level = PT32_ROOT_LEVEL;
2297 }
2298
2299 return 0;
2300}
2301
2302static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002304 int r;
2305
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002307 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308
2309 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002310 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002311 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002312 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002314 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002316 r = paging32_init_context(vcpu);
2317
2318 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2319
2320 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321}
2322
Joerg Roedelfb72d162008-02-07 13:47:44 +01002323static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2324{
Anthony Liguori35149e22008-04-02 14:46:56 -05002325 vcpu->arch.update_pte.pfn = bad_pfn;
2326
Joerg Roedelfb72d162008-02-07 13:47:44 +01002327 if (tdp_enabled)
2328 return init_kvm_tdp_mmu(vcpu);
2329 else
2330 return init_kvm_softmmu(vcpu);
2331}
2332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2334{
2335 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002336 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2337 vcpu->arch.mmu.free(vcpu);
2338 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 }
2340}
2341
2342int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2343{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002344 destroy_kvm_mmu(vcpu);
2345 return init_kvm_mmu(vcpu);
2346}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002347EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002348
2349int kvm_mmu_load(struct kvm_vcpu *vcpu)
2350{
Avi Kivity714b93d2007-01-05 16:36:53 -08002351 int r;
2352
Avi Kivitye2dec932007-01-05 16:36:54 -08002353 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002354 if (r)
2355 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002356 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002357 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002358 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002359 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002360 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002361 if (r)
2362 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002363 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002364 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002365out:
2366 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002368EXPORT_SYMBOL_GPL(kvm_mmu_load);
2369
2370void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2371{
2372 mmu_free_roots(vcpu);
2373}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
Avi Kivity09072da2007-05-01 14:16:52 +03002375static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002376 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002377 u64 *spte)
2378{
2379 u64 pte;
2380 struct kvm_mmu_page *child;
2381
2382 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002383 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002384 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2385 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002386 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002387 else {
2388 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002389 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002390 }
2391 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002392 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002393 if (is_large_pte(pte))
2394 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002395}
2396
Avi Kivity00284252007-05-01 16:53:31 +03002397static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002398 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002399 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002400 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002401{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002402 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2403 if (!vcpu->arch.update_pte.largepage ||
2404 sp->role.glevels == PT32_ROOT_LEVEL) {
2405 ++vcpu->kvm->stat.mmu_pde_zapped;
2406 return;
2407 }
2408 }
Avi Kivity00284252007-05-01 16:53:31 +03002409
Avi Kivity4cee5762007-11-18 16:37:07 +02002410 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002411 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002412 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002413 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002414 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002415}
2416
Avi Kivity79539ce2007-11-21 02:06:21 +02002417static bool need_remote_flush(u64 old, u64 new)
2418{
2419 if (!is_shadow_present_pte(old))
2420 return false;
2421 if (!is_shadow_present_pte(new))
2422 return true;
2423 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2424 return true;
2425 old ^= PT64_NX_MASK;
2426 new ^= PT64_NX_MASK;
2427 return (old & ~new & PT64_PERM_MASK) != 0;
2428}
2429
2430static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2431{
2432 if (need_remote_flush(old, new))
2433 kvm_flush_remote_tlbs(vcpu->kvm);
2434 else
2435 kvm_mmu_flush_tlb(vcpu);
2436}
2437
Avi Kivity12b7d282007-09-23 14:10:49 +02002438static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2439{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002441
Sheng Yang7b523452008-04-25 21:13:50 +08002442 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002443}
2444
Avi Kivityd7824ff2007-12-30 12:29:05 +02002445static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2446 const u8 *new, int bytes)
2447{
2448 gfn_t gfn;
2449 int r;
2450 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002451 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002452
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002453 vcpu->arch.update_pte.largepage = 0;
2454
Avi Kivityd7824ff2007-12-30 12:29:05 +02002455 if (bytes != 4 && bytes != 8)
2456 return;
2457
2458 /*
2459 * Assume that the pte write on a page table of the same type
2460 * as the current vcpu paging mode. This is nearly always true
2461 * (might be false while changing modes). Note it is verified later
2462 * by update_pte().
2463 */
2464 if (is_pae(vcpu)) {
2465 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2466 if ((bytes == 4) && (gpa % 4 == 0)) {
2467 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2468 if (r)
2469 return;
2470 memcpy((void *)&gpte + (gpa % 8), new, 4);
2471 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2472 memcpy((void *)&gpte, new, 8);
2473 }
2474 } else {
2475 if ((bytes == 4) && (gpa % 4 == 0))
2476 memcpy((void *)&gpte, new, 4);
2477 }
2478 if (!is_present_pte(gpte))
2479 return;
2480 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002481
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002482 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2483 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2484 vcpu->arch.update_pte.largepage = 1;
2485 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002486 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002487 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002488 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002489
Anthony Liguori35149e22008-04-02 14:46:56 -05002490 if (is_error_pfn(pfn)) {
2491 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002492 return;
2493 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002494 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002495 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002496}
2497
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002498static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2499{
2500 u64 *spte = vcpu->arch.last_pte_updated;
2501
2502 if (spte
2503 && vcpu->arch.last_pte_gfn == gfn
2504 && shadow_accessed_mask
2505 && !(*spte & shadow_accessed_mask)
2506 && is_shadow_present_pte(*spte))
2507 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2508}
2509
Avi Kivity09072da2007-05-01 14:16:52 +03002510void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002511 const u8 *new, int bytes,
2512 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002513{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002514 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002515 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002516 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002517 struct hlist_head *bucket;
2518 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002519 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002520 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002521 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002522 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002523 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002524 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002525 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002526 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002527 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002528 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002529 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002530
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002531 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002532 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002533 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002534 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002535 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002536 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002537 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002538 if (guest_initiated) {
2539 if (gfn == vcpu->arch.last_pt_write_gfn
2540 && !last_updated_pte_accessed(vcpu)) {
2541 ++vcpu->arch.last_pt_write_count;
2542 if (vcpu->arch.last_pt_write_count >= 3)
2543 flooded = 1;
2544 } else {
2545 vcpu->arch.last_pt_write_gfn = gfn;
2546 vcpu->arch.last_pt_write_count = 1;
2547 vcpu->arch.last_pte_updated = NULL;
2548 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002549 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002550 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002551 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002552 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002553 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002554 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002555 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002556 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002557 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002558 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002559 /*
2560 * Misaligned accesses are too much trouble to fix
2561 * up; also, they usually indicate a page is not used
2562 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002563 *
2564 * If we're seeing too many writes to a page,
2565 * it may no longer be a page table, or we may be
2566 * forking, in which case it is better to unmap the
2567 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002568 */
2569 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002570 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002571 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2572 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002573 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002574 continue;
2575 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002576 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002577 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002578 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002579 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002580 page_offset <<= 1; /* 32->64 */
2581 /*
2582 * A 32-bit pde maps 4MB while the shadow pdes map
2583 * only 2MB. So we need to double the offset again
2584 * and zap two pdes instead of one.
2585 */
2586 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002587 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002588 page_offset <<= 1;
2589 npte = 2;
2590 }
Avi Kivityfce06572007-05-01 16:44:05 +03002591 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002592 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002593 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002594 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002595 }
Avi Kivity4db35312007-11-21 15:28:32 +02002596 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002597 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2598 gentry = 0;
2599 r = kvm_read_guest_atomic(vcpu->kvm,
2600 gpa & ~(u64)(pte_size - 1),
2601 &gentry, pte_size);
2602 new = (const void *)&gentry;
2603 if (r < 0)
2604 new = NULL;
2605 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002606 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002607 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002608 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002609 if (new)
2610 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002611 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002612 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002613 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002614 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002615 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002616 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002617 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2618 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2619 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002620 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002621}
2622
Avi Kivitya4360362007-01-05 16:36:45 -08002623int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2624{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002625 gpa_t gpa;
2626 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002627
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002628 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002629
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002630 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002631 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002632 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002633 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002634}
Avi Kivity577bdc42008-07-19 08:57:05 +03002635EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002636
Avi Kivity22d95b12007-09-14 20:26:06 +03002637void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002638{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002639 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002640 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002641
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002642 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002643 struct kvm_mmu_page, link);
2644 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002645 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002646 }
2647}
Avi Kivityebeace82007-01-05 16:36:47 -08002648
Avi Kivity30677142007-10-28 18:48:59 +02002649int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2650{
2651 int r;
2652 enum emulation_result er;
2653
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002654 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002655 if (r < 0)
2656 goto out;
2657
2658 if (!r) {
2659 r = 1;
2660 goto out;
2661 }
2662
Avi Kivityb733bfb2007-10-28 18:52:05 +02002663 r = mmu_topup_memory_caches(vcpu);
2664 if (r)
2665 goto out;
2666
Avi Kivity30677142007-10-28 18:48:59 +02002667 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002668
2669 switch (er) {
2670 case EMULATE_DONE:
2671 return 1;
2672 case EMULATE_DO_MMIO:
2673 ++vcpu->stat.mmio_exits;
2674 return 0;
2675 case EMULATE_FAIL:
2676 kvm_report_emulation_failure(vcpu, "pagetable");
2677 return 1;
2678 default:
2679 BUG();
2680 }
2681out:
Avi Kivity30677142007-10-28 18:48:59 +02002682 return r;
2683}
2684EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2685
Marcelo Tosattia7052892008-09-23 13:18:35 -03002686void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2687{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002688 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002689 kvm_mmu_flush_tlb(vcpu);
2690 ++vcpu->stat.invlpg;
2691}
2692EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2693
Joerg Roedel18552672008-02-07 13:47:41 +01002694void kvm_enable_tdp(void)
2695{
2696 tdp_enabled = true;
2697}
2698EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2699
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002700void kvm_disable_tdp(void)
2701{
2702 tdp_enabled = false;
2703}
2704EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2705
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706static void free_mmu_pages(struct kvm_vcpu *vcpu)
2707{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002708 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709}
2710
2711static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2712{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002713 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 int i;
2715
2716 ASSERT(vcpu);
2717
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002718 if (vcpu->kvm->arch.n_requested_mmu_pages)
2719 vcpu->kvm->arch.n_free_mmu_pages =
2720 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002721 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002722 vcpu->kvm->arch.n_free_mmu_pages =
2723 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002724 /*
2725 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2726 * Therefore we need to allocate shadow page tables in the first
2727 * 4GB of memory, which happens to fit the DMA32 zone.
2728 */
2729 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2730 if (!page)
2731 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002732 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002733 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002734 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002735
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 return 0;
2737
2738error_1:
2739 free_mmu_pages(vcpu);
2740 return -ENOMEM;
2741}
2742
Ingo Molnar8018c272006-12-29 16:50:01 -08002743int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002746 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747
Ingo Molnar8018c272006-12-29 16:50:01 -08002748 return alloc_mmu_pages(vcpu);
2749}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750
Ingo Molnar8018c272006-12-29 16:50:01 -08002751int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2752{
2753 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002754 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002755
Ingo Molnar8018c272006-12-29 16:50:01 -08002756 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757}
2758
2759void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2760{
2761 ASSERT(vcpu);
2762
2763 destroy_kvm_mmu(vcpu);
2764 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002765 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766}
2767
Avi Kivity90cb0522007-07-17 13:04:56 +03002768void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769{
Avi Kivity4db35312007-11-21 15:28:32 +02002770 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002772 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 int i;
2774 u64 *pt;
2775
Sheng Yang291f26b2008-10-16 17:30:57 +08002776 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 continue;
2778
Avi Kivity4db35312007-11-21 15:28:32 +02002779 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2781 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002782 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 }
Avi Kivity171d5952008-08-27 16:40:51 +03002785 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002787
Avi Kivity90cb0522007-07-17 13:04:56 +03002788void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002789{
Avi Kivity4db35312007-11-21 15:28:32 +02002790 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002791
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002792 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002793 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002794 if (kvm_mmu_zap_page(kvm, sp))
2795 node = container_of(kvm->arch.active_mmu_pages.next,
2796 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002797 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002798
Avi Kivity90cb0522007-07-17 13:04:56 +03002799 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002800}
2801
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002802static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002803{
2804 struct kvm_mmu_page *page;
2805
2806 page = container_of(kvm->arch.active_mmu_pages.prev,
2807 struct kvm_mmu_page, link);
2808 kvm_mmu_zap_page(kvm, page);
2809}
2810
2811static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2812{
2813 struct kvm *kvm;
2814 struct kvm *kvm_freed = NULL;
2815 int cache_count = 0;
2816
2817 spin_lock(&kvm_lock);
2818
2819 list_for_each_entry(kvm, &vm_list, vm_list) {
2820 int npages;
2821
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002822 if (!down_read_trylock(&kvm->slots_lock))
2823 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002824 spin_lock(&kvm->mmu_lock);
2825 npages = kvm->arch.n_alloc_mmu_pages -
2826 kvm->arch.n_free_mmu_pages;
2827 cache_count += npages;
2828 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2829 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2830 cache_count--;
2831 kvm_freed = kvm;
2832 }
2833 nr_to_scan--;
2834
2835 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002836 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002837 }
2838 if (kvm_freed)
2839 list_move_tail(&kvm_freed->vm_list, &vm_list);
2840
2841 spin_unlock(&kvm_lock);
2842
2843 return cache_count;
2844}
2845
2846static struct shrinker mmu_shrinker = {
2847 .shrink = mmu_shrink,
2848 .seeks = DEFAULT_SEEKS * 10,
2849};
2850
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002851static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002852{
2853 if (pte_chain_cache)
2854 kmem_cache_destroy(pte_chain_cache);
2855 if (rmap_desc_cache)
2856 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002857 if (mmu_page_header_cache)
2858 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002859}
2860
Izik Eidus3ee16c82008-03-30 15:17:21 +03002861void kvm_mmu_module_exit(void)
2862{
2863 mmu_destroy_caches();
2864 unregister_shrinker(&mmu_shrinker);
2865}
2866
Avi Kivityb5a33a72007-04-15 16:31:09 +03002867int kvm_mmu_module_init(void)
2868{
2869 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2870 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002871 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002872 if (!pte_chain_cache)
2873 goto nomem;
2874 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2875 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002876 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002877 if (!rmap_desc_cache)
2878 goto nomem;
2879
Avi Kivityd3d25b02007-05-30 12:34:53 +03002880 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2881 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002882 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002883 if (!mmu_page_header_cache)
2884 goto nomem;
2885
Izik Eidus3ee16c82008-03-30 15:17:21 +03002886 register_shrinker(&mmu_shrinker);
2887
Avi Kivityb5a33a72007-04-15 16:31:09 +03002888 return 0;
2889
2890nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002891 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002892 return -ENOMEM;
2893}
2894
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002895/*
2896 * Caculate mmu pages needed for kvm.
2897 */
2898unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2899{
2900 int i;
2901 unsigned int nr_mmu_pages;
2902 unsigned int nr_pages = 0;
2903
2904 for (i = 0; i < kvm->nmemslots; i++)
2905 nr_pages += kvm->memslots[i].npages;
2906
2907 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2908 nr_mmu_pages = max(nr_mmu_pages,
2909 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2910
2911 return nr_mmu_pages;
2912}
2913
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002914static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2915 unsigned len)
2916{
2917 if (len > buffer->len)
2918 return NULL;
2919 return buffer->ptr;
2920}
2921
2922static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2923 unsigned len)
2924{
2925 void *ret;
2926
2927 ret = pv_mmu_peek_buffer(buffer, len);
2928 if (!ret)
2929 return ret;
2930 buffer->ptr += len;
2931 buffer->len -= len;
2932 buffer->processed += len;
2933 return ret;
2934}
2935
2936static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2937 gpa_t addr, gpa_t value)
2938{
2939 int bytes = 8;
2940 int r;
2941
2942 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2943 bytes = 4;
2944
2945 r = mmu_topup_memory_caches(vcpu);
2946 if (r)
2947 return r;
2948
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002949 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002950 return -EFAULT;
2951
2952 return 1;
2953}
2954
2955static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2956{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002957 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002958 return 1;
2959}
2960
2961static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2962{
2963 spin_lock(&vcpu->kvm->mmu_lock);
2964 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2965 spin_unlock(&vcpu->kvm->mmu_lock);
2966 return 1;
2967}
2968
2969static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2970 struct kvm_pv_mmu_op_buffer *buffer)
2971{
2972 struct kvm_mmu_op_header *header;
2973
2974 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2975 if (!header)
2976 return 0;
2977 switch (header->op) {
2978 case KVM_MMU_OP_WRITE_PTE: {
2979 struct kvm_mmu_op_write_pte *wpte;
2980
2981 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2982 if (!wpte)
2983 return 0;
2984 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2985 wpte->pte_val);
2986 }
2987 case KVM_MMU_OP_FLUSH_TLB: {
2988 struct kvm_mmu_op_flush_tlb *ftlb;
2989
2990 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2991 if (!ftlb)
2992 return 0;
2993 return kvm_pv_mmu_flush_tlb(vcpu);
2994 }
2995 case KVM_MMU_OP_RELEASE_PT: {
2996 struct kvm_mmu_op_release_pt *rpt;
2997
2998 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2999 if (!rpt)
3000 return 0;
3001 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3002 }
3003 default: return 0;
3004 }
3005}
3006
3007int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3008 gpa_t addr, unsigned long *ret)
3009{
3010 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003011 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003012
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003013 buffer->ptr = buffer->buf;
3014 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3015 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003016
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003017 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003018 if (r)
3019 goto out;
3020
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003021 while (buffer->len) {
3022 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003023 if (r < 0)
3024 goto out;
3025 if (r == 0)
3026 break;
3027 }
3028
3029 r = 1;
3030out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003031 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003032 return r;
3033}
3034
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003035#ifdef AUDIT
3036
3037static const char *audit_msg;
3038
3039static gva_t canonicalize(gva_t gva)
3040{
3041#ifdef CONFIG_X86_64
3042 gva = (long long)(gva << 16) >> 16;
3043#endif
3044 return gva;
3045}
3046
3047static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3048 gva_t va, int level)
3049{
3050 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3051 int i;
3052 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3053
3054 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3055 u64 ent = pt[i];
3056
Avi Kivityc7addb92007-09-16 18:58:32 +02003057 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003058 continue;
3059
3060 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003061 if (level > 1) {
3062 if (ent == shadow_notrap_nonpresent_pte)
3063 printk(KERN_ERR "audit: (%s) nontrapping pte"
3064 " in nonleaf level: levels %d gva %lx"
3065 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003066 vcpu->arch.mmu.root_level, va, level, ent);
Jan Kiszka34382532009-04-25 12:43:21 +02003067 else
3068 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003069 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003070 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003071 gfn_t gfn = gpa >> PAGE_SHIFT;
3072 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3073 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003074
Avi Kivityc7addb92007-09-16 18:58:32 +02003075 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003076 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003077 printk(KERN_ERR "xx audit error: (%s) levels %d"
3078 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003079 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003080 va, gpa, hpa, ent,
3081 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003082 else if (ent == shadow_notrap_nonpresent_pte
3083 && !is_error_hpa(hpa))
3084 printk(KERN_ERR "audit: (%s) notrap shadow,"
3085 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003086 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003087
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003088 }
3089 }
3090}
3091
3092static void audit_mappings(struct kvm_vcpu *vcpu)
3093{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003094 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003095
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003096 if (vcpu->arch.mmu.root_level == 4)
3097 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003098 else
3099 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003100 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003101 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003102 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003103 i << 30,
3104 2);
3105}
3106
3107static int count_rmaps(struct kvm_vcpu *vcpu)
3108{
3109 int nmaps = 0;
3110 int i, j, k;
3111
3112 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3113 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3114 struct kvm_rmap_desc *d;
3115
3116 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003117 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003118
Izik Eidus290fc382007-09-27 14:11:22 +02003119 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003120 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003121 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003122 ++nmaps;
3123 continue;
3124 }
Izik Eidus290fc382007-09-27 14:11:22 +02003125 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003126 while (d) {
3127 for (k = 0; k < RMAP_EXT; ++k)
3128 if (d->shadow_ptes[k])
3129 ++nmaps;
3130 else
3131 break;
3132 d = d->more;
3133 }
3134 }
3135 }
3136 return nmaps;
3137}
3138
3139static int count_writable_mappings(struct kvm_vcpu *vcpu)
3140{
3141 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003142 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003143 int i;
3144
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003145 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003146 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003147
Avi Kivity4db35312007-11-21 15:28:32 +02003148 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003149 continue;
3150
3151 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3152 u64 ent = pt[i];
3153
3154 if (!(ent & PT_PRESENT_MASK))
3155 continue;
3156 if (!(ent & PT_WRITABLE_MASK))
3157 continue;
3158 ++nmaps;
3159 }
3160 }
3161 return nmaps;
3162}
3163
3164static void audit_rmap(struct kvm_vcpu *vcpu)
3165{
3166 int n_rmap = count_rmaps(vcpu);
3167 int n_actual = count_writable_mappings(vcpu);
3168
3169 if (n_rmap != n_actual)
3170 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003171 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003172}
3173
3174static void audit_write_protection(struct kvm_vcpu *vcpu)
3175{
Avi Kivity4db35312007-11-21 15:28:32 +02003176 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003177 struct kvm_memory_slot *slot;
3178 unsigned long *rmapp;
3179 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003180
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003181 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003182 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003183 continue;
3184
Avi Kivity4db35312007-11-21 15:28:32 +02003185 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003186 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003187 rmapp = &slot->rmap[gfn - slot->base_gfn];
3188 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003189 printk(KERN_ERR "%s: (%s) shadow page has writable"
3190 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003191 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003192 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003193 }
3194}
3195
3196static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3197{
3198 int olddbg = dbg;
3199
3200 dbg = 0;
3201 audit_msg = msg;
3202 audit_rmap(vcpu);
3203 audit_write_protection(vcpu);
3204 audit_mappings(vcpu);
3205 dbg = olddbg;
3206}
3207
3208#endif