blob: ef060ec444a46d6ccd1f7e8f5aac66b225f7b9d7 [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 Kivity6aa8b732006-12-10 02:21:36 -080021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020028#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030029#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050030#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
32#include <asm/page.h>
33#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020034#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020035#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030069static int dbg = 0;
70module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080071#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
Marcelo Tosatti582801a2008-09-23 13:18:41 -030073static int oos_shadow = 1;
74module_param(oos_shadow, bool, 0644);
75
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080076#ifndef MMU_DEBUG
77#define ASSERT(x) do { } while (0)
78#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080079#define ASSERT(x) \
80 if (!(x)) { \
81 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
82 __FILE__, __LINE__, #x); \
83 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080084#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
Avi Kivity6aa8b732006-12-10 02:21:36 -080086#define PT_FIRST_AVAIL_BITS_SHIFT 9
87#define PT64_SECOND_AVAIL_BITS_SHIFT 52
88
Avi Kivity6aa8b732006-12-10 02:21:36 -080089#define VALID_PAGE(x) ((x) != INVALID_PAGE)
90
91#define PT64_LEVEL_BITS 9
92
93#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040094 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
96#define PT64_LEVEL_MASK(level) \
97 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
98
99#define PT64_INDEX(address, level)\
100 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
101
102
103#define PT32_LEVEL_BITS 10
104
105#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400106 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107
108#define PT32_LEVEL_MASK(level) \
109 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
110
111#define PT32_INDEX(address, level)\
112 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
113
114
Avi Kivity27aba762007-03-09 13:04:31 +0200115#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116#define PT64_DIR_BASE_ADDR_MASK \
117 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
118
119#define PT32_BASE_ADDR_MASK PAGE_MASK
120#define PT32_DIR_BASE_ADDR_MASK \
121 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
122
Avi Kivity79539ce2007-11-21 02:06:21 +0200123#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
124 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125
126#define PFERR_PRESENT_MASK (1U << 0)
127#define PFERR_WRITE_MASK (1U << 1)
128#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800129#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define PT_DIRECTORY_LEVEL 2
132#define PT_PAGE_TABLE_LEVEL 1
133
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800134#define RMAP_EXT 4
135
Avi Kivityfe135d22007-12-09 16:15:46 +0200136#define ACC_EXEC_MASK 1
137#define ACC_WRITE_MASK PT_WRITABLE_MASK
138#define ACC_USER_MASK PT_USER_MASK
139#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
140
Avi Kivity135f8c22008-08-21 17:49:56 +0300141#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
142
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800143struct kvm_rmap_desc {
144 u64 *shadow_ptes[RMAP_EXT];
145 struct kvm_rmap_desc *more;
146};
147
Avi Kivity2d111232008-12-25 14:39:47 +0200148struct kvm_shadow_walk_iterator {
149 u64 addr;
150 hpa_t shadow_addr;
151 int level;
152 u64 *sptep;
153 unsigned index;
154};
155
156#define for_each_shadow_entry(_vcpu, _addr, _walker) \
157 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
158 shadow_walk_okay(&(_walker)); \
159 shadow_walk_next(&(_walker)))
160
161
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300162struct kvm_unsync_walk {
163 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
164};
165
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300166typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
167
Avi Kivityb5a33a72007-04-15 16:31:09 +0300168static struct kmem_cache *pte_chain_cache;
169static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300170static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300171
Avi Kivityc7addb92007-09-16 18:58:32 +0200172static u64 __read_mostly shadow_trap_nonpresent_pte;
173static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800174static u64 __read_mostly shadow_base_present_pte;
175static u64 __read_mostly shadow_nx_mask;
176static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
177static u64 __read_mostly shadow_user_mask;
178static u64 __read_mostly shadow_accessed_mask;
179static u64 __read_mostly shadow_dirty_mask;
Sheng Yang64d4d522008-10-09 16:01:57 +0800180static u64 __read_mostly shadow_mt_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200181
182void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
183{
184 shadow_trap_nonpresent_pte = trap_pte;
185 shadow_notrap_nonpresent_pte = notrap_pte;
186}
187EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
188
Sheng Yang7b523452008-04-25 21:13:50 +0800189void kvm_mmu_set_base_ptes(u64 base_pte)
190{
191 shadow_base_present_pte = base_pte;
192}
193EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
194
195void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang64d4d522008-10-09 16:01:57 +0800196 u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800197{
198 shadow_user_mask = user_mask;
199 shadow_accessed_mask = accessed_mask;
200 shadow_dirty_mask = dirty_mask;
201 shadow_nx_mask = nx_mask;
202 shadow_x_mask = x_mask;
Sheng Yang64d4d522008-10-09 16:01:57 +0800203 shadow_mt_mask = mt_mask;
Sheng Yang7b523452008-04-25 21:13:50 +0800204}
205EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
206
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207static int is_write_protection(struct kvm_vcpu *vcpu)
208{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800209 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210}
211
212static int is_cpuid_PSE36(void)
213{
214 return 1;
215}
216
Avi Kivity73b10872007-01-26 00:56:41 -0800217static int is_nx(struct kvm_vcpu *vcpu)
218{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800219 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800220}
221
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222static int is_present_pte(unsigned long pte)
223{
224 return pte & PT_PRESENT_MASK;
225}
226
Avi Kivityc7addb92007-09-16 18:58:32 +0200227static int is_shadow_present_pte(u64 pte)
228{
Avi Kivityc7addb92007-09-16 18:58:32 +0200229 return pte != shadow_trap_nonpresent_pte
230 && pte != shadow_notrap_nonpresent_pte;
231}
232
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300233static int is_large_pte(u64 pte)
234{
235 return pte & PT_PAGE_SIZE_MASK;
236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static int is_writeble_pte(unsigned long pte)
239{
240 return pte & PT_WRITABLE_MASK;
241}
242
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200243static int is_dirty_pte(unsigned long pte)
244{
Sheng Yang7b523452008-04-25 21:13:50 +0800245 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200246}
247
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800248static int is_rmap_pte(u64 pte)
249{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200250 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800251}
252
Anthony Liguori35149e22008-04-02 14:46:56 -0500253static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200254{
Anthony Liguori35149e22008-04-02 14:46:56 -0500255 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200256}
257
Avi Kivityda928522007-11-21 13:54:47 +0200258static gfn_t pse36_gfn_delta(u32 gpte)
259{
260 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
261
262 return (gpte & PT32_DIR_PSE36_MASK) << shift;
263}
264
Avi Kivitye663ee62007-05-31 15:46:04 +0300265static void set_shadow_pte(u64 *sptep, u64 spte)
266{
267#ifdef CONFIG_X86_64
268 set_64bit((unsigned long *)sptep, spte);
269#else
270 set_64bit((unsigned long long *)sptep, spte);
271#endif
272}
273
Avi Kivitye2dec932007-01-05 16:36:54 -0800274static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300275 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800276{
277 void *obj;
278
279 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800280 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800281 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300282 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800283 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800284 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800285 cache->objects[cache->nobjs++] = obj;
286 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800287 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800288}
289
290static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
291{
292 while (mc->nobjs)
293 kfree(mc->objects[--mc->nobjs]);
294}
295
Avi Kivityc1158e62007-07-20 08:18:27 +0300296static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300297 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300298{
299 struct page *page;
300
301 if (cache->nobjs >= min)
302 return 0;
303 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300304 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300305 if (!page)
306 return -ENOMEM;
307 set_page_private(page, 0);
308 cache->objects[cache->nobjs++] = page_address(page);
309 }
310 return 0;
311}
312
313static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
314{
315 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300316 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300317}
318
Avi Kivity8c438502007-04-16 11:53:17 +0300319static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
320{
321 int r;
322
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800323 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 pte_chain_cache, 4);
325 if (r)
326 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800327 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200328 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300329 if (r)
330 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800331 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300332 if (r)
333 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800334 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300335 mmu_page_header_cache, 4);
336out:
Avi Kivity8c438502007-04-16 11:53:17 +0300337 return r;
338}
339
Avi Kivity714b93d2007-01-05 16:36:53 -0800340static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
341{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
343 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
344 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
345 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800346}
347
348static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
349 size_t size)
350{
351 void *p;
352
353 BUG_ON(!mc->nobjs);
354 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800355 return p;
356}
357
Avi Kivity714b93d2007-01-05 16:36:53 -0800358static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
359{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800360 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800361 sizeof(struct kvm_pte_chain));
362}
363
Avi Kivity90cb0522007-07-17 13:04:56 +0300364static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800365{
Avi Kivity90cb0522007-07-17 13:04:56 +0300366 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800367}
368
369static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
370{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800371 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800372 sizeof(struct kvm_rmap_desc));
373}
374
Avi Kivity90cb0522007-07-17 13:04:56 +0300375static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800376{
Avi Kivity90cb0522007-07-17 13:04:56 +0300377 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800378}
379
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800380/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300381 * Return the pointer to the largepage write count for a given
382 * gfn, handling slots that are not large page aligned.
383 */
384static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
385{
386 unsigned long idx;
387
388 idx = (gfn / KVM_PAGES_PER_HPAGE) -
389 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
390 return &slot->lpage_info[idx].write_count;
391}
392
393static void account_shadowed(struct kvm *kvm, gfn_t gfn)
394{
395 int *write_count;
396
Izik Eidus28430992008-10-03 17:40:32 +0300397 gfn = unalias_gfn(kvm, gfn);
398 write_count = slot_largepage_idx(gfn,
399 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300400 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300401}
402
403static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
404{
405 int *write_count;
406
Izik Eidus28430992008-10-03 17:40:32 +0300407 gfn = unalias_gfn(kvm, gfn);
408 write_count = slot_largepage_idx(gfn,
409 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300410 *write_count -= 1;
411 WARN_ON(*write_count < 0);
412}
413
414static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
415{
Izik Eidus28430992008-10-03 17:40:32 +0300416 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300417 int *largepage_idx;
418
Izik Eidus28430992008-10-03 17:40:32 +0300419 gfn = unalias_gfn(kvm, gfn);
420 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421 if (slot) {
422 largepage_idx = slot_largepage_idx(gfn, slot);
423 return *largepage_idx;
424 }
425
426 return 1;
427}
428
429static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
430{
431 struct vm_area_struct *vma;
432 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300433 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434
435 addr = gfn_to_hva(kvm, gfn);
436 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300437 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300438
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300439 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300440 vma = find_vma(current->mm, addr);
441 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300442 ret = 1;
443 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300444
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300445 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300446}
447
448static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
449{
450 struct kvm_memory_slot *slot;
451
452 if (has_wrprotected_page(vcpu->kvm, large_gfn))
453 return 0;
454
455 if (!host_largepage_backed(vcpu->kvm, large_gfn))
456 return 0;
457
458 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
459 if (slot && slot->dirty_bitmap)
460 return 0;
461
462 return 1;
463}
464
465/*
Izik Eidus290fc382007-09-27 14:11:22 +0200466 * Take gfn and return the reverse mapping to it.
467 * Note: gfn must be unaliased before this function get called
468 */
469
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200471{
472 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300473 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200474
475 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476 if (!lpage)
477 return &slot->rmap[gfn - slot->base_gfn];
478
479 idx = (gfn / KVM_PAGES_PER_HPAGE) -
480 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
481
482 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200483}
484
485/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 * Reverse mapping data structures:
487 *
Izik Eidus290fc382007-09-27 14:11:22 +0200488 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
489 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800490 *
Izik Eidus290fc382007-09-27 14:11:22 +0200491 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
492 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800493 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300494static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800495{
Avi Kivity4db35312007-11-21 15:28:32 +0200496 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800497 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200498 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800499 int i;
500
501 if (!is_rmap_pte(*spte))
502 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200503 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200504 sp = page_header(__pa(spte));
505 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200507 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800508 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200509 *rmapp = (unsigned long)spte;
510 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800511 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800512 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200513 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800514 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200515 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800516 } else {
517 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200518 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800519 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
520 desc = desc->more;
521 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800522 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800523 desc = desc->more;
524 }
525 for (i = 0; desc->shadow_ptes[i]; ++i)
526 ;
527 desc->shadow_ptes[i] = spte;
528 }
529}
530
Izik Eidus290fc382007-09-27 14:11:22 +0200531static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800532 struct kvm_rmap_desc *desc,
533 int i,
534 struct kvm_rmap_desc *prev_desc)
535{
536 int j;
537
538 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
539 ;
540 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000541 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800542 if (j != 0)
543 return;
544 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200545 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546 else
547 if (prev_desc)
548 prev_desc->more = desc->more;
549 else
Izik Eidus290fc382007-09-27 14:11:22 +0200550 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300551 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552}
553
Izik Eidus290fc382007-09-27 14:11:22 +0200554static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556 struct kvm_rmap_desc *desc;
557 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200558 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500559 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200560 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 int i;
562
563 if (!is_rmap_pte(*spte))
564 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200565 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500566 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800567 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500568 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200569 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500570 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200571 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500572 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300573 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200574 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
576 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200577 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800578 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200579 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
581 spte, *spte);
582 BUG();
583 }
Izik Eidus290fc382007-09-27 14:11:22 +0200584 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800585 } else {
586 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200587 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800588 prev_desc = NULL;
589 while (desc) {
590 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
591 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200592 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800593 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 prev_desc);
595 return;
596 }
597 prev_desc = desc;
598 desc = desc->more;
599 }
600 BUG();
601 }
602}
603
Izik Eidus98348e92007-10-16 14:42:30 +0200604static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800605{
Avi Kivity374cbac2007-01-05 16:36:43 -0800606 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200607 struct kvm_rmap_desc *prev_desc;
608 u64 *prev_spte;
609 int i;
610
611 if (!*rmapp)
612 return NULL;
613 else if (!(*rmapp & 1)) {
614 if (!spte)
615 return (u64 *)*rmapp;
616 return NULL;
617 }
618 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
619 prev_desc = NULL;
620 prev_spte = NULL;
621 while (desc) {
622 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
623 if (prev_spte == spte)
624 return desc->shadow_ptes[i];
625 prev_spte = desc->shadow_ptes[i];
626 }
627 desc = desc->more;
628 }
629 return NULL;
630}
631
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200632static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200633{
Izik Eidus290fc382007-09-27 14:11:22 +0200634 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800635 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800636 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800637
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500638 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300639 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800640
Izik Eidus98348e92007-10-16 14:42:30 +0200641 spte = rmap_next(kvm, rmapp, NULL);
642 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800643 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800644 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800645 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800646 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200647 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800648 write_protected = 1;
649 }
Izik Eidus9647c142007-10-16 14:43:46 +0200650 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800651 }
Izik Eidus855149a2008-03-20 18:17:24 +0200652 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500653 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200654
655 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500656 pfn = spte_to_pfn(*spte);
657 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200658 }
659
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300660 /* check for huge page mappings */
661 rmapp = gfn_to_rmap(kvm, gfn, 1);
662 spte = rmap_next(kvm, rmapp, NULL);
663 while (spte) {
664 BUG_ON(!spte);
665 BUG_ON(!(*spte & PT_PRESENT_MASK));
666 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
667 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
668 if (is_writeble_pte(*spte)) {
669 rmap_remove(kvm, spte);
670 --kvm->stat.lpages;
671 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300672 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300673 write_protected = 1;
674 }
675 spte = rmap_next(kvm, rmapp, spte);
676 }
677
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200678 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800679}
680
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200681static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
682{
683 u64 *spte;
684 int need_tlb_flush = 0;
685
686 while ((spte = rmap_next(kvm, rmapp, NULL))) {
687 BUG_ON(!(*spte & PT_PRESENT_MASK));
688 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
689 rmap_remove(kvm, spte);
690 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
691 need_tlb_flush = 1;
692 }
693 return need_tlb_flush;
694}
695
696static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
697 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
698{
699 int i;
700 int retval = 0;
701
702 /*
703 * If mmap_sem isn't taken, we can look the memslots with only
704 * the mmu_lock by skipping over the slots with userspace_addr == 0.
705 */
706 for (i = 0; i < kvm->nmemslots; i++) {
707 struct kvm_memory_slot *memslot = &kvm->memslots[i];
708 unsigned long start = memslot->userspace_addr;
709 unsigned long end;
710
711 /* mmu_lock protects userspace_addr */
712 if (!start)
713 continue;
714
715 end = start + (memslot->npages << PAGE_SHIFT);
716 if (hva >= start && hva < end) {
717 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
718 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
719 retval |= handler(kvm,
720 &memslot->lpage_info[
721 gfn_offset /
722 KVM_PAGES_PER_HPAGE].rmap_pde);
723 }
724 }
725
726 return retval;
727}
728
729int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
730{
731 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
732}
733
734static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
735{
736 u64 *spte;
737 int young = 0;
738
Sheng Yang534e38b2008-09-08 15:12:30 +0800739 /* always return old for EPT */
740 if (!shadow_accessed_mask)
741 return 0;
742
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200743 spte = rmap_next(kvm, rmapp, NULL);
744 while (spte) {
745 int _young;
746 u64 _spte = *spte;
747 BUG_ON(!(_spte & PT_PRESENT_MASK));
748 _young = _spte & PT_ACCESSED_MASK;
749 if (_young) {
750 young = 1;
751 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
752 }
753 spte = rmap_next(kvm, rmapp, spte);
754 }
755 return young;
756}
757
758int kvm_age_hva(struct kvm *kvm, unsigned long hva)
759{
760 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
761}
762
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800763#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300764static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765{
Avi Kivity139bdb22007-01-05 16:36:50 -0800766 u64 *pos;
767 u64 *end;
768
Avi Kivity47ad8e62007-05-06 15:50:58 +0300769 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300770 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800771 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800772 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800774 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 return 1;
776}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800777#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778
Avi Kivity4db35312007-11-21 15:28:32 +0200779static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800780{
Avi Kivity4db35312007-11-21 15:28:32 +0200781 ASSERT(is_empty_shadow_page(sp->spt));
782 list_del(&sp->link);
783 __free_page(virt_to_page(sp->spt));
784 __free_page(virt_to_page(sp->gfns));
785 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800786 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800787}
788
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800789static unsigned kvm_page_table_hashfn(gfn_t gfn)
790{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200791 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800792}
793
Avi Kivity25c0de22007-01-05 16:36:42 -0800794static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
795 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796{
Avi Kivity4db35312007-11-21 15:28:32 +0200797 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800799 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
800 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
801 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200802 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800803 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200804 INIT_LIST_HEAD(&sp->oos_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200805 ASSERT(is_empty_shadow_page(sp->spt));
Sheng Yang291f26b2008-10-16 17:30:57 +0800806 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200807 sp->multimapped = 0;
808 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800809 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200810 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811}
812
Avi Kivity714b93d2007-01-05 16:36:53 -0800813static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200814 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800815{
816 struct kvm_pte_chain *pte_chain;
817 struct hlist_node *node;
818 int i;
819
820 if (!parent_pte)
821 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200822 if (!sp->multimapped) {
823 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800824
825 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200826 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800827 return;
828 }
Avi Kivity4db35312007-11-21 15:28:32 +0200829 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800830 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200831 INIT_HLIST_HEAD(&sp->parent_ptes);
832 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800833 pte_chain->parent_ptes[0] = old;
834 }
Avi Kivity4db35312007-11-21 15:28:32 +0200835 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800836 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
837 continue;
838 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
839 if (!pte_chain->parent_ptes[i]) {
840 pte_chain->parent_ptes[i] = parent_pte;
841 return;
842 }
843 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800844 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800845 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200846 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800847 pte_chain->parent_ptes[0] = parent_pte;
848}
849
Avi Kivity4db35312007-11-21 15:28:32 +0200850static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800851 u64 *parent_pte)
852{
853 struct kvm_pte_chain *pte_chain;
854 struct hlist_node *node;
855 int i;
856
Avi Kivity4db35312007-11-21 15:28:32 +0200857 if (!sp->multimapped) {
858 BUG_ON(sp->parent_pte != parent_pte);
859 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800860 return;
861 }
Avi Kivity4db35312007-11-21 15:28:32 +0200862 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800863 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
864 if (!pte_chain->parent_ptes[i])
865 break;
866 if (pte_chain->parent_ptes[i] != parent_pte)
867 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800868 while (i + 1 < NR_PTE_CHAIN_ENTRIES
869 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800870 pte_chain->parent_ptes[i]
871 = pte_chain->parent_ptes[i + 1];
872 ++i;
873 }
874 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800875 if (i == 0) {
876 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300877 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200878 if (hlist_empty(&sp->parent_ptes)) {
879 sp->multimapped = 0;
880 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800881 }
882 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800883 return;
884 }
885 BUG();
886}
887
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300888
889static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
890 mmu_parent_walk_fn fn)
891{
892 struct kvm_pte_chain *pte_chain;
893 struct hlist_node *node;
894 struct kvm_mmu_page *parent_sp;
895 int i;
896
897 if (!sp->multimapped && sp->parent_pte) {
898 parent_sp = page_header(__pa(sp->parent_pte));
899 fn(vcpu, parent_sp);
900 mmu_parent_walk(vcpu, parent_sp, fn);
901 return;
902 }
903 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
904 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
905 if (!pte_chain->parent_ptes[i])
906 break;
907 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
908 fn(vcpu, parent_sp);
909 mmu_parent_walk(vcpu, parent_sp, fn);
910 }
911}
912
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300913static void kvm_mmu_update_unsync_bitmap(u64 *spte)
914{
915 unsigned int index;
916 struct kvm_mmu_page *sp = page_header(__pa(spte));
917
918 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200919 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
920 sp->unsync_children++;
921 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300922}
923
924static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
925{
926 struct kvm_pte_chain *pte_chain;
927 struct hlist_node *node;
928 int i;
929
930 if (!sp->parent_pte)
931 return;
932
933 if (!sp->multimapped) {
934 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
935 return;
936 }
937
938 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
939 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
940 if (!pte_chain->parent_ptes[i])
941 break;
942 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
943 }
944}
945
946static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
947{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300948 kvm_mmu_update_parents_unsync(sp);
949 return 1;
950}
951
952static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
953 struct kvm_mmu_page *sp)
954{
955 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
956 kvm_mmu_update_parents_unsync(sp);
957}
958
Avi Kivityd761a502008-05-29 14:55:03 +0300959static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
960 struct kvm_mmu_page *sp)
961{
962 int i;
963
964 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
965 sp->spt[i] = shadow_trap_nonpresent_pte;
966}
967
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300968static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
969 struct kvm_mmu_page *sp)
970{
971 return 1;
972}
973
Marcelo Tosattia7052892008-09-23 13:18:35 -0300974static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
975{
976}
977
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200978#define KVM_PAGE_ARRAY_NR 16
979
980struct kvm_mmu_pages {
981 struct mmu_page_and_offset {
982 struct kvm_mmu_page *sp;
983 unsigned int idx;
984 } page[KVM_PAGE_ARRAY_NR];
985 unsigned int nr;
986};
987
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300988#define for_each_unsync_children(bitmap, idx) \
989 for (idx = find_first_bit(bitmap, 512); \
990 idx < 512; \
991 idx = find_next_bit(bitmap, 512, idx+1))
992
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200993int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
994 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300995{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200996 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300997
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200998 if (sp->unsync)
999 for (i=0; i < pvec->nr; i++)
1000 if (pvec->page[i].sp == sp)
1001 return 0;
1002
1003 pvec->page[pvec->nr].sp = sp;
1004 pvec->page[pvec->nr].idx = idx;
1005 pvec->nr++;
1006 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1007}
1008
1009static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1010 struct kvm_mmu_pages *pvec)
1011{
1012 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001013
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001014 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001015 u64 ent = sp->spt[i];
1016
Marcelo Tosatti87917232008-12-22 18:49:30 -02001017 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001018 struct kvm_mmu_page *child;
1019 child = page_header(ent & PT64_BASE_ADDR_MASK);
1020
1021 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001022 if (mmu_pages_add(pvec, child, i))
1023 return -ENOSPC;
1024
1025 ret = __mmu_unsync_walk(child, pvec);
1026 if (!ret)
1027 __clear_bit(i, sp->unsync_child_bitmap);
1028 else if (ret > 0)
1029 nr_unsync_leaf += ret;
1030 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001031 return ret;
1032 }
1033
1034 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001035 nr_unsync_leaf++;
1036 if (mmu_pages_add(pvec, child, i))
1037 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001038 }
1039 }
1040 }
1041
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001042 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001043 sp->unsync_children = 0;
1044
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001045 return nr_unsync_leaf;
1046}
1047
1048static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1049 struct kvm_mmu_pages *pvec)
1050{
1051 if (!sp->unsync_children)
1052 return 0;
1053
1054 mmu_pages_add(pvec, sp, 0);
1055 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001056}
1057
Avi Kivity4db35312007-11-21 15:28:32 +02001058static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001059{
1060 unsigned index;
1061 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001062 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001063 struct hlist_node *node;
1064
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001065 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001066 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001067 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001068 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001069 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001070 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001071 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001072 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001073 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001074 }
1075 return NULL;
1076}
1077
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001078static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1079{
1080 list_del(&sp->oos_link);
1081 --kvm->stat.mmu_unsync_global;
1082}
1083
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001084static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1085{
1086 WARN_ON(!sp->unsync);
1087 sp->unsync = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001088 if (sp->global)
1089 kvm_unlink_unsync_global(kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001090 --kvm->stat.mmu_unsync;
1091}
1092
1093static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1094
1095static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1096{
1097 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1098 kvm_mmu_zap_page(vcpu->kvm, sp);
1099 return 1;
1100 }
1101
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001102 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1103 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001104 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001105 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1106 kvm_mmu_zap_page(vcpu->kvm, sp);
1107 return 1;
1108 }
1109
1110 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001111 return 0;
1112}
1113
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001114struct mmu_page_path {
1115 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1116 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001117};
1118
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001119#define for_each_sp(pvec, sp, parents, i) \
1120 for (i = mmu_pages_next(&pvec, &parents, -1), \
1121 sp = pvec.page[i].sp; \
1122 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1123 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001124
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001125int mmu_pages_next(struct kvm_mmu_pages *pvec, struct mmu_page_path *parents,
1126 int i)
1127{
1128 int n;
1129
1130 for (n = i+1; n < pvec->nr; n++) {
1131 struct kvm_mmu_page *sp = pvec->page[n].sp;
1132
1133 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1134 parents->idx[0] = pvec->page[n].idx;
1135 return n;
1136 }
1137
1138 parents->parent[sp->role.level-2] = sp;
1139 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1140 }
1141
1142 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001143}
1144
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001145void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001146{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001147 struct kvm_mmu_page *sp;
1148 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001149
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001150 do {
1151 unsigned int idx = parents->idx[level];
1152
1153 sp = parents->parent[level];
1154 if (!sp)
1155 return;
1156
1157 --sp->unsync_children;
1158 WARN_ON((int)sp->unsync_children < 0);
1159 __clear_bit(idx, sp->unsync_child_bitmap);
1160 level++;
1161 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1162}
1163
1164static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1165 struct mmu_page_path *parents,
1166 struct kvm_mmu_pages *pvec)
1167{
1168 parents->parent[parent->role.level-1] = NULL;
1169 pvec->nr = 0;
1170}
1171
1172static void mmu_sync_children(struct kvm_vcpu *vcpu,
1173 struct kvm_mmu_page *parent)
1174{
1175 int i;
1176 struct kvm_mmu_page *sp;
1177 struct mmu_page_path parents;
1178 struct kvm_mmu_pages pages;
1179
1180 kvm_mmu_pages_init(parent, &parents, &pages);
1181 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001182 int protected = 0;
1183
1184 for_each_sp(pages, sp, parents, i)
1185 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1186
1187 if (protected)
1188 kvm_flush_remote_tlbs(vcpu->kvm);
1189
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001190 for_each_sp(pages, sp, parents, i) {
1191 kvm_sync_page(vcpu, sp);
1192 mmu_pages_clear_parents(&parents);
1193 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001194 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001195 kvm_mmu_pages_init(parent, &parents, &pages);
1196 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001197}
1198
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001199static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1200 gfn_t gfn,
1201 gva_t gaddr,
1202 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001203 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001204 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001205 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001206{
1207 union kvm_mmu_page_role role;
1208 unsigned index;
1209 unsigned quadrant;
1210 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001211 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001212 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001213
Avi Kivitya770f6f2008-12-21 19:20:09 +02001214 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001215 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001216 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001217 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001218 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001219 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1220 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1221 role.quadrant = quadrant;
1222 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001223 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001224 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001225 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001226 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001227 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1228 if (sp->gfn == gfn) {
1229 if (sp->unsync)
1230 if (kvm_sync_page(vcpu, sp))
1231 continue;
1232
1233 if (sp->role.word != role.word)
1234 continue;
1235
Avi Kivity4db35312007-11-21 15:28:32 +02001236 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001237 if (sp->unsync_children) {
1238 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1239 kvm_mmu_mark_parents_unsync(vcpu, sp);
1240 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001241 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001242 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001243 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001244 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001245 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1246 if (!sp)
1247 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001248 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001249 sp->gfn = gfn;
1250 sp->role = role;
Avi Kivitye2078312008-12-21 19:36:59 +02001251 sp->global = role.cr4_pge;
Avi Kivity4db35312007-11-21 15:28:32 +02001252 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001253 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001254 if (rmap_write_protect(vcpu->kvm, gfn))
1255 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256 account_shadowed(vcpu->kvm, gfn);
1257 }
Avi Kivity131d8272008-05-29 14:56:28 +03001258 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1259 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1260 else
1261 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001262 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001263}
1264
Avi Kivity2d111232008-12-25 14:39:47 +02001265static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1266 struct kvm_vcpu *vcpu, u64 addr)
1267{
1268 iterator->addr = addr;
1269 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1270 iterator->level = vcpu->arch.mmu.shadow_root_level;
1271 if (iterator->level == PT32E_ROOT_LEVEL) {
1272 iterator->shadow_addr
1273 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1274 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1275 --iterator->level;
1276 if (!iterator->shadow_addr)
1277 iterator->level = 0;
1278 }
1279}
1280
1281static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1282{
1283 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1284 return false;
1285 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1286 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1287 return true;
1288}
1289
1290static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1291{
1292 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1293 --iterator->level;
1294}
1295
Avi Kivity90cb0522007-07-17 13:04:56 +03001296static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001297 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001298{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001299 unsigned i;
1300 u64 *pt;
1301 u64 ent;
1302
Avi Kivity4db35312007-11-21 15:28:32 +02001303 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001304
Avi Kivity4db35312007-11-21 15:28:32 +02001305 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001306 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001307 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001308 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001309 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001310 }
1311 return;
1312 }
1313
1314 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1315 ent = pt[i];
1316
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001317 if (is_shadow_present_pte(ent)) {
1318 if (!is_large_pte(ent)) {
1319 ent &= PT64_BASE_ADDR_MASK;
1320 mmu_page_remove_parent_pte(page_header(ent),
1321 &pt[i]);
1322 } else {
1323 --kvm->stat.lpages;
1324 rmap_remove(kvm, &pt[i]);
1325 }
1326 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001327 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001328 }
Avi Kivitya4360362007-01-05 16:36:45 -08001329}
1330
Avi Kivity4db35312007-11-21 15:28:32 +02001331static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001332{
Avi Kivity4db35312007-11-21 15:28:32 +02001333 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001334}
1335
Avi Kivity12b7d282007-09-23 14:10:49 +02001336static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1337{
1338 int i;
1339
1340 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1341 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001342 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001343}
1344
Avi Kivity31aa2b42008-07-11 17:59:46 +03001345static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001346{
1347 u64 *parent_pte;
1348
Avi Kivity4db35312007-11-21 15:28:32 +02001349 while (sp->multimapped || sp->parent_pte) {
1350 if (!sp->multimapped)
1351 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001352 else {
1353 struct kvm_pte_chain *chain;
1354
Avi Kivity4db35312007-11-21 15:28:32 +02001355 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001356 struct kvm_pte_chain, link);
1357 parent_pte = chain->parent_ptes[0];
1358 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001359 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001360 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001361 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001362 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001363}
1364
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001365static int mmu_zap_unsync_children(struct kvm *kvm,
1366 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001367{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001368 int i, zapped = 0;
1369 struct mmu_page_path parents;
1370 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001371
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001372 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001374
1375 kvm_mmu_pages_init(parent, &parents, &pages);
1376 while (mmu_unsync_walk(parent, &pages)) {
1377 struct kvm_mmu_page *sp;
1378
1379 for_each_sp(pages, sp, parents, i) {
1380 kvm_mmu_zap_page(kvm, sp);
1381 mmu_pages_clear_parents(&parents);
1382 }
1383 zapped += pages.nr;
1384 kvm_mmu_pages_init(parent, &parents, &pages);
1385 }
1386
1387 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001388}
1389
Marcelo Tosatti07385412008-09-23 13:18:37 -03001390static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001391{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001392 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001393 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001394 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001395 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001396 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001397 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001398 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001399 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001400 if (sp->unsync)
1401 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001402 if (!sp->root_count) {
1403 hlist_del(&sp->hash_link);
1404 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001405 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001406 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001407 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001408 kvm_reload_remote_mmus(kvm);
1409 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001410 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001411 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001412}
1413
Izik Eidus82ce2c92007-10-02 18:52:55 +02001414/*
1415 * Changing the number of mmu pages allocated to the vm
1416 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1417 */
1418void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1419{
1420 /*
1421 * If we set the number of mmu pages to be smaller be than the
1422 * number of actived pages , we must to free some mmu pages before we
1423 * change the value
1424 */
1425
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001426 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001427 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001428 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1429 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001430
1431 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1432 struct kvm_mmu_page *page;
1433
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001434 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001435 struct kvm_mmu_page, link);
1436 kvm_mmu_zap_page(kvm, page);
1437 n_used_mmu_pages--;
1438 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001439 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001440 }
1441 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001442 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1443 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001444
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001445 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001446}
1447
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001448static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001449{
1450 unsigned index;
1451 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001452 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001453 struct hlist_node *node, *n;
1454 int r;
1455
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001456 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001457 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001458 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001459 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001460 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001461 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001462 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001463 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001464 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001465 if (kvm_mmu_zap_page(kvm, sp))
1466 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001467 }
1468 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001469}
1470
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001471static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001472{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001473 unsigned index;
1474 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001475 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001476 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001477
Avi Kivity4677a3b2009-01-06 13:00:27 +02001478 index = kvm_page_table_hashfn(gfn);
1479 bucket = &kvm->arch.mmu_page_hash[index];
1480 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001481 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001482 && !sp->role.invalid) {
1483 pgprintk("%s: zap %lx %x\n",
1484 __func__, gfn, sp->role.word);
1485 kvm_mmu_zap_page(kvm, sp);
1486 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001487 }
1488}
1489
Avi Kivity38c335f2007-11-21 14:20:22 +02001490static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491{
Avi Kivity38c335f2007-11-21 14:20:22 +02001492 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001493 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
Sheng Yang291f26b2008-10-16 17:30:57 +08001495 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001498static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1499{
1500 int i;
1501 u64 *pt = sp->spt;
1502
1503 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1504 return;
1505
1506 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1507 if (pt[i] == shadow_notrap_nonpresent_pte)
1508 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1509 }
1510}
1511
Avi Kivity039576c2007-03-20 12:46:50 +02001512struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1513{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001514 struct page *page;
1515
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001516 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001517
1518 if (gpa == UNMAPPED_GVA)
1519 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001520
Izik Eidus72dc67a2008-02-10 18:04:15 +02001521 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001522
1523 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001524}
1525
Sheng Yang74be52e2008-10-09 16:01:56 +08001526/*
1527 * The function is based on mtrr_type_lookup() in
1528 * arch/x86/kernel/cpu/mtrr/generic.c
1529 */
1530static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1531 u64 start, u64 end)
1532{
1533 int i;
1534 u64 base, mask;
1535 u8 prev_match, curr_match;
1536 int num_var_ranges = KVM_NR_VAR_MTRR;
1537
1538 if (!mtrr_state->enabled)
1539 return 0xFF;
1540
1541 /* Make end inclusive end, instead of exclusive */
1542 end--;
1543
1544 /* Look in fixed ranges. Just return the type as per start */
1545 if (mtrr_state->have_fixed && (start < 0x100000)) {
1546 int idx;
1547
1548 if (start < 0x80000) {
1549 idx = 0;
1550 idx += (start >> 16);
1551 return mtrr_state->fixed_ranges[idx];
1552 } else if (start < 0xC0000) {
1553 idx = 1 * 8;
1554 idx += ((start - 0x80000) >> 14);
1555 return mtrr_state->fixed_ranges[idx];
1556 } else if (start < 0x1000000) {
1557 idx = 3 * 8;
1558 idx += ((start - 0xC0000) >> 12);
1559 return mtrr_state->fixed_ranges[idx];
1560 }
1561 }
1562
1563 /*
1564 * Look in variable ranges
1565 * Look of multiple ranges matching this address and pick type
1566 * as per MTRR precedence
1567 */
1568 if (!(mtrr_state->enabled & 2))
1569 return mtrr_state->def_type;
1570
1571 prev_match = 0xFF;
1572 for (i = 0; i < num_var_ranges; ++i) {
1573 unsigned short start_state, end_state;
1574
1575 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1576 continue;
1577
1578 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1579 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1580 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1581 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1582
1583 start_state = ((start & mask) == (base & mask));
1584 end_state = ((end & mask) == (base & mask));
1585 if (start_state != end_state)
1586 return 0xFE;
1587
1588 if ((start & mask) != (base & mask))
1589 continue;
1590
1591 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1592 if (prev_match == 0xFF) {
1593 prev_match = curr_match;
1594 continue;
1595 }
1596
1597 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1598 curr_match == MTRR_TYPE_UNCACHABLE)
1599 return MTRR_TYPE_UNCACHABLE;
1600
1601 if ((prev_match == MTRR_TYPE_WRBACK &&
1602 curr_match == MTRR_TYPE_WRTHROUGH) ||
1603 (prev_match == MTRR_TYPE_WRTHROUGH &&
1604 curr_match == MTRR_TYPE_WRBACK)) {
1605 prev_match = MTRR_TYPE_WRTHROUGH;
1606 curr_match = MTRR_TYPE_WRTHROUGH;
1607 }
1608
1609 if (prev_match != curr_match)
1610 return MTRR_TYPE_UNCACHABLE;
1611 }
1612
1613 if (prev_match != 0xFF)
1614 return prev_match;
1615
1616 return mtrr_state->def_type;
1617}
1618
1619static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1620{
1621 u8 mtrr;
1622
1623 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1624 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1625 if (mtrr == 0xfe || mtrr == 0xff)
1626 mtrr = MTRR_TYPE_WRBACK;
1627 return mtrr;
1628}
1629
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001630static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1631{
1632 unsigned index;
1633 struct hlist_head *bucket;
1634 struct kvm_mmu_page *s;
1635 struct hlist_node *node, *n;
1636
1637 index = kvm_page_table_hashfn(sp->gfn);
1638 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1639 /* don't unsync if pagetable is shadowed with multiple roles */
1640 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001641 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001642 continue;
1643 if (s->role.word != sp->role.word)
1644 return 1;
1645 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001646 ++vcpu->kvm->stat.mmu_unsync;
1647 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001648
1649 if (sp->global) {
1650 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1651 ++vcpu->kvm->stat.mmu_unsync_global;
1652 } else
1653 kvm_mmu_mark_parents_unsync(vcpu, sp);
1654
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655 mmu_convert_notrap(sp);
1656 return 0;
1657}
1658
1659static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1660 bool can_unsync)
1661{
1662 struct kvm_mmu_page *shadow;
1663
1664 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1665 if (shadow) {
1666 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1667 return 1;
1668 if (shadow->unsync)
1669 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001670 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001671 return kvm_unsync_page(vcpu, shadow);
1672 return 1;
1673 }
1674 return 0;
1675}
1676
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001677static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1678 unsigned pte_access, int user_fault,
1679 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001680 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001681 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001682{
1683 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001684 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001685 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001686 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1687
1688 if (!global && sp->global) {
1689 sp->global = 0;
1690 if (sp->unsync) {
1691 kvm_unlink_unsync_global(vcpu->kvm, sp);
1692 kvm_mmu_mark_parents_unsync(vcpu, sp);
1693 }
1694 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001695
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001696 /*
1697 * We don't set the accessed bit, since we sometimes want to see
1698 * whether the guest actually used the pte (in order to detect
1699 * demand paging).
1700 */
Sheng Yang7b523452008-04-25 21:13:50 +08001701 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001702 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001703 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001704 if (!dirty)
1705 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001706 if (pte_access & ACC_EXEC_MASK)
1707 spte |= shadow_x_mask;
1708 else
1709 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001710 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001711 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001712 if (largepage)
1713 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001714 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001715 if (!kvm_is_mmio_pfn(pfn)) {
1716 mt_mask = get_memory_type(vcpu, gfn) <<
1717 kvm_x86_ops->get_mt_mask_shift();
1718 mt_mask |= VMX_EPT_IGMT_BIT;
1719 } else
1720 mt_mask = MTRR_TYPE_UNCACHABLE <<
1721 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001722 spte |= mt_mask;
1723 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001724
Anthony Liguori35149e22008-04-02 14:46:56 -05001725 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001726
1727 if ((pte_access & ACC_WRITE_MASK)
1728 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001729
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001730 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1731 ret = 1;
1732 spte = shadow_trap_nonpresent_pte;
1733 goto set_pte;
1734 }
1735
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001736 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001737
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001738 /*
1739 * Optimization: for pte sync, if spte was writable the hash
1740 * lookup is unnecessary (and expensive). Write protection
1741 * is responsibility of mmu_get_page / kvm_sync_page.
1742 * Same reasoning can be applied to dirty page accounting.
1743 */
1744 if (!can_unsync && is_writeble_pte(*shadow_pte))
1745 goto set_pte;
1746
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001747 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001748 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001749 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001750 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001751 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001752 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001753 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001754 }
1755 }
1756
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001757 if (pte_access & ACC_WRITE_MASK)
1758 mark_page_dirty(vcpu->kvm, gfn);
1759
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001760set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001761 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001762 return ret;
1763}
1764
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001765static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1766 unsigned pt_access, unsigned pte_access,
1767 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001768 int *ptwrite, int largepage, int global,
1769 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001770{
1771 int was_rmapped = 0;
1772 int was_writeble = is_writeble_pte(*shadow_pte);
1773
1774 pgprintk("%s: spte %llx access %x write_fault %d"
1775 " user_fault %d gfn %lx\n",
1776 __func__, *shadow_pte, pt_access,
1777 write_fault, user_fault, gfn);
1778
1779 if (is_rmap_pte(*shadow_pte)) {
1780 /*
1781 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1782 * the parent of the now unreachable PTE.
1783 */
1784 if (largepage && !is_large_pte(*shadow_pte)) {
1785 struct kvm_mmu_page *child;
1786 u64 pte = *shadow_pte;
1787
1788 child = page_header(pte & PT64_BASE_ADDR_MASK);
1789 mmu_page_remove_parent_pte(child, shadow_pte);
1790 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1791 pgprintk("hfn old %lx new %lx\n",
1792 spte_to_pfn(*shadow_pte), pfn);
1793 rmap_remove(vcpu->kvm, shadow_pte);
1794 } else {
1795 if (largepage)
1796 was_rmapped = is_large_pte(*shadow_pte);
1797 else
1798 was_rmapped = 1;
1799 }
1800 }
1801 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001802 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001803 if (write_fault)
1804 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001805 kvm_x86_ops->tlb_flush(vcpu);
1806 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001807
1808 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1809 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1810 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1811 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1812 *shadow_pte, shadow_pte);
1813 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001814 ++vcpu->kvm->stat.lpages;
1815
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001816 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1817 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001818 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001819 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001820 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001821 } else {
1822 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001823 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001824 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001825 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001826 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001827 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001828 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001829 vcpu->arch.last_pte_gfn = gfn;
1830 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001831}
1832
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1834{
1835}
1836
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001837static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001838 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839{
Avi Kivity9f652d22008-12-25 14:54:25 +02001840 struct kvm_shadow_walk_iterator iterator;
1841 struct kvm_mmu_page *sp;
1842 int pt_write = 0;
1843 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844
Avi Kivity9f652d22008-12-25 14:54:25 +02001845 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1846 if (iterator.level == PT_PAGE_TABLE_LEVEL
1847 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1848 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1849 0, write, 1, &pt_write,
1850 largepage, 0, gfn, pfn, false);
1851 ++vcpu->stat.pf_fixed;
1852 break;
1853 }
1854
1855 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1856 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1857 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1858 iterator.level - 1,
1859 1, ACC_ALL, iterator.sptep);
1860 if (!sp) {
1861 pgprintk("nonpaging_map: ENOMEM\n");
1862 kvm_release_pfn_clean(pfn);
1863 return -ENOMEM;
1864 }
1865
1866 set_shadow_pte(iterator.sptep,
1867 __pa(sp->spt)
1868 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1869 | shadow_user_mask | shadow_x_mask);
1870 }
1871 }
1872 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873}
1874
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001875static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1876{
1877 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001878 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001879 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001880 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001881
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001882 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1883 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1884 largepage = 1;
1885 }
1886
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001887 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001888 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001889 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001890
Avi Kivityd196e342008-01-24 11:44:11 +02001891 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001892 if (is_error_pfn(pfn)) {
1893 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001894 return 1;
1895 }
1896
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001897 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001898 if (mmu_notifier_retry(vcpu, mmu_seq))
1899 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001900 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001901 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001902 spin_unlock(&vcpu->kvm->mmu_lock);
1903
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001904
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001905 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001906
1907out_unlock:
1908 spin_unlock(&vcpu->kvm->mmu_lock);
1909 kvm_release_pfn_clean(pfn);
1910 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001911}
1912
1913
Avi Kivity17ac10a2007-01-05 16:36:40 -08001914static void mmu_free_roots(struct kvm_vcpu *vcpu)
1915{
1916 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001917 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001918
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001919 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001920 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001921 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001922 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1923 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001924
Avi Kivity4db35312007-11-21 15:28:32 +02001925 sp = page_header(root);
1926 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001927 if (!sp->root_count && sp->role.invalid)
1928 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001929 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001930 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001931 return;
1932 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001933 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001934 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001935
Avi Kivity417726a2007-04-12 17:35:58 +03001936 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001937 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001938 sp = page_header(root);
1939 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001940 if (!sp->root_count && sp->role.invalid)
1941 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001942 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001943 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001944 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001945 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001946 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001947}
1948
1949static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1950{
1951 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001952 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001953 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001954 int direct = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001955
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001956 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001957
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001958 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1959 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001960
1961 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001962 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001963 direct = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001964 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001965 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001966 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001967 root = __pa(sp->spt);
1968 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001969 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001970 return;
1971 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001972 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001973 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001974 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001975 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001976 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001977
1978 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001979 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1980 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1981 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001982 continue;
1983 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1985 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001986 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001987 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001988 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001989 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001990 root = __pa(sp->spt);
1991 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001992 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001993 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001994 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001995}
1996
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001997static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1998{
1999 int i;
2000 struct kvm_mmu_page *sp;
2001
2002 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2003 return;
2004 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2005 hpa_t root = vcpu->arch.mmu.root_hpa;
2006 sp = page_header(root);
2007 mmu_sync_children(vcpu, sp);
2008 return;
2009 }
2010 for (i = 0; i < 4; ++i) {
2011 hpa_t root = vcpu->arch.mmu.pae_root[i];
2012
2013 if (root) {
2014 root &= PT64_BASE_ADDR_MASK;
2015 sp = page_header(root);
2016 mmu_sync_children(vcpu, sp);
2017 }
2018 }
2019}
2020
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002021static void mmu_sync_global(struct kvm_vcpu *vcpu)
2022{
2023 struct kvm *kvm = vcpu->kvm;
2024 struct kvm_mmu_page *sp, *n;
2025
2026 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2027 kvm_sync_page(vcpu, sp);
2028}
2029
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002030void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2031{
2032 spin_lock(&vcpu->kvm->mmu_lock);
2033 mmu_sync_roots(vcpu);
2034 spin_unlock(&vcpu->kvm->mmu_lock);
2035}
2036
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002037void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2038{
2039 spin_lock(&vcpu->kvm->mmu_lock);
2040 mmu_sync_global(vcpu);
2041 spin_unlock(&vcpu->kvm->mmu_lock);
2042}
2043
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2045{
2046 return vaddr;
2047}
2048
2049static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002050 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051{
Avi Kivitye8332402007-12-09 18:43:00 +02002052 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002053 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002055 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002056 r = mmu_topup_memory_caches(vcpu);
2057 if (r)
2058 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002059
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002061 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062
Avi Kivitye8332402007-12-09 18:43:00 +02002063 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064
Avi Kivitye8332402007-12-09 18:43:00 +02002065 return nonpaging_map(vcpu, gva & PAGE_MASK,
2066 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067}
2068
Joerg Roedelfb72d162008-02-07 13:47:44 +01002069static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2070 u32 error_code)
2071{
Anthony Liguori35149e22008-04-02 14:46:56 -05002072 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002073 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002074 int largepage = 0;
2075 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002076 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002077
2078 ASSERT(vcpu);
2079 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2080
2081 r = mmu_topup_memory_caches(vcpu);
2082 if (r)
2083 return r;
2084
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002085 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2086 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2087 largepage = 1;
2088 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002089 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002090 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002091 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002092 if (is_error_pfn(pfn)) {
2093 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002094 return 1;
2095 }
2096 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002097 if (mmu_notifier_retry(vcpu, mmu_seq))
2098 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002099 kvm_mmu_free_some_pages(vcpu);
2100 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002101 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002102 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002103
2104 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002105
2106out_unlock:
2107 spin_unlock(&vcpu->kvm->mmu_lock);
2108 kvm_release_pfn_clean(pfn);
2109 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002110}
2111
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112static void nonpaging_free(struct kvm_vcpu *vcpu)
2113{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002114 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115}
2116
2117static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2118{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002119 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120
2121 context->new_cr3 = nonpaging_new_cr3;
2122 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 context->gva_to_gpa = nonpaging_gva_to_gpa;
2124 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002125 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002126 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002127 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002128 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002130 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 return 0;
2132}
2133
Avi Kivityd835dfe2007-11-21 02:57:59 +02002134void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002136 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002137 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138}
2139
2140static void paging_new_cr3(struct kvm_vcpu *vcpu)
2141{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002142 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002143 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144}
2145
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146static void inject_page_fault(struct kvm_vcpu *vcpu,
2147 u64 addr,
2148 u32 err_code)
2149{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002150 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151}
2152
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153static void paging_free(struct kvm_vcpu *vcpu)
2154{
2155 nonpaging_free(vcpu);
2156}
2157
2158#define PTTYPE 64
2159#include "paging_tmpl.h"
2160#undef PTTYPE
2161
2162#define PTTYPE 32
2163#include "paging_tmpl.h"
2164#undef PTTYPE
2165
Avi Kivity17ac10a2007-01-05 16:36:40 -08002166static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002168 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169
2170 ASSERT(is_pae(vcpu));
2171 context->new_cr3 = paging_new_cr3;
2172 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002174 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002175 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002176 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002178 context->root_level = level;
2179 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002180 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 return 0;
2182}
2183
Avi Kivity17ac10a2007-01-05 16:36:40 -08002184static int paging64_init_context(struct kvm_vcpu *vcpu)
2185{
2186 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2187}
2188
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189static int paging32_init_context(struct kvm_vcpu *vcpu)
2190{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002191 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192
2193 context->new_cr3 = paging_new_cr3;
2194 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 context->gva_to_gpa = paging32_gva_to_gpa;
2196 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002197 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002198 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002199 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200 context->root_level = PT32_ROOT_LEVEL;
2201 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002202 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 return 0;
2204}
2205
2206static int paging32E_init_context(struct kvm_vcpu *vcpu)
2207{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002208 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209}
2210
Joerg Roedelfb72d162008-02-07 13:47:44 +01002211static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2212{
2213 struct kvm_mmu *context = &vcpu->arch.mmu;
2214
2215 context->new_cr3 = nonpaging_new_cr3;
2216 context->page_fault = tdp_page_fault;
2217 context->free = nonpaging_free;
2218 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002219 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002220 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002221 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002222 context->root_hpa = INVALID_PAGE;
2223
2224 if (!is_paging(vcpu)) {
2225 context->gva_to_gpa = nonpaging_gva_to_gpa;
2226 context->root_level = 0;
2227 } else if (is_long_mode(vcpu)) {
2228 context->gva_to_gpa = paging64_gva_to_gpa;
2229 context->root_level = PT64_ROOT_LEVEL;
2230 } else if (is_pae(vcpu)) {
2231 context->gva_to_gpa = paging64_gva_to_gpa;
2232 context->root_level = PT32E_ROOT_LEVEL;
2233 } else {
2234 context->gva_to_gpa = paging32_gva_to_gpa;
2235 context->root_level = PT32_ROOT_LEVEL;
2236 }
2237
2238 return 0;
2239}
2240
2241static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002243 int r;
2244
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002246 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247
2248 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002249 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002250 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002251 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002253 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002255 r = paging32_init_context(vcpu);
2256
2257 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2258
2259 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260}
2261
Joerg Roedelfb72d162008-02-07 13:47:44 +01002262static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2263{
Anthony Liguori35149e22008-04-02 14:46:56 -05002264 vcpu->arch.update_pte.pfn = bad_pfn;
2265
Joerg Roedelfb72d162008-02-07 13:47:44 +01002266 if (tdp_enabled)
2267 return init_kvm_tdp_mmu(vcpu);
2268 else
2269 return init_kvm_softmmu(vcpu);
2270}
2271
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2273{
2274 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002275 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2276 vcpu->arch.mmu.free(vcpu);
2277 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 }
2279}
2280
2281int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2282{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002283 destroy_kvm_mmu(vcpu);
2284 return init_kvm_mmu(vcpu);
2285}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002286EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002287
2288int kvm_mmu_load(struct kvm_vcpu *vcpu)
2289{
Avi Kivity714b93d2007-01-05 16:36:53 -08002290 int r;
2291
Avi Kivitye2dec932007-01-05 16:36:54 -08002292 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002293 if (r)
2294 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002295 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002296 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002297 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002298 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002299 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002300 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002301 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002302out:
2303 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002305EXPORT_SYMBOL_GPL(kvm_mmu_load);
2306
2307void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2308{
2309 mmu_free_roots(vcpu);
2310}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311
Avi Kivity09072da2007-05-01 14:16:52 +03002312static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002313 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002314 u64 *spte)
2315{
2316 u64 pte;
2317 struct kvm_mmu_page *child;
2318
2319 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002320 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002321 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2322 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002323 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002324 else {
2325 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002326 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002327 }
2328 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002329 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002330 if (is_large_pte(pte))
2331 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002332}
2333
Avi Kivity00284252007-05-01 16:53:31 +03002334static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002335 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002336 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002337 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002338{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002339 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2340 if (!vcpu->arch.update_pte.largepage ||
2341 sp->role.glevels == PT32_ROOT_LEVEL) {
2342 ++vcpu->kvm->stat.mmu_pde_zapped;
2343 return;
2344 }
2345 }
Avi Kivity00284252007-05-01 16:53:31 +03002346
Avi Kivity4cee5762007-11-18 16:37:07 +02002347 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002348 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002349 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002350 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002351 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002352}
2353
Avi Kivity79539ce2007-11-21 02:06:21 +02002354static bool need_remote_flush(u64 old, u64 new)
2355{
2356 if (!is_shadow_present_pte(old))
2357 return false;
2358 if (!is_shadow_present_pte(new))
2359 return true;
2360 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2361 return true;
2362 old ^= PT64_NX_MASK;
2363 new ^= PT64_NX_MASK;
2364 return (old & ~new & PT64_PERM_MASK) != 0;
2365}
2366
2367static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2368{
2369 if (need_remote_flush(old, new))
2370 kvm_flush_remote_tlbs(vcpu->kvm);
2371 else
2372 kvm_mmu_flush_tlb(vcpu);
2373}
2374
Avi Kivity12b7d282007-09-23 14:10:49 +02002375static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002377 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002378
Sheng Yang7b523452008-04-25 21:13:50 +08002379 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002380}
2381
Avi Kivityd7824ff2007-12-30 12:29:05 +02002382static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2383 const u8 *new, int bytes)
2384{
2385 gfn_t gfn;
2386 int r;
2387 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002388 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002389
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002390 vcpu->arch.update_pte.largepage = 0;
2391
Avi Kivityd7824ff2007-12-30 12:29:05 +02002392 if (bytes != 4 && bytes != 8)
2393 return;
2394
2395 /*
2396 * Assume that the pte write on a page table of the same type
2397 * as the current vcpu paging mode. This is nearly always true
2398 * (might be false while changing modes). Note it is verified later
2399 * by update_pte().
2400 */
2401 if (is_pae(vcpu)) {
2402 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2403 if ((bytes == 4) && (gpa % 4 == 0)) {
2404 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2405 if (r)
2406 return;
2407 memcpy((void *)&gpte + (gpa % 8), new, 4);
2408 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2409 memcpy((void *)&gpte, new, 8);
2410 }
2411 } else {
2412 if ((bytes == 4) && (gpa % 4 == 0))
2413 memcpy((void *)&gpte, new, 4);
2414 }
2415 if (!is_present_pte(gpte))
2416 return;
2417 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002418
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002419 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2420 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2421 vcpu->arch.update_pte.largepage = 1;
2422 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002423 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002424 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002425 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002426
Anthony Liguori35149e22008-04-02 14:46:56 -05002427 if (is_error_pfn(pfn)) {
2428 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002429 return;
2430 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002431 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002432 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002433}
2434
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002435static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2436{
2437 u64 *spte = vcpu->arch.last_pte_updated;
2438
2439 if (spte
2440 && vcpu->arch.last_pte_gfn == gfn
2441 && shadow_accessed_mask
2442 && !(*spte & shadow_accessed_mask)
2443 && is_shadow_present_pte(*spte))
2444 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2445}
2446
Avi Kivity09072da2007-05-01 14:16:52 +03002447void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002448 const u8 *new, int bytes,
2449 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002450{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002451 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002452 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002453 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002454 struct hlist_head *bucket;
2455 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002456 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002457 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002458 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002459 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002460 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002461 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002462 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002463 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002464 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002465 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002466 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002467
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002468 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002469 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002470 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002471 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002472 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002473 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002474 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002475 if (guest_initiated) {
2476 if (gfn == vcpu->arch.last_pt_write_gfn
2477 && !last_updated_pte_accessed(vcpu)) {
2478 ++vcpu->arch.last_pt_write_count;
2479 if (vcpu->arch.last_pt_write_count >= 3)
2480 flooded = 1;
2481 } else {
2482 vcpu->arch.last_pt_write_gfn = gfn;
2483 vcpu->arch.last_pt_write_count = 1;
2484 vcpu->arch.last_pte_updated = NULL;
2485 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002486 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002487 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002488 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002489 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002490 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002491 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002492 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002493 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002494 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002495 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002496 /*
2497 * Misaligned accesses are too much trouble to fix
2498 * up; also, they usually indicate a page is not used
2499 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002500 *
2501 * If we're seeing too many writes to a page,
2502 * it may no longer be a page table, or we may be
2503 * forking, in which case it is better to unmap the
2504 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002505 */
2506 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002507 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002508 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2509 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002510 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002511 continue;
2512 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002513 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002514 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002515 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002516 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002517 page_offset <<= 1; /* 32->64 */
2518 /*
2519 * A 32-bit pde maps 4MB while the shadow pdes map
2520 * only 2MB. So we need to double the offset again
2521 * and zap two pdes instead of one.
2522 */
2523 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002524 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002525 page_offset <<= 1;
2526 npte = 2;
2527 }
Avi Kivityfce06572007-05-01 16:44:05 +03002528 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002529 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002530 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002531 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002532 }
Avi Kivity4db35312007-11-21 15:28:32 +02002533 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002534 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2535 gentry = 0;
2536 r = kvm_read_guest_atomic(vcpu->kvm,
2537 gpa & ~(u64)(pte_size - 1),
2538 &gentry, pte_size);
2539 new = (const void *)&gentry;
2540 if (r < 0)
2541 new = NULL;
2542 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002543 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002544 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002545 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002546 if (new)
2547 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002548 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002549 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002550 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002551 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002552 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002553 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002554 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2555 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2556 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002557 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002558}
2559
Avi Kivitya4360362007-01-05 16:36:45 -08002560int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2561{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002562 gpa_t gpa;
2563 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002564
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002565 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002566
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002567 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002568 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002569 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002570 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002571}
Avi Kivity577bdc42008-07-19 08:57:05 +03002572EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002573
Avi Kivity22d95b12007-09-14 20:26:06 +03002574void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002575{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002576 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002577 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002578
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002579 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002580 struct kvm_mmu_page, link);
2581 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002582 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002583 }
2584}
Avi Kivityebeace82007-01-05 16:36:47 -08002585
Avi Kivity30677142007-10-28 18:48:59 +02002586int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2587{
2588 int r;
2589 enum emulation_result er;
2590
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002591 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002592 if (r < 0)
2593 goto out;
2594
2595 if (!r) {
2596 r = 1;
2597 goto out;
2598 }
2599
Avi Kivityb733bfb2007-10-28 18:52:05 +02002600 r = mmu_topup_memory_caches(vcpu);
2601 if (r)
2602 goto out;
2603
Avi Kivity30677142007-10-28 18:48:59 +02002604 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002605
2606 switch (er) {
2607 case EMULATE_DONE:
2608 return 1;
2609 case EMULATE_DO_MMIO:
2610 ++vcpu->stat.mmio_exits;
2611 return 0;
2612 case EMULATE_FAIL:
2613 kvm_report_emulation_failure(vcpu, "pagetable");
2614 return 1;
2615 default:
2616 BUG();
2617 }
2618out:
Avi Kivity30677142007-10-28 18:48:59 +02002619 return r;
2620}
2621EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2622
Marcelo Tosattia7052892008-09-23 13:18:35 -03002623void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2624{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002625 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002626 kvm_mmu_flush_tlb(vcpu);
2627 ++vcpu->stat.invlpg;
2628}
2629EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2630
Joerg Roedel18552672008-02-07 13:47:41 +01002631void kvm_enable_tdp(void)
2632{
2633 tdp_enabled = true;
2634}
2635EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2636
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002637void kvm_disable_tdp(void)
2638{
2639 tdp_enabled = false;
2640}
2641EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2642
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643static void free_mmu_pages(struct kvm_vcpu *vcpu)
2644{
Avi Kivity4db35312007-11-21 15:28:32 +02002645 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002647 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2648 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002649 struct kvm_mmu_page, link);
2650 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002651 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002652 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002653 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654}
2655
2656static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2657{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002658 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 int i;
2660
2661 ASSERT(vcpu);
2662
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002663 if (vcpu->kvm->arch.n_requested_mmu_pages)
2664 vcpu->kvm->arch.n_free_mmu_pages =
2665 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002666 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002667 vcpu->kvm->arch.n_free_mmu_pages =
2668 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002669 /*
2670 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2671 * Therefore we need to allocate shadow page tables in the first
2672 * 4GB of memory, which happens to fit the DMA32 zone.
2673 */
2674 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2675 if (!page)
2676 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002677 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002678 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002679 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002680
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681 return 0;
2682
2683error_1:
2684 free_mmu_pages(vcpu);
2685 return -ENOMEM;
2686}
2687
Ingo Molnar8018c272006-12-29 16:50:01 -08002688int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002691 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692
Ingo Molnar8018c272006-12-29 16:50:01 -08002693 return alloc_mmu_pages(vcpu);
2694}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695
Ingo Molnar8018c272006-12-29 16:50:01 -08002696int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2697{
2698 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002699 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002700
Ingo Molnar8018c272006-12-29 16:50:01 -08002701 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702}
2703
2704void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2705{
2706 ASSERT(vcpu);
2707
2708 destroy_kvm_mmu(vcpu);
2709 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002710 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711}
2712
Avi Kivity90cb0522007-07-17 13:04:56 +03002713void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714{
Avi Kivity4db35312007-11-21 15:28:32 +02002715 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716
Avi Kivity2245a282008-08-27 16:32:24 +03002717 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002718 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 int i;
2720 u64 *pt;
2721
Sheng Yang291f26b2008-10-16 17:30:57 +08002722 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 continue;
2724
Avi Kivity4db35312007-11-21 15:28:32 +02002725 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2727 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002728 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 }
Avi Kivity171d5952008-08-27 16:40:51 +03002731 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002732 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002734
Avi Kivity90cb0522007-07-17 13:04:56 +03002735void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002736{
Avi Kivity4db35312007-11-21 15:28:32 +02002737 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002738
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002739 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002740 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002741 if (kvm_mmu_zap_page(kvm, sp))
2742 node = container_of(kvm->arch.active_mmu_pages.next,
2743 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002744 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002745
Avi Kivity90cb0522007-07-17 13:04:56 +03002746 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002747}
2748
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002749static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002750{
2751 struct kvm_mmu_page *page;
2752
2753 page = container_of(kvm->arch.active_mmu_pages.prev,
2754 struct kvm_mmu_page, link);
2755 kvm_mmu_zap_page(kvm, page);
2756}
2757
2758static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2759{
2760 struct kvm *kvm;
2761 struct kvm *kvm_freed = NULL;
2762 int cache_count = 0;
2763
2764 spin_lock(&kvm_lock);
2765
2766 list_for_each_entry(kvm, &vm_list, vm_list) {
2767 int npages;
2768
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002769 if (!down_read_trylock(&kvm->slots_lock))
2770 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002771 spin_lock(&kvm->mmu_lock);
2772 npages = kvm->arch.n_alloc_mmu_pages -
2773 kvm->arch.n_free_mmu_pages;
2774 cache_count += npages;
2775 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2776 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2777 cache_count--;
2778 kvm_freed = kvm;
2779 }
2780 nr_to_scan--;
2781
2782 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002783 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002784 }
2785 if (kvm_freed)
2786 list_move_tail(&kvm_freed->vm_list, &vm_list);
2787
2788 spin_unlock(&kvm_lock);
2789
2790 return cache_count;
2791}
2792
2793static struct shrinker mmu_shrinker = {
2794 .shrink = mmu_shrink,
2795 .seeks = DEFAULT_SEEKS * 10,
2796};
2797
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002798static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002799{
2800 if (pte_chain_cache)
2801 kmem_cache_destroy(pte_chain_cache);
2802 if (rmap_desc_cache)
2803 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002804 if (mmu_page_header_cache)
2805 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002806}
2807
Izik Eidus3ee16c82008-03-30 15:17:21 +03002808void kvm_mmu_module_exit(void)
2809{
2810 mmu_destroy_caches();
2811 unregister_shrinker(&mmu_shrinker);
2812}
2813
Avi Kivityb5a33a72007-04-15 16:31:09 +03002814int kvm_mmu_module_init(void)
2815{
2816 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2817 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002818 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002819 if (!pte_chain_cache)
2820 goto nomem;
2821 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2822 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002823 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002824 if (!rmap_desc_cache)
2825 goto nomem;
2826
Avi Kivityd3d25b02007-05-30 12:34:53 +03002827 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2828 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002829 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002830 if (!mmu_page_header_cache)
2831 goto nomem;
2832
Izik Eidus3ee16c82008-03-30 15:17:21 +03002833 register_shrinker(&mmu_shrinker);
2834
Avi Kivityb5a33a72007-04-15 16:31:09 +03002835 return 0;
2836
2837nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002838 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002839 return -ENOMEM;
2840}
2841
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002842/*
2843 * Caculate mmu pages needed for kvm.
2844 */
2845unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2846{
2847 int i;
2848 unsigned int nr_mmu_pages;
2849 unsigned int nr_pages = 0;
2850
2851 for (i = 0; i < kvm->nmemslots; i++)
2852 nr_pages += kvm->memslots[i].npages;
2853
2854 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2855 nr_mmu_pages = max(nr_mmu_pages,
2856 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2857
2858 return nr_mmu_pages;
2859}
2860
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002861static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2862 unsigned len)
2863{
2864 if (len > buffer->len)
2865 return NULL;
2866 return buffer->ptr;
2867}
2868
2869static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2870 unsigned len)
2871{
2872 void *ret;
2873
2874 ret = pv_mmu_peek_buffer(buffer, len);
2875 if (!ret)
2876 return ret;
2877 buffer->ptr += len;
2878 buffer->len -= len;
2879 buffer->processed += len;
2880 return ret;
2881}
2882
2883static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2884 gpa_t addr, gpa_t value)
2885{
2886 int bytes = 8;
2887 int r;
2888
2889 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2890 bytes = 4;
2891
2892 r = mmu_topup_memory_caches(vcpu);
2893 if (r)
2894 return r;
2895
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002896 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002897 return -EFAULT;
2898
2899 return 1;
2900}
2901
2902static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2903{
2904 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002905 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002906 return 1;
2907}
2908
2909static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2910{
2911 spin_lock(&vcpu->kvm->mmu_lock);
2912 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2913 spin_unlock(&vcpu->kvm->mmu_lock);
2914 return 1;
2915}
2916
2917static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2918 struct kvm_pv_mmu_op_buffer *buffer)
2919{
2920 struct kvm_mmu_op_header *header;
2921
2922 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2923 if (!header)
2924 return 0;
2925 switch (header->op) {
2926 case KVM_MMU_OP_WRITE_PTE: {
2927 struct kvm_mmu_op_write_pte *wpte;
2928
2929 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2930 if (!wpte)
2931 return 0;
2932 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2933 wpte->pte_val);
2934 }
2935 case KVM_MMU_OP_FLUSH_TLB: {
2936 struct kvm_mmu_op_flush_tlb *ftlb;
2937
2938 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2939 if (!ftlb)
2940 return 0;
2941 return kvm_pv_mmu_flush_tlb(vcpu);
2942 }
2943 case KVM_MMU_OP_RELEASE_PT: {
2944 struct kvm_mmu_op_release_pt *rpt;
2945
2946 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2947 if (!rpt)
2948 return 0;
2949 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2950 }
2951 default: return 0;
2952 }
2953}
2954
2955int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2956 gpa_t addr, unsigned long *ret)
2957{
2958 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002959 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002960
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002961 buffer->ptr = buffer->buf;
2962 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2963 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002964
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002965 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002966 if (r)
2967 goto out;
2968
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002969 while (buffer->len) {
2970 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002971 if (r < 0)
2972 goto out;
2973 if (r == 0)
2974 break;
2975 }
2976
2977 r = 1;
2978out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002979 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002980 return r;
2981}
2982
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002983#ifdef AUDIT
2984
2985static const char *audit_msg;
2986
2987static gva_t canonicalize(gva_t gva)
2988{
2989#ifdef CONFIG_X86_64
2990 gva = (long long)(gva << 16) >> 16;
2991#endif
2992 return gva;
2993}
2994
2995static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2996 gva_t va, int level)
2997{
2998 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2999 int i;
3000 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3001
3002 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3003 u64 ent = pt[i];
3004
Avi Kivityc7addb92007-09-16 18:58:32 +02003005 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003006 continue;
3007
3008 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003009 if (level > 1) {
3010 if (ent == shadow_notrap_nonpresent_pte)
3011 printk(KERN_ERR "audit: (%s) nontrapping pte"
3012 " in nonleaf level: levels %d gva %lx"
3013 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003014 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003015
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003016 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003017 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003018 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003019 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003020
Avi Kivityc7addb92007-09-16 18:58:32 +02003021 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003022 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003023 printk(KERN_ERR "xx audit error: (%s) levels %d"
3024 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003025 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003026 va, gpa, hpa, ent,
3027 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003028 else if (ent == shadow_notrap_nonpresent_pte
3029 && !is_error_hpa(hpa))
3030 printk(KERN_ERR "audit: (%s) notrap shadow,"
3031 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003032 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003033
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003034 }
3035 }
3036}
3037
3038static void audit_mappings(struct kvm_vcpu *vcpu)
3039{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003040 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003041
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003042 if (vcpu->arch.mmu.root_level == 4)
3043 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003044 else
3045 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003046 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003047 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003048 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003049 i << 30,
3050 2);
3051}
3052
3053static int count_rmaps(struct kvm_vcpu *vcpu)
3054{
3055 int nmaps = 0;
3056 int i, j, k;
3057
3058 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3059 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3060 struct kvm_rmap_desc *d;
3061
3062 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003063 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003064
Izik Eidus290fc382007-09-27 14:11:22 +02003065 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003066 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003067 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003068 ++nmaps;
3069 continue;
3070 }
Izik Eidus290fc382007-09-27 14:11:22 +02003071 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003072 while (d) {
3073 for (k = 0; k < RMAP_EXT; ++k)
3074 if (d->shadow_ptes[k])
3075 ++nmaps;
3076 else
3077 break;
3078 d = d->more;
3079 }
3080 }
3081 }
3082 return nmaps;
3083}
3084
3085static int count_writable_mappings(struct kvm_vcpu *vcpu)
3086{
3087 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003088 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003089 int i;
3090
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003091 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003092 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003093
Avi Kivity4db35312007-11-21 15:28:32 +02003094 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003095 continue;
3096
3097 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3098 u64 ent = pt[i];
3099
3100 if (!(ent & PT_PRESENT_MASK))
3101 continue;
3102 if (!(ent & PT_WRITABLE_MASK))
3103 continue;
3104 ++nmaps;
3105 }
3106 }
3107 return nmaps;
3108}
3109
3110static void audit_rmap(struct kvm_vcpu *vcpu)
3111{
3112 int n_rmap = count_rmaps(vcpu);
3113 int n_actual = count_writable_mappings(vcpu);
3114
3115 if (n_rmap != n_actual)
3116 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003117 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003118}
3119
3120static void audit_write_protection(struct kvm_vcpu *vcpu)
3121{
Avi Kivity4db35312007-11-21 15:28:32 +02003122 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003123 struct kvm_memory_slot *slot;
3124 unsigned long *rmapp;
3125 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003126
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003127 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003128 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003129 continue;
3130
Avi Kivity4db35312007-11-21 15:28:32 +02003131 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003132 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003133 rmapp = &slot->rmap[gfn - slot->base_gfn];
3134 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003135 printk(KERN_ERR "%s: (%s) shadow page has writable"
3136 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003137 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003138 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003139 }
3140}
3141
3142static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3143{
3144 int olddbg = dbg;
3145
3146 dbg = 0;
3147 audit_msg = msg;
3148 audit_rmap(vcpu);
3149 audit_write_protection(vcpu);
3150 audit_mappings(vcpu);
3151 dbg = olddbg;
3152}
3153
3154#endif