blob: c0dda6447b9fdd024b06bd7dd49a0e4366795a13 [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 Kivityda9285212007-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 Kivityf6e2c02b2009-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
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001125 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1126 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001127 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1129 kvm_mmu_zap_page(vcpu->kvm, sp);
1130 return 1;
1131 }
1132
1133 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001134 return 0;
1135}
1136
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001137struct mmu_page_path {
1138 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1139 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001140};
1141
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001142#define for_each_sp(pvec, sp, parents, i) \
1143 for (i = mmu_pages_next(&pvec, &parents, -1), \
1144 sp = pvec.page[i].sp; \
1145 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1146 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001147
Hannes Edercded19f2009-02-21 02:19:13 +01001148static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1149 struct mmu_page_path *parents,
1150 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001151{
1152 int n;
1153
1154 for (n = i+1; n < pvec->nr; n++) {
1155 struct kvm_mmu_page *sp = pvec->page[n].sp;
1156
1157 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1158 parents->idx[0] = pvec->page[n].idx;
1159 return n;
1160 }
1161
1162 parents->parent[sp->role.level-2] = sp;
1163 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1164 }
1165
1166 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001167}
1168
Hannes Edercded19f2009-02-21 02:19:13 +01001169static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001170{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001171 struct kvm_mmu_page *sp;
1172 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001173
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001174 do {
1175 unsigned int idx = parents->idx[level];
1176
1177 sp = parents->parent[level];
1178 if (!sp)
1179 return;
1180
1181 --sp->unsync_children;
1182 WARN_ON((int)sp->unsync_children < 0);
1183 __clear_bit(idx, sp->unsync_child_bitmap);
1184 level++;
1185 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1186}
1187
1188static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1189 struct mmu_page_path *parents,
1190 struct kvm_mmu_pages *pvec)
1191{
1192 parents->parent[parent->role.level-1] = NULL;
1193 pvec->nr = 0;
1194}
1195
1196static void mmu_sync_children(struct kvm_vcpu *vcpu,
1197 struct kvm_mmu_page *parent)
1198{
1199 int i;
1200 struct kvm_mmu_page *sp;
1201 struct mmu_page_path parents;
1202 struct kvm_mmu_pages pages;
1203
1204 kvm_mmu_pages_init(parent, &parents, &pages);
1205 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001206 int protected = 0;
1207
1208 for_each_sp(pages, sp, parents, i)
1209 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1210
1211 if (protected)
1212 kvm_flush_remote_tlbs(vcpu->kvm);
1213
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001214 for_each_sp(pages, sp, parents, i) {
1215 kvm_sync_page(vcpu, sp);
1216 mmu_pages_clear_parents(&parents);
1217 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001218 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001219 kvm_mmu_pages_init(parent, &parents, &pages);
1220 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221}
1222
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001223static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1224 gfn_t gfn,
1225 gva_t gaddr,
1226 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001227 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001228 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001229 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001230{
1231 union kvm_mmu_page_role role;
1232 unsigned index;
1233 unsigned quadrant;
1234 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001235 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001236 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001237
Avi Kivitya770f6f2008-12-21 19:20:09 +02001238 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001239 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001240 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001241 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001242 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001243 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1244 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1245 role.quadrant = quadrant;
1246 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001247 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001248 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001249 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001250 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001251 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1252 if (sp->gfn == gfn) {
1253 if (sp->unsync)
1254 if (kvm_sync_page(vcpu, sp))
1255 continue;
1256
1257 if (sp->role.word != role.word)
1258 continue;
1259
Avi Kivity4db35312007-11-21 15:28:32 +02001260 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001261 if (sp->unsync_children) {
1262 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1263 kvm_mmu_mark_parents_unsync(vcpu, sp);
1264 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001265 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001266 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001267 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001268 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001269 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1270 if (!sp)
1271 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001272 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001273 sp->gfn = gfn;
1274 sp->role = role;
1275 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001276 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001277 if (rmap_write_protect(vcpu->kvm, gfn))
1278 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001279 account_shadowed(vcpu->kvm, gfn);
1280 }
Avi Kivity131d8272008-05-29 14:56:28 +03001281 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1282 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1283 else
1284 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001285 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001286}
1287
Avi Kivity2d111232008-12-25 14:39:47 +02001288static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1289 struct kvm_vcpu *vcpu, u64 addr)
1290{
1291 iterator->addr = addr;
1292 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1293 iterator->level = vcpu->arch.mmu.shadow_root_level;
1294 if (iterator->level == PT32E_ROOT_LEVEL) {
1295 iterator->shadow_addr
1296 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1297 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1298 --iterator->level;
1299 if (!iterator->shadow_addr)
1300 iterator->level = 0;
1301 }
1302}
1303
1304static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1305{
1306 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1307 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001308
1309 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1310 if (is_large_pte(*iterator->sptep))
1311 return false;
1312
Avi Kivity2d111232008-12-25 14:39:47 +02001313 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1314 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1315 return true;
1316}
1317
1318static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1319{
1320 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1321 --iterator->level;
1322}
1323
Avi Kivity90cb0522007-07-17 13:04:56 +03001324static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001325 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001326{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001327 unsigned i;
1328 u64 *pt;
1329 u64 ent;
1330
Avi Kivity4db35312007-11-21 15:28:32 +02001331 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001332
Avi Kivity697fe2e2007-01-05 16:36:46 -08001333 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1334 ent = pt[i];
1335
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001336 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001337 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001338 ent &= PT64_BASE_ADDR_MASK;
1339 mmu_page_remove_parent_pte(page_header(ent),
1340 &pt[i]);
1341 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001342 if (is_large_pte(ent))
1343 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001344 rmap_remove(kvm, &pt[i]);
1345 }
1346 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001347 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001348 }
Avi Kivitya4360362007-01-05 16:36:45 -08001349}
1350
Avi Kivity4db35312007-11-21 15:28:32 +02001351static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001352{
Avi Kivity4db35312007-11-21 15:28:32 +02001353 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001354}
1355
Avi Kivity12b7d282007-09-23 14:10:49 +02001356static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1357{
1358 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001359 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001360
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001361 kvm_for_each_vcpu(i, vcpu, kvm)
1362 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001363}
1364
Avi Kivity31aa2b42008-07-11 17:59:46 +03001365static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001366{
1367 u64 *parent_pte;
1368
Avi Kivity4db35312007-11-21 15:28:32 +02001369 while (sp->multimapped || sp->parent_pte) {
1370 if (!sp->multimapped)
1371 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001372 else {
1373 struct kvm_pte_chain *chain;
1374
Avi Kivity4db35312007-11-21 15:28:32 +02001375 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001376 struct kvm_pte_chain, link);
1377 parent_pte = chain->parent_ptes[0];
1378 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001379 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001380 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001381 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001382 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001383}
1384
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001385static int mmu_zap_unsync_children(struct kvm *kvm,
1386 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001387{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001388 int i, zapped = 0;
1389 struct mmu_page_path parents;
1390 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001391
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001392 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001393 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001394
1395 kvm_mmu_pages_init(parent, &parents, &pages);
1396 while (mmu_unsync_walk(parent, &pages)) {
1397 struct kvm_mmu_page *sp;
1398
1399 for_each_sp(pages, sp, parents, i) {
1400 kvm_mmu_zap_page(kvm, sp);
1401 mmu_pages_clear_parents(&parents);
1402 }
1403 zapped += pages.nr;
1404 kvm_mmu_pages_init(parent, &parents, &pages);
1405 }
1406
1407 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001408}
1409
Marcelo Tosatti07385412008-09-23 13:18:37 -03001410static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001411{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001412 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001413 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001414 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001415 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001416 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001417 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001418 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001419 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001420 if (sp->unsync)
1421 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001422 if (!sp->root_count) {
1423 hlist_del(&sp->hash_link);
1424 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001425 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001426 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001427 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001428 kvm_reload_remote_mmus(kvm);
1429 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001430 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001431 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001432}
1433
Izik Eidus82ce2c92007-10-02 18:52:55 +02001434/*
1435 * Changing the number of mmu pages allocated to the vm
1436 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1437 */
1438void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1439{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001440 int used_pages;
1441
1442 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1443 used_pages = max(0, used_pages);
1444
Izik Eidus82ce2c92007-10-02 18:52:55 +02001445 /*
1446 * If we set the number of mmu pages to be smaller be than the
1447 * number of actived pages , we must to free some mmu pages before we
1448 * change the value
1449 */
1450
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001451 if (used_pages > kvm_nr_mmu_pages) {
1452 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001453 struct kvm_mmu_page *page;
1454
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001455 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001456 struct kvm_mmu_page, link);
1457 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001458 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001459 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001460 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001461 }
1462 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001463 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1464 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001465
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001466 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001467}
1468
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001469static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001470{
1471 unsigned index;
1472 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001473 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001474 struct hlist_node *node, *n;
1475 int r;
1476
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001477 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001478 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001479 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001480 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001481 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001482 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001483 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001484 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001485 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001486 if (kvm_mmu_zap_page(kvm, sp))
1487 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001488 }
1489 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001490}
1491
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001492static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001493{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001494 unsigned index;
1495 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001496 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001497 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001498
Avi Kivity4677a3b2009-01-06 13:00:27 +02001499 index = kvm_page_table_hashfn(gfn);
1500 bucket = &kvm->arch.mmu_page_hash[index];
1501 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001502 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001503 && !sp->role.invalid) {
1504 pgprintk("%s: zap %lx %x\n",
1505 __func__, gfn, sp->role.word);
1506 kvm_mmu_zap_page(kvm, sp);
1507 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001508 }
1509}
1510
Avi Kivity38c335f2007-11-21 14:20:22 +02001511static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512{
Avi Kivity38c335f2007-11-21 14:20:22 +02001513 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001514 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515
Sheng Yang291f26b2008-10-16 17:30:57 +08001516 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517}
1518
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001519static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1520{
1521 int i;
1522 u64 *pt = sp->spt;
1523
1524 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1525 return;
1526
1527 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1528 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001529 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001530 }
1531}
1532
Avi Kivity039576c2007-03-20 12:46:50 +02001533struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1534{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001535 struct page *page;
1536
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001537 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001538
1539 if (gpa == UNMAPPED_GVA)
1540 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001541
Izik Eidus72dc67a2008-02-10 18:04:15 +02001542 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001543
1544 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001545}
1546
Sheng Yang74be52e2008-10-09 16:01:56 +08001547/*
1548 * The function is based on mtrr_type_lookup() in
1549 * arch/x86/kernel/cpu/mtrr/generic.c
1550 */
1551static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1552 u64 start, u64 end)
1553{
1554 int i;
1555 u64 base, mask;
1556 u8 prev_match, curr_match;
1557 int num_var_ranges = KVM_NR_VAR_MTRR;
1558
1559 if (!mtrr_state->enabled)
1560 return 0xFF;
1561
1562 /* Make end inclusive end, instead of exclusive */
1563 end--;
1564
1565 /* Look in fixed ranges. Just return the type as per start */
1566 if (mtrr_state->have_fixed && (start < 0x100000)) {
1567 int idx;
1568
1569 if (start < 0x80000) {
1570 idx = 0;
1571 idx += (start >> 16);
1572 return mtrr_state->fixed_ranges[idx];
1573 } else if (start < 0xC0000) {
1574 idx = 1 * 8;
1575 idx += ((start - 0x80000) >> 14);
1576 return mtrr_state->fixed_ranges[idx];
1577 } else if (start < 0x1000000) {
1578 idx = 3 * 8;
1579 idx += ((start - 0xC0000) >> 12);
1580 return mtrr_state->fixed_ranges[idx];
1581 }
1582 }
1583
1584 /*
1585 * Look in variable ranges
1586 * Look of multiple ranges matching this address and pick type
1587 * as per MTRR precedence
1588 */
1589 if (!(mtrr_state->enabled & 2))
1590 return mtrr_state->def_type;
1591
1592 prev_match = 0xFF;
1593 for (i = 0; i < num_var_ranges; ++i) {
1594 unsigned short start_state, end_state;
1595
1596 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1597 continue;
1598
1599 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1600 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1601 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1602 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1603
1604 start_state = ((start & mask) == (base & mask));
1605 end_state = ((end & mask) == (base & mask));
1606 if (start_state != end_state)
1607 return 0xFE;
1608
1609 if ((start & mask) != (base & mask))
1610 continue;
1611
1612 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1613 if (prev_match == 0xFF) {
1614 prev_match = curr_match;
1615 continue;
1616 }
1617
1618 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1619 curr_match == MTRR_TYPE_UNCACHABLE)
1620 return MTRR_TYPE_UNCACHABLE;
1621
1622 if ((prev_match == MTRR_TYPE_WRBACK &&
1623 curr_match == MTRR_TYPE_WRTHROUGH) ||
1624 (prev_match == MTRR_TYPE_WRTHROUGH &&
1625 curr_match == MTRR_TYPE_WRBACK)) {
1626 prev_match = MTRR_TYPE_WRTHROUGH;
1627 curr_match = MTRR_TYPE_WRTHROUGH;
1628 }
1629
1630 if (prev_match != curr_match)
1631 return MTRR_TYPE_UNCACHABLE;
1632 }
1633
1634 if (prev_match != 0xFF)
1635 return prev_match;
1636
1637 return mtrr_state->def_type;
1638}
1639
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001640u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001641{
1642 u8 mtrr;
1643
1644 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1645 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1646 if (mtrr == 0xfe || mtrr == 0xff)
1647 mtrr = MTRR_TYPE_WRBACK;
1648 return mtrr;
1649}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001650EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001651
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001652static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1653{
1654 unsigned index;
1655 struct hlist_head *bucket;
1656 struct kvm_mmu_page *s;
1657 struct hlist_node *node, *n;
1658
1659 index = kvm_page_table_hashfn(sp->gfn);
1660 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1661 /* don't unsync if pagetable is shadowed with multiple roles */
1662 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001663 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001664 continue;
1665 if (s->role.word != sp->role.word)
1666 return 1;
1667 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001668 ++vcpu->kvm->stat.mmu_unsync;
1669 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001670
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001671 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001672
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001673 mmu_convert_notrap(sp);
1674 return 0;
1675}
1676
1677static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1678 bool can_unsync)
1679{
1680 struct kvm_mmu_page *shadow;
1681
1682 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1683 if (shadow) {
1684 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1685 return 1;
1686 if (shadow->unsync)
1687 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001688 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001689 return kvm_unsync_page(vcpu, shadow);
1690 return 1;
1691 }
1692 return 0;
1693}
1694
Avi Kivityd555c332009-06-10 14:24:23 +03001695static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001696 unsigned pte_access, int user_fault,
1697 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001698 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001699 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001700{
1701 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001702 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001703
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001704 /*
1705 * We don't set the accessed bit, since we sometimes want to see
1706 * whether the guest actually used the pte (in order to detect
1707 * demand paging).
1708 */
Sheng Yang7b523452008-04-25 21:13:50 +08001709 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001710 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001711 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001712 if (!dirty)
1713 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001714 if (pte_access & ACC_EXEC_MASK)
1715 spte |= shadow_x_mask;
1716 else
1717 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001718 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001719 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001720 if (largepage)
1721 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001722 if (tdp_enabled)
1723 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1724 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001725
Anthony Liguori35149e22008-04-02 14:46:56 -05001726 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727
1728 if ((pte_access & ACC_WRITE_MASK)
1729 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001730
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001731 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1732 ret = 1;
1733 spte = shadow_trap_nonpresent_pte;
1734 goto set_pte;
1735 }
1736
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001737 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001738
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001739 /*
1740 * Optimization: for pte sync, if spte was writable the hash
1741 * lookup is unnecessary (and expensive). Write protection
1742 * is responsibility of mmu_get_page / kvm_sync_page.
1743 * Same reasoning can be applied to dirty page accounting.
1744 */
Avi Kivityd555c332009-06-10 14:24:23 +03001745 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001746 goto set_pte;
1747
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001748 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001749 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001750 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001751 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001752 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001753 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001754 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001755 }
1756 }
1757
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001758 if (pte_access & ACC_WRITE_MASK)
1759 mark_page_dirty(vcpu->kvm, gfn);
1760
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001761set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001762 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001763 return ret;
1764}
1765
Avi Kivityd555c332009-06-10 14:24:23 +03001766static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001767 unsigned pt_access, unsigned pte_access,
1768 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001769 int *ptwrite, int largepage, gfn_t gfn,
1770 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001771{
1772 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001773 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001774 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001775
1776 pgprintk("%s: spte %llx access %x write_fault %d"
1777 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001778 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001779 write_fault, user_fault, gfn);
1780
Avi Kivityd555c332009-06-10 14:24:23 +03001781 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001782 /*
1783 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1784 * the parent of the now unreachable PTE.
1785 */
Avi Kivityd555c332009-06-10 14:24:23 +03001786 if (largepage && !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001787 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001788 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001789
1790 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001791 mmu_page_remove_parent_pte(child, sptep);
1792 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001793 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001794 spte_to_pfn(*sptep), pfn);
1795 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001796 } else
1797 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001798 }
Avi Kivityd555c332009-06-10 14:24:23 +03001799 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001800 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001801 if (write_fault)
1802 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001803 kvm_x86_ops->tlb_flush(vcpu);
1804 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001805
Avi Kivityd555c332009-06-10 14:24:23 +03001806 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001807 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001808 is_large_pte(*sptep)? "2MB" : "4kB",
1809 is_present_pte(*sptep)?"RW":"R", gfn,
1810 *shadow_pte, sptep);
1811 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001812 ++vcpu->kvm->stat.lpages;
1813
Avi Kivityd555c332009-06-10 14:24:23 +03001814 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001815 if (!was_rmapped) {
Avi Kivityd555c332009-06-10 14:24:23 +03001816 rmap_count = rmap_add(vcpu, sptep, gfn, largepage);
1817 if (!is_rmap_spte(*sptep))
Anthony Liguori35149e22008-04-02 14:46:56 -05001818 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001819 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1820 rmap_recycle(vcpu, gfn, largepage);
Izik Eidus75e68e62008-01-12 23:49:09 +02001821 } else {
1822 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001823 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001824 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001825 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001826 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001827 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001828 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001829 vcpu->arch.last_pte_gfn = gfn;
1830 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001831}
1832
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1834{
1835}
1836
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001837static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001838 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839{
Avi Kivity9f652d212008-12-25 14:54:25 +02001840 struct kvm_shadow_walk_iterator iterator;
1841 struct kvm_mmu_page *sp;
1842 int pt_write = 0;
1843 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844
Avi Kivity9f652d212008-12-25 14:54:25 +02001845 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1846 if (iterator.level == PT_PAGE_TABLE_LEVEL
1847 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1848 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1849 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001850 largepage, gfn, pfn, false);
Avi Kivity9f652d212008-12-25 14:54:25 +02001851 ++vcpu->stat.pf_fixed;
1852 break;
1853 }
1854
1855 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1856 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1857 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1858 iterator.level - 1,
1859 1, ACC_ALL, iterator.sptep);
1860 if (!sp) {
1861 pgprintk("nonpaging_map: ENOMEM\n");
1862 kvm_release_pfn_clean(pfn);
1863 return -ENOMEM;
1864 }
1865
Avi Kivityd555c332009-06-10 14:24:23 +03001866 __set_spte(iterator.sptep,
1867 __pa(sp->spt)
1868 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1869 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02001870 }
1871 }
1872 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873}
1874
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001875static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1876{
1877 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001878 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001879 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001880 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001881
Joerg Roedelec04b262009-06-19 15:16:23 +02001882 if (is_largepage_backed(vcpu, gfn &
1883 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
1884 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001885 largepage = 1;
1886 }
1887
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001888 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001889 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001890 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001891
Avi Kivityd196e342008-01-24 11:44:11 +02001892 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001893 if (is_error_pfn(pfn)) {
1894 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001895 return 1;
1896 }
1897
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001898 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001899 if (mmu_notifier_retry(vcpu, mmu_seq))
1900 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001901 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001902 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001903 spin_unlock(&vcpu->kvm->mmu_lock);
1904
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001905
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001906 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001907
1908out_unlock:
1909 spin_unlock(&vcpu->kvm->mmu_lock);
1910 kvm_release_pfn_clean(pfn);
1911 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001912}
1913
1914
Avi Kivity17ac10a2007-01-05 16:36:40 -08001915static void mmu_free_roots(struct kvm_vcpu *vcpu)
1916{
1917 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001918 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001919
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001920 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001921 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001922 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001923 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1924 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001925
Avi Kivity4db35312007-11-21 15:28:32 +02001926 sp = page_header(root);
1927 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001928 if (!sp->root_count && sp->role.invalid)
1929 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001930 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001931 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001932 return;
1933 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001934 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001936
Avi Kivity417726a2007-04-12 17:35:58 +03001937 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001938 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001939 sp = page_header(root);
1940 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001941 if (!sp->root_count && sp->role.invalid)
1942 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001943 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001944 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001945 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001946 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001947 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001948}
1949
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001950static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1951{
1952 int ret = 0;
1953
1954 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1955 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1956 ret = 1;
1957 }
1958
1959 return ret;
1960}
1961
1962static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08001963{
1964 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001965 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001966 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001967 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001968 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001969
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001970 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001971
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001972 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1973 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001974
1975 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001976 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001977 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001978 if (mmu_check_root(vcpu, root_gfn))
1979 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001980 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001981 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001982 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001983 root = __pa(sp->spt);
1984 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001985 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001986 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001987 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001988 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001989 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001990 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001991 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001992 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001993
1994 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001995 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001996 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03001997 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001998 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001999 continue;
2000 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002001 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002002 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002003 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002004 if (mmu_check_root(vcpu, root_gfn))
2005 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002006 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002007 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002008 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002009 root = __pa(sp->spt);
2010 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002012 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002013 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002014 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002015}
2016
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002017static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2018{
2019 int i;
2020 struct kvm_mmu_page *sp;
2021
2022 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2023 return;
2024 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2025 hpa_t root = vcpu->arch.mmu.root_hpa;
2026 sp = page_header(root);
2027 mmu_sync_children(vcpu, sp);
2028 return;
2029 }
2030 for (i = 0; i < 4; ++i) {
2031 hpa_t root = vcpu->arch.mmu.pae_root[i];
2032
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002033 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002034 root &= PT64_BASE_ADDR_MASK;
2035 sp = page_header(root);
2036 mmu_sync_children(vcpu, sp);
2037 }
2038 }
2039}
2040
2041void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2042{
2043 spin_lock(&vcpu->kvm->mmu_lock);
2044 mmu_sync_roots(vcpu);
2045 spin_unlock(&vcpu->kvm->mmu_lock);
2046}
2047
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2049{
2050 return vaddr;
2051}
2052
2053static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002054 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055{
Avi Kivitye8332402007-12-09 18:43:00 +02002056 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002057 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002059 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002060 r = mmu_topup_memory_caches(vcpu);
2061 if (r)
2062 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002063
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002065 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066
Avi Kivitye8332402007-12-09 18:43:00 +02002067 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068
Avi Kivitye8332402007-12-09 18:43:00 +02002069 return nonpaging_map(vcpu, gva & PAGE_MASK,
2070 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071}
2072
Joerg Roedelfb72d162008-02-07 13:47:44 +01002073static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2074 u32 error_code)
2075{
Anthony Liguori35149e22008-04-02 14:46:56 -05002076 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002077 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002078 int largepage = 0;
2079 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002080 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002081
2082 ASSERT(vcpu);
2083 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2084
2085 r = mmu_topup_memory_caches(vcpu);
2086 if (r)
2087 return r;
2088
Joerg Roedelec04b262009-06-19 15:16:23 +02002089 if (is_largepage_backed(vcpu, gfn &
2090 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
2091 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002092 largepage = 1;
2093 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002094 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002095 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002096 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002097 if (is_error_pfn(pfn)) {
2098 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002099 return 1;
2100 }
2101 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002102 if (mmu_notifier_retry(vcpu, mmu_seq))
2103 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002104 kvm_mmu_free_some_pages(vcpu);
2105 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002106 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002107 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002108
2109 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002110
2111out_unlock:
2112 spin_unlock(&vcpu->kvm->mmu_lock);
2113 kvm_release_pfn_clean(pfn);
2114 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002115}
2116
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117static void nonpaging_free(struct kvm_vcpu *vcpu)
2118{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002119 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120}
2121
2122static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2123{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002124 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125
2126 context->new_cr3 = nonpaging_new_cr3;
2127 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128 context->gva_to_gpa = nonpaging_gva_to_gpa;
2129 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002130 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002131 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002132 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002133 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002135 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 return 0;
2137}
2138
Avi Kivityd835dfe2007-11-21 02:57:59 +02002139void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002141 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002142 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143}
2144
2145static void paging_new_cr3(struct kvm_vcpu *vcpu)
2146{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002147 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002148 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149}
2150
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151static void inject_page_fault(struct kvm_vcpu *vcpu,
2152 u64 addr,
2153 u32 err_code)
2154{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002155 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156}
2157
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158static void paging_free(struct kvm_vcpu *vcpu)
2159{
2160 nonpaging_free(vcpu);
2161}
2162
Dong, Eddie82725b22009-03-30 16:21:08 +08002163static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2164{
2165 int bit7;
2166
2167 bit7 = (gpte >> 7) & 1;
2168 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2169}
2170
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171#define PTTYPE 64
2172#include "paging_tmpl.h"
2173#undef PTTYPE
2174
2175#define PTTYPE 32
2176#include "paging_tmpl.h"
2177#undef PTTYPE
2178
Dong, Eddie82725b22009-03-30 16:21:08 +08002179static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2180{
2181 struct kvm_mmu *context = &vcpu->arch.mmu;
2182 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2183 u64 exb_bit_rsvd = 0;
2184
2185 if (!is_nx(vcpu))
2186 exb_bit_rsvd = rsvd_bits(63, 63);
2187 switch (level) {
2188 case PT32_ROOT_LEVEL:
2189 /* no rsvd bits for 2 level 4K page table entries */
2190 context->rsvd_bits_mask[0][1] = 0;
2191 context->rsvd_bits_mask[0][0] = 0;
2192 if (is_cpuid_PSE36())
2193 /* 36bits PSE 4MB page */
2194 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2195 else
2196 /* 32 bits PSE 4MB page */
2197 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002198 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002199 break;
2200 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002201 context->rsvd_bits_mask[0][2] =
2202 rsvd_bits(maxphyaddr, 63) |
2203 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002204 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002205 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002206 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2207 rsvd_bits(maxphyaddr, 62); /* PTE */
2208 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2209 rsvd_bits(maxphyaddr, 62) |
2210 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002211 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002212 break;
2213 case PT64_ROOT_LEVEL:
2214 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2215 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2216 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2217 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2218 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002219 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002220 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2221 rsvd_bits(maxphyaddr, 51);
2222 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2223 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2224 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002225 rsvd_bits(maxphyaddr, 51) |
2226 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002227 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002228 break;
2229 }
2230}
2231
Avi Kivity17ac10a2007-01-05 16:36:40 -08002232static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002234 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235
2236 ASSERT(is_pae(vcpu));
2237 context->new_cr3 = paging_new_cr3;
2238 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002240 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002241 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002242 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002244 context->root_level = level;
2245 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002246 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 return 0;
2248}
2249
Avi Kivity17ac10a2007-01-05 16:36:40 -08002250static int paging64_init_context(struct kvm_vcpu *vcpu)
2251{
Dong, Eddie82725b22009-03-30 16:21:08 +08002252 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002253 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2254}
2255
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256static int paging32_init_context(struct kvm_vcpu *vcpu)
2257{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002258 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259
Dong, Eddie82725b22009-03-30 16:21:08 +08002260 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 context->new_cr3 = paging_new_cr3;
2262 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 context->gva_to_gpa = paging32_gva_to_gpa;
2264 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002265 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002266 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002267 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 context->root_level = PT32_ROOT_LEVEL;
2269 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002270 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 return 0;
2272}
2273
2274static int paging32E_init_context(struct kvm_vcpu *vcpu)
2275{
Dong, Eddie82725b22009-03-30 16:21:08 +08002276 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002277 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278}
2279
Joerg Roedelfb72d162008-02-07 13:47:44 +01002280static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2281{
2282 struct kvm_mmu *context = &vcpu->arch.mmu;
2283
2284 context->new_cr3 = nonpaging_new_cr3;
2285 context->page_fault = tdp_page_fault;
2286 context->free = nonpaging_free;
2287 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002288 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002289 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002290 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002291 context->root_hpa = INVALID_PAGE;
2292
2293 if (!is_paging(vcpu)) {
2294 context->gva_to_gpa = nonpaging_gva_to_gpa;
2295 context->root_level = 0;
2296 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002297 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002298 context->gva_to_gpa = paging64_gva_to_gpa;
2299 context->root_level = PT64_ROOT_LEVEL;
2300 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002301 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002302 context->gva_to_gpa = paging64_gva_to_gpa;
2303 context->root_level = PT32E_ROOT_LEVEL;
2304 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002305 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002306 context->gva_to_gpa = paging32_gva_to_gpa;
2307 context->root_level = PT32_ROOT_LEVEL;
2308 }
2309
2310 return 0;
2311}
2312
2313static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002315 int r;
2316
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002318 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319
2320 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002321 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002322 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002323 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002325 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002327 r = paging32_init_context(vcpu);
2328
2329 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2330
2331 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332}
2333
Joerg Roedelfb72d162008-02-07 13:47:44 +01002334static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2335{
Anthony Liguori35149e22008-04-02 14:46:56 -05002336 vcpu->arch.update_pte.pfn = bad_pfn;
2337
Joerg Roedelfb72d162008-02-07 13:47:44 +01002338 if (tdp_enabled)
2339 return init_kvm_tdp_mmu(vcpu);
2340 else
2341 return init_kvm_softmmu(vcpu);
2342}
2343
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2345{
2346 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002347 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2348 vcpu->arch.mmu.free(vcpu);
2349 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 }
2351}
2352
2353int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2354{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002355 destroy_kvm_mmu(vcpu);
2356 return init_kvm_mmu(vcpu);
2357}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002358EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002359
2360int kvm_mmu_load(struct kvm_vcpu *vcpu)
2361{
Avi Kivity714b93d2007-01-05 16:36:53 -08002362 int r;
2363
Avi Kivitye2dec932007-01-05 16:36:54 -08002364 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002365 if (r)
2366 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002367 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002368 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002369 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002370 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002371 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002372 if (r)
2373 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002374 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002375 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002376out:
2377 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002379EXPORT_SYMBOL_GPL(kvm_mmu_load);
2380
2381void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2382{
2383 mmu_free_roots(vcpu);
2384}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385
Avi Kivity09072da2007-05-01 14:16:52 +03002386static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002387 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002388 u64 *spte)
2389{
2390 u64 pte;
2391 struct kvm_mmu_page *child;
2392
2393 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002394 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002395 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002396 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002397 else {
2398 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002399 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002400 }
2401 }
Avi Kivityd555c332009-06-10 14:24:23 +03002402 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002403 if (is_large_pte(pte))
2404 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002405}
2406
Avi Kivity00284252007-05-01 16:53:31 +03002407static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002408 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002409 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002410 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002411{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002412 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2413 if (!vcpu->arch.update_pte.largepage ||
2414 sp->role.glevels == PT32_ROOT_LEVEL) {
2415 ++vcpu->kvm->stat.mmu_pde_zapped;
2416 return;
2417 }
2418 }
Avi Kivity00284252007-05-01 16:53:31 +03002419
Avi Kivity4cee5762007-11-18 16:37:07 +02002420 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002421 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002422 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002423 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002424 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002425}
2426
Avi Kivity79539ce2007-11-21 02:06:21 +02002427static bool need_remote_flush(u64 old, u64 new)
2428{
2429 if (!is_shadow_present_pte(old))
2430 return false;
2431 if (!is_shadow_present_pte(new))
2432 return true;
2433 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2434 return true;
2435 old ^= PT64_NX_MASK;
2436 new ^= PT64_NX_MASK;
2437 return (old & ~new & PT64_PERM_MASK) != 0;
2438}
2439
2440static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2441{
2442 if (need_remote_flush(old, new))
2443 kvm_flush_remote_tlbs(vcpu->kvm);
2444 else
2445 kvm_mmu_flush_tlb(vcpu);
2446}
2447
Avi Kivity12b7d282007-09-23 14:10:49 +02002448static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2449{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002450 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002451
Sheng Yang7b523452008-04-25 21:13:50 +08002452 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002453}
2454
Avi Kivityd7824ff2007-12-30 12:29:05 +02002455static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2456 const u8 *new, int bytes)
2457{
2458 gfn_t gfn;
2459 int r;
2460 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002461 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002462
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002463 vcpu->arch.update_pte.largepage = 0;
2464
Avi Kivityd7824ff2007-12-30 12:29:05 +02002465 if (bytes != 4 && bytes != 8)
2466 return;
2467
2468 /*
2469 * Assume that the pte write on a page table of the same type
2470 * as the current vcpu paging mode. This is nearly always true
2471 * (might be false while changing modes). Note it is verified later
2472 * by update_pte().
2473 */
2474 if (is_pae(vcpu)) {
2475 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2476 if ((bytes == 4) && (gpa % 4 == 0)) {
2477 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2478 if (r)
2479 return;
2480 memcpy((void *)&gpte + (gpa % 8), new, 4);
2481 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2482 memcpy((void *)&gpte, new, 8);
2483 }
2484 } else {
2485 if ((bytes == 4) && (gpa % 4 == 0))
2486 memcpy((void *)&gpte, new, 4);
2487 }
Avi Kivity43a37952009-06-10 14:12:05 +03002488 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002489 return;
2490 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002491
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002492 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
Joerg Roedelec04b262009-06-19 15:16:23 +02002493 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002494 vcpu->arch.update_pte.largepage = 1;
2495 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002496 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002497 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002498 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002499
Anthony Liguori35149e22008-04-02 14:46:56 -05002500 if (is_error_pfn(pfn)) {
2501 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002502 return;
2503 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002504 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002505 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002506}
2507
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002508static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2509{
2510 u64 *spte = vcpu->arch.last_pte_updated;
2511
2512 if (spte
2513 && vcpu->arch.last_pte_gfn == gfn
2514 && shadow_accessed_mask
2515 && !(*spte & shadow_accessed_mask)
2516 && is_shadow_present_pte(*spte))
2517 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2518}
2519
Avi Kivity09072da2007-05-01 14:16:52 +03002520void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002521 const u8 *new, int bytes,
2522 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002523{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002524 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002525 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002526 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002527 struct hlist_head *bucket;
2528 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002529 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002530 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002531 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002532 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002533 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002534 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002535 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002536 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002537 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002538 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002539 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002540
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002541 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002542 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002543 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002544 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002545 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002546 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002547 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002548 if (guest_initiated) {
2549 if (gfn == vcpu->arch.last_pt_write_gfn
2550 && !last_updated_pte_accessed(vcpu)) {
2551 ++vcpu->arch.last_pt_write_count;
2552 if (vcpu->arch.last_pt_write_count >= 3)
2553 flooded = 1;
2554 } else {
2555 vcpu->arch.last_pt_write_gfn = gfn;
2556 vcpu->arch.last_pt_write_count = 1;
2557 vcpu->arch.last_pte_updated = NULL;
2558 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002559 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002560 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002561 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002562 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002563 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002564 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002565 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002566 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002567 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002568 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002569 /*
2570 * Misaligned accesses are too much trouble to fix
2571 * up; also, they usually indicate a page is not used
2572 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002573 *
2574 * If we're seeing too many writes to a page,
2575 * it may no longer be a page table, or we may be
2576 * forking, in which case it is better to unmap the
2577 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002578 */
2579 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002580 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002581 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2582 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002583 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002584 continue;
2585 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002586 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002587 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002588 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002589 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002590 page_offset <<= 1; /* 32->64 */
2591 /*
2592 * A 32-bit pde maps 4MB while the shadow pdes map
2593 * only 2MB. So we need to double the offset again
2594 * and zap two pdes instead of one.
2595 */
2596 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002597 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002598 page_offset <<= 1;
2599 npte = 2;
2600 }
Avi Kivityfce06572007-05-01 16:44:05 +03002601 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002602 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002603 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002604 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002605 }
Avi Kivity4db35312007-11-21 15:28:32 +02002606 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002607 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2608 gentry = 0;
2609 r = kvm_read_guest_atomic(vcpu->kvm,
2610 gpa & ~(u64)(pte_size - 1),
2611 &gentry, pte_size);
2612 new = (const void *)&gentry;
2613 if (r < 0)
2614 new = NULL;
2615 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002616 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002617 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002618 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002619 if (new)
2620 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002621 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002622 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002623 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002624 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002625 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002626 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002627 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2628 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2629 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002630 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002631}
2632
Avi Kivitya4360362007-01-05 16:36:45 -08002633int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2634{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002635 gpa_t gpa;
2636 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002637
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002638 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002639
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002640 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002641 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002642 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002643 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002644}
Avi Kivity577bdc42008-07-19 08:57:05 +03002645EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002646
Avi Kivity22d95b12007-09-14 20:26:06 +03002647void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002648{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002649 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002650 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002651
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002652 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002653 struct kvm_mmu_page, link);
2654 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002655 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002656 }
2657}
Avi Kivityebeace82007-01-05 16:36:47 -08002658
Avi Kivity30677142007-10-28 18:48:59 +02002659int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2660{
2661 int r;
2662 enum emulation_result er;
2663
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002664 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002665 if (r < 0)
2666 goto out;
2667
2668 if (!r) {
2669 r = 1;
2670 goto out;
2671 }
2672
Avi Kivityb733bfb2007-10-28 18:52:05 +02002673 r = mmu_topup_memory_caches(vcpu);
2674 if (r)
2675 goto out;
2676
Avi Kivity30677142007-10-28 18:48:59 +02002677 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002678
2679 switch (er) {
2680 case EMULATE_DONE:
2681 return 1;
2682 case EMULATE_DO_MMIO:
2683 ++vcpu->stat.mmio_exits;
2684 return 0;
2685 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002686 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2687 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2688 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002689 default:
2690 BUG();
2691 }
2692out:
Avi Kivity30677142007-10-28 18:48:59 +02002693 return r;
2694}
2695EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2696
Marcelo Tosattia7052892008-09-23 13:18:35 -03002697void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2698{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002699 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002700 kvm_mmu_flush_tlb(vcpu);
2701 ++vcpu->stat.invlpg;
2702}
2703EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2704
Joerg Roedel18552672008-02-07 13:47:41 +01002705void kvm_enable_tdp(void)
2706{
2707 tdp_enabled = true;
2708}
2709EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2710
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002711void kvm_disable_tdp(void)
2712{
2713 tdp_enabled = false;
2714}
2715EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2716
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717static void free_mmu_pages(struct kvm_vcpu *vcpu)
2718{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002719 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720}
2721
2722static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2723{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002724 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 int i;
2726
2727 ASSERT(vcpu);
2728
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002729 if (vcpu->kvm->arch.n_requested_mmu_pages)
2730 vcpu->kvm->arch.n_free_mmu_pages =
2731 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002732 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002733 vcpu->kvm->arch.n_free_mmu_pages =
2734 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002735 /*
2736 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2737 * Therefore we need to allocate shadow page tables in the first
2738 * 4GB of memory, which happens to fit the DMA32 zone.
2739 */
2740 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2741 if (!page)
2742 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002743 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002744 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002745 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002746
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 return 0;
2748
2749error_1:
2750 free_mmu_pages(vcpu);
2751 return -ENOMEM;
2752}
2753
Ingo Molnar8018c272006-12-29 16:50:01 -08002754int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002757 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758
Ingo Molnar8018c272006-12-29 16:50:01 -08002759 return alloc_mmu_pages(vcpu);
2760}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761
Ingo Molnar8018c272006-12-29 16:50:01 -08002762int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2763{
2764 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002765 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002766
Ingo Molnar8018c272006-12-29 16:50:01 -08002767 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768}
2769
2770void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2771{
2772 ASSERT(vcpu);
2773
2774 destroy_kvm_mmu(vcpu);
2775 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002776 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777}
2778
Avi Kivity90cb0522007-07-17 13:04:56 +03002779void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780{
Avi Kivity4db35312007-11-21 15:28:32 +02002781 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002783 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 int i;
2785 u64 *pt;
2786
Sheng Yang291f26b2008-10-16 17:30:57 +08002787 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 continue;
2789
Avi Kivity4db35312007-11-21 15:28:32 +02002790 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2792 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002793 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 }
Avi Kivity171d5952008-08-27 16:40:51 +03002796 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002798
Avi Kivity90cb0522007-07-17 13:04:56 +03002799void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002800{
Avi Kivity4db35312007-11-21 15:28:32 +02002801 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002802
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002803 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002804 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002805 if (kvm_mmu_zap_page(kvm, sp))
2806 node = container_of(kvm->arch.active_mmu_pages.next,
2807 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002808 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002809
Avi Kivity90cb0522007-07-17 13:04:56 +03002810 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002811}
2812
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002813static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002814{
2815 struct kvm_mmu_page *page;
2816
2817 page = container_of(kvm->arch.active_mmu_pages.prev,
2818 struct kvm_mmu_page, link);
2819 kvm_mmu_zap_page(kvm, page);
2820}
2821
2822static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2823{
2824 struct kvm *kvm;
2825 struct kvm *kvm_freed = NULL;
2826 int cache_count = 0;
2827
2828 spin_lock(&kvm_lock);
2829
2830 list_for_each_entry(kvm, &vm_list, vm_list) {
2831 int npages;
2832
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002833 if (!down_read_trylock(&kvm->slots_lock))
2834 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002835 spin_lock(&kvm->mmu_lock);
2836 npages = kvm->arch.n_alloc_mmu_pages -
2837 kvm->arch.n_free_mmu_pages;
2838 cache_count += npages;
2839 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2840 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2841 cache_count--;
2842 kvm_freed = kvm;
2843 }
2844 nr_to_scan--;
2845
2846 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002847 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002848 }
2849 if (kvm_freed)
2850 list_move_tail(&kvm_freed->vm_list, &vm_list);
2851
2852 spin_unlock(&kvm_lock);
2853
2854 return cache_count;
2855}
2856
2857static struct shrinker mmu_shrinker = {
2858 .shrink = mmu_shrink,
2859 .seeks = DEFAULT_SEEKS * 10,
2860};
2861
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002862static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002863{
2864 if (pte_chain_cache)
2865 kmem_cache_destroy(pte_chain_cache);
2866 if (rmap_desc_cache)
2867 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002868 if (mmu_page_header_cache)
2869 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002870}
2871
Izik Eidus3ee16c82008-03-30 15:17:21 +03002872void kvm_mmu_module_exit(void)
2873{
2874 mmu_destroy_caches();
2875 unregister_shrinker(&mmu_shrinker);
2876}
2877
Avi Kivityb5a33a72007-04-15 16:31:09 +03002878int kvm_mmu_module_init(void)
2879{
2880 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2881 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002882 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002883 if (!pte_chain_cache)
2884 goto nomem;
2885 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2886 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002887 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002888 if (!rmap_desc_cache)
2889 goto nomem;
2890
Avi Kivityd3d25b02007-05-30 12:34:53 +03002891 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2892 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002893 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002894 if (!mmu_page_header_cache)
2895 goto nomem;
2896
Izik Eidus3ee16c82008-03-30 15:17:21 +03002897 register_shrinker(&mmu_shrinker);
2898
Avi Kivityb5a33a72007-04-15 16:31:09 +03002899 return 0;
2900
2901nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002902 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002903 return -ENOMEM;
2904}
2905
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002906/*
2907 * Caculate mmu pages needed for kvm.
2908 */
2909unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2910{
2911 int i;
2912 unsigned int nr_mmu_pages;
2913 unsigned int nr_pages = 0;
2914
2915 for (i = 0; i < kvm->nmemslots; i++)
2916 nr_pages += kvm->memslots[i].npages;
2917
2918 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2919 nr_mmu_pages = max(nr_mmu_pages,
2920 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2921
2922 return nr_mmu_pages;
2923}
2924
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002925static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2926 unsigned len)
2927{
2928 if (len > buffer->len)
2929 return NULL;
2930 return buffer->ptr;
2931}
2932
2933static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2934 unsigned len)
2935{
2936 void *ret;
2937
2938 ret = pv_mmu_peek_buffer(buffer, len);
2939 if (!ret)
2940 return ret;
2941 buffer->ptr += len;
2942 buffer->len -= len;
2943 buffer->processed += len;
2944 return ret;
2945}
2946
2947static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2948 gpa_t addr, gpa_t value)
2949{
2950 int bytes = 8;
2951 int r;
2952
2953 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2954 bytes = 4;
2955
2956 r = mmu_topup_memory_caches(vcpu);
2957 if (r)
2958 return r;
2959
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002960 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002961 return -EFAULT;
2962
2963 return 1;
2964}
2965
2966static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2967{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002968 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002969 return 1;
2970}
2971
2972static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2973{
2974 spin_lock(&vcpu->kvm->mmu_lock);
2975 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2976 spin_unlock(&vcpu->kvm->mmu_lock);
2977 return 1;
2978}
2979
2980static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2981 struct kvm_pv_mmu_op_buffer *buffer)
2982{
2983 struct kvm_mmu_op_header *header;
2984
2985 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2986 if (!header)
2987 return 0;
2988 switch (header->op) {
2989 case KVM_MMU_OP_WRITE_PTE: {
2990 struct kvm_mmu_op_write_pte *wpte;
2991
2992 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2993 if (!wpte)
2994 return 0;
2995 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2996 wpte->pte_val);
2997 }
2998 case KVM_MMU_OP_FLUSH_TLB: {
2999 struct kvm_mmu_op_flush_tlb *ftlb;
3000
3001 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3002 if (!ftlb)
3003 return 0;
3004 return kvm_pv_mmu_flush_tlb(vcpu);
3005 }
3006 case KVM_MMU_OP_RELEASE_PT: {
3007 struct kvm_mmu_op_release_pt *rpt;
3008
3009 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3010 if (!rpt)
3011 return 0;
3012 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3013 }
3014 default: return 0;
3015 }
3016}
3017
3018int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3019 gpa_t addr, unsigned long *ret)
3020{
3021 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003022 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003023
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003024 buffer->ptr = buffer->buf;
3025 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3026 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003027
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003028 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003029 if (r)
3030 goto out;
3031
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003032 while (buffer->len) {
3033 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003034 if (r < 0)
3035 goto out;
3036 if (r == 0)
3037 break;
3038 }
3039
3040 r = 1;
3041out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003042 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003043 return r;
3044}
3045
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003046int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3047{
3048 struct kvm_shadow_walk_iterator iterator;
3049 int nr_sptes = 0;
3050
3051 spin_lock(&vcpu->kvm->mmu_lock);
3052 for_each_shadow_entry(vcpu, addr, iterator) {
3053 sptes[iterator.level-1] = *iterator.sptep;
3054 nr_sptes++;
3055 if (!is_shadow_present_pte(*iterator.sptep))
3056 break;
3057 }
3058 spin_unlock(&vcpu->kvm->mmu_lock);
3059
3060 return nr_sptes;
3061}
3062EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3063
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003064#ifdef AUDIT
3065
3066static const char *audit_msg;
3067
3068static gva_t canonicalize(gva_t gva)
3069{
3070#ifdef CONFIG_X86_64
3071 gva = (long long)(gva << 16) >> 16;
3072#endif
3073 return gva;
3074}
3075
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003076
3077typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3078 u64 *sptep);
3079
3080static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3081 inspect_spte_fn fn)
3082{
3083 int i;
3084
3085 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3086 u64 ent = sp->spt[i];
3087
3088 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003089 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003090 struct kvm_mmu_page *child;
3091 child = page_header(ent & PT64_BASE_ADDR_MASK);
3092 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003093 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003094 fn(kvm, sp, &sp->spt[i]);
3095 }
3096 }
3097}
3098
3099static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3100{
3101 int i;
3102 struct kvm_mmu_page *sp;
3103
3104 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3105 return;
3106 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3107 hpa_t root = vcpu->arch.mmu.root_hpa;
3108 sp = page_header(root);
3109 __mmu_spte_walk(vcpu->kvm, sp, fn);
3110 return;
3111 }
3112 for (i = 0; i < 4; ++i) {
3113 hpa_t root = vcpu->arch.mmu.pae_root[i];
3114
3115 if (root && VALID_PAGE(root)) {
3116 root &= PT64_BASE_ADDR_MASK;
3117 sp = page_header(root);
3118 __mmu_spte_walk(vcpu->kvm, sp, fn);
3119 }
3120 }
3121 return;
3122}
3123
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003124static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3125 gva_t va, int level)
3126{
3127 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3128 int i;
3129 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3130
3131 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3132 u64 ent = pt[i];
3133
Avi Kivityc7addb92007-09-16 18:58:32 +02003134 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003135 continue;
3136
3137 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003138 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3139 audit_mappings_page(vcpu, ent, va, level - 1);
3140 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003141 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003142 gfn_t gfn = gpa >> PAGE_SHIFT;
3143 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3144 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003145
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003146 if (is_error_pfn(pfn)) {
3147 kvm_release_pfn_clean(pfn);
3148 continue;
3149 }
3150
Avi Kivityc7addb92007-09-16 18:58:32 +02003151 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003152 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003153 printk(KERN_ERR "xx audit error: (%s) levels %d"
3154 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003155 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003156 va, gpa, hpa, ent,
3157 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003158 else if (ent == shadow_notrap_nonpresent_pte
3159 && !is_error_hpa(hpa))
3160 printk(KERN_ERR "audit: (%s) notrap shadow,"
3161 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003162 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003163
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003164 }
3165 }
3166}
3167
3168static void audit_mappings(struct kvm_vcpu *vcpu)
3169{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003170 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003171
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003172 if (vcpu->arch.mmu.root_level == 4)
3173 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003174 else
3175 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003176 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003177 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003178 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003179 i << 30,
3180 2);
3181}
3182
3183static int count_rmaps(struct kvm_vcpu *vcpu)
3184{
3185 int nmaps = 0;
3186 int i, j, k;
3187
3188 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3189 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3190 struct kvm_rmap_desc *d;
3191
3192 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003193 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003194
Izik Eidus290fc382007-09-27 14:11:22 +02003195 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003196 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003197 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003198 ++nmaps;
3199 continue;
3200 }
Izik Eidus290fc382007-09-27 14:11:22 +02003201 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003202 while (d) {
3203 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003204 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003205 ++nmaps;
3206 else
3207 break;
3208 d = d->more;
3209 }
3210 }
3211 }
3212 return nmaps;
3213}
3214
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003215void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003216{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003217 unsigned long *rmapp;
3218 struct kvm_mmu_page *rev_sp;
3219 gfn_t gfn;
3220
3221 if (*sptep & PT_WRITABLE_MASK) {
3222 rev_sp = page_header(__pa(sptep));
3223 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3224
3225 if (!gfn_to_memslot(kvm, gfn)) {
3226 if (!printk_ratelimit())
3227 return;
3228 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3229 audit_msg, gfn);
3230 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3231 audit_msg, sptep - rev_sp->spt,
3232 rev_sp->gfn);
3233 dump_stack();
3234 return;
3235 }
3236
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003237 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3238 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003239 if (!*rmapp) {
3240 if (!printk_ratelimit())
3241 return;
3242 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3243 audit_msg, *sptep);
3244 dump_stack();
3245 }
3246 }
3247
3248}
3249
3250void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3251{
3252 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3253}
3254
3255static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3256{
Avi Kivity4db35312007-11-21 15:28:32 +02003257 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003258 int i;
3259
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003260 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003261 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003262
Avi Kivity4db35312007-11-21 15:28:32 +02003263 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003264 continue;
3265
3266 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3267 u64 ent = pt[i];
3268
3269 if (!(ent & PT_PRESENT_MASK))
3270 continue;
3271 if (!(ent & PT_WRITABLE_MASK))
3272 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003273 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003274 }
3275 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003276 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003277}
3278
3279static void audit_rmap(struct kvm_vcpu *vcpu)
3280{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003281 check_writable_mappings_rmap(vcpu);
3282 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003283}
3284
3285static void audit_write_protection(struct kvm_vcpu *vcpu)
3286{
Avi Kivity4db35312007-11-21 15:28:32 +02003287 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003288 struct kvm_memory_slot *slot;
3289 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003290 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003291 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003292
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003293 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003294 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003295 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003296 if (sp->unsync)
3297 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003298
Avi Kivity4db35312007-11-21 15:28:32 +02003299 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003300 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003301 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003302
3303 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3304 while (spte) {
3305 if (*spte & PT_WRITABLE_MASK)
3306 printk(KERN_ERR "%s: (%s) shadow page has "
3307 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003308 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003309 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003310 spte = rmap_next(vcpu->kvm, rmapp, spte);
3311 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003312 }
3313}
3314
3315static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3316{
3317 int olddbg = dbg;
3318
3319 dbg = 0;
3320 audit_msg = msg;
3321 audit_rmap(vcpu);
3322 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003323 if (strcmp("pre pte write", audit_msg) != 0)
3324 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003325 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003326 dbg = olddbg;
3327}
3328
3329#endif