blob: 15850809b55b2bed64aeaa5bd3143571c2a45ecf [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 Kivity4db35312007-11-21 15:28:32 +02001474 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001475
Avi Kivity4db35312007-11-21 15:28:32 +02001476 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001477 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001478 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001479 }
1480}
1481
Avi Kivity38c335f2007-11-21 14:20:22 +02001482static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483{
Avi Kivity38c335f2007-11-21 14:20:22 +02001484 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001485 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
Sheng Yang291f26b2008-10-16 17:30:57 +08001487 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001490static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1491{
1492 int i;
1493 u64 *pt = sp->spt;
1494
1495 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1496 return;
1497
1498 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1499 if (pt[i] == shadow_notrap_nonpresent_pte)
1500 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1501 }
1502}
1503
Avi Kivity039576c2007-03-20 12:46:50 +02001504struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1505{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001506 struct page *page;
1507
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001508 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001509
1510 if (gpa == UNMAPPED_GVA)
1511 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001512
Izik Eidus72dc67a2008-02-10 18:04:15 +02001513 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001514
1515 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001516}
1517
Sheng Yang74be52e2008-10-09 16:01:56 +08001518/*
1519 * The function is based on mtrr_type_lookup() in
1520 * arch/x86/kernel/cpu/mtrr/generic.c
1521 */
1522static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1523 u64 start, u64 end)
1524{
1525 int i;
1526 u64 base, mask;
1527 u8 prev_match, curr_match;
1528 int num_var_ranges = KVM_NR_VAR_MTRR;
1529
1530 if (!mtrr_state->enabled)
1531 return 0xFF;
1532
1533 /* Make end inclusive end, instead of exclusive */
1534 end--;
1535
1536 /* Look in fixed ranges. Just return the type as per start */
1537 if (mtrr_state->have_fixed && (start < 0x100000)) {
1538 int idx;
1539
1540 if (start < 0x80000) {
1541 idx = 0;
1542 idx += (start >> 16);
1543 return mtrr_state->fixed_ranges[idx];
1544 } else if (start < 0xC0000) {
1545 idx = 1 * 8;
1546 idx += ((start - 0x80000) >> 14);
1547 return mtrr_state->fixed_ranges[idx];
1548 } else if (start < 0x1000000) {
1549 idx = 3 * 8;
1550 idx += ((start - 0xC0000) >> 12);
1551 return mtrr_state->fixed_ranges[idx];
1552 }
1553 }
1554
1555 /*
1556 * Look in variable ranges
1557 * Look of multiple ranges matching this address and pick type
1558 * as per MTRR precedence
1559 */
1560 if (!(mtrr_state->enabled & 2))
1561 return mtrr_state->def_type;
1562
1563 prev_match = 0xFF;
1564 for (i = 0; i < num_var_ranges; ++i) {
1565 unsigned short start_state, end_state;
1566
1567 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1568 continue;
1569
1570 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1571 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1572 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1573 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1574
1575 start_state = ((start & mask) == (base & mask));
1576 end_state = ((end & mask) == (base & mask));
1577 if (start_state != end_state)
1578 return 0xFE;
1579
1580 if ((start & mask) != (base & mask))
1581 continue;
1582
1583 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1584 if (prev_match == 0xFF) {
1585 prev_match = curr_match;
1586 continue;
1587 }
1588
1589 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1590 curr_match == MTRR_TYPE_UNCACHABLE)
1591 return MTRR_TYPE_UNCACHABLE;
1592
1593 if ((prev_match == MTRR_TYPE_WRBACK &&
1594 curr_match == MTRR_TYPE_WRTHROUGH) ||
1595 (prev_match == MTRR_TYPE_WRTHROUGH &&
1596 curr_match == MTRR_TYPE_WRBACK)) {
1597 prev_match = MTRR_TYPE_WRTHROUGH;
1598 curr_match = MTRR_TYPE_WRTHROUGH;
1599 }
1600
1601 if (prev_match != curr_match)
1602 return MTRR_TYPE_UNCACHABLE;
1603 }
1604
1605 if (prev_match != 0xFF)
1606 return prev_match;
1607
1608 return mtrr_state->def_type;
1609}
1610
1611static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1612{
1613 u8 mtrr;
1614
1615 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1616 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1617 if (mtrr == 0xfe || mtrr == 0xff)
1618 mtrr = MTRR_TYPE_WRBACK;
1619 return mtrr;
1620}
1621
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001622static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1623{
1624 unsigned index;
1625 struct hlist_head *bucket;
1626 struct kvm_mmu_page *s;
1627 struct hlist_node *node, *n;
1628
1629 index = kvm_page_table_hashfn(sp->gfn);
1630 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1631 /* don't unsync if pagetable is shadowed with multiple roles */
1632 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1633 if (s->gfn != sp->gfn || s->role.metaphysical)
1634 continue;
1635 if (s->role.word != sp->role.word)
1636 return 1;
1637 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001638 ++vcpu->kvm->stat.mmu_unsync;
1639 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001640
1641 if (sp->global) {
1642 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1643 ++vcpu->kvm->stat.mmu_unsync_global;
1644 } else
1645 kvm_mmu_mark_parents_unsync(vcpu, sp);
1646
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001647 mmu_convert_notrap(sp);
1648 return 0;
1649}
1650
1651static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1652 bool can_unsync)
1653{
1654 struct kvm_mmu_page *shadow;
1655
1656 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1657 if (shadow) {
1658 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1659 return 1;
1660 if (shadow->unsync)
1661 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001662 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001663 return kvm_unsync_page(vcpu, shadow);
1664 return 1;
1665 }
1666 return 0;
1667}
1668
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001669static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1670 unsigned pte_access, int user_fault,
1671 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001672 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001673 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001674{
1675 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001676 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001677 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001678 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1679
1680 if (!global && sp->global) {
1681 sp->global = 0;
1682 if (sp->unsync) {
1683 kvm_unlink_unsync_global(vcpu->kvm, sp);
1684 kvm_mmu_mark_parents_unsync(vcpu, sp);
1685 }
1686 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001687
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001688 /*
1689 * We don't set the accessed bit, since we sometimes want to see
1690 * whether the guest actually used the pte (in order to detect
1691 * demand paging).
1692 */
Sheng Yang7b523452008-04-25 21:13:50 +08001693 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001694 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001695 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001696 if (!dirty)
1697 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001698 if (pte_access & ACC_EXEC_MASK)
1699 spte |= shadow_x_mask;
1700 else
1701 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001702 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001703 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001704 if (largepage)
1705 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001706 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001707 if (!kvm_is_mmio_pfn(pfn)) {
1708 mt_mask = get_memory_type(vcpu, gfn) <<
1709 kvm_x86_ops->get_mt_mask_shift();
1710 mt_mask |= VMX_EPT_IGMT_BIT;
1711 } else
1712 mt_mask = MTRR_TYPE_UNCACHABLE <<
1713 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001714 spte |= mt_mask;
1715 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001716
Anthony Liguori35149e22008-04-02 14:46:56 -05001717 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001718
1719 if ((pte_access & ACC_WRITE_MASK)
1720 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001721
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001722 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1723 ret = 1;
1724 spte = shadow_trap_nonpresent_pte;
1725 goto set_pte;
1726 }
1727
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001728 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001729
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001730 /*
1731 * Optimization: for pte sync, if spte was writable the hash
1732 * lookup is unnecessary (and expensive). Write protection
1733 * is responsibility of mmu_get_page / kvm_sync_page.
1734 * Same reasoning can be applied to dirty page accounting.
1735 */
1736 if (!can_unsync && is_writeble_pte(*shadow_pte))
1737 goto set_pte;
1738
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001739 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001741 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001742 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001743 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001744 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001745 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001746 }
1747 }
1748
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001749 if (pte_access & ACC_WRITE_MASK)
1750 mark_page_dirty(vcpu->kvm, gfn);
1751
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001752set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001753 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001754 return ret;
1755}
1756
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001757static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1758 unsigned pt_access, unsigned pte_access,
1759 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001760 int *ptwrite, int largepage, int global,
1761 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001762{
1763 int was_rmapped = 0;
1764 int was_writeble = is_writeble_pte(*shadow_pte);
1765
1766 pgprintk("%s: spte %llx access %x write_fault %d"
1767 " user_fault %d gfn %lx\n",
1768 __func__, *shadow_pte, pt_access,
1769 write_fault, user_fault, gfn);
1770
1771 if (is_rmap_pte(*shadow_pte)) {
1772 /*
1773 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1774 * the parent of the now unreachable PTE.
1775 */
1776 if (largepage && !is_large_pte(*shadow_pte)) {
1777 struct kvm_mmu_page *child;
1778 u64 pte = *shadow_pte;
1779
1780 child = page_header(pte & PT64_BASE_ADDR_MASK);
1781 mmu_page_remove_parent_pte(child, shadow_pte);
1782 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1783 pgprintk("hfn old %lx new %lx\n",
1784 spte_to_pfn(*shadow_pte), pfn);
1785 rmap_remove(vcpu->kvm, shadow_pte);
1786 } else {
1787 if (largepage)
1788 was_rmapped = is_large_pte(*shadow_pte);
1789 else
1790 was_rmapped = 1;
1791 }
1792 }
1793 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001794 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001795 if (write_fault)
1796 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001797 kvm_x86_ops->tlb_flush(vcpu);
1798 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001799
1800 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1801 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1802 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1803 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1804 *shadow_pte, shadow_pte);
1805 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001806 ++vcpu->kvm->stat.lpages;
1807
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001808 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1809 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001810 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001811 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001812 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001813 } else {
1814 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001815 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001816 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001817 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001818 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001819 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001820 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001821 vcpu->arch.last_pte_gfn = gfn;
1822 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001823}
1824
Avi Kivity6aa8b732006-12-10 02:21:36 -08001825static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1826{
1827}
1828
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001829static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001830 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831{
Avi Kivity9f652d22008-12-25 14:54:25 +02001832 struct kvm_shadow_walk_iterator iterator;
1833 struct kvm_mmu_page *sp;
1834 int pt_write = 0;
1835 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836
Avi Kivity9f652d22008-12-25 14:54:25 +02001837 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1838 if (iterator.level == PT_PAGE_TABLE_LEVEL
1839 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1840 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1841 0, write, 1, &pt_write,
1842 largepage, 0, gfn, pfn, false);
1843 ++vcpu->stat.pf_fixed;
1844 break;
1845 }
1846
1847 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1848 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1849 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1850 iterator.level - 1,
1851 1, ACC_ALL, iterator.sptep);
1852 if (!sp) {
1853 pgprintk("nonpaging_map: ENOMEM\n");
1854 kvm_release_pfn_clean(pfn);
1855 return -ENOMEM;
1856 }
1857
1858 set_shadow_pte(iterator.sptep,
1859 __pa(sp->spt)
1860 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1861 | shadow_user_mask | shadow_x_mask);
1862 }
1863 }
1864 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865}
1866
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001867static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1868{
1869 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001870 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001871 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001872 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001873
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001874 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1875 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1876 largepage = 1;
1877 }
1878
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001879 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001880 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001881 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001882
Avi Kivityd196e342008-01-24 11:44:11 +02001883 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001884 if (is_error_pfn(pfn)) {
1885 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001886 return 1;
1887 }
1888
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001889 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001890 if (mmu_notifier_retry(vcpu, mmu_seq))
1891 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001892 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001893 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001894 spin_unlock(&vcpu->kvm->mmu_lock);
1895
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001896
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001897 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001898
1899out_unlock:
1900 spin_unlock(&vcpu->kvm->mmu_lock);
1901 kvm_release_pfn_clean(pfn);
1902 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001903}
1904
1905
Avi Kivity17ac10a2007-01-05 16:36:40 -08001906static void mmu_free_roots(struct kvm_vcpu *vcpu)
1907{
1908 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001909 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001910
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001911 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001912 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001913 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001914 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1915 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001916
Avi Kivity4db35312007-11-21 15:28:32 +02001917 sp = page_header(root);
1918 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001919 if (!sp->root_count && sp->role.invalid)
1920 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001921 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001922 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001923 return;
1924 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001925 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001926 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001927
Avi Kivity417726a2007-04-12 17:35:58 +03001928 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001929 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001930 sp = page_header(root);
1931 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001932 if (!sp->root_count && sp->role.invalid)
1933 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001934 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001936 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001937 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001938 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001939}
1940
1941static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1942{
1943 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001944 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001945 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001946 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001947
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001948 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001949
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001950 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1951 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001952
1953 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001954 if (tdp_enabled)
1955 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001956 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001957 PT64_ROOT_LEVEL, metaphysical,
1958 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001959 root = __pa(sp->spt);
1960 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001961 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001962 return;
1963 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001964 metaphysical = !is_paging(vcpu);
1965 if (tdp_enabled)
1966 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001967 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001968 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001969
1970 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001971 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1972 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1973 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001974 continue;
1975 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001976 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1977 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001978 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001979 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001980 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001981 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001982 root = __pa(sp->spt);
1983 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001985 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001986 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001987}
1988
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001989static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1990{
1991 int i;
1992 struct kvm_mmu_page *sp;
1993
1994 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1995 return;
1996 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1997 hpa_t root = vcpu->arch.mmu.root_hpa;
1998 sp = page_header(root);
1999 mmu_sync_children(vcpu, sp);
2000 return;
2001 }
2002 for (i = 0; i < 4; ++i) {
2003 hpa_t root = vcpu->arch.mmu.pae_root[i];
2004
2005 if (root) {
2006 root &= PT64_BASE_ADDR_MASK;
2007 sp = page_header(root);
2008 mmu_sync_children(vcpu, sp);
2009 }
2010 }
2011}
2012
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002013static void mmu_sync_global(struct kvm_vcpu *vcpu)
2014{
2015 struct kvm *kvm = vcpu->kvm;
2016 struct kvm_mmu_page *sp, *n;
2017
2018 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2019 kvm_sync_page(vcpu, sp);
2020}
2021
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002022void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2023{
2024 spin_lock(&vcpu->kvm->mmu_lock);
2025 mmu_sync_roots(vcpu);
2026 spin_unlock(&vcpu->kvm->mmu_lock);
2027}
2028
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002029void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2030{
2031 spin_lock(&vcpu->kvm->mmu_lock);
2032 mmu_sync_global(vcpu);
2033 spin_unlock(&vcpu->kvm->mmu_lock);
2034}
2035
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2037{
2038 return vaddr;
2039}
2040
2041static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002042 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043{
Avi Kivitye8332402007-12-09 18:43:00 +02002044 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002045 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002047 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002048 r = mmu_topup_memory_caches(vcpu);
2049 if (r)
2050 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002051
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002053 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054
Avi Kivitye8332402007-12-09 18:43:00 +02002055 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056
Avi Kivitye8332402007-12-09 18:43:00 +02002057 return nonpaging_map(vcpu, gva & PAGE_MASK,
2058 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059}
2060
Joerg Roedelfb72d162008-02-07 13:47:44 +01002061static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2062 u32 error_code)
2063{
Anthony Liguori35149e22008-04-02 14:46:56 -05002064 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002065 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002066 int largepage = 0;
2067 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002068 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002069
2070 ASSERT(vcpu);
2071 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2072
2073 r = mmu_topup_memory_caches(vcpu);
2074 if (r)
2075 return r;
2076
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002077 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2078 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2079 largepage = 1;
2080 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002081 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002082 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002083 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002084 if (is_error_pfn(pfn)) {
2085 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002086 return 1;
2087 }
2088 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002089 if (mmu_notifier_retry(vcpu, mmu_seq))
2090 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002091 kvm_mmu_free_some_pages(vcpu);
2092 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002093 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002094 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002095
2096 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002097
2098out_unlock:
2099 spin_unlock(&vcpu->kvm->mmu_lock);
2100 kvm_release_pfn_clean(pfn);
2101 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002102}
2103
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104static void nonpaging_free(struct kvm_vcpu *vcpu)
2105{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002106 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107}
2108
2109static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2110{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002111 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112
2113 context->new_cr3 = nonpaging_new_cr3;
2114 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 context->gva_to_gpa = nonpaging_gva_to_gpa;
2116 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002117 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002118 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002119 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002120 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002122 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 return 0;
2124}
2125
Avi Kivityd835dfe2007-11-21 02:57:59 +02002126void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002128 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002129 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130}
2131
2132static void paging_new_cr3(struct kvm_vcpu *vcpu)
2133{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002134 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002135 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136}
2137
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138static void inject_page_fault(struct kvm_vcpu *vcpu,
2139 u64 addr,
2140 u32 err_code)
2141{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002142 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143}
2144
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145static void paging_free(struct kvm_vcpu *vcpu)
2146{
2147 nonpaging_free(vcpu);
2148}
2149
2150#define PTTYPE 64
2151#include "paging_tmpl.h"
2152#undef PTTYPE
2153
2154#define PTTYPE 32
2155#include "paging_tmpl.h"
2156#undef PTTYPE
2157
Avi Kivity17ac10a2007-01-05 16:36:40 -08002158static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002160 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161
2162 ASSERT(is_pae(vcpu));
2163 context->new_cr3 = paging_new_cr3;
2164 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002166 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002167 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002168 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002170 context->root_level = level;
2171 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002172 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173 return 0;
2174}
2175
Avi Kivity17ac10a2007-01-05 16:36:40 -08002176static int paging64_init_context(struct kvm_vcpu *vcpu)
2177{
2178 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2179}
2180
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181static int paging32_init_context(struct kvm_vcpu *vcpu)
2182{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002183 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184
2185 context->new_cr3 = paging_new_cr3;
2186 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187 context->gva_to_gpa = paging32_gva_to_gpa;
2188 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002189 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002190 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002191 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 context->root_level = PT32_ROOT_LEVEL;
2193 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002194 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 return 0;
2196}
2197
2198static int paging32E_init_context(struct kvm_vcpu *vcpu)
2199{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002200 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201}
2202
Joerg Roedelfb72d162008-02-07 13:47:44 +01002203static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2204{
2205 struct kvm_mmu *context = &vcpu->arch.mmu;
2206
2207 context->new_cr3 = nonpaging_new_cr3;
2208 context->page_fault = tdp_page_fault;
2209 context->free = nonpaging_free;
2210 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002211 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002212 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002213 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002214 context->root_hpa = INVALID_PAGE;
2215
2216 if (!is_paging(vcpu)) {
2217 context->gva_to_gpa = nonpaging_gva_to_gpa;
2218 context->root_level = 0;
2219 } else if (is_long_mode(vcpu)) {
2220 context->gva_to_gpa = paging64_gva_to_gpa;
2221 context->root_level = PT64_ROOT_LEVEL;
2222 } else if (is_pae(vcpu)) {
2223 context->gva_to_gpa = paging64_gva_to_gpa;
2224 context->root_level = PT32E_ROOT_LEVEL;
2225 } else {
2226 context->gva_to_gpa = paging32_gva_to_gpa;
2227 context->root_level = PT32_ROOT_LEVEL;
2228 }
2229
2230 return 0;
2231}
2232
2233static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002235 int r;
2236
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239
2240 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002241 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002242 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002243 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002245 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002247 r = paging32_init_context(vcpu);
2248
2249 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2250
2251 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252}
2253
Joerg Roedelfb72d162008-02-07 13:47:44 +01002254static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2255{
Anthony Liguori35149e22008-04-02 14:46:56 -05002256 vcpu->arch.update_pte.pfn = bad_pfn;
2257
Joerg Roedelfb72d162008-02-07 13:47:44 +01002258 if (tdp_enabled)
2259 return init_kvm_tdp_mmu(vcpu);
2260 else
2261 return init_kvm_softmmu(vcpu);
2262}
2263
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2265{
2266 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002267 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2268 vcpu->arch.mmu.free(vcpu);
2269 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 }
2271}
2272
2273int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2274{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002275 destroy_kvm_mmu(vcpu);
2276 return init_kvm_mmu(vcpu);
2277}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002278EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002279
2280int kvm_mmu_load(struct kvm_vcpu *vcpu)
2281{
Avi Kivity714b93d2007-01-05 16:36:53 -08002282 int r;
2283
Avi Kivitye2dec932007-01-05 16:36:54 -08002284 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002285 if (r)
2286 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002287 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002288 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002289 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002290 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002291 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002292 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002293 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002294out:
2295 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002297EXPORT_SYMBOL_GPL(kvm_mmu_load);
2298
2299void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2300{
2301 mmu_free_roots(vcpu);
2302}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303
Avi Kivity09072da2007-05-01 14:16:52 +03002304static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002305 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002306 u64 *spte)
2307{
2308 u64 pte;
2309 struct kvm_mmu_page *child;
2310
2311 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002312 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002313 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2314 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002315 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002316 else {
2317 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002318 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002319 }
2320 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002321 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002322 if (is_large_pte(pte))
2323 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002324}
2325
Avi Kivity00284252007-05-01 16:53:31 +03002326static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002327 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002328 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002329 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002330{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002331 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2332 if (!vcpu->arch.update_pte.largepage ||
2333 sp->role.glevels == PT32_ROOT_LEVEL) {
2334 ++vcpu->kvm->stat.mmu_pde_zapped;
2335 return;
2336 }
2337 }
Avi Kivity00284252007-05-01 16:53:31 +03002338
Avi Kivity4cee5762007-11-18 16:37:07 +02002339 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002340 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002341 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002342 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002343 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002344}
2345
Avi Kivity79539ce2007-11-21 02:06:21 +02002346static bool need_remote_flush(u64 old, u64 new)
2347{
2348 if (!is_shadow_present_pte(old))
2349 return false;
2350 if (!is_shadow_present_pte(new))
2351 return true;
2352 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2353 return true;
2354 old ^= PT64_NX_MASK;
2355 new ^= PT64_NX_MASK;
2356 return (old & ~new & PT64_PERM_MASK) != 0;
2357}
2358
2359static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2360{
2361 if (need_remote_flush(old, new))
2362 kvm_flush_remote_tlbs(vcpu->kvm);
2363 else
2364 kvm_mmu_flush_tlb(vcpu);
2365}
2366
Avi Kivity12b7d282007-09-23 14:10:49 +02002367static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2368{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002369 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002370
Sheng Yang7b523452008-04-25 21:13:50 +08002371 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002372}
2373
Avi Kivityd7824ff2007-12-30 12:29:05 +02002374static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2375 const u8 *new, int bytes)
2376{
2377 gfn_t gfn;
2378 int r;
2379 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002380 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002381
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002382 vcpu->arch.update_pte.largepage = 0;
2383
Avi Kivityd7824ff2007-12-30 12:29:05 +02002384 if (bytes != 4 && bytes != 8)
2385 return;
2386
2387 /*
2388 * Assume that the pte write on a page table of the same type
2389 * as the current vcpu paging mode. This is nearly always true
2390 * (might be false while changing modes). Note it is verified later
2391 * by update_pte().
2392 */
2393 if (is_pae(vcpu)) {
2394 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2395 if ((bytes == 4) && (gpa % 4 == 0)) {
2396 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2397 if (r)
2398 return;
2399 memcpy((void *)&gpte + (gpa % 8), new, 4);
2400 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2401 memcpy((void *)&gpte, new, 8);
2402 }
2403 } else {
2404 if ((bytes == 4) && (gpa % 4 == 0))
2405 memcpy((void *)&gpte, new, 4);
2406 }
2407 if (!is_present_pte(gpte))
2408 return;
2409 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002410
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002411 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2412 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2413 vcpu->arch.update_pte.largepage = 1;
2414 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002415 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002416 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002417 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002418
Anthony Liguori35149e22008-04-02 14:46:56 -05002419 if (is_error_pfn(pfn)) {
2420 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002421 return;
2422 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002423 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002424 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002425}
2426
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002427static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2428{
2429 u64 *spte = vcpu->arch.last_pte_updated;
2430
2431 if (spte
2432 && vcpu->arch.last_pte_gfn == gfn
2433 && shadow_accessed_mask
2434 && !(*spte & shadow_accessed_mask)
2435 && is_shadow_present_pte(*spte))
2436 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2437}
2438
Avi Kivity09072da2007-05-01 14:16:52 +03002439void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002440 const u8 *new, int bytes,
2441 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002442{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002443 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002444 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002445 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002446 struct hlist_head *bucket;
2447 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002448 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002449 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002450 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002451 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002452 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002453 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002454 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002455 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002456 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002457 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002458 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002459
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002460 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002461 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002462 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002463 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002464 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002465 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002466 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002467 if (guest_initiated) {
2468 if (gfn == vcpu->arch.last_pt_write_gfn
2469 && !last_updated_pte_accessed(vcpu)) {
2470 ++vcpu->arch.last_pt_write_count;
2471 if (vcpu->arch.last_pt_write_count >= 3)
2472 flooded = 1;
2473 } else {
2474 vcpu->arch.last_pt_write_gfn = gfn;
2475 vcpu->arch.last_pt_write_count = 1;
2476 vcpu->arch.last_pte_updated = NULL;
2477 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002478 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002479 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002480 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002481 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002482 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002483 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002484 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002485 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002486 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002487 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002488 /*
2489 * Misaligned accesses are too much trouble to fix
2490 * up; also, they usually indicate a page is not used
2491 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002492 *
2493 * If we're seeing too many writes to a page,
2494 * it may no longer be a page table, or we may be
2495 * forking, in which case it is better to unmap the
2496 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002497 */
2498 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002499 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002500 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2501 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002502 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002503 continue;
2504 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002505 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002506 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002507 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002508 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002509 page_offset <<= 1; /* 32->64 */
2510 /*
2511 * A 32-bit pde maps 4MB while the shadow pdes map
2512 * only 2MB. So we need to double the offset again
2513 * and zap two pdes instead of one.
2514 */
2515 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002516 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002517 page_offset <<= 1;
2518 npte = 2;
2519 }
Avi Kivityfce06572007-05-01 16:44:05 +03002520 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002521 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002522 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002523 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002524 }
Avi Kivity4db35312007-11-21 15:28:32 +02002525 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002526 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2527 gentry = 0;
2528 r = kvm_read_guest_atomic(vcpu->kvm,
2529 gpa & ~(u64)(pte_size - 1),
2530 &gentry, pte_size);
2531 new = (const void *)&gentry;
2532 if (r < 0)
2533 new = NULL;
2534 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002535 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002536 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002537 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002538 if (new)
2539 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002540 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002541 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002542 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002543 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002544 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002545 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002546 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2547 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2548 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002549 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002550}
2551
Avi Kivitya4360362007-01-05 16:36:45 -08002552int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2553{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002554 gpa_t gpa;
2555 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002556
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002557 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002558
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002559 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002560 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002561 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002562 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002563}
Avi Kivity577bdc42008-07-19 08:57:05 +03002564EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002565
Avi Kivity22d95b12007-09-14 20:26:06 +03002566void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002567{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002568 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002569 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002570
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002571 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002572 struct kvm_mmu_page, link);
2573 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002574 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002575 }
2576}
Avi Kivityebeace82007-01-05 16:36:47 -08002577
Avi Kivity30677142007-10-28 18:48:59 +02002578int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2579{
2580 int r;
2581 enum emulation_result er;
2582
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002583 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002584 if (r < 0)
2585 goto out;
2586
2587 if (!r) {
2588 r = 1;
2589 goto out;
2590 }
2591
Avi Kivityb733bfb2007-10-28 18:52:05 +02002592 r = mmu_topup_memory_caches(vcpu);
2593 if (r)
2594 goto out;
2595
Avi Kivity30677142007-10-28 18:48:59 +02002596 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002597
2598 switch (er) {
2599 case EMULATE_DONE:
2600 return 1;
2601 case EMULATE_DO_MMIO:
2602 ++vcpu->stat.mmio_exits;
2603 return 0;
2604 case EMULATE_FAIL:
2605 kvm_report_emulation_failure(vcpu, "pagetable");
2606 return 1;
2607 default:
2608 BUG();
2609 }
2610out:
Avi Kivity30677142007-10-28 18:48:59 +02002611 return r;
2612}
2613EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2614
Marcelo Tosattia7052892008-09-23 13:18:35 -03002615void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2616{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002617 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002618 kvm_mmu_flush_tlb(vcpu);
2619 ++vcpu->stat.invlpg;
2620}
2621EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2622
Joerg Roedel18552672008-02-07 13:47:41 +01002623void kvm_enable_tdp(void)
2624{
2625 tdp_enabled = true;
2626}
2627EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2628
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002629void kvm_disable_tdp(void)
2630{
2631 tdp_enabled = false;
2632}
2633EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2634
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635static void free_mmu_pages(struct kvm_vcpu *vcpu)
2636{
Avi Kivity4db35312007-11-21 15:28:32 +02002637 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002639 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2640 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002641 struct kvm_mmu_page, link);
2642 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002643 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002644 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002645 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646}
2647
2648static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2649{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002650 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 int i;
2652
2653 ASSERT(vcpu);
2654
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002655 if (vcpu->kvm->arch.n_requested_mmu_pages)
2656 vcpu->kvm->arch.n_free_mmu_pages =
2657 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002658 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002659 vcpu->kvm->arch.n_free_mmu_pages =
2660 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002661 /*
2662 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2663 * Therefore we need to allocate shadow page tables in the first
2664 * 4GB of memory, which happens to fit the DMA32 zone.
2665 */
2666 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2667 if (!page)
2668 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002669 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002670 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002671 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002672
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 return 0;
2674
2675error_1:
2676 free_mmu_pages(vcpu);
2677 return -ENOMEM;
2678}
2679
Ingo Molnar8018c272006-12-29 16:50:01 -08002680int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002683 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684
Ingo Molnar8018c272006-12-29 16:50:01 -08002685 return alloc_mmu_pages(vcpu);
2686}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687
Ingo Molnar8018c272006-12-29 16:50:01 -08002688int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2689{
2690 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002691 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002692
Ingo Molnar8018c272006-12-29 16:50:01 -08002693 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694}
2695
2696void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2697{
2698 ASSERT(vcpu);
2699
2700 destroy_kvm_mmu(vcpu);
2701 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002702 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703}
2704
Avi Kivity90cb0522007-07-17 13:04:56 +03002705void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706{
Avi Kivity4db35312007-11-21 15:28:32 +02002707 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708
Avi Kivity2245a282008-08-27 16:32:24 +03002709 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002710 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 int i;
2712 u64 *pt;
2713
Sheng Yang291f26b2008-10-16 17:30:57 +08002714 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 continue;
2716
Avi Kivity4db35312007-11-21 15:28:32 +02002717 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2719 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002720 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 }
Avi Kivity171d5952008-08-27 16:40:51 +03002723 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002724 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002726
Avi Kivity90cb0522007-07-17 13:04:56 +03002727void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002728{
Avi Kivity4db35312007-11-21 15:28:32 +02002729 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002730
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002731 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002732 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002733 if (kvm_mmu_zap_page(kvm, sp))
2734 node = container_of(kvm->arch.active_mmu_pages.next,
2735 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002736 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002737
Avi Kivity90cb0522007-07-17 13:04:56 +03002738 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002739}
2740
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002741static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002742{
2743 struct kvm_mmu_page *page;
2744
2745 page = container_of(kvm->arch.active_mmu_pages.prev,
2746 struct kvm_mmu_page, link);
2747 kvm_mmu_zap_page(kvm, page);
2748}
2749
2750static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2751{
2752 struct kvm *kvm;
2753 struct kvm *kvm_freed = NULL;
2754 int cache_count = 0;
2755
2756 spin_lock(&kvm_lock);
2757
2758 list_for_each_entry(kvm, &vm_list, vm_list) {
2759 int npages;
2760
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002761 if (!down_read_trylock(&kvm->slots_lock))
2762 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002763 spin_lock(&kvm->mmu_lock);
2764 npages = kvm->arch.n_alloc_mmu_pages -
2765 kvm->arch.n_free_mmu_pages;
2766 cache_count += npages;
2767 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2768 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2769 cache_count--;
2770 kvm_freed = kvm;
2771 }
2772 nr_to_scan--;
2773
2774 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002775 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002776 }
2777 if (kvm_freed)
2778 list_move_tail(&kvm_freed->vm_list, &vm_list);
2779
2780 spin_unlock(&kvm_lock);
2781
2782 return cache_count;
2783}
2784
2785static struct shrinker mmu_shrinker = {
2786 .shrink = mmu_shrink,
2787 .seeks = DEFAULT_SEEKS * 10,
2788};
2789
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002790static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002791{
2792 if (pte_chain_cache)
2793 kmem_cache_destroy(pte_chain_cache);
2794 if (rmap_desc_cache)
2795 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002796 if (mmu_page_header_cache)
2797 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002798}
2799
Izik Eidus3ee16c82008-03-30 15:17:21 +03002800void kvm_mmu_module_exit(void)
2801{
2802 mmu_destroy_caches();
2803 unregister_shrinker(&mmu_shrinker);
2804}
2805
Avi Kivityb5a33a72007-04-15 16:31:09 +03002806int kvm_mmu_module_init(void)
2807{
2808 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2809 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002810 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002811 if (!pte_chain_cache)
2812 goto nomem;
2813 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2814 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002815 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002816 if (!rmap_desc_cache)
2817 goto nomem;
2818
Avi Kivityd3d25b02007-05-30 12:34:53 +03002819 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2820 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002821 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002822 if (!mmu_page_header_cache)
2823 goto nomem;
2824
Izik Eidus3ee16c82008-03-30 15:17:21 +03002825 register_shrinker(&mmu_shrinker);
2826
Avi Kivityb5a33a72007-04-15 16:31:09 +03002827 return 0;
2828
2829nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002830 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002831 return -ENOMEM;
2832}
2833
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002834/*
2835 * Caculate mmu pages needed for kvm.
2836 */
2837unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2838{
2839 int i;
2840 unsigned int nr_mmu_pages;
2841 unsigned int nr_pages = 0;
2842
2843 for (i = 0; i < kvm->nmemslots; i++)
2844 nr_pages += kvm->memslots[i].npages;
2845
2846 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2847 nr_mmu_pages = max(nr_mmu_pages,
2848 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2849
2850 return nr_mmu_pages;
2851}
2852
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002853static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2854 unsigned len)
2855{
2856 if (len > buffer->len)
2857 return NULL;
2858 return buffer->ptr;
2859}
2860
2861static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2862 unsigned len)
2863{
2864 void *ret;
2865
2866 ret = pv_mmu_peek_buffer(buffer, len);
2867 if (!ret)
2868 return ret;
2869 buffer->ptr += len;
2870 buffer->len -= len;
2871 buffer->processed += len;
2872 return ret;
2873}
2874
2875static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2876 gpa_t addr, gpa_t value)
2877{
2878 int bytes = 8;
2879 int r;
2880
2881 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2882 bytes = 4;
2883
2884 r = mmu_topup_memory_caches(vcpu);
2885 if (r)
2886 return r;
2887
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002888 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002889 return -EFAULT;
2890
2891 return 1;
2892}
2893
2894static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2895{
2896 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002897 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002898 return 1;
2899}
2900
2901static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2902{
2903 spin_lock(&vcpu->kvm->mmu_lock);
2904 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2905 spin_unlock(&vcpu->kvm->mmu_lock);
2906 return 1;
2907}
2908
2909static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2910 struct kvm_pv_mmu_op_buffer *buffer)
2911{
2912 struct kvm_mmu_op_header *header;
2913
2914 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2915 if (!header)
2916 return 0;
2917 switch (header->op) {
2918 case KVM_MMU_OP_WRITE_PTE: {
2919 struct kvm_mmu_op_write_pte *wpte;
2920
2921 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2922 if (!wpte)
2923 return 0;
2924 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2925 wpte->pte_val);
2926 }
2927 case KVM_MMU_OP_FLUSH_TLB: {
2928 struct kvm_mmu_op_flush_tlb *ftlb;
2929
2930 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2931 if (!ftlb)
2932 return 0;
2933 return kvm_pv_mmu_flush_tlb(vcpu);
2934 }
2935 case KVM_MMU_OP_RELEASE_PT: {
2936 struct kvm_mmu_op_release_pt *rpt;
2937
2938 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2939 if (!rpt)
2940 return 0;
2941 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2942 }
2943 default: return 0;
2944 }
2945}
2946
2947int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2948 gpa_t addr, unsigned long *ret)
2949{
2950 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002951 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002952
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002953 buffer->ptr = buffer->buf;
2954 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2955 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002956
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002957 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002958 if (r)
2959 goto out;
2960
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002961 while (buffer->len) {
2962 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002963 if (r < 0)
2964 goto out;
2965 if (r == 0)
2966 break;
2967 }
2968
2969 r = 1;
2970out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002971 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002972 return r;
2973}
2974
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002975#ifdef AUDIT
2976
2977static const char *audit_msg;
2978
2979static gva_t canonicalize(gva_t gva)
2980{
2981#ifdef CONFIG_X86_64
2982 gva = (long long)(gva << 16) >> 16;
2983#endif
2984 return gva;
2985}
2986
2987static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2988 gva_t va, int level)
2989{
2990 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2991 int i;
2992 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2993
2994 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2995 u64 ent = pt[i];
2996
Avi Kivityc7addb92007-09-16 18:58:32 +02002997 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002998 continue;
2999
3000 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003001 if (level > 1) {
3002 if (ent == shadow_notrap_nonpresent_pte)
3003 printk(KERN_ERR "audit: (%s) nontrapping pte"
3004 " in nonleaf level: levels %d gva %lx"
3005 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003006 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003007
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003008 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003009 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003010 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003011 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003012
Avi Kivityc7addb92007-09-16 18:58:32 +02003013 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003014 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003015 printk(KERN_ERR "xx audit error: (%s) levels %d"
3016 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003017 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003018 va, gpa, hpa, ent,
3019 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003020 else if (ent == shadow_notrap_nonpresent_pte
3021 && !is_error_hpa(hpa))
3022 printk(KERN_ERR "audit: (%s) notrap shadow,"
3023 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003024 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003025
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003026 }
3027 }
3028}
3029
3030static void audit_mappings(struct kvm_vcpu *vcpu)
3031{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003032 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003033
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003034 if (vcpu->arch.mmu.root_level == 4)
3035 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003036 else
3037 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003038 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003039 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003040 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003041 i << 30,
3042 2);
3043}
3044
3045static int count_rmaps(struct kvm_vcpu *vcpu)
3046{
3047 int nmaps = 0;
3048 int i, j, k;
3049
3050 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3051 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3052 struct kvm_rmap_desc *d;
3053
3054 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003055 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003056
Izik Eidus290fc382007-09-27 14:11:22 +02003057 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003058 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003059 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003060 ++nmaps;
3061 continue;
3062 }
Izik Eidus290fc382007-09-27 14:11:22 +02003063 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003064 while (d) {
3065 for (k = 0; k < RMAP_EXT; ++k)
3066 if (d->shadow_ptes[k])
3067 ++nmaps;
3068 else
3069 break;
3070 d = d->more;
3071 }
3072 }
3073 }
3074 return nmaps;
3075}
3076
3077static int count_writable_mappings(struct kvm_vcpu *vcpu)
3078{
3079 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003080 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003081 int i;
3082
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003083 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003084 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003085
Avi Kivity4db35312007-11-21 15:28:32 +02003086 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003087 continue;
3088
3089 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3090 u64 ent = pt[i];
3091
3092 if (!(ent & PT_PRESENT_MASK))
3093 continue;
3094 if (!(ent & PT_WRITABLE_MASK))
3095 continue;
3096 ++nmaps;
3097 }
3098 }
3099 return nmaps;
3100}
3101
3102static void audit_rmap(struct kvm_vcpu *vcpu)
3103{
3104 int n_rmap = count_rmaps(vcpu);
3105 int n_actual = count_writable_mappings(vcpu);
3106
3107 if (n_rmap != n_actual)
3108 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003109 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003110}
3111
3112static void audit_write_protection(struct kvm_vcpu *vcpu)
3113{
Avi Kivity4db35312007-11-21 15:28:32 +02003114 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003115 struct kvm_memory_slot *slot;
3116 unsigned long *rmapp;
3117 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003118
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003119 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003120 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003121 continue;
3122
Avi Kivity4db35312007-11-21 15:28:32 +02003123 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003124 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003125 rmapp = &slot->rmap[gfn - slot->base_gfn];
3126 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003127 printk(KERN_ERR "%s: (%s) shadow page has writable"
3128 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003129 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003130 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003131 }
3132}
3133
3134static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3135{
3136 int olddbg = dbg;
3137
3138 dbg = 0;
3139 audit_msg = msg;
3140 audit_rmap(vcpu);
3141 audit_write_protection(vcpu);
3142 audit_mappings(vcpu);
3143 dbg = olddbg;
3144}
3145
3146#endif