blob: c707936b2414c417c11c0d84d2ed100a7b0ae830 [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 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200396static int *slot_largepage_idx(gfn_t gfn,
397 struct kvm_memory_slot *slot,
398 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300399{
400 unsigned long idx;
401
Joerg Roedeld25797b2009-07-27 16:30:43 +0200402 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
403 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
404 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300405}
406
407static void account_shadowed(struct kvm *kvm, gfn_t gfn)
408{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200409 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300410 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200411 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300412
Izik Eidus28430992008-10-03 17:40:32 +0300413 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200414
415 slot = gfn_to_memslot_unaliased(kvm, gfn);
416 for (i = PT_DIRECTORY_LEVEL;
417 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
418 write_count = slot_largepage_idx(gfn, slot, i);
419 *write_count += 1;
420 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421}
422
423static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
424{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200425 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300426 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200427 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300428
Izik Eidus28430992008-10-03 17:40:32 +0300429 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200430 for (i = PT_DIRECTORY_LEVEL;
431 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
432 slot = gfn_to_memslot_unaliased(kvm, gfn);
433 write_count = slot_largepage_idx(gfn, slot, i);
434 *write_count -= 1;
435 WARN_ON(*write_count < 0);
436 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300437}
438
Joerg Roedeld25797b2009-07-27 16:30:43 +0200439static int has_wrprotected_page(struct kvm *kvm,
440 gfn_t gfn,
441 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300442{
Izik Eidus28430992008-10-03 17:40:32 +0300443 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300444 int *largepage_idx;
445
Izik Eidus28430992008-10-03 17:40:32 +0300446 gfn = unalias_gfn(kvm, gfn);
447 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200449 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450 return *largepage_idx;
451 }
452
453 return 1;
454}
455
Joerg Roedeld25797b2009-07-27 16:30:43 +0200456static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300457{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200458 unsigned long page_size = PAGE_SIZE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459 struct vm_area_struct *vma;
460 unsigned long addr;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200461 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300462
463 addr = gfn_to_hva(kvm, gfn);
464 if (kvm_is_error_hva(addr))
Joerg Roedeld25797b2009-07-27 16:30:43 +0200465 return page_size;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300467 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468 vma = find_vma(current->mm, addr);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200469 if (!vma)
470 goto out;
471
472 page_size = vma_kernel_pagesize(vma);
473
474out:
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300475 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476
Joerg Roedeld25797b2009-07-27 16:30:43 +0200477 for (i = PT_PAGE_TABLE_LEVEL;
478 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
479 if (page_size >= KVM_HPAGE_SIZE(i))
480 ret = i;
481 else
482 break;
483 }
484
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300485 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486}
487
Joerg Roedeld25797b2009-07-27 16:30:43 +0200488static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300489{
490 struct kvm_memory_slot *slot;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200491 int host_level;
492 int level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493
494 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
495 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200496 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300497
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498 host_level = host_mapping_level(vcpu->kvm, large_gfn);
499
500 if (host_level == PT_PAGE_TABLE_LEVEL)
501 return host_level;
502
503 for (level = PT_DIRECTORY_LEVEL; level <= host_level; ++level) {
504
505 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
506 break;
507 }
508
509 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300510}
511
512/*
Izik Eidus290fc382007-09-27 14:11:22 +0200513 * Take gfn and return the reverse mapping to it.
514 * Note: gfn must be unaliased before this function get called
515 */
516
Joerg Roedel44ad9942009-07-27 16:30:42 +0200517static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200518{
519 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300520 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200521
522 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200523 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300524 return &slot->rmap[gfn - slot->base_gfn];
525
Joerg Roedel44ad9942009-07-27 16:30:42 +0200526 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
527 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300528
Joerg Roedel44ad9942009-07-27 16:30:42 +0200529 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200530}
531
532/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 * Reverse mapping data structures:
534 *
Izik Eidus290fc382007-09-27 14:11:22 +0200535 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
536 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800537 *
Izik Eidus290fc382007-09-27 14:11:22 +0200538 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
539 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300540 *
541 * Returns the number of rmap entries before the spte was added or zero if
542 * the spte was not added.
543 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800544 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200545static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546{
Avi Kivity4db35312007-11-21 15:28:32 +0200547 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200549 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300550 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800551
Avi Kivity43a37952009-06-10 14:12:05 +0300552 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300553 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200554 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200555 sp = page_header(__pa(spte));
556 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200557 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200558 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200560 *rmapp = (unsigned long)spte;
561 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800563 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300564 desc->sptes[0] = (u64 *)*rmapp;
565 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200566 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800567 } else {
568 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200569 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300570 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300572 count += RMAP_EXT;
573 }
Avi Kivityd555c332009-06-10 14:24:23 +0300574 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800575 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800576 desc = desc->more;
577 }
Avi Kivityd555c332009-06-10 14:24:23 +0300578 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800579 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300580 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300582 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800583}
584
Izik Eidus290fc382007-09-27 14:11:22 +0200585static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 struct kvm_rmap_desc *desc,
587 int i,
588 struct kvm_rmap_desc *prev_desc)
589{
590 int j;
591
Avi Kivityd555c332009-06-10 14:24:23 +0300592 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800593 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300594 desc->sptes[i] = desc->sptes[j];
595 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 if (j != 0)
597 return;
598 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300599 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 else
601 if (prev_desc)
602 prev_desc->more = desc->more;
603 else
Izik Eidus290fc382007-09-27 14:11:22 +0200604 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300605 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606}
607
Izik Eidus290fc382007-09-27 14:11:22 +0200608static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800610 struct kvm_rmap_desc *desc;
611 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200612 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500613 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200614 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615 int i;
616
Avi Kivity43a37952009-06-10 14:12:05 +0300617 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800618 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200619 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500620 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800621 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500622 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200623 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500624 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200625 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500626 kvm_release_pfn_clean(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200627 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200628 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
630 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200631 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800632 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200633 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800634 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
635 spte, *spte);
636 BUG();
637 }
Izik Eidus290fc382007-09-27 14:11:22 +0200638 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 } else {
640 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200641 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 prev_desc = NULL;
643 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300644 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
645 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200646 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800647 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800648 prev_desc);
649 return;
650 }
651 prev_desc = desc;
652 desc = desc->more;
653 }
654 BUG();
655 }
656}
657
Izik Eidus98348e92007-10-16 14:42:30 +0200658static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800659{
Avi Kivity374cbac2007-01-05 16:36:43 -0800660 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200661 struct kvm_rmap_desc *prev_desc;
662 u64 *prev_spte;
663 int i;
664
665 if (!*rmapp)
666 return NULL;
667 else if (!(*rmapp & 1)) {
668 if (!spte)
669 return (u64 *)*rmapp;
670 return NULL;
671 }
672 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
673 prev_desc = NULL;
674 prev_spte = NULL;
675 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300676 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200677 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300678 return desc->sptes[i];
679 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200680 }
681 desc = desc->more;
682 }
683 return NULL;
684}
685
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200686static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200687{
Izik Eidus290fc382007-09-27 14:11:22 +0200688 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800689 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200690 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800691
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500692 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200693 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800694
Izik Eidus98348e92007-10-16 14:42:30 +0200695 spte = rmap_next(kvm, rmapp, NULL);
696 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800697 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800698 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800699 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800700 if (is_writeble_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300701 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800702 write_protected = 1;
703 }
Izik Eidus9647c142007-10-16 14:43:46 +0200704 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800705 }
Izik Eidus855149a2008-03-20 18:17:24 +0200706 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500707 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200708
709 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500710 pfn = spte_to_pfn(*spte);
711 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200712 }
713
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300714 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200715 for (i = PT_DIRECTORY_LEVEL;
716 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
717 rmapp = gfn_to_rmap(kvm, gfn, i);
718 spte = rmap_next(kvm, rmapp, NULL);
719 while (spte) {
720 BUG_ON(!spte);
721 BUG_ON(!(*spte & PT_PRESENT_MASK));
722 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
723 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
724 if (is_writeble_pte(*spte)) {
725 rmap_remove(kvm, spte);
726 --kvm->stat.lpages;
727 __set_spte(spte, shadow_trap_nonpresent_pte);
728 spte = NULL;
729 write_protected = 1;
730 }
731 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300732 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300733 }
734
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200735 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800736}
737
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200738static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
739{
740 u64 *spte;
741 int need_tlb_flush = 0;
742
743 while ((spte = rmap_next(kvm, rmapp, NULL))) {
744 BUG_ON(!(*spte & PT_PRESENT_MASK));
745 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
746 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300747 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200748 need_tlb_flush = 1;
749 }
750 return need_tlb_flush;
751}
752
753static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
754 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
755{
756 int i;
757 int retval = 0;
758
759 /*
760 * If mmap_sem isn't taken, we can look the memslots with only
761 * the mmu_lock by skipping over the slots with userspace_addr == 0.
762 */
763 for (i = 0; i < kvm->nmemslots; i++) {
764 struct kvm_memory_slot *memslot = &kvm->memslots[i];
765 unsigned long start = memslot->userspace_addr;
766 unsigned long end;
767
768 /* mmu_lock protects userspace_addr */
769 if (!start)
770 continue;
771
772 end = start + (memslot->npages << PAGE_SHIFT);
773 if (hva >= start && hva < end) {
774 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedelec04b262009-06-19 15:16:23 +0200775 int idx = gfn_offset /
776 KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200777 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
778 retval |= handler(kvm,
Joerg Roedelec04b262009-06-19 15:16:23 +0200779 &memslot->lpage_info[0][idx].rmap_pde);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200780 }
781 }
782
783 return retval;
784}
785
786int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
787{
788 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
789}
790
791static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
792{
793 u64 *spte;
794 int young = 0;
795
Sheng Yang534e38b2008-09-08 15:12:30 +0800796 /* always return old for EPT */
797 if (!shadow_accessed_mask)
798 return 0;
799
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200800 spte = rmap_next(kvm, rmapp, NULL);
801 while (spte) {
802 int _young;
803 u64 _spte = *spte;
804 BUG_ON(!(_spte & PT_PRESENT_MASK));
805 _young = _spte & PT_ACCESSED_MASK;
806 if (_young) {
807 young = 1;
808 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
809 }
810 spte = rmap_next(kvm, rmapp, spte);
811 }
812 return young;
813}
814
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300815#define RMAP_RECYCLE_THRESHOLD 1000
816
817static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
818{
819 unsigned long *rmapp;
820
821 gfn = unalias_gfn(vcpu->kvm, gfn);
822 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
823
824 kvm_unmap_rmapp(vcpu->kvm, rmapp);
825 kvm_flush_remote_tlbs(vcpu->kvm);
826}
827
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200828int kvm_age_hva(struct kvm *kvm, unsigned long hva)
829{
830 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
831}
832
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800833#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300834static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835{
Avi Kivity139bdb22007-01-05 16:36:50 -0800836 u64 *pos;
837 u64 *end;
838
Avi Kivity47ad8e62007-05-06 15:50:58 +0300839 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300840 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800841 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800842 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800844 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 return 1;
846}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800847#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
Avi Kivity4db35312007-11-21 15:28:32 +0200849static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800850{
Avi Kivity4db35312007-11-21 15:28:32 +0200851 ASSERT(is_empty_shadow_page(sp->spt));
852 list_del(&sp->link);
853 __free_page(virt_to_page(sp->spt));
854 __free_page(virt_to_page(sp->gfns));
855 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800856 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800857}
858
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800859static unsigned kvm_page_table_hashfn(gfn_t gfn)
860{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200861 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800862}
863
Avi Kivity25c0de22007-01-05 16:36:42 -0800864static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
865 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866{
Avi Kivity4db35312007-11-21 15:28:32 +0200867 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800869 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
870 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
871 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200872 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800873 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200874 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800875 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200876 sp->multimapped = 0;
877 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800878 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200879 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880}
881
Avi Kivity714b93d2007-01-05 16:36:53 -0800882static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200883 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800884{
885 struct kvm_pte_chain *pte_chain;
886 struct hlist_node *node;
887 int i;
888
889 if (!parent_pte)
890 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200891 if (!sp->multimapped) {
892 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800893
894 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200895 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800896 return;
897 }
Avi Kivity4db35312007-11-21 15:28:32 +0200898 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800899 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200900 INIT_HLIST_HEAD(&sp->parent_ptes);
901 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800902 pte_chain->parent_ptes[0] = old;
903 }
Avi Kivity4db35312007-11-21 15:28:32 +0200904 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800905 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
906 continue;
907 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
908 if (!pte_chain->parent_ptes[i]) {
909 pte_chain->parent_ptes[i] = parent_pte;
910 return;
911 }
912 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800913 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800914 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200915 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800916 pte_chain->parent_ptes[0] = parent_pte;
917}
918
Avi Kivity4db35312007-11-21 15:28:32 +0200919static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800920 u64 *parent_pte)
921{
922 struct kvm_pte_chain *pte_chain;
923 struct hlist_node *node;
924 int i;
925
Avi Kivity4db35312007-11-21 15:28:32 +0200926 if (!sp->multimapped) {
927 BUG_ON(sp->parent_pte != parent_pte);
928 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800929 return;
930 }
Avi Kivity4db35312007-11-21 15:28:32 +0200931 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800932 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
933 if (!pte_chain->parent_ptes[i])
934 break;
935 if (pte_chain->parent_ptes[i] != parent_pte)
936 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800937 while (i + 1 < NR_PTE_CHAIN_ENTRIES
938 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800939 pte_chain->parent_ptes[i]
940 = pte_chain->parent_ptes[i + 1];
941 ++i;
942 }
943 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800944 if (i == 0) {
945 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300946 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200947 if (hlist_empty(&sp->parent_ptes)) {
948 sp->multimapped = 0;
949 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800950 }
951 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800952 return;
953 }
954 BUG();
955}
956
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300957
958static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
959 mmu_parent_walk_fn fn)
960{
961 struct kvm_pte_chain *pte_chain;
962 struct hlist_node *node;
963 struct kvm_mmu_page *parent_sp;
964 int i;
965
966 if (!sp->multimapped && sp->parent_pte) {
967 parent_sp = page_header(__pa(sp->parent_pte));
968 fn(vcpu, parent_sp);
969 mmu_parent_walk(vcpu, parent_sp, fn);
970 return;
971 }
972 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
973 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
974 if (!pte_chain->parent_ptes[i])
975 break;
976 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
977 fn(vcpu, parent_sp);
978 mmu_parent_walk(vcpu, parent_sp, fn);
979 }
980}
981
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300982static void kvm_mmu_update_unsync_bitmap(u64 *spte)
983{
984 unsigned int index;
985 struct kvm_mmu_page *sp = page_header(__pa(spte));
986
987 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200988 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
989 sp->unsync_children++;
990 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300991}
992
993static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
994{
995 struct kvm_pte_chain *pte_chain;
996 struct hlist_node *node;
997 int i;
998
999 if (!sp->parent_pte)
1000 return;
1001
1002 if (!sp->multimapped) {
1003 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1004 return;
1005 }
1006
1007 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1008 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1009 if (!pte_chain->parent_ptes[i])
1010 break;
1011 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1012 }
1013}
1014
1015static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1016{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001017 kvm_mmu_update_parents_unsync(sp);
1018 return 1;
1019}
1020
1021static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1022 struct kvm_mmu_page *sp)
1023{
1024 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1025 kvm_mmu_update_parents_unsync(sp);
1026}
1027
Avi Kivityd761a502008-05-29 14:55:03 +03001028static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1029 struct kvm_mmu_page *sp)
1030{
1031 int i;
1032
1033 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1034 sp->spt[i] = shadow_trap_nonpresent_pte;
1035}
1036
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001037static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1038 struct kvm_mmu_page *sp)
1039{
1040 return 1;
1041}
1042
Marcelo Tosattia7052892008-09-23 13:18:35 -03001043static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1044{
1045}
1046
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001047#define KVM_PAGE_ARRAY_NR 16
1048
1049struct kvm_mmu_pages {
1050 struct mmu_page_and_offset {
1051 struct kvm_mmu_page *sp;
1052 unsigned int idx;
1053 } page[KVM_PAGE_ARRAY_NR];
1054 unsigned int nr;
1055};
1056
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001057#define for_each_unsync_children(bitmap, idx) \
1058 for (idx = find_first_bit(bitmap, 512); \
1059 idx < 512; \
1060 idx = find_next_bit(bitmap, 512, idx+1))
1061
Hannes Edercded19f2009-02-21 02:19:13 +01001062static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1063 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001064{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001065 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001066
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001067 if (sp->unsync)
1068 for (i=0; i < pvec->nr; i++)
1069 if (pvec->page[i].sp == sp)
1070 return 0;
1071
1072 pvec->page[pvec->nr].sp = sp;
1073 pvec->page[pvec->nr].idx = idx;
1074 pvec->nr++;
1075 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1076}
1077
1078static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1079 struct kvm_mmu_pages *pvec)
1080{
1081 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001082
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001083 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001084 u64 ent = sp->spt[i];
1085
Marcelo Tosatti87917232008-12-22 18:49:30 -02001086 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001087 struct kvm_mmu_page *child;
1088 child = page_header(ent & PT64_BASE_ADDR_MASK);
1089
1090 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001091 if (mmu_pages_add(pvec, child, i))
1092 return -ENOSPC;
1093
1094 ret = __mmu_unsync_walk(child, pvec);
1095 if (!ret)
1096 __clear_bit(i, sp->unsync_child_bitmap);
1097 else if (ret > 0)
1098 nr_unsync_leaf += ret;
1099 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001100 return ret;
1101 }
1102
1103 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001104 nr_unsync_leaf++;
1105 if (mmu_pages_add(pvec, child, i))
1106 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001107 }
1108 }
1109 }
1110
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001111 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001112 sp->unsync_children = 0;
1113
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001114 return nr_unsync_leaf;
1115}
1116
1117static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1118 struct kvm_mmu_pages *pvec)
1119{
1120 if (!sp->unsync_children)
1121 return 0;
1122
1123 mmu_pages_add(pvec, sp, 0);
1124 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001125}
1126
Avi Kivity4db35312007-11-21 15:28:32 +02001127static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001128{
1129 unsigned index;
1130 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001131 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001132 struct hlist_node *node;
1133
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001134 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001135 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001136 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001137 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001138 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001139 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001140 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001141 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001142 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001143 }
1144 return NULL;
1145}
1146
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001147static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1148{
1149 WARN_ON(!sp->unsync);
1150 sp->unsync = 0;
1151 --kvm->stat.mmu_unsync;
1152}
1153
1154static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1155
1156static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1157{
1158 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1159 kvm_mmu_zap_page(vcpu->kvm, sp);
1160 return 1;
1161 }
1162
Avi Kivityf691fe12009-07-06 15:58:14 +03001163 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001164 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1165 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001166 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001167 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1168 kvm_mmu_zap_page(vcpu->kvm, sp);
1169 return 1;
1170 }
1171
1172 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001173 return 0;
1174}
1175
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001176struct mmu_page_path {
1177 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1178 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001179};
1180
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001181#define for_each_sp(pvec, sp, parents, i) \
1182 for (i = mmu_pages_next(&pvec, &parents, -1), \
1183 sp = pvec.page[i].sp; \
1184 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1185 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001186
Hannes Edercded19f2009-02-21 02:19:13 +01001187static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1188 struct mmu_page_path *parents,
1189 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001190{
1191 int n;
1192
1193 for (n = i+1; n < pvec->nr; n++) {
1194 struct kvm_mmu_page *sp = pvec->page[n].sp;
1195
1196 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1197 parents->idx[0] = pvec->page[n].idx;
1198 return n;
1199 }
1200
1201 parents->parent[sp->role.level-2] = sp;
1202 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1203 }
1204
1205 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001206}
1207
Hannes Edercded19f2009-02-21 02:19:13 +01001208static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001209{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001210 struct kvm_mmu_page *sp;
1211 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001212
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001213 do {
1214 unsigned int idx = parents->idx[level];
1215
1216 sp = parents->parent[level];
1217 if (!sp)
1218 return;
1219
1220 --sp->unsync_children;
1221 WARN_ON((int)sp->unsync_children < 0);
1222 __clear_bit(idx, sp->unsync_child_bitmap);
1223 level++;
1224 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1225}
1226
1227static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1228 struct mmu_page_path *parents,
1229 struct kvm_mmu_pages *pvec)
1230{
1231 parents->parent[parent->role.level-1] = NULL;
1232 pvec->nr = 0;
1233}
1234
1235static void mmu_sync_children(struct kvm_vcpu *vcpu,
1236 struct kvm_mmu_page *parent)
1237{
1238 int i;
1239 struct kvm_mmu_page *sp;
1240 struct mmu_page_path parents;
1241 struct kvm_mmu_pages pages;
1242
1243 kvm_mmu_pages_init(parent, &parents, &pages);
1244 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001245 int protected = 0;
1246
1247 for_each_sp(pages, sp, parents, i)
1248 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1249
1250 if (protected)
1251 kvm_flush_remote_tlbs(vcpu->kvm);
1252
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001253 for_each_sp(pages, sp, parents, i) {
1254 kvm_sync_page(vcpu, sp);
1255 mmu_pages_clear_parents(&parents);
1256 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001257 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001258 kvm_mmu_pages_init(parent, &parents, &pages);
1259 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001260}
1261
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001262static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1263 gfn_t gfn,
1264 gva_t gaddr,
1265 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001266 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001267 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001268 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001269{
1270 union kvm_mmu_page_role role;
1271 unsigned index;
1272 unsigned quadrant;
1273 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001274 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001275 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001276
Avi Kivitya770f6f2008-12-21 19:20:09 +02001277 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001278 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001279 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001280 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001281 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001282 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1283 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1284 role.quadrant = quadrant;
1285 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001286 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001287 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001288 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1289 if (sp->gfn == gfn) {
1290 if (sp->unsync)
1291 if (kvm_sync_page(vcpu, sp))
1292 continue;
1293
1294 if (sp->role.word != role.word)
1295 continue;
1296
Avi Kivity4db35312007-11-21 15:28:32 +02001297 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001298 if (sp->unsync_children) {
1299 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1300 kvm_mmu_mark_parents_unsync(vcpu, sp);
1301 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001302 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001303 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001304 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001305 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001306 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1307 if (!sp)
1308 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001309 sp->gfn = gfn;
1310 sp->role = role;
1311 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001312 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001313 if (rmap_write_protect(vcpu->kvm, gfn))
1314 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001315 account_shadowed(vcpu->kvm, gfn);
1316 }
Avi Kivity131d8272008-05-29 14:56:28 +03001317 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1318 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1319 else
1320 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001321 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001322 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001323}
1324
Avi Kivity2d111232008-12-25 14:39:47 +02001325static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1326 struct kvm_vcpu *vcpu, u64 addr)
1327{
1328 iterator->addr = addr;
1329 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1330 iterator->level = vcpu->arch.mmu.shadow_root_level;
1331 if (iterator->level == PT32E_ROOT_LEVEL) {
1332 iterator->shadow_addr
1333 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1334 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1335 --iterator->level;
1336 if (!iterator->shadow_addr)
1337 iterator->level = 0;
1338 }
1339}
1340
1341static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1342{
1343 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1344 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001345
1346 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1347 if (is_large_pte(*iterator->sptep))
1348 return false;
1349
Avi Kivity2d111232008-12-25 14:39:47 +02001350 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1351 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1352 return true;
1353}
1354
1355static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1356{
1357 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1358 --iterator->level;
1359}
1360
Avi Kivity90cb0522007-07-17 13:04:56 +03001361static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001362 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001363{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001364 unsigned i;
1365 u64 *pt;
1366 u64 ent;
1367
Avi Kivity4db35312007-11-21 15:28:32 +02001368 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001369
Avi Kivity697fe2e2007-01-05 16:36:46 -08001370 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1371 ent = pt[i];
1372
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001373 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001374 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001375 ent &= PT64_BASE_ADDR_MASK;
1376 mmu_page_remove_parent_pte(page_header(ent),
1377 &pt[i]);
1378 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001379 if (is_large_pte(ent))
1380 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001381 rmap_remove(kvm, &pt[i]);
1382 }
1383 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001384 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001385 }
Avi Kivitya4360362007-01-05 16:36:45 -08001386}
1387
Avi Kivity4db35312007-11-21 15:28:32 +02001388static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001389{
Avi Kivity4db35312007-11-21 15:28:32 +02001390 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001391}
1392
Avi Kivity12b7d282007-09-23 14:10:49 +02001393static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1394{
1395 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001396 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001397
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001398 kvm_for_each_vcpu(i, vcpu, kvm)
1399 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001400}
1401
Avi Kivity31aa2b42008-07-11 17:59:46 +03001402static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001403{
1404 u64 *parent_pte;
1405
Avi Kivity4db35312007-11-21 15:28:32 +02001406 while (sp->multimapped || sp->parent_pte) {
1407 if (!sp->multimapped)
1408 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001409 else {
1410 struct kvm_pte_chain *chain;
1411
Avi Kivity4db35312007-11-21 15:28:32 +02001412 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001413 struct kvm_pte_chain, link);
1414 parent_pte = chain->parent_ptes[0];
1415 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001416 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001417 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001418 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001419 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001420}
1421
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001422static int mmu_zap_unsync_children(struct kvm *kvm,
1423 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001424{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001425 int i, zapped = 0;
1426 struct mmu_page_path parents;
1427 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001428
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001429 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001430 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001431
1432 kvm_mmu_pages_init(parent, &parents, &pages);
1433 while (mmu_unsync_walk(parent, &pages)) {
1434 struct kvm_mmu_page *sp;
1435
1436 for_each_sp(pages, sp, parents, i) {
1437 kvm_mmu_zap_page(kvm, sp);
1438 mmu_pages_clear_parents(&parents);
1439 }
1440 zapped += pages.nr;
1441 kvm_mmu_pages_init(parent, &parents, &pages);
1442 }
1443
1444 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001445}
1446
Marcelo Tosatti07385412008-09-23 13:18:37 -03001447static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001448{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001449 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001450
1451 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001452 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001453 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001454 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001455 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001456 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001457 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001458 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001459 if (sp->unsync)
1460 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001461 if (!sp->root_count) {
1462 hlist_del(&sp->hash_link);
1463 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001464 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001465 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001466 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001467 kvm_reload_remote_mmus(kvm);
1468 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001469 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001470 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001471}
1472
Izik Eidus82ce2c92007-10-02 18:52:55 +02001473/*
1474 * Changing the number of mmu pages allocated to the vm
1475 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1476 */
1477void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1478{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001479 int used_pages;
1480
1481 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1482 used_pages = max(0, used_pages);
1483
Izik Eidus82ce2c92007-10-02 18:52:55 +02001484 /*
1485 * If we set the number of mmu pages to be smaller be than the
1486 * number of actived pages , we must to free some mmu pages before we
1487 * change the value
1488 */
1489
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001490 if (used_pages > kvm_nr_mmu_pages) {
1491 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001492 struct kvm_mmu_page *page;
1493
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001494 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001495 struct kvm_mmu_page, link);
1496 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001497 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001498 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001499 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001500 }
1501 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001502 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1503 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001504
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001505 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001506}
1507
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001508static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001509{
1510 unsigned index;
1511 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001512 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001513 struct hlist_node *node, *n;
1514 int r;
1515
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001516 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001517 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001518 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001519 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001520 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001521 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001522 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001523 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001524 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001525 if (kvm_mmu_zap_page(kvm, sp))
1526 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001527 }
1528 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001529}
1530
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001531static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001532{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001533 unsigned index;
1534 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001535 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001536 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001537
Avi Kivity4677a3b2009-01-06 13:00:27 +02001538 index = kvm_page_table_hashfn(gfn);
1539 bucket = &kvm->arch.mmu_page_hash[index];
1540 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001541 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001542 && !sp->role.invalid) {
1543 pgprintk("%s: zap %lx %x\n",
1544 __func__, gfn, sp->role.word);
1545 kvm_mmu_zap_page(kvm, sp);
1546 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001547 }
1548}
1549
Avi Kivity38c335f2007-11-21 14:20:22 +02001550static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551{
Avi Kivity38c335f2007-11-21 14:20:22 +02001552 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001553 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554
Sheng Yang291f26b2008-10-16 17:30:57 +08001555 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556}
1557
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001558static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1559{
1560 int i;
1561 u64 *pt = sp->spt;
1562
1563 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1564 return;
1565
1566 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1567 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001568 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001569 }
1570}
1571
Avi Kivity039576c2007-03-20 12:46:50 +02001572struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1573{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001574 struct page *page;
1575
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001576 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001577
1578 if (gpa == UNMAPPED_GVA)
1579 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001580
Izik Eidus72dc67a2008-02-10 18:04:15 +02001581 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001582
1583 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001584}
1585
Sheng Yang74be52e2008-10-09 16:01:56 +08001586/*
1587 * The function is based on mtrr_type_lookup() in
1588 * arch/x86/kernel/cpu/mtrr/generic.c
1589 */
1590static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1591 u64 start, u64 end)
1592{
1593 int i;
1594 u64 base, mask;
1595 u8 prev_match, curr_match;
1596 int num_var_ranges = KVM_NR_VAR_MTRR;
1597
1598 if (!mtrr_state->enabled)
1599 return 0xFF;
1600
1601 /* Make end inclusive end, instead of exclusive */
1602 end--;
1603
1604 /* Look in fixed ranges. Just return the type as per start */
1605 if (mtrr_state->have_fixed && (start < 0x100000)) {
1606 int idx;
1607
1608 if (start < 0x80000) {
1609 idx = 0;
1610 idx += (start >> 16);
1611 return mtrr_state->fixed_ranges[idx];
1612 } else if (start < 0xC0000) {
1613 idx = 1 * 8;
1614 idx += ((start - 0x80000) >> 14);
1615 return mtrr_state->fixed_ranges[idx];
1616 } else if (start < 0x1000000) {
1617 idx = 3 * 8;
1618 idx += ((start - 0xC0000) >> 12);
1619 return mtrr_state->fixed_ranges[idx];
1620 }
1621 }
1622
1623 /*
1624 * Look in variable ranges
1625 * Look of multiple ranges matching this address and pick type
1626 * as per MTRR precedence
1627 */
1628 if (!(mtrr_state->enabled & 2))
1629 return mtrr_state->def_type;
1630
1631 prev_match = 0xFF;
1632 for (i = 0; i < num_var_ranges; ++i) {
1633 unsigned short start_state, end_state;
1634
1635 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1636 continue;
1637
1638 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1639 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1640 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1641 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1642
1643 start_state = ((start & mask) == (base & mask));
1644 end_state = ((end & mask) == (base & mask));
1645 if (start_state != end_state)
1646 return 0xFE;
1647
1648 if ((start & mask) != (base & mask))
1649 continue;
1650
1651 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1652 if (prev_match == 0xFF) {
1653 prev_match = curr_match;
1654 continue;
1655 }
1656
1657 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1658 curr_match == MTRR_TYPE_UNCACHABLE)
1659 return MTRR_TYPE_UNCACHABLE;
1660
1661 if ((prev_match == MTRR_TYPE_WRBACK &&
1662 curr_match == MTRR_TYPE_WRTHROUGH) ||
1663 (prev_match == MTRR_TYPE_WRTHROUGH &&
1664 curr_match == MTRR_TYPE_WRBACK)) {
1665 prev_match = MTRR_TYPE_WRTHROUGH;
1666 curr_match = MTRR_TYPE_WRTHROUGH;
1667 }
1668
1669 if (prev_match != curr_match)
1670 return MTRR_TYPE_UNCACHABLE;
1671 }
1672
1673 if (prev_match != 0xFF)
1674 return prev_match;
1675
1676 return mtrr_state->def_type;
1677}
1678
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001679u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001680{
1681 u8 mtrr;
1682
1683 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1684 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1685 if (mtrr == 0xfe || mtrr == 0xff)
1686 mtrr = MTRR_TYPE_WRBACK;
1687 return mtrr;
1688}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001689EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001690
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001691static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1692{
1693 unsigned index;
1694 struct hlist_head *bucket;
1695 struct kvm_mmu_page *s;
1696 struct hlist_node *node, *n;
1697
Avi Kivityf691fe12009-07-06 15:58:14 +03001698 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001699 index = kvm_page_table_hashfn(sp->gfn);
1700 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1701 /* don't unsync if pagetable is shadowed with multiple roles */
1702 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001703 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001704 continue;
1705 if (s->role.word != sp->role.word)
1706 return 1;
1707 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001708 ++vcpu->kvm->stat.mmu_unsync;
1709 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001710
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001711 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001712
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001713 mmu_convert_notrap(sp);
1714 return 0;
1715}
1716
1717static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1718 bool can_unsync)
1719{
1720 struct kvm_mmu_page *shadow;
1721
1722 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1723 if (shadow) {
1724 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1725 return 1;
1726 if (shadow->unsync)
1727 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001728 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001729 return kvm_unsync_page(vcpu, shadow);
1730 return 1;
1731 }
1732 return 0;
1733}
1734
Avi Kivityd555c332009-06-10 14:24:23 +03001735static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001736 unsigned pte_access, int user_fault,
1737 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001738 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001739 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740{
1741 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001742 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001743
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001744 /*
1745 * We don't set the accessed bit, since we sometimes want to see
1746 * whether the guest actually used the pte (in order to detect
1747 * demand paging).
1748 */
Sheng Yang7b523452008-04-25 21:13:50 +08001749 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001750 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001751 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001752 if (!dirty)
1753 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001754 if (pte_access & ACC_EXEC_MASK)
1755 spte |= shadow_x_mask;
1756 else
1757 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001758 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001759 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001760 if (largepage)
1761 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001762 if (tdp_enabled)
1763 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1764 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001765
Anthony Liguori35149e22008-04-02 14:46:56 -05001766 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001767
1768 if ((pte_access & ACC_WRITE_MASK)
1769 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001770
Joerg Roedeld25797b2009-07-27 16:30:43 +02001771 if (largepage && has_wrprotected_page(vcpu->kvm, gfn, 1)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001772 ret = 1;
1773 spte = shadow_trap_nonpresent_pte;
1774 goto set_pte;
1775 }
1776
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001777 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001778
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001779 /*
1780 * Optimization: for pte sync, if spte was writable the hash
1781 * lookup is unnecessary (and expensive). Write protection
1782 * is responsibility of mmu_get_page / kvm_sync_page.
1783 * Same reasoning can be applied to dirty page accounting.
1784 */
Avi Kivityd555c332009-06-10 14:24:23 +03001785 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001786 goto set_pte;
1787
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001788 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001789 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001790 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001791 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001792 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001793 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001794 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001795 }
1796 }
1797
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001798 if (pte_access & ACC_WRITE_MASK)
1799 mark_page_dirty(vcpu->kvm, gfn);
1800
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001801set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001802 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001803 return ret;
1804}
1805
Avi Kivityd555c332009-06-10 14:24:23 +03001806static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001807 unsigned pt_access, unsigned pte_access,
1808 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001809 int *ptwrite, int largepage, gfn_t gfn,
1810 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001811{
1812 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001813 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001814 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001815
1816 pgprintk("%s: spte %llx access %x write_fault %d"
1817 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001818 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001819 write_fault, user_fault, gfn);
1820
Avi Kivityd555c332009-06-10 14:24:23 +03001821 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001822 /*
1823 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1824 * the parent of the now unreachable PTE.
1825 */
Avi Kivityd555c332009-06-10 14:24:23 +03001826 if (largepage && !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001827 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001828 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001829
1830 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001831 mmu_page_remove_parent_pte(child, sptep);
1832 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001833 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001834 spte_to_pfn(*sptep), pfn);
1835 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001836 } else
1837 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001838 }
Avi Kivityd555c332009-06-10 14:24:23 +03001839 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001840 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001841 if (write_fault)
1842 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001843 kvm_x86_ops->tlb_flush(vcpu);
1844 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001845
Avi Kivityd555c332009-06-10 14:24:23 +03001846 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001847 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001848 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001849 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1850 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001851 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001852 ++vcpu->kvm->stat.lpages;
1853
Avi Kivityd555c332009-06-10 14:24:23 +03001854 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001855 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001856 rmap_count = rmap_add(vcpu, sptep, gfn);
Avi Kivityd555c332009-06-10 14:24:23 +03001857 if (!is_rmap_spte(*sptep))
Anthony Liguori35149e22008-04-02 14:46:56 -05001858 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001859 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1860 rmap_recycle(vcpu, gfn, largepage);
Izik Eidus75e68e62008-01-12 23:49:09 +02001861 } else {
1862 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001863 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001864 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001865 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001866 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001867 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001868 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001869 vcpu->arch.last_pte_gfn = gfn;
1870 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001871}
1872
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1874{
1875}
1876
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001877static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001878 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879{
Avi Kivity9f652d212008-12-25 14:54:25 +02001880 struct kvm_shadow_walk_iterator iterator;
1881 struct kvm_mmu_page *sp;
1882 int pt_write = 0;
1883 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884
Avi Kivity9f652d212008-12-25 14:54:25 +02001885 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1886 if (iterator.level == PT_PAGE_TABLE_LEVEL
1887 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1888 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1889 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001890 largepage, gfn, pfn, false);
Avi Kivity9f652d212008-12-25 14:54:25 +02001891 ++vcpu->stat.pf_fixed;
1892 break;
1893 }
1894
1895 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1896 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1897 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1898 iterator.level - 1,
1899 1, ACC_ALL, iterator.sptep);
1900 if (!sp) {
1901 pgprintk("nonpaging_map: ENOMEM\n");
1902 kvm_release_pfn_clean(pfn);
1903 return -ENOMEM;
1904 }
1905
Avi Kivityd555c332009-06-10 14:24:23 +03001906 __set_spte(iterator.sptep,
1907 __pa(sp->spt)
1908 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1909 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02001910 }
1911 }
1912 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913}
1914
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001915static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1916{
1917 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001918 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001919 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001920 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001921
Joerg Roedeld25797b2009-07-27 16:30:43 +02001922 if (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL) {
Joerg Roedelec04b262009-06-19 15:16:23 +02001923 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001924 largepage = 1;
1925 }
1926
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001927 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001928 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001929 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001930
Avi Kivityd196e342008-01-24 11:44:11 +02001931 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001932 if (is_error_pfn(pfn)) {
1933 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001934 return 1;
1935 }
1936
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001937 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001938 if (mmu_notifier_retry(vcpu, mmu_seq))
1939 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001940 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001941 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001942 spin_unlock(&vcpu->kvm->mmu_lock);
1943
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001944
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001945 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001946
1947out_unlock:
1948 spin_unlock(&vcpu->kvm->mmu_lock);
1949 kvm_release_pfn_clean(pfn);
1950 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001951}
1952
1953
Avi Kivity17ac10a2007-01-05 16:36:40 -08001954static void mmu_free_roots(struct kvm_vcpu *vcpu)
1955{
1956 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001957 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001958
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001959 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001960 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001961 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001962 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1963 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001964
Avi Kivity4db35312007-11-21 15:28:32 +02001965 sp = page_header(root);
1966 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001967 if (!sp->root_count && sp->role.invalid)
1968 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001969 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001970 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001971 return;
1972 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001973 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001974 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001975
Avi Kivity417726a2007-04-12 17:35:58 +03001976 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001977 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001978 sp = page_header(root);
1979 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001980 if (!sp->root_count && sp->role.invalid)
1981 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001982 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001983 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001984 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001985 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001986 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001987}
1988
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001989static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1990{
1991 int ret = 0;
1992
1993 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1994 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1995 ret = 1;
1996 }
1997
1998 return ret;
1999}
2000
2001static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002002{
2003 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002004 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002005 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002006 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002007 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002008
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002009 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002010
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2012 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002013
2014 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002015 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002016 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002017 if (mmu_check_root(vcpu, root_gfn))
2018 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002019 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002020 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002021 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002022 root = __pa(sp->spt);
2023 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002024 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002025 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002026 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002027 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002028 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002029 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002030 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002031 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002032
2033 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002034 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002035 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002036 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002037 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002038 continue;
2039 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002040 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002041 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002042 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002043 if (mmu_check_root(vcpu, root_gfn))
2044 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002045 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002046 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002047 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002048 root = __pa(sp->spt);
2049 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002050 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002051 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002053 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002054}
2055
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002056static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2057{
2058 int i;
2059 struct kvm_mmu_page *sp;
2060
2061 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2062 return;
2063 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2064 hpa_t root = vcpu->arch.mmu.root_hpa;
2065 sp = page_header(root);
2066 mmu_sync_children(vcpu, sp);
2067 return;
2068 }
2069 for (i = 0; i < 4; ++i) {
2070 hpa_t root = vcpu->arch.mmu.pae_root[i];
2071
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002072 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002073 root &= PT64_BASE_ADDR_MASK;
2074 sp = page_header(root);
2075 mmu_sync_children(vcpu, sp);
2076 }
2077 }
2078}
2079
2080void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2081{
2082 spin_lock(&vcpu->kvm->mmu_lock);
2083 mmu_sync_roots(vcpu);
2084 spin_unlock(&vcpu->kvm->mmu_lock);
2085}
2086
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2088{
2089 return vaddr;
2090}
2091
2092static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002093 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094{
Avi Kivitye8332402007-12-09 18:43:00 +02002095 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002096 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002098 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002099 r = mmu_topup_memory_caches(vcpu);
2100 if (r)
2101 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002102
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002104 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105
Avi Kivitye8332402007-12-09 18:43:00 +02002106 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107
Avi Kivitye8332402007-12-09 18:43:00 +02002108 return nonpaging_map(vcpu, gva & PAGE_MASK,
2109 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110}
2111
Joerg Roedelfb72d162008-02-07 13:47:44 +01002112static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2113 u32 error_code)
2114{
Anthony Liguori35149e22008-04-02 14:46:56 -05002115 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002116 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002117 int largepage = 0;
2118 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002119 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002120
2121 ASSERT(vcpu);
2122 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2123
2124 r = mmu_topup_memory_caches(vcpu);
2125 if (r)
2126 return r;
2127
Joerg Roedeld25797b2009-07-27 16:30:43 +02002128 if (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL) {
Joerg Roedelec04b262009-06-19 15:16:23 +02002129 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002130 largepage = 1;
2131 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002132 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002133 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002134 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002135 if (is_error_pfn(pfn)) {
2136 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002137 return 1;
2138 }
2139 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002140 if (mmu_notifier_retry(vcpu, mmu_seq))
2141 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002142 kvm_mmu_free_some_pages(vcpu);
2143 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002144 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002145 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002146
2147 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002148
2149out_unlock:
2150 spin_unlock(&vcpu->kvm->mmu_lock);
2151 kvm_release_pfn_clean(pfn);
2152 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002153}
2154
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155static void nonpaging_free(struct kvm_vcpu *vcpu)
2156{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002157 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158}
2159
2160static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2161{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002162 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163
2164 context->new_cr3 = nonpaging_new_cr3;
2165 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166 context->gva_to_gpa = nonpaging_gva_to_gpa;
2167 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002168 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002169 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002170 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002171 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002173 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174 return 0;
2175}
2176
Avi Kivityd835dfe2007-11-21 02:57:59 +02002177void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002179 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002180 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181}
2182
2183static void paging_new_cr3(struct kvm_vcpu *vcpu)
2184{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002185 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002186 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187}
2188
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189static void inject_page_fault(struct kvm_vcpu *vcpu,
2190 u64 addr,
2191 u32 err_code)
2192{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002193 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194}
2195
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196static void paging_free(struct kvm_vcpu *vcpu)
2197{
2198 nonpaging_free(vcpu);
2199}
2200
Dong, Eddie82725b22009-03-30 16:21:08 +08002201static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2202{
2203 int bit7;
2204
2205 bit7 = (gpte >> 7) & 1;
2206 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2207}
2208
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209#define PTTYPE 64
2210#include "paging_tmpl.h"
2211#undef PTTYPE
2212
2213#define PTTYPE 32
2214#include "paging_tmpl.h"
2215#undef PTTYPE
2216
Dong, Eddie82725b22009-03-30 16:21:08 +08002217static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2218{
2219 struct kvm_mmu *context = &vcpu->arch.mmu;
2220 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2221 u64 exb_bit_rsvd = 0;
2222
2223 if (!is_nx(vcpu))
2224 exb_bit_rsvd = rsvd_bits(63, 63);
2225 switch (level) {
2226 case PT32_ROOT_LEVEL:
2227 /* no rsvd bits for 2 level 4K page table entries */
2228 context->rsvd_bits_mask[0][1] = 0;
2229 context->rsvd_bits_mask[0][0] = 0;
2230 if (is_cpuid_PSE36())
2231 /* 36bits PSE 4MB page */
2232 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2233 else
2234 /* 32 bits PSE 4MB page */
2235 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002236 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002237 break;
2238 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002239 context->rsvd_bits_mask[0][2] =
2240 rsvd_bits(maxphyaddr, 63) |
2241 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002242 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002243 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002244 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2245 rsvd_bits(maxphyaddr, 62); /* PTE */
2246 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2247 rsvd_bits(maxphyaddr, 62) |
2248 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002249 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002250 break;
2251 case PT64_ROOT_LEVEL:
2252 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2253 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2254 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2255 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2256 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002257 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002258 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2259 rsvd_bits(maxphyaddr, 51);
2260 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2261 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2262 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002263 rsvd_bits(maxphyaddr, 51) |
2264 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002265 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002266 break;
2267 }
2268}
2269
Avi Kivity17ac10a2007-01-05 16:36:40 -08002270static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002272 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273
2274 ASSERT(is_pae(vcpu));
2275 context->new_cr3 = paging_new_cr3;
2276 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002278 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002279 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002280 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002282 context->root_level = level;
2283 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002284 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 return 0;
2286}
2287
Avi Kivity17ac10a2007-01-05 16:36:40 -08002288static int paging64_init_context(struct kvm_vcpu *vcpu)
2289{
Dong, Eddie82725b22009-03-30 16:21:08 +08002290 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002291 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2292}
2293
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294static int paging32_init_context(struct kvm_vcpu *vcpu)
2295{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002296 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297
Dong, Eddie82725b22009-03-30 16:21:08 +08002298 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 context->new_cr3 = paging_new_cr3;
2300 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 context->gva_to_gpa = paging32_gva_to_gpa;
2302 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002303 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002304 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002305 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 context->root_level = PT32_ROOT_LEVEL;
2307 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002308 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 return 0;
2310}
2311
2312static int paging32E_init_context(struct kvm_vcpu *vcpu)
2313{
Dong, Eddie82725b22009-03-30 16:21:08 +08002314 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002315 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316}
2317
Joerg Roedelfb72d162008-02-07 13:47:44 +01002318static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2319{
2320 struct kvm_mmu *context = &vcpu->arch.mmu;
2321
2322 context->new_cr3 = nonpaging_new_cr3;
2323 context->page_fault = tdp_page_fault;
2324 context->free = nonpaging_free;
2325 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002326 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002327 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002328 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002329 context->root_hpa = INVALID_PAGE;
2330
2331 if (!is_paging(vcpu)) {
2332 context->gva_to_gpa = nonpaging_gva_to_gpa;
2333 context->root_level = 0;
2334 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002335 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002336 context->gva_to_gpa = paging64_gva_to_gpa;
2337 context->root_level = PT64_ROOT_LEVEL;
2338 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002339 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002340 context->gva_to_gpa = paging64_gva_to_gpa;
2341 context->root_level = PT32E_ROOT_LEVEL;
2342 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002343 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002344 context->gva_to_gpa = paging32_gva_to_gpa;
2345 context->root_level = PT32_ROOT_LEVEL;
2346 }
2347
2348 return 0;
2349}
2350
2351static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002353 int r;
2354
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002356 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357
2358 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002359 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002360 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002361 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002363 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002365 r = paging32_init_context(vcpu);
2366
2367 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2368
2369 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370}
2371
Joerg Roedelfb72d162008-02-07 13:47:44 +01002372static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2373{
Anthony Liguori35149e22008-04-02 14:46:56 -05002374 vcpu->arch.update_pte.pfn = bad_pfn;
2375
Joerg Roedelfb72d162008-02-07 13:47:44 +01002376 if (tdp_enabled)
2377 return init_kvm_tdp_mmu(vcpu);
2378 else
2379 return init_kvm_softmmu(vcpu);
2380}
2381
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2383{
2384 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002385 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2386 vcpu->arch.mmu.free(vcpu);
2387 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 }
2389}
2390
2391int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2392{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002393 destroy_kvm_mmu(vcpu);
2394 return init_kvm_mmu(vcpu);
2395}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002396EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002397
2398int kvm_mmu_load(struct kvm_vcpu *vcpu)
2399{
Avi Kivity714b93d2007-01-05 16:36:53 -08002400 int r;
2401
Avi Kivitye2dec932007-01-05 16:36:54 -08002402 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002403 if (r)
2404 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002405 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002406 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002407 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002408 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002409 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002410 if (r)
2411 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002412 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002413 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002414out:
2415 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002417EXPORT_SYMBOL_GPL(kvm_mmu_load);
2418
2419void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2420{
2421 mmu_free_roots(vcpu);
2422}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423
Avi Kivity09072da2007-05-01 14:16:52 +03002424static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002425 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002426 u64 *spte)
2427{
2428 u64 pte;
2429 struct kvm_mmu_page *child;
2430
2431 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002432 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002433 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002434 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002435 else {
2436 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002437 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002438 }
2439 }
Avi Kivityd555c332009-06-10 14:24:23 +03002440 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002441 if (is_large_pte(pte))
2442 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002443}
2444
Avi Kivity00284252007-05-01 16:53:31 +03002445static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002446 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002447 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002448 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002449{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002450 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2451 if (!vcpu->arch.update_pte.largepage ||
2452 sp->role.glevels == PT32_ROOT_LEVEL) {
2453 ++vcpu->kvm->stat.mmu_pde_zapped;
2454 return;
2455 }
2456 }
Avi Kivity00284252007-05-01 16:53:31 +03002457
Avi Kivity4cee5762007-11-18 16:37:07 +02002458 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002459 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002460 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002461 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002462 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002463}
2464
Avi Kivity79539ce2007-11-21 02:06:21 +02002465static bool need_remote_flush(u64 old, u64 new)
2466{
2467 if (!is_shadow_present_pte(old))
2468 return false;
2469 if (!is_shadow_present_pte(new))
2470 return true;
2471 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2472 return true;
2473 old ^= PT64_NX_MASK;
2474 new ^= PT64_NX_MASK;
2475 return (old & ~new & PT64_PERM_MASK) != 0;
2476}
2477
2478static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2479{
2480 if (need_remote_flush(old, new))
2481 kvm_flush_remote_tlbs(vcpu->kvm);
2482 else
2483 kvm_mmu_flush_tlb(vcpu);
2484}
2485
Avi Kivity12b7d282007-09-23 14:10:49 +02002486static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2487{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002488 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002489
Sheng Yang7b523452008-04-25 21:13:50 +08002490 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002491}
2492
Avi Kivityd7824ff2007-12-30 12:29:05 +02002493static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2494 const u8 *new, int bytes)
2495{
2496 gfn_t gfn;
2497 int r;
2498 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002499 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002500
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002501 vcpu->arch.update_pte.largepage = 0;
2502
Avi Kivityd7824ff2007-12-30 12:29:05 +02002503 if (bytes != 4 && bytes != 8)
2504 return;
2505
2506 /*
2507 * Assume that the pte write on a page table of the same type
2508 * as the current vcpu paging mode. This is nearly always true
2509 * (might be false while changing modes). Note it is verified later
2510 * by update_pte().
2511 */
2512 if (is_pae(vcpu)) {
2513 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2514 if ((bytes == 4) && (gpa % 4 == 0)) {
2515 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2516 if (r)
2517 return;
2518 memcpy((void *)&gpte + (gpa % 8), new, 4);
2519 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2520 memcpy((void *)&gpte, new, 8);
2521 }
2522 } else {
2523 if ((bytes == 4) && (gpa % 4 == 0))
2524 memcpy((void *)&gpte, new, 4);
2525 }
Avi Kivity43a37952009-06-10 14:12:05 +03002526 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002527 return;
2528 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002529
Joerg Roedeld25797b2009-07-27 16:30:43 +02002530 if (is_large_pte(gpte) &&
2531 (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL)) {
Joerg Roedelec04b262009-06-19 15:16:23 +02002532 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002533 vcpu->arch.update_pte.largepage = 1;
2534 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002535 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002536 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002537 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002538
Anthony Liguori35149e22008-04-02 14:46:56 -05002539 if (is_error_pfn(pfn)) {
2540 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002541 return;
2542 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002543 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002544 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002545}
2546
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002547static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2548{
2549 u64 *spte = vcpu->arch.last_pte_updated;
2550
2551 if (spte
2552 && vcpu->arch.last_pte_gfn == gfn
2553 && shadow_accessed_mask
2554 && !(*spte & shadow_accessed_mask)
2555 && is_shadow_present_pte(*spte))
2556 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2557}
2558
Avi Kivity09072da2007-05-01 14:16:52 +03002559void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002560 const u8 *new, int bytes,
2561 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002562{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002563 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002564 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002565 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002566 struct hlist_head *bucket;
2567 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002568 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002569 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002570 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002571 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002572 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002573 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002574 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002575 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002576 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002577 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002578 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002579
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002580 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002581 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002582 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002583 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002584 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002585 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002586 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002587 if (guest_initiated) {
2588 if (gfn == vcpu->arch.last_pt_write_gfn
2589 && !last_updated_pte_accessed(vcpu)) {
2590 ++vcpu->arch.last_pt_write_count;
2591 if (vcpu->arch.last_pt_write_count >= 3)
2592 flooded = 1;
2593 } else {
2594 vcpu->arch.last_pt_write_gfn = gfn;
2595 vcpu->arch.last_pt_write_count = 1;
2596 vcpu->arch.last_pte_updated = NULL;
2597 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002598 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002599 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002600 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002601 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002602 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002603 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002604 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002605 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002606 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002607 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002608 /*
2609 * Misaligned accesses are too much trouble to fix
2610 * up; also, they usually indicate a page is not used
2611 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002612 *
2613 * If we're seeing too many writes to a page,
2614 * it may no longer be a page table, or we may be
2615 * forking, in which case it is better to unmap the
2616 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002617 */
2618 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002619 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002620 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2621 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002622 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002623 continue;
2624 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002625 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002626 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002627 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002628 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002629 page_offset <<= 1; /* 32->64 */
2630 /*
2631 * A 32-bit pde maps 4MB while the shadow pdes map
2632 * only 2MB. So we need to double the offset again
2633 * and zap two pdes instead of one.
2634 */
2635 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002636 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002637 page_offset <<= 1;
2638 npte = 2;
2639 }
Avi Kivityfce06572007-05-01 16:44:05 +03002640 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002641 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002642 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002643 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002644 }
Avi Kivity4db35312007-11-21 15:28:32 +02002645 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002646 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2647 gentry = 0;
2648 r = kvm_read_guest_atomic(vcpu->kvm,
2649 gpa & ~(u64)(pte_size - 1),
2650 &gentry, pte_size);
2651 new = (const void *)&gentry;
2652 if (r < 0)
2653 new = NULL;
2654 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002655 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002656 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002657 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002658 if (new)
2659 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002660 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002661 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002662 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002663 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002664 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002665 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002666 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2667 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2668 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002669 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002670}
2671
Avi Kivitya4360362007-01-05 16:36:45 -08002672int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2673{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002674 gpa_t gpa;
2675 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002676
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002677 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002678
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002679 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002680 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002681 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002682 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002683}
Avi Kivity577bdc42008-07-19 08:57:05 +03002684EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002685
Avi Kivity22d95b12007-09-14 20:26:06 +03002686void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002687{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002688 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002689 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002690
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002691 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002692 struct kvm_mmu_page, link);
2693 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002694 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002695 }
2696}
Avi Kivityebeace82007-01-05 16:36:47 -08002697
Avi Kivity30677142007-10-28 18:48:59 +02002698int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2699{
2700 int r;
2701 enum emulation_result er;
2702
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002703 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002704 if (r < 0)
2705 goto out;
2706
2707 if (!r) {
2708 r = 1;
2709 goto out;
2710 }
2711
Avi Kivityb733bfb2007-10-28 18:52:05 +02002712 r = mmu_topup_memory_caches(vcpu);
2713 if (r)
2714 goto out;
2715
Avi Kivity30677142007-10-28 18:48:59 +02002716 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002717
2718 switch (er) {
2719 case EMULATE_DONE:
2720 return 1;
2721 case EMULATE_DO_MMIO:
2722 ++vcpu->stat.mmio_exits;
2723 return 0;
2724 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002725 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2726 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2727 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002728 default:
2729 BUG();
2730 }
2731out:
Avi Kivity30677142007-10-28 18:48:59 +02002732 return r;
2733}
2734EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2735
Marcelo Tosattia7052892008-09-23 13:18:35 -03002736void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2737{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002738 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002739 kvm_mmu_flush_tlb(vcpu);
2740 ++vcpu->stat.invlpg;
2741}
2742EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2743
Joerg Roedel18552672008-02-07 13:47:41 +01002744void kvm_enable_tdp(void)
2745{
2746 tdp_enabled = true;
2747}
2748EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2749
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002750void kvm_disable_tdp(void)
2751{
2752 tdp_enabled = false;
2753}
2754EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2755
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756static void free_mmu_pages(struct kvm_vcpu *vcpu)
2757{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002758 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759}
2760
2761static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2762{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002763 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 int i;
2765
2766 ASSERT(vcpu);
2767
Marcelo Tosatti6a1ac772009-07-15 15:34:41 -03002768 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002769 if (vcpu->kvm->arch.n_requested_mmu_pages)
2770 vcpu->kvm->arch.n_free_mmu_pages =
2771 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002772 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002773 vcpu->kvm->arch.n_free_mmu_pages =
2774 vcpu->kvm->arch.n_alloc_mmu_pages;
Marcelo Tosatti6a1ac772009-07-15 15:34:41 -03002775 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002776 /*
2777 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2778 * Therefore we need to allocate shadow page tables in the first
2779 * 4GB of memory, which happens to fit the DMA32 zone.
2780 */
2781 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2782 if (!page)
2783 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002784 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002785 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002786 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002787
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 return 0;
2789
2790error_1:
2791 free_mmu_pages(vcpu);
2792 return -ENOMEM;
2793}
2794
Ingo Molnar8018c272006-12-29 16:50:01 -08002795int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002798 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799
Ingo Molnar8018c272006-12-29 16:50:01 -08002800 return alloc_mmu_pages(vcpu);
2801}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802
Ingo Molnar8018c272006-12-29 16:50:01 -08002803int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2804{
2805 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002806 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002807
Ingo Molnar8018c272006-12-29 16:50:01 -08002808 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809}
2810
2811void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2812{
2813 ASSERT(vcpu);
2814
2815 destroy_kvm_mmu(vcpu);
2816 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002817 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818}
2819
Avi Kivity90cb0522007-07-17 13:04:56 +03002820void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821{
Avi Kivity4db35312007-11-21 15:28:32 +02002822 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002824 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 int i;
2826 u64 *pt;
2827
Sheng Yang291f26b2008-10-16 17:30:57 +08002828 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 continue;
2830
Avi Kivity4db35312007-11-21 15:28:32 +02002831 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2833 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002834 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 }
Avi Kivity171d5952008-08-27 16:40:51 +03002837 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002839
Avi Kivity90cb0522007-07-17 13:04:56 +03002840void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002841{
Avi Kivity4db35312007-11-21 15:28:32 +02002842 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002843
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002844 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002845 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002846 if (kvm_mmu_zap_page(kvm, sp))
2847 node = container_of(kvm->arch.active_mmu_pages.next,
2848 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002849 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002850
Avi Kivity90cb0522007-07-17 13:04:56 +03002851 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002852}
2853
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002854static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002855{
2856 struct kvm_mmu_page *page;
2857
2858 page = container_of(kvm->arch.active_mmu_pages.prev,
2859 struct kvm_mmu_page, link);
2860 kvm_mmu_zap_page(kvm, page);
2861}
2862
2863static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2864{
2865 struct kvm *kvm;
2866 struct kvm *kvm_freed = NULL;
2867 int cache_count = 0;
2868
2869 spin_lock(&kvm_lock);
2870
2871 list_for_each_entry(kvm, &vm_list, vm_list) {
2872 int npages;
2873
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002874 if (!down_read_trylock(&kvm->slots_lock))
2875 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002876 spin_lock(&kvm->mmu_lock);
2877 npages = kvm->arch.n_alloc_mmu_pages -
2878 kvm->arch.n_free_mmu_pages;
2879 cache_count += npages;
2880 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2881 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2882 cache_count--;
2883 kvm_freed = kvm;
2884 }
2885 nr_to_scan--;
2886
2887 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002888 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002889 }
2890 if (kvm_freed)
2891 list_move_tail(&kvm_freed->vm_list, &vm_list);
2892
2893 spin_unlock(&kvm_lock);
2894
2895 return cache_count;
2896}
2897
2898static struct shrinker mmu_shrinker = {
2899 .shrink = mmu_shrink,
2900 .seeks = DEFAULT_SEEKS * 10,
2901};
2902
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002903static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002904{
2905 if (pte_chain_cache)
2906 kmem_cache_destroy(pte_chain_cache);
2907 if (rmap_desc_cache)
2908 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002909 if (mmu_page_header_cache)
2910 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002911}
2912
Izik Eidus3ee16c82008-03-30 15:17:21 +03002913void kvm_mmu_module_exit(void)
2914{
2915 mmu_destroy_caches();
2916 unregister_shrinker(&mmu_shrinker);
2917}
2918
Avi Kivityb5a33a72007-04-15 16:31:09 +03002919int kvm_mmu_module_init(void)
2920{
2921 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2922 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002923 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002924 if (!pte_chain_cache)
2925 goto nomem;
2926 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2927 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002928 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002929 if (!rmap_desc_cache)
2930 goto nomem;
2931
Avi Kivityd3d25b02007-05-30 12:34:53 +03002932 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2933 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002934 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002935 if (!mmu_page_header_cache)
2936 goto nomem;
2937
Izik Eidus3ee16c82008-03-30 15:17:21 +03002938 register_shrinker(&mmu_shrinker);
2939
Avi Kivityb5a33a72007-04-15 16:31:09 +03002940 return 0;
2941
2942nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002943 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002944 return -ENOMEM;
2945}
2946
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002947/*
2948 * Caculate mmu pages needed for kvm.
2949 */
2950unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2951{
2952 int i;
2953 unsigned int nr_mmu_pages;
2954 unsigned int nr_pages = 0;
2955
2956 for (i = 0; i < kvm->nmemslots; i++)
2957 nr_pages += kvm->memslots[i].npages;
2958
2959 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2960 nr_mmu_pages = max(nr_mmu_pages,
2961 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2962
2963 return nr_mmu_pages;
2964}
2965
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002966static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2967 unsigned len)
2968{
2969 if (len > buffer->len)
2970 return NULL;
2971 return buffer->ptr;
2972}
2973
2974static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2975 unsigned len)
2976{
2977 void *ret;
2978
2979 ret = pv_mmu_peek_buffer(buffer, len);
2980 if (!ret)
2981 return ret;
2982 buffer->ptr += len;
2983 buffer->len -= len;
2984 buffer->processed += len;
2985 return ret;
2986}
2987
2988static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2989 gpa_t addr, gpa_t value)
2990{
2991 int bytes = 8;
2992 int r;
2993
2994 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2995 bytes = 4;
2996
2997 r = mmu_topup_memory_caches(vcpu);
2998 if (r)
2999 return r;
3000
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003001 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003002 return -EFAULT;
3003
3004 return 1;
3005}
3006
3007static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3008{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003009 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003010 return 1;
3011}
3012
3013static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3014{
3015 spin_lock(&vcpu->kvm->mmu_lock);
3016 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3017 spin_unlock(&vcpu->kvm->mmu_lock);
3018 return 1;
3019}
3020
3021static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3022 struct kvm_pv_mmu_op_buffer *buffer)
3023{
3024 struct kvm_mmu_op_header *header;
3025
3026 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3027 if (!header)
3028 return 0;
3029 switch (header->op) {
3030 case KVM_MMU_OP_WRITE_PTE: {
3031 struct kvm_mmu_op_write_pte *wpte;
3032
3033 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3034 if (!wpte)
3035 return 0;
3036 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3037 wpte->pte_val);
3038 }
3039 case KVM_MMU_OP_FLUSH_TLB: {
3040 struct kvm_mmu_op_flush_tlb *ftlb;
3041
3042 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3043 if (!ftlb)
3044 return 0;
3045 return kvm_pv_mmu_flush_tlb(vcpu);
3046 }
3047 case KVM_MMU_OP_RELEASE_PT: {
3048 struct kvm_mmu_op_release_pt *rpt;
3049
3050 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3051 if (!rpt)
3052 return 0;
3053 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3054 }
3055 default: return 0;
3056 }
3057}
3058
3059int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3060 gpa_t addr, unsigned long *ret)
3061{
3062 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003063 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003064
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003065 buffer->ptr = buffer->buf;
3066 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3067 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003068
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003069 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003070 if (r)
3071 goto out;
3072
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003073 while (buffer->len) {
3074 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003075 if (r < 0)
3076 goto out;
3077 if (r == 0)
3078 break;
3079 }
3080
3081 r = 1;
3082out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003083 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003084 return r;
3085}
3086
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003087int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3088{
3089 struct kvm_shadow_walk_iterator iterator;
3090 int nr_sptes = 0;
3091
3092 spin_lock(&vcpu->kvm->mmu_lock);
3093 for_each_shadow_entry(vcpu, addr, iterator) {
3094 sptes[iterator.level-1] = *iterator.sptep;
3095 nr_sptes++;
3096 if (!is_shadow_present_pte(*iterator.sptep))
3097 break;
3098 }
3099 spin_unlock(&vcpu->kvm->mmu_lock);
3100
3101 return nr_sptes;
3102}
3103EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3104
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003105#ifdef AUDIT
3106
3107static const char *audit_msg;
3108
3109static gva_t canonicalize(gva_t gva)
3110{
3111#ifdef CONFIG_X86_64
3112 gva = (long long)(gva << 16) >> 16;
3113#endif
3114 return gva;
3115}
3116
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003117
3118typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3119 u64 *sptep);
3120
3121static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3122 inspect_spte_fn fn)
3123{
3124 int i;
3125
3126 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3127 u64 ent = sp->spt[i];
3128
3129 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003130 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003131 struct kvm_mmu_page *child;
3132 child = page_header(ent & PT64_BASE_ADDR_MASK);
3133 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003134 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003135 fn(kvm, sp, &sp->spt[i]);
3136 }
3137 }
3138}
3139
3140static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3141{
3142 int i;
3143 struct kvm_mmu_page *sp;
3144
3145 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3146 return;
3147 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3148 hpa_t root = vcpu->arch.mmu.root_hpa;
3149 sp = page_header(root);
3150 __mmu_spte_walk(vcpu->kvm, sp, fn);
3151 return;
3152 }
3153 for (i = 0; i < 4; ++i) {
3154 hpa_t root = vcpu->arch.mmu.pae_root[i];
3155
3156 if (root && VALID_PAGE(root)) {
3157 root &= PT64_BASE_ADDR_MASK;
3158 sp = page_header(root);
3159 __mmu_spte_walk(vcpu->kvm, sp, fn);
3160 }
3161 }
3162 return;
3163}
3164
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003165static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3166 gva_t va, int level)
3167{
3168 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3169 int i;
3170 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3171
3172 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3173 u64 ent = pt[i];
3174
Avi Kivityc7addb92007-09-16 18:58:32 +02003175 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003176 continue;
3177
3178 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003179 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3180 audit_mappings_page(vcpu, ent, va, level - 1);
3181 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003182 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003183 gfn_t gfn = gpa >> PAGE_SHIFT;
3184 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3185 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003186
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003187 if (is_error_pfn(pfn)) {
3188 kvm_release_pfn_clean(pfn);
3189 continue;
3190 }
3191
Avi Kivityc7addb92007-09-16 18:58:32 +02003192 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003193 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003194 printk(KERN_ERR "xx audit error: (%s) levels %d"
3195 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003196 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003197 va, gpa, hpa, ent,
3198 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003199 else if (ent == shadow_notrap_nonpresent_pte
3200 && !is_error_hpa(hpa))
3201 printk(KERN_ERR "audit: (%s) notrap shadow,"
3202 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003203 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003204
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003205 }
3206 }
3207}
3208
3209static void audit_mappings(struct kvm_vcpu *vcpu)
3210{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003211 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003212
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003213 if (vcpu->arch.mmu.root_level == 4)
3214 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003215 else
3216 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003217 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003218 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003219 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003220 i << 30,
3221 2);
3222}
3223
3224static int count_rmaps(struct kvm_vcpu *vcpu)
3225{
3226 int nmaps = 0;
3227 int i, j, k;
3228
3229 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3230 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3231 struct kvm_rmap_desc *d;
3232
3233 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003234 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003235
Izik Eidus290fc382007-09-27 14:11:22 +02003236 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003237 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003238 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003239 ++nmaps;
3240 continue;
3241 }
Izik Eidus290fc382007-09-27 14:11:22 +02003242 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003243 while (d) {
3244 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003245 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003246 ++nmaps;
3247 else
3248 break;
3249 d = d->more;
3250 }
3251 }
3252 }
3253 return nmaps;
3254}
3255
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003256void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003257{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003258 unsigned long *rmapp;
3259 struct kvm_mmu_page *rev_sp;
3260 gfn_t gfn;
3261
3262 if (*sptep & PT_WRITABLE_MASK) {
3263 rev_sp = page_header(__pa(sptep));
3264 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3265
3266 if (!gfn_to_memslot(kvm, gfn)) {
3267 if (!printk_ratelimit())
3268 return;
3269 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3270 audit_msg, gfn);
3271 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3272 audit_msg, sptep - rev_sp->spt,
3273 rev_sp->gfn);
3274 dump_stack();
3275 return;
3276 }
3277
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003278 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3279 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003280 if (!*rmapp) {
3281 if (!printk_ratelimit())
3282 return;
3283 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3284 audit_msg, *sptep);
3285 dump_stack();
3286 }
3287 }
3288
3289}
3290
3291void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3292{
3293 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3294}
3295
3296static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3297{
Avi Kivity4db35312007-11-21 15:28:32 +02003298 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003299 int i;
3300
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003301 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003302 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303
Avi Kivity4db35312007-11-21 15:28:32 +02003304 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003305 continue;
3306
3307 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3308 u64 ent = pt[i];
3309
3310 if (!(ent & PT_PRESENT_MASK))
3311 continue;
3312 if (!(ent & PT_WRITABLE_MASK))
3313 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003314 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315 }
3316 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003317 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003318}
3319
3320static void audit_rmap(struct kvm_vcpu *vcpu)
3321{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003322 check_writable_mappings_rmap(vcpu);
3323 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003324}
3325
3326static void audit_write_protection(struct kvm_vcpu *vcpu)
3327{
Avi Kivity4db35312007-11-21 15:28:32 +02003328 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003329 struct kvm_memory_slot *slot;
3330 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003331 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003332 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003333
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003334 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003335 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003336 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003337 if (sp->unsync)
3338 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003339
Avi Kivity4db35312007-11-21 15:28:32 +02003340 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003341 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003342 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003343
3344 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3345 while (spte) {
3346 if (*spte & PT_WRITABLE_MASK)
3347 printk(KERN_ERR "%s: (%s) shadow page has "
3348 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003349 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003350 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003351 spte = rmap_next(vcpu->kvm, rmapp, spte);
3352 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003353 }
3354}
3355
3356static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3357{
3358 int olddbg = dbg;
3359
3360 dbg = 0;
3361 audit_msg = msg;
3362 audit_rmap(vcpu);
3363 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003364 if (strcmp("pre pte write", audit_msg) != 0)
3365 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003366 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367 dbg = olddbg;
3368}
3369
3370#endif