blob: 58c35dead321e74987161bb37b5f91f2460ddd30 [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);
Avi Kivity4db35312007-11-21 15:28:32 +0200796 ASSERT(is_empty_shadow_page(sp->spt));
Sheng Yang291f26b2008-10-16 17:30:57 +0800797 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200798 sp->multimapped = 0;
799 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800800 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200801 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802}
803
Avi Kivity714b93d2007-01-05 16:36:53 -0800804static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200805 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800806{
807 struct kvm_pte_chain *pte_chain;
808 struct hlist_node *node;
809 int i;
810
811 if (!parent_pte)
812 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200813 if (!sp->multimapped) {
814 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800815
816 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200817 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800818 return;
819 }
Avi Kivity4db35312007-11-21 15:28:32 +0200820 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800821 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200822 INIT_HLIST_HEAD(&sp->parent_ptes);
823 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800824 pte_chain->parent_ptes[0] = old;
825 }
Avi Kivity4db35312007-11-21 15:28:32 +0200826 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800827 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
828 continue;
829 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
830 if (!pte_chain->parent_ptes[i]) {
831 pte_chain->parent_ptes[i] = parent_pte;
832 return;
833 }
834 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800835 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800836 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200837 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800838 pte_chain->parent_ptes[0] = parent_pte;
839}
840
Avi Kivity4db35312007-11-21 15:28:32 +0200841static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800842 u64 *parent_pte)
843{
844 struct kvm_pte_chain *pte_chain;
845 struct hlist_node *node;
846 int i;
847
Avi Kivity4db35312007-11-21 15:28:32 +0200848 if (!sp->multimapped) {
849 BUG_ON(sp->parent_pte != parent_pte);
850 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800851 return;
852 }
Avi Kivity4db35312007-11-21 15:28:32 +0200853 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800854 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
855 if (!pte_chain->parent_ptes[i])
856 break;
857 if (pte_chain->parent_ptes[i] != parent_pte)
858 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800859 while (i + 1 < NR_PTE_CHAIN_ENTRIES
860 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800861 pte_chain->parent_ptes[i]
862 = pte_chain->parent_ptes[i + 1];
863 ++i;
864 }
865 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800866 if (i == 0) {
867 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300868 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200869 if (hlist_empty(&sp->parent_ptes)) {
870 sp->multimapped = 0;
871 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800872 }
873 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800874 return;
875 }
876 BUG();
877}
878
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300879
880static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
881 mmu_parent_walk_fn fn)
882{
883 struct kvm_pte_chain *pte_chain;
884 struct hlist_node *node;
885 struct kvm_mmu_page *parent_sp;
886 int i;
887
888 if (!sp->multimapped && sp->parent_pte) {
889 parent_sp = page_header(__pa(sp->parent_pte));
890 fn(vcpu, parent_sp);
891 mmu_parent_walk(vcpu, parent_sp, fn);
892 return;
893 }
894 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
895 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
896 if (!pte_chain->parent_ptes[i])
897 break;
898 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
899 fn(vcpu, parent_sp);
900 mmu_parent_walk(vcpu, parent_sp, fn);
901 }
902}
903
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300904static void kvm_mmu_update_unsync_bitmap(u64 *spte)
905{
906 unsigned int index;
907 struct kvm_mmu_page *sp = page_header(__pa(spte));
908
909 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200910 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
911 sp->unsync_children++;
912 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300913}
914
915static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
916{
917 struct kvm_pte_chain *pte_chain;
918 struct hlist_node *node;
919 int i;
920
921 if (!sp->parent_pte)
922 return;
923
924 if (!sp->multimapped) {
925 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
926 return;
927 }
928
929 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
930 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
931 if (!pte_chain->parent_ptes[i])
932 break;
933 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
934 }
935}
936
937static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
938{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300939 kvm_mmu_update_parents_unsync(sp);
940 return 1;
941}
942
943static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
944 struct kvm_mmu_page *sp)
945{
946 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
947 kvm_mmu_update_parents_unsync(sp);
948}
949
Avi Kivityd761a502008-05-29 14:55:03 +0300950static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
951 struct kvm_mmu_page *sp)
952{
953 int i;
954
955 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
956 sp->spt[i] = shadow_trap_nonpresent_pte;
957}
958
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300959static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
960 struct kvm_mmu_page *sp)
961{
962 return 1;
963}
964
Marcelo Tosattia7052892008-09-23 13:18:35 -0300965static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
966{
967}
968
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200969#define KVM_PAGE_ARRAY_NR 16
970
971struct kvm_mmu_pages {
972 struct mmu_page_and_offset {
973 struct kvm_mmu_page *sp;
974 unsigned int idx;
975 } page[KVM_PAGE_ARRAY_NR];
976 unsigned int nr;
977};
978
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300979#define for_each_unsync_children(bitmap, idx) \
980 for (idx = find_first_bit(bitmap, 512); \
981 idx < 512; \
982 idx = find_next_bit(bitmap, 512, idx+1))
983
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200984int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
985 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300986{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200987 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300988
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200989 if (sp->unsync)
990 for (i=0; i < pvec->nr; i++)
991 if (pvec->page[i].sp == sp)
992 return 0;
993
994 pvec->page[pvec->nr].sp = sp;
995 pvec->page[pvec->nr].idx = idx;
996 pvec->nr++;
997 return (pvec->nr == KVM_PAGE_ARRAY_NR);
998}
999
1000static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1001 struct kvm_mmu_pages *pvec)
1002{
1003 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001004
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001005 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001006 u64 ent = sp->spt[i];
1007
1008 if (is_shadow_present_pte(ent)) {
1009 struct kvm_mmu_page *child;
1010 child = page_header(ent & PT64_BASE_ADDR_MASK);
1011
1012 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001013 if (mmu_pages_add(pvec, child, i))
1014 return -ENOSPC;
1015
1016 ret = __mmu_unsync_walk(child, pvec);
1017 if (!ret)
1018 __clear_bit(i, sp->unsync_child_bitmap);
1019 else if (ret > 0)
1020 nr_unsync_leaf += ret;
1021 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001022 return ret;
1023 }
1024
1025 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001026 nr_unsync_leaf++;
1027 if (mmu_pages_add(pvec, child, i))
1028 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001029 }
1030 }
1031 }
1032
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001033 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001034 sp->unsync_children = 0;
1035
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001036 return nr_unsync_leaf;
1037}
1038
1039static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1040 struct kvm_mmu_pages *pvec)
1041{
1042 if (!sp->unsync_children)
1043 return 0;
1044
1045 mmu_pages_add(pvec, sp, 0);
1046 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001047}
1048
Avi Kivity4db35312007-11-21 15:28:32 +02001049static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001050{
1051 unsigned index;
1052 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001053 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001054 struct hlist_node *node;
1055
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001056 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001057 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001058 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001059 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001060 if (sp->gfn == gfn && !sp->role.metaphysical
1061 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001062 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001063 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001064 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001065 }
1066 return NULL;
1067}
1068
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001069static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1070{
1071 WARN_ON(!sp->unsync);
1072 sp->unsync = 0;
1073 --kvm->stat.mmu_unsync;
1074}
1075
1076static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1077
1078static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1079{
1080 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1081 kvm_mmu_zap_page(vcpu->kvm, sp);
1082 return 1;
1083 }
1084
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001085 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1086 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001087 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001088 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1089 kvm_mmu_zap_page(vcpu->kvm, sp);
1090 return 1;
1091 }
1092
1093 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001094 return 0;
1095}
1096
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001097struct mmu_page_path {
1098 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1099 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001100};
1101
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001102#define for_each_sp(pvec, sp, parents, i) \
1103 for (i = mmu_pages_next(&pvec, &parents, -1), \
1104 sp = pvec.page[i].sp; \
1105 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1106 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001107
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001108int mmu_pages_next(struct kvm_mmu_pages *pvec, struct mmu_page_path *parents,
1109 int i)
1110{
1111 int n;
1112
1113 for (n = i+1; n < pvec->nr; n++) {
1114 struct kvm_mmu_page *sp = pvec->page[n].sp;
1115
1116 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1117 parents->idx[0] = pvec->page[n].idx;
1118 return n;
1119 }
1120
1121 parents->parent[sp->role.level-2] = sp;
1122 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1123 }
1124
1125 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001126}
1127
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001128void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001129{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001130 struct kvm_mmu_page *sp;
1131 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001132
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001133 do {
1134 unsigned int idx = parents->idx[level];
1135
1136 sp = parents->parent[level];
1137 if (!sp)
1138 return;
1139
1140 --sp->unsync_children;
1141 WARN_ON((int)sp->unsync_children < 0);
1142 __clear_bit(idx, sp->unsync_child_bitmap);
1143 level++;
1144 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1145}
1146
1147static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1148 struct mmu_page_path *parents,
1149 struct kvm_mmu_pages *pvec)
1150{
1151 parents->parent[parent->role.level-1] = NULL;
1152 pvec->nr = 0;
1153}
1154
1155static void mmu_sync_children(struct kvm_vcpu *vcpu,
1156 struct kvm_mmu_page *parent)
1157{
1158 int i;
1159 struct kvm_mmu_page *sp;
1160 struct mmu_page_path parents;
1161 struct kvm_mmu_pages pages;
1162
1163 kvm_mmu_pages_init(parent, &parents, &pages);
1164 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001165 int protected = 0;
1166
1167 for_each_sp(pages, sp, parents, i)
1168 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1169
1170 if (protected)
1171 kvm_flush_remote_tlbs(vcpu->kvm);
1172
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001173 for_each_sp(pages, sp, parents, i) {
1174 kvm_sync_page(vcpu, sp);
1175 mmu_pages_clear_parents(&parents);
1176 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001177 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001178 kvm_mmu_pages_init(parent, &parents, &pages);
1179 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001180}
1181
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001182static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1183 gfn_t gfn,
1184 gva_t gaddr,
1185 unsigned level,
1186 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001187 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001188 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001189{
1190 union kvm_mmu_page_role role;
1191 unsigned index;
1192 unsigned quadrant;
1193 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001194 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001195 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001196
1197 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001198 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001199 role.level = level;
1200 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001201 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001202 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001203 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1204 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1205 role.quadrant = quadrant;
1206 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001207 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001208 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001209 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001210 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001211 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1212 if (sp->gfn == gfn) {
1213 if (sp->unsync)
1214 if (kvm_sync_page(vcpu, sp))
1215 continue;
1216
1217 if (sp->role.word != role.word)
1218 continue;
1219
Avi Kivity4db35312007-11-21 15:28:32 +02001220 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001221 if (sp->unsync_children) {
1222 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1223 kvm_mmu_mark_parents_unsync(vcpu, sp);
1224 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001225 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001226 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001227 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001228 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001229 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1230 if (!sp)
1231 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001232 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001233 sp->gfn = gfn;
1234 sp->role = role;
1235 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001236 if (!metaphysical) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001237 if (rmap_write_protect(vcpu->kvm, gfn))
1238 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001239 account_shadowed(vcpu->kvm, gfn);
1240 }
Avi Kivity131d8272008-05-29 14:56:28 +03001241 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1242 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1243 else
1244 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001245 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001246}
1247
Avi Kivity3d000db2008-08-22 19:24:38 +03001248static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001249 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +03001250{
1251 hpa_t shadow_addr;
1252 int level;
1253 int r;
1254 u64 *sptep;
1255 unsigned index;
1256
1257 shadow_addr = vcpu->arch.mmu.root_hpa;
1258 level = vcpu->arch.mmu.shadow_root_level;
1259 if (level == PT32E_ROOT_LEVEL) {
1260 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1261 shadow_addr &= PT64_BASE_ADDR_MASK;
1262 --level;
1263 }
1264
1265 while (level >= PT_PAGE_TABLE_LEVEL) {
1266 index = SHADOW_PT_INDEX(addr, level);
1267 sptep = ((u64 *)__va(shadow_addr)) + index;
1268 r = walker->entry(walker, vcpu, addr, sptep, level);
1269 if (r)
1270 return r;
1271 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1272 --level;
1273 }
1274 return 0;
1275}
1276
Avi Kivity90cb0522007-07-17 13:04:56 +03001277static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001278 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001279{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001280 unsigned i;
1281 u64 *pt;
1282 u64 ent;
1283
Avi Kivity4db35312007-11-21 15:28:32 +02001284 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001285
Avi Kivity4db35312007-11-21 15:28:32 +02001286 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001287 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001288 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001289 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001290 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001291 }
1292 return;
1293 }
1294
1295 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1296 ent = pt[i];
1297
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001298 if (is_shadow_present_pte(ent)) {
1299 if (!is_large_pte(ent)) {
1300 ent &= PT64_BASE_ADDR_MASK;
1301 mmu_page_remove_parent_pte(page_header(ent),
1302 &pt[i]);
1303 } else {
1304 --kvm->stat.lpages;
1305 rmap_remove(kvm, &pt[i]);
1306 }
1307 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001308 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001309 }
Avi Kivitya4360362007-01-05 16:36:45 -08001310}
1311
Avi Kivity4db35312007-11-21 15:28:32 +02001312static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001313{
Avi Kivity4db35312007-11-21 15:28:32 +02001314 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001315}
1316
Avi Kivity12b7d282007-09-23 14:10:49 +02001317static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1318{
1319 int i;
1320
1321 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1322 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001323 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001324}
1325
Avi Kivity31aa2b42008-07-11 17:59:46 +03001326static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001327{
1328 u64 *parent_pte;
1329
Avi Kivity4db35312007-11-21 15:28:32 +02001330 while (sp->multimapped || sp->parent_pte) {
1331 if (!sp->multimapped)
1332 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001333 else {
1334 struct kvm_pte_chain *chain;
1335
Avi Kivity4db35312007-11-21 15:28:32 +02001336 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001337 struct kvm_pte_chain, link);
1338 parent_pte = chain->parent_ptes[0];
1339 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001340 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001341 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001342 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001343 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001344}
1345
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001346static int mmu_zap_unsync_children(struct kvm *kvm,
1347 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001348{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001349 int i, zapped = 0;
1350 struct mmu_page_path parents;
1351 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001352
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001353 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001354 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001355
1356 kvm_mmu_pages_init(parent, &parents, &pages);
1357 while (mmu_unsync_walk(parent, &pages)) {
1358 struct kvm_mmu_page *sp;
1359
1360 for_each_sp(pages, sp, parents, i) {
1361 kvm_mmu_zap_page(kvm, sp);
1362 mmu_pages_clear_parents(&parents);
1363 }
1364 zapped += pages.nr;
1365 kvm_mmu_pages_init(parent, &parents, &pages);
1366 }
1367
1368 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001369}
1370
Marcelo Tosatti07385412008-09-23 13:18:37 -03001371static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001372{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001374 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001375 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001376 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001377 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001378 kvm_flush_remote_tlbs(kvm);
1379 if (!sp->role.invalid && !sp->role.metaphysical)
1380 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001381 if (sp->unsync)
1382 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001383 if (!sp->root_count) {
1384 hlist_del(&sp->hash_link);
1385 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001386 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001387 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001388 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001389 kvm_reload_remote_mmus(kvm);
1390 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001391 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001392 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001393}
1394
Izik Eidus82ce2c92007-10-02 18:52:55 +02001395/*
1396 * Changing the number of mmu pages allocated to the vm
1397 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1398 */
1399void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1400{
1401 /*
1402 * If we set the number of mmu pages to be smaller be than the
1403 * number of actived pages , we must to free some mmu pages before we
1404 * change the value
1405 */
1406
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001407 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001408 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001409 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1410 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001411
1412 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1413 struct kvm_mmu_page *page;
1414
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001415 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001416 struct kvm_mmu_page, link);
1417 kvm_mmu_zap_page(kvm, page);
1418 n_used_mmu_pages--;
1419 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001420 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001421 }
1422 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001423 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1424 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001425
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001426 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001427}
1428
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001429static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001430{
1431 unsigned index;
1432 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001433 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001434 struct hlist_node *node, *n;
1435 int r;
1436
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001437 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001438 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001439 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001440 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001441 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1442 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001443 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001444 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001445 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001446 if (kvm_mmu_zap_page(kvm, sp))
1447 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001448 }
1449 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001450}
1451
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001452static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001453{
Avi Kivity4db35312007-11-21 15:28:32 +02001454 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001455
Avi Kivity4db35312007-11-21 15:28:32 +02001456 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001457 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001458 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001459 }
1460}
1461
Avi Kivity38c335f2007-11-21 14:20:22 +02001462static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463{
Avi Kivity38c335f2007-11-21 14:20:22 +02001464 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001465 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466
Sheng Yang291f26b2008-10-16 17:30:57 +08001467 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468}
1469
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001470static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1471{
1472 int i;
1473 u64 *pt = sp->spt;
1474
1475 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1476 return;
1477
1478 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1479 if (pt[i] == shadow_notrap_nonpresent_pte)
1480 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1481 }
1482}
1483
Avi Kivity039576c2007-03-20 12:46:50 +02001484struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1485{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001486 struct page *page;
1487
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001488 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001489
1490 if (gpa == UNMAPPED_GVA)
1491 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001492
Izik Eidus72dc67a2008-02-10 18:04:15 +02001493 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001494
1495 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001496}
1497
Sheng Yang74be52e2008-10-09 16:01:56 +08001498/*
1499 * The function is based on mtrr_type_lookup() in
1500 * arch/x86/kernel/cpu/mtrr/generic.c
1501 */
1502static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1503 u64 start, u64 end)
1504{
1505 int i;
1506 u64 base, mask;
1507 u8 prev_match, curr_match;
1508 int num_var_ranges = KVM_NR_VAR_MTRR;
1509
1510 if (!mtrr_state->enabled)
1511 return 0xFF;
1512
1513 /* Make end inclusive end, instead of exclusive */
1514 end--;
1515
1516 /* Look in fixed ranges. Just return the type as per start */
1517 if (mtrr_state->have_fixed && (start < 0x100000)) {
1518 int idx;
1519
1520 if (start < 0x80000) {
1521 idx = 0;
1522 idx += (start >> 16);
1523 return mtrr_state->fixed_ranges[idx];
1524 } else if (start < 0xC0000) {
1525 idx = 1 * 8;
1526 idx += ((start - 0x80000) >> 14);
1527 return mtrr_state->fixed_ranges[idx];
1528 } else if (start < 0x1000000) {
1529 idx = 3 * 8;
1530 idx += ((start - 0xC0000) >> 12);
1531 return mtrr_state->fixed_ranges[idx];
1532 }
1533 }
1534
1535 /*
1536 * Look in variable ranges
1537 * Look of multiple ranges matching this address and pick type
1538 * as per MTRR precedence
1539 */
1540 if (!(mtrr_state->enabled & 2))
1541 return mtrr_state->def_type;
1542
1543 prev_match = 0xFF;
1544 for (i = 0; i < num_var_ranges; ++i) {
1545 unsigned short start_state, end_state;
1546
1547 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1548 continue;
1549
1550 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1551 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1552 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1553 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1554
1555 start_state = ((start & mask) == (base & mask));
1556 end_state = ((end & mask) == (base & mask));
1557 if (start_state != end_state)
1558 return 0xFE;
1559
1560 if ((start & mask) != (base & mask))
1561 continue;
1562
1563 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1564 if (prev_match == 0xFF) {
1565 prev_match = curr_match;
1566 continue;
1567 }
1568
1569 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1570 curr_match == MTRR_TYPE_UNCACHABLE)
1571 return MTRR_TYPE_UNCACHABLE;
1572
1573 if ((prev_match == MTRR_TYPE_WRBACK &&
1574 curr_match == MTRR_TYPE_WRTHROUGH) ||
1575 (prev_match == MTRR_TYPE_WRTHROUGH &&
1576 curr_match == MTRR_TYPE_WRBACK)) {
1577 prev_match = MTRR_TYPE_WRTHROUGH;
1578 curr_match = MTRR_TYPE_WRTHROUGH;
1579 }
1580
1581 if (prev_match != curr_match)
1582 return MTRR_TYPE_UNCACHABLE;
1583 }
1584
1585 if (prev_match != 0xFF)
1586 return prev_match;
1587
1588 return mtrr_state->def_type;
1589}
1590
1591static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1592{
1593 u8 mtrr;
1594
1595 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1596 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1597 if (mtrr == 0xfe || mtrr == 0xff)
1598 mtrr = MTRR_TYPE_WRBACK;
1599 return mtrr;
1600}
1601
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001602static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1603{
1604 unsigned index;
1605 struct hlist_head *bucket;
1606 struct kvm_mmu_page *s;
1607 struct hlist_node *node, *n;
1608
1609 index = kvm_page_table_hashfn(sp->gfn);
1610 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1611 /* don't unsync if pagetable is shadowed with multiple roles */
1612 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1613 if (s->gfn != sp->gfn || s->role.metaphysical)
1614 continue;
1615 if (s->role.word != sp->role.word)
1616 return 1;
1617 }
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001618 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001619 ++vcpu->kvm->stat.mmu_unsync;
1620 sp->unsync = 1;
1621 mmu_convert_notrap(sp);
1622 return 0;
1623}
1624
1625static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1626 bool can_unsync)
1627{
1628 struct kvm_mmu_page *shadow;
1629
1630 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1631 if (shadow) {
1632 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1633 return 1;
1634 if (shadow->unsync)
1635 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001636 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001637 return kvm_unsync_page(vcpu, shadow);
1638 return 1;
1639 }
1640 return 0;
1641}
1642
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001643static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1644 unsigned pte_access, int user_fault,
1645 int write_fault, int dirty, int largepage,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001646 gfn_t gfn, pfn_t pfn, bool speculative,
1647 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001648{
1649 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001650 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001651 u64 mt_mask = shadow_mt_mask;
1652
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001653 /*
1654 * We don't set the accessed bit, since we sometimes want to see
1655 * whether the guest actually used the pte (in order to detect
1656 * demand paging).
1657 */
Sheng Yang7b523452008-04-25 21:13:50 +08001658 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001659 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001660 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001661 if (!dirty)
1662 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001663 if (pte_access & ACC_EXEC_MASK)
1664 spte |= shadow_x_mask;
1665 else
1666 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001667 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001668 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001669 if (largepage)
1670 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001671 if (mt_mask) {
1672 mt_mask = get_memory_type(vcpu, gfn) <<
1673 kvm_x86_ops->get_mt_mask_shift();
1674 spte |= mt_mask;
1675 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001676
Anthony Liguori35149e22008-04-02 14:46:56 -05001677 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001678
1679 if ((pte_access & ACC_WRITE_MASK)
1680 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001681
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001682 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1683 ret = 1;
1684 spte = shadow_trap_nonpresent_pte;
1685 goto set_pte;
1686 }
1687
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001688 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001689
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001690 /*
1691 * Optimization: for pte sync, if spte was writable the hash
1692 * lookup is unnecessary (and expensive). Write protection
1693 * is responsibility of mmu_get_page / kvm_sync_page.
1694 * Same reasoning can be applied to dirty page accounting.
1695 */
1696 if (!can_unsync && is_writeble_pte(*shadow_pte))
1697 goto set_pte;
1698
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001699 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001700 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001701 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001702 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001703 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001704 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001705 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001706 }
1707 }
1708
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001709 if (pte_access & ACC_WRITE_MASK)
1710 mark_page_dirty(vcpu->kvm, gfn);
1711
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001712set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001713 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001714 return ret;
1715}
1716
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001717static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1718 unsigned pt_access, unsigned pte_access,
1719 int user_fault, int write_fault, int dirty,
1720 int *ptwrite, int largepage, gfn_t gfn,
1721 pfn_t pfn, bool speculative)
1722{
1723 int was_rmapped = 0;
1724 int was_writeble = is_writeble_pte(*shadow_pte);
1725
1726 pgprintk("%s: spte %llx access %x write_fault %d"
1727 " user_fault %d gfn %lx\n",
1728 __func__, *shadow_pte, pt_access,
1729 write_fault, user_fault, gfn);
1730
1731 if (is_rmap_pte(*shadow_pte)) {
1732 /*
1733 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1734 * the parent of the now unreachable PTE.
1735 */
1736 if (largepage && !is_large_pte(*shadow_pte)) {
1737 struct kvm_mmu_page *child;
1738 u64 pte = *shadow_pte;
1739
1740 child = page_header(pte & PT64_BASE_ADDR_MASK);
1741 mmu_page_remove_parent_pte(child, shadow_pte);
1742 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1743 pgprintk("hfn old %lx new %lx\n",
1744 spte_to_pfn(*shadow_pte), pfn);
1745 rmap_remove(vcpu->kvm, shadow_pte);
1746 } else {
1747 if (largepage)
1748 was_rmapped = is_large_pte(*shadow_pte);
1749 else
1750 was_rmapped = 1;
1751 }
1752 }
1753 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001754 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001755 if (write_fault)
1756 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001757 kvm_x86_ops->tlb_flush(vcpu);
1758 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001759
1760 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1761 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1762 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1763 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1764 *shadow_pte, shadow_pte);
1765 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001766 ++vcpu->kvm->stat.lpages;
1767
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001768 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1769 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001770 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001771 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001772 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001773 } else {
1774 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001775 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001776 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001777 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001778 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001779 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001780 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001781 vcpu->arch.last_pte_gfn = gfn;
1782 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001783}
1784
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1786{
1787}
1788
Avi Kivity140754b2008-08-22 19:28:04 +03001789struct direct_shadow_walk {
1790 struct kvm_shadow_walk walker;
1791 pfn_t pfn;
1792 int write;
1793 int largepage;
1794 int pt_write;
1795};
1796
1797static int direct_map_entry(struct kvm_shadow_walk *_walk,
1798 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001799 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001800{
1801 struct direct_shadow_walk *walk =
1802 container_of(_walk, struct direct_shadow_walk, walker);
1803 struct kvm_mmu_page *sp;
1804 gfn_t pseudo_gfn;
1805 gfn_t gfn = addr >> PAGE_SHIFT;
1806
1807 if (level == PT_PAGE_TABLE_LEVEL
1808 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1809 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1810 0, walk->write, 1, &walk->pt_write,
1811 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001812 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001813 return 1;
1814 }
1815
1816 if (*sptep == shadow_trap_nonpresent_pte) {
1817 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001818 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001819 1, ACC_ALL, sptep);
1820 if (!sp) {
1821 pgprintk("nonpaging_map: ENOMEM\n");
1822 kvm_release_pfn_clean(walk->pfn);
1823 return -ENOMEM;
1824 }
1825
1826 set_shadow_pte(sptep,
1827 __pa(sp->spt)
1828 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1829 | shadow_user_mask | shadow_x_mask);
1830 }
1831 return 0;
1832}
1833
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001834static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001835 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836{
Avi Kivity140754b2008-08-22 19:28:04 +03001837 int r;
1838 struct direct_shadow_walk walker = {
1839 .walker = { .entry = direct_map_entry, },
1840 .pfn = pfn,
1841 .largepage = largepage,
1842 .write = write,
1843 .pt_write = 0,
1844 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001846 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001847 if (r < 0)
1848 return r;
1849 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850}
1851
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001852static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1853{
1854 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001855 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001856 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001857 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001858
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001859 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1860 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1861 largepage = 1;
1862 }
1863
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001864 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001865 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001866 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001867
Avi Kivityd196e342008-01-24 11:44:11 +02001868 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001869 if (is_error_pfn(pfn)) {
1870 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001871 return 1;
1872 }
1873
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001874 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001875 if (mmu_notifier_retry(vcpu, mmu_seq))
1876 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001877 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001878 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001879 spin_unlock(&vcpu->kvm->mmu_lock);
1880
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001881
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001882 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001883
1884out_unlock:
1885 spin_unlock(&vcpu->kvm->mmu_lock);
1886 kvm_release_pfn_clean(pfn);
1887 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001888}
1889
1890
Avi Kivity17ac10a2007-01-05 16:36:40 -08001891static void mmu_free_roots(struct kvm_vcpu *vcpu)
1892{
1893 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001894 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001895
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001896 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001897 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001898 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001899 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1900 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001901
Avi Kivity4db35312007-11-21 15:28:32 +02001902 sp = page_header(root);
1903 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001904 if (!sp->root_count && sp->role.invalid)
1905 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001906 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001907 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001908 return;
1909 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001910 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001911 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001912
Avi Kivity417726a2007-04-12 17:35:58 +03001913 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001914 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001915 sp = page_header(root);
1916 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001917 if (!sp->root_count && sp->role.invalid)
1918 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001919 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001920 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001921 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001922 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001923 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001924}
1925
1926static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1927{
1928 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001929 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001930 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001931 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001932
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001933 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001934
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1936 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001937
1938 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001939 if (tdp_enabled)
1940 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001941 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001942 PT64_ROOT_LEVEL, metaphysical,
1943 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001944 root = __pa(sp->spt);
1945 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001946 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001947 return;
1948 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001949 metaphysical = !is_paging(vcpu);
1950 if (tdp_enabled)
1951 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001952 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001953 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001954
1955 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001956 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1957 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1958 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001959 continue;
1960 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001961 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1962 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001963 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001964 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001965 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001966 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001967 root = __pa(sp->spt);
1968 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001969 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001970 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001971 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001972}
1973
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001974static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1975{
1976 int i;
1977 struct kvm_mmu_page *sp;
1978
1979 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1980 return;
1981 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1982 hpa_t root = vcpu->arch.mmu.root_hpa;
1983 sp = page_header(root);
1984 mmu_sync_children(vcpu, sp);
1985 return;
1986 }
1987 for (i = 0; i < 4; ++i) {
1988 hpa_t root = vcpu->arch.mmu.pae_root[i];
1989
1990 if (root) {
1991 root &= PT64_BASE_ADDR_MASK;
1992 sp = page_header(root);
1993 mmu_sync_children(vcpu, sp);
1994 }
1995 }
1996}
1997
1998void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1999{
2000 spin_lock(&vcpu->kvm->mmu_lock);
2001 mmu_sync_roots(vcpu);
2002 spin_unlock(&vcpu->kvm->mmu_lock);
2003}
2004
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2006{
2007 return vaddr;
2008}
2009
2010static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002011 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012{
Avi Kivitye8332402007-12-09 18:43:00 +02002013 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002014 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002016 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002017 r = mmu_topup_memory_caches(vcpu);
2018 if (r)
2019 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002020
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002022 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023
Avi Kivitye8332402007-12-09 18:43:00 +02002024 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025
Avi Kivitye8332402007-12-09 18:43:00 +02002026 return nonpaging_map(vcpu, gva & PAGE_MASK,
2027 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028}
2029
Joerg Roedelfb72d162008-02-07 13:47:44 +01002030static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2031 u32 error_code)
2032{
Anthony Liguori35149e22008-04-02 14:46:56 -05002033 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002034 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002035 int largepage = 0;
2036 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002037 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002038
2039 ASSERT(vcpu);
2040 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2041
2042 r = mmu_topup_memory_caches(vcpu);
2043 if (r)
2044 return r;
2045
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002046 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2047 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2048 largepage = 1;
2049 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002050 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002051 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002052 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002053 if (is_error_pfn(pfn)) {
2054 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002055 return 1;
2056 }
2057 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002058 if (mmu_notifier_retry(vcpu, mmu_seq))
2059 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002060 kvm_mmu_free_some_pages(vcpu);
2061 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002062 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002063 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002064
2065 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002066
2067out_unlock:
2068 spin_unlock(&vcpu->kvm->mmu_lock);
2069 kvm_release_pfn_clean(pfn);
2070 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002071}
2072
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073static void nonpaging_free(struct kvm_vcpu *vcpu)
2074{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002075 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076}
2077
2078static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2079{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002080 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081
2082 context->new_cr3 = nonpaging_new_cr3;
2083 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084 context->gva_to_gpa = nonpaging_gva_to_gpa;
2085 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002086 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002087 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002088 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002089 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002091 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 return 0;
2093}
2094
Avi Kivityd835dfe2007-11-21 02:57:59 +02002095void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002097 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002098 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099}
2100
2101static void paging_new_cr3(struct kvm_vcpu *vcpu)
2102{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002103 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002104 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105}
2106
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107static void inject_page_fault(struct kvm_vcpu *vcpu,
2108 u64 addr,
2109 u32 err_code)
2110{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002111 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void paging_free(struct kvm_vcpu *vcpu)
2115{
2116 nonpaging_free(vcpu);
2117}
2118
2119#define PTTYPE 64
2120#include "paging_tmpl.h"
2121#undef PTTYPE
2122
2123#define PTTYPE 32
2124#include "paging_tmpl.h"
2125#undef PTTYPE
2126
Avi Kivity17ac10a2007-01-05 16:36:40 -08002127static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002129 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130
2131 ASSERT(is_pae(vcpu));
2132 context->new_cr3 = paging_new_cr3;
2133 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002135 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002136 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002137 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002139 context->root_level = level;
2140 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002141 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 return 0;
2143}
2144
Avi Kivity17ac10a2007-01-05 16:36:40 -08002145static int paging64_init_context(struct kvm_vcpu *vcpu)
2146{
2147 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2148}
2149
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150static int paging32_init_context(struct kvm_vcpu *vcpu)
2151{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002152 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153
2154 context->new_cr3 = paging_new_cr3;
2155 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 context->gva_to_gpa = paging32_gva_to_gpa;
2157 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002158 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002159 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002160 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161 context->root_level = PT32_ROOT_LEVEL;
2162 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002163 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 return 0;
2165}
2166
2167static int paging32E_init_context(struct kvm_vcpu *vcpu)
2168{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002169 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170}
2171
Joerg Roedelfb72d162008-02-07 13:47:44 +01002172static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2173{
2174 struct kvm_mmu *context = &vcpu->arch.mmu;
2175
2176 context->new_cr3 = nonpaging_new_cr3;
2177 context->page_fault = tdp_page_fault;
2178 context->free = nonpaging_free;
2179 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002180 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002181 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002182 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002183 context->root_hpa = INVALID_PAGE;
2184
2185 if (!is_paging(vcpu)) {
2186 context->gva_to_gpa = nonpaging_gva_to_gpa;
2187 context->root_level = 0;
2188 } else if (is_long_mode(vcpu)) {
2189 context->gva_to_gpa = paging64_gva_to_gpa;
2190 context->root_level = PT64_ROOT_LEVEL;
2191 } else if (is_pae(vcpu)) {
2192 context->gva_to_gpa = paging64_gva_to_gpa;
2193 context->root_level = PT32E_ROOT_LEVEL;
2194 } else {
2195 context->gva_to_gpa = paging32_gva_to_gpa;
2196 context->root_level = PT32_ROOT_LEVEL;
2197 }
2198
2199 return 0;
2200}
2201
2202static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
2204 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002205 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206
2207 if (!is_paging(vcpu))
2208 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002209 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210 return paging64_init_context(vcpu);
2211 else if (is_pae(vcpu))
2212 return paging32E_init_context(vcpu);
2213 else
2214 return paging32_init_context(vcpu);
2215}
2216
Joerg Roedelfb72d162008-02-07 13:47:44 +01002217static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2218{
Anthony Liguori35149e22008-04-02 14:46:56 -05002219 vcpu->arch.update_pte.pfn = bad_pfn;
2220
Joerg Roedelfb72d162008-02-07 13:47:44 +01002221 if (tdp_enabled)
2222 return init_kvm_tdp_mmu(vcpu);
2223 else
2224 return init_kvm_softmmu(vcpu);
2225}
2226
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2228{
2229 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002230 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2231 vcpu->arch.mmu.free(vcpu);
2232 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 }
2234}
2235
2236int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2237{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002238 destroy_kvm_mmu(vcpu);
2239 return init_kvm_mmu(vcpu);
2240}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002241EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002242
2243int kvm_mmu_load(struct kvm_vcpu *vcpu)
2244{
Avi Kivity714b93d2007-01-05 16:36:53 -08002245 int r;
2246
Avi Kivitye2dec932007-01-05 16:36:54 -08002247 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002248 if (r)
2249 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002250 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002251 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002252 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002253 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002254 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002255 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002256 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002257out:
2258 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002260EXPORT_SYMBOL_GPL(kvm_mmu_load);
2261
2262void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2263{
2264 mmu_free_roots(vcpu);
2265}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266
Avi Kivity09072da2007-05-01 14:16:52 +03002267static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002268 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002269 u64 *spte)
2270{
2271 u64 pte;
2272 struct kvm_mmu_page *child;
2273
2274 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002275 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002276 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2277 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002278 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002279 else {
2280 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002281 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002282 }
2283 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002284 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002285 if (is_large_pte(pte))
2286 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002287}
2288
Avi Kivity00284252007-05-01 16:53:31 +03002289static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002290 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002291 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002292 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002293{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002294 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2295 if (!vcpu->arch.update_pte.largepage ||
2296 sp->role.glevels == PT32_ROOT_LEVEL) {
2297 ++vcpu->kvm->stat.mmu_pde_zapped;
2298 return;
2299 }
2300 }
Avi Kivity00284252007-05-01 16:53:31 +03002301
Avi Kivity4cee5762007-11-18 16:37:07 +02002302 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002303 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002304 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002305 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002306 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002307}
2308
Avi Kivity79539ce2007-11-21 02:06:21 +02002309static bool need_remote_flush(u64 old, u64 new)
2310{
2311 if (!is_shadow_present_pte(old))
2312 return false;
2313 if (!is_shadow_present_pte(new))
2314 return true;
2315 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2316 return true;
2317 old ^= PT64_NX_MASK;
2318 new ^= PT64_NX_MASK;
2319 return (old & ~new & PT64_PERM_MASK) != 0;
2320}
2321
2322static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2323{
2324 if (need_remote_flush(old, new))
2325 kvm_flush_remote_tlbs(vcpu->kvm);
2326 else
2327 kvm_mmu_flush_tlb(vcpu);
2328}
2329
Avi Kivity12b7d282007-09-23 14:10:49 +02002330static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2331{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002332 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002333
Sheng Yang7b523452008-04-25 21:13:50 +08002334 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002335}
2336
Avi Kivityd7824ff2007-12-30 12:29:05 +02002337static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2338 const u8 *new, int bytes)
2339{
2340 gfn_t gfn;
2341 int r;
2342 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002343 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002344
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002345 vcpu->arch.update_pte.largepage = 0;
2346
Avi Kivityd7824ff2007-12-30 12:29:05 +02002347 if (bytes != 4 && bytes != 8)
2348 return;
2349
2350 /*
2351 * Assume that the pte write on a page table of the same type
2352 * as the current vcpu paging mode. This is nearly always true
2353 * (might be false while changing modes). Note it is verified later
2354 * by update_pte().
2355 */
2356 if (is_pae(vcpu)) {
2357 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2358 if ((bytes == 4) && (gpa % 4 == 0)) {
2359 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2360 if (r)
2361 return;
2362 memcpy((void *)&gpte + (gpa % 8), new, 4);
2363 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2364 memcpy((void *)&gpte, new, 8);
2365 }
2366 } else {
2367 if ((bytes == 4) && (gpa % 4 == 0))
2368 memcpy((void *)&gpte, new, 4);
2369 }
2370 if (!is_present_pte(gpte))
2371 return;
2372 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002373
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002374 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2375 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2376 vcpu->arch.update_pte.largepage = 1;
2377 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002378 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002379 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002380 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002381
Anthony Liguori35149e22008-04-02 14:46:56 -05002382 if (is_error_pfn(pfn)) {
2383 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002384 return;
2385 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002386 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002387 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002388}
2389
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002390static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2391{
2392 u64 *spte = vcpu->arch.last_pte_updated;
2393
2394 if (spte
2395 && vcpu->arch.last_pte_gfn == gfn
2396 && shadow_accessed_mask
2397 && !(*spte & shadow_accessed_mask)
2398 && is_shadow_present_pte(*spte))
2399 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2400}
2401
Avi Kivity09072da2007-05-01 14:16:52 +03002402void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08002403 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002404{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002405 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002406 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002407 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002408 struct hlist_head *bucket;
2409 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002410 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002411 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002412 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002413 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002414 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002415 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002416 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002417 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002418 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002419 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002420 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002421
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002422 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002423 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002424 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002425 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002426 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002427 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002428 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002429 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02002430 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002431 ++vcpu->arch.last_pt_write_count;
2432 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08002433 flooded = 1;
2434 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 vcpu->arch.last_pt_write_gfn = gfn;
2436 vcpu->arch.last_pt_write_count = 1;
2437 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002438 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002439 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002440 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002441 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002442 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002443 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002444 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002445 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002446 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002447 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002448 /*
2449 * Misaligned accesses are too much trouble to fix
2450 * up; also, they usually indicate a page is not used
2451 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002452 *
2453 * If we're seeing too many writes to a page,
2454 * it may no longer be a page table, or we may be
2455 * forking, in which case it is better to unmap the
2456 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002457 */
2458 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002459 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002460 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2461 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002462 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002463 continue;
2464 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002465 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002466 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002467 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002468 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002469 page_offset <<= 1; /* 32->64 */
2470 /*
2471 * A 32-bit pde maps 4MB while the shadow pdes map
2472 * only 2MB. So we need to double the offset again
2473 * and zap two pdes instead of one.
2474 */
2475 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002476 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002477 page_offset <<= 1;
2478 npte = 2;
2479 }
Avi Kivityfce06572007-05-01 16:44:05 +03002480 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002481 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002482 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002483 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002484 }
Avi Kivity4db35312007-11-21 15:28:32 +02002485 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002486 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2487 gentry = 0;
2488 r = kvm_read_guest_atomic(vcpu->kvm,
2489 gpa & ~(u64)(pte_size - 1),
2490 &gentry, pte_size);
2491 new = (const void *)&gentry;
2492 if (r < 0)
2493 new = NULL;
2494 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002495 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002496 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002497 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002498 if (new)
2499 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002500 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002501 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002502 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002503 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002504 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002505 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002506 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2507 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2508 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002509 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002510}
2511
Avi Kivitya4360362007-01-05 16:36:45 -08002512int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2513{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002514 gpa_t gpa;
2515 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002516
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002517 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002518
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002519 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002520 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002521 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002522 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002523}
Avi Kivity577bdc42008-07-19 08:57:05 +03002524EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002525
Avi Kivity22d95b12007-09-14 20:26:06 +03002526void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002527{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002528 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002529 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002530
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002531 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002532 struct kvm_mmu_page, link);
2533 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002534 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002535 }
2536}
Avi Kivityebeace82007-01-05 16:36:47 -08002537
Avi Kivity30677142007-10-28 18:48:59 +02002538int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2539{
2540 int r;
2541 enum emulation_result er;
2542
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002543 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002544 if (r < 0)
2545 goto out;
2546
2547 if (!r) {
2548 r = 1;
2549 goto out;
2550 }
2551
Avi Kivityb733bfb2007-10-28 18:52:05 +02002552 r = mmu_topup_memory_caches(vcpu);
2553 if (r)
2554 goto out;
2555
Avi Kivity30677142007-10-28 18:48:59 +02002556 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002557
2558 switch (er) {
2559 case EMULATE_DONE:
2560 return 1;
2561 case EMULATE_DO_MMIO:
2562 ++vcpu->stat.mmio_exits;
2563 return 0;
2564 case EMULATE_FAIL:
2565 kvm_report_emulation_failure(vcpu, "pagetable");
2566 return 1;
2567 default:
2568 BUG();
2569 }
2570out:
Avi Kivity30677142007-10-28 18:48:59 +02002571 return r;
2572}
2573EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2574
Marcelo Tosattia7052892008-09-23 13:18:35 -03002575void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2576{
2577 spin_lock(&vcpu->kvm->mmu_lock);
2578 vcpu->arch.mmu.invlpg(vcpu, gva);
2579 spin_unlock(&vcpu->kvm->mmu_lock);
2580 kvm_mmu_flush_tlb(vcpu);
2581 ++vcpu->stat.invlpg;
2582}
2583EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2584
Joerg Roedel18552672008-02-07 13:47:41 +01002585void kvm_enable_tdp(void)
2586{
2587 tdp_enabled = true;
2588}
2589EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2590
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002591void kvm_disable_tdp(void)
2592{
2593 tdp_enabled = false;
2594}
2595EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2596
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597static void free_mmu_pages(struct kvm_vcpu *vcpu)
2598{
Avi Kivity4db35312007-11-21 15:28:32 +02002599 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002601 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2602 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002603 struct kvm_mmu_page, link);
2604 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002605 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002606 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002607 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608}
2609
2610static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2611{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002612 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 int i;
2614
2615 ASSERT(vcpu);
2616
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002617 if (vcpu->kvm->arch.n_requested_mmu_pages)
2618 vcpu->kvm->arch.n_free_mmu_pages =
2619 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002620 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002621 vcpu->kvm->arch.n_free_mmu_pages =
2622 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002623 /*
2624 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2625 * Therefore we need to allocate shadow page tables in the first
2626 * 4GB of memory, which happens to fit the DMA32 zone.
2627 */
2628 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2629 if (!page)
2630 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002631 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002632 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002633 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002634
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 return 0;
2636
2637error_1:
2638 free_mmu_pages(vcpu);
2639 return -ENOMEM;
2640}
2641
Ingo Molnar8018c272006-12-29 16:50:01 -08002642int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002645 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646
Ingo Molnar8018c272006-12-29 16:50:01 -08002647 return alloc_mmu_pages(vcpu);
2648}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649
Ingo Molnar8018c272006-12-29 16:50:01 -08002650int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2651{
2652 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002653 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002654
Ingo Molnar8018c272006-12-29 16:50:01 -08002655 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656}
2657
2658void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2659{
2660 ASSERT(vcpu);
2661
2662 destroy_kvm_mmu(vcpu);
2663 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002664 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665}
2666
Avi Kivity90cb0522007-07-17 13:04:56 +03002667void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668{
Avi Kivity4db35312007-11-21 15:28:32 +02002669 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670
Avi Kivity2245a282008-08-27 16:32:24 +03002671 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002672 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 int i;
2674 u64 *pt;
2675
Sheng Yang291f26b2008-10-16 17:30:57 +08002676 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 continue;
2678
Avi Kivity4db35312007-11-21 15:28:32 +02002679 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2681 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002682 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 }
Avi Kivity171d5952008-08-27 16:40:51 +03002685 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002686 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002688
Avi Kivity90cb0522007-07-17 13:04:56 +03002689void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002690{
Avi Kivity4db35312007-11-21 15:28:32 +02002691 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002692
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002693 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002694 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002695 if (kvm_mmu_zap_page(kvm, sp))
2696 node = container_of(kvm->arch.active_mmu_pages.next,
2697 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002698 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002699
Avi Kivity90cb0522007-07-17 13:04:56 +03002700 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002701}
2702
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002703static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002704{
2705 struct kvm_mmu_page *page;
2706
2707 page = container_of(kvm->arch.active_mmu_pages.prev,
2708 struct kvm_mmu_page, link);
2709 kvm_mmu_zap_page(kvm, page);
2710}
2711
2712static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2713{
2714 struct kvm *kvm;
2715 struct kvm *kvm_freed = NULL;
2716 int cache_count = 0;
2717
2718 spin_lock(&kvm_lock);
2719
2720 list_for_each_entry(kvm, &vm_list, vm_list) {
2721 int npages;
2722
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002723 if (!down_read_trylock(&kvm->slots_lock))
2724 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002725 spin_lock(&kvm->mmu_lock);
2726 npages = kvm->arch.n_alloc_mmu_pages -
2727 kvm->arch.n_free_mmu_pages;
2728 cache_count += npages;
2729 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2730 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2731 cache_count--;
2732 kvm_freed = kvm;
2733 }
2734 nr_to_scan--;
2735
2736 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002737 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002738 }
2739 if (kvm_freed)
2740 list_move_tail(&kvm_freed->vm_list, &vm_list);
2741
2742 spin_unlock(&kvm_lock);
2743
2744 return cache_count;
2745}
2746
2747static struct shrinker mmu_shrinker = {
2748 .shrink = mmu_shrink,
2749 .seeks = DEFAULT_SEEKS * 10,
2750};
2751
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002752static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002753{
2754 if (pte_chain_cache)
2755 kmem_cache_destroy(pte_chain_cache);
2756 if (rmap_desc_cache)
2757 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002758 if (mmu_page_header_cache)
2759 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002760}
2761
Izik Eidus3ee16c82008-03-30 15:17:21 +03002762void kvm_mmu_module_exit(void)
2763{
2764 mmu_destroy_caches();
2765 unregister_shrinker(&mmu_shrinker);
2766}
2767
Avi Kivityb5a33a72007-04-15 16:31:09 +03002768int kvm_mmu_module_init(void)
2769{
2770 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2771 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002772 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002773 if (!pte_chain_cache)
2774 goto nomem;
2775 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2776 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002777 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002778 if (!rmap_desc_cache)
2779 goto nomem;
2780
Avi Kivityd3d25b02007-05-30 12:34:53 +03002781 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2782 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002783 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002784 if (!mmu_page_header_cache)
2785 goto nomem;
2786
Izik Eidus3ee16c82008-03-30 15:17:21 +03002787 register_shrinker(&mmu_shrinker);
2788
Avi Kivityb5a33a72007-04-15 16:31:09 +03002789 return 0;
2790
2791nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002792 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002793 return -ENOMEM;
2794}
2795
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002796/*
2797 * Caculate mmu pages needed for kvm.
2798 */
2799unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2800{
2801 int i;
2802 unsigned int nr_mmu_pages;
2803 unsigned int nr_pages = 0;
2804
2805 for (i = 0; i < kvm->nmemslots; i++)
2806 nr_pages += kvm->memslots[i].npages;
2807
2808 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2809 nr_mmu_pages = max(nr_mmu_pages,
2810 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2811
2812 return nr_mmu_pages;
2813}
2814
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002815static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2816 unsigned len)
2817{
2818 if (len > buffer->len)
2819 return NULL;
2820 return buffer->ptr;
2821}
2822
2823static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2824 unsigned len)
2825{
2826 void *ret;
2827
2828 ret = pv_mmu_peek_buffer(buffer, len);
2829 if (!ret)
2830 return ret;
2831 buffer->ptr += len;
2832 buffer->len -= len;
2833 buffer->processed += len;
2834 return ret;
2835}
2836
2837static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2838 gpa_t addr, gpa_t value)
2839{
2840 int bytes = 8;
2841 int r;
2842
2843 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2844 bytes = 4;
2845
2846 r = mmu_topup_memory_caches(vcpu);
2847 if (r)
2848 return r;
2849
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002850 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002851 return -EFAULT;
2852
2853 return 1;
2854}
2855
2856static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2857{
2858 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002859 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002860 return 1;
2861}
2862
2863static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2864{
2865 spin_lock(&vcpu->kvm->mmu_lock);
2866 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2867 spin_unlock(&vcpu->kvm->mmu_lock);
2868 return 1;
2869}
2870
2871static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2872 struct kvm_pv_mmu_op_buffer *buffer)
2873{
2874 struct kvm_mmu_op_header *header;
2875
2876 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2877 if (!header)
2878 return 0;
2879 switch (header->op) {
2880 case KVM_MMU_OP_WRITE_PTE: {
2881 struct kvm_mmu_op_write_pte *wpte;
2882
2883 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2884 if (!wpte)
2885 return 0;
2886 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2887 wpte->pte_val);
2888 }
2889 case KVM_MMU_OP_FLUSH_TLB: {
2890 struct kvm_mmu_op_flush_tlb *ftlb;
2891
2892 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2893 if (!ftlb)
2894 return 0;
2895 return kvm_pv_mmu_flush_tlb(vcpu);
2896 }
2897 case KVM_MMU_OP_RELEASE_PT: {
2898 struct kvm_mmu_op_release_pt *rpt;
2899
2900 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2901 if (!rpt)
2902 return 0;
2903 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2904 }
2905 default: return 0;
2906 }
2907}
2908
2909int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2910 gpa_t addr, unsigned long *ret)
2911{
2912 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002913 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002914
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002915 buffer->ptr = buffer->buf;
2916 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2917 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002918
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002919 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002920 if (r)
2921 goto out;
2922
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002923 while (buffer->len) {
2924 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002925 if (r < 0)
2926 goto out;
2927 if (r == 0)
2928 break;
2929 }
2930
2931 r = 1;
2932out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002933 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002934 return r;
2935}
2936
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002937#ifdef AUDIT
2938
2939static const char *audit_msg;
2940
2941static gva_t canonicalize(gva_t gva)
2942{
2943#ifdef CONFIG_X86_64
2944 gva = (long long)(gva << 16) >> 16;
2945#endif
2946 return gva;
2947}
2948
2949static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2950 gva_t va, int level)
2951{
2952 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2953 int i;
2954 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2955
2956 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2957 u64 ent = pt[i];
2958
Avi Kivityc7addb92007-09-16 18:58:32 +02002959 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002960 continue;
2961
2962 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002963 if (level > 1) {
2964 if (ent == shadow_notrap_nonpresent_pte)
2965 printk(KERN_ERR "audit: (%s) nontrapping pte"
2966 " in nonleaf level: levels %d gva %lx"
2967 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002968 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002969
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002970 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002971 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002972 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002973 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002974
Avi Kivityc7addb92007-09-16 18:58:32 +02002975 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002976 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002977 printk(KERN_ERR "xx audit error: (%s) levels %d"
2978 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002979 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002980 va, gpa, hpa, ent,
2981 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002982 else if (ent == shadow_notrap_nonpresent_pte
2983 && !is_error_hpa(hpa))
2984 printk(KERN_ERR "audit: (%s) notrap shadow,"
2985 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002986 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002987
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002988 }
2989 }
2990}
2991
2992static void audit_mappings(struct kvm_vcpu *vcpu)
2993{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002994 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002995
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002996 if (vcpu->arch.mmu.root_level == 4)
2997 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002998 else
2999 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003000 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003001 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003002 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003003 i << 30,
3004 2);
3005}
3006
3007static int count_rmaps(struct kvm_vcpu *vcpu)
3008{
3009 int nmaps = 0;
3010 int i, j, k;
3011
3012 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3013 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3014 struct kvm_rmap_desc *d;
3015
3016 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003017 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003018
Izik Eidus290fc382007-09-27 14:11:22 +02003019 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003020 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003021 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003022 ++nmaps;
3023 continue;
3024 }
Izik Eidus290fc382007-09-27 14:11:22 +02003025 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003026 while (d) {
3027 for (k = 0; k < RMAP_EXT; ++k)
3028 if (d->shadow_ptes[k])
3029 ++nmaps;
3030 else
3031 break;
3032 d = d->more;
3033 }
3034 }
3035 }
3036 return nmaps;
3037}
3038
3039static int count_writable_mappings(struct kvm_vcpu *vcpu)
3040{
3041 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02003042 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003043 int i;
3044
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003045 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003046 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003047
Avi Kivity4db35312007-11-21 15:28:32 +02003048 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003049 continue;
3050
3051 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3052 u64 ent = pt[i];
3053
3054 if (!(ent & PT_PRESENT_MASK))
3055 continue;
3056 if (!(ent & PT_WRITABLE_MASK))
3057 continue;
3058 ++nmaps;
3059 }
3060 }
3061 return nmaps;
3062}
3063
3064static void audit_rmap(struct kvm_vcpu *vcpu)
3065{
3066 int n_rmap = count_rmaps(vcpu);
3067 int n_actual = count_writable_mappings(vcpu);
3068
3069 if (n_rmap != n_actual)
3070 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003071 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003072}
3073
3074static void audit_write_protection(struct kvm_vcpu *vcpu)
3075{
Avi Kivity4db35312007-11-21 15:28:32 +02003076 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003077 struct kvm_memory_slot *slot;
3078 unsigned long *rmapp;
3079 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003080
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003081 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003082 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003083 continue;
3084
Avi Kivity4db35312007-11-21 15:28:32 +02003085 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003086 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003087 rmapp = &slot->rmap[gfn - slot->base_gfn];
3088 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003089 printk(KERN_ERR "%s: (%s) shadow page has writable"
3090 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003091 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003092 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003093 }
3094}
3095
3096static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3097{
3098 int olddbg = dbg;
3099
3100 dbg = 0;
3101 audit_msg = msg;
3102 audit_rmap(vcpu);
3103 audit_write_protection(vcpu);
3104 audit_mappings(vcpu);
3105 dbg = olddbg;
3106}
3107
3108#endif