blob: 99c239c5c0ac7becff901a5bbdd399af4ec48293 [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;
Avi Kivityc7addb92007-09-16 18:58:32 +0200171
172void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
173{
174 shadow_trap_nonpresent_pte = trap_pte;
175 shadow_notrap_nonpresent_pte = notrap_pte;
176}
177EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
178
Sheng Yang7b523452008-04-25 21:13:50 +0800179void kvm_mmu_set_base_ptes(u64 base_pte)
180{
181 shadow_base_present_pte = base_pte;
182}
183EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
184
185void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
186 u64 dirty_mask, u64 nx_mask, u64 x_mask)
187{
188 shadow_user_mask = user_mask;
189 shadow_accessed_mask = accessed_mask;
190 shadow_dirty_mask = dirty_mask;
191 shadow_nx_mask = nx_mask;
192 shadow_x_mask = x_mask;
193}
194EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
195
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196static int is_write_protection(struct kvm_vcpu *vcpu)
197{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800198 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199}
200
201static int is_cpuid_PSE36(void)
202{
203 return 1;
204}
205
Avi Kivity73b10872007-01-26 00:56:41 -0800206static int is_nx(struct kvm_vcpu *vcpu)
207{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800208 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800209}
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static int is_present_pte(unsigned long pte)
212{
213 return pte & PT_PRESENT_MASK;
214}
215
Avi Kivityc7addb92007-09-16 18:58:32 +0200216static int is_shadow_present_pte(u64 pte)
217{
Avi Kivityc7addb92007-09-16 18:58:32 +0200218 return pte != shadow_trap_nonpresent_pte
219 && pte != shadow_notrap_nonpresent_pte;
220}
221
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300222static int is_large_pte(u64 pte)
223{
224 return pte & PT_PAGE_SIZE_MASK;
225}
226
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227static int is_writeble_pte(unsigned long pte)
228{
229 return pte & PT_WRITABLE_MASK;
230}
231
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200232static int is_dirty_pte(unsigned long pte)
233{
Sheng Yang7b523452008-04-25 21:13:50 +0800234 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200235}
236
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800237static int is_rmap_pte(u64 pte)
238{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200239 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800240}
241
Anthony Liguori35149e22008-04-02 14:46:56 -0500242static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200243{
Anthony Liguori35149e22008-04-02 14:46:56 -0500244 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200245}
246
Avi Kivityda928522007-11-21 13:54:47 +0200247static gfn_t pse36_gfn_delta(u32 gpte)
248{
249 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
250
251 return (gpte & PT32_DIR_PSE36_MASK) << shift;
252}
253
Avi Kivitye663ee62007-05-31 15:46:04 +0300254static void set_shadow_pte(u64 *sptep, u64 spte)
255{
256#ifdef CONFIG_X86_64
257 set_64bit((unsigned long *)sptep, spte);
258#else
259 set_64bit((unsigned long long *)sptep, spte);
260#endif
261}
262
Avi Kivitye2dec932007-01-05 16:36:54 -0800263static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300264 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800265{
266 void *obj;
267
268 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800269 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800270 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300271 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800272 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800273 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800274 cache->objects[cache->nobjs++] = obj;
275 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800276 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800277}
278
279static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
280{
281 while (mc->nobjs)
282 kfree(mc->objects[--mc->nobjs]);
283}
284
Avi Kivityc1158e62007-07-20 08:18:27 +0300285static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300286 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300287{
288 struct page *page;
289
290 if (cache->nobjs >= min)
291 return 0;
292 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300293 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300294 if (!page)
295 return -ENOMEM;
296 set_page_private(page, 0);
297 cache->objects[cache->nobjs++] = page_address(page);
298 }
299 return 0;
300}
301
302static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
303{
304 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300305 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300306}
307
Avi Kivity8c438502007-04-16 11:53:17 +0300308static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
309{
310 int r;
311
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800312 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300313 pte_chain_cache, 4);
314 if (r)
315 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800316 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300317 rmap_desc_cache, 1);
318 if (r)
319 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800320 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300321 if (r)
322 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800323 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 mmu_page_header_cache, 4);
325out:
Avi Kivity8c438502007-04-16 11:53:17 +0300326 return r;
327}
328
Avi Kivity714b93d2007-01-05 16:36:53 -0800329static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
330{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800331 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
332 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
333 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
334 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800335}
336
337static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
338 size_t size)
339{
340 void *p;
341
342 BUG_ON(!mc->nobjs);
343 p = mc->objects[--mc->nobjs];
344 memset(p, 0, size);
345 return p;
346}
347
Avi Kivity714b93d2007-01-05 16:36:53 -0800348static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
349{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800351 sizeof(struct kvm_pte_chain));
352}
353
Avi Kivity90cb0522007-07-17 13:04:56 +0300354static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800355{
Avi Kivity90cb0522007-07-17 13:04:56 +0300356 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800357}
358
359static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
360{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800361 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 sizeof(struct kvm_rmap_desc));
363}
364
Avi Kivity90cb0522007-07-17 13:04:56 +0300365static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800366{
Avi Kivity90cb0522007-07-17 13:04:56 +0300367 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800368}
369
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300371 * Return the pointer to the largepage write count for a given
372 * gfn, handling slots that are not large page aligned.
373 */
374static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
375{
376 unsigned long idx;
377
378 idx = (gfn / KVM_PAGES_PER_HPAGE) -
379 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
380 return &slot->lpage_info[idx].write_count;
381}
382
383static void account_shadowed(struct kvm *kvm, gfn_t gfn)
384{
385 int *write_count;
386
387 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
388 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300389}
390
391static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
392{
393 int *write_count;
394
395 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
396 *write_count -= 1;
397 WARN_ON(*write_count < 0);
398}
399
400static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
401{
402 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
403 int *largepage_idx;
404
405 if (slot) {
406 largepage_idx = slot_largepage_idx(gfn, slot);
407 return *largepage_idx;
408 }
409
410 return 1;
411}
412
413static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
414{
415 struct vm_area_struct *vma;
416 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300417 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418
419 addr = gfn_to_hva(kvm, gfn);
420 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300421 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300422
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300423 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300424 vma = find_vma(current->mm, addr);
425 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300426 ret = 1;
427 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300428
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300429 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300430}
431
432static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
433{
434 struct kvm_memory_slot *slot;
435
436 if (has_wrprotected_page(vcpu->kvm, large_gfn))
437 return 0;
438
439 if (!host_largepage_backed(vcpu->kvm, large_gfn))
440 return 0;
441
442 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
443 if (slot && slot->dirty_bitmap)
444 return 0;
445
446 return 1;
447}
448
449/*
Izik Eidus290fc382007-09-27 14:11:22 +0200450 * Take gfn and return the reverse mapping to it.
451 * Note: gfn must be unaliased before this function get called
452 */
453
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300454static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200455{
456 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300457 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200458
459 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300460 if (!lpage)
461 return &slot->rmap[gfn - slot->base_gfn];
462
463 idx = (gfn / KVM_PAGES_PER_HPAGE) -
464 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
465
466 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200467}
468
469/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800470 * Reverse mapping data structures:
471 *
Izik Eidus290fc382007-09-27 14:11:22 +0200472 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
473 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800474 *
Izik Eidus290fc382007-09-27 14:11:22 +0200475 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
476 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800477 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300478static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800479{
Avi Kivity4db35312007-11-21 15:28:32 +0200480 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800481 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200482 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800483 int i;
484
485 if (!is_rmap_pte(*spte))
486 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200487 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200488 sp = page_header(__pa(spte));
489 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300490 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200491 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800492 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200493 *rmapp = (unsigned long)spte;
494 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800495 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800496 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200497 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200499 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 } else {
501 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200502 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800503 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
504 desc = desc->more;
505 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800506 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800507 desc = desc->more;
508 }
509 for (i = 0; desc->shadow_ptes[i]; ++i)
510 ;
511 desc->shadow_ptes[i] = spte;
512 }
513}
514
Izik Eidus290fc382007-09-27 14:11:22 +0200515static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800516 struct kvm_rmap_desc *desc,
517 int i,
518 struct kvm_rmap_desc *prev_desc)
519{
520 int j;
521
522 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
523 ;
524 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000525 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800526 if (j != 0)
527 return;
528 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200529 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800530 else
531 if (prev_desc)
532 prev_desc->more = desc->more;
533 else
Izik Eidus290fc382007-09-27 14:11:22 +0200534 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300535 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800536}
537
Izik Eidus290fc382007-09-27 14:11:22 +0200538static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800539{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800540 struct kvm_rmap_desc *desc;
541 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200542 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500543 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200544 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800545 int i;
546
547 if (!is_rmap_pte(*spte))
548 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200549 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500550 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800551 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500552 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200553 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500554 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200555 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500556 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300557 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200558 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
560 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200561 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200563 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800564 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
565 spte, *spte);
566 BUG();
567 }
Izik Eidus290fc382007-09-27 14:11:22 +0200568 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800569 } else {
570 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200571 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 prev_desc = NULL;
573 while (desc) {
574 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
575 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200576 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800577 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800578 prev_desc);
579 return;
580 }
581 prev_desc = desc;
582 desc = desc->more;
583 }
584 BUG();
585 }
586}
587
Izik Eidus98348e92007-10-16 14:42:30 +0200588static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800589{
Avi Kivity374cbac2007-01-05 16:36:43 -0800590 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200591 struct kvm_rmap_desc *prev_desc;
592 u64 *prev_spte;
593 int i;
594
595 if (!*rmapp)
596 return NULL;
597 else if (!(*rmapp & 1)) {
598 if (!spte)
599 return (u64 *)*rmapp;
600 return NULL;
601 }
602 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
603 prev_desc = NULL;
604 prev_spte = NULL;
605 while (desc) {
606 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
607 if (prev_spte == spte)
608 return desc->shadow_ptes[i];
609 prev_spte = desc->shadow_ptes[i];
610 }
611 desc = desc->more;
612 }
613 return NULL;
614}
615
616static void rmap_write_protect(struct kvm *kvm, u64 gfn)
617{
Izik Eidus290fc382007-09-27 14:11:22 +0200618 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800619 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800620 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800621
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500622 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300623 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800624
Izik Eidus98348e92007-10-16 14:42:30 +0200625 spte = rmap_next(kvm, rmapp, NULL);
626 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800627 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800628 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800629 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800630 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200631 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800632 write_protected = 1;
633 }
Izik Eidus9647c142007-10-16 14:43:46 +0200634 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800635 }
Izik Eidus855149a2008-03-20 18:17:24 +0200636 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500637 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200638
639 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500640 pfn = spte_to_pfn(*spte);
641 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200642 }
643
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300644 /* check for huge page mappings */
645 rmapp = gfn_to_rmap(kvm, gfn, 1);
646 spte = rmap_next(kvm, rmapp, NULL);
647 while (spte) {
648 BUG_ON(!spte);
649 BUG_ON(!(*spte & PT_PRESENT_MASK));
650 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
651 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
652 if (is_writeble_pte(*spte)) {
653 rmap_remove(kvm, spte);
654 --kvm->stat.lpages;
655 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300656 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300657 write_protected = 1;
658 }
659 spte = rmap_next(kvm, rmapp, spte);
660 }
661
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800662 if (write_protected)
663 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800664}
665
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200666static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
667{
668 u64 *spte;
669 int need_tlb_flush = 0;
670
671 while ((spte = rmap_next(kvm, rmapp, NULL))) {
672 BUG_ON(!(*spte & PT_PRESENT_MASK));
673 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
674 rmap_remove(kvm, spte);
675 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
676 need_tlb_flush = 1;
677 }
678 return need_tlb_flush;
679}
680
681static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
682 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
683{
684 int i;
685 int retval = 0;
686
687 /*
688 * If mmap_sem isn't taken, we can look the memslots with only
689 * the mmu_lock by skipping over the slots with userspace_addr == 0.
690 */
691 for (i = 0; i < kvm->nmemslots; i++) {
692 struct kvm_memory_slot *memslot = &kvm->memslots[i];
693 unsigned long start = memslot->userspace_addr;
694 unsigned long end;
695
696 /* mmu_lock protects userspace_addr */
697 if (!start)
698 continue;
699
700 end = start + (memslot->npages << PAGE_SHIFT);
701 if (hva >= start && hva < end) {
702 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
703 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
704 retval |= handler(kvm,
705 &memslot->lpage_info[
706 gfn_offset /
707 KVM_PAGES_PER_HPAGE].rmap_pde);
708 }
709 }
710
711 return retval;
712}
713
714int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
715{
716 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
717}
718
719static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
720{
721 u64 *spte;
722 int young = 0;
723
Sheng Yang534e38b2008-09-08 15:12:30 +0800724 /* always return old for EPT */
725 if (!shadow_accessed_mask)
726 return 0;
727
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200728 spte = rmap_next(kvm, rmapp, NULL);
729 while (spte) {
730 int _young;
731 u64 _spte = *spte;
732 BUG_ON(!(_spte & PT_PRESENT_MASK));
733 _young = _spte & PT_ACCESSED_MASK;
734 if (_young) {
735 young = 1;
736 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
737 }
738 spte = rmap_next(kvm, rmapp, spte);
739 }
740 return young;
741}
742
743int kvm_age_hva(struct kvm *kvm, unsigned long hva)
744{
745 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
746}
747
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800748#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300749static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750{
Avi Kivity139bdb22007-01-05 16:36:50 -0800751 u64 *pos;
752 u64 *end;
753
Avi Kivity47ad8e62007-05-06 15:50:58 +0300754 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300755 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800756 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800757 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800759 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 return 1;
761}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800762#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Avi Kivity4db35312007-11-21 15:28:32 +0200764static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800765{
Avi Kivity4db35312007-11-21 15:28:32 +0200766 ASSERT(is_empty_shadow_page(sp->spt));
767 list_del(&sp->link);
768 __free_page(virt_to_page(sp->spt));
769 __free_page(virt_to_page(sp->gfns));
770 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800771 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800772}
773
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800774static unsigned kvm_page_table_hashfn(gfn_t gfn)
775{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200776 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800777}
778
Avi Kivity25c0de22007-01-05 16:36:42 -0800779static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
780 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781{
Avi Kivity4db35312007-11-21 15:28:32 +0200782 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800784 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
785 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
786 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200787 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800788 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200789 ASSERT(is_empty_shadow_page(sp->spt));
790 sp->slot_bitmap = 0;
791 sp->multimapped = 0;
792 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800793 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200794 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795}
796
Avi Kivity714b93d2007-01-05 16:36:53 -0800797static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200798 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800799{
800 struct kvm_pte_chain *pte_chain;
801 struct hlist_node *node;
802 int i;
803
804 if (!parent_pte)
805 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200806 if (!sp->multimapped) {
807 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800808
809 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200810 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800811 return;
812 }
Avi Kivity4db35312007-11-21 15:28:32 +0200813 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800814 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200815 INIT_HLIST_HEAD(&sp->parent_ptes);
816 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800817 pte_chain->parent_ptes[0] = old;
818 }
Avi Kivity4db35312007-11-21 15:28:32 +0200819 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800820 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
821 continue;
822 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
823 if (!pte_chain->parent_ptes[i]) {
824 pte_chain->parent_ptes[i] = parent_pte;
825 return;
826 }
827 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800828 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800829 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200830 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800831 pte_chain->parent_ptes[0] = parent_pte;
832}
833
Avi Kivity4db35312007-11-21 15:28:32 +0200834static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835 u64 *parent_pte)
836{
837 struct kvm_pte_chain *pte_chain;
838 struct hlist_node *node;
839 int i;
840
Avi Kivity4db35312007-11-21 15:28:32 +0200841 if (!sp->multimapped) {
842 BUG_ON(sp->parent_pte != parent_pte);
843 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800844 return;
845 }
Avi Kivity4db35312007-11-21 15:28:32 +0200846 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800847 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
848 if (!pte_chain->parent_ptes[i])
849 break;
850 if (pte_chain->parent_ptes[i] != parent_pte)
851 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800852 while (i + 1 < NR_PTE_CHAIN_ENTRIES
853 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800854 pte_chain->parent_ptes[i]
855 = pte_chain->parent_ptes[i + 1];
856 ++i;
857 }
858 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800859 if (i == 0) {
860 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300861 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200862 if (hlist_empty(&sp->parent_ptes)) {
863 sp->multimapped = 0;
864 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800865 }
866 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800867 return;
868 }
869 BUG();
870}
871
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300872
873static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
874 mmu_parent_walk_fn fn)
875{
876 struct kvm_pte_chain *pte_chain;
877 struct hlist_node *node;
878 struct kvm_mmu_page *parent_sp;
879 int i;
880
881 if (!sp->multimapped && sp->parent_pte) {
882 parent_sp = page_header(__pa(sp->parent_pte));
883 fn(vcpu, parent_sp);
884 mmu_parent_walk(vcpu, parent_sp, fn);
885 return;
886 }
887 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
888 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
889 if (!pte_chain->parent_ptes[i])
890 break;
891 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
892 fn(vcpu, parent_sp);
893 mmu_parent_walk(vcpu, parent_sp, fn);
894 }
895}
896
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300897static void kvm_mmu_update_unsync_bitmap(u64 *spte)
898{
899 unsigned int index;
900 struct kvm_mmu_page *sp = page_header(__pa(spte));
901
902 index = spte - sp->spt;
903 __set_bit(index, sp->unsync_child_bitmap);
904 sp->unsync_children = 1;
905}
906
907static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
908{
909 struct kvm_pte_chain *pte_chain;
910 struct hlist_node *node;
911 int i;
912
913 if (!sp->parent_pte)
914 return;
915
916 if (!sp->multimapped) {
917 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
918 return;
919 }
920
921 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
922 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
923 if (!pte_chain->parent_ptes[i])
924 break;
925 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
926 }
927}
928
929static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
930{
931 sp->unsync_children = 1;
932 kvm_mmu_update_parents_unsync(sp);
933 return 1;
934}
935
936static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
937 struct kvm_mmu_page *sp)
938{
939 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
940 kvm_mmu_update_parents_unsync(sp);
941}
942
Avi Kivityd761a502008-05-29 14:55:03 +0300943static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
944 struct kvm_mmu_page *sp)
945{
946 int i;
947
948 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
949 sp->spt[i] = shadow_trap_nonpresent_pte;
950}
951
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300952static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
953 struct kvm_mmu_page *sp)
954{
955 return 1;
956}
957
Marcelo Tosattia7052892008-09-23 13:18:35 -0300958static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
959{
960}
961
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300962#define for_each_unsync_children(bitmap, idx) \
963 for (idx = find_first_bit(bitmap, 512); \
964 idx < 512; \
965 idx = find_next_bit(bitmap, 512, idx+1))
966
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300967static int mmu_unsync_walk(struct kvm_mmu_page *sp,
968 struct kvm_unsync_walk *walker)
969{
970 int i, ret;
971
972 if (!sp->unsync_children)
973 return 0;
974
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300975 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300976 u64 ent = sp->spt[i];
977
978 if (is_shadow_present_pte(ent)) {
979 struct kvm_mmu_page *child;
980 child = page_header(ent & PT64_BASE_ADDR_MASK);
981
982 if (child->unsync_children) {
983 ret = mmu_unsync_walk(child, walker);
984 if (ret)
985 return ret;
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300986 __clear_bit(i, sp->unsync_child_bitmap);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300987 }
988
989 if (child->unsync) {
990 ret = walker->entry(child, walker);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300991 __clear_bit(i, sp->unsync_child_bitmap);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300992 if (ret)
993 return ret;
994 }
995 }
996 }
997
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300998 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300999 sp->unsync_children = 0;
1000
1001 return 0;
1002}
1003
Avi Kivity4db35312007-11-21 15:28:32 +02001004static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001005{
1006 unsigned index;
1007 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001008 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001009 struct hlist_node *node;
1010
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001011 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001012 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001013 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001014 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001015 if (sp->gfn == gfn && !sp->role.metaphysical
1016 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001017 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001018 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001019 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001020 }
1021 return NULL;
1022}
1023
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001024static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1025{
1026 WARN_ON(!sp->unsync);
1027 sp->unsync = 0;
1028 --kvm->stat.mmu_unsync;
1029}
1030
1031static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1032
1033static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1034{
1035 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1036 kvm_mmu_zap_page(vcpu->kvm, sp);
1037 return 1;
1038 }
1039
1040 rmap_write_protect(vcpu->kvm, sp->gfn);
1041 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1042 kvm_mmu_zap_page(vcpu->kvm, sp);
1043 return 1;
1044 }
1045
1046 kvm_mmu_flush_tlb(vcpu);
1047 kvm_unlink_unsync_page(vcpu->kvm, sp);
1048 return 0;
1049}
1050
1051struct sync_walker {
1052 struct kvm_vcpu *vcpu;
1053 struct kvm_unsync_walk walker;
1054};
1055
1056static int mmu_sync_fn(struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk)
1057{
1058 struct sync_walker *sync_walk = container_of(walk, struct sync_walker,
1059 walker);
1060 struct kvm_vcpu *vcpu = sync_walk->vcpu;
1061
1062 kvm_sync_page(vcpu, sp);
1063 return (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock));
1064}
1065
1066static void mmu_sync_children(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1067{
1068 struct sync_walker walker = {
1069 .walker = { .entry = mmu_sync_fn, },
1070 .vcpu = vcpu,
1071 };
1072
1073 while (mmu_unsync_walk(sp, &walker.walker))
1074 cond_resched_lock(&vcpu->kvm->mmu_lock);
1075}
1076
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001077static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1078 gfn_t gfn,
1079 gva_t gaddr,
1080 unsigned level,
1081 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +02001082 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001083 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001084{
1085 union kvm_mmu_page_role role;
1086 unsigned index;
1087 unsigned quadrant;
1088 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001089 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001090 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001091
1092 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001093 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001094 role.level = level;
1095 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +02001096 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001097 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001098 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1099 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1100 role.quadrant = quadrant;
1101 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001102 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001103 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001104 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001105 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001106 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1107 if (sp->gfn == gfn) {
1108 if (sp->unsync)
1109 if (kvm_sync_page(vcpu, sp))
1110 continue;
1111
1112 if (sp->role.word != role.word)
1113 continue;
1114
Avi Kivity4db35312007-11-21 15:28:32 +02001115 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001116 if (sp->unsync_children) {
1117 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1118 kvm_mmu_mark_parents_unsync(vcpu, sp);
1119 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001120 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001121 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001122 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001123 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001124 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1125 if (!sp)
1126 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001127 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001128 sp->gfn = gfn;
1129 sp->role = role;
1130 hlist_add_head(&sp->hash_link, bucket);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001131 if (!metaphysical) {
Anthony Liguori4a4c9922007-10-10 20:08:41 -05001132 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001133 account_shadowed(vcpu->kvm, gfn);
1134 }
Avi Kivity131d8272008-05-29 14:56:28 +03001135 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1136 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1137 else
1138 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001139 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001140}
1141
Avi Kivity3d000db2008-08-22 19:24:38 +03001142static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001143 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +03001144{
1145 hpa_t shadow_addr;
1146 int level;
1147 int r;
1148 u64 *sptep;
1149 unsigned index;
1150
1151 shadow_addr = vcpu->arch.mmu.root_hpa;
1152 level = vcpu->arch.mmu.shadow_root_level;
1153 if (level == PT32E_ROOT_LEVEL) {
1154 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1155 shadow_addr &= PT64_BASE_ADDR_MASK;
1156 --level;
1157 }
1158
1159 while (level >= PT_PAGE_TABLE_LEVEL) {
1160 index = SHADOW_PT_INDEX(addr, level);
1161 sptep = ((u64 *)__va(shadow_addr)) + index;
1162 r = walker->entry(walker, vcpu, addr, sptep, level);
1163 if (r)
1164 return r;
1165 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1166 --level;
1167 }
1168 return 0;
1169}
1170
Avi Kivity90cb0522007-07-17 13:04:56 +03001171static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001172 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001173{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001174 unsigned i;
1175 u64 *pt;
1176 u64 ent;
1177
Avi Kivity4db35312007-11-21 15:28:32 +02001178 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001179
Avi Kivity4db35312007-11-21 15:28:32 +02001180 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001181 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001182 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001183 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001184 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001185 }
1186 return;
1187 }
1188
1189 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1190 ent = pt[i];
1191
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001192 if (is_shadow_present_pte(ent)) {
1193 if (!is_large_pte(ent)) {
1194 ent &= PT64_BASE_ADDR_MASK;
1195 mmu_page_remove_parent_pte(page_header(ent),
1196 &pt[i]);
1197 } else {
1198 --kvm->stat.lpages;
1199 rmap_remove(kvm, &pt[i]);
1200 }
1201 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001202 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001203 }
Avi Kivitya4360362007-01-05 16:36:45 -08001204}
1205
Avi Kivity4db35312007-11-21 15:28:32 +02001206static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001207{
Avi Kivity4db35312007-11-21 15:28:32 +02001208 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001209}
1210
Avi Kivity12b7d282007-09-23 14:10:49 +02001211static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1212{
1213 int i;
1214
1215 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1216 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001217 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001218}
1219
Avi Kivity31aa2b42008-07-11 17:59:46 +03001220static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001221{
1222 u64 *parent_pte;
1223
Avi Kivity4db35312007-11-21 15:28:32 +02001224 while (sp->multimapped || sp->parent_pte) {
1225 if (!sp->multimapped)
1226 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001227 else {
1228 struct kvm_pte_chain *chain;
1229
Avi Kivity4db35312007-11-21 15:28:32 +02001230 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001231 struct kvm_pte_chain, link);
1232 parent_pte = chain->parent_ptes[0];
1233 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001234 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001235 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001236 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001237 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001238}
1239
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001240struct zap_walker {
1241 struct kvm_unsync_walk walker;
1242 struct kvm *kvm;
1243 int zapped;
1244};
1245
1246static int mmu_zap_fn(struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk)
1247{
1248 struct zap_walker *zap_walk = container_of(walk, struct zap_walker,
1249 walker);
1250 kvm_mmu_zap_page(zap_walk->kvm, sp);
1251 zap_walk->zapped = 1;
1252 return 0;
1253}
1254
1255static int mmu_zap_unsync_children(struct kvm *kvm, struct kvm_mmu_page *sp)
1256{
1257 struct zap_walker walker = {
1258 .walker = { .entry = mmu_zap_fn, },
1259 .kvm = kvm,
1260 .zapped = 0,
1261 };
1262
1263 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
1264 return 0;
1265 mmu_unsync_walk(sp, &walker.walker);
1266 return walker.zapped;
1267}
1268
Marcelo Tosatti07385412008-09-23 13:18:37 -03001269static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001270{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001271 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001272 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001273 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001274 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001275 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001276 kvm_flush_remote_tlbs(kvm);
1277 if (!sp->role.invalid && !sp->role.metaphysical)
1278 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001279 if (sp->unsync)
1280 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001281 if (!sp->root_count) {
1282 hlist_del(&sp->hash_link);
1283 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001284 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001285 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001286 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001287 kvm_reload_remote_mmus(kvm);
1288 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001289 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001290 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001291}
1292
Izik Eidus82ce2c92007-10-02 18:52:55 +02001293/*
1294 * Changing the number of mmu pages allocated to the vm
1295 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1296 */
1297void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1298{
1299 /*
1300 * If we set the number of mmu pages to be smaller be than the
1301 * number of actived pages , we must to free some mmu pages before we
1302 * change the value
1303 */
1304
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001305 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001306 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001307 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1308 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001309
1310 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1311 struct kvm_mmu_page *page;
1312
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001313 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001314 struct kvm_mmu_page, link);
1315 kvm_mmu_zap_page(kvm, page);
1316 n_used_mmu_pages--;
1317 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001318 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001319 }
1320 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001321 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1322 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001323
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001324 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001325}
1326
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001327static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001328{
1329 unsigned index;
1330 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001331 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001332 struct hlist_node *node, *n;
1333 int r;
1334
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001335 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001336 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001337 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001338 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001339 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1340 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001341 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001342 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001343 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001344 if (kvm_mmu_zap_page(kvm, sp))
1345 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001346 }
1347 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001348}
1349
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001350static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001351{
Avi Kivity4db35312007-11-21 15:28:32 +02001352 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001353
Avi Kivity4db35312007-11-21 15:28:32 +02001354 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001355 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001356 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001357 }
1358}
1359
Avi Kivity38c335f2007-11-21 14:20:22 +02001360static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361{
Avi Kivity38c335f2007-11-21 14:20:22 +02001362 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001363 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364
Avi Kivity4db35312007-11-21 15:28:32 +02001365 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001368static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1369{
1370 int i;
1371 u64 *pt = sp->spt;
1372
1373 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1374 return;
1375
1376 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1377 if (pt[i] == shadow_notrap_nonpresent_pte)
1378 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1379 }
1380}
1381
Avi Kivity039576c2007-03-20 12:46:50 +02001382struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1383{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001384 struct page *page;
1385
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001386 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001387
1388 if (gpa == UNMAPPED_GVA)
1389 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001390
Izik Eidus72dc67a2008-02-10 18:04:15 +02001391 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001392
1393 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001394}
1395
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001396static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1397{
1398 unsigned index;
1399 struct hlist_head *bucket;
1400 struct kvm_mmu_page *s;
1401 struct hlist_node *node, *n;
1402
1403 index = kvm_page_table_hashfn(sp->gfn);
1404 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1405 /* don't unsync if pagetable is shadowed with multiple roles */
1406 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1407 if (s->gfn != sp->gfn || s->role.metaphysical)
1408 continue;
1409 if (s->role.word != sp->role.word)
1410 return 1;
1411 }
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001412 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001413 ++vcpu->kvm->stat.mmu_unsync;
1414 sp->unsync = 1;
1415 mmu_convert_notrap(sp);
1416 return 0;
1417}
1418
1419static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1420 bool can_unsync)
1421{
1422 struct kvm_mmu_page *shadow;
1423
1424 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1425 if (shadow) {
1426 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1427 return 1;
1428 if (shadow->unsync)
1429 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001430 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001431 return kvm_unsync_page(vcpu, shadow);
1432 return 1;
1433 }
1434 return 0;
1435}
1436
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001437static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1438 unsigned pte_access, int user_fault,
1439 int write_fault, int dirty, int largepage,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001440 gfn_t gfn, pfn_t pfn, bool speculative,
1441 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001442{
1443 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001444 int ret = 0;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001445 /*
1446 * We don't set the accessed bit, since we sometimes want to see
1447 * whether the guest actually used the pte (in order to detect
1448 * demand paging).
1449 */
Sheng Yang7b523452008-04-25 21:13:50 +08001450 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001451 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001452 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001453 if (!dirty)
1454 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001455 if (pte_access & ACC_EXEC_MASK)
1456 spte |= shadow_x_mask;
1457 else
1458 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001459 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001460 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001461 if (largepage)
1462 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001463
Anthony Liguori35149e22008-04-02 14:46:56 -05001464 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001465
1466 if ((pte_access & ACC_WRITE_MASK)
1467 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001468
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001469 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1470 ret = 1;
1471 spte = shadow_trap_nonpresent_pte;
1472 goto set_pte;
1473 }
1474
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001475 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001476
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001477 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001478 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001479 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001480 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001481 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001482 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001483 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001484 }
1485 }
1486
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001487 if (pte_access & ACC_WRITE_MASK)
1488 mark_page_dirty(vcpu->kvm, gfn);
1489
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001490set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001491 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001492 return ret;
1493}
1494
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001495static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1496 unsigned pt_access, unsigned pte_access,
1497 int user_fault, int write_fault, int dirty,
1498 int *ptwrite, int largepage, gfn_t gfn,
1499 pfn_t pfn, bool speculative)
1500{
1501 int was_rmapped = 0;
1502 int was_writeble = is_writeble_pte(*shadow_pte);
1503
1504 pgprintk("%s: spte %llx access %x write_fault %d"
1505 " user_fault %d gfn %lx\n",
1506 __func__, *shadow_pte, pt_access,
1507 write_fault, user_fault, gfn);
1508
1509 if (is_rmap_pte(*shadow_pte)) {
1510 /*
1511 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1512 * the parent of the now unreachable PTE.
1513 */
1514 if (largepage && !is_large_pte(*shadow_pte)) {
1515 struct kvm_mmu_page *child;
1516 u64 pte = *shadow_pte;
1517
1518 child = page_header(pte & PT64_BASE_ADDR_MASK);
1519 mmu_page_remove_parent_pte(child, shadow_pte);
1520 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1521 pgprintk("hfn old %lx new %lx\n",
1522 spte_to_pfn(*shadow_pte), pfn);
1523 rmap_remove(vcpu->kvm, shadow_pte);
1524 } else {
1525 if (largepage)
1526 was_rmapped = is_large_pte(*shadow_pte);
1527 else
1528 was_rmapped = 1;
1529 }
1530 }
1531 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001532 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001533 if (write_fault)
1534 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001535 kvm_x86_ops->tlb_flush(vcpu);
1536 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001537
1538 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1539 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1540 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1541 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1542 *shadow_pte, shadow_pte);
1543 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001544 ++vcpu->kvm->stat.lpages;
1545
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001546 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1547 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001548 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001549 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001550 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001551 } else {
1552 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001553 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001554 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001555 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001556 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001557 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001558 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001559 vcpu->arch.last_pte_gfn = gfn;
1560 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001561}
1562
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1564{
1565}
1566
Avi Kivity140754b2008-08-22 19:28:04 +03001567struct direct_shadow_walk {
1568 struct kvm_shadow_walk walker;
1569 pfn_t pfn;
1570 int write;
1571 int largepage;
1572 int pt_write;
1573};
1574
1575static int direct_map_entry(struct kvm_shadow_walk *_walk,
1576 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001577 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001578{
1579 struct direct_shadow_walk *walk =
1580 container_of(_walk, struct direct_shadow_walk, walker);
1581 struct kvm_mmu_page *sp;
1582 gfn_t pseudo_gfn;
1583 gfn_t gfn = addr >> PAGE_SHIFT;
1584
1585 if (level == PT_PAGE_TABLE_LEVEL
1586 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1587 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1588 0, walk->write, 1, &walk->pt_write,
1589 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001590 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001591 return 1;
1592 }
1593
1594 if (*sptep == shadow_trap_nonpresent_pte) {
1595 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001596 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001597 1, ACC_ALL, sptep);
1598 if (!sp) {
1599 pgprintk("nonpaging_map: ENOMEM\n");
1600 kvm_release_pfn_clean(walk->pfn);
1601 return -ENOMEM;
1602 }
1603
1604 set_shadow_pte(sptep,
1605 __pa(sp->spt)
1606 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1607 | shadow_user_mask | shadow_x_mask);
1608 }
1609 return 0;
1610}
1611
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001612static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001613 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614{
Avi Kivity140754b2008-08-22 19:28:04 +03001615 int r;
1616 struct direct_shadow_walk walker = {
1617 .walker = { .entry = direct_map_entry, },
1618 .pfn = pfn,
1619 .largepage = largepage,
1620 .write = write,
1621 .pt_write = 0,
1622 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001624 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001625 if (r < 0)
1626 return r;
1627 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628}
1629
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001630static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1631{
1632 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001633 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001634 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001635 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001636
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001637 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1638 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1639 largepage = 1;
1640 }
1641
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001642 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001643 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001644 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001645
Avi Kivityd196e342008-01-24 11:44:11 +02001646 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001647 if (is_error_pfn(pfn)) {
1648 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001649 return 1;
1650 }
1651
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001652 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001653 if (mmu_notifier_retry(vcpu, mmu_seq))
1654 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001655 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001656 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001657 spin_unlock(&vcpu->kvm->mmu_lock);
1658
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001659
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001660 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001661
1662out_unlock:
1663 spin_unlock(&vcpu->kvm->mmu_lock);
1664 kvm_release_pfn_clean(pfn);
1665 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001666}
1667
1668
Avi Kivity17ac10a2007-01-05 16:36:40 -08001669static void mmu_free_roots(struct kvm_vcpu *vcpu)
1670{
1671 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001672 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001673
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001674 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001675 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001676 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001677 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1678 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001679
Avi Kivity4db35312007-11-21 15:28:32 +02001680 sp = page_header(root);
1681 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001682 if (!sp->root_count && sp->role.invalid)
1683 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001684 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001685 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001686 return;
1687 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001688 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001689 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001690
Avi Kivity417726a2007-04-12 17:35:58 +03001691 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001692 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001693 sp = page_header(root);
1694 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001695 if (!sp->root_count && sp->role.invalid)
1696 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001697 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001698 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001699 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001700 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001701 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001702}
1703
1704static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1705{
1706 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001707 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001708 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001709 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001710
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001711 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001712
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001713 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1714 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001715
1716 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001717 if (tdp_enabled)
1718 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001719 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001720 PT64_ROOT_LEVEL, metaphysical,
1721 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001722 root = __pa(sp->spt);
1723 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001724 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001725 return;
1726 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001727 metaphysical = !is_paging(vcpu);
1728 if (tdp_enabled)
1729 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001730 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001731 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001732
1733 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001734 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1735 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1736 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001737 continue;
1738 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001739 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1740 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001741 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001742 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001743 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001744 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001745 root = __pa(sp->spt);
1746 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001747 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001748 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001749 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001750}
1751
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001752static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1753{
1754 int i;
1755 struct kvm_mmu_page *sp;
1756
1757 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1758 return;
1759 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1760 hpa_t root = vcpu->arch.mmu.root_hpa;
1761 sp = page_header(root);
1762 mmu_sync_children(vcpu, sp);
1763 return;
1764 }
1765 for (i = 0; i < 4; ++i) {
1766 hpa_t root = vcpu->arch.mmu.pae_root[i];
1767
1768 if (root) {
1769 root &= PT64_BASE_ADDR_MASK;
1770 sp = page_header(root);
1771 mmu_sync_children(vcpu, sp);
1772 }
1773 }
1774}
1775
1776void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1777{
1778 spin_lock(&vcpu->kvm->mmu_lock);
1779 mmu_sync_roots(vcpu);
1780 spin_unlock(&vcpu->kvm->mmu_lock);
1781}
1782
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1784{
1785 return vaddr;
1786}
1787
1788static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001789 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790{
Avi Kivitye8332402007-12-09 18:43:00 +02001791 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001792 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001794 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001795 r = mmu_topup_memory_caches(vcpu);
1796 if (r)
1797 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001798
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001800 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801
Avi Kivitye8332402007-12-09 18:43:00 +02001802 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803
Avi Kivitye8332402007-12-09 18:43:00 +02001804 return nonpaging_map(vcpu, gva & PAGE_MASK,
1805 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806}
1807
Joerg Roedelfb72d162008-02-07 13:47:44 +01001808static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1809 u32 error_code)
1810{
Anthony Liguori35149e22008-04-02 14:46:56 -05001811 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001812 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001813 int largepage = 0;
1814 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001815 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001816
1817 ASSERT(vcpu);
1818 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1819
1820 r = mmu_topup_memory_caches(vcpu);
1821 if (r)
1822 return r;
1823
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001824 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1825 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1826 largepage = 1;
1827 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001828 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001829 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001830 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05001831 if (is_error_pfn(pfn)) {
1832 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001833 return 1;
1834 }
1835 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001836 if (mmu_notifier_retry(vcpu, mmu_seq))
1837 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001838 kvm_mmu_free_some_pages(vcpu);
1839 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03001840 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001841 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001842
1843 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001844
1845out_unlock:
1846 spin_unlock(&vcpu->kvm->mmu_lock);
1847 kvm_release_pfn_clean(pfn);
1848 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001849}
1850
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851static void nonpaging_free(struct kvm_vcpu *vcpu)
1852{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001853 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854}
1855
1856static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1857{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001858 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859
1860 context->new_cr3 = nonpaging_new_cr3;
1861 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 context->gva_to_gpa = nonpaging_gva_to_gpa;
1863 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001864 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001865 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001866 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001867 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001869 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 return 0;
1871}
1872
Avi Kivityd835dfe2007-11-21 02:57:59 +02001873void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001875 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001876 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877}
1878
1879static void paging_new_cr3(struct kvm_vcpu *vcpu)
1880{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001881 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001882 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883}
1884
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885static void inject_page_fault(struct kvm_vcpu *vcpu,
1886 u64 addr,
1887 u32 err_code)
1888{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001889 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890}
1891
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892static void paging_free(struct kvm_vcpu *vcpu)
1893{
1894 nonpaging_free(vcpu);
1895}
1896
1897#define PTTYPE 64
1898#include "paging_tmpl.h"
1899#undef PTTYPE
1900
1901#define PTTYPE 32
1902#include "paging_tmpl.h"
1903#undef PTTYPE
1904
Avi Kivity17ac10a2007-01-05 16:36:40 -08001905static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001907 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908
1909 ASSERT(is_pae(vcpu));
1910 context->new_cr3 = paging_new_cr3;
1911 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001913 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001914 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001915 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001917 context->root_level = level;
1918 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001919 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 return 0;
1921}
1922
Avi Kivity17ac10a2007-01-05 16:36:40 -08001923static int paging64_init_context(struct kvm_vcpu *vcpu)
1924{
1925 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1926}
1927
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928static int paging32_init_context(struct kvm_vcpu *vcpu)
1929{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001930 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931
1932 context->new_cr3 = paging_new_cr3;
1933 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934 context->gva_to_gpa = paging32_gva_to_gpa;
1935 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001936 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001937 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001938 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 context->root_level = PT32_ROOT_LEVEL;
1940 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001941 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 return 0;
1943}
1944
1945static int paging32E_init_context(struct kvm_vcpu *vcpu)
1946{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001947 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948}
1949
Joerg Roedelfb72d162008-02-07 13:47:44 +01001950static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1951{
1952 struct kvm_mmu *context = &vcpu->arch.mmu;
1953
1954 context->new_cr3 = nonpaging_new_cr3;
1955 context->page_fault = tdp_page_fault;
1956 context->free = nonpaging_free;
1957 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001958 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001959 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08001960 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01001961 context->root_hpa = INVALID_PAGE;
1962
1963 if (!is_paging(vcpu)) {
1964 context->gva_to_gpa = nonpaging_gva_to_gpa;
1965 context->root_level = 0;
1966 } else if (is_long_mode(vcpu)) {
1967 context->gva_to_gpa = paging64_gva_to_gpa;
1968 context->root_level = PT64_ROOT_LEVEL;
1969 } else if (is_pae(vcpu)) {
1970 context->gva_to_gpa = paging64_gva_to_gpa;
1971 context->root_level = PT32E_ROOT_LEVEL;
1972 } else {
1973 context->gva_to_gpa = paging32_gva_to_gpa;
1974 context->root_level = PT32_ROOT_LEVEL;
1975 }
1976
1977 return 0;
1978}
1979
1980static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981{
1982 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001983 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984
1985 if (!is_paging(vcpu))
1986 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001987 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 return paging64_init_context(vcpu);
1989 else if (is_pae(vcpu))
1990 return paging32E_init_context(vcpu);
1991 else
1992 return paging32_init_context(vcpu);
1993}
1994
Joerg Roedelfb72d162008-02-07 13:47:44 +01001995static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1996{
Anthony Liguori35149e22008-04-02 14:46:56 -05001997 vcpu->arch.update_pte.pfn = bad_pfn;
1998
Joerg Roedelfb72d162008-02-07 13:47:44 +01001999 if (tdp_enabled)
2000 return init_kvm_tdp_mmu(vcpu);
2001 else
2002 return init_kvm_softmmu(vcpu);
2003}
2004
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2006{
2007 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2009 vcpu->arch.mmu.free(vcpu);
2010 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 }
2012}
2013
2014int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2015{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002016 destroy_kvm_mmu(vcpu);
2017 return init_kvm_mmu(vcpu);
2018}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002019EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002020
2021int kvm_mmu_load(struct kvm_vcpu *vcpu)
2022{
Avi Kivity714b93d2007-01-05 16:36:53 -08002023 int r;
2024
Avi Kivitye2dec932007-01-05 16:36:54 -08002025 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002026 if (r)
2027 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002028 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002029 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002030 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002031 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002032 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002033 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002034 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002035out:
2036 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002038EXPORT_SYMBOL_GPL(kvm_mmu_load);
2039
2040void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2041{
2042 mmu_free_roots(vcpu);
2043}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044
Avi Kivity09072da2007-05-01 14:16:52 +03002045static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002046 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002047 u64 *spte)
2048{
2049 u64 pte;
2050 struct kvm_mmu_page *child;
2051
2052 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002053 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002054 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2055 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02002056 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002057 else {
2058 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002059 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002060 }
2061 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002062 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002063 if (is_large_pte(pte))
2064 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002065}
2066
Avi Kivity00284252007-05-01 16:53:31 +03002067static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002068 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002069 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002070 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002071{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002072 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2073 if (!vcpu->arch.update_pte.largepage ||
2074 sp->role.glevels == PT32_ROOT_LEVEL) {
2075 ++vcpu->kvm->stat.mmu_pde_zapped;
2076 return;
2077 }
2078 }
Avi Kivity00284252007-05-01 16:53:31 +03002079
Avi Kivity4cee5762007-11-18 16:37:07 +02002080 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002081 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002082 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002083 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002084 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002085}
2086
Avi Kivity79539ce2007-11-21 02:06:21 +02002087static bool need_remote_flush(u64 old, u64 new)
2088{
2089 if (!is_shadow_present_pte(old))
2090 return false;
2091 if (!is_shadow_present_pte(new))
2092 return true;
2093 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2094 return true;
2095 old ^= PT64_NX_MASK;
2096 new ^= PT64_NX_MASK;
2097 return (old & ~new & PT64_PERM_MASK) != 0;
2098}
2099
2100static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2101{
2102 if (need_remote_flush(old, new))
2103 kvm_flush_remote_tlbs(vcpu->kvm);
2104 else
2105 kvm_mmu_flush_tlb(vcpu);
2106}
2107
Avi Kivity12b7d282007-09-23 14:10:49 +02002108static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2109{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002111
Sheng Yang7b523452008-04-25 21:13:50 +08002112 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002113}
2114
Avi Kivityd7824ff2007-12-30 12:29:05 +02002115static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2116 const u8 *new, int bytes)
2117{
2118 gfn_t gfn;
2119 int r;
2120 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002121 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002122
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002123 vcpu->arch.update_pte.largepage = 0;
2124
Avi Kivityd7824ff2007-12-30 12:29:05 +02002125 if (bytes != 4 && bytes != 8)
2126 return;
2127
2128 /*
2129 * Assume that the pte write on a page table of the same type
2130 * as the current vcpu paging mode. This is nearly always true
2131 * (might be false while changing modes). Note it is verified later
2132 * by update_pte().
2133 */
2134 if (is_pae(vcpu)) {
2135 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2136 if ((bytes == 4) && (gpa % 4 == 0)) {
2137 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2138 if (r)
2139 return;
2140 memcpy((void *)&gpte + (gpa % 8), new, 4);
2141 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2142 memcpy((void *)&gpte, new, 8);
2143 }
2144 } else {
2145 if ((bytes == 4) && (gpa % 4 == 0))
2146 memcpy((void *)&gpte, new, 4);
2147 }
2148 if (!is_present_pte(gpte))
2149 return;
2150 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002151
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002152 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2153 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2154 vcpu->arch.update_pte.largepage = 1;
2155 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002156 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002157 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002158 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002159
Anthony Liguori35149e22008-04-02 14:46:56 -05002160 if (is_error_pfn(pfn)) {
2161 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002162 return;
2163 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002164 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002165 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002166}
2167
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002168static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2169{
2170 u64 *spte = vcpu->arch.last_pte_updated;
2171
2172 if (spte
2173 && vcpu->arch.last_pte_gfn == gfn
2174 && shadow_accessed_mask
2175 && !(*spte & shadow_accessed_mask)
2176 && is_shadow_present_pte(*spte))
2177 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2178}
2179
Avi Kivity09072da2007-05-01 14:16:52 +03002180void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08002181 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002182{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002183 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002184 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002185 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002186 struct hlist_head *bucket;
2187 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002188 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002189 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002190 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002191 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002192 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002193 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002194 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002195 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002196 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002197 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002198 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002199
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002200 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002201 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002202 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002203 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002204 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002205 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002206 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002207 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02002208 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002209 ++vcpu->arch.last_pt_write_count;
2210 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08002211 flooded = 1;
2212 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002213 vcpu->arch.last_pt_write_gfn = gfn;
2214 vcpu->arch.last_pt_write_count = 1;
2215 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002216 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002217 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002218 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002219 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002220 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002221 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002222 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002223 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002224 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002225 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002226 /*
2227 * Misaligned accesses are too much trouble to fix
2228 * up; also, they usually indicate a page is not used
2229 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002230 *
2231 * If we're seeing too many writes to a page,
2232 * it may no longer be a page table, or we may be
2233 * forking, in which case it is better to unmap the
2234 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002235 */
2236 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002237 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002238 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2239 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002240 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002241 continue;
2242 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002243 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002244 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002245 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002246 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002247 page_offset <<= 1; /* 32->64 */
2248 /*
2249 * A 32-bit pde maps 4MB while the shadow pdes map
2250 * only 2MB. So we need to double the offset again
2251 * and zap two pdes instead of one.
2252 */
2253 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002254 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002255 page_offset <<= 1;
2256 npte = 2;
2257 }
Avi Kivityfce06572007-05-01 16:44:05 +03002258 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002259 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002260 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002261 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002262 }
Avi Kivity4db35312007-11-21 15:28:32 +02002263 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002264 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2265 gentry = 0;
2266 r = kvm_read_guest_atomic(vcpu->kvm,
2267 gpa & ~(u64)(pte_size - 1),
2268 &gentry, pte_size);
2269 new = (const void *)&gentry;
2270 if (r < 0)
2271 new = NULL;
2272 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002273 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002274 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002275 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002276 if (new)
2277 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002278 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002279 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002280 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002281 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002282 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002283 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002284 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2285 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2286 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002287 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002288}
2289
Avi Kivitya4360362007-01-05 16:36:45 -08002290int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2291{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002292 gpa_t gpa;
2293 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002294
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002295 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002296
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002297 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002298 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002299 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002300 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002301}
Avi Kivity577bdc42008-07-19 08:57:05 +03002302EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002303
Avi Kivity22d95b12007-09-14 20:26:06 +03002304void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002305{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002306 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002307 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002308
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002309 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002310 struct kvm_mmu_page, link);
2311 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002312 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002313 }
2314}
Avi Kivityebeace82007-01-05 16:36:47 -08002315
Avi Kivity30677142007-10-28 18:48:59 +02002316int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2317{
2318 int r;
2319 enum emulation_result er;
2320
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002321 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002322 if (r < 0)
2323 goto out;
2324
2325 if (!r) {
2326 r = 1;
2327 goto out;
2328 }
2329
Avi Kivityb733bfb2007-10-28 18:52:05 +02002330 r = mmu_topup_memory_caches(vcpu);
2331 if (r)
2332 goto out;
2333
Avi Kivity30677142007-10-28 18:48:59 +02002334 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002335
2336 switch (er) {
2337 case EMULATE_DONE:
2338 return 1;
2339 case EMULATE_DO_MMIO:
2340 ++vcpu->stat.mmio_exits;
2341 return 0;
2342 case EMULATE_FAIL:
2343 kvm_report_emulation_failure(vcpu, "pagetable");
2344 return 1;
2345 default:
2346 BUG();
2347 }
2348out:
Avi Kivity30677142007-10-28 18:48:59 +02002349 return r;
2350}
2351EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2352
Marcelo Tosattia7052892008-09-23 13:18:35 -03002353void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2354{
2355 spin_lock(&vcpu->kvm->mmu_lock);
2356 vcpu->arch.mmu.invlpg(vcpu, gva);
2357 spin_unlock(&vcpu->kvm->mmu_lock);
2358 kvm_mmu_flush_tlb(vcpu);
2359 ++vcpu->stat.invlpg;
2360}
2361EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2362
Joerg Roedel18552672008-02-07 13:47:41 +01002363void kvm_enable_tdp(void)
2364{
2365 tdp_enabled = true;
2366}
2367EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2368
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002369void kvm_disable_tdp(void)
2370{
2371 tdp_enabled = false;
2372}
2373EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2374
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375static void free_mmu_pages(struct kvm_vcpu *vcpu)
2376{
Avi Kivity4db35312007-11-21 15:28:32 +02002377 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002379 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2380 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002381 struct kvm_mmu_page, link);
2382 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002383 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002384 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002385 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
2388static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2389{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002390 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 int i;
2392
2393 ASSERT(vcpu);
2394
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002395 if (vcpu->kvm->arch.n_requested_mmu_pages)
2396 vcpu->kvm->arch.n_free_mmu_pages =
2397 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002398 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002399 vcpu->kvm->arch.n_free_mmu_pages =
2400 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002401 /*
2402 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2403 * Therefore we need to allocate shadow page tables in the first
2404 * 4GB of memory, which happens to fit the DMA32 zone.
2405 */
2406 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2407 if (!page)
2408 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002409 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002410 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002411 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002412
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 return 0;
2414
2415error_1:
2416 free_mmu_pages(vcpu);
2417 return -ENOMEM;
2418}
2419
Ingo Molnar8018c272006-12-29 16:50:01 -08002420int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002423 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424
Ingo Molnar8018c272006-12-29 16:50:01 -08002425 return alloc_mmu_pages(vcpu);
2426}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
Ingo Molnar8018c272006-12-29 16:50:01 -08002428int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2429{
2430 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002431 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002432
Ingo Molnar8018c272006-12-29 16:50:01 -08002433 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434}
2435
2436void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2437{
2438 ASSERT(vcpu);
2439
2440 destroy_kvm_mmu(vcpu);
2441 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002442 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
Avi Kivity90cb0522007-07-17 13:04:56 +03002445void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446{
Avi Kivity4db35312007-11-21 15:28:32 +02002447 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448
Avi Kivity2245a282008-08-27 16:32:24 +03002449 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002450 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451 int i;
2452 u64 *pt;
2453
Avi Kivity4db35312007-11-21 15:28:32 +02002454 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 continue;
2456
Avi Kivity4db35312007-11-21 15:28:32 +02002457 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2459 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002460 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 }
Avi Kivity171d5952008-08-27 16:40:51 +03002463 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002464 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002466
Avi Kivity90cb0522007-07-17 13:04:56 +03002467void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002468{
Avi Kivity4db35312007-11-21 15:28:32 +02002469 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002470
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002471 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002472 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002473 if (kvm_mmu_zap_page(kvm, sp))
2474 node = container_of(kvm->arch.active_mmu_pages.next,
2475 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002476 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002477
Avi Kivity90cb0522007-07-17 13:04:56 +03002478 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002479}
2480
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002481static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002482{
2483 struct kvm_mmu_page *page;
2484
2485 page = container_of(kvm->arch.active_mmu_pages.prev,
2486 struct kvm_mmu_page, link);
2487 kvm_mmu_zap_page(kvm, page);
2488}
2489
2490static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2491{
2492 struct kvm *kvm;
2493 struct kvm *kvm_freed = NULL;
2494 int cache_count = 0;
2495
2496 spin_lock(&kvm_lock);
2497
2498 list_for_each_entry(kvm, &vm_list, vm_list) {
2499 int npages;
2500
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002501 if (!down_read_trylock(&kvm->slots_lock))
2502 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002503 spin_lock(&kvm->mmu_lock);
2504 npages = kvm->arch.n_alloc_mmu_pages -
2505 kvm->arch.n_free_mmu_pages;
2506 cache_count += npages;
2507 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2508 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2509 cache_count--;
2510 kvm_freed = kvm;
2511 }
2512 nr_to_scan--;
2513
2514 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002515 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002516 }
2517 if (kvm_freed)
2518 list_move_tail(&kvm_freed->vm_list, &vm_list);
2519
2520 spin_unlock(&kvm_lock);
2521
2522 return cache_count;
2523}
2524
2525static struct shrinker mmu_shrinker = {
2526 .shrink = mmu_shrink,
2527 .seeks = DEFAULT_SEEKS * 10,
2528};
2529
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002530static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002531{
2532 if (pte_chain_cache)
2533 kmem_cache_destroy(pte_chain_cache);
2534 if (rmap_desc_cache)
2535 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002536 if (mmu_page_header_cache)
2537 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002538}
2539
Izik Eidus3ee16c82008-03-30 15:17:21 +03002540void kvm_mmu_module_exit(void)
2541{
2542 mmu_destroy_caches();
2543 unregister_shrinker(&mmu_shrinker);
2544}
2545
Avi Kivityb5a33a72007-04-15 16:31:09 +03002546int kvm_mmu_module_init(void)
2547{
2548 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2549 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002550 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002551 if (!pte_chain_cache)
2552 goto nomem;
2553 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2554 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002555 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002556 if (!rmap_desc_cache)
2557 goto nomem;
2558
Avi Kivityd3d25b02007-05-30 12:34:53 +03002559 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2560 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002561 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002562 if (!mmu_page_header_cache)
2563 goto nomem;
2564
Izik Eidus3ee16c82008-03-30 15:17:21 +03002565 register_shrinker(&mmu_shrinker);
2566
Avi Kivityb5a33a72007-04-15 16:31:09 +03002567 return 0;
2568
2569nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002570 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002571 return -ENOMEM;
2572}
2573
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002574/*
2575 * Caculate mmu pages needed for kvm.
2576 */
2577unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2578{
2579 int i;
2580 unsigned int nr_mmu_pages;
2581 unsigned int nr_pages = 0;
2582
2583 for (i = 0; i < kvm->nmemslots; i++)
2584 nr_pages += kvm->memslots[i].npages;
2585
2586 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2587 nr_mmu_pages = max(nr_mmu_pages,
2588 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2589
2590 return nr_mmu_pages;
2591}
2592
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002593static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2594 unsigned len)
2595{
2596 if (len > buffer->len)
2597 return NULL;
2598 return buffer->ptr;
2599}
2600
2601static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2602 unsigned len)
2603{
2604 void *ret;
2605
2606 ret = pv_mmu_peek_buffer(buffer, len);
2607 if (!ret)
2608 return ret;
2609 buffer->ptr += len;
2610 buffer->len -= len;
2611 buffer->processed += len;
2612 return ret;
2613}
2614
2615static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2616 gpa_t addr, gpa_t value)
2617{
2618 int bytes = 8;
2619 int r;
2620
2621 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2622 bytes = 4;
2623
2624 r = mmu_topup_memory_caches(vcpu);
2625 if (r)
2626 return r;
2627
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002628 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002629 return -EFAULT;
2630
2631 return 1;
2632}
2633
2634static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2635{
2636 kvm_x86_ops->tlb_flush(vcpu);
2637 return 1;
2638}
2639
2640static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2641{
2642 spin_lock(&vcpu->kvm->mmu_lock);
2643 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2644 spin_unlock(&vcpu->kvm->mmu_lock);
2645 return 1;
2646}
2647
2648static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2649 struct kvm_pv_mmu_op_buffer *buffer)
2650{
2651 struct kvm_mmu_op_header *header;
2652
2653 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2654 if (!header)
2655 return 0;
2656 switch (header->op) {
2657 case KVM_MMU_OP_WRITE_PTE: {
2658 struct kvm_mmu_op_write_pte *wpte;
2659
2660 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2661 if (!wpte)
2662 return 0;
2663 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2664 wpte->pte_val);
2665 }
2666 case KVM_MMU_OP_FLUSH_TLB: {
2667 struct kvm_mmu_op_flush_tlb *ftlb;
2668
2669 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2670 if (!ftlb)
2671 return 0;
2672 return kvm_pv_mmu_flush_tlb(vcpu);
2673 }
2674 case KVM_MMU_OP_RELEASE_PT: {
2675 struct kvm_mmu_op_release_pt *rpt;
2676
2677 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2678 if (!rpt)
2679 return 0;
2680 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2681 }
2682 default: return 0;
2683 }
2684}
2685
2686int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2687 gpa_t addr, unsigned long *ret)
2688{
2689 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002690 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002691
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002692 buffer->ptr = buffer->buf;
2693 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2694 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002695
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002696 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002697 if (r)
2698 goto out;
2699
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002700 while (buffer->len) {
2701 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002702 if (r < 0)
2703 goto out;
2704 if (r == 0)
2705 break;
2706 }
2707
2708 r = 1;
2709out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002710 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002711 return r;
2712}
2713
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002714#ifdef AUDIT
2715
2716static const char *audit_msg;
2717
2718static gva_t canonicalize(gva_t gva)
2719{
2720#ifdef CONFIG_X86_64
2721 gva = (long long)(gva << 16) >> 16;
2722#endif
2723 return gva;
2724}
2725
2726static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2727 gva_t va, int level)
2728{
2729 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2730 int i;
2731 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2732
2733 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2734 u64 ent = pt[i];
2735
Avi Kivityc7addb92007-09-16 18:58:32 +02002736 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002737 continue;
2738
2739 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002740 if (level > 1) {
2741 if (ent == shadow_notrap_nonpresent_pte)
2742 printk(KERN_ERR "audit: (%s) nontrapping pte"
2743 " in nonleaf level: levels %d gva %lx"
2744 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002745 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002746
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002747 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002748 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002749 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002750 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002751
Avi Kivityc7addb92007-09-16 18:58:32 +02002752 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002753 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002754 printk(KERN_ERR "xx audit error: (%s) levels %d"
2755 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002756 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002757 va, gpa, hpa, ent,
2758 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002759 else if (ent == shadow_notrap_nonpresent_pte
2760 && !is_error_hpa(hpa))
2761 printk(KERN_ERR "audit: (%s) notrap shadow,"
2762 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002763 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002764
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002765 }
2766 }
2767}
2768
2769static void audit_mappings(struct kvm_vcpu *vcpu)
2770{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002771 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002772
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002773 if (vcpu->arch.mmu.root_level == 4)
2774 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002775 else
2776 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002777 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002778 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002779 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002780 i << 30,
2781 2);
2782}
2783
2784static int count_rmaps(struct kvm_vcpu *vcpu)
2785{
2786 int nmaps = 0;
2787 int i, j, k;
2788
2789 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2790 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2791 struct kvm_rmap_desc *d;
2792
2793 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002794 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002795
Izik Eidus290fc382007-09-27 14:11:22 +02002796 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002797 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002798 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002799 ++nmaps;
2800 continue;
2801 }
Izik Eidus290fc382007-09-27 14:11:22 +02002802 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002803 while (d) {
2804 for (k = 0; k < RMAP_EXT; ++k)
2805 if (d->shadow_ptes[k])
2806 ++nmaps;
2807 else
2808 break;
2809 d = d->more;
2810 }
2811 }
2812 }
2813 return nmaps;
2814}
2815
2816static int count_writable_mappings(struct kvm_vcpu *vcpu)
2817{
2818 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002819 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002820 int i;
2821
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002822 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002823 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002824
Avi Kivity4db35312007-11-21 15:28:32 +02002825 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002826 continue;
2827
2828 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2829 u64 ent = pt[i];
2830
2831 if (!(ent & PT_PRESENT_MASK))
2832 continue;
2833 if (!(ent & PT_WRITABLE_MASK))
2834 continue;
2835 ++nmaps;
2836 }
2837 }
2838 return nmaps;
2839}
2840
2841static void audit_rmap(struct kvm_vcpu *vcpu)
2842{
2843 int n_rmap = count_rmaps(vcpu);
2844 int n_actual = count_writable_mappings(vcpu);
2845
2846 if (n_rmap != n_actual)
2847 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002848 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002849}
2850
2851static void audit_write_protection(struct kvm_vcpu *vcpu)
2852{
Avi Kivity4db35312007-11-21 15:28:32 +02002853 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002854 struct kvm_memory_slot *slot;
2855 unsigned long *rmapp;
2856 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002857
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002858 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002859 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002860 continue;
2861
Avi Kivity4db35312007-11-21 15:28:32 +02002862 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2863 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002864 rmapp = &slot->rmap[gfn - slot->base_gfn];
2865 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002866 printk(KERN_ERR "%s: (%s) shadow page has writable"
2867 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002868 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002869 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002870 }
2871}
2872
2873static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2874{
2875 int olddbg = dbg;
2876
2877 dbg = 0;
2878 audit_msg = msg;
2879 audit_rmap(vcpu);
2880 audit_write_protection(vcpu);
2881 audit_mappings(vcpu);
2882 dbg = olddbg;
2883}
2884
2885#endif