blob: 5f03ec324e35e2c8406fd437b5ec8d946a46f175 [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;
800 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800801 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200802 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803}
804
Avi Kivity714b93d2007-01-05 16:36:53 -0800805static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200806 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800807{
808 struct kvm_pte_chain *pte_chain;
809 struct hlist_node *node;
810 int i;
811
812 if (!parent_pte)
813 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200814 if (!sp->multimapped) {
815 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800816
817 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200818 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800819 return;
820 }
Avi Kivity4db35312007-11-21 15:28:32 +0200821 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800822 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200823 INIT_HLIST_HEAD(&sp->parent_ptes);
824 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800825 pte_chain->parent_ptes[0] = old;
826 }
Avi Kivity4db35312007-11-21 15:28:32 +0200827 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800828 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
829 continue;
830 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
831 if (!pte_chain->parent_ptes[i]) {
832 pte_chain->parent_ptes[i] = parent_pte;
833 return;
834 }
835 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800836 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800837 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200838 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800839 pte_chain->parent_ptes[0] = parent_pte;
840}
841
Avi Kivity4db35312007-11-21 15:28:32 +0200842static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843 u64 *parent_pte)
844{
845 struct kvm_pte_chain *pte_chain;
846 struct hlist_node *node;
847 int i;
848
Avi Kivity4db35312007-11-21 15:28:32 +0200849 if (!sp->multimapped) {
850 BUG_ON(sp->parent_pte != parent_pte);
851 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852 return;
853 }
Avi Kivity4db35312007-11-21 15:28:32 +0200854 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800855 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
856 if (!pte_chain->parent_ptes[i])
857 break;
858 if (pte_chain->parent_ptes[i] != parent_pte)
859 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800860 while (i + 1 < NR_PTE_CHAIN_ENTRIES
861 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800862 pte_chain->parent_ptes[i]
863 = pte_chain->parent_ptes[i + 1];
864 ++i;
865 }
866 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800867 if (i == 0) {
868 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300869 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200870 if (hlist_empty(&sp->parent_ptes)) {
871 sp->multimapped = 0;
872 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800873 }
874 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 return;
876 }
877 BUG();
878}
879
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300880
881static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
882 mmu_parent_walk_fn fn)
883{
884 struct kvm_pte_chain *pte_chain;
885 struct hlist_node *node;
886 struct kvm_mmu_page *parent_sp;
887 int i;
888
889 if (!sp->multimapped && sp->parent_pte) {
890 parent_sp = page_header(__pa(sp->parent_pte));
891 fn(vcpu, parent_sp);
892 mmu_parent_walk(vcpu, parent_sp, fn);
893 return;
894 }
895 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
896 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
897 if (!pte_chain->parent_ptes[i])
898 break;
899 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
900 fn(vcpu, parent_sp);
901 mmu_parent_walk(vcpu, parent_sp, fn);
902 }
903}
904
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300905static void kvm_mmu_update_unsync_bitmap(u64 *spte)
906{
907 unsigned int index;
908 struct kvm_mmu_page *sp = page_header(__pa(spte));
909
910 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200911 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
912 sp->unsync_children++;
913 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300914}
915
916static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
917{
918 struct kvm_pte_chain *pte_chain;
919 struct hlist_node *node;
920 int i;
921
922 if (!sp->parent_pte)
923 return;
924
925 if (!sp->multimapped) {
926 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
927 return;
928 }
929
930 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
931 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
932 if (!pte_chain->parent_ptes[i])
933 break;
934 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
935 }
936}
937
938static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
939{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300940 kvm_mmu_update_parents_unsync(sp);
941 return 1;
942}
943
944static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
945 struct kvm_mmu_page *sp)
946{
947 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
948 kvm_mmu_update_parents_unsync(sp);
949}
950
Avi Kivityd761a502008-05-29 14:55:03 +0300951static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
952 struct kvm_mmu_page *sp)
953{
954 int i;
955
956 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
957 sp->spt[i] = shadow_trap_nonpresent_pte;
958}
959
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300960static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
961 struct kvm_mmu_page *sp)
962{
963 return 1;
964}
965
Marcelo Tosattia7052892008-09-23 13:18:35 -0300966static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
967{
968}
969
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200970#define KVM_PAGE_ARRAY_NR 16
971
972struct kvm_mmu_pages {
973 struct mmu_page_and_offset {
974 struct kvm_mmu_page *sp;
975 unsigned int idx;
976 } page[KVM_PAGE_ARRAY_NR];
977 unsigned int nr;
978};
979
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300980#define for_each_unsync_children(bitmap, idx) \
981 for (idx = find_first_bit(bitmap, 512); \
982 idx < 512; \
983 idx = find_next_bit(bitmap, 512, idx+1))
984
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200985int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
986 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300987{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200988 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300989
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200990 if (sp->unsync)
991 for (i=0; i < pvec->nr; i++)
992 if (pvec->page[i].sp == sp)
993 return 0;
994
995 pvec->page[pvec->nr].sp = sp;
996 pvec->page[pvec->nr].idx = idx;
997 pvec->nr++;
998 return (pvec->nr == KVM_PAGE_ARRAY_NR);
999}
1000
1001static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1002 struct kvm_mmu_pages *pvec)
1003{
1004 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001005
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001006 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001007 u64 ent = sp->spt[i];
1008
Marcelo Tosatti87917232008-12-22 18:49:30 -02001009 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001010 struct kvm_mmu_page *child;
1011 child = page_header(ent & PT64_BASE_ADDR_MASK);
1012
1013 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001014 if (mmu_pages_add(pvec, child, i))
1015 return -ENOSPC;
1016
1017 ret = __mmu_unsync_walk(child, pvec);
1018 if (!ret)
1019 __clear_bit(i, sp->unsync_child_bitmap);
1020 else if (ret > 0)
1021 nr_unsync_leaf += ret;
1022 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001023 return ret;
1024 }
1025
1026 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001027 nr_unsync_leaf++;
1028 if (mmu_pages_add(pvec, child, i))
1029 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001030 }
1031 }
1032 }
1033
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001034 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001035 sp->unsync_children = 0;
1036
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001037 return nr_unsync_leaf;
1038}
1039
1040static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1041 struct kvm_mmu_pages *pvec)
1042{
1043 if (!sp->unsync_children)
1044 return 0;
1045
1046 mmu_pages_add(pvec, sp, 0);
1047 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001048}
1049
Avi Kivity4db35312007-11-21 15:28:32 +02001050static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001051{
1052 unsigned index;
1053 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001054 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001055 struct hlist_node *node;
1056
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001057 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001058 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001059 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001060 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001061 if (sp->gfn == gfn && !sp->role.metaphysical
1062 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001063 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001064 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001065 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001066 }
1067 return NULL;
1068}
1069
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001070static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1071{
1072 list_del(&sp->oos_link);
1073 --kvm->stat.mmu_unsync_global;
1074}
1075
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001076static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1077{
1078 WARN_ON(!sp->unsync);
1079 sp->unsync = 0;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001080 if (sp->global)
1081 kvm_unlink_unsync_global(kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001082 --kvm->stat.mmu_unsync;
1083}
1084
1085static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1086
1087static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1088{
1089 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1090 kvm_mmu_zap_page(vcpu->kvm, sp);
1091 return 1;
1092 }
1093
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001094 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1095 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001096 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001097 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1098 kvm_mmu_zap_page(vcpu->kvm, sp);
1099 return 1;
1100 }
1101
1102 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001103 return 0;
1104}
1105
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001106struct mmu_page_path {
1107 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1108 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001109};
1110
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111#define for_each_sp(pvec, sp, parents, i) \
1112 for (i = mmu_pages_next(&pvec, &parents, -1), \
1113 sp = pvec.page[i].sp; \
1114 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1115 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001116
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001117int mmu_pages_next(struct kvm_mmu_pages *pvec, struct mmu_page_path *parents,
1118 int i)
1119{
1120 int n;
1121
1122 for (n = i+1; n < pvec->nr; n++) {
1123 struct kvm_mmu_page *sp = pvec->page[n].sp;
1124
1125 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1126 parents->idx[0] = pvec->page[n].idx;
1127 return n;
1128 }
1129
1130 parents->parent[sp->role.level-2] = sp;
1131 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1132 }
1133
1134 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001135}
1136
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001137void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001138{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001139 struct kvm_mmu_page *sp;
1140 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001141
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001142 do {
1143 unsigned int idx = parents->idx[level];
1144
1145 sp = parents->parent[level];
1146 if (!sp)
1147 return;
1148
1149 --sp->unsync_children;
1150 WARN_ON((int)sp->unsync_children < 0);
1151 __clear_bit(idx, sp->unsync_child_bitmap);
1152 level++;
1153 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1154}
1155
1156static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1157 struct mmu_page_path *parents,
1158 struct kvm_mmu_pages *pvec)
1159{
1160 parents->parent[parent->role.level-1] = NULL;
1161 pvec->nr = 0;
1162}
1163
1164static void mmu_sync_children(struct kvm_vcpu *vcpu,
1165 struct kvm_mmu_page *parent)
1166{
1167 int i;
1168 struct kvm_mmu_page *sp;
1169 struct mmu_page_path parents;
1170 struct kvm_mmu_pages pages;
1171
1172 kvm_mmu_pages_init(parent, &parents, &pages);
1173 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001174 int protected = 0;
1175
1176 for_each_sp(pages, sp, parents, i)
1177 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1178
1179 if (protected)
1180 kvm_flush_remote_tlbs(vcpu->kvm);
1181
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001182 for_each_sp(pages, sp, parents, i) {
1183 kvm_sync_page(vcpu, sp);
1184 mmu_pages_clear_parents(&parents);
1185 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001186 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001187 kvm_mmu_pages_init(parent, &parents, &pages);
1188 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001189}
1190
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001191static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1192 gfn_t gfn,
1193 gva_t gaddr,
1194 unsigned level,
1195 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001196 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001197 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001198{
1199 union kvm_mmu_page_role role;
1200 unsigned index;
1201 unsigned quadrant;
1202 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001203 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001204 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001205
Avi Kivitya770f6f2008-12-21 19:20:09 +02001206 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001207 role.level = level;
1208 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001209 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001210 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001211 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1212 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1213 role.quadrant = quadrant;
1214 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001215 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001216 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001217 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001218 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001219 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1220 if (sp->gfn == gfn) {
1221 if (sp->unsync)
1222 if (kvm_sync_page(vcpu, sp))
1223 continue;
1224
1225 if (sp->role.word != role.word)
1226 continue;
1227
Avi Kivity4db35312007-11-21 15:28:32 +02001228 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001229 if (sp->unsync_children) {
1230 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1231 kvm_mmu_mark_parents_unsync(vcpu, sp);
1232 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001233 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001234 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001235 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001236 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001237 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1238 if (!sp)
1239 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001240 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001241 sp->gfn = gfn;
1242 sp->role = role;
Avi Kivitye2078312008-12-21 19:36:59 +02001243 sp->global = role.cr4_pge;
Avi Kivity4db35312007-11-21 15:28:32 +02001244 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001245 if (!metaphysical) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001246 if (rmap_write_protect(vcpu->kvm, gfn))
1247 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248 account_shadowed(vcpu->kvm, gfn);
1249 }
Avi Kivity131d8272008-05-29 14:56:28 +03001250 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1251 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1252 else
1253 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001254 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001255}
1256
Avi Kivity3d000db2008-08-22 19:24:38 +03001257static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001258 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +03001259{
1260 hpa_t shadow_addr;
1261 int level;
1262 int r;
1263 u64 *sptep;
1264 unsigned index;
1265
1266 shadow_addr = vcpu->arch.mmu.root_hpa;
1267 level = vcpu->arch.mmu.shadow_root_level;
1268 if (level == PT32E_ROOT_LEVEL) {
1269 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1270 shadow_addr &= PT64_BASE_ADDR_MASK;
Marcelo Tosattieb64f1e2008-12-09 16:07:22 +01001271 if (!shadow_addr)
1272 return 1;
Avi Kivity3d000db2008-08-22 19:24:38 +03001273 --level;
1274 }
1275
1276 while (level >= PT_PAGE_TABLE_LEVEL) {
1277 index = SHADOW_PT_INDEX(addr, level);
1278 sptep = ((u64 *)__va(shadow_addr)) + index;
1279 r = walker->entry(walker, vcpu, addr, sptep, level);
1280 if (r)
1281 return r;
1282 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1283 --level;
1284 }
1285 return 0;
1286}
1287
Avi Kivity90cb0522007-07-17 13:04:56 +03001288static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001289 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001290{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001291 unsigned i;
1292 u64 *pt;
1293 u64 ent;
1294
Avi Kivity4db35312007-11-21 15:28:32 +02001295 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001296
Avi Kivity4db35312007-11-21 15:28:32 +02001297 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001298 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001299 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001300 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001301 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001302 }
1303 return;
1304 }
1305
1306 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1307 ent = pt[i];
1308
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001309 if (is_shadow_present_pte(ent)) {
1310 if (!is_large_pte(ent)) {
1311 ent &= PT64_BASE_ADDR_MASK;
1312 mmu_page_remove_parent_pte(page_header(ent),
1313 &pt[i]);
1314 } else {
1315 --kvm->stat.lpages;
1316 rmap_remove(kvm, &pt[i]);
1317 }
1318 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001319 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001320 }
Avi Kivitya4360362007-01-05 16:36:45 -08001321}
1322
Avi Kivity4db35312007-11-21 15:28:32 +02001323static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001324{
Avi Kivity4db35312007-11-21 15:28:32 +02001325 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001326}
1327
Avi Kivity12b7d282007-09-23 14:10:49 +02001328static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1329{
1330 int i;
1331
1332 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1333 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001334 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001335}
1336
Avi Kivity31aa2b42008-07-11 17:59:46 +03001337static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001338{
1339 u64 *parent_pte;
1340
Avi Kivity4db35312007-11-21 15:28:32 +02001341 while (sp->multimapped || sp->parent_pte) {
1342 if (!sp->multimapped)
1343 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001344 else {
1345 struct kvm_pte_chain *chain;
1346
Avi Kivity4db35312007-11-21 15:28:32 +02001347 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001348 struct kvm_pte_chain, link);
1349 parent_pte = chain->parent_ptes[0];
1350 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001351 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001352 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001353 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001354 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001355}
1356
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001357static int mmu_zap_unsync_children(struct kvm *kvm,
1358 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001359{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001360 int i, zapped = 0;
1361 struct mmu_page_path parents;
1362 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001363
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001364 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001365 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001366
1367 kvm_mmu_pages_init(parent, &parents, &pages);
1368 while (mmu_unsync_walk(parent, &pages)) {
1369 struct kvm_mmu_page *sp;
1370
1371 for_each_sp(pages, sp, parents, i) {
1372 kvm_mmu_zap_page(kvm, sp);
1373 mmu_pages_clear_parents(&parents);
1374 }
1375 zapped += pages.nr;
1376 kvm_mmu_pages_init(parent, &parents, &pages);
1377 }
1378
1379 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001380}
1381
Marcelo Tosatti07385412008-09-23 13:18:37 -03001382static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001383{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001384 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001385 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001386 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001387 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001388 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001389 kvm_flush_remote_tlbs(kvm);
1390 if (!sp->role.invalid && !sp->role.metaphysical)
1391 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001392 if (sp->unsync)
1393 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001394 if (!sp->root_count) {
1395 hlist_del(&sp->hash_link);
1396 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001397 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001398 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001399 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001400 kvm_reload_remote_mmus(kvm);
1401 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001402 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001403 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001404}
1405
Izik Eidus82ce2c92007-10-02 18:52:55 +02001406/*
1407 * Changing the number of mmu pages allocated to the vm
1408 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1409 */
1410void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1411{
1412 /*
1413 * If we set the number of mmu pages to be smaller be than the
1414 * number of actived pages , we must to free some mmu pages before we
1415 * change the value
1416 */
1417
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001418 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001419 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001420 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1421 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001422
1423 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1424 struct kvm_mmu_page *page;
1425
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001426 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001427 struct kvm_mmu_page, link);
1428 kvm_mmu_zap_page(kvm, page);
1429 n_used_mmu_pages--;
1430 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001431 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001432 }
1433 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001434 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1435 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001436
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001437 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001438}
1439
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001440static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001441{
1442 unsigned index;
1443 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001444 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001445 struct hlist_node *node, *n;
1446 int r;
1447
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001448 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001449 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001450 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001451 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001452 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1453 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001454 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001455 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001456 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001457 if (kvm_mmu_zap_page(kvm, sp))
1458 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001459 }
1460 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001461}
1462
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001463static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001464{
Avi Kivity4db35312007-11-21 15:28:32 +02001465 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001466
Avi Kivity4db35312007-11-21 15:28:32 +02001467 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001468 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001469 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001470 }
1471}
1472
Avi Kivity38c335f2007-11-21 14:20:22 +02001473static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474{
Avi Kivity38c335f2007-11-21 14:20:22 +02001475 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001476 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477
Sheng Yang291f26b2008-10-16 17:30:57 +08001478 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479}
1480
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001481static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1482{
1483 int i;
1484 u64 *pt = sp->spt;
1485
1486 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1487 return;
1488
1489 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1490 if (pt[i] == shadow_notrap_nonpresent_pte)
1491 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1492 }
1493}
1494
Avi Kivity039576c2007-03-20 12:46:50 +02001495struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1496{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001497 struct page *page;
1498
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001499 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001500
1501 if (gpa == UNMAPPED_GVA)
1502 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001503
Izik Eidus72dc67a2008-02-10 18:04:15 +02001504 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001505
1506 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001507}
1508
Sheng Yang74be52e2008-10-09 16:01:56 +08001509/*
1510 * The function is based on mtrr_type_lookup() in
1511 * arch/x86/kernel/cpu/mtrr/generic.c
1512 */
1513static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1514 u64 start, u64 end)
1515{
1516 int i;
1517 u64 base, mask;
1518 u8 prev_match, curr_match;
1519 int num_var_ranges = KVM_NR_VAR_MTRR;
1520
1521 if (!mtrr_state->enabled)
1522 return 0xFF;
1523
1524 /* Make end inclusive end, instead of exclusive */
1525 end--;
1526
1527 /* Look in fixed ranges. Just return the type as per start */
1528 if (mtrr_state->have_fixed && (start < 0x100000)) {
1529 int idx;
1530
1531 if (start < 0x80000) {
1532 idx = 0;
1533 idx += (start >> 16);
1534 return mtrr_state->fixed_ranges[idx];
1535 } else if (start < 0xC0000) {
1536 idx = 1 * 8;
1537 idx += ((start - 0x80000) >> 14);
1538 return mtrr_state->fixed_ranges[idx];
1539 } else if (start < 0x1000000) {
1540 idx = 3 * 8;
1541 idx += ((start - 0xC0000) >> 12);
1542 return mtrr_state->fixed_ranges[idx];
1543 }
1544 }
1545
1546 /*
1547 * Look in variable ranges
1548 * Look of multiple ranges matching this address and pick type
1549 * as per MTRR precedence
1550 */
1551 if (!(mtrr_state->enabled & 2))
1552 return mtrr_state->def_type;
1553
1554 prev_match = 0xFF;
1555 for (i = 0; i < num_var_ranges; ++i) {
1556 unsigned short start_state, end_state;
1557
1558 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1559 continue;
1560
1561 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1562 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1563 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1564 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1565
1566 start_state = ((start & mask) == (base & mask));
1567 end_state = ((end & mask) == (base & mask));
1568 if (start_state != end_state)
1569 return 0xFE;
1570
1571 if ((start & mask) != (base & mask))
1572 continue;
1573
1574 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1575 if (prev_match == 0xFF) {
1576 prev_match = curr_match;
1577 continue;
1578 }
1579
1580 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1581 curr_match == MTRR_TYPE_UNCACHABLE)
1582 return MTRR_TYPE_UNCACHABLE;
1583
1584 if ((prev_match == MTRR_TYPE_WRBACK &&
1585 curr_match == MTRR_TYPE_WRTHROUGH) ||
1586 (prev_match == MTRR_TYPE_WRTHROUGH &&
1587 curr_match == MTRR_TYPE_WRBACK)) {
1588 prev_match = MTRR_TYPE_WRTHROUGH;
1589 curr_match = MTRR_TYPE_WRTHROUGH;
1590 }
1591
1592 if (prev_match != curr_match)
1593 return MTRR_TYPE_UNCACHABLE;
1594 }
1595
1596 if (prev_match != 0xFF)
1597 return prev_match;
1598
1599 return mtrr_state->def_type;
1600}
1601
1602static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1603{
1604 u8 mtrr;
1605
1606 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1607 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1608 if (mtrr == 0xfe || mtrr == 0xff)
1609 mtrr = MTRR_TYPE_WRBACK;
1610 return mtrr;
1611}
1612
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001613static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1614{
1615 unsigned index;
1616 struct hlist_head *bucket;
1617 struct kvm_mmu_page *s;
1618 struct hlist_node *node, *n;
1619
1620 index = kvm_page_table_hashfn(sp->gfn);
1621 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1622 /* don't unsync if pagetable is shadowed with multiple roles */
1623 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1624 if (s->gfn != sp->gfn || s->role.metaphysical)
1625 continue;
1626 if (s->role.word != sp->role.word)
1627 return 1;
1628 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001629 ++vcpu->kvm->stat.mmu_unsync;
1630 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001631
1632 if (sp->global) {
1633 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1634 ++vcpu->kvm->stat.mmu_unsync_global;
1635 } else
1636 kvm_mmu_mark_parents_unsync(vcpu, sp);
1637
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001638 mmu_convert_notrap(sp);
1639 return 0;
1640}
1641
1642static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1643 bool can_unsync)
1644{
1645 struct kvm_mmu_page *shadow;
1646
1647 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1648 if (shadow) {
1649 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1650 return 1;
1651 if (shadow->unsync)
1652 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001653 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001654 return kvm_unsync_page(vcpu, shadow);
1655 return 1;
1656 }
1657 return 0;
1658}
1659
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001660static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1661 unsigned pte_access, int user_fault,
1662 int write_fault, int dirty, int largepage,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001663 int global, gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001664 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001665{
1666 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001667 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001668 u64 mt_mask = shadow_mt_mask;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001669 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1670
1671 if (!global && sp->global) {
1672 sp->global = 0;
1673 if (sp->unsync) {
1674 kvm_unlink_unsync_global(vcpu->kvm, sp);
1675 kvm_mmu_mark_parents_unsync(vcpu, sp);
1676 }
1677 }
Sheng Yang64d4d522008-10-09 16:01:57 +08001678
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001679 /*
1680 * We don't set the accessed bit, since we sometimes want to see
1681 * whether the guest actually used the pte (in order to detect
1682 * demand paging).
1683 */
Sheng Yang7b523452008-04-25 21:13:50 +08001684 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001685 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001686 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001687 if (!dirty)
1688 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001689 if (pte_access & ACC_EXEC_MASK)
1690 spte |= shadow_x_mask;
1691 else
1692 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001693 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001694 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001695 if (largepage)
1696 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001697 if (mt_mask) {
Sheng Yang2aaf69d2009-01-21 16:52:16 +08001698 if (!kvm_is_mmio_pfn(pfn)) {
1699 mt_mask = get_memory_type(vcpu, gfn) <<
1700 kvm_x86_ops->get_mt_mask_shift();
1701 mt_mask |= VMX_EPT_IGMT_BIT;
1702 } else
1703 mt_mask = MTRR_TYPE_UNCACHABLE <<
1704 kvm_x86_ops->get_mt_mask_shift();
Sheng Yang64d4d522008-10-09 16:01:57 +08001705 spte |= mt_mask;
1706 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001707
Anthony Liguori35149e22008-04-02 14:46:56 -05001708 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001709
1710 if ((pte_access & ACC_WRITE_MASK)
1711 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001712
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001713 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1714 ret = 1;
1715 spte = shadow_trap_nonpresent_pte;
1716 goto set_pte;
1717 }
1718
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001719 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001720
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001721 /*
1722 * Optimization: for pte sync, if spte was writable the hash
1723 * lookup is unnecessary (and expensive). Write protection
1724 * is responsibility of mmu_get_page / kvm_sync_page.
1725 * Same reasoning can be applied to dirty page accounting.
1726 */
1727 if (!can_unsync && is_writeble_pte(*shadow_pte))
1728 goto set_pte;
1729
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001730 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001731 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001732 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001733 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001734 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001735 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001736 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001737 }
1738 }
1739
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001740 if (pte_access & ACC_WRITE_MASK)
1741 mark_page_dirty(vcpu->kvm, gfn);
1742
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001743set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001744 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001745 return ret;
1746}
1747
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001748static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1749 unsigned pt_access, unsigned pte_access,
1750 int user_fault, int write_fault, int dirty,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001751 int *ptwrite, int largepage, int global,
1752 gfn_t gfn, pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001753{
1754 int was_rmapped = 0;
1755 int was_writeble = is_writeble_pte(*shadow_pte);
1756
1757 pgprintk("%s: spte %llx access %x write_fault %d"
1758 " user_fault %d gfn %lx\n",
1759 __func__, *shadow_pte, pt_access,
1760 write_fault, user_fault, gfn);
1761
1762 if (is_rmap_pte(*shadow_pte)) {
1763 /*
1764 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1765 * the parent of the now unreachable PTE.
1766 */
1767 if (largepage && !is_large_pte(*shadow_pte)) {
1768 struct kvm_mmu_page *child;
1769 u64 pte = *shadow_pte;
1770
1771 child = page_header(pte & PT64_BASE_ADDR_MASK);
1772 mmu_page_remove_parent_pte(child, shadow_pte);
1773 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1774 pgprintk("hfn old %lx new %lx\n",
1775 spte_to_pfn(*shadow_pte), pfn);
1776 rmap_remove(vcpu->kvm, shadow_pte);
1777 } else {
1778 if (largepage)
1779 was_rmapped = is_large_pte(*shadow_pte);
1780 else
1781 was_rmapped = 1;
1782 }
1783 }
1784 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001785 dirty, largepage, global, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001786 if (write_fault)
1787 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001788 kvm_x86_ops->tlb_flush(vcpu);
1789 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001790
1791 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1792 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1793 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1794 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1795 *shadow_pte, shadow_pte);
1796 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001797 ++vcpu->kvm->stat.lpages;
1798
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001799 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1800 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001801 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001802 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001803 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001804 } else {
1805 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001806 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001807 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001808 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001809 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001810 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001811 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001812 vcpu->arch.last_pte_gfn = gfn;
1813 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001814}
1815
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1817{
1818}
1819
Avi Kivity140754b2008-08-22 19:28:04 +03001820struct direct_shadow_walk {
1821 struct kvm_shadow_walk walker;
1822 pfn_t pfn;
1823 int write;
1824 int largepage;
1825 int pt_write;
1826};
1827
1828static int direct_map_entry(struct kvm_shadow_walk *_walk,
1829 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001830 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001831{
1832 struct direct_shadow_walk *walk =
1833 container_of(_walk, struct direct_shadow_walk, walker);
1834 struct kvm_mmu_page *sp;
1835 gfn_t pseudo_gfn;
1836 gfn_t gfn = addr >> PAGE_SHIFT;
1837
1838 if (level == PT_PAGE_TABLE_LEVEL
1839 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1840 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1841 0, walk->write, 1, &walk->pt_write,
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001842 walk->largepage, 0, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001843 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001844 return 1;
1845 }
1846
1847 if (*sptep == shadow_trap_nonpresent_pte) {
1848 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001849 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001850 1, ACC_ALL, sptep);
1851 if (!sp) {
1852 pgprintk("nonpaging_map: ENOMEM\n");
1853 kvm_release_pfn_clean(walk->pfn);
1854 return -ENOMEM;
1855 }
1856
1857 set_shadow_pte(sptep,
1858 __pa(sp->spt)
1859 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1860 | shadow_user_mask | shadow_x_mask);
1861 }
1862 return 0;
1863}
1864
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001865static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001866 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867{
Avi Kivity140754b2008-08-22 19:28:04 +03001868 int r;
1869 struct direct_shadow_walk walker = {
1870 .walker = { .entry = direct_map_entry, },
1871 .pfn = pfn,
1872 .largepage = largepage,
1873 .write = write,
1874 .pt_write = 0,
1875 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001877 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001878 if (r < 0)
1879 return r;
1880 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881}
1882
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001883static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1884{
1885 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001886 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001887 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001888 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001889
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001890 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1891 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1892 largepage = 1;
1893 }
1894
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001895 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001896 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001897 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001898
Avi Kivityd196e342008-01-24 11:44:11 +02001899 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001900 if (is_error_pfn(pfn)) {
1901 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001902 return 1;
1903 }
1904
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001905 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001906 if (mmu_notifier_retry(vcpu, mmu_seq))
1907 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001908 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001909 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001910 spin_unlock(&vcpu->kvm->mmu_lock);
1911
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001912
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001913 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001914
1915out_unlock:
1916 spin_unlock(&vcpu->kvm->mmu_lock);
1917 kvm_release_pfn_clean(pfn);
1918 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001919}
1920
1921
Avi Kivity17ac10a2007-01-05 16:36:40 -08001922static void mmu_free_roots(struct kvm_vcpu *vcpu)
1923{
1924 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001925 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001926
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001927 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001928 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001929 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001930 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1931 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001932
Avi Kivity4db35312007-11-21 15:28:32 +02001933 sp = page_header(root);
1934 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001935 if (!sp->root_count && sp->role.invalid)
1936 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001937 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001938 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001939 return;
1940 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001941 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001942 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001943
Avi Kivity417726a2007-04-12 17:35:58 +03001944 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001945 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001946 sp = page_header(root);
1947 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001948 if (!sp->root_count && sp->role.invalid)
1949 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001950 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001951 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001952 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001953 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001954 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001955}
1956
1957static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1958{
1959 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001960 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001961 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001962 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001963
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001964 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001965
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001966 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1967 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001968
1969 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001970 if (tdp_enabled)
1971 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001972 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001973 PT64_ROOT_LEVEL, metaphysical,
1974 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001975 root = __pa(sp->spt);
1976 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001977 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001978 return;
1979 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001980 metaphysical = !is_paging(vcpu);
1981 if (tdp_enabled)
1982 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001983 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001985
1986 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001987 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1988 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1989 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001990 continue;
1991 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001992 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1993 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001994 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001995 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001996 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001997 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001998 root = __pa(sp->spt);
1999 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002000 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002001 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002002 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002003}
2004
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002005static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2006{
2007 int i;
2008 struct kvm_mmu_page *sp;
2009
2010 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2011 return;
2012 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2013 hpa_t root = vcpu->arch.mmu.root_hpa;
2014 sp = page_header(root);
2015 mmu_sync_children(vcpu, sp);
2016 return;
2017 }
2018 for (i = 0; i < 4; ++i) {
2019 hpa_t root = vcpu->arch.mmu.pae_root[i];
2020
2021 if (root) {
2022 root &= PT64_BASE_ADDR_MASK;
2023 sp = page_header(root);
2024 mmu_sync_children(vcpu, sp);
2025 }
2026 }
2027}
2028
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002029static void mmu_sync_global(struct kvm_vcpu *vcpu)
2030{
2031 struct kvm *kvm = vcpu->kvm;
2032 struct kvm_mmu_page *sp, *n;
2033
2034 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2035 kvm_sync_page(vcpu, sp);
2036}
2037
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002038void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2039{
2040 spin_lock(&vcpu->kvm->mmu_lock);
2041 mmu_sync_roots(vcpu);
2042 spin_unlock(&vcpu->kvm->mmu_lock);
2043}
2044
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002045void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2046{
2047 spin_lock(&vcpu->kvm->mmu_lock);
2048 mmu_sync_global(vcpu);
2049 spin_unlock(&vcpu->kvm->mmu_lock);
2050}
2051
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2053{
2054 return vaddr;
2055}
2056
2057static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002058 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059{
Avi Kivitye8332402007-12-09 18:43:00 +02002060 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002061 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002063 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002064 r = mmu_topup_memory_caches(vcpu);
2065 if (r)
2066 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002067
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002069 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070
Avi Kivitye8332402007-12-09 18:43:00 +02002071 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072
Avi Kivitye8332402007-12-09 18:43:00 +02002073 return nonpaging_map(vcpu, gva & PAGE_MASK,
2074 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075}
2076
Joerg Roedelfb72d162008-02-07 13:47:44 +01002077static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2078 u32 error_code)
2079{
Anthony Liguori35149e22008-04-02 14:46:56 -05002080 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002081 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002082 int largepage = 0;
2083 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002084 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002085
2086 ASSERT(vcpu);
2087 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2088
2089 r = mmu_topup_memory_caches(vcpu);
2090 if (r)
2091 return r;
2092
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002093 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2094 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2095 largepage = 1;
2096 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002097 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002098 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002099 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002100 if (is_error_pfn(pfn)) {
2101 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002102 return 1;
2103 }
2104 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002105 if (mmu_notifier_retry(vcpu, mmu_seq))
2106 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002107 kvm_mmu_free_some_pages(vcpu);
2108 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002109 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002110 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002111
2112 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002113
2114out_unlock:
2115 spin_unlock(&vcpu->kvm->mmu_lock);
2116 kvm_release_pfn_clean(pfn);
2117 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002118}
2119
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120static void nonpaging_free(struct kvm_vcpu *vcpu)
2121{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002122 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123}
2124
2125static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2126{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002127 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128
2129 context->new_cr3 = nonpaging_new_cr3;
2130 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 context->gva_to_gpa = nonpaging_gva_to_gpa;
2132 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002133 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002134 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002135 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002136 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002138 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 return 0;
2140}
2141
Avi Kivityd835dfe2007-11-21 02:57:59 +02002142void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002144 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002145 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146}
2147
2148static void paging_new_cr3(struct kvm_vcpu *vcpu)
2149{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002150 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002151 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152}
2153
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154static void inject_page_fault(struct kvm_vcpu *vcpu,
2155 u64 addr,
2156 u32 err_code)
2157{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002158 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159}
2160
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161static void paging_free(struct kvm_vcpu *vcpu)
2162{
2163 nonpaging_free(vcpu);
2164}
2165
2166#define PTTYPE 64
2167#include "paging_tmpl.h"
2168#undef PTTYPE
2169
2170#define PTTYPE 32
2171#include "paging_tmpl.h"
2172#undef PTTYPE
2173
Avi Kivity17ac10a2007-01-05 16:36:40 -08002174static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002176 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177
2178 ASSERT(is_pae(vcpu));
2179 context->new_cr3 = paging_new_cr3;
2180 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002182 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002183 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002184 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002186 context->root_level = level;
2187 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002188 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 return 0;
2190}
2191
Avi Kivity17ac10a2007-01-05 16:36:40 -08002192static int paging64_init_context(struct kvm_vcpu *vcpu)
2193{
2194 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2195}
2196
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197static int paging32_init_context(struct kvm_vcpu *vcpu)
2198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002199 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200
2201 context->new_cr3 = paging_new_cr3;
2202 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 context->gva_to_gpa = paging32_gva_to_gpa;
2204 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002205 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002206 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002207 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208 context->root_level = PT32_ROOT_LEVEL;
2209 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002210 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211 return 0;
2212}
2213
2214static int paging32E_init_context(struct kvm_vcpu *vcpu)
2215{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002216 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217}
2218
Joerg Roedelfb72d162008-02-07 13:47:44 +01002219static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2220{
2221 struct kvm_mmu *context = &vcpu->arch.mmu;
2222
2223 context->new_cr3 = nonpaging_new_cr3;
2224 context->page_fault = tdp_page_fault;
2225 context->free = nonpaging_free;
2226 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002227 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002228 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002229 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002230 context->root_hpa = INVALID_PAGE;
2231
2232 if (!is_paging(vcpu)) {
2233 context->gva_to_gpa = nonpaging_gva_to_gpa;
2234 context->root_level = 0;
2235 } else if (is_long_mode(vcpu)) {
2236 context->gva_to_gpa = paging64_gva_to_gpa;
2237 context->root_level = PT64_ROOT_LEVEL;
2238 } else if (is_pae(vcpu)) {
2239 context->gva_to_gpa = paging64_gva_to_gpa;
2240 context->root_level = PT32E_ROOT_LEVEL;
2241 } else {
2242 context->gva_to_gpa = paging32_gva_to_gpa;
2243 context->root_level = PT32_ROOT_LEVEL;
2244 }
2245
2246 return 0;
2247}
2248
2249static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002251 int r;
2252
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002254 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255
2256 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002257 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002258 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002259 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002261 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002263 r = paging32_init_context(vcpu);
2264
2265 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2266
2267 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268}
2269
Joerg Roedelfb72d162008-02-07 13:47:44 +01002270static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2271{
Anthony Liguori35149e22008-04-02 14:46:56 -05002272 vcpu->arch.update_pte.pfn = bad_pfn;
2273
Joerg Roedelfb72d162008-02-07 13:47:44 +01002274 if (tdp_enabled)
2275 return init_kvm_tdp_mmu(vcpu);
2276 else
2277 return init_kvm_softmmu(vcpu);
2278}
2279
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2281{
2282 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002283 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2284 vcpu->arch.mmu.free(vcpu);
2285 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 }
2287}
2288
2289int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2290{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002291 destroy_kvm_mmu(vcpu);
2292 return init_kvm_mmu(vcpu);
2293}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002294EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002295
2296int kvm_mmu_load(struct kvm_vcpu *vcpu)
2297{
Avi Kivity714b93d2007-01-05 16:36:53 -08002298 int r;
2299
Avi Kivitye2dec932007-01-05 16:36:54 -08002300 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002301 if (r)
2302 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002303 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002304 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002305 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002306 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002307 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002308 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002309 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002310out:
2311 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002313EXPORT_SYMBOL_GPL(kvm_mmu_load);
2314
2315void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2316{
2317 mmu_free_roots(vcpu);
2318}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319
Avi Kivity09072da2007-05-01 14:16:52 +03002320static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002321 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002322 u64 *spte)
2323{
2324 u64 pte;
2325 struct kvm_mmu_page *child;
2326
2327 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002328 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002329 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2330 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002331 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002332 else {
2333 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002334 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002335 }
2336 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002337 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002338 if (is_large_pte(pte))
2339 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002340}
2341
Avi Kivity00284252007-05-01 16:53:31 +03002342static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002343 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002344 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002345 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002346{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002347 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2348 if (!vcpu->arch.update_pte.largepage ||
2349 sp->role.glevels == PT32_ROOT_LEVEL) {
2350 ++vcpu->kvm->stat.mmu_pde_zapped;
2351 return;
2352 }
2353 }
Avi Kivity00284252007-05-01 16:53:31 +03002354
Avi Kivity4cee5762007-11-18 16:37:07 +02002355 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002356 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002357 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002358 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002359 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002360}
2361
Avi Kivity79539ce2007-11-21 02:06:21 +02002362static bool need_remote_flush(u64 old, u64 new)
2363{
2364 if (!is_shadow_present_pte(old))
2365 return false;
2366 if (!is_shadow_present_pte(new))
2367 return true;
2368 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2369 return true;
2370 old ^= PT64_NX_MASK;
2371 new ^= PT64_NX_MASK;
2372 return (old & ~new & PT64_PERM_MASK) != 0;
2373}
2374
2375static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2376{
2377 if (need_remote_flush(old, new))
2378 kvm_flush_remote_tlbs(vcpu->kvm);
2379 else
2380 kvm_mmu_flush_tlb(vcpu);
2381}
2382
Avi Kivity12b7d282007-09-23 14:10:49 +02002383static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2384{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002385 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002386
Sheng Yang7b523452008-04-25 21:13:50 +08002387 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002388}
2389
Avi Kivityd7824ff2007-12-30 12:29:05 +02002390static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2391 const u8 *new, int bytes)
2392{
2393 gfn_t gfn;
2394 int r;
2395 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002396 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002397
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002398 vcpu->arch.update_pte.largepage = 0;
2399
Avi Kivityd7824ff2007-12-30 12:29:05 +02002400 if (bytes != 4 && bytes != 8)
2401 return;
2402
2403 /*
2404 * Assume that the pte write on a page table of the same type
2405 * as the current vcpu paging mode. This is nearly always true
2406 * (might be false while changing modes). Note it is verified later
2407 * by update_pte().
2408 */
2409 if (is_pae(vcpu)) {
2410 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2411 if ((bytes == 4) && (gpa % 4 == 0)) {
2412 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2413 if (r)
2414 return;
2415 memcpy((void *)&gpte + (gpa % 8), new, 4);
2416 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2417 memcpy((void *)&gpte, new, 8);
2418 }
2419 } else {
2420 if ((bytes == 4) && (gpa % 4 == 0))
2421 memcpy((void *)&gpte, new, 4);
2422 }
2423 if (!is_present_pte(gpte))
2424 return;
2425 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002426
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002427 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2428 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2429 vcpu->arch.update_pte.largepage = 1;
2430 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002431 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002432 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002433 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002434
Anthony Liguori35149e22008-04-02 14:46:56 -05002435 if (is_error_pfn(pfn)) {
2436 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002437 return;
2438 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002439 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002440 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002441}
2442
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002443static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2444{
2445 u64 *spte = vcpu->arch.last_pte_updated;
2446
2447 if (spte
2448 && vcpu->arch.last_pte_gfn == gfn
2449 && shadow_accessed_mask
2450 && !(*spte & shadow_accessed_mask)
2451 && is_shadow_present_pte(*spte))
2452 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2453}
2454
Avi Kivity09072da2007-05-01 14:16:52 +03002455void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002456 const u8 *new, int bytes,
2457 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002458{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002459 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002460 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002461 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002462 struct hlist_head *bucket;
2463 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002464 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002465 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002466 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002467 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002468 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002469 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002470 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002471 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002472 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002473 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002474 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002475
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002476 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002477 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002478 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002479 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002480 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002481 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002482 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002483 if (guest_initiated) {
2484 if (gfn == vcpu->arch.last_pt_write_gfn
2485 && !last_updated_pte_accessed(vcpu)) {
2486 ++vcpu->arch.last_pt_write_count;
2487 if (vcpu->arch.last_pt_write_count >= 3)
2488 flooded = 1;
2489 } else {
2490 vcpu->arch.last_pt_write_gfn = gfn;
2491 vcpu->arch.last_pt_write_count = 1;
2492 vcpu->arch.last_pte_updated = NULL;
2493 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002494 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002495 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002496 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002497 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002498 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002499 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002500 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002501 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002502 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002503 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002504 /*
2505 * Misaligned accesses are too much trouble to fix
2506 * up; also, they usually indicate a page is not used
2507 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002508 *
2509 * If we're seeing too many writes to a page,
2510 * it may no longer be a page table, or we may be
2511 * forking, in which case it is better to unmap the
2512 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002513 */
2514 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002515 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002516 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2517 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002518 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002519 continue;
2520 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002521 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002522 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002523 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002524 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002525 page_offset <<= 1; /* 32->64 */
2526 /*
2527 * A 32-bit pde maps 4MB while the shadow pdes map
2528 * only 2MB. So we need to double the offset again
2529 * and zap two pdes instead of one.
2530 */
2531 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002532 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002533 page_offset <<= 1;
2534 npte = 2;
2535 }
Avi Kivityfce06572007-05-01 16:44:05 +03002536 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002537 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002538 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002539 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002540 }
Avi Kivity4db35312007-11-21 15:28:32 +02002541 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002542 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2543 gentry = 0;
2544 r = kvm_read_guest_atomic(vcpu->kvm,
2545 gpa & ~(u64)(pte_size - 1),
2546 &gentry, pte_size);
2547 new = (const void *)&gentry;
2548 if (r < 0)
2549 new = NULL;
2550 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002551 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002552 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002553 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002554 if (new)
2555 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002556 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002557 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002558 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002559 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002560 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002561 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002562 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2563 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2564 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002565 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002566}
2567
Avi Kivitya4360362007-01-05 16:36:45 -08002568int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2569{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002570 gpa_t gpa;
2571 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002572
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002573 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002574
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002575 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002576 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002577 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002578 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002579}
Avi Kivity577bdc42008-07-19 08:57:05 +03002580EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002581
Avi Kivity22d95b12007-09-14 20:26:06 +03002582void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002583{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002584 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002585 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002586
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002587 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002588 struct kvm_mmu_page, link);
2589 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002590 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002591 }
2592}
Avi Kivityebeace82007-01-05 16:36:47 -08002593
Avi Kivity30677142007-10-28 18:48:59 +02002594int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2595{
2596 int r;
2597 enum emulation_result er;
2598
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002599 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002600 if (r < 0)
2601 goto out;
2602
2603 if (!r) {
2604 r = 1;
2605 goto out;
2606 }
2607
Avi Kivityb733bfb2007-10-28 18:52:05 +02002608 r = mmu_topup_memory_caches(vcpu);
2609 if (r)
2610 goto out;
2611
Avi Kivity30677142007-10-28 18:48:59 +02002612 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002613
2614 switch (er) {
2615 case EMULATE_DONE:
2616 return 1;
2617 case EMULATE_DO_MMIO:
2618 ++vcpu->stat.mmio_exits;
2619 return 0;
2620 case EMULATE_FAIL:
2621 kvm_report_emulation_failure(vcpu, "pagetable");
2622 return 1;
2623 default:
2624 BUG();
2625 }
2626out:
Avi Kivity30677142007-10-28 18:48:59 +02002627 return r;
2628}
2629EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2630
Marcelo Tosattia7052892008-09-23 13:18:35 -03002631void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2632{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002633 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002634 kvm_mmu_flush_tlb(vcpu);
2635 ++vcpu->stat.invlpg;
2636}
2637EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2638
Joerg Roedel18552672008-02-07 13:47:41 +01002639void kvm_enable_tdp(void)
2640{
2641 tdp_enabled = true;
2642}
2643EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2644
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002645void kvm_disable_tdp(void)
2646{
2647 tdp_enabled = false;
2648}
2649EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2650
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651static void free_mmu_pages(struct kvm_vcpu *vcpu)
2652{
Avi Kivity4db35312007-11-21 15:28:32 +02002653 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002655 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2656 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002657 struct kvm_mmu_page, link);
2658 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002659 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002660 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002661 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662}
2663
2664static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2665{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002666 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 int i;
2668
2669 ASSERT(vcpu);
2670
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002671 if (vcpu->kvm->arch.n_requested_mmu_pages)
2672 vcpu->kvm->arch.n_free_mmu_pages =
2673 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002674 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002675 vcpu->kvm->arch.n_free_mmu_pages =
2676 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002677 /*
2678 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2679 * Therefore we need to allocate shadow page tables in the first
2680 * 4GB of memory, which happens to fit the DMA32 zone.
2681 */
2682 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2683 if (!page)
2684 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002685 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002686 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002687 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002688
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 return 0;
2690
2691error_1:
2692 free_mmu_pages(vcpu);
2693 return -ENOMEM;
2694}
2695
Ingo Molnar8018c272006-12-29 16:50:01 -08002696int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002699 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700
Ingo Molnar8018c272006-12-29 16:50:01 -08002701 return alloc_mmu_pages(vcpu);
2702}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703
Ingo Molnar8018c272006-12-29 16:50:01 -08002704int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2705{
2706 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002707 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002708
Ingo Molnar8018c272006-12-29 16:50:01 -08002709 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710}
2711
2712void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2713{
2714 ASSERT(vcpu);
2715
2716 destroy_kvm_mmu(vcpu);
2717 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002718 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719}
2720
Avi Kivity90cb0522007-07-17 13:04:56 +03002721void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722{
Avi Kivity4db35312007-11-21 15:28:32 +02002723 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724
Avi Kivity2245a282008-08-27 16:32:24 +03002725 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002726 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 int i;
2728 u64 *pt;
2729
Sheng Yang291f26b2008-10-16 17:30:57 +08002730 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 continue;
2732
Avi Kivity4db35312007-11-21 15:28:32 +02002733 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2735 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002736 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 }
Avi Kivity171d5952008-08-27 16:40:51 +03002739 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002740 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002742
Avi Kivity90cb0522007-07-17 13:04:56 +03002743void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002744{
Avi Kivity4db35312007-11-21 15:28:32 +02002745 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002746
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002747 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002748 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002749 if (kvm_mmu_zap_page(kvm, sp))
2750 node = container_of(kvm->arch.active_mmu_pages.next,
2751 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002752 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002753
Avi Kivity90cb0522007-07-17 13:04:56 +03002754 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002755}
2756
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002757static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002758{
2759 struct kvm_mmu_page *page;
2760
2761 page = container_of(kvm->arch.active_mmu_pages.prev,
2762 struct kvm_mmu_page, link);
2763 kvm_mmu_zap_page(kvm, page);
2764}
2765
2766static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2767{
2768 struct kvm *kvm;
2769 struct kvm *kvm_freed = NULL;
2770 int cache_count = 0;
2771
2772 spin_lock(&kvm_lock);
2773
2774 list_for_each_entry(kvm, &vm_list, vm_list) {
2775 int npages;
2776
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002777 if (!down_read_trylock(&kvm->slots_lock))
2778 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002779 spin_lock(&kvm->mmu_lock);
2780 npages = kvm->arch.n_alloc_mmu_pages -
2781 kvm->arch.n_free_mmu_pages;
2782 cache_count += npages;
2783 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2784 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2785 cache_count--;
2786 kvm_freed = kvm;
2787 }
2788 nr_to_scan--;
2789
2790 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002791 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002792 }
2793 if (kvm_freed)
2794 list_move_tail(&kvm_freed->vm_list, &vm_list);
2795
2796 spin_unlock(&kvm_lock);
2797
2798 return cache_count;
2799}
2800
2801static struct shrinker mmu_shrinker = {
2802 .shrink = mmu_shrink,
2803 .seeks = DEFAULT_SEEKS * 10,
2804};
2805
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002806static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002807{
2808 if (pte_chain_cache)
2809 kmem_cache_destroy(pte_chain_cache);
2810 if (rmap_desc_cache)
2811 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002812 if (mmu_page_header_cache)
2813 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002814}
2815
Izik Eidus3ee16c82008-03-30 15:17:21 +03002816void kvm_mmu_module_exit(void)
2817{
2818 mmu_destroy_caches();
2819 unregister_shrinker(&mmu_shrinker);
2820}
2821
Avi Kivityb5a33a72007-04-15 16:31:09 +03002822int kvm_mmu_module_init(void)
2823{
2824 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2825 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002826 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002827 if (!pte_chain_cache)
2828 goto nomem;
2829 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2830 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002831 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002832 if (!rmap_desc_cache)
2833 goto nomem;
2834
Avi Kivityd3d25b02007-05-30 12:34:53 +03002835 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2836 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002837 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002838 if (!mmu_page_header_cache)
2839 goto nomem;
2840
Izik Eidus3ee16c82008-03-30 15:17:21 +03002841 register_shrinker(&mmu_shrinker);
2842
Avi Kivityb5a33a72007-04-15 16:31:09 +03002843 return 0;
2844
2845nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002846 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002847 return -ENOMEM;
2848}
2849
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002850/*
2851 * Caculate mmu pages needed for kvm.
2852 */
2853unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2854{
2855 int i;
2856 unsigned int nr_mmu_pages;
2857 unsigned int nr_pages = 0;
2858
2859 for (i = 0; i < kvm->nmemslots; i++)
2860 nr_pages += kvm->memslots[i].npages;
2861
2862 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2863 nr_mmu_pages = max(nr_mmu_pages,
2864 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2865
2866 return nr_mmu_pages;
2867}
2868
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002869static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2870 unsigned len)
2871{
2872 if (len > buffer->len)
2873 return NULL;
2874 return buffer->ptr;
2875}
2876
2877static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2878 unsigned len)
2879{
2880 void *ret;
2881
2882 ret = pv_mmu_peek_buffer(buffer, len);
2883 if (!ret)
2884 return ret;
2885 buffer->ptr += len;
2886 buffer->len -= len;
2887 buffer->processed += len;
2888 return ret;
2889}
2890
2891static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2892 gpa_t addr, gpa_t value)
2893{
2894 int bytes = 8;
2895 int r;
2896
2897 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2898 bytes = 4;
2899
2900 r = mmu_topup_memory_caches(vcpu);
2901 if (r)
2902 return r;
2903
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002904 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002905 return -EFAULT;
2906
2907 return 1;
2908}
2909
2910static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2911{
2912 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002913 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002914 return 1;
2915}
2916
2917static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2918{
2919 spin_lock(&vcpu->kvm->mmu_lock);
2920 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2921 spin_unlock(&vcpu->kvm->mmu_lock);
2922 return 1;
2923}
2924
2925static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2926 struct kvm_pv_mmu_op_buffer *buffer)
2927{
2928 struct kvm_mmu_op_header *header;
2929
2930 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2931 if (!header)
2932 return 0;
2933 switch (header->op) {
2934 case KVM_MMU_OP_WRITE_PTE: {
2935 struct kvm_mmu_op_write_pte *wpte;
2936
2937 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2938 if (!wpte)
2939 return 0;
2940 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2941 wpte->pte_val);
2942 }
2943 case KVM_MMU_OP_FLUSH_TLB: {
2944 struct kvm_mmu_op_flush_tlb *ftlb;
2945
2946 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2947 if (!ftlb)
2948 return 0;
2949 return kvm_pv_mmu_flush_tlb(vcpu);
2950 }
2951 case KVM_MMU_OP_RELEASE_PT: {
2952 struct kvm_mmu_op_release_pt *rpt;
2953
2954 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2955 if (!rpt)
2956 return 0;
2957 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2958 }
2959 default: return 0;
2960 }
2961}
2962
2963int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2964 gpa_t addr, unsigned long *ret)
2965{
2966 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002967 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002968
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002969 buffer->ptr = buffer->buf;
2970 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2971 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002972
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002973 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002974 if (r)
2975 goto out;
2976
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002977 while (buffer->len) {
2978 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002979 if (r < 0)
2980 goto out;
2981 if (r == 0)
2982 break;
2983 }
2984
2985 r = 1;
2986out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002987 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002988 return r;
2989}
2990
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002991#ifdef AUDIT
2992
2993static const char *audit_msg;
2994
2995static gva_t canonicalize(gva_t gva)
2996{
2997#ifdef CONFIG_X86_64
2998 gva = (long long)(gva << 16) >> 16;
2999#endif
3000 return gva;
3001}
3002
3003static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3004 gva_t va, int level)
3005{
3006 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3007 int i;
3008 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3009
3010 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3011 u64 ent = pt[i];
3012
Avi Kivityc7addb92007-09-16 18:58:32 +02003013 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003014 continue;
3015
3016 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02003017 if (level > 1) {
3018 if (ent == shadow_notrap_nonpresent_pte)
3019 printk(KERN_ERR "audit: (%s) nontrapping pte"
3020 " in nonleaf level: levels %d gva %lx"
3021 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003022 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02003023
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003024 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02003025 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003026 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003027 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003028
Avi Kivityc7addb92007-09-16 18:58:32 +02003029 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003030 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003031 printk(KERN_ERR "xx audit error: (%s) levels %d"
3032 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003033 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003034 va, gpa, hpa, ent,
3035 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003036 else if (ent == shadow_notrap_nonpresent_pte
3037 && !is_error_hpa(hpa))
3038 printk(KERN_ERR "audit: (%s) notrap shadow,"
3039 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003040 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003041
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003042 }
3043 }
3044}
3045
3046static void audit_mappings(struct kvm_vcpu *vcpu)
3047{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003048 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003049
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003050 if (vcpu->arch.mmu.root_level == 4)
3051 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003052 else
3053 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003054 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003055 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003056 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003057 i << 30,
3058 2);
3059}
3060
3061static int count_rmaps(struct kvm_vcpu *vcpu)
3062{
3063 int nmaps = 0;
3064 int i, j, k;
3065
3066 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3067 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3068 struct kvm_rmap_desc *d;
3069
3070 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003071 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003072
Izik Eidus290fc382007-09-27 14:11:22 +02003073 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003074 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003075 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003076 ++nmaps;
3077 continue;
3078 }
Izik Eidus290fc382007-09-27 14:11:22 +02003079 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003080 while (d) {
3081 for (k = 0; k < RMAP_EXT; ++k)
3082 if (d->shadow_ptes[k])
3083 ++nmaps;
3084 else
3085 break;
3086 d = d->more;
3087 }
3088 }
3089 }
3090 return nmaps;
3091}
3092
3093static int count_writable_mappings(struct kvm_vcpu *vcpu)
3094{
3095 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003096 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003097 int i;
3098
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003099 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003100 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003101
Avi Kivity4db35312007-11-21 15:28:32 +02003102 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003103 continue;
3104
3105 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3106 u64 ent = pt[i];
3107
3108 if (!(ent & PT_PRESENT_MASK))
3109 continue;
3110 if (!(ent & PT_WRITABLE_MASK))
3111 continue;
3112 ++nmaps;
3113 }
3114 }
3115 return nmaps;
3116}
3117
3118static void audit_rmap(struct kvm_vcpu *vcpu)
3119{
3120 int n_rmap = count_rmaps(vcpu);
3121 int n_actual = count_writable_mappings(vcpu);
3122
3123 if (n_rmap != n_actual)
3124 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003125 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003126}
3127
3128static void audit_write_protection(struct kvm_vcpu *vcpu)
3129{
Avi Kivity4db35312007-11-21 15:28:32 +02003130 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003131 struct kvm_memory_slot *slot;
3132 unsigned long *rmapp;
3133 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003134
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003135 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003136 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003137 continue;
3138
Avi Kivity4db35312007-11-21 15:28:32 +02003139 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003140 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003141 rmapp = &slot->rmap[gfn - slot->base_gfn];
3142 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003143 printk(KERN_ERR "%s: (%s) shadow page has writable"
3144 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003145 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003146 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003147 }
3148}
3149
3150static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3151{
3152 int olddbg = dbg;
3153
3154 dbg = 0;
3155 audit_msg = msg;
3156 audit_rmap(vcpu);
3157 audit_write_protection(vcpu);
3158 audit_mappings(vcpu);
3159 dbg = olddbg;
3160}
3161
3162#endif