blob: 641c07844e6eb7ce8a03f81cc057b611082f72ef [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 Kivity3d000db2008-08-22 19:24:38 +0300148struct kvm_shadow_walk {
149 int (*entry)(struct kvm_shadow_walk *walk, struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +0800150 u64 addr, u64 *spte, int level);
Avi Kivity3d000db2008-08-22 19:24:38 +0300151};
152
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300153struct kvm_unsync_walk {
154 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
155};
156
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300157typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
158
Avi Kivityb5a33a72007-04-15 16:31:09 +0300159static struct kmem_cache *pte_chain_cache;
160static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300161static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300162
Avi Kivityc7addb92007-09-16 18:58:32 +0200163static u64 __read_mostly shadow_trap_nonpresent_pte;
164static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800165static u64 __read_mostly shadow_base_present_pte;
166static u64 __read_mostly shadow_nx_mask;
167static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
168static u64 __read_mostly shadow_user_mask;
169static u64 __read_mostly shadow_accessed_mask;
170static u64 __read_mostly shadow_dirty_mask;
Sheng Yang64d4d522008-10-09 16:01:57 +0800171static u64 __read_mostly shadow_mt_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200172
173void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
174{
175 shadow_trap_nonpresent_pte = trap_pte;
176 shadow_notrap_nonpresent_pte = notrap_pte;
177}
178EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
179
Sheng Yang7b523452008-04-25 21:13:50 +0800180void kvm_mmu_set_base_ptes(u64 base_pte)
181{
182 shadow_base_present_pte = base_pte;
183}
184EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
185
186void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang64d4d522008-10-09 16:01:57 +0800187 u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800188{
189 shadow_user_mask = user_mask;
190 shadow_accessed_mask = accessed_mask;
191 shadow_dirty_mask = dirty_mask;
192 shadow_nx_mask = nx_mask;
193 shadow_x_mask = x_mask;
Sheng Yang64d4d522008-10-09 16:01:57 +0800194 shadow_mt_mask = mt_mask;
Sheng Yang7b523452008-04-25 21:13:50 +0800195}
196EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static int is_write_protection(struct kvm_vcpu *vcpu)
199{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800200 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201}
202
203static int is_cpuid_PSE36(void)
204{
205 return 1;
206}
207
Avi Kivity73b10872007-01-26 00:56:41 -0800208static int is_nx(struct kvm_vcpu *vcpu)
209{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800210 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800211}
212
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213static int is_present_pte(unsigned long pte)
214{
215 return pte & PT_PRESENT_MASK;
216}
217
Avi Kivityc7addb92007-09-16 18:58:32 +0200218static int is_shadow_present_pte(u64 pte)
219{
Avi Kivityc7addb92007-09-16 18:58:32 +0200220 return pte != shadow_trap_nonpresent_pte
221 && pte != shadow_notrap_nonpresent_pte;
222}
223
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300224static int is_large_pte(u64 pte)
225{
226 return pte & PT_PAGE_SIZE_MASK;
227}
228
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229static int is_writeble_pte(unsigned long pte)
230{
231 return pte & PT_WRITABLE_MASK;
232}
233
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200234static int is_dirty_pte(unsigned long pte)
235{
Sheng Yang7b523452008-04-25 21:13:50 +0800236 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200237}
238
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800239static int is_rmap_pte(u64 pte)
240{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200241 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800242}
243
Anthony Liguori35149e22008-04-02 14:46:56 -0500244static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200245{
Anthony Liguori35149e22008-04-02 14:46:56 -0500246 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200247}
248
Avi Kivityda928522007-11-21 13:54:47 +0200249static gfn_t pse36_gfn_delta(u32 gpte)
250{
251 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
252
253 return (gpte & PT32_DIR_PSE36_MASK) << shift;
254}
255
Avi Kivitye663ee62007-05-31 15:46:04 +0300256static void set_shadow_pte(u64 *sptep, u64 spte)
257{
258#ifdef CONFIG_X86_64
259 set_64bit((unsigned long *)sptep, spte);
260#else
261 set_64bit((unsigned long long *)sptep, spte);
262#endif
263}
264
Avi Kivitye2dec932007-01-05 16:36:54 -0800265static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300266 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800267{
268 void *obj;
269
270 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800271 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800272 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300273 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800274 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800275 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800276 cache->objects[cache->nobjs++] = obj;
277 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800278 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800279}
280
281static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
282{
283 while (mc->nobjs)
284 kfree(mc->objects[--mc->nobjs]);
285}
286
Avi Kivityc1158e62007-07-20 08:18:27 +0300287static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300288 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300289{
290 struct page *page;
291
292 if (cache->nobjs >= min)
293 return 0;
294 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300295 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300296 if (!page)
297 return -ENOMEM;
298 set_page_private(page, 0);
299 cache->objects[cache->nobjs++] = page_address(page);
300 }
301 return 0;
302}
303
304static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
305{
306 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300307 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300308}
309
Avi Kivity8c438502007-04-16 11:53:17 +0300310static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
311{
312 int r;
313
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800314 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300315 pte_chain_cache, 4);
316 if (r)
317 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800318 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200319 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300320 if (r)
321 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800322 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300323 if (r)
324 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800325 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300326 mmu_page_header_cache, 4);
327out:
Avi Kivity8c438502007-04-16 11:53:17 +0300328 return r;
329}
330
Avi Kivity714b93d2007-01-05 16:36:53 -0800331static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
332{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
334 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
335 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
336 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800337}
338
339static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
340 size_t size)
341{
342 void *p;
343
344 BUG_ON(!mc->nobjs);
345 p = mc->objects[--mc->nobjs];
346 memset(p, 0, size);
347 return p;
348}
349
Avi Kivity714b93d2007-01-05 16:36:53 -0800350static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
351{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800353 sizeof(struct kvm_pte_chain));
354}
355
Avi Kivity90cb0522007-07-17 13:04:56 +0300356static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800357{
Avi Kivity90cb0522007-07-17 13:04:56 +0300358 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800359}
360
361static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
362{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800363 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800364 sizeof(struct kvm_rmap_desc));
365}
366
Avi Kivity90cb0522007-07-17 13:04:56 +0300367static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800368{
Avi Kivity90cb0522007-07-17 13:04:56 +0300369 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800370}
371
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800372/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300373 * Return the pointer to the largepage write count for a given
374 * gfn, handling slots that are not large page aligned.
375 */
376static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
377{
378 unsigned long idx;
379
380 idx = (gfn / KVM_PAGES_PER_HPAGE) -
381 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
382 return &slot->lpage_info[idx].write_count;
383}
384
385static void account_shadowed(struct kvm *kvm, gfn_t gfn)
386{
387 int *write_count;
388
Izik Eidus28430992008-10-03 17:40:32 +0300389 gfn = unalias_gfn(kvm, gfn);
390 write_count = slot_largepage_idx(gfn,
391 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300392 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300393}
394
395static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
396{
397 int *write_count;
398
Izik Eidus28430992008-10-03 17:40:32 +0300399 gfn = unalias_gfn(kvm, gfn);
400 write_count = slot_largepage_idx(gfn,
401 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300402 *write_count -= 1;
403 WARN_ON(*write_count < 0);
404}
405
406static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
407{
Izik Eidus28430992008-10-03 17:40:32 +0300408 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409 int *largepage_idx;
410
Izik Eidus28430992008-10-03 17:40:32 +0300411 gfn = unalias_gfn(kvm, gfn);
412 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300413 if (slot) {
414 largepage_idx = slot_largepage_idx(gfn, slot);
415 return *largepage_idx;
416 }
417
418 return 1;
419}
420
421static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
422{
423 struct vm_area_struct *vma;
424 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300425 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300426
427 addr = gfn_to_hva(kvm, gfn);
428 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300429 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300430
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300431 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300432 vma = find_vma(current->mm, addr);
433 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300434 ret = 1;
435 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300436
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300437 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300438}
439
440static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
441{
442 struct kvm_memory_slot *slot;
443
444 if (has_wrprotected_page(vcpu->kvm, large_gfn))
445 return 0;
446
447 if (!host_largepage_backed(vcpu->kvm, large_gfn))
448 return 0;
449
450 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
451 if (slot && slot->dirty_bitmap)
452 return 0;
453
454 return 1;
455}
456
457/*
Izik Eidus290fc382007-09-27 14:11:22 +0200458 * Take gfn and return the reverse mapping to it.
459 * Note: gfn must be unaliased before this function get called
460 */
461
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300462static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200463{
464 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300465 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200466
467 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300468 if (!lpage)
469 return &slot->rmap[gfn - slot->base_gfn];
470
471 idx = (gfn / KVM_PAGES_PER_HPAGE) -
472 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
473
474 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200475}
476
477/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800478 * Reverse mapping data structures:
479 *
Izik Eidus290fc382007-09-27 14:11:22 +0200480 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
481 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800482 *
Izik Eidus290fc382007-09-27 14:11:22 +0200483 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
484 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800485 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800487{
Avi Kivity4db35312007-11-21 15:28:32 +0200488 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800489 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200490 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 int i;
492
493 if (!is_rmap_pte(*spte))
494 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200495 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200496 sp = page_header(__pa(spte));
497 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300498 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200499 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200501 *rmapp = (unsigned long)spte;
502 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800503 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800504 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200505 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800506 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200507 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800508 } else {
509 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200510 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800511 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
512 desc = desc->more;
513 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800514 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800515 desc = desc->more;
516 }
517 for (i = 0; desc->shadow_ptes[i]; ++i)
518 ;
519 desc->shadow_ptes[i] = spte;
520 }
521}
522
Izik Eidus290fc382007-09-27 14:11:22 +0200523static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 struct kvm_rmap_desc *desc,
525 int i,
526 struct kvm_rmap_desc *prev_desc)
527{
528 int j;
529
530 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
531 ;
532 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000533 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800534 if (j != 0)
535 return;
536 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200537 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 else
539 if (prev_desc)
540 prev_desc->more = desc->more;
541 else
Izik Eidus290fc382007-09-27 14:11:22 +0200542 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300543 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800544}
545
Izik Eidus290fc382007-09-27 14:11:22 +0200546static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548 struct kvm_rmap_desc *desc;
549 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200550 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500551 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200552 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553 int i;
554
555 if (!is_rmap_pte(*spte))
556 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200557 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500558 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800559 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500560 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200561 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500562 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200563 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500564 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300565 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200566 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800567 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
568 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200569 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800570 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200571 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
573 spte, *spte);
574 BUG();
575 }
Izik Eidus290fc382007-09-27 14:11:22 +0200576 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 } else {
578 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200579 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 prev_desc = NULL;
581 while (desc) {
582 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
583 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200584 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800585 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 prev_desc);
587 return;
588 }
589 prev_desc = desc;
590 desc = desc->more;
591 }
592 BUG();
593 }
594}
595
Izik Eidus98348e92007-10-16 14:42:30 +0200596static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800597{
Avi Kivity374cbac2007-01-05 16:36:43 -0800598 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200599 struct kvm_rmap_desc *prev_desc;
600 u64 *prev_spte;
601 int i;
602
603 if (!*rmapp)
604 return NULL;
605 else if (!(*rmapp & 1)) {
606 if (!spte)
607 return (u64 *)*rmapp;
608 return NULL;
609 }
610 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
611 prev_desc = NULL;
612 prev_spte = NULL;
613 while (desc) {
614 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
615 if (prev_spte == spte)
616 return desc->shadow_ptes[i];
617 prev_spte = desc->shadow_ptes[i];
618 }
619 desc = desc->more;
620 }
621 return NULL;
622}
623
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200624static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200625{
Izik Eidus290fc382007-09-27 14:11:22 +0200626 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800627 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800628 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800629
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500630 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300631 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800632
Izik Eidus98348e92007-10-16 14:42:30 +0200633 spte = rmap_next(kvm, rmapp, NULL);
634 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800635 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800636 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800637 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800638 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200639 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800640 write_protected = 1;
641 }
Izik Eidus9647c142007-10-16 14:43:46 +0200642 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800643 }
Izik Eidus855149a2008-03-20 18:17:24 +0200644 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500645 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200646
647 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500648 pfn = spte_to_pfn(*spte);
649 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200650 }
651
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300652 /* check for huge page mappings */
653 rmapp = gfn_to_rmap(kvm, gfn, 1);
654 spte = rmap_next(kvm, rmapp, NULL);
655 while (spte) {
656 BUG_ON(!spte);
657 BUG_ON(!(*spte & PT_PRESENT_MASK));
658 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
659 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
660 if (is_writeble_pte(*spte)) {
661 rmap_remove(kvm, spte);
662 --kvm->stat.lpages;
663 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300664 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300665 write_protected = 1;
666 }
667 spte = rmap_next(kvm, rmapp, spte);
668 }
669
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200670 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800671}
672
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200673static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
674{
675 u64 *spte;
676 int need_tlb_flush = 0;
677
678 while ((spte = rmap_next(kvm, rmapp, NULL))) {
679 BUG_ON(!(*spte & PT_PRESENT_MASK));
680 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
681 rmap_remove(kvm, spte);
682 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
683 need_tlb_flush = 1;
684 }
685 return need_tlb_flush;
686}
687
688static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
689 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
690{
691 int i;
692 int retval = 0;
693
694 /*
695 * If mmap_sem isn't taken, we can look the memslots with only
696 * the mmu_lock by skipping over the slots with userspace_addr == 0.
697 */
698 for (i = 0; i < kvm->nmemslots; i++) {
699 struct kvm_memory_slot *memslot = &kvm->memslots[i];
700 unsigned long start = memslot->userspace_addr;
701 unsigned long end;
702
703 /* mmu_lock protects userspace_addr */
704 if (!start)
705 continue;
706
707 end = start + (memslot->npages << PAGE_SHIFT);
708 if (hva >= start && hva < end) {
709 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
710 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
711 retval |= handler(kvm,
712 &memslot->lpage_info[
713 gfn_offset /
714 KVM_PAGES_PER_HPAGE].rmap_pde);
715 }
716 }
717
718 return retval;
719}
720
721int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
722{
723 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
724}
725
726static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
727{
728 u64 *spte;
729 int young = 0;
730
Sheng Yang534e38b2008-09-08 15:12:30 +0800731 /* always return old for EPT */
732 if (!shadow_accessed_mask)
733 return 0;
734
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200735 spte = rmap_next(kvm, rmapp, NULL);
736 while (spte) {
737 int _young;
738 u64 _spte = *spte;
739 BUG_ON(!(_spte & PT_PRESENT_MASK));
740 _young = _spte & PT_ACCESSED_MASK;
741 if (_young) {
742 young = 1;
743 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
744 }
745 spte = rmap_next(kvm, rmapp, spte);
746 }
747 return young;
748}
749
750int kvm_age_hva(struct kvm *kvm, unsigned long hva)
751{
752 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
753}
754
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800755#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300756static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757{
Avi Kivity139bdb22007-01-05 16:36:50 -0800758 u64 *pos;
759 u64 *end;
760
Avi Kivity47ad8e62007-05-06 15:50:58 +0300761 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300762 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800763 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800764 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800766 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 return 1;
768}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800769#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770
Avi Kivity4db35312007-11-21 15:28:32 +0200771static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800772{
Avi Kivity4db35312007-11-21 15:28:32 +0200773 ASSERT(is_empty_shadow_page(sp->spt));
774 list_del(&sp->link);
775 __free_page(virt_to_page(sp->spt));
776 __free_page(virt_to_page(sp->gfns));
777 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800778 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800779}
780
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800781static unsigned kvm_page_table_hashfn(gfn_t gfn)
782{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200783 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800784}
785
Avi Kivity25c0de22007-01-05 16:36:42 -0800786static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
787 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788{
Avi Kivity4db35312007-11-21 15:28:32 +0200789 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800791 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
792 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
793 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200794 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800795 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200796 INIT_LIST_HEAD(&sp->oos_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200797 ASSERT(is_empty_shadow_page(sp->spt));
Sheng Yang291f26b2008-10-16 17:30:57 +0800798 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200799 sp->multimapped = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200800 sp->global = 1;
Avi Kivity4db35312007-11-21 15:28:32 +0200801 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800802 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200803 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804}
805
Avi Kivity714b93d2007-01-05 16:36:53 -0800806static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200807 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800808{
809 struct kvm_pte_chain *pte_chain;
810 struct hlist_node *node;
811 int i;
812
813 if (!parent_pte)
814 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200815 if (!sp->multimapped) {
816 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800817
818 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200819 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800820 return;
821 }
Avi Kivity4db35312007-11-21 15:28:32 +0200822 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800823 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200824 INIT_HLIST_HEAD(&sp->parent_ptes);
825 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800826 pte_chain->parent_ptes[0] = old;
827 }
Avi Kivity4db35312007-11-21 15:28:32 +0200828 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800829 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
830 continue;
831 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
832 if (!pte_chain->parent_ptes[i]) {
833 pte_chain->parent_ptes[i] = parent_pte;
834 return;
835 }
836 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800837 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800838 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200839 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800840 pte_chain->parent_ptes[0] = parent_pte;
841}
842
Avi Kivity4db35312007-11-21 15:28:32 +0200843static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800844 u64 *parent_pte)
845{
846 struct kvm_pte_chain *pte_chain;
847 struct hlist_node *node;
848 int i;
849
Avi Kivity4db35312007-11-21 15:28:32 +0200850 if (!sp->multimapped) {
851 BUG_ON(sp->parent_pte != parent_pte);
852 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800853 return;
854 }
Avi Kivity4db35312007-11-21 15:28:32 +0200855 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800856 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
857 if (!pte_chain->parent_ptes[i])
858 break;
859 if (pte_chain->parent_ptes[i] != parent_pte)
860 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800861 while (i + 1 < NR_PTE_CHAIN_ENTRIES
862 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800863 pte_chain->parent_ptes[i]
864 = pte_chain->parent_ptes[i + 1];
865 ++i;
866 }
867 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800868 if (i == 0) {
869 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300870 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200871 if (hlist_empty(&sp->parent_ptes)) {
872 sp->multimapped = 0;
873 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800874 }
875 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800876 return;
877 }
878 BUG();
879}
880
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300881
882static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
883 mmu_parent_walk_fn fn)
884{
885 struct kvm_pte_chain *pte_chain;
886 struct hlist_node *node;
887 struct kvm_mmu_page *parent_sp;
888 int i;
889
890 if (!sp->multimapped && sp->parent_pte) {
891 parent_sp = page_header(__pa(sp->parent_pte));
892 fn(vcpu, parent_sp);
893 mmu_parent_walk(vcpu, parent_sp, fn);
894 return;
895 }
896 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
897 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
898 if (!pte_chain->parent_ptes[i])
899 break;
900 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
901 fn(vcpu, parent_sp);
902 mmu_parent_walk(vcpu, parent_sp, fn);
903 }
904}
905
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300906static void kvm_mmu_update_unsync_bitmap(u64 *spte)
907{
908 unsigned int index;
909 struct kvm_mmu_page *sp = page_header(__pa(spte));
910
911 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200912 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
913 sp->unsync_children++;
914 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300915}
916
917static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
918{
919 struct kvm_pte_chain *pte_chain;
920 struct hlist_node *node;
921 int i;
922
923 if (!sp->parent_pte)
924 return;
925
926 if (!sp->multimapped) {
927 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
928 return;
929 }
930
931 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
932 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
933 if (!pte_chain->parent_ptes[i])
934 break;
935 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
936 }
937}
938
939static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
940{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300941 kvm_mmu_update_parents_unsync(sp);
942 return 1;
943}
944
945static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
946 struct kvm_mmu_page *sp)
947{
948 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
949 kvm_mmu_update_parents_unsync(sp);
950}
951
Avi Kivityd761a502008-05-29 14:55:03 +0300952static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
953 struct kvm_mmu_page *sp)
954{
955 int i;
956
957 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
958 sp->spt[i] = shadow_trap_nonpresent_pte;
959}
960
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300961static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
962 struct kvm_mmu_page *sp)
963{
964 return 1;
965}
966
Marcelo Tosattia7052892008-09-23 13:18:35 -0300967static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
968{
969}
970
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200971#define KVM_PAGE_ARRAY_NR 16
972
973struct kvm_mmu_pages {
974 struct mmu_page_and_offset {
975 struct kvm_mmu_page *sp;
976 unsigned int idx;
977 } page[KVM_PAGE_ARRAY_NR];
978 unsigned int nr;
979};
980
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300981#define for_each_unsync_children(bitmap, idx) \
982 for (idx = find_first_bit(bitmap, 512); \
983 idx < 512; \
984 idx = find_next_bit(bitmap, 512, idx+1))
985
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200986int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
987 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300988{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200989 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300990
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200991 if (sp->unsync)
992 for (i=0; i < pvec->nr; i++)
993 if (pvec->page[i].sp == sp)
994 return 0;
995
996 pvec->page[pvec->nr].sp = sp;
997 pvec->page[pvec->nr].idx = idx;
998 pvec->nr++;
999 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1000}
1001
1002static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1003 struct kvm_mmu_pages *pvec)
1004{
1005 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001006
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001007 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001008 u64 ent = sp->spt[i];
1009
1010 if (is_shadow_present_pte(ent)) {
1011 struct kvm_mmu_page *child;
1012 child = page_header(ent & PT64_BASE_ADDR_MASK);
1013
1014 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001015 if (mmu_pages_add(pvec, child, i))
1016 return -ENOSPC;
1017
1018 ret = __mmu_unsync_walk(child, pvec);
1019 if (!ret)
1020 __clear_bit(i, sp->unsync_child_bitmap);
1021 else if (ret > 0)
1022 nr_unsync_leaf += ret;
1023 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001024 return ret;
1025 }
1026
1027 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001028 nr_unsync_leaf++;
1029 if (mmu_pages_add(pvec, child, i))
1030 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001031 }
1032 }
1033 }
1034
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001035 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001036 sp->unsync_children = 0;
1037
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001038 return nr_unsync_leaf;
1039}
1040
1041static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1042 struct kvm_mmu_pages *pvec)
1043{
1044 if (!sp->unsync_children)
1045 return 0;
1046
1047 mmu_pages_add(pvec, sp, 0);
1048 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001049}
1050
Avi Kivity4db35312007-11-21 15:28:32 +02001051static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001052{
1053 unsigned index;
1054 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001055 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001056 struct hlist_node *node;
1057
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001058 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001059 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001060 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001061 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001062 if (sp->gfn == gfn && !sp->role.metaphysical
1063 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001064 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001065 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001066 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001067 }
1068 return NULL;
1069}
1070
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001071static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1072{
1073 list_del(&sp->oos_link);
1074 --kvm->stat.mmu_unsync_global;
1075}
1076
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001077static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1078{
1079 WARN_ON(!sp->unsync);
1080 sp->unsync = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001081 if (sp->global)
1082 kvm_unlink_unsync_global(kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001083 --kvm->stat.mmu_unsync;
1084}
1085
1086static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1087
1088static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1089{
1090 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1091 kvm_mmu_zap_page(vcpu->kvm, sp);
1092 return 1;
1093 }
1094
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001095 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1096 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001097 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001098 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1099 kvm_mmu_zap_page(vcpu->kvm, sp);
1100 return 1;
1101 }
1102
1103 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001104 return 0;
1105}
1106
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001107struct mmu_page_path {
1108 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1109 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001110};
1111
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001112#define for_each_sp(pvec, sp, parents, i) \
1113 for (i = mmu_pages_next(&pvec, &parents, -1), \
1114 sp = pvec.page[i].sp; \
1115 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1116 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001117
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001118int mmu_pages_next(struct kvm_mmu_pages *pvec, struct mmu_page_path *parents,
1119 int i)
1120{
1121 int n;
1122
1123 for (n = i+1; n < pvec->nr; n++) {
1124 struct kvm_mmu_page *sp = pvec->page[n].sp;
1125
1126 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1127 parents->idx[0] = pvec->page[n].idx;
1128 return n;
1129 }
1130
1131 parents->parent[sp->role.level-2] = sp;
1132 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1133 }
1134
1135 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001136}
1137
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001138void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001139{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001140 struct kvm_mmu_page *sp;
1141 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001142
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001143 do {
1144 unsigned int idx = parents->idx[level];
1145
1146 sp = parents->parent[level];
1147 if (!sp)
1148 return;
1149
1150 --sp->unsync_children;
1151 WARN_ON((int)sp->unsync_children < 0);
1152 __clear_bit(idx, sp->unsync_child_bitmap);
1153 level++;
1154 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1155}
1156
1157static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1158 struct mmu_page_path *parents,
1159 struct kvm_mmu_pages *pvec)
1160{
1161 parents->parent[parent->role.level-1] = NULL;
1162 pvec->nr = 0;
1163}
1164
1165static void mmu_sync_children(struct kvm_vcpu *vcpu,
1166 struct kvm_mmu_page *parent)
1167{
1168 int i;
1169 struct kvm_mmu_page *sp;
1170 struct mmu_page_path parents;
1171 struct kvm_mmu_pages pages;
1172
1173 kvm_mmu_pages_init(parent, &parents, &pages);
1174 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001175 int protected = 0;
1176
1177 for_each_sp(pages, sp, parents, i)
1178 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1179
1180 if (protected)
1181 kvm_flush_remote_tlbs(vcpu->kvm);
1182
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001183 for_each_sp(pages, sp, parents, i) {
1184 kvm_sync_page(vcpu, sp);
1185 mmu_pages_clear_parents(&parents);
1186 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001187 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001188 kvm_mmu_pages_init(parent, &parents, &pages);
1189 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001190}
1191
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001192static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1193 gfn_t gfn,
1194 gva_t gaddr,
1195 unsigned level,
1196 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001197 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001198 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001199{
1200 union kvm_mmu_page_role role;
1201 unsigned index;
1202 unsigned quadrant;
1203 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001204 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001205 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001206
1207 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001208 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001209 role.level = level;
1210 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001211 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001212 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001213 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1214 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1215 role.quadrant = quadrant;
1216 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001217 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001218 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001219 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001220 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1222 if (sp->gfn == gfn) {
1223 if (sp->unsync)
1224 if (kvm_sync_page(vcpu, sp))
1225 continue;
1226
1227 if (sp->role.word != role.word)
1228 continue;
1229
Avi Kivity4db35312007-11-21 15:28:32 +02001230 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001231 if (sp->unsync_children) {
1232 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1233 kvm_mmu_mark_parents_unsync(vcpu, sp);
1234 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001235 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001236 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001237 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001238 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001239 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1240 if (!sp)
1241 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001242 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001243 sp->gfn = gfn;
1244 sp->role = role;
1245 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001246 if (!metaphysical) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001247 if (rmap_write_protect(vcpu->kvm, gfn))
1248 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001249 account_shadowed(vcpu->kvm, gfn);
1250 }
Avi Kivity131d8272008-05-29 14:56:28 +03001251 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1252 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1253 else
1254 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001255 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001256}
1257
Avi Kivity3d000db2008-08-22 19:24:38 +03001258static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001259 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +03001260{
1261 hpa_t shadow_addr;
1262 int level;
1263 int r;
1264 u64 *sptep;
1265 unsigned index;
1266
1267 shadow_addr = vcpu->arch.mmu.root_hpa;
1268 level = vcpu->arch.mmu.shadow_root_level;
1269 if (level == PT32E_ROOT_LEVEL) {
1270 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1271 shadow_addr &= PT64_BASE_ADDR_MASK;
Marcelo Tosattieb64f1e2008-12-09 16:07:22 +01001272 if (!shadow_addr)
1273 return 1;
Avi Kivity3d000db2008-08-22 19:24:38 +03001274 --level;
1275 }
1276
1277 while (level >= PT_PAGE_TABLE_LEVEL) {
1278 index = SHADOW_PT_INDEX(addr, level);
1279 sptep = ((u64 *)__va(shadow_addr)) + index;
1280 r = walker->entry(walker, vcpu, addr, sptep, level);
1281 if (r)
1282 return r;
1283 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1284 --level;
1285 }
1286 return 0;
1287}
1288
Avi Kivity90cb0522007-07-17 13:04:56 +03001289static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001290 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001291{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001292 unsigned i;
1293 u64 *pt;
1294 u64 ent;
1295
Avi Kivity4db35312007-11-21 15:28:32 +02001296 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001297
Avi Kivity4db35312007-11-21 15:28:32 +02001298 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001299 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001300 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001301 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001302 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001303 }
1304 return;
1305 }
1306
1307 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1308 ent = pt[i];
1309
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001310 if (is_shadow_present_pte(ent)) {
1311 if (!is_large_pte(ent)) {
1312 ent &= PT64_BASE_ADDR_MASK;
1313 mmu_page_remove_parent_pte(page_header(ent),
1314 &pt[i]);
1315 } else {
1316 --kvm->stat.lpages;
1317 rmap_remove(kvm, &pt[i]);
1318 }
1319 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001320 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001321 }
Avi Kivitya4360362007-01-05 16:36:45 -08001322}
1323
Avi Kivity4db35312007-11-21 15:28:32 +02001324static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001325{
Avi Kivity4db35312007-11-21 15:28:32 +02001326 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001327}
1328
Avi Kivity12b7d282007-09-23 14:10:49 +02001329static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1330{
1331 int i;
1332
1333 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1334 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001335 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001336}
1337
Avi Kivity31aa2b42008-07-11 17:59:46 +03001338static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001339{
1340 u64 *parent_pte;
1341
Avi Kivity4db35312007-11-21 15:28:32 +02001342 while (sp->multimapped || sp->parent_pte) {
1343 if (!sp->multimapped)
1344 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001345 else {
1346 struct kvm_pte_chain *chain;
1347
Avi Kivity4db35312007-11-21 15:28:32 +02001348 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001349 struct kvm_pte_chain, link);
1350 parent_pte = chain->parent_ptes[0];
1351 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001352 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001353 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001354 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001355 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001356}
1357
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001358static int mmu_zap_unsync_children(struct kvm *kvm,
1359 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001360{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001361 int i, zapped = 0;
1362 struct mmu_page_path parents;
1363 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001364
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001365 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001366 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001367
1368 kvm_mmu_pages_init(parent, &parents, &pages);
1369 while (mmu_unsync_walk(parent, &pages)) {
1370 struct kvm_mmu_page *sp;
1371
1372 for_each_sp(pages, sp, parents, i) {
1373 kvm_mmu_zap_page(kvm, sp);
1374 mmu_pages_clear_parents(&parents);
1375 }
1376 zapped += pages.nr;
1377 kvm_mmu_pages_init(parent, &parents, &pages);
1378 }
1379
1380 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001381}
1382
Marcelo Tosatti07385412008-09-23 13:18:37 -03001383static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001384{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001385 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001386 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001387 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001388 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001389 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001390 kvm_flush_remote_tlbs(kvm);
1391 if (!sp->role.invalid && !sp->role.metaphysical)
1392 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001393 if (sp->unsync)
1394 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001395 if (!sp->root_count) {
1396 hlist_del(&sp->hash_link);
1397 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001398 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001399 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001400 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001401 kvm_reload_remote_mmus(kvm);
1402 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001403 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001404 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001405}
1406
Izik Eidus82ce2c92007-10-02 18:52:55 +02001407/*
1408 * Changing the number of mmu pages allocated to the vm
1409 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1410 */
1411void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1412{
1413 /*
1414 * If we set the number of mmu pages to be smaller be than the
1415 * number of actived pages , we must to free some mmu pages before we
1416 * change the value
1417 */
1418
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001419 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001420 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001421 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1422 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001423
1424 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1425 struct kvm_mmu_page *page;
1426
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001427 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001428 struct kvm_mmu_page, link);
1429 kvm_mmu_zap_page(kvm, page);
1430 n_used_mmu_pages--;
1431 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001432 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001433 }
1434 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001435 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1436 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001437
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001438 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001439}
1440
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001441static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001442{
1443 unsigned index;
1444 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001445 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001446 struct hlist_node *node, *n;
1447 int r;
1448
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001449 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001450 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001451 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001452 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001453 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1454 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001455 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001456 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001457 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001458 if (kvm_mmu_zap_page(kvm, sp))
1459 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001460 }
1461 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001462}
1463
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001464static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001465{
Avi Kivity4db35312007-11-21 15:28:32 +02001466 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001467
Avi Kivity4db35312007-11-21 15:28:32 +02001468 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001469 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001470 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001471 }
1472}
1473
Avi Kivity38c335f2007-11-21 14:20:22 +02001474static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475{
Avi Kivity38c335f2007-11-21 14:20:22 +02001476 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001477 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478
Sheng Yang291f26b2008-10-16 17:30:57 +08001479 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480}
1481
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001482static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1483{
1484 int i;
1485 u64 *pt = sp->spt;
1486
1487 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1488 return;
1489
1490 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1491 if (pt[i] == shadow_notrap_nonpresent_pte)
1492 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1493 }
1494}
1495
Avi Kivity039576c2007-03-20 12:46:50 +02001496struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1497{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001498 struct page *page;
1499
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001500 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001501
1502 if (gpa == UNMAPPED_GVA)
1503 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001504
Izik Eidus72dc67a2008-02-10 18:04:15 +02001505 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001506
1507 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001508}
1509
Sheng Yang74be52e2008-10-09 16:01:56 +08001510/*
1511 * The function is based on mtrr_type_lookup() in
1512 * arch/x86/kernel/cpu/mtrr/generic.c
1513 */
1514static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1515 u64 start, u64 end)
1516{
1517 int i;
1518 u64 base, mask;
1519 u8 prev_match, curr_match;
1520 int num_var_ranges = KVM_NR_VAR_MTRR;
1521
1522 if (!mtrr_state->enabled)
1523 return 0xFF;
1524
1525 /* Make end inclusive end, instead of exclusive */
1526 end--;
1527
1528 /* Look in fixed ranges. Just return the type as per start */
1529 if (mtrr_state->have_fixed && (start < 0x100000)) {
1530 int idx;
1531
1532 if (start < 0x80000) {
1533 idx = 0;
1534 idx += (start >> 16);
1535 return mtrr_state->fixed_ranges[idx];
1536 } else if (start < 0xC0000) {
1537 idx = 1 * 8;
1538 idx += ((start - 0x80000) >> 14);
1539 return mtrr_state->fixed_ranges[idx];
1540 } else if (start < 0x1000000) {
1541 idx = 3 * 8;
1542 idx += ((start - 0xC0000) >> 12);
1543 return mtrr_state->fixed_ranges[idx];
1544 }
1545 }
1546
1547 /*
1548 * Look in variable ranges
1549 * Look of multiple ranges matching this address and pick type
1550 * as per MTRR precedence
1551 */
1552 if (!(mtrr_state->enabled & 2))
1553 return mtrr_state->def_type;
1554
1555 prev_match = 0xFF;
1556 for (i = 0; i < num_var_ranges; ++i) {
1557 unsigned short start_state, end_state;
1558
1559 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1560 continue;
1561
1562 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1563 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1564 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1565 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1566
1567 start_state = ((start & mask) == (base & mask));
1568 end_state = ((end & mask) == (base & mask));
1569 if (start_state != end_state)
1570 return 0xFE;
1571
1572 if ((start & mask) != (base & mask))
1573 continue;
1574
1575 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1576 if (prev_match == 0xFF) {
1577 prev_match = curr_match;
1578 continue;
1579 }
1580
1581 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1582 curr_match == MTRR_TYPE_UNCACHABLE)
1583 return MTRR_TYPE_UNCACHABLE;
1584
1585 if ((prev_match == MTRR_TYPE_WRBACK &&
1586 curr_match == MTRR_TYPE_WRTHROUGH) ||
1587 (prev_match == MTRR_TYPE_WRTHROUGH &&
1588 curr_match == MTRR_TYPE_WRBACK)) {
1589 prev_match = MTRR_TYPE_WRTHROUGH;
1590 curr_match = MTRR_TYPE_WRTHROUGH;
1591 }
1592
1593 if (prev_match != curr_match)
1594 return MTRR_TYPE_UNCACHABLE;
1595 }
1596
1597 if (prev_match != 0xFF)
1598 return prev_match;
1599
1600 return mtrr_state->def_type;
1601}
1602
1603static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1604{
1605 u8 mtrr;
1606
1607 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1608 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1609 if (mtrr == 0xfe || mtrr == 0xff)
1610 mtrr = MTRR_TYPE_WRBACK;
1611 return mtrr;
1612}
1613
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001614static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1615{
1616 unsigned index;
1617 struct hlist_head *bucket;
1618 struct kvm_mmu_page *s;
1619 struct hlist_node *node, *n;
1620
1621 index = kvm_page_table_hashfn(sp->gfn);
1622 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1623 /* don't unsync if pagetable is shadowed with multiple roles */
1624 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1625 if (s->gfn != sp->gfn || s->role.metaphysical)
1626 continue;
1627 if (s->role.word != sp->role.word)
1628 return 1;
1629 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001630 ++vcpu->kvm->stat.mmu_unsync;
1631 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001632
1633 if (sp->global) {
1634 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1635 ++vcpu->kvm->stat.mmu_unsync_global;
1636 } else
1637 kvm_mmu_mark_parents_unsync(vcpu, sp);
1638
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001639 mmu_convert_notrap(sp);
1640 return 0;
1641}
1642
1643static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1644 bool can_unsync)
1645{
1646 struct kvm_mmu_page *shadow;
1647
1648 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1649 if (shadow) {
1650 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1651 return 1;
1652 if (shadow->unsync)
1653 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001654 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655 return kvm_unsync_page(vcpu, shadow);
1656 return 1;
1657 }
1658 return 0;
1659}
1660
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001661static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1662 unsigned pte_access, int user_fault,
1663 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001664 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001665 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001666{
1667 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001668 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001669 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001670 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1671
1672 if (!global && sp->global) {
1673 sp->global = 0;
1674 if (sp->unsync) {
1675 kvm_unlink_unsync_global(vcpu->kvm, sp);
1676 kvm_mmu_mark_parents_unsync(vcpu, sp);
1677 }
1678 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001679
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001680 /*
1681 * We don't set the accessed bit, since we sometimes want to see
1682 * whether the guest actually used the pte (in order to detect
1683 * demand paging).
1684 */
Sheng Yang7b523452008-04-25 21:13:50 +08001685 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001686 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001687 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001688 if (!dirty)
1689 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001690 if (pte_access & ACC_EXEC_MASK)
1691 spte |= shadow_x_mask;
1692 else
1693 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001694 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001695 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001696 if (largepage)
1697 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001698 if (mt_mask) {
1699 mt_mask = get_memory_type(vcpu, gfn) <<
1700 kvm_x86_ops->get_mt_mask_shift();
1701 spte |= mt_mask;
1702 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001703
Anthony Liguori35149e22008-04-02 14:46:56 -05001704 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001705
1706 if ((pte_access & ACC_WRITE_MASK)
1707 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001708
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001709 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1710 ret = 1;
1711 spte = shadow_trap_nonpresent_pte;
1712 goto set_pte;
1713 }
1714
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001715 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001716
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001717 /*
1718 * Optimization: for pte sync, if spte was writable the hash
1719 * lookup is unnecessary (and expensive). Write protection
1720 * is responsibility of mmu_get_page / kvm_sync_page.
1721 * Same reasoning can be applied to dirty page accounting.
1722 */
1723 if (!can_unsync && is_writeble_pte(*shadow_pte))
1724 goto set_pte;
1725
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001726 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001728 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001729 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001730 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001731 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001732 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001733 }
1734 }
1735
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001736 if (pte_access & ACC_WRITE_MASK)
1737 mark_page_dirty(vcpu->kvm, gfn);
1738
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001739set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001741 return ret;
1742}
1743
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001744static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1745 unsigned pt_access, unsigned pte_access,
1746 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001747 int *ptwrite, int largepage, int global,
1748 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001749{
1750 int was_rmapped = 0;
1751 int was_writeble = is_writeble_pte(*shadow_pte);
1752
1753 pgprintk("%s: spte %llx access %x write_fault %d"
1754 " user_fault %d gfn %lx\n",
1755 __func__, *shadow_pte, pt_access,
1756 write_fault, user_fault, gfn);
1757
1758 if (is_rmap_pte(*shadow_pte)) {
1759 /*
1760 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1761 * the parent of the now unreachable PTE.
1762 */
1763 if (largepage && !is_large_pte(*shadow_pte)) {
1764 struct kvm_mmu_page *child;
1765 u64 pte = *shadow_pte;
1766
1767 child = page_header(pte & PT64_BASE_ADDR_MASK);
1768 mmu_page_remove_parent_pte(child, shadow_pte);
1769 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1770 pgprintk("hfn old %lx new %lx\n",
1771 spte_to_pfn(*shadow_pte), pfn);
1772 rmap_remove(vcpu->kvm, shadow_pte);
1773 } else {
1774 if (largepage)
1775 was_rmapped = is_large_pte(*shadow_pte);
1776 else
1777 was_rmapped = 1;
1778 }
1779 }
1780 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001781 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001782 if (write_fault)
1783 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001784 kvm_x86_ops->tlb_flush(vcpu);
1785 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001786
1787 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1788 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1789 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1790 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1791 *shadow_pte, shadow_pte);
1792 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001793 ++vcpu->kvm->stat.lpages;
1794
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001795 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1796 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001797 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001798 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001799 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001800 } else {
1801 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001802 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001803 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001804 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001805 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001806 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001807 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001808 vcpu->arch.last_pte_gfn = gfn;
1809 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001810}
1811
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1813{
1814}
1815
Avi Kivity140754b2008-08-22 19:28:04 +03001816struct direct_shadow_walk {
1817 struct kvm_shadow_walk walker;
1818 pfn_t pfn;
1819 int write;
1820 int largepage;
1821 int pt_write;
1822};
1823
1824static int direct_map_entry(struct kvm_shadow_walk *_walk,
1825 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001826 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001827{
1828 struct direct_shadow_walk *walk =
1829 container_of(_walk, struct direct_shadow_walk, walker);
1830 struct kvm_mmu_page *sp;
1831 gfn_t pseudo_gfn;
1832 gfn_t gfn = addr >> PAGE_SHIFT;
1833
1834 if (level == PT_PAGE_TABLE_LEVEL
1835 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1836 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1837 0, walk->write, 1, &walk->pt_write,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001838 walk->largepage, 0, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001839 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001840 return 1;
1841 }
1842
1843 if (*sptep == shadow_trap_nonpresent_pte) {
1844 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001845 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001846 1, ACC_ALL, sptep);
1847 if (!sp) {
1848 pgprintk("nonpaging_map: ENOMEM\n");
1849 kvm_release_pfn_clean(walk->pfn);
1850 return -ENOMEM;
1851 }
1852
1853 set_shadow_pte(sptep,
1854 __pa(sp->spt)
1855 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1856 | shadow_user_mask | shadow_x_mask);
1857 }
1858 return 0;
1859}
1860
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001861static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001862 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Avi Kivity140754b2008-08-22 19:28:04 +03001864 int r;
1865 struct direct_shadow_walk walker = {
1866 .walker = { .entry = direct_map_entry, },
1867 .pfn = pfn,
1868 .largepage = largepage,
1869 .write = write,
1870 .pt_write = 0,
1871 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001873 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001874 if (r < 0)
1875 return r;
1876 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877}
1878
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001879static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1880{
1881 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001882 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001883 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001884 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001885
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001886 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1887 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1888 largepage = 1;
1889 }
1890
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001891 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001892 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001893 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001894
Avi Kivityd196e342008-01-24 11:44:11 +02001895 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001896 if (is_error_pfn(pfn)) {
1897 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001898 return 1;
1899 }
1900
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001901 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001902 if (mmu_notifier_retry(vcpu, mmu_seq))
1903 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001904 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001905 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001906 spin_unlock(&vcpu->kvm->mmu_lock);
1907
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001908
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001909 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001910
1911out_unlock:
1912 spin_unlock(&vcpu->kvm->mmu_lock);
1913 kvm_release_pfn_clean(pfn);
1914 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001915}
1916
1917
Avi Kivity17ac10a2007-01-05 16:36:40 -08001918static void mmu_free_roots(struct kvm_vcpu *vcpu)
1919{
1920 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001921 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001922
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001923 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001924 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001925 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001926 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1927 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001928
Avi Kivity4db35312007-11-21 15:28:32 +02001929 sp = page_header(root);
1930 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001931 if (!sp->root_count && sp->role.invalid)
1932 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001933 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001934 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001935 return;
1936 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001937 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001938 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001939
Avi Kivity417726a2007-04-12 17:35:58 +03001940 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001941 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001942 sp = page_header(root);
1943 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001944 if (!sp->root_count && sp->role.invalid)
1945 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001946 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001947 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001948 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001949 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001950 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001951}
1952
1953static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1954{
1955 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001956 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001957 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001958 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001959
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001960 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001961
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001962 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1963 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001964
1965 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001966 if (tdp_enabled)
1967 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001968 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001969 PT64_ROOT_LEVEL, metaphysical,
1970 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001971 root = __pa(sp->spt);
1972 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001973 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001974 return;
1975 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001976 metaphysical = !is_paging(vcpu);
1977 if (tdp_enabled)
1978 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001979 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001980 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001981
1982 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001983 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1984 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1985 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001986 continue;
1987 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001988 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1989 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001990 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001991 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001992 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001993 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001994 root = __pa(sp->spt);
1995 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001996 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001997 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001998 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001999}
2000
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002001static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2002{
2003 int i;
2004 struct kvm_mmu_page *sp;
2005
2006 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2007 return;
2008 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2009 hpa_t root = vcpu->arch.mmu.root_hpa;
2010 sp = page_header(root);
2011 mmu_sync_children(vcpu, sp);
2012 return;
2013 }
2014 for (i = 0; i < 4; ++i) {
2015 hpa_t root = vcpu->arch.mmu.pae_root[i];
2016
2017 if (root) {
2018 root &= PT64_BASE_ADDR_MASK;
2019 sp = page_header(root);
2020 mmu_sync_children(vcpu, sp);
2021 }
2022 }
2023}
2024
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002025static void mmu_sync_global(struct kvm_vcpu *vcpu)
2026{
2027 struct kvm *kvm = vcpu->kvm;
2028 struct kvm_mmu_page *sp, *n;
2029
2030 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2031 kvm_sync_page(vcpu, sp);
2032}
2033
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002034void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2035{
2036 spin_lock(&vcpu->kvm->mmu_lock);
2037 mmu_sync_roots(vcpu);
2038 spin_unlock(&vcpu->kvm->mmu_lock);
2039}
2040
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002041void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2042{
2043 spin_lock(&vcpu->kvm->mmu_lock);
2044 mmu_sync_global(vcpu);
2045 spin_unlock(&vcpu->kvm->mmu_lock);
2046}
2047
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2049{
2050 return vaddr;
2051}
2052
2053static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002054 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055{
Avi Kivitye8332402007-12-09 18:43:00 +02002056 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002057 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002059 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002060 r = mmu_topup_memory_caches(vcpu);
2061 if (r)
2062 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002063
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002065 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066
Avi Kivitye8332402007-12-09 18:43:00 +02002067 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068
Avi Kivitye8332402007-12-09 18:43:00 +02002069 return nonpaging_map(vcpu, gva & PAGE_MASK,
2070 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071}
2072
Joerg Roedelfb72d162008-02-07 13:47:44 +01002073static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2074 u32 error_code)
2075{
Anthony Liguori35149e22008-04-02 14:46:56 -05002076 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002077 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002078 int largepage = 0;
2079 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002080 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002081
2082 ASSERT(vcpu);
2083 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2084
2085 r = mmu_topup_memory_caches(vcpu);
2086 if (r)
2087 return r;
2088
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002089 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2090 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2091 largepage = 1;
2092 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002093 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002094 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002095 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002096 if (is_error_pfn(pfn)) {
2097 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002098 return 1;
2099 }
2100 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002101 if (mmu_notifier_retry(vcpu, mmu_seq))
2102 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002103 kvm_mmu_free_some_pages(vcpu);
2104 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002105 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002106 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002107
2108 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002109
2110out_unlock:
2111 spin_unlock(&vcpu->kvm->mmu_lock);
2112 kvm_release_pfn_clean(pfn);
2113 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002114}
2115
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116static void nonpaging_free(struct kvm_vcpu *vcpu)
2117{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002118 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119}
2120
2121static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2122{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002123 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124
2125 context->new_cr3 = nonpaging_new_cr3;
2126 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 context->gva_to_gpa = nonpaging_gva_to_gpa;
2128 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002129 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002130 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002131 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002132 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002134 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135 return 0;
2136}
2137
Avi Kivityd835dfe2007-11-21 02:57:59 +02002138void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002140 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002141 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142}
2143
2144static void paging_new_cr3(struct kvm_vcpu *vcpu)
2145{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002146 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002147 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148}
2149
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150static void inject_page_fault(struct kvm_vcpu *vcpu,
2151 u64 addr,
2152 u32 err_code)
2153{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002154 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155}
2156
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157static void paging_free(struct kvm_vcpu *vcpu)
2158{
2159 nonpaging_free(vcpu);
2160}
2161
2162#define PTTYPE 64
2163#include "paging_tmpl.h"
2164#undef PTTYPE
2165
2166#define PTTYPE 32
2167#include "paging_tmpl.h"
2168#undef PTTYPE
2169
Avi Kivity17ac10a2007-01-05 16:36:40 -08002170static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002172 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173
2174 ASSERT(is_pae(vcpu));
2175 context->new_cr3 = paging_new_cr3;
2176 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002178 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002179 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002180 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002182 context->root_level = level;
2183 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002184 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 return 0;
2186}
2187
Avi Kivity17ac10a2007-01-05 16:36:40 -08002188static int paging64_init_context(struct kvm_vcpu *vcpu)
2189{
2190 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2191}
2192
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193static int paging32_init_context(struct kvm_vcpu *vcpu)
2194{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002195 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196
2197 context->new_cr3 = paging_new_cr3;
2198 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 context->gva_to_gpa = paging32_gva_to_gpa;
2200 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002201 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002202 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002203 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204 context->root_level = PT32_ROOT_LEVEL;
2205 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002206 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 return 0;
2208}
2209
2210static int paging32E_init_context(struct kvm_vcpu *vcpu)
2211{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002212 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213}
2214
Joerg Roedelfb72d162008-02-07 13:47:44 +01002215static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2216{
2217 struct kvm_mmu *context = &vcpu->arch.mmu;
2218
2219 context->new_cr3 = nonpaging_new_cr3;
2220 context->page_fault = tdp_page_fault;
2221 context->free = nonpaging_free;
2222 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002223 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002224 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002225 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002226 context->root_hpa = INVALID_PAGE;
2227
2228 if (!is_paging(vcpu)) {
2229 context->gva_to_gpa = nonpaging_gva_to_gpa;
2230 context->root_level = 0;
2231 } else if (is_long_mode(vcpu)) {
2232 context->gva_to_gpa = paging64_gva_to_gpa;
2233 context->root_level = PT64_ROOT_LEVEL;
2234 } else if (is_pae(vcpu)) {
2235 context->gva_to_gpa = paging64_gva_to_gpa;
2236 context->root_level = PT32E_ROOT_LEVEL;
2237 } else {
2238 context->gva_to_gpa = paging32_gva_to_gpa;
2239 context->root_level = PT32_ROOT_LEVEL;
2240 }
2241
2242 return 0;
2243}
2244
2245static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246{
2247 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002248 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249
2250 if (!is_paging(vcpu))
2251 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002252 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253 return paging64_init_context(vcpu);
2254 else if (is_pae(vcpu))
2255 return paging32E_init_context(vcpu);
2256 else
2257 return paging32_init_context(vcpu);
2258}
2259
Joerg Roedelfb72d162008-02-07 13:47:44 +01002260static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2261{
Anthony Liguori35149e22008-04-02 14:46:56 -05002262 vcpu->arch.update_pte.pfn = bad_pfn;
2263
Joerg Roedelfb72d162008-02-07 13:47:44 +01002264 if (tdp_enabled)
2265 return init_kvm_tdp_mmu(vcpu);
2266 else
2267 return init_kvm_softmmu(vcpu);
2268}
2269
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2271{
2272 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002273 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2274 vcpu->arch.mmu.free(vcpu);
2275 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 }
2277}
2278
2279int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2280{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002281 destroy_kvm_mmu(vcpu);
2282 return init_kvm_mmu(vcpu);
2283}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002284EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002285
2286int kvm_mmu_load(struct kvm_vcpu *vcpu)
2287{
Avi Kivity714b93d2007-01-05 16:36:53 -08002288 int r;
2289
Avi Kivitye2dec932007-01-05 16:36:54 -08002290 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002291 if (r)
2292 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002293 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002294 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002295 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002296 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002297 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002298 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002299 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002300out:
2301 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002303EXPORT_SYMBOL_GPL(kvm_mmu_load);
2304
2305void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2306{
2307 mmu_free_roots(vcpu);
2308}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309
Avi Kivity09072da2007-05-01 14:16:52 +03002310static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002311 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002312 u64 *spte)
2313{
2314 u64 pte;
2315 struct kvm_mmu_page *child;
2316
2317 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002318 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002319 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2320 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002321 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002322 else {
2323 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002324 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002325 }
2326 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002327 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002328 if (is_large_pte(pte))
2329 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002330}
2331
Avi Kivity00284252007-05-01 16:53:31 +03002332static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002333 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002334 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002335 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002336{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002337 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2338 if (!vcpu->arch.update_pte.largepage ||
2339 sp->role.glevels == PT32_ROOT_LEVEL) {
2340 ++vcpu->kvm->stat.mmu_pde_zapped;
2341 return;
2342 }
2343 }
Avi Kivity00284252007-05-01 16:53:31 +03002344
Avi Kivity4cee5762007-11-18 16:37:07 +02002345 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002346 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002347 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002348 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002349 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002350}
2351
Avi Kivity79539ce2007-11-21 02:06:21 +02002352static bool need_remote_flush(u64 old, u64 new)
2353{
2354 if (!is_shadow_present_pte(old))
2355 return false;
2356 if (!is_shadow_present_pte(new))
2357 return true;
2358 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2359 return true;
2360 old ^= PT64_NX_MASK;
2361 new ^= PT64_NX_MASK;
2362 return (old & ~new & PT64_PERM_MASK) != 0;
2363}
2364
2365static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2366{
2367 if (need_remote_flush(old, new))
2368 kvm_flush_remote_tlbs(vcpu->kvm);
2369 else
2370 kvm_mmu_flush_tlb(vcpu);
2371}
2372
Avi Kivity12b7d282007-09-23 14:10:49 +02002373static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2374{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002375 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002376
Sheng Yang7b523452008-04-25 21:13:50 +08002377 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002378}
2379
Avi Kivityd7824ff2007-12-30 12:29:05 +02002380static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2381 const u8 *new, int bytes)
2382{
2383 gfn_t gfn;
2384 int r;
2385 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002386 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002387
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002388 vcpu->arch.update_pte.largepage = 0;
2389
Avi Kivityd7824ff2007-12-30 12:29:05 +02002390 if (bytes != 4 && bytes != 8)
2391 return;
2392
2393 /*
2394 * Assume that the pte write on a page table of the same type
2395 * as the current vcpu paging mode. This is nearly always true
2396 * (might be false while changing modes). Note it is verified later
2397 * by update_pte().
2398 */
2399 if (is_pae(vcpu)) {
2400 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2401 if ((bytes == 4) && (gpa % 4 == 0)) {
2402 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2403 if (r)
2404 return;
2405 memcpy((void *)&gpte + (gpa % 8), new, 4);
2406 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2407 memcpy((void *)&gpte, new, 8);
2408 }
2409 } else {
2410 if ((bytes == 4) && (gpa % 4 == 0))
2411 memcpy((void *)&gpte, new, 4);
2412 }
2413 if (!is_present_pte(gpte))
2414 return;
2415 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002416
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002417 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2418 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2419 vcpu->arch.update_pte.largepage = 1;
2420 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002421 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002422 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002423 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002424
Anthony Liguori35149e22008-04-02 14:46:56 -05002425 if (is_error_pfn(pfn)) {
2426 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002427 return;
2428 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002429 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002430 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002431}
2432
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002433static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2434{
2435 u64 *spte = vcpu->arch.last_pte_updated;
2436
2437 if (spte
2438 && vcpu->arch.last_pte_gfn == gfn
2439 && shadow_accessed_mask
2440 && !(*spte & shadow_accessed_mask)
2441 && is_shadow_present_pte(*spte))
2442 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2443}
2444
Avi Kivity09072da2007-05-01 14:16:52 +03002445void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002446 const u8 *new, int bytes,
2447 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002448{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002449 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002450 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002451 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002452 struct hlist_head *bucket;
2453 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002454 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002455 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002456 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002457 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002458 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002459 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002460 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002461 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002462 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002463 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002464 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002465
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002466 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002467 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002468 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002469 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002470 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002471 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002472 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002473 if (guest_initiated) {
2474 if (gfn == vcpu->arch.last_pt_write_gfn
2475 && !last_updated_pte_accessed(vcpu)) {
2476 ++vcpu->arch.last_pt_write_count;
2477 if (vcpu->arch.last_pt_write_count >= 3)
2478 flooded = 1;
2479 } else {
2480 vcpu->arch.last_pt_write_gfn = gfn;
2481 vcpu->arch.last_pt_write_count = 1;
2482 vcpu->arch.last_pte_updated = NULL;
2483 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002484 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002485 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002486 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002487 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002488 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002489 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002490 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002491 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002492 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002493 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002494 /*
2495 * Misaligned accesses are too much trouble to fix
2496 * up; also, they usually indicate a page is not used
2497 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002498 *
2499 * If we're seeing too many writes to a page,
2500 * it may no longer be a page table, or we may be
2501 * forking, in which case it is better to unmap the
2502 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002503 */
2504 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002505 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002506 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2507 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002508 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002509 continue;
2510 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002511 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002512 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002513 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002514 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002515 page_offset <<= 1; /* 32->64 */
2516 /*
2517 * A 32-bit pde maps 4MB while the shadow pdes map
2518 * only 2MB. So we need to double the offset again
2519 * and zap two pdes instead of one.
2520 */
2521 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002522 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002523 page_offset <<= 1;
2524 npte = 2;
2525 }
Avi Kivityfce06572007-05-01 16:44:05 +03002526 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002527 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002528 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002529 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002530 }
Avi Kivity4db35312007-11-21 15:28:32 +02002531 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002532 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2533 gentry = 0;
2534 r = kvm_read_guest_atomic(vcpu->kvm,
2535 gpa & ~(u64)(pte_size - 1),
2536 &gentry, pte_size);
2537 new = (const void *)&gentry;
2538 if (r < 0)
2539 new = NULL;
2540 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002541 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002542 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002543 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002544 if (new)
2545 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002546 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002547 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002548 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002549 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002550 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002551 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002552 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2553 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2554 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002555 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002556}
2557
Avi Kivitya4360362007-01-05 16:36:45 -08002558int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2559{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002560 gpa_t gpa;
2561 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002562
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002563 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002564
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002565 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002566 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002567 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002568 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002569}
Avi Kivity577bdc42008-07-19 08:57:05 +03002570EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002571
Avi Kivity22d95b12007-09-14 20:26:06 +03002572void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002573{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002574 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002575 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002576
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002577 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002578 struct kvm_mmu_page, link);
2579 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002580 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002581 }
2582}
Avi Kivityebeace82007-01-05 16:36:47 -08002583
Avi Kivity30677142007-10-28 18:48:59 +02002584int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2585{
2586 int r;
2587 enum emulation_result er;
2588
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002589 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002590 if (r < 0)
2591 goto out;
2592
2593 if (!r) {
2594 r = 1;
2595 goto out;
2596 }
2597
Avi Kivityb733bfb2007-10-28 18:52:05 +02002598 r = mmu_topup_memory_caches(vcpu);
2599 if (r)
2600 goto out;
2601
Avi Kivity30677142007-10-28 18:48:59 +02002602 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002603
2604 switch (er) {
2605 case EMULATE_DONE:
2606 return 1;
2607 case EMULATE_DO_MMIO:
2608 ++vcpu->stat.mmio_exits;
2609 return 0;
2610 case EMULATE_FAIL:
2611 kvm_report_emulation_failure(vcpu, "pagetable");
2612 return 1;
2613 default:
2614 BUG();
2615 }
2616out:
Avi Kivity30677142007-10-28 18:48:59 +02002617 return r;
2618}
2619EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2620
Marcelo Tosattia7052892008-09-23 13:18:35 -03002621void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2622{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002623 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002624 kvm_mmu_flush_tlb(vcpu);
2625 ++vcpu->stat.invlpg;
2626}
2627EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2628
Joerg Roedel18552672008-02-07 13:47:41 +01002629void kvm_enable_tdp(void)
2630{
2631 tdp_enabled = true;
2632}
2633EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2634
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002635void kvm_disable_tdp(void)
2636{
2637 tdp_enabled = false;
2638}
2639EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2640
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641static void free_mmu_pages(struct kvm_vcpu *vcpu)
2642{
Avi Kivity4db35312007-11-21 15:28:32 +02002643 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002645 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2646 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002647 struct kvm_mmu_page, link);
2648 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002649 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002650 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002651 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652}
2653
2654static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2655{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002656 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 int i;
2658
2659 ASSERT(vcpu);
2660
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002661 if (vcpu->kvm->arch.n_requested_mmu_pages)
2662 vcpu->kvm->arch.n_free_mmu_pages =
2663 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002664 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002665 vcpu->kvm->arch.n_free_mmu_pages =
2666 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002667 /*
2668 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2669 * Therefore we need to allocate shadow page tables in the first
2670 * 4GB of memory, which happens to fit the DMA32 zone.
2671 */
2672 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2673 if (!page)
2674 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002675 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002676 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002677 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002678
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 return 0;
2680
2681error_1:
2682 free_mmu_pages(vcpu);
2683 return -ENOMEM;
2684}
2685
Ingo Molnar8018c272006-12-29 16:50:01 -08002686int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002689 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690
Ingo Molnar8018c272006-12-29 16:50:01 -08002691 return alloc_mmu_pages(vcpu);
2692}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693
Ingo Molnar8018c272006-12-29 16:50:01 -08002694int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2695{
2696 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002697 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002698
Ingo Molnar8018c272006-12-29 16:50:01 -08002699 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700}
2701
2702void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2703{
2704 ASSERT(vcpu);
2705
2706 destroy_kvm_mmu(vcpu);
2707 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002708 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709}
2710
Avi Kivity90cb0522007-07-17 13:04:56 +03002711void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712{
Avi Kivity4db35312007-11-21 15:28:32 +02002713 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714
Avi Kivity2245a282008-08-27 16:32:24 +03002715 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002716 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717 int i;
2718 u64 *pt;
2719
Sheng Yang291f26b2008-10-16 17:30:57 +08002720 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721 continue;
2722
Avi Kivity4db35312007-11-21 15:28:32 +02002723 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2725 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002726 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 }
Avi Kivity171d5952008-08-27 16:40:51 +03002729 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002730 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002732
Avi Kivity90cb0522007-07-17 13:04:56 +03002733void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002734{
Avi Kivity4db35312007-11-21 15:28:32 +02002735 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002736
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002737 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002738 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002739 if (kvm_mmu_zap_page(kvm, sp))
2740 node = container_of(kvm->arch.active_mmu_pages.next,
2741 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002742 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002743
Avi Kivity90cb0522007-07-17 13:04:56 +03002744 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002745}
2746
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002747static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002748{
2749 struct kvm_mmu_page *page;
2750
2751 page = container_of(kvm->arch.active_mmu_pages.prev,
2752 struct kvm_mmu_page, link);
2753 kvm_mmu_zap_page(kvm, page);
2754}
2755
2756static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2757{
2758 struct kvm *kvm;
2759 struct kvm *kvm_freed = NULL;
2760 int cache_count = 0;
2761
2762 spin_lock(&kvm_lock);
2763
2764 list_for_each_entry(kvm, &vm_list, vm_list) {
2765 int npages;
2766
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002767 if (!down_read_trylock(&kvm->slots_lock))
2768 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002769 spin_lock(&kvm->mmu_lock);
2770 npages = kvm->arch.n_alloc_mmu_pages -
2771 kvm->arch.n_free_mmu_pages;
2772 cache_count += npages;
2773 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2774 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2775 cache_count--;
2776 kvm_freed = kvm;
2777 }
2778 nr_to_scan--;
2779
2780 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002781 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002782 }
2783 if (kvm_freed)
2784 list_move_tail(&kvm_freed->vm_list, &vm_list);
2785
2786 spin_unlock(&kvm_lock);
2787
2788 return cache_count;
2789}
2790
2791static struct shrinker mmu_shrinker = {
2792 .shrink = mmu_shrink,
2793 .seeks = DEFAULT_SEEKS * 10,
2794};
2795
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002796static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002797{
2798 if (pte_chain_cache)
2799 kmem_cache_destroy(pte_chain_cache);
2800 if (rmap_desc_cache)
2801 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002802 if (mmu_page_header_cache)
2803 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002804}
2805
Izik Eidus3ee16c82008-03-30 15:17:21 +03002806void kvm_mmu_module_exit(void)
2807{
2808 mmu_destroy_caches();
2809 unregister_shrinker(&mmu_shrinker);
2810}
2811
Avi Kivityb5a33a72007-04-15 16:31:09 +03002812int kvm_mmu_module_init(void)
2813{
2814 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2815 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002816 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002817 if (!pte_chain_cache)
2818 goto nomem;
2819 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2820 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002821 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002822 if (!rmap_desc_cache)
2823 goto nomem;
2824
Avi Kivityd3d25b02007-05-30 12:34:53 +03002825 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2826 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002827 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002828 if (!mmu_page_header_cache)
2829 goto nomem;
2830
Izik Eidus3ee16c82008-03-30 15:17:21 +03002831 register_shrinker(&mmu_shrinker);
2832
Avi Kivityb5a33a72007-04-15 16:31:09 +03002833 return 0;
2834
2835nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002836 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002837 return -ENOMEM;
2838}
2839
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002840/*
2841 * Caculate mmu pages needed for kvm.
2842 */
2843unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2844{
2845 int i;
2846 unsigned int nr_mmu_pages;
2847 unsigned int nr_pages = 0;
2848
2849 for (i = 0; i < kvm->nmemslots; i++)
2850 nr_pages += kvm->memslots[i].npages;
2851
2852 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2853 nr_mmu_pages = max(nr_mmu_pages,
2854 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2855
2856 return nr_mmu_pages;
2857}
2858
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002859static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2860 unsigned len)
2861{
2862 if (len > buffer->len)
2863 return NULL;
2864 return buffer->ptr;
2865}
2866
2867static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2868 unsigned len)
2869{
2870 void *ret;
2871
2872 ret = pv_mmu_peek_buffer(buffer, len);
2873 if (!ret)
2874 return ret;
2875 buffer->ptr += len;
2876 buffer->len -= len;
2877 buffer->processed += len;
2878 return ret;
2879}
2880
2881static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2882 gpa_t addr, gpa_t value)
2883{
2884 int bytes = 8;
2885 int r;
2886
2887 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2888 bytes = 4;
2889
2890 r = mmu_topup_memory_caches(vcpu);
2891 if (r)
2892 return r;
2893
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002894 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002895 return -EFAULT;
2896
2897 return 1;
2898}
2899
2900static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2901{
2902 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002903 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002904 return 1;
2905}
2906
2907static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2908{
2909 spin_lock(&vcpu->kvm->mmu_lock);
2910 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2911 spin_unlock(&vcpu->kvm->mmu_lock);
2912 return 1;
2913}
2914
2915static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2916 struct kvm_pv_mmu_op_buffer *buffer)
2917{
2918 struct kvm_mmu_op_header *header;
2919
2920 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2921 if (!header)
2922 return 0;
2923 switch (header->op) {
2924 case KVM_MMU_OP_WRITE_PTE: {
2925 struct kvm_mmu_op_write_pte *wpte;
2926
2927 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2928 if (!wpte)
2929 return 0;
2930 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2931 wpte->pte_val);
2932 }
2933 case KVM_MMU_OP_FLUSH_TLB: {
2934 struct kvm_mmu_op_flush_tlb *ftlb;
2935
2936 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2937 if (!ftlb)
2938 return 0;
2939 return kvm_pv_mmu_flush_tlb(vcpu);
2940 }
2941 case KVM_MMU_OP_RELEASE_PT: {
2942 struct kvm_mmu_op_release_pt *rpt;
2943
2944 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2945 if (!rpt)
2946 return 0;
2947 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2948 }
2949 default: return 0;
2950 }
2951}
2952
2953int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2954 gpa_t addr, unsigned long *ret)
2955{
2956 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002957 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002958
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002959 buffer->ptr = buffer->buf;
2960 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2961 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002962
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002963 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002964 if (r)
2965 goto out;
2966
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002967 while (buffer->len) {
2968 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002969 if (r < 0)
2970 goto out;
2971 if (r == 0)
2972 break;
2973 }
2974
2975 r = 1;
2976out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002977 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002978 return r;
2979}
2980
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002981#ifdef AUDIT
2982
2983static const char *audit_msg;
2984
2985static gva_t canonicalize(gva_t gva)
2986{
2987#ifdef CONFIG_X86_64
2988 gva = (long long)(gva << 16) >> 16;
2989#endif
2990 return gva;
2991}
2992
2993static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2994 gva_t va, int level)
2995{
2996 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2997 int i;
2998 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2999
3000 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3001 u64 ent = pt[i];
3002
Avi Kivityc7addb92007-09-16 18:58:32 +02003003 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003004 continue;
3005
3006 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003007 if (level > 1) {
3008 if (ent == shadow_notrap_nonpresent_pte)
3009 printk(KERN_ERR "audit: (%s) nontrapping pte"
3010 " in nonleaf level: levels %d gva %lx"
3011 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003012 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003013
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003014 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003015 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003016 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003017 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003018
Avi Kivityc7addb92007-09-16 18:58:32 +02003019 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003020 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003021 printk(KERN_ERR "xx audit error: (%s) levels %d"
3022 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003023 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003024 va, gpa, hpa, ent,
3025 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003026 else if (ent == shadow_notrap_nonpresent_pte
3027 && !is_error_hpa(hpa))
3028 printk(KERN_ERR "audit: (%s) notrap shadow,"
3029 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003030 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003031
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003032 }
3033 }
3034}
3035
3036static void audit_mappings(struct kvm_vcpu *vcpu)
3037{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003038 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003039
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003040 if (vcpu->arch.mmu.root_level == 4)
3041 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003042 else
3043 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003044 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003045 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003046 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003047 i << 30,
3048 2);
3049}
3050
3051static int count_rmaps(struct kvm_vcpu *vcpu)
3052{
3053 int nmaps = 0;
3054 int i, j, k;
3055
3056 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3057 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3058 struct kvm_rmap_desc *d;
3059
3060 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003061 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003062
Izik Eidus290fc382007-09-27 14:11:22 +02003063 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003064 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003065 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003066 ++nmaps;
3067 continue;
3068 }
Izik Eidus290fc382007-09-27 14:11:22 +02003069 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003070 while (d) {
3071 for (k = 0; k < RMAP_EXT; ++k)
3072 if (d->shadow_ptes[k])
3073 ++nmaps;
3074 else
3075 break;
3076 d = d->more;
3077 }
3078 }
3079 }
3080 return nmaps;
3081}
3082
3083static int count_writable_mappings(struct kvm_vcpu *vcpu)
3084{
3085 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003086 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003087 int i;
3088
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003089 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003090 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003091
Avi Kivity4db35312007-11-21 15:28:32 +02003092 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003093 continue;
3094
3095 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3096 u64 ent = pt[i];
3097
3098 if (!(ent & PT_PRESENT_MASK))
3099 continue;
3100 if (!(ent & PT_WRITABLE_MASK))
3101 continue;
3102 ++nmaps;
3103 }
3104 }
3105 return nmaps;
3106}
3107
3108static void audit_rmap(struct kvm_vcpu *vcpu)
3109{
3110 int n_rmap = count_rmaps(vcpu);
3111 int n_actual = count_writable_mappings(vcpu);
3112
3113 if (n_rmap != n_actual)
3114 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003115 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003116}
3117
3118static void audit_write_protection(struct kvm_vcpu *vcpu)
3119{
Avi Kivity4db35312007-11-21 15:28:32 +02003120 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003121 struct kvm_memory_slot *slot;
3122 unsigned long *rmapp;
3123 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003124
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003125 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003126 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003127 continue;
3128
Avi Kivity4db35312007-11-21 15:28:32 +02003129 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003130 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003131 rmapp = &slot->rmap[gfn - slot->base_gfn];
3132 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003133 printk(KERN_ERR "%s: (%s) shadow page has writable"
3134 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003135 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003136 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003137 }
3138}
3139
3140static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3141{
3142 int olddbg = dbg;
3143
3144 dbg = 0;
3145 audit_msg = msg;
3146 audit_rmap(vcpu);
3147 audit_write_protection(vcpu);
3148 audit_mappings(vcpu);
3149 dbg = olddbg;
3150}
3151
3152#endif