blob: 110c224ed1fb1802b496cdeb2f61e15439cf9d65 [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;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200260 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300261 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{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200756 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200757 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 Roedel852e3c12009-07-27 16:30:44 +0200775
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200776 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200777
778 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
779 int idx = gfn_offset;
780 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
781 retval |= handler(kvm,
782 &memslot->lpage_info[j][idx].rmap_pde);
783 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200784 }
785 }
786
787 return retval;
788}
789
790int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
791{
792 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
793}
794
795static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
796{
797 u64 *spte;
798 int young = 0;
799
Sheng Yang534e38b2008-09-08 15:12:30 +0800800 /* always return old for EPT */
801 if (!shadow_accessed_mask)
802 return 0;
803
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200804 spte = rmap_next(kvm, rmapp, NULL);
805 while (spte) {
806 int _young;
807 u64 _spte = *spte;
808 BUG_ON(!(_spte & PT_PRESENT_MASK));
809 _young = _spte & PT_ACCESSED_MASK;
810 if (_young) {
811 young = 1;
812 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
813 }
814 spte = rmap_next(kvm, rmapp, spte);
815 }
816 return young;
817}
818
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300819#define RMAP_RECYCLE_THRESHOLD 1000
820
Joerg Roedel852e3c12009-07-27 16:30:44 +0200821static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300822{
823 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200824 struct kvm_mmu_page *sp;
825
826 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300827
828 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200829 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300830
831 kvm_unmap_rmapp(vcpu->kvm, rmapp);
832 kvm_flush_remote_tlbs(vcpu->kvm);
833}
834
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200835int kvm_age_hva(struct kvm *kvm, unsigned long hva)
836{
837 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
838}
839
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800840#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300841static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842{
Avi Kivity139bdb22007-01-05 16:36:50 -0800843 u64 *pos;
844 u64 *end;
845
Avi Kivity47ad8e62007-05-06 15:50:58 +0300846 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300847 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800848 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800849 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800851 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 return 1;
853}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800854#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855
Avi Kivity4db35312007-11-21 15:28:32 +0200856static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800857{
Avi Kivity4db35312007-11-21 15:28:32 +0200858 ASSERT(is_empty_shadow_page(sp->spt));
859 list_del(&sp->link);
860 __free_page(virt_to_page(sp->spt));
861 __free_page(virt_to_page(sp->gfns));
862 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800863 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800864}
865
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800866static unsigned kvm_page_table_hashfn(gfn_t gfn)
867{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200868 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800869}
870
Avi Kivity25c0de22007-01-05 16:36:42 -0800871static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
872 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873{
Avi Kivity4db35312007-11-21 15:28:32 +0200874 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800876 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
877 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
878 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200879 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800880 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200881 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800882 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200883 sp->multimapped = 0;
884 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800885 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200886 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887}
888
Avi Kivity714b93d2007-01-05 16:36:53 -0800889static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200890 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800891{
892 struct kvm_pte_chain *pte_chain;
893 struct hlist_node *node;
894 int i;
895
896 if (!parent_pte)
897 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200898 if (!sp->multimapped) {
899 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800900
901 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200902 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800903 return;
904 }
Avi Kivity4db35312007-11-21 15:28:32 +0200905 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800906 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200907 INIT_HLIST_HEAD(&sp->parent_ptes);
908 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800909 pte_chain->parent_ptes[0] = old;
910 }
Avi Kivity4db35312007-11-21 15:28:32 +0200911 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800912 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
913 continue;
914 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
915 if (!pte_chain->parent_ptes[i]) {
916 pte_chain->parent_ptes[i] = parent_pte;
917 return;
918 }
919 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800920 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800921 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200922 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800923 pte_chain->parent_ptes[0] = parent_pte;
924}
925
Avi Kivity4db35312007-11-21 15:28:32 +0200926static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927 u64 *parent_pte)
928{
929 struct kvm_pte_chain *pte_chain;
930 struct hlist_node *node;
931 int i;
932
Avi Kivity4db35312007-11-21 15:28:32 +0200933 if (!sp->multimapped) {
934 BUG_ON(sp->parent_pte != parent_pte);
935 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800936 return;
937 }
Avi Kivity4db35312007-11-21 15:28:32 +0200938 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800939 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
940 if (!pte_chain->parent_ptes[i])
941 break;
942 if (pte_chain->parent_ptes[i] != parent_pte)
943 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800944 while (i + 1 < NR_PTE_CHAIN_ENTRIES
945 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800946 pte_chain->parent_ptes[i]
947 = pte_chain->parent_ptes[i + 1];
948 ++i;
949 }
950 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800951 if (i == 0) {
952 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300953 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200954 if (hlist_empty(&sp->parent_ptes)) {
955 sp->multimapped = 0;
956 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800957 }
958 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800959 return;
960 }
961 BUG();
962}
963
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300964
965static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
966 mmu_parent_walk_fn fn)
967{
968 struct kvm_pte_chain *pte_chain;
969 struct hlist_node *node;
970 struct kvm_mmu_page *parent_sp;
971 int i;
972
973 if (!sp->multimapped && sp->parent_pte) {
974 parent_sp = page_header(__pa(sp->parent_pte));
975 fn(vcpu, parent_sp);
976 mmu_parent_walk(vcpu, parent_sp, fn);
977 return;
978 }
979 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
980 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
981 if (!pte_chain->parent_ptes[i])
982 break;
983 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
984 fn(vcpu, parent_sp);
985 mmu_parent_walk(vcpu, parent_sp, fn);
986 }
987}
988
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300989static void kvm_mmu_update_unsync_bitmap(u64 *spte)
990{
991 unsigned int index;
992 struct kvm_mmu_page *sp = page_header(__pa(spte));
993
994 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200995 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
996 sp->unsync_children++;
997 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300998}
999
1000static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1001{
1002 struct kvm_pte_chain *pte_chain;
1003 struct hlist_node *node;
1004 int i;
1005
1006 if (!sp->parent_pte)
1007 return;
1008
1009 if (!sp->multimapped) {
1010 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1011 return;
1012 }
1013
1014 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1015 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1016 if (!pte_chain->parent_ptes[i])
1017 break;
1018 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1019 }
1020}
1021
1022static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1023{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001024 kvm_mmu_update_parents_unsync(sp);
1025 return 1;
1026}
1027
1028static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1029 struct kvm_mmu_page *sp)
1030{
1031 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1032 kvm_mmu_update_parents_unsync(sp);
1033}
1034
Avi Kivityd761a502008-05-29 14:55:03 +03001035static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1036 struct kvm_mmu_page *sp)
1037{
1038 int i;
1039
1040 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1041 sp->spt[i] = shadow_trap_nonpresent_pte;
1042}
1043
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001044static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1045 struct kvm_mmu_page *sp)
1046{
1047 return 1;
1048}
1049
Marcelo Tosattia7052892008-09-23 13:18:35 -03001050static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1051{
1052}
1053
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001054#define KVM_PAGE_ARRAY_NR 16
1055
1056struct kvm_mmu_pages {
1057 struct mmu_page_and_offset {
1058 struct kvm_mmu_page *sp;
1059 unsigned int idx;
1060 } page[KVM_PAGE_ARRAY_NR];
1061 unsigned int nr;
1062};
1063
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001064#define for_each_unsync_children(bitmap, idx) \
1065 for (idx = find_first_bit(bitmap, 512); \
1066 idx < 512; \
1067 idx = find_next_bit(bitmap, 512, idx+1))
1068
Hannes Edercded19f2009-02-21 02:19:13 +01001069static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1070 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001071{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001072 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001073
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001074 if (sp->unsync)
1075 for (i=0; i < pvec->nr; i++)
1076 if (pvec->page[i].sp == sp)
1077 return 0;
1078
1079 pvec->page[pvec->nr].sp = sp;
1080 pvec->page[pvec->nr].idx = idx;
1081 pvec->nr++;
1082 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1083}
1084
1085static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1086 struct kvm_mmu_pages *pvec)
1087{
1088 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001089
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001090 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001091 u64 ent = sp->spt[i];
1092
Marcelo Tosatti87917232008-12-22 18:49:30 -02001093 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001094 struct kvm_mmu_page *child;
1095 child = page_header(ent & PT64_BASE_ADDR_MASK);
1096
1097 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001098 if (mmu_pages_add(pvec, child, i))
1099 return -ENOSPC;
1100
1101 ret = __mmu_unsync_walk(child, pvec);
1102 if (!ret)
1103 __clear_bit(i, sp->unsync_child_bitmap);
1104 else if (ret > 0)
1105 nr_unsync_leaf += ret;
1106 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001107 return ret;
1108 }
1109
1110 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111 nr_unsync_leaf++;
1112 if (mmu_pages_add(pvec, child, i))
1113 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001114 }
1115 }
1116 }
1117
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001118 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001119 sp->unsync_children = 0;
1120
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001121 return nr_unsync_leaf;
1122}
1123
1124static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1125 struct kvm_mmu_pages *pvec)
1126{
1127 if (!sp->unsync_children)
1128 return 0;
1129
1130 mmu_pages_add(pvec, sp, 0);
1131 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001132}
1133
Avi Kivity4db35312007-11-21 15:28:32 +02001134static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001135{
1136 unsigned index;
1137 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001138 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001139 struct hlist_node *node;
1140
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001141 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001142 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001143 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001144 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001145 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001146 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001147 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001148 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001149 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001150 }
1151 return NULL;
1152}
1153
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001154static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1155{
1156 WARN_ON(!sp->unsync);
1157 sp->unsync = 0;
1158 --kvm->stat.mmu_unsync;
1159}
1160
1161static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1162
1163static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1164{
1165 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1166 kvm_mmu_zap_page(vcpu->kvm, sp);
1167 return 1;
1168 }
1169
Avi Kivityf691fe12009-07-06 15:58:14 +03001170 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001171 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1172 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001173 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001174 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1175 kvm_mmu_zap_page(vcpu->kvm, sp);
1176 return 1;
1177 }
1178
1179 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001180 return 0;
1181}
1182
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001183struct mmu_page_path {
1184 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1185 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001186};
1187
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001188#define for_each_sp(pvec, sp, parents, i) \
1189 for (i = mmu_pages_next(&pvec, &parents, -1), \
1190 sp = pvec.page[i].sp; \
1191 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1192 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193
Hannes Edercded19f2009-02-21 02:19:13 +01001194static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1195 struct mmu_page_path *parents,
1196 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001197{
1198 int n;
1199
1200 for (n = i+1; n < pvec->nr; n++) {
1201 struct kvm_mmu_page *sp = pvec->page[n].sp;
1202
1203 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1204 parents->idx[0] = pvec->page[n].idx;
1205 return n;
1206 }
1207
1208 parents->parent[sp->role.level-2] = sp;
1209 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1210 }
1211
1212 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001213}
1214
Hannes Edercded19f2009-02-21 02:19:13 +01001215static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001216{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001217 struct kvm_mmu_page *sp;
1218 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001219
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001220 do {
1221 unsigned int idx = parents->idx[level];
1222
1223 sp = parents->parent[level];
1224 if (!sp)
1225 return;
1226
1227 --sp->unsync_children;
1228 WARN_ON((int)sp->unsync_children < 0);
1229 __clear_bit(idx, sp->unsync_child_bitmap);
1230 level++;
1231 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1232}
1233
1234static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1235 struct mmu_page_path *parents,
1236 struct kvm_mmu_pages *pvec)
1237{
1238 parents->parent[parent->role.level-1] = NULL;
1239 pvec->nr = 0;
1240}
1241
1242static void mmu_sync_children(struct kvm_vcpu *vcpu,
1243 struct kvm_mmu_page *parent)
1244{
1245 int i;
1246 struct kvm_mmu_page *sp;
1247 struct mmu_page_path parents;
1248 struct kvm_mmu_pages pages;
1249
1250 kvm_mmu_pages_init(parent, &parents, &pages);
1251 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001252 int protected = 0;
1253
1254 for_each_sp(pages, sp, parents, i)
1255 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1256
1257 if (protected)
1258 kvm_flush_remote_tlbs(vcpu->kvm);
1259
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001260 for_each_sp(pages, sp, parents, i) {
1261 kvm_sync_page(vcpu, sp);
1262 mmu_pages_clear_parents(&parents);
1263 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001264 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001265 kvm_mmu_pages_init(parent, &parents, &pages);
1266 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001267}
1268
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001269static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1270 gfn_t gfn,
1271 gva_t gaddr,
1272 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001273 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001274 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001275 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001276{
1277 union kvm_mmu_page_role role;
1278 unsigned index;
1279 unsigned quadrant;
1280 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001281 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001282 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001283
Avi Kivitya770f6f2008-12-21 19:20:09 +02001284 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001285 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001286 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001287 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001288 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001289 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1290 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1291 role.quadrant = quadrant;
1292 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001293 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001294 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001295 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1296 if (sp->gfn == gfn) {
1297 if (sp->unsync)
1298 if (kvm_sync_page(vcpu, sp))
1299 continue;
1300
1301 if (sp->role.word != role.word)
1302 continue;
1303
Avi Kivity4db35312007-11-21 15:28:32 +02001304 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001305 if (sp->unsync_children) {
1306 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1307 kvm_mmu_mark_parents_unsync(vcpu, sp);
1308 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001309 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001310 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001311 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001312 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001313 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1314 if (!sp)
1315 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001316 sp->gfn = gfn;
1317 sp->role = role;
1318 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001319 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001320 if (rmap_write_protect(vcpu->kvm, gfn))
1321 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001322 account_shadowed(vcpu->kvm, gfn);
1323 }
Avi Kivity131d8272008-05-29 14:56:28 +03001324 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1325 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1326 else
1327 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001328 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001329 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001330}
1331
Avi Kivity2d111232008-12-25 14:39:47 +02001332static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1333 struct kvm_vcpu *vcpu, u64 addr)
1334{
1335 iterator->addr = addr;
1336 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1337 iterator->level = vcpu->arch.mmu.shadow_root_level;
1338 if (iterator->level == PT32E_ROOT_LEVEL) {
1339 iterator->shadow_addr
1340 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1341 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1342 --iterator->level;
1343 if (!iterator->shadow_addr)
1344 iterator->level = 0;
1345 }
1346}
1347
1348static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1349{
1350 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1351 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001352
1353 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1354 if (is_large_pte(*iterator->sptep))
1355 return false;
1356
Avi Kivity2d111232008-12-25 14:39:47 +02001357 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1358 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1359 return true;
1360}
1361
1362static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1363{
1364 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1365 --iterator->level;
1366}
1367
Avi Kivity90cb0522007-07-17 13:04:56 +03001368static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001369 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001370{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001371 unsigned i;
1372 u64 *pt;
1373 u64 ent;
1374
Avi Kivity4db35312007-11-21 15:28:32 +02001375 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001376
Avi Kivity697fe2e2007-01-05 16:36:46 -08001377 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1378 ent = pt[i];
1379
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001380 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001381 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001382 ent &= PT64_BASE_ADDR_MASK;
1383 mmu_page_remove_parent_pte(page_header(ent),
1384 &pt[i]);
1385 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001386 if (is_large_pte(ent))
1387 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001388 rmap_remove(kvm, &pt[i]);
1389 }
1390 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001391 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001392 }
Avi Kivitya4360362007-01-05 16:36:45 -08001393}
1394
Avi Kivity4db35312007-11-21 15:28:32 +02001395static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001396{
Avi Kivity4db35312007-11-21 15:28:32 +02001397 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001398}
1399
Avi Kivity12b7d282007-09-23 14:10:49 +02001400static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1401{
1402 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001403 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001404
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001405 kvm_for_each_vcpu(i, vcpu, kvm)
1406 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001407}
1408
Avi Kivity31aa2b42008-07-11 17:59:46 +03001409static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001410{
1411 u64 *parent_pte;
1412
Avi Kivity4db35312007-11-21 15:28:32 +02001413 while (sp->multimapped || sp->parent_pte) {
1414 if (!sp->multimapped)
1415 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001416 else {
1417 struct kvm_pte_chain *chain;
1418
Avi Kivity4db35312007-11-21 15:28:32 +02001419 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001420 struct kvm_pte_chain, link);
1421 parent_pte = chain->parent_ptes[0];
1422 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001423 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001424 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001425 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001426 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001427}
1428
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001429static int mmu_zap_unsync_children(struct kvm *kvm,
1430 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001431{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001432 int i, zapped = 0;
1433 struct mmu_page_path parents;
1434 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001435
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001436 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001437 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001438
1439 kvm_mmu_pages_init(parent, &parents, &pages);
1440 while (mmu_unsync_walk(parent, &pages)) {
1441 struct kvm_mmu_page *sp;
1442
1443 for_each_sp(pages, sp, parents, i) {
1444 kvm_mmu_zap_page(kvm, sp);
1445 mmu_pages_clear_parents(&parents);
1446 }
1447 zapped += pages.nr;
1448 kvm_mmu_pages_init(parent, &parents, &pages);
1449 }
1450
1451 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001452}
1453
Marcelo Tosatti07385412008-09-23 13:18:37 -03001454static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001455{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001456 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001457
1458 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001459 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001460 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001461 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001462 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001463 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001464 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001465 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001466 if (sp->unsync)
1467 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001468 if (!sp->root_count) {
1469 hlist_del(&sp->hash_link);
1470 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001471 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001472 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001473 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001474 kvm_reload_remote_mmus(kvm);
1475 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001476 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001477 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001478}
1479
Izik Eidus82ce2c92007-10-02 18:52:55 +02001480/*
1481 * Changing the number of mmu pages allocated to the vm
1482 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1483 */
1484void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1485{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001486 int used_pages;
1487
1488 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1489 used_pages = max(0, used_pages);
1490
Izik Eidus82ce2c92007-10-02 18:52:55 +02001491 /*
1492 * If we set the number of mmu pages to be smaller be than the
1493 * number of actived pages , we must to free some mmu pages before we
1494 * change the value
1495 */
1496
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001497 if (used_pages > kvm_nr_mmu_pages) {
1498 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001499 struct kvm_mmu_page *page;
1500
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001501 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001502 struct kvm_mmu_page, link);
1503 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001504 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001505 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001506 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001507 }
1508 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001509 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1510 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001511
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001512 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001513}
1514
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001515static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001516{
1517 unsigned index;
1518 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001519 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001520 struct hlist_node *node, *n;
1521 int r;
1522
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001523 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001524 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001525 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001526 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001527 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001528 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001529 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001530 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001531 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001532 if (kvm_mmu_zap_page(kvm, sp))
1533 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001534 }
1535 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001536}
1537
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001538static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001539{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001540 unsigned index;
1541 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001542 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001543 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001544
Avi Kivity4677a3b2009-01-06 13:00:27 +02001545 index = kvm_page_table_hashfn(gfn);
1546 bucket = &kvm->arch.mmu_page_hash[index];
1547 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001548 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001549 && !sp->role.invalid) {
1550 pgprintk("%s: zap %lx %x\n",
1551 __func__, gfn, sp->role.word);
1552 kvm_mmu_zap_page(kvm, sp);
1553 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001554 }
1555}
1556
Avi Kivity38c335f2007-11-21 14:20:22 +02001557static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558{
Avi Kivity38c335f2007-11-21 14:20:22 +02001559 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001560 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561
Sheng Yang291f26b2008-10-16 17:30:57 +08001562 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563}
1564
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001565static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1566{
1567 int i;
1568 u64 *pt = sp->spt;
1569
1570 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1571 return;
1572
1573 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1574 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001575 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001576 }
1577}
1578
Avi Kivity039576c2007-03-20 12:46:50 +02001579struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1580{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001581 struct page *page;
1582
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001583 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001584
1585 if (gpa == UNMAPPED_GVA)
1586 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001587
Izik Eidus72dc67a2008-02-10 18:04:15 +02001588 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001589
1590 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001591}
1592
Sheng Yang74be52e2008-10-09 16:01:56 +08001593/*
1594 * The function is based on mtrr_type_lookup() in
1595 * arch/x86/kernel/cpu/mtrr/generic.c
1596 */
1597static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1598 u64 start, u64 end)
1599{
1600 int i;
1601 u64 base, mask;
1602 u8 prev_match, curr_match;
1603 int num_var_ranges = KVM_NR_VAR_MTRR;
1604
1605 if (!mtrr_state->enabled)
1606 return 0xFF;
1607
1608 /* Make end inclusive end, instead of exclusive */
1609 end--;
1610
1611 /* Look in fixed ranges. Just return the type as per start */
1612 if (mtrr_state->have_fixed && (start < 0x100000)) {
1613 int idx;
1614
1615 if (start < 0x80000) {
1616 idx = 0;
1617 idx += (start >> 16);
1618 return mtrr_state->fixed_ranges[idx];
1619 } else if (start < 0xC0000) {
1620 idx = 1 * 8;
1621 idx += ((start - 0x80000) >> 14);
1622 return mtrr_state->fixed_ranges[idx];
1623 } else if (start < 0x1000000) {
1624 idx = 3 * 8;
1625 idx += ((start - 0xC0000) >> 12);
1626 return mtrr_state->fixed_ranges[idx];
1627 }
1628 }
1629
1630 /*
1631 * Look in variable ranges
1632 * Look of multiple ranges matching this address and pick type
1633 * as per MTRR precedence
1634 */
1635 if (!(mtrr_state->enabled & 2))
1636 return mtrr_state->def_type;
1637
1638 prev_match = 0xFF;
1639 for (i = 0; i < num_var_ranges; ++i) {
1640 unsigned short start_state, end_state;
1641
1642 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1643 continue;
1644
1645 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1646 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1647 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1648 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1649
1650 start_state = ((start & mask) == (base & mask));
1651 end_state = ((end & mask) == (base & mask));
1652 if (start_state != end_state)
1653 return 0xFE;
1654
1655 if ((start & mask) != (base & mask))
1656 continue;
1657
1658 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1659 if (prev_match == 0xFF) {
1660 prev_match = curr_match;
1661 continue;
1662 }
1663
1664 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1665 curr_match == MTRR_TYPE_UNCACHABLE)
1666 return MTRR_TYPE_UNCACHABLE;
1667
1668 if ((prev_match == MTRR_TYPE_WRBACK &&
1669 curr_match == MTRR_TYPE_WRTHROUGH) ||
1670 (prev_match == MTRR_TYPE_WRTHROUGH &&
1671 curr_match == MTRR_TYPE_WRBACK)) {
1672 prev_match = MTRR_TYPE_WRTHROUGH;
1673 curr_match = MTRR_TYPE_WRTHROUGH;
1674 }
1675
1676 if (prev_match != curr_match)
1677 return MTRR_TYPE_UNCACHABLE;
1678 }
1679
1680 if (prev_match != 0xFF)
1681 return prev_match;
1682
1683 return mtrr_state->def_type;
1684}
1685
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001686u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001687{
1688 u8 mtrr;
1689
1690 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1691 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1692 if (mtrr == 0xfe || mtrr == 0xff)
1693 mtrr = MTRR_TYPE_WRBACK;
1694 return mtrr;
1695}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001696EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001697
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001698static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1699{
1700 unsigned index;
1701 struct hlist_head *bucket;
1702 struct kvm_mmu_page *s;
1703 struct hlist_node *node, *n;
1704
Avi Kivityf691fe12009-07-06 15:58:14 +03001705 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001706 index = kvm_page_table_hashfn(sp->gfn);
1707 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1708 /* don't unsync if pagetable is shadowed with multiple roles */
1709 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001710 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001711 continue;
1712 if (s->role.word != sp->role.word)
1713 return 1;
1714 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001715 ++vcpu->kvm->stat.mmu_unsync;
1716 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001717
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001718 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001719
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001720 mmu_convert_notrap(sp);
1721 return 0;
1722}
1723
1724static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1725 bool can_unsync)
1726{
1727 struct kvm_mmu_page *shadow;
1728
1729 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1730 if (shadow) {
1731 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1732 return 1;
1733 if (shadow->unsync)
1734 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001735 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001736 return kvm_unsync_page(vcpu, shadow);
1737 return 1;
1738 }
1739 return 0;
1740}
1741
Avi Kivityd555c332009-06-10 14:24:23 +03001742static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001743 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001744 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001745 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001746 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001747{
1748 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001749 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001750
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001751 /*
1752 * We don't set the accessed bit, since we sometimes want to see
1753 * whether the guest actually used the pte (in order to detect
1754 * demand paging).
1755 */
Sheng Yang7b523452008-04-25 21:13:50 +08001756 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001757 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001758 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001759 if (!dirty)
1760 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001761 if (pte_access & ACC_EXEC_MASK)
1762 spte |= shadow_x_mask;
1763 else
1764 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001765 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001766 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001767 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001768 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001769 if (tdp_enabled)
1770 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1771 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001772
Anthony Liguori35149e22008-04-02 14:46:56 -05001773 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001774
1775 if ((pte_access & ACC_WRITE_MASK)
1776 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001777
Joerg Roedel852e3c12009-07-27 16:30:44 +02001778 if (level > PT_PAGE_TABLE_LEVEL &&
1779 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001780 ret = 1;
1781 spte = shadow_trap_nonpresent_pte;
1782 goto set_pte;
1783 }
1784
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001785 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001786
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001787 /*
1788 * Optimization: for pte sync, if spte was writable the hash
1789 * lookup is unnecessary (and expensive). Write protection
1790 * is responsibility of mmu_get_page / kvm_sync_page.
1791 * Same reasoning can be applied to dirty page accounting.
1792 */
Avi Kivityd555c332009-06-10 14:24:23 +03001793 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001794 goto set_pte;
1795
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001796 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001797 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001798 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001799 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001800 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001801 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001802 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001803 }
1804 }
1805
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001806 if (pte_access & ACC_WRITE_MASK)
1807 mark_page_dirty(vcpu->kvm, gfn);
1808
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001809set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001810 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001811 return ret;
1812}
1813
Avi Kivityd555c332009-06-10 14:24:23 +03001814static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001815 unsigned pt_access, unsigned pte_access,
1816 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001817 int *ptwrite, int level, gfn_t gfn,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001818 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001819{
1820 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001821 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001822 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001823
1824 pgprintk("%s: spte %llx access %x write_fault %d"
1825 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001826 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001827 write_fault, user_fault, gfn);
1828
Avi Kivityd555c332009-06-10 14:24:23 +03001829 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001830 /*
1831 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1832 * the parent of the now unreachable PTE.
1833 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001834 if (level > PT_PAGE_TABLE_LEVEL &&
1835 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001836 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001837 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001838
1839 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001840 mmu_page_remove_parent_pte(child, sptep);
1841 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001842 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001843 spte_to_pfn(*sptep), pfn);
1844 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001845 } else
1846 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001847 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001848
Avi Kivityd555c332009-06-10 14:24:23 +03001849 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001850 dirty, level, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001851 if (write_fault)
1852 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001853 kvm_x86_ops->tlb_flush(vcpu);
1854 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001855
Avi Kivityd555c332009-06-10 14:24:23 +03001856 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001857 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001858 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001859 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1860 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001861 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001862 ++vcpu->kvm->stat.lpages;
1863
Avi Kivityd555c332009-06-10 14:24:23 +03001864 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001865 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001866 rmap_count = rmap_add(vcpu, sptep, gfn);
Avi Kivityd555c332009-06-10 14:24:23 +03001867 if (!is_rmap_spte(*sptep))
Anthony Liguori35149e22008-04-02 14:46:56 -05001868 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001869 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001870 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001871 } else {
1872 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001873 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001874 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001875 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001876 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001877 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001878 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001879 vcpu->arch.last_pte_gfn = gfn;
1880 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001881}
1882
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1884{
1885}
1886
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001887static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001888 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889{
Avi Kivity9f652d212008-12-25 14:54:25 +02001890 struct kvm_shadow_walk_iterator iterator;
1891 struct kvm_mmu_page *sp;
1892 int pt_write = 0;
1893 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894
Avi Kivity9f652d212008-12-25 14:54:25 +02001895 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001896 if (iterator.level == level) {
Avi Kivity9f652d212008-12-25 14:54:25 +02001897 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1898 0, write, 1, &pt_write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001899 level, gfn, pfn, false);
Avi Kivity9f652d212008-12-25 14:54:25 +02001900 ++vcpu->stat.pf_fixed;
1901 break;
1902 }
1903
1904 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1905 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1906 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1907 iterator.level - 1,
1908 1, ACC_ALL, iterator.sptep);
1909 if (!sp) {
1910 pgprintk("nonpaging_map: ENOMEM\n");
1911 kvm_release_pfn_clean(pfn);
1912 return -ENOMEM;
1913 }
1914
Avi Kivityd555c332009-06-10 14:24:23 +03001915 __set_spte(iterator.sptep,
1916 __pa(sp->spt)
1917 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1918 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02001919 }
1920 }
1921 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922}
1923
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001924static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1925{
1926 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001927 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001928 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001929 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001930
Joerg Roedel852e3c12009-07-27 16:30:44 +02001931 level = mapping_level(vcpu, gfn);
1932
1933 /*
1934 * This path builds a PAE pagetable - so we can map 2mb pages at
1935 * maximum. Therefore check if the level is larger than that.
1936 */
1937 if (level > PT_DIRECTORY_LEVEL)
1938 level = PT_DIRECTORY_LEVEL;
1939
1940 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001941
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001942 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001943 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001944 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001945
Avi Kivityd196e342008-01-24 11:44:11 +02001946 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001947 if (is_error_pfn(pfn)) {
1948 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001949 return 1;
1950 }
1951
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001952 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001953 if (mmu_notifier_retry(vcpu, mmu_seq))
1954 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001955 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02001956 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001957 spin_unlock(&vcpu->kvm->mmu_lock);
1958
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001959
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001960 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001961
1962out_unlock:
1963 spin_unlock(&vcpu->kvm->mmu_lock);
1964 kvm_release_pfn_clean(pfn);
1965 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001966}
1967
1968
Avi Kivity17ac10a2007-01-05 16:36:40 -08001969static void mmu_free_roots(struct kvm_vcpu *vcpu)
1970{
1971 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001972 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001973
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001974 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001975 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001976 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001977 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1978 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001979
Avi Kivity4db35312007-11-21 15:28:32 +02001980 sp = page_header(root);
1981 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001982 if (!sp->root_count && sp->role.invalid)
1983 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001985 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001986 return;
1987 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001988 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001989 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001990
Avi Kivity417726a2007-04-12 17:35:58 +03001991 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001992 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001993 sp = page_header(root);
1994 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001995 if (!sp->root_count && sp->role.invalid)
1996 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001997 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001998 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001999 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002000 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002001 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002002}
2003
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002004static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2005{
2006 int ret = 0;
2007
2008 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2009 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2010 ret = 1;
2011 }
2012
2013 return ret;
2014}
2015
2016static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002017{
2018 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002019 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002020 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002021 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002022 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002023
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002024 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002025
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002026 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2027 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002028
2029 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002030 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002031 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002032 if (mmu_check_root(vcpu, root_gfn))
2033 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002034 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002035 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002036 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002037 root = __pa(sp->spt);
2038 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002039 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002040 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002041 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002042 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002043 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002044 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002045 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002046 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002047
2048 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002049 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002050 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002051 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002053 continue;
2054 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002055 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002056 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002057 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002058 if (mmu_check_root(vcpu, root_gfn))
2059 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002060 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002061 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002062 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002063 root = __pa(sp->spt);
2064 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002065 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002066 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002067 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002068 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002069}
2070
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002071static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2072{
2073 int i;
2074 struct kvm_mmu_page *sp;
2075
2076 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2077 return;
2078 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2079 hpa_t root = vcpu->arch.mmu.root_hpa;
2080 sp = page_header(root);
2081 mmu_sync_children(vcpu, sp);
2082 return;
2083 }
2084 for (i = 0; i < 4; ++i) {
2085 hpa_t root = vcpu->arch.mmu.pae_root[i];
2086
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002087 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002088 root &= PT64_BASE_ADDR_MASK;
2089 sp = page_header(root);
2090 mmu_sync_children(vcpu, sp);
2091 }
2092 }
2093}
2094
2095void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2096{
2097 spin_lock(&vcpu->kvm->mmu_lock);
2098 mmu_sync_roots(vcpu);
2099 spin_unlock(&vcpu->kvm->mmu_lock);
2100}
2101
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2103{
2104 return vaddr;
2105}
2106
2107static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002108 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109{
Avi Kivitye8332402007-12-09 18:43:00 +02002110 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002111 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002113 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002114 r = mmu_topup_memory_caches(vcpu);
2115 if (r)
2116 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002117
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002119 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120
Avi Kivitye8332402007-12-09 18:43:00 +02002121 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
Avi Kivitye8332402007-12-09 18:43:00 +02002123 return nonpaging_map(vcpu, gva & PAGE_MASK,
2124 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125}
2126
Joerg Roedelfb72d162008-02-07 13:47:44 +01002127static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2128 u32 error_code)
2129{
Anthony Liguori35149e22008-04-02 14:46:56 -05002130 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002131 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002132 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002133 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002134 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002135
2136 ASSERT(vcpu);
2137 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2138
2139 r = mmu_topup_memory_caches(vcpu);
2140 if (r)
2141 return r;
2142
Joerg Roedel852e3c12009-07-27 16:30:44 +02002143 level = mapping_level(vcpu, gfn);
2144
2145 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2146
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002147 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002148 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002149 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002150 if (is_error_pfn(pfn)) {
2151 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002152 return 1;
2153 }
2154 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002155 if (mmu_notifier_retry(vcpu, mmu_seq))
2156 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002157 kvm_mmu_free_some_pages(vcpu);
2158 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002159 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002160 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002161
2162 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002163
2164out_unlock:
2165 spin_unlock(&vcpu->kvm->mmu_lock);
2166 kvm_release_pfn_clean(pfn);
2167 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002168}
2169
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170static void nonpaging_free(struct kvm_vcpu *vcpu)
2171{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002172 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173}
2174
2175static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2176{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002177 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178
2179 context->new_cr3 = nonpaging_new_cr3;
2180 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 context->gva_to_gpa = nonpaging_gva_to_gpa;
2182 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002183 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002184 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002185 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002186 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002188 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 return 0;
2190}
2191
Avi Kivityd835dfe2007-11-21 02:57:59 +02002192void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002194 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002195 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196}
2197
2198static void paging_new_cr3(struct kvm_vcpu *vcpu)
2199{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002200 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002201 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202}
2203
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204static void inject_page_fault(struct kvm_vcpu *vcpu,
2205 u64 addr,
2206 u32 err_code)
2207{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002208 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209}
2210
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211static void paging_free(struct kvm_vcpu *vcpu)
2212{
2213 nonpaging_free(vcpu);
2214}
2215
Dong, Eddie82725b22009-03-30 16:21:08 +08002216static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2217{
2218 int bit7;
2219
2220 bit7 = (gpte >> 7) & 1;
2221 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2222}
2223
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224#define PTTYPE 64
2225#include "paging_tmpl.h"
2226#undef PTTYPE
2227
2228#define PTTYPE 32
2229#include "paging_tmpl.h"
2230#undef PTTYPE
2231
Dong, Eddie82725b22009-03-30 16:21:08 +08002232static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2233{
2234 struct kvm_mmu *context = &vcpu->arch.mmu;
2235 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2236 u64 exb_bit_rsvd = 0;
2237
2238 if (!is_nx(vcpu))
2239 exb_bit_rsvd = rsvd_bits(63, 63);
2240 switch (level) {
2241 case PT32_ROOT_LEVEL:
2242 /* no rsvd bits for 2 level 4K page table entries */
2243 context->rsvd_bits_mask[0][1] = 0;
2244 context->rsvd_bits_mask[0][0] = 0;
2245 if (is_cpuid_PSE36())
2246 /* 36bits PSE 4MB page */
2247 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2248 else
2249 /* 32 bits PSE 4MB page */
2250 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002251 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002252 break;
2253 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002254 context->rsvd_bits_mask[0][2] =
2255 rsvd_bits(maxphyaddr, 63) |
2256 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002257 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002258 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002259 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2260 rsvd_bits(maxphyaddr, 62); /* PTE */
2261 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2262 rsvd_bits(maxphyaddr, 62) |
2263 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002264 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002265 break;
2266 case PT64_ROOT_LEVEL:
2267 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2268 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2269 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2270 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2271 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002272 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002273 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2274 rsvd_bits(maxphyaddr, 51);
2275 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2276 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2277 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002278 rsvd_bits(maxphyaddr, 51) |
2279 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002280 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002281 break;
2282 }
2283}
2284
Avi Kivity17ac10a2007-01-05 16:36:40 -08002285static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002287 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288
2289 ASSERT(is_pae(vcpu));
2290 context->new_cr3 = paging_new_cr3;
2291 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002293 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002294 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002295 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002297 context->root_level = level;
2298 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002299 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 return 0;
2301}
2302
Avi Kivity17ac10a2007-01-05 16:36:40 -08002303static int paging64_init_context(struct kvm_vcpu *vcpu)
2304{
Dong, Eddie82725b22009-03-30 16:21:08 +08002305 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002306 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2307}
2308
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309static int paging32_init_context(struct kvm_vcpu *vcpu)
2310{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002311 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
Dong, Eddie82725b22009-03-30 16:21:08 +08002313 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 context->new_cr3 = paging_new_cr3;
2315 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 context->gva_to_gpa = paging32_gva_to_gpa;
2317 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002318 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002319 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002320 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 context->root_level = PT32_ROOT_LEVEL;
2322 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002323 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 return 0;
2325}
2326
2327static int paging32E_init_context(struct kvm_vcpu *vcpu)
2328{
Dong, Eddie82725b22009-03-30 16:21:08 +08002329 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002330 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331}
2332
Joerg Roedelfb72d162008-02-07 13:47:44 +01002333static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2334{
2335 struct kvm_mmu *context = &vcpu->arch.mmu;
2336
2337 context->new_cr3 = nonpaging_new_cr3;
2338 context->page_fault = tdp_page_fault;
2339 context->free = nonpaging_free;
2340 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002341 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002342 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002343 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002344 context->root_hpa = INVALID_PAGE;
2345
2346 if (!is_paging(vcpu)) {
2347 context->gva_to_gpa = nonpaging_gva_to_gpa;
2348 context->root_level = 0;
2349 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002350 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002351 context->gva_to_gpa = paging64_gva_to_gpa;
2352 context->root_level = PT64_ROOT_LEVEL;
2353 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002354 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002355 context->gva_to_gpa = paging64_gva_to_gpa;
2356 context->root_level = PT32E_ROOT_LEVEL;
2357 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002358 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002359 context->gva_to_gpa = paging32_gva_to_gpa;
2360 context->root_level = PT32_ROOT_LEVEL;
2361 }
2362
2363 return 0;
2364}
2365
2366static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002368 int r;
2369
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002371 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372
2373 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002374 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002375 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002376 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002378 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002380 r = paging32_init_context(vcpu);
2381
2382 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2383
2384 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385}
2386
Joerg Roedelfb72d162008-02-07 13:47:44 +01002387static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2388{
Anthony Liguori35149e22008-04-02 14:46:56 -05002389 vcpu->arch.update_pte.pfn = bad_pfn;
2390
Joerg Roedelfb72d162008-02-07 13:47:44 +01002391 if (tdp_enabled)
2392 return init_kvm_tdp_mmu(vcpu);
2393 else
2394 return init_kvm_softmmu(vcpu);
2395}
2396
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2398{
2399 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002400 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2401 vcpu->arch.mmu.free(vcpu);
2402 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 }
2404}
2405
2406int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2407{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002408 destroy_kvm_mmu(vcpu);
2409 return init_kvm_mmu(vcpu);
2410}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002411EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002412
2413int kvm_mmu_load(struct kvm_vcpu *vcpu)
2414{
Avi Kivity714b93d2007-01-05 16:36:53 -08002415 int r;
2416
Avi Kivitye2dec932007-01-05 16:36:54 -08002417 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002418 if (r)
2419 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002420 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002421 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002422 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002423 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002424 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002425 if (r)
2426 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002427 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002429out:
2430 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002432EXPORT_SYMBOL_GPL(kvm_mmu_load);
2433
2434void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2435{
2436 mmu_free_roots(vcpu);
2437}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438
Avi Kivity09072da2007-05-01 14:16:52 +03002439static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002440 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002441 u64 *spte)
2442{
2443 u64 pte;
2444 struct kvm_mmu_page *child;
2445
2446 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002447 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002448 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002449 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002450 else {
2451 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002452 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002453 }
2454 }
Avi Kivityd555c332009-06-10 14:24:23 +03002455 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002456 if (is_large_pte(pte))
2457 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002458}
2459
Avi Kivity00284252007-05-01 16:53:31 +03002460static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002461 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002462 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002463 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002464{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002465 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002466 if (vcpu->arch.update_pte.level == PT_PAGE_TABLE_LEVEL ||
Marcelo Tosatti30945382008-06-11 20:32:40 -03002467 sp->role.glevels == PT32_ROOT_LEVEL) {
2468 ++vcpu->kvm->stat.mmu_pde_zapped;
2469 return;
2470 }
2471 }
Avi Kivity00284252007-05-01 16:53:31 +03002472
Avi Kivity4cee5762007-11-18 16:37:07 +02002473 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002474 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002475 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002476 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002477 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002478}
2479
Avi Kivity79539ce2007-11-21 02:06:21 +02002480static bool need_remote_flush(u64 old, u64 new)
2481{
2482 if (!is_shadow_present_pte(old))
2483 return false;
2484 if (!is_shadow_present_pte(new))
2485 return true;
2486 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2487 return true;
2488 old ^= PT64_NX_MASK;
2489 new ^= PT64_NX_MASK;
2490 return (old & ~new & PT64_PERM_MASK) != 0;
2491}
2492
2493static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2494{
2495 if (need_remote_flush(old, new))
2496 kvm_flush_remote_tlbs(vcpu->kvm);
2497 else
2498 kvm_mmu_flush_tlb(vcpu);
2499}
2500
Avi Kivity12b7d282007-09-23 14:10:49 +02002501static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2502{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002503 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002504
Sheng Yang7b523452008-04-25 21:13:50 +08002505 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002506}
2507
Avi Kivityd7824ff2007-12-30 12:29:05 +02002508static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2509 const u8 *new, int bytes)
2510{
2511 gfn_t gfn;
2512 int r;
2513 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002514 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002515
Joerg Roedel852e3c12009-07-27 16:30:44 +02002516 vcpu->arch.update_pte.level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002517
Avi Kivityd7824ff2007-12-30 12:29:05 +02002518 if (bytes != 4 && bytes != 8)
2519 return;
2520
2521 /*
2522 * Assume that the pte write on a page table of the same type
2523 * as the current vcpu paging mode. This is nearly always true
2524 * (might be false while changing modes). Note it is verified later
2525 * by update_pte().
2526 */
2527 if (is_pae(vcpu)) {
2528 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2529 if ((bytes == 4) && (gpa % 4 == 0)) {
2530 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2531 if (r)
2532 return;
2533 memcpy((void *)&gpte + (gpa % 8), new, 4);
2534 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2535 memcpy((void *)&gpte, new, 8);
2536 }
2537 } else {
2538 if ((bytes == 4) && (gpa % 4 == 0))
2539 memcpy((void *)&gpte, new, 4);
2540 }
Avi Kivity43a37952009-06-10 14:12:05 +03002541 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002542 return;
2543 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002544
Joerg Roedeld25797b2009-07-27 16:30:43 +02002545 if (is_large_pte(gpte) &&
2546 (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL)) {
Joerg Roedelec04b262009-06-19 15:16:23 +02002547 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002548 vcpu->arch.update_pte.level = PT_DIRECTORY_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002549 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002550 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002551 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002552 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002553
Anthony Liguori35149e22008-04-02 14:46:56 -05002554 if (is_error_pfn(pfn)) {
2555 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002556 return;
2557 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002558 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002559 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002560}
2561
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002562static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2563{
2564 u64 *spte = vcpu->arch.last_pte_updated;
2565
2566 if (spte
2567 && vcpu->arch.last_pte_gfn == gfn
2568 && shadow_accessed_mask
2569 && !(*spte & shadow_accessed_mask)
2570 && is_shadow_present_pte(*spte))
2571 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2572}
2573
Avi Kivity09072da2007-05-01 14:16:52 +03002574void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002575 const u8 *new, int bytes,
2576 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002577{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002578 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002579 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002580 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002581 struct hlist_head *bucket;
2582 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002583 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002584 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002585 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002586 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002587 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002588 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002589 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002590 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002591 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002592 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002593 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002594
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002595 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002596 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002597 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002598 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002599 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002600 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002601 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002602 if (guest_initiated) {
2603 if (gfn == vcpu->arch.last_pt_write_gfn
2604 && !last_updated_pte_accessed(vcpu)) {
2605 ++vcpu->arch.last_pt_write_count;
2606 if (vcpu->arch.last_pt_write_count >= 3)
2607 flooded = 1;
2608 } else {
2609 vcpu->arch.last_pt_write_gfn = gfn;
2610 vcpu->arch.last_pt_write_count = 1;
2611 vcpu->arch.last_pte_updated = NULL;
2612 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002613 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002614 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002615 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002616 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002617 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002618 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002619 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002620 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002621 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002622 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002623 /*
2624 * Misaligned accesses are too much trouble to fix
2625 * up; also, they usually indicate a page is not used
2626 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002627 *
2628 * If we're seeing too many writes to a page,
2629 * it may no longer be a page table, or we may be
2630 * forking, in which case it is better to unmap the
2631 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002632 */
2633 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002634 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002635 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2636 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002637 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002638 continue;
2639 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002640 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002641 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002642 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002643 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002644 page_offset <<= 1; /* 32->64 */
2645 /*
2646 * A 32-bit pde maps 4MB while the shadow pdes map
2647 * only 2MB. So we need to double the offset again
2648 * and zap two pdes instead of one.
2649 */
2650 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002651 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002652 page_offset <<= 1;
2653 npte = 2;
2654 }
Avi Kivityfce06572007-05-01 16:44:05 +03002655 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002656 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002657 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002658 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002659 }
Avi Kivity4db35312007-11-21 15:28:32 +02002660 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002661 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2662 gentry = 0;
2663 r = kvm_read_guest_atomic(vcpu->kvm,
2664 gpa & ~(u64)(pte_size - 1),
2665 &gentry, pte_size);
2666 new = (const void *)&gentry;
2667 if (r < 0)
2668 new = NULL;
2669 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002670 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002671 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002672 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002673 if (new)
2674 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002675 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002676 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002677 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002678 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002679 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002680 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002681 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2682 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2683 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002684 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002685}
2686
Avi Kivitya4360362007-01-05 16:36:45 -08002687int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2688{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002689 gpa_t gpa;
2690 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002691
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002692 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002693
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002694 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002695 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002696 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002697 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002698}
Avi Kivity577bdc42008-07-19 08:57:05 +03002699EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002700
Avi Kivity22d95b12007-09-14 20:26:06 +03002701void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002702{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002703 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002704 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002705
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002706 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002707 struct kvm_mmu_page, link);
2708 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002709 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002710 }
2711}
Avi Kivityebeace82007-01-05 16:36:47 -08002712
Avi Kivity30677142007-10-28 18:48:59 +02002713int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2714{
2715 int r;
2716 enum emulation_result er;
2717
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002718 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002719 if (r < 0)
2720 goto out;
2721
2722 if (!r) {
2723 r = 1;
2724 goto out;
2725 }
2726
Avi Kivityb733bfb2007-10-28 18:52:05 +02002727 r = mmu_topup_memory_caches(vcpu);
2728 if (r)
2729 goto out;
2730
Avi Kivity30677142007-10-28 18:48:59 +02002731 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002732
2733 switch (er) {
2734 case EMULATE_DONE:
2735 return 1;
2736 case EMULATE_DO_MMIO:
2737 ++vcpu->stat.mmio_exits;
2738 return 0;
2739 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002740 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2741 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2742 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002743 default:
2744 BUG();
2745 }
2746out:
Avi Kivity30677142007-10-28 18:48:59 +02002747 return r;
2748}
2749EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2750
Marcelo Tosattia7052892008-09-23 13:18:35 -03002751void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2752{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002753 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002754 kvm_mmu_flush_tlb(vcpu);
2755 ++vcpu->stat.invlpg;
2756}
2757EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2758
Joerg Roedel18552672008-02-07 13:47:41 +01002759void kvm_enable_tdp(void)
2760{
2761 tdp_enabled = true;
2762}
2763EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2764
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002765void kvm_disable_tdp(void)
2766{
2767 tdp_enabled = false;
2768}
2769EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2770
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771static void free_mmu_pages(struct kvm_vcpu *vcpu)
2772{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002773 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774}
2775
2776static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2777{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002778 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 int i;
2780
2781 ASSERT(vcpu);
2782
Marcelo Tosatti6a1ac772009-07-15 15:34:41 -03002783 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002784 if (vcpu->kvm->arch.n_requested_mmu_pages)
2785 vcpu->kvm->arch.n_free_mmu_pages =
2786 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002787 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002788 vcpu->kvm->arch.n_free_mmu_pages =
2789 vcpu->kvm->arch.n_alloc_mmu_pages;
Marcelo Tosatti6a1ac772009-07-15 15:34:41 -03002790 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002791 /*
2792 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2793 * Therefore we need to allocate shadow page tables in the first
2794 * 4GB of memory, which happens to fit the DMA32 zone.
2795 */
2796 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2797 if (!page)
2798 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002799 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002800 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002801 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002802
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 return 0;
2804
2805error_1:
2806 free_mmu_pages(vcpu);
2807 return -ENOMEM;
2808}
2809
Ingo Molnar8018c272006-12-29 16:50:01 -08002810int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002813 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814
Ingo Molnar8018c272006-12-29 16:50:01 -08002815 return alloc_mmu_pages(vcpu);
2816}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817
Ingo Molnar8018c272006-12-29 16:50:01 -08002818int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2819{
2820 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002821 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002822
Ingo Molnar8018c272006-12-29 16:50:01 -08002823 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824}
2825
2826void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2827{
2828 ASSERT(vcpu);
2829
2830 destroy_kvm_mmu(vcpu);
2831 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002832 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833}
2834
Avi Kivity90cb0522007-07-17 13:04:56 +03002835void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836{
Avi Kivity4db35312007-11-21 15:28:32 +02002837 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002839 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 int i;
2841 u64 *pt;
2842
Sheng Yang291f26b2008-10-16 17:30:57 +08002843 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 continue;
2845
Avi Kivity4db35312007-11-21 15:28:32 +02002846 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2848 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002849 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 }
Avi Kivity171d5952008-08-27 16:40:51 +03002852 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002854
Avi Kivity90cb0522007-07-17 13:04:56 +03002855void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002856{
Avi Kivity4db35312007-11-21 15:28:32 +02002857 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002858
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002859 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002860 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002861 if (kvm_mmu_zap_page(kvm, sp))
2862 node = container_of(kvm->arch.active_mmu_pages.next,
2863 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002864 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002865
Avi Kivity90cb0522007-07-17 13:04:56 +03002866 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002867}
2868
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002869static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002870{
2871 struct kvm_mmu_page *page;
2872
2873 page = container_of(kvm->arch.active_mmu_pages.prev,
2874 struct kvm_mmu_page, link);
2875 kvm_mmu_zap_page(kvm, page);
2876}
2877
2878static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2879{
2880 struct kvm *kvm;
2881 struct kvm *kvm_freed = NULL;
2882 int cache_count = 0;
2883
2884 spin_lock(&kvm_lock);
2885
2886 list_for_each_entry(kvm, &vm_list, vm_list) {
2887 int npages;
2888
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002889 if (!down_read_trylock(&kvm->slots_lock))
2890 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002891 spin_lock(&kvm->mmu_lock);
2892 npages = kvm->arch.n_alloc_mmu_pages -
2893 kvm->arch.n_free_mmu_pages;
2894 cache_count += npages;
2895 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2896 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2897 cache_count--;
2898 kvm_freed = kvm;
2899 }
2900 nr_to_scan--;
2901
2902 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002903 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002904 }
2905 if (kvm_freed)
2906 list_move_tail(&kvm_freed->vm_list, &vm_list);
2907
2908 spin_unlock(&kvm_lock);
2909
2910 return cache_count;
2911}
2912
2913static struct shrinker mmu_shrinker = {
2914 .shrink = mmu_shrink,
2915 .seeks = DEFAULT_SEEKS * 10,
2916};
2917
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002918static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002919{
2920 if (pte_chain_cache)
2921 kmem_cache_destroy(pte_chain_cache);
2922 if (rmap_desc_cache)
2923 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002924 if (mmu_page_header_cache)
2925 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002926}
2927
Izik Eidus3ee16c82008-03-30 15:17:21 +03002928void kvm_mmu_module_exit(void)
2929{
2930 mmu_destroy_caches();
2931 unregister_shrinker(&mmu_shrinker);
2932}
2933
Avi Kivityb5a33a72007-04-15 16:31:09 +03002934int kvm_mmu_module_init(void)
2935{
2936 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2937 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002938 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002939 if (!pte_chain_cache)
2940 goto nomem;
2941 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2942 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002943 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002944 if (!rmap_desc_cache)
2945 goto nomem;
2946
Avi Kivityd3d25b02007-05-30 12:34:53 +03002947 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2948 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002949 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002950 if (!mmu_page_header_cache)
2951 goto nomem;
2952
Izik Eidus3ee16c82008-03-30 15:17:21 +03002953 register_shrinker(&mmu_shrinker);
2954
Avi Kivityb5a33a72007-04-15 16:31:09 +03002955 return 0;
2956
2957nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002958 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002959 return -ENOMEM;
2960}
2961
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002962/*
2963 * Caculate mmu pages needed for kvm.
2964 */
2965unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2966{
2967 int i;
2968 unsigned int nr_mmu_pages;
2969 unsigned int nr_pages = 0;
2970
2971 for (i = 0; i < kvm->nmemslots; i++)
2972 nr_pages += kvm->memslots[i].npages;
2973
2974 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2975 nr_mmu_pages = max(nr_mmu_pages,
2976 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2977
2978 return nr_mmu_pages;
2979}
2980
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002981static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2982 unsigned len)
2983{
2984 if (len > buffer->len)
2985 return NULL;
2986 return buffer->ptr;
2987}
2988
2989static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2990 unsigned len)
2991{
2992 void *ret;
2993
2994 ret = pv_mmu_peek_buffer(buffer, len);
2995 if (!ret)
2996 return ret;
2997 buffer->ptr += len;
2998 buffer->len -= len;
2999 buffer->processed += len;
3000 return ret;
3001}
3002
3003static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3004 gpa_t addr, gpa_t value)
3005{
3006 int bytes = 8;
3007 int r;
3008
3009 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3010 bytes = 4;
3011
3012 r = mmu_topup_memory_caches(vcpu);
3013 if (r)
3014 return r;
3015
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003016 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003017 return -EFAULT;
3018
3019 return 1;
3020}
3021
3022static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3023{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003024 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003025 return 1;
3026}
3027
3028static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3029{
3030 spin_lock(&vcpu->kvm->mmu_lock);
3031 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3032 spin_unlock(&vcpu->kvm->mmu_lock);
3033 return 1;
3034}
3035
3036static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3037 struct kvm_pv_mmu_op_buffer *buffer)
3038{
3039 struct kvm_mmu_op_header *header;
3040
3041 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3042 if (!header)
3043 return 0;
3044 switch (header->op) {
3045 case KVM_MMU_OP_WRITE_PTE: {
3046 struct kvm_mmu_op_write_pte *wpte;
3047
3048 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3049 if (!wpte)
3050 return 0;
3051 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3052 wpte->pte_val);
3053 }
3054 case KVM_MMU_OP_FLUSH_TLB: {
3055 struct kvm_mmu_op_flush_tlb *ftlb;
3056
3057 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3058 if (!ftlb)
3059 return 0;
3060 return kvm_pv_mmu_flush_tlb(vcpu);
3061 }
3062 case KVM_MMU_OP_RELEASE_PT: {
3063 struct kvm_mmu_op_release_pt *rpt;
3064
3065 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3066 if (!rpt)
3067 return 0;
3068 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3069 }
3070 default: return 0;
3071 }
3072}
3073
3074int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3075 gpa_t addr, unsigned long *ret)
3076{
3077 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003078 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003079
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003080 buffer->ptr = buffer->buf;
3081 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3082 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003083
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003084 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003085 if (r)
3086 goto out;
3087
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003088 while (buffer->len) {
3089 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003090 if (r < 0)
3091 goto out;
3092 if (r == 0)
3093 break;
3094 }
3095
3096 r = 1;
3097out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003098 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003099 return r;
3100}
3101
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003102int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3103{
3104 struct kvm_shadow_walk_iterator iterator;
3105 int nr_sptes = 0;
3106
3107 spin_lock(&vcpu->kvm->mmu_lock);
3108 for_each_shadow_entry(vcpu, addr, iterator) {
3109 sptes[iterator.level-1] = *iterator.sptep;
3110 nr_sptes++;
3111 if (!is_shadow_present_pte(*iterator.sptep))
3112 break;
3113 }
3114 spin_unlock(&vcpu->kvm->mmu_lock);
3115
3116 return nr_sptes;
3117}
3118EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3119
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003120#ifdef AUDIT
3121
3122static const char *audit_msg;
3123
3124static gva_t canonicalize(gva_t gva)
3125{
3126#ifdef CONFIG_X86_64
3127 gva = (long long)(gva << 16) >> 16;
3128#endif
3129 return gva;
3130}
3131
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003132
3133typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3134 u64 *sptep);
3135
3136static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3137 inspect_spte_fn fn)
3138{
3139 int i;
3140
3141 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3142 u64 ent = sp->spt[i];
3143
3144 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003145 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003146 struct kvm_mmu_page *child;
3147 child = page_header(ent & PT64_BASE_ADDR_MASK);
3148 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003149 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003150 fn(kvm, sp, &sp->spt[i]);
3151 }
3152 }
3153}
3154
3155static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3156{
3157 int i;
3158 struct kvm_mmu_page *sp;
3159
3160 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3161 return;
3162 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3163 hpa_t root = vcpu->arch.mmu.root_hpa;
3164 sp = page_header(root);
3165 __mmu_spte_walk(vcpu->kvm, sp, fn);
3166 return;
3167 }
3168 for (i = 0; i < 4; ++i) {
3169 hpa_t root = vcpu->arch.mmu.pae_root[i];
3170
3171 if (root && VALID_PAGE(root)) {
3172 root &= PT64_BASE_ADDR_MASK;
3173 sp = page_header(root);
3174 __mmu_spte_walk(vcpu->kvm, sp, fn);
3175 }
3176 }
3177 return;
3178}
3179
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003180static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3181 gva_t va, int level)
3182{
3183 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3184 int i;
3185 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3186
3187 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3188 u64 ent = pt[i];
3189
Avi Kivityc7addb92007-09-16 18:58:32 +02003190 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003191 continue;
3192
3193 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003194 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3195 audit_mappings_page(vcpu, ent, va, level - 1);
3196 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003197 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003198 gfn_t gfn = gpa >> PAGE_SHIFT;
3199 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3200 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003201
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003202 if (is_error_pfn(pfn)) {
3203 kvm_release_pfn_clean(pfn);
3204 continue;
3205 }
3206
Avi Kivityc7addb92007-09-16 18:58:32 +02003207 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003208 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003209 printk(KERN_ERR "xx audit error: (%s) levels %d"
3210 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003211 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003212 va, gpa, hpa, ent,
3213 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003214 else if (ent == shadow_notrap_nonpresent_pte
3215 && !is_error_hpa(hpa))
3216 printk(KERN_ERR "audit: (%s) notrap shadow,"
3217 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003218 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003219
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003220 }
3221 }
3222}
3223
3224static void audit_mappings(struct kvm_vcpu *vcpu)
3225{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003226 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003227
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003228 if (vcpu->arch.mmu.root_level == 4)
3229 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003230 else
3231 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003232 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003233 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003234 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003235 i << 30,
3236 2);
3237}
3238
3239static int count_rmaps(struct kvm_vcpu *vcpu)
3240{
3241 int nmaps = 0;
3242 int i, j, k;
3243
3244 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3245 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3246 struct kvm_rmap_desc *d;
3247
3248 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003249 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003250
Izik Eidus290fc382007-09-27 14:11:22 +02003251 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003252 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003253 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003254 ++nmaps;
3255 continue;
3256 }
Izik Eidus290fc382007-09-27 14:11:22 +02003257 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003258 while (d) {
3259 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003260 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003261 ++nmaps;
3262 else
3263 break;
3264 d = d->more;
3265 }
3266 }
3267 }
3268 return nmaps;
3269}
3270
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003271void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003272{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003273 unsigned long *rmapp;
3274 struct kvm_mmu_page *rev_sp;
3275 gfn_t gfn;
3276
3277 if (*sptep & PT_WRITABLE_MASK) {
3278 rev_sp = page_header(__pa(sptep));
3279 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3280
3281 if (!gfn_to_memslot(kvm, gfn)) {
3282 if (!printk_ratelimit())
3283 return;
3284 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3285 audit_msg, gfn);
3286 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3287 audit_msg, sptep - rev_sp->spt,
3288 rev_sp->gfn);
3289 dump_stack();
3290 return;
3291 }
3292
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003293 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3294 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003295 if (!*rmapp) {
3296 if (!printk_ratelimit())
3297 return;
3298 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3299 audit_msg, *sptep);
3300 dump_stack();
3301 }
3302 }
3303
3304}
3305
3306void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3307{
3308 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3309}
3310
3311static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3312{
Avi Kivity4db35312007-11-21 15:28:32 +02003313 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003314 int i;
3315
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003316 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003317 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003318
Avi Kivity4db35312007-11-21 15:28:32 +02003319 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003320 continue;
3321
3322 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3323 u64 ent = pt[i];
3324
3325 if (!(ent & PT_PRESENT_MASK))
3326 continue;
3327 if (!(ent & PT_WRITABLE_MASK))
3328 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003329 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003330 }
3331 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003332 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003333}
3334
3335static void audit_rmap(struct kvm_vcpu *vcpu)
3336{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003337 check_writable_mappings_rmap(vcpu);
3338 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003339}
3340
3341static void audit_write_protection(struct kvm_vcpu *vcpu)
3342{
Avi Kivity4db35312007-11-21 15:28:32 +02003343 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003344 struct kvm_memory_slot *slot;
3345 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003346 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003347 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003348
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003349 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003350 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003351 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003352 if (sp->unsync)
3353 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003354
Avi Kivity4db35312007-11-21 15:28:32 +02003355 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003356 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003357 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003358
3359 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3360 while (spte) {
3361 if (*spte & PT_WRITABLE_MASK)
3362 printk(KERN_ERR "%s: (%s) shadow page has "
3363 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003364 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003365 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003366 spte = rmap_next(vcpu->kvm, rmapp, spte);
3367 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003368 }
3369}
3370
3371static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3372{
3373 int olddbg = dbg;
3374
3375 dbg = 0;
3376 audit_msg = msg;
3377 audit_rmap(vcpu);
3378 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003379 if (strcmp("pre pte write", audit_msg) != 0)
3380 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003381 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003382 dbg = olddbg;
3383}
3384
3385#endif