blob: f1f08159e11e5663dc21d6e6417c0253d1805602 [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 Kivity07420172009-07-06 12:21:32 +0300143#define CREATE_TRACE_POINTS
144#include "mmutrace.h"
145
Avi Kivity135f8c22008-08-21 17:49:56 +0300146#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
147
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800148struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300149 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800150 struct kvm_rmap_desc *more;
151};
152
Avi Kivity2d111232008-12-25 14:39:47 +0200153struct kvm_shadow_walk_iterator {
154 u64 addr;
155 hpa_t shadow_addr;
156 int level;
157 u64 *sptep;
158 unsigned index;
159};
160
161#define for_each_shadow_entry(_vcpu, _addr, _walker) \
162 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
163 shadow_walk_okay(&(_walker)); \
164 shadow_walk_next(&(_walker)))
165
166
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300167struct kvm_unsync_walk {
168 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
169};
170
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300171typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
172
Avi Kivityb5a33a72007-04-15 16:31:09 +0300173static struct kmem_cache *pte_chain_cache;
174static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300175static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300176
Avi Kivityc7addb92007-09-16 18:58:32 +0200177static u64 __read_mostly shadow_trap_nonpresent_pte;
178static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800179static u64 __read_mostly shadow_base_present_pte;
180static u64 __read_mostly shadow_nx_mask;
181static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
182static u64 __read_mostly shadow_user_mask;
183static u64 __read_mostly shadow_accessed_mask;
184static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200185
Dong, Eddie82725b22009-03-30 16:21:08 +0800186static inline u64 rsvd_bits(int s, int e)
187{
188 return ((1ULL << (e - s + 1)) - 1) << s;
189}
190
Avi Kivityc7addb92007-09-16 18:58:32 +0200191void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
192{
193 shadow_trap_nonpresent_pte = trap_pte;
194 shadow_notrap_nonpresent_pte = notrap_pte;
195}
196EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
197
Sheng Yang7b523452008-04-25 21:13:50 +0800198void kvm_mmu_set_base_ptes(u64 base_pte)
199{
200 shadow_base_present_pte = base_pte;
201}
202EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
203
204void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800205 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800206{
207 shadow_user_mask = user_mask;
208 shadow_accessed_mask = accessed_mask;
209 shadow_dirty_mask = dirty_mask;
210 shadow_nx_mask = nx_mask;
211 shadow_x_mask = x_mask;
212}
213EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
214
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215static int is_write_protection(struct kvm_vcpu *vcpu)
216{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800217 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218}
219
220static int is_cpuid_PSE36(void)
221{
222 return 1;
223}
224
Avi Kivity73b10872007-01-26 00:56:41 -0800225static int is_nx(struct kvm_vcpu *vcpu)
226{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800227 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800228}
229
Avi Kivityc7addb92007-09-16 18:58:32 +0200230static int is_shadow_present_pte(u64 pte)
231{
Avi Kivityc7addb92007-09-16 18:58:32 +0200232 return pte != shadow_trap_nonpresent_pte
233 && pte != shadow_notrap_nonpresent_pte;
234}
235
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300236static int is_large_pte(u64 pte)
237{
238 return pte & PT_PAGE_SIZE_MASK;
239}
240
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241static int is_writeble_pte(unsigned long pte)
242{
243 return pte & PT_WRITABLE_MASK;
244}
245
Avi Kivity43a37952009-06-10 14:12:05 +0300246static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200247{
Avi Kivity439e2182009-06-10 12:56:54 +0300248 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200249}
250
Avi Kivity43a37952009-06-10 14:12:05 +0300251static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800252{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200253 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800254}
255
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300256static int is_last_spte(u64 pte, int level)
257{
258 if (level == PT_PAGE_TABLE_LEVEL)
259 return 1;
260 if (level == PT_DIRECTORY_LEVEL && is_large_pte(pte))
261 return 1;
262 return 0;
263}
264
Anthony Liguori35149e22008-04-02 14:46:56 -0500265static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200266{
Anthony Liguori35149e22008-04-02 14:46:56 -0500267 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200268}
269
Avi Kivityda928522007-11-21 13:54:47 +0200270static gfn_t pse36_gfn_delta(u32 gpte)
271{
272 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
273
274 return (gpte & PT32_DIR_PSE36_MASK) << shift;
275}
276
Avi Kivityd555c332009-06-10 14:24:23 +0300277static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300278{
279#ifdef CONFIG_X86_64
280 set_64bit((unsigned long *)sptep, spte);
281#else
282 set_64bit((unsigned long long *)sptep, spte);
283#endif
284}
285
Avi Kivitye2dec932007-01-05 16:36:54 -0800286static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300287 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800288{
289 void *obj;
290
291 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800292 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800293 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300294 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800295 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800296 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800297 cache->objects[cache->nobjs++] = obj;
298 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800299 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800300}
301
302static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
303{
304 while (mc->nobjs)
305 kfree(mc->objects[--mc->nobjs]);
306}
307
Avi Kivityc1158e62007-07-20 08:18:27 +0300308static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300309 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300310{
311 struct page *page;
312
313 if (cache->nobjs >= min)
314 return 0;
315 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300316 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300317 if (!page)
318 return -ENOMEM;
319 set_page_private(page, 0);
320 cache->objects[cache->nobjs++] = page_address(page);
321 }
322 return 0;
323}
324
325static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
326{
327 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300328 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300329}
330
Avi Kivity8c438502007-04-16 11:53:17 +0300331static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
332{
333 int r;
334
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800335 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300336 pte_chain_cache, 4);
337 if (r)
338 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800339 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200340 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300341 if (r)
342 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300344 if (r)
345 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800346 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300347 mmu_page_header_cache, 4);
348out:
Avi Kivity8c438502007-04-16 11:53:17 +0300349 return r;
350}
351
Avi Kivity714b93d2007-01-05 16:36:53 -0800352static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
353{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800354 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
355 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
356 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
357 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800358}
359
360static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
361 size_t size)
362{
363 void *p;
364
365 BUG_ON(!mc->nobjs);
366 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800367 return p;
368}
369
Avi Kivity714b93d2007-01-05 16:36:53 -0800370static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
371{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800372 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800373 sizeof(struct kvm_pte_chain));
374}
375
Avi Kivity90cb0522007-07-17 13:04:56 +0300376static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800377{
Avi Kivity90cb0522007-07-17 13:04:56 +0300378 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800379}
380
381static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
382{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800383 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800384 sizeof(struct kvm_rmap_desc));
385}
386
Avi Kivity90cb0522007-07-17 13:04:56 +0300387static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800388{
Avi Kivity90cb0522007-07-17 13:04:56 +0300389 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800390}
391
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800392/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300393 * Return the pointer to the largepage write count for a given
394 * gfn, handling slots that are not large page aligned.
395 */
396static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
397{
398 unsigned long idx;
399
Joerg Roedelec04b262009-06-19 15:16:23 +0200400 idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
401 (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
402 return &slot->lpage_info[0][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300403}
404
405static void account_shadowed(struct kvm *kvm, gfn_t gfn)
406{
407 int *write_count;
408
Izik Eidus28430992008-10-03 17:40:32 +0300409 gfn = unalias_gfn(kvm, gfn);
410 write_count = slot_largepage_idx(gfn,
411 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300412 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300413}
414
415static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
416{
417 int *write_count;
418
Izik Eidus28430992008-10-03 17:40:32 +0300419 gfn = unalias_gfn(kvm, gfn);
420 write_count = slot_largepage_idx(gfn,
421 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300422 *write_count -= 1;
423 WARN_ON(*write_count < 0);
424}
425
426static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
427{
Izik Eidus28430992008-10-03 17:40:32 +0300428 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300429 int *largepage_idx;
430
Izik Eidus28430992008-10-03 17:40:32 +0300431 gfn = unalias_gfn(kvm, gfn);
432 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300433 if (slot) {
434 largepage_idx = slot_largepage_idx(gfn, slot);
435 return *largepage_idx;
436 }
437
438 return 1;
439}
440
441static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
442{
443 struct vm_area_struct *vma;
444 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300445 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300446
447 addr = gfn_to_hva(kvm, gfn);
448 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300449 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300451 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452 vma = find_vma(current->mm, addr);
453 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300454 ret = 1;
455 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300456
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300457 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300458}
459
460static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
461{
462 struct kvm_memory_slot *slot;
463
464 if (has_wrprotected_page(vcpu->kvm, large_gfn))
465 return 0;
466
467 if (!host_largepage_backed(vcpu->kvm, large_gfn))
468 return 0;
469
470 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
471 if (slot && slot->dirty_bitmap)
472 return 0;
473
474 return 1;
475}
476
477/*
Izik Eidus290fc382007-09-27 14:11:22 +0200478 * Take gfn and return the reverse mapping to it.
479 * Note: gfn must be unaliased before this function get called
480 */
481
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200483{
484 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300485 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200486
487 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300488 if (!lpage)
489 return &slot->rmap[gfn - slot->base_gfn];
490
Joerg Roedelec04b262009-06-19 15:16:23 +0200491 idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
492 (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493
Joerg Roedelec04b262009-06-19 15:16:23 +0200494 return &slot->lpage_info[0][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200495}
496
497/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498 * Reverse mapping data structures:
499 *
Izik Eidus290fc382007-09-27 14:11:22 +0200500 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
501 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800502 *
Izik Eidus290fc382007-09-27 14:11:22 +0200503 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
504 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300505 *
506 * Returns the number of rmap entries before the spte was added or zero if
507 * the spte was not added.
508 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800509 */
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300510static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800511{
Avi Kivity4db35312007-11-21 15:28:32 +0200512 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800513 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200514 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300515 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800516
Avi Kivity43a37952009-06-10 14:12:05 +0300517 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300518 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200519 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200520 sp = page_header(__pa(spte));
521 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300522 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200523 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200525 *rmapp = (unsigned long)spte;
526 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800528 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300529 desc->sptes[0] = (u64 *)*rmapp;
530 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200531 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800532 } else {
533 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200534 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300535 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800536 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300537 count += RMAP_EXT;
538 }
Avi Kivityd555c332009-06-10 14:24:23 +0300539 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800540 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541 desc = desc->more;
542 }
Avi Kivityd555c332009-06-10 14:24:23 +0300543 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800544 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300545 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300547 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548}
549
Izik Eidus290fc382007-09-27 14:11:22 +0200550static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800551 struct kvm_rmap_desc *desc,
552 int i,
553 struct kvm_rmap_desc *prev_desc)
554{
555 int j;
556
Avi Kivityd555c332009-06-10 14:24:23 +0300557 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300559 desc->sptes[i] = desc->sptes[j];
560 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 if (j != 0)
562 return;
563 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300564 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800565 else
566 if (prev_desc)
567 prev_desc->more = desc->more;
568 else
Izik Eidus290fc382007-09-27 14:11:22 +0200569 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300570 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571}
572
Izik Eidus290fc382007-09-27 14:11:22 +0200573static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800574{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 struct kvm_rmap_desc *desc;
576 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200577 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500578 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200579 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 int i;
581
Avi Kivity43a37952009-06-10 14:12:05 +0300582 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800583 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200584 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500585 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800586 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500587 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200588 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500589 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200590 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500591 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300592 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200593 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
595 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200596 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800597 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200598 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800599 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
600 spte, *spte);
601 BUG();
602 }
Izik Eidus290fc382007-09-27 14:11:22 +0200603 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800604 } else {
605 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200606 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800607 prev_desc = NULL;
608 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300609 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
610 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200611 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800612 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613 prev_desc);
614 return;
615 }
616 prev_desc = desc;
617 desc = desc->more;
618 }
619 BUG();
620 }
621}
622
Izik Eidus98348e92007-10-16 14:42:30 +0200623static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800624{
Avi Kivity374cbac2007-01-05 16:36:43 -0800625 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200626 struct kvm_rmap_desc *prev_desc;
627 u64 *prev_spte;
628 int i;
629
630 if (!*rmapp)
631 return NULL;
632 else if (!(*rmapp & 1)) {
633 if (!spte)
634 return (u64 *)*rmapp;
635 return NULL;
636 }
637 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
638 prev_desc = NULL;
639 prev_spte = NULL;
640 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300641 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200642 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300643 return desc->sptes[i];
644 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200645 }
646 desc = desc->more;
647 }
648 return NULL;
649}
650
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200651static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200652{
Izik Eidus290fc382007-09-27 14:11:22 +0200653 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800654 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800655 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800656
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500657 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300658 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800659
Izik Eidus98348e92007-10-16 14:42:30 +0200660 spte = rmap_next(kvm, rmapp, NULL);
661 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800662 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800663 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800664 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800665 if (is_writeble_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300666 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800667 write_protected = 1;
668 }
Izik Eidus9647c142007-10-16 14:43:46 +0200669 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800670 }
Izik Eidus855149a2008-03-20 18:17:24 +0200671 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500672 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200673
674 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500675 pfn = spte_to_pfn(*spte);
676 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200677 }
678
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300679 /* check for huge page mappings */
680 rmapp = gfn_to_rmap(kvm, gfn, 1);
681 spte = rmap_next(kvm, rmapp, NULL);
682 while (spte) {
683 BUG_ON(!spte);
684 BUG_ON(!(*spte & PT_PRESENT_MASK));
685 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
686 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
687 if (is_writeble_pte(*spte)) {
688 rmap_remove(kvm, spte);
689 --kvm->stat.lpages;
Avi Kivityd555c332009-06-10 14:24:23 +0300690 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300691 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300692 write_protected = 1;
693 }
694 spte = rmap_next(kvm, rmapp, spte);
695 }
696
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200697 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800698}
699
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200700static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
701{
702 u64 *spte;
703 int need_tlb_flush = 0;
704
705 while ((spte = rmap_next(kvm, rmapp, NULL))) {
706 BUG_ON(!(*spte & PT_PRESENT_MASK));
707 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
708 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300709 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200710 need_tlb_flush = 1;
711 }
712 return need_tlb_flush;
713}
714
715static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
716 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
717{
718 int i;
719 int retval = 0;
720
721 /*
722 * If mmap_sem isn't taken, we can look the memslots with only
723 * the mmu_lock by skipping over the slots with userspace_addr == 0.
724 */
725 for (i = 0; i < kvm->nmemslots; i++) {
726 struct kvm_memory_slot *memslot = &kvm->memslots[i];
727 unsigned long start = memslot->userspace_addr;
728 unsigned long end;
729
730 /* mmu_lock protects userspace_addr */
731 if (!start)
732 continue;
733
734 end = start + (memslot->npages << PAGE_SHIFT);
735 if (hva >= start && hva < end) {
736 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedelec04b262009-06-19 15:16:23 +0200737 int idx = gfn_offset /
738 KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200739 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
740 retval |= handler(kvm,
Joerg Roedelec04b262009-06-19 15:16:23 +0200741 &memslot->lpage_info[0][idx].rmap_pde);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200742 }
743 }
744
745 return retval;
746}
747
748int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
749{
750 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
751}
752
753static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
754{
755 u64 *spte;
756 int young = 0;
757
Sheng Yang534e38b2008-09-08 15:12:30 +0800758 /* always return old for EPT */
759 if (!shadow_accessed_mask)
760 return 0;
761
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200762 spte = rmap_next(kvm, rmapp, NULL);
763 while (spte) {
764 int _young;
765 u64 _spte = *spte;
766 BUG_ON(!(_spte & PT_PRESENT_MASK));
767 _young = _spte & PT_ACCESSED_MASK;
768 if (_young) {
769 young = 1;
770 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
771 }
772 spte = rmap_next(kvm, rmapp, spte);
773 }
774 return young;
775}
776
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300777#define RMAP_RECYCLE_THRESHOLD 1000
778
779static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
780{
781 unsigned long *rmapp;
782
783 gfn = unalias_gfn(vcpu->kvm, gfn);
784 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
785
786 kvm_unmap_rmapp(vcpu->kvm, rmapp);
787 kvm_flush_remote_tlbs(vcpu->kvm);
788}
789
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200790int kvm_age_hva(struct kvm *kvm, unsigned long hva)
791{
792 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
793}
794
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800795#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300796static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797{
Avi Kivity139bdb22007-01-05 16:36:50 -0800798 u64 *pos;
799 u64 *end;
800
Avi Kivity47ad8e62007-05-06 15:50:58 +0300801 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300802 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800803 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800804 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800806 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 return 1;
808}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800809#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810
Avi Kivity4db35312007-11-21 15:28:32 +0200811static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800812{
Avi Kivity4db35312007-11-21 15:28:32 +0200813 ASSERT(is_empty_shadow_page(sp->spt));
814 list_del(&sp->link);
815 __free_page(virt_to_page(sp->spt));
816 __free_page(virt_to_page(sp->gfns));
817 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800818 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800819}
820
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800821static unsigned kvm_page_table_hashfn(gfn_t gfn)
822{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200823 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800824}
825
Avi Kivity25c0de22007-01-05 16:36:42 -0800826static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
827 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828{
Avi Kivity4db35312007-11-21 15:28:32 +0200829 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800831 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
832 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
833 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200834 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800835 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200836 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800837 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200838 sp->multimapped = 0;
839 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800840 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200841 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842}
843
Avi Kivity714b93d2007-01-05 16:36:53 -0800844static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200845 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846{
847 struct kvm_pte_chain *pte_chain;
848 struct hlist_node *node;
849 int i;
850
851 if (!parent_pte)
852 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200853 if (!sp->multimapped) {
854 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800855
856 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200857 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800858 return;
859 }
Avi Kivity4db35312007-11-21 15:28:32 +0200860 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800861 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200862 INIT_HLIST_HEAD(&sp->parent_ptes);
863 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800864 pte_chain->parent_ptes[0] = old;
865 }
Avi Kivity4db35312007-11-21 15:28:32 +0200866 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800867 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
868 continue;
869 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
870 if (!pte_chain->parent_ptes[i]) {
871 pte_chain->parent_ptes[i] = parent_pte;
872 return;
873 }
874 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800875 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800876 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200877 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800878 pte_chain->parent_ptes[0] = parent_pte;
879}
880
Avi Kivity4db35312007-11-21 15:28:32 +0200881static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800882 u64 *parent_pte)
883{
884 struct kvm_pte_chain *pte_chain;
885 struct hlist_node *node;
886 int i;
887
Avi Kivity4db35312007-11-21 15:28:32 +0200888 if (!sp->multimapped) {
889 BUG_ON(sp->parent_pte != parent_pte);
890 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800891 return;
892 }
Avi Kivity4db35312007-11-21 15:28:32 +0200893 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800894 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
895 if (!pte_chain->parent_ptes[i])
896 break;
897 if (pte_chain->parent_ptes[i] != parent_pte)
898 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800899 while (i + 1 < NR_PTE_CHAIN_ENTRIES
900 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800901 pte_chain->parent_ptes[i]
902 = pte_chain->parent_ptes[i + 1];
903 ++i;
904 }
905 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800906 if (i == 0) {
907 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300908 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200909 if (hlist_empty(&sp->parent_ptes)) {
910 sp->multimapped = 0;
911 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800912 }
913 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800914 return;
915 }
916 BUG();
917}
918
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300919
920static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
921 mmu_parent_walk_fn fn)
922{
923 struct kvm_pte_chain *pte_chain;
924 struct hlist_node *node;
925 struct kvm_mmu_page *parent_sp;
926 int i;
927
928 if (!sp->multimapped && sp->parent_pte) {
929 parent_sp = page_header(__pa(sp->parent_pte));
930 fn(vcpu, parent_sp);
931 mmu_parent_walk(vcpu, parent_sp, fn);
932 return;
933 }
934 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
935 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
936 if (!pte_chain->parent_ptes[i])
937 break;
938 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
939 fn(vcpu, parent_sp);
940 mmu_parent_walk(vcpu, parent_sp, fn);
941 }
942}
943
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300944static void kvm_mmu_update_unsync_bitmap(u64 *spte)
945{
946 unsigned int index;
947 struct kvm_mmu_page *sp = page_header(__pa(spte));
948
949 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200950 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
951 sp->unsync_children++;
952 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300953}
954
955static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
956{
957 struct kvm_pte_chain *pte_chain;
958 struct hlist_node *node;
959 int i;
960
961 if (!sp->parent_pte)
962 return;
963
964 if (!sp->multimapped) {
965 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
966 return;
967 }
968
969 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
970 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
971 if (!pte_chain->parent_ptes[i])
972 break;
973 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
974 }
975}
976
977static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
978{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300979 kvm_mmu_update_parents_unsync(sp);
980 return 1;
981}
982
983static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
984 struct kvm_mmu_page *sp)
985{
986 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
987 kvm_mmu_update_parents_unsync(sp);
988}
989
Avi Kivityd761a502008-05-29 14:55:03 +0300990static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
991 struct kvm_mmu_page *sp)
992{
993 int i;
994
995 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
996 sp->spt[i] = shadow_trap_nonpresent_pte;
997}
998
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300999static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1000 struct kvm_mmu_page *sp)
1001{
1002 return 1;
1003}
1004
Marcelo Tosattia7052892008-09-23 13:18:35 -03001005static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1006{
1007}
1008
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001009#define KVM_PAGE_ARRAY_NR 16
1010
1011struct kvm_mmu_pages {
1012 struct mmu_page_and_offset {
1013 struct kvm_mmu_page *sp;
1014 unsigned int idx;
1015 } page[KVM_PAGE_ARRAY_NR];
1016 unsigned int nr;
1017};
1018
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001019#define for_each_unsync_children(bitmap, idx) \
1020 for (idx = find_first_bit(bitmap, 512); \
1021 idx < 512; \
1022 idx = find_next_bit(bitmap, 512, idx+1))
1023
Hannes Edercded19f2009-02-21 02:19:13 +01001024static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1025 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001026{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001027 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001028
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001029 if (sp->unsync)
1030 for (i=0; i < pvec->nr; i++)
1031 if (pvec->page[i].sp == sp)
1032 return 0;
1033
1034 pvec->page[pvec->nr].sp = sp;
1035 pvec->page[pvec->nr].idx = idx;
1036 pvec->nr++;
1037 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1038}
1039
1040static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1041 struct kvm_mmu_pages *pvec)
1042{
1043 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001044
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001045 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001046 u64 ent = sp->spt[i];
1047
Marcelo Tosatti87917232008-12-22 18:49:30 -02001048 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001049 struct kvm_mmu_page *child;
1050 child = page_header(ent & PT64_BASE_ADDR_MASK);
1051
1052 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001053 if (mmu_pages_add(pvec, child, i))
1054 return -ENOSPC;
1055
1056 ret = __mmu_unsync_walk(child, pvec);
1057 if (!ret)
1058 __clear_bit(i, sp->unsync_child_bitmap);
1059 else if (ret > 0)
1060 nr_unsync_leaf += ret;
1061 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001062 return ret;
1063 }
1064
1065 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001066 nr_unsync_leaf++;
1067 if (mmu_pages_add(pvec, child, i))
1068 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001069 }
1070 }
1071 }
1072
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001073 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001074 sp->unsync_children = 0;
1075
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001076 return nr_unsync_leaf;
1077}
1078
1079static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1080 struct kvm_mmu_pages *pvec)
1081{
1082 if (!sp->unsync_children)
1083 return 0;
1084
1085 mmu_pages_add(pvec, sp, 0);
1086 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001087}
1088
Avi Kivity4db35312007-11-21 15:28:32 +02001089static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001090{
1091 unsigned index;
1092 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001093 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001094 struct hlist_node *node;
1095
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001096 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001097 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001098 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001099 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001100 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001101 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001102 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001103 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001104 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001105 }
1106 return NULL;
1107}
1108
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001109static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1110{
1111 WARN_ON(!sp->unsync);
1112 sp->unsync = 0;
1113 --kvm->stat.mmu_unsync;
1114}
1115
1116static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1117
1118static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1119{
1120 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1121 kvm_mmu_zap_page(vcpu->kvm, sp);
1122 return 1;
1123 }
1124
Avi Kivityf691fe12009-07-06 15:58:14 +03001125 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001126 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1127 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001128 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001129 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1130 kvm_mmu_zap_page(vcpu->kvm, sp);
1131 return 1;
1132 }
1133
1134 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001135 return 0;
1136}
1137
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001138struct mmu_page_path {
1139 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1140 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001141};
1142
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001143#define for_each_sp(pvec, sp, parents, i) \
1144 for (i = mmu_pages_next(&pvec, &parents, -1), \
1145 sp = pvec.page[i].sp; \
1146 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1147 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001148
Hannes Edercded19f2009-02-21 02:19:13 +01001149static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1150 struct mmu_page_path *parents,
1151 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001152{
1153 int n;
1154
1155 for (n = i+1; n < pvec->nr; n++) {
1156 struct kvm_mmu_page *sp = pvec->page[n].sp;
1157
1158 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1159 parents->idx[0] = pvec->page[n].idx;
1160 return n;
1161 }
1162
1163 parents->parent[sp->role.level-2] = sp;
1164 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1165 }
1166
1167 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001168}
1169
Hannes Edercded19f2009-02-21 02:19:13 +01001170static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001171{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001172 struct kvm_mmu_page *sp;
1173 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001174
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001175 do {
1176 unsigned int idx = parents->idx[level];
1177
1178 sp = parents->parent[level];
1179 if (!sp)
1180 return;
1181
1182 --sp->unsync_children;
1183 WARN_ON((int)sp->unsync_children < 0);
1184 __clear_bit(idx, sp->unsync_child_bitmap);
1185 level++;
1186 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1187}
1188
1189static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1190 struct mmu_page_path *parents,
1191 struct kvm_mmu_pages *pvec)
1192{
1193 parents->parent[parent->role.level-1] = NULL;
1194 pvec->nr = 0;
1195}
1196
1197static void mmu_sync_children(struct kvm_vcpu *vcpu,
1198 struct kvm_mmu_page *parent)
1199{
1200 int i;
1201 struct kvm_mmu_page *sp;
1202 struct mmu_page_path parents;
1203 struct kvm_mmu_pages pages;
1204
1205 kvm_mmu_pages_init(parent, &parents, &pages);
1206 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001207 int protected = 0;
1208
1209 for_each_sp(pages, sp, parents, i)
1210 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1211
1212 if (protected)
1213 kvm_flush_remote_tlbs(vcpu->kvm);
1214
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001215 for_each_sp(pages, sp, parents, i) {
1216 kvm_sync_page(vcpu, sp);
1217 mmu_pages_clear_parents(&parents);
1218 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001219 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001220 kvm_mmu_pages_init(parent, &parents, &pages);
1221 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001222}
1223
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001224static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1225 gfn_t gfn,
1226 gva_t gaddr,
1227 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001228 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001229 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001230 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001231{
1232 union kvm_mmu_page_role role;
1233 unsigned index;
1234 unsigned quadrant;
1235 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001236 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001237 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001238
Avi Kivitya770f6f2008-12-21 19:20:09 +02001239 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001240 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001241 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001242 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001243 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001244 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1245 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1246 role.quadrant = quadrant;
1247 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001248 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001249 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1251 if (sp->gfn == gfn) {
1252 if (sp->unsync)
1253 if (kvm_sync_page(vcpu, sp))
1254 continue;
1255
1256 if (sp->role.word != role.word)
1257 continue;
1258
Avi Kivity4db35312007-11-21 15:28:32 +02001259 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001260 if (sp->unsync_children) {
1261 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1262 kvm_mmu_mark_parents_unsync(vcpu, sp);
1263 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001264 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001265 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001266 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001267 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001268 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1269 if (!sp)
1270 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001271 sp->gfn = gfn;
1272 sp->role = role;
1273 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001274 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001275 if (rmap_write_protect(vcpu->kvm, gfn))
1276 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001277 account_shadowed(vcpu->kvm, gfn);
1278 }
Avi Kivity131d8272008-05-29 14:56:28 +03001279 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1280 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1281 else
1282 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001283 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001284 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001285}
1286
Avi Kivity2d111232008-12-25 14:39:47 +02001287static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1288 struct kvm_vcpu *vcpu, u64 addr)
1289{
1290 iterator->addr = addr;
1291 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1292 iterator->level = vcpu->arch.mmu.shadow_root_level;
1293 if (iterator->level == PT32E_ROOT_LEVEL) {
1294 iterator->shadow_addr
1295 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1296 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1297 --iterator->level;
1298 if (!iterator->shadow_addr)
1299 iterator->level = 0;
1300 }
1301}
1302
1303static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1304{
1305 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1306 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001307
1308 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1309 if (is_large_pte(*iterator->sptep))
1310 return false;
1311
Avi Kivity2d111232008-12-25 14:39:47 +02001312 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1313 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1314 return true;
1315}
1316
1317static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1318{
1319 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1320 --iterator->level;
1321}
1322
Avi Kivity90cb0522007-07-17 13:04:56 +03001323static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001324 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001325{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001326 unsigned i;
1327 u64 *pt;
1328 u64 ent;
1329
Avi Kivity4db35312007-11-21 15:28:32 +02001330 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001331
Avi Kivity697fe2e2007-01-05 16:36:46 -08001332 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1333 ent = pt[i];
1334
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001335 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001336 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001337 ent &= PT64_BASE_ADDR_MASK;
1338 mmu_page_remove_parent_pte(page_header(ent),
1339 &pt[i]);
1340 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001341 if (is_large_pte(ent))
1342 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001343 rmap_remove(kvm, &pt[i]);
1344 }
1345 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001346 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001347 }
Avi Kivitya4360362007-01-05 16:36:45 -08001348}
1349
Avi Kivity4db35312007-11-21 15:28:32 +02001350static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001351{
Avi Kivity4db35312007-11-21 15:28:32 +02001352 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001353}
1354
Avi Kivity12b7d282007-09-23 14:10:49 +02001355static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1356{
1357 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001358 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001359
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001360 kvm_for_each_vcpu(i, vcpu, kvm)
1361 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001362}
1363
Avi Kivity31aa2b42008-07-11 17:59:46 +03001364static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001365{
1366 u64 *parent_pte;
1367
Avi Kivity4db35312007-11-21 15:28:32 +02001368 while (sp->multimapped || sp->parent_pte) {
1369 if (!sp->multimapped)
1370 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001371 else {
1372 struct kvm_pte_chain *chain;
1373
Avi Kivity4db35312007-11-21 15:28:32 +02001374 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001375 struct kvm_pte_chain, link);
1376 parent_pte = chain->parent_ptes[0];
1377 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001378 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001379 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001380 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001381 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001382}
1383
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001384static int mmu_zap_unsync_children(struct kvm *kvm,
1385 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001386{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001387 int i, zapped = 0;
1388 struct mmu_page_path parents;
1389 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001390
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001391 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001392 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001393
1394 kvm_mmu_pages_init(parent, &parents, &pages);
1395 while (mmu_unsync_walk(parent, &pages)) {
1396 struct kvm_mmu_page *sp;
1397
1398 for_each_sp(pages, sp, parents, i) {
1399 kvm_mmu_zap_page(kvm, sp);
1400 mmu_pages_clear_parents(&parents);
1401 }
1402 zapped += pages.nr;
1403 kvm_mmu_pages_init(parent, &parents, &pages);
1404 }
1405
1406 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001407}
1408
Marcelo Tosatti07385412008-09-23 13:18:37 -03001409static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001410{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001411 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001412
1413 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001414 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001415 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001416 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001417 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001418 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001419 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001420 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001421 if (sp->unsync)
1422 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001423 if (!sp->root_count) {
1424 hlist_del(&sp->hash_link);
1425 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001426 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001427 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001428 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001429 kvm_reload_remote_mmus(kvm);
1430 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001431 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001432 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001433}
1434
Izik Eidus82ce2c92007-10-02 18:52:55 +02001435/*
1436 * Changing the number of mmu pages allocated to the vm
1437 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1438 */
1439void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1440{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001441 int used_pages;
1442
1443 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1444 used_pages = max(0, used_pages);
1445
Izik Eidus82ce2c92007-10-02 18:52:55 +02001446 /*
1447 * If we set the number of mmu pages to be smaller be than the
1448 * number of actived pages , we must to free some mmu pages before we
1449 * change the value
1450 */
1451
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001452 if (used_pages > kvm_nr_mmu_pages) {
1453 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001454 struct kvm_mmu_page *page;
1455
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001456 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001457 struct kvm_mmu_page, link);
1458 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001459 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001460 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001461 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001462 }
1463 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001464 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1465 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001466
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001467 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001468}
1469
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001470static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001471{
1472 unsigned index;
1473 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001474 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001475 struct hlist_node *node, *n;
1476 int r;
1477
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001478 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001479 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001480 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001481 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001482 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001483 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001484 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001485 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001486 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001487 if (kvm_mmu_zap_page(kvm, sp))
1488 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001489 }
1490 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001491}
1492
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001493static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001494{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001495 unsigned index;
1496 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001497 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001498 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001499
Avi Kivity4677a3b2009-01-06 13:00:27 +02001500 index = kvm_page_table_hashfn(gfn);
1501 bucket = &kvm->arch.mmu_page_hash[index];
1502 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001503 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001504 && !sp->role.invalid) {
1505 pgprintk("%s: zap %lx %x\n",
1506 __func__, gfn, sp->role.word);
1507 kvm_mmu_zap_page(kvm, sp);
1508 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001509 }
1510}
1511
Avi Kivity38c335f2007-11-21 14:20:22 +02001512static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513{
Avi Kivity38c335f2007-11-21 14:20:22 +02001514 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001515 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516
Sheng Yang291f26b2008-10-16 17:30:57 +08001517 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518}
1519
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001520static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1521{
1522 int i;
1523 u64 *pt = sp->spt;
1524
1525 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1526 return;
1527
1528 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1529 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001530 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001531 }
1532}
1533
Avi Kivity039576c2007-03-20 12:46:50 +02001534struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1535{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001536 struct page *page;
1537
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001538 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001539
1540 if (gpa == UNMAPPED_GVA)
1541 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001542
Izik Eidus72dc67a2008-02-10 18:04:15 +02001543 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001544
1545 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001546}
1547
Sheng Yang74be52e2008-10-09 16:01:56 +08001548/*
1549 * The function is based on mtrr_type_lookup() in
1550 * arch/x86/kernel/cpu/mtrr/generic.c
1551 */
1552static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1553 u64 start, u64 end)
1554{
1555 int i;
1556 u64 base, mask;
1557 u8 prev_match, curr_match;
1558 int num_var_ranges = KVM_NR_VAR_MTRR;
1559
1560 if (!mtrr_state->enabled)
1561 return 0xFF;
1562
1563 /* Make end inclusive end, instead of exclusive */
1564 end--;
1565
1566 /* Look in fixed ranges. Just return the type as per start */
1567 if (mtrr_state->have_fixed && (start < 0x100000)) {
1568 int idx;
1569
1570 if (start < 0x80000) {
1571 idx = 0;
1572 idx += (start >> 16);
1573 return mtrr_state->fixed_ranges[idx];
1574 } else if (start < 0xC0000) {
1575 idx = 1 * 8;
1576 idx += ((start - 0x80000) >> 14);
1577 return mtrr_state->fixed_ranges[idx];
1578 } else if (start < 0x1000000) {
1579 idx = 3 * 8;
1580 idx += ((start - 0xC0000) >> 12);
1581 return mtrr_state->fixed_ranges[idx];
1582 }
1583 }
1584
1585 /*
1586 * Look in variable ranges
1587 * Look of multiple ranges matching this address and pick type
1588 * as per MTRR precedence
1589 */
1590 if (!(mtrr_state->enabled & 2))
1591 return mtrr_state->def_type;
1592
1593 prev_match = 0xFF;
1594 for (i = 0; i < num_var_ranges; ++i) {
1595 unsigned short start_state, end_state;
1596
1597 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1598 continue;
1599
1600 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1601 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1602 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1603 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1604
1605 start_state = ((start & mask) == (base & mask));
1606 end_state = ((end & mask) == (base & mask));
1607 if (start_state != end_state)
1608 return 0xFE;
1609
1610 if ((start & mask) != (base & mask))
1611 continue;
1612
1613 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1614 if (prev_match == 0xFF) {
1615 prev_match = curr_match;
1616 continue;
1617 }
1618
1619 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1620 curr_match == MTRR_TYPE_UNCACHABLE)
1621 return MTRR_TYPE_UNCACHABLE;
1622
1623 if ((prev_match == MTRR_TYPE_WRBACK &&
1624 curr_match == MTRR_TYPE_WRTHROUGH) ||
1625 (prev_match == MTRR_TYPE_WRTHROUGH &&
1626 curr_match == MTRR_TYPE_WRBACK)) {
1627 prev_match = MTRR_TYPE_WRTHROUGH;
1628 curr_match = MTRR_TYPE_WRTHROUGH;
1629 }
1630
1631 if (prev_match != curr_match)
1632 return MTRR_TYPE_UNCACHABLE;
1633 }
1634
1635 if (prev_match != 0xFF)
1636 return prev_match;
1637
1638 return mtrr_state->def_type;
1639}
1640
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001641u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001642{
1643 u8 mtrr;
1644
1645 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1646 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1647 if (mtrr == 0xfe || mtrr == 0xff)
1648 mtrr = MTRR_TYPE_WRBACK;
1649 return mtrr;
1650}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001651EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001652
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001653static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1654{
1655 unsigned index;
1656 struct hlist_head *bucket;
1657 struct kvm_mmu_page *s;
1658 struct hlist_node *node, *n;
1659
Avi Kivityf691fe12009-07-06 15:58:14 +03001660 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001661 index = kvm_page_table_hashfn(sp->gfn);
1662 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1663 /* don't unsync if pagetable is shadowed with multiple roles */
1664 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001665 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001666 continue;
1667 if (s->role.word != sp->role.word)
1668 return 1;
1669 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001670 ++vcpu->kvm->stat.mmu_unsync;
1671 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001672
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001673 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001674
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001675 mmu_convert_notrap(sp);
1676 return 0;
1677}
1678
1679static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1680 bool can_unsync)
1681{
1682 struct kvm_mmu_page *shadow;
1683
1684 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1685 if (shadow) {
1686 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1687 return 1;
1688 if (shadow->unsync)
1689 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001690 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001691 return kvm_unsync_page(vcpu, shadow);
1692 return 1;
1693 }
1694 return 0;
1695}
1696
Avi Kivityd555c332009-06-10 14:24:23 +03001697static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001698 unsigned pte_access, int user_fault,
1699 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001700 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001701 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001702{
1703 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001704 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001705
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001706 /*
1707 * We don't set the accessed bit, since we sometimes want to see
1708 * whether the guest actually used the pte (in order to detect
1709 * demand paging).
1710 */
Sheng Yang7b523452008-04-25 21:13:50 +08001711 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001712 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001713 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001714 if (!dirty)
1715 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001716 if (pte_access & ACC_EXEC_MASK)
1717 spte |= shadow_x_mask;
1718 else
1719 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001720 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001721 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001722 if (largepage)
1723 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001724 if (tdp_enabled)
1725 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1726 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727
Anthony Liguori35149e22008-04-02 14:46:56 -05001728 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001729
1730 if ((pte_access & ACC_WRITE_MASK)
1731 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001732
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001733 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1734 ret = 1;
1735 spte = shadow_trap_nonpresent_pte;
1736 goto set_pte;
1737 }
1738
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001739 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001741 /*
1742 * Optimization: for pte sync, if spte was writable the hash
1743 * lookup is unnecessary (and expensive). Write protection
1744 * is responsibility of mmu_get_page / kvm_sync_page.
1745 * Same reasoning can be applied to dirty page accounting.
1746 */
Avi Kivityd555c332009-06-10 14:24:23 +03001747 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001748 goto set_pte;
1749
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001750 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001751 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001752 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001753 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001754 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001755 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001756 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001757 }
1758 }
1759
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001760 if (pte_access & ACC_WRITE_MASK)
1761 mark_page_dirty(vcpu->kvm, gfn);
1762
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001763set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001764 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001765 return ret;
1766}
1767
Avi Kivityd555c332009-06-10 14:24:23 +03001768static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001769 unsigned pt_access, unsigned pte_access,
1770 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001771 int *ptwrite, int largepage, gfn_t gfn,
1772 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001773{
1774 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001775 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001776 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001777
1778 pgprintk("%s: spte %llx access %x write_fault %d"
1779 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001780 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001781 write_fault, user_fault, gfn);
1782
Avi Kivityd555c332009-06-10 14:24:23 +03001783 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001784 /*
1785 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1786 * the parent of the now unreachable PTE.
1787 */
Avi Kivityd555c332009-06-10 14:24:23 +03001788 if (largepage && !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001789 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001790 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001791
1792 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001793 mmu_page_remove_parent_pte(child, sptep);
1794 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001795 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001796 spte_to_pfn(*sptep), pfn);
1797 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001798 } else
1799 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001800 }
Avi Kivityd555c332009-06-10 14:24:23 +03001801 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001802 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001803 if (write_fault)
1804 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001805 kvm_x86_ops->tlb_flush(vcpu);
1806 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001807
Avi Kivityd555c332009-06-10 14:24:23 +03001808 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001809 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001810 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001811 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1812 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001813 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001814 ++vcpu->kvm->stat.lpages;
1815
Avi Kivityd555c332009-06-10 14:24:23 +03001816 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817 if (!was_rmapped) {
Avi Kivityd555c332009-06-10 14:24:23 +03001818 rmap_count = rmap_add(vcpu, sptep, gfn, largepage);
1819 if (!is_rmap_spte(*sptep))
Anthony Liguori35149e22008-04-02 14:46:56 -05001820 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001821 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1822 rmap_recycle(vcpu, gfn, largepage);
Izik Eidus75e68e62008-01-12 23:49:09 +02001823 } else {
1824 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001825 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001826 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001827 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001828 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001829 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001830 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001831 vcpu->arch.last_pte_gfn = gfn;
1832 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001833}
1834
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1836{
1837}
1838
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001839static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001840 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841{
Avi Kivity9f652d22008-12-25 14:54:25 +02001842 struct kvm_shadow_walk_iterator iterator;
1843 struct kvm_mmu_page *sp;
1844 int pt_write = 0;
1845 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846
Avi Kivity9f652d22008-12-25 14:54:25 +02001847 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1848 if (iterator.level == PT_PAGE_TABLE_LEVEL
1849 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1850 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1851 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001852 largepage, gfn, pfn, false);
Avi Kivity9f652d22008-12-25 14:54:25 +02001853 ++vcpu->stat.pf_fixed;
1854 break;
1855 }
1856
1857 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1858 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1859 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1860 iterator.level - 1,
1861 1, ACC_ALL, iterator.sptep);
1862 if (!sp) {
1863 pgprintk("nonpaging_map: ENOMEM\n");
1864 kvm_release_pfn_clean(pfn);
1865 return -ENOMEM;
1866 }
1867
Avi Kivityd555c332009-06-10 14:24:23 +03001868 __set_spte(iterator.sptep,
1869 __pa(sp->spt)
1870 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1871 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001872 }
1873 }
1874 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875}
1876
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001877static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1878{
1879 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001880 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001881 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001882 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001883
Joerg Roedelec04b262009-06-19 15:16:23 +02001884 if (is_largepage_backed(vcpu, gfn &
1885 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
1886 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001887 largepage = 1;
1888 }
1889
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001890 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001891 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001892 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001893
Avi Kivityd196e342008-01-24 11:44:11 +02001894 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001895 if (is_error_pfn(pfn)) {
1896 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001897 return 1;
1898 }
1899
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001900 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001901 if (mmu_notifier_retry(vcpu, mmu_seq))
1902 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001903 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001904 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001905 spin_unlock(&vcpu->kvm->mmu_lock);
1906
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001907
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001908 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001909
1910out_unlock:
1911 spin_unlock(&vcpu->kvm->mmu_lock);
1912 kvm_release_pfn_clean(pfn);
1913 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001914}
1915
1916
Avi Kivity17ac10a2007-01-05 16:36:40 -08001917static void mmu_free_roots(struct kvm_vcpu *vcpu)
1918{
1919 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001920 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001921
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001922 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001923 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001924 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001925 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1926 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001927
Avi Kivity4db35312007-11-21 15:28:32 +02001928 sp = page_header(root);
1929 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001930 if (!sp->root_count && sp->role.invalid)
1931 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001932 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001933 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001934 return;
1935 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001936 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001937 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001938
Avi Kivity417726a2007-04-12 17:35:58 +03001939 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001940 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001941 sp = page_header(root);
1942 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001943 if (!sp->root_count && sp->role.invalid)
1944 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001945 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001946 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001947 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001948 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001949 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001950}
1951
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001952static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1953{
1954 int ret = 0;
1955
1956 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1957 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1958 ret = 1;
1959 }
1960
1961 return ret;
1962}
1963
1964static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08001965{
1966 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001967 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001968 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001969 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001970 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001971
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001972 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001973
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001974 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1975 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001976
1977 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001978 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001979 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001980 if (mmu_check_root(vcpu, root_gfn))
1981 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001982 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001983 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001984 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001985 root = __pa(sp->spt);
1986 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001987 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001988 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001989 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001990 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001991 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001992 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001993 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001994 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001995
1996 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001997 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001998 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03001999 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002000 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002001 continue;
2002 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002003 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002004 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002005 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002006 if (mmu_check_root(vcpu, root_gfn))
2007 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002008 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002009 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002010 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002011 root = __pa(sp->spt);
2012 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002013 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002014 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002015 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002016 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002017}
2018
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002019static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2020{
2021 int i;
2022 struct kvm_mmu_page *sp;
2023
2024 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2025 return;
2026 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2027 hpa_t root = vcpu->arch.mmu.root_hpa;
2028 sp = page_header(root);
2029 mmu_sync_children(vcpu, sp);
2030 return;
2031 }
2032 for (i = 0; i < 4; ++i) {
2033 hpa_t root = vcpu->arch.mmu.pae_root[i];
2034
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002035 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002036 root &= PT64_BASE_ADDR_MASK;
2037 sp = page_header(root);
2038 mmu_sync_children(vcpu, sp);
2039 }
2040 }
2041}
2042
2043void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2044{
2045 spin_lock(&vcpu->kvm->mmu_lock);
2046 mmu_sync_roots(vcpu);
2047 spin_unlock(&vcpu->kvm->mmu_lock);
2048}
2049
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2051{
2052 return vaddr;
2053}
2054
2055static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002056 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057{
Avi Kivitye8332402007-12-09 18:43:00 +02002058 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002059 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002061 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002062 r = mmu_topup_memory_caches(vcpu);
2063 if (r)
2064 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002065
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002067 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068
Avi Kivitye8332402007-12-09 18:43:00 +02002069 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070
Avi Kivitye8332402007-12-09 18:43:00 +02002071 return nonpaging_map(vcpu, gva & PAGE_MASK,
2072 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073}
2074
Joerg Roedelfb72d162008-02-07 13:47:44 +01002075static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2076 u32 error_code)
2077{
Anthony Liguori35149e22008-04-02 14:46:56 -05002078 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002079 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002080 int largepage = 0;
2081 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002082 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002083
2084 ASSERT(vcpu);
2085 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2086
2087 r = mmu_topup_memory_caches(vcpu);
2088 if (r)
2089 return r;
2090
Joerg Roedelec04b262009-06-19 15:16:23 +02002091 if (is_largepage_backed(vcpu, gfn &
2092 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
2093 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002094 largepage = 1;
2095 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002096 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002097 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002098 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002099 if (is_error_pfn(pfn)) {
2100 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002101 return 1;
2102 }
2103 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002104 if (mmu_notifier_retry(vcpu, mmu_seq))
2105 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002106 kvm_mmu_free_some_pages(vcpu);
2107 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002108 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002109 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002110
2111 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002112
2113out_unlock:
2114 spin_unlock(&vcpu->kvm->mmu_lock);
2115 kvm_release_pfn_clean(pfn);
2116 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002117}
2118
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119static void nonpaging_free(struct kvm_vcpu *vcpu)
2120{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002121 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122}
2123
2124static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2125{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002126 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127
2128 context->new_cr3 = nonpaging_new_cr3;
2129 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130 context->gva_to_gpa = nonpaging_gva_to_gpa;
2131 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002132 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002133 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002134 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002135 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002137 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 return 0;
2139}
2140
Avi Kivityd835dfe2007-11-21 02:57:59 +02002141void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002143 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002144 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145}
2146
2147static void paging_new_cr3(struct kvm_vcpu *vcpu)
2148{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002149 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002150 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151}
2152
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153static void inject_page_fault(struct kvm_vcpu *vcpu,
2154 u64 addr,
2155 u32 err_code)
2156{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002157 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158}
2159
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160static void paging_free(struct kvm_vcpu *vcpu)
2161{
2162 nonpaging_free(vcpu);
2163}
2164
Dong, Eddie82725b22009-03-30 16:21:08 +08002165static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2166{
2167 int bit7;
2168
2169 bit7 = (gpte >> 7) & 1;
2170 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2171}
2172
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173#define PTTYPE 64
2174#include "paging_tmpl.h"
2175#undef PTTYPE
2176
2177#define PTTYPE 32
2178#include "paging_tmpl.h"
2179#undef PTTYPE
2180
Dong, Eddie82725b22009-03-30 16:21:08 +08002181static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2182{
2183 struct kvm_mmu *context = &vcpu->arch.mmu;
2184 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2185 u64 exb_bit_rsvd = 0;
2186
2187 if (!is_nx(vcpu))
2188 exb_bit_rsvd = rsvd_bits(63, 63);
2189 switch (level) {
2190 case PT32_ROOT_LEVEL:
2191 /* no rsvd bits for 2 level 4K page table entries */
2192 context->rsvd_bits_mask[0][1] = 0;
2193 context->rsvd_bits_mask[0][0] = 0;
2194 if (is_cpuid_PSE36())
2195 /* 36bits PSE 4MB page */
2196 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2197 else
2198 /* 32 bits PSE 4MB page */
2199 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
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 PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002203 context->rsvd_bits_mask[0][2] =
2204 rsvd_bits(maxphyaddr, 63) |
2205 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002206 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002207 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002208 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2209 rsvd_bits(maxphyaddr, 62); /* PTE */
2210 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2211 rsvd_bits(maxphyaddr, 62) |
2212 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002213 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002214 break;
2215 case PT64_ROOT_LEVEL:
2216 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2217 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2218 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2219 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2220 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002221 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002222 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2223 rsvd_bits(maxphyaddr, 51);
2224 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2225 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2226 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002227 rsvd_bits(maxphyaddr, 51) |
2228 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002229 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002230 break;
2231 }
2232}
2233
Avi Kivity17ac10a2007-01-05 16:36:40 -08002234static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002236 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237
2238 ASSERT(is_pae(vcpu));
2239 context->new_cr3 = paging_new_cr3;
2240 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002242 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002243 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002244 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002246 context->root_level = level;
2247 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002248 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 return 0;
2250}
2251
Avi Kivity17ac10a2007-01-05 16:36:40 -08002252static int paging64_init_context(struct kvm_vcpu *vcpu)
2253{
Dong, Eddie82725b22009-03-30 16:21:08 +08002254 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002255 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2256}
2257
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258static int paging32_init_context(struct kvm_vcpu *vcpu)
2259{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002260 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261
Dong, Eddie82725b22009-03-30 16:21:08 +08002262 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 context->new_cr3 = paging_new_cr3;
2264 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 context->gva_to_gpa = paging32_gva_to_gpa;
2266 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002267 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002268 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002269 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 context->root_level = PT32_ROOT_LEVEL;
2271 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002272 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 return 0;
2274}
2275
2276static int paging32E_init_context(struct kvm_vcpu *vcpu)
2277{
Dong, Eddie82725b22009-03-30 16:21:08 +08002278 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002279 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280}
2281
Joerg Roedelfb72d162008-02-07 13:47:44 +01002282static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2283{
2284 struct kvm_mmu *context = &vcpu->arch.mmu;
2285
2286 context->new_cr3 = nonpaging_new_cr3;
2287 context->page_fault = tdp_page_fault;
2288 context->free = nonpaging_free;
2289 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002290 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002291 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002292 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002293 context->root_hpa = INVALID_PAGE;
2294
2295 if (!is_paging(vcpu)) {
2296 context->gva_to_gpa = nonpaging_gva_to_gpa;
2297 context->root_level = 0;
2298 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002299 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002300 context->gva_to_gpa = paging64_gva_to_gpa;
2301 context->root_level = PT64_ROOT_LEVEL;
2302 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002303 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002304 context->gva_to_gpa = paging64_gva_to_gpa;
2305 context->root_level = PT32E_ROOT_LEVEL;
2306 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002307 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002308 context->gva_to_gpa = paging32_gva_to_gpa;
2309 context->root_level = PT32_ROOT_LEVEL;
2310 }
2311
2312 return 0;
2313}
2314
2315static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002317 int r;
2318
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002320 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
2322 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002323 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002324 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002325 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002327 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002329 r = paging32_init_context(vcpu);
2330
2331 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2332
2333 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334}
2335
Joerg Roedelfb72d162008-02-07 13:47:44 +01002336static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2337{
Anthony Liguori35149e22008-04-02 14:46:56 -05002338 vcpu->arch.update_pte.pfn = bad_pfn;
2339
Joerg Roedelfb72d162008-02-07 13:47:44 +01002340 if (tdp_enabled)
2341 return init_kvm_tdp_mmu(vcpu);
2342 else
2343 return init_kvm_softmmu(vcpu);
2344}
2345
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2347{
2348 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2350 vcpu->arch.mmu.free(vcpu);
2351 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 }
2353}
2354
2355int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2356{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002357 destroy_kvm_mmu(vcpu);
2358 return init_kvm_mmu(vcpu);
2359}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002360EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002361
2362int kvm_mmu_load(struct kvm_vcpu *vcpu)
2363{
Avi Kivity714b93d2007-01-05 16:36:53 -08002364 int r;
2365
Avi Kivitye2dec932007-01-05 16:36:54 -08002366 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002367 if (r)
2368 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002369 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002370 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002371 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002372 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002373 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002374 if (r)
2375 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002376 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002377 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002378out:
2379 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002381EXPORT_SYMBOL_GPL(kvm_mmu_load);
2382
2383void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2384{
2385 mmu_free_roots(vcpu);
2386}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387
Avi Kivity09072da2007-05-01 14:16:52 +03002388static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002389 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002390 u64 *spte)
2391{
2392 u64 pte;
2393 struct kvm_mmu_page *child;
2394
2395 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002396 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002397 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002398 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002399 else {
2400 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002401 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002402 }
2403 }
Avi Kivityd555c332009-06-10 14:24:23 +03002404 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002405 if (is_large_pte(pte))
2406 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002407}
2408
Avi Kivity00284252007-05-01 16:53:31 +03002409static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002410 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002411 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002412 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002413{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002414 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2415 if (!vcpu->arch.update_pte.largepage ||
2416 sp->role.glevels == PT32_ROOT_LEVEL) {
2417 ++vcpu->kvm->stat.mmu_pde_zapped;
2418 return;
2419 }
2420 }
Avi Kivity00284252007-05-01 16:53:31 +03002421
Avi Kivity4cee5762007-11-18 16:37:07 +02002422 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002423 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002424 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002425 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002426 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002427}
2428
Avi Kivity79539ce2007-11-21 02:06:21 +02002429static bool need_remote_flush(u64 old, u64 new)
2430{
2431 if (!is_shadow_present_pte(old))
2432 return false;
2433 if (!is_shadow_present_pte(new))
2434 return true;
2435 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2436 return true;
2437 old ^= PT64_NX_MASK;
2438 new ^= PT64_NX_MASK;
2439 return (old & ~new & PT64_PERM_MASK) != 0;
2440}
2441
2442static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2443{
2444 if (need_remote_flush(old, new))
2445 kvm_flush_remote_tlbs(vcpu->kvm);
2446 else
2447 kvm_mmu_flush_tlb(vcpu);
2448}
2449
Avi Kivity12b7d282007-09-23 14:10:49 +02002450static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2451{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002452 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002453
Sheng Yang7b523452008-04-25 21:13:50 +08002454 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002455}
2456
Avi Kivityd7824ff2007-12-30 12:29:05 +02002457static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2458 const u8 *new, int bytes)
2459{
2460 gfn_t gfn;
2461 int r;
2462 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002463 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002464
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002465 vcpu->arch.update_pte.largepage = 0;
2466
Avi Kivityd7824ff2007-12-30 12:29:05 +02002467 if (bytes != 4 && bytes != 8)
2468 return;
2469
2470 /*
2471 * Assume that the pte write on a page table of the same type
2472 * as the current vcpu paging mode. This is nearly always true
2473 * (might be false while changing modes). Note it is verified later
2474 * by update_pte().
2475 */
2476 if (is_pae(vcpu)) {
2477 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2478 if ((bytes == 4) && (gpa % 4 == 0)) {
2479 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2480 if (r)
2481 return;
2482 memcpy((void *)&gpte + (gpa % 8), new, 4);
2483 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2484 memcpy((void *)&gpte, new, 8);
2485 }
2486 } else {
2487 if ((bytes == 4) && (gpa % 4 == 0))
2488 memcpy((void *)&gpte, new, 4);
2489 }
Avi Kivity43a37952009-06-10 14:12:05 +03002490 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002491 return;
2492 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002493
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002494 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
Joerg Roedelec04b262009-06-19 15:16:23 +02002495 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002496 vcpu->arch.update_pte.largepage = 1;
2497 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002498 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002499 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002500 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002501
Anthony Liguori35149e22008-04-02 14:46:56 -05002502 if (is_error_pfn(pfn)) {
2503 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002504 return;
2505 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002506 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002507 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002508}
2509
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002510static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2511{
2512 u64 *spte = vcpu->arch.last_pte_updated;
2513
2514 if (spte
2515 && vcpu->arch.last_pte_gfn == gfn
2516 && shadow_accessed_mask
2517 && !(*spte & shadow_accessed_mask)
2518 && is_shadow_present_pte(*spte))
2519 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2520}
2521
Avi Kivity09072da2007-05-01 14:16:52 +03002522void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002523 const u8 *new, int bytes,
2524 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002525{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002526 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002527 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002528 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002529 struct hlist_head *bucket;
2530 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002531 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002532 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002533 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002534 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002535 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002536 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002537 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002538 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002539 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002540 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002541 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002542
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002543 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002544 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002545 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002546 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002547 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002548 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002549 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002550 if (guest_initiated) {
2551 if (gfn == vcpu->arch.last_pt_write_gfn
2552 && !last_updated_pte_accessed(vcpu)) {
2553 ++vcpu->arch.last_pt_write_count;
2554 if (vcpu->arch.last_pt_write_count >= 3)
2555 flooded = 1;
2556 } else {
2557 vcpu->arch.last_pt_write_gfn = gfn;
2558 vcpu->arch.last_pt_write_count = 1;
2559 vcpu->arch.last_pte_updated = NULL;
2560 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002561 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002562 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002563 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002564 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002565 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002566 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002567 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002568 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002569 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002570 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002571 /*
2572 * Misaligned accesses are too much trouble to fix
2573 * up; also, they usually indicate a page is not used
2574 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002575 *
2576 * If we're seeing too many writes to a page,
2577 * it may no longer be a page table, or we may be
2578 * forking, in which case it is better to unmap the
2579 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002580 */
2581 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002582 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002583 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2584 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002585 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002586 continue;
2587 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002588 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002589 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002590 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002591 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002592 page_offset <<= 1; /* 32->64 */
2593 /*
2594 * A 32-bit pde maps 4MB while the shadow pdes map
2595 * only 2MB. So we need to double the offset again
2596 * and zap two pdes instead of one.
2597 */
2598 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002599 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002600 page_offset <<= 1;
2601 npte = 2;
2602 }
Avi Kivityfce06572007-05-01 16:44:05 +03002603 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002604 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002605 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002606 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002607 }
Avi Kivity4db35312007-11-21 15:28:32 +02002608 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002609 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2610 gentry = 0;
2611 r = kvm_read_guest_atomic(vcpu->kvm,
2612 gpa & ~(u64)(pte_size - 1),
2613 &gentry, pte_size);
2614 new = (const void *)&gentry;
2615 if (r < 0)
2616 new = NULL;
2617 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002618 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002619 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002620 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002621 if (new)
2622 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002623 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002624 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002625 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002626 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002627 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002628 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002629 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2630 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2631 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002632 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002633}
2634
Avi Kivitya4360362007-01-05 16:36:45 -08002635int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2636{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002637 gpa_t gpa;
2638 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002639
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002640 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002641
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002642 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002643 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002644 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002645 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002646}
Avi Kivity577bdc42008-07-19 08:57:05 +03002647EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002648
Avi Kivity22d95b12007-09-14 20:26:06 +03002649void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002650{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002651 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002652 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002653
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002654 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002655 struct kvm_mmu_page, link);
2656 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002657 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002658 }
2659}
Avi Kivityebeace82007-01-05 16:36:47 -08002660
Avi Kivity30677142007-10-28 18:48:59 +02002661int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2662{
2663 int r;
2664 enum emulation_result er;
2665
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002666 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002667 if (r < 0)
2668 goto out;
2669
2670 if (!r) {
2671 r = 1;
2672 goto out;
2673 }
2674
Avi Kivityb733bfb2007-10-28 18:52:05 +02002675 r = mmu_topup_memory_caches(vcpu);
2676 if (r)
2677 goto out;
2678
Avi Kivity30677142007-10-28 18:48:59 +02002679 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002680
2681 switch (er) {
2682 case EMULATE_DONE:
2683 return 1;
2684 case EMULATE_DO_MMIO:
2685 ++vcpu->stat.mmio_exits;
2686 return 0;
2687 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002688 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2689 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2690 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002691 default:
2692 BUG();
2693 }
2694out:
Avi Kivity30677142007-10-28 18:48:59 +02002695 return r;
2696}
2697EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2698
Marcelo Tosattia7052892008-09-23 13:18:35 -03002699void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2700{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002701 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002702 kvm_mmu_flush_tlb(vcpu);
2703 ++vcpu->stat.invlpg;
2704}
2705EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2706
Joerg Roedel18552672008-02-07 13:47:41 +01002707void kvm_enable_tdp(void)
2708{
2709 tdp_enabled = true;
2710}
2711EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2712
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002713void kvm_disable_tdp(void)
2714{
2715 tdp_enabled = false;
2716}
2717EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2718
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719static void free_mmu_pages(struct kvm_vcpu *vcpu)
2720{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002721 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722}
2723
2724static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2725{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002726 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 int i;
2728
2729 ASSERT(vcpu);
2730
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002731 if (vcpu->kvm->arch.n_requested_mmu_pages)
2732 vcpu->kvm->arch.n_free_mmu_pages =
2733 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002734 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002735 vcpu->kvm->arch.n_free_mmu_pages =
2736 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002737 /*
2738 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2739 * Therefore we need to allocate shadow page tables in the first
2740 * 4GB of memory, which happens to fit the DMA32 zone.
2741 */
2742 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2743 if (!page)
2744 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002745 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002746 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002747 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002748
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749 return 0;
2750
2751error_1:
2752 free_mmu_pages(vcpu);
2753 return -ENOMEM;
2754}
2755
Ingo Molnar8018c272006-12-29 16:50:01 -08002756int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002759 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760
Ingo Molnar8018c272006-12-29 16:50:01 -08002761 return alloc_mmu_pages(vcpu);
2762}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763
Ingo Molnar8018c272006-12-29 16:50:01 -08002764int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2765{
2766 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002767 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002768
Ingo Molnar8018c272006-12-29 16:50:01 -08002769 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770}
2771
2772void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2773{
2774 ASSERT(vcpu);
2775
2776 destroy_kvm_mmu(vcpu);
2777 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002778 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779}
2780
Avi Kivity90cb0522007-07-17 13:04:56 +03002781void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782{
Avi Kivity4db35312007-11-21 15:28:32 +02002783 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002785 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 int i;
2787 u64 *pt;
2788
Sheng Yang291f26b2008-10-16 17:30:57 +08002789 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 continue;
2791
Avi Kivity4db35312007-11-21 15:28:32 +02002792 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2794 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002795 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 }
Avi Kivity171d5952008-08-27 16:40:51 +03002798 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002800
Avi Kivity90cb0522007-07-17 13:04:56 +03002801void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002802{
Avi Kivity4db35312007-11-21 15:28:32 +02002803 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002804
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002805 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002806 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002807 if (kvm_mmu_zap_page(kvm, sp))
2808 node = container_of(kvm->arch.active_mmu_pages.next,
2809 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002810 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002811
Avi Kivity90cb0522007-07-17 13:04:56 +03002812 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002813}
2814
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002815static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002816{
2817 struct kvm_mmu_page *page;
2818
2819 page = container_of(kvm->arch.active_mmu_pages.prev,
2820 struct kvm_mmu_page, link);
2821 kvm_mmu_zap_page(kvm, page);
2822}
2823
2824static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2825{
2826 struct kvm *kvm;
2827 struct kvm *kvm_freed = NULL;
2828 int cache_count = 0;
2829
2830 spin_lock(&kvm_lock);
2831
2832 list_for_each_entry(kvm, &vm_list, vm_list) {
2833 int npages;
2834
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002835 if (!down_read_trylock(&kvm->slots_lock))
2836 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002837 spin_lock(&kvm->mmu_lock);
2838 npages = kvm->arch.n_alloc_mmu_pages -
2839 kvm->arch.n_free_mmu_pages;
2840 cache_count += npages;
2841 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2842 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2843 cache_count--;
2844 kvm_freed = kvm;
2845 }
2846 nr_to_scan--;
2847
2848 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002849 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002850 }
2851 if (kvm_freed)
2852 list_move_tail(&kvm_freed->vm_list, &vm_list);
2853
2854 spin_unlock(&kvm_lock);
2855
2856 return cache_count;
2857}
2858
2859static struct shrinker mmu_shrinker = {
2860 .shrink = mmu_shrink,
2861 .seeks = DEFAULT_SEEKS * 10,
2862};
2863
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002864static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002865{
2866 if (pte_chain_cache)
2867 kmem_cache_destroy(pte_chain_cache);
2868 if (rmap_desc_cache)
2869 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002870 if (mmu_page_header_cache)
2871 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002872}
2873
Izik Eidus3ee16c82008-03-30 15:17:21 +03002874void kvm_mmu_module_exit(void)
2875{
2876 mmu_destroy_caches();
2877 unregister_shrinker(&mmu_shrinker);
2878}
2879
Avi Kivityb5a33a72007-04-15 16:31:09 +03002880int kvm_mmu_module_init(void)
2881{
2882 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2883 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002884 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002885 if (!pte_chain_cache)
2886 goto nomem;
2887 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2888 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002889 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002890 if (!rmap_desc_cache)
2891 goto nomem;
2892
Avi Kivityd3d25b02007-05-30 12:34:53 +03002893 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2894 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002895 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002896 if (!mmu_page_header_cache)
2897 goto nomem;
2898
Izik Eidus3ee16c82008-03-30 15:17:21 +03002899 register_shrinker(&mmu_shrinker);
2900
Avi Kivityb5a33a72007-04-15 16:31:09 +03002901 return 0;
2902
2903nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002904 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002905 return -ENOMEM;
2906}
2907
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002908/*
2909 * Caculate mmu pages needed for kvm.
2910 */
2911unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2912{
2913 int i;
2914 unsigned int nr_mmu_pages;
2915 unsigned int nr_pages = 0;
2916
2917 for (i = 0; i < kvm->nmemslots; i++)
2918 nr_pages += kvm->memslots[i].npages;
2919
2920 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2921 nr_mmu_pages = max(nr_mmu_pages,
2922 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2923
2924 return nr_mmu_pages;
2925}
2926
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002927static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2928 unsigned len)
2929{
2930 if (len > buffer->len)
2931 return NULL;
2932 return buffer->ptr;
2933}
2934
2935static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2936 unsigned len)
2937{
2938 void *ret;
2939
2940 ret = pv_mmu_peek_buffer(buffer, len);
2941 if (!ret)
2942 return ret;
2943 buffer->ptr += len;
2944 buffer->len -= len;
2945 buffer->processed += len;
2946 return ret;
2947}
2948
2949static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2950 gpa_t addr, gpa_t value)
2951{
2952 int bytes = 8;
2953 int r;
2954
2955 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2956 bytes = 4;
2957
2958 r = mmu_topup_memory_caches(vcpu);
2959 if (r)
2960 return r;
2961
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002962 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002963 return -EFAULT;
2964
2965 return 1;
2966}
2967
2968static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2969{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002970 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002971 return 1;
2972}
2973
2974static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2975{
2976 spin_lock(&vcpu->kvm->mmu_lock);
2977 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2978 spin_unlock(&vcpu->kvm->mmu_lock);
2979 return 1;
2980}
2981
2982static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2983 struct kvm_pv_mmu_op_buffer *buffer)
2984{
2985 struct kvm_mmu_op_header *header;
2986
2987 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2988 if (!header)
2989 return 0;
2990 switch (header->op) {
2991 case KVM_MMU_OP_WRITE_PTE: {
2992 struct kvm_mmu_op_write_pte *wpte;
2993
2994 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2995 if (!wpte)
2996 return 0;
2997 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2998 wpte->pte_val);
2999 }
3000 case KVM_MMU_OP_FLUSH_TLB: {
3001 struct kvm_mmu_op_flush_tlb *ftlb;
3002
3003 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3004 if (!ftlb)
3005 return 0;
3006 return kvm_pv_mmu_flush_tlb(vcpu);
3007 }
3008 case KVM_MMU_OP_RELEASE_PT: {
3009 struct kvm_mmu_op_release_pt *rpt;
3010
3011 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3012 if (!rpt)
3013 return 0;
3014 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3015 }
3016 default: return 0;
3017 }
3018}
3019
3020int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3021 gpa_t addr, unsigned long *ret)
3022{
3023 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003024 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003025
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003026 buffer->ptr = buffer->buf;
3027 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3028 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003029
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003030 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003031 if (r)
3032 goto out;
3033
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003034 while (buffer->len) {
3035 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003036 if (r < 0)
3037 goto out;
3038 if (r == 0)
3039 break;
3040 }
3041
3042 r = 1;
3043out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003044 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003045 return r;
3046}
3047
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003048int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3049{
3050 struct kvm_shadow_walk_iterator iterator;
3051 int nr_sptes = 0;
3052
3053 spin_lock(&vcpu->kvm->mmu_lock);
3054 for_each_shadow_entry(vcpu, addr, iterator) {
3055 sptes[iterator.level-1] = *iterator.sptep;
3056 nr_sptes++;
3057 if (!is_shadow_present_pte(*iterator.sptep))
3058 break;
3059 }
3060 spin_unlock(&vcpu->kvm->mmu_lock);
3061
3062 return nr_sptes;
3063}
3064EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3065
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003066#ifdef AUDIT
3067
3068static const char *audit_msg;
3069
3070static gva_t canonicalize(gva_t gva)
3071{
3072#ifdef CONFIG_X86_64
3073 gva = (long long)(gva << 16) >> 16;
3074#endif
3075 return gva;
3076}
3077
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003078
3079typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3080 u64 *sptep);
3081
3082static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3083 inspect_spte_fn fn)
3084{
3085 int i;
3086
3087 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3088 u64 ent = sp->spt[i];
3089
3090 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003091 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003092 struct kvm_mmu_page *child;
3093 child = page_header(ent & PT64_BASE_ADDR_MASK);
3094 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003095 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003096 fn(kvm, sp, &sp->spt[i]);
3097 }
3098 }
3099}
3100
3101static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3102{
3103 int i;
3104 struct kvm_mmu_page *sp;
3105
3106 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3107 return;
3108 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3109 hpa_t root = vcpu->arch.mmu.root_hpa;
3110 sp = page_header(root);
3111 __mmu_spte_walk(vcpu->kvm, sp, fn);
3112 return;
3113 }
3114 for (i = 0; i < 4; ++i) {
3115 hpa_t root = vcpu->arch.mmu.pae_root[i];
3116
3117 if (root && VALID_PAGE(root)) {
3118 root &= PT64_BASE_ADDR_MASK;
3119 sp = page_header(root);
3120 __mmu_spte_walk(vcpu->kvm, sp, fn);
3121 }
3122 }
3123 return;
3124}
3125
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003126static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3127 gva_t va, int level)
3128{
3129 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3130 int i;
3131 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3132
3133 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3134 u64 ent = pt[i];
3135
Avi Kivityc7addb92007-09-16 18:58:32 +02003136 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003137 continue;
3138
3139 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003140 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3141 audit_mappings_page(vcpu, ent, va, level - 1);
3142 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003143 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003144 gfn_t gfn = gpa >> PAGE_SHIFT;
3145 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3146 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003147
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003148 if (is_error_pfn(pfn)) {
3149 kvm_release_pfn_clean(pfn);
3150 continue;
3151 }
3152
Avi Kivityc7addb92007-09-16 18:58:32 +02003153 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003154 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003155 printk(KERN_ERR "xx audit error: (%s) levels %d"
3156 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003157 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003158 va, gpa, hpa, ent,
3159 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003160 else if (ent == shadow_notrap_nonpresent_pte
3161 && !is_error_hpa(hpa))
3162 printk(KERN_ERR "audit: (%s) notrap shadow,"
3163 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003164 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003165
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003166 }
3167 }
3168}
3169
3170static void audit_mappings(struct kvm_vcpu *vcpu)
3171{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003172 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003173
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003174 if (vcpu->arch.mmu.root_level == 4)
3175 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003176 else
3177 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003178 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003179 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003180 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003181 i << 30,
3182 2);
3183}
3184
3185static int count_rmaps(struct kvm_vcpu *vcpu)
3186{
3187 int nmaps = 0;
3188 int i, j, k;
3189
3190 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3191 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3192 struct kvm_rmap_desc *d;
3193
3194 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003195 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003196
Izik Eidus290fc382007-09-27 14:11:22 +02003197 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003198 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003199 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003200 ++nmaps;
3201 continue;
3202 }
Izik Eidus290fc382007-09-27 14:11:22 +02003203 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003204 while (d) {
3205 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003206 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003207 ++nmaps;
3208 else
3209 break;
3210 d = d->more;
3211 }
3212 }
3213 }
3214 return nmaps;
3215}
3216
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003217void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003218{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003219 unsigned long *rmapp;
3220 struct kvm_mmu_page *rev_sp;
3221 gfn_t gfn;
3222
3223 if (*sptep & PT_WRITABLE_MASK) {
3224 rev_sp = page_header(__pa(sptep));
3225 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3226
3227 if (!gfn_to_memslot(kvm, gfn)) {
3228 if (!printk_ratelimit())
3229 return;
3230 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3231 audit_msg, gfn);
3232 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3233 audit_msg, sptep - rev_sp->spt,
3234 rev_sp->gfn);
3235 dump_stack();
3236 return;
3237 }
3238
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003239 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3240 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003241 if (!*rmapp) {
3242 if (!printk_ratelimit())
3243 return;
3244 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3245 audit_msg, *sptep);
3246 dump_stack();
3247 }
3248 }
3249
3250}
3251
3252void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3253{
3254 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3255}
3256
3257static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3258{
Avi Kivity4db35312007-11-21 15:28:32 +02003259 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003260 int i;
3261
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003262 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003263 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003264
Avi Kivity4db35312007-11-21 15:28:32 +02003265 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003266 continue;
3267
3268 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3269 u64 ent = pt[i];
3270
3271 if (!(ent & PT_PRESENT_MASK))
3272 continue;
3273 if (!(ent & PT_WRITABLE_MASK))
3274 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003275 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003276 }
3277 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003278 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003279}
3280
3281static void audit_rmap(struct kvm_vcpu *vcpu)
3282{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003283 check_writable_mappings_rmap(vcpu);
3284 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003285}
3286
3287static void audit_write_protection(struct kvm_vcpu *vcpu)
3288{
Avi Kivity4db35312007-11-21 15:28:32 +02003289 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003290 struct kvm_memory_slot *slot;
3291 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003292 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003293 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003294
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003295 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003296 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003297 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003298 if (sp->unsync)
3299 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003300
Avi Kivity4db35312007-11-21 15:28:32 +02003301 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003302 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003303 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003304
3305 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3306 while (spte) {
3307 if (*spte & PT_WRITABLE_MASK)
3308 printk(KERN_ERR "%s: (%s) shadow page has "
3309 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003310 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003311 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003312 spte = rmap_next(vcpu->kvm, rmapp, spte);
3313 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003314 }
3315}
3316
3317static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3318{
3319 int olddbg = dbg;
3320
3321 dbg = 0;
3322 audit_msg = msg;
3323 audit_rmap(vcpu);
3324 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003325 if (strcmp("pre pte write", audit_msg) != 0)
3326 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003327 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003328 dbg = olddbg;
3329}
3330
3331#endif