blob: 8687758b52956c456259227170b889c3a15fbbcb [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
20#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
33#include <asm/page.h>
34#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020035#include <asm/io.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
389 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
390 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300391}
392
393static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
394{
395 int *write_count;
396
397 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
398 *write_count -= 1;
399 WARN_ON(*write_count < 0);
400}
401
402static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
403{
404 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
405 int *largepage_idx;
406
407 if (slot) {
408 largepage_idx = slot_largepage_idx(gfn, slot);
409 return *largepage_idx;
410 }
411
412 return 1;
413}
414
415static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
416{
417 struct vm_area_struct *vma;
418 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300419 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300420
421 addr = gfn_to_hva(kvm, gfn);
422 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300423 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300424
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300425 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300426 vma = find_vma(current->mm, addr);
427 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300428 ret = 1;
429 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300430
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300431 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300432}
433
434static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
435{
436 struct kvm_memory_slot *slot;
437
438 if (has_wrprotected_page(vcpu->kvm, large_gfn))
439 return 0;
440
441 if (!host_largepage_backed(vcpu->kvm, large_gfn))
442 return 0;
443
444 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
445 if (slot && slot->dirty_bitmap)
446 return 0;
447
448 return 1;
449}
450
451/*
Izik Eidus290fc382007-09-27 14:11:22 +0200452 * Take gfn and return the reverse mapping to it.
453 * Note: gfn must be unaliased before this function get called
454 */
455
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300456static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200457{
458 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200460
461 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300462 if (!lpage)
463 return &slot->rmap[gfn - slot->base_gfn];
464
465 idx = (gfn / KVM_PAGES_PER_HPAGE) -
466 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
467
468 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200469}
470
471/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800472 * Reverse mapping data structures:
473 *
Izik Eidus290fc382007-09-27 14:11:22 +0200474 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
475 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800476 *
Izik Eidus290fc382007-09-27 14:11:22 +0200477 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
478 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800479 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300480static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800481{
Avi Kivity4db35312007-11-21 15:28:32 +0200482 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800483 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200484 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800485 int i;
486
487 if (!is_rmap_pte(*spte))
488 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200489 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200490 sp = page_header(__pa(spte));
491 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200493 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200495 *rmapp = (unsigned long)spte;
496 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800497 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800498 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200499 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200501 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800502 } else {
503 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200504 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800505 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
506 desc = desc->more;
507 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800508 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800509 desc = desc->more;
510 }
511 for (i = 0; desc->shadow_ptes[i]; ++i)
512 ;
513 desc->shadow_ptes[i] = spte;
514 }
515}
516
Izik Eidus290fc382007-09-27 14:11:22 +0200517static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800518 struct kvm_rmap_desc *desc,
519 int i,
520 struct kvm_rmap_desc *prev_desc)
521{
522 int j;
523
524 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
525 ;
526 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000527 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800528 if (j != 0)
529 return;
530 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200531 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800532 else
533 if (prev_desc)
534 prev_desc->more = desc->more;
535 else
Izik Eidus290fc382007-09-27 14:11:22 +0200536 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300537 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538}
539
Izik Eidus290fc382007-09-27 14:11:22 +0200540static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800542 struct kvm_rmap_desc *desc;
543 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200544 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500545 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200546 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 int i;
548
549 if (!is_rmap_pte(*spte))
550 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200551 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500552 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800553 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500554 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200555 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500556 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200557 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500558 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300559 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200560 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
562 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200563 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800564 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200565 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
567 spte, *spte);
568 BUG();
569 }
Izik Eidus290fc382007-09-27 14:11:22 +0200570 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 } else {
572 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200573 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800574 prev_desc = NULL;
575 while (desc) {
576 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
577 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200578 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800579 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 prev_desc);
581 return;
582 }
583 prev_desc = desc;
584 desc = desc->more;
585 }
586 BUG();
587 }
588}
589
Izik Eidus98348e92007-10-16 14:42:30 +0200590static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800591{
Avi Kivity374cbac2007-01-05 16:36:43 -0800592 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200593 struct kvm_rmap_desc *prev_desc;
594 u64 *prev_spte;
595 int i;
596
597 if (!*rmapp)
598 return NULL;
599 else if (!(*rmapp & 1)) {
600 if (!spte)
601 return (u64 *)*rmapp;
602 return NULL;
603 }
604 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
605 prev_desc = NULL;
606 prev_spte = NULL;
607 while (desc) {
608 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
609 if (prev_spte == spte)
610 return desc->shadow_ptes[i];
611 prev_spte = desc->shadow_ptes[i];
612 }
613 desc = desc->more;
614 }
615 return NULL;
616}
617
618static void rmap_write_protect(struct kvm *kvm, u64 gfn)
619{
Izik Eidus290fc382007-09-27 14:11:22 +0200620 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800621 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800622 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800623
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500624 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300625 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800626
Izik Eidus98348e92007-10-16 14:42:30 +0200627 spte = rmap_next(kvm, rmapp, NULL);
628 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800629 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800630 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800631 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800632 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200633 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800634 write_protected = 1;
635 }
Izik Eidus9647c142007-10-16 14:43:46 +0200636 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800637 }
Izik Eidus855149a2008-03-20 18:17:24 +0200638 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500639 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200640
641 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500642 pfn = spte_to_pfn(*spte);
643 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200644 }
645
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300646 /* check for huge page mappings */
647 rmapp = gfn_to_rmap(kvm, gfn, 1);
648 spte = rmap_next(kvm, rmapp, NULL);
649 while (spte) {
650 BUG_ON(!spte);
651 BUG_ON(!(*spte & PT_PRESENT_MASK));
652 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
653 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
654 if (is_writeble_pte(*spte)) {
655 rmap_remove(kvm, spte);
656 --kvm->stat.lpages;
657 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300658 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300659 write_protected = 1;
660 }
661 spte = rmap_next(kvm, rmapp, spte);
662 }
663
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800664 if (write_protected)
665 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800666}
667
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200668static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
669{
670 u64 *spte;
671 int need_tlb_flush = 0;
672
673 while ((spte = rmap_next(kvm, rmapp, NULL))) {
674 BUG_ON(!(*spte & PT_PRESENT_MASK));
675 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
676 rmap_remove(kvm, spte);
677 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
678 need_tlb_flush = 1;
679 }
680 return need_tlb_flush;
681}
682
683static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
684 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
685{
686 int i;
687 int retval = 0;
688
689 /*
690 * If mmap_sem isn't taken, we can look the memslots with only
691 * the mmu_lock by skipping over the slots with userspace_addr == 0.
692 */
693 for (i = 0; i < kvm->nmemslots; i++) {
694 struct kvm_memory_slot *memslot = &kvm->memslots[i];
695 unsigned long start = memslot->userspace_addr;
696 unsigned long end;
697
698 /* mmu_lock protects userspace_addr */
699 if (!start)
700 continue;
701
702 end = start + (memslot->npages << PAGE_SHIFT);
703 if (hva >= start && hva < end) {
704 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
705 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
706 retval |= handler(kvm,
707 &memslot->lpage_info[
708 gfn_offset /
709 KVM_PAGES_PER_HPAGE].rmap_pde);
710 }
711 }
712
713 return retval;
714}
715
716int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
717{
718 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
719}
720
721static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
722{
723 u64 *spte;
724 int young = 0;
725
Sheng Yang534e38b2008-09-08 15:12:30 +0800726 /* always return old for EPT */
727 if (!shadow_accessed_mask)
728 return 0;
729
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200730 spte = rmap_next(kvm, rmapp, NULL);
731 while (spte) {
732 int _young;
733 u64 _spte = *spte;
734 BUG_ON(!(_spte & PT_PRESENT_MASK));
735 _young = _spte & PT_ACCESSED_MASK;
736 if (_young) {
737 young = 1;
738 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
739 }
740 spte = rmap_next(kvm, rmapp, spte);
741 }
742 return young;
743}
744
745int kvm_age_hva(struct kvm *kvm, unsigned long hva)
746{
747 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
748}
749
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800750#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300751static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752{
Avi Kivity139bdb22007-01-05 16:36:50 -0800753 u64 *pos;
754 u64 *end;
755
Avi Kivity47ad8e62007-05-06 15:50:58 +0300756 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300757 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800758 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800759 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800761 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 return 1;
763}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800764#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765
Avi Kivity4db35312007-11-21 15:28:32 +0200766static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800767{
Avi Kivity4db35312007-11-21 15:28:32 +0200768 ASSERT(is_empty_shadow_page(sp->spt));
769 list_del(&sp->link);
770 __free_page(virt_to_page(sp->spt));
771 __free_page(virt_to_page(sp->gfns));
772 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800773 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800774}
775
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800776static unsigned kvm_page_table_hashfn(gfn_t gfn)
777{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200778 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800779}
780
Avi Kivity25c0de22007-01-05 16:36:42 -0800781static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
782 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783{
Avi Kivity4db35312007-11-21 15:28:32 +0200784 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800786 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
787 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
788 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200789 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800790 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200791 ASSERT(is_empty_shadow_page(sp->spt));
Sheng Yang291f26b2008-10-16 17:30:57 +0800792 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200793 sp->multimapped = 0;
794 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800795 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200796 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797}
798
Avi Kivity714b93d2007-01-05 16:36:53 -0800799static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200800 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800801{
802 struct kvm_pte_chain *pte_chain;
803 struct hlist_node *node;
804 int i;
805
806 if (!parent_pte)
807 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200808 if (!sp->multimapped) {
809 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800810
811 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200812 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800813 return;
814 }
Avi Kivity4db35312007-11-21 15:28:32 +0200815 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800816 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200817 INIT_HLIST_HEAD(&sp->parent_ptes);
818 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800819 pte_chain->parent_ptes[0] = old;
820 }
Avi Kivity4db35312007-11-21 15:28:32 +0200821 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
823 continue;
824 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
825 if (!pte_chain->parent_ptes[i]) {
826 pte_chain->parent_ptes[i] = parent_pte;
827 return;
828 }
829 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800830 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800831 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200832 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800833 pte_chain->parent_ptes[0] = parent_pte;
834}
835
Avi Kivity4db35312007-11-21 15:28:32 +0200836static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800837 u64 *parent_pte)
838{
839 struct kvm_pte_chain *pte_chain;
840 struct hlist_node *node;
841 int i;
842
Avi Kivity4db35312007-11-21 15:28:32 +0200843 if (!sp->multimapped) {
844 BUG_ON(sp->parent_pte != parent_pte);
845 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 return;
847 }
Avi Kivity4db35312007-11-21 15:28:32 +0200848 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800849 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
850 if (!pte_chain->parent_ptes[i])
851 break;
852 if (pte_chain->parent_ptes[i] != parent_pte)
853 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800854 while (i + 1 < NR_PTE_CHAIN_ENTRIES
855 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800856 pte_chain->parent_ptes[i]
857 = pte_chain->parent_ptes[i + 1];
858 ++i;
859 }
860 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800861 if (i == 0) {
862 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300863 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200864 if (hlist_empty(&sp->parent_ptes)) {
865 sp->multimapped = 0;
866 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800867 }
868 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800869 return;
870 }
871 BUG();
872}
873
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300874
875static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
876 mmu_parent_walk_fn fn)
877{
878 struct kvm_pte_chain *pte_chain;
879 struct hlist_node *node;
880 struct kvm_mmu_page *parent_sp;
881 int i;
882
883 if (!sp->multimapped && sp->parent_pte) {
884 parent_sp = page_header(__pa(sp->parent_pte));
885 fn(vcpu, parent_sp);
886 mmu_parent_walk(vcpu, parent_sp, fn);
887 return;
888 }
889 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
890 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
891 if (!pte_chain->parent_ptes[i])
892 break;
893 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
894 fn(vcpu, parent_sp);
895 mmu_parent_walk(vcpu, parent_sp, fn);
896 }
897}
898
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300899static void kvm_mmu_update_unsync_bitmap(u64 *spte)
900{
901 unsigned int index;
902 struct kvm_mmu_page *sp = page_header(__pa(spte));
903
904 index = spte - sp->spt;
905 __set_bit(index, sp->unsync_child_bitmap);
906 sp->unsync_children = 1;
907}
908
909static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
910{
911 struct kvm_pte_chain *pte_chain;
912 struct hlist_node *node;
913 int i;
914
915 if (!sp->parent_pte)
916 return;
917
918 if (!sp->multimapped) {
919 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
920 return;
921 }
922
923 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
924 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
925 if (!pte_chain->parent_ptes[i])
926 break;
927 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
928 }
929}
930
931static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
932{
933 sp->unsync_children = 1;
934 kvm_mmu_update_parents_unsync(sp);
935 return 1;
936}
937
938static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
939 struct kvm_mmu_page *sp)
940{
941 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
942 kvm_mmu_update_parents_unsync(sp);
943}
944
Avi Kivityd761a502008-05-29 14:55:03 +0300945static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
946 struct kvm_mmu_page *sp)
947{
948 int i;
949
950 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
951 sp->spt[i] = shadow_trap_nonpresent_pte;
952}
953
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300954static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
955 struct kvm_mmu_page *sp)
956{
957 return 1;
958}
959
Marcelo Tosattia7052892008-09-23 13:18:35 -0300960static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
961{
962}
963
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300964#define for_each_unsync_children(bitmap, idx) \
965 for (idx = find_first_bit(bitmap, 512); \
966 idx < 512; \
967 idx = find_next_bit(bitmap, 512, idx+1))
968
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300969static int mmu_unsync_walk(struct kvm_mmu_page *sp,
970 struct kvm_unsync_walk *walker)
971{
972 int i, ret;
973
974 if (!sp->unsync_children)
975 return 0;
976
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300977 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300978 u64 ent = sp->spt[i];
979
980 if (is_shadow_present_pte(ent)) {
981 struct kvm_mmu_page *child;
982 child = page_header(ent & PT64_BASE_ADDR_MASK);
983
984 if (child->unsync_children) {
985 ret = mmu_unsync_walk(child, walker);
986 if (ret)
987 return ret;
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300988 __clear_bit(i, sp->unsync_child_bitmap);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300989 }
990
991 if (child->unsync) {
992 ret = walker->entry(child, walker);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300993 __clear_bit(i, sp->unsync_child_bitmap);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300994 if (ret)
995 return ret;
996 }
997 }
998 }
999
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001000 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001001 sp->unsync_children = 0;
1002
1003 return 0;
1004}
1005
Avi Kivity4db35312007-11-21 15:28:32 +02001006static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007{
1008 unsigned index;
1009 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001010 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001011 struct hlist_node *node;
1012
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001013 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001014 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001015 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001016 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001017 if (sp->gfn == gfn && !sp->role.metaphysical
1018 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001019 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001020 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001021 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001022 }
1023 return NULL;
1024}
1025
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001026static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1027{
1028 WARN_ON(!sp->unsync);
1029 sp->unsync = 0;
1030 --kvm->stat.mmu_unsync;
1031}
1032
1033static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1034
1035static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1036{
1037 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1038 kvm_mmu_zap_page(vcpu->kvm, sp);
1039 return 1;
1040 }
1041
1042 rmap_write_protect(vcpu->kvm, sp->gfn);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001043 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001044 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1045 kvm_mmu_zap_page(vcpu->kvm, sp);
1046 return 1;
1047 }
1048
1049 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001050 return 0;
1051}
1052
1053struct sync_walker {
1054 struct kvm_vcpu *vcpu;
1055 struct kvm_unsync_walk walker;
1056};
1057
1058static int mmu_sync_fn(struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk)
1059{
1060 struct sync_walker *sync_walk = container_of(walk, struct sync_walker,
1061 walker);
1062 struct kvm_vcpu *vcpu = sync_walk->vcpu;
1063
1064 kvm_sync_page(vcpu, sp);
1065 return (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock));
1066}
1067
1068static void mmu_sync_children(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1069{
1070 struct sync_walker walker = {
1071 .walker = { .entry = mmu_sync_fn, },
1072 .vcpu = vcpu,
1073 };
1074
1075 while (mmu_unsync_walk(sp, &walker.walker))
1076 cond_resched_lock(&vcpu->kvm->mmu_lock);
1077}
1078
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001079static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1080 gfn_t gfn,
1081 gva_t gaddr,
1082 unsigned level,
1083 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001084 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001085 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001086{
1087 union kvm_mmu_page_role role;
1088 unsigned index;
1089 unsigned quadrant;
1090 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001091 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001092 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001093
1094 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001095 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001096 role.level = level;
1097 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001098 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001099 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001100 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1101 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1102 role.quadrant = quadrant;
1103 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001104 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001105 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001106 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001107 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001108 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1109 if (sp->gfn == gfn) {
1110 if (sp->unsync)
1111 if (kvm_sync_page(vcpu, sp))
1112 continue;
1113
1114 if (sp->role.word != role.word)
1115 continue;
1116
Avi Kivity4db35312007-11-21 15:28:32 +02001117 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001118 if (sp->unsync_children) {
1119 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1120 kvm_mmu_mark_parents_unsync(vcpu, sp);
1121 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001122 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001123 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001124 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001125 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001126 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1127 if (!sp)
1128 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001129 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001130 sp->gfn = gfn;
1131 sp->role = role;
1132 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001133 if (!metaphysical) {
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001134 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001135 account_shadowed(vcpu->kvm, gfn);
1136 }
Avi Kivity131d8272008-05-29 14:56:28 +03001137 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1138 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1139 else
1140 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001141 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001142}
1143
Avi Kivity3d000db2008-08-22 19:24:38 +03001144static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001145 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +03001146{
1147 hpa_t shadow_addr;
1148 int level;
1149 int r;
1150 u64 *sptep;
1151 unsigned index;
1152
1153 shadow_addr = vcpu->arch.mmu.root_hpa;
1154 level = vcpu->arch.mmu.shadow_root_level;
1155 if (level == PT32E_ROOT_LEVEL) {
1156 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1157 shadow_addr &= PT64_BASE_ADDR_MASK;
1158 --level;
1159 }
1160
1161 while (level >= PT_PAGE_TABLE_LEVEL) {
1162 index = SHADOW_PT_INDEX(addr, level);
1163 sptep = ((u64 *)__va(shadow_addr)) + index;
1164 r = walker->entry(walker, vcpu, addr, sptep, level);
1165 if (r)
1166 return r;
1167 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1168 --level;
1169 }
1170 return 0;
1171}
1172
Avi Kivity90cb0522007-07-17 13:04:56 +03001173static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001174 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001175{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001176 unsigned i;
1177 u64 *pt;
1178 u64 ent;
1179
Avi Kivity4db35312007-11-21 15:28:32 +02001180 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001181
Avi Kivity4db35312007-11-21 15:28:32 +02001182 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001183 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001184 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001185 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001186 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001187 }
1188 return;
1189 }
1190
1191 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1192 ent = pt[i];
1193
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001194 if (is_shadow_present_pte(ent)) {
1195 if (!is_large_pte(ent)) {
1196 ent &= PT64_BASE_ADDR_MASK;
1197 mmu_page_remove_parent_pte(page_header(ent),
1198 &pt[i]);
1199 } else {
1200 --kvm->stat.lpages;
1201 rmap_remove(kvm, &pt[i]);
1202 }
1203 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001204 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001205 }
Avi Kivitya4360362007-01-05 16:36:45 -08001206}
1207
Avi Kivity4db35312007-11-21 15:28:32 +02001208static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001209{
Avi Kivity4db35312007-11-21 15:28:32 +02001210 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001211}
1212
Avi Kivity12b7d282007-09-23 14:10:49 +02001213static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1214{
1215 int i;
1216
1217 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1218 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001219 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001220}
1221
Avi Kivity31aa2b42008-07-11 17:59:46 +03001222static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001223{
1224 u64 *parent_pte;
1225
Avi Kivity4db35312007-11-21 15:28:32 +02001226 while (sp->multimapped || sp->parent_pte) {
1227 if (!sp->multimapped)
1228 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001229 else {
1230 struct kvm_pte_chain *chain;
1231
Avi Kivity4db35312007-11-21 15:28:32 +02001232 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001233 struct kvm_pte_chain, link);
1234 parent_pte = chain->parent_ptes[0];
1235 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001236 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001237 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001238 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001239 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001240}
1241
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001242struct zap_walker {
1243 struct kvm_unsync_walk walker;
1244 struct kvm *kvm;
1245 int zapped;
1246};
1247
1248static int mmu_zap_fn(struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk)
1249{
1250 struct zap_walker *zap_walk = container_of(walk, struct zap_walker,
1251 walker);
1252 kvm_mmu_zap_page(zap_walk->kvm, sp);
1253 zap_walk->zapped = 1;
1254 return 0;
1255}
1256
1257static int mmu_zap_unsync_children(struct kvm *kvm, struct kvm_mmu_page *sp)
1258{
1259 struct zap_walker walker = {
1260 .walker = { .entry = mmu_zap_fn, },
1261 .kvm = kvm,
1262 .zapped = 0,
1263 };
1264
1265 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
1266 return 0;
1267 mmu_unsync_walk(sp, &walker.walker);
1268 return walker.zapped;
1269}
1270
Marcelo Tosatti07385412008-09-23 13:18:37 -03001271static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001272{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001273 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001274 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001275 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001276 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001277 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001278 kvm_flush_remote_tlbs(kvm);
1279 if (!sp->role.invalid && !sp->role.metaphysical)
1280 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001281 if (sp->unsync)
1282 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001283 if (!sp->root_count) {
1284 hlist_del(&sp->hash_link);
1285 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001286 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001287 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001288 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001289 kvm_reload_remote_mmus(kvm);
1290 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001291 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001292 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001293}
1294
Izik Eidus82ce2c92007-10-02 18:52:55 +02001295/*
1296 * Changing the number of mmu pages allocated to the vm
1297 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1298 */
1299void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1300{
1301 /*
1302 * If we set the number of mmu pages to be smaller be than the
1303 * number of actived pages , we must to free some mmu pages before we
1304 * change the value
1305 */
1306
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001307 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001308 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001309 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1310 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001311
1312 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1313 struct kvm_mmu_page *page;
1314
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001315 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001316 struct kvm_mmu_page, link);
1317 kvm_mmu_zap_page(kvm, page);
1318 n_used_mmu_pages--;
1319 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001320 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001321 }
1322 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001323 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1324 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001325
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001326 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001327}
1328
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001329static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001330{
1331 unsigned index;
1332 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001333 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001334 struct hlist_node *node, *n;
1335 int r;
1336
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001337 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001338 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001339 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001340 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001341 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1342 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001343 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001344 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001345 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001346 if (kvm_mmu_zap_page(kvm, sp))
1347 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001348 }
1349 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001350}
1351
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001352static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001353{
Avi Kivity4db35312007-11-21 15:28:32 +02001354 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001355
Avi Kivity4db35312007-11-21 15:28:32 +02001356 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001357 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001358 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001359 }
1360}
1361
Avi Kivity38c335f2007-11-21 14:20:22 +02001362static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363{
Avi Kivity38c335f2007-11-21 14:20:22 +02001364 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001365 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366
Sheng Yang291f26b2008-10-16 17:30:57 +08001367 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368}
1369
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001370static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1371{
1372 int i;
1373 u64 *pt = sp->spt;
1374
1375 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1376 return;
1377
1378 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1379 if (pt[i] == shadow_notrap_nonpresent_pte)
1380 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1381 }
1382}
1383
Avi Kivity039576c2007-03-20 12:46:50 +02001384struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1385{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001386 struct page *page;
1387
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001388 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001389
1390 if (gpa == UNMAPPED_GVA)
1391 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001392
Izik Eidus72dc67a2008-02-10 18:04:15 +02001393 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001394
1395 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001396}
1397
Sheng Yang74be52e2008-10-09 16:01:56 +08001398/*
1399 * The function is based on mtrr_type_lookup() in
1400 * arch/x86/kernel/cpu/mtrr/generic.c
1401 */
1402static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1403 u64 start, u64 end)
1404{
1405 int i;
1406 u64 base, mask;
1407 u8 prev_match, curr_match;
1408 int num_var_ranges = KVM_NR_VAR_MTRR;
1409
1410 if (!mtrr_state->enabled)
1411 return 0xFF;
1412
1413 /* Make end inclusive end, instead of exclusive */
1414 end--;
1415
1416 /* Look in fixed ranges. Just return the type as per start */
1417 if (mtrr_state->have_fixed && (start < 0x100000)) {
1418 int idx;
1419
1420 if (start < 0x80000) {
1421 idx = 0;
1422 idx += (start >> 16);
1423 return mtrr_state->fixed_ranges[idx];
1424 } else if (start < 0xC0000) {
1425 idx = 1 * 8;
1426 idx += ((start - 0x80000) >> 14);
1427 return mtrr_state->fixed_ranges[idx];
1428 } else if (start < 0x1000000) {
1429 idx = 3 * 8;
1430 idx += ((start - 0xC0000) >> 12);
1431 return mtrr_state->fixed_ranges[idx];
1432 }
1433 }
1434
1435 /*
1436 * Look in variable ranges
1437 * Look of multiple ranges matching this address and pick type
1438 * as per MTRR precedence
1439 */
1440 if (!(mtrr_state->enabled & 2))
1441 return mtrr_state->def_type;
1442
1443 prev_match = 0xFF;
1444 for (i = 0; i < num_var_ranges; ++i) {
1445 unsigned short start_state, end_state;
1446
1447 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1448 continue;
1449
1450 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1451 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1452 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1453 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1454
1455 start_state = ((start & mask) == (base & mask));
1456 end_state = ((end & mask) == (base & mask));
1457 if (start_state != end_state)
1458 return 0xFE;
1459
1460 if ((start & mask) != (base & mask))
1461 continue;
1462
1463 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1464 if (prev_match == 0xFF) {
1465 prev_match = curr_match;
1466 continue;
1467 }
1468
1469 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1470 curr_match == MTRR_TYPE_UNCACHABLE)
1471 return MTRR_TYPE_UNCACHABLE;
1472
1473 if ((prev_match == MTRR_TYPE_WRBACK &&
1474 curr_match == MTRR_TYPE_WRTHROUGH) ||
1475 (prev_match == MTRR_TYPE_WRTHROUGH &&
1476 curr_match == MTRR_TYPE_WRBACK)) {
1477 prev_match = MTRR_TYPE_WRTHROUGH;
1478 curr_match = MTRR_TYPE_WRTHROUGH;
1479 }
1480
1481 if (prev_match != curr_match)
1482 return MTRR_TYPE_UNCACHABLE;
1483 }
1484
1485 if (prev_match != 0xFF)
1486 return prev_match;
1487
1488 return mtrr_state->def_type;
1489}
1490
1491static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1492{
1493 u8 mtrr;
1494
1495 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1496 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1497 if (mtrr == 0xfe || mtrr == 0xff)
1498 mtrr = MTRR_TYPE_WRBACK;
1499 return mtrr;
1500}
1501
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001502static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1503{
1504 unsigned index;
1505 struct hlist_head *bucket;
1506 struct kvm_mmu_page *s;
1507 struct hlist_node *node, *n;
1508
1509 index = kvm_page_table_hashfn(sp->gfn);
1510 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1511 /* don't unsync if pagetable is shadowed with multiple roles */
1512 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1513 if (s->gfn != sp->gfn || s->role.metaphysical)
1514 continue;
1515 if (s->role.word != sp->role.word)
1516 return 1;
1517 }
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001518 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001519 ++vcpu->kvm->stat.mmu_unsync;
1520 sp->unsync = 1;
1521 mmu_convert_notrap(sp);
1522 return 0;
1523}
1524
1525static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1526 bool can_unsync)
1527{
1528 struct kvm_mmu_page *shadow;
1529
1530 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1531 if (shadow) {
1532 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1533 return 1;
1534 if (shadow->unsync)
1535 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001536 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001537 return kvm_unsync_page(vcpu, shadow);
1538 return 1;
1539 }
1540 return 0;
1541}
1542
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001543static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1544 unsigned pte_access, int user_fault,
1545 int write_fault, int dirty, int largepage,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001546 gfn_t gfn, pfn_t pfn, bool speculative,
1547 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001548{
1549 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001550 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001551 u64 mt_mask = shadow_mt_mask;
1552
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001553 /*
1554 * We don't set the accessed bit, since we sometimes want to see
1555 * whether the guest actually used the pte (in order to detect
1556 * demand paging).
1557 */
Sheng Yang7b523452008-04-25 21:13:50 +08001558 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001559 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001560 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001561 if (!dirty)
1562 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001563 if (pte_access & ACC_EXEC_MASK)
1564 spte |= shadow_x_mask;
1565 else
1566 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001567 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001568 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001569 if (largepage)
1570 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang64d4d522008-10-09 16:01:57 +08001571 if (mt_mask) {
1572 mt_mask = get_memory_type(vcpu, gfn) <<
1573 kvm_x86_ops->get_mt_mask_shift();
1574 spte |= mt_mask;
1575 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001576
Anthony Liguori35149e22008-04-02 14:46:56 -05001577 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001578
1579 if ((pte_access & ACC_WRITE_MASK)
1580 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001581
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001582 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1583 ret = 1;
1584 spte = shadow_trap_nonpresent_pte;
1585 goto set_pte;
1586 }
1587
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001588 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001589
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001590 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001591 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001592 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001593 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001594 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001595 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001596 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001597 }
1598 }
1599
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001600 if (pte_access & ACC_WRITE_MASK)
1601 mark_page_dirty(vcpu->kvm, gfn);
1602
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001603set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001604 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001605 return ret;
1606}
1607
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001608static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1609 unsigned pt_access, unsigned pte_access,
1610 int user_fault, int write_fault, int dirty,
1611 int *ptwrite, int largepage, gfn_t gfn,
1612 pfn_t pfn, bool speculative)
1613{
1614 int was_rmapped = 0;
1615 int was_writeble = is_writeble_pte(*shadow_pte);
1616
1617 pgprintk("%s: spte %llx access %x write_fault %d"
1618 " user_fault %d gfn %lx\n",
1619 __func__, *shadow_pte, pt_access,
1620 write_fault, user_fault, gfn);
1621
1622 if (is_rmap_pte(*shadow_pte)) {
1623 /*
1624 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1625 * the parent of the now unreachable PTE.
1626 */
1627 if (largepage && !is_large_pte(*shadow_pte)) {
1628 struct kvm_mmu_page *child;
1629 u64 pte = *shadow_pte;
1630
1631 child = page_header(pte & PT64_BASE_ADDR_MASK);
1632 mmu_page_remove_parent_pte(child, shadow_pte);
1633 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1634 pgprintk("hfn old %lx new %lx\n",
1635 spte_to_pfn(*shadow_pte), pfn);
1636 rmap_remove(vcpu->kvm, shadow_pte);
1637 } else {
1638 if (largepage)
1639 was_rmapped = is_large_pte(*shadow_pte);
1640 else
1641 was_rmapped = 1;
1642 }
1643 }
1644 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001645 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001646 if (write_fault)
1647 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001648 kvm_x86_ops->tlb_flush(vcpu);
1649 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001650
1651 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1652 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1653 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1654 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1655 *shadow_pte, shadow_pte);
1656 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001657 ++vcpu->kvm->stat.lpages;
1658
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001659 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1660 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001661 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001662 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001663 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001664 } else {
1665 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001666 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001667 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001668 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001669 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001670 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001671 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001672 vcpu->arch.last_pte_gfn = gfn;
1673 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001674}
1675
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1677{
1678}
1679
Avi Kivity140754b2008-08-22 19:28:04 +03001680struct direct_shadow_walk {
1681 struct kvm_shadow_walk walker;
1682 pfn_t pfn;
1683 int write;
1684 int largepage;
1685 int pt_write;
1686};
1687
1688static int direct_map_entry(struct kvm_shadow_walk *_walk,
1689 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001690 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001691{
1692 struct direct_shadow_walk *walk =
1693 container_of(_walk, struct direct_shadow_walk, walker);
1694 struct kvm_mmu_page *sp;
1695 gfn_t pseudo_gfn;
1696 gfn_t gfn = addr >> PAGE_SHIFT;
1697
1698 if (level == PT_PAGE_TABLE_LEVEL
1699 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1700 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1701 0, walk->write, 1, &walk->pt_write,
1702 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001703 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001704 return 1;
1705 }
1706
1707 if (*sptep == shadow_trap_nonpresent_pte) {
1708 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001709 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001710 1, ACC_ALL, sptep);
1711 if (!sp) {
1712 pgprintk("nonpaging_map: ENOMEM\n");
1713 kvm_release_pfn_clean(walk->pfn);
1714 return -ENOMEM;
1715 }
1716
1717 set_shadow_pte(sptep,
1718 __pa(sp->spt)
1719 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1720 | shadow_user_mask | shadow_x_mask);
1721 }
1722 return 0;
1723}
1724
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001725static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001726 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727{
Avi Kivity140754b2008-08-22 19:28:04 +03001728 int r;
1729 struct direct_shadow_walk walker = {
1730 .walker = { .entry = direct_map_entry, },
1731 .pfn = pfn,
1732 .largepage = largepage,
1733 .write = write,
1734 .pt_write = 0,
1735 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001737 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001738 if (r < 0)
1739 return r;
1740 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741}
1742
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001743static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1744{
1745 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001746 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001747 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001748 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001749
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001750 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1751 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1752 largepage = 1;
1753 }
1754
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001755 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001756 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001757 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001758
Avi Kivityd196e342008-01-24 11:44:11 +02001759 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001760 if (is_error_pfn(pfn)) {
1761 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001762 return 1;
1763 }
1764
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001765 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001766 if (mmu_notifier_retry(vcpu, mmu_seq))
1767 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001768 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001769 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001770 spin_unlock(&vcpu->kvm->mmu_lock);
1771
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001772
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001773 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001774
1775out_unlock:
1776 spin_unlock(&vcpu->kvm->mmu_lock);
1777 kvm_release_pfn_clean(pfn);
1778 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001779}
1780
1781
Avi Kivity17ac10a2007-01-05 16:36:40 -08001782static void mmu_free_roots(struct kvm_vcpu *vcpu)
1783{
1784 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001785 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001786
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001787 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001788 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001789 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001790 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1791 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001792
Avi Kivity4db35312007-11-21 15:28:32 +02001793 sp = page_header(root);
1794 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001795 if (!sp->root_count && sp->role.invalid)
1796 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001797 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001798 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001799 return;
1800 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001801 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001802 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001803
Avi Kivity417726a2007-04-12 17:35:58 +03001804 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001805 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001806 sp = page_header(root);
1807 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001808 if (!sp->root_count && sp->role.invalid)
1809 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001810 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001811 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001812 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001813 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001814 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001815}
1816
1817static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1818{
1819 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001820 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001821 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001822 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001823
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001824 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001825
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001826 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1827 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001828
1829 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001830 if (tdp_enabled)
1831 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001832 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001833 PT64_ROOT_LEVEL, metaphysical,
1834 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001835 root = __pa(sp->spt);
1836 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001837 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001838 return;
1839 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001840 metaphysical = !is_paging(vcpu);
1841 if (tdp_enabled)
1842 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001843 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001844 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001845
1846 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001847 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1848 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1849 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001850 continue;
1851 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001852 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1853 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001854 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001855 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001856 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001857 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001858 root = __pa(sp->spt);
1859 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001860 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001861 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001862 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001863}
1864
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001865static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1866{
1867 int i;
1868 struct kvm_mmu_page *sp;
1869
1870 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1871 return;
1872 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1873 hpa_t root = vcpu->arch.mmu.root_hpa;
1874 sp = page_header(root);
1875 mmu_sync_children(vcpu, sp);
1876 return;
1877 }
1878 for (i = 0; i < 4; ++i) {
1879 hpa_t root = vcpu->arch.mmu.pae_root[i];
1880
1881 if (root) {
1882 root &= PT64_BASE_ADDR_MASK;
1883 sp = page_header(root);
1884 mmu_sync_children(vcpu, sp);
1885 }
1886 }
1887}
1888
1889void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1890{
1891 spin_lock(&vcpu->kvm->mmu_lock);
1892 mmu_sync_roots(vcpu);
1893 spin_unlock(&vcpu->kvm->mmu_lock);
1894}
1895
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1897{
1898 return vaddr;
1899}
1900
1901static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001902 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
Avi Kivitye8332402007-12-09 18:43:00 +02001904 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001905 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001907 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001908 r = mmu_topup_memory_caches(vcpu);
1909 if (r)
1910 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001911
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001913 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914
Avi Kivitye8332402007-12-09 18:43:00 +02001915 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916
Avi Kivitye8332402007-12-09 18:43:00 +02001917 return nonpaging_map(vcpu, gva & PAGE_MASK,
1918 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919}
1920
Joerg Roedelfb72d162008-02-07 13:47:44 +01001921static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1922 u32 error_code)
1923{
Anthony Liguori35149e22008-04-02 14:46:56 -05001924 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001925 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001926 int largepage = 0;
1927 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001928 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001929
1930 ASSERT(vcpu);
1931 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1932
1933 r = mmu_topup_memory_caches(vcpu);
1934 if (r)
1935 return r;
1936
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001937 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1938 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1939 largepage = 1;
1940 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001941 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001942 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001943 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05001944 if (is_error_pfn(pfn)) {
1945 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001946 return 1;
1947 }
1948 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001949 if (mmu_notifier_retry(vcpu, mmu_seq))
1950 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001951 kvm_mmu_free_some_pages(vcpu);
1952 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03001953 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001954 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001955
1956 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001957
1958out_unlock:
1959 spin_unlock(&vcpu->kvm->mmu_lock);
1960 kvm_release_pfn_clean(pfn);
1961 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001962}
1963
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964static void nonpaging_free(struct kvm_vcpu *vcpu)
1965{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001966 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967}
1968
1969static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1970{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001971 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972
1973 context->new_cr3 = nonpaging_new_cr3;
1974 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 context->gva_to_gpa = nonpaging_gva_to_gpa;
1976 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001977 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001978 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001979 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001980 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001982 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 return 0;
1984}
1985
Avi Kivityd835dfe2007-11-21 02:57:59 +02001986void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001988 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001989 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990}
1991
1992static void paging_new_cr3(struct kvm_vcpu *vcpu)
1993{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001994 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001995 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996}
1997
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998static void inject_page_fault(struct kvm_vcpu *vcpu,
1999 u64 addr,
2000 u32 err_code)
2001{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002002 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003}
2004
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005static void paging_free(struct kvm_vcpu *vcpu)
2006{
2007 nonpaging_free(vcpu);
2008}
2009
2010#define PTTYPE 64
2011#include "paging_tmpl.h"
2012#undef PTTYPE
2013
2014#define PTTYPE 32
2015#include "paging_tmpl.h"
2016#undef PTTYPE
2017
Avi Kivity17ac10a2007-01-05 16:36:40 -08002018static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002020 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021
2022 ASSERT(is_pae(vcpu));
2023 context->new_cr3 = paging_new_cr3;
2024 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002026 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002027 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002028 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002030 context->root_level = level;
2031 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002032 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033 return 0;
2034}
2035
Avi Kivity17ac10a2007-01-05 16:36:40 -08002036static int paging64_init_context(struct kvm_vcpu *vcpu)
2037{
2038 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2039}
2040
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041static int paging32_init_context(struct kvm_vcpu *vcpu)
2042{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002043 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044
2045 context->new_cr3 = paging_new_cr3;
2046 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 context->gva_to_gpa = paging32_gva_to_gpa;
2048 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002049 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002050 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002051 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052 context->root_level = PT32_ROOT_LEVEL;
2053 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002054 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055 return 0;
2056}
2057
2058static int paging32E_init_context(struct kvm_vcpu *vcpu)
2059{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002060 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061}
2062
Joerg Roedelfb72d162008-02-07 13:47:44 +01002063static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2064{
2065 struct kvm_mmu *context = &vcpu->arch.mmu;
2066
2067 context->new_cr3 = nonpaging_new_cr3;
2068 context->page_fault = tdp_page_fault;
2069 context->free = nonpaging_free;
2070 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002071 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002072 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002073 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002074 context->root_hpa = INVALID_PAGE;
2075
2076 if (!is_paging(vcpu)) {
2077 context->gva_to_gpa = nonpaging_gva_to_gpa;
2078 context->root_level = 0;
2079 } else if (is_long_mode(vcpu)) {
2080 context->gva_to_gpa = paging64_gva_to_gpa;
2081 context->root_level = PT64_ROOT_LEVEL;
2082 } else if (is_pae(vcpu)) {
2083 context->gva_to_gpa = paging64_gva_to_gpa;
2084 context->root_level = PT32E_ROOT_LEVEL;
2085 } else {
2086 context->gva_to_gpa = paging32_gva_to_gpa;
2087 context->root_level = PT32_ROOT_LEVEL;
2088 }
2089
2090 return 0;
2091}
2092
2093static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094{
2095 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002096 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097
2098 if (!is_paging(vcpu))
2099 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002100 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 return paging64_init_context(vcpu);
2102 else if (is_pae(vcpu))
2103 return paging32E_init_context(vcpu);
2104 else
2105 return paging32_init_context(vcpu);
2106}
2107
Joerg Roedelfb72d162008-02-07 13:47:44 +01002108static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2109{
Anthony Liguori35149e22008-04-02 14:46:56 -05002110 vcpu->arch.update_pte.pfn = bad_pfn;
2111
Joerg Roedelfb72d162008-02-07 13:47:44 +01002112 if (tdp_enabled)
2113 return init_kvm_tdp_mmu(vcpu);
2114 else
2115 return init_kvm_softmmu(vcpu);
2116}
2117
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2119{
2120 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002121 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2122 vcpu->arch.mmu.free(vcpu);
2123 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 }
2125}
2126
2127int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2128{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002129 destroy_kvm_mmu(vcpu);
2130 return init_kvm_mmu(vcpu);
2131}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002132EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002133
2134int kvm_mmu_load(struct kvm_vcpu *vcpu)
2135{
Avi Kivity714b93d2007-01-05 16:36:53 -08002136 int r;
2137
Avi Kivitye2dec932007-01-05 16:36:54 -08002138 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002139 if (r)
2140 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002141 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002142 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002143 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002144 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002145 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002146 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002147 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002148out:
2149 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002151EXPORT_SYMBOL_GPL(kvm_mmu_load);
2152
2153void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2154{
2155 mmu_free_roots(vcpu);
2156}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157
Avi Kivity09072da2007-05-01 14:16:52 +03002158static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002159 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002160 u64 *spte)
2161{
2162 u64 pte;
2163 struct kvm_mmu_page *child;
2164
2165 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002166 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002167 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2168 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002169 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002170 else {
2171 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002172 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002173 }
2174 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002175 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002176 if (is_large_pte(pte))
2177 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002178}
2179
Avi Kivity00284252007-05-01 16:53:31 +03002180static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002181 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002182 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002183 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002184{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002185 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2186 if (!vcpu->arch.update_pte.largepage ||
2187 sp->role.glevels == PT32_ROOT_LEVEL) {
2188 ++vcpu->kvm->stat.mmu_pde_zapped;
2189 return;
2190 }
2191 }
Avi Kivity00284252007-05-01 16:53:31 +03002192
Avi Kivity4cee5762007-11-18 16:37:07 +02002193 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002194 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002195 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002196 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002197 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002198}
2199
Avi Kivity79539ce2007-11-21 02:06:21 +02002200static bool need_remote_flush(u64 old, u64 new)
2201{
2202 if (!is_shadow_present_pte(old))
2203 return false;
2204 if (!is_shadow_present_pte(new))
2205 return true;
2206 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2207 return true;
2208 old ^= PT64_NX_MASK;
2209 new ^= PT64_NX_MASK;
2210 return (old & ~new & PT64_PERM_MASK) != 0;
2211}
2212
2213static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2214{
2215 if (need_remote_flush(old, new))
2216 kvm_flush_remote_tlbs(vcpu->kvm);
2217 else
2218 kvm_mmu_flush_tlb(vcpu);
2219}
2220
Avi Kivity12b7d282007-09-23 14:10:49 +02002221static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2222{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002223 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002224
Sheng Yang7b523452008-04-25 21:13:50 +08002225 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002226}
2227
Avi Kivityd7824ff2007-12-30 12:29:05 +02002228static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2229 const u8 *new, int bytes)
2230{
2231 gfn_t gfn;
2232 int r;
2233 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002234 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002235
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002236 vcpu->arch.update_pte.largepage = 0;
2237
Avi Kivityd7824ff2007-12-30 12:29:05 +02002238 if (bytes != 4 && bytes != 8)
2239 return;
2240
2241 /*
2242 * Assume that the pte write on a page table of the same type
2243 * as the current vcpu paging mode. This is nearly always true
2244 * (might be false while changing modes). Note it is verified later
2245 * by update_pte().
2246 */
2247 if (is_pae(vcpu)) {
2248 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2249 if ((bytes == 4) && (gpa % 4 == 0)) {
2250 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2251 if (r)
2252 return;
2253 memcpy((void *)&gpte + (gpa % 8), new, 4);
2254 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2255 memcpy((void *)&gpte, new, 8);
2256 }
2257 } else {
2258 if ((bytes == 4) && (gpa % 4 == 0))
2259 memcpy((void *)&gpte, new, 4);
2260 }
2261 if (!is_present_pte(gpte))
2262 return;
2263 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002264
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002265 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2266 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2267 vcpu->arch.update_pte.largepage = 1;
2268 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002269 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002270 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002271 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002272
Anthony Liguori35149e22008-04-02 14:46:56 -05002273 if (is_error_pfn(pfn)) {
2274 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002275 return;
2276 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002277 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002278 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002279}
2280
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002281static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2282{
2283 u64 *spte = vcpu->arch.last_pte_updated;
2284
2285 if (spte
2286 && vcpu->arch.last_pte_gfn == gfn
2287 && shadow_accessed_mask
2288 && !(*spte & shadow_accessed_mask)
2289 && is_shadow_present_pte(*spte))
2290 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2291}
2292
Avi Kivity09072da2007-05-01 14:16:52 +03002293void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08002294 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002295{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002296 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002297 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002298 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002299 struct hlist_head *bucket;
2300 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002301 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002302 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002303 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002304 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002305 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002306 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002307 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002308 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002309 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002310 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002311 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002312
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002313 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002314 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002315 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002316 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002317 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002318 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002319 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002320 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02002321 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002322 ++vcpu->arch.last_pt_write_count;
2323 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08002324 flooded = 1;
2325 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002326 vcpu->arch.last_pt_write_gfn = gfn;
2327 vcpu->arch.last_pt_write_count = 1;
2328 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002329 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002330 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002331 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002332 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002333 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002334 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002335 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002336 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002337 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002338 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002339 /*
2340 * Misaligned accesses are too much trouble to fix
2341 * up; also, they usually indicate a page is not used
2342 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002343 *
2344 * If we're seeing too many writes to a page,
2345 * it may no longer be a page table, or we may be
2346 * forking, in which case it is better to unmap the
2347 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002348 */
2349 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002350 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002351 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2352 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002353 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002354 continue;
2355 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002356 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002357 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002358 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002359 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002360 page_offset <<= 1; /* 32->64 */
2361 /*
2362 * A 32-bit pde maps 4MB while the shadow pdes map
2363 * only 2MB. So we need to double the offset again
2364 * and zap two pdes instead of one.
2365 */
2366 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002367 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002368 page_offset <<= 1;
2369 npte = 2;
2370 }
Avi Kivityfce06572007-05-01 16:44:05 +03002371 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002372 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002373 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002374 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002375 }
Avi Kivity4db35312007-11-21 15:28:32 +02002376 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002377 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2378 gentry = 0;
2379 r = kvm_read_guest_atomic(vcpu->kvm,
2380 gpa & ~(u64)(pte_size - 1),
2381 &gentry, pte_size);
2382 new = (const void *)&gentry;
2383 if (r < 0)
2384 new = NULL;
2385 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002386 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002387 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002388 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002389 if (new)
2390 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002391 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002392 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002393 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002394 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002395 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002396 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002397 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2398 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2399 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002400 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002401}
2402
Avi Kivitya4360362007-01-05 16:36:45 -08002403int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2404{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002405 gpa_t gpa;
2406 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002407
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002408 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002409
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002410 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002411 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002412 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002413 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002414}
Avi Kivity577bdc42008-07-19 08:57:05 +03002415EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002416
Avi Kivity22d95b12007-09-14 20:26:06 +03002417void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002418{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002419 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002420 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002421
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002422 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002423 struct kvm_mmu_page, link);
2424 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002425 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002426 }
2427}
Avi Kivityebeace82007-01-05 16:36:47 -08002428
Avi Kivity30677142007-10-28 18:48:59 +02002429int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2430{
2431 int r;
2432 enum emulation_result er;
2433
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002434 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002435 if (r < 0)
2436 goto out;
2437
2438 if (!r) {
2439 r = 1;
2440 goto out;
2441 }
2442
Avi Kivityb733bfb2007-10-28 18:52:05 +02002443 r = mmu_topup_memory_caches(vcpu);
2444 if (r)
2445 goto out;
2446
Avi Kivity30677142007-10-28 18:48:59 +02002447 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002448
2449 switch (er) {
2450 case EMULATE_DONE:
2451 return 1;
2452 case EMULATE_DO_MMIO:
2453 ++vcpu->stat.mmio_exits;
2454 return 0;
2455 case EMULATE_FAIL:
2456 kvm_report_emulation_failure(vcpu, "pagetable");
2457 return 1;
2458 default:
2459 BUG();
2460 }
2461out:
Avi Kivity30677142007-10-28 18:48:59 +02002462 return r;
2463}
2464EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2465
Marcelo Tosattia7052892008-09-23 13:18:35 -03002466void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2467{
2468 spin_lock(&vcpu->kvm->mmu_lock);
2469 vcpu->arch.mmu.invlpg(vcpu, gva);
2470 spin_unlock(&vcpu->kvm->mmu_lock);
2471 kvm_mmu_flush_tlb(vcpu);
2472 ++vcpu->stat.invlpg;
2473}
2474EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2475
Joerg Roedel18552672008-02-07 13:47:41 +01002476void kvm_enable_tdp(void)
2477{
2478 tdp_enabled = true;
2479}
2480EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2481
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002482void kvm_disable_tdp(void)
2483{
2484 tdp_enabled = false;
2485}
2486EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2487
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488static void free_mmu_pages(struct kvm_vcpu *vcpu)
2489{
Avi Kivity4db35312007-11-21 15:28:32 +02002490 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002492 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2493 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002494 struct kvm_mmu_page, link);
2495 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002496 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002497 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002498 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499}
2500
2501static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2502{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002503 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 int i;
2505
2506 ASSERT(vcpu);
2507
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002508 if (vcpu->kvm->arch.n_requested_mmu_pages)
2509 vcpu->kvm->arch.n_free_mmu_pages =
2510 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002511 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002512 vcpu->kvm->arch.n_free_mmu_pages =
2513 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002514 /*
2515 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2516 * Therefore we need to allocate shadow page tables in the first
2517 * 4GB of memory, which happens to fit the DMA32 zone.
2518 */
2519 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2520 if (!page)
2521 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002522 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002523 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002524 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002525
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 return 0;
2527
2528error_1:
2529 free_mmu_pages(vcpu);
2530 return -ENOMEM;
2531}
2532
Ingo Molnar8018c272006-12-29 16:50:01 -08002533int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002536 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537
Ingo Molnar8018c272006-12-29 16:50:01 -08002538 return alloc_mmu_pages(vcpu);
2539}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540
Ingo Molnar8018c272006-12-29 16:50:01 -08002541int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2542{
2543 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002544 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002545
Ingo Molnar8018c272006-12-29 16:50:01 -08002546 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547}
2548
2549void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2550{
2551 ASSERT(vcpu);
2552
2553 destroy_kvm_mmu(vcpu);
2554 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002555 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556}
2557
Avi Kivity90cb0522007-07-17 13:04:56 +03002558void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559{
Avi Kivity4db35312007-11-21 15:28:32 +02002560 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561
Avi Kivity2245a282008-08-27 16:32:24 +03002562 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002563 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 int i;
2565 u64 *pt;
2566
Sheng Yang291f26b2008-10-16 17:30:57 +08002567 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 continue;
2569
Avi Kivity4db35312007-11-21 15:28:32 +02002570 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2572 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002573 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 }
Avi Kivity171d5952008-08-27 16:40:51 +03002576 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002577 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002579
Avi Kivity90cb0522007-07-17 13:04:56 +03002580void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002581{
Avi Kivity4db35312007-11-21 15:28:32 +02002582 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002583
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002584 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002585 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002586 if (kvm_mmu_zap_page(kvm, sp))
2587 node = container_of(kvm->arch.active_mmu_pages.next,
2588 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002589 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002590
Avi Kivity90cb0522007-07-17 13:04:56 +03002591 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002592}
2593
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002594static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002595{
2596 struct kvm_mmu_page *page;
2597
2598 page = container_of(kvm->arch.active_mmu_pages.prev,
2599 struct kvm_mmu_page, link);
2600 kvm_mmu_zap_page(kvm, page);
2601}
2602
2603static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2604{
2605 struct kvm *kvm;
2606 struct kvm *kvm_freed = NULL;
2607 int cache_count = 0;
2608
2609 spin_lock(&kvm_lock);
2610
2611 list_for_each_entry(kvm, &vm_list, vm_list) {
2612 int npages;
2613
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002614 if (!down_read_trylock(&kvm->slots_lock))
2615 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002616 spin_lock(&kvm->mmu_lock);
2617 npages = kvm->arch.n_alloc_mmu_pages -
2618 kvm->arch.n_free_mmu_pages;
2619 cache_count += npages;
2620 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2621 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2622 cache_count--;
2623 kvm_freed = kvm;
2624 }
2625 nr_to_scan--;
2626
2627 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002628 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002629 }
2630 if (kvm_freed)
2631 list_move_tail(&kvm_freed->vm_list, &vm_list);
2632
2633 spin_unlock(&kvm_lock);
2634
2635 return cache_count;
2636}
2637
2638static struct shrinker mmu_shrinker = {
2639 .shrink = mmu_shrink,
2640 .seeks = DEFAULT_SEEKS * 10,
2641};
2642
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002643static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002644{
2645 if (pte_chain_cache)
2646 kmem_cache_destroy(pte_chain_cache);
2647 if (rmap_desc_cache)
2648 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002649 if (mmu_page_header_cache)
2650 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002651}
2652
Izik Eidus3ee16c82008-03-30 15:17:21 +03002653void kvm_mmu_module_exit(void)
2654{
2655 mmu_destroy_caches();
2656 unregister_shrinker(&mmu_shrinker);
2657}
2658
Avi Kivityb5a33a72007-04-15 16:31:09 +03002659int kvm_mmu_module_init(void)
2660{
2661 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2662 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002663 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002664 if (!pte_chain_cache)
2665 goto nomem;
2666 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2667 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002668 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002669 if (!rmap_desc_cache)
2670 goto nomem;
2671
Avi Kivityd3d25b02007-05-30 12:34:53 +03002672 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2673 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002674 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002675 if (!mmu_page_header_cache)
2676 goto nomem;
2677
Izik Eidus3ee16c82008-03-30 15:17:21 +03002678 register_shrinker(&mmu_shrinker);
2679
Avi Kivityb5a33a72007-04-15 16:31:09 +03002680 return 0;
2681
2682nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002683 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002684 return -ENOMEM;
2685}
2686
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002687/*
2688 * Caculate mmu pages needed for kvm.
2689 */
2690unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2691{
2692 int i;
2693 unsigned int nr_mmu_pages;
2694 unsigned int nr_pages = 0;
2695
2696 for (i = 0; i < kvm->nmemslots; i++)
2697 nr_pages += kvm->memslots[i].npages;
2698
2699 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2700 nr_mmu_pages = max(nr_mmu_pages,
2701 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2702
2703 return nr_mmu_pages;
2704}
2705
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002706static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2707 unsigned len)
2708{
2709 if (len > buffer->len)
2710 return NULL;
2711 return buffer->ptr;
2712}
2713
2714static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2715 unsigned len)
2716{
2717 void *ret;
2718
2719 ret = pv_mmu_peek_buffer(buffer, len);
2720 if (!ret)
2721 return ret;
2722 buffer->ptr += len;
2723 buffer->len -= len;
2724 buffer->processed += len;
2725 return ret;
2726}
2727
2728static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2729 gpa_t addr, gpa_t value)
2730{
2731 int bytes = 8;
2732 int r;
2733
2734 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2735 bytes = 4;
2736
2737 r = mmu_topup_memory_caches(vcpu);
2738 if (r)
2739 return r;
2740
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002741 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002742 return -EFAULT;
2743
2744 return 1;
2745}
2746
2747static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2748{
2749 kvm_x86_ops->tlb_flush(vcpu);
Marcelo Tosatti6ad9f152008-10-15 07:45:08 -02002750 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002751 return 1;
2752}
2753
2754static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2755{
2756 spin_lock(&vcpu->kvm->mmu_lock);
2757 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2758 spin_unlock(&vcpu->kvm->mmu_lock);
2759 return 1;
2760}
2761
2762static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2763 struct kvm_pv_mmu_op_buffer *buffer)
2764{
2765 struct kvm_mmu_op_header *header;
2766
2767 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2768 if (!header)
2769 return 0;
2770 switch (header->op) {
2771 case KVM_MMU_OP_WRITE_PTE: {
2772 struct kvm_mmu_op_write_pte *wpte;
2773
2774 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2775 if (!wpte)
2776 return 0;
2777 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2778 wpte->pte_val);
2779 }
2780 case KVM_MMU_OP_FLUSH_TLB: {
2781 struct kvm_mmu_op_flush_tlb *ftlb;
2782
2783 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2784 if (!ftlb)
2785 return 0;
2786 return kvm_pv_mmu_flush_tlb(vcpu);
2787 }
2788 case KVM_MMU_OP_RELEASE_PT: {
2789 struct kvm_mmu_op_release_pt *rpt;
2790
2791 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2792 if (!rpt)
2793 return 0;
2794 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2795 }
2796 default: return 0;
2797 }
2798}
2799
2800int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2801 gpa_t addr, unsigned long *ret)
2802{
2803 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002804 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002805
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002806 buffer->ptr = buffer->buf;
2807 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2808 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002809
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002810 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002811 if (r)
2812 goto out;
2813
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002814 while (buffer->len) {
2815 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002816 if (r < 0)
2817 goto out;
2818 if (r == 0)
2819 break;
2820 }
2821
2822 r = 1;
2823out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002824 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002825 return r;
2826}
2827
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002828#ifdef AUDIT
2829
2830static const char *audit_msg;
2831
2832static gva_t canonicalize(gva_t gva)
2833{
2834#ifdef CONFIG_X86_64
2835 gva = (long long)(gva << 16) >> 16;
2836#endif
2837 return gva;
2838}
2839
2840static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2841 gva_t va, int level)
2842{
2843 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2844 int i;
2845 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2846
2847 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2848 u64 ent = pt[i];
2849
Avi Kivityc7addb92007-09-16 18:58:32 +02002850 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002851 continue;
2852
2853 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002854 if (level > 1) {
2855 if (ent == shadow_notrap_nonpresent_pte)
2856 printk(KERN_ERR "audit: (%s) nontrapping pte"
2857 " in nonleaf level: levels %d gva %lx"
2858 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002859 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002860
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002861 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002862 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002863 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002864 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002865
Avi Kivityc7addb92007-09-16 18:58:32 +02002866 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002867 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002868 printk(KERN_ERR "xx audit error: (%s) levels %d"
2869 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002870 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002871 va, gpa, hpa, ent,
2872 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002873 else if (ent == shadow_notrap_nonpresent_pte
2874 && !is_error_hpa(hpa))
2875 printk(KERN_ERR "audit: (%s) notrap shadow,"
2876 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002877 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002878
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002879 }
2880 }
2881}
2882
2883static void audit_mappings(struct kvm_vcpu *vcpu)
2884{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002885 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002886
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002887 if (vcpu->arch.mmu.root_level == 4)
2888 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002889 else
2890 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002891 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002892 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002893 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002894 i << 30,
2895 2);
2896}
2897
2898static int count_rmaps(struct kvm_vcpu *vcpu)
2899{
2900 int nmaps = 0;
2901 int i, j, k;
2902
2903 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2904 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2905 struct kvm_rmap_desc *d;
2906
2907 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002908 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002909
Izik Eidus290fc382007-09-27 14:11:22 +02002910 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002911 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002912 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002913 ++nmaps;
2914 continue;
2915 }
Izik Eidus290fc382007-09-27 14:11:22 +02002916 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002917 while (d) {
2918 for (k = 0; k < RMAP_EXT; ++k)
2919 if (d->shadow_ptes[k])
2920 ++nmaps;
2921 else
2922 break;
2923 d = d->more;
2924 }
2925 }
2926 }
2927 return nmaps;
2928}
2929
2930static int count_writable_mappings(struct kvm_vcpu *vcpu)
2931{
2932 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002933 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002934 int i;
2935
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002936 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002937 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002938
Avi Kivity4db35312007-11-21 15:28:32 +02002939 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002940 continue;
2941
2942 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2943 u64 ent = pt[i];
2944
2945 if (!(ent & PT_PRESENT_MASK))
2946 continue;
2947 if (!(ent & PT_WRITABLE_MASK))
2948 continue;
2949 ++nmaps;
2950 }
2951 }
2952 return nmaps;
2953}
2954
2955static void audit_rmap(struct kvm_vcpu *vcpu)
2956{
2957 int n_rmap = count_rmaps(vcpu);
2958 int n_actual = count_writable_mappings(vcpu);
2959
2960 if (n_rmap != n_actual)
2961 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002962 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002963}
2964
2965static void audit_write_protection(struct kvm_vcpu *vcpu)
2966{
Avi Kivity4db35312007-11-21 15:28:32 +02002967 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002968 struct kvm_memory_slot *slot;
2969 unsigned long *rmapp;
2970 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002971
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002972 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002973 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002974 continue;
2975
Avi Kivity4db35312007-11-21 15:28:32 +02002976 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2977 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002978 rmapp = &slot->rmap[gfn - slot->base_gfn];
2979 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002980 printk(KERN_ERR "%s: (%s) shadow page has writable"
2981 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002982 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002983 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002984 }
2985}
2986
2987static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2988{
2989 int olddbg = dbg;
2990
2991 dbg = 0;
2992 audit_msg = msg;
2993 audit_rmap(vcpu);
2994 audit_write_protection(vcpu);
2995 audit_mappings(vcpu);
2996 dbg = olddbg;
2997}
2998
2999#endif