blob: 731e6fe9cb078bea1e2655d03f70c5ed81850b77 [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
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080073#ifndef MMU_DEBUG
74#define ASSERT(x) do { } while (0)
75#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080076#define ASSERT(x) \
77 if (!(x)) { \
78 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
79 __FILE__, __LINE__, #x); \
80 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080081#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define PT_FIRST_AVAIL_BITS_SHIFT 9
84#define PT64_SECOND_AVAIL_BITS_SHIFT 52
85
Avi Kivity6aa8b732006-12-10 02:21:36 -080086#define VALID_PAGE(x) ((x) != INVALID_PAGE)
87
88#define PT64_LEVEL_BITS 9
89
90#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040091 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
93#define PT64_LEVEL_MASK(level) \
94 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
95
96#define PT64_INDEX(address, level)\
97 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
98
99
100#define PT32_LEVEL_BITS 10
101
102#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400103 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104
105#define PT32_LEVEL_MASK(level) \
106 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
107
108#define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
110
111
Avi Kivity27aba762007-03-09 13:04:31 +0200112#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113#define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
115
116#define PT32_BASE_ADDR_MASK PAGE_MASK
117#define PT32_DIR_BASE_ADDR_MASK \
118 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
119
Avi Kivity79539ce2007-11-21 02:06:21 +0200120#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
121 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
123#define PFERR_PRESENT_MASK (1U << 0)
124#define PFERR_WRITE_MASK (1U << 1)
125#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800126#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128#define PT_DIRECTORY_LEVEL 2
129#define PT_PAGE_TABLE_LEVEL 1
130
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800131#define RMAP_EXT 4
132
Avi Kivityfe135d22007-12-09 16:15:46 +0200133#define ACC_EXEC_MASK 1
134#define ACC_WRITE_MASK PT_WRITABLE_MASK
135#define ACC_USER_MASK PT_USER_MASK
136#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
137
Avi Kivity135f8c22008-08-21 17:49:56 +0300138#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
139
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800140struct kvm_rmap_desc {
141 u64 *shadow_ptes[RMAP_EXT];
142 struct kvm_rmap_desc *more;
143};
144
Avi Kivity3d000db2008-08-22 19:24:38 +0300145struct kvm_shadow_walk {
146 int (*entry)(struct kvm_shadow_walk *walk, struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +0800147 u64 addr, u64 *spte, int level);
Avi Kivity3d000db2008-08-22 19:24:38 +0300148};
149
Avi Kivityb5a33a72007-04-15 16:31:09 +0300150static struct kmem_cache *pte_chain_cache;
151static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300152static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300153
Avi Kivityc7addb92007-09-16 18:58:32 +0200154static u64 __read_mostly shadow_trap_nonpresent_pte;
155static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800156static u64 __read_mostly shadow_base_present_pte;
157static u64 __read_mostly shadow_nx_mask;
158static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
159static u64 __read_mostly shadow_user_mask;
160static u64 __read_mostly shadow_accessed_mask;
161static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200162
163void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
164{
165 shadow_trap_nonpresent_pte = trap_pte;
166 shadow_notrap_nonpresent_pte = notrap_pte;
167}
168EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
169
Sheng Yang7b523452008-04-25 21:13:50 +0800170void kvm_mmu_set_base_ptes(u64 base_pte)
171{
172 shadow_base_present_pte = base_pte;
173}
174EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
175
176void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
177 u64 dirty_mask, u64 nx_mask, u64 x_mask)
178{
179 shadow_user_mask = user_mask;
180 shadow_accessed_mask = accessed_mask;
181 shadow_dirty_mask = dirty_mask;
182 shadow_nx_mask = nx_mask;
183 shadow_x_mask = x_mask;
184}
185EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
186
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187static int is_write_protection(struct kvm_vcpu *vcpu)
188{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800189 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190}
191
192static int is_cpuid_PSE36(void)
193{
194 return 1;
195}
196
Avi Kivity73b10872007-01-26 00:56:41 -0800197static int is_nx(struct kvm_vcpu *vcpu)
198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800199 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800200}
201
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202static int is_present_pte(unsigned long pte)
203{
204 return pte & PT_PRESENT_MASK;
205}
206
Avi Kivityc7addb92007-09-16 18:58:32 +0200207static int is_shadow_present_pte(u64 pte)
208{
Avi Kivityc7addb92007-09-16 18:58:32 +0200209 return pte != shadow_trap_nonpresent_pte
210 && pte != shadow_notrap_nonpresent_pte;
211}
212
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300213static int is_large_pte(u64 pte)
214{
215 return pte & PT_PAGE_SIZE_MASK;
216}
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218static int is_writeble_pte(unsigned long pte)
219{
220 return pte & PT_WRITABLE_MASK;
221}
222
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200223static int is_dirty_pte(unsigned long pte)
224{
Sheng Yang7b523452008-04-25 21:13:50 +0800225 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200226}
227
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800228static int is_rmap_pte(u64 pte)
229{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200230 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800231}
232
Anthony Liguori35149e22008-04-02 14:46:56 -0500233static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200234{
Anthony Liguori35149e22008-04-02 14:46:56 -0500235 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200236}
237
Avi Kivityda928522007-11-21 13:54:47 +0200238static gfn_t pse36_gfn_delta(u32 gpte)
239{
240 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
241
242 return (gpte & PT32_DIR_PSE36_MASK) << shift;
243}
244
Avi Kivitye663ee62007-05-31 15:46:04 +0300245static void set_shadow_pte(u64 *sptep, u64 spte)
246{
247#ifdef CONFIG_X86_64
248 set_64bit((unsigned long *)sptep, spte);
249#else
250 set_64bit((unsigned long long *)sptep, spte);
251#endif
252}
253
Avi Kivitye2dec932007-01-05 16:36:54 -0800254static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300255 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800256{
257 void *obj;
258
259 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800260 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800261 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300262 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800263 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800264 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800265 cache->objects[cache->nobjs++] = obj;
266 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800267 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800268}
269
270static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
271{
272 while (mc->nobjs)
273 kfree(mc->objects[--mc->nobjs]);
274}
275
Avi Kivityc1158e62007-07-20 08:18:27 +0300276static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300277 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300278{
279 struct page *page;
280
281 if (cache->nobjs >= min)
282 return 0;
283 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300284 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300285 if (!page)
286 return -ENOMEM;
287 set_page_private(page, 0);
288 cache->objects[cache->nobjs++] = page_address(page);
289 }
290 return 0;
291}
292
293static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
294{
295 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300296 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300297}
298
Avi Kivity8c438502007-04-16 11:53:17 +0300299static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
300{
301 int r;
302
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800303 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300304 pte_chain_cache, 4);
305 if (r)
306 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800307 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300308 rmap_desc_cache, 1);
309 if (r)
310 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800311 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300312 if (r)
313 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800314 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300315 mmu_page_header_cache, 4);
316out:
Avi Kivity8c438502007-04-16 11:53:17 +0300317 return r;
318}
319
Avi Kivity714b93d2007-01-05 16:36:53 -0800320static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
321{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800322 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
323 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
324 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
325 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800326}
327
328static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
329 size_t size)
330{
331 void *p;
332
333 BUG_ON(!mc->nobjs);
334 p = mc->objects[--mc->nobjs];
335 memset(p, 0, size);
336 return p;
337}
338
Avi Kivity714b93d2007-01-05 16:36:53 -0800339static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
340{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800341 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800342 sizeof(struct kvm_pte_chain));
343}
344
Avi Kivity90cb0522007-07-17 13:04:56 +0300345static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800346{
Avi Kivity90cb0522007-07-17 13:04:56 +0300347 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800348}
349
350static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
351{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800353 sizeof(struct kvm_rmap_desc));
354}
355
Avi Kivity90cb0522007-07-17 13:04:56 +0300356static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800357{
Avi Kivity90cb0522007-07-17 13:04:56 +0300358 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800359}
360
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800361/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300362 * Return the pointer to the largepage write count for a given
363 * gfn, handling slots that are not large page aligned.
364 */
365static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
366{
367 unsigned long idx;
368
369 idx = (gfn / KVM_PAGES_PER_HPAGE) -
370 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
371 return &slot->lpage_info[idx].write_count;
372}
373
374static void account_shadowed(struct kvm *kvm, gfn_t gfn)
375{
376 int *write_count;
377
378 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
379 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300380}
381
382static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
383{
384 int *write_count;
385
386 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
387 *write_count -= 1;
388 WARN_ON(*write_count < 0);
389}
390
391static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
392{
393 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
394 int *largepage_idx;
395
396 if (slot) {
397 largepage_idx = slot_largepage_idx(gfn, slot);
398 return *largepage_idx;
399 }
400
401 return 1;
402}
403
404static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
405{
406 struct vm_area_struct *vma;
407 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300408 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409
410 addr = gfn_to_hva(kvm, gfn);
411 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300412 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300413
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300414 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300415 vma = find_vma(current->mm, addr);
416 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300417 ret = 1;
418 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300419
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300420 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421}
422
423static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
424{
425 struct kvm_memory_slot *slot;
426
427 if (has_wrprotected_page(vcpu->kvm, large_gfn))
428 return 0;
429
430 if (!host_largepage_backed(vcpu->kvm, large_gfn))
431 return 0;
432
433 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
434 if (slot && slot->dirty_bitmap)
435 return 0;
436
437 return 1;
438}
439
440/*
Izik Eidus290fc382007-09-27 14:11:22 +0200441 * Take gfn and return the reverse mapping to it.
442 * Note: gfn must be unaliased before this function get called
443 */
444
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200446{
447 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200449
450 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300451 if (!lpage)
452 return &slot->rmap[gfn - slot->base_gfn];
453
454 idx = (gfn / KVM_PAGES_PER_HPAGE) -
455 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
456
457 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200458}
459
460/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800461 * Reverse mapping data structures:
462 *
Izik Eidus290fc382007-09-27 14:11:22 +0200463 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
464 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800465 *
Izik Eidus290fc382007-09-27 14:11:22 +0200466 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
467 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800468 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300469static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800470{
Avi Kivity4db35312007-11-21 15:28:32 +0200471 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800472 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200473 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800474 int i;
475
476 if (!is_rmap_pte(*spte))
477 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200478 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200479 sp = page_header(__pa(spte));
480 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200482 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800483 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200484 *rmapp = (unsigned long)spte;
485 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800487 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200488 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800489 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200490 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 } else {
492 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200493 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
495 desc = desc->more;
496 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800497 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800498 desc = desc->more;
499 }
500 for (i = 0; desc->shadow_ptes[i]; ++i)
501 ;
502 desc->shadow_ptes[i] = spte;
503 }
504}
505
Izik Eidus290fc382007-09-27 14:11:22 +0200506static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800507 struct kvm_rmap_desc *desc,
508 int i,
509 struct kvm_rmap_desc *prev_desc)
510{
511 int j;
512
513 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
514 ;
515 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000516 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800517 if (j != 0)
518 return;
519 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200520 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800521 else
522 if (prev_desc)
523 prev_desc->more = desc->more;
524 else
Izik Eidus290fc382007-09-27 14:11:22 +0200525 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300526 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527}
528
Izik Eidus290fc382007-09-27 14:11:22 +0200529static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800530{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800531 struct kvm_rmap_desc *desc;
532 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200533 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500534 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200535 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800536 int i;
537
538 if (!is_rmap_pte(*spte))
539 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200540 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500541 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800542 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500543 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200544 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500545 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200546 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500547 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300548 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200549 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
551 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200552 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800553 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200554 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
556 spte, *spte);
557 BUG();
558 }
Izik Eidus290fc382007-09-27 14:11:22 +0200559 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800560 } else {
561 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200562 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 prev_desc = NULL;
564 while (desc) {
565 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
566 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200567 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800568 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800569 prev_desc);
570 return;
571 }
572 prev_desc = desc;
573 desc = desc->more;
574 }
575 BUG();
576 }
577}
578
Izik Eidus98348e92007-10-16 14:42:30 +0200579static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800580{
Avi Kivity374cbac2007-01-05 16:36:43 -0800581 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200582 struct kvm_rmap_desc *prev_desc;
583 u64 *prev_spte;
584 int i;
585
586 if (!*rmapp)
587 return NULL;
588 else if (!(*rmapp & 1)) {
589 if (!spte)
590 return (u64 *)*rmapp;
591 return NULL;
592 }
593 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
594 prev_desc = NULL;
595 prev_spte = NULL;
596 while (desc) {
597 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
598 if (prev_spte == spte)
599 return desc->shadow_ptes[i];
600 prev_spte = desc->shadow_ptes[i];
601 }
602 desc = desc->more;
603 }
604 return NULL;
605}
606
607static void rmap_write_protect(struct kvm *kvm, u64 gfn)
608{
Izik Eidus290fc382007-09-27 14:11:22 +0200609 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800610 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800611 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800612
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500613 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300614 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800615
Izik Eidus98348e92007-10-16 14:42:30 +0200616 spte = rmap_next(kvm, rmapp, NULL);
617 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800618 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800619 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800620 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800621 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200622 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800623 write_protected = 1;
624 }
Izik Eidus9647c142007-10-16 14:43:46 +0200625 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800626 }
Izik Eidus855149a2008-03-20 18:17:24 +0200627 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500628 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200629
630 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500631 pfn = spte_to_pfn(*spte);
632 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200633 }
634
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300635 /* check for huge page mappings */
636 rmapp = gfn_to_rmap(kvm, gfn, 1);
637 spte = rmap_next(kvm, rmapp, NULL);
638 while (spte) {
639 BUG_ON(!spte);
640 BUG_ON(!(*spte & PT_PRESENT_MASK));
641 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
642 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
643 if (is_writeble_pte(*spte)) {
644 rmap_remove(kvm, spte);
645 --kvm->stat.lpages;
646 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300647 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300648 write_protected = 1;
649 }
650 spte = rmap_next(kvm, rmapp, spte);
651 }
652
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800653 if (write_protected)
654 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300655
656 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800657}
658
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200659static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
660{
661 u64 *spte;
662 int need_tlb_flush = 0;
663
664 while ((spte = rmap_next(kvm, rmapp, NULL))) {
665 BUG_ON(!(*spte & PT_PRESENT_MASK));
666 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
667 rmap_remove(kvm, spte);
668 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
669 need_tlb_flush = 1;
670 }
671 return need_tlb_flush;
672}
673
674static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
675 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
676{
677 int i;
678 int retval = 0;
679
680 /*
681 * If mmap_sem isn't taken, we can look the memslots with only
682 * the mmu_lock by skipping over the slots with userspace_addr == 0.
683 */
684 for (i = 0; i < kvm->nmemslots; i++) {
685 struct kvm_memory_slot *memslot = &kvm->memslots[i];
686 unsigned long start = memslot->userspace_addr;
687 unsigned long end;
688
689 /* mmu_lock protects userspace_addr */
690 if (!start)
691 continue;
692
693 end = start + (memslot->npages << PAGE_SHIFT);
694 if (hva >= start && hva < end) {
695 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
696 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
697 retval |= handler(kvm,
698 &memslot->lpage_info[
699 gfn_offset /
700 KVM_PAGES_PER_HPAGE].rmap_pde);
701 }
702 }
703
704 return retval;
705}
706
707int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
708{
709 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
710}
711
712static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
713{
714 u64 *spte;
715 int young = 0;
716
Sheng Yang534e38b2008-09-08 15:12:30 +0800717 /* always return old for EPT */
718 if (!shadow_accessed_mask)
719 return 0;
720
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200721 spte = rmap_next(kvm, rmapp, NULL);
722 while (spte) {
723 int _young;
724 u64 _spte = *spte;
725 BUG_ON(!(_spte & PT_PRESENT_MASK));
726 _young = _spte & PT_ACCESSED_MASK;
727 if (_young) {
728 young = 1;
729 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
730 }
731 spte = rmap_next(kvm, rmapp, spte);
732 }
733 return young;
734}
735
736int kvm_age_hva(struct kvm *kvm, unsigned long hva)
737{
738 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
739}
740
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800741#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300742static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743{
Avi Kivity139bdb22007-01-05 16:36:50 -0800744 u64 *pos;
745 u64 *end;
746
Avi Kivity47ad8e62007-05-06 15:50:58 +0300747 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300748 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800749 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800750 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800752 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 return 1;
754}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800755#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756
Avi Kivity4db35312007-11-21 15:28:32 +0200757static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800758{
Avi Kivity4db35312007-11-21 15:28:32 +0200759 ASSERT(is_empty_shadow_page(sp->spt));
760 list_del(&sp->link);
761 __free_page(virt_to_page(sp->spt));
762 __free_page(virt_to_page(sp->gfns));
763 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800764 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800765}
766
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800767static unsigned kvm_page_table_hashfn(gfn_t gfn)
768{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200769 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800770}
771
Avi Kivity25c0de22007-01-05 16:36:42 -0800772static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
773 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774{
Avi Kivity4db35312007-11-21 15:28:32 +0200775 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800777 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
778 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
779 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200780 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800781 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200782 ASSERT(is_empty_shadow_page(sp->spt));
783 sp->slot_bitmap = 0;
784 sp->multimapped = 0;
785 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800786 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200787 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788}
789
Avi Kivity714b93d2007-01-05 16:36:53 -0800790static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200791 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800792{
793 struct kvm_pte_chain *pte_chain;
794 struct hlist_node *node;
795 int i;
796
797 if (!parent_pte)
798 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200799 if (!sp->multimapped) {
800 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800801
802 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200803 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800804 return;
805 }
Avi Kivity4db35312007-11-21 15:28:32 +0200806 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800807 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200808 INIT_HLIST_HEAD(&sp->parent_ptes);
809 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800810 pte_chain->parent_ptes[0] = old;
811 }
Avi Kivity4db35312007-11-21 15:28:32 +0200812 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800813 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
814 continue;
815 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
816 if (!pte_chain->parent_ptes[i]) {
817 pte_chain->parent_ptes[i] = parent_pte;
818 return;
819 }
820 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800821 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200823 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800824 pte_chain->parent_ptes[0] = parent_pte;
825}
826
Avi Kivity4db35312007-11-21 15:28:32 +0200827static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800828 u64 *parent_pte)
829{
830 struct kvm_pte_chain *pte_chain;
831 struct hlist_node *node;
832 int i;
833
Avi Kivity4db35312007-11-21 15:28:32 +0200834 if (!sp->multimapped) {
835 BUG_ON(sp->parent_pte != parent_pte);
836 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800837 return;
838 }
Avi Kivity4db35312007-11-21 15:28:32 +0200839 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800840 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
841 if (!pte_chain->parent_ptes[i])
842 break;
843 if (pte_chain->parent_ptes[i] != parent_pte)
844 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800845 while (i + 1 < NR_PTE_CHAIN_ENTRIES
846 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800847 pte_chain->parent_ptes[i]
848 = pte_chain->parent_ptes[i + 1];
849 ++i;
850 }
851 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800852 if (i == 0) {
853 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300854 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200855 if (hlist_empty(&sp->parent_ptes)) {
856 sp->multimapped = 0;
857 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800858 }
859 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800860 return;
861 }
862 BUG();
863}
864
Avi Kivityd761a502008-05-29 14:55:03 +0300865static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
866 struct kvm_mmu_page *sp)
867{
868 int i;
869
870 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
871 sp->spt[i] = shadow_trap_nonpresent_pte;
872}
873
Avi Kivity4db35312007-11-21 15:28:32 +0200874static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875{
876 unsigned index;
877 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200878 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800879 struct hlist_node *node;
880
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800881 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200882 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800883 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200884 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500885 if (sp->gfn == gfn && !sp->role.metaphysical
886 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800887 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800888 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200889 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800890 }
891 return NULL;
892}
893
894static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
895 gfn_t gfn,
896 gva_t gaddr,
897 unsigned level,
898 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200899 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200900 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800901{
902 union kvm_mmu_page_role role;
903 unsigned index;
904 unsigned quadrant;
905 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200906 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800907 struct hlist_node *node;
908
909 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800910 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911 role.level = level;
912 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200913 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800914 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800915 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
916 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
917 role.quadrant = quadrant;
918 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800919 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800920 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200921 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800922 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200923 hlist_for_each_entry(sp, node, bucket, hash_link)
924 if (sp->gfn == gfn && sp->role.word == role.word) {
925 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800926 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200927 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800928 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200929 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200930 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
931 if (!sp)
932 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800933 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200934 sp->gfn = gfn;
935 sp->role = role;
936 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800937 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500938 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity131d8272008-05-29 14:56:28 +0300939 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
940 vcpu->arch.mmu.prefetch_page(vcpu, sp);
941 else
942 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200943 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800944}
945
Avi Kivity3d000db2008-08-22 19:24:38 +0300946static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +0800947 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +0300948{
949 hpa_t shadow_addr;
950 int level;
951 int r;
952 u64 *sptep;
953 unsigned index;
954
955 shadow_addr = vcpu->arch.mmu.root_hpa;
956 level = vcpu->arch.mmu.shadow_root_level;
957 if (level == PT32E_ROOT_LEVEL) {
958 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
959 shadow_addr &= PT64_BASE_ADDR_MASK;
960 --level;
961 }
962
963 while (level >= PT_PAGE_TABLE_LEVEL) {
964 index = SHADOW_PT_INDEX(addr, level);
965 sptep = ((u64 *)__va(shadow_addr)) + index;
966 r = walker->entry(walker, vcpu, addr, sptep, level);
967 if (r)
968 return r;
969 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
970 --level;
971 }
972 return 0;
973}
974
Avi Kivity90cb0522007-07-17 13:04:56 +0300975static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200976 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800977{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800978 unsigned i;
979 u64 *pt;
980 u64 ent;
981
Avi Kivity4db35312007-11-21 15:28:32 +0200982 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800983
Avi Kivity4db35312007-11-21 15:28:32 +0200984 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800985 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200986 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200987 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200988 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800989 }
990 return;
991 }
992
993 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
994 ent = pt[i];
995
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300996 if (is_shadow_present_pte(ent)) {
997 if (!is_large_pte(ent)) {
998 ent &= PT64_BASE_ADDR_MASK;
999 mmu_page_remove_parent_pte(page_header(ent),
1000 &pt[i]);
1001 } else {
1002 --kvm->stat.lpages;
1003 rmap_remove(kvm, &pt[i]);
1004 }
1005 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001006 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001007 }
Avi Kivitya4360362007-01-05 16:36:45 -08001008}
1009
Avi Kivity4db35312007-11-21 15:28:32 +02001010static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001011{
Avi Kivity4db35312007-11-21 15:28:32 +02001012 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001013}
1014
Avi Kivity12b7d282007-09-23 14:10:49 +02001015static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1016{
1017 int i;
1018
1019 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1020 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001021 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001022}
1023
Avi Kivity31aa2b42008-07-11 17:59:46 +03001024static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001025{
1026 u64 *parent_pte;
1027
Avi Kivity4db35312007-11-21 15:28:32 +02001028 while (sp->multimapped || sp->parent_pte) {
1029 if (!sp->multimapped)
1030 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001031 else {
1032 struct kvm_pte_chain *chain;
1033
Avi Kivity4db35312007-11-21 15:28:32 +02001034 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001035 struct kvm_pte_chain, link);
1036 parent_pte = chain->parent_ptes[0];
1037 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001038 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001039 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001040 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001041 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001042}
1043
1044static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1045{
1046 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +02001047 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001048 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001049 kvm_flush_remote_tlbs(kvm);
1050 if (!sp->role.invalid && !sp->role.metaphysical)
1051 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001052 if (!sp->root_count) {
1053 hlist_del(&sp->hash_link);
1054 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001055 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001056 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001057 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001058 kvm_reload_remote_mmus(kvm);
1059 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001060 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -08001061}
1062
Izik Eidus82ce2c92007-10-02 18:52:55 +02001063/*
1064 * Changing the number of mmu pages allocated to the vm
1065 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1066 */
1067void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1068{
1069 /*
1070 * If we set the number of mmu pages to be smaller be than the
1071 * number of actived pages , we must to free some mmu pages before we
1072 * change the value
1073 */
1074
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001075 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001076 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001077 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1078 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001079
1080 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1081 struct kvm_mmu_page *page;
1082
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001083 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001084 struct kvm_mmu_page, link);
1085 kvm_mmu_zap_page(kvm, page);
1086 n_used_mmu_pages--;
1087 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001088 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001089 }
1090 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001091 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1092 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001093
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001094 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001095}
1096
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001097static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001098{
1099 unsigned index;
1100 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001101 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001102 struct hlist_node *node, *n;
1103 int r;
1104
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001105 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001106 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001107 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001108 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001109 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1110 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001111 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001112 sp->role.word);
1113 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -08001114 r = 1;
1115 }
1116 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001117}
1118
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001119static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001120{
Avi Kivity4db35312007-11-21 15:28:32 +02001121 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001122
Avi Kivity4db35312007-11-21 15:28:32 +02001123 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001124 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001125 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001126 }
1127}
1128
Avi Kivity38c335f2007-11-21 14:20:22 +02001129static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130{
Avi Kivity38c335f2007-11-21 14:20:22 +02001131 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001132 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133
Avi Kivity4db35312007-11-21 15:28:32 +02001134 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135}
1136
Avi Kivity039576c2007-03-20 12:46:50 +02001137struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1138{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001139 struct page *page;
1140
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001141 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001142
1143 if (gpa == UNMAPPED_GVA)
1144 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001145
Izik Eidus72dc67a2008-02-10 18:04:15 +02001146 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001147
1148 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001149}
1150
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001151static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1152 unsigned pte_access, int user_fault,
1153 int write_fault, int dirty, int largepage,
1154 gfn_t gfn, pfn_t pfn, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001155{
1156 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001157 int ret = 0;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001158 /*
1159 * We don't set the accessed bit, since we sometimes want to see
1160 * whether the guest actually used the pte (in order to detect
1161 * demand paging).
1162 */
Sheng Yang7b523452008-04-25 21:13:50 +08001163 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001164 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001165 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001166 if (!dirty)
1167 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001168 if (pte_access & ACC_EXEC_MASK)
1169 spte |= shadow_x_mask;
1170 else
1171 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001172 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001173 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001174 if (largepage)
1175 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001176
Anthony Liguori35149e22008-04-02 14:46:56 -05001177 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001178
1179 if ((pte_access & ACC_WRITE_MASK)
1180 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1181 struct kvm_mmu_page *shadow;
1182
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001183 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1184 ret = 1;
1185 spte = shadow_trap_nonpresent_pte;
1186 goto set_pte;
1187 }
1188
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001189 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001190
1191 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001192 if (shadow) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001193 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001194 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001195 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001196 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001197 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001198 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001199 }
1200 }
1201
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001202 if (pte_access & ACC_WRITE_MASK)
1203 mark_page_dirty(vcpu->kvm, gfn);
1204
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001205set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001206 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001207 return ret;
1208}
1209
1210
1211static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1212 unsigned pt_access, unsigned pte_access,
1213 int user_fault, int write_fault, int dirty,
1214 int *ptwrite, int largepage, gfn_t gfn,
1215 pfn_t pfn, bool speculative)
1216{
1217 int was_rmapped = 0;
1218 int was_writeble = is_writeble_pte(*shadow_pte);
1219
1220 pgprintk("%s: spte %llx access %x write_fault %d"
1221 " user_fault %d gfn %lx\n",
1222 __func__, *shadow_pte, pt_access,
1223 write_fault, user_fault, gfn);
1224
1225 if (is_rmap_pte(*shadow_pte)) {
1226 /*
1227 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1228 * the parent of the now unreachable PTE.
1229 */
1230 if (largepage && !is_large_pte(*shadow_pte)) {
1231 struct kvm_mmu_page *child;
1232 u64 pte = *shadow_pte;
1233
1234 child = page_header(pte & PT64_BASE_ADDR_MASK);
1235 mmu_page_remove_parent_pte(child, shadow_pte);
1236 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1237 pgprintk("hfn old %lx new %lx\n",
1238 spte_to_pfn(*shadow_pte), pfn);
1239 rmap_remove(vcpu->kvm, shadow_pte);
1240 } else {
1241 if (largepage)
1242 was_rmapped = is_large_pte(*shadow_pte);
1243 else
1244 was_rmapped = 1;
1245 }
1246 }
1247 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001248 dirty, largepage, gfn, pfn, speculative)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001249 if (write_fault)
1250 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001251 kvm_x86_ops->tlb_flush(vcpu);
1252 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001253
1254 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1255 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1256 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1257 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1258 *shadow_pte, shadow_pte);
1259 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001260 ++vcpu->kvm->stat.lpages;
1261
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001262 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1263 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001264 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001265 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001266 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001267 } else {
1268 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001269 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001270 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001271 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001272 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001273 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001274 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001275 vcpu->arch.last_pte_gfn = gfn;
1276 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001277}
1278
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1280{
1281}
1282
Avi Kivity140754b2008-08-22 19:28:04 +03001283struct direct_shadow_walk {
1284 struct kvm_shadow_walk walker;
1285 pfn_t pfn;
1286 int write;
1287 int largepage;
1288 int pt_write;
1289};
1290
1291static int direct_map_entry(struct kvm_shadow_walk *_walk,
1292 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001293 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001294{
1295 struct direct_shadow_walk *walk =
1296 container_of(_walk, struct direct_shadow_walk, walker);
1297 struct kvm_mmu_page *sp;
1298 gfn_t pseudo_gfn;
1299 gfn_t gfn = addr >> PAGE_SHIFT;
1300
1301 if (level == PT_PAGE_TABLE_LEVEL
1302 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1303 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1304 0, walk->write, 1, &walk->pt_write,
1305 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001306 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001307 return 1;
1308 }
1309
1310 if (*sptep == shadow_trap_nonpresent_pte) {
1311 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001312 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001313 1, ACC_ALL, sptep);
1314 if (!sp) {
1315 pgprintk("nonpaging_map: ENOMEM\n");
1316 kvm_release_pfn_clean(walk->pfn);
1317 return -ENOMEM;
1318 }
1319
1320 set_shadow_pte(sptep,
1321 __pa(sp->spt)
1322 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1323 | shadow_user_mask | shadow_x_mask);
1324 }
1325 return 0;
1326}
1327
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001328static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001329 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330{
Avi Kivity140754b2008-08-22 19:28:04 +03001331 int r;
1332 struct direct_shadow_walk walker = {
1333 .walker = { .entry = direct_map_entry, },
1334 .pfn = pfn,
1335 .largepage = largepage,
1336 .write = write,
1337 .pt_write = 0,
1338 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001340 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001341 if (r < 0)
1342 return r;
1343 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001346static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1347{
1348 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001349 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001350 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001351 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001352
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001353 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1354 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1355 largepage = 1;
1356 }
1357
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001358 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001359 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001360 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001361
Avi Kivityd196e342008-01-24 11:44:11 +02001362 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001363 if (is_error_pfn(pfn)) {
1364 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001365 return 1;
1366 }
1367
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001368 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001369 if (mmu_notifier_retry(vcpu, mmu_seq))
1370 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001371 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001372 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001373 spin_unlock(&vcpu->kvm->mmu_lock);
1374
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001375
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001376 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001377
1378out_unlock:
1379 spin_unlock(&vcpu->kvm->mmu_lock);
1380 kvm_release_pfn_clean(pfn);
1381 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001382}
1383
1384
Avi Kivity17ac10a2007-01-05 16:36:40 -08001385static void mmu_free_roots(struct kvm_vcpu *vcpu)
1386{
1387 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001388 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001389
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001390 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001391 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001392 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001393 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1394 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001395
Avi Kivity4db35312007-11-21 15:28:32 +02001396 sp = page_header(root);
1397 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001398 if (!sp->root_count && sp->role.invalid)
1399 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001400 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001401 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001402 return;
1403 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001404 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001405 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001406
Avi Kivity417726a2007-04-12 17:35:58 +03001407 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001408 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001409 sp = page_header(root);
1410 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001411 if (!sp->root_count && sp->role.invalid)
1412 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001413 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001414 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001415 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001416 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001417 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001418}
1419
1420static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1421{
1422 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001423 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001424 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001425 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001426
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001427 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001428
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001429 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1430 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001431
1432 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001433 if (tdp_enabled)
1434 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001435 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001436 PT64_ROOT_LEVEL, metaphysical,
1437 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001438 root = __pa(sp->spt);
1439 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001440 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001441 return;
1442 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001443 metaphysical = !is_paging(vcpu);
1444 if (tdp_enabled)
1445 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001446 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001447 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001448
1449 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001450 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1451 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1452 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001453 continue;
1454 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001455 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1456 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001457 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001458 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001459 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001460 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001461 root = __pa(sp->spt);
1462 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001463 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001464 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001465 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001466}
1467
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1469{
1470 return vaddr;
1471}
1472
1473static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001474 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475{
Avi Kivitye8332402007-12-09 18:43:00 +02001476 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001477 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001479 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001480 r = mmu_topup_memory_caches(vcpu);
1481 if (r)
1482 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001483
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001485 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486
Avi Kivitye8332402007-12-09 18:43:00 +02001487 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488
Avi Kivitye8332402007-12-09 18:43:00 +02001489 return nonpaging_map(vcpu, gva & PAGE_MASK,
1490 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491}
1492
Joerg Roedelfb72d162008-02-07 13:47:44 +01001493static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1494 u32 error_code)
1495{
Anthony Liguori35149e22008-04-02 14:46:56 -05001496 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001497 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001498 int largepage = 0;
1499 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001500 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001501
1502 ASSERT(vcpu);
1503 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1504
1505 r = mmu_topup_memory_caches(vcpu);
1506 if (r)
1507 return r;
1508
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001509 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1510 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1511 largepage = 1;
1512 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001513 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001514 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001515 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05001516 if (is_error_pfn(pfn)) {
1517 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001518 return 1;
1519 }
1520 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001521 if (mmu_notifier_retry(vcpu, mmu_seq))
1522 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001523 kvm_mmu_free_some_pages(vcpu);
1524 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03001525 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001526 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001527
1528 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001529
1530out_unlock:
1531 spin_unlock(&vcpu->kvm->mmu_lock);
1532 kvm_release_pfn_clean(pfn);
1533 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001534}
1535
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536static void nonpaging_free(struct kvm_vcpu *vcpu)
1537{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001538 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539}
1540
1541static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1542{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001543 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544
1545 context->new_cr3 = nonpaging_new_cr3;
1546 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547 context->gva_to_gpa = nonpaging_gva_to_gpa;
1548 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001549 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001550 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001552 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553 return 0;
1554}
1555
Avi Kivityd835dfe2007-11-21 02:57:59 +02001556void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001558 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001559 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560}
1561
1562static void paging_new_cr3(struct kvm_vcpu *vcpu)
1563{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001564 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001565 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566}
1567
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568static void inject_page_fault(struct kvm_vcpu *vcpu,
1569 u64 addr,
1570 u32 err_code)
1571{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001572 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573}
1574
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575static void paging_free(struct kvm_vcpu *vcpu)
1576{
1577 nonpaging_free(vcpu);
1578}
1579
1580#define PTTYPE 64
1581#include "paging_tmpl.h"
1582#undef PTTYPE
1583
1584#define PTTYPE 32
1585#include "paging_tmpl.h"
1586#undef PTTYPE
1587
Avi Kivity17ac10a2007-01-05 16:36:40 -08001588static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001590 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591
1592 ASSERT(is_pae(vcpu));
1593 context->new_cr3 = paging_new_cr3;
1594 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001596 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001598 context->root_level = level;
1599 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001600 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601 return 0;
1602}
1603
Avi Kivity17ac10a2007-01-05 16:36:40 -08001604static int paging64_init_context(struct kvm_vcpu *vcpu)
1605{
1606 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1607}
1608
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609static int paging32_init_context(struct kvm_vcpu *vcpu)
1610{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001611 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612
1613 context->new_cr3 = paging_new_cr3;
1614 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 context->gva_to_gpa = paging32_gva_to_gpa;
1616 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001617 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 context->root_level = PT32_ROOT_LEVEL;
1619 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001620 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 return 0;
1622}
1623
1624static int paging32E_init_context(struct kvm_vcpu *vcpu)
1625{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001626 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627}
1628
Joerg Roedelfb72d162008-02-07 13:47:44 +01001629static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1630{
1631 struct kvm_mmu *context = &vcpu->arch.mmu;
1632
1633 context->new_cr3 = nonpaging_new_cr3;
1634 context->page_fault = tdp_page_fault;
1635 context->free = nonpaging_free;
1636 context->prefetch_page = nonpaging_prefetch_page;
Sheng Yang67253af2008-04-25 10:20:22 +08001637 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01001638 context->root_hpa = INVALID_PAGE;
1639
1640 if (!is_paging(vcpu)) {
1641 context->gva_to_gpa = nonpaging_gva_to_gpa;
1642 context->root_level = 0;
1643 } else if (is_long_mode(vcpu)) {
1644 context->gva_to_gpa = paging64_gva_to_gpa;
1645 context->root_level = PT64_ROOT_LEVEL;
1646 } else if (is_pae(vcpu)) {
1647 context->gva_to_gpa = paging64_gva_to_gpa;
1648 context->root_level = PT32E_ROOT_LEVEL;
1649 } else {
1650 context->gva_to_gpa = paging32_gva_to_gpa;
1651 context->root_level = PT32_ROOT_LEVEL;
1652 }
1653
1654 return 0;
1655}
1656
1657static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658{
1659 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001660 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661
1662 if (!is_paging(vcpu))
1663 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001664 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 return paging64_init_context(vcpu);
1666 else if (is_pae(vcpu))
1667 return paging32E_init_context(vcpu);
1668 else
1669 return paging32_init_context(vcpu);
1670}
1671
Joerg Roedelfb72d162008-02-07 13:47:44 +01001672static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1673{
Anthony Liguori35149e22008-04-02 14:46:56 -05001674 vcpu->arch.update_pte.pfn = bad_pfn;
1675
Joerg Roedelfb72d162008-02-07 13:47:44 +01001676 if (tdp_enabled)
1677 return init_kvm_tdp_mmu(vcpu);
1678 else
1679 return init_kvm_softmmu(vcpu);
1680}
1681
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1683{
1684 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001685 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1686 vcpu->arch.mmu.free(vcpu);
1687 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 }
1689}
1690
1691int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1692{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001693 destroy_kvm_mmu(vcpu);
1694 return init_kvm_mmu(vcpu);
1695}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001696EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001697
1698int kvm_mmu_load(struct kvm_vcpu *vcpu)
1699{
Avi Kivity714b93d2007-01-05 16:36:53 -08001700 int r;
1701
Avi Kivitye2dec932007-01-05 16:36:54 -08001702 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001703 if (r)
1704 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001705 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001706 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001707 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001708 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001709 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001710 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001711out:
1712 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001714EXPORT_SYMBOL_GPL(kvm_mmu_load);
1715
1716void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1717{
1718 mmu_free_roots(vcpu);
1719}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720
Avi Kivity09072da2007-05-01 14:16:52 +03001721static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001722 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001723 u64 *spte)
1724{
1725 u64 pte;
1726 struct kvm_mmu_page *child;
1727
1728 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001729 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001730 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1731 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001732 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001733 else {
1734 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001735 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001736 }
1737 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001738 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001739 if (is_large_pte(pte))
1740 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001741}
1742
Avi Kivity00284252007-05-01 16:53:31 +03001743static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001744 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001745 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001746 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001747{
Marcelo Tosatti30945382008-06-11 20:32:40 -03001748 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1749 if (!vcpu->arch.update_pte.largepage ||
1750 sp->role.glevels == PT32_ROOT_LEVEL) {
1751 ++vcpu->kvm->stat.mmu_pde_zapped;
1752 return;
1753 }
1754 }
Avi Kivity00284252007-05-01 16:53:31 +03001755
Avi Kivity4cee5762007-11-18 16:37:07 +02001756 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001757 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001758 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001759 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001760 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001761}
1762
Avi Kivity79539ce2007-11-21 02:06:21 +02001763static bool need_remote_flush(u64 old, u64 new)
1764{
1765 if (!is_shadow_present_pte(old))
1766 return false;
1767 if (!is_shadow_present_pte(new))
1768 return true;
1769 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1770 return true;
1771 old ^= PT64_NX_MASK;
1772 new ^= PT64_NX_MASK;
1773 return (old & ~new & PT64_PERM_MASK) != 0;
1774}
1775
1776static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1777{
1778 if (need_remote_flush(old, new))
1779 kvm_flush_remote_tlbs(vcpu->kvm);
1780 else
1781 kvm_mmu_flush_tlb(vcpu);
1782}
1783
Avi Kivity12b7d282007-09-23 14:10:49 +02001784static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1785{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001786 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001787
Sheng Yang7b523452008-04-25 21:13:50 +08001788 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02001789}
1790
Avi Kivityd7824ff2007-12-30 12:29:05 +02001791static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1792 const u8 *new, int bytes)
1793{
1794 gfn_t gfn;
1795 int r;
1796 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001797 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001798
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001799 vcpu->arch.update_pte.largepage = 0;
1800
Avi Kivityd7824ff2007-12-30 12:29:05 +02001801 if (bytes != 4 && bytes != 8)
1802 return;
1803
1804 /*
1805 * Assume that the pte write on a page table of the same type
1806 * as the current vcpu paging mode. This is nearly always true
1807 * (might be false while changing modes). Note it is verified later
1808 * by update_pte().
1809 */
1810 if (is_pae(vcpu)) {
1811 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1812 if ((bytes == 4) && (gpa % 4 == 0)) {
1813 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1814 if (r)
1815 return;
1816 memcpy((void *)&gpte + (gpa % 8), new, 4);
1817 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1818 memcpy((void *)&gpte, new, 8);
1819 }
1820 } else {
1821 if ((bytes == 4) && (gpa % 4 == 0))
1822 memcpy((void *)&gpte, new, 4);
1823 }
1824 if (!is_present_pte(gpte))
1825 return;
1826 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001827
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001828 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1829 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1830 vcpu->arch.update_pte.largepage = 1;
1831 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001832 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001833 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001834 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001835
Anthony Liguori35149e22008-04-02 14:46:56 -05001836 if (is_error_pfn(pfn)) {
1837 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001838 return;
1839 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001840 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001841 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001842}
1843
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001844static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1845{
1846 u64 *spte = vcpu->arch.last_pte_updated;
1847
1848 if (spte
1849 && vcpu->arch.last_pte_gfn == gfn
1850 && shadow_accessed_mask
1851 && !(*spte & shadow_accessed_mask)
1852 && is_shadow_present_pte(*spte))
1853 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1854}
1855
Avi Kivity09072da2007-05-01 14:16:52 +03001856void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001857 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001858{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001859 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001860 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001861 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001862 struct hlist_head *bucket;
1863 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001864 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001865 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001866 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001867 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001868 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001869 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001870 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001871 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001872 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001873 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001874 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001875
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001876 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001877 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001878 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001879 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02001880 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001881 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001882 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001883 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001884 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001885 ++vcpu->arch.last_pt_write_count;
1886 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001887 flooded = 1;
1888 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001889 vcpu->arch.last_pt_write_gfn = gfn;
1890 vcpu->arch.last_pt_write_count = 1;
1891 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001892 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001893 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001894 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001895 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001896 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001897 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001898 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001899 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001900 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001901 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001902 /*
1903 * Misaligned accesses are too much trouble to fix
1904 * up; also, they usually indicate a page is not used
1905 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001906 *
1907 * If we're seeing too many writes to a page,
1908 * it may no longer be a page table, or we may be
1909 * forking, in which case it is better to unmap the
1910 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001911 */
1912 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001913 gpa, bytes, sp->role.word);
1914 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001915 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001916 continue;
1917 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001918 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001919 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001920 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001921 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001922 page_offset <<= 1; /* 32->64 */
1923 /*
1924 * A 32-bit pde maps 4MB while the shadow pdes map
1925 * only 2MB. So we need to double the offset again
1926 * and zap two pdes instead of one.
1927 */
1928 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001929 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001930 page_offset <<= 1;
1931 npte = 2;
1932 }
Avi Kivityfce06572007-05-01 16:44:05 +03001933 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001934 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001935 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001936 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001937 }
Avi Kivity4db35312007-11-21 15:28:32 +02001938 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001939 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1940 gentry = 0;
1941 r = kvm_read_guest_atomic(vcpu->kvm,
1942 gpa & ~(u64)(pte_size - 1),
1943 &gentry, pte_size);
1944 new = (const void *)&gentry;
1945 if (r < 0)
1946 new = NULL;
1947 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001948 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001949 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001950 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001951 if (new)
1952 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001953 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001954 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001955 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001956 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001957 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001958 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05001959 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1960 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1961 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001962 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001963}
1964
Avi Kivitya4360362007-01-05 16:36:45 -08001965int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1966{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001967 gpa_t gpa;
1968 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001969
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001970 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001971
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001972 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001973 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001974 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001975 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001976}
Avi Kivity577bdc42008-07-19 08:57:05 +03001977EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08001978
Avi Kivity22d95b12007-09-14 20:26:06 +03001979void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001980{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001981 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001982 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001983
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001984 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001985 struct kvm_mmu_page, link);
1986 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001987 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001988 }
1989}
Avi Kivityebeace82007-01-05 16:36:47 -08001990
Avi Kivity30677142007-10-28 18:48:59 +02001991int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1992{
1993 int r;
1994 enum emulation_result er;
1995
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001996 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001997 if (r < 0)
1998 goto out;
1999
2000 if (!r) {
2001 r = 1;
2002 goto out;
2003 }
2004
Avi Kivityb733bfb2007-10-28 18:52:05 +02002005 r = mmu_topup_memory_caches(vcpu);
2006 if (r)
2007 goto out;
2008
Avi Kivity30677142007-10-28 18:48:59 +02002009 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002010
2011 switch (er) {
2012 case EMULATE_DONE:
2013 return 1;
2014 case EMULATE_DO_MMIO:
2015 ++vcpu->stat.mmio_exits;
2016 return 0;
2017 case EMULATE_FAIL:
2018 kvm_report_emulation_failure(vcpu, "pagetable");
2019 return 1;
2020 default:
2021 BUG();
2022 }
2023out:
Avi Kivity30677142007-10-28 18:48:59 +02002024 return r;
2025}
2026EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2027
Joerg Roedel18552672008-02-07 13:47:41 +01002028void kvm_enable_tdp(void)
2029{
2030 tdp_enabled = true;
2031}
2032EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2033
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002034void kvm_disable_tdp(void)
2035{
2036 tdp_enabled = false;
2037}
2038EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2039
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040static void free_mmu_pages(struct kvm_vcpu *vcpu)
2041{
Avi Kivity4db35312007-11-21 15:28:32 +02002042 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002044 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2045 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002046 struct kvm_mmu_page, link);
2047 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002048 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002049 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002050 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051}
2052
2053static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2054{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002055 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 int i;
2057
2058 ASSERT(vcpu);
2059
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002060 if (vcpu->kvm->arch.n_requested_mmu_pages)
2061 vcpu->kvm->arch.n_free_mmu_pages =
2062 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002063 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002064 vcpu->kvm->arch.n_free_mmu_pages =
2065 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002066 /*
2067 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2068 * Therefore we need to allocate shadow page tables in the first
2069 * 4GB of memory, which happens to fit the DMA32 zone.
2070 */
2071 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2072 if (!page)
2073 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002074 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002075 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002076 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002077
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 return 0;
2079
2080error_1:
2081 free_mmu_pages(vcpu);
2082 return -ENOMEM;
2083}
2084
Ingo Molnar8018c272006-12-29 16:50:01 -08002085int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002088 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089
Ingo Molnar8018c272006-12-29 16:50:01 -08002090 return alloc_mmu_pages(vcpu);
2091}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092
Ingo Molnar8018c272006-12-29 16:50:01 -08002093int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2094{
2095 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002096 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002097
Ingo Molnar8018c272006-12-29 16:50:01 -08002098 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099}
2100
2101void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2102{
2103 ASSERT(vcpu);
2104
2105 destroy_kvm_mmu(vcpu);
2106 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002107 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108}
2109
Avi Kivity90cb0522007-07-17 13:04:56 +03002110void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111{
Avi Kivity4db35312007-11-21 15:28:32 +02002112 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113
Avi Kivity2245a282008-08-27 16:32:24 +03002114 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002115 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 int i;
2117 u64 *pt;
2118
Avi Kivity4db35312007-11-21 15:28:32 +02002119 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 continue;
2121
Avi Kivity4db35312007-11-21 15:28:32 +02002122 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2124 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002125 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 }
Avi Kivity171d5952008-08-27 16:40:51 +03002128 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002129 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002131
Avi Kivity90cb0522007-07-17 13:04:56 +03002132void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002133{
Avi Kivity4db35312007-11-21 15:28:32 +02002134 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002135
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002136 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002137 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02002138 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002139 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002140
Avi Kivity90cb0522007-07-17 13:04:56 +03002141 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002142}
2143
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002144static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002145{
2146 struct kvm_mmu_page *page;
2147
2148 page = container_of(kvm->arch.active_mmu_pages.prev,
2149 struct kvm_mmu_page, link);
2150 kvm_mmu_zap_page(kvm, page);
2151}
2152
2153static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2154{
2155 struct kvm *kvm;
2156 struct kvm *kvm_freed = NULL;
2157 int cache_count = 0;
2158
2159 spin_lock(&kvm_lock);
2160
2161 list_for_each_entry(kvm, &vm_list, vm_list) {
2162 int npages;
2163
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002164 if (!down_read_trylock(&kvm->slots_lock))
2165 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002166 spin_lock(&kvm->mmu_lock);
2167 npages = kvm->arch.n_alloc_mmu_pages -
2168 kvm->arch.n_free_mmu_pages;
2169 cache_count += npages;
2170 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2171 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2172 cache_count--;
2173 kvm_freed = kvm;
2174 }
2175 nr_to_scan--;
2176
2177 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002178 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002179 }
2180 if (kvm_freed)
2181 list_move_tail(&kvm_freed->vm_list, &vm_list);
2182
2183 spin_unlock(&kvm_lock);
2184
2185 return cache_count;
2186}
2187
2188static struct shrinker mmu_shrinker = {
2189 .shrink = mmu_shrink,
2190 .seeks = DEFAULT_SEEKS * 10,
2191};
2192
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002193static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002194{
2195 if (pte_chain_cache)
2196 kmem_cache_destroy(pte_chain_cache);
2197 if (rmap_desc_cache)
2198 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002199 if (mmu_page_header_cache)
2200 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002201}
2202
Izik Eidus3ee16c82008-03-30 15:17:21 +03002203void kvm_mmu_module_exit(void)
2204{
2205 mmu_destroy_caches();
2206 unregister_shrinker(&mmu_shrinker);
2207}
2208
Avi Kivityb5a33a72007-04-15 16:31:09 +03002209int kvm_mmu_module_init(void)
2210{
2211 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2212 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002213 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002214 if (!pte_chain_cache)
2215 goto nomem;
2216 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2217 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002218 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002219 if (!rmap_desc_cache)
2220 goto nomem;
2221
Avi Kivityd3d25b02007-05-30 12:34:53 +03002222 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2223 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002224 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002225 if (!mmu_page_header_cache)
2226 goto nomem;
2227
Izik Eidus3ee16c82008-03-30 15:17:21 +03002228 register_shrinker(&mmu_shrinker);
2229
Avi Kivityb5a33a72007-04-15 16:31:09 +03002230 return 0;
2231
2232nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002233 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002234 return -ENOMEM;
2235}
2236
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002237/*
2238 * Caculate mmu pages needed for kvm.
2239 */
2240unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2241{
2242 int i;
2243 unsigned int nr_mmu_pages;
2244 unsigned int nr_pages = 0;
2245
2246 for (i = 0; i < kvm->nmemslots; i++)
2247 nr_pages += kvm->memslots[i].npages;
2248
2249 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2250 nr_mmu_pages = max(nr_mmu_pages,
2251 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2252
2253 return nr_mmu_pages;
2254}
2255
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002256static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2257 unsigned len)
2258{
2259 if (len > buffer->len)
2260 return NULL;
2261 return buffer->ptr;
2262}
2263
2264static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2265 unsigned len)
2266{
2267 void *ret;
2268
2269 ret = pv_mmu_peek_buffer(buffer, len);
2270 if (!ret)
2271 return ret;
2272 buffer->ptr += len;
2273 buffer->len -= len;
2274 buffer->processed += len;
2275 return ret;
2276}
2277
2278static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2279 gpa_t addr, gpa_t value)
2280{
2281 int bytes = 8;
2282 int r;
2283
2284 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2285 bytes = 4;
2286
2287 r = mmu_topup_memory_caches(vcpu);
2288 if (r)
2289 return r;
2290
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002291 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002292 return -EFAULT;
2293
2294 return 1;
2295}
2296
2297static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2298{
2299 kvm_x86_ops->tlb_flush(vcpu);
2300 return 1;
2301}
2302
2303static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2304{
2305 spin_lock(&vcpu->kvm->mmu_lock);
2306 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2307 spin_unlock(&vcpu->kvm->mmu_lock);
2308 return 1;
2309}
2310
2311static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2312 struct kvm_pv_mmu_op_buffer *buffer)
2313{
2314 struct kvm_mmu_op_header *header;
2315
2316 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2317 if (!header)
2318 return 0;
2319 switch (header->op) {
2320 case KVM_MMU_OP_WRITE_PTE: {
2321 struct kvm_mmu_op_write_pte *wpte;
2322
2323 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2324 if (!wpte)
2325 return 0;
2326 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2327 wpte->pte_val);
2328 }
2329 case KVM_MMU_OP_FLUSH_TLB: {
2330 struct kvm_mmu_op_flush_tlb *ftlb;
2331
2332 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2333 if (!ftlb)
2334 return 0;
2335 return kvm_pv_mmu_flush_tlb(vcpu);
2336 }
2337 case KVM_MMU_OP_RELEASE_PT: {
2338 struct kvm_mmu_op_release_pt *rpt;
2339
2340 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2341 if (!rpt)
2342 return 0;
2343 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2344 }
2345 default: return 0;
2346 }
2347}
2348
2349int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2350 gpa_t addr, unsigned long *ret)
2351{
2352 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002353 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002354
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002355 buffer->ptr = buffer->buf;
2356 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2357 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002358
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002359 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002360 if (r)
2361 goto out;
2362
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002363 while (buffer->len) {
2364 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002365 if (r < 0)
2366 goto out;
2367 if (r == 0)
2368 break;
2369 }
2370
2371 r = 1;
2372out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002373 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002374 return r;
2375}
2376
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002377#ifdef AUDIT
2378
2379static const char *audit_msg;
2380
2381static gva_t canonicalize(gva_t gva)
2382{
2383#ifdef CONFIG_X86_64
2384 gva = (long long)(gva << 16) >> 16;
2385#endif
2386 return gva;
2387}
2388
2389static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2390 gva_t va, int level)
2391{
2392 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2393 int i;
2394 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2395
2396 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2397 u64 ent = pt[i];
2398
Avi Kivityc7addb92007-09-16 18:58:32 +02002399 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002400 continue;
2401
2402 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002403 if (level > 1) {
2404 if (ent == shadow_notrap_nonpresent_pte)
2405 printk(KERN_ERR "audit: (%s) nontrapping pte"
2406 " in nonleaf level: levels %d gva %lx"
2407 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002408 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002409
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002410 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002411 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002412 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002413 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002414
Avi Kivityc7addb92007-09-16 18:58:32 +02002415 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002416 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002417 printk(KERN_ERR "xx audit error: (%s) levels %d"
2418 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002419 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002420 va, gpa, hpa, ent,
2421 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002422 else if (ent == shadow_notrap_nonpresent_pte
2423 && !is_error_hpa(hpa))
2424 printk(KERN_ERR "audit: (%s) notrap shadow,"
2425 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002426 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002427
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002428 }
2429 }
2430}
2431
2432static void audit_mappings(struct kvm_vcpu *vcpu)
2433{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002434 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002435
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002436 if (vcpu->arch.mmu.root_level == 4)
2437 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002438 else
2439 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002441 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002442 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002443 i << 30,
2444 2);
2445}
2446
2447static int count_rmaps(struct kvm_vcpu *vcpu)
2448{
2449 int nmaps = 0;
2450 int i, j, k;
2451
2452 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2453 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2454 struct kvm_rmap_desc *d;
2455
2456 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002457 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002458
Izik Eidus290fc382007-09-27 14:11:22 +02002459 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002460 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002461 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002462 ++nmaps;
2463 continue;
2464 }
Izik Eidus290fc382007-09-27 14:11:22 +02002465 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002466 while (d) {
2467 for (k = 0; k < RMAP_EXT; ++k)
2468 if (d->shadow_ptes[k])
2469 ++nmaps;
2470 else
2471 break;
2472 d = d->more;
2473 }
2474 }
2475 }
2476 return nmaps;
2477}
2478
2479static int count_writable_mappings(struct kvm_vcpu *vcpu)
2480{
2481 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002482 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002483 int i;
2484
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002485 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002486 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002487
Avi Kivity4db35312007-11-21 15:28:32 +02002488 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002489 continue;
2490
2491 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2492 u64 ent = pt[i];
2493
2494 if (!(ent & PT_PRESENT_MASK))
2495 continue;
2496 if (!(ent & PT_WRITABLE_MASK))
2497 continue;
2498 ++nmaps;
2499 }
2500 }
2501 return nmaps;
2502}
2503
2504static void audit_rmap(struct kvm_vcpu *vcpu)
2505{
2506 int n_rmap = count_rmaps(vcpu);
2507 int n_actual = count_writable_mappings(vcpu);
2508
2509 if (n_rmap != n_actual)
2510 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002511 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002512}
2513
2514static void audit_write_protection(struct kvm_vcpu *vcpu)
2515{
Avi Kivity4db35312007-11-21 15:28:32 +02002516 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002517 struct kvm_memory_slot *slot;
2518 unsigned long *rmapp;
2519 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002520
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002521 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002522 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002523 continue;
2524
Avi Kivity4db35312007-11-21 15:28:32 +02002525 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2526 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002527 rmapp = &slot->rmap[gfn - slot->base_gfn];
2528 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002529 printk(KERN_ERR "%s: (%s) shadow page has writable"
2530 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002531 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002532 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002533 }
2534}
2535
2536static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2537{
2538 int olddbg = dbg;
2539
2540 dbg = 0;
2541 audit_msg = msg;
2542 audit_rmap(vcpu);
2543 audit_write_protection(vcpu);
2544 audit_mappings(vcpu);
2545 dbg = olddbg;
2546}
2547
2548#endif