blob: aac0499947d864786003b8b993f738b797a2877b [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];
355 memset(p, 0, size);
356 return p;
357}
358
Avi Kivity714b93d2007-01-05 16:36:53 -0800359static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
360{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800361 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 sizeof(struct kvm_pte_chain));
363}
364
Avi Kivity90cb0522007-07-17 13:04:56 +0300365static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800366{
Avi Kivity90cb0522007-07-17 13:04:56 +0300367 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800368}
369
370static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
371{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800372 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800373 sizeof(struct kvm_rmap_desc));
374}
375
Avi Kivity90cb0522007-07-17 13:04:56 +0300376static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800377{
Avi Kivity90cb0522007-07-17 13:04:56 +0300378 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800379}
380
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800381/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300382 * Return the pointer to the largepage write count for a given
383 * gfn, handling slots that are not large page aligned.
384 */
385static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
386{
387 unsigned long idx;
388
389 idx = (gfn / KVM_PAGES_PER_HPAGE) -
390 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
391 return &slot->lpage_info[idx].write_count;
392}
393
394static void account_shadowed(struct kvm *kvm, gfn_t gfn)
395{
396 int *write_count;
397
Izik Eidus28430992008-10-03 17:40:32 +0300398 gfn = unalias_gfn(kvm, gfn);
399 write_count = slot_largepage_idx(gfn,
400 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300401 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300402}
403
404static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
405{
406 int *write_count;
407
Izik Eidus28430992008-10-03 17:40:32 +0300408 gfn = unalias_gfn(kvm, gfn);
409 write_count = slot_largepage_idx(gfn,
410 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300411 *write_count -= 1;
412 WARN_ON(*write_count < 0);
413}
414
415static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
416{
Izik Eidus28430992008-10-03 17:40:32 +0300417 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418 int *largepage_idx;
419
Izik Eidus28430992008-10-03 17:40:32 +0300420 gfn = unalias_gfn(kvm, gfn);
421 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300422 if (slot) {
423 largepage_idx = slot_largepage_idx(gfn, slot);
424 return *largepage_idx;
425 }
426
427 return 1;
428}
429
430static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
431{
432 struct vm_area_struct *vma;
433 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300434 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300435
436 addr = gfn_to_hva(kvm, gfn);
437 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300438 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300440 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441 vma = find_vma(current->mm, addr);
442 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300443 ret = 1;
444 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300446 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300447}
448
449static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
450{
451 struct kvm_memory_slot *slot;
452
453 if (has_wrprotected_page(vcpu->kvm, large_gfn))
454 return 0;
455
456 if (!host_largepage_backed(vcpu->kvm, large_gfn))
457 return 0;
458
459 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
460 if (slot && slot->dirty_bitmap)
461 return 0;
462
463 return 1;
464}
465
466/*
Izik Eidus290fc382007-09-27 14:11:22 +0200467 * Take gfn and return the reverse mapping to it.
468 * Note: gfn must be unaliased before this function get called
469 */
470
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300471static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200472{
473 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200475
476 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477 if (!lpage)
478 return &slot->rmap[gfn - slot->base_gfn];
479
480 idx = (gfn / KVM_PAGES_PER_HPAGE) -
481 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
482
483 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200484}
485
486/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800487 * Reverse mapping data structures:
488 *
Izik Eidus290fc382007-09-27 14:11:22 +0200489 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
490 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 *
Izik Eidus290fc382007-09-27 14:11:22 +0200492 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
493 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300495static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496{
Avi Kivity4db35312007-11-21 15:28:32 +0200497 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200499 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 int i;
501
502 if (!is_rmap_pte(*spte))
503 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200504 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200505 sp = page_header(__pa(spte));
506 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300507 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200508 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800509 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200510 *rmapp = (unsigned long)spte;
511 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800512 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800513 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200514 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800515 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200516 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800517 } else {
518 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200519 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800520 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
521 desc = desc->more;
522 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800523 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 desc = desc->more;
525 }
526 for (i = 0; desc->shadow_ptes[i]; ++i)
527 ;
528 desc->shadow_ptes[i] = spte;
529 }
530}
531
Izik Eidus290fc382007-09-27 14:11:22 +0200532static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 struct kvm_rmap_desc *desc,
534 int i,
535 struct kvm_rmap_desc *prev_desc)
536{
537 int j;
538
539 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
540 ;
541 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000542 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543 if (j != 0)
544 return;
545 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200546 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 else
548 if (prev_desc)
549 prev_desc->more = desc->more;
550 else
Izik Eidus290fc382007-09-27 14:11:22 +0200551 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300552 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553}
554
Izik Eidus290fc382007-09-27 14:11:22 +0200555static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800556{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 struct kvm_rmap_desc *desc;
558 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200559 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500560 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200561 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 int i;
563
564 if (!is_rmap_pte(*spte))
565 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200566 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500567 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800568 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500569 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200570 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500571 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200572 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500573 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300574 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200575 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800576 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
577 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200578 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800579 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200580 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800581 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
582 spte, *spte);
583 BUG();
584 }
Izik Eidus290fc382007-09-27 14:11:22 +0200585 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 } else {
587 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200588 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 prev_desc = NULL;
590 while (desc) {
591 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
592 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200593 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800594 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800595 prev_desc);
596 return;
597 }
598 prev_desc = desc;
599 desc = desc->more;
600 }
601 BUG();
602 }
603}
604
Izik Eidus98348e92007-10-16 14:42:30 +0200605static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800606{
Avi Kivity374cbac2007-01-05 16:36:43 -0800607 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200608 struct kvm_rmap_desc *prev_desc;
609 u64 *prev_spte;
610 int i;
611
612 if (!*rmapp)
613 return NULL;
614 else if (!(*rmapp & 1)) {
615 if (!spte)
616 return (u64 *)*rmapp;
617 return NULL;
618 }
619 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
620 prev_desc = NULL;
621 prev_spte = NULL;
622 while (desc) {
623 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
624 if (prev_spte == spte)
625 return desc->shadow_ptes[i];
626 prev_spte = desc->shadow_ptes[i];
627 }
628 desc = desc->more;
629 }
630 return NULL;
631}
632
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200633static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200634{
Izik Eidus290fc382007-09-27 14:11:22 +0200635 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800636 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800637 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800638
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500639 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300640 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800641
Izik Eidus98348e92007-10-16 14:42:30 +0200642 spte = rmap_next(kvm, rmapp, NULL);
643 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800644 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800645 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800646 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800647 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200648 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800649 write_protected = 1;
650 }
Izik Eidus9647c142007-10-16 14:43:46 +0200651 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800652 }
Izik Eidus855149a2008-03-20 18:17:24 +0200653 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500654 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200655
656 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500657 pfn = spte_to_pfn(*spte);
658 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200659 }
660
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300661 /* check for huge page mappings */
662 rmapp = gfn_to_rmap(kvm, gfn, 1);
663 spte = rmap_next(kvm, rmapp, NULL);
664 while (spte) {
665 BUG_ON(!spte);
666 BUG_ON(!(*spte & PT_PRESENT_MASK));
667 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
668 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
669 if (is_writeble_pte(*spte)) {
670 rmap_remove(kvm, spte);
671 --kvm->stat.lpages;
672 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300673 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300674 write_protected = 1;
675 }
676 spte = rmap_next(kvm, rmapp, spte);
677 }
678
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200679 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800680}
681
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200682static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
683{
684 u64 *spte;
685 int need_tlb_flush = 0;
686
687 while ((spte = rmap_next(kvm, rmapp, NULL))) {
688 BUG_ON(!(*spte & PT_PRESENT_MASK));
689 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
690 rmap_remove(kvm, spte);
691 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
692 need_tlb_flush = 1;
693 }
694 return need_tlb_flush;
695}
696
697static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
698 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
699{
700 int i;
701 int retval = 0;
702
703 /*
704 * If mmap_sem isn't taken, we can look the memslots with only
705 * the mmu_lock by skipping over the slots with userspace_addr == 0.
706 */
707 for (i = 0; i < kvm->nmemslots; i++) {
708 struct kvm_memory_slot *memslot = &kvm->memslots[i];
709 unsigned long start = memslot->userspace_addr;
710 unsigned long end;
711
712 /* mmu_lock protects userspace_addr */
713 if (!start)
714 continue;
715
716 end = start + (memslot->npages << PAGE_SHIFT);
717 if (hva >= start && hva < end) {
718 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
719 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
720 retval |= handler(kvm,
721 &memslot->lpage_info[
722 gfn_offset /
723 KVM_PAGES_PER_HPAGE].rmap_pde);
724 }
725 }
726
727 return retval;
728}
729
730int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
731{
732 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
733}
734
735static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
736{
737 u64 *spte;
738 int young = 0;
739
Sheng Yang534e38b2008-09-08 15:12:30 +0800740 /* always return old for EPT */
741 if (!shadow_accessed_mask)
742 return 0;
743
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200744 spte = rmap_next(kvm, rmapp, NULL);
745 while (spte) {
746 int _young;
747 u64 _spte = *spte;
748 BUG_ON(!(_spte & PT_PRESENT_MASK));
749 _young = _spte & PT_ACCESSED_MASK;
750 if (_young) {
751 young = 1;
752 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
753 }
754 spte = rmap_next(kvm, rmapp, spte);
755 }
756 return young;
757}
758
759int kvm_age_hva(struct kvm *kvm, unsigned long hva)
760{
761 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
762}
763
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800764#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300765static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766{
Avi Kivity139bdb22007-01-05 16:36:50 -0800767 u64 *pos;
768 u64 *end;
769
Avi Kivity47ad8e62007-05-06 15:50:58 +0300770 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300771 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800772 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800773 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800775 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 return 1;
777}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800778#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779
Avi Kivity4db35312007-11-21 15:28:32 +0200780static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800781{
Avi Kivity4db35312007-11-21 15:28:32 +0200782 ASSERT(is_empty_shadow_page(sp->spt));
783 list_del(&sp->link);
784 __free_page(virt_to_page(sp->spt));
785 __free_page(virt_to_page(sp->gfns));
786 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800787 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800788}
789
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800790static unsigned kvm_page_table_hashfn(gfn_t gfn)
791{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200792 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800793}
794
Avi Kivity25c0de22007-01-05 16:36:42 -0800795static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
796 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797{
Avi Kivity4db35312007-11-21 15:28:32 +0200798 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800800 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
801 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
802 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200803 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800804 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200805 INIT_LIST_HEAD(&sp->oos_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200806 ASSERT(is_empty_shadow_page(sp->spt));
Sheng Yang291f26b2008-10-16 17:30:57 +0800807 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200808 sp->multimapped = 0;
809 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800810 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200811 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812}
813
Avi Kivity714b93d2007-01-05 16:36:53 -0800814static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200815 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800816{
817 struct kvm_pte_chain *pte_chain;
818 struct hlist_node *node;
819 int i;
820
821 if (!parent_pte)
822 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200823 if (!sp->multimapped) {
824 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800825
826 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200827 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800828 return;
829 }
Avi Kivity4db35312007-11-21 15:28:32 +0200830 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800831 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200832 INIT_HLIST_HEAD(&sp->parent_ptes);
833 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800834 pte_chain->parent_ptes[0] = old;
835 }
Avi Kivity4db35312007-11-21 15:28:32 +0200836 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800837 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
838 continue;
839 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
840 if (!pte_chain->parent_ptes[i]) {
841 pte_chain->parent_ptes[i] = parent_pte;
842 return;
843 }
844 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800845 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200847 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800848 pte_chain->parent_ptes[0] = parent_pte;
849}
850
Avi Kivity4db35312007-11-21 15:28:32 +0200851static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852 u64 *parent_pte)
853{
854 struct kvm_pte_chain *pte_chain;
855 struct hlist_node *node;
856 int i;
857
Avi Kivity4db35312007-11-21 15:28:32 +0200858 if (!sp->multimapped) {
859 BUG_ON(sp->parent_pte != parent_pte);
860 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800861 return;
862 }
Avi Kivity4db35312007-11-21 15:28:32 +0200863 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800864 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
865 if (!pte_chain->parent_ptes[i])
866 break;
867 if (pte_chain->parent_ptes[i] != parent_pte)
868 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800869 while (i + 1 < NR_PTE_CHAIN_ENTRIES
870 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800871 pte_chain->parent_ptes[i]
872 = pte_chain->parent_ptes[i + 1];
873 ++i;
874 }
875 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800876 if (i == 0) {
877 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300878 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200879 if (hlist_empty(&sp->parent_ptes)) {
880 sp->multimapped = 0;
881 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800882 }
883 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800884 return;
885 }
886 BUG();
887}
888
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300889
890static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
891 mmu_parent_walk_fn fn)
892{
893 struct kvm_pte_chain *pte_chain;
894 struct hlist_node *node;
895 struct kvm_mmu_page *parent_sp;
896 int i;
897
898 if (!sp->multimapped && sp->parent_pte) {
899 parent_sp = page_header(__pa(sp->parent_pte));
900 fn(vcpu, parent_sp);
901 mmu_parent_walk(vcpu, parent_sp, fn);
902 return;
903 }
904 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
905 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
906 if (!pte_chain->parent_ptes[i])
907 break;
908 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
909 fn(vcpu, parent_sp);
910 mmu_parent_walk(vcpu, parent_sp, fn);
911 }
912}
913
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300914static void kvm_mmu_update_unsync_bitmap(u64 *spte)
915{
916 unsigned int index;
917 struct kvm_mmu_page *sp = page_header(__pa(spte));
918
919 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200920 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
921 sp->unsync_children++;
922 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300923}
924
925static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
926{
927 struct kvm_pte_chain *pte_chain;
928 struct hlist_node *node;
929 int i;
930
931 if (!sp->parent_pte)
932 return;
933
934 if (!sp->multimapped) {
935 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
936 return;
937 }
938
939 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
940 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
941 if (!pte_chain->parent_ptes[i])
942 break;
943 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
944 }
945}
946
947static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
948{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300949 kvm_mmu_update_parents_unsync(sp);
950 return 1;
951}
952
953static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
954 struct kvm_mmu_page *sp)
955{
956 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
957 kvm_mmu_update_parents_unsync(sp);
958}
959
Avi Kivityd761a502008-05-29 14:55:03 +0300960static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
961 struct kvm_mmu_page *sp)
962{
963 int i;
964
965 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
966 sp->spt[i] = shadow_trap_nonpresent_pte;
967}
968
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300969static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
970 struct kvm_mmu_page *sp)
971{
972 return 1;
973}
974
Marcelo Tosattia7052892008-09-23 13:18:35 -0300975static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
976{
977}
978
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200979#define KVM_PAGE_ARRAY_NR 16
980
981struct kvm_mmu_pages {
982 struct mmu_page_and_offset {
983 struct kvm_mmu_page *sp;
984 unsigned int idx;
985 } page[KVM_PAGE_ARRAY_NR];
986 unsigned int nr;
987};
988
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300989#define for_each_unsync_children(bitmap, idx) \
990 for (idx = find_first_bit(bitmap, 512); \
991 idx < 512; \
992 idx = find_next_bit(bitmap, 512, idx+1))
993
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200994int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
995 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300996{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200997 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300998
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200999 if (sp->unsync)
1000 for (i=0; i < pvec->nr; i++)
1001 if (pvec->page[i].sp == sp)
1002 return 0;
1003
1004 pvec->page[pvec->nr].sp = sp;
1005 pvec->page[pvec->nr].idx = idx;
1006 pvec->nr++;
1007 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1008}
1009
1010static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1011 struct kvm_mmu_pages *pvec)
1012{
1013 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001014
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001015 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001016 u64 ent = sp->spt[i];
1017
Marcelo Tosatti87917232008-12-22 18:49:30 -02001018 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001019 struct kvm_mmu_page *child;
1020 child = page_header(ent & PT64_BASE_ADDR_MASK);
1021
1022 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001023 if (mmu_pages_add(pvec, child, i))
1024 return -ENOSPC;
1025
1026 ret = __mmu_unsync_walk(child, pvec);
1027 if (!ret)
1028 __clear_bit(i, sp->unsync_child_bitmap);
1029 else if (ret > 0)
1030 nr_unsync_leaf += ret;
1031 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001032 return ret;
1033 }
1034
1035 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001036 nr_unsync_leaf++;
1037 if (mmu_pages_add(pvec, child, i))
1038 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001039 }
1040 }
1041 }
1042
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001043 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001044 sp->unsync_children = 0;
1045
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001046 return nr_unsync_leaf;
1047}
1048
1049static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1050 struct kvm_mmu_pages *pvec)
1051{
1052 if (!sp->unsync_children)
1053 return 0;
1054
1055 mmu_pages_add(pvec, sp, 0);
1056 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001057}
1058
Avi Kivity4db35312007-11-21 15:28:32 +02001059static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001060{
1061 unsigned index;
1062 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001063 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001064 struct hlist_node *node;
1065
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001066 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001067 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001068 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001069 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001070 if (sp->gfn == gfn && !sp->role.metaphysical
1071 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001072 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001073 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001074 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001075 }
1076 return NULL;
1077}
1078
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001079static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1080{
1081 list_del(&sp->oos_link);
1082 --kvm->stat.mmu_unsync_global;
1083}
1084
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001085static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1086{
1087 WARN_ON(!sp->unsync);
1088 sp->unsync = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001089 if (sp->global)
1090 kvm_unlink_unsync_global(kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001091 --kvm->stat.mmu_unsync;
1092}
1093
1094static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1095
1096static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1097{
1098 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1099 kvm_mmu_zap_page(vcpu->kvm, sp);
1100 return 1;
1101 }
1102
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001103 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1104 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001105 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001106 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1107 kvm_mmu_zap_page(vcpu->kvm, sp);
1108 return 1;
1109 }
1110
1111 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001112 return 0;
1113}
1114
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001115struct mmu_page_path {
1116 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1117 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001118};
1119
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001120#define for_each_sp(pvec, sp, parents, i) \
1121 for (i = mmu_pages_next(&pvec, &parents, -1), \
1122 sp = pvec.page[i].sp; \
1123 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1124 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001125
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001126int mmu_pages_next(struct kvm_mmu_pages *pvec, struct mmu_page_path *parents,
1127 int i)
1128{
1129 int n;
1130
1131 for (n = i+1; n < pvec->nr; n++) {
1132 struct kvm_mmu_page *sp = pvec->page[n].sp;
1133
1134 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1135 parents->idx[0] = pvec->page[n].idx;
1136 return n;
1137 }
1138
1139 parents->parent[sp->role.level-2] = sp;
1140 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1141 }
1142
1143 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001144}
1145
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001146void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001147{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001148 struct kvm_mmu_page *sp;
1149 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001150
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001151 do {
1152 unsigned int idx = parents->idx[level];
1153
1154 sp = parents->parent[level];
1155 if (!sp)
1156 return;
1157
1158 --sp->unsync_children;
1159 WARN_ON((int)sp->unsync_children < 0);
1160 __clear_bit(idx, sp->unsync_child_bitmap);
1161 level++;
1162 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1163}
1164
1165static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1166 struct mmu_page_path *parents,
1167 struct kvm_mmu_pages *pvec)
1168{
1169 parents->parent[parent->role.level-1] = NULL;
1170 pvec->nr = 0;
1171}
1172
1173static void mmu_sync_children(struct kvm_vcpu *vcpu,
1174 struct kvm_mmu_page *parent)
1175{
1176 int i;
1177 struct kvm_mmu_page *sp;
1178 struct mmu_page_path parents;
1179 struct kvm_mmu_pages pages;
1180
1181 kvm_mmu_pages_init(parent, &parents, &pages);
1182 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001183 int protected = 0;
1184
1185 for_each_sp(pages, sp, parents, i)
1186 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1187
1188 if (protected)
1189 kvm_flush_remote_tlbs(vcpu->kvm);
1190
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001191 for_each_sp(pages, sp, parents, i) {
1192 kvm_sync_page(vcpu, sp);
1193 mmu_pages_clear_parents(&parents);
1194 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001195 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001196 kvm_mmu_pages_init(parent, &parents, &pages);
1197 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001198}
1199
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001200static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1201 gfn_t gfn,
1202 gva_t gaddr,
1203 unsigned level,
1204 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001205 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001206 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001207{
1208 union kvm_mmu_page_role role;
1209 unsigned index;
1210 unsigned quadrant;
1211 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001212 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001213 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001214
Avi Kivitya770f6f2008-12-21 19:20:09 +02001215 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001216 role.level = level;
1217 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001218 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001219 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001220 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1221 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1222 role.quadrant = quadrant;
1223 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001224 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001225 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001226 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001227 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001228 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1229 if (sp->gfn == gfn) {
1230 if (sp->unsync)
1231 if (kvm_sync_page(vcpu, sp))
1232 continue;
1233
1234 if (sp->role.word != role.word)
1235 continue;
1236
Avi Kivity4db35312007-11-21 15:28:32 +02001237 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001238 if (sp->unsync_children) {
1239 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1240 kvm_mmu_mark_parents_unsync(vcpu, sp);
1241 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001242 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001243 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001244 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001245 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001246 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1247 if (!sp)
1248 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001249 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001250 sp->gfn = gfn;
1251 sp->role = role;
Avi Kivitye2078312008-12-21 19:36:59 +02001252 sp->global = role.cr4_pge;
Avi Kivity4db35312007-11-21 15:28:32 +02001253 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001254 if (!metaphysical) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001255 if (rmap_write_protect(vcpu->kvm, gfn))
1256 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001257 account_shadowed(vcpu->kvm, gfn);
1258 }
Avi Kivity131d8272008-05-29 14:56:28 +03001259 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1260 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1261 else
1262 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001263 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001264}
1265
Avi Kivity2d111232008-12-25 14:39:47 +02001266static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1267 struct kvm_vcpu *vcpu, u64 addr)
1268{
1269 iterator->addr = addr;
1270 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1271 iterator->level = vcpu->arch.mmu.shadow_root_level;
1272 if (iterator->level == PT32E_ROOT_LEVEL) {
1273 iterator->shadow_addr
1274 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1275 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1276 --iterator->level;
1277 if (!iterator->shadow_addr)
1278 iterator->level = 0;
1279 }
1280}
1281
1282static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1283{
1284 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1285 return false;
1286 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1287 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1288 return true;
1289}
1290
1291static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1292{
1293 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1294 --iterator->level;
1295}
1296
Avi Kivity90cb0522007-07-17 13:04:56 +03001297static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001298 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001299{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001300 unsigned i;
1301 u64 *pt;
1302 u64 ent;
1303
Avi Kivity4db35312007-11-21 15:28:32 +02001304 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001305
Avi Kivity4db35312007-11-21 15:28:32 +02001306 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001307 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001308 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001309 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001310 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001311 }
1312 return;
1313 }
1314
1315 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1316 ent = pt[i];
1317
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001318 if (is_shadow_present_pte(ent)) {
1319 if (!is_large_pte(ent)) {
1320 ent &= PT64_BASE_ADDR_MASK;
1321 mmu_page_remove_parent_pte(page_header(ent),
1322 &pt[i]);
1323 } else {
1324 --kvm->stat.lpages;
1325 rmap_remove(kvm, &pt[i]);
1326 }
1327 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001328 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001329 }
Avi Kivitya4360362007-01-05 16:36:45 -08001330}
1331
Avi Kivity4db35312007-11-21 15:28:32 +02001332static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001333{
Avi Kivity4db35312007-11-21 15:28:32 +02001334 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001335}
1336
Avi Kivity12b7d282007-09-23 14:10:49 +02001337static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1338{
1339 int i;
1340
1341 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1342 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001343 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001344}
1345
Avi Kivity31aa2b42008-07-11 17:59:46 +03001346static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001347{
1348 u64 *parent_pte;
1349
Avi Kivity4db35312007-11-21 15:28:32 +02001350 while (sp->multimapped || sp->parent_pte) {
1351 if (!sp->multimapped)
1352 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001353 else {
1354 struct kvm_pte_chain *chain;
1355
Avi Kivity4db35312007-11-21 15:28:32 +02001356 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001357 struct kvm_pte_chain, link);
1358 parent_pte = chain->parent_ptes[0];
1359 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001360 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001361 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001362 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001363 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001364}
1365
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001366static int mmu_zap_unsync_children(struct kvm *kvm,
1367 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001368{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001369 int i, zapped = 0;
1370 struct mmu_page_path parents;
1371 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001372
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001373 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001374 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001375
1376 kvm_mmu_pages_init(parent, &parents, &pages);
1377 while (mmu_unsync_walk(parent, &pages)) {
1378 struct kvm_mmu_page *sp;
1379
1380 for_each_sp(pages, sp, parents, i) {
1381 kvm_mmu_zap_page(kvm, sp);
1382 mmu_pages_clear_parents(&parents);
1383 }
1384 zapped += pages.nr;
1385 kvm_mmu_pages_init(parent, &parents, &pages);
1386 }
1387
1388 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001389}
1390
Marcelo Tosatti07385412008-09-23 13:18:37 -03001391static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001392{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001393 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001394 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001395 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001396 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001397 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001398 kvm_flush_remote_tlbs(kvm);
1399 if (!sp->role.invalid && !sp->role.metaphysical)
1400 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001401 if (sp->unsync)
1402 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001403 if (!sp->root_count) {
1404 hlist_del(&sp->hash_link);
1405 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001406 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001407 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001408 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001409 kvm_reload_remote_mmus(kvm);
1410 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001411 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001412 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001413}
1414
Izik Eidus82ce2c92007-10-02 18:52:55 +02001415/*
1416 * Changing the number of mmu pages allocated to the vm
1417 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1418 */
1419void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1420{
1421 /*
1422 * If we set the number of mmu pages to be smaller be than the
1423 * number of actived pages , we must to free some mmu pages before we
1424 * change the value
1425 */
1426
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001427 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001428 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001429 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1430 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001431
1432 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1433 struct kvm_mmu_page *page;
1434
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001435 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001436 struct kvm_mmu_page, link);
1437 kvm_mmu_zap_page(kvm, page);
1438 n_used_mmu_pages--;
1439 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001440 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001441 }
1442 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001443 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1444 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001445
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001446 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001447}
1448
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001449static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001450{
1451 unsigned index;
1452 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001453 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001454 struct hlist_node *node, *n;
1455 int r;
1456
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001457 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001458 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001459 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001460 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001461 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1462 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001463 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001464 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001465 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001466 if (kvm_mmu_zap_page(kvm, sp))
1467 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001468 }
1469 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001470}
1471
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001472static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001473{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001474 unsigned index;
1475 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001476 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001477 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001478
Avi Kivity4677a3b2009-01-06 13:00:27 +02001479 index = kvm_page_table_hashfn(gfn);
1480 bucket = &kvm->arch.mmu_page_hash[index];
1481 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1482 if (sp->gfn == gfn && !sp->role.metaphysical
1483 && !sp->role.invalid) {
1484 pgprintk("%s: zap %lx %x\n",
1485 __func__, gfn, sp->role.word);
1486 kvm_mmu_zap_page(kvm, sp);
1487 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001488 }
1489}
1490
Avi Kivity38c335f2007-11-21 14:20:22 +02001491static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492{
Avi Kivity38c335f2007-11-21 14:20:22 +02001493 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001494 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495
Sheng Yang291f26b2008-10-16 17:30:57 +08001496 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497}
1498
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001499static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1500{
1501 int i;
1502 u64 *pt = sp->spt;
1503
1504 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1505 return;
1506
1507 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1508 if (pt[i] == shadow_notrap_nonpresent_pte)
1509 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1510 }
1511}
1512
Avi Kivity039576c2007-03-20 12:46:50 +02001513struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1514{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001515 struct page *page;
1516
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001517 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001518
1519 if (gpa == UNMAPPED_GVA)
1520 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001521
Izik Eidus72dc67a2008-02-10 18:04:15 +02001522 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001523
1524 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001525}
1526
Sheng Yang74be52e2008-10-09 16:01:56 +08001527/*
1528 * The function is based on mtrr_type_lookup() in
1529 * arch/x86/kernel/cpu/mtrr/generic.c
1530 */
1531static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1532 u64 start, u64 end)
1533{
1534 int i;
1535 u64 base, mask;
1536 u8 prev_match, curr_match;
1537 int num_var_ranges = KVM_NR_VAR_MTRR;
1538
1539 if (!mtrr_state->enabled)
1540 return 0xFF;
1541
1542 /* Make end inclusive end, instead of exclusive */
1543 end--;
1544
1545 /* Look in fixed ranges. Just return the type as per start */
1546 if (mtrr_state->have_fixed && (start < 0x100000)) {
1547 int idx;
1548
1549 if (start < 0x80000) {
1550 idx = 0;
1551 idx += (start >> 16);
1552 return mtrr_state->fixed_ranges[idx];
1553 } else if (start < 0xC0000) {
1554 idx = 1 * 8;
1555 idx += ((start - 0x80000) >> 14);
1556 return mtrr_state->fixed_ranges[idx];
1557 } else if (start < 0x1000000) {
1558 idx = 3 * 8;
1559 idx += ((start - 0xC0000) >> 12);
1560 return mtrr_state->fixed_ranges[idx];
1561 }
1562 }
1563
1564 /*
1565 * Look in variable ranges
1566 * Look of multiple ranges matching this address and pick type
1567 * as per MTRR precedence
1568 */
1569 if (!(mtrr_state->enabled & 2))
1570 return mtrr_state->def_type;
1571
1572 prev_match = 0xFF;
1573 for (i = 0; i < num_var_ranges; ++i) {
1574 unsigned short start_state, end_state;
1575
1576 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1577 continue;
1578
1579 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1580 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1581 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1582 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1583
1584 start_state = ((start & mask) == (base & mask));
1585 end_state = ((end & mask) == (base & mask));
1586 if (start_state != end_state)
1587 return 0xFE;
1588
1589 if ((start & mask) != (base & mask))
1590 continue;
1591
1592 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1593 if (prev_match == 0xFF) {
1594 prev_match = curr_match;
1595 continue;
1596 }
1597
1598 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1599 curr_match == MTRR_TYPE_UNCACHABLE)
1600 return MTRR_TYPE_UNCACHABLE;
1601
1602 if ((prev_match == MTRR_TYPE_WRBACK &&
1603 curr_match == MTRR_TYPE_WRTHROUGH) ||
1604 (prev_match == MTRR_TYPE_WRTHROUGH &&
1605 curr_match == MTRR_TYPE_WRBACK)) {
1606 prev_match = MTRR_TYPE_WRTHROUGH;
1607 curr_match = MTRR_TYPE_WRTHROUGH;
1608 }
1609
1610 if (prev_match != curr_match)
1611 return MTRR_TYPE_UNCACHABLE;
1612 }
1613
1614 if (prev_match != 0xFF)
1615 return prev_match;
1616
1617 return mtrr_state->def_type;
1618}
1619
1620static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1621{
1622 u8 mtrr;
1623
1624 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1625 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1626 if (mtrr == 0xfe || mtrr == 0xff)
1627 mtrr = MTRR_TYPE_WRBACK;
1628 return mtrr;
1629}
1630
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001631static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1632{
1633 unsigned index;
1634 struct hlist_head *bucket;
1635 struct kvm_mmu_page *s;
1636 struct hlist_node *node, *n;
1637
1638 index = kvm_page_table_hashfn(sp->gfn);
1639 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1640 /* don't unsync if pagetable is shadowed with multiple roles */
1641 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1642 if (s->gfn != sp->gfn || s->role.metaphysical)
1643 continue;
1644 if (s->role.word != sp->role.word)
1645 return 1;
1646 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001647 ++vcpu->kvm->stat.mmu_unsync;
1648 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001649
1650 if (sp->global) {
1651 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1652 ++vcpu->kvm->stat.mmu_unsync_global;
1653 } else
1654 kvm_mmu_mark_parents_unsync(vcpu, sp);
1655
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001656 mmu_convert_notrap(sp);
1657 return 0;
1658}
1659
1660static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1661 bool can_unsync)
1662{
1663 struct kvm_mmu_page *shadow;
1664
1665 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1666 if (shadow) {
1667 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1668 return 1;
1669 if (shadow->unsync)
1670 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001671 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001672 return kvm_unsync_page(vcpu, shadow);
1673 return 1;
1674 }
1675 return 0;
1676}
1677
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001678static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1679 unsigned pte_access, int user_fault,
1680 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001681 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001682 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001683{
1684 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001685 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001686 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001687 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1688
1689 if (!global && sp->global) {
1690 sp->global = 0;
1691 if (sp->unsync) {
1692 kvm_unlink_unsync_global(vcpu->kvm, sp);
1693 kvm_mmu_mark_parents_unsync(vcpu, sp);
1694 }
1695 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001696
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001697 /*
1698 * We don't set the accessed bit, since we sometimes want to see
1699 * whether the guest actually used the pte (in order to detect
1700 * demand paging).
1701 */
Sheng Yang7b523452008-04-25 21:13:50 +08001702 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001703 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001704 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001705 if (!dirty)
1706 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001707 if (pte_access & ACC_EXEC_MASK)
1708 spte |= shadow_x_mask;
1709 else
1710 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001711 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001712 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001713 if (largepage)
1714 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001715 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001716 if (!kvm_is_mmio_pfn(pfn)) {
1717 mt_mask = get_memory_type(vcpu, gfn) <<
1718 kvm_x86_ops->get_mt_mask_shift();
1719 mt_mask |= VMX_EPT_IGMT_BIT;
1720 } else
1721 mt_mask = MTRR_TYPE_UNCACHABLE <<
1722 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001723 spte |= mt_mask;
1724 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001725
Anthony Liguori35149e22008-04-02 14:46:56 -05001726 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727
1728 if ((pte_access & ACC_WRITE_MASK)
1729 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001730
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001731 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1732 ret = 1;
1733 spte = shadow_trap_nonpresent_pte;
1734 goto set_pte;
1735 }
1736
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001737 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001738
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001739 /*
1740 * Optimization: for pte sync, if spte was writable the hash
1741 * lookup is unnecessary (and expensive). Write protection
1742 * is responsibility of mmu_get_page / kvm_sync_page.
1743 * Same reasoning can be applied to dirty page accounting.
1744 */
1745 if (!can_unsync && is_writeble_pte(*shadow_pte))
1746 goto set_pte;
1747
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001748 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001749 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001750 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001751 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001752 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001753 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001754 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001755 }
1756 }
1757
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001758 if (pte_access & ACC_WRITE_MASK)
1759 mark_page_dirty(vcpu->kvm, gfn);
1760
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001761set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001762 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001763 return ret;
1764}
1765
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001766static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1767 unsigned pt_access, unsigned pte_access,
1768 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001769 int *ptwrite, int largepage, int global,
1770 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001771{
1772 int was_rmapped = 0;
1773 int was_writeble = is_writeble_pte(*shadow_pte);
1774
1775 pgprintk("%s: spte %llx access %x write_fault %d"
1776 " user_fault %d gfn %lx\n",
1777 __func__, *shadow_pte, pt_access,
1778 write_fault, user_fault, gfn);
1779
1780 if (is_rmap_pte(*shadow_pte)) {
1781 /*
1782 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1783 * the parent of the now unreachable PTE.
1784 */
1785 if (largepage && !is_large_pte(*shadow_pte)) {
1786 struct kvm_mmu_page *child;
1787 u64 pte = *shadow_pte;
1788
1789 child = page_header(pte & PT64_BASE_ADDR_MASK);
1790 mmu_page_remove_parent_pte(child, shadow_pte);
1791 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1792 pgprintk("hfn old %lx new %lx\n",
1793 spte_to_pfn(*shadow_pte), pfn);
1794 rmap_remove(vcpu->kvm, shadow_pte);
1795 } else {
1796 if (largepage)
1797 was_rmapped = is_large_pte(*shadow_pte);
1798 else
1799 was_rmapped = 1;
1800 }
1801 }
1802 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001803 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001804 if (write_fault)
1805 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001806 kvm_x86_ops->tlb_flush(vcpu);
1807 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001808
1809 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1810 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1811 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1812 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1813 *shadow_pte, shadow_pte);
1814 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001815 ++vcpu->kvm->stat.lpages;
1816
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1818 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001819 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001820 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001821 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001822 } else {
1823 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001824 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001825 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001826 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001827 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001828 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001829 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001830 vcpu->arch.last_pte_gfn = gfn;
1831 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001832}
1833
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1835{
1836}
1837
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001838static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001839 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840{
Avi Kivity9f652d22008-12-25 14:54:25 +02001841 struct kvm_shadow_walk_iterator iterator;
1842 struct kvm_mmu_page *sp;
1843 int pt_write = 0;
1844 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845
Avi Kivity9f652d22008-12-25 14:54:25 +02001846 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1847 if (iterator.level == PT_PAGE_TABLE_LEVEL
1848 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1849 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1850 0, write, 1, &pt_write,
1851 largepage, 0, gfn, pfn, false);
1852 ++vcpu->stat.pf_fixed;
1853 break;
1854 }
1855
1856 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1857 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1858 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1859 iterator.level - 1,
1860 1, ACC_ALL, iterator.sptep);
1861 if (!sp) {
1862 pgprintk("nonpaging_map: ENOMEM\n");
1863 kvm_release_pfn_clean(pfn);
1864 return -ENOMEM;
1865 }
1866
1867 set_shadow_pte(iterator.sptep,
1868 __pa(sp->spt)
1869 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1870 | shadow_user_mask | shadow_x_mask);
1871 }
1872 }
1873 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874}
1875
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001876static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1877{
1878 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001879 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001880 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001881 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001882
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001883 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1884 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1885 largepage = 1;
1886 }
1887
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001888 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001889 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001890 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001891
Avi Kivityd196e342008-01-24 11:44:11 +02001892 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001893 if (is_error_pfn(pfn)) {
1894 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001895 return 1;
1896 }
1897
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001898 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001899 if (mmu_notifier_retry(vcpu, mmu_seq))
1900 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001901 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001902 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001903 spin_unlock(&vcpu->kvm->mmu_lock);
1904
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001905
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001906 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001907
1908out_unlock:
1909 spin_unlock(&vcpu->kvm->mmu_lock);
1910 kvm_release_pfn_clean(pfn);
1911 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001912}
1913
1914
Avi Kivity17ac10a2007-01-05 16:36:40 -08001915static void mmu_free_roots(struct kvm_vcpu *vcpu)
1916{
1917 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001918 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001919
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001920 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001921 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001922 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001923 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1924 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001925
Avi Kivity4db35312007-11-21 15:28:32 +02001926 sp = page_header(root);
1927 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001928 if (!sp->root_count && sp->role.invalid)
1929 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001930 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001931 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001932 return;
1933 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001934 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001936
Avi Kivity417726a2007-04-12 17:35:58 +03001937 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001938 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001939 sp = page_header(root);
1940 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001941 if (!sp->root_count && sp->role.invalid)
1942 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001943 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001944 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001945 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001946 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001947 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001948}
1949
1950static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1951{
1952 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001953 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001954 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001955 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001956
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001957 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001958
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001959 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1960 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001961
1962 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001963 if (tdp_enabled)
1964 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001965 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001966 PT64_ROOT_LEVEL, metaphysical,
1967 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001968 root = __pa(sp->spt);
1969 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001970 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001971 return;
1972 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001973 metaphysical = !is_paging(vcpu);
1974 if (tdp_enabled)
1975 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001976 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001977 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001978
1979 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001980 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1981 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1982 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001983 continue;
1984 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001985 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1986 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001987 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001988 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001989 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001990 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001991 root = __pa(sp->spt);
1992 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001993 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001994 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001995 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001996}
1997
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001998static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1999{
2000 int i;
2001 struct kvm_mmu_page *sp;
2002
2003 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2004 return;
2005 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2006 hpa_t root = vcpu->arch.mmu.root_hpa;
2007 sp = page_header(root);
2008 mmu_sync_children(vcpu, sp);
2009 return;
2010 }
2011 for (i = 0; i < 4; ++i) {
2012 hpa_t root = vcpu->arch.mmu.pae_root[i];
2013
2014 if (root) {
2015 root &= PT64_BASE_ADDR_MASK;
2016 sp = page_header(root);
2017 mmu_sync_children(vcpu, sp);
2018 }
2019 }
2020}
2021
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002022static void mmu_sync_global(struct kvm_vcpu *vcpu)
2023{
2024 struct kvm *kvm = vcpu->kvm;
2025 struct kvm_mmu_page *sp, *n;
2026
2027 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2028 kvm_sync_page(vcpu, sp);
2029}
2030
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002031void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2032{
2033 spin_lock(&vcpu->kvm->mmu_lock);
2034 mmu_sync_roots(vcpu);
2035 spin_unlock(&vcpu->kvm->mmu_lock);
2036}
2037
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002038void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2039{
2040 spin_lock(&vcpu->kvm->mmu_lock);
2041 mmu_sync_global(vcpu);
2042 spin_unlock(&vcpu->kvm->mmu_lock);
2043}
2044
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2046{
2047 return vaddr;
2048}
2049
2050static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002051 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052{
Avi Kivitye8332402007-12-09 18:43:00 +02002053 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002054 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002056 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002057 r = mmu_topup_memory_caches(vcpu);
2058 if (r)
2059 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002060
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002062 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063
Avi Kivitye8332402007-12-09 18:43:00 +02002064 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065
Avi Kivitye8332402007-12-09 18:43:00 +02002066 return nonpaging_map(vcpu, gva & PAGE_MASK,
2067 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068}
2069
Joerg Roedelfb72d162008-02-07 13:47:44 +01002070static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2071 u32 error_code)
2072{
Anthony Liguori35149e22008-04-02 14:46:56 -05002073 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002074 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002075 int largepage = 0;
2076 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002077 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002078
2079 ASSERT(vcpu);
2080 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2081
2082 r = mmu_topup_memory_caches(vcpu);
2083 if (r)
2084 return r;
2085
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002086 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2087 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2088 largepage = 1;
2089 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002090 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002091 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002092 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002093 if (is_error_pfn(pfn)) {
2094 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002095 return 1;
2096 }
2097 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002098 if (mmu_notifier_retry(vcpu, mmu_seq))
2099 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002100 kvm_mmu_free_some_pages(vcpu);
2101 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002102 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002103 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002104
2105 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002106
2107out_unlock:
2108 spin_unlock(&vcpu->kvm->mmu_lock);
2109 kvm_release_pfn_clean(pfn);
2110 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002111}
2112
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113static void nonpaging_free(struct kvm_vcpu *vcpu)
2114{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002115 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116}
2117
2118static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2119{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002120 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121
2122 context->new_cr3 = nonpaging_new_cr3;
2123 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 context->gva_to_gpa = nonpaging_gva_to_gpa;
2125 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002126 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002127 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002128 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002129 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002131 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132 return 0;
2133}
2134
Avi Kivityd835dfe2007-11-21 02:57:59 +02002135void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002137 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002138 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139}
2140
2141static void paging_new_cr3(struct kvm_vcpu *vcpu)
2142{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002143 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002144 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145}
2146
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147static void inject_page_fault(struct kvm_vcpu *vcpu,
2148 u64 addr,
2149 u32 err_code)
2150{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002151 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152}
2153
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154static void paging_free(struct kvm_vcpu *vcpu)
2155{
2156 nonpaging_free(vcpu);
2157}
2158
2159#define PTTYPE 64
2160#include "paging_tmpl.h"
2161#undef PTTYPE
2162
2163#define PTTYPE 32
2164#include "paging_tmpl.h"
2165#undef PTTYPE
2166
Avi Kivity17ac10a2007-01-05 16:36:40 -08002167static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002169 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170
2171 ASSERT(is_pae(vcpu));
2172 context->new_cr3 = paging_new_cr3;
2173 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002175 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002176 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002177 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002179 context->root_level = level;
2180 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002181 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182 return 0;
2183}
2184
Avi Kivity17ac10a2007-01-05 16:36:40 -08002185static int paging64_init_context(struct kvm_vcpu *vcpu)
2186{
2187 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2188}
2189
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190static int paging32_init_context(struct kvm_vcpu *vcpu)
2191{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002192 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193
2194 context->new_cr3 = paging_new_cr3;
2195 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196 context->gva_to_gpa = paging32_gva_to_gpa;
2197 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002198 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002199 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002200 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201 context->root_level = PT32_ROOT_LEVEL;
2202 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002203 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204 return 0;
2205}
2206
2207static int paging32E_init_context(struct kvm_vcpu *vcpu)
2208{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002209 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210}
2211
Joerg Roedelfb72d162008-02-07 13:47:44 +01002212static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2213{
2214 struct kvm_mmu *context = &vcpu->arch.mmu;
2215
2216 context->new_cr3 = nonpaging_new_cr3;
2217 context->page_fault = tdp_page_fault;
2218 context->free = nonpaging_free;
2219 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002220 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002221 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002222 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002223 context->root_hpa = INVALID_PAGE;
2224
2225 if (!is_paging(vcpu)) {
2226 context->gva_to_gpa = nonpaging_gva_to_gpa;
2227 context->root_level = 0;
2228 } else if (is_long_mode(vcpu)) {
2229 context->gva_to_gpa = paging64_gva_to_gpa;
2230 context->root_level = PT64_ROOT_LEVEL;
2231 } else if (is_pae(vcpu)) {
2232 context->gva_to_gpa = paging64_gva_to_gpa;
2233 context->root_level = PT32E_ROOT_LEVEL;
2234 } else {
2235 context->gva_to_gpa = paging32_gva_to_gpa;
2236 context->root_level = PT32_ROOT_LEVEL;
2237 }
2238
2239 return 0;
2240}
2241
2242static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002244 int r;
2245
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002247 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248
2249 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002250 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002251 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002252 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002254 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002256 r = paging32_init_context(vcpu);
2257
2258 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2259
2260 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261}
2262
Joerg Roedelfb72d162008-02-07 13:47:44 +01002263static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2264{
Anthony Liguori35149e22008-04-02 14:46:56 -05002265 vcpu->arch.update_pte.pfn = bad_pfn;
2266
Joerg Roedelfb72d162008-02-07 13:47:44 +01002267 if (tdp_enabled)
2268 return init_kvm_tdp_mmu(vcpu);
2269 else
2270 return init_kvm_softmmu(vcpu);
2271}
2272
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2274{
2275 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002276 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2277 vcpu->arch.mmu.free(vcpu);
2278 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 }
2280}
2281
2282int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2283{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002284 destroy_kvm_mmu(vcpu);
2285 return init_kvm_mmu(vcpu);
2286}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002287EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002288
2289int kvm_mmu_load(struct kvm_vcpu *vcpu)
2290{
Avi Kivity714b93d2007-01-05 16:36:53 -08002291 int r;
2292
Avi Kivitye2dec932007-01-05 16:36:54 -08002293 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002294 if (r)
2295 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002296 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002297 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002298 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002299 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002300 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002301 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002302 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002303out:
2304 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002306EXPORT_SYMBOL_GPL(kvm_mmu_load);
2307
2308void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2309{
2310 mmu_free_roots(vcpu);
2311}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
Avi Kivity09072da2007-05-01 14:16:52 +03002313static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002314 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002315 u64 *spte)
2316{
2317 u64 pte;
2318 struct kvm_mmu_page *child;
2319
2320 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002321 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002322 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2323 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002324 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002325 else {
2326 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002327 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002328 }
2329 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002330 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002331 if (is_large_pte(pte))
2332 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002333}
2334
Avi Kivity00284252007-05-01 16:53:31 +03002335static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002336 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002337 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002338 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002339{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002340 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2341 if (!vcpu->arch.update_pte.largepage ||
2342 sp->role.glevels == PT32_ROOT_LEVEL) {
2343 ++vcpu->kvm->stat.mmu_pde_zapped;
2344 return;
2345 }
2346 }
Avi Kivity00284252007-05-01 16:53:31 +03002347
Avi Kivity4cee5762007-11-18 16:37:07 +02002348 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002349 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002350 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002351 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002352 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002353}
2354
Avi Kivity79539ce2007-11-21 02:06:21 +02002355static bool need_remote_flush(u64 old, u64 new)
2356{
2357 if (!is_shadow_present_pte(old))
2358 return false;
2359 if (!is_shadow_present_pte(new))
2360 return true;
2361 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2362 return true;
2363 old ^= PT64_NX_MASK;
2364 new ^= PT64_NX_MASK;
2365 return (old & ~new & PT64_PERM_MASK) != 0;
2366}
2367
2368static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2369{
2370 if (need_remote_flush(old, new))
2371 kvm_flush_remote_tlbs(vcpu->kvm);
2372 else
2373 kvm_mmu_flush_tlb(vcpu);
2374}
2375
Avi Kivity12b7d282007-09-23 14:10:49 +02002376static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2377{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002378 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002379
Sheng Yang7b523452008-04-25 21:13:50 +08002380 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002381}
2382
Avi Kivityd7824ff2007-12-30 12:29:05 +02002383static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2384 const u8 *new, int bytes)
2385{
2386 gfn_t gfn;
2387 int r;
2388 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002389 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002390
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002391 vcpu->arch.update_pte.largepage = 0;
2392
Avi Kivityd7824ff2007-12-30 12:29:05 +02002393 if (bytes != 4 && bytes != 8)
2394 return;
2395
2396 /*
2397 * Assume that the pte write on a page table of the same type
2398 * as the current vcpu paging mode. This is nearly always true
2399 * (might be false while changing modes). Note it is verified later
2400 * by update_pte().
2401 */
2402 if (is_pae(vcpu)) {
2403 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2404 if ((bytes == 4) && (gpa % 4 == 0)) {
2405 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2406 if (r)
2407 return;
2408 memcpy((void *)&gpte + (gpa % 8), new, 4);
2409 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2410 memcpy((void *)&gpte, new, 8);
2411 }
2412 } else {
2413 if ((bytes == 4) && (gpa % 4 == 0))
2414 memcpy((void *)&gpte, new, 4);
2415 }
2416 if (!is_present_pte(gpte))
2417 return;
2418 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002419
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002420 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2421 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2422 vcpu->arch.update_pte.largepage = 1;
2423 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002424 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002425 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002426 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002427
Anthony Liguori35149e22008-04-02 14:46:56 -05002428 if (is_error_pfn(pfn)) {
2429 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002430 return;
2431 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002432 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002433 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002434}
2435
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002436static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2437{
2438 u64 *spte = vcpu->arch.last_pte_updated;
2439
2440 if (spte
2441 && vcpu->arch.last_pte_gfn == gfn
2442 && shadow_accessed_mask
2443 && !(*spte & shadow_accessed_mask)
2444 && is_shadow_present_pte(*spte))
2445 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2446}
2447
Avi Kivity09072da2007-05-01 14:16:52 +03002448void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002449 const u8 *new, int bytes,
2450 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002451{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002452 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002453 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002454 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002455 struct hlist_head *bucket;
2456 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002457 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002458 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002459 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002460 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002461 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002462 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002463 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002464 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002465 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002466 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002467 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002468
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002469 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002470 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002471 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002472 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002473 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002474 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002475 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002476 if (guest_initiated) {
2477 if (gfn == vcpu->arch.last_pt_write_gfn
2478 && !last_updated_pte_accessed(vcpu)) {
2479 ++vcpu->arch.last_pt_write_count;
2480 if (vcpu->arch.last_pt_write_count >= 3)
2481 flooded = 1;
2482 } else {
2483 vcpu->arch.last_pt_write_gfn = gfn;
2484 vcpu->arch.last_pt_write_count = 1;
2485 vcpu->arch.last_pte_updated = NULL;
2486 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002487 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002488 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002489 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002490 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002491 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002492 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002493 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002494 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002495 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002496 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002497 /*
2498 * Misaligned accesses are too much trouble to fix
2499 * up; also, they usually indicate a page is not used
2500 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002501 *
2502 * If we're seeing too many writes to a page,
2503 * it may no longer be a page table, or we may be
2504 * forking, in which case it is better to unmap the
2505 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002506 */
2507 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002508 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002509 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2510 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002511 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002512 continue;
2513 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002514 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002515 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002516 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002517 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002518 page_offset <<= 1; /* 32->64 */
2519 /*
2520 * A 32-bit pde maps 4MB while the shadow pdes map
2521 * only 2MB. So we need to double the offset again
2522 * and zap two pdes instead of one.
2523 */
2524 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002525 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002526 page_offset <<= 1;
2527 npte = 2;
2528 }
Avi Kivityfce06572007-05-01 16:44:05 +03002529 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002530 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002531 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002532 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002533 }
Avi Kivity4db35312007-11-21 15:28:32 +02002534 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002535 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2536 gentry = 0;
2537 r = kvm_read_guest_atomic(vcpu->kvm,
2538 gpa & ~(u64)(pte_size - 1),
2539 &gentry, pte_size);
2540 new = (const void *)&gentry;
2541 if (r < 0)
2542 new = NULL;
2543 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002544 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002545 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002546 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002547 if (new)
2548 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002549 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002550 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002551 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002552 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002553 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002554 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002555 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2556 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2557 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002558 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002559}
2560
Avi Kivitya4360362007-01-05 16:36:45 -08002561int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2562{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002563 gpa_t gpa;
2564 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002565
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002566 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002567
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002568 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002569 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002570 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002571 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002572}
Avi Kivity577bdc42008-07-19 08:57:05 +03002573EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002574
Avi Kivity22d95b12007-09-14 20:26:06 +03002575void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002576{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002577 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002578 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002579
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002580 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002581 struct kvm_mmu_page, link);
2582 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002583 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002584 }
2585}
Avi Kivityebeace82007-01-05 16:36:47 -08002586
Avi Kivity30677142007-10-28 18:48:59 +02002587int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2588{
2589 int r;
2590 enum emulation_result er;
2591
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002592 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002593 if (r < 0)
2594 goto out;
2595
2596 if (!r) {
2597 r = 1;
2598 goto out;
2599 }
2600
Avi Kivityb733bfb2007-10-28 18:52:05 +02002601 r = mmu_topup_memory_caches(vcpu);
2602 if (r)
2603 goto out;
2604
Avi Kivity30677142007-10-28 18:48:59 +02002605 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002606
2607 switch (er) {
2608 case EMULATE_DONE:
2609 return 1;
2610 case EMULATE_DO_MMIO:
2611 ++vcpu->stat.mmio_exits;
2612 return 0;
2613 case EMULATE_FAIL:
2614 kvm_report_emulation_failure(vcpu, "pagetable");
2615 return 1;
2616 default:
2617 BUG();
2618 }
2619out:
Avi Kivity30677142007-10-28 18:48:59 +02002620 return r;
2621}
2622EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2623
Marcelo Tosattia7052892008-09-23 13:18:35 -03002624void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2625{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002626 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002627 kvm_mmu_flush_tlb(vcpu);
2628 ++vcpu->stat.invlpg;
2629}
2630EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2631
Joerg Roedel18552672008-02-07 13:47:41 +01002632void kvm_enable_tdp(void)
2633{
2634 tdp_enabled = true;
2635}
2636EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2637
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002638void kvm_disable_tdp(void)
2639{
2640 tdp_enabled = false;
2641}
2642EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2643
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644static void free_mmu_pages(struct kvm_vcpu *vcpu)
2645{
Avi Kivity4db35312007-11-21 15:28:32 +02002646 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002648 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2649 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002650 struct kvm_mmu_page, link);
2651 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002652 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002653 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002654 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655}
2656
2657static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2658{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002659 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 int i;
2661
2662 ASSERT(vcpu);
2663
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002664 if (vcpu->kvm->arch.n_requested_mmu_pages)
2665 vcpu->kvm->arch.n_free_mmu_pages =
2666 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002667 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002668 vcpu->kvm->arch.n_free_mmu_pages =
2669 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002670 /*
2671 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2672 * Therefore we need to allocate shadow page tables in the first
2673 * 4GB of memory, which happens to fit the DMA32 zone.
2674 */
2675 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2676 if (!page)
2677 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002678 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002679 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002680 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002681
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 return 0;
2683
2684error_1:
2685 free_mmu_pages(vcpu);
2686 return -ENOMEM;
2687}
2688
Ingo Molnar8018c272006-12-29 16:50:01 -08002689int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002692 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693
Ingo Molnar8018c272006-12-29 16:50:01 -08002694 return alloc_mmu_pages(vcpu);
2695}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696
Ingo Molnar8018c272006-12-29 16:50:01 -08002697int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2698{
2699 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002700 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002701
Ingo Molnar8018c272006-12-29 16:50:01 -08002702 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703}
2704
2705void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2706{
2707 ASSERT(vcpu);
2708
2709 destroy_kvm_mmu(vcpu);
2710 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002711 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
Avi Kivity90cb0522007-07-17 13:04:56 +03002714void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715{
Avi Kivity4db35312007-11-21 15:28:32 +02002716 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717
Avi Kivity2245a282008-08-27 16:32:24 +03002718 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002719 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 int i;
2721 u64 *pt;
2722
Sheng Yang291f26b2008-10-16 17:30:57 +08002723 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 continue;
2725
Avi Kivity4db35312007-11-21 15:28:32 +02002726 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2728 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002729 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 }
Avi Kivity171d5952008-08-27 16:40:51 +03002732 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002733 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002735
Avi Kivity90cb0522007-07-17 13:04:56 +03002736void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002737{
Avi Kivity4db35312007-11-21 15:28:32 +02002738 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002739
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002740 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002741 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002742 if (kvm_mmu_zap_page(kvm, sp))
2743 node = container_of(kvm->arch.active_mmu_pages.next,
2744 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002745 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002746
Avi Kivity90cb0522007-07-17 13:04:56 +03002747 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002748}
2749
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002750static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002751{
2752 struct kvm_mmu_page *page;
2753
2754 page = container_of(kvm->arch.active_mmu_pages.prev,
2755 struct kvm_mmu_page, link);
2756 kvm_mmu_zap_page(kvm, page);
2757}
2758
2759static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2760{
2761 struct kvm *kvm;
2762 struct kvm *kvm_freed = NULL;
2763 int cache_count = 0;
2764
2765 spin_lock(&kvm_lock);
2766
2767 list_for_each_entry(kvm, &vm_list, vm_list) {
2768 int npages;
2769
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002770 if (!down_read_trylock(&kvm->slots_lock))
2771 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002772 spin_lock(&kvm->mmu_lock);
2773 npages = kvm->arch.n_alloc_mmu_pages -
2774 kvm->arch.n_free_mmu_pages;
2775 cache_count += npages;
2776 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2777 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2778 cache_count--;
2779 kvm_freed = kvm;
2780 }
2781 nr_to_scan--;
2782
2783 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002784 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002785 }
2786 if (kvm_freed)
2787 list_move_tail(&kvm_freed->vm_list, &vm_list);
2788
2789 spin_unlock(&kvm_lock);
2790
2791 return cache_count;
2792}
2793
2794static struct shrinker mmu_shrinker = {
2795 .shrink = mmu_shrink,
2796 .seeks = DEFAULT_SEEKS * 10,
2797};
2798
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002799static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002800{
2801 if (pte_chain_cache)
2802 kmem_cache_destroy(pte_chain_cache);
2803 if (rmap_desc_cache)
2804 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002805 if (mmu_page_header_cache)
2806 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002807}
2808
Izik Eidus3ee16c82008-03-30 15:17:21 +03002809void kvm_mmu_module_exit(void)
2810{
2811 mmu_destroy_caches();
2812 unregister_shrinker(&mmu_shrinker);
2813}
2814
Avi Kivityb5a33a72007-04-15 16:31:09 +03002815int kvm_mmu_module_init(void)
2816{
2817 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2818 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002819 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002820 if (!pte_chain_cache)
2821 goto nomem;
2822 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2823 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002824 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002825 if (!rmap_desc_cache)
2826 goto nomem;
2827
Avi Kivityd3d25b02007-05-30 12:34:53 +03002828 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2829 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002830 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002831 if (!mmu_page_header_cache)
2832 goto nomem;
2833
Izik Eidus3ee16c82008-03-30 15:17:21 +03002834 register_shrinker(&mmu_shrinker);
2835
Avi Kivityb5a33a72007-04-15 16:31:09 +03002836 return 0;
2837
2838nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002839 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002840 return -ENOMEM;
2841}
2842
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002843/*
2844 * Caculate mmu pages needed for kvm.
2845 */
2846unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2847{
2848 int i;
2849 unsigned int nr_mmu_pages;
2850 unsigned int nr_pages = 0;
2851
2852 for (i = 0; i < kvm->nmemslots; i++)
2853 nr_pages += kvm->memslots[i].npages;
2854
2855 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2856 nr_mmu_pages = max(nr_mmu_pages,
2857 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2858
2859 return nr_mmu_pages;
2860}
2861
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002862static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2863 unsigned len)
2864{
2865 if (len > buffer->len)
2866 return NULL;
2867 return buffer->ptr;
2868}
2869
2870static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2871 unsigned len)
2872{
2873 void *ret;
2874
2875 ret = pv_mmu_peek_buffer(buffer, len);
2876 if (!ret)
2877 return ret;
2878 buffer->ptr += len;
2879 buffer->len -= len;
2880 buffer->processed += len;
2881 return ret;
2882}
2883
2884static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2885 gpa_t addr, gpa_t value)
2886{
2887 int bytes = 8;
2888 int r;
2889
2890 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2891 bytes = 4;
2892
2893 r = mmu_topup_memory_caches(vcpu);
2894 if (r)
2895 return r;
2896
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002897 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002898 return -EFAULT;
2899
2900 return 1;
2901}
2902
2903static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2904{
2905 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002906 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002907 return 1;
2908}
2909
2910static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2911{
2912 spin_lock(&vcpu->kvm->mmu_lock);
2913 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2914 spin_unlock(&vcpu->kvm->mmu_lock);
2915 return 1;
2916}
2917
2918static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2919 struct kvm_pv_mmu_op_buffer *buffer)
2920{
2921 struct kvm_mmu_op_header *header;
2922
2923 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2924 if (!header)
2925 return 0;
2926 switch (header->op) {
2927 case KVM_MMU_OP_WRITE_PTE: {
2928 struct kvm_mmu_op_write_pte *wpte;
2929
2930 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2931 if (!wpte)
2932 return 0;
2933 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2934 wpte->pte_val);
2935 }
2936 case KVM_MMU_OP_FLUSH_TLB: {
2937 struct kvm_mmu_op_flush_tlb *ftlb;
2938
2939 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2940 if (!ftlb)
2941 return 0;
2942 return kvm_pv_mmu_flush_tlb(vcpu);
2943 }
2944 case KVM_MMU_OP_RELEASE_PT: {
2945 struct kvm_mmu_op_release_pt *rpt;
2946
2947 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2948 if (!rpt)
2949 return 0;
2950 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2951 }
2952 default: return 0;
2953 }
2954}
2955
2956int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2957 gpa_t addr, unsigned long *ret)
2958{
2959 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002960 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002961
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002962 buffer->ptr = buffer->buf;
2963 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2964 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002965
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002966 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002967 if (r)
2968 goto out;
2969
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002970 while (buffer->len) {
2971 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002972 if (r < 0)
2973 goto out;
2974 if (r == 0)
2975 break;
2976 }
2977
2978 r = 1;
2979out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002980 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002981 return r;
2982}
2983
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002984#ifdef AUDIT
2985
2986static const char *audit_msg;
2987
2988static gva_t canonicalize(gva_t gva)
2989{
2990#ifdef CONFIG_X86_64
2991 gva = (long long)(gva << 16) >> 16;
2992#endif
2993 return gva;
2994}
2995
2996static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2997 gva_t va, int level)
2998{
2999 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3000 int i;
3001 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3002
3003 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3004 u64 ent = pt[i];
3005
Avi Kivityc7addb92007-09-16 18:58:32 +02003006 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003007 continue;
3008
3009 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003010 if (level > 1) {
3011 if (ent == shadow_notrap_nonpresent_pte)
3012 printk(KERN_ERR "audit: (%s) nontrapping pte"
3013 " in nonleaf level: levels %d gva %lx"
3014 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003015 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003016
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003017 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003018 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003019 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003020 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003021
Avi Kivityc7addb92007-09-16 18:58:32 +02003022 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003023 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003024 printk(KERN_ERR "xx audit error: (%s) levels %d"
3025 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003026 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003027 va, gpa, hpa, ent,
3028 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003029 else if (ent == shadow_notrap_nonpresent_pte
3030 && !is_error_hpa(hpa))
3031 printk(KERN_ERR "audit: (%s) notrap shadow,"
3032 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003033 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003034
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003035 }
3036 }
3037}
3038
3039static void audit_mappings(struct kvm_vcpu *vcpu)
3040{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003041 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003042
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003043 if (vcpu->arch.mmu.root_level == 4)
3044 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003045 else
3046 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003047 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003048 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003049 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003050 i << 30,
3051 2);
3052}
3053
3054static int count_rmaps(struct kvm_vcpu *vcpu)
3055{
3056 int nmaps = 0;
3057 int i, j, k;
3058
3059 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3060 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3061 struct kvm_rmap_desc *d;
3062
3063 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003064 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003065
Izik Eidus290fc382007-09-27 14:11:22 +02003066 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003067 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003068 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003069 ++nmaps;
3070 continue;
3071 }
Izik Eidus290fc382007-09-27 14:11:22 +02003072 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003073 while (d) {
3074 for (k = 0; k < RMAP_EXT; ++k)
3075 if (d->shadow_ptes[k])
3076 ++nmaps;
3077 else
3078 break;
3079 d = d->more;
3080 }
3081 }
3082 }
3083 return nmaps;
3084}
3085
3086static int count_writable_mappings(struct kvm_vcpu *vcpu)
3087{
3088 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003089 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003090 int i;
3091
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003092 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003093 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003094
Avi Kivity4db35312007-11-21 15:28:32 +02003095 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003096 continue;
3097
3098 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3099 u64 ent = pt[i];
3100
3101 if (!(ent & PT_PRESENT_MASK))
3102 continue;
3103 if (!(ent & PT_WRITABLE_MASK))
3104 continue;
3105 ++nmaps;
3106 }
3107 }
3108 return nmaps;
3109}
3110
3111static void audit_rmap(struct kvm_vcpu *vcpu)
3112{
3113 int n_rmap = count_rmaps(vcpu);
3114 int n_actual = count_writable_mappings(vcpu);
3115
3116 if (n_rmap != n_actual)
3117 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003118 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003119}
3120
3121static void audit_write_protection(struct kvm_vcpu *vcpu)
3122{
Avi Kivity4db35312007-11-21 15:28:32 +02003123 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003124 struct kvm_memory_slot *slot;
3125 unsigned long *rmapp;
3126 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003127
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003128 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003129 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003130 continue;
3131
Avi Kivity4db35312007-11-21 15:28:32 +02003132 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003133 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003134 rmapp = &slot->rmap[gfn - slot->base_gfn];
3135 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003136 printk(KERN_ERR "%s: (%s) shadow page has writable"
3137 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003138 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003139 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003140 }
3141}
3142
3143static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3144{
3145 int olddbg = dbg;
3146
3147 dbg = 0;
3148 audit_msg = msg;
3149 audit_rmap(vcpu);
3150 audit_write_protection(vcpu);
3151 audit_mappings(vcpu);
3152 dbg = olddbg;
3153}
3154
3155#endif