blob: dd20b199a7c0a34e6434e23f59059ac6c02e440e [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
624static void rmap_write_protect(struct kvm *kvm, u64 gfn)
625{
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
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800670 if (write_protected)
671 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800672}
673
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200674static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
675{
676 u64 *spte;
677 int need_tlb_flush = 0;
678
679 while ((spte = rmap_next(kvm, rmapp, NULL))) {
680 BUG_ON(!(*spte & PT_PRESENT_MASK));
681 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
682 rmap_remove(kvm, spte);
683 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
684 need_tlb_flush = 1;
685 }
686 return need_tlb_flush;
687}
688
689static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
690 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
691{
692 int i;
693 int retval = 0;
694
695 /*
696 * If mmap_sem isn't taken, we can look the memslots with only
697 * the mmu_lock by skipping over the slots with userspace_addr == 0.
698 */
699 for (i = 0; i < kvm->nmemslots; i++) {
700 struct kvm_memory_slot *memslot = &kvm->memslots[i];
701 unsigned long start = memslot->userspace_addr;
702 unsigned long end;
703
704 /* mmu_lock protects userspace_addr */
705 if (!start)
706 continue;
707
708 end = start + (memslot->npages << PAGE_SHIFT);
709 if (hva >= start && hva < end) {
710 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
711 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
712 retval |= handler(kvm,
713 &memslot->lpage_info[
714 gfn_offset /
715 KVM_PAGES_PER_HPAGE].rmap_pde);
716 }
717 }
718
719 return retval;
720}
721
722int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
723{
724 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
725}
726
727static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
728{
729 u64 *spte;
730 int young = 0;
731
Sheng Yang534e38b2008-09-08 15:12:30 +0800732 /* always return old for EPT */
733 if (!shadow_accessed_mask)
734 return 0;
735
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200736 spte = rmap_next(kvm, rmapp, NULL);
737 while (spte) {
738 int _young;
739 u64 _spte = *spte;
740 BUG_ON(!(_spte & PT_PRESENT_MASK));
741 _young = _spte & PT_ACCESSED_MASK;
742 if (_young) {
743 young = 1;
744 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
745 }
746 spte = rmap_next(kvm, rmapp, spte);
747 }
748 return young;
749}
750
751int kvm_age_hva(struct kvm *kvm, unsigned long hva)
752{
753 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
754}
755
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800756#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300757static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758{
Avi Kivity139bdb22007-01-05 16:36:50 -0800759 u64 *pos;
760 u64 *end;
761
Avi Kivity47ad8e62007-05-06 15:50:58 +0300762 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300763 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800764 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800765 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800767 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 return 1;
769}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800770#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771
Avi Kivity4db35312007-11-21 15:28:32 +0200772static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800773{
Avi Kivity4db35312007-11-21 15:28:32 +0200774 ASSERT(is_empty_shadow_page(sp->spt));
775 list_del(&sp->link);
776 __free_page(virt_to_page(sp->spt));
777 __free_page(virt_to_page(sp->gfns));
778 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800779 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800780}
781
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800782static unsigned kvm_page_table_hashfn(gfn_t gfn)
783{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200784 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800785}
786
Avi Kivity25c0de22007-01-05 16:36:42 -0800787static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
788 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
Avi Kivity4db35312007-11-21 15:28:32 +0200790 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800792 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
793 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
794 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200795 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800796 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200797 ASSERT(is_empty_shadow_page(sp->spt));
Sheng Yang291f26b2008-10-16 17:30:57 +0800798 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200799 sp->multimapped = 0;
800 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800801 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200802 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803}
804
Avi Kivity714b93d2007-01-05 16:36:53 -0800805static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200806 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800807{
808 struct kvm_pte_chain *pte_chain;
809 struct hlist_node *node;
810 int i;
811
812 if (!parent_pte)
813 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200814 if (!sp->multimapped) {
815 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800816
817 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200818 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800819 return;
820 }
Avi Kivity4db35312007-11-21 15:28:32 +0200821 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800822 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200823 INIT_HLIST_HEAD(&sp->parent_ptes);
824 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800825 pte_chain->parent_ptes[0] = old;
826 }
Avi Kivity4db35312007-11-21 15:28:32 +0200827 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800828 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
829 continue;
830 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
831 if (!pte_chain->parent_ptes[i]) {
832 pte_chain->parent_ptes[i] = parent_pte;
833 return;
834 }
835 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800836 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800837 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200838 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800839 pte_chain->parent_ptes[0] = parent_pte;
840}
841
Avi Kivity4db35312007-11-21 15:28:32 +0200842static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843 u64 *parent_pte)
844{
845 struct kvm_pte_chain *pte_chain;
846 struct hlist_node *node;
847 int i;
848
Avi Kivity4db35312007-11-21 15:28:32 +0200849 if (!sp->multimapped) {
850 BUG_ON(sp->parent_pte != parent_pte);
851 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852 return;
853 }
Avi Kivity4db35312007-11-21 15:28:32 +0200854 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800855 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
856 if (!pte_chain->parent_ptes[i])
857 break;
858 if (pte_chain->parent_ptes[i] != parent_pte)
859 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800860 while (i + 1 < NR_PTE_CHAIN_ENTRIES
861 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800862 pte_chain->parent_ptes[i]
863 = pte_chain->parent_ptes[i + 1];
864 ++i;
865 }
866 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800867 if (i == 0) {
868 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300869 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200870 if (hlist_empty(&sp->parent_ptes)) {
871 sp->multimapped = 0;
872 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800873 }
874 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 return;
876 }
877 BUG();
878}
879
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300880
881static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
882 mmu_parent_walk_fn fn)
883{
884 struct kvm_pte_chain *pte_chain;
885 struct hlist_node *node;
886 struct kvm_mmu_page *parent_sp;
887 int i;
888
889 if (!sp->multimapped && sp->parent_pte) {
890 parent_sp = page_header(__pa(sp->parent_pte));
891 fn(vcpu, parent_sp);
892 mmu_parent_walk(vcpu, parent_sp, fn);
893 return;
894 }
895 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
896 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
897 if (!pte_chain->parent_ptes[i])
898 break;
899 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
900 fn(vcpu, parent_sp);
901 mmu_parent_walk(vcpu, parent_sp, fn);
902 }
903}
904
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300905static void kvm_mmu_update_unsync_bitmap(u64 *spte)
906{
907 unsigned int index;
908 struct kvm_mmu_page *sp = page_header(__pa(spte));
909
910 index = spte - sp->spt;
911 __set_bit(index, sp->unsync_child_bitmap);
912 sp->unsync_children = 1;
913}
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{
939 sp->unsync_children = 1;
940 kvm_mmu_update_parents_unsync(sp);
941 return 1;
942}
943
944static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
945 struct kvm_mmu_page *sp)
946{
947 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
948 kvm_mmu_update_parents_unsync(sp);
949}
950
Avi Kivityd761a502008-05-29 14:55:03 +0300951static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
952 struct kvm_mmu_page *sp)
953{
954 int i;
955
956 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
957 sp->spt[i] = shadow_trap_nonpresent_pte;
958}
959
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300960static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
961 struct kvm_mmu_page *sp)
962{
963 return 1;
964}
965
Marcelo Tosattia7052892008-09-23 13:18:35 -0300966static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
967{
968}
969
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300970#define for_each_unsync_children(bitmap, idx) \
971 for (idx = find_first_bit(bitmap, 512); \
972 idx < 512; \
973 idx = find_next_bit(bitmap, 512, idx+1))
974
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300975static int mmu_unsync_walk(struct kvm_mmu_page *sp,
976 struct kvm_unsync_walk *walker)
977{
978 int i, ret;
979
980 if (!sp->unsync_children)
981 return 0;
982
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300983 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300984 u64 ent = sp->spt[i];
985
986 if (is_shadow_present_pte(ent)) {
987 struct kvm_mmu_page *child;
988 child = page_header(ent & PT64_BASE_ADDR_MASK);
989
990 if (child->unsync_children) {
991 ret = mmu_unsync_walk(child, walker);
992 if (ret)
993 return ret;
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300994 __clear_bit(i, sp->unsync_child_bitmap);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300995 }
996
997 if (child->unsync) {
998 ret = walker->entry(child, walker);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300999 __clear_bit(i, sp->unsync_child_bitmap);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001000 if (ret)
1001 return ret;
1002 }
1003 }
1004 }
1005
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001006 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001007 sp->unsync_children = 0;
1008
1009 return 0;
1010}
1011
Avi Kivity4db35312007-11-21 15:28:32 +02001012static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001013{
1014 unsigned index;
1015 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001016 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001017 struct hlist_node *node;
1018
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001019 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001020 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001021 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001022 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001023 if (sp->gfn == gfn && !sp->role.metaphysical
1024 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001025 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001026 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001027 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001028 }
1029 return NULL;
1030}
1031
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001032static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1033{
1034 WARN_ON(!sp->unsync);
1035 sp->unsync = 0;
1036 --kvm->stat.mmu_unsync;
1037}
1038
1039static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1040
1041static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1042{
1043 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1044 kvm_mmu_zap_page(vcpu->kvm, sp);
1045 return 1;
1046 }
1047
1048 rmap_write_protect(vcpu->kvm, sp->gfn);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001049 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001050 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1051 kvm_mmu_zap_page(vcpu->kvm, sp);
1052 return 1;
1053 }
1054
1055 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001056 return 0;
1057}
1058
1059struct sync_walker {
1060 struct kvm_vcpu *vcpu;
1061 struct kvm_unsync_walk walker;
1062};
1063
1064static int mmu_sync_fn(struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk)
1065{
1066 struct sync_walker *sync_walk = container_of(walk, struct sync_walker,
1067 walker);
1068 struct kvm_vcpu *vcpu = sync_walk->vcpu;
1069
1070 kvm_sync_page(vcpu, sp);
1071 return (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock));
1072}
1073
1074static void mmu_sync_children(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1075{
1076 struct sync_walker walker = {
1077 .walker = { .entry = mmu_sync_fn, },
1078 .vcpu = vcpu,
1079 };
1080
1081 while (mmu_unsync_walk(sp, &walker.walker))
1082 cond_resched_lock(&vcpu->kvm->mmu_lock);
1083}
1084
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001085static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1086 gfn_t gfn,
1087 gva_t gaddr,
1088 unsigned level,
1089 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001090 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001091 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001092{
1093 union kvm_mmu_page_role role;
1094 unsigned index;
1095 unsigned quadrant;
1096 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001097 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001098 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001099
1100 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001101 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001102 role.level = level;
1103 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001104 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001105 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001106 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1107 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1108 role.quadrant = quadrant;
1109 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001110 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001111 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001112 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001113 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001114 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1115 if (sp->gfn == gfn) {
1116 if (sp->unsync)
1117 if (kvm_sync_page(vcpu, sp))
1118 continue;
1119
1120 if (sp->role.word != role.word)
1121 continue;
1122
Avi Kivity4db35312007-11-21 15:28:32 +02001123 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124 if (sp->unsync_children) {
1125 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1126 kvm_mmu_mark_parents_unsync(vcpu, sp);
1127 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001128 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001129 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001130 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001131 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001132 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1133 if (!sp)
1134 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001135 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001136 sp->gfn = gfn;
1137 sp->role = role;
1138 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001139 if (!metaphysical) {
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001140 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001141 account_shadowed(vcpu->kvm, gfn);
1142 }
Avi Kivity131d8272008-05-29 14:56:28 +03001143 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1144 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1145 else
1146 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001147 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001148}
1149
Avi Kivity3d000db2008-08-22 19:24:38 +03001150static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001151 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +03001152{
1153 hpa_t shadow_addr;
1154 int level;
1155 int r;
1156 u64 *sptep;
1157 unsigned index;
1158
1159 shadow_addr = vcpu->arch.mmu.root_hpa;
1160 level = vcpu->arch.mmu.shadow_root_level;
1161 if (level == PT32E_ROOT_LEVEL) {
1162 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1163 shadow_addr &= PT64_BASE_ADDR_MASK;
1164 --level;
1165 }
1166
1167 while (level >= PT_PAGE_TABLE_LEVEL) {
1168 index = SHADOW_PT_INDEX(addr, level);
1169 sptep = ((u64 *)__va(shadow_addr)) + index;
1170 r = walker->entry(walker, vcpu, addr, sptep, level);
1171 if (r)
1172 return r;
1173 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1174 --level;
1175 }
1176 return 0;
1177}
1178
Avi Kivity90cb0522007-07-17 13:04:56 +03001179static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001180 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001181{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001182 unsigned i;
1183 u64 *pt;
1184 u64 ent;
1185
Avi Kivity4db35312007-11-21 15:28:32 +02001186 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001187
Avi Kivity4db35312007-11-21 15:28:32 +02001188 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001189 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001190 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001191 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001192 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001193 }
1194 return;
1195 }
1196
1197 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1198 ent = pt[i];
1199
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001200 if (is_shadow_present_pte(ent)) {
1201 if (!is_large_pte(ent)) {
1202 ent &= PT64_BASE_ADDR_MASK;
1203 mmu_page_remove_parent_pte(page_header(ent),
1204 &pt[i]);
1205 } else {
1206 --kvm->stat.lpages;
1207 rmap_remove(kvm, &pt[i]);
1208 }
1209 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001210 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001211 }
Avi Kivitya4360362007-01-05 16:36:45 -08001212}
1213
Avi Kivity4db35312007-11-21 15:28:32 +02001214static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001215{
Avi Kivity4db35312007-11-21 15:28:32 +02001216 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001217}
1218
Avi Kivity12b7d282007-09-23 14:10:49 +02001219static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1220{
1221 int i;
1222
1223 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1224 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001225 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001226}
1227
Avi Kivity31aa2b42008-07-11 17:59:46 +03001228static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001229{
1230 u64 *parent_pte;
1231
Avi Kivity4db35312007-11-21 15:28:32 +02001232 while (sp->multimapped || sp->parent_pte) {
1233 if (!sp->multimapped)
1234 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001235 else {
1236 struct kvm_pte_chain *chain;
1237
Avi Kivity4db35312007-11-21 15:28:32 +02001238 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001239 struct kvm_pte_chain, link);
1240 parent_pte = chain->parent_ptes[0];
1241 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001242 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001243 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001244 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001245 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001246}
1247
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248struct zap_walker {
1249 struct kvm_unsync_walk walker;
1250 struct kvm *kvm;
1251 int zapped;
1252};
1253
1254static int mmu_zap_fn(struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk)
1255{
1256 struct zap_walker *zap_walk = container_of(walk, struct zap_walker,
1257 walker);
1258 kvm_mmu_zap_page(zap_walk->kvm, sp);
1259 zap_walk->zapped = 1;
1260 return 0;
1261}
1262
1263static int mmu_zap_unsync_children(struct kvm *kvm, struct kvm_mmu_page *sp)
1264{
1265 struct zap_walker walker = {
1266 .walker = { .entry = mmu_zap_fn, },
1267 .kvm = kvm,
1268 .zapped = 0,
1269 };
1270
1271 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
1272 return 0;
1273 mmu_unsync_walk(sp, &walker.walker);
1274 return walker.zapped;
1275}
1276
Marcelo Tosatti07385412008-09-23 13:18:37 -03001277static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001278{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001279 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001280 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001281 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001282 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001283 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001284 kvm_flush_remote_tlbs(kvm);
1285 if (!sp->role.invalid && !sp->role.metaphysical)
1286 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001287 if (sp->unsync)
1288 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001289 if (!sp->root_count) {
1290 hlist_del(&sp->hash_link);
1291 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001292 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001293 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001294 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001295 kvm_reload_remote_mmus(kvm);
1296 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001297 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001298 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001299}
1300
Izik Eidus82ce2c92007-10-02 18:52:55 +02001301/*
1302 * Changing the number of mmu pages allocated to the vm
1303 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1304 */
1305void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1306{
1307 /*
1308 * If we set the number of mmu pages to be smaller be than the
1309 * number of actived pages , we must to free some mmu pages before we
1310 * change the value
1311 */
1312
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001313 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001314 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001315 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1316 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001317
1318 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1319 struct kvm_mmu_page *page;
1320
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001321 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001322 struct kvm_mmu_page, link);
1323 kvm_mmu_zap_page(kvm, page);
1324 n_used_mmu_pages--;
1325 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001326 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001327 }
1328 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001329 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1330 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001331
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001332 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001333}
1334
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001335static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001336{
1337 unsigned index;
1338 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001339 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001340 struct hlist_node *node, *n;
1341 int r;
1342
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001343 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001344 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001345 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001346 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001347 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1348 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001349 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001350 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001351 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001352 if (kvm_mmu_zap_page(kvm, sp))
1353 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001354 }
1355 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001356}
1357
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001358static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001359{
Avi Kivity4db35312007-11-21 15:28:32 +02001360 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001361
Avi Kivity4db35312007-11-21 15:28:32 +02001362 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001363 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001364 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001365 }
1366}
1367
Avi Kivity38c335f2007-11-21 14:20:22 +02001368static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369{
Avi Kivity38c335f2007-11-21 14:20:22 +02001370 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001371 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372
Sheng Yang291f26b2008-10-16 17:30:57 +08001373 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374}
1375
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001376static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1377{
1378 int i;
1379 u64 *pt = sp->spt;
1380
1381 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1382 return;
1383
1384 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1385 if (pt[i] == shadow_notrap_nonpresent_pte)
1386 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1387 }
1388}
1389
Avi Kivity039576c2007-03-20 12:46:50 +02001390struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1391{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001392 struct page *page;
1393
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001394 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001395
1396 if (gpa == UNMAPPED_GVA)
1397 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001398
Izik Eidus72dc67a2008-02-10 18:04:15 +02001399 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001400
1401 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001402}
1403
Sheng Yang74be52e2008-10-09 16:01:56 +08001404/*
1405 * The function is based on mtrr_type_lookup() in
1406 * arch/x86/kernel/cpu/mtrr/generic.c
1407 */
1408static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1409 u64 start, u64 end)
1410{
1411 int i;
1412 u64 base, mask;
1413 u8 prev_match, curr_match;
1414 int num_var_ranges = KVM_NR_VAR_MTRR;
1415
1416 if (!mtrr_state->enabled)
1417 return 0xFF;
1418
1419 /* Make end inclusive end, instead of exclusive */
1420 end--;
1421
1422 /* Look in fixed ranges. Just return the type as per start */
1423 if (mtrr_state->have_fixed && (start < 0x100000)) {
1424 int idx;
1425
1426 if (start < 0x80000) {
1427 idx = 0;
1428 idx += (start >> 16);
1429 return mtrr_state->fixed_ranges[idx];
1430 } else if (start < 0xC0000) {
1431 idx = 1 * 8;
1432 idx += ((start - 0x80000) >> 14);
1433 return mtrr_state->fixed_ranges[idx];
1434 } else if (start < 0x1000000) {
1435 idx = 3 * 8;
1436 idx += ((start - 0xC0000) >> 12);
1437 return mtrr_state->fixed_ranges[idx];
1438 }
1439 }
1440
1441 /*
1442 * Look in variable ranges
1443 * Look of multiple ranges matching this address and pick type
1444 * as per MTRR precedence
1445 */
1446 if (!(mtrr_state->enabled & 2))
1447 return mtrr_state->def_type;
1448
1449 prev_match = 0xFF;
1450 for (i = 0; i < num_var_ranges; ++i) {
1451 unsigned short start_state, end_state;
1452
1453 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1454 continue;
1455
1456 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1457 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1458 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1459 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1460
1461 start_state = ((start & mask) == (base & mask));
1462 end_state = ((end & mask) == (base & mask));
1463 if (start_state != end_state)
1464 return 0xFE;
1465
1466 if ((start & mask) != (base & mask))
1467 continue;
1468
1469 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1470 if (prev_match == 0xFF) {
1471 prev_match = curr_match;
1472 continue;
1473 }
1474
1475 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1476 curr_match == MTRR_TYPE_UNCACHABLE)
1477 return MTRR_TYPE_UNCACHABLE;
1478
1479 if ((prev_match == MTRR_TYPE_WRBACK &&
1480 curr_match == MTRR_TYPE_WRTHROUGH) ||
1481 (prev_match == MTRR_TYPE_WRTHROUGH &&
1482 curr_match == MTRR_TYPE_WRBACK)) {
1483 prev_match = MTRR_TYPE_WRTHROUGH;
1484 curr_match = MTRR_TYPE_WRTHROUGH;
1485 }
1486
1487 if (prev_match != curr_match)
1488 return MTRR_TYPE_UNCACHABLE;
1489 }
1490
1491 if (prev_match != 0xFF)
1492 return prev_match;
1493
1494 return mtrr_state->def_type;
1495}
1496
1497static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1498{
1499 u8 mtrr;
1500
1501 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1502 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1503 if (mtrr == 0xfe || mtrr == 0xff)
1504 mtrr = MTRR_TYPE_WRBACK;
1505 return mtrr;
1506}
1507
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001508static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1509{
1510 unsigned index;
1511 struct hlist_head *bucket;
1512 struct kvm_mmu_page *s;
1513 struct hlist_node *node, *n;
1514
1515 index = kvm_page_table_hashfn(sp->gfn);
1516 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1517 /* don't unsync if pagetable is shadowed with multiple roles */
1518 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1519 if (s->gfn != sp->gfn || s->role.metaphysical)
1520 continue;
1521 if (s->role.word != sp->role.word)
1522 return 1;
1523 }
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001524 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001525 ++vcpu->kvm->stat.mmu_unsync;
1526 sp->unsync = 1;
1527 mmu_convert_notrap(sp);
1528 return 0;
1529}
1530
1531static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1532 bool can_unsync)
1533{
1534 struct kvm_mmu_page *shadow;
1535
1536 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1537 if (shadow) {
1538 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1539 return 1;
1540 if (shadow->unsync)
1541 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001542 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001543 return kvm_unsync_page(vcpu, shadow);
1544 return 1;
1545 }
1546 return 0;
1547}
1548
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001549static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1550 unsigned pte_access, int user_fault,
1551 int write_fault, int dirty, int largepage,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001552 gfn_t gfn, pfn_t pfn, bool speculative,
1553 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001554{
1555 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001556 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001557 u64 mt_mask = shadow_mt_mask;
1558
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001559 /*
1560 * We don't set the accessed bit, since we sometimes want to see
1561 * whether the guest actually used the pte (in order to detect
1562 * demand paging).
1563 */
Sheng Yang7b523452008-04-25 21:13:50 +08001564 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001565 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001566 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001567 if (!dirty)
1568 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001569 if (pte_access & ACC_EXEC_MASK)
1570 spte |= shadow_x_mask;
1571 else
1572 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001573 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001574 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001575 if (largepage)
1576 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001577 if (mt_mask) {
1578 mt_mask = get_memory_type(vcpu, gfn) <<
1579 kvm_x86_ops->get_mt_mask_shift();
1580 spte |= mt_mask;
1581 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001582
Anthony Liguori35149e22008-04-02 14:46:56 -05001583 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001584
1585 if ((pte_access & ACC_WRITE_MASK)
1586 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001587
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001588 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1589 ret = 1;
1590 spte = shadow_trap_nonpresent_pte;
1591 goto set_pte;
1592 }
1593
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001594 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001595
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001596 /*
1597 * Optimization: for pte sync, if spte was writable the hash
1598 * lookup is unnecessary (and expensive). Write protection
1599 * is responsibility of mmu_get_page / kvm_sync_page.
1600 * Same reasoning can be applied to dirty page accounting.
1601 */
1602 if (!can_unsync && is_writeble_pte(*shadow_pte))
1603 goto set_pte;
1604
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001605 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001606 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001607 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001608 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001609 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001610 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001611 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001612 }
1613 }
1614
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001615 if (pte_access & ACC_WRITE_MASK)
1616 mark_page_dirty(vcpu->kvm, gfn);
1617
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001618set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001619 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001620 return ret;
1621}
1622
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001623static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1624 unsigned pt_access, unsigned pte_access,
1625 int user_fault, int write_fault, int dirty,
1626 int *ptwrite, int largepage, gfn_t gfn,
1627 pfn_t pfn, bool speculative)
1628{
1629 int was_rmapped = 0;
1630 int was_writeble = is_writeble_pte(*shadow_pte);
1631
1632 pgprintk("%s: spte %llx access %x write_fault %d"
1633 " user_fault %d gfn %lx\n",
1634 __func__, *shadow_pte, pt_access,
1635 write_fault, user_fault, gfn);
1636
1637 if (is_rmap_pte(*shadow_pte)) {
1638 /*
1639 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1640 * the parent of the now unreachable PTE.
1641 */
1642 if (largepage && !is_large_pte(*shadow_pte)) {
1643 struct kvm_mmu_page *child;
1644 u64 pte = *shadow_pte;
1645
1646 child = page_header(pte & PT64_BASE_ADDR_MASK);
1647 mmu_page_remove_parent_pte(child, shadow_pte);
1648 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1649 pgprintk("hfn old %lx new %lx\n",
1650 spte_to_pfn(*shadow_pte), pfn);
1651 rmap_remove(vcpu->kvm, shadow_pte);
1652 } else {
1653 if (largepage)
1654 was_rmapped = is_large_pte(*shadow_pte);
1655 else
1656 was_rmapped = 1;
1657 }
1658 }
1659 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001660 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001661 if (write_fault)
1662 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001663 kvm_x86_ops->tlb_flush(vcpu);
1664 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001665
1666 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1667 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1668 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1669 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1670 *shadow_pte, shadow_pte);
1671 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001672 ++vcpu->kvm->stat.lpages;
1673
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001674 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1675 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001676 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001677 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001678 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001679 } else {
1680 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001681 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001682 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001683 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001684 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001685 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001686 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001687 vcpu->arch.last_pte_gfn = gfn;
1688 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001689}
1690
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1692{
1693}
1694
Avi Kivity140754b2008-08-22 19:28:04 +03001695struct direct_shadow_walk {
1696 struct kvm_shadow_walk walker;
1697 pfn_t pfn;
1698 int write;
1699 int largepage;
1700 int pt_write;
1701};
1702
1703static int direct_map_entry(struct kvm_shadow_walk *_walk,
1704 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001705 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001706{
1707 struct direct_shadow_walk *walk =
1708 container_of(_walk, struct direct_shadow_walk, walker);
1709 struct kvm_mmu_page *sp;
1710 gfn_t pseudo_gfn;
1711 gfn_t gfn = addr >> PAGE_SHIFT;
1712
1713 if (level == PT_PAGE_TABLE_LEVEL
1714 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1715 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1716 0, walk->write, 1, &walk->pt_write,
1717 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001718 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001719 return 1;
1720 }
1721
1722 if (*sptep == shadow_trap_nonpresent_pte) {
1723 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001724 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001725 1, ACC_ALL, sptep);
1726 if (!sp) {
1727 pgprintk("nonpaging_map: ENOMEM\n");
1728 kvm_release_pfn_clean(walk->pfn);
1729 return -ENOMEM;
1730 }
1731
1732 set_shadow_pte(sptep,
1733 __pa(sp->spt)
1734 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1735 | shadow_user_mask | shadow_x_mask);
1736 }
1737 return 0;
1738}
1739
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001740static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001741 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742{
Avi Kivity140754b2008-08-22 19:28:04 +03001743 int r;
1744 struct direct_shadow_walk walker = {
1745 .walker = { .entry = direct_map_entry, },
1746 .pfn = pfn,
1747 .largepage = largepage,
1748 .write = write,
1749 .pt_write = 0,
1750 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001752 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001753 if (r < 0)
1754 return r;
1755 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756}
1757
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001758static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1759{
1760 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001761 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001762 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001763 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001764
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001765 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1766 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1767 largepage = 1;
1768 }
1769
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001770 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001771 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001772 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001773
Avi Kivityd196e342008-01-24 11:44:11 +02001774 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001775 if (is_error_pfn(pfn)) {
1776 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001777 return 1;
1778 }
1779
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001780 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001781 if (mmu_notifier_retry(vcpu, mmu_seq))
1782 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001783 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001784 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001785 spin_unlock(&vcpu->kvm->mmu_lock);
1786
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001787
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001788 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001789
1790out_unlock:
1791 spin_unlock(&vcpu->kvm->mmu_lock);
1792 kvm_release_pfn_clean(pfn);
1793 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001794}
1795
1796
Avi Kivity17ac10a2007-01-05 16:36:40 -08001797static void mmu_free_roots(struct kvm_vcpu *vcpu)
1798{
1799 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001800 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001801
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001802 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001803 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001804 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001805 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1806 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001807
Avi Kivity4db35312007-11-21 15:28:32 +02001808 sp = page_header(root);
1809 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001810 if (!sp->root_count && sp->role.invalid)
1811 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001812 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001813 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001814 return;
1815 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001816 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001817 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001818
Avi Kivity417726a2007-04-12 17:35:58 +03001819 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001820 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001821 sp = page_header(root);
1822 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001823 if (!sp->root_count && sp->role.invalid)
1824 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001825 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001826 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001827 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001828 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001829 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001830}
1831
1832static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1833{
1834 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001835 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001836 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001837 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001838
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001839 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001840
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001841 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1842 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001843
1844 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001845 if (tdp_enabled)
1846 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001847 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001848 PT64_ROOT_LEVEL, metaphysical,
1849 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001850 root = __pa(sp->spt);
1851 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001852 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001853 return;
1854 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001855 metaphysical = !is_paging(vcpu);
1856 if (tdp_enabled)
1857 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001858 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001859 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001860
1861 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001862 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1863 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1864 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001865 continue;
1866 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001867 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1868 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001869 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001870 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001871 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001872 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001873 root = __pa(sp->spt);
1874 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001875 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001876 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001877 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001878}
1879
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001880static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1881{
1882 int i;
1883 struct kvm_mmu_page *sp;
1884
1885 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1886 return;
1887 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1888 hpa_t root = vcpu->arch.mmu.root_hpa;
1889 sp = page_header(root);
1890 mmu_sync_children(vcpu, sp);
1891 return;
1892 }
1893 for (i = 0; i < 4; ++i) {
1894 hpa_t root = vcpu->arch.mmu.pae_root[i];
1895
1896 if (root) {
1897 root &= PT64_BASE_ADDR_MASK;
1898 sp = page_header(root);
1899 mmu_sync_children(vcpu, sp);
1900 }
1901 }
1902}
1903
1904void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1905{
1906 spin_lock(&vcpu->kvm->mmu_lock);
1907 mmu_sync_roots(vcpu);
1908 spin_unlock(&vcpu->kvm->mmu_lock);
1909}
1910
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1912{
1913 return vaddr;
1914}
1915
1916static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001917 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918{
Avi Kivitye8332402007-12-09 18:43:00 +02001919 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001920 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001922 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001923 r = mmu_topup_memory_caches(vcpu);
1924 if (r)
1925 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001926
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001928 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929
Avi Kivitye8332402007-12-09 18:43:00 +02001930 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931
Avi Kivitye8332402007-12-09 18:43:00 +02001932 return nonpaging_map(vcpu, gva & PAGE_MASK,
1933 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934}
1935
Joerg Roedelfb72d162008-02-07 13:47:44 +01001936static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1937 u32 error_code)
1938{
Anthony Liguori35149e22008-04-02 14:46:56 -05001939 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001940 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001941 int largepage = 0;
1942 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001943 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001944
1945 ASSERT(vcpu);
1946 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1947
1948 r = mmu_topup_memory_caches(vcpu);
1949 if (r)
1950 return r;
1951
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001952 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1953 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1954 largepage = 1;
1955 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001956 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001957 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001958 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05001959 if (is_error_pfn(pfn)) {
1960 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001961 return 1;
1962 }
1963 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001964 if (mmu_notifier_retry(vcpu, mmu_seq))
1965 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001966 kvm_mmu_free_some_pages(vcpu);
1967 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03001968 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001969 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001970
1971 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001972
1973out_unlock:
1974 spin_unlock(&vcpu->kvm->mmu_lock);
1975 kvm_release_pfn_clean(pfn);
1976 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001977}
1978
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979static void nonpaging_free(struct kvm_vcpu *vcpu)
1980{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001981 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982}
1983
1984static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1985{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001986 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987
1988 context->new_cr3 = nonpaging_new_cr3;
1989 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 context->gva_to_gpa = nonpaging_gva_to_gpa;
1991 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001992 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001993 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001994 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001995 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001997 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998 return 0;
1999}
2000
Avi Kivityd835dfe2007-11-21 02:57:59 +02002001void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002003 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002004 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005}
2006
2007static void paging_new_cr3(struct kvm_vcpu *vcpu)
2008{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002009 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002010 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011}
2012
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013static void inject_page_fault(struct kvm_vcpu *vcpu,
2014 u64 addr,
2015 u32 err_code)
2016{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002017 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018}
2019
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020static void paging_free(struct kvm_vcpu *vcpu)
2021{
2022 nonpaging_free(vcpu);
2023}
2024
2025#define PTTYPE 64
2026#include "paging_tmpl.h"
2027#undef PTTYPE
2028
2029#define PTTYPE 32
2030#include "paging_tmpl.h"
2031#undef PTTYPE
2032
Avi Kivity17ac10a2007-01-05 16:36:40 -08002033static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002035 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036
2037 ASSERT(is_pae(vcpu));
2038 context->new_cr3 = paging_new_cr3;
2039 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002041 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002042 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002043 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002045 context->root_level = level;
2046 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002047 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 return 0;
2049}
2050
Avi Kivity17ac10a2007-01-05 16:36:40 -08002051static int paging64_init_context(struct kvm_vcpu *vcpu)
2052{
2053 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2054}
2055
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056static int paging32_init_context(struct kvm_vcpu *vcpu)
2057{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002058 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059
2060 context->new_cr3 = paging_new_cr3;
2061 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062 context->gva_to_gpa = paging32_gva_to_gpa;
2063 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002064 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002065 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002066 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067 context->root_level = PT32_ROOT_LEVEL;
2068 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002069 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070 return 0;
2071}
2072
2073static int paging32E_init_context(struct kvm_vcpu *vcpu)
2074{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002075 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076}
2077
Joerg Roedelfb72d162008-02-07 13:47:44 +01002078static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2079{
2080 struct kvm_mmu *context = &vcpu->arch.mmu;
2081
2082 context->new_cr3 = nonpaging_new_cr3;
2083 context->page_fault = tdp_page_fault;
2084 context->free = nonpaging_free;
2085 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002086 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002087 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002088 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002089 context->root_hpa = INVALID_PAGE;
2090
2091 if (!is_paging(vcpu)) {
2092 context->gva_to_gpa = nonpaging_gva_to_gpa;
2093 context->root_level = 0;
2094 } else if (is_long_mode(vcpu)) {
2095 context->gva_to_gpa = paging64_gva_to_gpa;
2096 context->root_level = PT64_ROOT_LEVEL;
2097 } else if (is_pae(vcpu)) {
2098 context->gva_to_gpa = paging64_gva_to_gpa;
2099 context->root_level = PT32E_ROOT_LEVEL;
2100 } else {
2101 context->gva_to_gpa = paging32_gva_to_gpa;
2102 context->root_level = PT32_ROOT_LEVEL;
2103 }
2104
2105 return 0;
2106}
2107
2108static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109{
2110 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002111 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112
2113 if (!is_paging(vcpu))
2114 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002115 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 return paging64_init_context(vcpu);
2117 else if (is_pae(vcpu))
2118 return paging32E_init_context(vcpu);
2119 else
2120 return paging32_init_context(vcpu);
2121}
2122
Joerg Roedelfb72d162008-02-07 13:47:44 +01002123static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2124{
Anthony Liguori35149e22008-04-02 14:46:56 -05002125 vcpu->arch.update_pte.pfn = bad_pfn;
2126
Joerg Roedelfb72d162008-02-07 13:47:44 +01002127 if (tdp_enabled)
2128 return init_kvm_tdp_mmu(vcpu);
2129 else
2130 return init_kvm_softmmu(vcpu);
2131}
2132
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2134{
2135 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002136 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2137 vcpu->arch.mmu.free(vcpu);
2138 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 }
2140}
2141
2142int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2143{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002144 destroy_kvm_mmu(vcpu);
2145 return init_kvm_mmu(vcpu);
2146}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002147EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002148
2149int kvm_mmu_load(struct kvm_vcpu *vcpu)
2150{
Avi Kivity714b93d2007-01-05 16:36:53 -08002151 int r;
2152
Avi Kivitye2dec932007-01-05 16:36:54 -08002153 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002154 if (r)
2155 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002156 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002157 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002158 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002159 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002160 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002161 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002162 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002163out:
2164 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002166EXPORT_SYMBOL_GPL(kvm_mmu_load);
2167
2168void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2169{
2170 mmu_free_roots(vcpu);
2171}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172
Avi Kivity09072da2007-05-01 14:16:52 +03002173static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002174 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002175 u64 *spte)
2176{
2177 u64 pte;
2178 struct kvm_mmu_page *child;
2179
2180 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002181 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002182 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2183 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002184 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002185 else {
2186 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002187 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002188 }
2189 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002190 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002191 if (is_large_pte(pte))
2192 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002193}
2194
Avi Kivity00284252007-05-01 16:53:31 +03002195static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002196 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002197 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002198 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002199{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002200 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2201 if (!vcpu->arch.update_pte.largepage ||
2202 sp->role.glevels == PT32_ROOT_LEVEL) {
2203 ++vcpu->kvm->stat.mmu_pde_zapped;
2204 return;
2205 }
2206 }
Avi Kivity00284252007-05-01 16:53:31 +03002207
Avi Kivity4cee5762007-11-18 16:37:07 +02002208 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002209 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002210 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002211 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002212 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002213}
2214
Avi Kivity79539ce2007-11-21 02:06:21 +02002215static bool need_remote_flush(u64 old, u64 new)
2216{
2217 if (!is_shadow_present_pte(old))
2218 return false;
2219 if (!is_shadow_present_pte(new))
2220 return true;
2221 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2222 return true;
2223 old ^= PT64_NX_MASK;
2224 new ^= PT64_NX_MASK;
2225 return (old & ~new & PT64_PERM_MASK) != 0;
2226}
2227
2228static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2229{
2230 if (need_remote_flush(old, new))
2231 kvm_flush_remote_tlbs(vcpu->kvm);
2232 else
2233 kvm_mmu_flush_tlb(vcpu);
2234}
2235
Avi Kivity12b7d282007-09-23 14:10:49 +02002236static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2237{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002239
Sheng Yang7b523452008-04-25 21:13:50 +08002240 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002241}
2242
Avi Kivityd7824ff2007-12-30 12:29:05 +02002243static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2244 const u8 *new, int bytes)
2245{
2246 gfn_t gfn;
2247 int r;
2248 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002249 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002250
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002251 vcpu->arch.update_pte.largepage = 0;
2252
Avi Kivityd7824ff2007-12-30 12:29:05 +02002253 if (bytes != 4 && bytes != 8)
2254 return;
2255
2256 /*
2257 * Assume that the pte write on a page table of the same type
2258 * as the current vcpu paging mode. This is nearly always true
2259 * (might be false while changing modes). Note it is verified later
2260 * by update_pte().
2261 */
2262 if (is_pae(vcpu)) {
2263 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2264 if ((bytes == 4) && (gpa % 4 == 0)) {
2265 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2266 if (r)
2267 return;
2268 memcpy((void *)&gpte + (gpa % 8), new, 4);
2269 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2270 memcpy((void *)&gpte, new, 8);
2271 }
2272 } else {
2273 if ((bytes == 4) && (gpa % 4 == 0))
2274 memcpy((void *)&gpte, new, 4);
2275 }
2276 if (!is_present_pte(gpte))
2277 return;
2278 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002279
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002280 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2281 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2282 vcpu->arch.update_pte.largepage = 1;
2283 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002284 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002285 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002286 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002287
Anthony Liguori35149e22008-04-02 14:46:56 -05002288 if (is_error_pfn(pfn)) {
2289 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002290 return;
2291 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002292 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002293 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002294}
2295
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002296static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2297{
2298 u64 *spte = vcpu->arch.last_pte_updated;
2299
2300 if (spte
2301 && vcpu->arch.last_pte_gfn == gfn
2302 && shadow_accessed_mask
2303 && !(*spte & shadow_accessed_mask)
2304 && is_shadow_present_pte(*spte))
2305 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2306}
2307
Avi Kivity09072da2007-05-01 14:16:52 +03002308void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08002309 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002310{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002311 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002312 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002313 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002314 struct hlist_head *bucket;
2315 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002316 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002317 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002318 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002319 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002320 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002321 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002322 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002323 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002324 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002325 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002326 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002327
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002328 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002329 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002330 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002331 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002332 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002333 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002334 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002335 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02002336 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002337 ++vcpu->arch.last_pt_write_count;
2338 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08002339 flooded = 1;
2340 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002341 vcpu->arch.last_pt_write_gfn = gfn;
2342 vcpu->arch.last_pt_write_count = 1;
2343 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002344 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002345 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002346 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002347 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002348 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002349 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002350 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002351 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002352 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002353 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002354 /*
2355 * Misaligned accesses are too much trouble to fix
2356 * up; also, they usually indicate a page is not used
2357 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002358 *
2359 * If we're seeing too many writes to a page,
2360 * it may no longer be a page table, or we may be
2361 * forking, in which case it is better to unmap the
2362 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002363 */
2364 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002365 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002366 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2367 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002368 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002369 continue;
2370 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002371 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002372 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002373 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002374 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002375 page_offset <<= 1; /* 32->64 */
2376 /*
2377 * A 32-bit pde maps 4MB while the shadow pdes map
2378 * only 2MB. So we need to double the offset again
2379 * and zap two pdes instead of one.
2380 */
2381 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002382 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002383 page_offset <<= 1;
2384 npte = 2;
2385 }
Avi Kivityfce06572007-05-01 16:44:05 +03002386 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002387 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002388 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002389 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002390 }
Avi Kivity4db35312007-11-21 15:28:32 +02002391 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002392 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2393 gentry = 0;
2394 r = kvm_read_guest_atomic(vcpu->kvm,
2395 gpa & ~(u64)(pte_size - 1),
2396 &gentry, pte_size);
2397 new = (const void *)&gentry;
2398 if (r < 0)
2399 new = NULL;
2400 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002401 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002402 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002403 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002404 if (new)
2405 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002406 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002407 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002408 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002409 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002410 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002411 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002412 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2413 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2414 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002415 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002416}
2417
Avi Kivitya4360362007-01-05 16:36:45 -08002418int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2419{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002420 gpa_t gpa;
2421 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002422
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002423 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002424
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002425 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002426 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002427 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002428 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002429}
Avi Kivity577bdc42008-07-19 08:57:05 +03002430EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002431
Avi Kivity22d95b12007-09-14 20:26:06 +03002432void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002433{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002434 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002435 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002436
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002437 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002438 struct kvm_mmu_page, link);
2439 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002440 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002441 }
2442}
Avi Kivityebeace82007-01-05 16:36:47 -08002443
Avi Kivity30677142007-10-28 18:48:59 +02002444int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2445{
2446 int r;
2447 enum emulation_result er;
2448
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002449 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002450 if (r < 0)
2451 goto out;
2452
2453 if (!r) {
2454 r = 1;
2455 goto out;
2456 }
2457
Avi Kivityb733bfb2007-10-28 18:52:05 +02002458 r = mmu_topup_memory_caches(vcpu);
2459 if (r)
2460 goto out;
2461
Avi Kivity30677142007-10-28 18:48:59 +02002462 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002463
2464 switch (er) {
2465 case EMULATE_DONE:
2466 return 1;
2467 case EMULATE_DO_MMIO:
2468 ++vcpu->stat.mmio_exits;
2469 return 0;
2470 case EMULATE_FAIL:
2471 kvm_report_emulation_failure(vcpu, "pagetable");
2472 return 1;
2473 default:
2474 BUG();
2475 }
2476out:
Avi Kivity30677142007-10-28 18:48:59 +02002477 return r;
2478}
2479EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2480
Marcelo Tosattia7052892008-09-23 13:18:35 -03002481void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2482{
2483 spin_lock(&vcpu->kvm->mmu_lock);
2484 vcpu->arch.mmu.invlpg(vcpu, gva);
2485 spin_unlock(&vcpu->kvm->mmu_lock);
2486 kvm_mmu_flush_tlb(vcpu);
2487 ++vcpu->stat.invlpg;
2488}
2489EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2490
Joerg Roedel18552672008-02-07 13:47:41 +01002491void kvm_enable_tdp(void)
2492{
2493 tdp_enabled = true;
2494}
2495EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2496
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002497void kvm_disable_tdp(void)
2498{
2499 tdp_enabled = false;
2500}
2501EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2502
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503static void free_mmu_pages(struct kvm_vcpu *vcpu)
2504{
Avi Kivity4db35312007-11-21 15:28:32 +02002505 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002507 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2508 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002509 struct kvm_mmu_page, link);
2510 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002511 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002512 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002513 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514}
2515
2516static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2517{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002518 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 int i;
2520
2521 ASSERT(vcpu);
2522
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002523 if (vcpu->kvm->arch.n_requested_mmu_pages)
2524 vcpu->kvm->arch.n_free_mmu_pages =
2525 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002526 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002527 vcpu->kvm->arch.n_free_mmu_pages =
2528 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002529 /*
2530 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2531 * Therefore we need to allocate shadow page tables in the first
2532 * 4GB of memory, which happens to fit the DMA32 zone.
2533 */
2534 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2535 if (!page)
2536 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002537 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002538 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002539 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002540
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 return 0;
2542
2543error_1:
2544 free_mmu_pages(vcpu);
2545 return -ENOMEM;
2546}
2547
Ingo Molnar8018c272006-12-29 16:50:01 -08002548int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002551 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552
Ingo Molnar8018c272006-12-29 16:50:01 -08002553 return alloc_mmu_pages(vcpu);
2554}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555
Ingo Molnar8018c272006-12-29 16:50:01 -08002556int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2557{
2558 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002559 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002560
Ingo Molnar8018c272006-12-29 16:50:01 -08002561 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562}
2563
2564void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2565{
2566 ASSERT(vcpu);
2567
2568 destroy_kvm_mmu(vcpu);
2569 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002570 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571}
2572
Avi Kivity90cb0522007-07-17 13:04:56 +03002573void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574{
Avi Kivity4db35312007-11-21 15:28:32 +02002575 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576
Avi Kivity2245a282008-08-27 16:32:24 +03002577 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002578 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579 int i;
2580 u64 *pt;
2581
Sheng Yang291f26b2008-10-16 17:30:57 +08002582 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 continue;
2584
Avi Kivity4db35312007-11-21 15:28:32 +02002585 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2587 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002588 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 }
Avi Kivity171d5952008-08-27 16:40:51 +03002591 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002592 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002594
Avi Kivity90cb0522007-07-17 13:04:56 +03002595void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002596{
Avi Kivity4db35312007-11-21 15:28:32 +02002597 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002598
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002599 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002600 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002601 if (kvm_mmu_zap_page(kvm, sp))
2602 node = container_of(kvm->arch.active_mmu_pages.next,
2603 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002604 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002605
Avi Kivity90cb0522007-07-17 13:04:56 +03002606 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002607}
2608
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002609static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002610{
2611 struct kvm_mmu_page *page;
2612
2613 page = container_of(kvm->arch.active_mmu_pages.prev,
2614 struct kvm_mmu_page, link);
2615 kvm_mmu_zap_page(kvm, page);
2616}
2617
2618static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2619{
2620 struct kvm *kvm;
2621 struct kvm *kvm_freed = NULL;
2622 int cache_count = 0;
2623
2624 spin_lock(&kvm_lock);
2625
2626 list_for_each_entry(kvm, &vm_list, vm_list) {
2627 int npages;
2628
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002629 if (!down_read_trylock(&kvm->slots_lock))
2630 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002631 spin_lock(&kvm->mmu_lock);
2632 npages = kvm->arch.n_alloc_mmu_pages -
2633 kvm->arch.n_free_mmu_pages;
2634 cache_count += npages;
2635 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2636 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2637 cache_count--;
2638 kvm_freed = kvm;
2639 }
2640 nr_to_scan--;
2641
2642 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002643 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002644 }
2645 if (kvm_freed)
2646 list_move_tail(&kvm_freed->vm_list, &vm_list);
2647
2648 spin_unlock(&kvm_lock);
2649
2650 return cache_count;
2651}
2652
2653static struct shrinker mmu_shrinker = {
2654 .shrink = mmu_shrink,
2655 .seeks = DEFAULT_SEEKS * 10,
2656};
2657
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002658static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002659{
2660 if (pte_chain_cache)
2661 kmem_cache_destroy(pte_chain_cache);
2662 if (rmap_desc_cache)
2663 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002664 if (mmu_page_header_cache)
2665 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002666}
2667
Izik Eidus3ee16c82008-03-30 15:17:21 +03002668void kvm_mmu_module_exit(void)
2669{
2670 mmu_destroy_caches();
2671 unregister_shrinker(&mmu_shrinker);
2672}
2673
Avi Kivityb5a33a72007-04-15 16:31:09 +03002674int kvm_mmu_module_init(void)
2675{
2676 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2677 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002678 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002679 if (!pte_chain_cache)
2680 goto nomem;
2681 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2682 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002683 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002684 if (!rmap_desc_cache)
2685 goto nomem;
2686
Avi Kivityd3d25b02007-05-30 12:34:53 +03002687 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2688 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002689 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002690 if (!mmu_page_header_cache)
2691 goto nomem;
2692
Izik Eidus3ee16c82008-03-30 15:17:21 +03002693 register_shrinker(&mmu_shrinker);
2694
Avi Kivityb5a33a72007-04-15 16:31:09 +03002695 return 0;
2696
2697nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002698 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002699 return -ENOMEM;
2700}
2701
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002702/*
2703 * Caculate mmu pages needed for kvm.
2704 */
2705unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2706{
2707 int i;
2708 unsigned int nr_mmu_pages;
2709 unsigned int nr_pages = 0;
2710
2711 for (i = 0; i < kvm->nmemslots; i++)
2712 nr_pages += kvm->memslots[i].npages;
2713
2714 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2715 nr_mmu_pages = max(nr_mmu_pages,
2716 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2717
2718 return nr_mmu_pages;
2719}
2720
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002721static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2722 unsigned len)
2723{
2724 if (len > buffer->len)
2725 return NULL;
2726 return buffer->ptr;
2727}
2728
2729static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2730 unsigned len)
2731{
2732 void *ret;
2733
2734 ret = pv_mmu_peek_buffer(buffer, len);
2735 if (!ret)
2736 return ret;
2737 buffer->ptr += len;
2738 buffer->len -= len;
2739 buffer->processed += len;
2740 return ret;
2741}
2742
2743static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2744 gpa_t addr, gpa_t value)
2745{
2746 int bytes = 8;
2747 int r;
2748
2749 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2750 bytes = 4;
2751
2752 r = mmu_topup_memory_caches(vcpu);
2753 if (r)
2754 return r;
2755
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002756 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002757 return -EFAULT;
2758
2759 return 1;
2760}
2761
2762static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2763{
2764 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002765 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002766 return 1;
2767}
2768
2769static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2770{
2771 spin_lock(&vcpu->kvm->mmu_lock);
2772 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2773 spin_unlock(&vcpu->kvm->mmu_lock);
2774 return 1;
2775}
2776
2777static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2778 struct kvm_pv_mmu_op_buffer *buffer)
2779{
2780 struct kvm_mmu_op_header *header;
2781
2782 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2783 if (!header)
2784 return 0;
2785 switch (header->op) {
2786 case KVM_MMU_OP_WRITE_PTE: {
2787 struct kvm_mmu_op_write_pte *wpte;
2788
2789 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2790 if (!wpte)
2791 return 0;
2792 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2793 wpte->pte_val);
2794 }
2795 case KVM_MMU_OP_FLUSH_TLB: {
2796 struct kvm_mmu_op_flush_tlb *ftlb;
2797
2798 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2799 if (!ftlb)
2800 return 0;
2801 return kvm_pv_mmu_flush_tlb(vcpu);
2802 }
2803 case KVM_MMU_OP_RELEASE_PT: {
2804 struct kvm_mmu_op_release_pt *rpt;
2805
2806 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2807 if (!rpt)
2808 return 0;
2809 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2810 }
2811 default: return 0;
2812 }
2813}
2814
2815int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2816 gpa_t addr, unsigned long *ret)
2817{
2818 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002819 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002820
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002821 buffer->ptr = buffer->buf;
2822 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2823 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002824
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002825 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002826 if (r)
2827 goto out;
2828
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002829 while (buffer->len) {
2830 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002831 if (r < 0)
2832 goto out;
2833 if (r == 0)
2834 break;
2835 }
2836
2837 r = 1;
2838out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002839 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002840 return r;
2841}
2842
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002843#ifdef AUDIT
2844
2845static const char *audit_msg;
2846
2847static gva_t canonicalize(gva_t gva)
2848{
2849#ifdef CONFIG_X86_64
2850 gva = (long long)(gva << 16) >> 16;
2851#endif
2852 return gva;
2853}
2854
2855static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2856 gva_t va, int level)
2857{
2858 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2859 int i;
2860 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2861
2862 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2863 u64 ent = pt[i];
2864
Avi Kivityc7addb92007-09-16 18:58:32 +02002865 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002866 continue;
2867
2868 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002869 if (level > 1) {
2870 if (ent == shadow_notrap_nonpresent_pte)
2871 printk(KERN_ERR "audit: (%s) nontrapping pte"
2872 " in nonleaf level: levels %d gva %lx"
2873 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002874 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002875
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002876 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002877 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002878 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002879 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002880
Avi Kivityc7addb92007-09-16 18:58:32 +02002881 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002882 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002883 printk(KERN_ERR "xx audit error: (%s) levels %d"
2884 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002885 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002886 va, gpa, hpa, ent,
2887 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002888 else if (ent == shadow_notrap_nonpresent_pte
2889 && !is_error_hpa(hpa))
2890 printk(KERN_ERR "audit: (%s) notrap shadow,"
2891 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002892 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002893
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002894 }
2895 }
2896}
2897
2898static void audit_mappings(struct kvm_vcpu *vcpu)
2899{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002900 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002901
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002902 if (vcpu->arch.mmu.root_level == 4)
2903 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002904 else
2905 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002906 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002907 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002908 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002909 i << 30,
2910 2);
2911}
2912
2913static int count_rmaps(struct kvm_vcpu *vcpu)
2914{
2915 int nmaps = 0;
2916 int i, j, k;
2917
2918 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2919 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2920 struct kvm_rmap_desc *d;
2921
2922 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002923 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002924
Izik Eidus290fc382007-09-27 14:11:22 +02002925 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002926 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002927 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002928 ++nmaps;
2929 continue;
2930 }
Izik Eidus290fc382007-09-27 14:11:22 +02002931 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002932 while (d) {
2933 for (k = 0; k < RMAP_EXT; ++k)
2934 if (d->shadow_ptes[k])
2935 ++nmaps;
2936 else
2937 break;
2938 d = d->more;
2939 }
2940 }
2941 }
2942 return nmaps;
2943}
2944
2945static int count_writable_mappings(struct kvm_vcpu *vcpu)
2946{
2947 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002948 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002949 int i;
2950
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002951 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002952 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002953
Avi Kivity4db35312007-11-21 15:28:32 +02002954 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002955 continue;
2956
2957 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2958 u64 ent = pt[i];
2959
2960 if (!(ent & PT_PRESENT_MASK))
2961 continue;
2962 if (!(ent & PT_WRITABLE_MASK))
2963 continue;
2964 ++nmaps;
2965 }
2966 }
2967 return nmaps;
2968}
2969
2970static void audit_rmap(struct kvm_vcpu *vcpu)
2971{
2972 int n_rmap = count_rmaps(vcpu);
2973 int n_actual = count_writable_mappings(vcpu);
2974
2975 if (n_rmap != n_actual)
2976 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002977 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002978}
2979
2980static void audit_write_protection(struct kvm_vcpu *vcpu)
2981{
Avi Kivity4db35312007-11-21 15:28:32 +02002982 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002983 struct kvm_memory_slot *slot;
2984 unsigned long *rmapp;
2985 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002986
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002987 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002988 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002989 continue;
2990
Avi Kivity4db35312007-11-21 15:28:32 +02002991 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03002992 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002993 rmapp = &slot->rmap[gfn - slot->base_gfn];
2994 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002995 printk(KERN_ERR "%s: (%s) shadow page has writable"
2996 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002997 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002998 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002999 }
3000}
3001
3002static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3003{
3004 int olddbg = dbg;
3005
3006 dbg = 0;
3007 audit_msg = msg;
3008 audit_rmap(vcpu);
3009 audit_write_protection(vcpu);
3010 audit_mappings(vcpu);
3011 dbg = olddbg;
3012}
3013
3014#endif