blob: 4a21b0f8491c4a6dda5925d5992de6a9c68c58c0 [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);
Sheng Yang291f26b2008-10-16 17:30:57 +0800805 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200806 sp->multimapped = 0;
807 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800808 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200809 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810}
811
Avi Kivity714b93d2007-01-05 16:36:53 -0800812static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200813 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800814{
815 struct kvm_pte_chain *pte_chain;
816 struct hlist_node *node;
817 int i;
818
819 if (!parent_pte)
820 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200821 if (!sp->multimapped) {
822 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800823
824 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200825 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800826 return;
827 }
Avi Kivity4db35312007-11-21 15:28:32 +0200828 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800829 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200830 INIT_HLIST_HEAD(&sp->parent_ptes);
831 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800832 pte_chain->parent_ptes[0] = old;
833 }
Avi Kivity4db35312007-11-21 15:28:32 +0200834 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
836 continue;
837 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
838 if (!pte_chain->parent_ptes[i]) {
839 pte_chain->parent_ptes[i] = parent_pte;
840 return;
841 }
842 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800843 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800844 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200845 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 pte_chain->parent_ptes[0] = parent_pte;
847}
848
Avi Kivity4db35312007-11-21 15:28:32 +0200849static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800850 u64 *parent_pte)
851{
852 struct kvm_pte_chain *pte_chain;
853 struct hlist_node *node;
854 int i;
855
Avi Kivity4db35312007-11-21 15:28:32 +0200856 if (!sp->multimapped) {
857 BUG_ON(sp->parent_pte != parent_pte);
858 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800859 return;
860 }
Avi Kivity4db35312007-11-21 15:28:32 +0200861 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800862 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
863 if (!pte_chain->parent_ptes[i])
864 break;
865 if (pte_chain->parent_ptes[i] != parent_pte)
866 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800867 while (i + 1 < NR_PTE_CHAIN_ENTRIES
868 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800869 pte_chain->parent_ptes[i]
870 = pte_chain->parent_ptes[i + 1];
871 ++i;
872 }
873 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800874 if (i == 0) {
875 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300876 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200877 if (hlist_empty(&sp->parent_ptes)) {
878 sp->multimapped = 0;
879 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800880 }
881 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800882 return;
883 }
884 BUG();
885}
886
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300887
888static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
889 mmu_parent_walk_fn fn)
890{
891 struct kvm_pte_chain *pte_chain;
892 struct hlist_node *node;
893 struct kvm_mmu_page *parent_sp;
894 int i;
895
896 if (!sp->multimapped && sp->parent_pte) {
897 parent_sp = page_header(__pa(sp->parent_pte));
898 fn(vcpu, parent_sp);
899 mmu_parent_walk(vcpu, parent_sp, fn);
900 return;
901 }
902 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
903 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
904 if (!pte_chain->parent_ptes[i])
905 break;
906 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
907 fn(vcpu, parent_sp);
908 mmu_parent_walk(vcpu, parent_sp, fn);
909 }
910}
911
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300912static void kvm_mmu_update_unsync_bitmap(u64 *spte)
913{
914 unsigned int index;
915 struct kvm_mmu_page *sp = page_header(__pa(spte));
916
917 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200918 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
919 sp->unsync_children++;
920 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300921}
922
923static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
924{
925 struct kvm_pte_chain *pte_chain;
926 struct hlist_node *node;
927 int i;
928
929 if (!sp->parent_pte)
930 return;
931
932 if (!sp->multimapped) {
933 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
934 return;
935 }
936
937 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
938 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
939 if (!pte_chain->parent_ptes[i])
940 break;
941 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
942 }
943}
944
945static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
946{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300947 kvm_mmu_update_parents_unsync(sp);
948 return 1;
949}
950
951static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
952 struct kvm_mmu_page *sp)
953{
954 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
955 kvm_mmu_update_parents_unsync(sp);
956}
957
Avi Kivityd761a502008-05-29 14:55:03 +0300958static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
959 struct kvm_mmu_page *sp)
960{
961 int i;
962
963 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
964 sp->spt[i] = shadow_trap_nonpresent_pte;
965}
966
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300967static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
968 struct kvm_mmu_page *sp)
969{
970 return 1;
971}
972
Marcelo Tosattia7052892008-09-23 13:18:35 -0300973static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
974{
975}
976
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200977#define KVM_PAGE_ARRAY_NR 16
978
979struct kvm_mmu_pages {
980 struct mmu_page_and_offset {
981 struct kvm_mmu_page *sp;
982 unsigned int idx;
983 } page[KVM_PAGE_ARRAY_NR];
984 unsigned int nr;
985};
986
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300987#define for_each_unsync_children(bitmap, idx) \
988 for (idx = find_first_bit(bitmap, 512); \
989 idx < 512; \
990 idx = find_next_bit(bitmap, 512, idx+1))
991
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200992int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
993 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300994{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200995 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300996
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200997 if (sp->unsync)
998 for (i=0; i < pvec->nr; i++)
999 if (pvec->page[i].sp == sp)
1000 return 0;
1001
1002 pvec->page[pvec->nr].sp = sp;
1003 pvec->page[pvec->nr].idx = idx;
1004 pvec->nr++;
1005 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1006}
1007
1008static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1009 struct kvm_mmu_pages *pvec)
1010{
1011 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001012
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001013 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001014 u64 ent = sp->spt[i];
1015
Marcelo Tosatti87917232008-12-22 18:49:30 -02001016 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001017 struct kvm_mmu_page *child;
1018 child = page_header(ent & PT64_BASE_ADDR_MASK);
1019
1020 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001021 if (mmu_pages_add(pvec, child, i))
1022 return -ENOSPC;
1023
1024 ret = __mmu_unsync_walk(child, pvec);
1025 if (!ret)
1026 __clear_bit(i, sp->unsync_child_bitmap);
1027 else if (ret > 0)
1028 nr_unsync_leaf += ret;
1029 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001030 return ret;
1031 }
1032
1033 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001034 nr_unsync_leaf++;
1035 if (mmu_pages_add(pvec, child, i))
1036 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001037 }
1038 }
1039 }
1040
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001041 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001042 sp->unsync_children = 0;
1043
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001044 return nr_unsync_leaf;
1045}
1046
1047static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1048 struct kvm_mmu_pages *pvec)
1049{
1050 if (!sp->unsync_children)
1051 return 0;
1052
1053 mmu_pages_add(pvec, sp, 0);
1054 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001055}
1056
Avi Kivity4db35312007-11-21 15:28:32 +02001057static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001058{
1059 unsigned index;
1060 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001061 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001062 struct hlist_node *node;
1063
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001064 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001065 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001066 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001067 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001068 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001069 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001070 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001071 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001072 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001073 }
1074 return NULL;
1075}
1076
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001077static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1078{
1079 list_del(&sp->oos_link);
1080 --kvm->stat.mmu_unsync_global;
1081}
1082
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001083static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1084{
1085 WARN_ON(!sp->unsync);
1086 sp->unsync = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001087 if (sp->global)
1088 kvm_unlink_unsync_global(kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001089 --kvm->stat.mmu_unsync;
1090}
1091
1092static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1093
1094static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1095{
1096 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1097 kvm_mmu_zap_page(vcpu->kvm, sp);
1098 return 1;
1099 }
1100
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001101 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1102 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001103 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001104 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1105 kvm_mmu_zap_page(vcpu->kvm, sp);
1106 return 1;
1107 }
1108
1109 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001110 return 0;
1111}
1112
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001113struct mmu_page_path {
1114 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1115 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001116};
1117
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001118#define for_each_sp(pvec, sp, parents, i) \
1119 for (i = mmu_pages_next(&pvec, &parents, -1), \
1120 sp = pvec.page[i].sp; \
1121 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1122 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001123
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001124int mmu_pages_next(struct kvm_mmu_pages *pvec, struct mmu_page_path *parents,
1125 int i)
1126{
1127 int n;
1128
1129 for (n = i+1; n < pvec->nr; n++) {
1130 struct kvm_mmu_page *sp = pvec->page[n].sp;
1131
1132 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1133 parents->idx[0] = pvec->page[n].idx;
1134 return n;
1135 }
1136
1137 parents->parent[sp->role.level-2] = sp;
1138 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1139 }
1140
1141 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001142}
1143
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001144void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001145{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001146 struct kvm_mmu_page *sp;
1147 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001148
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001149 do {
1150 unsigned int idx = parents->idx[level];
1151
1152 sp = parents->parent[level];
1153 if (!sp)
1154 return;
1155
1156 --sp->unsync_children;
1157 WARN_ON((int)sp->unsync_children < 0);
1158 __clear_bit(idx, sp->unsync_child_bitmap);
1159 level++;
1160 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1161}
1162
1163static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1164 struct mmu_page_path *parents,
1165 struct kvm_mmu_pages *pvec)
1166{
1167 parents->parent[parent->role.level-1] = NULL;
1168 pvec->nr = 0;
1169}
1170
1171static void mmu_sync_children(struct kvm_vcpu *vcpu,
1172 struct kvm_mmu_page *parent)
1173{
1174 int i;
1175 struct kvm_mmu_page *sp;
1176 struct mmu_page_path parents;
1177 struct kvm_mmu_pages pages;
1178
1179 kvm_mmu_pages_init(parent, &parents, &pages);
1180 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001181 int protected = 0;
1182
1183 for_each_sp(pages, sp, parents, i)
1184 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1185
1186 if (protected)
1187 kvm_flush_remote_tlbs(vcpu->kvm);
1188
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001189 for_each_sp(pages, sp, parents, i) {
1190 kvm_sync_page(vcpu, sp);
1191 mmu_pages_clear_parents(&parents);
1192 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001194 kvm_mmu_pages_init(parent, &parents, &pages);
1195 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001196}
1197
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001198static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1199 gfn_t gfn,
1200 gva_t gaddr,
1201 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001202 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001203 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001204 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001205{
1206 union kvm_mmu_page_role role;
1207 unsigned index;
1208 unsigned quadrant;
1209 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001210 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001211 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001212
Avi Kivitya770f6f2008-12-21 19:20:09 +02001213 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001214 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001215 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001216 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001217 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001218 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1219 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1220 role.quadrant = quadrant;
1221 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001222 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001223 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001224 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001225 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001226 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1227 if (sp->gfn == gfn) {
1228 if (sp->unsync)
1229 if (kvm_sync_page(vcpu, sp))
1230 continue;
1231
1232 if (sp->role.word != role.word)
1233 continue;
1234
Avi Kivity4db35312007-11-21 15:28:32 +02001235 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001236 if (sp->unsync_children) {
1237 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1238 kvm_mmu_mark_parents_unsync(vcpu, sp);
1239 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001240 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001241 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001242 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001243 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001244 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1245 if (!sp)
1246 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001247 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001248 sp->gfn = gfn;
1249 sp->role = role;
Avi Kivitye2078312008-12-21 19:36:59 +02001250 sp->global = role.cr4_pge;
Avi Kivity4db35312007-11-21 15:28:32 +02001251 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001252 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001253 if (rmap_write_protect(vcpu->kvm, gfn))
1254 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001255 account_shadowed(vcpu->kvm, gfn);
1256 }
Avi Kivity131d8272008-05-29 14:56:28 +03001257 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1258 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1259 else
1260 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001261 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001262}
1263
Avi Kivity2d111232008-12-25 14:39:47 +02001264static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1265 struct kvm_vcpu *vcpu, u64 addr)
1266{
1267 iterator->addr = addr;
1268 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1269 iterator->level = vcpu->arch.mmu.shadow_root_level;
1270 if (iterator->level == PT32E_ROOT_LEVEL) {
1271 iterator->shadow_addr
1272 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1273 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1274 --iterator->level;
1275 if (!iterator->shadow_addr)
1276 iterator->level = 0;
1277 }
1278}
1279
1280static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1281{
1282 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1283 return false;
1284 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1285 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1286 return true;
1287}
1288
1289static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1290{
1291 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1292 --iterator->level;
1293}
1294
Avi Kivity90cb0522007-07-17 13:04:56 +03001295static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001296 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001297{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001298 unsigned i;
1299 u64 *pt;
1300 u64 ent;
1301
Avi Kivity4db35312007-11-21 15:28:32 +02001302 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001303
Avi Kivity4db35312007-11-21 15:28:32 +02001304 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001305 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001306 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001307 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001308 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001309 }
1310 return;
1311 }
1312
1313 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1314 ent = pt[i];
1315
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001316 if (is_shadow_present_pte(ent)) {
1317 if (!is_large_pte(ent)) {
1318 ent &= PT64_BASE_ADDR_MASK;
1319 mmu_page_remove_parent_pte(page_header(ent),
1320 &pt[i]);
1321 } else {
1322 --kvm->stat.lpages;
1323 rmap_remove(kvm, &pt[i]);
1324 }
1325 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001326 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001327 }
Avi Kivitya4360362007-01-05 16:36:45 -08001328}
1329
Avi Kivity4db35312007-11-21 15:28:32 +02001330static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001331{
Avi Kivity4db35312007-11-21 15:28:32 +02001332 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001333}
1334
Avi Kivity12b7d282007-09-23 14:10:49 +02001335static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1336{
1337 int i;
1338
1339 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1340 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001341 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001342}
1343
Avi Kivity31aa2b42008-07-11 17:59:46 +03001344static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001345{
1346 u64 *parent_pte;
1347
Avi Kivity4db35312007-11-21 15:28:32 +02001348 while (sp->multimapped || sp->parent_pte) {
1349 if (!sp->multimapped)
1350 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001351 else {
1352 struct kvm_pte_chain *chain;
1353
Avi Kivity4db35312007-11-21 15:28:32 +02001354 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001355 struct kvm_pte_chain, link);
1356 parent_pte = chain->parent_ptes[0];
1357 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001358 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001359 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001360 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001361 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001362}
1363
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001364static int mmu_zap_unsync_children(struct kvm *kvm,
1365 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001366{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001367 int i, zapped = 0;
1368 struct mmu_page_path parents;
1369 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001370
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001371 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001372 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001373
1374 kvm_mmu_pages_init(parent, &parents, &pages);
1375 while (mmu_unsync_walk(parent, &pages)) {
1376 struct kvm_mmu_page *sp;
1377
1378 for_each_sp(pages, sp, parents, i) {
1379 kvm_mmu_zap_page(kvm, sp);
1380 mmu_pages_clear_parents(&parents);
1381 }
1382 zapped += pages.nr;
1383 kvm_mmu_pages_init(parent, &parents, &pages);
1384 }
1385
1386 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001387}
1388
Marcelo Tosatti07385412008-09-23 13:18:37 -03001389static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001390{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001391 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001392 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001393 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001394 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001395 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001396 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001397 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001398 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001399 if (sp->unsync)
1400 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001401 if (!sp->root_count) {
1402 hlist_del(&sp->hash_link);
1403 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001404 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001405 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001406 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001407 kvm_reload_remote_mmus(kvm);
1408 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001409 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001410 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001411}
1412
Izik Eidus82ce2c92007-10-02 18:52:55 +02001413/*
1414 * Changing the number of mmu pages allocated to the vm
1415 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1416 */
1417void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1418{
1419 /*
1420 * If we set the number of mmu pages to be smaller be than the
1421 * number of actived pages , we must to free some mmu pages before we
1422 * change the value
1423 */
1424
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001425 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001426 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001427 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1428 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001429
1430 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1431 struct kvm_mmu_page *page;
1432
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001433 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001434 struct kvm_mmu_page, link);
1435 kvm_mmu_zap_page(kvm, page);
1436 n_used_mmu_pages--;
1437 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001438 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001439 }
1440 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001441 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1442 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001443
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001444 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001445}
1446
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001447static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001448{
1449 unsigned index;
1450 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001451 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001452 struct hlist_node *node, *n;
1453 int r;
1454
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001455 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001456 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001457 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001458 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001459 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001460 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001461 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001462 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001463 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001464 if (kvm_mmu_zap_page(kvm, sp))
1465 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001466 }
1467 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001468}
1469
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001470static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001471{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001472 unsigned index;
1473 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001474 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001475 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001476
Avi Kivity4677a3b2009-01-06 13:00:27 +02001477 index = kvm_page_table_hashfn(gfn);
1478 bucket = &kvm->arch.mmu_page_hash[index];
1479 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001480 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001481 && !sp->role.invalid) {
1482 pgprintk("%s: zap %lx %x\n",
1483 __func__, gfn, sp->role.word);
1484 kvm_mmu_zap_page(kvm, sp);
1485 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001486 }
1487}
1488
Avi Kivity38c335f2007-11-21 14:20:22 +02001489static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490{
Avi Kivity38c335f2007-11-21 14:20:22 +02001491 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001492 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493
Sheng Yang291f26b2008-10-16 17:30:57 +08001494 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495}
1496
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001497static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1498{
1499 int i;
1500 u64 *pt = sp->spt;
1501
1502 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1503 return;
1504
1505 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1506 if (pt[i] == shadow_notrap_nonpresent_pte)
1507 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1508 }
1509}
1510
Avi Kivity039576c2007-03-20 12:46:50 +02001511struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1512{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001513 struct page *page;
1514
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001515 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001516
1517 if (gpa == UNMAPPED_GVA)
1518 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001519
Izik Eidus72dc67a2008-02-10 18:04:15 +02001520 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001521
1522 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001523}
1524
Sheng Yang74be52e2008-10-09 16:01:56 +08001525/*
1526 * The function is based on mtrr_type_lookup() in
1527 * arch/x86/kernel/cpu/mtrr/generic.c
1528 */
1529static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1530 u64 start, u64 end)
1531{
1532 int i;
1533 u64 base, mask;
1534 u8 prev_match, curr_match;
1535 int num_var_ranges = KVM_NR_VAR_MTRR;
1536
1537 if (!mtrr_state->enabled)
1538 return 0xFF;
1539
1540 /* Make end inclusive end, instead of exclusive */
1541 end--;
1542
1543 /* Look in fixed ranges. Just return the type as per start */
1544 if (mtrr_state->have_fixed && (start < 0x100000)) {
1545 int idx;
1546
1547 if (start < 0x80000) {
1548 idx = 0;
1549 idx += (start >> 16);
1550 return mtrr_state->fixed_ranges[idx];
1551 } else if (start < 0xC0000) {
1552 idx = 1 * 8;
1553 idx += ((start - 0x80000) >> 14);
1554 return mtrr_state->fixed_ranges[idx];
1555 } else if (start < 0x1000000) {
1556 idx = 3 * 8;
1557 idx += ((start - 0xC0000) >> 12);
1558 return mtrr_state->fixed_ranges[idx];
1559 }
1560 }
1561
1562 /*
1563 * Look in variable ranges
1564 * Look of multiple ranges matching this address and pick type
1565 * as per MTRR precedence
1566 */
1567 if (!(mtrr_state->enabled & 2))
1568 return mtrr_state->def_type;
1569
1570 prev_match = 0xFF;
1571 for (i = 0; i < num_var_ranges; ++i) {
1572 unsigned short start_state, end_state;
1573
1574 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1575 continue;
1576
1577 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1578 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1579 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1580 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1581
1582 start_state = ((start & mask) == (base & mask));
1583 end_state = ((end & mask) == (base & mask));
1584 if (start_state != end_state)
1585 return 0xFE;
1586
1587 if ((start & mask) != (base & mask))
1588 continue;
1589
1590 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1591 if (prev_match == 0xFF) {
1592 prev_match = curr_match;
1593 continue;
1594 }
1595
1596 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1597 curr_match == MTRR_TYPE_UNCACHABLE)
1598 return MTRR_TYPE_UNCACHABLE;
1599
1600 if ((prev_match == MTRR_TYPE_WRBACK &&
1601 curr_match == MTRR_TYPE_WRTHROUGH) ||
1602 (prev_match == MTRR_TYPE_WRTHROUGH &&
1603 curr_match == MTRR_TYPE_WRBACK)) {
1604 prev_match = MTRR_TYPE_WRTHROUGH;
1605 curr_match = MTRR_TYPE_WRTHROUGH;
1606 }
1607
1608 if (prev_match != curr_match)
1609 return MTRR_TYPE_UNCACHABLE;
1610 }
1611
1612 if (prev_match != 0xFF)
1613 return prev_match;
1614
1615 return mtrr_state->def_type;
1616}
1617
1618static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1619{
1620 u8 mtrr;
1621
1622 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1623 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1624 if (mtrr == 0xfe || mtrr == 0xff)
1625 mtrr = MTRR_TYPE_WRBACK;
1626 return mtrr;
1627}
1628
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001629static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1630{
1631 unsigned index;
1632 struct hlist_head *bucket;
1633 struct kvm_mmu_page *s;
1634 struct hlist_node *node, *n;
1635
1636 index = kvm_page_table_hashfn(sp->gfn);
1637 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1638 /* don't unsync if pagetable is shadowed with multiple roles */
1639 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001640 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001641 continue;
1642 if (s->role.word != sp->role.word)
1643 return 1;
1644 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001645 ++vcpu->kvm->stat.mmu_unsync;
1646 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001647
1648 if (sp->global) {
1649 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1650 ++vcpu->kvm->stat.mmu_unsync_global;
1651 } else
1652 kvm_mmu_mark_parents_unsync(vcpu, sp);
1653
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001654 mmu_convert_notrap(sp);
1655 return 0;
1656}
1657
1658static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1659 bool can_unsync)
1660{
1661 struct kvm_mmu_page *shadow;
1662
1663 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1664 if (shadow) {
1665 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1666 return 1;
1667 if (shadow->unsync)
1668 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001669 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001670 return kvm_unsync_page(vcpu, shadow);
1671 return 1;
1672 }
1673 return 0;
1674}
1675
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001676static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1677 unsigned pte_access, int user_fault,
1678 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001679 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001680 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001681{
1682 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001683 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001684 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001685 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1686
1687 if (!global && sp->global) {
1688 sp->global = 0;
1689 if (sp->unsync) {
1690 kvm_unlink_unsync_global(vcpu->kvm, sp);
1691 kvm_mmu_mark_parents_unsync(vcpu, sp);
1692 }
1693 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001694
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001695 /*
1696 * We don't set the accessed bit, since we sometimes want to see
1697 * whether the guest actually used the pte (in order to detect
1698 * demand paging).
1699 */
Sheng Yang7b523452008-04-25 21:13:50 +08001700 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001701 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001702 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001703 if (!dirty)
1704 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001705 if (pte_access & ACC_EXEC_MASK)
1706 spte |= shadow_x_mask;
1707 else
1708 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001709 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001710 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001711 if (largepage)
1712 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001713 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001714 if (!kvm_is_mmio_pfn(pfn)) {
1715 mt_mask = get_memory_type(vcpu, gfn) <<
1716 kvm_x86_ops->get_mt_mask_shift();
1717 mt_mask |= VMX_EPT_IGMT_BIT;
1718 } else
1719 mt_mask = MTRR_TYPE_UNCACHABLE <<
1720 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001721 spte |= mt_mask;
1722 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001723
Anthony Liguori35149e22008-04-02 14:46:56 -05001724 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001725
1726 if ((pte_access & ACC_WRITE_MASK)
1727 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001728
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001729 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1730 ret = 1;
1731 spte = shadow_trap_nonpresent_pte;
1732 goto set_pte;
1733 }
1734
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001735 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001736
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001737 /*
1738 * Optimization: for pte sync, if spte was writable the hash
1739 * lookup is unnecessary (and expensive). Write protection
1740 * is responsibility of mmu_get_page / kvm_sync_page.
1741 * Same reasoning can be applied to dirty page accounting.
1742 */
1743 if (!can_unsync && is_writeble_pte(*shadow_pte))
1744 goto set_pte;
1745
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001746 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001747 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001748 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001749 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001750 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001751 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001752 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001753 }
1754 }
1755
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001756 if (pte_access & ACC_WRITE_MASK)
1757 mark_page_dirty(vcpu->kvm, gfn);
1758
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001759set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001760 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001761 return ret;
1762}
1763
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001764static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1765 unsigned pt_access, unsigned pte_access,
1766 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001767 int *ptwrite, int largepage, int global,
1768 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001769{
1770 int was_rmapped = 0;
1771 int was_writeble = is_writeble_pte(*shadow_pte);
1772
1773 pgprintk("%s: spte %llx access %x write_fault %d"
1774 " user_fault %d gfn %lx\n",
1775 __func__, *shadow_pte, pt_access,
1776 write_fault, user_fault, gfn);
1777
1778 if (is_rmap_pte(*shadow_pte)) {
1779 /*
1780 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1781 * the parent of the now unreachable PTE.
1782 */
1783 if (largepage && !is_large_pte(*shadow_pte)) {
1784 struct kvm_mmu_page *child;
1785 u64 pte = *shadow_pte;
1786
1787 child = page_header(pte & PT64_BASE_ADDR_MASK);
1788 mmu_page_remove_parent_pte(child, shadow_pte);
1789 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1790 pgprintk("hfn old %lx new %lx\n",
1791 spte_to_pfn(*shadow_pte), pfn);
1792 rmap_remove(vcpu->kvm, shadow_pte);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001793 } else
1794 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001795 }
1796 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001797 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001798 if (write_fault)
1799 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001800 kvm_x86_ops->tlb_flush(vcpu);
1801 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001802
1803 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1804 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1805 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1806 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1807 *shadow_pte, shadow_pte);
1808 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001809 ++vcpu->kvm->stat.lpages;
1810
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001811 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1812 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001813 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001814 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001815 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001816 } else {
1817 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001818 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001819 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001820 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001821 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001822 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001823 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001824 vcpu->arch.last_pte_gfn = gfn;
1825 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001826}
1827
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1829{
1830}
1831
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001832static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001833 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834{
Avi Kivity9f652d22008-12-25 14:54:25 +02001835 struct kvm_shadow_walk_iterator iterator;
1836 struct kvm_mmu_page *sp;
1837 int pt_write = 0;
1838 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839
Avi Kivity9f652d22008-12-25 14:54:25 +02001840 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1841 if (iterator.level == PT_PAGE_TABLE_LEVEL
1842 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1843 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1844 0, write, 1, &pt_write,
1845 largepage, 0, gfn, pfn, false);
1846 ++vcpu->stat.pf_fixed;
1847 break;
1848 }
1849
1850 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1851 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1852 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1853 iterator.level - 1,
1854 1, ACC_ALL, iterator.sptep);
1855 if (!sp) {
1856 pgprintk("nonpaging_map: ENOMEM\n");
1857 kvm_release_pfn_clean(pfn);
1858 return -ENOMEM;
1859 }
1860
1861 set_shadow_pte(iterator.sptep,
1862 __pa(sp->spt)
1863 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1864 | shadow_user_mask | shadow_x_mask);
1865 }
1866 }
1867 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868}
1869
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001870static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1871{
1872 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001873 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001874 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001875 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001876
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001877 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1878 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1879 largepage = 1;
1880 }
1881
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001882 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001883 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001884 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001885
Avi Kivityd196e342008-01-24 11:44:11 +02001886 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001887 if (is_error_pfn(pfn)) {
1888 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001889 return 1;
1890 }
1891
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001892 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001893 if (mmu_notifier_retry(vcpu, mmu_seq))
1894 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001895 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001896 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001897 spin_unlock(&vcpu->kvm->mmu_lock);
1898
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001899
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001900 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001901
1902out_unlock:
1903 spin_unlock(&vcpu->kvm->mmu_lock);
1904 kvm_release_pfn_clean(pfn);
1905 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001906}
1907
1908
Avi Kivity17ac10a2007-01-05 16:36:40 -08001909static void mmu_free_roots(struct kvm_vcpu *vcpu)
1910{
1911 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001912 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001913
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001914 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001915 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001916 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001917 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1918 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001919
Avi Kivity4db35312007-11-21 15:28:32 +02001920 sp = page_header(root);
1921 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001922 if (!sp->root_count && sp->role.invalid)
1923 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001924 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001925 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001926 return;
1927 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001928 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001929 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001930
Avi Kivity417726a2007-04-12 17:35:58 +03001931 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001932 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001933 sp = page_header(root);
1934 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001935 if (!sp->root_count && sp->role.invalid)
1936 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001937 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001938 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001939 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001940 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001941 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001942}
1943
1944static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1945{
1946 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001947 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001948 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001949 int direct = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001950
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001951 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001952
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001953 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1954 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001955
1956 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001957 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001958 direct = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001959 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001960 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001961 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001962 root = __pa(sp->spt);
1963 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001964 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001965 return;
1966 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001967 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001968 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001969 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001970 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001971 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001972
1973 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001974 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1975 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1976 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001977 continue;
1978 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001979 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1980 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001981 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001982 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001983 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001984 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001985 root = __pa(sp->spt);
1986 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001987 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001988 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001989 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001990}
1991
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001992static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1993{
1994 int i;
1995 struct kvm_mmu_page *sp;
1996
1997 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1998 return;
1999 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2000 hpa_t root = vcpu->arch.mmu.root_hpa;
2001 sp = page_header(root);
2002 mmu_sync_children(vcpu, sp);
2003 return;
2004 }
2005 for (i = 0; i < 4; ++i) {
2006 hpa_t root = vcpu->arch.mmu.pae_root[i];
2007
2008 if (root) {
2009 root &= PT64_BASE_ADDR_MASK;
2010 sp = page_header(root);
2011 mmu_sync_children(vcpu, sp);
2012 }
2013 }
2014}
2015
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002016static void mmu_sync_global(struct kvm_vcpu *vcpu)
2017{
2018 struct kvm *kvm = vcpu->kvm;
2019 struct kvm_mmu_page *sp, *n;
2020
2021 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2022 kvm_sync_page(vcpu, sp);
2023}
2024
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002025void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2026{
2027 spin_lock(&vcpu->kvm->mmu_lock);
2028 mmu_sync_roots(vcpu);
2029 spin_unlock(&vcpu->kvm->mmu_lock);
2030}
2031
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002032void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2033{
2034 spin_lock(&vcpu->kvm->mmu_lock);
2035 mmu_sync_global(vcpu);
2036 spin_unlock(&vcpu->kvm->mmu_lock);
2037}
2038
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2040{
2041 return vaddr;
2042}
2043
2044static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002045 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046{
Avi Kivitye8332402007-12-09 18:43:00 +02002047 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002048 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002050 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002051 r = mmu_topup_memory_caches(vcpu);
2052 if (r)
2053 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002054
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002056 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057
Avi Kivitye8332402007-12-09 18:43:00 +02002058 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059
Avi Kivitye8332402007-12-09 18:43:00 +02002060 return nonpaging_map(vcpu, gva & PAGE_MASK,
2061 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062}
2063
Joerg Roedelfb72d162008-02-07 13:47:44 +01002064static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2065 u32 error_code)
2066{
Anthony Liguori35149e22008-04-02 14:46:56 -05002067 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002068 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002069 int largepage = 0;
2070 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002071 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002072
2073 ASSERT(vcpu);
2074 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2075
2076 r = mmu_topup_memory_caches(vcpu);
2077 if (r)
2078 return r;
2079
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002080 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2081 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2082 largepage = 1;
2083 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002084 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002085 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002086 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002087 if (is_error_pfn(pfn)) {
2088 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002089 return 1;
2090 }
2091 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002092 if (mmu_notifier_retry(vcpu, mmu_seq))
2093 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002094 kvm_mmu_free_some_pages(vcpu);
2095 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002096 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002097 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002098
2099 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002100
2101out_unlock:
2102 spin_unlock(&vcpu->kvm->mmu_lock);
2103 kvm_release_pfn_clean(pfn);
2104 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002105}
2106
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107static void nonpaging_free(struct kvm_vcpu *vcpu)
2108{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002109 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110}
2111
2112static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2113{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002114 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
2116 context->new_cr3 = nonpaging_new_cr3;
2117 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 context->gva_to_gpa = nonpaging_gva_to_gpa;
2119 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002120 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002121 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002122 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002123 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002125 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 return 0;
2127}
2128
Avi Kivityd835dfe2007-11-21 02:57:59 +02002129void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002131 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002132 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133}
2134
2135static void paging_new_cr3(struct kvm_vcpu *vcpu)
2136{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002137 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002138 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139}
2140
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141static void inject_page_fault(struct kvm_vcpu *vcpu,
2142 u64 addr,
2143 u32 err_code)
2144{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002145 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146}
2147
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148static void paging_free(struct kvm_vcpu *vcpu)
2149{
2150 nonpaging_free(vcpu);
2151}
2152
2153#define PTTYPE 64
2154#include "paging_tmpl.h"
2155#undef PTTYPE
2156
2157#define PTTYPE 32
2158#include "paging_tmpl.h"
2159#undef PTTYPE
2160
Avi Kivity17ac10a2007-01-05 16:36:40 -08002161static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002163 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164
2165 ASSERT(is_pae(vcpu));
2166 context->new_cr3 = paging_new_cr3;
2167 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002169 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002170 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002171 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002173 context->root_level = level;
2174 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002175 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176 return 0;
2177}
2178
Avi Kivity17ac10a2007-01-05 16:36:40 -08002179static int paging64_init_context(struct kvm_vcpu *vcpu)
2180{
2181 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2182}
2183
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184static int paging32_init_context(struct kvm_vcpu *vcpu)
2185{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002186 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187
2188 context->new_cr3 = paging_new_cr3;
2189 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190 context->gva_to_gpa = paging32_gva_to_gpa;
2191 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002192 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002193 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002194 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 context->root_level = PT32_ROOT_LEVEL;
2196 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002197 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 return 0;
2199}
2200
2201static int paging32E_init_context(struct kvm_vcpu *vcpu)
2202{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002203 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204}
2205
Joerg Roedelfb72d162008-02-07 13:47:44 +01002206static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2207{
2208 struct kvm_mmu *context = &vcpu->arch.mmu;
2209
2210 context->new_cr3 = nonpaging_new_cr3;
2211 context->page_fault = tdp_page_fault;
2212 context->free = nonpaging_free;
2213 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002214 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002215 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002216 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002217 context->root_hpa = INVALID_PAGE;
2218
2219 if (!is_paging(vcpu)) {
2220 context->gva_to_gpa = nonpaging_gva_to_gpa;
2221 context->root_level = 0;
2222 } else if (is_long_mode(vcpu)) {
2223 context->gva_to_gpa = paging64_gva_to_gpa;
2224 context->root_level = PT64_ROOT_LEVEL;
2225 } else if (is_pae(vcpu)) {
2226 context->gva_to_gpa = paging64_gva_to_gpa;
2227 context->root_level = PT32E_ROOT_LEVEL;
2228 } else {
2229 context->gva_to_gpa = paging32_gva_to_gpa;
2230 context->root_level = PT32_ROOT_LEVEL;
2231 }
2232
2233 return 0;
2234}
2235
2236static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002238 int r;
2239
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002241 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242
2243 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002244 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002245 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002246 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002248 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002250 r = paging32_init_context(vcpu);
2251
2252 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2253
2254 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255}
2256
Joerg Roedelfb72d162008-02-07 13:47:44 +01002257static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2258{
Anthony Liguori35149e22008-04-02 14:46:56 -05002259 vcpu->arch.update_pte.pfn = bad_pfn;
2260
Joerg Roedelfb72d162008-02-07 13:47:44 +01002261 if (tdp_enabled)
2262 return init_kvm_tdp_mmu(vcpu);
2263 else
2264 return init_kvm_softmmu(vcpu);
2265}
2266
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2268{
2269 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002270 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2271 vcpu->arch.mmu.free(vcpu);
2272 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 }
2274}
2275
2276int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2277{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002278 destroy_kvm_mmu(vcpu);
2279 return init_kvm_mmu(vcpu);
2280}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002281EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002282
2283int kvm_mmu_load(struct kvm_vcpu *vcpu)
2284{
Avi Kivity714b93d2007-01-05 16:36:53 -08002285 int r;
2286
Avi Kivitye2dec932007-01-05 16:36:54 -08002287 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002288 if (r)
2289 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002290 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002291 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002292 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002293 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002294 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002295 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002296 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002297out:
2298 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002300EXPORT_SYMBOL_GPL(kvm_mmu_load);
2301
2302void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2303{
2304 mmu_free_roots(vcpu);
2305}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306
Avi Kivity09072da2007-05-01 14:16:52 +03002307static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002308 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002309 u64 *spte)
2310{
2311 u64 pte;
2312 struct kvm_mmu_page *child;
2313
2314 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002315 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002316 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2317 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002318 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002319 else {
2320 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002321 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002322 }
2323 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002324 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002325 if (is_large_pte(pte))
2326 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002327}
2328
Avi Kivity00284252007-05-01 16:53:31 +03002329static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002330 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002331 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002332 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002333{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002334 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2335 if (!vcpu->arch.update_pte.largepage ||
2336 sp->role.glevels == PT32_ROOT_LEVEL) {
2337 ++vcpu->kvm->stat.mmu_pde_zapped;
2338 return;
2339 }
2340 }
Avi Kivity00284252007-05-01 16:53:31 +03002341
Avi Kivity4cee5762007-11-18 16:37:07 +02002342 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002343 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002344 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002345 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002346 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002347}
2348
Avi Kivity79539ce2007-11-21 02:06:21 +02002349static bool need_remote_flush(u64 old, u64 new)
2350{
2351 if (!is_shadow_present_pte(old))
2352 return false;
2353 if (!is_shadow_present_pte(new))
2354 return true;
2355 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2356 return true;
2357 old ^= PT64_NX_MASK;
2358 new ^= PT64_NX_MASK;
2359 return (old & ~new & PT64_PERM_MASK) != 0;
2360}
2361
2362static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2363{
2364 if (need_remote_flush(old, new))
2365 kvm_flush_remote_tlbs(vcpu->kvm);
2366 else
2367 kvm_mmu_flush_tlb(vcpu);
2368}
2369
Avi Kivity12b7d282007-09-23 14:10:49 +02002370static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2371{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002372 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002373
Sheng Yang7b523452008-04-25 21:13:50 +08002374 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002375}
2376
Avi Kivityd7824ff2007-12-30 12:29:05 +02002377static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2378 const u8 *new, int bytes)
2379{
2380 gfn_t gfn;
2381 int r;
2382 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002383 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002384
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002385 vcpu->arch.update_pte.largepage = 0;
2386
Avi Kivityd7824ff2007-12-30 12:29:05 +02002387 if (bytes != 4 && bytes != 8)
2388 return;
2389
2390 /*
2391 * Assume that the pte write on a page table of the same type
2392 * as the current vcpu paging mode. This is nearly always true
2393 * (might be false while changing modes). Note it is verified later
2394 * by update_pte().
2395 */
2396 if (is_pae(vcpu)) {
2397 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2398 if ((bytes == 4) && (gpa % 4 == 0)) {
2399 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2400 if (r)
2401 return;
2402 memcpy((void *)&gpte + (gpa % 8), new, 4);
2403 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2404 memcpy((void *)&gpte, new, 8);
2405 }
2406 } else {
2407 if ((bytes == 4) && (gpa % 4 == 0))
2408 memcpy((void *)&gpte, new, 4);
2409 }
2410 if (!is_present_pte(gpte))
2411 return;
2412 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002413
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002414 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2415 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2416 vcpu->arch.update_pte.largepage = 1;
2417 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002418 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002419 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002420 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002421
Anthony Liguori35149e22008-04-02 14:46:56 -05002422 if (is_error_pfn(pfn)) {
2423 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002424 return;
2425 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002426 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002427 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002428}
2429
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002430static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2431{
2432 u64 *spte = vcpu->arch.last_pte_updated;
2433
2434 if (spte
2435 && vcpu->arch.last_pte_gfn == gfn
2436 && shadow_accessed_mask
2437 && !(*spte & shadow_accessed_mask)
2438 && is_shadow_present_pte(*spte))
2439 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2440}
2441
Avi Kivity09072da2007-05-01 14:16:52 +03002442void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002443 const u8 *new, int bytes,
2444 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002445{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002446 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002447 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002448 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002449 struct hlist_head *bucket;
2450 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002451 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002452 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002453 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002454 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002455 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002456 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002457 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002458 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002459 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002460 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002461 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002462
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002463 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002464 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002465 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002466 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002467 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002468 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002469 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002470 if (guest_initiated) {
2471 if (gfn == vcpu->arch.last_pt_write_gfn
2472 && !last_updated_pte_accessed(vcpu)) {
2473 ++vcpu->arch.last_pt_write_count;
2474 if (vcpu->arch.last_pt_write_count >= 3)
2475 flooded = 1;
2476 } else {
2477 vcpu->arch.last_pt_write_gfn = gfn;
2478 vcpu->arch.last_pt_write_count = 1;
2479 vcpu->arch.last_pte_updated = NULL;
2480 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002481 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002482 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002483 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002484 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002485 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002486 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002487 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002488 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002489 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002490 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002491 /*
2492 * Misaligned accesses are too much trouble to fix
2493 * up; also, they usually indicate a page is not used
2494 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002495 *
2496 * If we're seeing too many writes to a page,
2497 * it may no longer be a page table, or we may be
2498 * forking, in which case it is better to unmap the
2499 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002500 */
2501 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002502 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002503 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2504 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002505 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002506 continue;
2507 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002508 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002509 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002510 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002511 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002512 page_offset <<= 1; /* 32->64 */
2513 /*
2514 * A 32-bit pde maps 4MB while the shadow pdes map
2515 * only 2MB. So we need to double the offset again
2516 * and zap two pdes instead of one.
2517 */
2518 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002519 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002520 page_offset <<= 1;
2521 npte = 2;
2522 }
Avi Kivityfce06572007-05-01 16:44:05 +03002523 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002524 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002525 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002526 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002527 }
Avi Kivity4db35312007-11-21 15:28:32 +02002528 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002529 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2530 gentry = 0;
2531 r = kvm_read_guest_atomic(vcpu->kvm,
2532 gpa & ~(u64)(pte_size - 1),
2533 &gentry, pte_size);
2534 new = (const void *)&gentry;
2535 if (r < 0)
2536 new = NULL;
2537 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002538 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002539 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002540 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002541 if (new)
2542 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002543 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002544 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002545 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002546 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002547 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002548 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002549 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2550 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2551 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002552 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002553}
2554
Avi Kivitya4360362007-01-05 16:36:45 -08002555int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2556{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002557 gpa_t gpa;
2558 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002559
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002560 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002561
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002562 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002563 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002564 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002565 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002566}
Avi Kivity577bdc42008-07-19 08:57:05 +03002567EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002568
Avi Kivity22d95b12007-09-14 20:26:06 +03002569void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002570{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002571 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002572 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002573
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002574 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002575 struct kvm_mmu_page, link);
2576 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002577 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002578 }
2579}
Avi Kivityebeace82007-01-05 16:36:47 -08002580
Avi Kivity30677142007-10-28 18:48:59 +02002581int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2582{
2583 int r;
2584 enum emulation_result er;
2585
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002586 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002587 if (r < 0)
2588 goto out;
2589
2590 if (!r) {
2591 r = 1;
2592 goto out;
2593 }
2594
Avi Kivityb733bfb2007-10-28 18:52:05 +02002595 r = mmu_topup_memory_caches(vcpu);
2596 if (r)
2597 goto out;
2598
Avi Kivity30677142007-10-28 18:48:59 +02002599 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002600
2601 switch (er) {
2602 case EMULATE_DONE:
2603 return 1;
2604 case EMULATE_DO_MMIO:
2605 ++vcpu->stat.mmio_exits;
2606 return 0;
2607 case EMULATE_FAIL:
2608 kvm_report_emulation_failure(vcpu, "pagetable");
2609 return 1;
2610 default:
2611 BUG();
2612 }
2613out:
Avi Kivity30677142007-10-28 18:48:59 +02002614 return r;
2615}
2616EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2617
Marcelo Tosattia7052892008-09-23 13:18:35 -03002618void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2619{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002620 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002621 kvm_mmu_flush_tlb(vcpu);
2622 ++vcpu->stat.invlpg;
2623}
2624EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2625
Joerg Roedel18552672008-02-07 13:47:41 +01002626void kvm_enable_tdp(void)
2627{
2628 tdp_enabled = true;
2629}
2630EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2631
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002632void kvm_disable_tdp(void)
2633{
2634 tdp_enabled = false;
2635}
2636EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2637
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638static void free_mmu_pages(struct kvm_vcpu *vcpu)
2639{
Avi Kivity4db35312007-11-21 15:28:32 +02002640 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002642 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2643 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002644 struct kvm_mmu_page, link);
2645 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002646 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002647 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002648 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649}
2650
2651static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2652{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002653 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654 int i;
2655
2656 ASSERT(vcpu);
2657
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002658 if (vcpu->kvm->arch.n_requested_mmu_pages)
2659 vcpu->kvm->arch.n_free_mmu_pages =
2660 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002661 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002662 vcpu->kvm->arch.n_free_mmu_pages =
2663 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002664 /*
2665 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2666 * Therefore we need to allocate shadow page tables in the first
2667 * 4GB of memory, which happens to fit the DMA32 zone.
2668 */
2669 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2670 if (!page)
2671 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002672 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002673 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002674 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002675
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 return 0;
2677
2678error_1:
2679 free_mmu_pages(vcpu);
2680 return -ENOMEM;
2681}
2682
Ingo Molnar8018c272006-12-29 16:50:01 -08002683int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002686 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687
Ingo Molnar8018c272006-12-29 16:50:01 -08002688 return alloc_mmu_pages(vcpu);
2689}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690
Ingo Molnar8018c272006-12-29 16:50:01 -08002691int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2692{
2693 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002694 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002695
Ingo Molnar8018c272006-12-29 16:50:01 -08002696 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697}
2698
2699void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2700{
2701 ASSERT(vcpu);
2702
2703 destroy_kvm_mmu(vcpu);
2704 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002705 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706}
2707
Avi Kivity90cb0522007-07-17 13:04:56 +03002708void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709{
Avi Kivity4db35312007-11-21 15:28:32 +02002710 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711
Avi Kivity2245a282008-08-27 16:32:24 +03002712 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002713 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 int i;
2715 u64 *pt;
2716
Sheng Yang291f26b2008-10-16 17:30:57 +08002717 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718 continue;
2719
Avi Kivity4db35312007-11-21 15:28:32 +02002720 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2722 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002723 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 }
Avi Kivity171d5952008-08-27 16:40:51 +03002726 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002727 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002729
Avi Kivity90cb0522007-07-17 13:04:56 +03002730void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002731{
Avi Kivity4db35312007-11-21 15:28:32 +02002732 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002733
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002734 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002735 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002736 if (kvm_mmu_zap_page(kvm, sp))
2737 node = container_of(kvm->arch.active_mmu_pages.next,
2738 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002739 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002740
Avi Kivity90cb0522007-07-17 13:04:56 +03002741 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002742}
2743
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002744static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002745{
2746 struct kvm_mmu_page *page;
2747
2748 page = container_of(kvm->arch.active_mmu_pages.prev,
2749 struct kvm_mmu_page, link);
2750 kvm_mmu_zap_page(kvm, page);
2751}
2752
2753static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2754{
2755 struct kvm *kvm;
2756 struct kvm *kvm_freed = NULL;
2757 int cache_count = 0;
2758
2759 spin_lock(&kvm_lock);
2760
2761 list_for_each_entry(kvm, &vm_list, vm_list) {
2762 int npages;
2763
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002764 if (!down_read_trylock(&kvm->slots_lock))
2765 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002766 spin_lock(&kvm->mmu_lock);
2767 npages = kvm->arch.n_alloc_mmu_pages -
2768 kvm->arch.n_free_mmu_pages;
2769 cache_count += npages;
2770 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2771 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2772 cache_count--;
2773 kvm_freed = kvm;
2774 }
2775 nr_to_scan--;
2776
2777 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002778 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002779 }
2780 if (kvm_freed)
2781 list_move_tail(&kvm_freed->vm_list, &vm_list);
2782
2783 spin_unlock(&kvm_lock);
2784
2785 return cache_count;
2786}
2787
2788static struct shrinker mmu_shrinker = {
2789 .shrink = mmu_shrink,
2790 .seeks = DEFAULT_SEEKS * 10,
2791};
2792
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002793static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002794{
2795 if (pte_chain_cache)
2796 kmem_cache_destroy(pte_chain_cache);
2797 if (rmap_desc_cache)
2798 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002799 if (mmu_page_header_cache)
2800 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002801}
2802
Izik Eidus3ee16c82008-03-30 15:17:21 +03002803void kvm_mmu_module_exit(void)
2804{
2805 mmu_destroy_caches();
2806 unregister_shrinker(&mmu_shrinker);
2807}
2808
Avi Kivityb5a33a72007-04-15 16:31:09 +03002809int kvm_mmu_module_init(void)
2810{
2811 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2812 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002813 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002814 if (!pte_chain_cache)
2815 goto nomem;
2816 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2817 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002818 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002819 if (!rmap_desc_cache)
2820 goto nomem;
2821
Avi Kivityd3d25b02007-05-30 12:34:53 +03002822 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2823 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002824 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002825 if (!mmu_page_header_cache)
2826 goto nomem;
2827
Izik Eidus3ee16c82008-03-30 15:17:21 +03002828 register_shrinker(&mmu_shrinker);
2829
Avi Kivityb5a33a72007-04-15 16:31:09 +03002830 return 0;
2831
2832nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002833 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002834 return -ENOMEM;
2835}
2836
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002837/*
2838 * Caculate mmu pages needed for kvm.
2839 */
2840unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2841{
2842 int i;
2843 unsigned int nr_mmu_pages;
2844 unsigned int nr_pages = 0;
2845
2846 for (i = 0; i < kvm->nmemslots; i++)
2847 nr_pages += kvm->memslots[i].npages;
2848
2849 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2850 nr_mmu_pages = max(nr_mmu_pages,
2851 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2852
2853 return nr_mmu_pages;
2854}
2855
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002856static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2857 unsigned len)
2858{
2859 if (len > buffer->len)
2860 return NULL;
2861 return buffer->ptr;
2862}
2863
2864static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2865 unsigned len)
2866{
2867 void *ret;
2868
2869 ret = pv_mmu_peek_buffer(buffer, len);
2870 if (!ret)
2871 return ret;
2872 buffer->ptr += len;
2873 buffer->len -= len;
2874 buffer->processed += len;
2875 return ret;
2876}
2877
2878static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2879 gpa_t addr, gpa_t value)
2880{
2881 int bytes = 8;
2882 int r;
2883
2884 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2885 bytes = 4;
2886
2887 r = mmu_topup_memory_caches(vcpu);
2888 if (r)
2889 return r;
2890
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002891 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002892 return -EFAULT;
2893
2894 return 1;
2895}
2896
2897static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2898{
2899 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002900 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002901 return 1;
2902}
2903
2904static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2905{
2906 spin_lock(&vcpu->kvm->mmu_lock);
2907 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2908 spin_unlock(&vcpu->kvm->mmu_lock);
2909 return 1;
2910}
2911
2912static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2913 struct kvm_pv_mmu_op_buffer *buffer)
2914{
2915 struct kvm_mmu_op_header *header;
2916
2917 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2918 if (!header)
2919 return 0;
2920 switch (header->op) {
2921 case KVM_MMU_OP_WRITE_PTE: {
2922 struct kvm_mmu_op_write_pte *wpte;
2923
2924 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2925 if (!wpte)
2926 return 0;
2927 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2928 wpte->pte_val);
2929 }
2930 case KVM_MMU_OP_FLUSH_TLB: {
2931 struct kvm_mmu_op_flush_tlb *ftlb;
2932
2933 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2934 if (!ftlb)
2935 return 0;
2936 return kvm_pv_mmu_flush_tlb(vcpu);
2937 }
2938 case KVM_MMU_OP_RELEASE_PT: {
2939 struct kvm_mmu_op_release_pt *rpt;
2940
2941 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2942 if (!rpt)
2943 return 0;
2944 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2945 }
2946 default: return 0;
2947 }
2948}
2949
2950int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2951 gpa_t addr, unsigned long *ret)
2952{
2953 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002954 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002955
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002956 buffer->ptr = buffer->buf;
2957 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2958 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002959
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002960 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002961 if (r)
2962 goto out;
2963
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002964 while (buffer->len) {
2965 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002966 if (r < 0)
2967 goto out;
2968 if (r == 0)
2969 break;
2970 }
2971
2972 r = 1;
2973out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002974 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002975 return r;
2976}
2977
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002978#ifdef AUDIT
2979
2980static const char *audit_msg;
2981
2982static gva_t canonicalize(gva_t gva)
2983{
2984#ifdef CONFIG_X86_64
2985 gva = (long long)(gva << 16) >> 16;
2986#endif
2987 return gva;
2988}
2989
2990static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2991 gva_t va, int level)
2992{
2993 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2994 int i;
2995 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2996
2997 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2998 u64 ent = pt[i];
2999
Avi Kivityc7addb92007-09-16 18:58:32 +02003000 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003001 continue;
3002
3003 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003004 if (level > 1) {
3005 if (ent == shadow_notrap_nonpresent_pte)
3006 printk(KERN_ERR "audit: (%s) nontrapping pte"
3007 " in nonleaf level: levels %d gva %lx"
3008 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003009 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003010
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003011 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003012 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003013 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003014 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003015
Avi Kivityc7addb92007-09-16 18:58:32 +02003016 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003017 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003018 printk(KERN_ERR "xx audit error: (%s) levels %d"
3019 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003020 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003021 va, gpa, hpa, ent,
3022 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003023 else if (ent == shadow_notrap_nonpresent_pte
3024 && !is_error_hpa(hpa))
3025 printk(KERN_ERR "audit: (%s) notrap shadow,"
3026 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003027 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003028
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003029 }
3030 }
3031}
3032
3033static void audit_mappings(struct kvm_vcpu *vcpu)
3034{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003035 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003036
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003037 if (vcpu->arch.mmu.root_level == 4)
3038 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003039 else
3040 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003041 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003042 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003043 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003044 i << 30,
3045 2);
3046}
3047
3048static int count_rmaps(struct kvm_vcpu *vcpu)
3049{
3050 int nmaps = 0;
3051 int i, j, k;
3052
3053 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3054 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3055 struct kvm_rmap_desc *d;
3056
3057 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003058 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003059
Izik Eidus290fc382007-09-27 14:11:22 +02003060 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003061 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003062 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003063 ++nmaps;
3064 continue;
3065 }
Izik Eidus290fc382007-09-27 14:11:22 +02003066 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003067 while (d) {
3068 for (k = 0; k < RMAP_EXT; ++k)
3069 if (d->shadow_ptes[k])
3070 ++nmaps;
3071 else
3072 break;
3073 d = d->more;
3074 }
3075 }
3076 }
3077 return nmaps;
3078}
3079
3080static int count_writable_mappings(struct kvm_vcpu *vcpu)
3081{
3082 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003083 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003084 int i;
3085
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003086 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003087 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003088
Avi Kivity4db35312007-11-21 15:28:32 +02003089 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003090 continue;
3091
3092 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3093 u64 ent = pt[i];
3094
3095 if (!(ent & PT_PRESENT_MASK))
3096 continue;
3097 if (!(ent & PT_WRITABLE_MASK))
3098 continue;
3099 ++nmaps;
3100 }
3101 }
3102 return nmaps;
3103}
3104
3105static void audit_rmap(struct kvm_vcpu *vcpu)
3106{
3107 int n_rmap = count_rmaps(vcpu);
3108 int n_actual = count_writable_mappings(vcpu);
3109
3110 if (n_rmap != n_actual)
3111 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003112 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003113}
3114
3115static void audit_write_protection(struct kvm_vcpu *vcpu)
3116{
Avi Kivity4db35312007-11-21 15:28:32 +02003117 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003118 struct kvm_memory_slot *slot;
3119 unsigned long *rmapp;
3120 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003121
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003122 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003123 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003124 continue;
3125
Avi Kivity4db35312007-11-21 15:28:32 +02003126 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003127 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003128 rmapp = &slot->rmap[gfn - slot->base_gfn];
3129 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003130 printk(KERN_ERR "%s: (%s) shadow page has writable"
3131 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003132 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003133 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003134 }
3135}
3136
3137static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3138{
3139 int olddbg = dbg;
3140
3141 dbg = 0;
3142 audit_msg = msg;
3143 audit_rmap(vcpu);
3144 audit_write_protection(vcpu);
3145 audit_mappings(vcpu);
3146 dbg = olddbg;
3147}
3148
3149#endif